blob: 334d253fe75a36dd974601001c455e0f26786f8f [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
David Howellsc4028952006-11-22 14:57:56 +0000700 INIT_DELAYED_WORK(&hostdata->coroutine, 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
Tejun Heoa684b8d2011-01-24 14:57:28 +0100800 cancel_delayed_work_sync(&hostdata->coroutine);
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
862 /* Run the coroutine if it isn't already running. */
863 /* Kick off command processing */
Finn Thain0ad0eff2016-01-03 16:05:21 +1100864 queue_delayed_work(hostdata->work_q, &hostdata->coroutine, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
869 * NCR5380_main - NCR state machines
870 *
871 * NCR5380_main is a coroutine that runs as long as more work can
872 * be done on the NCR5380 host adapters in a system. Both
873 * NCR5380_queue_command() and NCR5380_intr() will try to start it
874 * in case it is not running.
875 *
876 * Locks: called as its own thread with no locks held. Takes the
877 * host lock and called routines may take the isa dma lock.
878 */
879
David Howellsc4028952006-11-22 14:57:56 +0000880static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
David Howellsc4028952006-11-22 14:57:56 +0000882 struct NCR5380_hostdata *hostdata =
883 container_of(work, struct NCR5380_hostdata, coroutine.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100885 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int done;
887
888 spin_lock_irq(instance->host_lock);
889 do {
890 /* Lock held here */
891 done = 1;
Finn Thainae753a32016-01-03 16:05:23 +1100892 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100893 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Search through the issue_queue for a command destined
896 * for a target that's not busy.
897 */
Finn Thain710ddd02014-11-12 16:12:02 +1100898 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 {
900 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200901 dprintk(NDEBUG_LISTS, "MAIN tmp=%p target=%d busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200903 if (!(hostdata->busy[tmp->device->id] &
904 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (prev) {
906 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
907 prev->host_scribble = tmp->host_scribble;
908 } else {
909 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +1100910 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 tmp->host_scribble = NULL;
913
914 /*
915 * Attempt to establish an I_T_L nexus here.
916 * On success, instance->hostdata->connected is set.
917 * On failure, we must add the command back to the
918 * issue queue so we can keep trying.
919 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200920 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Finn Thain76f13b92014-11-12 16:11:53 +1100922 /*
923 * REQUEST SENSE commands are issued without tagged
924 * queueing, even on SCSI-II devices because the
925 * contingent allegiance condition exists for the
926 * entire unit.
927 */
928
929 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100930 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100932 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 LIST(tmp, hostdata->issue_queue);
934 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
935 hostdata->issue_queue = tmp;
936 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100937 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Finn Thainae753a32016-01-03 16:05:23 +1100939 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* lock held here still */
942 } /* if target/lun is not busy */
943 } /* for */
944 /* exited locked */
945 } /* if (!hostdata->connected) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (hostdata->connected
947#ifdef REAL_DMA
948 && !hostdata->dmalen
949#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100951 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100953 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 } while (!done);
957
958 spin_unlock_irq(instance->host_lock);
959}
960
961#ifndef DONT_USE_INTR
962
963/**
964 * NCR5380_intr - generic NCR5380 irq handler
965 * @irq: interrupt number
966 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 *
968 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
969 * from the disconnected queue, and restarting NCR5380_main()
970 * as required.
971 *
972 * Locks: takes the needed instance locks
973 */
974
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800975static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800977 struct Scsi_Host *instance = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
979 int done;
980 unsigned char basr;
981 unsigned long flags;
982
Finn Thain52a6a1c2014-03-18 11:42:18 +1100983 dprintk(NDEBUG_INTR, "scsi : NCR5380 irq %d triggered\n",
984 instance->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 do {
987 done = 1;
988 spin_lock_irqsave(instance->host_lock, flags);
989 /* Look for pending interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 basr = NCR5380_read(BUS_AND_STATUS_REG);
991 /* XXX dispatch to appropriate routine if found and done=0 */
992 if (basr & BASR_IRQ) {
993 NCR5380_dprint(NDEBUG_INTR, instance);
994 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
995 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100996 dprintk(NDEBUG_INTR, "scsi%d : SEL interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 NCR5380_reselect(instance);
998 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
999 } else if (basr & BASR_PARITY_ERROR) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001000 dprintk(NDEBUG_INTR, "scsi%d : PARITY interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1002 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001003 dprintk(NDEBUG_INTR, "scsi%d : RESET interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1005 } else {
1006#if defined(REAL_DMA)
1007 /*
1008 * We should only get PHASE MISMATCH and EOP interrupts
1009 * if we have DMA enabled, so do a sanity check based on
1010 * the current setting of the MODE register.
1011 */
1012
1013 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001014 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (!hostdata->connected)
1017 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1018
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001019 transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 hostdata->connected->SCp.this_residual -= transferred;
1021 hostdata->connected->SCp.ptr += transferred;
1022 hostdata->dmalen = 0;
1023
1024 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1025
1026 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1027 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1028
1029 NCR5380_write(MODE_REG, MR_BASE);
1030 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1031 }
1032#else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001033 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 -07001034 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1035#endif
1036 }
1037 } /* if BASR_IRQ */
1038 spin_unlock_irqrestore(instance->host_lock, flags);
1039 if(!done)
Finn Thain0ad0eff2016-01-03 16:05:21 +11001040 queue_delayed_work(hostdata->work_q,
1041 &hostdata->coroutine, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 } while (!done);
1043 return IRQ_HANDLED;
1044}
1045
1046#endif
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
Finn Thain710ddd02014-11-12 16:12:02 +11001049 * Function : int NCR5380_select(struct Scsi_Host *instance,
1050 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
1052 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1053 * including ARBITRATION, SELECTION, and initial message out for
1054 * IDENTIFY and queue messages.
1055 *
1056 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001057 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 *
Finn Thain63238762016-01-03 16:05:15 +11001059 * Returns : -1 if selection failed but should be retried.
1060 * 0 if selection failed and should not be retried.
1061 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *
1063 * Side effects :
1064 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1065 * with registers as they should have been on entry - ie
1066 * SELECT_ENABLE will be set appropriately, the NCR5380
1067 * will cease to drive any SCSI bus signals.
1068 *
1069 * If successful : I_T_L or I_T_L_Q nexus will be established,
1070 * instance->connected will be set to cmd.
1071 * SELECT interrupt will be disabled.
1072 *
1073 * If failed (no target) : cmd->scsi_done() will be called, and the
1074 * cmd->result host byte set to DID_BAD_TARGET.
1075 *
1076 * Locks: caller holds hostdata lock in IRQ mode
1077 */
1078
Finn Thain710ddd02014-11-12 16:12:02 +11001079static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1082 unsigned char tmp[3], phase;
1083 unsigned char *data;
1084 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001088 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /*
1091 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1092 * data bus during SELECTION.
1093 */
1094
1095 NCR5380_write(TARGET_COMMAND_REG, 0);
1096
1097 /*
1098 * Start arbitration.
1099 */
1100
1101 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1102 NCR5380_write(MODE_REG, MR_ARBITRATE);
1103
1104
1105 /* We can be relaxed here, interrupts are on, we are
1106 in workqueue context, the birds are singing in the trees */
1107 spin_unlock_irq(instance->host_lock);
1108 err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
1109 spin_lock_irq(instance->host_lock);
1110 if (err < 0) {
1111 printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
1112 NCR5380_write(MODE_REG, MR_BASE);
1113 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001114 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
Finn Thain52a6a1c2014-03-18 11:42:18 +11001117 dprintk(NDEBUG_ARBITRATION, "scsi%d : arbitration complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /*
1120 * The arbitration delay is 2.2us, but this is a minimum and there is
1121 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1122 * the integral nature of udelay().
1123 *
1124 */
1125
1126 udelay(3);
1127
1128 /* Check for lost arbitration */
1129 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)) {
1130 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001131 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001132 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Finn Thaincf13b082016-01-03 16:05:18 +11001134
1135 /* After/during arbitration, BSY should be asserted.
1136 * IBM DPES-31080 Version S31Q works now
1137 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1138 */
1139 NCR5380_write(INITIATOR_COMMAND_REG,
1140 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (!(hostdata->flags & FLAG_DTC3181E) &&
1143 /* RvC: DTC3181E has some trouble with this
1144 * so we simply removed it. Seems to work with
1145 * only Mustek scanner attached
1146 */
1147 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1148 NCR5380_write(MODE_REG, MR_BASE);
1149 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001150 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001151 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153 /*
1154 * Again, bus clear + bus settle time is 1.2us, however, this is
1155 * a minimum so we'll udelay ceil(1.2)
1156 */
1157
Finn Thain9c3f0e22016-01-03 16:05:11 +11001158 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1159 udelay(15);
1160 else
1161 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Finn Thain52a6a1c2014-03-18 11:42:18 +11001163 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /*
1166 * Now that we have won arbitration, start Selection process, asserting
1167 * the host and target ID's on the SCSI bus.
1168 */
1169
Jeff Garzik422c0d62005-10-24 18:05:09 -04001170 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /*
1173 * Raise ATN while SEL is true before BSY goes false from arbitration,
1174 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1175 * phase immediately after selection.
1176 */
1177
1178 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1179 NCR5380_write(MODE_REG, MR_BASE);
1180
1181 /*
1182 * Reselect interrupts must be turned off prior to the dropping of BSY,
1183 * otherwise we will trigger an interrupt.
1184 */
1185 NCR5380_write(SELECT_ENABLE_REG, 0);
1186
1187 /*
1188 * The initiator shall then wait at least two deskew delays and release
1189 * the BSY signal.
1190 */
1191 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1192
1193 /* Reset BSY */
1194 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1195
1196 /*
1197 * Something weird happens when we cease to drive BSY - looks
1198 * like the board/chip is letting us do another read before the
1199 * appropriate propagation delay has expired, and we're confusing
1200 * a BSY signal from ourselves as the target's response to SELECTION.
1201 *
1202 * A small delay (the 'C++' frontend breaks the pipeline with an
1203 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1204 * tighter 'C' code breaks and requires this) solves the problem -
1205 * the 1 us delay is arbitrary, and only used because this delay will
1206 * be the same on other platforms and since it works here, it should
1207 * work there.
1208 *
1209 * wingel suggests that this could be due to failing to wait
1210 * one deskew delay.
1211 */
1212
1213 udelay(1);
1214
Finn Thain52a6a1c2014-03-18 11:42:18 +11001215 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /*
1218 * The SCSI specification calls for a 250 ms timeout for the actual
1219 * selection.
1220 */
1221
Finn Thainae753a32016-01-03 16:05:23 +11001222 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1223 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1226 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1227 NCR5380_reselect(instance);
1228 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1229 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1230 return -1;
1231 }
Finn Thainae753a32016-01-03 16:05:23 +11001232
1233 if (err < 0) {
1234 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1235 cmd->result = DID_BAD_TARGET << 16;
1236 cmd->scsi_done(cmd);
1237 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1238 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1239 instance->host_no);
1240 return 0;
1241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * No less than two deskew delays after the initiator detects the
1245 * BSY signal is true, it shall release the SEL signal and may
1246 * change the DATA BUS. -wingel
1247 */
1248
1249 udelay(1);
1250
1251 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Since we followed the SCSI spec, and raised ATN while SEL
1255 * was true but before BSY was false during selection, the information
1256 * transfer phase should be a MESSAGE OUT phase so that we can send the
1257 * IDENTIFY message.
1258 *
1259 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1260 * message (2 bytes) with a tag ID that we increment with every command
1261 * until it wraps back to 0.
1262 *
1263 * XXX - it turns out that there are some broken SCSI-II devices,
1264 * which claim to support tagged queuing but fail when more than
1265 * some number of commands are issued at once.
1266 */
1267
1268 /* Wait for start of REQ/ACK handshake */
1269
1270 spin_unlock_irq(instance->host_lock);
1271 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1272 spin_lock_irq(instance->host_lock);
1273
Finn Thain1cc160e2016-01-03 16:05:32 +11001274 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1276 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001277 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
Finn Thain52a6a1c2014-03-18 11:42:18 +11001280 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 +11001281 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 len = 1;
1284 cmd->tag = 0;
1285
1286 /* Send message(s) */
1287 data = tmp;
1288 phase = PHASE_MSGOUT;
1289 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001290 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* XXX need to handle errors here */
1292 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001293 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Boaz Harrosh28424d32007-09-10 22:37:45 +03001295 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300/*
1301 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1302 * unsigned char *phase, int *count, unsigned char **data)
1303 *
1304 * Purpose : transfers data in given phase using polled I/O
1305 *
1306 * Inputs : instance - instance of driver, *phase - pointer to
1307 * what phase is expected, *count - pointer to number of
1308 * bytes to transfer, **data - pointer to data pointer.
1309 *
1310 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001311 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * is in same phase.
1313 *
1314 * Also, *phase, *count, *data are modified in place.
1315 *
1316 * XXX Note : handling for bus free may be useful.
1317 */
1318
1319/*
1320 * Note : this code is not as quick as it could be, however it
1321 * IS 100% reliable, and for the actual data transfer where speed
1322 * counts, we will always do a pseudo DMA or DMA transfer.
1323 */
1324
1325static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 unsigned char p = *phase, tmp;
1327 int c = *count;
1328 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001331 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001333 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /*
1336 * The NCR5380 chip will only drive the SCSI bus when the
1337 * phase specified in the appropriate bits of the TARGET COMMAND
1338 * REGISTER match the STATUS REGISTER
1339 */
1340
1341 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 do {
1344 /*
1345 * Wait for assertion of REQ, after which the phase bits will be
1346 * valid
1347 */
1348
Finn Thain686f3992016-01-03 16:05:26 +11001349 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Finn Thain52a6a1c2014-03-18 11:42:18 +11001352 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001355 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001356 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1358 break;
1359 }
1360 /* Do actual transfer from SCSI bus to / from memory */
1361 if (!(p & SR_IO))
1362 NCR5380_write(OUTPUT_DATA_REG, *d);
1363 else
1364 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1365
1366 ++d;
1367
1368 /*
1369 * The SCSI standard suggests that in MSGOUT phase, the initiator
1370 * should drop ATN on the last byte of the message phase
1371 * after REQ has been asserted for the handshake but before
1372 * the initiator raises ACK.
1373 */
1374
1375 if (!(p & SR_IO)) {
1376 if (!((p & SR_MSG) && c > 1)) {
1377 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1378 NCR5380_dprint(NDEBUG_PIO, instance);
1379 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1380 } else {
1381 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1382 NCR5380_dprint(NDEBUG_PIO, instance);
1383 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1384 }
1385 } else {
1386 NCR5380_dprint(NDEBUG_PIO, instance);
1387 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1388 }
1389
Finn Thaina2edc4a2016-01-03 16:05:27 +11001390 if (NCR5380_poll_politely(instance,
1391 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1392 break;
1393
Finn Thain52a6a1c2014-03-18 11:42:18 +11001394 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396/*
1397 * We have several special cases to consider during REQ/ACK handshaking :
1398 * 1. We were in MSGOUT phase, and we are on the last byte of the
1399 * message. ATN must be dropped as ACK is dropped.
1400 *
1401 * 2. We are in a MSGIN phase, and we are on the last byte of the
1402 * message. We must exit with ACK asserted, so that the calling
1403 * code may raise ATN before dropping ACK to reject the message.
1404 *
1405 * 3. ACK and ATN are clear and the target may proceed as normal.
1406 */
1407 if (!(p == PHASE_MSGIN && c == 1)) {
1408 if (p == PHASE_MSGOUT && c > 1)
1409 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1410 else
1411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1412 }
1413 } while (--c);
1414
Finn Thain52a6a1c2014-03-18 11:42:18 +11001415 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 *count = c;
1418 *data = d;
1419 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001420 /* The phase read from the bus is valid if either REQ is (already)
1421 * asserted or if ACK hasn't been released yet. The latter applies if
1422 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1423 */
1424 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *phase = tmp & PHASE_MASK;
1426 else
1427 *phase = PHASE_UNKNOWN;
1428
1429 if (!c || (*phase == p))
1430 return 0;
1431 else
1432 return -1;
1433}
1434
1435/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001436 * do_reset - issue a reset command
1437 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001439 * Issue a reset sequence to the NCR5380 and try and get the bus
1440 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001442 * This clears the reset interrupt flag because there may be no handler for
1443 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1444 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 */
1446
Finn Thain54d8fe42016-01-03 16:05:06 +11001447static void do_reset(struct Scsi_Host *instance)
1448{
Finn Thain636b1ec2016-01-03 16:05:10 +11001449 unsigned long flags;
1450
1451 local_irq_save(flags);
1452 NCR5380_write(TARGET_COMMAND_REG,
1453 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001455 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001457 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1458 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*
1462 * Function : do_abort (Scsi_Host *host)
1463 *
1464 * Purpose : abort the currently established nexus. Should only be
1465 * called from a routine which can drop into a
1466 *
1467 * Returns : 0 on success, -1 on failure.
1468 *
1469 * Locks: queue lock held by caller
1470 * FIXME: sort this out and get new_eh running
1471 */
1472
Finn Thain54d8fe42016-01-03 16:05:06 +11001473static int do_abort(struct Scsi_Host *instance)
1474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 unsigned char *msgptr, phase, tmp;
1476 int len;
1477 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /* Request message out phase */
1480 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1481
1482 /*
1483 * Wait for the target to indicate a valid phase by asserting
1484 * REQ. Once this happens, we'll have either a MSGOUT phase
1485 * and can immediately send the ABORT message, or we'll have some
1486 * other phase and will have to source/sink data.
1487 *
1488 * We really don't care what value was on the bus or what value
1489 * the target sees, so we just handshake.
1490 */
1491
Finn Thain54d8fe42016-01-03 16:05:06 +11001492 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001493 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -1;
1495
Finn Thainf35d3472016-01-03 16:05:33 +11001496 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1499
Finn Thainf35d3472016-01-03 16:05:33 +11001500 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001502 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Finn Thain1cc160e2016-01-03 16:05:32 +11001504 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -1;
1506 }
1507 tmp = ABORT;
1508 msgptr = &tmp;
1509 len = 1;
1510 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001511 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /*
1514 * If we got here, and the command completed successfully,
1515 * we're about to go into bus free state.
1516 */
1517
1518 return len ? -1 : 0;
1519}
1520
1521#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1522/*
1523 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1524 * unsigned char *phase, int *count, unsigned char **data)
1525 *
1526 * Purpose : transfers data in given phase using either real
1527 * or pseudo DMA.
1528 *
1529 * Inputs : instance - instance of driver, *phase - pointer to
1530 * what phase is expected, *count - pointer to number of
1531 * bytes to transfer, **data - pointer to data pointer.
1532 *
1533 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001534 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * is in same phase.
1536 *
1537 * Also, *phase, *count, *data are modified in place.
1538 *
1539 * Locks: io_request lock held by caller
1540 */
1541
1542
1543static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 register int c = *count;
1545 register unsigned char p = *phase;
1546 register unsigned char *d = *data;
1547 unsigned char tmp;
1548 int foo;
1549#if defined(REAL_DMA_POLL)
1550 int cnt, toPIO;
1551 unsigned char saved_data = 0, overrun = 0, residue;
1552#endif
1553
1554 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1557 *phase = tmp;
1558 return -1;
1559 }
1560#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1561#ifdef READ_OVERRUNS
1562 if (p & SR_IO) {
1563 c -= 2;
1564 }
1565#endif
Finn Thain52a6a1c2014-03-18 11:42:18 +11001566 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 -07001567 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1568#endif
1569
1570 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1571
1572#ifdef REAL_DMA
1573 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1574#elif defined(REAL_DMA_POLL)
1575 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1576#else
1577 /*
1578 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1579 * were not disabled for the duration of a single DMA transfer, from
1580 * before the setting of DMA mode to after transfer of the last byte.
1581 */
1582
1583#if defined(PSEUDO_DMA) && defined(UNSAFE)
1584 spin_unlock_irq(instance->host_lock);
1585#endif
1586 /* KLL May need eop and parity in 53c400 */
1587 if (hostdata->flags & FLAG_NCR53C400)
Roel Kluin085267a2010-08-10 18:01:11 -07001588 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1589 MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1590 MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 else
1592 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1593#endif /* def REAL_DMA */
1594
Finn Thain52a6a1c2014-03-18 11:42:18 +11001595 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
1598 * On the PAS16 at least I/O recovery delays are not needed here.
1599 * Everyone else seems to want them.
1600 */
1601
1602 if (p & SR_IO) {
1603 io_recovery_delay(1);
1604 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1605 } else {
1606 io_recovery_delay(1);
1607 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1608 io_recovery_delay(1);
1609 NCR5380_write(START_DMA_SEND_REG, 0);
1610 io_recovery_delay(1);
1611 }
1612
1613#if defined(REAL_DMA_POLL)
1614 do {
1615 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1616 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1617
1618/*
1619 At this point, either we've completed DMA, or we have a phase mismatch,
1620 or we've unexpectedly lost BUSY (which is a real error).
1621
1622 For write DMAs, we want to wait until the last byte has been
1623 transferred out over the bus before we turn off DMA mode. Alas, there
1624 seems to be no terribly good way of doing this on a 5380 under all
1625 conditions. For non-scatter-gather operations, we can wait until REQ
1626 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1627 are nastier, since the device will be expecting more data than we
1628 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1629 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1630 why this signal was added to the newer chips) but on the older 538[01]
1631 this signal does not exist. The workaround for this lack is a watchdog;
1632 we bail out of the wait-loop after a modest amount of wait-time if
1633 the usual exit conditions are not met. Not a terribly clean or
1634 correct solution :-%
1635
1636 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1637 If the chip is in DMA mode for an READ, it will respond to a target's
1638 REQ by latching the SCSI data into the INPUT DATA register and asserting
1639 ACK, even if it has _already_ been notified by the DMA controller that
1640 the current DMA transfer has completed! If the NCR5380 is then taken
1641 out of DMA mode, this already-acknowledged byte is lost.
1642
1643 This is not a problem for "one DMA transfer per command" reads, because
1644 the situation will never arise... either all of the data is DMA'ed
1645 properly, or the target switches to MESSAGE IN phase to signal a
1646 disconnection (either operation bringing the DMA to a clean halt).
1647 However, in order to handle scatter-reads, we must work around the
1648 problem. The chosen fix is to DMA N-2 bytes, then check for the
1649 condition before taking the NCR5380 out of DMA mode. One or two extra
1650 bytes are transferred via PIO as necessary to fill out the original
1651 request.
1652 */
1653
1654 if (p & SR_IO) {
1655#ifdef READ_OVERRUNS
1656 udelay(10);
1657 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1658 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1659 overrun = 1;
1660 }
1661#endif
1662 } else {
1663 int limit = 100;
1664 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1665 if (!(tmp & BASR_PHASE_MATCH))
1666 break;
1667 if (--limit < 0)
1668 break;
1669 }
1670 }
1671
Finn Thain52a6a1c2014-03-18 11:42:18 +11001672 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 -07001673
1674 NCR5380_write(MODE_REG, MR_BASE);
1675 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1676
1677 residue = NCR5380_dma_residual(instance);
1678 c -= residue;
1679 *count -= c;
1680 *data += c;
1681 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1682
1683#ifdef READ_OVERRUNS
1684 if (*phase == p && (p & SR_IO) && residue == 0) {
1685 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001686 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 **data = saved_data;
1688 *data += 1;
1689 *count -= 1;
1690 cnt = toPIO = 1;
1691 } else {
1692 printk("No overrun??\n");
1693 cnt = toPIO = 2;
1694 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001695 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 NCR5380_transfer_pio(instance, phase, &cnt, data);
1697 *count -= toPIO - cnt;
1698 }
1699#endif
1700
Finn Thain52a6a1c2014-03-18 11:42:18 +11001701 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 -07001702 return 0;
1703
1704#elif defined(REAL_DMA)
1705 return 0;
1706#else /* defined(REAL_DMA_POLL) */
1707 if (p & SR_IO) {
1708#ifdef DMA_WORKS_RIGHT
1709 foo = NCR5380_pread(instance, d, c);
1710#else
1711 int diff = 1;
1712 if (hostdata->flags & FLAG_NCR53C400) {
1713 diff = 0;
1714 }
1715 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1716 /*
1717 * We can't disable DMA mode after successfully transferring
1718 * what we plan to be the last byte, since that would open up
1719 * a race condition where if the target asserted REQ before
1720 * we got the DMA mode reset, the NCR5380 would have latched
1721 * an additional byte into the INPUT DATA register and we'd
1722 * have dropped it.
1723 *
1724 * The workaround was to transfer one fewer bytes than we
1725 * intended to with the pseudo-DMA read function, wait for
1726 * the chip to latch the last byte, read it, and then disable
1727 * pseudo-DMA mode.
1728 *
1729 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1730 * REQ is deasserted when ACK is asserted, and not reasserted
1731 * until ACK goes false. Since the NCR5380 won't lower ACK
1732 * until DACK is asserted, which won't happen unless we twiddle
1733 * the DMA port or we take the NCR5380 out of DMA mode, we
1734 * can guarantee that we won't handshake another extra
1735 * byte.
1736 */
1737
1738 if (!(hostdata->flags & FLAG_NCR53C400)) {
1739 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1740 /* Wait for clean handshake */
1741 while (NCR5380_read(STATUS_REG) & SR_REQ);
1742 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1743 }
1744 }
1745#endif
1746 } else {
1747#ifdef DMA_WORKS_RIGHT
1748 foo = NCR5380_pwrite(instance, d, c);
1749#else
1750 int timeout;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001751 dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1753 /*
1754 * Wait for the last byte to be sent. If REQ is being asserted for
1755 * the byte we're interested, we'll ACK it and it will go false.
1756 */
1757 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
1758 timeout = 20000;
1759 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
1760
1761 if (!timeout)
Finn Thain52a6a1c2014-03-18 11:42:18 +11001762 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
1765 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1766 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
1767 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001768 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 }
1771 } else {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001772 dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
Finn Thain52a6a1c2014-03-18 11:42:18 +11001774 dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 }
1777#endif
1778 }
1779 NCR5380_write(MODE_REG, MR_BASE);
1780 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1781
1782 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001783 dprintk(NDEBUG_C400_PWRITE, "53C400w: Checking for IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001785 dprintk(NDEBUG_C400_PWRITE, "53C400w: got it, reading reset interrupt reg\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1787 } else {
1788 printk("53C400w: IRQ NOT THERE!\n");
1789 }
1790 }
1791 *data = d + c;
1792 *count = 0;
1793 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1794#if defined(PSEUDO_DMA) && defined(UNSAFE)
1795 spin_lock_irq(instance->host_lock);
1796#endif /* defined(REAL_DMA_POLL) */
1797 return foo;
1798#endif /* def REAL_DMA */
1799}
1800#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1801
1802/*
1803 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1804 *
1805 * Purpose : run through the various SCSI phases and do as the target
1806 * directs us to. Operates on the currently connected command,
1807 * instance->connected.
1808 *
1809 * Inputs : instance, instance for which we are doing commands
1810 *
1811 * Side effects : SCSI things happen, the disconnected queue will be
1812 * modified if a command disconnects, *instance->connected will
1813 * change.
1814 *
1815 * XXX Note : we need to watch for bus free or a reset condition here
1816 * to recover from an unexpected bus free condition.
1817 *
1818 * Locks: io_request_lock held by caller in IRQ mode
1819 */
1820
1821static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1823 unsigned char msgout = NOP;
1824 int sink = 0;
1825 int len;
1826#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1827 int transfersize;
1828#endif
1829 unsigned char *data;
1830 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001831 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 while (1) {
1834 tmp = NCR5380_read(STATUS_REG);
1835 /* We only have a valid SCSI phase when REQ is asserted */
1836 if (tmp & SR_REQ) {
1837 phase = (tmp & PHASE_MASK);
1838 if (phase != old_phase) {
1839 old_phase = phase;
1840 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1841 }
1842 if (sink && (phase != PHASE_MSGOUT)) {
1843 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1844
1845 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1846 while (NCR5380_read(STATUS_REG) & SR_REQ);
1847 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1848 sink = 0;
1849 continue;
1850 }
1851 switch (phase) {
1852 case PHASE_DATAIN:
1853 case PHASE_DATAOUT:
1854#if (NDEBUG & NDEBUG_NO_DATAOUT)
1855 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1856 sink = 1;
1857 do_abort(instance);
1858 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001859 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return;
1861#endif
1862 /*
1863 * If there is no room left in the current buffer in the
1864 * scatter-gather list, move onto the next one.
1865 */
1866
1867 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1868 ++cmd->SCp.buffer;
1869 --cmd->SCp.buffers_residual;
1870 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001871 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001872 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 -07001873 }
1874 /*
1875 * The preferred transfer method is going to be
1876 * PSEUDO-DMA for systems that are strictly PIO,
1877 * since we can let the hardware do the handshaking.
1878 *
1879 * For this to work, we need to know the transfersize
1880 * ahead of time, since the pseudo-DMA code will sit
1881 * in an unconditional loop.
1882 */
1883
1884#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001885 transfersize = 0;
1886 if (!cmd->device->borken &&
1887 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1888 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Finn Thainff3d4572016-01-03 16:05:25 +11001890 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 len = transfersize;
1892 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1893 /*
1894 * If the watchdog timer fires, all future accesses to this
1895 * device will use the polled-IO.
1896 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001897 scmd_printk(KERN_INFO, cmd,
1898 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 sink = 1;
1901 do_abort(instance);
1902 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001903 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /* XXX - need to source or sink data here, as appropriate */
1905 } else
1906 cmd->SCp.this_residual -= transfersize - len;
1907 } else
1908#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1909 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1910 &cmd->SCp.ptr);
1911 break;
1912 case PHASE_MSGIN:
1913 len = 1;
1914 data = &tmp;
1915 NCR5380_transfer_pio(instance, &phase, &len, &data);
1916 cmd->SCp.Message = tmp;
1917
1918 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 case ABORT:
1920 case COMMAND_COMPLETE:
1921 /* Accept message by clearing ACK */
1922 sink = 1;
1923 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1924 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001925 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1926 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 /*
1929 * I'm not sure what the correct thing to do here is :
1930 *
1931 * If the command that just executed is NOT a request
1932 * sense, the obvious thing to do is to set the result
1933 * code to the values of the stored parameters.
1934 *
1935 * If it was a REQUEST SENSE command, we need some way
1936 * to differentiate between the failure code of the original
1937 * and the failure code of the REQUEST sense - the obvious
1938 * case is success, where we fall through and leave the result
1939 * code unchanged.
1940 *
1941 * The non-obvious place is where the REQUEST SENSE failed
1942 */
1943
1944 if (cmd->cmnd[0] != REQUEST_SENSE)
1945 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1946 else if (status_byte(cmd->SCp.Status) != GOOD)
1947 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1948
Boaz Harrosh28424d32007-09-10 22:37:45 +03001949 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1950 hostdata->ses.cmd_len) {
1951 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1952 hostdata->ses.cmd_len = 0 ;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Boaz Harrosh28424d32007-09-10 22:37:45 +03001955 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1956 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1957
Finn Thain52a6a1c2014-03-18 11:42:18 +11001958 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 LIST(cmd, hostdata->issue_queue);
1961 cmd->host_scribble = (unsigned char *)
1962 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11001963 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001964 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11001965 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 cmd->scsi_done(cmd);
1967 }
1968
1969 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1970 /*
1971 * Restore phase bits to 0 so an interrupted selection,
1972 * arbitration can resume.
1973 */
1974 NCR5380_write(TARGET_COMMAND_REG, 0);
1975
1976 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
1977 barrier();
1978 return;
1979 case MESSAGE_REJECT:
1980 /* Accept message by clearing ACK */
1981 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1982 switch (hostdata->last_message) {
1983 case HEAD_OF_QUEUE_TAG:
1984 case ORDERED_QUEUE_TAG:
1985 case SIMPLE_QUEUE_TAG:
1986 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001987 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 break;
1989 default:
1990 break;
1991 }
Finn Thain340b9612016-01-03 16:05:31 +11001992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 case DISCONNECT:{
1994 /* Accept message by clearing ACK */
1995 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 LIST(cmd, hostdata->disconnected_queue);
1997 cmd->host_scribble = (unsigned char *)
1998 hostdata->disconnected_queue;
1999 hostdata->connected = NULL;
2000 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002001 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 -07002002 /*
2003 * Restore phase bits to 0 so an interrupted selection,
2004 * arbitration can resume.
2005 */
2006 NCR5380_write(TARGET_COMMAND_REG, 0);
2007
2008 /* Enable reselect interrupts */
2009 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2010 /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
2011 /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
2012 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2013 barrier();
2014 return;
2015 }
2016 /*
2017 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2018 * operation, in violation of the SCSI spec so we can safely
2019 * ignore SAVE/RESTORE pointers calls.
2020 *
2021 * Unfortunately, some disks violate the SCSI spec and
2022 * don't issue the required SAVE_POINTERS message before
2023 * disconnecting, and we have to break spec to remain
2024 * compatible.
2025 */
2026 case SAVE_POINTERS:
2027 case RESTORE_POINTERS:
2028 /* Accept message by clearing ACK */
2029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2030 break;
2031 case EXTENDED_MESSAGE:
2032/*
2033 * Extended messages are sent in the following format :
2034 * Byte
2035 * 0 EXTENDED_MESSAGE == 1
2036 * 1 length (includes one byte for code, doesn't
2037 * include first two bytes)
2038 * 2 code
2039 * 3..length+1 arguments
2040 *
2041 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002042 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
2044 extended_msg[0] = EXTENDED_MESSAGE;
2045 /* Accept first byte by clearing ACK */
2046 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002047 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 len = 2;
2050 data = extended_msg + 1;
2051 phase = PHASE_MSGIN;
2052 NCR5380_transfer_pio(instance, &phase, &len, &data);
2053
Finn Thain52a6a1c2014-03-18 11:42:18 +11002054 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 -07002055
2056 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2057 /* Accept third byte by clearing ACK */
2058 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2059 len = extended_msg[1] - 1;
2060 data = extended_msg + 3;
2061 phase = PHASE_MSGIN;
2062
2063 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002064 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 switch (extended_msg[2]) {
2067 case EXTENDED_SDTR:
2068 case EXTENDED_WDTR:
2069 case EXTENDED_MODIFY_DATA_POINTER:
2070 case EXTENDED_EXTENDED_IDENTIFY:
2071 tmp = 0;
2072 }
2073 } else if (len) {
2074 printk("scsi%d: error receiving extended message\n", instance->host_no);
2075 tmp = 0;
2076 } else {
2077 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2078 tmp = 0;
2079 }
2080 /* Fall through to reject message */
2081
2082 /*
2083 * If we get something weird that we aren't expecting,
2084 * reject it.
2085 */
2086 default:
2087 if (!tmp) {
2088 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002089 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 printk("\n");
2091 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002092 scmd_printk(KERN_INFO, cmd,
2093 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002095 scmd_printk(KERN_INFO, cmd,
2096 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 msgout = MESSAGE_REJECT;
2099 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2100 break;
2101 } /* switch (tmp) */
2102 break;
2103 case PHASE_MSGOUT:
2104 len = 1;
2105 data = &msgout;
2106 hostdata->last_message = msgout;
2107 NCR5380_transfer_pio(instance, &phase, &len, &data);
2108 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002109 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 hostdata->connected = NULL;
2111 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 cmd->scsi_done(cmd);
2113 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2114 return;
2115 }
2116 msgout = NOP;
2117 break;
2118 case PHASE_CMDOUT:
2119 len = cmd->cmd_len;
2120 data = cmd->cmnd;
2121 /*
2122 * XXX for performance reasons, on machines with a
2123 * PSEUDO-DMA architecture we should probably
2124 * use the dma transfer function.
2125 */
2126 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 break;
2128 case PHASE_STATIN:
2129 len = 1;
2130 data = &tmp;
2131 NCR5380_transfer_pio(instance, &phase, &len, &data);
2132 cmd->SCp.Status = tmp;
2133 break;
2134 default:
2135 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002136 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002138 } else {
2139 spin_unlock_irq(instance->host_lock);
2140 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2141 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 } /* while (1) */
2144}
2145
2146/*
2147 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2148 *
2149 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002150 * 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 -07002151 * nexus has been reestablished,
2152 *
2153 * Inputs : instance - this instance of the NCR5380.
2154 *
2155 * Locks: io_request_lock held by caller if IRQ driven
2156 */
2157
2158static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2160 instance->hostdata;
2161 unsigned char target_mask;
2162 unsigned char lun, phase;
2163 int len;
2164 unsigned char msg[3];
2165 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002166 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int abort = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 /*
2170 * Disable arbitration, etc. since the host adapter obviously
2171 * lost, and tell an interrupted NCR5380_select() to restart.
2172 */
2173
2174 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002177 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 /*
2180 * At this point, we have detected that our SCSI ID is on the bus,
2181 * SEL is true and BSY was false for at least one bus settle delay
2182 * (400 ns).
2183 *
2184 * We must assert BSY ourselves, until the target drops the SEL
2185 * signal.
2186 */
2187
2188 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2189
2190 /* FIXME: timeout too long, must fail to workqueue */
2191 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2192 abort = 1;
2193
2194 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2195
2196 /*
2197 * Wait for target to go into MSGIN.
2198 * FIXME: timeout needed and fail to work queeu
2199 */
2200
Finn Thain1cc160e2016-01-03 16:05:32 +11002201 if (NCR5380_poll_politely(instance,
2202 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 abort = 1;
2204
2205 len = 1;
2206 data = msg;
2207 phase = PHASE_MSGIN;
2208 NCR5380_transfer_pio(instance, &phase, &len, &data);
2209
2210 if (!(msg[0] & 0x80)) {
2211 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002212 spi_print_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 abort = 1;
2214 } else {
2215 /* Accept message by clearing ACK */
2216 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2217 lun = (msg[0] & 0x07);
2218
2219 /*
2220 * We need to add code for SCSI-II to track which devices have
2221 * I_T_L_Q nexuses established, and which have simple I_T_L
2222 * nexuses so we can chose to do additional data transfer.
2223 */
2224
2225 /*
2226 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2227 * just reestablished, and remove it from the disconnected queue.
2228 */
2229
2230
Finn Thain710ddd02014-11-12 16:12:02 +11002231 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 +02002232 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 ) {
2234 if (prev) {
2235 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2236 prev->host_scribble = tmp->host_scribble;
2237 } else {
2238 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002239 hostdata->disconnected_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241 tmp->host_scribble = NULL;
2242 break;
2243 }
2244 if (!tmp) {
2245 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2246 /*
2247 * Since we have an established nexus that we can't do anything with,
2248 * we must abort it.
2249 */
2250 abort = 1;
2251 }
2252 }
2253
2254 if (abort) {
2255 do_abort(instance);
2256 } else {
2257 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002258 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 -07002259 }
2260}
2261
2262/*
2263 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2264 *
2265 * Purpose : called by interrupt handler when DMA finishes or a phase
2266 * mismatch occurs (which would finish the DMA transfer).
2267 *
2268 * Inputs : instance - this instance of the NCR5380.
2269 *
Finn Thain710ddd02014-11-12 16:12:02 +11002270 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * nexus has been reestablished, on failure NULL is returned.
2272 */
2273
2274#ifdef REAL_DMA
2275static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002276 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 /*
2280 * XXX this might not be right.
2281 *
2282 * Wait for final byte to transfer, ie wait for ACK to go false.
2283 *
2284 * We should use the Last Byte Sent bit, unfortunately this is
2285 * not available on the 5380/5381 (only the various CMOS chips)
2286 *
2287 * FIXME: timeout, and need to handle long timeout/irq case
2288 */
2289
2290 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2291
2292 NCR5380_write(MODE_REG, MR_BASE);
2293 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2294
2295 /*
2296 * The only places we should see a phase mismatch and have to send
2297 * data from the same set of pointers will be the data transfer
2298 * phases. So, residual, requested length are only important here.
2299 */
2300
2301 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2302 transferred = instance->dmalen - NCR5380_dma_residual();
2303 hostdata->connected->SCp.this_residual -= transferred;
2304 hostdata->connected->SCp.ptr += transferred;
2305 }
2306}
2307#endif /* def REAL_DMA */
2308
2309/*
Finn Thain710ddd02014-11-12 16:12:02 +11002310 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 *
2312 * Purpose : abort a command
2313 *
Finn Thain710ddd02014-11-12 16:12:02 +11002314 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002315 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * used.
2317 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002318 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002320 * XXX - there is no way to abort the command that is currently
2321 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * a problem, we could implement longjmp() / setjmp(), setjmp()
2323 * called where the loop started in NCR5380_main().
2324 *
2325 * Locks: host lock taken by caller
2326 */
2327
Finn Thain710ddd02014-11-12 16:12:02 +11002328static int NCR5380_abort(struct scsi_cmnd *cmd)
2329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct Scsi_Host *instance = cmd->device->host;
2331 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002332 struct scsi_cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002333
2334 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Finn Thain9dafbd92016-01-03 16:05:28 +11002336 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 NCR5380_print_status(instance);
2338
Finn Thain52a6a1c2014-03-18 11:42:18 +11002339 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2340 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 -07002341
2342#if 0
2343/*
2344 * Case 1 : If the command is the currently executing command,
2345 * we'll set the aborted flag and return control so that
2346 * information transfer routine can exit cleanly.
2347 */
2348
2349 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002350 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/*
2352 * We should perform BSY checking, and make sure we haven't slipped
2353 * into BUS FREE.
2354 */
2355
2356 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2357/*
2358 * Since we can't change phases until we've completed the current
2359 * handshake, we have to source or sink a byte of data if the current
2360 * phase is not MSGOUT.
2361 */
2362
2363/*
2364 * Return control to the executing NCR drive so we can clear the
2365 * aborted flag and get back into our main loop.
2366 */
2367
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002368 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370#endif
2371
2372/*
2373 * Case 2 : If the command hasn't been issued yet, we simply remove it
2374 * from the issue queue.
2375 */
2376
Finn Thain52a6a1c2014-03-18 11:42:18 +11002377 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002378 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 -07002379 if (cmd == tmp) {
2380 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002381 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002383 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002385 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002386 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return SUCCESS;
2388 }
2389#if (NDEBUG & NDEBUG_ABORT)
2390 /* KLL */
2391 else if (prev == tmp)
2392 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2393#endif
2394
2395/*
2396 * Case 3 : If any commands are connected, we're going to fail the abort
2397 * and let the high level SCSI driver retry at a later time or
2398 * issue a reset.
2399 *
2400 * Timeouts, and therefore aborted commands, will be highly unlikely
2401 * and handling them cleanly in this situation would make the common
2402 * case of noresets less efficient, and would pollute our code. So,
2403 * we fail.
2404 */
2405
2406 if (hostdata->connected) {
Finn Thain9dafbd92016-01-03 16:05:28 +11002407 spin_unlock_irq(instance->host_lock);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002408 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return FAILED;
2410 }
2411/*
2412 * Case 4: If the command is currently disconnected from the bus, and
2413 * there are no connected commands, we reconnect the I_T_L or
2414 * I_T_L_Q nexus associated with it, go into message out, and send
2415 * an abort message.
2416 *
2417 * This case is especially ugly. In order to reestablish the nexus, we
2418 * need to call NCR5380_select(). The easiest way to implement this
2419 * function was to abort if the bus was busy, and let the interrupt
2420 * handler triggered on the SEL for reselect take care of lost arbitrations
2421 * where necessary, meaning interrupts need to be enabled.
2422 *
2423 * When interrupts are enabled, the queues may change - so we
2424 * can't remove it from the disconnected queue before selecting it
2425 * because that could cause a failure in hashing the nexus if that
2426 * device reselected.
2427 *
2428 * Since the queues may change, we can't use the pointers from when we
2429 * first locate it.
2430 *
2431 * So, we must first locate the command, and if NCR5380_select()
2432 * succeeds, then issue the abort, relocate the command and remove
2433 * it from the disconnected queue.
2434 */
2435
Finn Thain710ddd02014-11-12 16:12:02 +11002436 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002438 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Finn Thain9dafbd92016-01-03 16:05:28 +11002440 if (NCR5380_select(instance, cmd)) {
2441 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return FAILED;
Finn Thain9dafbd92016-01-03 16:05:28 +11002443 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11002444 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 do_abort(instance);
2447
Finn Thain710ddd02014-11-12 16:12:02 +11002448 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 -07002449 if (cmd == tmp) {
2450 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002451 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002453 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002455 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return SUCCESS;
2457 }
2458 }
2459/*
2460 * Case 5 : If we reached this point, the command was not found in any of
2461 * the queues.
2462 *
2463 * We probably reached this point because of an unlikely race condition
2464 * between the command completing successfully and the abortion code,
2465 * so we won't panic, but we will notify the user in case something really
2466 * broke.
2467 */
Finn Thain9dafbd92016-01-03 16:05:28 +11002468 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2470 " before abortion\n", instance->host_no);
2471 return FAILED;
2472}
2473
2474
Finn Thain3be1b3e2016-01-03 16:05:12 +11002475/**
2476 * NCR5380_bus_reset - reset the SCSI bus
2477 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002479 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 */
2481
Finn Thain710ddd02014-11-12 16:12:02 +11002482static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002483{
2484 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002486 spin_lock_irq(instance->host_lock);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002487
2488#if (NDEBUG & NDEBUG_ANY)
2489 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2490 NCR5380_print_status(instance);
2491#endif
2492
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002493 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002494
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002495 spin_unlock_irq(instance->host_lock);
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return SUCCESS;
2498}