blob: 8ff59509585cde89a3edf00b1462d2aaa4639e98 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * For more information, please consult
15 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Revision 1.10 1998/9/2 Alan Cox
Alan Coxfa195af2008-10-27 15:16:36 +000029 * (alan@lxorguk.ukuu.org.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Fixed up the timer lockups reported so far. Things still suck. Looking
31 * forward to 2.3 and per device request queues. Then it'll be possible to
32 * SMP thread this beast and improve life no end.
33
34 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
35 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
36 * (hopefully) fixed and enhanced USLEEP
37 * added support for DTC3181E card (for Mustek scanner)
38 *
39
40 * Revision 1.8 Ingmar Baumgart
41 * (ingmar@gonzo.schwaben.de)
42 * added support for NCR53C400a card
43 *
44
45 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
46 * added proc_info
47 * added support needed for DTC 3180/3280
48 * fixed a couple of bugs
49 *
50
51 * Revision 1.5 1994/01/19 09:14:57 drew
52 * Fixed udelay() hack that was being used on DATAOUT phases
53 * instead of a proper wait for the final handshake.
54 *
55 * Revision 1.4 1994/01/19 06:44:25 drew
56 * *** empty log message ***
57 *
58 * Revision 1.3 1994/01/19 05:24:40 drew
59 * Added support for TCR LAST_BYTE_SENT bit.
60 *
61 * Revision 1.2 1994/01/15 06:14:11 drew
62 * REAL DMA support, bug fixes.
63 *
64 * Revision 1.1 1994/01/15 06:00:54 drew
65 * Initial revision
66 *
67 */
68
69/*
70 * Further development / testing that should be done :
71 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
72 * code so that everything does the same thing that's done at the
73 * end of a pseudo-DMA read operation.
74 *
75 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
76 * basically, transfer size needs to be reduced by one
77 * and the last byte read as is done with PSEUDO_DMA.
78 *
79 * 4. Test SCSI-II tagged queueing (I have no devices which support
80 * tagged queueing)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
db9dff32005-04-03 14:53:59 -050082#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050083#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#if (NDEBUG & NDEBUG_LISTS)
86#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
87#define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
88#else
89#define LIST(x,y)
90#define REMOVE(w,x,y,z)
91#endif
92
93#ifndef notyet
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#undef REAL_DMA
95#endif
96
97#ifdef REAL_DMA_POLL
98#undef READ_OVERRUNS
99#define READ_OVERRUNS
100#endif
101
102#ifdef BOARD_REQUIRES_NO_DELAY
103#define io_recovery_delay(x)
104#else
105#define io_recovery_delay(x) udelay(x)
106#endif
107
108/*
109 * Design
110 *
111 * This is a generic 5380 driver. To use it on a different platform,
112 * one simply writes appropriate system specific macros (ie, data
113 * transfer - some PC's will use the I/O bus, 68K's must use
114 * memory mapped) and drops this file in their 'C' wrapper.
115 *
116 * (Note from hch: unfortunately it was not enough for the different
117 * m68k folks and instead of improving this driver they copied it
118 * and hacked it up for their needs. As a consequence they lost
119 * most updates to this driver. Maybe someone will fix all these
120 * drivers to use a common core one day..)
121 *
122 * As far as command queueing, two queues are maintained for
123 * each 5380 in the system - commands that haven't been issued yet,
124 * and commands that are currently executing. This means that an
125 * unlimited number of commands may be queued, letting
126 * more commands propagate from the higher driver levels giving higher
127 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
128 * allowing multiple commands to propagate all the way to a SCSI-II device
129 * while a command is already executing.
130 *
131 *
132 * Issues specific to the NCR5380 :
133 *
134 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
135 * piece of hardware that requires you to sit in a loop polling for
136 * the REQ signal as long as you are connected. Some devices are
137 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100138 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * broken devices are the exception rather than the rule and I'd rather
140 * spend my time optimizing for the normal case.
141 *
142 * Architecture :
143 *
144 * At the heart of the design is a coroutine, NCR5380_main,
145 * which is started from a workqueue for each NCR5380 host in the
146 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
147 * removing the commands from the issue queue and calling
148 * NCR5380_select() if a nexus is not established.
149 *
150 * Once a nexus is established, the NCR5380_information_transfer()
151 * phase goes through the various phases as instructed by the target.
152 * if the target goes into MSG IN and sends a DISCONNECT message,
153 * the command structure is placed into the per instance disconnected
154 * queue, and NCR5380_main tries to find more work. If the target is
155 * idle for too long, the system will try to sleep.
156 *
157 * If a command has disconnected, eventually an interrupt will trigger,
158 * calling NCR5380_intr() which will in turn call NCR5380_reselect
159 * to reestablish a nexus. This will run main if necessary.
160 *
161 * On command termination, the done function will be called as
162 * appropriate.
163 *
164 * SCSI pointers are maintained in the SCp field of SCSI command
165 * structures, being initialized after the command is connected
166 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
167 * Note that in violation of the standard, an implicit SAVE POINTERS operation
168 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
169 */
170
171/*
172 * Using this file :
173 * This file a skeleton Linux SCSI driver for the NCR 5380 series
174 * of chips. To use it, you write an architecture specific functions
175 * and macros and include this file in your driver.
176 *
177 * These macros control options :
178 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
179 * defined.
180 *
181 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
182 * for commands that return with a CHECK CONDITION status.
183 *
184 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
185 * transceivers.
186 *
187 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
188 * override-configure an IRQ.
189 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
191 *
192 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
193 *
194 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
195 * rely on phase mismatch and EOP interrupts to determine end
196 * of phase.
197 *
198 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
199 * only really want to use this if you're having a problem with
200 * dropped characters during high speed communications, and even
201 * then, you're going to be better off twiddling with transfersize
202 * in the high level code.
203 *
204 * Defaults for these will be provided although the user may want to adjust
205 * these to allocate CPU resources to the SCSI driver or "real" code.
206 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * These macros MUST be defined :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * NCR5380_read(register) - read from the specified register
210 *
211 * NCR5380_write(register, value) - write to the specific register
212 *
213 * NCR5380_implementation_fields - additional fields needed for this
214 * specific implementation of the NCR5380
215 *
216 * Either real DMA *or* pseudo DMA may be implemented
217 * REAL functions :
218 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
219 * Note that the DMA setup functions should return the number of bytes
220 * that they were able to program the controller for.
221 *
222 * Also note that generic i386/PC versions of these macros are
223 * available as NCR5380_i386_dma_write_setup,
224 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
225 *
226 * NCR5380_dma_write_setup(instance, src, count) - initialize
227 * NCR5380_dma_read_setup(instance, dst, count) - initialize
228 * NCR5380_dma_residual(instance); - residual count
229 *
230 * PSEUDO functions :
231 * NCR5380_pwrite(instance, src, count)
232 * NCR5380_pread(instance, dst, count);
233 *
234 * The generic driver is initialized by calling NCR5380_init(instance),
235 * after setting the appropriate host specific fields and ID. If the
236 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
237 * possible) function may be used.
238 */
239
Finn Thain54d8fe42016-01-03 16:05:06 +1100240static int do_abort(struct Scsi_Host *);
241static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/*
244 * initialize_SCp - init the scsi pointer field
245 * @cmd: command block to set up
246 *
247 * Set up the internal fields in the SCSI command.
248 */
249
Finn Thain710ddd02014-11-12 16:12:02 +1100250static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 /*
253 * Initialize the Scsi Pointer field so that all of the commands in the
254 * various queues are valid.
255 */
256
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200257 if (scsi_bufflen(cmd)) {
258 cmd->SCp.buffer = scsi_sglist(cmd);
259 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200260 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 cmd->SCp.this_residual = cmd->SCp.buffer->length;
262 } else {
263 cmd->SCp.buffer = NULL;
264 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200265 cmd->SCp.ptr = NULL;
266 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268}
269
270/**
Finn Thain2f854b82016-01-03 16:05:22 +1100271 * NCR5380_poll_politely - wait for chip register value
272 * @instance: controller to poll
273 * @reg: 5380 register to poll
274 * @bit: Bitmask to check
275 * @val: Value required to exit
276 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
Finn Thain2f854b82016-01-03 16:05:22 +1100278 * Polls the chip in a reasonably efficient manner waiting for an
279 * event to occur. After a short quick poll we begin to yield the CPU
280 * (if possible). In irq contexts the time-out is arbitrarily limited.
281 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
Finn Thain2f854b82016-01-03 16:05:22 +1100283 * Returns 0 if event occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Finn Thain2f854b82016-01-03 16:05:22 +1100286static int NCR5380_poll_politely(struct Scsi_Host *instance,
287 int reg, int bit, int val, int wait)
288{
289 struct NCR5380_hostdata *hostdata = shost_priv(instance);
290 unsigned long deadline = jiffies + wait;
291 unsigned long n;
292
293 /* Busy-wait for up to 10 ms */
294 n = min(10000U, jiffies_to_usecs(wait));
295 n *= hostdata->accesses_per_ms;
296 n /= 1000;
297 do {
298 if ((NCR5380_read(reg) & bit) == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100301 } while (n--);
302
303 if (irqs_disabled() || in_interrupt())
304 return -ETIMEDOUT;
305
306 /* Repeatedly sleep for 1 ms until deadline */
307 while (time_is_after_jiffies(deadline)) {
308 schedule_timeout_uninterruptible(1);
309 if ((NCR5380_read(reg) & bit) == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Finn Thain2f854b82016-01-03 16:05:22 +1100312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ETIMEDOUT;
314}
315
316static struct {
317 unsigned char value;
318 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700319} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 {PHASE_DATAOUT, "DATAOUT"},
321 {PHASE_DATAIN, "DATAIN"},
322 {PHASE_CMDOUT, "CMDOUT"},
323 {PHASE_STATIN, "STATIN"},
324 {PHASE_MSGOUT, "MSGOUT"},
325 {PHASE_MSGIN, "MSGIN"},
326 {PHASE_UNKNOWN, "UNKNOWN"}
327};
328
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100329#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static struct {
331 unsigned char mask;
332 const char *name;
333} signals[] = {
334 {SR_DBP, "PARITY"},
335 {SR_RST, "RST"},
336 {SR_BSY, "BSY"},
337 {SR_REQ, "REQ"},
338 {SR_MSG, "MSG"},
339 {SR_CD, "CD"},
340 {SR_IO, "IO"},
341 {SR_SEL, "SEL"},
342 {0, NULL}
343},
344basrs[] = {
345 {BASR_ATN, "ATN"},
346 {BASR_ACK, "ACK"},
347 {0, NULL}
348},
349icrs[] = {
350 {ICR_ASSERT_RST, "ASSERT RST"},
351 {ICR_ASSERT_ACK, "ASSERT ACK"},
352 {ICR_ASSERT_BSY, "ASSERT BSY"},
353 {ICR_ASSERT_SEL, "ASSERT SEL"},
354 {ICR_ASSERT_ATN, "ASSERT ATN"},
355 {ICR_ASSERT_DATA, "ASSERT DATA"},
356 {0, NULL}
357},
358mrs[] = {
359 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
360 {MR_TARGET, "MODE TARGET"},
361 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
362 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
363 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
364 {MR_DMA_MODE, "MODE DMA"},
365 {MR_ARBITRATE, "MODE ARBITRATION"},
366 {0, NULL}
367};
368
369/**
370 * NCR5380_print - print scsi bus signals
371 * @instance: adapter state to dump
372 *
373 * Print the SCSI bus signals for debugging purposes
374 *
375 * Locks: caller holds hostdata lock (not essential)
376 */
377
378static void NCR5380_print(struct Scsi_Host *instance)
379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
383 status = NCR5380_read(STATUS_REG);
384 mr = NCR5380_read(MODE_REG);
385 icr = NCR5380_read(INITIATOR_COMMAND_REG);
386 basr = NCR5380_read(BUS_AND_STATUS_REG);
387
388 printk("STATUS_REG: %02x ", status);
389 for (i = 0; signals[i].mask; ++i)
390 if (status & signals[i].mask)
391 printk(",%s", signals[i].name);
392 printk("\nBASR: %02x ", basr);
393 for (i = 0; basrs[i].mask; ++i)
394 if (basr & basrs[i].mask)
395 printk(",%s", basrs[i].name);
396 printk("\nICR: %02x ", icr);
397 for (i = 0; icrs[i].mask; ++i)
398 if (icr & icrs[i].mask)
399 printk(",%s", icrs[i].name);
400 printk("\nMODE: %02x ", mr);
401 for (i = 0; mrs[i].mask; ++i)
402 if (mr & mrs[i].mask)
403 printk(",%s", mrs[i].name);
404 printk("\n");
405}
406
407
408/*
409 * NCR5380_print_phase - show SCSI phase
410 * @instance: adapter to dump
411 *
412 * Print the current SCSI phase for debugging purposes
413 *
414 * Locks: none
415 */
416
417static void NCR5380_print_phase(struct Scsi_Host *instance)
418{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned char status;
420 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 status = NCR5380_read(STATUS_REG);
423 if (!(status & SR_REQ))
424 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
425 else {
426 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
427 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
428 }
429}
430#endif
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Finn Thaind5f7e652016-01-03 16:05:03 +1100433static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435/**
436 * probe_intr - helper for IRQ autoprobe
437 * @irq: interrupt number
438 * @dev_id: unused
439 * @regs: unused
440 *
441 * Set a flag to indicate the IRQ in question was received. This is
442 * used by the IRQ probe code.
443 */
444
David Howells7d12e782006-10-05 14:55:46 +0100445static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 probe_irq = irq;
448 return IRQ_HANDLED;
449}
450
451/**
452 * NCR5380_probe_irq - find the IRQ of an NCR5380
453 * @instance: NCR5380 controller
454 * @possible: bitmask of ISA IRQ lines
455 *
456 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
457 * and then looking to see what interrupt actually turned up.
458 *
459 * Locks: none, irqs must be enabled on entry
460 */
461
Andrew Morton702809c2007-05-23 14:41:56 -0700462static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
463 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
466 unsigned long timeout;
467 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Finn Thain22f5f102014-11-12 16:11:56 +1100469 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100470 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 trying_irqs |= mask;
472
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500473 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100474 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /*
477 * A interrupt is triggered whenever BSY = false, SEL = true
478 * and a bit set in the SELECT_ENABLE_REG is asserted on the
479 * SCSI bus.
480 *
481 * Note that the bus is only driven when the phase control signals
482 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
483 * to zero.
484 */
485
486 NCR5380_write(TARGET_COMMAND_REG, 0);
487 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
488 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
489 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
490
Finn Thain22f5f102014-11-12 16:11:56 +1100491 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800492 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 NCR5380_write(SELECT_ENABLE_REG, 0);
495 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
496
Finn Thain22f5f102014-11-12 16:11:56 +1100497 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (trying_irqs & mask)
499 free_irq(i, NULL);
500
501 return probe_irq;
502}
503
504/**
Finn Thain8c325132014-11-12 16:11:58 +1100505 * NCR58380_info - report driver and host information
506 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *
Finn Thain8c325132014-11-12 16:11:58 +1100508 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
510 * Locks: none
511 */
512
Finn Thain8c325132014-11-12 16:11:58 +1100513static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Finn Thain8c325132014-11-12 16:11:58 +1100515 struct NCR5380_hostdata *hostdata = shost_priv(instance);
516
517 return hostdata->info;
518}
519
520static void prepare_info(struct Scsi_Host *instance)
521{
522 struct NCR5380_hostdata *hostdata = shost_priv(instance);
523
524 snprintf(hostdata->info, sizeof(hostdata->info),
525 "%s, io_port 0x%lx, n_io_port %d, "
526 "base 0x%lx, irq %d, "
527 "can_queue %d, cmd_per_lun %d, "
528 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100529 "flags { %s%s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100530 "options { %s} ",
531 instance->hostt->name, instance->io_port, instance->n_io_port,
532 instance->base, instance->irq,
533 instance->can_queue, instance->cmd_per_lun,
534 instance->sg_tablesize, instance->this_id,
535 hostdata->flags & FLAG_NCR53C400 ? "NCR53C400 " : "",
536 hostdata->flags & FLAG_DTC3181E ? "DTC3181E " : "",
537 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100538 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100540 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100543 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544#endif
545#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100546 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
548#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100549 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
551#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100552 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
554#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100555 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
557#ifdef UNSAFE
Finn Thain8c325132014-11-12 16:11:58 +1100558 "UNSAFE "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#endif
Finn Thain8c325132014-11-12 16:11:58 +1100560 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/**
564 * NCR5380_print_status - dump controller info
565 * @instance: controller to dump
566 *
567 * Print commands in the various queues, called from NCR5380_abort
568 * and NCR5380_debug to aid debugging.
569 *
570 * Locks: called functions disable irqs
571 */
572
Finn Thain3be1b3e2016-01-03 16:05:12 +1100573static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 NCR5380_dprint(NDEBUG_ANY, instance);
576 NCR5380_dprint_phase(NDEBUG_ANY, instance);
577}
578
Finn Thaina9c2dc42014-11-12 16:11:59 +1100579#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/******************************************/
581/*
582 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
583 *
584 * *buffer: I/O buffer
585 * **start: if inout == FALSE pointer into buffer where user read should start
586 * offset: current offset
587 * length: length of buffer
588 * hostno: Scsi_Host host_no
589 * inout: TRUE - user is writing; FALSE - user is reading
590 *
591 * Return the number of bytes read from or written
592 */
593
Al Virodd7ab712013-03-31 01:15:54 -0400594static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
595 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100597 struct NCR5380_hostdata *hostdata = shost_priv(instance);
598
599 hostdata->spin_max_r = 0;
600 hostdata->spin_max_w = 0;
601 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400602}
Finn Thaina9c2dc42014-11-12 16:11:59 +1100603#endif
Al Virodd7ab712013-03-31 01:15:54 -0400604
Al Virodd7ab712013-03-31 01:15:54 -0400605static
Finn Thain710ddd02014-11-12 16:12:02 +1100606void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m);
Al Virodd7ab712013-03-31 01:15:54 -0400607static
608void lprint_command(unsigned char *cmd, struct seq_file *m);
609static
610void lprint_opcode(int opcode, struct seq_file *m);
611
612static int __maybe_unused NCR5380_show_info(struct seq_file *m,
613 struct Scsi_Host *instance)
614{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100616 struct scsi_cmnd *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
619
Finn Thaina9c2dc42014-11-12 16:11:59 +1100620#ifdef PSEUDO_DMA
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100621 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100622 hostdata->spin_max_w, hostdata->spin_max_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623#endif
624 spin_lock_irq(instance->host_lock);
625 if (!hostdata->connected)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100626 seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 else
Finn Thain710ddd02014-11-12 16:12:02 +1100628 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100629 seq_printf(m, "scsi%d: issue_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100630 for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400631 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100633 seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100634 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400635 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_unlock_irq(instance->host_lock);
Al Virodd7ab712013-03-31 01:15:54 -0400637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Finn Thain710ddd02014-11-12 16:12:02 +1100640static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100642 seq_printf(m, "scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100643 seq_puts(m, " command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400644 lprint_command(cmd->cmnd, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Al Virodd7ab712013-03-31 01:15:54 -0400647static void lprint_command(unsigned char *command, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int i, s;
Al Virodd7ab712013-03-31 01:15:54 -0400650 lprint_opcode(command[0], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100652 seq_printf(m, "%02x ", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100653 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Al Virodd7ab712013-03-31 01:15:54 -0400656static void lprint_opcode(int opcode, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100658 seq_printf(m, "%2d (0x%02x)", opcode, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661
662/**
663 * NCR5380_init - initialise an NCR5380
664 * @instance: adapter to configure
665 * @flags: control flags
666 *
667 * Initializes *instance and corresponding 5380 chip,
668 * with flags OR'd into the initial flags value.
669 *
670 * Notes : I assume that the host, hostno, and id bits have been
671 * set correctly. I don't care about the irq and other fields.
672 *
673 * Returns 0 for success
674 *
675 * Locks: interrupts must be enabled when we are called
676 */
677
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800678static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Finn Thainb6488f92016-01-03 16:05:08 +1100680 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain2f854b82016-01-03 16:05:22 +1100682 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if(in_interrupt())
685 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 hostdata->id_mask = 1 << instance->this_id;
688 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
689 if (i > hostdata->id_mask)
690 hostdata->id_higher_mask |= i;
691 for (i = 0; i < 8; ++i)
692 hostdata->busy[i] = 0;
693#ifdef REAL_DMA
694 hostdata->dmalen = 0;
695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 hostdata->connected = NULL;
697 hostdata->issue_queue = NULL;
698 hostdata->disconnected_queue = NULL;
699
Finn Thain8d8601a2016-01-03 16:05:37 +1100700 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100701 hostdata->work_q = alloc_workqueue("ncr5380_%d",
702 WQ_UNBOUND | WQ_MEM_RECLAIM,
703 1, instance->host_no);
704 if (!hostdata->work_q)
705 return -ENOMEM;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* The CHECK code seems to break the 53C400. Will check it later maybe */
708 if (flags & FLAG_NCR53C400)
709 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
710 else
711 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
712
713 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Finn Thain8c325132014-11-12 16:11:58 +1100715 prepare_info(instance);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
718 NCR5380_write(MODE_REG, MR_BASE);
719 NCR5380_write(TARGET_COMMAND_REG, 0);
720 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100721
722 /* Calibrate register polling loop */
723 i = 0;
724 deadline = jiffies + 1;
725 do {
726 cpu_relax();
727 } while (time_is_after_jiffies(deadline));
728 deadline += msecs_to_jiffies(256);
729 do {
730 NCR5380_read(STATUS_REG);
731 ++i;
732 cpu_relax();
733 } while (time_is_after_jiffies(deadline));
734 hostdata->accesses_per_ms = i / 256;
735
Finn Thainb6488f92016-01-03 16:05:08 +1100736 return 0;
737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Finn Thainb6488f92016-01-03 16:05:08 +1100739/**
740 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
741 * @instance: adapter to check
742 *
743 * If the system crashed, it may have crashed with a connected target and
744 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
745 * currently established nexus, which we know nothing about. Failing that
746 * do a bus reset.
747 *
748 * Note that a bus reset will cause the chip to assert IRQ.
749 *
750 * Returns 0 if successful, otherwise -ENXIO.
751 */
752
753static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
754{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100755 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100756 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
759 switch (pass) {
760 case 1:
761 case 3:
762 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100763 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
764 NCR5380_poll_politely(instance,
765 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100768 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 do_abort(instance);
770 break;
771 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100772 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100774 /* Wait after a reset; the SCSI standard calls for
775 * 250ms, we wait 500ms to be on the safe side.
776 * But some Toshiba CD-ROMs need ten times that.
777 */
778 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
779 msleep(2500);
780 else
781 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100784 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -ENXIO;
786 }
787 }
788 return 0;
789}
790
791/**
792 * NCR5380_exit - remove an NCR5380
793 * @instance: adapter to remove
794 */
795
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800796static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
799
Finn Thain8d8601a2016-01-03 16:05:37 +1100800 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100801 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/**
Finn Thain1bb40582016-01-03 16:05:29 +1100805 * NCR5380_queue_command - queue a command
806 * @instance: the relevant SCSI adapter
807 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
Finn Thain1bb40582016-01-03 16:05:29 +1100809 * cmd is added to the per-instance issue queue, with minor
810 * twiddling done to the host specific fields of cmd. If the
811 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
813
Finn Thain1bb40582016-01-03 16:05:29 +1100814static int NCR5380_queue_command(struct Scsi_Host *instance,
815 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Finn Thain1bb40582016-01-03 16:05:29 +1100817 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100818 struct scsi_cmnd *tmp;
Finn Thain1bb40582016-01-03 16:05:29 +1100819 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821#if (NDEBUG & NDEBUG_NO_WRITE)
822 switch (cmd->cmnd[0]) {
823 case WRITE_6:
824 case WRITE_10:
825 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
826 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100827 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829 }
830#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * We use the host_scribble field as a pointer to the next command
834 * in a queue
835 */
836
837 cmd->host_scribble = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 cmd->result = 0;
839
Finn Thain1bb40582016-01-03 16:05:29 +1100840 spin_lock_irqsave(instance->host_lock, flags);
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * Insert the cmd into the issue queue. Note that REQUEST SENSE
844 * commands are added to the head of the queue since any command will
845 * clear the contingent allegiance condition that exists and the
846 * sense data is only guaranteed to be valid while the condition exists.
847 */
848
849 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
850 LIST(cmd, hostdata->issue_queue);
851 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
852 hostdata->issue_queue = cmd;
853 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100854 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (struct scsi_cmnd *) tmp->host_scribble);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 LIST(cmd, tmp);
856 tmp->host_scribble = (unsigned char *) cmd;
857 }
Finn Thain1bb40582016-01-03 16:05:29 +1100858 spin_unlock_irqrestore(instance->host_lock, flags);
859
Finn Thain52a6a1c2014-03-18 11:42:18 +1100860 dprintk(NDEBUG_QUEUES, "scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100863 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/**
868 * NCR5380_main - NCR state machines
869 *
870 * NCR5380_main is a coroutine that runs as long as more work can
871 * be done on the NCR5380 host adapters in a system. Both
872 * NCR5380_queue_command() and NCR5380_intr() will try to start it
873 * in case it is not running.
874 *
875 * Locks: called as its own thread with no locks held. Takes the
876 * host lock and called routines may take the isa dma lock.
877 */
878
David Howellsc4028952006-11-22 14:57:56 +0000879static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
David Howellsc4028952006-11-22 14:57:56 +0000881 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100882 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100884 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int done;
886
887 spin_lock_irq(instance->host_lock);
888 do {
889 /* Lock held here */
890 done = 1;
Finn Thainae753a32016-01-03 16:05:23 +1100891 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100892 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /*
894 * Search through the issue_queue for a command destined
895 * for a target that's not busy.
896 */
Finn Thain710ddd02014-11-12 16:12:02 +1100897 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 {
899 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200900 dprintk(NDEBUG_LISTS, "MAIN tmp=%p target=%d busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200902 if (!(hostdata->busy[tmp->device->id] &
903 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (prev) {
905 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
906 prev->host_scribble = tmp->host_scribble;
907 } else {
908 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +1100909 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 tmp->host_scribble = NULL;
912
913 /*
914 * Attempt to establish an I_T_L nexus here.
915 * On success, instance->hostdata->connected is set.
916 * On failure, we must add the command back to the
917 * issue queue so we can keep trying.
918 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200919 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Finn Thain76f13b92014-11-12 16:11:53 +1100921 /*
922 * REQUEST SENSE commands are issued without tagged
923 * queueing, even on SCSI-II devices because the
924 * contingent allegiance condition exists for the
925 * entire unit.
926 */
927
928 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100929 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100931 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 LIST(tmp, hostdata->issue_queue);
933 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
934 hostdata->issue_queue = tmp;
935 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100936 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Finn Thainae753a32016-01-03 16:05:23 +1100938 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* lock held here still */
941 } /* if target/lun is not busy */
942 } /* for */
943 /* exited locked */
944 } /* if (!hostdata->connected) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (hostdata->connected
946#ifdef REAL_DMA
947 && !hostdata->dmalen
948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100950 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100952 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 } while (!done);
956
957 spin_unlock_irq(instance->host_lock);
958}
959
960#ifndef DONT_USE_INTR
961
962/**
963 * NCR5380_intr - generic NCR5380 irq handler
964 * @irq: interrupt number
965 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
967 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
968 * from the disconnected queue, and restarting NCR5380_main()
969 * as required.
970 *
971 * Locks: takes the needed instance locks
972 */
973
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800974static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800976 struct Scsi_Host *instance = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
978 int done;
979 unsigned char basr;
980 unsigned long flags;
981
Finn Thain52a6a1c2014-03-18 11:42:18 +1100982 dprintk(NDEBUG_INTR, "scsi : NCR5380 irq %d triggered\n",
983 instance->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 do {
986 done = 1;
987 spin_lock_irqsave(instance->host_lock, flags);
988 /* Look for pending interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 basr = NCR5380_read(BUS_AND_STATUS_REG);
990 /* XXX dispatch to appropriate routine if found and done=0 */
991 if (basr & BASR_IRQ) {
992 NCR5380_dprint(NDEBUG_INTR, instance);
993 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
994 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100995 dprintk(NDEBUG_INTR, "scsi%d : SEL interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 NCR5380_reselect(instance);
997 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
998 } else if (basr & BASR_PARITY_ERROR) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100999 dprintk(NDEBUG_INTR, "scsi%d : PARITY interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1001 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001002 dprintk(NDEBUG_INTR, "scsi%d : RESET interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1004 } else {
1005#if defined(REAL_DMA)
1006 /*
1007 * We should only get PHASE MISMATCH and EOP interrupts
1008 * if we have DMA enabled, so do a sanity check based on
1009 * the current setting of the MODE register.
1010 */
1011
1012 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001013 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (!hostdata->connected)
1016 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1017
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001018 transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 hostdata->connected->SCp.this_residual -= transferred;
1020 hostdata->connected->SCp.ptr += transferred;
1021 hostdata->dmalen = 0;
1022
1023 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1024
1025 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1026 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1027
1028 NCR5380_write(MODE_REG, MR_BASE);
1029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1030 }
1031#else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001032 dprintk(NDEBUG_INTR, "scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1034#endif
1035 }
1036 } /* if BASR_IRQ */
1037 spin_unlock_irqrestore(instance->host_lock, flags);
1038 if(!done)
Finn Thain8d8601a2016-01-03 16:05:37 +11001039 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 } while (!done);
1041 return IRQ_HANDLED;
1042}
1043
1044#endif
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
Finn Thain710ddd02014-11-12 16:12:02 +11001047 * Function : int NCR5380_select(struct Scsi_Host *instance,
1048 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
1050 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1051 * including ARBITRATION, SELECTION, and initial message out for
1052 * IDENTIFY and queue messages.
1053 *
1054 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001055 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
Finn Thain63238762016-01-03 16:05:15 +11001057 * Returns : -1 if selection failed but should be retried.
1058 * 0 if selection failed and should not be retried.
1059 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
1061 * Side effects :
1062 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1063 * with registers as they should have been on entry - ie
1064 * SELECT_ENABLE will be set appropriately, the NCR5380
1065 * will cease to drive any SCSI bus signals.
1066 *
1067 * If successful : I_T_L or I_T_L_Q nexus will be established,
1068 * instance->connected will be set to cmd.
1069 * SELECT interrupt will be disabled.
1070 *
1071 * If failed (no target) : cmd->scsi_done() will be called, and the
1072 * cmd->result host byte set to DID_BAD_TARGET.
1073 *
1074 * Locks: caller holds hostdata lock in IRQ mode
1075 */
1076
Finn Thain710ddd02014-11-12 16:12:02 +11001077static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1080 unsigned char tmp[3], phase;
1081 unsigned char *data;
1082 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int err;
Finn Thain55500d92016-01-03 16:05:35 +11001084 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001087 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /*
1090 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1091 * data bus during SELECTION.
1092 */
1093
1094 NCR5380_write(TARGET_COMMAND_REG, 0);
1095
1096 /*
1097 * Start arbitration.
1098 */
1099
1100 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1101 NCR5380_write(MODE_REG, MR_ARBITRATE);
1102
Finn Thain55500d92016-01-03 16:05:35 +11001103 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1104 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
1106
Finn Thain55500d92016-01-03 16:05:35 +11001107 spin_unlock_irq(instance->host_lock);
1108 timeout = jiffies + HZ;
1109 while (1) {
1110 if (time_is_before_jiffies(timeout)) {
1111 NCR5380_write(MODE_REG, MR_BASE);
1112 shost_printk(KERN_ERR, instance,
1113 "select: arbitration timeout\n");
1114 spin_lock_irq(instance->host_lock);
1115 return -1;
1116 }
1117 spin_lock_irq(instance->host_lock);
1118 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1119 /* Reselection interrupt */
1120 return -1;
1121 }
1122 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1123 break;
1124 spin_unlock_irq(instance->host_lock);
1125 }
1126
1127 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 udelay(3);
1129
1130 /* Check for lost arbitration */
1131 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)) {
1132 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001133 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001134 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Finn Thaincf13b082016-01-03 16:05:18 +11001136
1137 /* After/during arbitration, BSY should be asserted.
1138 * IBM DPES-31080 Version S31Q works now
1139 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1140 */
1141 NCR5380_write(INITIATOR_COMMAND_REG,
1142 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (!(hostdata->flags & FLAG_DTC3181E) &&
1145 /* RvC: DTC3181E has some trouble with this
1146 * so we simply removed it. Seems to work with
1147 * only Mustek scanner attached
1148 */
1149 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1150 NCR5380_write(MODE_REG, MR_BASE);
1151 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001152 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001153 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 /*
1156 * Again, bus clear + bus settle time is 1.2us, however, this is
1157 * a minimum so we'll udelay ceil(1.2)
1158 */
1159
Finn Thain9c3f0e22016-01-03 16:05:11 +11001160 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1161 udelay(15);
1162 else
1163 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Finn Thain52a6a1c2014-03-18 11:42:18 +11001165 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /*
1168 * Now that we have won arbitration, start Selection process, asserting
1169 * the host and target ID's on the SCSI bus.
1170 */
1171
Jeff Garzik422c0d62005-10-24 18:05:09 -04001172 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
1175 * Raise ATN while SEL is true before BSY goes false from arbitration,
1176 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1177 * phase immediately after selection.
1178 */
1179
1180 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1181 NCR5380_write(MODE_REG, MR_BASE);
1182
1183 /*
1184 * Reselect interrupts must be turned off prior to the dropping of BSY,
1185 * otherwise we will trigger an interrupt.
1186 */
1187 NCR5380_write(SELECT_ENABLE_REG, 0);
1188
1189 /*
1190 * The initiator shall then wait at least two deskew delays and release
1191 * the BSY signal.
1192 */
1193 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1194
1195 /* Reset BSY */
1196 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1197
1198 /*
1199 * Something weird happens when we cease to drive BSY - looks
1200 * like the board/chip is letting us do another read before the
1201 * appropriate propagation delay has expired, and we're confusing
1202 * a BSY signal from ourselves as the target's response to SELECTION.
1203 *
1204 * A small delay (the 'C++' frontend breaks the pipeline with an
1205 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1206 * tighter 'C' code breaks and requires this) solves the problem -
1207 * the 1 us delay is arbitrary, and only used because this delay will
1208 * be the same on other platforms and since it works here, it should
1209 * work there.
1210 *
1211 * wingel suggests that this could be due to failing to wait
1212 * one deskew delay.
1213 */
1214
1215 udelay(1);
1216
Finn Thain52a6a1c2014-03-18 11:42:18 +11001217 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * The SCSI specification calls for a 250 ms timeout for the actual
1221 * selection.
1222 */
1223
Finn Thainae753a32016-01-03 16:05:23 +11001224 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1225 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1228 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1229 NCR5380_reselect(instance);
1230 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1231 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1232 return -1;
1233 }
Finn Thainae753a32016-01-03 16:05:23 +11001234
1235 if (err < 0) {
1236 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1237 cmd->result = DID_BAD_TARGET << 16;
1238 cmd->scsi_done(cmd);
1239 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1240 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1241 instance->host_no);
1242 return 0;
1243 }
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * No less than two deskew delays after the initiator detects the
1247 * BSY signal is true, it shall release the SEL signal and may
1248 * change the DATA BUS. -wingel
1249 */
1250
1251 udelay(1);
1252
1253 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /*
1256 * Since we followed the SCSI spec, and raised ATN while SEL
1257 * was true but before BSY was false during selection, the information
1258 * transfer phase should be a MESSAGE OUT phase so that we can send the
1259 * IDENTIFY message.
1260 *
1261 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1262 * message (2 bytes) with a tag ID that we increment with every command
1263 * until it wraps back to 0.
1264 *
1265 * XXX - it turns out that there are some broken SCSI-II devices,
1266 * which claim to support tagged queuing but fail when more than
1267 * some number of commands are issued at once.
1268 */
1269
1270 /* Wait for start of REQ/ACK handshake */
1271
1272 spin_unlock_irq(instance->host_lock);
1273 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1274 spin_lock_irq(instance->host_lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001275 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001276 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1277 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001279 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
Finn Thain52a6a1c2014-03-18 11:42:18 +11001282 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 +11001283 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 len = 1;
1286 cmd->tag = 0;
1287
1288 /* Send message(s) */
1289 data = tmp;
1290 phase = PHASE_MSGOUT;
1291 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001292 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* XXX need to handle errors here */
1294 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001295 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Boaz Harrosh28424d32007-09-10 22:37:45 +03001297 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302/*
1303 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1304 * unsigned char *phase, int *count, unsigned char **data)
1305 *
1306 * Purpose : transfers data in given phase using polled I/O
1307 *
1308 * Inputs : instance - instance of driver, *phase - pointer to
1309 * what phase is expected, *count - pointer to number of
1310 * bytes to transfer, **data - pointer to data pointer.
1311 *
1312 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001313 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * is in same phase.
1315 *
1316 * Also, *phase, *count, *data are modified in place.
1317 *
1318 * XXX Note : handling for bus free may be useful.
1319 */
1320
1321/*
1322 * Note : this code is not as quick as it could be, however it
1323 * IS 100% reliable, and for the actual data transfer where speed
1324 * counts, we will always do a pseudo DMA or DMA transfer.
1325 */
1326
1327static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 unsigned char p = *phase, tmp;
1329 int c = *count;
1330 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001333 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001335 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /*
1338 * The NCR5380 chip will only drive the SCSI bus when the
1339 * phase specified in the appropriate bits of the TARGET COMMAND
1340 * REGISTER match the STATUS REGISTER
1341 */
1342
1343 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 do {
1346 /*
1347 * Wait for assertion of REQ, after which the phase bits will be
1348 * valid
1349 */
1350
Finn Thain686f3992016-01-03 16:05:26 +11001351 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Finn Thain52a6a1c2014-03-18 11:42:18 +11001354 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001357 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001358 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1360 break;
1361 }
1362 /* Do actual transfer from SCSI bus to / from memory */
1363 if (!(p & SR_IO))
1364 NCR5380_write(OUTPUT_DATA_REG, *d);
1365 else
1366 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1367
1368 ++d;
1369
1370 /*
1371 * The SCSI standard suggests that in MSGOUT phase, the initiator
1372 * should drop ATN on the last byte of the message phase
1373 * after REQ has been asserted for the handshake but before
1374 * the initiator raises ACK.
1375 */
1376
1377 if (!(p & SR_IO)) {
1378 if (!((p & SR_MSG) && c > 1)) {
1379 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1380 NCR5380_dprint(NDEBUG_PIO, instance);
1381 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1382 } else {
1383 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1384 NCR5380_dprint(NDEBUG_PIO, instance);
1385 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1386 }
1387 } else {
1388 NCR5380_dprint(NDEBUG_PIO, instance);
1389 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1390 }
1391
Finn Thaina2edc4a2016-01-03 16:05:27 +11001392 if (NCR5380_poll_politely(instance,
1393 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1394 break;
1395
Finn Thain52a6a1c2014-03-18 11:42:18 +11001396 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/*
1399 * We have several special cases to consider during REQ/ACK handshaking :
1400 * 1. We were in MSGOUT phase, and we are on the last byte of the
1401 * message. ATN must be dropped as ACK is dropped.
1402 *
1403 * 2. We are in a MSGIN phase, and we are on the last byte of the
1404 * message. We must exit with ACK asserted, so that the calling
1405 * code may raise ATN before dropping ACK to reject the message.
1406 *
1407 * 3. ACK and ATN are clear and the target may proceed as normal.
1408 */
1409 if (!(p == PHASE_MSGIN && c == 1)) {
1410 if (p == PHASE_MSGOUT && c > 1)
1411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1412 else
1413 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1414 }
1415 } while (--c);
1416
Finn Thain52a6a1c2014-03-18 11:42:18 +11001417 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 *count = c;
1420 *data = d;
1421 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001422 /* The phase read from the bus is valid if either REQ is (already)
1423 * asserted or if ACK hasn't been released yet. The latter applies if
1424 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1425 */
1426 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 *phase = tmp & PHASE_MASK;
1428 else
1429 *phase = PHASE_UNKNOWN;
1430
1431 if (!c || (*phase == p))
1432 return 0;
1433 else
1434 return -1;
1435}
1436
1437/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001438 * do_reset - issue a reset command
1439 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001441 * Issue a reset sequence to the NCR5380 and try and get the bus
1442 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001444 * This clears the reset interrupt flag because there may be no handler for
1445 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1446 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
1448
Finn Thain54d8fe42016-01-03 16:05:06 +11001449static void do_reset(struct Scsi_Host *instance)
1450{
Finn Thain636b1ec2016-01-03 16:05:10 +11001451 unsigned long flags;
1452
1453 local_irq_save(flags);
1454 NCR5380_write(TARGET_COMMAND_REG,
1455 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001457 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001459 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1460 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Finn Thain80d3eb62016-01-03 16:05:34 +11001463/**
1464 * do_abort - abort the currently established nexus by going to
1465 * MESSAGE OUT phase and sending an ABORT message.
1466 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001468 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 */
1470
Finn Thain54d8fe42016-01-03 16:05:06 +11001471static int do_abort(struct Scsi_Host *instance)
1472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 unsigned char *msgptr, phase, tmp;
1474 int len;
1475 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /* Request message out phase */
1478 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1479
1480 /*
1481 * Wait for the target to indicate a valid phase by asserting
1482 * REQ. Once this happens, we'll have either a MSGOUT phase
1483 * and can immediately send the ABORT message, or we'll have some
1484 * other phase and will have to source/sink data.
1485 *
1486 * We really don't care what value was on the bus or what value
1487 * the target sees, so we just handshake.
1488 */
1489
Finn Thain80d3eb62016-01-03 16:05:34 +11001490 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001491 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001492 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Finn Thainf35d3472016-01-03 16:05:33 +11001494 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1497
Finn Thainf35d3472016-01-03 16:05:33 +11001498 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001500 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001501 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001502 goto timeout;
1503 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 tmp = ABORT;
1506 msgptr = &tmp;
1507 len = 1;
1508 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001509 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /*
1512 * If we got here, and the command completed successfully,
1513 * we're about to go into bus free state.
1514 */
1515
1516 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001517
1518timeout:
1519 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1520 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1524/*
1525 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1526 * unsigned char *phase, int *count, unsigned char **data)
1527 *
1528 * Purpose : transfers data in given phase using either real
1529 * or pseudo DMA.
1530 *
1531 * Inputs : instance - instance of driver, *phase - pointer to
1532 * what phase is expected, *count - pointer to number of
1533 * bytes to transfer, **data - pointer to data pointer.
1534 *
1535 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001536 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * is in same phase.
1538 *
1539 * Also, *phase, *count, *data are modified in place.
1540 *
1541 * Locks: io_request lock held by caller
1542 */
1543
1544
1545static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 register int c = *count;
1547 register unsigned char p = *phase;
1548 register unsigned char *d = *data;
1549 unsigned char tmp;
1550 int foo;
1551#if defined(REAL_DMA_POLL)
1552 int cnt, toPIO;
1553 unsigned char saved_data = 0, overrun = 0, residue;
1554#endif
1555
1556 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1559 *phase = tmp;
1560 return -1;
1561 }
1562#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1563#ifdef READ_OVERRUNS
1564 if (p & SR_IO) {
1565 c -= 2;
1566 }
1567#endif
Finn Thain52a6a1c2014-03-18 11:42:18 +11001568 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 -07001569 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1570#endif
1571
1572 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1573
1574#ifdef REAL_DMA
1575 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1576#elif defined(REAL_DMA_POLL)
1577 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1578#else
1579 /*
1580 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1581 * were not disabled for the duration of a single DMA transfer, from
1582 * before the setting of DMA mode to after transfer of the last byte.
1583 */
1584
1585#if defined(PSEUDO_DMA) && defined(UNSAFE)
1586 spin_unlock_irq(instance->host_lock);
1587#endif
1588 /* KLL May need eop and parity in 53c400 */
1589 if (hostdata->flags & FLAG_NCR53C400)
Roel Kluin085267a2010-08-10 18:01:11 -07001590 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1591 MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1592 MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 else
1594 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1595#endif /* def REAL_DMA */
1596
Finn Thain52a6a1c2014-03-18 11:42:18 +11001597 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /*
1600 * On the PAS16 at least I/O recovery delays are not needed here.
1601 * Everyone else seems to want them.
1602 */
1603
1604 if (p & SR_IO) {
1605 io_recovery_delay(1);
1606 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1607 } else {
1608 io_recovery_delay(1);
1609 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1610 io_recovery_delay(1);
1611 NCR5380_write(START_DMA_SEND_REG, 0);
1612 io_recovery_delay(1);
1613 }
1614
1615#if defined(REAL_DMA_POLL)
1616 do {
1617 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1618 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1619
1620/*
1621 At this point, either we've completed DMA, or we have a phase mismatch,
1622 or we've unexpectedly lost BUSY (which is a real error).
1623
1624 For write DMAs, we want to wait until the last byte has been
1625 transferred out over the bus before we turn off DMA mode. Alas, there
1626 seems to be no terribly good way of doing this on a 5380 under all
1627 conditions. For non-scatter-gather operations, we can wait until REQ
1628 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1629 are nastier, since the device will be expecting more data than we
1630 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1631 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1632 why this signal was added to the newer chips) but on the older 538[01]
1633 this signal does not exist. The workaround for this lack is a watchdog;
1634 we bail out of the wait-loop after a modest amount of wait-time if
1635 the usual exit conditions are not met. Not a terribly clean or
1636 correct solution :-%
1637
1638 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1639 If the chip is in DMA mode for an READ, it will respond to a target's
1640 REQ by latching the SCSI data into the INPUT DATA register and asserting
1641 ACK, even if it has _already_ been notified by the DMA controller that
1642 the current DMA transfer has completed! If the NCR5380 is then taken
1643 out of DMA mode, this already-acknowledged byte is lost.
1644
1645 This is not a problem for "one DMA transfer per command" reads, because
1646 the situation will never arise... either all of the data is DMA'ed
1647 properly, or the target switches to MESSAGE IN phase to signal a
1648 disconnection (either operation bringing the DMA to a clean halt).
1649 However, in order to handle scatter-reads, we must work around the
1650 problem. The chosen fix is to DMA N-2 bytes, then check for the
1651 condition before taking the NCR5380 out of DMA mode. One or two extra
1652 bytes are transferred via PIO as necessary to fill out the original
1653 request.
1654 */
1655
1656 if (p & SR_IO) {
1657#ifdef READ_OVERRUNS
1658 udelay(10);
1659 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1660 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1661 overrun = 1;
1662 }
1663#endif
1664 } else {
1665 int limit = 100;
1666 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1667 if (!(tmp & BASR_PHASE_MATCH))
1668 break;
1669 if (--limit < 0)
1670 break;
1671 }
1672 }
1673
Finn Thain52a6a1c2014-03-18 11:42:18 +11001674 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 -07001675
1676 NCR5380_write(MODE_REG, MR_BASE);
1677 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1678
1679 residue = NCR5380_dma_residual(instance);
1680 c -= residue;
1681 *count -= c;
1682 *data += c;
1683 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1684
1685#ifdef READ_OVERRUNS
1686 if (*phase == p && (p & SR_IO) && residue == 0) {
1687 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001688 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 **data = saved_data;
1690 *data += 1;
1691 *count -= 1;
1692 cnt = toPIO = 1;
1693 } else {
1694 printk("No overrun??\n");
1695 cnt = toPIO = 2;
1696 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001697 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 NCR5380_transfer_pio(instance, phase, &cnt, data);
1699 *count -= toPIO - cnt;
1700 }
1701#endif
1702
Finn Thain52a6a1c2014-03-18 11:42:18 +11001703 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 -07001704 return 0;
1705
1706#elif defined(REAL_DMA)
1707 return 0;
1708#else /* defined(REAL_DMA_POLL) */
1709 if (p & SR_IO) {
1710#ifdef DMA_WORKS_RIGHT
1711 foo = NCR5380_pread(instance, d, c);
1712#else
1713 int diff = 1;
1714 if (hostdata->flags & FLAG_NCR53C400) {
1715 diff = 0;
1716 }
1717 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1718 /*
1719 * We can't disable DMA mode after successfully transferring
1720 * what we plan to be the last byte, since that would open up
1721 * a race condition where if the target asserted REQ before
1722 * we got the DMA mode reset, the NCR5380 would have latched
1723 * an additional byte into the INPUT DATA register and we'd
1724 * have dropped it.
1725 *
1726 * The workaround was to transfer one fewer bytes than we
1727 * intended to with the pseudo-DMA read function, wait for
1728 * the chip to latch the last byte, read it, and then disable
1729 * pseudo-DMA mode.
1730 *
1731 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1732 * REQ is deasserted when ACK is asserted, and not reasserted
1733 * until ACK goes false. Since the NCR5380 won't lower ACK
1734 * until DACK is asserted, which won't happen unless we twiddle
1735 * the DMA port or we take the NCR5380 out of DMA mode, we
1736 * can guarantee that we won't handshake another extra
1737 * byte.
1738 */
1739
1740 if (!(hostdata->flags & FLAG_NCR53C400)) {
1741 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1742 /* Wait for clean handshake */
1743 while (NCR5380_read(STATUS_REG) & SR_REQ);
1744 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1745 }
1746 }
1747#endif
1748 } else {
1749#ifdef DMA_WORKS_RIGHT
1750 foo = NCR5380_pwrite(instance, d, c);
1751#else
1752 int timeout;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001753 dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1755 /*
1756 * Wait for the last byte to be sent. If REQ is being asserted for
1757 * the byte we're interested, we'll ACK it and it will go false.
1758 */
1759 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
1760 timeout = 20000;
1761 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
1762
1763 if (!timeout)
Finn Thain52a6a1c2014-03-18 11:42:18 +11001764 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
1767 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1768 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
1769 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001770 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 }
1773 } else {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001774 dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
Finn Thain52a6a1c2014-03-18 11:42:18 +11001776 dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 }
1779#endif
1780 }
1781 NCR5380_write(MODE_REG, MR_BASE);
1782 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1783
1784 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001785 dprintk(NDEBUG_C400_PWRITE, "53C400w: Checking for IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001787 dprintk(NDEBUG_C400_PWRITE, "53C400w: got it, reading reset interrupt reg\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1789 } else {
1790 printk("53C400w: IRQ NOT THERE!\n");
1791 }
1792 }
1793 *data = d + c;
1794 *count = 0;
1795 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1796#if defined(PSEUDO_DMA) && defined(UNSAFE)
1797 spin_lock_irq(instance->host_lock);
1798#endif /* defined(REAL_DMA_POLL) */
1799 return foo;
1800#endif /* def REAL_DMA */
1801}
1802#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1803
1804/*
1805 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1806 *
1807 * Purpose : run through the various SCSI phases and do as the target
1808 * directs us to. Operates on the currently connected command,
1809 * instance->connected.
1810 *
1811 * Inputs : instance, instance for which we are doing commands
1812 *
1813 * Side effects : SCSI things happen, the disconnected queue will be
1814 * modified if a command disconnects, *instance->connected will
1815 * change.
1816 *
1817 * XXX Note : we need to watch for bus free or a reset condition here
1818 * to recover from an unexpected bus free condition.
1819 *
1820 * Locks: io_request_lock held by caller in IRQ mode
1821 */
1822
1823static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1825 unsigned char msgout = NOP;
1826 int sink = 0;
1827 int len;
1828#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1829 int transfersize;
1830#endif
1831 unsigned char *data;
1832 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001833 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 while (1) {
1836 tmp = NCR5380_read(STATUS_REG);
1837 /* We only have a valid SCSI phase when REQ is asserted */
1838 if (tmp & SR_REQ) {
1839 phase = (tmp & PHASE_MASK);
1840 if (phase != old_phase) {
1841 old_phase = phase;
1842 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1843 }
1844 if (sink && (phase != PHASE_MSGOUT)) {
1845 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1846
1847 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1848 while (NCR5380_read(STATUS_REG) & SR_REQ);
1849 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1850 sink = 0;
1851 continue;
1852 }
1853 switch (phase) {
1854 case PHASE_DATAIN:
1855 case PHASE_DATAOUT:
1856#if (NDEBUG & NDEBUG_NO_DATAOUT)
1857 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1858 sink = 1;
1859 do_abort(instance);
1860 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001861 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return;
1863#endif
1864 /*
1865 * If there is no room left in the current buffer in the
1866 * scatter-gather list, move onto the next one.
1867 */
1868
1869 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1870 ++cmd->SCp.buffer;
1871 --cmd->SCp.buffers_residual;
1872 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001873 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001874 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 -07001875 }
1876 /*
1877 * The preferred transfer method is going to be
1878 * PSEUDO-DMA for systems that are strictly PIO,
1879 * since we can let the hardware do the handshaking.
1880 *
1881 * For this to work, we need to know the transfersize
1882 * ahead of time, since the pseudo-DMA code will sit
1883 * in an unconditional loop.
1884 */
1885
1886#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001887 transfersize = 0;
1888 if (!cmd->device->borken &&
1889 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1890 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Finn Thainff3d4572016-01-03 16:05:25 +11001892 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 len = transfersize;
1894 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1895 /*
1896 * If the watchdog timer fires, all future accesses to this
1897 * device will use the polled-IO.
1898 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001899 scmd_printk(KERN_INFO, cmd,
1900 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 sink = 1;
1903 do_abort(instance);
1904 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001905 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* XXX - need to source or sink data here, as appropriate */
1907 } else
1908 cmd->SCp.this_residual -= transfersize - len;
1909 } else
1910#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1911 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1912 &cmd->SCp.ptr);
1913 break;
1914 case PHASE_MSGIN:
1915 len = 1;
1916 data = &tmp;
1917 NCR5380_transfer_pio(instance, &phase, &len, &data);
1918 cmd->SCp.Message = tmp;
1919
1920 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 case ABORT:
1922 case COMMAND_COMPLETE:
1923 /* Accept message by clearing ACK */
1924 sink = 1;
1925 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1926 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001927 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1928 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /*
1931 * I'm not sure what the correct thing to do here is :
1932 *
1933 * If the command that just executed is NOT a request
1934 * sense, the obvious thing to do is to set the result
1935 * code to the values of the stored parameters.
1936 *
1937 * If it was a REQUEST SENSE command, we need some way
1938 * to differentiate between the failure code of the original
1939 * and the failure code of the REQUEST sense - the obvious
1940 * case is success, where we fall through and leave the result
1941 * code unchanged.
1942 *
1943 * The non-obvious place is where the REQUEST SENSE failed
1944 */
1945
1946 if (cmd->cmnd[0] != REQUEST_SENSE)
1947 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1948 else if (status_byte(cmd->SCp.Status) != GOOD)
1949 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1950
Boaz Harrosh28424d32007-09-10 22:37:45 +03001951 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1952 hostdata->ses.cmd_len) {
1953 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1954 hostdata->ses.cmd_len = 0 ;
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Boaz Harrosh28424d32007-09-10 22:37:45 +03001957 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1958 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1959
Finn Thain52a6a1c2014-03-18 11:42:18 +11001960 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 LIST(cmd, hostdata->issue_queue);
1963 cmd->host_scribble = (unsigned char *)
1964 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11001965 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001966 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11001967 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 cmd->scsi_done(cmd);
1969 }
1970
1971 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1972 /*
1973 * Restore phase bits to 0 so an interrupted selection,
1974 * arbitration can resume.
1975 */
1976 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return;
1978 case MESSAGE_REJECT:
1979 /* Accept message by clearing ACK */
1980 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1981 switch (hostdata->last_message) {
1982 case HEAD_OF_QUEUE_TAG:
1983 case ORDERED_QUEUE_TAG:
1984 case SIMPLE_QUEUE_TAG:
1985 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001986 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 break;
1988 default:
1989 break;
1990 }
Finn Thain340b9612016-01-03 16:05:31 +11001991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 case DISCONNECT:{
1993 /* Accept message by clearing ACK */
1994 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 LIST(cmd, hostdata->disconnected_queue);
1996 cmd->host_scribble = (unsigned char *)
1997 hostdata->disconnected_queue;
1998 hostdata->connected = NULL;
1999 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002000 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d lun %llu was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
2002 * Restore phase bits to 0 so an interrupted selection,
2003 * arbitration can resume.
2004 */
2005 NCR5380_write(TARGET_COMMAND_REG, 0);
2006
2007 /* Enable reselect interrupts */
2008 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return;
2010 }
2011 /*
2012 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2013 * operation, in violation of the SCSI spec so we can safely
2014 * ignore SAVE/RESTORE pointers calls.
2015 *
2016 * Unfortunately, some disks violate the SCSI spec and
2017 * don't issue the required SAVE_POINTERS message before
2018 * disconnecting, and we have to break spec to remain
2019 * compatible.
2020 */
2021 case SAVE_POINTERS:
2022 case RESTORE_POINTERS:
2023 /* Accept message by clearing ACK */
2024 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2025 break;
2026 case EXTENDED_MESSAGE:
2027/*
2028 * Extended messages are sent in the following format :
2029 * Byte
2030 * 0 EXTENDED_MESSAGE == 1
2031 * 1 length (includes one byte for code, doesn't
2032 * include first two bytes)
2033 * 2 code
2034 * 3..length+1 arguments
2035 *
2036 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002037 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 */
2039 extended_msg[0] = EXTENDED_MESSAGE;
2040 /* Accept first byte by clearing ACK */
2041 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002042 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 len = 2;
2045 data = extended_msg + 1;
2046 phase = PHASE_MSGIN;
2047 NCR5380_transfer_pio(instance, &phase, &len, &data);
2048
Finn Thain52a6a1c2014-03-18 11:42:18 +11002049 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 -07002050
2051 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2052 /* Accept third byte by clearing ACK */
2053 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2054 len = extended_msg[1] - 1;
2055 data = extended_msg + 3;
2056 phase = PHASE_MSGIN;
2057
2058 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002059 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 switch (extended_msg[2]) {
2062 case EXTENDED_SDTR:
2063 case EXTENDED_WDTR:
2064 case EXTENDED_MODIFY_DATA_POINTER:
2065 case EXTENDED_EXTENDED_IDENTIFY:
2066 tmp = 0;
2067 }
2068 } else if (len) {
2069 printk("scsi%d: error receiving extended message\n", instance->host_no);
2070 tmp = 0;
2071 } else {
2072 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2073 tmp = 0;
2074 }
2075 /* Fall through to reject message */
2076
2077 /*
2078 * If we get something weird that we aren't expecting,
2079 * reject it.
2080 */
2081 default:
2082 if (!tmp) {
2083 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002084 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 printk("\n");
2086 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002087 scmd_printk(KERN_INFO, cmd,
2088 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002090 scmd_printk(KERN_INFO, cmd,
2091 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 msgout = MESSAGE_REJECT;
2094 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2095 break;
2096 } /* switch (tmp) */
2097 break;
2098 case PHASE_MSGOUT:
2099 len = 1;
2100 data = &msgout;
2101 hostdata->last_message = msgout;
2102 NCR5380_transfer_pio(instance, &phase, &len, &data);
2103 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002104 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 hostdata->connected = NULL;
2106 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 cmd->scsi_done(cmd);
2108 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2109 return;
2110 }
2111 msgout = NOP;
2112 break;
2113 case PHASE_CMDOUT:
2114 len = cmd->cmd_len;
2115 data = cmd->cmnd;
2116 /*
2117 * XXX for performance reasons, on machines with a
2118 * PSEUDO-DMA architecture we should probably
2119 * use the dma transfer function.
2120 */
2121 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 break;
2123 case PHASE_STATIN:
2124 len = 1;
2125 data = &tmp;
2126 NCR5380_transfer_pio(instance, &phase, &len, &data);
2127 cmd->SCp.Status = tmp;
2128 break;
2129 default:
2130 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002131 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002133 } else {
2134 spin_unlock_irq(instance->host_lock);
2135 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2136 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 } /* while (1) */
2139}
2140
2141/*
2142 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2143 *
2144 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002145 * 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 -07002146 * nexus has been reestablished,
2147 *
2148 * Inputs : instance - this instance of the NCR5380.
2149 *
2150 * Locks: io_request_lock held by caller if IRQ driven
2151 */
2152
2153static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2155 instance->hostdata;
2156 unsigned char target_mask;
2157 unsigned char lun, phase;
2158 int len;
2159 unsigned char msg[3];
2160 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002161 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int abort = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 /*
2165 * Disable arbitration, etc. since the host adapter obviously
2166 * lost, and tell an interrupted NCR5380_select() to restart.
2167 */
2168
2169 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002172 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 /*
2175 * At this point, we have detected that our SCSI ID is on the bus,
2176 * SEL is true and BSY was false for at least one bus settle delay
2177 * (400 ns).
2178 *
2179 * We must assert BSY ourselves, until the target drops the SEL
2180 * signal.
2181 */
2182
2183 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2184
2185 /* FIXME: timeout too long, must fail to workqueue */
2186 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2187 abort = 1;
2188
2189 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2190
2191 /*
2192 * Wait for target to go into MSGIN.
2193 * FIXME: timeout needed and fail to work queeu
2194 */
2195
Finn Thain1cc160e2016-01-03 16:05:32 +11002196 if (NCR5380_poll_politely(instance,
2197 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 abort = 1;
2199
2200 len = 1;
2201 data = msg;
2202 phase = PHASE_MSGIN;
2203 NCR5380_transfer_pio(instance, &phase, &len, &data);
2204
2205 if (!(msg[0] & 0x80)) {
2206 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002207 spi_print_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 abort = 1;
2209 } else {
2210 /* Accept message by clearing ACK */
2211 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2212 lun = (msg[0] & 0x07);
2213
2214 /*
2215 * We need to add code for SCSI-II to track which devices have
2216 * I_T_L_Q nexuses established, and which have simple I_T_L
2217 * nexuses so we can chose to do additional data transfer.
2218 */
2219
2220 /*
2221 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2222 * just reestablished, and remove it from the disconnected queue.
2223 */
2224
2225
Finn Thain710ddd02014-11-12 16:12:02 +11002226 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002227 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 ) {
2229 if (prev) {
2230 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2231 prev->host_scribble = tmp->host_scribble;
2232 } else {
2233 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002234 hostdata->disconnected_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 tmp->host_scribble = NULL;
2237 break;
2238 }
2239 if (!tmp) {
2240 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2241 /*
2242 * Since we have an established nexus that we can't do anything with,
2243 * we must abort it.
2244 */
2245 abort = 1;
2246 }
2247 }
2248
2249 if (abort) {
2250 do_abort(instance);
2251 } else {
2252 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002253 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n", instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255}
2256
2257/*
2258 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2259 *
2260 * Purpose : called by interrupt handler when DMA finishes or a phase
2261 * mismatch occurs (which would finish the DMA transfer).
2262 *
2263 * Inputs : instance - this instance of the NCR5380.
2264 *
Finn Thain710ddd02014-11-12 16:12:02 +11002265 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 * nexus has been reestablished, on failure NULL is returned.
2267 */
2268
2269#ifdef REAL_DMA
2270static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002271 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /*
2275 * XXX this might not be right.
2276 *
2277 * Wait for final byte to transfer, ie wait for ACK to go false.
2278 *
2279 * We should use the Last Byte Sent bit, unfortunately this is
2280 * not available on the 5380/5381 (only the various CMOS chips)
2281 *
2282 * FIXME: timeout, and need to handle long timeout/irq case
2283 */
2284
2285 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2286
2287 NCR5380_write(MODE_REG, MR_BASE);
2288 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2289
2290 /*
2291 * The only places we should see a phase mismatch and have to send
2292 * data from the same set of pointers will be the data transfer
2293 * phases. So, residual, requested length are only important here.
2294 */
2295
2296 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2297 transferred = instance->dmalen - NCR5380_dma_residual();
2298 hostdata->connected->SCp.this_residual -= transferred;
2299 hostdata->connected->SCp.ptr += transferred;
2300 }
2301}
2302#endif /* def REAL_DMA */
2303
2304/*
Finn Thain710ddd02014-11-12 16:12:02 +11002305 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 *
2307 * Purpose : abort a command
2308 *
Finn Thain710ddd02014-11-12 16:12:02 +11002309 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002310 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * used.
2312 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002313 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002315 * XXX - there is no way to abort the command that is currently
2316 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 * a problem, we could implement longjmp() / setjmp(), setjmp()
2318 * called where the loop started in NCR5380_main().
2319 *
2320 * Locks: host lock taken by caller
2321 */
2322
Finn Thain710ddd02014-11-12 16:12:02 +11002323static int NCR5380_abort(struct scsi_cmnd *cmd)
2324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 struct Scsi_Host *instance = cmd->device->host;
2326 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002327 struct scsi_cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002328
2329 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Finn Thain9dafbd92016-01-03 16:05:28 +11002331 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 NCR5380_print_status(instance);
2333
Finn Thain52a6a1c2014-03-18 11:42:18 +11002334 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2335 dprintk(NDEBUG_ABORT, " basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337#if 0
2338/*
2339 * Case 1 : If the command is the currently executing command,
2340 * we'll set the aborted flag and return control so that
2341 * information transfer routine can exit cleanly.
2342 */
2343
2344 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002345 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346/*
2347 * We should perform BSY checking, and make sure we haven't slipped
2348 * into BUS FREE.
2349 */
2350
2351 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2352/*
2353 * Since we can't change phases until we've completed the current
2354 * handshake, we have to source or sink a byte of data if the current
2355 * phase is not MSGOUT.
2356 */
2357
2358/*
2359 * Return control to the executing NCR drive so we can clear the
2360 * aborted flag and get back into our main loop.
2361 */
2362
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002363 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365#endif
2366
2367/*
2368 * Case 2 : If the command hasn't been issued yet, we simply remove it
2369 * from the issue queue.
2370 */
2371
Finn Thain52a6a1c2014-03-18 11:42:18 +11002372 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002373 for (prev = (struct scsi_cmnd **) &(hostdata->issue_queue), tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (cmd == tmp) {
2375 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002376 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002378 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002380 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002381 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return SUCCESS;
2383 }
2384#if (NDEBUG & NDEBUG_ABORT)
2385 /* KLL */
2386 else if (prev == tmp)
2387 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2388#endif
2389
2390/*
2391 * Case 3 : If any commands are connected, we're going to fail the abort
2392 * and let the high level SCSI driver retry at a later time or
2393 * issue a reset.
2394 *
2395 * Timeouts, and therefore aborted commands, will be highly unlikely
2396 * and handling them cleanly in this situation would make the common
2397 * case of noresets less efficient, and would pollute our code. So,
2398 * we fail.
2399 */
2400
2401 if (hostdata->connected) {
Finn Thain9dafbd92016-01-03 16:05:28 +11002402 spin_unlock_irq(instance->host_lock);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002403 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return FAILED;
2405 }
2406/*
2407 * Case 4: If the command is currently disconnected from the bus, and
2408 * there are no connected commands, we reconnect the I_T_L or
2409 * I_T_L_Q nexus associated with it, go into message out, and send
2410 * an abort message.
2411 *
2412 * This case is especially ugly. In order to reestablish the nexus, we
2413 * need to call NCR5380_select(). The easiest way to implement this
2414 * function was to abort if the bus was busy, and let the interrupt
2415 * handler triggered on the SEL for reselect take care of lost arbitrations
2416 * where necessary, meaning interrupts need to be enabled.
2417 *
2418 * When interrupts are enabled, the queues may change - so we
2419 * can't remove it from the disconnected queue before selecting it
2420 * because that could cause a failure in hashing the nexus if that
2421 * device reselected.
2422 *
2423 * Since the queues may change, we can't use the pointers from when we
2424 * first locate it.
2425 *
2426 * So, we must first locate the command, and if NCR5380_select()
2427 * succeeds, then issue the abort, relocate the command and remove
2428 * it from the disconnected queue.
2429 */
2430
Finn Thain710ddd02014-11-12 16:12:02 +11002431 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002433 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Finn Thain9dafbd92016-01-03 16:05:28 +11002435 if (NCR5380_select(instance, cmd)) {
2436 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return FAILED;
Finn Thain9dafbd92016-01-03 16:05:28 +11002438 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11002439 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 do_abort(instance);
2442
Finn Thain710ddd02014-11-12 16:12:02 +11002443 for (prev = (struct scsi_cmnd **) &(hostdata->disconnected_queue), tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (cmd == tmp) {
2445 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002446 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002448 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002450 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return SUCCESS;
2452 }
2453 }
2454/*
2455 * Case 5 : If we reached this point, the command was not found in any of
2456 * the queues.
2457 *
2458 * We probably reached this point because of an unlikely race condition
2459 * between the command completing successfully and the abortion code,
2460 * so we won't panic, but we will notify the user in case something really
2461 * broke.
2462 */
Finn Thain9dafbd92016-01-03 16:05:28 +11002463 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2465 " before abortion\n", instance->host_no);
2466 return FAILED;
2467}
2468
2469
Finn Thain3be1b3e2016-01-03 16:05:12 +11002470/**
2471 * NCR5380_bus_reset - reset the SCSI bus
2472 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002474 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 */
2476
Finn Thain710ddd02014-11-12 16:12:02 +11002477static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002478{
2479 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002481 spin_lock_irq(instance->host_lock);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002482
2483#if (NDEBUG & NDEBUG_ANY)
2484 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2485 NCR5380_print_status(instance);
2486#endif
2487
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002488 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002489
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002490 spin_unlock_irq(instance->host_lock);
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return SUCCESS;
2493}