blob: 08319164f0121613a1b1f3ca6cf12385717324a1 [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 Thain677e0192016-01-03 16:05:59 +1100729 * complete_cmd - finish processing a command and return it to the SCSI ML
730 * @instance: the host instance
731 * @cmd: command to complete
732 */
733
734static void complete_cmd(struct Scsi_Host *instance,
735 struct scsi_cmnd *cmd)
736{
737 struct NCR5380_hostdata *hostdata = shost_priv(instance);
738
739 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
740
741 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
742
743 cmd->scsi_done(cmd);
744}
745
746/**
Finn Thain1bb40582016-01-03 16:05:29 +1100747 * NCR5380_queue_command - queue a command
748 * @instance: the relevant SCSI adapter
749 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 *
Finn Thain1bb40582016-01-03 16:05:29 +1100751 * cmd is added to the per-instance issue queue, with minor
752 * twiddling done to the host specific fields of cmd. If the
753 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
755
Finn Thain1bb40582016-01-03 16:05:29 +1100756static int NCR5380_queue_command(struct Scsi_Host *instance,
757 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Finn Thain1bb40582016-01-03 16:05:29 +1100759 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100760 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100761 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763#if (NDEBUG & NDEBUG_NO_WRITE)
764 switch (cmd->cmnd[0]) {
765 case WRITE_6:
766 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100767 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100769 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771 }
772#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 cmd->result = 0;
775
Finn Thain11d2f632016-01-03 16:05:51 +1100776 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Insert the cmd into the issue queue. Note that REQUEST SENSE
780 * commands are added to the head of the queue since any command will
781 * clear the contingent allegiance condition that exists and the
782 * sense data is only guaranteed to be valid while the condition exists.
783 */
784
Finn Thain32b26a12016-01-03 16:05:58 +1100785 if (cmd->cmnd[0] == REQUEST_SENSE)
786 list_add(&ncmd->list, &hostdata->unissued);
787 else
788 list_add_tail(&ncmd->list, &hostdata->unissued);
789
Finn Thain11d2f632016-01-03 16:05:51 +1100790 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100791
Finn Thaindbb6b352016-01-03 16:05:53 +1100792 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
793 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100796 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
801 * NCR5380_main - NCR state machines
802 *
803 * NCR5380_main is a coroutine that runs as long as more work can
804 * be done on the NCR5380 host adapters in a system. Both
805 * NCR5380_queue_command() and NCR5380_intr() will try to start it
806 * in case it is not running.
807 *
808 * Locks: called as its own thread with no locks held. Takes the
809 * host lock and called routines may take the isa dma lock.
810 */
811
David Howellsc4028952006-11-22 14:57:56 +0000812static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
David Howellsc4028952006-11-22 14:57:56 +0000814 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100815 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct Scsi_Host *instance = hostdata->host;
Finn Thain32b26a12016-01-03 16:05:58 +1100817 struct NCR5380_cmd *ncmd, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int done;
819
Finn Thain11d2f632016-01-03 16:05:51 +1100820 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100823
Finn Thainae753a32016-01-03 16:05:23 +1100824 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100825 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Search through the issue_queue for a command destined
828 * for a target that's not busy.
829 */
Finn Thain32b26a12016-01-03 16:05:58 +1100830 list_for_each_entry_safe(ncmd, n, &hostdata->unissued,
831 list) {
832 struct scsi_cmnd *tmp = NCR5380_to_scmd(ncmd);
833
Finn Thain0d3d9a42016-01-03 16:05:55 +1100834 dsprintk(NDEBUG_QUEUES, instance, "main: tmp=%p target=%d busy=%d lun=%llu\n",
835 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
836 tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200838 if (!(hostdata->busy[tmp->device->id] &
839 (1 << (u8)(tmp->device->lun & 0xff)))) {
Finn Thain32b26a12016-01-03 16:05:58 +1100840 list_del(&ncmd->list);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100841 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES,
Finn Thain32b26a12016-01-03 16:05:58 +1100842 instance, "main: removed %p from issue queue\n",
843 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /*
846 * Attempt to establish an I_T_L nexus here.
847 * On success, instance->hostdata->connected is set.
848 * On failure, we must add the command back to the
849 * issue queue so we can keep trying.
850 */
Finn Thain76f13b92014-11-12 16:11:53 +1100851 /*
852 * REQUEST SENSE commands are issued without tagged
853 * queueing, even on SCSI-II devices because the
854 * contingent allegiance condition exists for the
855 * entire unit.
856 */
857
858 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100859 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100861 /* Need to retry */
Finn Thain32b26a12016-01-03 16:05:58 +1100862 list_add(&ncmd->list, &hostdata->unissued);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100863 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES,
864 instance, "main: select() failed, %p returned to issue queue\n",
865 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Finn Thainae753a32016-01-03 16:05:23 +1100868 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100869 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 } /* if target/lun is not busy */
871 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } /* if (!hostdata->connected) */
Finn Thain11d2f632016-01-03 16:05:51 +1100873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (hostdata->connected
875#ifdef REAL_DMA
876 && !hostdata->dmalen
877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100879 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100881 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } while (!done);
Finn Thain11d2f632016-01-03 16:05:51 +1100885 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888#ifndef DONT_USE_INTR
889
890/**
Finn Thaincd400822016-01-03 16:05:40 +1100891 * NCR5380_intr - generic NCR5380 irq handler
892 * @irq: interrupt number
893 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
Finn Thaincd400822016-01-03 16:05:40 +1100895 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
896 * from the disconnected queue, and restarting NCR5380_main()
897 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
Finn Thaincd400822016-01-03 16:05:40 +1100899 * The chip can assert IRQ in any of six different conditions. The IRQ flag
900 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
901 * Three of these six conditions are latched in the Bus and Status Register:
902 * - End of DMA (cleared by ending DMA Mode)
903 * - Parity error (cleared by reading RPIR)
904 * - Loss of BSY (cleared by reading RPIR)
905 * Two conditions have flag bits that are not latched:
906 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
907 * - Bus reset (non-maskable)
908 * The remaining condition has no flag bit at all:
909 * - Selection/reselection
910 *
911 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
912 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
913 * claimed that "the design of the [DP8490] interrupt logic ensures
914 * interrupts will not be lost (they can be on the DP5380)."
915 * The L5380/53C80 datasheet from LOGIC Devices has more details.
916 *
917 * Checking for bus reset by reading RST is futile because of interrupt
918 * latency, but a bus reset will reset chip logic. Checking for parity error
919 * is unnecessary because that interrupt is never enabled. A Loss of BSY
920 * condition will clear DMA Mode. We can tell when this occurs because the
921 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923
Finn Thaincd400822016-01-03 16:05:40 +1100924static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800926 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100927 struct NCR5380_hostdata *hostdata = shost_priv(instance);
928 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned char basr;
930 unsigned long flags;
931
Finn Thain11d2f632016-01-03 16:05:51 +1100932 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Finn Thaincd400822016-01-03 16:05:40 +1100934 basr = NCR5380_read(BUS_AND_STATUS_REG);
935 if (basr & BASR_IRQ) {
936 unsigned char mr = NCR5380_read(MODE_REG);
937 unsigned char sr = NCR5380_read(STATUS_REG);
938
939 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
940 instance->host_no, irq, basr, sr, mr);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +1100943 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
944 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
945 * We ack IRQ after clearing Mode Register. Workarounds
946 * for End of DMA errata need to happen in DMA Mode.
947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Finn Thaincd400822016-01-03 16:05:40 +1100949 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Finn Thaincd400822016-01-03 16:05:40 +1100951 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Finn Thaincd400822016-01-03 16:05:40 +1100953 if (!hostdata->connected)
954 panic("scsi%d : DMA interrupt with no connected cmd\n",
955 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Finn Thaincd400822016-01-03 16:05:40 +1100957 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
958 hostdata->connected->SCp.this_residual -= transferred;
959 hostdata->connected->SCp.ptr += transferred;
960 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Finn Thaincd400822016-01-03 16:05:40 +1100962 /* FIXME: we need to poll briefly then defer a workqueue task ! */
963 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
964
965 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
966 NCR5380_write(MODE_REG, MR_BASE);
967 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
968 } else
969#endif /* REAL_DMA */
970 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
971 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
972 /* Probably reselected */
973 NCR5380_write(SELECT_ENABLE_REG, 0);
974 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
975
976 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
977 instance->host_no);
978
979 if (!hostdata->connected) {
980 NCR5380_reselect(instance);
981 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Finn Thaincd400822016-01-03 16:05:40 +1100983 if (!hostdata->connected)
984 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
985 } else {
986 /* Probably Bus Reset */
987 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
988
989 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
990 }
991 handled = 1;
992 } else {
993 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
994 }
995
Finn Thain11d2f632016-01-03 16:05:51 +1100996 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100997
998 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001#endif
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
Finn Thain710ddd02014-11-12 16:12:02 +11001004 * Function : int NCR5380_select(struct Scsi_Host *instance,
1005 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 *
1007 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1008 * including ARBITRATION, SELECTION, and initial message out for
1009 * IDENTIFY and queue messages.
1010 *
1011 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001012 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
Finn Thain63238762016-01-03 16:05:15 +11001014 * Returns : -1 if selection failed but should be retried.
1015 * 0 if selection failed and should not be retried.
1016 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 *
1018 * Side effects :
1019 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1020 * with registers as they should have been on entry - ie
1021 * SELECT_ENABLE will be set appropriately, the NCR5380
1022 * will cease to drive any SCSI bus signals.
1023 *
1024 * If successful : I_T_L or I_T_L_Q nexus will be established,
1025 * instance->connected will be set to cmd.
1026 * SELECT interrupt will be disabled.
1027 *
1028 * If failed (no target) : cmd->scsi_done() will be called, and the
1029 * cmd->result host byte set to DID_BAD_TARGET.
1030 *
1031 * Locks: caller holds hostdata lock in IRQ mode
1032 */
1033
Finn Thain710ddd02014-11-12 16:12:02 +11001034static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Finn Thaine8a60142016-01-03 16:05:54 +11001036 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 unsigned char tmp[3], phase;
1038 unsigned char *data;
1039 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001043 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
1046 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1047 * data bus during SELECTION.
1048 */
1049
1050 NCR5380_write(TARGET_COMMAND_REG, 0);
1051
1052 /*
1053 * Start arbitration.
1054 */
1055
1056 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1057 NCR5380_write(MODE_REG, MR_ARBITRATE);
1058
Finn Thain55500d92016-01-03 16:05:35 +11001059 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1060 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
1062
Finn Thain11d2f632016-01-03 16:05:51 +11001063 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001064 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1065 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1066 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001067 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001068 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1069 /* Reselection interrupt */
1070 return -1;
1071 }
1072 if (err < 0) {
1073 NCR5380_write(MODE_REG, MR_BASE);
1074 shost_printk(KERN_ERR, instance,
1075 "select: arbitration timeout\n");
1076 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001077 }
Finn Thain11d2f632016-01-03 16:05:51 +11001078 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001079
1080 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 udelay(3);
1082
1083 /* Check for lost arbitration */
1084 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)) {
1085 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001086 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain11d2f632016-01-03 16:05:51 +11001087 spin_lock_irq(&hostdata->lock);
Finn Thain63238762016-01-03 16:05:15 +11001088 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Finn Thaincf13b082016-01-03 16:05:18 +11001090
1091 /* After/during arbitration, BSY should be asserted.
1092 * IBM DPES-31080 Version S31Q works now
1093 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1094 */
1095 NCR5380_write(INITIATOR_COMMAND_REG,
1096 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
1099 * Again, bus clear + bus settle time is 1.2us, however, this is
1100 * a minimum so we'll udelay ceil(1.2)
1101 */
1102
Finn Thain9c3f0e22016-01-03 16:05:11 +11001103 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1104 udelay(15);
1105 else
1106 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Finn Thain11d2f632016-01-03 16:05:51 +11001108 spin_lock_irq(&hostdata->lock);
1109
Finn Thain72064a72016-01-03 16:05:44 +11001110 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1111 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1112 return -1;
1113
Finn Thain52a6a1c2014-03-18 11:42:18 +11001114 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /*
1117 * Now that we have won arbitration, start Selection process, asserting
1118 * the host and target ID's on the SCSI bus.
1119 */
1120
Jeff Garzik422c0d62005-10-24 18:05:09 -04001121 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * Raise ATN while SEL is true before BSY goes false from arbitration,
1125 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1126 * phase immediately after selection.
1127 */
1128
1129 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1130 NCR5380_write(MODE_REG, MR_BASE);
1131
1132 /*
1133 * Reselect interrupts must be turned off prior to the dropping of BSY,
1134 * otherwise we will trigger an interrupt.
1135 */
1136 NCR5380_write(SELECT_ENABLE_REG, 0);
1137
Finn Thain11d2f632016-01-03 16:05:51 +11001138 spin_unlock_irq(&hostdata->lock);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /*
1141 * The initiator shall then wait at least two deskew delays and release
1142 * the BSY signal.
1143 */
1144 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1145
1146 /* Reset BSY */
1147 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1148
1149 /*
1150 * Something weird happens when we cease to drive BSY - looks
1151 * like the board/chip is letting us do another read before the
1152 * appropriate propagation delay has expired, and we're confusing
1153 * a BSY signal from ourselves as the target's response to SELECTION.
1154 *
1155 * A small delay (the 'C++' frontend breaks the pipeline with an
1156 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1157 * tighter 'C' code breaks and requires this) solves the problem -
1158 * the 1 us delay is arbitrary, and only used because this delay will
1159 * be the same on other platforms and since it works here, it should
1160 * work there.
1161 *
1162 * wingel suggests that this could be due to failing to wait
1163 * one deskew delay.
1164 */
1165
1166 udelay(1);
1167
Finn Thain52a6a1c2014-03-18 11:42:18 +11001168 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * The SCSI specification calls for a 250 ms timeout for the actual
1172 * selection.
1173 */
1174
Finn Thainae753a32016-01-03 16:05:23 +11001175 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1176 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001179 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1181 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001182 if (!hostdata->connected)
1183 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -1;
1186 }
Finn Thainae753a32016-01-03 16:05:23 +11001187
1188 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001189 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001190 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1191 cmd->result = DID_BAD_TARGET << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001192 complete_cmd(instance, cmd);
Finn Thainae753a32016-01-03 16:05:23 +11001193 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1194 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1195 instance->host_no);
1196 return 0;
1197 }
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * No less than two deskew delays after the initiator detects the
1201 * BSY signal is true, it shall release the SEL signal and may
1202 * change the DATA BUS. -wingel
1203 */
1204
1205 udelay(1);
1206
1207 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * Since we followed the SCSI spec, and raised ATN while SEL
1211 * was true but before BSY was false during selection, the information
1212 * transfer phase should be a MESSAGE OUT phase so that we can send the
1213 * IDENTIFY message.
1214 *
1215 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1216 * message (2 bytes) with a tag ID that we increment with every command
1217 * until it wraps back to 0.
1218 *
1219 * XXX - it turns out that there are some broken SCSI-II devices,
1220 * which claim to support tagged queuing but fail when more than
1221 * some number of commands are issued at once.
1222 */
1223
1224 /* Wait for start of REQ/ACK handshake */
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001227 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001228 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001229 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1230 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001232 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
Finn Thain52a6a1c2014-03-18 11:42:18 +11001235 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 +11001236 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 len = 1;
1239 cmd->tag = 0;
1240
1241 /* Send message(s) */
1242 data = tmp;
1243 phase = PHASE_MSGOUT;
1244 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001245 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001249 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Boaz Harrosh28424d32007-09-10 22:37:45 +03001251 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256/*
1257 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1258 * unsigned char *phase, int *count, unsigned char **data)
1259 *
1260 * Purpose : transfers data in given phase using polled I/O
1261 *
1262 * Inputs : instance - instance of driver, *phase - pointer to
1263 * what phase is expected, *count - pointer to number of
1264 * bytes to transfer, **data - pointer to data pointer.
1265 *
1266 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001267 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * is in same phase.
1269 *
1270 * Also, *phase, *count, *data are modified in place.
1271 *
1272 * XXX Note : handling for bus free may be useful.
1273 */
1274
1275/*
1276 * Note : this code is not as quick as it could be, however it
1277 * IS 100% reliable, and for the actual data transfer where speed
1278 * counts, we will always do a pseudo DMA or DMA transfer.
1279 */
1280
1281static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 unsigned char p = *phase, tmp;
1283 int c = *count;
1284 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001287 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001289 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /*
1292 * The NCR5380 chip will only drive the SCSI bus when the
1293 * phase specified in the appropriate bits of the TARGET COMMAND
1294 * REGISTER match the STATUS REGISTER
1295 */
1296
1297 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 do {
1300 /*
1301 * Wait for assertion of REQ, after which the phase bits will be
1302 * valid
1303 */
1304
Finn Thain686f3992016-01-03 16:05:26 +11001305 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Finn Thain52a6a1c2014-03-18 11:42:18 +11001308 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001311 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001312 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1314 break;
1315 }
1316 /* Do actual transfer from SCSI bus to / from memory */
1317 if (!(p & SR_IO))
1318 NCR5380_write(OUTPUT_DATA_REG, *d);
1319 else
1320 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1321
1322 ++d;
1323
1324 /*
1325 * The SCSI standard suggests that in MSGOUT phase, the initiator
1326 * should drop ATN on the last byte of the message phase
1327 * after REQ has been asserted for the handshake but before
1328 * the initiator raises ACK.
1329 */
1330
1331 if (!(p & SR_IO)) {
1332 if (!((p & SR_MSG) && c > 1)) {
1333 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1334 NCR5380_dprint(NDEBUG_PIO, instance);
1335 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1336 } else {
1337 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1338 NCR5380_dprint(NDEBUG_PIO, instance);
1339 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1340 }
1341 } else {
1342 NCR5380_dprint(NDEBUG_PIO, instance);
1343 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1344 }
1345
Finn Thaina2edc4a2016-01-03 16:05:27 +11001346 if (NCR5380_poll_politely(instance,
1347 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1348 break;
1349
Finn Thain52a6a1c2014-03-18 11:42:18 +11001350 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352/*
1353 * We have several special cases to consider during REQ/ACK handshaking :
1354 * 1. We were in MSGOUT phase, and we are on the last byte of the
1355 * message. ATN must be dropped as ACK is dropped.
1356 *
1357 * 2. We are in a MSGIN phase, and we are on the last byte of the
1358 * message. We must exit with ACK asserted, so that the calling
1359 * code may raise ATN before dropping ACK to reject the message.
1360 *
1361 * 3. ACK and ATN are clear and the target may proceed as normal.
1362 */
1363 if (!(p == PHASE_MSGIN && c == 1)) {
1364 if (p == PHASE_MSGOUT && c > 1)
1365 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1366 else
1367 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1368 }
1369 } while (--c);
1370
Finn Thain52a6a1c2014-03-18 11:42:18 +11001371 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 *count = c;
1374 *data = d;
1375 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001376 /* The phase read from the bus is valid if either REQ is (already)
1377 * asserted or if ACK hasn't been released yet. The latter applies if
1378 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1379 */
1380 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 *phase = tmp & PHASE_MASK;
1382 else
1383 *phase = PHASE_UNKNOWN;
1384
1385 if (!c || (*phase == p))
1386 return 0;
1387 else
1388 return -1;
1389}
1390
1391/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001392 * do_reset - issue a reset command
1393 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001395 * Issue a reset sequence to the NCR5380 and try and get the bus
1396 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001398 * This clears the reset interrupt flag because there may be no handler for
1399 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1400 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
1402
Finn Thain54d8fe42016-01-03 16:05:06 +11001403static void do_reset(struct Scsi_Host *instance)
1404{
Finn Thain636b1ec2016-01-03 16:05:10 +11001405 unsigned long flags;
1406
1407 local_irq_save(flags);
1408 NCR5380_write(TARGET_COMMAND_REG,
1409 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001411 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001413 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1414 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Finn Thain80d3eb62016-01-03 16:05:34 +11001417/**
1418 * do_abort - abort the currently established nexus by going to
1419 * MESSAGE OUT phase and sending an ABORT message.
1420 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001422 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424
Finn Thain54d8fe42016-01-03 16:05:06 +11001425static int do_abort(struct Scsi_Host *instance)
1426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 unsigned char *msgptr, phase, tmp;
1428 int len;
1429 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* Request message out phase */
1432 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1433
1434 /*
1435 * Wait for the target to indicate a valid phase by asserting
1436 * REQ. Once this happens, we'll have either a MSGOUT phase
1437 * and can immediately send the ABORT message, or we'll have some
1438 * other phase and will have to source/sink data.
1439 *
1440 * We really don't care what value was on the bus or what value
1441 * the target sees, so we just handshake.
1442 */
1443
Finn Thain80d3eb62016-01-03 16:05:34 +11001444 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001445 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001446 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Finn Thainf35d3472016-01-03 16:05:33 +11001448 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1451
Finn Thainf35d3472016-01-03 16:05:33 +11001452 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001454 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001455 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001456 goto timeout;
1457 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 tmp = ABORT;
1460 msgptr = &tmp;
1461 len = 1;
1462 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001463 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /*
1466 * If we got here, and the command completed successfully,
1467 * we're about to go into bus free state.
1468 */
1469
1470 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001471
1472timeout:
1473 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1474 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1478/*
1479 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1480 * unsigned char *phase, int *count, unsigned char **data)
1481 *
1482 * Purpose : transfers data in given phase using either real
1483 * or pseudo DMA.
1484 *
1485 * Inputs : instance - instance of driver, *phase - pointer to
1486 * what phase is expected, *count - pointer to number of
1487 * bytes to transfer, **data - pointer to data pointer.
1488 *
1489 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001490 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * is in same phase.
1492 *
1493 * Also, *phase, *count, *data are modified in place.
1494 *
1495 * Locks: io_request lock held by caller
1496 */
1497
1498
1499static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 register int c = *count;
1501 register unsigned char p = *phase;
1502 register unsigned char *d = *data;
1503 unsigned char tmp;
1504 int foo;
1505#if defined(REAL_DMA_POLL)
1506 int cnt, toPIO;
1507 unsigned char saved_data = 0, overrun = 0, residue;
1508#endif
1509
Finn Thaine8a60142016-01-03 16:05:54 +11001510 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1513 *phase = tmp;
1514 return -1;
1515 }
1516#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001518 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
1519 c -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001521 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 -07001522 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1523#endif
1524
1525 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1526
1527#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001528 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1529 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001531 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532#else
1533 /*
1534 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1535 * were not disabled for the duration of a single DMA transfer, from
1536 * before the setting of DMA mode to after transfer of the last byte.
1537 */
1538
Finn Thain55181be2016-01-03 16:05:42 +11001539 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001540 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1541 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 else
Finn Thaincd400822016-01-03 16:05:40 +11001543 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544#endif /* def REAL_DMA */
1545
Finn Thain52a6a1c2014-03-18 11:42:18 +11001546 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /*
1549 * On the PAS16 at least I/O recovery delays are not needed here.
1550 * Everyone else seems to want them.
1551 */
1552
1553 if (p & SR_IO) {
1554 io_recovery_delay(1);
1555 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1556 } else {
1557 io_recovery_delay(1);
1558 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1559 io_recovery_delay(1);
1560 NCR5380_write(START_DMA_SEND_REG, 0);
1561 io_recovery_delay(1);
1562 }
1563
1564#if defined(REAL_DMA_POLL)
1565 do {
1566 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1567 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1568
1569/*
1570 At this point, either we've completed DMA, or we have a phase mismatch,
1571 or we've unexpectedly lost BUSY (which is a real error).
1572
1573 For write DMAs, we want to wait until the last byte has been
1574 transferred out over the bus before we turn off DMA mode. Alas, there
1575 seems to be no terribly good way of doing this on a 5380 under all
1576 conditions. For non-scatter-gather operations, we can wait until REQ
1577 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1578 are nastier, since the device will be expecting more data than we
1579 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1580 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1581 why this signal was added to the newer chips) but on the older 538[01]
1582 this signal does not exist. The workaround for this lack is a watchdog;
1583 we bail out of the wait-loop after a modest amount of wait-time if
1584 the usual exit conditions are not met. Not a terribly clean or
1585 correct solution :-%
1586
1587 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1588 If the chip is in DMA mode for an READ, it will respond to a target's
1589 REQ by latching the SCSI data into the INPUT DATA register and asserting
1590 ACK, even if it has _already_ been notified by the DMA controller that
1591 the current DMA transfer has completed! If the NCR5380 is then taken
1592 out of DMA mode, this already-acknowledged byte is lost.
1593
1594 This is not a problem for "one DMA transfer per command" reads, because
1595 the situation will never arise... either all of the data is DMA'ed
1596 properly, or the target switches to MESSAGE IN phase to signal a
1597 disconnection (either operation bringing the DMA to a clean halt).
1598 However, in order to handle scatter-reads, we must work around the
1599 problem. The chosen fix is to DMA N-2 bytes, then check for the
1600 condition before taking the NCR5380 out of DMA mode. One or two extra
1601 bytes are transferred via PIO as necessary to fill out the original
1602 request.
1603 */
1604
1605 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001606 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
1607 udelay(10);
1608 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1609 (BASR_PHASE_MATCH | BASR_ACK)) {
1610 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1611 overrun = 1;
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 } else {
1615 int limit = 100;
1616 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1617 if (!(tmp & BASR_PHASE_MATCH))
1618 break;
1619 if (--limit < 0)
1620 break;
1621 }
1622 }
1623
Finn Thain52a6a1c2014-03-18 11:42:18 +11001624 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 -07001625
1626 NCR5380_write(MODE_REG, MR_BASE);
1627 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1628
1629 residue = NCR5380_dma_residual(instance);
1630 c -= residue;
1631 *count -= c;
1632 *data += c;
1633 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1634
Finn Thain9db60242016-01-03 16:05:43 +11001635 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
1636 *phase == p && (p & SR_IO) && residue == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001638 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 **data = saved_data;
1640 *data += 1;
1641 *count -= 1;
1642 cnt = toPIO = 1;
1643 } else {
1644 printk("No overrun??\n");
1645 cnt = toPIO = 2;
1646 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001647 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 NCR5380_transfer_pio(instance, phase, &cnt, data);
1649 *count -= toPIO - cnt;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Finn Thain52a6a1c2014-03-18 11:42:18 +11001652 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 -07001653 return 0;
1654
1655#elif defined(REAL_DMA)
1656 return 0;
1657#else /* defined(REAL_DMA_POLL) */
1658 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001659 foo = NCR5380_pread(instance, d,
1660 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1661 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /*
1663 * We can't disable DMA mode after successfully transferring
1664 * what we plan to be the last byte, since that would open up
1665 * a race condition where if the target asserted REQ before
1666 * we got the DMA mode reset, the NCR5380 would have latched
1667 * an additional byte into the INPUT DATA register and we'd
1668 * have dropped it.
1669 *
1670 * The workaround was to transfer one fewer bytes than we
1671 * intended to with the pseudo-DMA read function, wait for
1672 * the chip to latch the last byte, read it, and then disable
1673 * pseudo-DMA mode.
1674 *
1675 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1676 * REQ is deasserted when ACK is asserted, and not reasserted
1677 * until ACK goes false. Since the NCR5380 won't lower ACK
1678 * until DACK is asserted, which won't happen unless we twiddle
1679 * the DMA port or we take the NCR5380 out of DMA mode, we
1680 * can guarantee that we won't handshake another extra
1681 * byte.
1682 */
1683
Finn Thain55181be2016-01-03 16:05:42 +11001684 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1685 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1686 foo = -1;
1687 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Finn Thain55181be2016-01-03 16:05:42 +11001689 if (NCR5380_poll_politely(instance, STATUS_REG,
1690 SR_REQ, 0, HZ) < 0) {
1691 foo = -1;
1692 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1693 }
1694 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001698 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * Wait for the last byte to be sent. If REQ is being asserted for
1701 * the byte we're interested, we'll ACK it and it will go false.
1702 */
Finn Thain55181be2016-01-03 16:05:42 +11001703 if (NCR5380_poll_politely2(instance,
1704 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1705 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1706 foo = -1;
1707 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711 NCR5380_write(MODE_REG, MR_BASE);
1712 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001713 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *data = d + c;
1715 *count = 0;
1716 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return foo;
1718#endif /* def REAL_DMA */
1719}
1720#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1721
1722/*
1723 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1724 *
1725 * Purpose : run through the various SCSI phases and do as the target
1726 * directs us to. Operates on the currently connected command,
1727 * instance->connected.
1728 *
1729 * Inputs : instance, instance for which we are doing commands
1730 *
1731 * Side effects : SCSI things happen, the disconnected queue will be
1732 * modified if a command disconnects, *instance->connected will
1733 * change.
1734 *
1735 * XXX Note : we need to watch for bus free or a reset condition here
1736 * to recover from an unexpected bus free condition.
1737 *
1738 * Locks: io_request_lock held by caller in IRQ mode
1739 */
1740
1741static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11001742 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 unsigned char msgout = NOP;
1744 int sink = 0;
1745 int len;
1746#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1747 int transfersize;
1748#endif
1749 unsigned char *data;
1750 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001751 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Finn Thain11d2f632016-01-03 16:05:51 +11001753 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001754 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 tmp = NCR5380_read(STATUS_REG);
1757 /* We only have a valid SCSI phase when REQ is asserted */
1758 if (tmp & SR_REQ) {
1759 phase = (tmp & PHASE_MASK);
1760 if (phase != old_phase) {
1761 old_phase = phase;
1762 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1763 }
1764 if (sink && (phase != PHASE_MSGOUT)) {
1765 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1766
1767 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1768 while (NCR5380_read(STATUS_REG) & SR_REQ);
1769 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1770 sink = 0;
1771 continue;
1772 }
1773 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 case PHASE_DATAOUT:
1775#if (NDEBUG & NDEBUG_NO_DATAOUT)
1776 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1777 sink = 1;
1778 do_abort(instance);
1779 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001780 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return;
1782#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001783 case PHASE_DATAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * If there is no room left in the current buffer in the
1786 * scatter-gather list, move onto the next one.
1787 */
1788
1789 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1790 ++cmd->SCp.buffer;
1791 --cmd->SCp.buffers_residual;
1792 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001793 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001794 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 -07001795 }
1796 /*
1797 * The preferred transfer method is going to be
1798 * PSEUDO-DMA for systems that are strictly PIO,
1799 * since we can let the hardware do the handshaking.
1800 *
1801 * For this to work, we need to know the transfersize
1802 * ahead of time, since the pseudo-DMA code will sit
1803 * in an unconditional loop.
1804 */
1805
1806#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001807 transfersize = 0;
1808 if (!cmd->device->borken &&
1809 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1810 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Finn Thainff3d4572016-01-03 16:05:25 +11001812 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 len = transfersize;
1814 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1815 /*
1816 * If the watchdog timer fires, all future accesses to this
1817 * device will use the polled-IO.
1818 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001819 scmd_printk(KERN_INFO, cmd,
1820 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sink = 1;
1823 do_abort(instance);
1824 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001825 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /* XXX - need to source or sink data here, as appropriate */
1827 } else
1828 cmd->SCp.this_residual -= transfersize - len;
1829 } else
1830#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
Finn Thain11d2f632016-01-03 16:05:51 +11001831 {
1832 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1834 &cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11001835 spin_lock_irq(&hostdata->lock);
1836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
1838 case PHASE_MSGIN:
1839 len = 1;
1840 data = &tmp;
1841 NCR5380_transfer_pio(instance, &phase, &len, &data);
1842 cmd->SCp.Message = tmp;
1843
1844 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 case ABORT:
1846 case COMMAND_COMPLETE:
1847 /* Accept message by clearing ACK */
1848 sink = 1;
1849 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001850 dsprintk(NDEBUG_QUEUES, instance,
1851 "COMMAND COMPLETE %p target %d lun %llu\n",
1852 cmd, scmd_id(cmd), cmd->device->lun);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /*
1857 * I'm not sure what the correct thing to do here is :
1858 *
1859 * If the command that just executed is NOT a request
1860 * sense, the obvious thing to do is to set the result
1861 * code to the values of the stored parameters.
1862 *
1863 * If it was a REQUEST SENSE command, we need some way
1864 * to differentiate between the failure code of the original
1865 * and the failure code of the REQUEST sense - the obvious
1866 * case is success, where we fall through and leave the result
1867 * code unchanged.
1868 *
1869 * The non-obvious place is where the REQUEST SENSE failed
1870 */
1871
1872 if (cmd->cmnd[0] != REQUEST_SENSE)
1873 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1874 else if (status_byte(cmd->SCp.Status) != GOOD)
1875 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1876
Boaz Harrosh28424d32007-09-10 22:37:45 +03001877 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1878 hostdata->ses.cmd_len) {
1879 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1880 hostdata->ses.cmd_len = 0 ;
1881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Boaz Harrosh28424d32007-09-10 22:37:45 +03001883 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1884 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1885
Finn Thain32b26a12016-01-03 16:05:58 +11001886 list_add(&ncmd->list, &hostdata->unissued);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001887 dsprintk(NDEBUG_AUTOSENSE | NDEBUG_QUEUES,
1888 instance, "REQUEST SENSE cmd %p added to head of issue queue\n",
Finn Thaindbb6b352016-01-03 16:05:53 +11001889 cmd);
Finn Thain677e0192016-01-03 16:05:59 +11001890 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Finn Thain997acab2014-11-12 16:11:54 +11001891 } else {
Finn Thain677e0192016-01-03 16:05:59 +11001892 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /*
1896 * Restore phase bits to 0 so an interrupted selection,
1897 * arbitration can resume.
1898 */
1899 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001900
1901 /* Enable reselect interrupts */
1902 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return;
1904 case MESSAGE_REJECT:
1905 /* Accept message by clearing ACK */
1906 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1907 switch (hostdata->last_message) {
1908 case HEAD_OF_QUEUE_TAG:
1909 case ORDERED_QUEUE_TAG:
1910 case SIMPLE_QUEUE_TAG:
1911 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001912 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914 default:
1915 break;
1916 }
Finn Thain340b9612016-01-03 16:05:31 +11001917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 case DISCONNECT:{
1919 /* Accept message by clearing ACK */
1920 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11001922 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001923 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1924 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1925 cmd, scmd_id(cmd), cmd->device->lun);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /*
1928 * Restore phase bits to 0 so an interrupted selection,
1929 * arbitration can resume.
1930 */
1931 NCR5380_write(TARGET_COMMAND_REG, 0);
1932
1933 /* Enable reselect interrupts */
1934 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return;
1936 }
1937 /*
1938 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1939 * operation, in violation of the SCSI spec so we can safely
1940 * ignore SAVE/RESTORE pointers calls.
1941 *
1942 * Unfortunately, some disks violate the SCSI spec and
1943 * don't issue the required SAVE_POINTERS message before
1944 * disconnecting, and we have to break spec to remain
1945 * compatible.
1946 */
1947 case SAVE_POINTERS:
1948 case RESTORE_POINTERS:
1949 /* Accept message by clearing ACK */
1950 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1951 break;
1952 case EXTENDED_MESSAGE:
1953/*
1954 * Extended messages are sent in the following format :
1955 * Byte
1956 * 0 EXTENDED_MESSAGE == 1
1957 * 1 length (includes one byte for code, doesn't
1958 * include first two bytes)
1959 * 2 code
1960 * 3..length+1 arguments
1961 *
1962 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001963 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965 extended_msg[0] = EXTENDED_MESSAGE;
1966 /* Accept first byte by clearing ACK */
1967 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001968
1969 spin_unlock_irq(&hostdata->lock);
1970
Finn Thain52a6a1c2014-03-18 11:42:18 +11001971 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 len = 2;
1974 data = extended_msg + 1;
1975 phase = PHASE_MSGIN;
1976 NCR5380_transfer_pio(instance, &phase, &len, &data);
1977
Finn Thain52a6a1c2014-03-18 11:42:18 +11001978 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 -07001979
Finn Thaine0783ed2016-01-03 16:05:45 +11001980 if (!len && extended_msg[1] > 0 &&
1981 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* Accept third byte by clearing ACK */
1983 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1984 len = extended_msg[1] - 1;
1985 data = extended_msg + 3;
1986 phase = PHASE_MSGIN;
1987
1988 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001989 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 switch (extended_msg[2]) {
1992 case EXTENDED_SDTR:
1993 case EXTENDED_WDTR:
1994 case EXTENDED_MODIFY_DATA_POINTER:
1995 case EXTENDED_EXTENDED_IDENTIFY:
1996 tmp = 0;
1997 }
1998 } else if (len) {
1999 printk("scsi%d: error receiving extended message\n", instance->host_no);
2000 tmp = 0;
2001 } else {
2002 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2003 tmp = 0;
2004 }
Finn Thain11d2f632016-01-03 16:05:51 +11002005
2006 spin_lock_irq(&hostdata->lock);
2007 if (!hostdata->connected)
2008 return;
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* Fall through to reject message */
2011
2012 /*
2013 * If we get something weird that we aren't expecting,
2014 * reject it.
2015 */
2016 default:
2017 if (!tmp) {
2018 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002019 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 printk("\n");
2021 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002022 scmd_printk(KERN_INFO, cmd,
2023 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002025 scmd_printk(KERN_INFO, cmd,
2026 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 msgout = MESSAGE_REJECT;
2029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2030 break;
2031 } /* switch (tmp) */
2032 break;
2033 case PHASE_MSGOUT:
2034 len = 1;
2035 data = &msgout;
2036 hostdata->last_message = msgout;
2037 NCR5380_transfer_pio(instance, &phase, &len, &data);
2038 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 hostdata->connected = NULL;
2040 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002041 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2043 return;
2044 }
2045 msgout = NOP;
2046 break;
2047 case PHASE_CMDOUT:
2048 len = cmd->cmd_len;
2049 data = cmd->cmnd;
2050 /*
2051 * XXX for performance reasons, on machines with a
2052 * PSEUDO-DMA architecture we should probably
2053 * use the dma transfer function.
2054 */
2055 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 break;
2057 case PHASE_STATIN:
2058 len = 1;
2059 data = &tmp;
2060 NCR5380_transfer_pio(instance, &phase, &len, &data);
2061 cmd->SCp.Status = tmp;
2062 break;
2063 default:
2064 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002065 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002067 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002068 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002069 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002070 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Finn Thain11d2f632016-01-03 16:05:51 +11002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075/*
2076 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2077 *
2078 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002079 * 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 -07002080 * nexus has been reestablished,
2081 *
2082 * Inputs : instance - this instance of the NCR5380.
2083 *
2084 * Locks: io_request_lock held by caller if IRQ driven
2085 */
2086
2087static void NCR5380_reselect(struct Scsi_Host *instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11002088 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned char target_mask;
2090 unsigned char lun, phase;
2091 int len;
2092 unsigned char msg[3];
2093 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11002094 struct NCR5380_cmd *ncmd;
2095 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /*
2098 * Disable arbitration, etc. since the host adapter obviously
2099 * lost, and tell an interrupted NCR5380_select() to restart.
2100 */
2101
2102 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain72064a72016-01-03 16:05:44 +11002105 dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /*
2108 * At this point, we have detected that our SCSI ID is on the bus,
2109 * SEL is true and BSY was false for at least one bus settle delay
2110 * (400 ns).
2111 *
2112 * We must assert BSY ourselves, until the target drops the SEL
2113 * signal.
2114 */
2115
2116 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002117 if (NCR5380_poll_politely(instance,
2118 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2119 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2120 return;
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2123
2124 /*
2125 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 */
2127
Finn Thain1cc160e2016-01-03 16:05:32 +11002128 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002129 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2130 do_abort(instance);
2131 return;
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 len = 1;
2135 data = msg;
2136 phase = PHASE_MSGIN;
2137 NCR5380_transfer_pio(instance, &phase, &len, &data);
2138
Finn Thain72064a72016-01-03 16:05:44 +11002139 if (len) {
2140 do_abort(instance);
2141 return;
2142 }
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002145 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002146 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002147 printk("\n");
2148 do_abort(instance);
2149 return;
2150 }
2151 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Finn Thain72064a72016-01-03 16:05:44 +11002153 /*
2154 * We need to add code for SCSI-II to track which devices have
2155 * I_T_L_Q nexuses established, and which have simple I_T_L
2156 * nexuses so we can chose to do additional data transfer.
2157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Finn Thain72064a72016-01-03 16:05:44 +11002159 /*
2160 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2161 * just reestablished, and remove it from the disconnected queue.
2162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Finn Thain32b26a12016-01-03 16:05:58 +11002164 tmp = NULL;
2165 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2166 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2167
2168 if (target_mask == (1 << scmd_id(cmd)) &&
2169 lun == (u8)cmd->device->lun) {
2170 list_del(&ncmd->list);
2171 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002175
2176 if (tmp) {
2177 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2178 "reselect: removed %p from disconnected queue\n", tmp);
2179 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002180 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2181 target_mask, lun);
2182 /*
2183 * Since we have an established nexus that we can't do anything with,
2184 * we must abort it.
2185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002187 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Finn Thain72064a72016-01-03 16:05:44 +11002189
2190 /* Accept message by clearing ACK */
2191 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2192
2193 hostdata->connected = tmp;
2194 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n",
2195 instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198/*
2199 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2200 *
2201 * Purpose : called by interrupt handler when DMA finishes or a phase
2202 * mismatch occurs (which would finish the DMA transfer).
2203 *
2204 * Inputs : instance - this instance of the NCR5380.
2205 *
Finn Thain710ddd02014-11-12 16:12:02 +11002206 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 * nexus has been reestablished, on failure NULL is returned.
2208 */
2209
2210#ifdef REAL_DMA
2211static void NCR5380_dma_complete(NCR5380_instance * instance) {
Finn Thaine8a60142016-01-03 16:05:54 +11002212 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /*
2216 * XXX this might not be right.
2217 *
2218 * Wait for final byte to transfer, ie wait for ACK to go false.
2219 *
2220 * We should use the Last Byte Sent bit, unfortunately this is
2221 * not available on the 5380/5381 (only the various CMOS chips)
2222 *
2223 * FIXME: timeout, and need to handle long timeout/irq case
2224 */
2225
2226 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2229
2230 /*
2231 * The only places we should see a phase mismatch and have to send
2232 * data from the same set of pointers will be the data transfer
2233 * phases. So, residual, requested length are only important here.
2234 */
2235
2236 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2237 transferred = instance->dmalen - NCR5380_dma_residual();
2238 hostdata->connected->SCp.this_residual -= transferred;
2239 hostdata->connected->SCp.ptr += transferred;
2240 }
2241}
2242#endif /* def REAL_DMA */
2243
2244/*
Finn Thain710ddd02014-11-12 16:12:02 +11002245 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 *
2247 * Purpose : abort a command
2248 *
Finn Thain710ddd02014-11-12 16:12:02 +11002249 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002250 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 * used.
2252 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002253 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002255 * XXX - there is no way to abort the command that is currently
2256 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 * a problem, we could implement longjmp() / setjmp(), setjmp()
2258 * called where the loop started in NCR5380_main().
2259 *
2260 * Locks: host lock taken by caller
2261 */
2262
Finn Thain710ddd02014-11-12 16:12:02 +11002263static int NCR5380_abort(struct scsi_cmnd *cmd)
2264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002266 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002267 unsigned long flags;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002268
Finn Thain11d2f632016-01-03 16:05:51 +11002269 spin_lock_irqsave(&hostdata->lock, flags);
2270
Finn Thain32b26a12016-01-03 16:05:58 +11002271#if (NDEBUG & NDEBUG_ANY)
2272 scmd_printk(KERN_INFO, cmd, "aborting command\n");
2273#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002274 NCR5380_dprint(NDEBUG_ANY, instance);
2275 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Finn Thain11d2f632016-01-03 16:05:51 +11002277 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return FAILED;
2280}
2281
2282
Finn Thain3be1b3e2016-01-03 16:05:12 +11002283/**
2284 * NCR5380_bus_reset - reset the SCSI bus
2285 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002287 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
2289
Finn Thain710ddd02014-11-12 16:12:02 +11002290static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002291{
2292 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002293 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2294 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Finn Thain11d2f632016-01-03 16:05:51 +11002296 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002297
2298#if (NDEBUG & NDEBUG_ANY)
2299 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
Finn Thain3be1b3e2016-01-03 16:05:12 +11002300#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002301 NCR5380_dprint(NDEBUG_ANY, instance);
2302 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002303
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002304 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002305
Finn Thain11d2f632016-01-03 16:05:51 +11002306 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return SUCCESS;
2309}