blob: 2ac01d86d330d3ed3cc47bc251e66884c1f31413 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * For more information, please consult
15 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Revision 1.10 1998/9/2 Alan Cox
Alan Coxfa195af2008-10-27 15:16:36 +000029 * (alan@lxorguk.ukuu.org.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Fixed up the timer lockups reported so far. Things still suck. Looking
31 * forward to 2.3 and per device request queues. Then it'll be possible to
32 * SMP thread this beast and improve life no end.
33
34 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
35 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
36 * (hopefully) fixed and enhanced USLEEP
37 * added support for DTC3181E card (for Mustek scanner)
38 *
39
40 * Revision 1.8 Ingmar Baumgart
41 * (ingmar@gonzo.schwaben.de)
42 * added support for NCR53C400a card
43 *
44
45 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
46 * added proc_info
47 * added support needed for DTC 3180/3280
48 * fixed a couple of bugs
49 *
50
51 * Revision 1.5 1994/01/19 09:14:57 drew
52 * Fixed udelay() hack that was being used on DATAOUT phases
53 * instead of a proper wait for the final handshake.
54 *
55 * Revision 1.4 1994/01/19 06:44:25 drew
56 * *** empty log message ***
57 *
58 * Revision 1.3 1994/01/19 05:24:40 drew
59 * Added support for TCR LAST_BYTE_SENT bit.
60 *
61 * Revision 1.2 1994/01/15 06:14:11 drew
62 * REAL DMA support, bug fixes.
63 *
64 * Revision 1.1 1994/01/15 06:00:54 drew
65 * Initial revision
66 *
67 */
68
69/*
70 * Further development / testing that should be done :
71 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
72 * code so that everything does the same thing that's done at the
73 * end of a pseudo-DMA read operation.
74 *
75 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
76 * basically, transfer size needs to be reduced by one
77 * and the last byte read as is done with PSEUDO_DMA.
78 *
79 * 4. Test SCSI-II tagged queueing (I have no devices which support
80 * tagged queueing)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83#if (NDEBUG & NDEBUG_LISTS)
84#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
85#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); }
86#else
87#define LIST(x,y)
88#define REMOVE(w,x,y,z)
89#endif
90
91#ifndef notyet
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#undef REAL_DMA
93#endif
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#ifdef BOARD_REQUIRES_NO_DELAY
96#define io_recovery_delay(x)
97#else
98#define io_recovery_delay(x) udelay(x)
99#endif
100
101/*
102 * Design
103 *
104 * This is a generic 5380 driver. To use it on a different platform,
105 * one simply writes appropriate system specific macros (ie, data
106 * transfer - some PC's will use the I/O bus, 68K's must use
107 * memory mapped) and drops this file in their 'C' wrapper.
108 *
109 * (Note from hch: unfortunately it was not enough for the different
110 * m68k folks and instead of improving this driver they copied it
111 * and hacked it up for their needs. As a consequence they lost
112 * most updates to this driver. Maybe someone will fix all these
113 * drivers to use a common core one day..)
114 *
115 * As far as command queueing, two queues are maintained for
116 * each 5380 in the system - commands that haven't been issued yet,
117 * and commands that are currently executing. This means that an
118 * unlimited number of commands may be queued, letting
119 * more commands propagate from the higher driver levels giving higher
120 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
121 * allowing multiple commands to propagate all the way to a SCSI-II device
122 * while a command is already executing.
123 *
124 *
125 * Issues specific to the NCR5380 :
126 *
127 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
128 * piece of hardware that requires you to sit in a loop polling for
129 * the REQ signal as long as you are connected. Some devices are
130 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100131 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * broken devices are the exception rather than the rule and I'd rather
133 * spend my time optimizing for the normal case.
134 *
135 * Architecture :
136 *
137 * At the heart of the design is a coroutine, NCR5380_main,
138 * which is started from a workqueue for each NCR5380 host in the
139 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
140 * removing the commands from the issue queue and calling
141 * NCR5380_select() if a nexus is not established.
142 *
143 * Once a nexus is established, the NCR5380_information_transfer()
144 * phase goes through the various phases as instructed by the target.
145 * if the target goes into MSG IN and sends a DISCONNECT message,
146 * the command structure is placed into the per instance disconnected
147 * queue, and NCR5380_main tries to find more work. If the target is
148 * idle for too long, the system will try to sleep.
149 *
150 * If a command has disconnected, eventually an interrupt will trigger,
151 * calling NCR5380_intr() which will in turn call NCR5380_reselect
152 * to reestablish a nexus. This will run main if necessary.
153 *
154 * On command termination, the done function will be called as
155 * appropriate.
156 *
157 * SCSI pointers are maintained in the SCp field of SCSI command
158 * structures, being initialized after the command is connected
159 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
160 * Note that in violation of the standard, an implicit SAVE POINTERS operation
161 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
162 */
163
164/*
165 * Using this file :
166 * This file a skeleton Linux SCSI driver for the NCR 5380 series
167 * of chips. To use it, you write an architecture specific functions
168 * and macros and include this file in your driver.
169 *
170 * These macros control options :
171 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
172 * defined.
173 *
174 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
175 * for commands that return with a CHECK CONDITION status.
176 *
177 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
178 * transceivers.
179 *
180 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
181 * override-configure an IRQ.
182 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
184 *
185 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
186 *
187 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
188 * rely on phase mismatch and EOP interrupts to determine end
189 * of phase.
190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Defaults for these will be provided although the user may want to adjust
192 * these to allocate CPU resources to the SCSI driver or "real" code.
193 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * These macros MUST be defined :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * NCR5380_read(register) - read from the specified register
197 *
198 * NCR5380_write(register, value) - write to the specific register
199 *
200 * NCR5380_implementation_fields - additional fields needed for this
201 * specific implementation of the NCR5380
202 *
203 * Either real DMA *or* pseudo DMA may be implemented
204 * REAL functions :
205 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
206 * Note that the DMA setup functions should return the number of bytes
207 * that they were able to program the controller for.
208 *
209 * Also note that generic i386/PC versions of these macros are
210 * available as NCR5380_i386_dma_write_setup,
211 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
212 *
213 * NCR5380_dma_write_setup(instance, src, count) - initialize
214 * NCR5380_dma_read_setup(instance, dst, count) - initialize
215 * NCR5380_dma_residual(instance); - residual count
216 *
217 * PSEUDO functions :
218 * NCR5380_pwrite(instance, src, count)
219 * NCR5380_pread(instance, dst, count);
220 *
221 * The generic driver is initialized by calling NCR5380_init(instance),
222 * after setting the appropriate host specific fields and ID. If the
223 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
224 * possible) function may be used.
225 */
226
Finn Thain54d8fe42016-01-03 16:05:06 +1100227static int do_abort(struct Scsi_Host *);
228static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230/*
231 * initialize_SCp - init the scsi pointer field
232 * @cmd: command block to set up
233 *
234 * Set up the internal fields in the SCSI command.
235 */
236
Finn Thain710ddd02014-11-12 16:12:02 +1100237static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 /*
240 * Initialize the Scsi Pointer field so that all of the commands in the
241 * various queues are valid.
242 */
243
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200244 if (scsi_bufflen(cmd)) {
245 cmd->SCp.buffer = scsi_sglist(cmd);
246 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200247 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cmd->SCp.this_residual = cmd->SCp.buffer->length;
249 } else {
250 cmd->SCp.buffer = NULL;
251 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200252 cmd->SCp.ptr = NULL;
253 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255}
256
257/**
Finn Thainb32ade12016-01-03 16:05:41 +1100258 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100259 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100260 * @reg1: 5380 register to poll
261 * @bit1: Bitmask to check
262 * @val1: Expected value
263 * @reg2: Second 5380 register to poll
264 * @bit2: Second bitmask to check
265 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100266 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *
Finn Thain2f854b82016-01-03 16:05:22 +1100268 * Polls the chip in a reasonably efficient manner waiting for an
269 * event to occur. After a short quick poll we begin to yield the CPU
270 * (if possible). In irq contexts the time-out is arbitrarily limited.
271 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
Finn Thainb32ade12016-01-03 16:05:41 +1100273 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Finn Thainb32ade12016-01-03 16:05:41 +1100276static int NCR5380_poll_politely2(struct Scsi_Host *instance,
277 int reg1, int bit1, int val1,
278 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100279{
280 struct NCR5380_hostdata *hostdata = shost_priv(instance);
281 unsigned long deadline = jiffies + wait;
282 unsigned long n;
283
284 /* Busy-wait for up to 10 ms */
285 n = min(10000U, jiffies_to_usecs(wait));
286 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100287 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100288 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100289 if ((NCR5380_read(reg1) & bit1) == val1)
290 return 0;
291 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100294 } while (n--);
295
296 if (irqs_disabled() || in_interrupt())
297 return -ETIMEDOUT;
298
299 /* Repeatedly sleep for 1 ms until deadline */
300 while (time_is_after_jiffies(deadline)) {
301 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100302 if ((NCR5380_read(reg1) & bit1) == val1)
303 return 0;
304 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Finn Thain2f854b82016-01-03 16:05:22 +1100307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -ETIMEDOUT;
309}
310
Finn Thainb32ade12016-01-03 16:05:41 +1100311static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
312 int reg, int bit, int val, int wait)
313{
314 return NCR5380_poll_politely2(instance, reg, bit, val,
315 reg, bit, val, wait);
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static struct {
319 unsigned char value;
320 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700321} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 {PHASE_DATAOUT, "DATAOUT"},
323 {PHASE_DATAIN, "DATAIN"},
324 {PHASE_CMDOUT, "CMDOUT"},
325 {PHASE_STATIN, "STATIN"},
326 {PHASE_MSGOUT, "MSGOUT"},
327 {PHASE_MSGIN, "MSGIN"},
328 {PHASE_UNKNOWN, "UNKNOWN"}
329};
330
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100331#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static struct {
333 unsigned char mask;
334 const char *name;
335} signals[] = {
336 {SR_DBP, "PARITY"},
337 {SR_RST, "RST"},
338 {SR_BSY, "BSY"},
339 {SR_REQ, "REQ"},
340 {SR_MSG, "MSG"},
341 {SR_CD, "CD"},
342 {SR_IO, "IO"},
343 {SR_SEL, "SEL"},
344 {0, NULL}
345},
346basrs[] = {
347 {BASR_ATN, "ATN"},
348 {BASR_ACK, "ACK"},
349 {0, NULL}
350},
351icrs[] = {
352 {ICR_ASSERT_RST, "ASSERT RST"},
353 {ICR_ASSERT_ACK, "ASSERT ACK"},
354 {ICR_ASSERT_BSY, "ASSERT BSY"},
355 {ICR_ASSERT_SEL, "ASSERT SEL"},
356 {ICR_ASSERT_ATN, "ASSERT ATN"},
357 {ICR_ASSERT_DATA, "ASSERT DATA"},
358 {0, NULL}
359},
360mrs[] = {
361 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
362 {MR_TARGET, "MODE TARGET"},
363 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
364 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
365 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
366 {MR_DMA_MODE, "MODE DMA"},
367 {MR_ARBITRATE, "MODE ARBITRATION"},
368 {0, NULL}
369};
370
371/**
372 * NCR5380_print - print scsi bus signals
373 * @instance: adapter state to dump
374 *
375 * Print the SCSI bus signals for debugging purposes
376 *
377 * Locks: caller holds hostdata lock (not essential)
378 */
379
380static void NCR5380_print(struct Scsi_Host *instance)
381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
385 status = NCR5380_read(STATUS_REG);
386 mr = NCR5380_read(MODE_REG);
387 icr = NCR5380_read(INITIATOR_COMMAND_REG);
388 basr = NCR5380_read(BUS_AND_STATUS_REG);
389
390 printk("STATUS_REG: %02x ", status);
391 for (i = 0; signals[i].mask; ++i)
392 if (status & signals[i].mask)
393 printk(",%s", signals[i].name);
394 printk("\nBASR: %02x ", basr);
395 for (i = 0; basrs[i].mask; ++i)
396 if (basr & basrs[i].mask)
397 printk(",%s", basrs[i].name);
398 printk("\nICR: %02x ", icr);
399 for (i = 0; icrs[i].mask; ++i)
400 if (icr & icrs[i].mask)
401 printk(",%s", icrs[i].name);
402 printk("\nMODE: %02x ", mr);
403 for (i = 0; mrs[i].mask; ++i)
404 if (mr & mrs[i].mask)
405 printk(",%s", mrs[i].name);
406 printk("\n");
407}
408
409
410/*
411 * NCR5380_print_phase - show SCSI phase
412 * @instance: adapter to dump
413 *
414 * Print the current SCSI phase for debugging purposes
415 *
416 * Locks: none
417 */
418
419static void NCR5380_print_phase(struct Scsi_Host *instance)
420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned char status;
422 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 status = NCR5380_read(STATUS_REG);
425 if (!(status & SR_REQ))
426 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
427 else {
428 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
429 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
430 }
431}
432#endif
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Finn Thaind5f7e652016-01-03 16:05:03 +1100435static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/**
438 * probe_intr - helper for IRQ autoprobe
439 * @irq: interrupt number
440 * @dev_id: unused
441 * @regs: unused
442 *
443 * Set a flag to indicate the IRQ in question was received. This is
444 * used by the IRQ probe code.
445 */
446
David Howells7d12e782006-10-05 14:55:46 +0100447static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 probe_irq = irq;
450 return IRQ_HANDLED;
451}
452
453/**
454 * NCR5380_probe_irq - find the IRQ of an NCR5380
455 * @instance: NCR5380 controller
456 * @possible: bitmask of ISA IRQ lines
457 *
458 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
459 * and then looking to see what interrupt actually turned up.
460 *
461 * Locks: none, irqs must be enabled on entry
462 */
463
Andrew Morton702809c2007-05-23 14:41:56 -0700464static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
465 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
468 unsigned long timeout;
469 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Finn Thain22f5f102014-11-12 16:11:56 +1100471 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100472 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 trying_irqs |= mask;
474
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500475 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100476 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * A interrupt is triggered whenever BSY = false, SEL = true
480 * and a bit set in the SELECT_ENABLE_REG is asserted on the
481 * SCSI bus.
482 *
483 * Note that the bus is only driven when the phase control signals
484 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
485 * to zero.
486 */
487
488 NCR5380_write(TARGET_COMMAND_REG, 0);
489 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
490 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
491 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
492
Finn Thain22f5f102014-11-12 16:11:56 +1100493 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800494 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 NCR5380_write(SELECT_ENABLE_REG, 0);
497 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
498
Finn Thain22f5f102014-11-12 16:11:56 +1100499 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (trying_irqs & mask)
501 free_irq(i, NULL);
502
503 return probe_irq;
504}
505
506/**
Finn Thain8c325132014-11-12 16:11:58 +1100507 * NCR58380_info - report driver and host information
508 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
Finn Thain8c325132014-11-12 16:11:58 +1100510 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
512 * Locks: none
513 */
514
Finn Thain8c325132014-11-12 16:11:58 +1100515static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Finn Thain8c325132014-11-12 16:11:58 +1100517 struct NCR5380_hostdata *hostdata = shost_priv(instance);
518
519 return hostdata->info;
520}
521
522static void prepare_info(struct Scsi_Host *instance)
523{
524 struct NCR5380_hostdata *hostdata = shost_priv(instance);
525
526 snprintf(hostdata->info, sizeof(hostdata->info),
527 "%s, io_port 0x%lx, n_io_port %d, "
528 "base 0x%lx, irq %d, "
529 "can_queue %d, cmd_per_lun %d, "
530 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100531 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100532 "options { %s} ",
533 instance->hostt->name, instance->io_port, instance->n_io_port,
534 instance->base, instance->irq,
535 instance->can_queue, instance->cmd_per_lun,
536 instance->sg_tablesize, instance->this_id,
Finn Thain55181be2016-01-03 16:05:42 +1100537 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100538 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100539 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100541 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100544 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545#endif
546#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100547 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#endif
549#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100550 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#endif
552#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100553 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100556 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
Finn Thain8c325132014-11-12 16:11:58 +1100558 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Finn Thaina9c2dc42014-11-12 16:11:59 +1100561#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/******************************************/
563/*
564 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
565 *
566 * *buffer: I/O buffer
567 * **start: if inout == FALSE pointer into buffer where user read should start
568 * offset: current offset
569 * length: length of buffer
570 * hostno: Scsi_Host host_no
571 * inout: TRUE - user is writing; FALSE - user is reading
572 *
573 * Return the number of bytes read from or written
574 */
575
Al Virodd7ab712013-03-31 01:15:54 -0400576static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
577 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100579 struct NCR5380_hostdata *hostdata = shost_priv(instance);
580
581 hostdata->spin_max_r = 0;
582 hostdata->spin_max_w = 0;
583 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400584}
Al Virodd7ab712013-03-31 01:15:54 -0400585
586static int __maybe_unused NCR5380_show_info(struct seq_file *m,
587 struct Scsi_Host *instance)
588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct NCR5380_hostdata *hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
592
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100593 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100594 hostdata->spin_max_w, hostdata->spin_max_r);
Al Virodd7ab712013-03-31 01:15:54 -0400595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
Finn Thaine5c3fdd2016-01-03 16:05:52 +1100597#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599/**
600 * NCR5380_init - initialise an NCR5380
601 * @instance: adapter to configure
602 * @flags: control flags
603 *
604 * Initializes *instance and corresponding 5380 chip,
605 * with flags OR'd into the initial flags value.
606 *
607 * Notes : I assume that the host, hostno, and id bits have been
608 * set correctly. I don't care about the irq and other fields.
609 *
610 * Returns 0 for success
611 *
612 * Locks: interrupts must be enabled when we are called
613 */
614
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800615static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Finn Thainb6488f92016-01-03 16:05:08 +1100617 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain2f854b82016-01-03 16:05:22 +1100619 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if(in_interrupt())
622 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 hostdata->id_mask = 1 << instance->this_id;
625 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
626 if (i > hostdata->id_mask)
627 hostdata->id_higher_mask |= i;
628 for (i = 0; i < 8; ++i)
629 hostdata->busy[i] = 0;
630#ifdef REAL_DMA
631 hostdata->dmalen = 0;
632#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100633 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 hostdata->connected = NULL;
635 hostdata->issue_queue = NULL;
636 hostdata->disconnected_queue = NULL;
Finn Thain55181be2016-01-03 16:05:42 +1100637 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Finn Thain8d8601a2016-01-03 16:05:37 +1100639 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100640 hostdata->work_q = alloc_workqueue("ncr5380_%d",
641 WQ_UNBOUND | WQ_MEM_RECLAIM,
642 1, instance->host_no);
643 if (!hostdata->work_q)
644 return -ENOMEM;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Finn Thain8c325132014-11-12 16:11:58 +1100648 prepare_info(instance);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
651 NCR5380_write(MODE_REG, MR_BASE);
652 NCR5380_write(TARGET_COMMAND_REG, 0);
653 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100654
655 /* Calibrate register polling loop */
656 i = 0;
657 deadline = jiffies + 1;
658 do {
659 cpu_relax();
660 } while (time_is_after_jiffies(deadline));
661 deadline += msecs_to_jiffies(256);
662 do {
663 NCR5380_read(STATUS_REG);
664 ++i;
665 cpu_relax();
666 } while (time_is_after_jiffies(deadline));
667 hostdata->accesses_per_ms = i / 256;
668
Finn Thainb6488f92016-01-03 16:05:08 +1100669 return 0;
670}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Finn Thainb6488f92016-01-03 16:05:08 +1100672/**
673 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
674 * @instance: adapter to check
675 *
676 * If the system crashed, it may have crashed with a connected target and
677 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
678 * currently established nexus, which we know nothing about. Failing that
679 * do a bus reset.
680 *
681 * Note that a bus reset will cause the chip to assert IRQ.
682 *
683 * Returns 0 if successful, otherwise -ENXIO.
684 */
685
686static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
687{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100688 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100689 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
692 switch (pass) {
693 case 1:
694 case 3:
695 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100696 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
697 NCR5380_poll_politely(instance,
698 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
700 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100701 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 do_abort(instance);
703 break;
704 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100705 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100707 /* Wait after a reset; the SCSI standard calls for
708 * 250ms, we wait 500ms to be on the safe side.
709 * But some Toshiba CD-ROMs need ten times that.
710 */
711 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
712 msleep(2500);
713 else
714 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100717 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -ENXIO;
719 }
720 }
721 return 0;
722}
723
724/**
725 * NCR5380_exit - remove an NCR5380
726 * @instance: adapter to remove
727 */
728
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800729static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
732
Finn Thain8d8601a2016-01-03 16:05:37 +1100733 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100734 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/**
Finn Thain1bb40582016-01-03 16:05:29 +1100738 * NCR5380_queue_command - queue a command
739 * @instance: the relevant SCSI adapter
740 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
Finn Thain1bb40582016-01-03 16:05:29 +1100742 * cmd is added to the per-instance issue queue, with minor
743 * twiddling done to the host specific fields of cmd. If the
744 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
746
Finn Thain1bb40582016-01-03 16:05:29 +1100747static int NCR5380_queue_command(struct Scsi_Host *instance,
748 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Finn Thain1bb40582016-01-03 16:05:29 +1100750 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100751 struct scsi_cmnd *tmp;
Finn Thain1bb40582016-01-03 16:05:29 +1100752 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754#if (NDEBUG & NDEBUG_NO_WRITE)
755 switch (cmd->cmnd[0]) {
756 case WRITE_6:
757 case WRITE_10:
758 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
759 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100760 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 0;
762 }
763#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * We use the host_scribble field as a pointer to the next command
767 * in a queue
768 */
769
770 cmd->host_scribble = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 cmd->result = 0;
772
Finn Thain11d2f632016-01-03 16:05:51 +1100773 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
776 * Insert the cmd into the issue queue. Note that REQUEST SENSE
777 * commands are added to the head of the queue since any command will
778 * clear the contingent allegiance condition that exists and the
779 * sense data is only guaranteed to be valid while the condition exists.
780 */
781
782 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
783 LIST(cmd, hostdata->issue_queue);
784 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
785 hostdata->issue_queue = cmd;
786 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100787 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 -0700788 LIST(cmd, tmp);
789 tmp->host_scribble = (unsigned char *) cmd;
790 }
Finn Thain11d2f632016-01-03 16:05:51 +1100791 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100792
Finn Thain52a6a1c2014-03-18 11:42:18 +1100793 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 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100796 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
801 * NCR5380_main - NCR state machines
802 *
803 * NCR5380_main is a coroutine that runs as long as more work can
804 * be done on the NCR5380 host adapters in a system. Both
805 * NCR5380_queue_command() and NCR5380_intr() will try to start it
806 * in case it is not running.
807 *
808 * Locks: called as its own thread with no locks held. Takes the
809 * host lock and called routines may take the isa dma lock.
810 */
811
David Howellsc4028952006-11-22 14:57:56 +0000812static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
David Howellsc4028952006-11-22 14:57:56 +0000814 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100815 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100817 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int done;
819
Finn Thain11d2f632016-01-03 16:05:51 +1100820 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100823
Finn Thainae753a32016-01-03 16:05:23 +1100824 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100825 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Search through the issue_queue for a command destined
828 * for a target that's not busy.
829 */
Finn Thain710ddd02014-11-12 16:12:02 +1100830 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 -0700831 {
832 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200833 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 -0700834 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200835 if (!(hostdata->busy[tmp->device->id] &
836 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (prev) {
838 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
839 prev->host_scribble = tmp->host_scribble;
840 } else {
841 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +1100842 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 tmp->host_scribble = NULL;
845
846 /*
847 * Attempt to establish an I_T_L nexus here.
848 * On success, instance->hostdata->connected is set.
849 * On failure, we must add the command back to the
850 * issue queue so we can keep trying.
851 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200852 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 -0700853
Finn Thain76f13b92014-11-12 16:11:53 +1100854 /*
855 * REQUEST SENSE commands are issued without tagged
856 * queueing, even on SCSI-II devices because the
857 * contingent allegiance condition exists for the
858 * entire unit.
859 */
860
861 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100862 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100864 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 LIST(tmp, hostdata->issue_queue);
866 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
867 hostdata->issue_queue = tmp;
868 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100869 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 -0700870 }
Finn Thainae753a32016-01-03 16:05:23 +1100871 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } /* if target/lun is not busy */
874 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 } /* if (!hostdata->connected) */
Finn Thain11d2f632016-01-03 16:05:51 +1100876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (hostdata->connected
878#ifdef REAL_DMA
879 && !hostdata->dmalen
880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100882 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100884 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } while (!done);
Finn Thain11d2f632016-01-03 16:05:51 +1100888 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891#ifndef DONT_USE_INTR
892
893/**
Finn Thaincd400822016-01-03 16:05:40 +1100894 * NCR5380_intr - generic NCR5380 irq handler
895 * @irq: interrupt number
896 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 *
Finn Thaincd400822016-01-03 16:05:40 +1100898 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
899 * from the disconnected queue, and restarting NCR5380_main()
900 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *
Finn Thaincd400822016-01-03 16:05:40 +1100902 * The chip can assert IRQ in any of six different conditions. The IRQ flag
903 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
904 * Three of these six conditions are latched in the Bus and Status Register:
905 * - End of DMA (cleared by ending DMA Mode)
906 * - Parity error (cleared by reading RPIR)
907 * - Loss of BSY (cleared by reading RPIR)
908 * Two conditions have flag bits that are not latched:
909 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
910 * - Bus reset (non-maskable)
911 * The remaining condition has no flag bit at all:
912 * - Selection/reselection
913 *
914 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
915 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
916 * claimed that "the design of the [DP8490] interrupt logic ensures
917 * interrupts will not be lost (they can be on the DP5380)."
918 * The L5380/53C80 datasheet from LOGIC Devices has more details.
919 *
920 * Checking for bus reset by reading RST is futile because of interrupt
921 * latency, but a bus reset will reset chip logic. Checking for parity error
922 * is unnecessary because that interrupt is never enabled. A Loss of BSY
923 * condition will clear DMA Mode. We can tell when this occurs because the
924 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926
Finn Thaincd400822016-01-03 16:05:40 +1100927static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800929 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100930 struct NCR5380_hostdata *hostdata = shost_priv(instance);
931 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 unsigned char basr;
933 unsigned long flags;
934
Finn Thain11d2f632016-01-03 16:05:51 +1100935 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Finn Thaincd400822016-01-03 16:05:40 +1100937 basr = NCR5380_read(BUS_AND_STATUS_REG);
938 if (basr & BASR_IRQ) {
939 unsigned char mr = NCR5380_read(MODE_REG);
940 unsigned char sr = NCR5380_read(STATUS_REG);
941
942 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
943 instance->host_no, irq, basr, sr, mr);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +1100946 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
947 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
948 * We ack IRQ after clearing Mode Register. Workarounds
949 * for End of DMA errata need to happen in DMA Mode.
950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Finn Thaincd400822016-01-03 16:05:40 +1100952 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Finn Thaincd400822016-01-03 16:05:40 +1100954 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Finn Thaincd400822016-01-03 16:05:40 +1100956 if (!hostdata->connected)
957 panic("scsi%d : DMA interrupt with no connected cmd\n",
958 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Finn Thaincd400822016-01-03 16:05:40 +1100960 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
961 hostdata->connected->SCp.this_residual -= transferred;
962 hostdata->connected->SCp.ptr += transferred;
963 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Finn Thaincd400822016-01-03 16:05:40 +1100965 /* FIXME: we need to poll briefly then defer a workqueue task ! */
966 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
967
968 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
969 NCR5380_write(MODE_REG, MR_BASE);
970 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
971 } else
972#endif /* REAL_DMA */
973 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
974 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
975 /* Probably reselected */
976 NCR5380_write(SELECT_ENABLE_REG, 0);
977 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
978
979 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
980 instance->host_no);
981
982 if (!hostdata->connected) {
983 NCR5380_reselect(instance);
984 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Finn Thaincd400822016-01-03 16:05:40 +1100986 if (!hostdata->connected)
987 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
988 } else {
989 /* Probably Bus Reset */
990 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
991
992 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
993 }
994 handled = 1;
995 } else {
996 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
997 }
998
Finn Thain11d2f632016-01-03 16:05:51 +1100999 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +11001000
1001 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004#endif
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
Finn Thain710ddd02014-11-12 16:12:02 +11001007 * Function : int NCR5380_select(struct Scsi_Host *instance,
1008 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 *
1010 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1011 * including ARBITRATION, SELECTION, and initial message out for
1012 * IDENTIFY and queue messages.
1013 *
1014 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001015 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
Finn Thain63238762016-01-03 16:05:15 +11001017 * Returns : -1 if selection failed but should be retried.
1018 * 0 if selection failed and should not be retried.
1019 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 *
1021 * Side effects :
1022 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1023 * with registers as they should have been on entry - ie
1024 * SELECT_ENABLE will be set appropriately, the NCR5380
1025 * will cease to drive any SCSI bus signals.
1026 *
1027 * If successful : I_T_L or I_T_L_Q nexus will be established,
1028 * instance->connected will be set to cmd.
1029 * SELECT interrupt will be disabled.
1030 *
1031 * If failed (no target) : cmd->scsi_done() will be called, and the
1032 * cmd->result host byte set to DID_BAD_TARGET.
1033 *
1034 * Locks: caller holds hostdata lock in IRQ mode
1035 */
1036
Finn Thain710ddd02014-11-12 16:12:02 +11001037static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1040 unsigned char tmp[3], phase;
1041 unsigned char *data;
1042 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001046 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /*
1049 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1050 * data bus during SELECTION.
1051 */
1052
1053 NCR5380_write(TARGET_COMMAND_REG, 0);
1054
1055 /*
1056 * Start arbitration.
1057 */
1058
1059 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1060 NCR5380_write(MODE_REG, MR_ARBITRATE);
1061
Finn Thain55500d92016-01-03 16:05:35 +11001062 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1063 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 */
1065
Finn Thain11d2f632016-01-03 16:05:51 +11001066 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001067 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1068 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1069 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001070 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001071 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1072 /* Reselection interrupt */
1073 return -1;
1074 }
1075 if (err < 0) {
1076 NCR5380_write(MODE_REG, MR_BASE);
1077 shost_printk(KERN_ERR, instance,
1078 "select: arbitration timeout\n");
1079 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001080 }
Finn Thain11d2f632016-01-03 16:05:51 +11001081 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001082
1083 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 udelay(3);
1085
1086 /* Check for lost arbitration */
1087 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)) {
1088 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001089 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain11d2f632016-01-03 16:05:51 +11001090 spin_lock_irq(&hostdata->lock);
Finn Thain63238762016-01-03 16:05:15 +11001091 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Finn Thaincf13b082016-01-03 16:05:18 +11001093
1094 /* After/during arbitration, BSY should be asserted.
1095 * IBM DPES-31080 Version S31Q works now
1096 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1097 */
1098 NCR5380_write(INITIATOR_COMMAND_REG,
1099 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * Again, bus clear + bus settle time is 1.2us, however, this is
1103 * a minimum so we'll udelay ceil(1.2)
1104 */
1105
Finn Thain9c3f0e22016-01-03 16:05:11 +11001106 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1107 udelay(15);
1108 else
1109 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Finn Thain11d2f632016-01-03 16:05:51 +11001111 spin_lock_irq(&hostdata->lock);
1112
Finn Thain72064a72016-01-03 16:05:44 +11001113 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1114 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1115 return -1;
1116
Finn Thain52a6a1c2014-03-18 11:42:18 +11001117 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /*
1120 * Now that we have won arbitration, start Selection process, asserting
1121 * the host and target ID's on the SCSI bus.
1122 */
1123
Jeff Garzik422c0d62005-10-24 18:05:09 -04001124 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * Raise ATN while SEL is true before BSY goes false from arbitration,
1128 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1129 * phase immediately after selection.
1130 */
1131
1132 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1133 NCR5380_write(MODE_REG, MR_BASE);
1134
1135 /*
1136 * Reselect interrupts must be turned off prior to the dropping of BSY,
1137 * otherwise we will trigger an interrupt.
1138 */
1139 NCR5380_write(SELECT_ENABLE_REG, 0);
1140
Finn Thain11d2f632016-01-03 16:05:51 +11001141 spin_unlock_irq(&hostdata->lock);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * The initiator shall then wait at least two deskew delays and release
1145 * the BSY signal.
1146 */
1147 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1148
1149 /* Reset BSY */
1150 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1151
1152 /*
1153 * Something weird happens when we cease to drive BSY - looks
1154 * like the board/chip is letting us do another read before the
1155 * appropriate propagation delay has expired, and we're confusing
1156 * a BSY signal from ourselves as the target's response to SELECTION.
1157 *
1158 * A small delay (the 'C++' frontend breaks the pipeline with an
1159 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1160 * tighter 'C' code breaks and requires this) solves the problem -
1161 * the 1 us delay is arbitrary, and only used because this delay will
1162 * be the same on other platforms and since it works here, it should
1163 * work there.
1164 *
1165 * wingel suggests that this could be due to failing to wait
1166 * one deskew delay.
1167 */
1168
1169 udelay(1);
1170
Finn Thain52a6a1c2014-03-18 11:42:18 +11001171 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /*
1174 * The SCSI specification calls for a 250 ms timeout for the actual
1175 * selection.
1176 */
1177
Finn Thainae753a32016-01-03 16:05:23 +11001178 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1179 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001182 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1184 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001185 if (!hostdata->connected)
1186 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -1;
1189 }
Finn Thainae753a32016-01-03 16:05:23 +11001190
1191 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001192 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001193 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1194 cmd->result = DID_BAD_TARGET << 16;
1195 cmd->scsi_done(cmd);
1196 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1197 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1198 instance->host_no);
1199 return 0;
1200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * No less than two deskew delays after the initiator detects the
1204 * BSY signal is true, it shall release the SEL signal and may
1205 * change the DATA BUS. -wingel
1206 */
1207
1208 udelay(1);
1209
1210 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
1213 * Since we followed the SCSI spec, and raised ATN while SEL
1214 * was true but before BSY was false during selection, the information
1215 * transfer phase should be a MESSAGE OUT phase so that we can send the
1216 * IDENTIFY message.
1217 *
1218 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1219 * message (2 bytes) with a tag ID that we increment with every command
1220 * until it wraps back to 0.
1221 *
1222 * XXX - it turns out that there are some broken SCSI-II devices,
1223 * which claim to support tagged queuing but fail when more than
1224 * some number of commands are issued at once.
1225 */
1226
1227 /* Wait for start of REQ/ACK handshake */
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001230 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001231 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001232 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1233 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001235 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
Finn Thain52a6a1c2014-03-18 11:42:18 +11001238 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 +11001239 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 len = 1;
1242 cmd->tag = 0;
1243
1244 /* Send message(s) */
1245 data = tmp;
1246 phase = PHASE_MSGOUT;
1247 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001248 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001252 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Boaz Harrosh28424d32007-09-10 22:37:45 +03001254 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259/*
1260 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1261 * unsigned char *phase, int *count, unsigned char **data)
1262 *
1263 * Purpose : transfers data in given phase using polled I/O
1264 *
1265 * Inputs : instance - instance of driver, *phase - pointer to
1266 * what phase is expected, *count - pointer to number of
1267 * bytes to transfer, **data - pointer to data pointer.
1268 *
1269 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001270 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * is in same phase.
1272 *
1273 * Also, *phase, *count, *data are modified in place.
1274 *
1275 * XXX Note : handling for bus free may be useful.
1276 */
1277
1278/*
1279 * Note : this code is not as quick as it could be, however it
1280 * IS 100% reliable, and for the actual data transfer where speed
1281 * counts, we will always do a pseudo DMA or DMA transfer.
1282 */
1283
1284static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned char p = *phase, tmp;
1286 int c = *count;
1287 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001290 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001292 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /*
1295 * The NCR5380 chip will only drive the SCSI bus when the
1296 * phase specified in the appropriate bits of the TARGET COMMAND
1297 * REGISTER match the STATUS REGISTER
1298 */
1299
1300 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 do {
1303 /*
1304 * Wait for assertion of REQ, after which the phase bits will be
1305 * valid
1306 */
1307
Finn Thain686f3992016-01-03 16:05:26 +11001308 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Finn Thain52a6a1c2014-03-18 11:42:18 +11001311 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001314 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001315 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1317 break;
1318 }
1319 /* Do actual transfer from SCSI bus to / from memory */
1320 if (!(p & SR_IO))
1321 NCR5380_write(OUTPUT_DATA_REG, *d);
1322 else
1323 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1324
1325 ++d;
1326
1327 /*
1328 * The SCSI standard suggests that in MSGOUT phase, the initiator
1329 * should drop ATN on the last byte of the message phase
1330 * after REQ has been asserted for the handshake but before
1331 * the initiator raises ACK.
1332 */
1333
1334 if (!(p & SR_IO)) {
1335 if (!((p & SR_MSG) && c > 1)) {
1336 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1337 NCR5380_dprint(NDEBUG_PIO, instance);
1338 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1339 } else {
1340 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1341 NCR5380_dprint(NDEBUG_PIO, instance);
1342 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1343 }
1344 } else {
1345 NCR5380_dprint(NDEBUG_PIO, instance);
1346 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1347 }
1348
Finn Thaina2edc4a2016-01-03 16:05:27 +11001349 if (NCR5380_poll_politely(instance,
1350 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1351 break;
1352
Finn Thain52a6a1c2014-03-18 11:42:18 +11001353 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355/*
1356 * We have several special cases to consider during REQ/ACK handshaking :
1357 * 1. We were in MSGOUT phase, and we are on the last byte of the
1358 * message. ATN must be dropped as ACK is dropped.
1359 *
1360 * 2. We are in a MSGIN phase, and we are on the last byte of the
1361 * message. We must exit with ACK asserted, so that the calling
1362 * code may raise ATN before dropping ACK to reject the message.
1363 *
1364 * 3. ACK and ATN are clear and the target may proceed as normal.
1365 */
1366 if (!(p == PHASE_MSGIN && c == 1)) {
1367 if (p == PHASE_MSGOUT && c > 1)
1368 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1369 else
1370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1371 }
1372 } while (--c);
1373
Finn Thain52a6a1c2014-03-18 11:42:18 +11001374 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 *count = c;
1377 *data = d;
1378 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001379 /* The phase read from the bus is valid if either REQ is (already)
1380 * asserted or if ACK hasn't been released yet. The latter applies if
1381 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1382 */
1383 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 *phase = tmp & PHASE_MASK;
1385 else
1386 *phase = PHASE_UNKNOWN;
1387
1388 if (!c || (*phase == p))
1389 return 0;
1390 else
1391 return -1;
1392}
1393
1394/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001395 * do_reset - issue a reset command
1396 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001398 * Issue a reset sequence to the NCR5380 and try and get the bus
1399 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001401 * This clears the reset interrupt flag because there may be no handler for
1402 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1403 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405
Finn Thain54d8fe42016-01-03 16:05:06 +11001406static void do_reset(struct Scsi_Host *instance)
1407{
Finn Thain636b1ec2016-01-03 16:05:10 +11001408 unsigned long flags;
1409
1410 local_irq_save(flags);
1411 NCR5380_write(TARGET_COMMAND_REG,
1412 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001414 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001416 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1417 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Finn Thain80d3eb62016-01-03 16:05:34 +11001420/**
1421 * do_abort - abort the currently established nexus by going to
1422 * MESSAGE OUT phase and sending an ABORT message.
1423 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001425 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
1427
Finn Thain54d8fe42016-01-03 16:05:06 +11001428static int do_abort(struct Scsi_Host *instance)
1429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 unsigned char *msgptr, phase, tmp;
1431 int len;
1432 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* Request message out phase */
1435 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1436
1437 /*
1438 * Wait for the target to indicate a valid phase by asserting
1439 * REQ. Once this happens, we'll have either a MSGOUT phase
1440 * and can immediately send the ABORT message, or we'll have some
1441 * other phase and will have to source/sink data.
1442 *
1443 * We really don't care what value was on the bus or what value
1444 * the target sees, so we just handshake.
1445 */
1446
Finn Thain80d3eb62016-01-03 16:05:34 +11001447 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001448 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001449 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Finn Thainf35d3472016-01-03 16:05:33 +11001451 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1454
Finn Thainf35d3472016-01-03 16:05:33 +11001455 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001457 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001458 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001459 goto timeout;
1460 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 tmp = ABORT;
1463 msgptr = &tmp;
1464 len = 1;
1465 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001466 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /*
1469 * If we got here, and the command completed successfully,
1470 * we're about to go into bus free state.
1471 */
1472
1473 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001474
1475timeout:
1476 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1477 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1481/*
1482 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1483 * unsigned char *phase, int *count, unsigned char **data)
1484 *
1485 * Purpose : transfers data in given phase using either real
1486 * or pseudo DMA.
1487 *
1488 * Inputs : instance - instance of driver, *phase - pointer to
1489 * what phase is expected, *count - pointer to number of
1490 * bytes to transfer, **data - pointer to data pointer.
1491 *
1492 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001493 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * is in same phase.
1495 *
1496 * Also, *phase, *count, *data are modified in place.
1497 *
1498 * Locks: io_request lock held by caller
1499 */
1500
1501
1502static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 register int c = *count;
1504 register unsigned char p = *phase;
1505 register unsigned char *d = *data;
1506 unsigned char tmp;
1507 int foo;
1508#if defined(REAL_DMA_POLL)
1509 int cnt, toPIO;
1510 unsigned char saved_data = 0, overrun = 0, residue;
1511#endif
1512
1513 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1516 *phase = tmp;
1517 return -1;
1518 }
1519#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001521 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
1522 c -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001524 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 -07001525 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1526#endif
1527
1528 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1529
1530#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001531 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1532 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001534 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535#else
1536 /*
1537 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1538 * were not disabled for the duration of a single DMA transfer, from
1539 * before the setting of DMA mode to after transfer of the last byte.
1540 */
1541
Finn Thain55181be2016-01-03 16:05:42 +11001542 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001543 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1544 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 else
Finn Thaincd400822016-01-03 16:05:40 +11001546 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547#endif /* def REAL_DMA */
1548
Finn Thain52a6a1c2014-03-18 11:42:18 +11001549 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /*
1552 * On the PAS16 at least I/O recovery delays are not needed here.
1553 * Everyone else seems to want them.
1554 */
1555
1556 if (p & SR_IO) {
1557 io_recovery_delay(1);
1558 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1559 } else {
1560 io_recovery_delay(1);
1561 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1562 io_recovery_delay(1);
1563 NCR5380_write(START_DMA_SEND_REG, 0);
1564 io_recovery_delay(1);
1565 }
1566
1567#if defined(REAL_DMA_POLL)
1568 do {
1569 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1570 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1571
1572/*
1573 At this point, either we've completed DMA, or we have a phase mismatch,
1574 or we've unexpectedly lost BUSY (which is a real error).
1575
1576 For write DMAs, we want to wait until the last byte has been
1577 transferred out over the bus before we turn off DMA mode. Alas, there
1578 seems to be no terribly good way of doing this on a 5380 under all
1579 conditions. For non-scatter-gather operations, we can wait until REQ
1580 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1581 are nastier, since the device will be expecting more data than we
1582 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1583 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1584 why this signal was added to the newer chips) but on the older 538[01]
1585 this signal does not exist. The workaround for this lack is a watchdog;
1586 we bail out of the wait-loop after a modest amount of wait-time if
1587 the usual exit conditions are not met. Not a terribly clean or
1588 correct solution :-%
1589
1590 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1591 If the chip is in DMA mode for an READ, it will respond to a target's
1592 REQ by latching the SCSI data into the INPUT DATA register and asserting
1593 ACK, even if it has _already_ been notified by the DMA controller that
1594 the current DMA transfer has completed! If the NCR5380 is then taken
1595 out of DMA mode, this already-acknowledged byte is lost.
1596
1597 This is not a problem for "one DMA transfer per command" reads, because
1598 the situation will never arise... either all of the data is DMA'ed
1599 properly, or the target switches to MESSAGE IN phase to signal a
1600 disconnection (either operation bringing the DMA to a clean halt).
1601 However, in order to handle scatter-reads, we must work around the
1602 problem. The chosen fix is to DMA N-2 bytes, then check for the
1603 condition before taking the NCR5380 out of DMA mode. One or two extra
1604 bytes are transferred via PIO as necessary to fill out the original
1605 request.
1606 */
1607
1608 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001609 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
1610 udelay(10);
1611 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1612 (BASR_PHASE_MATCH | BASR_ACK)) {
1613 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1614 overrun = 1;
1615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 } else {
1618 int limit = 100;
1619 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1620 if (!(tmp & BASR_PHASE_MATCH))
1621 break;
1622 if (--limit < 0)
1623 break;
1624 }
1625 }
1626
Finn Thain52a6a1c2014-03-18 11:42:18 +11001627 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 -07001628
1629 NCR5380_write(MODE_REG, MR_BASE);
1630 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1631
1632 residue = NCR5380_dma_residual(instance);
1633 c -= residue;
1634 *count -= c;
1635 *data += c;
1636 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1637
Finn Thain9db60242016-01-03 16:05:43 +11001638 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
1639 *phase == p && (p & SR_IO) && residue == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001641 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 **data = saved_data;
1643 *data += 1;
1644 *count -= 1;
1645 cnt = toPIO = 1;
1646 } else {
1647 printk("No overrun??\n");
1648 cnt = toPIO = 2;
1649 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001650 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 NCR5380_transfer_pio(instance, phase, &cnt, data);
1652 *count -= toPIO - cnt;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Finn Thain52a6a1c2014-03-18 11:42:18 +11001655 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 -07001656 return 0;
1657
1658#elif defined(REAL_DMA)
1659 return 0;
1660#else /* defined(REAL_DMA_POLL) */
1661 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001662 foo = NCR5380_pread(instance, d,
1663 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1664 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * We can't disable DMA mode after successfully transferring
1667 * what we plan to be the last byte, since that would open up
1668 * a race condition where if the target asserted REQ before
1669 * we got the DMA mode reset, the NCR5380 would have latched
1670 * an additional byte into the INPUT DATA register and we'd
1671 * have dropped it.
1672 *
1673 * The workaround was to transfer one fewer bytes than we
1674 * intended to with the pseudo-DMA read function, wait for
1675 * the chip to latch the last byte, read it, and then disable
1676 * pseudo-DMA mode.
1677 *
1678 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1679 * REQ is deasserted when ACK is asserted, and not reasserted
1680 * until ACK goes false. Since the NCR5380 won't lower ACK
1681 * until DACK is asserted, which won't happen unless we twiddle
1682 * the DMA port or we take the NCR5380 out of DMA mode, we
1683 * can guarantee that we won't handshake another extra
1684 * byte.
1685 */
1686
Finn Thain55181be2016-01-03 16:05:42 +11001687 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1688 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1689 foo = -1;
1690 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Finn Thain55181be2016-01-03 16:05:42 +11001692 if (NCR5380_poll_politely(instance, STATUS_REG,
1693 SR_REQ, 0, HZ) < 0) {
1694 foo = -1;
1695 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1696 }
1697 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001701 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * Wait for the last byte to be sent. If REQ is being asserted for
1704 * the byte we're interested, we'll ACK it and it will go false.
1705 */
Finn Thain55181be2016-01-03 16:05:42 +11001706 if (NCR5380_poll_politely2(instance,
1707 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1708 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1709 foo = -1;
1710 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 NCR5380_write(MODE_REG, MR_BASE);
1715 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001716 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 *data = d + c;
1718 *count = 0;
1719 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return foo;
1721#endif /* def REAL_DMA */
1722}
1723#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1724
1725/*
1726 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1727 *
1728 * Purpose : run through the various SCSI phases and do as the target
1729 * directs us to. Operates on the currently connected command,
1730 * instance->connected.
1731 *
1732 * Inputs : instance, instance for which we are doing commands
1733 *
1734 * Side effects : SCSI things happen, the disconnected queue will be
1735 * modified if a command disconnects, *instance->connected will
1736 * change.
1737 *
1738 * XXX Note : we need to watch for bus free or a reset condition here
1739 * to recover from an unexpected bus free condition.
1740 *
1741 * Locks: io_request_lock held by caller in IRQ mode
1742 */
1743
1744static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1746 unsigned char msgout = NOP;
1747 int sink = 0;
1748 int len;
1749#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1750 int transfersize;
1751#endif
1752 unsigned char *data;
1753 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001754 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Finn Thain11d2f632016-01-03 16:05:51 +11001756 while ((cmd = hostdata->connected)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 tmp = NCR5380_read(STATUS_REG);
1758 /* We only have a valid SCSI phase when REQ is asserted */
1759 if (tmp & SR_REQ) {
1760 phase = (tmp & PHASE_MASK);
1761 if (phase != old_phase) {
1762 old_phase = phase;
1763 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1764 }
1765 if (sink && (phase != PHASE_MSGOUT)) {
1766 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1767
1768 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1769 while (NCR5380_read(STATUS_REG) & SR_REQ);
1770 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1771 sink = 0;
1772 continue;
1773 }
1774 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 case PHASE_DATAOUT:
1776#if (NDEBUG & NDEBUG_NO_DATAOUT)
1777 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1778 sink = 1;
1779 do_abort(instance);
1780 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001781 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return;
1783#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001784 case PHASE_DATAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /*
1786 * If there is no room left in the current buffer in the
1787 * scatter-gather list, move onto the next one.
1788 */
1789
1790 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1791 ++cmd->SCp.buffer;
1792 --cmd->SCp.buffers_residual;
1793 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001794 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001795 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 -07001796 }
1797 /*
1798 * The preferred transfer method is going to be
1799 * PSEUDO-DMA for systems that are strictly PIO,
1800 * since we can let the hardware do the handshaking.
1801 *
1802 * For this to work, we need to know the transfersize
1803 * ahead of time, since the pseudo-DMA code will sit
1804 * in an unconditional loop.
1805 */
1806
1807#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001808 transfersize = 0;
1809 if (!cmd->device->borken &&
1810 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1811 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Finn Thainff3d4572016-01-03 16:05:25 +11001813 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 len = transfersize;
1815 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1816 /*
1817 * If the watchdog timer fires, all future accesses to this
1818 * device will use the polled-IO.
1819 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001820 scmd_printk(KERN_INFO, cmd,
1821 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 sink = 1;
1824 do_abort(instance);
1825 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001826 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* XXX - need to source or sink data here, as appropriate */
1828 } else
1829 cmd->SCp.this_residual -= transfersize - len;
1830 } else
1831#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
Finn Thain11d2f632016-01-03 16:05:51 +11001832 {
1833 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1835 &cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11001836 spin_lock_irq(&hostdata->lock);
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 break;
1839 case PHASE_MSGIN:
1840 len = 1;
1841 data = &tmp;
1842 NCR5380_transfer_pio(instance, &phase, &len, &data);
1843 cmd->SCp.Message = tmp;
1844
1845 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 case ABORT:
1847 case COMMAND_COMPLETE:
1848 /* Accept message by clearing ACK */
1849 sink = 1;
1850 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1851 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001852 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1853 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /*
1856 * I'm not sure what the correct thing to do here is :
1857 *
1858 * If the command that just executed is NOT a request
1859 * sense, the obvious thing to do is to set the result
1860 * code to the values of the stored parameters.
1861 *
1862 * If it was a REQUEST SENSE command, we need some way
1863 * to differentiate between the failure code of the original
1864 * and the failure code of the REQUEST sense - the obvious
1865 * case is success, where we fall through and leave the result
1866 * code unchanged.
1867 *
1868 * The non-obvious place is where the REQUEST SENSE failed
1869 */
1870
1871 if (cmd->cmnd[0] != REQUEST_SENSE)
1872 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1873 else if (status_byte(cmd->SCp.Status) != GOOD)
1874 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1875
Boaz Harrosh28424d32007-09-10 22:37:45 +03001876 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1877 hostdata->ses.cmd_len) {
1878 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1879 hostdata->ses.cmd_len = 0 ;
1880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Boaz Harrosh28424d32007-09-10 22:37:45 +03001882 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1883 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1884
Finn Thain52a6a1c2014-03-18 11:42:18 +11001885 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 LIST(cmd, hostdata->issue_queue);
1888 cmd->host_scribble = (unsigned char *)
1889 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11001890 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001891 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11001892 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 cmd->scsi_done(cmd);
1894 }
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /*
1897 * Restore phase bits to 0 so an interrupted selection,
1898 * arbitration can resume.
1899 */
1900 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001901
1902 /* Enable reselect interrupts */
1903 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return;
1905 case MESSAGE_REJECT:
1906 /* Accept message by clearing ACK */
1907 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1908 switch (hostdata->last_message) {
1909 case HEAD_OF_QUEUE_TAG:
1910 case ORDERED_QUEUE_TAG:
1911 case SIMPLE_QUEUE_TAG:
1912 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001913 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915 default:
1916 break;
1917 }
Finn Thain340b9612016-01-03 16:05:31 +11001918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 case DISCONNECT:{
1920 /* Accept message by clearing ACK */
1921 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 LIST(cmd, hostdata->disconnected_queue);
1923 cmd->host_scribble = (unsigned char *)
1924 hostdata->disconnected_queue;
1925 hostdata->connected = NULL;
1926 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001927 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 -07001928 /*
1929 * Restore phase bits to 0 so an interrupted selection,
1930 * arbitration can resume.
1931 */
1932 NCR5380_write(TARGET_COMMAND_REG, 0);
1933
1934 /* Enable reselect interrupts */
1935 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return;
1937 }
1938 /*
1939 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1940 * operation, in violation of the SCSI spec so we can safely
1941 * ignore SAVE/RESTORE pointers calls.
1942 *
1943 * Unfortunately, some disks violate the SCSI spec and
1944 * don't issue the required SAVE_POINTERS message before
1945 * disconnecting, and we have to break spec to remain
1946 * compatible.
1947 */
1948 case SAVE_POINTERS:
1949 case RESTORE_POINTERS:
1950 /* Accept message by clearing ACK */
1951 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1952 break;
1953 case EXTENDED_MESSAGE:
1954/*
1955 * Extended messages are sent in the following format :
1956 * Byte
1957 * 0 EXTENDED_MESSAGE == 1
1958 * 1 length (includes one byte for code, doesn't
1959 * include first two bytes)
1960 * 2 code
1961 * 3..length+1 arguments
1962 *
1963 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001964 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
1966 extended_msg[0] = EXTENDED_MESSAGE;
1967 /* Accept first byte by clearing ACK */
1968 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001969
1970 spin_unlock_irq(&hostdata->lock);
1971
Finn Thain52a6a1c2014-03-18 11:42:18 +11001972 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 len = 2;
1975 data = extended_msg + 1;
1976 phase = PHASE_MSGIN;
1977 NCR5380_transfer_pio(instance, &phase, &len, &data);
1978
Finn Thain52a6a1c2014-03-18 11:42:18 +11001979 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 -07001980
Finn Thaine0783ed2016-01-03 16:05:45 +11001981 if (!len && extended_msg[1] > 0 &&
1982 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* Accept third byte by clearing ACK */
1984 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1985 len = extended_msg[1] - 1;
1986 data = extended_msg + 3;
1987 phase = PHASE_MSGIN;
1988
1989 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001990 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 switch (extended_msg[2]) {
1993 case EXTENDED_SDTR:
1994 case EXTENDED_WDTR:
1995 case EXTENDED_MODIFY_DATA_POINTER:
1996 case EXTENDED_EXTENDED_IDENTIFY:
1997 tmp = 0;
1998 }
1999 } else if (len) {
2000 printk("scsi%d: error receiving extended message\n", instance->host_no);
2001 tmp = 0;
2002 } else {
2003 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2004 tmp = 0;
2005 }
Finn Thain11d2f632016-01-03 16:05:51 +11002006
2007 spin_lock_irq(&hostdata->lock);
2008 if (!hostdata->connected)
2009 return;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* Fall through to reject message */
2012
2013 /*
2014 * If we get something weird that we aren't expecting,
2015 * reject it.
2016 */
2017 default:
2018 if (!tmp) {
2019 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002020 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 printk("\n");
2022 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002023 scmd_printk(KERN_INFO, cmd,
2024 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002026 scmd_printk(KERN_INFO, cmd,
2027 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 msgout = MESSAGE_REJECT;
2030 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2031 break;
2032 } /* switch (tmp) */
2033 break;
2034 case PHASE_MSGOUT:
2035 len = 1;
2036 data = &msgout;
2037 hostdata->last_message = msgout;
2038 NCR5380_transfer_pio(instance, &phase, &len, &data);
2039 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002040 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 hostdata->connected = NULL;
2042 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 cmd->scsi_done(cmd);
2044 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2045 return;
2046 }
2047 msgout = NOP;
2048 break;
2049 case PHASE_CMDOUT:
2050 len = cmd->cmd_len;
2051 data = cmd->cmnd;
2052 /*
2053 * XXX for performance reasons, on machines with a
2054 * PSEUDO-DMA architecture we should probably
2055 * use the dma transfer function.
2056 */
2057 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 break;
2059 case PHASE_STATIN:
2060 len = 1;
2061 data = &tmp;
2062 NCR5380_transfer_pio(instance, &phase, &len, &data);
2063 cmd->SCp.Status = tmp;
2064 break;
2065 default:
2066 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002067 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002069 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002070 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002071 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002072 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Finn Thain11d2f632016-01-03 16:05:51 +11002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077/*
2078 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2079 *
2080 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002081 * 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 -07002082 * nexus has been reestablished,
2083 *
2084 * Inputs : instance - this instance of the NCR5380.
2085 *
2086 * Locks: io_request_lock held by caller if IRQ driven
2087 */
2088
2089static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2091 instance->hostdata;
2092 unsigned char target_mask;
2093 unsigned char lun, phase;
2094 int len;
2095 unsigned char msg[3];
2096 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002097 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /*
2100 * Disable arbitration, etc. since the host adapter obviously
2101 * lost, and tell an interrupted NCR5380_select() to restart.
2102 */
2103
2104 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain72064a72016-01-03 16:05:44 +11002107 dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 /*
2110 * At this point, we have detected that our SCSI ID is on the bus,
2111 * SEL is true and BSY was false for at least one bus settle delay
2112 * (400 ns).
2113 *
2114 * We must assert BSY ourselves, until the target drops the SEL
2115 * signal.
2116 */
2117
2118 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002119 if (NCR5380_poll_politely(instance,
2120 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2121 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2122 return;
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2125
2126 /*
2127 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 */
2129
Finn Thain1cc160e2016-01-03 16:05:32 +11002130 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002131 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2132 do_abort(instance);
2133 return;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 len = 1;
2137 data = msg;
2138 phase = PHASE_MSGIN;
2139 NCR5380_transfer_pio(instance, &phase, &len, &data);
2140
Finn Thain72064a72016-01-03 16:05:44 +11002141 if (len) {
2142 do_abort(instance);
2143 return;
2144 }
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002147 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002148 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002149 printk("\n");
2150 do_abort(instance);
2151 return;
2152 }
2153 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Finn Thain72064a72016-01-03 16:05:44 +11002155 /*
2156 * We need to add code for SCSI-II to track which devices have
2157 * I_T_L_Q nexuses established, and which have simple I_T_L
2158 * nexuses so we can chose to do additional data transfer.
2159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Finn Thain72064a72016-01-03 16:05:44 +11002161 /*
2162 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2163 * just reestablished, and remove it from the disconnected queue.
2164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Finn Thain72064a72016-01-03 16:05:44 +11002166 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
2167 tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble) {
2168 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)) {
2169 if (prev) {
2170 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2171 prev->host_scribble = tmp->host_scribble;
2172 } else {
2173 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2174 hostdata->disconnected_queue =
2175 (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Finn Thain72064a72016-01-03 16:05:44 +11002177 tmp->host_scribble = NULL;
2178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 }
Finn Thain72064a72016-01-03 16:05:44 +11002181 if (!tmp) {
2182 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2183 target_mask, lun);
2184 /*
2185 * Since we have an established nexus that we can't do anything with,
2186 * we must abort it.
2187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Finn Thain72064a72016-01-03 16:05:44 +11002191
2192 /* Accept message by clearing ACK */
2193 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2194
2195 hostdata->connected = tmp;
2196 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n",
2197 instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
2200/*
2201 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2202 *
2203 * Purpose : called by interrupt handler when DMA finishes or a phase
2204 * mismatch occurs (which would finish the DMA transfer).
2205 *
2206 * Inputs : instance - this instance of the NCR5380.
2207 *
Finn Thain710ddd02014-11-12 16:12:02 +11002208 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * nexus has been reestablished, on failure NULL is returned.
2210 */
2211
2212#ifdef REAL_DMA
2213static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002214 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 /*
2218 * XXX this might not be right.
2219 *
2220 * Wait for final byte to transfer, ie wait for ACK to go false.
2221 *
2222 * We should use the Last Byte Sent bit, unfortunately this is
2223 * not available on the 5380/5381 (only the various CMOS chips)
2224 *
2225 * FIXME: timeout, and need to handle long timeout/irq case
2226 */
2227
2228 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2231
2232 /*
2233 * The only places we should see a phase mismatch and have to send
2234 * data from the same set of pointers will be the data transfer
2235 * phases. So, residual, requested length are only important here.
2236 */
2237
2238 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2239 transferred = instance->dmalen - NCR5380_dma_residual();
2240 hostdata->connected->SCp.this_residual -= transferred;
2241 hostdata->connected->SCp.ptr += transferred;
2242 }
2243}
2244#endif /* def REAL_DMA */
2245
2246/*
Finn Thain710ddd02014-11-12 16:12:02 +11002247 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 *
2249 * Purpose : abort a command
2250 *
Finn Thain710ddd02014-11-12 16:12:02 +11002251 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002252 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 * used.
2254 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002255 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002257 * XXX - there is no way to abort the command that is currently
2258 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * a problem, we could implement longjmp() / setjmp(), setjmp()
2260 * called where the loop started in NCR5380_main().
2261 *
2262 * Locks: host lock taken by caller
2263 */
2264
Finn Thain710ddd02014-11-12 16:12:02 +11002265static int NCR5380_abort(struct scsi_cmnd *cmd)
2266{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 struct Scsi_Host *instance = cmd->device->host;
2268 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002269 struct scsi_cmnd *tmp, **prev;
Finn Thain11d2f632016-01-03 16:05:51 +11002270 unsigned long flags;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002271
2272 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Finn Thain11d2f632016-01-03 16:05:51 +11002274 spin_lock_irqsave(&hostdata->lock, flags);
2275
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002276 NCR5380_dprint(NDEBUG_ANY, instance);
2277 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Finn Thain52a6a1c2014-03-18 11:42:18 +11002279 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2280 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 -07002281
2282#if 0
2283/*
2284 * Case 1 : If the command is the currently executing command,
2285 * we'll set the aborted flag and return control so that
2286 * information transfer routine can exit cleanly.
2287 */
2288
2289 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002290 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/*
2292 * We should perform BSY checking, and make sure we haven't slipped
2293 * into BUS FREE.
2294 */
2295
2296 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2297/*
2298 * Since we can't change phases until we've completed the current
2299 * handshake, we have to source or sink a byte of data if the current
2300 * phase is not MSGOUT.
2301 */
2302
2303/*
2304 * Return control to the executing NCR drive so we can clear the
2305 * aborted flag and get back into our main loop.
2306 */
2307
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002308 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310#endif
2311
2312/*
2313 * Case 2 : If the command hasn't been issued yet, we simply remove it
2314 * from the issue queue.
2315 */
2316
Finn Thain52a6a1c2014-03-18 11:42:18 +11002317 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002318 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 -07002319 if (cmd == tmp) {
2320 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002321 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 tmp->host_scribble = NULL;
Finn Thain11d2f632016-01-03 16:05:51 +11002323 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002325 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002326 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return SUCCESS;
2328 }
2329#if (NDEBUG & NDEBUG_ABORT)
2330 /* KLL */
2331 else if (prev == tmp)
2332 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2333#endif
2334
2335/*
2336 * Case 3 : If any commands are connected, we're going to fail the abort
2337 * and let the high level SCSI driver retry at a later time or
2338 * issue a reset.
2339 *
2340 * Timeouts, and therefore aborted commands, will be highly unlikely
2341 * and handling them cleanly in this situation would make the common
2342 * case of noresets less efficient, and would pollute our code. So,
2343 * we fail.
2344 */
2345
2346 if (hostdata->connected) {
Finn Thain11d2f632016-01-03 16:05:51 +11002347 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002348 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return FAILED;
2350 }
2351/*
2352 * Case 4: If the command is currently disconnected from the bus, and
2353 * there are no connected commands, we reconnect the I_T_L or
2354 * I_T_L_Q nexus associated with it, go into message out, and send
2355 * an abort message.
2356 *
2357 * This case is especially ugly. In order to reestablish the nexus, we
2358 * need to call NCR5380_select(). The easiest way to implement this
2359 * function was to abort if the bus was busy, and let the interrupt
2360 * handler triggered on the SEL for reselect take care of lost arbitrations
2361 * where necessary, meaning interrupts need to be enabled.
2362 *
2363 * When interrupts are enabled, the queues may change - so we
2364 * can't remove it from the disconnected queue before selecting it
2365 * because that could cause a failure in hashing the nexus if that
2366 * device reselected.
2367 *
2368 * Since the queues may change, we can't use the pointers from when we
2369 * first locate it.
2370 *
2371 * So, we must first locate the command, and if NCR5380_select()
2372 * succeeds, then issue the abort, relocate the command and remove
2373 * it from the disconnected queue.
2374 */
2375
Finn Thain710ddd02014-11-12 16:12:02 +11002376 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002378 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Finn Thain9dafbd92016-01-03 16:05:28 +11002380 if (NCR5380_select(instance, cmd)) {
Finn Thain11d2f632016-01-03 16:05:51 +11002381 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return FAILED;
Finn Thain9dafbd92016-01-03 16:05:28 +11002383 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11002384 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 do_abort(instance);
2387
Finn Thain710ddd02014-11-12 16:12:02 +11002388 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 -07002389 if (cmd == tmp) {
2390 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002391 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 tmp->host_scribble = NULL;
Finn Thain11d2f632016-01-03 16:05:51 +11002393 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002395 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return SUCCESS;
2397 }
2398 }
2399/*
2400 * Case 5 : If we reached this point, the command was not found in any of
2401 * the queues.
2402 *
2403 * We probably reached this point because of an unlikely race condition
2404 * between the command completing successfully and the abortion code,
2405 * so we won't panic, but we will notify the user in case something really
2406 * broke.
2407 */
Finn Thain11d2f632016-01-03 16:05:51 +11002408 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2410 " before abortion\n", instance->host_no);
2411 return FAILED;
2412}
2413
2414
Finn Thain3be1b3e2016-01-03 16:05:12 +11002415/**
2416 * NCR5380_bus_reset - reset the SCSI bus
2417 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002419 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 */
2421
Finn Thain710ddd02014-11-12 16:12:02 +11002422static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002423{
2424 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002425 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2426 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Finn Thain11d2f632016-01-03 16:05:51 +11002428 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002429
2430#if (NDEBUG & NDEBUG_ANY)
2431 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
Finn Thain3be1b3e2016-01-03 16:05:12 +11002432#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002433 NCR5380_dprint(NDEBUG_ANY, instance);
2434 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002435
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002436 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002437
Finn Thain11d2f632016-01-03 16:05:51 +11002438 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return SUCCESS;
2441}