blob: 38b03af36a6a86a45c385b71f7584f5f34ab09c2 [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 */
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifndef notyet
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#undef REAL_DMA
85#endif
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#ifdef BOARD_REQUIRES_NO_DELAY
88#define io_recovery_delay(x)
89#else
90#define io_recovery_delay(x) udelay(x)
91#endif
92
93/*
94 * Design
95 *
96 * This is a generic 5380 driver. To use it on a different platform,
97 * one simply writes appropriate system specific macros (ie, data
98 * transfer - some PC's will use the I/O bus, 68K's must use
99 * memory mapped) and drops this file in their 'C' wrapper.
100 *
101 * (Note from hch: unfortunately it was not enough for the different
102 * m68k folks and instead of improving this driver they copied it
103 * and hacked it up for their needs. As a consequence they lost
104 * most updates to this driver. Maybe someone will fix all these
105 * drivers to use a common core one day..)
106 *
107 * As far as command queueing, two queues are maintained for
108 * each 5380 in the system - commands that haven't been issued yet,
109 * and commands that are currently executing. This means that an
110 * unlimited number of commands may be queued, letting
111 * more commands propagate from the higher driver levels giving higher
112 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
113 * allowing multiple commands to propagate all the way to a SCSI-II device
114 * while a command is already executing.
115 *
116 *
117 * Issues specific to the NCR5380 :
118 *
119 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
120 * piece of hardware that requires you to sit in a loop polling for
121 * the REQ signal as long as you are connected. Some devices are
122 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100123 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * broken devices are the exception rather than the rule and I'd rather
125 * spend my time optimizing for the normal case.
126 *
127 * Architecture :
128 *
129 * At the heart of the design is a coroutine, NCR5380_main,
130 * which is started from a workqueue for each NCR5380 host in the
131 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
132 * removing the commands from the issue queue and calling
133 * NCR5380_select() if a nexus is not established.
134 *
135 * Once a nexus is established, the NCR5380_information_transfer()
136 * phase goes through the various phases as instructed by the target.
137 * if the target goes into MSG IN and sends a DISCONNECT message,
138 * the command structure is placed into the per instance disconnected
139 * queue, and NCR5380_main tries to find more work. If the target is
140 * idle for too long, the system will try to sleep.
141 *
142 * If a command has disconnected, eventually an interrupt will trigger,
143 * calling NCR5380_intr() which will in turn call NCR5380_reselect
144 * to reestablish a nexus. This will run main if necessary.
145 *
146 * On command termination, the done function will be called as
147 * appropriate.
148 *
149 * SCSI pointers are maintained in the SCp field of SCSI command
150 * structures, being initialized after the command is connected
151 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
152 * Note that in violation of the standard, an implicit SAVE POINTERS operation
153 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
154 */
155
156/*
157 * Using this file :
158 * This file a skeleton Linux SCSI driver for the NCR 5380 series
159 * of chips. To use it, you write an architecture specific functions
160 * and macros and include this file in your driver.
161 *
162 * These macros control options :
163 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
164 * defined.
165 *
166 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
167 * for commands that return with a CHECK CONDITION status.
168 *
169 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
170 * transceivers.
171 *
172 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
173 * override-configure an IRQ.
174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
176 *
177 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
178 *
179 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
180 * rely on phase mismatch and EOP interrupts to determine end
181 * of phase.
182 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Defaults for these will be provided although the user may want to adjust
184 * these to allocate CPU resources to the SCSI driver or "real" code.
185 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * These macros MUST be defined :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
188 * NCR5380_read(register) - read from the specified register
189 *
190 * NCR5380_write(register, value) - write to the specific register
191 *
192 * NCR5380_implementation_fields - additional fields needed for this
193 * specific implementation of the NCR5380
194 *
195 * Either real DMA *or* pseudo DMA may be implemented
196 * REAL functions :
197 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
198 * Note that the DMA setup functions should return the number of bytes
199 * that they were able to program the controller for.
200 *
201 * Also note that generic i386/PC versions of these macros are
202 * available as NCR5380_i386_dma_write_setup,
203 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
204 *
205 * NCR5380_dma_write_setup(instance, src, count) - initialize
206 * NCR5380_dma_read_setup(instance, dst, count) - initialize
207 * NCR5380_dma_residual(instance); - residual count
208 *
209 * PSEUDO functions :
210 * NCR5380_pwrite(instance, src, count)
211 * NCR5380_pread(instance, dst, count);
212 *
213 * The generic driver is initialized by calling NCR5380_init(instance),
214 * after setting the appropriate host specific fields and ID. If the
215 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
216 * possible) function may be used.
217 */
218
Finn Thain54d8fe42016-01-03 16:05:06 +1100219static int do_abort(struct Scsi_Host *);
220static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/*
223 * initialize_SCp - init the scsi pointer field
224 * @cmd: command block to set up
225 *
226 * Set up the internal fields in the SCSI command.
227 */
228
Finn Thain710ddd02014-11-12 16:12:02 +1100229static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 /*
232 * Initialize the Scsi Pointer field so that all of the commands in the
233 * various queues are valid.
234 */
235
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200236 if (scsi_bufflen(cmd)) {
237 cmd->SCp.buffer = scsi_sglist(cmd);
238 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200239 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 cmd->SCp.this_residual = cmd->SCp.buffer->length;
241 } else {
242 cmd->SCp.buffer = NULL;
243 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200244 cmd->SCp.ptr = NULL;
245 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100247
248 cmd->SCp.Status = 0;
249 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
Finn Thainb32ade12016-01-03 16:05:41 +1100253 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100254 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100255 * @reg1: 5380 register to poll
256 * @bit1: Bitmask to check
257 * @val1: Expected value
258 * @reg2: Second 5380 register to poll
259 * @bit2: Second bitmask to check
260 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100261 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
Finn Thain2f854b82016-01-03 16:05:22 +1100263 * Polls the chip in a reasonably efficient manner waiting for an
264 * event to occur. After a short quick poll we begin to yield the CPU
265 * (if possible). In irq contexts the time-out is arbitrarily limited.
266 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *
Finn Thainb32ade12016-01-03 16:05:41 +1100268 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Finn Thainb32ade12016-01-03 16:05:41 +1100271static int NCR5380_poll_politely2(struct Scsi_Host *instance,
272 int reg1, int bit1, int val1,
273 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100274{
275 struct NCR5380_hostdata *hostdata = shost_priv(instance);
276 unsigned long deadline = jiffies + wait;
277 unsigned long n;
278
279 /* Busy-wait for up to 10 ms */
280 n = min(10000U, jiffies_to_usecs(wait));
281 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100282 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100283 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100284 if ((NCR5380_read(reg1) & bit1) == val1)
285 return 0;
286 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100289 } while (n--);
290
291 if (irqs_disabled() || in_interrupt())
292 return -ETIMEDOUT;
293
294 /* Repeatedly sleep for 1 ms until deadline */
295 while (time_is_after_jiffies(deadline)) {
296 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100297 if ((NCR5380_read(reg1) & bit1) == val1)
298 return 0;
299 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Finn Thain2f854b82016-01-03 16:05:22 +1100302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -ETIMEDOUT;
304}
305
Finn Thainb32ade12016-01-03 16:05:41 +1100306static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
307 int reg, int bit, int val, int wait)
308{
309 return NCR5380_poll_politely2(instance, reg, bit, val,
310 reg, bit, val, wait);
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static struct {
314 unsigned char value;
315 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700316} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 {PHASE_DATAOUT, "DATAOUT"},
318 {PHASE_DATAIN, "DATAIN"},
319 {PHASE_CMDOUT, "CMDOUT"},
320 {PHASE_STATIN, "STATIN"},
321 {PHASE_MSGOUT, "MSGOUT"},
322 {PHASE_MSGIN, "MSGIN"},
323 {PHASE_UNKNOWN, "UNKNOWN"}
324};
325
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100326#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct {
328 unsigned char mask;
329 const char *name;
330} signals[] = {
331 {SR_DBP, "PARITY"},
332 {SR_RST, "RST"},
333 {SR_BSY, "BSY"},
334 {SR_REQ, "REQ"},
335 {SR_MSG, "MSG"},
336 {SR_CD, "CD"},
337 {SR_IO, "IO"},
338 {SR_SEL, "SEL"},
339 {0, NULL}
340},
341basrs[] = {
342 {BASR_ATN, "ATN"},
343 {BASR_ACK, "ACK"},
344 {0, NULL}
345},
346icrs[] = {
347 {ICR_ASSERT_RST, "ASSERT RST"},
348 {ICR_ASSERT_ACK, "ASSERT ACK"},
349 {ICR_ASSERT_BSY, "ASSERT BSY"},
350 {ICR_ASSERT_SEL, "ASSERT SEL"},
351 {ICR_ASSERT_ATN, "ASSERT ATN"},
352 {ICR_ASSERT_DATA, "ASSERT DATA"},
353 {0, NULL}
354},
355mrs[] = {
356 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
357 {MR_TARGET, "MODE TARGET"},
358 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
359 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
360 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
361 {MR_DMA_MODE, "MODE DMA"},
362 {MR_ARBITRATE, "MODE ARBITRATION"},
363 {0, NULL}
364};
365
366/**
367 * NCR5380_print - print scsi bus signals
368 * @instance: adapter state to dump
369 *
370 * Print the SCSI bus signals for debugging purposes
371 *
372 * Locks: caller holds hostdata lock (not essential)
373 */
374
375static void NCR5380_print(struct Scsi_Host *instance)
376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
380 status = NCR5380_read(STATUS_REG);
381 mr = NCR5380_read(MODE_REG);
382 icr = NCR5380_read(INITIATOR_COMMAND_REG);
383 basr = NCR5380_read(BUS_AND_STATUS_REG);
384
385 printk("STATUS_REG: %02x ", status);
386 for (i = 0; signals[i].mask; ++i)
387 if (status & signals[i].mask)
388 printk(",%s", signals[i].name);
389 printk("\nBASR: %02x ", basr);
390 for (i = 0; basrs[i].mask; ++i)
391 if (basr & basrs[i].mask)
392 printk(",%s", basrs[i].name);
393 printk("\nICR: %02x ", icr);
394 for (i = 0; icrs[i].mask; ++i)
395 if (icr & icrs[i].mask)
396 printk(",%s", icrs[i].name);
397 printk("\nMODE: %02x ", mr);
398 for (i = 0; mrs[i].mask; ++i)
399 if (mr & mrs[i].mask)
400 printk(",%s", mrs[i].name);
401 printk("\n");
402}
403
404
405/*
406 * NCR5380_print_phase - show SCSI phase
407 * @instance: adapter to dump
408 *
409 * Print the current SCSI phase for debugging purposes
410 *
411 * Locks: none
412 */
413
414static void NCR5380_print_phase(struct Scsi_Host *instance)
415{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned char status;
417 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 status = NCR5380_read(STATUS_REG);
420 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100421 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 else {
423 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100424 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426}
427#endif
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Finn Thaind5f7e652016-01-03 16:05:03 +1100430static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/**
433 * probe_intr - helper for IRQ autoprobe
434 * @irq: interrupt number
435 * @dev_id: unused
436 * @regs: unused
437 *
438 * Set a flag to indicate the IRQ in question was received. This is
439 * used by the IRQ probe code.
440 */
441
David Howells7d12e782006-10-05 14:55:46 +0100442static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 probe_irq = irq;
445 return IRQ_HANDLED;
446}
447
448/**
449 * NCR5380_probe_irq - find the IRQ of an NCR5380
450 * @instance: NCR5380 controller
451 * @possible: bitmask of ISA IRQ lines
452 *
453 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
454 * and then looking to see what interrupt actually turned up.
455 *
456 * Locks: none, irqs must be enabled on entry
457 */
458
Andrew Morton702809c2007-05-23 14:41:56 -0700459static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
460 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Finn Thaine8a60142016-01-03 16:05:54 +1100462 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned long timeout;
464 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Finn Thain22f5f102014-11-12 16:11:56 +1100466 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100467 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 trying_irqs |= mask;
469
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500470 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100471 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * A interrupt is triggered whenever BSY = false, SEL = true
475 * and a bit set in the SELECT_ENABLE_REG is asserted on the
476 * SCSI bus.
477 *
478 * Note that the bus is only driven when the phase control signals
479 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
480 * to zero.
481 */
482
483 NCR5380_write(TARGET_COMMAND_REG, 0);
484 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
485 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
486 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
487
Finn Thain22f5f102014-11-12 16:11:56 +1100488 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800489 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 NCR5380_write(SELECT_ENABLE_REG, 0);
492 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
493
Finn Thain22f5f102014-11-12 16:11:56 +1100494 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (trying_irqs & mask)
496 free_irq(i, NULL);
497
498 return probe_irq;
499}
500
501/**
Finn Thain8c325132014-11-12 16:11:58 +1100502 * NCR58380_info - report driver and host information
503 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Finn Thain8c325132014-11-12 16:11:58 +1100505 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
507 * Locks: none
508 */
509
Finn Thain8c325132014-11-12 16:11:58 +1100510static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Finn Thain8c325132014-11-12 16:11:58 +1100512 struct NCR5380_hostdata *hostdata = shost_priv(instance);
513
514 return hostdata->info;
515}
516
517static void prepare_info(struct Scsi_Host *instance)
518{
519 struct NCR5380_hostdata *hostdata = shost_priv(instance);
520
521 snprintf(hostdata->info, sizeof(hostdata->info),
522 "%s, io_port 0x%lx, n_io_port %d, "
523 "base 0x%lx, irq %d, "
524 "can_queue %d, cmd_per_lun %d, "
525 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100526 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100527 "options { %s} ",
528 instance->hostt->name, instance->io_port, instance->n_io_port,
529 instance->base, instance->irq,
530 instance->can_queue, instance->cmd_per_lun,
531 instance->sg_tablesize, instance->this_id,
Finn Thain55181be2016-01-03 16:05:42 +1100532 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100533 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100534 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100536 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100539 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif
541#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100542 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#endif
544#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100545 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#endif
547#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100548 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif
550#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100551 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#endif
Finn Thain8c325132014-11-12 16:11:58 +1100553 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Finn Thaina9c2dc42014-11-12 16:11:59 +1100556#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/******************************************/
558/*
559 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
560 *
561 * *buffer: I/O buffer
562 * **start: if inout == FALSE pointer into buffer where user read should start
563 * offset: current offset
564 * length: length of buffer
565 * hostno: Scsi_Host host_no
566 * inout: TRUE - user is writing; FALSE - user is reading
567 *
568 * Return the number of bytes read from or written
569 */
570
Al Virodd7ab712013-03-31 01:15:54 -0400571static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
572 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100574 struct NCR5380_hostdata *hostdata = shost_priv(instance);
575
576 hostdata->spin_max_r = 0;
577 hostdata->spin_max_w = 0;
578 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400579}
Al Virodd7ab712013-03-31 01:15:54 -0400580
581static int __maybe_unused NCR5380_show_info(struct seq_file *m,
582 struct Scsi_Host *instance)
583{
Finn Thaine8a60142016-01-03 16:05:54 +1100584 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100586 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100587 hostdata->spin_max_w, hostdata->spin_max_r);
Al Virodd7ab712013-03-31 01:15:54 -0400588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Finn Thaine5c3fdd2016-01-03 16:05:52 +1100590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592/**
593 * NCR5380_init - initialise an NCR5380
594 * @instance: adapter to configure
595 * @flags: control flags
596 *
597 * Initializes *instance and corresponding 5380 chip,
598 * with flags OR'd into the initial flags value.
599 *
600 * Notes : I assume that the host, hostno, and id bits have been
601 * set correctly. I don't care about the irq and other fields.
602 *
603 * Returns 0 for success
604 *
605 * Locks: interrupts must be enabled when we are called
606 */
607
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800608static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Finn Thaine8a60142016-01-03 16:05:54 +1100610 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100611 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100612 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if(in_interrupt())
615 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 hostdata->id_mask = 1 << instance->this_id;
618 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
619 if (i > hostdata->id_mask)
620 hostdata->id_higher_mask |= i;
621 for (i = 0; i < 8; ++i)
622 hostdata->busy[i] = 0;
623#ifdef REAL_DMA
624 hostdata->dmalen = 0;
625#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100626 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100628 hostdata->sensing = NULL;
629 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100630 INIT_LIST_HEAD(&hostdata->unissued);
631 INIT_LIST_HEAD(&hostdata->disconnected);
632
Finn Thain55181be2016-01-03 16:05:42 +1100633 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Finn Thain8d8601a2016-01-03 16:05:37 +1100635 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100636 hostdata->work_q = alloc_workqueue("ncr5380_%d",
637 WQ_UNBOUND | WQ_MEM_RECLAIM,
638 1, instance->host_no);
639 if (!hostdata->work_q)
640 return -ENOMEM;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Finn Thain8c325132014-11-12 16:11:58 +1100644 prepare_info(instance);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
647 NCR5380_write(MODE_REG, MR_BASE);
648 NCR5380_write(TARGET_COMMAND_REG, 0);
649 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100650
651 /* Calibrate register polling loop */
652 i = 0;
653 deadline = jiffies + 1;
654 do {
655 cpu_relax();
656 } while (time_is_after_jiffies(deadline));
657 deadline += msecs_to_jiffies(256);
658 do {
659 NCR5380_read(STATUS_REG);
660 ++i;
661 cpu_relax();
662 } while (time_is_after_jiffies(deadline));
663 hostdata->accesses_per_ms = i / 256;
664
Finn Thainb6488f92016-01-03 16:05:08 +1100665 return 0;
666}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Finn Thainb6488f92016-01-03 16:05:08 +1100668/**
669 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
670 * @instance: adapter to check
671 *
672 * If the system crashed, it may have crashed with a connected target and
673 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
674 * currently established nexus, which we know nothing about. Failing that
675 * do a bus reset.
676 *
677 * Note that a bus reset will cause the chip to assert IRQ.
678 *
679 * Returns 0 if successful, otherwise -ENXIO.
680 */
681
682static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
683{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100684 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100685 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
688 switch (pass) {
689 case 1:
690 case 3:
691 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100692 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
693 NCR5380_poll_politely(instance,
694 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100697 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 do_abort(instance);
699 break;
700 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100701 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100703 /* Wait after a reset; the SCSI standard calls for
704 * 250ms, we wait 500ms to be on the safe side.
705 * But some Toshiba CD-ROMs need ten times that.
706 */
707 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
708 msleep(2500);
709 else
710 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100713 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -ENXIO;
715 }
716 }
717 return 0;
718}
719
720/**
721 * NCR5380_exit - remove an NCR5380
722 * @instance: adapter to remove
723 */
724
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800725static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Finn Thaine8a60142016-01-03 16:05:54 +1100727 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Finn Thain8d8601a2016-01-03 16:05:37 +1100729 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100730 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/**
Finn Thain677e0192016-01-03 16:05:59 +1100734 * complete_cmd - finish processing a command and return it to the SCSI ML
735 * @instance: the host instance
736 * @cmd: command to complete
737 */
738
739static void complete_cmd(struct Scsi_Host *instance,
740 struct scsi_cmnd *cmd)
741{
742 struct NCR5380_hostdata *hostdata = shost_priv(instance);
743
744 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
745
Finn Thainf27db8e2016-01-03 16:06:00 +1100746 if (hostdata->sensing == cmd) {
747 /* Autosense processing ends here */
748 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
749 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
750 set_host_byte(cmd, DID_ERROR);
751 } else
752 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
753 hostdata->sensing = NULL;
754 }
755
Finn Thain677e0192016-01-03 16:05:59 +1100756 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
757
758 cmd->scsi_done(cmd);
759}
760
761/**
Finn Thain1bb40582016-01-03 16:05:29 +1100762 * NCR5380_queue_command - queue a command
763 * @instance: the relevant SCSI adapter
764 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
Finn Thain1bb40582016-01-03 16:05:29 +1100766 * cmd is added to the per-instance issue queue, with minor
767 * twiddling done to the host specific fields of cmd. If the
768 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
770
Finn Thain1bb40582016-01-03 16:05:29 +1100771static int NCR5380_queue_command(struct Scsi_Host *instance,
772 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Finn Thain1bb40582016-01-03 16:05:29 +1100774 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100775 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100776 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778#if (NDEBUG & NDEBUG_NO_WRITE)
779 switch (cmd->cmnd[0]) {
780 case WRITE_6:
781 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100782 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100784 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786 }
787#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 cmd->result = 0;
790
Finn Thain11d2f632016-01-03 16:05:51 +1100791 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * Insert the cmd into the issue queue. Note that REQUEST SENSE
795 * commands are added to the head of the queue since any command will
796 * clear the contingent allegiance condition that exists and the
797 * sense data is only guaranteed to be valid while the condition exists.
798 */
799
Finn Thain32b26a12016-01-03 16:05:58 +1100800 if (cmd->cmnd[0] == REQUEST_SENSE)
801 list_add(&ncmd->list, &hostdata->unissued);
802 else
803 list_add_tail(&ncmd->list, &hostdata->unissued);
804
Finn Thain11d2f632016-01-03 16:05:51 +1100805 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100806
Finn Thaindbb6b352016-01-03 16:05:53 +1100807 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
808 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100811 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0;
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100816 * dequeue_next_cmd - dequeue a command for processing
817 * @instance: the scsi host instance
818 *
819 * Priority is given to commands on the autosense queue. These commands
820 * need autosense because of a CHECK CONDITION result.
821 *
822 * Returns a command pointer if a command is found for a target that is
823 * not already busy. Otherwise returns NULL.
824 */
825
826static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
827{
828 struct NCR5380_hostdata *hostdata = shost_priv(instance);
829 struct NCR5380_cmd *ncmd;
830 struct scsi_cmnd *cmd;
831
832 if (list_empty(&hostdata->autosense)) {
833 list_for_each_entry(ncmd, &hostdata->unissued, list) {
834 cmd = NCR5380_to_scmd(ncmd);
835 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
836 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
837
838 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
839 list_del(&ncmd->list);
840 dsprintk(NDEBUG_QUEUES, instance,
841 "dequeue: removed %p from issue queue\n", cmd);
842 return cmd;
843 }
844 }
845 } else {
846 /* Autosense processing begins here */
847 ncmd = list_first_entry(&hostdata->autosense,
848 struct NCR5380_cmd, list);
849 list_del(&ncmd->list);
850 cmd = NCR5380_to_scmd(ncmd);
851 dsprintk(NDEBUG_QUEUES, instance,
852 "dequeue: removed %p from autosense queue\n", cmd);
853 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
854 hostdata->sensing = cmd;
855 return cmd;
856 }
857 return NULL;
858}
859
860static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
861{
862 struct NCR5380_hostdata *hostdata = shost_priv(instance);
863 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
864
865 if (hostdata->sensing) {
866 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
867 list_add(&ncmd->list, &hostdata->autosense);
868 hostdata->sensing = NULL;
869 } else
870 list_add(&ncmd->list, &hostdata->unissued);
871}
872
873/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * NCR5380_main - NCR state machines
875 *
876 * NCR5380_main is a coroutine that runs as long as more work can
877 * be done on the NCR5380 host adapters in a system. Both
878 * NCR5380_queue_command() and NCR5380_intr() will try to start it
879 * in case it is not running.
880 *
881 * Locks: called as its own thread with no locks held. Takes the
882 * host lock and called routines may take the isa dma lock.
883 */
884
David Howellsc4028952006-11-22 14:57:56 +0000885static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
David Howellsc4028952006-11-22 14:57:56 +0000887 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100888 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct Scsi_Host *instance = hostdata->host;
Finn Thainf27db8e2016-01-03 16:06:00 +1100890 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int done;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100895
Finn Thain0a4e3612016-01-03 16:06:07 +1100896 spin_lock_irq(&hostdata->lock);
Finn Thainf27db8e2016-01-03 16:06:00 +1100897 while (!hostdata->connected &&
898 (cmd = dequeue_next_cmd(instance))) {
899
900 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100903 * Attempt to establish an I_T_L nexus here.
904 * On success, instance->hostdata->connected is set.
905 * On failure, we must add the command back to the
906 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100908 /*
909 * REQUEST SENSE commands are issued without tagged
910 * queueing, even on SCSI-II devices because the
911 * contingent allegiance condition exists for the
912 * entire unit.
913 */
Finn Thain32b26a12016-01-03 16:05:58 +1100914
Finn Thain707d62b2016-01-03 16:06:02 +1100915 cmd = NCR5380_select(instance, cmd);
916 if (!cmd) {
917 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +1100918 } else {
919 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
920 "main: select failed, returning %p to queue\n", cmd);
921 requeue_cmd(instance, cmd);
922 }
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (hostdata->connected
925#ifdef REAL_DMA
926 && !hostdata->dmalen
927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 ) {
Finn Thainb7465452016-01-03 16:06:05 +1100929 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100932 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100933 spin_unlock_irq(&hostdata->lock);
934 if (!done)
935 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939#ifndef DONT_USE_INTR
940
941/**
Finn Thaincd400822016-01-03 16:05:40 +1100942 * NCR5380_intr - generic NCR5380 irq handler
943 * @irq: interrupt number
944 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
Finn Thaincd400822016-01-03 16:05:40 +1100946 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
947 * from the disconnected queue, and restarting NCR5380_main()
948 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 *
Finn Thaincd400822016-01-03 16:05:40 +1100950 * The chip can assert IRQ in any of six different conditions. The IRQ flag
951 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
952 * Three of these six conditions are latched in the Bus and Status Register:
953 * - End of DMA (cleared by ending DMA Mode)
954 * - Parity error (cleared by reading RPIR)
955 * - Loss of BSY (cleared by reading RPIR)
956 * Two conditions have flag bits that are not latched:
957 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
958 * - Bus reset (non-maskable)
959 * The remaining condition has no flag bit at all:
960 * - Selection/reselection
961 *
962 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
963 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
964 * claimed that "the design of the [DP8490] interrupt logic ensures
965 * interrupts will not be lost (they can be on the DP5380)."
966 * The L5380/53C80 datasheet from LOGIC Devices has more details.
967 *
968 * Checking for bus reset by reading RST is futile because of interrupt
969 * latency, but a bus reset will reset chip logic. Checking for parity error
970 * is unnecessary because that interrupt is never enabled. A Loss of BSY
971 * condition will clear DMA Mode. We can tell when this occurs because the
972 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
974
Finn Thaincd400822016-01-03 16:05:40 +1100975static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800977 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100978 struct NCR5380_hostdata *hostdata = shost_priv(instance);
979 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 unsigned char basr;
981 unsigned long flags;
982
Finn Thain11d2f632016-01-03 16:05:51 +1100983 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Finn Thaincd400822016-01-03 16:05:40 +1100985 basr = NCR5380_read(BUS_AND_STATUS_REG);
986 if (basr & BASR_IRQ) {
987 unsigned char mr = NCR5380_read(MODE_REG);
988 unsigned char sr = NCR5380_read(STATUS_REG);
989
Finn Thainb7465452016-01-03 16:06:05 +1100990 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
991 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +1100994 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
995 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
996 * We ack IRQ after clearing Mode Register. Workarounds
997 * for End of DMA errata need to happen in DMA Mode.
998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Finn Thainb7465452016-01-03 16:06:05 +11001000 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Finn Thaincd400822016-01-03 16:05:40 +11001002 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Finn Thaincd400822016-01-03 16:05:40 +11001004 if (!hostdata->connected)
1005 panic("scsi%d : DMA interrupt with no connected cmd\n",
1006 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Finn Thaincd400822016-01-03 16:05:40 +11001008 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
1009 hostdata->connected->SCp.this_residual -= transferred;
1010 hostdata->connected->SCp.ptr += transferred;
1011 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Finn Thaincd400822016-01-03 16:05:40 +11001013 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1014 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
1015
1016 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1017 NCR5380_write(MODE_REG, MR_BASE);
1018 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1019 } else
1020#endif /* REAL_DMA */
1021 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1022 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1023 /* Probably reselected */
1024 NCR5380_write(SELECT_ENABLE_REG, 0);
1025 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1026
Finn Thainb7465452016-01-03 16:06:05 +11001027 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +11001028
1029 if (!hostdata->connected) {
1030 NCR5380_reselect(instance);
1031 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Finn Thaincd400822016-01-03 16:05:40 +11001033 if (!hostdata->connected)
1034 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1035 } else {
1036 /* Probably Bus Reset */
1037 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1038
Finn Thainb7465452016-01-03 16:06:05 +11001039 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +11001040 }
1041 handled = 1;
1042 } else {
1043 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
1044 }
1045
Finn Thain11d2f632016-01-03 16:05:51 +11001046 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +11001047
1048 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051#endif
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
Finn Thain710ddd02014-11-12 16:12:02 +11001054 * Function : int NCR5380_select(struct Scsi_Host *instance,
1055 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
1057 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1058 * including ARBITRATION, SELECTION, and initial message out for
1059 * IDENTIFY and queue messages.
1060 *
1061 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001062 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
Finn Thain707d62b2016-01-03 16:06:02 +11001064 * Returns cmd if selection failed but should be retried,
1065 * NULL if selection failed and should not be retried, or
1066 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *
1068 * Side effects :
1069 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1070 * with registers as they should have been on entry - ie
1071 * SELECT_ENABLE will be set appropriately, the NCR5380
1072 * will cease to drive any SCSI bus signals.
1073 *
1074 * If successful : I_T_L or I_T_L_Q nexus will be established,
1075 * instance->connected will be set to cmd.
1076 * SELECT interrupt will be disabled.
1077 *
1078 * If failed (no target) : cmd->scsi_done() will be called, and the
1079 * cmd->result host byte set to DID_BAD_TARGET.
1080 *
1081 * Locks: caller holds hostdata lock in IRQ mode
1082 */
1083
Finn Thain707d62b2016-01-03 16:06:02 +11001084static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1085 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Finn Thaine8a60142016-01-03 16:05:54 +11001087 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 unsigned char tmp[3], phase;
1089 unsigned char *data;
1090 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +11001094 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1095 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Finn Thain707d62b2016-01-03 16:06:02 +11001097 /*
1098 * Arbitration and selection phases are slow and involve dropping the
1099 * lock, so we have to watch out for EH. An exception handler may
1100 * change 'selecting' to NULL. This function will then return NULL
1101 * so that the caller will forget about 'cmd'. (During information
1102 * transfer phases, EH may change 'connected' to NULL.)
1103 */
1104 hostdata->selecting = cmd;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1108 * data bus during SELECTION.
1109 */
1110
1111 NCR5380_write(TARGET_COMMAND_REG, 0);
1112
1113 /*
1114 * Start arbitration.
1115 */
1116
1117 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1118 NCR5380_write(MODE_REG, MR_ARBITRATE);
1119
Finn Thain55500d92016-01-03 16:05:35 +11001120 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1121 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
1123
Finn Thain11d2f632016-01-03 16:05:51 +11001124 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001125 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1126 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1127 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001128 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001129 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1130 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001131 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001132 }
1133 if (err < 0) {
1134 NCR5380_write(MODE_REG, MR_BASE);
1135 shost_printk(KERN_ERR, instance,
1136 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001137 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001138 }
Finn Thain11d2f632016-01-03 16:05:51 +11001139 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001140
1141 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 udelay(3);
1143
1144 /* Check for lost arbitration */
1145 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)) {
1146 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001147 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001148 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
Finn Thaincf13b082016-01-03 16:05:18 +11001151
1152 /* After/during arbitration, BSY should be asserted.
1153 * IBM DPES-31080 Version S31Q works now
1154 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1155 */
1156 NCR5380_write(INITIATOR_COMMAND_REG,
1157 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * Again, bus clear + bus settle time is 1.2us, however, this is
1161 * a minimum so we'll udelay ceil(1.2)
1162 */
1163
Finn Thain9c3f0e22016-01-03 16:05:11 +11001164 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1165 udelay(15);
1166 else
1167 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Finn Thain11d2f632016-01-03 16:05:51 +11001169 spin_lock_irq(&hostdata->lock);
1170
Finn Thain72064a72016-01-03 16:05:44 +11001171 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1172 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001173 goto out;
1174
1175 if (!hostdata->selecting) {
1176 NCR5380_write(MODE_REG, MR_BASE);
1177 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1178 goto out;
1179 }
Finn Thain72064a72016-01-03 16:05:44 +11001180
Finn Thainb7465452016-01-03 16:06:05 +11001181 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /*
1184 * Now that we have won arbitration, start Selection process, asserting
1185 * the host and target ID's on the SCSI bus.
1186 */
1187
Jeff Garzik422c0d62005-10-24 18:05:09 -04001188 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * Raise ATN while SEL is true before BSY goes false from arbitration,
1192 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1193 * phase immediately after selection.
1194 */
1195
1196 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1197 NCR5380_write(MODE_REG, MR_BASE);
1198
1199 /*
1200 * Reselect interrupts must be turned off prior to the dropping of BSY,
1201 * otherwise we will trigger an interrupt.
1202 */
1203 NCR5380_write(SELECT_ENABLE_REG, 0);
1204
Finn Thain11d2f632016-01-03 16:05:51 +11001205 spin_unlock_irq(&hostdata->lock);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * The initiator shall then wait at least two deskew delays and release
1209 * the BSY signal.
1210 */
1211 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1212
1213 /* Reset BSY */
1214 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1215
1216 /*
1217 * Something weird happens when we cease to drive BSY - looks
1218 * like the board/chip is letting us do another read before the
1219 * appropriate propagation delay has expired, and we're confusing
1220 * a BSY signal from ourselves as the target's response to SELECTION.
1221 *
1222 * A small delay (the 'C++' frontend breaks the pipeline with an
1223 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1224 * tighter 'C' code breaks and requires this) solves the problem -
1225 * the 1 us delay is arbitrary, and only used because this delay will
1226 * be the same on other platforms and since it works here, it should
1227 * work there.
1228 *
1229 * wingel suggests that this could be due to failing to wait
1230 * one deskew delay.
1231 */
1232
1233 udelay(1);
1234
Finn Thainb7465452016-01-03 16:06:05 +11001235 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /*
1238 * The SCSI specification calls for a 250 ms timeout for the actual
1239 * selection.
1240 */
1241
Finn Thainae753a32016-01-03 16:05:23 +11001242 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1243 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001246 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1248 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001249 if (!hostdata->connected)
1250 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001251 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Finn Thainae753a32016-01-03 16:05:23 +11001254
1255 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001256 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001257 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001258 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001259 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1260 if (hostdata->selecting) {
1261 cmd->result = DID_BAD_TARGET << 16;
1262 complete_cmd(instance, cmd);
1263 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1264 cmd = NULL;
1265 }
1266 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001267 }
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * No less than two deskew delays after the initiator detects the
1271 * BSY signal is true, it shall release the SEL signal and may
1272 * change the DATA BUS. -wingel
1273 */
1274
1275 udelay(1);
1276
1277 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Since we followed the SCSI spec, and raised ATN while SEL
1281 * was true but before BSY was false during selection, the information
1282 * transfer phase should be a MESSAGE OUT phase so that we can send the
1283 * IDENTIFY message.
1284 *
1285 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1286 * message (2 bytes) with a tag ID that we increment with every command
1287 * until it wraps back to 0.
1288 *
1289 * XXX - it turns out that there are some broken SCSI-II devices,
1290 * which claim to support tagged queuing but fail when more than
1291 * some number of commands are issued at once.
1292 */
1293
1294 /* Wait for start of REQ/ACK handshake */
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001297 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001298 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001299 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1300 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001302 goto out;
1303 }
1304 if (!hostdata->selecting) {
1305 do_abort(instance);
1306 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Finn Thainb7465452016-01-03 16:06:05 +11001309 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1310 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001311 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 len = 1;
1314 cmd->tag = 0;
1315
1316 /* Send message(s) */
1317 data = tmp;
1318 phase = PHASE_MSGOUT;
1319 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001320 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001324 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Boaz Harrosh28424d32007-09-10 22:37:45 +03001326 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Finn Thain707d62b2016-01-03 16:06:02 +11001328 cmd = NULL;
1329
1330out:
1331 if (!hostdata->selecting)
1332 return NULL;
1333 hostdata->selecting = NULL;
1334 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337/*
1338 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1339 * unsigned char *phase, int *count, unsigned char **data)
1340 *
1341 * Purpose : transfers data in given phase using polled I/O
1342 *
1343 * Inputs : instance - instance of driver, *phase - pointer to
1344 * what phase is expected, *count - pointer to number of
1345 * bytes to transfer, **data - pointer to data pointer.
1346 *
1347 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001348 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * is in same phase.
1350 *
1351 * Also, *phase, *count, *data are modified in place.
1352 *
1353 * XXX Note : handling for bus free may be useful.
1354 */
1355
1356/*
1357 * Note : this code is not as quick as it could be, however it
1358 * IS 100% reliable, and for the actual data transfer where speed
1359 * counts, we will always do a pseudo DMA or DMA transfer.
1360 */
1361
1362static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 unsigned char p = *phase, tmp;
1364 int c = *count;
1365 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
1368 * The NCR5380 chip will only drive the SCSI bus when the
1369 * phase specified in the appropriate bits of the TARGET COMMAND
1370 * REGISTER match the STATUS REGISTER
1371 */
1372
1373 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 do {
1376 /*
1377 * Wait for assertion of REQ, after which the phase bits will be
1378 * valid
1379 */
1380
Finn Thain686f3992016-01-03 16:05:26 +11001381 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Finn Thainb7465452016-01-03 16:06:05 +11001384 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001387 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001388 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1389 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 }
1392 /* Do actual transfer from SCSI bus to / from memory */
1393 if (!(p & SR_IO))
1394 NCR5380_write(OUTPUT_DATA_REG, *d);
1395 else
1396 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1397
1398 ++d;
1399
1400 /*
1401 * The SCSI standard suggests that in MSGOUT phase, the initiator
1402 * should drop ATN on the last byte of the message phase
1403 * after REQ has been asserted for the handshake but before
1404 * the initiator raises ACK.
1405 */
1406
1407 if (!(p & SR_IO)) {
1408 if (!((p & SR_MSG) && c > 1)) {
1409 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1410 NCR5380_dprint(NDEBUG_PIO, instance);
1411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1412 } else {
1413 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1414 NCR5380_dprint(NDEBUG_PIO, instance);
1415 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1416 }
1417 } else {
1418 NCR5380_dprint(NDEBUG_PIO, instance);
1419 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1420 }
1421
Finn Thaina2edc4a2016-01-03 16:05:27 +11001422 if (NCR5380_poll_politely(instance,
1423 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1424 break;
1425
Finn Thainb7465452016-01-03 16:06:05 +11001426 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428/*
1429 * We have several special cases to consider during REQ/ACK handshaking :
1430 * 1. We were in MSGOUT phase, and we are on the last byte of the
1431 * message. ATN must be dropped as ACK is dropped.
1432 *
1433 * 2. We are in a MSGIN phase, and we are on the last byte of the
1434 * message. We must exit with ACK asserted, so that the calling
1435 * code may raise ATN before dropping ACK to reject the message.
1436 *
1437 * 3. ACK and ATN are clear and the target may proceed as normal.
1438 */
1439 if (!(p == PHASE_MSGIN && c == 1)) {
1440 if (p == PHASE_MSGOUT && c > 1)
1441 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1442 else
1443 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1444 }
1445 } while (--c);
1446
Finn Thainb7465452016-01-03 16:06:05 +11001447 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 *count = c;
1450 *data = d;
1451 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001452 /* The phase read from the bus is valid if either REQ is (already)
1453 * asserted or if ACK hasn't been released yet. The latter applies if
1454 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1455 */
1456 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *phase = tmp & PHASE_MASK;
1458 else
1459 *phase = PHASE_UNKNOWN;
1460
1461 if (!c || (*phase == p))
1462 return 0;
1463 else
1464 return -1;
1465}
1466
1467/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001468 * do_reset - issue a reset command
1469 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001471 * Issue a reset sequence to the NCR5380 and try and get the bus
1472 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001474 * This clears the reset interrupt flag because there may be no handler for
1475 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1476 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 */
1478
Finn Thain54d8fe42016-01-03 16:05:06 +11001479static void do_reset(struct Scsi_Host *instance)
1480{
Finn Thain636b1ec2016-01-03 16:05:10 +11001481 unsigned long flags;
1482
1483 local_irq_save(flags);
1484 NCR5380_write(TARGET_COMMAND_REG,
1485 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001487 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001489 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1490 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Finn Thain80d3eb62016-01-03 16:05:34 +11001493/**
1494 * do_abort - abort the currently established nexus by going to
1495 * MESSAGE OUT phase and sending an ABORT message.
1496 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001498 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
1500
Finn Thain54d8fe42016-01-03 16:05:06 +11001501static int do_abort(struct Scsi_Host *instance)
1502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned char *msgptr, phase, tmp;
1504 int len;
1505 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* Request message out phase */
1508 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1509
1510 /*
1511 * Wait for the target to indicate a valid phase by asserting
1512 * REQ. Once this happens, we'll have either a MSGOUT phase
1513 * and can immediately send the ABORT message, or we'll have some
1514 * other phase and will have to source/sink data.
1515 *
1516 * We really don't care what value was on the bus or what value
1517 * the target sees, so we just handshake.
1518 */
1519
Finn Thain80d3eb62016-01-03 16:05:34 +11001520 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001521 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001522 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Finn Thainf35d3472016-01-03 16:05:33 +11001524 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1527
Finn Thainf35d3472016-01-03 16:05:33 +11001528 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001530 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001531 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001532 goto timeout;
1533 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535 tmp = ABORT;
1536 msgptr = &tmp;
1537 len = 1;
1538 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001539 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /*
1542 * If we got here, and the command completed successfully,
1543 * we're about to go into bus free state.
1544 */
1545
1546 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001547
1548timeout:
1549 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1550 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1554/*
1555 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1556 * unsigned char *phase, int *count, unsigned char **data)
1557 *
1558 * Purpose : transfers data in given phase using either real
1559 * or pseudo DMA.
1560 *
1561 * Inputs : instance - instance of driver, *phase - pointer to
1562 * what phase is expected, *count - pointer to number of
1563 * bytes to transfer, **data - pointer to data pointer.
1564 *
1565 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001566 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * is in same phase.
1568 *
1569 * Also, *phase, *count, *data are modified in place.
1570 *
1571 * Locks: io_request lock held by caller
1572 */
1573
1574
1575static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 register int c = *count;
1577 register unsigned char p = *phase;
1578 register unsigned char *d = *data;
1579 unsigned char tmp;
1580 int foo;
1581#if defined(REAL_DMA_POLL)
1582 int cnt, toPIO;
1583 unsigned char saved_data = 0, overrun = 0, residue;
1584#endif
1585
Finn Thaine8a60142016-01-03 16:05:54 +11001586 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1589 *phase = tmp;
1590 return -1;
1591 }
1592#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001594 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
1595 c -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
Finn Thainb7465452016-01-03 16:06:05 +11001598
1599 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1600 (p & SR_IO) ? "receive" : "send", c, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601#endif
1602
1603 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1604
1605#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001606 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1607 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001609 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610#else
1611 /*
1612 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1613 * were not disabled for the duration of a single DMA transfer, from
1614 * before the setting of DMA mode to after transfer of the last byte.
1615 */
1616
Finn Thain55181be2016-01-03 16:05:42 +11001617 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001618 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1619 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 else
Finn Thaincd400822016-01-03 16:05:40 +11001621 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622#endif /* def REAL_DMA */
1623
Finn Thain52a6a1c2014-03-18 11:42:18 +11001624 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /*
1627 * On the PAS16 at least I/O recovery delays are not needed here.
1628 * Everyone else seems to want them.
1629 */
1630
1631 if (p & SR_IO) {
1632 io_recovery_delay(1);
1633 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1634 } else {
1635 io_recovery_delay(1);
1636 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1637 io_recovery_delay(1);
1638 NCR5380_write(START_DMA_SEND_REG, 0);
1639 io_recovery_delay(1);
1640 }
1641
1642#if defined(REAL_DMA_POLL)
1643 do {
1644 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1645 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1646
1647/*
1648 At this point, either we've completed DMA, or we have a phase mismatch,
1649 or we've unexpectedly lost BUSY (which is a real error).
1650
1651 For write DMAs, we want to wait until the last byte has been
1652 transferred out over the bus before we turn off DMA mode. Alas, there
1653 seems to be no terribly good way of doing this on a 5380 under all
1654 conditions. For non-scatter-gather operations, we can wait until REQ
1655 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1656 are nastier, since the device will be expecting more data than we
1657 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1658 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1659 why this signal was added to the newer chips) but on the older 538[01]
1660 this signal does not exist. The workaround for this lack is a watchdog;
1661 we bail out of the wait-loop after a modest amount of wait-time if
1662 the usual exit conditions are not met. Not a terribly clean or
1663 correct solution :-%
1664
1665 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1666 If the chip is in DMA mode for an READ, it will respond to a target's
1667 REQ by latching the SCSI data into the INPUT DATA register and asserting
1668 ACK, even if it has _already_ been notified by the DMA controller that
1669 the current DMA transfer has completed! If the NCR5380 is then taken
1670 out of DMA mode, this already-acknowledged byte is lost.
1671
1672 This is not a problem for "one DMA transfer per command" reads, because
1673 the situation will never arise... either all of the data is DMA'ed
1674 properly, or the target switches to MESSAGE IN phase to signal a
1675 disconnection (either operation bringing the DMA to a clean halt).
1676 However, in order to handle scatter-reads, we must work around the
1677 problem. The chosen fix is to DMA N-2 bytes, then check for the
1678 condition before taking the NCR5380 out of DMA mode. One or two extra
1679 bytes are transferred via PIO as necessary to fill out the original
1680 request.
1681 */
1682
1683 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001684 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
1685 udelay(10);
1686 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1687 (BASR_PHASE_MATCH | BASR_ACK)) {
1688 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1689 overrun = 1;
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 } else {
1693 int limit = 100;
1694 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1695 if (!(tmp & BASR_PHASE_MATCH))
1696 break;
1697 if (--limit < 0)
1698 break;
1699 }
1700 }
1701
Finn Thainb7465452016-01-03 16:06:05 +11001702 dsprintk(NDEBUG_DMA, "polled DMA transfer complete, basr 0x%02x, sr 0x%02x\n",
1703 tmp, NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 NCR5380_write(MODE_REG, MR_BASE);
1706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1707
1708 residue = NCR5380_dma_residual(instance);
1709 c -= residue;
1710 *count -= c;
1711 *data += c;
1712 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1713
Finn Thain9db60242016-01-03 16:05:43 +11001714 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
1715 *phase == p && (p & SR_IO) && residue == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001717 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 **data = saved_data;
1719 *data += 1;
1720 *count -= 1;
1721 cnt = toPIO = 1;
1722 } else {
1723 printk("No overrun??\n");
1724 cnt = toPIO = 2;
1725 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001726 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 NCR5380_transfer_pio(instance, phase, &cnt, data);
1728 *count -= toPIO - cnt;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Finn Thain52a6a1c2014-03-18 11:42:18 +11001731 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 -07001732 return 0;
1733
1734#elif defined(REAL_DMA)
1735 return 0;
1736#else /* defined(REAL_DMA_POLL) */
1737 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001738 foo = NCR5380_pread(instance, d,
1739 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1740 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
1742 * We can't disable DMA mode after successfully transferring
1743 * what we plan to be the last byte, since that would open up
1744 * a race condition where if the target asserted REQ before
1745 * we got the DMA mode reset, the NCR5380 would have latched
1746 * an additional byte into the INPUT DATA register and we'd
1747 * have dropped it.
1748 *
1749 * The workaround was to transfer one fewer bytes than we
1750 * intended to with the pseudo-DMA read function, wait for
1751 * the chip to latch the last byte, read it, and then disable
1752 * pseudo-DMA mode.
1753 *
1754 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1755 * REQ is deasserted when ACK is asserted, and not reasserted
1756 * until ACK goes false. Since the NCR5380 won't lower ACK
1757 * until DACK is asserted, which won't happen unless we twiddle
1758 * the DMA port or we take the NCR5380 out of DMA mode, we
1759 * can guarantee that we won't handshake another extra
1760 * byte.
1761 */
1762
Finn Thain55181be2016-01-03 16:05:42 +11001763 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1764 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1765 foo = -1;
1766 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
Finn Thain55181be2016-01-03 16:05:42 +11001768 if (NCR5380_poll_politely(instance, STATUS_REG,
1769 SR_REQ, 0, HZ) < 0) {
1770 foo = -1;
1771 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1772 }
1773 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001777 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /*
1779 * Wait for the last byte to be sent. If REQ is being asserted for
1780 * the byte we're interested, we'll ACK it and it will go false.
1781 */
Finn Thain55181be2016-01-03 16:05:42 +11001782 if (NCR5380_poll_politely2(instance,
1783 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1784 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1785 foo = -1;
1786 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 NCR5380_write(MODE_REG, MR_BASE);
1791 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001792 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 *data = d + c;
1794 *count = 0;
1795 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return foo;
1797#endif /* def REAL_DMA */
1798}
1799#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1800
1801/*
1802 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1803 *
1804 * Purpose : run through the various SCSI phases and do as the target
1805 * directs us to. Operates on the currently connected command,
1806 * instance->connected.
1807 *
1808 * Inputs : instance, instance for which we are doing commands
1809 *
1810 * Side effects : SCSI things happen, the disconnected queue will be
1811 * modified if a command disconnects, *instance->connected will
1812 * change.
1813 *
1814 * XXX Note : we need to watch for bus free or a reset condition here
1815 * to recover from an unexpected bus free condition.
1816 *
1817 * Locks: io_request_lock held by caller in IRQ mode
1818 */
1819
1820static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11001821 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 unsigned char msgout = NOP;
1823 int sink = 0;
1824 int len;
1825#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1826 int transfersize;
1827#endif
1828 unsigned char *data;
1829 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001830 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Finn Thain11d2f632016-01-03 16:05:51 +11001832 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001833 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 tmp = NCR5380_read(STATUS_REG);
1836 /* We only have a valid SCSI phase when REQ is asserted */
1837 if (tmp & SR_REQ) {
1838 phase = (tmp & PHASE_MASK);
1839 if (phase != old_phase) {
1840 old_phase = phase;
1841 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1842 }
1843 if (sink && (phase != PHASE_MSGOUT)) {
1844 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1845
1846 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1847 while (NCR5380_read(STATUS_REG) & SR_REQ);
1848 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1849 sink = 0;
1850 continue;
1851 }
1852 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 case PHASE_DATAOUT:
1854#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001855 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 sink = 1;
1857 do_abort(instance);
1858 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001859 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return;
1861#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001862 case PHASE_DATAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /*
1864 * If there is no room left in the current buffer in the
1865 * scatter-gather list, move onto the next one.
1866 */
1867
1868 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1869 ++cmd->SCp.buffer;
1870 --cmd->SCp.buffers_residual;
1871 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001872 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001873 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1874 cmd->SCp.this_residual,
1875 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877 /*
1878 * The preferred transfer method is going to be
1879 * PSEUDO-DMA for systems that are strictly PIO,
1880 * since we can let the hardware do the handshaking.
1881 *
1882 * For this to work, we need to know the transfersize
1883 * ahead of time, since the pseudo-DMA code will sit
1884 * in an unconditional loop.
1885 */
1886
1887#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001888 transfersize = 0;
1889 if (!cmd->device->borken &&
1890 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1891 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Finn Thainff3d4572016-01-03 16:05:25 +11001893 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 len = transfersize;
1895 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1896 /*
1897 * If the watchdog timer fires, all future accesses to this
1898 * device will use the polled-IO.
1899 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001900 scmd_printk(KERN_INFO, cmd,
1901 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 sink = 1;
1904 do_abort(instance);
1905 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001906 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* XXX - need to source or sink data here, as appropriate */
1908 } else
1909 cmd->SCp.this_residual -= transfersize - len;
1910 } else
1911#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
Finn Thain11d2f632016-01-03 16:05:51 +11001912 {
1913 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1915 &cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11001916 spin_lock_irq(&hostdata->lock);
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 break;
1919 case PHASE_MSGIN:
1920 len = 1;
1921 data = &tmp;
1922 NCR5380_transfer_pio(instance, &phase, &len, &data);
1923 cmd->SCp.Message = tmp;
1924
1925 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 case ABORT:
1927 case COMMAND_COMPLETE:
1928 /* Accept message by clearing ACK */
1929 sink = 1;
1930 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001931 dsprintk(NDEBUG_QUEUES, instance,
1932 "COMMAND COMPLETE %p target %d lun %llu\n",
1933 cmd, scmd_id(cmd), cmd->device->lun);
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Finn Thainf27db8e2016-01-03 16:06:00 +11001937 cmd->result &= ~0xffff;
1938 cmd->result |= cmd->SCp.Status;
1939 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Finn Thainf27db8e2016-01-03 16:06:00 +11001941 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001942 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001943 else {
1944 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1945 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1946 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1947 cmd);
1948 list_add_tail(&ncmd->list,
1949 &hostdata->autosense);
1950 } else
1951 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /*
1955 * Restore phase bits to 0 so an interrupted selection,
1956 * arbitration can resume.
1957 */
1958 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001959
1960 /* Enable reselect interrupts */
1961 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return;
1963 case MESSAGE_REJECT:
1964 /* Accept message by clearing ACK */
1965 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1966 switch (hostdata->last_message) {
1967 case HEAD_OF_QUEUE_TAG:
1968 case ORDERED_QUEUE_TAG:
1969 case SIMPLE_QUEUE_TAG:
1970 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001971 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 break;
1973 default:
1974 break;
1975 }
Finn Thain340b9612016-01-03 16:05:31 +11001976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 case DISCONNECT:{
1978 /* Accept message by clearing ACK */
1979 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11001981 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001982 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1983 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1984 cmd, scmd_id(cmd), cmd->device->lun);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * Restore phase bits to 0 so an interrupted selection,
1988 * arbitration can resume.
1989 */
1990 NCR5380_write(TARGET_COMMAND_REG, 0);
1991
1992 /* Enable reselect interrupts */
1993 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return;
1995 }
1996 /*
1997 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1998 * operation, in violation of the SCSI spec so we can safely
1999 * ignore SAVE/RESTORE pointers calls.
2000 *
2001 * Unfortunately, some disks violate the SCSI spec and
2002 * don't issue the required SAVE_POINTERS message before
2003 * disconnecting, and we have to break spec to remain
2004 * compatible.
2005 */
2006 case SAVE_POINTERS:
2007 case RESTORE_POINTERS:
2008 /* Accept message by clearing ACK */
2009 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2010 break;
2011 case EXTENDED_MESSAGE:
2012/*
2013 * Extended messages are sent in the following format :
2014 * Byte
2015 * 0 EXTENDED_MESSAGE == 1
2016 * 1 length (includes one byte for code, doesn't
2017 * include first two bytes)
2018 * 2 code
2019 * 3..length+1 arguments
2020 *
2021 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002022 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 */
2024 extended_msg[0] = EXTENDED_MESSAGE;
2025 /* Accept first byte by clearing ACK */
2026 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11002027
2028 spin_unlock_irq(&hostdata->lock);
2029
Finn Thainb7465452016-01-03 16:06:05 +11002030 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 len = 2;
2033 data = extended_msg + 1;
2034 phase = PHASE_MSGIN;
2035 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002036 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
2037 (int)extended_msg[1],
2038 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Finn Thaine0783ed2016-01-03 16:05:45 +11002040 if (!len && extended_msg[1] > 0 &&
2041 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* Accept third byte by clearing ACK */
2043 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2044 len = extended_msg[1] - 1;
2045 data = extended_msg + 3;
2046 phase = PHASE_MSGIN;
2047
2048 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002049 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
2050 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) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002060 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 tmp = 0;
2062 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002063 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2064 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 tmp = 0;
2066 }
Finn Thain11d2f632016-01-03 16:05:51 +11002067
2068 spin_lock_irq(&hostdata->lock);
2069 if (!hostdata->connected)
2070 return;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /* Fall through to reject message */
2073
2074 /*
2075 * If we get something weird that we aren't expecting,
2076 * reject it.
2077 */
2078 default:
2079 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002080 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002081 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 printk("\n");
2083 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002084 scmd_printk(KERN_INFO, cmd,
2085 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002087 scmd_printk(KERN_INFO, cmd,
2088 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 msgout = MESSAGE_REJECT;
2091 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2092 break;
2093 } /* switch (tmp) */
2094 break;
2095 case PHASE_MSGOUT:
2096 len = 1;
2097 data = &msgout;
2098 hostdata->last_message = msgout;
2099 NCR5380_transfer_pio(instance, &phase, &len, &data);
2100 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 hostdata->connected = NULL;
2102 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002103 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2105 return;
2106 }
2107 msgout = NOP;
2108 break;
2109 case PHASE_CMDOUT:
2110 len = cmd->cmd_len;
2111 data = cmd->cmnd;
2112 /*
2113 * XXX for performance reasons, on machines with a
2114 * PSEUDO-DMA architecture we should probably
2115 * use the dma transfer function.
2116 */
2117 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 break;
2119 case PHASE_STATIN:
2120 len = 1;
2121 data = &tmp;
2122 NCR5380_transfer_pio(instance, &phase, &len, &data);
2123 cmd->SCp.Status = tmp;
2124 break;
2125 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002126 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11002127 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002129 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002130 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002131 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002132 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Finn Thain11d2f632016-01-03 16:05:51 +11002134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/*
2138 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2139 *
2140 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002141 * 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 -07002142 * nexus has been reestablished,
2143 *
2144 * Inputs : instance - this instance of the NCR5380.
2145 *
2146 * Locks: io_request_lock held by caller if IRQ driven
2147 */
2148
2149static void NCR5380_reselect(struct Scsi_Host *instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11002150 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 unsigned char target_mask;
2152 unsigned char lun, phase;
2153 int len;
2154 unsigned char msg[3];
2155 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11002156 struct NCR5380_cmd *ncmd;
2157 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 /*
2160 * Disable arbitration, etc. since the host adapter obviously
2161 * lost, and tell an interrupted NCR5380_select() to restart.
2162 */
2163
2164 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11002167
2168 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /*
2171 * At this point, we have detected that our SCSI ID is on the bus,
2172 * SEL is true and BSY was false for at least one bus settle delay
2173 * (400 ns).
2174 *
2175 * We must assert BSY ourselves, until the target drops the SEL
2176 * signal.
2177 */
2178
2179 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002180 if (NCR5380_poll_politely(instance,
2181 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2182 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2183 return;
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2186
2187 /*
2188 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 */
2190
Finn Thain1cc160e2016-01-03 16:05:32 +11002191 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002192 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2193 do_abort(instance);
2194 return;
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 len = 1;
2198 data = msg;
2199 phase = PHASE_MSGIN;
2200 NCR5380_transfer_pio(instance, &phase, &len, &data);
2201
Finn Thain72064a72016-01-03 16:05:44 +11002202 if (len) {
2203 do_abort(instance);
2204 return;
2205 }
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002208 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002209 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002210 printk("\n");
2211 do_abort(instance);
2212 return;
2213 }
2214 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Finn Thain72064a72016-01-03 16:05:44 +11002216 /*
2217 * We need to add code for SCSI-II to track which devices have
2218 * I_T_L_Q nexuses established, and which have simple I_T_L
2219 * nexuses so we can chose to do additional data transfer.
2220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Finn Thain72064a72016-01-03 16:05:44 +11002222 /*
2223 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2224 * just reestablished, and remove it from the disconnected queue.
2225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Finn Thain32b26a12016-01-03 16:05:58 +11002227 tmp = NULL;
2228 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2229 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2230
2231 if (target_mask == (1 << scmd_id(cmd)) &&
2232 lun == (u8)cmd->device->lun) {
2233 list_del(&ncmd->list);
2234 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002238
2239 if (tmp) {
2240 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2241 "reselect: removed %p from disconnected queue\n", tmp);
2242 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002243 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2244 target_mask, lun);
2245 /*
2246 * Since we have an established nexus that we can't do anything with,
2247 * we must abort it.
2248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002250 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Finn Thain72064a72016-01-03 16:05:44 +11002252
2253 /* Accept message by clearing ACK */
2254 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2255
2256 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002257 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2258 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
2261/*
2262 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2263 *
2264 * Purpose : called by interrupt handler when DMA finishes or a phase
2265 * mismatch occurs (which would finish the DMA transfer).
2266 *
2267 * Inputs : instance - this instance of the NCR5380.
2268 *
Finn Thain710ddd02014-11-12 16:12:02 +11002269 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * nexus has been reestablished, on failure NULL is returned.
2271 */
2272
2273#ifdef REAL_DMA
2274static void NCR5380_dma_complete(NCR5380_instance * instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11002275 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /*
2279 * XXX this might not be right.
2280 *
2281 * Wait for final byte to transfer, ie wait for ACK to go false.
2282 *
2283 * We should use the Last Byte Sent bit, unfortunately this is
2284 * not available on the 5380/5381 (only the various CMOS chips)
2285 *
2286 * FIXME: timeout, and need to handle long timeout/irq case
2287 */
2288
2289 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2292
2293 /*
2294 * The only places we should see a phase mismatch and have to send
2295 * data from the same set of pointers will be the data transfer
2296 * phases. So, residual, requested length are only important here.
2297 */
2298
2299 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2300 transferred = instance->dmalen - NCR5380_dma_residual();
2301 hostdata->connected->SCp.this_residual -= transferred;
2302 hostdata->connected->SCp.ptr += transferred;
2303 }
2304}
2305#endif /* def REAL_DMA */
2306
Finn Thain8b00c3d2016-01-03 16:06:01 +11002307/**
2308 * list_find_cmd - test for presence of a command in a linked list
2309 * @haystack: list of commands
2310 * @needle: command to search for
2311 */
2312
2313static bool list_find_cmd(struct list_head *haystack,
2314 struct scsi_cmnd *needle)
2315{
2316 struct NCR5380_cmd *ncmd;
2317
2318 list_for_each_entry(ncmd, haystack, list)
2319 if (NCR5380_to_scmd(ncmd) == needle)
2320 return true;
2321 return false;
2322}
2323
2324/**
2325 * list_remove_cmd - remove a command from linked list
2326 * @haystack: list of commands
2327 * @needle: command to remove
2328 */
2329
2330static bool list_del_cmd(struct list_head *haystack,
2331 struct scsi_cmnd *needle)
2332{
2333 if (list_find_cmd(haystack, needle)) {
2334 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2335
2336 list_del(&ncmd->list);
2337 return true;
2338 }
2339 return false;
2340}
2341
2342/**
2343 * NCR5380_abort - scsi host eh_abort_handler() method
2344 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002346 * Try to abort a given command by removing it from queues and/or sending
2347 * the target an abort message. This may not succeed in causing a target
2348 * to abort the command. Nonetheless, the low-level driver must forget about
2349 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002351 * The normal path taken by a command is as follows. For EH we trace this
2352 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002354 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2355 * [disconnected -> connected ->]...
2356 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002358 * If cmd is unissued then just remove it.
2359 * If cmd is disconnected, try to select the target.
2360 * If cmd is connected, try to send an abort message.
2361 * If cmd is waiting for autosense, give it a chance to complete but check
2362 * that it isn't left connected.
2363 * If cmd was not found at all then presumably it has already been completed,
2364 * in which case return SUCCESS to try to avoid further EH measures.
2365 * If the command has not completed yet, we must not fail to find it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 */
2367
Finn Thain710ddd02014-11-12 16:12:02 +11002368static int NCR5380_abort(struct scsi_cmnd *cmd)
2369{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002371 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002372 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002373 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002374
Finn Thain11d2f632016-01-03 16:05:51 +11002375 spin_lock_irqsave(&hostdata->lock, flags);
2376
Finn Thain32b26a12016-01-03 16:05:58 +11002377#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002378 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002379#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002380 NCR5380_dprint(NDEBUG_ANY, instance);
2381 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Finn Thain8b00c3d2016-01-03 16:06:01 +11002383 if (list_del_cmd(&hostdata->unissued, cmd)) {
2384 dsprintk(NDEBUG_ABORT, instance,
2385 "abort: removed %p from issue queue\n", cmd);
2386 cmd->result = DID_ABORT << 16;
2387 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2388 }
2389
Finn Thain707d62b2016-01-03 16:06:02 +11002390 if (hostdata->selecting == cmd) {
2391 dsprintk(NDEBUG_ABORT, instance,
2392 "abort: cmd %p == selecting\n", cmd);
2393 hostdata->selecting = NULL;
2394 cmd->result = DID_ABORT << 16;
2395 complete_cmd(instance, cmd);
2396 goto out;
2397 }
2398
Finn Thain8b00c3d2016-01-03 16:06:01 +11002399 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2400 dsprintk(NDEBUG_ABORT, instance,
2401 "abort: removed %p from disconnected list\n", cmd);
2402 cmd->result = DID_ERROR << 16;
2403 if (!hostdata->connected)
2404 NCR5380_select(instance, cmd);
2405 if (hostdata->connected != cmd) {
2406 complete_cmd(instance, cmd);
2407 result = FAILED;
2408 goto out;
2409 }
2410 }
2411
2412 if (hostdata->connected == cmd) {
2413 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2414 hostdata->connected = NULL;
2415 if (do_abort(instance)) {
2416 set_host_byte(cmd, DID_ERROR);
2417 complete_cmd(instance, cmd);
2418 result = FAILED;
2419 goto out;
2420 }
2421 set_host_byte(cmd, DID_ABORT);
2422#ifdef REAL_DMA
2423 hostdata->dma_len = 0;
2424#endif
2425 if (cmd->cmnd[0] == REQUEST_SENSE)
2426 complete_cmd(instance, cmd);
2427 else {
2428 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
2429
2430 /* Perform autosense for this command */
2431 list_add(&ncmd->list, &hostdata->autosense);
2432 }
2433 }
2434
2435 if (list_find_cmd(&hostdata->autosense, cmd)) {
2436 dsprintk(NDEBUG_ABORT, instance,
2437 "abort: found %p on sense queue\n", cmd);
2438 spin_unlock_irqrestore(&hostdata->lock, flags);
2439 queue_work(hostdata->work_q, &hostdata->main_task);
2440 msleep(1000);
2441 spin_lock_irqsave(&hostdata->lock, flags);
2442 if (list_del_cmd(&hostdata->autosense, cmd)) {
2443 dsprintk(NDEBUG_ABORT, instance,
2444 "abort: removed %p from sense queue\n", cmd);
2445 set_host_byte(cmd, DID_ABORT);
2446 complete_cmd(instance, cmd);
2447 goto out;
2448 }
2449 }
2450
2451 if (hostdata->connected == cmd) {
2452 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2453 hostdata->connected = NULL;
2454 if (do_abort(instance)) {
2455 set_host_byte(cmd, DID_ERROR);
2456 complete_cmd(instance, cmd);
2457 result = FAILED;
2458 goto out;
2459 }
2460 set_host_byte(cmd, DID_ABORT);
2461#ifdef REAL_DMA
2462 hostdata->dma_len = 0;
2463#endif
2464 complete_cmd(instance, cmd);
2465 }
2466
2467out:
2468 if (result == FAILED)
2469 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2470 else
2471 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2472
2473 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002474 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002475
Finn Thain8b00c3d2016-01-03 16:06:01 +11002476 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479
Finn Thain3be1b3e2016-01-03 16:05:12 +11002480/**
2481 * NCR5380_bus_reset - reset the SCSI bus
2482 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002484 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
2486
Finn Thain710ddd02014-11-12 16:12:02 +11002487static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002488{
2489 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002490 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002491 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002492 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002493 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Finn Thain11d2f632016-01-03 16:05:51 +11002495 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002496
2497#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002498 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002499#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002500 NCR5380_dprint(NDEBUG_ANY, instance);
2501 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002502
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002503 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002504
Finn Thain62717f52016-01-03 16:06:03 +11002505 /* reset NCR registers */
2506 NCR5380_write(MODE_REG, MR_BASE);
2507 NCR5380_write(TARGET_COMMAND_REG, 0);
2508 NCR5380_write(SELECT_ENABLE_REG, 0);
2509
2510 /* After the reset, there are no more connected or disconnected commands
2511 * and no busy units; so clear the low-level status here to avoid
2512 * conflicts when the mid-level code tries to wake up the affected
2513 * commands!
2514 */
2515
2516 hostdata->selecting = NULL;
2517
2518 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2519 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2520
2521 set_host_byte(cmd, DID_RESET);
2522 cmd->scsi_done(cmd);
2523 }
2524
2525 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2526 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2527
2528 set_host_byte(cmd, DID_RESET);
2529 cmd->scsi_done(cmd);
2530 }
2531
2532 if (hostdata->connected) {
2533 set_host_byte(hostdata->connected, DID_RESET);
2534 complete_cmd(instance, hostdata->connected);
2535 hostdata->connected = NULL;
2536 }
2537
2538 if (hostdata->sensing) {
2539 set_host_byte(hostdata->connected, DID_RESET);
2540 complete_cmd(instance, hostdata->sensing);
2541 hostdata->sensing = NULL;
2542 }
2543
2544 for (i = 0; i < 8; ++i)
2545 hostdata->busy[i] = 0;
2546#ifdef REAL_DMA
2547 hostdata->dma_len = 0;
2548#endif
2549
2550 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002551 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return SUCCESS;
2554}