blob: 6b861fd3fbb11bb0c084ef7472783e85972010df [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 }
247}
248
249/**
Finn Thainb32ade12016-01-03 16:05:41 +1100250 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100251 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100252 * @reg1: 5380 register to poll
253 * @bit1: Bitmask to check
254 * @val1: Expected value
255 * @reg2: Second 5380 register to poll
256 * @bit2: Second bitmask to check
257 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100258 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
Finn Thain2f854b82016-01-03 16:05:22 +1100260 * Polls the chip in a reasonably efficient manner waiting for an
261 * event to occur. After a short quick poll we begin to yield the CPU
262 * (if possible). In irq contexts the time-out is arbitrarily limited.
263 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
Finn Thainb32ade12016-01-03 16:05:41 +1100265 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Finn Thainb32ade12016-01-03 16:05:41 +1100268static int NCR5380_poll_politely2(struct Scsi_Host *instance,
269 int reg1, int bit1, int val1,
270 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100271{
272 struct NCR5380_hostdata *hostdata = shost_priv(instance);
273 unsigned long deadline = jiffies + wait;
274 unsigned long n;
275
276 /* Busy-wait for up to 10 ms */
277 n = min(10000U, jiffies_to_usecs(wait));
278 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100279 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100280 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100281 if ((NCR5380_read(reg1) & bit1) == val1)
282 return 0;
283 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100286 } while (n--);
287
288 if (irqs_disabled() || in_interrupt())
289 return -ETIMEDOUT;
290
291 /* Repeatedly sleep for 1 ms until deadline */
292 while (time_is_after_jiffies(deadline)) {
293 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100294 if ((NCR5380_read(reg1) & bit1) == val1)
295 return 0;
296 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Finn Thain2f854b82016-01-03 16:05:22 +1100299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -ETIMEDOUT;
301}
302
Finn Thainb32ade12016-01-03 16:05:41 +1100303static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
304 int reg, int bit, int val, int wait)
305{
306 return NCR5380_poll_politely2(instance, reg, bit, val,
307 reg, bit, val, wait);
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static struct {
311 unsigned char value;
312 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700313} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 {PHASE_DATAOUT, "DATAOUT"},
315 {PHASE_DATAIN, "DATAIN"},
316 {PHASE_CMDOUT, "CMDOUT"},
317 {PHASE_STATIN, "STATIN"},
318 {PHASE_MSGOUT, "MSGOUT"},
319 {PHASE_MSGIN, "MSGIN"},
320 {PHASE_UNKNOWN, "UNKNOWN"}
321};
322
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100323#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static struct {
325 unsigned char mask;
326 const char *name;
327} signals[] = {
328 {SR_DBP, "PARITY"},
329 {SR_RST, "RST"},
330 {SR_BSY, "BSY"},
331 {SR_REQ, "REQ"},
332 {SR_MSG, "MSG"},
333 {SR_CD, "CD"},
334 {SR_IO, "IO"},
335 {SR_SEL, "SEL"},
336 {0, NULL}
337},
338basrs[] = {
339 {BASR_ATN, "ATN"},
340 {BASR_ACK, "ACK"},
341 {0, NULL}
342},
343icrs[] = {
344 {ICR_ASSERT_RST, "ASSERT RST"},
345 {ICR_ASSERT_ACK, "ASSERT ACK"},
346 {ICR_ASSERT_BSY, "ASSERT BSY"},
347 {ICR_ASSERT_SEL, "ASSERT SEL"},
348 {ICR_ASSERT_ATN, "ASSERT ATN"},
349 {ICR_ASSERT_DATA, "ASSERT DATA"},
350 {0, NULL}
351},
352mrs[] = {
353 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
354 {MR_TARGET, "MODE TARGET"},
355 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
356 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
357 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
358 {MR_DMA_MODE, "MODE DMA"},
359 {MR_ARBITRATE, "MODE ARBITRATION"},
360 {0, NULL}
361};
362
363/**
364 * NCR5380_print - print scsi bus signals
365 * @instance: adapter state to dump
366 *
367 * Print the SCSI bus signals for debugging purposes
368 *
369 * Locks: caller holds hostdata lock (not essential)
370 */
371
372static void NCR5380_print(struct Scsi_Host *instance)
373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
377 status = NCR5380_read(STATUS_REG);
378 mr = NCR5380_read(MODE_REG);
379 icr = NCR5380_read(INITIATOR_COMMAND_REG);
380 basr = NCR5380_read(BUS_AND_STATUS_REG);
381
382 printk("STATUS_REG: %02x ", status);
383 for (i = 0; signals[i].mask; ++i)
384 if (status & signals[i].mask)
385 printk(",%s", signals[i].name);
386 printk("\nBASR: %02x ", basr);
387 for (i = 0; basrs[i].mask; ++i)
388 if (basr & basrs[i].mask)
389 printk(",%s", basrs[i].name);
390 printk("\nICR: %02x ", icr);
391 for (i = 0; icrs[i].mask; ++i)
392 if (icr & icrs[i].mask)
393 printk(",%s", icrs[i].name);
394 printk("\nMODE: %02x ", mr);
395 for (i = 0; mrs[i].mask; ++i)
396 if (mr & mrs[i].mask)
397 printk(",%s", mrs[i].name);
398 printk("\n");
399}
400
401
402/*
403 * NCR5380_print_phase - show SCSI phase
404 * @instance: adapter to dump
405 *
406 * Print the current SCSI phase for debugging purposes
407 *
408 * Locks: none
409 */
410
411static void NCR5380_print_phase(struct Scsi_Host *instance)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned char status;
414 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 status = NCR5380_read(STATUS_REG);
417 if (!(status & SR_REQ))
418 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
419 else {
420 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
421 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
422 }
423}
424#endif
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Finn Thaind5f7e652016-01-03 16:05:03 +1100427static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/**
430 * probe_intr - helper for IRQ autoprobe
431 * @irq: interrupt number
432 * @dev_id: unused
433 * @regs: unused
434 *
435 * Set a flag to indicate the IRQ in question was received. This is
436 * used by the IRQ probe code.
437 */
438
David Howells7d12e782006-10-05 14:55:46 +0100439static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 probe_irq = irq;
442 return IRQ_HANDLED;
443}
444
445/**
446 * NCR5380_probe_irq - find the IRQ of an NCR5380
447 * @instance: NCR5380 controller
448 * @possible: bitmask of ISA IRQ lines
449 *
450 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
451 * and then looking to see what interrupt actually turned up.
452 *
453 * Locks: none, irqs must be enabled on entry
454 */
455
Andrew Morton702809c2007-05-23 14:41:56 -0700456static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
457 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Finn Thaine8a60142016-01-03 16:05:54 +1100459 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 unsigned long timeout;
461 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Finn Thain22f5f102014-11-12 16:11:56 +1100463 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100464 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 trying_irqs |= mask;
466
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500467 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100468 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /*
471 * A interrupt is triggered whenever BSY = false, SEL = true
472 * and a bit set in the SELECT_ENABLE_REG is asserted on the
473 * SCSI bus.
474 *
475 * Note that the bus is only driven when the phase control signals
476 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
477 * to zero.
478 */
479
480 NCR5380_write(TARGET_COMMAND_REG, 0);
481 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
482 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
483 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
484
Finn Thain22f5f102014-11-12 16:11:56 +1100485 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800486 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 NCR5380_write(SELECT_ENABLE_REG, 0);
489 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
490
Finn Thain22f5f102014-11-12 16:11:56 +1100491 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (trying_irqs & mask)
493 free_irq(i, NULL);
494
495 return probe_irq;
496}
497
498/**
Finn Thain8c325132014-11-12 16:11:58 +1100499 * NCR58380_info - report driver and host information
500 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *
Finn Thain8c325132014-11-12 16:11:58 +1100502 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *
504 * Locks: none
505 */
506
Finn Thain8c325132014-11-12 16:11:58 +1100507static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Finn Thain8c325132014-11-12 16:11:58 +1100509 struct NCR5380_hostdata *hostdata = shost_priv(instance);
510
511 return hostdata->info;
512}
513
514static void prepare_info(struct Scsi_Host *instance)
515{
516 struct NCR5380_hostdata *hostdata = shost_priv(instance);
517
518 snprintf(hostdata->info, sizeof(hostdata->info),
519 "%s, io_port 0x%lx, n_io_port %d, "
520 "base 0x%lx, irq %d, "
521 "can_queue %d, cmd_per_lun %d, "
522 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100523 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100524 "options { %s} ",
525 instance->hostt->name, instance->io_port, instance->n_io_port,
526 instance->base, instance->irq,
527 instance->can_queue, instance->cmd_per_lun,
528 instance->sg_tablesize, instance->this_id,
Finn Thain55181be2016-01-03 16:05:42 +1100529 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100530 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100531 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100533 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100536 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#endif
538#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100539 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif
541#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100542 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#endif
544#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100545 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#endif
547#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100548 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif
Finn Thain8c325132014-11-12 16:11:58 +1100550 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Finn Thaina9c2dc42014-11-12 16:11:59 +1100553#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/******************************************/
555/*
556 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
557 *
558 * *buffer: I/O buffer
559 * **start: if inout == FALSE pointer into buffer where user read should start
560 * offset: current offset
561 * length: length of buffer
562 * hostno: Scsi_Host host_no
563 * inout: TRUE - user is writing; FALSE - user is reading
564 *
565 * Return the number of bytes read from or written
566 */
567
Al Virodd7ab712013-03-31 01:15:54 -0400568static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
569 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100571 struct NCR5380_hostdata *hostdata = shost_priv(instance);
572
573 hostdata->spin_max_r = 0;
574 hostdata->spin_max_w = 0;
575 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400576}
Al Virodd7ab712013-03-31 01:15:54 -0400577
578static int __maybe_unused NCR5380_show_info(struct seq_file *m,
579 struct Scsi_Host *instance)
580{
Finn Thaine8a60142016-01-03 16:05:54 +1100581 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100583 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100584 hostdata->spin_max_w, hostdata->spin_max_r);
Al Virodd7ab712013-03-31 01:15:54 -0400585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
Finn Thaine5c3fdd2016-01-03 16:05:52 +1100587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/**
590 * NCR5380_init - initialise an NCR5380
591 * @instance: adapter to configure
592 * @flags: control flags
593 *
594 * Initializes *instance and corresponding 5380 chip,
595 * with flags OR'd into the initial flags value.
596 *
597 * Notes : I assume that the host, hostno, and id bits have been
598 * set correctly. I don't care about the irq and other fields.
599 *
600 * Returns 0 for success
601 *
602 * Locks: interrupts must be enabled when we are called
603 */
604
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800605static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Finn Thaine8a60142016-01-03 16:05:54 +1100607 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100608 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100609 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if(in_interrupt())
612 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 hostdata->id_mask = 1 << instance->this_id;
615 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
616 if (i > hostdata->id_mask)
617 hostdata->id_higher_mask |= i;
618 for (i = 0; i < 8; ++i)
619 hostdata->busy[i] = 0;
620#ifdef REAL_DMA
621 hostdata->dmalen = 0;
622#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100623 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +1100625 INIT_LIST_HEAD(&hostdata->unissued);
626 INIT_LIST_HEAD(&hostdata->disconnected);
627
Finn Thain55181be2016-01-03 16:05:42 +1100628 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Finn Thain8d8601a2016-01-03 16:05:37 +1100630 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100631 hostdata->work_q = alloc_workqueue("ncr5380_%d",
632 WQ_UNBOUND | WQ_MEM_RECLAIM,
633 1, instance->host_no);
634 if (!hostdata->work_q)
635 return -ENOMEM;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Finn Thain8c325132014-11-12 16:11:58 +1100639 prepare_info(instance);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
642 NCR5380_write(MODE_REG, MR_BASE);
643 NCR5380_write(TARGET_COMMAND_REG, 0);
644 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100645
646 /* Calibrate register polling loop */
647 i = 0;
648 deadline = jiffies + 1;
649 do {
650 cpu_relax();
651 } while (time_is_after_jiffies(deadline));
652 deadline += msecs_to_jiffies(256);
653 do {
654 NCR5380_read(STATUS_REG);
655 ++i;
656 cpu_relax();
657 } while (time_is_after_jiffies(deadline));
658 hostdata->accesses_per_ms = i / 256;
659
Finn Thainb6488f92016-01-03 16:05:08 +1100660 return 0;
661}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Finn Thainb6488f92016-01-03 16:05:08 +1100663/**
664 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
665 * @instance: adapter to check
666 *
667 * If the system crashed, it may have crashed with a connected target and
668 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
669 * currently established nexus, which we know nothing about. Failing that
670 * do a bus reset.
671 *
672 * Note that a bus reset will cause the chip to assert IRQ.
673 *
674 * Returns 0 if successful, otherwise -ENXIO.
675 */
676
677static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
678{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100679 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100680 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
683 switch (pass) {
684 case 1:
685 case 3:
686 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100687 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
688 NCR5380_poll_politely(instance,
689 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100692 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 do_abort(instance);
694 break;
695 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100696 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100698 /* Wait after a reset; the SCSI standard calls for
699 * 250ms, we wait 500ms to be on the safe side.
700 * But some Toshiba CD-ROMs need ten times that.
701 */
702 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
703 msleep(2500);
704 else
705 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100708 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -ENXIO;
710 }
711 }
712 return 0;
713}
714
715/**
716 * NCR5380_exit - remove an NCR5380
717 * @instance: adapter to remove
718 */
719
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800720static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Finn Thaine8a60142016-01-03 16:05:54 +1100722 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Finn Thain8d8601a2016-01-03 16:05:37 +1100724 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100725 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/**
Finn Thain1bb40582016-01-03 16:05:29 +1100729 * NCR5380_queue_command - queue a command
730 * @instance: the relevant SCSI adapter
731 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
Finn Thain1bb40582016-01-03 16:05:29 +1100733 * cmd is added to the per-instance issue queue, with minor
734 * twiddling done to the host specific fields of cmd. If the
735 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
737
Finn Thain1bb40582016-01-03 16:05:29 +1100738static int NCR5380_queue_command(struct Scsi_Host *instance,
739 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Finn Thain1bb40582016-01-03 16:05:29 +1100741 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100742 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100743 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745#if (NDEBUG & NDEBUG_NO_WRITE)
746 switch (cmd->cmnd[0]) {
747 case WRITE_6:
748 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100749 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100751 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753 }
754#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 cmd->result = 0;
757
Finn Thain11d2f632016-01-03 16:05:51 +1100758 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Insert the cmd into the issue queue. Note that REQUEST SENSE
762 * commands are added to the head of the queue since any command will
763 * clear the contingent allegiance condition that exists and the
764 * sense data is only guaranteed to be valid while the condition exists.
765 */
766
Finn Thain32b26a12016-01-03 16:05:58 +1100767 if (cmd->cmnd[0] == REQUEST_SENSE)
768 list_add(&ncmd->list, &hostdata->unissued);
769 else
770 list_add_tail(&ncmd->list, &hostdata->unissued);
771
Finn Thain11d2f632016-01-03 16:05:51 +1100772 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100773
Finn Thaindbb6b352016-01-03 16:05:53 +1100774 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
775 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100778 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return 0;
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/**
783 * NCR5380_main - NCR state machines
784 *
785 * NCR5380_main is a coroutine that runs as long as more work can
786 * be done on the NCR5380 host adapters in a system. Both
787 * NCR5380_queue_command() and NCR5380_intr() will try to start it
788 * in case it is not running.
789 *
790 * Locks: called as its own thread with no locks held. Takes the
791 * host lock and called routines may take the isa dma lock.
792 */
793
David Howellsc4028952006-11-22 14:57:56 +0000794static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
David Howellsc4028952006-11-22 14:57:56 +0000796 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100797 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct Scsi_Host *instance = hostdata->host;
Finn Thain32b26a12016-01-03 16:05:58 +1100799 struct NCR5380_cmd *ncmd, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int done;
801
Finn Thain11d2f632016-01-03 16:05:51 +1100802 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100805
Finn Thainae753a32016-01-03 16:05:23 +1100806 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100807 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * Search through the issue_queue for a command destined
810 * for a target that's not busy.
811 */
Finn Thain32b26a12016-01-03 16:05:58 +1100812 list_for_each_entry_safe(ncmd, n, &hostdata->unissued,
813 list) {
814 struct scsi_cmnd *tmp = NCR5380_to_scmd(ncmd);
815
Finn Thain0d3d9a42016-01-03 16:05:55 +1100816 dsprintk(NDEBUG_QUEUES, instance, "main: tmp=%p target=%d busy=%d lun=%llu\n",
817 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
818 tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200820 if (!(hostdata->busy[tmp->device->id] &
821 (1 << (u8)(tmp->device->lun & 0xff)))) {
Finn Thain32b26a12016-01-03 16:05:58 +1100822 list_del(&ncmd->list);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100823 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES,
Finn Thain32b26a12016-01-03 16:05:58 +1100824 instance, "main: removed %p from issue queue\n",
825 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /*
828 * Attempt to establish an I_T_L nexus here.
829 * On success, instance->hostdata->connected is set.
830 * On failure, we must add the command back to the
831 * issue queue so we can keep trying.
832 */
Finn Thain76f13b92014-11-12 16:11:53 +1100833 /*
834 * REQUEST SENSE commands are issued without tagged
835 * queueing, even on SCSI-II devices because the
836 * contingent allegiance condition exists for the
837 * entire unit.
838 */
839
840 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100841 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100843 /* Need to retry */
Finn Thain32b26a12016-01-03 16:05:58 +1100844 list_add(&ncmd->list, &hostdata->unissued);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100845 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES,
846 instance, "main: select() failed, %p returned to issue queue\n",
847 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Finn Thainae753a32016-01-03 16:05:23 +1100850 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 } /* if target/lun is not busy */
853 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 } /* if (!hostdata->connected) */
Finn Thain11d2f632016-01-03 16:05:51 +1100855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (hostdata->connected
857#ifdef REAL_DMA
858 && !hostdata->dmalen
859#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100861 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100863 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 } while (!done);
Finn Thain11d2f632016-01-03 16:05:51 +1100867 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870#ifndef DONT_USE_INTR
871
872/**
Finn Thaincd400822016-01-03 16:05:40 +1100873 * NCR5380_intr - generic NCR5380 irq handler
874 * @irq: interrupt number
875 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
Finn Thaincd400822016-01-03 16:05:40 +1100877 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
878 * from the disconnected queue, and restarting NCR5380_main()
879 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
Finn Thaincd400822016-01-03 16:05:40 +1100881 * The chip can assert IRQ in any of six different conditions. The IRQ flag
882 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
883 * Three of these six conditions are latched in the Bus and Status Register:
884 * - End of DMA (cleared by ending DMA Mode)
885 * - Parity error (cleared by reading RPIR)
886 * - Loss of BSY (cleared by reading RPIR)
887 * Two conditions have flag bits that are not latched:
888 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
889 * - Bus reset (non-maskable)
890 * The remaining condition has no flag bit at all:
891 * - Selection/reselection
892 *
893 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
894 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
895 * claimed that "the design of the [DP8490] interrupt logic ensures
896 * interrupts will not be lost (they can be on the DP5380)."
897 * The L5380/53C80 datasheet from LOGIC Devices has more details.
898 *
899 * Checking for bus reset by reading RST is futile because of interrupt
900 * latency, but a bus reset will reset chip logic. Checking for parity error
901 * is unnecessary because that interrupt is never enabled. A Loss of BSY
902 * condition will clear DMA Mode. We can tell when this occurs because the
903 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
905
Finn Thaincd400822016-01-03 16:05:40 +1100906static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800908 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100909 struct NCR5380_hostdata *hostdata = shost_priv(instance);
910 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned char basr;
912 unsigned long flags;
913
Finn Thain11d2f632016-01-03 16:05:51 +1100914 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Finn Thaincd400822016-01-03 16:05:40 +1100916 basr = NCR5380_read(BUS_AND_STATUS_REG);
917 if (basr & BASR_IRQ) {
918 unsigned char mr = NCR5380_read(MODE_REG);
919 unsigned char sr = NCR5380_read(STATUS_REG);
920
921 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
922 instance->host_no, irq, basr, sr, mr);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +1100925 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
926 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
927 * We ack IRQ after clearing Mode Register. Workarounds
928 * for End of DMA errata need to happen in DMA Mode.
929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Finn Thaincd400822016-01-03 16:05:40 +1100931 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Finn Thaincd400822016-01-03 16:05:40 +1100933 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Finn Thaincd400822016-01-03 16:05:40 +1100935 if (!hostdata->connected)
936 panic("scsi%d : DMA interrupt with no connected cmd\n",
937 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Finn Thaincd400822016-01-03 16:05:40 +1100939 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
940 hostdata->connected->SCp.this_residual -= transferred;
941 hostdata->connected->SCp.ptr += transferred;
942 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Finn Thaincd400822016-01-03 16:05:40 +1100944 /* FIXME: we need to poll briefly then defer a workqueue task ! */
945 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
946
947 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
948 NCR5380_write(MODE_REG, MR_BASE);
949 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
950 } else
951#endif /* REAL_DMA */
952 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
953 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
954 /* Probably reselected */
955 NCR5380_write(SELECT_ENABLE_REG, 0);
956 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
957
958 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
959 instance->host_no);
960
961 if (!hostdata->connected) {
962 NCR5380_reselect(instance);
963 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Finn Thaincd400822016-01-03 16:05:40 +1100965 if (!hostdata->connected)
966 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
967 } else {
968 /* Probably Bus Reset */
969 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
970
971 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
972 }
973 handled = 1;
974 } else {
975 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
976 }
977
Finn Thain11d2f632016-01-03 16:05:51 +1100978 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100979
980 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983#endif
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/*
Finn Thain710ddd02014-11-12 16:12:02 +1100986 * Function : int NCR5380_select(struct Scsi_Host *instance,
987 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 *
989 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
990 * including ARBITRATION, SELECTION, and initial message out for
991 * IDENTIFY and queue messages.
992 *
993 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +1100994 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 *
Finn Thain63238762016-01-03 16:05:15 +1100996 * Returns : -1 if selection failed but should be retried.
997 * 0 if selection failed and should not be retried.
998 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 *
1000 * Side effects :
1001 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1002 * with registers as they should have been on entry - ie
1003 * SELECT_ENABLE will be set appropriately, the NCR5380
1004 * will cease to drive any SCSI bus signals.
1005 *
1006 * If successful : I_T_L or I_T_L_Q nexus will be established,
1007 * instance->connected will be set to cmd.
1008 * SELECT interrupt will be disabled.
1009 *
1010 * If failed (no target) : cmd->scsi_done() will be called, and the
1011 * cmd->result host byte set to DID_BAD_TARGET.
1012 *
1013 * Locks: caller holds hostdata lock in IRQ mode
1014 */
1015
Finn Thain710ddd02014-11-12 16:12:02 +11001016static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Finn Thaine8a60142016-01-03 16:05:54 +11001018 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 unsigned char tmp[3], phase;
1020 unsigned char *data;
1021 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001025 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /*
1028 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1029 * data bus during SELECTION.
1030 */
1031
1032 NCR5380_write(TARGET_COMMAND_REG, 0);
1033
1034 /*
1035 * Start arbitration.
1036 */
1037
1038 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1039 NCR5380_write(MODE_REG, MR_ARBITRATE);
1040
Finn Thain55500d92016-01-03 16:05:35 +11001041 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1042 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044
Finn Thain11d2f632016-01-03 16:05:51 +11001045 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001046 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1047 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1048 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001049 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001050 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1051 /* Reselection interrupt */
1052 return -1;
1053 }
1054 if (err < 0) {
1055 NCR5380_write(MODE_REG, MR_BASE);
1056 shost_printk(KERN_ERR, instance,
1057 "select: arbitration timeout\n");
1058 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001059 }
Finn Thain11d2f632016-01-03 16:05:51 +11001060 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001061
1062 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 udelay(3);
1064
1065 /* Check for lost arbitration */
1066 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)) {
1067 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001068 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain11d2f632016-01-03 16:05:51 +11001069 spin_lock_irq(&hostdata->lock);
Finn Thain63238762016-01-03 16:05:15 +11001070 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Finn Thaincf13b082016-01-03 16:05:18 +11001072
1073 /* After/during arbitration, BSY should be asserted.
1074 * IBM DPES-31080 Version S31Q works now
1075 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1076 */
1077 NCR5380_write(INITIATOR_COMMAND_REG,
1078 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * Again, bus clear + bus settle time is 1.2us, however, this is
1082 * a minimum so we'll udelay ceil(1.2)
1083 */
1084
Finn Thain9c3f0e22016-01-03 16:05:11 +11001085 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1086 udelay(15);
1087 else
1088 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Finn Thain11d2f632016-01-03 16:05:51 +11001090 spin_lock_irq(&hostdata->lock);
1091
Finn Thain72064a72016-01-03 16:05:44 +11001092 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1093 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1094 return -1;
1095
Finn Thain52a6a1c2014-03-18 11:42:18 +11001096 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /*
1099 * Now that we have won arbitration, start Selection process, asserting
1100 * the host and target ID's on the SCSI bus.
1101 */
1102
Jeff Garzik422c0d62005-10-24 18:05:09 -04001103 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /*
1106 * Raise ATN while SEL is true before BSY goes false from arbitration,
1107 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1108 * phase immediately after selection.
1109 */
1110
1111 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1112 NCR5380_write(MODE_REG, MR_BASE);
1113
1114 /*
1115 * Reselect interrupts must be turned off prior to the dropping of BSY,
1116 * otherwise we will trigger an interrupt.
1117 */
1118 NCR5380_write(SELECT_ENABLE_REG, 0);
1119
Finn Thain11d2f632016-01-03 16:05:51 +11001120 spin_unlock_irq(&hostdata->lock);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
1123 * The initiator shall then wait at least two deskew delays and release
1124 * the BSY signal.
1125 */
1126 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1127
1128 /* Reset BSY */
1129 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1130
1131 /*
1132 * Something weird happens when we cease to drive BSY - looks
1133 * like the board/chip is letting us do another read before the
1134 * appropriate propagation delay has expired, and we're confusing
1135 * a BSY signal from ourselves as the target's response to SELECTION.
1136 *
1137 * A small delay (the 'C++' frontend breaks the pipeline with an
1138 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1139 * tighter 'C' code breaks and requires this) solves the problem -
1140 * the 1 us delay is arbitrary, and only used because this delay will
1141 * be the same on other platforms and since it works here, it should
1142 * work there.
1143 *
1144 * wingel suggests that this could be due to failing to wait
1145 * one deskew delay.
1146 */
1147
1148 udelay(1);
1149
Finn Thain52a6a1c2014-03-18 11:42:18 +11001150 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /*
1153 * The SCSI specification calls for a 250 ms timeout for the actual
1154 * selection.
1155 */
1156
Finn Thainae753a32016-01-03 16:05:23 +11001157 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1158 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001161 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1163 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001164 if (!hostdata->connected)
1165 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -1;
1168 }
Finn Thainae753a32016-01-03 16:05:23 +11001169
1170 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001171 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001172 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1173 cmd->result = DID_BAD_TARGET << 16;
1174 cmd->scsi_done(cmd);
1175 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1176 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1177 instance->host_no);
1178 return 0;
1179 }
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * No less than two deskew delays after the initiator detects the
1183 * BSY signal is true, it shall release the SEL signal and may
1184 * change the DATA BUS. -wingel
1185 */
1186
1187 udelay(1);
1188
1189 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * Since we followed the SCSI spec, and raised ATN while SEL
1193 * was true but before BSY was false during selection, the information
1194 * transfer phase should be a MESSAGE OUT phase so that we can send the
1195 * IDENTIFY message.
1196 *
1197 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1198 * message (2 bytes) with a tag ID that we increment with every command
1199 * until it wraps back to 0.
1200 *
1201 * XXX - it turns out that there are some broken SCSI-II devices,
1202 * which claim to support tagged queuing but fail when more than
1203 * some number of commands are issued at once.
1204 */
1205
1206 /* Wait for start of REQ/ACK handshake */
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001209 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001210 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001211 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1212 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001214 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
Finn Thain52a6a1c2014-03-18 11:42:18 +11001217 dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
Finn Thain22f5f102014-11-12 16:11:56 +11001218 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 len = 1;
1221 cmd->tag = 0;
1222
1223 /* Send message(s) */
1224 data = tmp;
1225 phase = PHASE_MSGOUT;
1226 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001227 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001231 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Boaz Harrosh28424d32007-09-10 22:37:45 +03001233 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238/*
1239 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1240 * unsigned char *phase, int *count, unsigned char **data)
1241 *
1242 * Purpose : transfers data in given phase using polled I/O
1243 *
1244 * Inputs : instance - instance of driver, *phase - pointer to
1245 * what phase is expected, *count - pointer to number of
1246 * bytes to transfer, **data - pointer to data pointer.
1247 *
1248 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001249 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * is in same phase.
1251 *
1252 * Also, *phase, *count, *data are modified in place.
1253 *
1254 * XXX Note : handling for bus free may be useful.
1255 */
1256
1257/*
1258 * Note : this code is not as quick as it could be, however it
1259 * IS 100% reliable, and for the actual data transfer where speed
1260 * counts, we will always do a pseudo DMA or DMA transfer.
1261 */
1262
1263static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 unsigned char p = *phase, tmp;
1265 int c = *count;
1266 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001269 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001271 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /*
1274 * The NCR5380 chip will only drive the SCSI bus when the
1275 * phase specified in the appropriate bits of the TARGET COMMAND
1276 * REGISTER match the STATUS REGISTER
1277 */
1278
1279 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 do {
1282 /*
1283 * Wait for assertion of REQ, after which the phase bits will be
1284 * valid
1285 */
1286
Finn Thain686f3992016-01-03 16:05:26 +11001287 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Finn Thain52a6a1c2014-03-18 11:42:18 +11001290 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001293 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001294 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1296 break;
1297 }
1298 /* Do actual transfer from SCSI bus to / from memory */
1299 if (!(p & SR_IO))
1300 NCR5380_write(OUTPUT_DATA_REG, *d);
1301 else
1302 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1303
1304 ++d;
1305
1306 /*
1307 * The SCSI standard suggests that in MSGOUT phase, the initiator
1308 * should drop ATN on the last byte of the message phase
1309 * after REQ has been asserted for the handshake but before
1310 * the initiator raises ACK.
1311 */
1312
1313 if (!(p & SR_IO)) {
1314 if (!((p & SR_MSG) && c > 1)) {
1315 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1316 NCR5380_dprint(NDEBUG_PIO, instance);
1317 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1318 } else {
1319 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1320 NCR5380_dprint(NDEBUG_PIO, instance);
1321 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1322 }
1323 } else {
1324 NCR5380_dprint(NDEBUG_PIO, instance);
1325 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1326 }
1327
Finn Thaina2edc4a2016-01-03 16:05:27 +11001328 if (NCR5380_poll_politely(instance,
1329 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1330 break;
1331
Finn Thain52a6a1c2014-03-18 11:42:18 +11001332 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334/*
1335 * We have several special cases to consider during REQ/ACK handshaking :
1336 * 1. We were in MSGOUT phase, and we are on the last byte of the
1337 * message. ATN must be dropped as ACK is dropped.
1338 *
1339 * 2. We are in a MSGIN phase, and we are on the last byte of the
1340 * message. We must exit with ACK asserted, so that the calling
1341 * code may raise ATN before dropping ACK to reject the message.
1342 *
1343 * 3. ACK and ATN are clear and the target may proceed as normal.
1344 */
1345 if (!(p == PHASE_MSGIN && c == 1)) {
1346 if (p == PHASE_MSGOUT && c > 1)
1347 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1348 else
1349 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1350 }
1351 } while (--c);
1352
Finn Thain52a6a1c2014-03-18 11:42:18 +11001353 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 *count = c;
1356 *data = d;
1357 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001358 /* The phase read from the bus is valid if either REQ is (already)
1359 * asserted or if ACK hasn't been released yet. The latter applies if
1360 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1361 */
1362 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *phase = tmp & PHASE_MASK;
1364 else
1365 *phase = PHASE_UNKNOWN;
1366
1367 if (!c || (*phase == p))
1368 return 0;
1369 else
1370 return -1;
1371}
1372
1373/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001374 * do_reset - issue a reset command
1375 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001377 * Issue a reset sequence to the NCR5380 and try and get the bus
1378 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001380 * This clears the reset interrupt flag because there may be no handler for
1381 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1382 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384
Finn Thain54d8fe42016-01-03 16:05:06 +11001385static void do_reset(struct Scsi_Host *instance)
1386{
Finn Thain636b1ec2016-01-03 16:05:10 +11001387 unsigned long flags;
1388
1389 local_irq_save(flags);
1390 NCR5380_write(TARGET_COMMAND_REG,
1391 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001393 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001395 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1396 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Finn Thain80d3eb62016-01-03 16:05:34 +11001399/**
1400 * do_abort - abort the currently established nexus by going to
1401 * MESSAGE OUT phase and sending an ABORT message.
1402 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001404 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
1406
Finn Thain54d8fe42016-01-03 16:05:06 +11001407static int do_abort(struct Scsi_Host *instance)
1408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 unsigned char *msgptr, phase, tmp;
1410 int len;
1411 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /* Request message out phase */
1414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1415
1416 /*
1417 * Wait for the target to indicate a valid phase by asserting
1418 * REQ. Once this happens, we'll have either a MSGOUT phase
1419 * and can immediately send the ABORT message, or we'll have some
1420 * other phase and will have to source/sink data.
1421 *
1422 * We really don't care what value was on the bus or what value
1423 * the target sees, so we just handshake.
1424 */
1425
Finn Thain80d3eb62016-01-03 16:05:34 +11001426 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001427 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001428 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Finn Thainf35d3472016-01-03 16:05:33 +11001430 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1433
Finn Thainf35d3472016-01-03 16:05:33 +11001434 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001436 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001437 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001438 goto timeout;
1439 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 tmp = ABORT;
1442 msgptr = &tmp;
1443 len = 1;
1444 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001445 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /*
1448 * If we got here, and the command completed successfully,
1449 * we're about to go into bus free state.
1450 */
1451
1452 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001453
1454timeout:
1455 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1456 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1460/*
1461 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1462 * unsigned char *phase, int *count, unsigned char **data)
1463 *
1464 * Purpose : transfers data in given phase using either real
1465 * or pseudo DMA.
1466 *
1467 * Inputs : instance - instance of driver, *phase - pointer to
1468 * what phase is expected, *count - pointer to number of
1469 * bytes to transfer, **data - pointer to data pointer.
1470 *
1471 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001472 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * is in same phase.
1474 *
1475 * Also, *phase, *count, *data are modified in place.
1476 *
1477 * Locks: io_request lock held by caller
1478 */
1479
1480
1481static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 register int c = *count;
1483 register unsigned char p = *phase;
1484 register unsigned char *d = *data;
1485 unsigned char tmp;
1486 int foo;
1487#if defined(REAL_DMA_POLL)
1488 int cnt, toPIO;
1489 unsigned char saved_data = 0, overrun = 0, residue;
1490#endif
1491
Finn Thaine8a60142016-01-03 16:05:54 +11001492 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1495 *phase = tmp;
1496 return -1;
1497 }
1498#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001500 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
1501 c -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001503 dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1505#endif
1506
1507 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1508
1509#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001510 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1511 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001513 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514#else
1515 /*
1516 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1517 * were not disabled for the duration of a single DMA transfer, from
1518 * before the setting of DMA mode to after transfer of the last byte.
1519 */
1520
Finn Thain55181be2016-01-03 16:05:42 +11001521 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001522 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1523 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 else
Finn Thaincd400822016-01-03 16:05:40 +11001525 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#endif /* def REAL_DMA */
1527
Finn Thain52a6a1c2014-03-18 11:42:18 +11001528 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 /*
1531 * On the PAS16 at least I/O recovery delays are not needed here.
1532 * Everyone else seems to want them.
1533 */
1534
1535 if (p & SR_IO) {
1536 io_recovery_delay(1);
1537 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1538 } else {
1539 io_recovery_delay(1);
1540 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1541 io_recovery_delay(1);
1542 NCR5380_write(START_DMA_SEND_REG, 0);
1543 io_recovery_delay(1);
1544 }
1545
1546#if defined(REAL_DMA_POLL)
1547 do {
1548 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1549 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1550
1551/*
1552 At this point, either we've completed DMA, or we have a phase mismatch,
1553 or we've unexpectedly lost BUSY (which is a real error).
1554
1555 For write DMAs, we want to wait until the last byte has been
1556 transferred out over the bus before we turn off DMA mode. Alas, there
1557 seems to be no terribly good way of doing this on a 5380 under all
1558 conditions. For non-scatter-gather operations, we can wait until REQ
1559 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1560 are nastier, since the device will be expecting more data than we
1561 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1562 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1563 why this signal was added to the newer chips) but on the older 538[01]
1564 this signal does not exist. The workaround for this lack is a watchdog;
1565 we bail out of the wait-loop after a modest amount of wait-time if
1566 the usual exit conditions are not met. Not a terribly clean or
1567 correct solution :-%
1568
1569 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1570 If the chip is in DMA mode for an READ, it will respond to a target's
1571 REQ by latching the SCSI data into the INPUT DATA register and asserting
1572 ACK, even if it has _already_ been notified by the DMA controller that
1573 the current DMA transfer has completed! If the NCR5380 is then taken
1574 out of DMA mode, this already-acknowledged byte is lost.
1575
1576 This is not a problem for "one DMA transfer per command" reads, because
1577 the situation will never arise... either all of the data is DMA'ed
1578 properly, or the target switches to MESSAGE IN phase to signal a
1579 disconnection (either operation bringing the DMA to a clean halt).
1580 However, in order to handle scatter-reads, we must work around the
1581 problem. The chosen fix is to DMA N-2 bytes, then check for the
1582 condition before taking the NCR5380 out of DMA mode. One or two extra
1583 bytes are transferred via PIO as necessary to fill out the original
1584 request.
1585 */
1586
1587 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001588 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
1589 udelay(10);
1590 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1591 (BASR_PHASE_MATCH | BASR_ACK)) {
1592 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1593 overrun = 1;
1594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 } else {
1597 int limit = 100;
1598 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1599 if (!(tmp & BASR_PHASE_MATCH))
1600 break;
1601 if (--limit < 0)
1602 break;
1603 }
1604 }
1605
Finn Thain52a6a1c2014-03-18 11:42:18 +11001606 dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 NCR5380_write(MODE_REG, MR_BASE);
1609 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1610
1611 residue = NCR5380_dma_residual(instance);
1612 c -= residue;
1613 *count -= c;
1614 *data += c;
1615 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1616
Finn Thain9db60242016-01-03 16:05:43 +11001617 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
1618 *phase == p && (p & SR_IO) && residue == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001620 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 **data = saved_data;
1622 *data += 1;
1623 *count -= 1;
1624 cnt = toPIO = 1;
1625 } else {
1626 printk("No overrun??\n");
1627 cnt = toPIO = 2;
1628 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001629 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 NCR5380_transfer_pio(instance, phase, &cnt, data);
1631 *count -= toPIO - cnt;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Finn Thain52a6a1c2014-03-18 11:42:18 +11001634 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 -07001635 return 0;
1636
1637#elif defined(REAL_DMA)
1638 return 0;
1639#else /* defined(REAL_DMA_POLL) */
1640 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001641 foo = NCR5380_pread(instance, d,
1642 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1643 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * We can't disable DMA mode after successfully transferring
1646 * what we plan to be the last byte, since that would open up
1647 * a race condition where if the target asserted REQ before
1648 * we got the DMA mode reset, the NCR5380 would have latched
1649 * an additional byte into the INPUT DATA register and we'd
1650 * have dropped it.
1651 *
1652 * The workaround was to transfer one fewer bytes than we
1653 * intended to with the pseudo-DMA read function, wait for
1654 * the chip to latch the last byte, read it, and then disable
1655 * pseudo-DMA mode.
1656 *
1657 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1658 * REQ is deasserted when ACK is asserted, and not reasserted
1659 * until ACK goes false. Since the NCR5380 won't lower ACK
1660 * until DACK is asserted, which won't happen unless we twiddle
1661 * the DMA port or we take the NCR5380 out of DMA mode, we
1662 * can guarantee that we won't handshake another extra
1663 * byte.
1664 */
1665
Finn Thain55181be2016-01-03 16:05:42 +11001666 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1667 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1668 foo = -1;
1669 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
Finn Thain55181be2016-01-03 16:05:42 +11001671 if (NCR5380_poll_politely(instance, STATUS_REG,
1672 SR_REQ, 0, HZ) < 0) {
1673 foo = -1;
1674 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1675 }
1676 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001680 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * Wait for the last byte to be sent. If REQ is being asserted for
1683 * the byte we're interested, we'll ACK it and it will go false.
1684 */
Finn Thain55181be2016-01-03 16:05:42 +11001685 if (NCR5380_poll_politely2(instance,
1686 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1687 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1688 foo = -1;
1689 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693 NCR5380_write(MODE_REG, MR_BASE);
1694 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001695 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 *data = d + c;
1697 *count = 0;
1698 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return foo;
1700#endif /* def REAL_DMA */
1701}
1702#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1703
1704/*
1705 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1706 *
1707 * Purpose : run through the various SCSI phases and do as the target
1708 * directs us to. Operates on the currently connected command,
1709 * instance->connected.
1710 *
1711 * Inputs : instance, instance for which we are doing commands
1712 *
1713 * Side effects : SCSI things happen, the disconnected queue will be
1714 * modified if a command disconnects, *instance->connected will
1715 * change.
1716 *
1717 * XXX Note : we need to watch for bus free or a reset condition here
1718 * to recover from an unexpected bus free condition.
1719 *
1720 * Locks: io_request_lock held by caller in IRQ mode
1721 */
1722
1723static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11001724 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 unsigned char msgout = NOP;
1726 int sink = 0;
1727 int len;
1728#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1729 int transfersize;
1730#endif
1731 unsigned char *data;
1732 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001733 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Finn Thain11d2f632016-01-03 16:05:51 +11001735 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001736 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 tmp = NCR5380_read(STATUS_REG);
1739 /* We only have a valid SCSI phase when REQ is asserted */
1740 if (tmp & SR_REQ) {
1741 phase = (tmp & PHASE_MASK);
1742 if (phase != old_phase) {
1743 old_phase = phase;
1744 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1745 }
1746 if (sink && (phase != PHASE_MSGOUT)) {
1747 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1748
1749 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1750 while (NCR5380_read(STATUS_REG) & SR_REQ);
1751 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1752 sink = 0;
1753 continue;
1754 }
1755 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 case PHASE_DATAOUT:
1757#if (NDEBUG & NDEBUG_NO_DATAOUT)
1758 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1759 sink = 1;
1760 do_abort(instance);
1761 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001762 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return;
1764#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001765 case PHASE_DATAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /*
1767 * If there is no room left in the current buffer in the
1768 * scatter-gather list, move onto the next one.
1769 */
1770
1771 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1772 ++cmd->SCp.buffer;
1773 --cmd->SCp.buffers_residual;
1774 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001775 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001776 dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 /*
1779 * The preferred transfer method is going to be
1780 * PSEUDO-DMA for systems that are strictly PIO,
1781 * since we can let the hardware do the handshaking.
1782 *
1783 * For this to work, we need to know the transfersize
1784 * ahead of time, since the pseudo-DMA code will sit
1785 * in an unconditional loop.
1786 */
1787
1788#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001789 transfersize = 0;
1790 if (!cmd->device->borken &&
1791 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1792 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Finn Thainff3d4572016-01-03 16:05:25 +11001794 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 len = transfersize;
1796 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1797 /*
1798 * If the watchdog timer fires, all future accesses to this
1799 * device will use the polled-IO.
1800 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001801 scmd_printk(KERN_INFO, cmd,
1802 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 sink = 1;
1805 do_abort(instance);
1806 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001807 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* XXX - need to source or sink data here, as appropriate */
1809 } else
1810 cmd->SCp.this_residual -= transfersize - len;
1811 } else
1812#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
Finn Thain11d2f632016-01-03 16:05:51 +11001813 {
1814 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1816 &cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11001817 spin_lock_irq(&hostdata->lock);
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 break;
1820 case PHASE_MSGIN:
1821 len = 1;
1822 data = &tmp;
1823 NCR5380_transfer_pio(instance, &phase, &len, &data);
1824 cmd->SCp.Message = tmp;
1825
1826 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 case ABORT:
1828 case COMMAND_COMPLETE:
1829 /* Accept message by clearing ACK */
1830 sink = 1;
1831 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001832 dsprintk(NDEBUG_QUEUES, instance,
1833 "COMMAND COMPLETE %p target %d lun %llu\n",
1834 cmd, scmd_id(cmd), cmd->device->lun);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001837 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 /*
1840 * I'm not sure what the correct thing to do here is :
1841 *
1842 * If the command that just executed is NOT a request
1843 * sense, the obvious thing to do is to set the result
1844 * code to the values of the stored parameters.
1845 *
1846 * If it was a REQUEST SENSE command, we need some way
1847 * to differentiate between the failure code of the original
1848 * and the failure code of the REQUEST sense - the obvious
1849 * case is success, where we fall through and leave the result
1850 * code unchanged.
1851 *
1852 * The non-obvious place is where the REQUEST SENSE failed
1853 */
1854
1855 if (cmd->cmnd[0] != REQUEST_SENSE)
1856 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1857 else if (status_byte(cmd->SCp.Status) != GOOD)
1858 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1859
Boaz Harrosh28424d32007-09-10 22:37:45 +03001860 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1861 hostdata->ses.cmd_len) {
1862 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1863 hostdata->ses.cmd_len = 0 ;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Boaz Harrosh28424d32007-09-10 22:37:45 +03001866 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1867 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1868
Finn Thain32b26a12016-01-03 16:05:58 +11001869 list_add(&ncmd->list, &hostdata->unissued);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001870 dsprintk(NDEBUG_AUTOSENSE | NDEBUG_QUEUES,
1871 instance, "REQUEST SENSE cmd %p added to head of issue queue\n",
Finn Thaindbb6b352016-01-03 16:05:53 +11001872 cmd);
Finn Thain997acab2014-11-12 16:11:54 +11001873 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 cmd->scsi_done(cmd);
1875 }
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /*
1878 * Restore phase bits to 0 so an interrupted selection,
1879 * arbitration can resume.
1880 */
1881 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001882
1883 /* Enable reselect interrupts */
1884 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return;
1886 case MESSAGE_REJECT:
1887 /* Accept message by clearing ACK */
1888 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1889 switch (hostdata->last_message) {
1890 case HEAD_OF_QUEUE_TAG:
1891 case ORDERED_QUEUE_TAG:
1892 case SIMPLE_QUEUE_TAG:
1893 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001894 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 break;
1896 default:
1897 break;
1898 }
Finn Thain340b9612016-01-03 16:05:31 +11001899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 case DISCONNECT:{
1901 /* Accept message by clearing ACK */
1902 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11001904 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001905 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1906 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1907 cmd, scmd_id(cmd), cmd->device->lun);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
1910 * Restore phase bits to 0 so an interrupted selection,
1911 * arbitration can resume.
1912 */
1913 NCR5380_write(TARGET_COMMAND_REG, 0);
1914
1915 /* Enable reselect interrupts */
1916 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return;
1918 }
1919 /*
1920 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1921 * operation, in violation of the SCSI spec so we can safely
1922 * ignore SAVE/RESTORE pointers calls.
1923 *
1924 * Unfortunately, some disks violate the SCSI spec and
1925 * don't issue the required SAVE_POINTERS message before
1926 * disconnecting, and we have to break spec to remain
1927 * compatible.
1928 */
1929 case SAVE_POINTERS:
1930 case RESTORE_POINTERS:
1931 /* Accept message by clearing ACK */
1932 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1933 break;
1934 case EXTENDED_MESSAGE:
1935/*
1936 * Extended messages are sent in the following format :
1937 * Byte
1938 * 0 EXTENDED_MESSAGE == 1
1939 * 1 length (includes one byte for code, doesn't
1940 * include first two bytes)
1941 * 2 code
1942 * 3..length+1 arguments
1943 *
1944 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001945 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
1947 extended_msg[0] = EXTENDED_MESSAGE;
1948 /* Accept first byte by clearing ACK */
1949 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001950
1951 spin_unlock_irq(&hostdata->lock);
1952
Finn Thain52a6a1c2014-03-18 11:42:18 +11001953 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 len = 2;
1956 data = extended_msg + 1;
1957 phase = PHASE_MSGIN;
1958 NCR5380_transfer_pio(instance, &phase, &len, &data);
1959
Finn Thain52a6a1c2014-03-18 11:42:18 +11001960 dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Finn Thaine0783ed2016-01-03 16:05:45 +11001962 if (!len && extended_msg[1] > 0 &&
1963 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* Accept third byte by clearing ACK */
1965 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1966 len = extended_msg[1] - 1;
1967 data = extended_msg + 3;
1968 phase = PHASE_MSGIN;
1969
1970 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001971 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 switch (extended_msg[2]) {
1974 case EXTENDED_SDTR:
1975 case EXTENDED_WDTR:
1976 case EXTENDED_MODIFY_DATA_POINTER:
1977 case EXTENDED_EXTENDED_IDENTIFY:
1978 tmp = 0;
1979 }
1980 } else if (len) {
1981 printk("scsi%d: error receiving extended message\n", instance->host_no);
1982 tmp = 0;
1983 } else {
1984 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
1985 tmp = 0;
1986 }
Finn Thain11d2f632016-01-03 16:05:51 +11001987
1988 spin_lock_irq(&hostdata->lock);
1989 if (!hostdata->connected)
1990 return;
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /* Fall through to reject message */
1993
1994 /*
1995 * If we get something weird that we aren't expecting,
1996 * reject it.
1997 */
1998 default:
1999 if (!tmp) {
2000 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002001 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 printk("\n");
2003 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002004 scmd_printk(KERN_INFO, cmd,
2005 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002007 scmd_printk(KERN_INFO, cmd,
2008 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 msgout = MESSAGE_REJECT;
2011 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2012 break;
2013 } /* switch (tmp) */
2014 break;
2015 case PHASE_MSGOUT:
2016 len = 1;
2017 data = &msgout;
2018 hostdata->last_message = msgout;
2019 NCR5380_transfer_pio(instance, &phase, &len, &data);
2020 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002021 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 hostdata->connected = NULL;
2023 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 cmd->scsi_done(cmd);
2025 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2026 return;
2027 }
2028 msgout = NOP;
2029 break;
2030 case PHASE_CMDOUT:
2031 len = cmd->cmd_len;
2032 data = cmd->cmnd;
2033 /*
2034 * XXX for performance reasons, on machines with a
2035 * PSEUDO-DMA architecture we should probably
2036 * use the dma transfer function.
2037 */
2038 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 break;
2040 case PHASE_STATIN:
2041 len = 1;
2042 data = &tmp;
2043 NCR5380_transfer_pio(instance, &phase, &len, &data);
2044 cmd->SCp.Status = tmp;
2045 break;
2046 default:
2047 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002048 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002050 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002051 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002052 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002053 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
Finn Thain11d2f632016-01-03 16:05:51 +11002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058/*
2059 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2060 *
2061 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002062 * 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 -07002063 * nexus has been reestablished,
2064 *
2065 * Inputs : instance - this instance of the NCR5380.
2066 *
2067 * Locks: io_request_lock held by caller if IRQ driven
2068 */
2069
2070static void NCR5380_reselect(struct Scsi_Host *instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11002071 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 unsigned char target_mask;
2073 unsigned char lun, phase;
2074 int len;
2075 unsigned char msg[3];
2076 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11002077 struct NCR5380_cmd *ncmd;
2078 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /*
2081 * Disable arbitration, etc. since the host adapter obviously
2082 * lost, and tell an interrupted NCR5380_select() to restart.
2083 */
2084
2085 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain72064a72016-01-03 16:05:44 +11002088 dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /*
2091 * At this point, we have detected that our SCSI ID is on the bus,
2092 * SEL is true and BSY was false for at least one bus settle delay
2093 * (400 ns).
2094 *
2095 * We must assert BSY ourselves, until the target drops the SEL
2096 * signal.
2097 */
2098
2099 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002100 if (NCR5380_poll_politely(instance,
2101 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2102 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2103 return;
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2106
2107 /*
2108 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
2110
Finn Thain1cc160e2016-01-03 16:05:32 +11002111 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002112 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2113 do_abort(instance);
2114 return;
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 len = 1;
2118 data = msg;
2119 phase = PHASE_MSGIN;
2120 NCR5380_transfer_pio(instance, &phase, &len, &data);
2121
Finn Thain72064a72016-01-03 16:05:44 +11002122 if (len) {
2123 do_abort(instance);
2124 return;
2125 }
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002128 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002129 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002130 printk("\n");
2131 do_abort(instance);
2132 return;
2133 }
2134 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Finn Thain72064a72016-01-03 16:05:44 +11002136 /*
2137 * We need to add code for SCSI-II to track which devices have
2138 * I_T_L_Q nexuses established, and which have simple I_T_L
2139 * nexuses so we can chose to do additional data transfer.
2140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Finn Thain72064a72016-01-03 16:05:44 +11002142 /*
2143 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2144 * just reestablished, and remove it from the disconnected queue.
2145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Finn Thain32b26a12016-01-03 16:05:58 +11002147 tmp = NULL;
2148 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2149 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2150
2151 if (target_mask == (1 << scmd_id(cmd)) &&
2152 lun == (u8)cmd->device->lun) {
2153 list_del(&ncmd->list);
2154 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002158
2159 if (tmp) {
2160 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2161 "reselect: removed %p from disconnected queue\n", tmp);
2162 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002163 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2164 target_mask, lun);
2165 /*
2166 * Since we have an established nexus that we can't do anything with,
2167 * we must abort it.
2168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
Finn Thain72064a72016-01-03 16:05:44 +11002172
2173 /* Accept message by clearing ACK */
2174 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2175
2176 hostdata->connected = tmp;
2177 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n",
2178 instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181/*
2182 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2183 *
2184 * Purpose : called by interrupt handler when DMA finishes or a phase
2185 * mismatch occurs (which would finish the DMA transfer).
2186 *
2187 * Inputs : instance - this instance of the NCR5380.
2188 *
Finn Thain710ddd02014-11-12 16:12:02 +11002189 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * nexus has been reestablished, on failure NULL is returned.
2191 */
2192
2193#ifdef REAL_DMA
2194static void NCR5380_dma_complete(NCR5380_instance * instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11002195 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /*
2199 * XXX this might not be right.
2200 *
2201 * Wait for final byte to transfer, ie wait for ACK to go false.
2202 *
2203 * We should use the Last Byte Sent bit, unfortunately this is
2204 * not available on the 5380/5381 (only the various CMOS chips)
2205 *
2206 * FIXME: timeout, and need to handle long timeout/irq case
2207 */
2208
2209 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2212
2213 /*
2214 * The only places we should see a phase mismatch and have to send
2215 * data from the same set of pointers will be the data transfer
2216 * phases. So, residual, requested length are only important here.
2217 */
2218
2219 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2220 transferred = instance->dmalen - NCR5380_dma_residual();
2221 hostdata->connected->SCp.this_residual -= transferred;
2222 hostdata->connected->SCp.ptr += transferred;
2223 }
2224}
2225#endif /* def REAL_DMA */
2226
2227/*
Finn Thain710ddd02014-11-12 16:12:02 +11002228 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 *
2230 * Purpose : abort a command
2231 *
Finn Thain710ddd02014-11-12 16:12:02 +11002232 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002233 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 * used.
2235 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002236 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002238 * XXX - there is no way to abort the command that is currently
2239 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 * a problem, we could implement longjmp() / setjmp(), setjmp()
2241 * called where the loop started in NCR5380_main().
2242 *
2243 * Locks: host lock taken by caller
2244 */
2245
Finn Thain710ddd02014-11-12 16:12:02 +11002246static int NCR5380_abort(struct scsi_cmnd *cmd)
2247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002249 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002250 unsigned long flags;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002251
Finn Thain11d2f632016-01-03 16:05:51 +11002252 spin_lock_irqsave(&hostdata->lock, flags);
2253
Finn Thain32b26a12016-01-03 16:05:58 +11002254#if (NDEBUG & NDEBUG_ANY)
2255 scmd_printk(KERN_INFO, cmd, "aborting command\n");
2256#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002257 NCR5380_dprint(NDEBUG_ANY, instance);
2258 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Finn Thain11d2f632016-01-03 16:05:51 +11002260 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return FAILED;
2263}
2264
2265
Finn Thain3be1b3e2016-01-03 16:05:12 +11002266/**
2267 * NCR5380_bus_reset - reset the SCSI bus
2268 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002270 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
2272
Finn Thain710ddd02014-11-12 16:12:02 +11002273static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002274{
2275 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002276 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2277 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Finn Thain11d2f632016-01-03 16:05:51 +11002279 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002280
2281#if (NDEBUG & NDEBUG_ANY)
2282 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
Finn Thain3be1b3e2016-01-03 16:05:12 +11002283#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002284 NCR5380_dprint(NDEBUG_ANY, instance);
2285 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002286
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002287 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002288
Finn Thain11d2f632016-01-03 16:05:51 +11002289 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return SUCCESS;
2292}