blob: c6b69ee0021acdb4363f3c1b9c2bc1bbf8cd2e58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * For more information, please consult
15 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Revision 1.10 1998/9/2 Alan Cox
Alan Coxfa195af2008-10-27 15:16:36 +000029 * (alan@lxorguk.ukuu.org.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Fixed up the timer lockups reported so far. Things still suck. Looking
31 * forward to 2.3 and per device request queues. Then it'll be possible to
32 * SMP thread this beast and improve life no end.
33
34 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
35 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
36 * (hopefully) fixed and enhanced USLEEP
37 * added support for DTC3181E card (for Mustek scanner)
38 *
39
40 * Revision 1.8 Ingmar Baumgart
41 * (ingmar@gonzo.schwaben.de)
42 * added support for NCR53C400a card
43 *
44
45 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
46 * added proc_info
47 * added support needed for DTC 3180/3280
48 * fixed a couple of bugs
49 *
50
51 * Revision 1.5 1994/01/19 09:14:57 drew
52 * Fixed udelay() hack that was being used on DATAOUT phases
53 * instead of a proper wait for the final handshake.
54 *
55 * Revision 1.4 1994/01/19 06:44:25 drew
56 * *** empty log message ***
57 *
58 * Revision 1.3 1994/01/19 05:24:40 drew
59 * Added support for TCR LAST_BYTE_SENT bit.
60 *
61 * Revision 1.2 1994/01/15 06:14:11 drew
62 * REAL DMA support, bug fixes.
63 *
64 * Revision 1.1 1994/01/15 06:00:54 drew
65 * Initial revision
66 *
67 */
68
69/*
70 * Further development / testing that should be done :
71 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
72 * code so that everything does the same thing that's done at the
73 * end of a pseudo-DMA read operation.
74 *
75 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
76 * basically, transfer size needs to be reduced by one
77 * and the last byte read as is done with PSEUDO_DMA.
78 *
79 * 4. Test SCSI-II tagged queueing (I have no devices which support
80 * tagged queueing)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
db9dff32005-04-03 14:53:59 -050082#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050083#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#if (NDEBUG & NDEBUG_LISTS)
86#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
87#define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
88#else
89#define LIST(x,y)
90#define REMOVE(w,x,y,z)
91#endif
92
93#ifndef notyet
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#undef REAL_DMA
95#endif
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#ifdef BOARD_REQUIRES_NO_DELAY
98#define io_recovery_delay(x)
99#else
100#define io_recovery_delay(x) udelay(x)
101#endif
102
103/*
104 * Design
105 *
106 * This is a generic 5380 driver. To use it on a different platform,
107 * one simply writes appropriate system specific macros (ie, data
108 * transfer - some PC's will use the I/O bus, 68K's must use
109 * memory mapped) and drops this file in their 'C' wrapper.
110 *
111 * (Note from hch: unfortunately it was not enough for the different
112 * m68k folks and instead of improving this driver they copied it
113 * and hacked it up for their needs. As a consequence they lost
114 * most updates to this driver. Maybe someone will fix all these
115 * drivers to use a common core one day..)
116 *
117 * As far as command queueing, two queues are maintained for
118 * each 5380 in the system - commands that haven't been issued yet,
119 * and commands that are currently executing. This means that an
120 * unlimited number of commands may be queued, letting
121 * more commands propagate from the higher driver levels giving higher
122 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
123 * allowing multiple commands to propagate all the way to a SCSI-II device
124 * while a command is already executing.
125 *
126 *
127 * Issues specific to the NCR5380 :
128 *
129 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
130 * piece of hardware that requires you to sit in a loop polling for
131 * the REQ signal as long as you are connected. Some devices are
132 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100133 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * broken devices are the exception rather than the rule and I'd rather
135 * spend my time optimizing for the normal case.
136 *
137 * Architecture :
138 *
139 * At the heart of the design is a coroutine, NCR5380_main,
140 * which is started from a workqueue for each NCR5380 host in the
141 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
142 * removing the commands from the issue queue and calling
143 * NCR5380_select() if a nexus is not established.
144 *
145 * Once a nexus is established, the NCR5380_information_transfer()
146 * phase goes through the various phases as instructed by the target.
147 * if the target goes into MSG IN and sends a DISCONNECT message,
148 * the command structure is placed into the per instance disconnected
149 * queue, and NCR5380_main tries to find more work. If the target is
150 * idle for too long, the system will try to sleep.
151 *
152 * If a command has disconnected, eventually an interrupt will trigger,
153 * calling NCR5380_intr() which will in turn call NCR5380_reselect
154 * to reestablish a nexus. This will run main if necessary.
155 *
156 * On command termination, the done function will be called as
157 * appropriate.
158 *
159 * SCSI pointers are maintained in the SCp field of SCSI command
160 * structures, being initialized after the command is connected
161 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
162 * Note that in violation of the standard, an implicit SAVE POINTERS operation
163 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
164 */
165
166/*
167 * Using this file :
168 * This file a skeleton Linux SCSI driver for the NCR 5380 series
169 * of chips. To use it, you write an architecture specific functions
170 * and macros and include this file in your driver.
171 *
172 * These macros control options :
173 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
174 * defined.
175 *
176 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
177 * for commands that return with a CHECK CONDITION status.
178 *
179 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
180 * transceivers.
181 *
182 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
183 * override-configure an IRQ.
184 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
186 *
187 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
188 *
189 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
190 * rely on phase mismatch and EOP interrupts to determine end
191 * of phase.
192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * Defaults for these will be provided although the user may want to adjust
194 * these to allocate CPU resources to the SCSI driver or "real" code.
195 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * These macros MUST be defined :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
198 * NCR5380_read(register) - read from the specified register
199 *
200 * NCR5380_write(register, value) - write to the specific register
201 *
202 * NCR5380_implementation_fields - additional fields needed for this
203 * specific implementation of the NCR5380
204 *
205 * Either real DMA *or* pseudo DMA may be implemented
206 * REAL functions :
207 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
208 * Note that the DMA setup functions should return the number of bytes
209 * that they were able to program the controller for.
210 *
211 * Also note that generic i386/PC versions of these macros are
212 * available as NCR5380_i386_dma_write_setup,
213 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
214 *
215 * NCR5380_dma_write_setup(instance, src, count) - initialize
216 * NCR5380_dma_read_setup(instance, dst, count) - initialize
217 * NCR5380_dma_residual(instance); - residual count
218 *
219 * PSEUDO functions :
220 * NCR5380_pwrite(instance, src, count)
221 * NCR5380_pread(instance, dst, count);
222 *
223 * The generic driver is initialized by calling NCR5380_init(instance),
224 * after setting the appropriate host specific fields and ID. If the
225 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
226 * possible) function may be used.
227 */
228
Finn Thain54d8fe42016-01-03 16:05:06 +1100229static int do_abort(struct Scsi_Host *);
230static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*
233 * initialize_SCp - init the scsi pointer field
234 * @cmd: command block to set up
235 *
236 * Set up the internal fields in the SCSI command.
237 */
238
Finn Thain710ddd02014-11-12 16:12:02 +1100239static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 /*
242 * Initialize the Scsi Pointer field so that all of the commands in the
243 * various queues are valid.
244 */
245
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200246 if (scsi_bufflen(cmd)) {
247 cmd->SCp.buffer = scsi_sglist(cmd);
248 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200249 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cmd->SCp.this_residual = cmd->SCp.buffer->length;
251 } else {
252 cmd->SCp.buffer = NULL;
253 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200254 cmd->SCp.ptr = NULL;
255 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257}
258
259/**
Finn Thainb32ade12016-01-03 16:05:41 +1100260 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100261 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100262 * @reg1: 5380 register to poll
263 * @bit1: Bitmask to check
264 * @val1: Expected value
265 * @reg2: Second 5380 register to poll
266 * @bit2: Second bitmask to check
267 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100268 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
Finn Thain2f854b82016-01-03 16:05:22 +1100270 * Polls the chip in a reasonably efficient manner waiting for an
271 * event to occur. After a short quick poll we begin to yield the CPU
272 * (if possible). In irq contexts the time-out is arbitrarily limited.
273 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
Finn Thainb32ade12016-01-03 16:05:41 +1100275 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Finn Thainb32ade12016-01-03 16:05:41 +1100278static int NCR5380_poll_politely2(struct Scsi_Host *instance,
279 int reg1, int bit1, int val1,
280 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100281{
282 struct NCR5380_hostdata *hostdata = shost_priv(instance);
283 unsigned long deadline = jiffies + wait;
284 unsigned long n;
285
286 /* Busy-wait for up to 10 ms */
287 n = min(10000U, jiffies_to_usecs(wait));
288 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100289 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100290 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100291 if ((NCR5380_read(reg1) & bit1) == val1)
292 return 0;
293 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100296 } while (n--);
297
298 if (irqs_disabled() || in_interrupt())
299 return -ETIMEDOUT;
300
301 /* Repeatedly sleep for 1 ms until deadline */
302 while (time_is_after_jiffies(deadline)) {
303 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100304 if ((NCR5380_read(reg1) & bit1) == val1)
305 return 0;
306 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Finn Thain2f854b82016-01-03 16:05:22 +1100309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -ETIMEDOUT;
311}
312
Finn Thainb32ade12016-01-03 16:05:41 +1100313static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
314 int reg, int bit, int val, int wait)
315{
316 return NCR5380_poll_politely2(instance, reg, bit, val,
317 reg, bit, val, wait);
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static struct {
321 unsigned char value;
322 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700323} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 {PHASE_DATAOUT, "DATAOUT"},
325 {PHASE_DATAIN, "DATAIN"},
326 {PHASE_CMDOUT, "CMDOUT"},
327 {PHASE_STATIN, "STATIN"},
328 {PHASE_MSGOUT, "MSGOUT"},
329 {PHASE_MSGIN, "MSGIN"},
330 {PHASE_UNKNOWN, "UNKNOWN"}
331};
332
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100333#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static struct {
335 unsigned char mask;
336 const char *name;
337} signals[] = {
338 {SR_DBP, "PARITY"},
339 {SR_RST, "RST"},
340 {SR_BSY, "BSY"},
341 {SR_REQ, "REQ"},
342 {SR_MSG, "MSG"},
343 {SR_CD, "CD"},
344 {SR_IO, "IO"},
345 {SR_SEL, "SEL"},
346 {0, NULL}
347},
348basrs[] = {
349 {BASR_ATN, "ATN"},
350 {BASR_ACK, "ACK"},
351 {0, NULL}
352},
353icrs[] = {
354 {ICR_ASSERT_RST, "ASSERT RST"},
355 {ICR_ASSERT_ACK, "ASSERT ACK"},
356 {ICR_ASSERT_BSY, "ASSERT BSY"},
357 {ICR_ASSERT_SEL, "ASSERT SEL"},
358 {ICR_ASSERT_ATN, "ASSERT ATN"},
359 {ICR_ASSERT_DATA, "ASSERT DATA"},
360 {0, NULL}
361},
362mrs[] = {
363 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
364 {MR_TARGET, "MODE TARGET"},
365 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
366 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
367 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
368 {MR_DMA_MODE, "MODE DMA"},
369 {MR_ARBITRATE, "MODE ARBITRATION"},
370 {0, NULL}
371};
372
373/**
374 * NCR5380_print - print scsi bus signals
375 * @instance: adapter state to dump
376 *
377 * Print the SCSI bus signals for debugging purposes
378 *
379 * Locks: caller holds hostdata lock (not essential)
380 */
381
382static void NCR5380_print(struct Scsi_Host *instance)
383{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
387 status = NCR5380_read(STATUS_REG);
388 mr = NCR5380_read(MODE_REG);
389 icr = NCR5380_read(INITIATOR_COMMAND_REG);
390 basr = NCR5380_read(BUS_AND_STATUS_REG);
391
392 printk("STATUS_REG: %02x ", status);
393 for (i = 0; signals[i].mask; ++i)
394 if (status & signals[i].mask)
395 printk(",%s", signals[i].name);
396 printk("\nBASR: %02x ", basr);
397 for (i = 0; basrs[i].mask; ++i)
398 if (basr & basrs[i].mask)
399 printk(",%s", basrs[i].name);
400 printk("\nICR: %02x ", icr);
401 for (i = 0; icrs[i].mask; ++i)
402 if (icr & icrs[i].mask)
403 printk(",%s", icrs[i].name);
404 printk("\nMODE: %02x ", mr);
405 for (i = 0; mrs[i].mask; ++i)
406 if (mr & mrs[i].mask)
407 printk(",%s", mrs[i].name);
408 printk("\n");
409}
410
411
412/*
413 * NCR5380_print_phase - show SCSI phase
414 * @instance: adapter to dump
415 *
416 * Print the current SCSI phase for debugging purposes
417 *
418 * Locks: none
419 */
420
421static void NCR5380_print_phase(struct Scsi_Host *instance)
422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unsigned char status;
424 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 status = NCR5380_read(STATUS_REG);
427 if (!(status & SR_REQ))
428 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
429 else {
430 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
431 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
432 }
433}
434#endif
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Finn Thaind5f7e652016-01-03 16:05:03 +1100437static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/**
440 * probe_intr - helper for IRQ autoprobe
441 * @irq: interrupt number
442 * @dev_id: unused
443 * @regs: unused
444 *
445 * Set a flag to indicate the IRQ in question was received. This is
446 * used by the IRQ probe code.
447 */
448
David Howells7d12e782006-10-05 14:55:46 +0100449static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 probe_irq = irq;
452 return IRQ_HANDLED;
453}
454
455/**
456 * NCR5380_probe_irq - find the IRQ of an NCR5380
457 * @instance: NCR5380 controller
458 * @possible: bitmask of ISA IRQ lines
459 *
460 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
461 * and then looking to see what interrupt actually turned up.
462 *
463 * Locks: none, irqs must be enabled on entry
464 */
465
Andrew Morton702809c2007-05-23 14:41:56 -0700466static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
467 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
470 unsigned long timeout;
471 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Finn Thain22f5f102014-11-12 16:11:56 +1100473 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100474 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 trying_irqs |= mask;
476
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500477 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100478 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /*
481 * A interrupt is triggered whenever BSY = false, SEL = true
482 * and a bit set in the SELECT_ENABLE_REG is asserted on the
483 * SCSI bus.
484 *
485 * Note that the bus is only driven when the phase control signals
486 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
487 * to zero.
488 */
489
490 NCR5380_write(TARGET_COMMAND_REG, 0);
491 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
492 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
493 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
494
Finn Thain22f5f102014-11-12 16:11:56 +1100495 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800496 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 NCR5380_write(SELECT_ENABLE_REG, 0);
499 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
500
Finn Thain22f5f102014-11-12 16:11:56 +1100501 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (trying_irqs & mask)
503 free_irq(i, NULL);
504
505 return probe_irq;
506}
507
508/**
Finn Thain8c325132014-11-12 16:11:58 +1100509 * NCR58380_info - report driver and host information
510 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
Finn Thain8c325132014-11-12 16:11:58 +1100512 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
514 * Locks: none
515 */
516
Finn Thain8c325132014-11-12 16:11:58 +1100517static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Finn Thain8c325132014-11-12 16:11:58 +1100519 struct NCR5380_hostdata *hostdata = shost_priv(instance);
520
521 return hostdata->info;
522}
523
524static void prepare_info(struct Scsi_Host *instance)
525{
526 struct NCR5380_hostdata *hostdata = shost_priv(instance);
527
528 snprintf(hostdata->info, sizeof(hostdata->info),
529 "%s, io_port 0x%lx, n_io_port %d, "
530 "base 0x%lx, irq %d, "
531 "can_queue %d, cmd_per_lun %d, "
532 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100533 "flags { %s%s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100534 "options { %s} ",
535 instance->hostt->name, instance->io_port, instance->n_io_port,
536 instance->base, instance->irq,
537 instance->can_queue, instance->cmd_per_lun,
538 instance->sg_tablesize, instance->this_id,
Finn Thain55181be2016-01-03 16:05:42 +1100539 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100540 hostdata->flags & FLAG_DTC3181E ? "DTC3181E " : "",
541 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100542 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100544 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100547 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#endif
549#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100550 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#endif
552#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100553 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100556 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
558#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100559 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#endif
Finn Thain8c325132014-11-12 16:11:58 +1100561 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/**
565 * NCR5380_print_status - dump controller info
566 * @instance: controller to dump
567 *
568 * Print commands in the various queues, called from NCR5380_abort
569 * and NCR5380_debug to aid debugging.
570 *
571 * Locks: called functions disable irqs
572 */
573
Finn Thain3be1b3e2016-01-03 16:05:12 +1100574static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 NCR5380_dprint(NDEBUG_ANY, instance);
577 NCR5380_dprint_phase(NDEBUG_ANY, instance);
578}
579
Finn Thaina9c2dc42014-11-12 16:11:59 +1100580#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/******************************************/
582/*
583 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
584 *
585 * *buffer: I/O buffer
586 * **start: if inout == FALSE pointer into buffer where user read should start
587 * offset: current offset
588 * length: length of buffer
589 * hostno: Scsi_Host host_no
590 * inout: TRUE - user is writing; FALSE - user is reading
591 *
592 * Return the number of bytes read from or written
593 */
594
Al Virodd7ab712013-03-31 01:15:54 -0400595static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
596 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100598 struct NCR5380_hostdata *hostdata = shost_priv(instance);
599
600 hostdata->spin_max_r = 0;
601 hostdata->spin_max_w = 0;
602 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400603}
Finn Thaina9c2dc42014-11-12 16:11:59 +1100604#endif
Al Virodd7ab712013-03-31 01:15:54 -0400605
Al Virodd7ab712013-03-31 01:15:54 -0400606static
Finn Thain710ddd02014-11-12 16:12:02 +1100607void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m);
Al Virodd7ab712013-03-31 01:15:54 -0400608static
609void lprint_command(unsigned char *cmd, struct seq_file *m);
610static
611void lprint_opcode(int opcode, struct seq_file *m);
612
613static int __maybe_unused NCR5380_show_info(struct seq_file *m,
614 struct Scsi_Host *instance)
615{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100617 struct scsi_cmnd *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
620
Finn Thaina9c2dc42014-11-12 16:11:59 +1100621#ifdef PSEUDO_DMA
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100622 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100623 hostdata->spin_max_w, hostdata->spin_max_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624#endif
625 spin_lock_irq(instance->host_lock);
626 if (!hostdata->connected)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100627 seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 else
Finn Thain710ddd02014-11-12 16:12:02 +1100629 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100630 seq_printf(m, "scsi%d: issue_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100631 for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400632 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100634 seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100635 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400636 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 spin_unlock_irq(instance->host_lock);
Al Virodd7ab712013-03-31 01:15:54 -0400638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Finn Thain710ddd02014-11-12 16:12:02 +1100641static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100643 seq_printf(m, "scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100644 seq_puts(m, " command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400645 lprint_command(cmd->cmnd, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Al Virodd7ab712013-03-31 01:15:54 -0400648static void lprint_command(unsigned char *command, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 int i, s;
Al Virodd7ab712013-03-31 01:15:54 -0400651 lprint_opcode(command[0], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100653 seq_printf(m, "%02x ", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100654 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Al Virodd7ab712013-03-31 01:15:54 -0400657static void lprint_opcode(int opcode, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100659 seq_printf(m, "%2d (0x%02x)", opcode, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662
663/**
664 * NCR5380_init - initialise an NCR5380
665 * @instance: adapter to configure
666 * @flags: control flags
667 *
668 * Initializes *instance and corresponding 5380 chip,
669 * with flags OR'd into the initial flags value.
670 *
671 * Notes : I assume that the host, hostno, and id bits have been
672 * set correctly. I don't care about the irq and other fields.
673 *
674 * Returns 0 for success
675 *
676 * Locks: interrupts must be enabled when we are called
677 */
678
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800679static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Finn Thainb6488f92016-01-03 16:05:08 +1100681 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain2f854b82016-01-03 16:05:22 +1100683 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if(in_interrupt())
686 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 hostdata->id_mask = 1 << instance->this_id;
689 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
690 if (i > hostdata->id_mask)
691 hostdata->id_higher_mask |= i;
692 for (i = 0; i < 8; ++i)
693 hostdata->busy[i] = 0;
694#ifdef REAL_DMA
695 hostdata->dmalen = 0;
696#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 hostdata->connected = NULL;
698 hostdata->issue_queue = NULL;
699 hostdata->disconnected_queue = NULL;
Finn Thain55181be2016-01-03 16:05:42 +1100700 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Finn Thain8d8601a2016-01-03 16:05:37 +1100702 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100703 hostdata->work_q = alloc_workqueue("ncr5380_%d",
704 WQ_UNBOUND | WQ_MEM_RECLAIM,
705 1, instance->host_no);
706 if (!hostdata->work_q)
707 return -ENOMEM;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Finn Thain8c325132014-11-12 16:11:58 +1100711 prepare_info(instance);
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
714 NCR5380_write(MODE_REG, MR_BASE);
715 NCR5380_write(TARGET_COMMAND_REG, 0);
716 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100717
718 /* Calibrate register polling loop */
719 i = 0;
720 deadline = jiffies + 1;
721 do {
722 cpu_relax();
723 } while (time_is_after_jiffies(deadline));
724 deadline += msecs_to_jiffies(256);
725 do {
726 NCR5380_read(STATUS_REG);
727 ++i;
728 cpu_relax();
729 } while (time_is_after_jiffies(deadline));
730 hostdata->accesses_per_ms = i / 256;
731
Finn Thainb6488f92016-01-03 16:05:08 +1100732 return 0;
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Finn Thainb6488f92016-01-03 16:05:08 +1100735/**
736 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
737 * @instance: adapter to check
738 *
739 * If the system crashed, it may have crashed with a connected target and
740 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
741 * currently established nexus, which we know nothing about. Failing that
742 * do a bus reset.
743 *
744 * Note that a bus reset will cause the chip to assert IRQ.
745 *
746 * Returns 0 if successful, otherwise -ENXIO.
747 */
748
749static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
750{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100751 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100752 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
755 switch (pass) {
756 case 1:
757 case 3:
758 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100759 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
760 NCR5380_poll_politely(instance,
761 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100764 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 do_abort(instance);
766 break;
767 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100768 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100770 /* Wait after a reset; the SCSI standard calls for
771 * 250ms, we wait 500ms to be on the safe side.
772 * But some Toshiba CD-ROMs need ten times that.
773 */
774 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
775 msleep(2500);
776 else
777 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 break;
779 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100780 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -ENXIO;
782 }
783 }
784 return 0;
785}
786
787/**
788 * NCR5380_exit - remove an NCR5380
789 * @instance: adapter to remove
790 */
791
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800792static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
795
Finn Thain8d8601a2016-01-03 16:05:37 +1100796 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100797 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/**
Finn Thain1bb40582016-01-03 16:05:29 +1100801 * NCR5380_queue_command - queue a command
802 * @instance: the relevant SCSI adapter
803 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Finn Thain1bb40582016-01-03 16:05:29 +1100805 * cmd is added to the per-instance issue queue, with minor
806 * twiddling done to the host specific fields of cmd. If the
807 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809
Finn Thain1bb40582016-01-03 16:05:29 +1100810static int NCR5380_queue_command(struct Scsi_Host *instance,
811 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Finn Thain1bb40582016-01-03 16:05:29 +1100813 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100814 struct scsi_cmnd *tmp;
Finn Thain1bb40582016-01-03 16:05:29 +1100815 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817#if (NDEBUG & NDEBUG_NO_WRITE)
818 switch (cmd->cmnd[0]) {
819 case WRITE_6:
820 case WRITE_10:
821 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
822 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100823 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825 }
826#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /*
829 * We use the host_scribble field as a pointer to the next command
830 * in a queue
831 */
832
833 cmd->host_scribble = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 cmd->result = 0;
835
Finn Thain1bb40582016-01-03 16:05:29 +1100836 spin_lock_irqsave(instance->host_lock, flags);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * Insert the cmd into the issue queue. Note that REQUEST SENSE
840 * commands are added to the head of the queue since any command will
841 * clear the contingent allegiance condition that exists and the
842 * sense data is only guaranteed to be valid while the condition exists.
843 */
844
845 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
846 LIST(cmd, hostdata->issue_queue);
847 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
848 hostdata->issue_queue = cmd;
849 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100850 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 -0700851 LIST(cmd, tmp);
852 tmp->host_scribble = (unsigned char *) cmd;
853 }
Finn Thain1bb40582016-01-03 16:05:29 +1100854 spin_unlock_irqrestore(instance->host_lock, flags);
855
Finn Thain52a6a1c2014-03-18 11:42:18 +1100856 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 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100859 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/**
864 * NCR5380_main - NCR state machines
865 *
866 * NCR5380_main is a coroutine that runs as long as more work can
867 * be done on the NCR5380 host adapters in a system. Both
868 * NCR5380_queue_command() and NCR5380_intr() will try to start it
869 * in case it is not running.
870 *
871 * Locks: called as its own thread with no locks held. Takes the
872 * host lock and called routines may take the isa dma lock.
873 */
874
David Howellsc4028952006-11-22 14:57:56 +0000875static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
David Howellsc4028952006-11-22 14:57:56 +0000877 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100878 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100880 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int done;
882
883 spin_lock_irq(instance->host_lock);
884 do {
885 /* Lock held here */
886 done = 1;
Finn Thainae753a32016-01-03 16:05:23 +1100887 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100888 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /*
890 * Search through the issue_queue for a command destined
891 * for a target that's not busy.
892 */
Finn Thain710ddd02014-11-12 16:12:02 +1100893 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 -0700894 {
895 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200896 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 -0700897 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200898 if (!(hostdata->busy[tmp->device->id] &
899 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (prev) {
901 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
902 prev->host_scribble = tmp->host_scribble;
903 } else {
904 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +1100905 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 tmp->host_scribble = NULL;
908
909 /*
910 * Attempt to establish an I_T_L nexus here.
911 * On success, instance->hostdata->connected is set.
912 * On failure, we must add the command back to the
913 * issue queue so we can keep trying.
914 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200915 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 -0700916
Finn Thain76f13b92014-11-12 16:11:53 +1100917 /*
918 * REQUEST SENSE commands are issued without tagged
919 * queueing, even on SCSI-II devices because the
920 * contingent allegiance condition exists for the
921 * entire unit.
922 */
923
924 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100925 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100927 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 LIST(tmp, hostdata->issue_queue);
929 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
930 hostdata->issue_queue = tmp;
931 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100932 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 -0700933 }
Finn Thainae753a32016-01-03 16:05:23 +1100934 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /* lock held here still */
937 } /* if target/lun is not busy */
938 } /* for */
939 /* exited locked */
940 } /* if (!hostdata->connected) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (hostdata->connected
942#ifdef REAL_DMA
943 && !hostdata->dmalen
944#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100946 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100948 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 } while (!done);
952
953 spin_unlock_irq(instance->host_lock);
954}
955
956#ifndef DONT_USE_INTR
957
958/**
Finn Thaincd400822016-01-03 16:05:40 +1100959 * NCR5380_intr - generic NCR5380 irq handler
960 * @irq: interrupt number
961 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 *
Finn Thaincd400822016-01-03 16:05:40 +1100963 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
964 * from the disconnected queue, and restarting NCR5380_main()
965 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
Finn Thaincd400822016-01-03 16:05:40 +1100967 * The chip can assert IRQ in any of six different conditions. The IRQ flag
968 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
969 * Three of these six conditions are latched in the Bus and Status Register:
970 * - End of DMA (cleared by ending DMA Mode)
971 * - Parity error (cleared by reading RPIR)
972 * - Loss of BSY (cleared by reading RPIR)
973 * Two conditions have flag bits that are not latched:
974 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
975 * - Bus reset (non-maskable)
976 * The remaining condition has no flag bit at all:
977 * - Selection/reselection
978 *
979 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
980 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
981 * claimed that "the design of the [DP8490] interrupt logic ensures
982 * interrupts will not be lost (they can be on the DP5380)."
983 * The L5380/53C80 datasheet from LOGIC Devices has more details.
984 *
985 * Checking for bus reset by reading RST is futile because of interrupt
986 * latency, but a bus reset will reset chip logic. Checking for parity error
987 * is unnecessary because that interrupt is never enabled. A Loss of BSY
988 * condition will clear DMA Mode. We can tell when this occurs because the
989 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
991
Finn Thaincd400822016-01-03 16:05:40 +1100992static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800994 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100995 struct NCR5380_hostdata *hostdata = shost_priv(instance);
996 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unsigned char basr;
998 unsigned long flags;
999
Finn Thaincd400822016-01-03 16:05:40 +11001000 spin_lock_irqsave(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Finn Thaincd400822016-01-03 16:05:40 +11001002 basr = NCR5380_read(BUS_AND_STATUS_REG);
1003 if (basr & BASR_IRQ) {
1004 unsigned char mr = NCR5380_read(MODE_REG);
1005 unsigned char sr = NCR5380_read(STATUS_REG);
1006
1007 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1008 instance->host_no, irq, basr, sr, mr);
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001011 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1012 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1013 * We ack IRQ after clearing Mode Register. Workarounds
1014 * for End of DMA errata need to happen in DMA Mode.
1015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Finn Thaincd400822016-01-03 16:05:40 +11001017 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Finn Thaincd400822016-01-03 16:05:40 +11001019 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Finn Thaincd400822016-01-03 16:05:40 +11001021 if (!hostdata->connected)
1022 panic("scsi%d : DMA interrupt with no connected cmd\n",
1023 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Finn Thaincd400822016-01-03 16:05:40 +11001025 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
1026 hostdata->connected->SCp.this_residual -= transferred;
1027 hostdata->connected->SCp.ptr += transferred;
1028 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Finn Thaincd400822016-01-03 16:05:40 +11001030 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1031 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
1032
1033 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1034 NCR5380_write(MODE_REG, MR_BASE);
1035 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1036 } else
1037#endif /* REAL_DMA */
1038 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1039 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1040 /* Probably reselected */
1041 NCR5380_write(SELECT_ENABLE_REG, 0);
1042 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1043
1044 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1045 instance->host_no);
1046
1047 if (!hostdata->connected) {
1048 NCR5380_reselect(instance);
1049 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Finn Thaincd400822016-01-03 16:05:40 +11001051 if (!hostdata->connected)
1052 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1053 } else {
1054 /* Probably Bus Reset */
1055 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1056
1057 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
1058 }
1059 handled = 1;
1060 } else {
1061 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
1062 }
1063
1064 spin_unlock_irqrestore(instance->host_lock, flags);
1065
1066 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069#endif
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
Finn Thain710ddd02014-11-12 16:12:02 +11001072 * Function : int NCR5380_select(struct Scsi_Host *instance,
1073 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
1075 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1076 * including ARBITRATION, SELECTION, and initial message out for
1077 * IDENTIFY and queue messages.
1078 *
1079 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001080 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 *
Finn Thain63238762016-01-03 16:05:15 +11001082 * Returns : -1 if selection failed but should be retried.
1083 * 0 if selection failed and should not be retried.
1084 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
1086 * Side effects :
1087 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1088 * with registers as they should have been on entry - ie
1089 * SELECT_ENABLE will be set appropriately, the NCR5380
1090 * will cease to drive any SCSI bus signals.
1091 *
1092 * If successful : I_T_L or I_T_L_Q nexus will be established,
1093 * instance->connected will be set to cmd.
1094 * SELECT interrupt will be disabled.
1095 *
1096 * If failed (no target) : cmd->scsi_done() will be called, and the
1097 * cmd->result host byte set to DID_BAD_TARGET.
1098 *
1099 * Locks: caller holds hostdata lock in IRQ mode
1100 */
1101
Finn Thain710ddd02014-11-12 16:12:02 +11001102static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1105 unsigned char tmp[3], phase;
1106 unsigned char *data;
1107 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001111 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /*
1114 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1115 * data bus during SELECTION.
1116 */
1117
1118 NCR5380_write(TARGET_COMMAND_REG, 0);
1119
1120 /*
1121 * Start arbitration.
1122 */
1123
1124 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1125 NCR5380_write(MODE_REG, MR_ARBITRATE);
1126
Finn Thain55500d92016-01-03 16:05:35 +11001127 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1128 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
1130
Finn Thain55500d92016-01-03 16:05:35 +11001131 spin_unlock_irq(instance->host_lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001132 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1133 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1134 ICR_ARBITRATION_PROGRESS, HZ);
1135 spin_lock_irq(instance->host_lock);
1136 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1137 /* Reselection interrupt */
1138 return -1;
1139 }
1140 if (err < 0) {
1141 NCR5380_write(MODE_REG, MR_BASE);
1142 shost_printk(KERN_ERR, instance,
1143 "select: arbitration timeout\n");
1144 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001145 }
1146
1147 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 udelay(3);
1149
1150 /* Check for lost arbitration */
1151 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)) {
1152 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001153 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001154 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Finn Thaincf13b082016-01-03 16:05:18 +11001156
1157 /* After/during arbitration, BSY should be asserted.
1158 * IBM DPES-31080 Version S31Q works now
1159 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1160 */
1161 NCR5380_write(INITIATOR_COMMAND_REG,
1162 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (!(hostdata->flags & FLAG_DTC3181E) &&
1165 /* RvC: DTC3181E has some trouble with this
1166 * so we simply removed it. Seems to work with
1167 * only Mustek scanner attached
1168 */
1169 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1170 NCR5380_write(MODE_REG, MR_BASE);
1171 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001172 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001173 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 /*
1176 * Again, bus clear + bus settle time is 1.2us, however, this is
1177 * a minimum so we'll udelay ceil(1.2)
1178 */
1179
Finn Thain9c3f0e22016-01-03 16:05:11 +11001180 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1181 udelay(15);
1182 else
1183 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Finn Thain72064a72016-01-03 16:05:44 +11001185 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1186 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1187 return -1;
1188
Finn Thain52a6a1c2014-03-18 11:42:18 +11001189 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /*
1192 * Now that we have won arbitration, start Selection process, asserting
1193 * the host and target ID's on the SCSI bus.
1194 */
1195
Jeff Garzik422c0d62005-10-24 18:05:09 -04001196 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 /*
1199 * Raise ATN while SEL is true before BSY goes false from arbitration,
1200 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1201 * phase immediately after selection.
1202 */
1203
1204 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1205 NCR5380_write(MODE_REG, MR_BASE);
1206
1207 /*
1208 * Reselect interrupts must be turned off prior to the dropping of BSY,
1209 * otherwise we will trigger an interrupt.
1210 */
1211 NCR5380_write(SELECT_ENABLE_REG, 0);
1212
1213 /*
1214 * The initiator shall then wait at least two deskew delays and release
1215 * the BSY signal.
1216 */
1217 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1218
1219 /* Reset BSY */
1220 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1221
1222 /*
1223 * Something weird happens when we cease to drive BSY - looks
1224 * like the board/chip is letting us do another read before the
1225 * appropriate propagation delay has expired, and we're confusing
1226 * a BSY signal from ourselves as the target's response to SELECTION.
1227 *
1228 * A small delay (the 'C++' frontend breaks the pipeline with an
1229 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1230 * tighter 'C' code breaks and requires this) solves the problem -
1231 * the 1 us delay is arbitrary, and only used because this delay will
1232 * be the same on other platforms and since it works here, it should
1233 * work there.
1234 *
1235 * wingel suggests that this could be due to failing to wait
1236 * one deskew delay.
1237 */
1238
1239 udelay(1);
1240
Finn Thain52a6a1c2014-03-18 11:42:18 +11001241 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /*
1244 * The SCSI specification calls for a 250 ms timeout for the actual
1245 * selection.
1246 */
1247
Finn Thainae753a32016-01-03 16:05:23 +11001248 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1249 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1252 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1253 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001254 if (!hostdata->connected)
1255 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -1;
1258 }
Finn Thainae753a32016-01-03 16:05:23 +11001259
1260 if (err < 0) {
1261 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1262 cmd->result = DID_BAD_TARGET << 16;
1263 cmd->scsi_done(cmd);
1264 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1265 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1266 instance->host_no);
1267 return 0;
1268 }
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * No less than two deskew delays after the initiator detects the
1272 * BSY signal is true, it shall release the SEL signal and may
1273 * change the DATA BUS. -wingel
1274 */
1275
1276 udelay(1);
1277
1278 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * Since we followed the SCSI spec, and raised ATN while SEL
1282 * was true but before BSY was false during selection, the information
1283 * transfer phase should be a MESSAGE OUT phase so that we can send the
1284 * IDENTIFY message.
1285 *
1286 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1287 * message (2 bytes) with a tag ID that we increment with every command
1288 * until it wraps back to 0.
1289 *
1290 * XXX - it turns out that there are some broken SCSI-II devices,
1291 * which claim to support tagged queuing but fail when more than
1292 * some number of commands are issued at once.
1293 */
1294
1295 /* Wait for start of REQ/ACK handshake */
1296
1297 spin_unlock_irq(instance->host_lock);
1298 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1299 spin_lock_irq(instance->host_lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001300 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001301 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1302 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001304 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Finn Thain52a6a1c2014-03-18 11:42:18 +11001307 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 +11001308 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 len = 1;
1311 cmd->tag = 0;
1312
1313 /* Send message(s) */
1314 data = tmp;
1315 phase = PHASE_MSGOUT;
1316 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001317 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* XXX need to handle errors here */
1319 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001320 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Boaz Harrosh28424d32007-09-10 22:37:45 +03001322 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327/*
1328 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1329 * unsigned char *phase, int *count, unsigned char **data)
1330 *
1331 * Purpose : transfers data in given phase using polled I/O
1332 *
1333 * Inputs : instance - instance of driver, *phase - pointer to
1334 * what phase is expected, *count - pointer to number of
1335 * bytes to transfer, **data - pointer to data pointer.
1336 *
1337 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001338 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * is in same phase.
1340 *
1341 * Also, *phase, *count, *data are modified in place.
1342 *
1343 * XXX Note : handling for bus free may be useful.
1344 */
1345
1346/*
1347 * Note : this code is not as quick as it could be, however it
1348 * IS 100% reliable, and for the actual data transfer where speed
1349 * counts, we will always do a pseudo DMA or DMA transfer.
1350 */
1351
1352static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 unsigned char p = *phase, tmp;
1354 int c = *count;
1355 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001358 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001360 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /*
1363 * The NCR5380 chip will only drive the SCSI bus when the
1364 * phase specified in the appropriate bits of the TARGET COMMAND
1365 * REGISTER match the STATUS REGISTER
1366 */
1367
1368 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 do {
1371 /*
1372 * Wait for assertion of REQ, after which the phase bits will be
1373 * valid
1374 */
1375
Finn Thain686f3992016-01-03 16:05:26 +11001376 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Finn Thain52a6a1c2014-03-18 11:42:18 +11001379 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001382 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001383 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1385 break;
1386 }
1387 /* Do actual transfer from SCSI bus to / from memory */
1388 if (!(p & SR_IO))
1389 NCR5380_write(OUTPUT_DATA_REG, *d);
1390 else
1391 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1392
1393 ++d;
1394
1395 /*
1396 * The SCSI standard suggests that in MSGOUT phase, the initiator
1397 * should drop ATN on the last byte of the message phase
1398 * after REQ has been asserted for the handshake but before
1399 * the initiator raises ACK.
1400 */
1401
1402 if (!(p & SR_IO)) {
1403 if (!((p & SR_MSG) && c > 1)) {
1404 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1405 NCR5380_dprint(NDEBUG_PIO, instance);
1406 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1407 } else {
1408 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1409 NCR5380_dprint(NDEBUG_PIO, instance);
1410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1411 }
1412 } else {
1413 NCR5380_dprint(NDEBUG_PIO, instance);
1414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1415 }
1416
Finn Thaina2edc4a2016-01-03 16:05:27 +11001417 if (NCR5380_poll_politely(instance,
1418 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1419 break;
1420
Finn Thain52a6a1c2014-03-18 11:42:18 +11001421 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423/*
1424 * We have several special cases to consider during REQ/ACK handshaking :
1425 * 1. We were in MSGOUT phase, and we are on the last byte of the
1426 * message. ATN must be dropped as ACK is dropped.
1427 *
1428 * 2. We are in a MSGIN phase, and we are on the last byte of the
1429 * message. We must exit with ACK asserted, so that the calling
1430 * code may raise ATN before dropping ACK to reject the message.
1431 *
1432 * 3. ACK and ATN are clear and the target may proceed as normal.
1433 */
1434 if (!(p == PHASE_MSGIN && c == 1)) {
1435 if (p == PHASE_MSGOUT && c > 1)
1436 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1437 else
1438 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1439 }
1440 } while (--c);
1441
Finn Thain52a6a1c2014-03-18 11:42:18 +11001442 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 *count = c;
1445 *data = d;
1446 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001447 /* The phase read from the bus is valid if either REQ is (already)
1448 * asserted or if ACK hasn't been released yet. The latter applies if
1449 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1450 */
1451 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *phase = tmp & PHASE_MASK;
1453 else
1454 *phase = PHASE_UNKNOWN;
1455
1456 if (!c || (*phase == p))
1457 return 0;
1458 else
1459 return -1;
1460}
1461
1462/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001463 * do_reset - issue a reset command
1464 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001466 * Issue a reset sequence to the NCR5380 and try and get the bus
1467 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001469 * This clears the reset interrupt flag because there may be no handler for
1470 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1471 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 */
1473
Finn Thain54d8fe42016-01-03 16:05:06 +11001474static void do_reset(struct Scsi_Host *instance)
1475{
Finn Thain636b1ec2016-01-03 16:05:10 +11001476 unsigned long flags;
1477
1478 local_irq_save(flags);
1479 NCR5380_write(TARGET_COMMAND_REG,
1480 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001482 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001484 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1485 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Finn Thain80d3eb62016-01-03 16:05:34 +11001488/**
1489 * do_abort - abort the currently established nexus by going to
1490 * MESSAGE OUT phase and sending an ABORT message.
1491 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001493 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
1495
Finn Thain54d8fe42016-01-03 16:05:06 +11001496static int do_abort(struct Scsi_Host *instance)
1497{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 unsigned char *msgptr, phase, tmp;
1499 int len;
1500 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 /* Request message out phase */
1503 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1504
1505 /*
1506 * Wait for the target to indicate a valid phase by asserting
1507 * REQ. Once this happens, we'll have either a MSGOUT phase
1508 * and can immediately send the ABORT message, or we'll have some
1509 * other phase and will have to source/sink data.
1510 *
1511 * We really don't care what value was on the bus or what value
1512 * the target sees, so we just handshake.
1513 */
1514
Finn Thain80d3eb62016-01-03 16:05:34 +11001515 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001516 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001517 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Finn Thainf35d3472016-01-03 16:05:33 +11001519 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1522
Finn Thainf35d3472016-01-03 16:05:33 +11001523 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001525 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001526 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001527 goto timeout;
1528 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 tmp = ABORT;
1531 msgptr = &tmp;
1532 len = 1;
1533 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001534 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /*
1537 * If we got here, and the command completed successfully,
1538 * we're about to go into bus free state.
1539 */
1540
1541 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001542
1543timeout:
1544 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1545 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1549/*
1550 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1551 * unsigned char *phase, int *count, unsigned char **data)
1552 *
1553 * Purpose : transfers data in given phase using either real
1554 * or pseudo DMA.
1555 *
1556 * Inputs : instance - instance of driver, *phase - pointer to
1557 * what phase is expected, *count - pointer to number of
1558 * bytes to transfer, **data - pointer to data pointer.
1559 *
1560 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001561 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 * is in same phase.
1563 *
1564 * Also, *phase, *count, *data are modified in place.
1565 *
1566 * Locks: io_request lock held by caller
1567 */
1568
1569
1570static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 register int c = *count;
1572 register unsigned char p = *phase;
1573 register unsigned char *d = *data;
1574 unsigned char tmp;
1575 int foo;
1576#if defined(REAL_DMA_POLL)
1577 int cnt, toPIO;
1578 unsigned char saved_data = 0, overrun = 0, residue;
1579#endif
1580
1581 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1584 *phase = tmp;
1585 return -1;
1586 }
1587#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001589 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
1590 c -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001592 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 -07001593 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1594#endif
1595
1596 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1597
1598#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001599 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1600 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001602 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603#else
1604 /*
1605 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1606 * were not disabled for the duration of a single DMA transfer, from
1607 * before the setting of DMA mode to after transfer of the last byte.
1608 */
1609
Finn Thain55181be2016-01-03 16:05:42 +11001610 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001611 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1612 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 else
Finn Thaincd400822016-01-03 16:05:40 +11001614 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615#endif /* def REAL_DMA */
1616
Finn Thain52a6a1c2014-03-18 11:42:18 +11001617 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 /*
1620 * On the PAS16 at least I/O recovery delays are not needed here.
1621 * Everyone else seems to want them.
1622 */
1623
1624 if (p & SR_IO) {
1625 io_recovery_delay(1);
1626 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1627 } else {
1628 io_recovery_delay(1);
1629 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1630 io_recovery_delay(1);
1631 NCR5380_write(START_DMA_SEND_REG, 0);
1632 io_recovery_delay(1);
1633 }
1634
1635#if defined(REAL_DMA_POLL)
1636 do {
1637 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1638 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1639
1640/*
1641 At this point, either we've completed DMA, or we have a phase mismatch,
1642 or we've unexpectedly lost BUSY (which is a real error).
1643
1644 For write DMAs, we want to wait until the last byte has been
1645 transferred out over the bus before we turn off DMA mode. Alas, there
1646 seems to be no terribly good way of doing this on a 5380 under all
1647 conditions. For non-scatter-gather operations, we can wait until REQ
1648 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1649 are nastier, since the device will be expecting more data than we
1650 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1651 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1652 why this signal was added to the newer chips) but on the older 538[01]
1653 this signal does not exist. The workaround for this lack is a watchdog;
1654 we bail out of the wait-loop after a modest amount of wait-time if
1655 the usual exit conditions are not met. Not a terribly clean or
1656 correct solution :-%
1657
1658 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1659 If the chip is in DMA mode for an READ, it will respond to a target's
1660 REQ by latching the SCSI data into the INPUT DATA register and asserting
1661 ACK, even if it has _already_ been notified by the DMA controller that
1662 the current DMA transfer has completed! If the NCR5380 is then taken
1663 out of DMA mode, this already-acknowledged byte is lost.
1664
1665 This is not a problem for "one DMA transfer per command" reads, because
1666 the situation will never arise... either all of the data is DMA'ed
1667 properly, or the target switches to MESSAGE IN phase to signal a
1668 disconnection (either operation bringing the DMA to a clean halt).
1669 However, in order to handle scatter-reads, we must work around the
1670 problem. The chosen fix is to DMA N-2 bytes, then check for the
1671 condition before taking the NCR5380 out of DMA mode. One or two extra
1672 bytes are transferred via PIO as necessary to fill out the original
1673 request.
1674 */
1675
1676 if (p & SR_IO) {
Finn Thain9db60242016-01-03 16:05:43 +11001677 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
1678 udelay(10);
1679 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1680 (BASR_PHASE_MATCH | BASR_ACK)) {
1681 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1682 overrun = 1;
1683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 } else {
1686 int limit = 100;
1687 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1688 if (!(tmp & BASR_PHASE_MATCH))
1689 break;
1690 if (--limit < 0)
1691 break;
1692 }
1693 }
1694
Finn Thain52a6a1c2014-03-18 11:42:18 +11001695 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 -07001696
1697 NCR5380_write(MODE_REG, MR_BASE);
1698 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1699
1700 residue = NCR5380_dma_residual(instance);
1701 c -= residue;
1702 *count -= c;
1703 *data += c;
1704 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1705
Finn Thain9db60242016-01-03 16:05:43 +11001706 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
1707 *phase == p && (p & SR_IO) && residue == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001709 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 **data = saved_data;
1711 *data += 1;
1712 *count -= 1;
1713 cnt = toPIO = 1;
1714 } else {
1715 printk("No overrun??\n");
1716 cnt = toPIO = 2;
1717 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001718 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 NCR5380_transfer_pio(instance, phase, &cnt, data);
1720 *count -= toPIO - cnt;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Finn Thain52a6a1c2014-03-18 11:42:18 +11001723 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 -07001724 return 0;
1725
1726#elif defined(REAL_DMA)
1727 return 0;
1728#else /* defined(REAL_DMA_POLL) */
1729 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001730 foo = NCR5380_pread(instance, d,
1731 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1732 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /*
1734 * We can't disable DMA mode after successfully transferring
1735 * what we plan to be the last byte, since that would open up
1736 * a race condition where if the target asserted REQ before
1737 * we got the DMA mode reset, the NCR5380 would have latched
1738 * an additional byte into the INPUT DATA register and we'd
1739 * have dropped it.
1740 *
1741 * The workaround was to transfer one fewer bytes than we
1742 * intended to with the pseudo-DMA read function, wait for
1743 * the chip to latch the last byte, read it, and then disable
1744 * pseudo-DMA mode.
1745 *
1746 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1747 * REQ is deasserted when ACK is asserted, and not reasserted
1748 * until ACK goes false. Since the NCR5380 won't lower ACK
1749 * until DACK is asserted, which won't happen unless we twiddle
1750 * the DMA port or we take the NCR5380 out of DMA mode, we
1751 * can guarantee that we won't handshake another extra
1752 * byte.
1753 */
1754
Finn Thain55181be2016-01-03 16:05:42 +11001755 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1756 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1757 foo = -1;
1758 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Finn Thain55181be2016-01-03 16:05:42 +11001760 if (NCR5380_poll_politely(instance, STATUS_REG,
1761 SR_REQ, 0, HZ) < 0) {
1762 foo = -1;
1763 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1764 }
1765 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001769 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * Wait for the last byte to be sent. If REQ is being asserted for
1772 * the byte we're interested, we'll ACK it and it will go false.
1773 */
Finn Thain55181be2016-01-03 16:05:42 +11001774 if (NCR5380_poll_politely2(instance,
1775 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1776 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1777 foo = -1;
1778 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782 NCR5380_write(MODE_REG, MR_BASE);
1783 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001784 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 *data = d + c;
1786 *count = 0;
1787 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return foo;
1789#endif /* def REAL_DMA */
1790}
1791#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1792
1793/*
1794 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1795 *
1796 * Purpose : run through the various SCSI phases and do as the target
1797 * directs us to. Operates on the currently connected command,
1798 * instance->connected.
1799 *
1800 * Inputs : instance, instance for which we are doing commands
1801 *
1802 * Side effects : SCSI things happen, the disconnected queue will be
1803 * modified if a command disconnects, *instance->connected will
1804 * change.
1805 *
1806 * XXX Note : we need to watch for bus free or a reset condition here
1807 * to recover from an unexpected bus free condition.
1808 *
1809 * Locks: io_request_lock held by caller in IRQ mode
1810 */
1811
1812static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1814 unsigned char msgout = NOP;
1815 int sink = 0;
1816 int len;
1817#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1818 int transfersize;
1819#endif
1820 unsigned char *data;
1821 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001822 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 while (1) {
1825 tmp = NCR5380_read(STATUS_REG);
1826 /* We only have a valid SCSI phase when REQ is asserted */
1827 if (tmp & SR_REQ) {
1828 phase = (tmp & PHASE_MASK);
1829 if (phase != old_phase) {
1830 old_phase = phase;
1831 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1832 }
1833 if (sink && (phase != PHASE_MSGOUT)) {
1834 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1835
1836 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1837 while (NCR5380_read(STATUS_REG) & SR_REQ);
1838 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1839 sink = 0;
1840 continue;
1841 }
1842 switch (phase) {
1843 case PHASE_DATAIN:
1844 case PHASE_DATAOUT:
1845#if (NDEBUG & NDEBUG_NO_DATAOUT)
1846 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1847 sink = 1;
1848 do_abort(instance);
1849 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001850 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return;
1852#endif
1853 /*
1854 * If there is no room left in the current buffer in the
1855 * scatter-gather list, move onto the next one.
1856 */
1857
1858 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1859 ++cmd->SCp.buffer;
1860 --cmd->SCp.buffers_residual;
1861 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001862 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001863 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 -07001864 }
1865 /*
1866 * The preferred transfer method is going to be
1867 * PSEUDO-DMA for systems that are strictly PIO,
1868 * since we can let the hardware do the handshaking.
1869 *
1870 * For this to work, we need to know the transfersize
1871 * ahead of time, since the pseudo-DMA code will sit
1872 * in an unconditional loop.
1873 */
1874
1875#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001876 transfersize = 0;
1877 if (!cmd->device->borken &&
1878 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1879 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Finn Thainff3d4572016-01-03 16:05:25 +11001881 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 len = transfersize;
1883 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1884 /*
1885 * If the watchdog timer fires, all future accesses to this
1886 * device will use the polled-IO.
1887 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001888 scmd_printk(KERN_INFO, cmd,
1889 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 sink = 1;
1892 do_abort(instance);
1893 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001894 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* XXX - need to source or sink data here, as appropriate */
1896 } else
1897 cmd->SCp.this_residual -= transfersize - len;
1898 } else
1899#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1900 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1901 &cmd->SCp.ptr);
1902 break;
1903 case PHASE_MSGIN:
1904 len = 1;
1905 data = &tmp;
1906 NCR5380_transfer_pio(instance, &phase, &len, &data);
1907 cmd->SCp.Message = tmp;
1908
1909 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 case ABORT:
1911 case COMMAND_COMPLETE:
1912 /* Accept message by clearing ACK */
1913 sink = 1;
1914 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1915 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001916 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1917 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /*
1920 * I'm not sure what the correct thing to do here is :
1921 *
1922 * If the command that just executed is NOT a request
1923 * sense, the obvious thing to do is to set the result
1924 * code to the values of the stored parameters.
1925 *
1926 * If it was a REQUEST SENSE command, we need some way
1927 * to differentiate between the failure code of the original
1928 * and the failure code of the REQUEST sense - the obvious
1929 * case is success, where we fall through and leave the result
1930 * code unchanged.
1931 *
1932 * The non-obvious place is where the REQUEST SENSE failed
1933 */
1934
1935 if (cmd->cmnd[0] != REQUEST_SENSE)
1936 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1937 else if (status_byte(cmd->SCp.Status) != GOOD)
1938 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1939
Boaz Harrosh28424d32007-09-10 22:37:45 +03001940 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1941 hostdata->ses.cmd_len) {
1942 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1943 hostdata->ses.cmd_len = 0 ;
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Boaz Harrosh28424d32007-09-10 22:37:45 +03001946 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1947 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1948
Finn Thain52a6a1c2014-03-18 11:42:18 +11001949 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 LIST(cmd, hostdata->issue_queue);
1952 cmd->host_scribble = (unsigned char *)
1953 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11001954 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001955 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11001956 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 cmd->scsi_done(cmd);
1958 }
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * Restore phase bits to 0 so an interrupted selection,
1962 * arbitration can resume.
1963 */
1964 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001965
1966 /* Enable reselect interrupts */
1967 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return;
1969 case MESSAGE_REJECT:
1970 /* Accept message by clearing ACK */
1971 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1972 switch (hostdata->last_message) {
1973 case HEAD_OF_QUEUE_TAG:
1974 case ORDERED_QUEUE_TAG:
1975 case SIMPLE_QUEUE_TAG:
1976 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001977 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
1979 default:
1980 break;
1981 }
Finn Thain340b9612016-01-03 16:05:31 +11001982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 case DISCONNECT:{
1984 /* Accept message by clearing ACK */
1985 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 LIST(cmd, hostdata->disconnected_queue);
1987 cmd->host_scribble = (unsigned char *)
1988 hostdata->disconnected_queue;
1989 hostdata->connected = NULL;
1990 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001991 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 -07001992 /*
1993 * Restore phase bits to 0 so an interrupted selection,
1994 * arbitration can resume.
1995 */
1996 NCR5380_write(TARGET_COMMAND_REG, 0);
1997
1998 /* Enable reselect interrupts */
1999 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return;
2001 }
2002 /*
2003 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2004 * operation, in violation of the SCSI spec so we can safely
2005 * ignore SAVE/RESTORE pointers calls.
2006 *
2007 * Unfortunately, some disks violate the SCSI spec and
2008 * don't issue the required SAVE_POINTERS message before
2009 * disconnecting, and we have to break spec to remain
2010 * compatible.
2011 */
2012 case SAVE_POINTERS:
2013 case RESTORE_POINTERS:
2014 /* Accept message by clearing ACK */
2015 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2016 break;
2017 case EXTENDED_MESSAGE:
2018/*
2019 * Extended messages are sent in the following format :
2020 * Byte
2021 * 0 EXTENDED_MESSAGE == 1
2022 * 1 length (includes one byte for code, doesn't
2023 * include first two bytes)
2024 * 2 code
2025 * 3..length+1 arguments
2026 *
2027 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002028 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 */
2030 extended_msg[0] = EXTENDED_MESSAGE;
2031 /* Accept first byte by clearing ACK */
2032 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002033 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 len = 2;
2036 data = extended_msg + 1;
2037 phase = PHASE_MSGIN;
2038 NCR5380_transfer_pio(instance, &phase, &len, &data);
2039
Finn Thain52a6a1c2014-03-18 11:42:18 +11002040 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 -07002041
Finn Thaine0783ed2016-01-03 16:05:45 +11002042 if (!len && extended_msg[1] > 0 &&
2043 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* Accept third byte by clearing ACK */
2045 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2046 len = extended_msg[1] - 1;
2047 data = extended_msg + 3;
2048 phase = PHASE_MSGIN;
2049
2050 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002051 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 switch (extended_msg[2]) {
2054 case EXTENDED_SDTR:
2055 case EXTENDED_WDTR:
2056 case EXTENDED_MODIFY_DATA_POINTER:
2057 case EXTENDED_EXTENDED_IDENTIFY:
2058 tmp = 0;
2059 }
2060 } else if (len) {
2061 printk("scsi%d: error receiving extended message\n", instance->host_no);
2062 tmp = 0;
2063 } else {
2064 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2065 tmp = 0;
2066 }
2067 /* Fall through to reject message */
2068
2069 /*
2070 * If we get something weird that we aren't expecting,
2071 * reject it.
2072 */
2073 default:
2074 if (!tmp) {
2075 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002076 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 printk("\n");
2078 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002079 scmd_printk(KERN_INFO, cmd,
2080 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002082 scmd_printk(KERN_INFO, cmd,
2083 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 msgout = MESSAGE_REJECT;
2086 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2087 break;
2088 } /* switch (tmp) */
2089 break;
2090 case PHASE_MSGOUT:
2091 len = 1;
2092 data = &msgout;
2093 hostdata->last_message = msgout;
2094 NCR5380_transfer_pio(instance, &phase, &len, &data);
2095 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002096 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 hostdata->connected = NULL;
2098 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 cmd->scsi_done(cmd);
2100 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2101 return;
2102 }
2103 msgout = NOP;
2104 break;
2105 case PHASE_CMDOUT:
2106 len = cmd->cmd_len;
2107 data = cmd->cmnd;
2108 /*
2109 * XXX for performance reasons, on machines with a
2110 * PSEUDO-DMA architecture we should probably
2111 * use the dma transfer function.
2112 */
2113 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 break;
2115 case PHASE_STATIN:
2116 len = 1;
2117 data = &tmp;
2118 NCR5380_transfer_pio(instance, &phase, &len, &data);
2119 cmd->SCp.Status = tmp;
2120 break;
2121 default:
2122 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002123 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002125 } else {
2126 spin_unlock_irq(instance->host_lock);
2127 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2128 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 } /* while (1) */
2131}
2132
2133/*
2134 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2135 *
2136 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002137 * 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 -07002138 * nexus has been reestablished,
2139 *
2140 * Inputs : instance - this instance of the NCR5380.
2141 *
2142 * Locks: io_request_lock held by caller if IRQ driven
2143 */
2144
2145static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2147 instance->hostdata;
2148 unsigned char target_mask;
2149 unsigned char lun, phase;
2150 int len;
2151 unsigned char msg[3];
2152 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002153 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /*
2156 * Disable arbitration, etc. since the host adapter obviously
2157 * lost, and tell an interrupted NCR5380_select() to restart.
2158 */
2159
2160 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain72064a72016-01-03 16:05:44 +11002163 dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 /*
2166 * At this point, we have detected that our SCSI ID is on the bus,
2167 * SEL is true and BSY was false for at least one bus settle delay
2168 * (400 ns).
2169 *
2170 * We must assert BSY ourselves, until the target drops the SEL
2171 * signal.
2172 */
2173
2174 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002175 if (NCR5380_poll_politely(instance,
2176 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2177 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2178 return;
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2181
2182 /*
2183 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
2185
Finn Thain1cc160e2016-01-03 16:05:32 +11002186 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002187 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2188 do_abort(instance);
2189 return;
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 len = 1;
2193 data = msg;
2194 phase = PHASE_MSGIN;
2195 NCR5380_transfer_pio(instance, &phase, &len, &data);
2196
Finn Thain72064a72016-01-03 16:05:44 +11002197 if (len) {
2198 do_abort(instance);
2199 return;
2200 }
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002203 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002204 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002205 printk("\n");
2206 do_abort(instance);
2207 return;
2208 }
2209 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Finn Thain72064a72016-01-03 16:05:44 +11002211 /*
2212 * We need to add code for SCSI-II to track which devices have
2213 * I_T_L_Q nexuses established, and which have simple I_T_L
2214 * nexuses so we can chose to do additional data transfer.
2215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Finn Thain72064a72016-01-03 16:05:44 +11002217 /*
2218 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2219 * just reestablished, and remove it from the disconnected queue.
2220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Finn Thain72064a72016-01-03 16:05:44 +11002222 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
2223 tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble) {
2224 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)) {
2225 if (prev) {
2226 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2227 prev->host_scribble = tmp->host_scribble;
2228 } else {
2229 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2230 hostdata->disconnected_queue =
2231 (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Finn Thain72064a72016-01-03 16:05:44 +11002233 tmp->host_scribble = NULL;
2234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 }
Finn Thain72064a72016-01-03 16:05:44 +11002237 if (!tmp) {
2238 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2239 target_mask, lun);
2240 /*
2241 * Since we have an established nexus that we can't do anything with,
2242 * we must abort it.
2243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002245 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Finn Thain72064a72016-01-03 16:05:44 +11002247
2248 /* Accept message by clearing ACK */
2249 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2250
2251 hostdata->connected = tmp;
2252 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n",
2253 instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
2256/*
2257 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2258 *
2259 * Purpose : called by interrupt handler when DMA finishes or a phase
2260 * mismatch occurs (which would finish the DMA transfer).
2261 *
2262 * Inputs : instance - this instance of the NCR5380.
2263 *
Finn Thain710ddd02014-11-12 16:12:02 +11002264 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * nexus has been reestablished, on failure NULL is returned.
2266 */
2267
2268#ifdef REAL_DMA
2269static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002270 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /*
2274 * XXX this might not be right.
2275 *
2276 * Wait for final byte to transfer, ie wait for ACK to go false.
2277 *
2278 * We should use the Last Byte Sent bit, unfortunately this is
2279 * not available on the 5380/5381 (only the various CMOS chips)
2280 *
2281 * FIXME: timeout, and need to handle long timeout/irq case
2282 */
2283
2284 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2287
2288 /*
2289 * The only places we should see a phase mismatch and have to send
2290 * data from the same set of pointers will be the data transfer
2291 * phases. So, residual, requested length are only important here.
2292 */
2293
2294 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2295 transferred = instance->dmalen - NCR5380_dma_residual();
2296 hostdata->connected->SCp.this_residual -= transferred;
2297 hostdata->connected->SCp.ptr += transferred;
2298 }
2299}
2300#endif /* def REAL_DMA */
2301
2302/*
Finn Thain710ddd02014-11-12 16:12:02 +11002303 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
2305 * Purpose : abort a command
2306 *
Finn Thain710ddd02014-11-12 16:12:02 +11002307 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002308 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 * used.
2310 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002311 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002313 * XXX - there is no way to abort the command that is currently
2314 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * a problem, we could implement longjmp() / setjmp(), setjmp()
2316 * called where the loop started in NCR5380_main().
2317 *
2318 * Locks: host lock taken by caller
2319 */
2320
Finn Thain710ddd02014-11-12 16:12:02 +11002321static int NCR5380_abort(struct scsi_cmnd *cmd)
2322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 struct Scsi_Host *instance = cmd->device->host;
2324 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002325 struct scsi_cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002326
2327 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Finn Thain9dafbd92016-01-03 16:05:28 +11002329 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 NCR5380_print_status(instance);
2331
Finn Thain52a6a1c2014-03-18 11:42:18 +11002332 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2333 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 -07002334
2335#if 0
2336/*
2337 * Case 1 : If the command is the currently executing command,
2338 * we'll set the aborted flag and return control so that
2339 * information transfer routine can exit cleanly.
2340 */
2341
2342 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002343 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344/*
2345 * We should perform BSY checking, and make sure we haven't slipped
2346 * into BUS FREE.
2347 */
2348
2349 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2350/*
2351 * Since we can't change phases until we've completed the current
2352 * handshake, we have to source or sink a byte of data if the current
2353 * phase is not MSGOUT.
2354 */
2355
2356/*
2357 * Return control to the executing NCR drive so we can clear the
2358 * aborted flag and get back into our main loop.
2359 */
2360
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002361 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363#endif
2364
2365/*
2366 * Case 2 : If the command hasn't been issued yet, we simply remove it
2367 * from the issue queue.
2368 */
2369
Finn Thain52a6a1c2014-03-18 11:42:18 +11002370 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002371 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 -07002372 if (cmd == tmp) {
2373 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002374 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002376 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002378 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002379 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return SUCCESS;
2381 }
2382#if (NDEBUG & NDEBUG_ABORT)
2383 /* KLL */
2384 else if (prev == tmp)
2385 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2386#endif
2387
2388/*
2389 * Case 3 : If any commands are connected, we're going to fail the abort
2390 * and let the high level SCSI driver retry at a later time or
2391 * issue a reset.
2392 *
2393 * Timeouts, and therefore aborted commands, will be highly unlikely
2394 * and handling them cleanly in this situation would make the common
2395 * case of noresets less efficient, and would pollute our code. So,
2396 * we fail.
2397 */
2398
2399 if (hostdata->connected) {
Finn Thain9dafbd92016-01-03 16:05:28 +11002400 spin_unlock_irq(instance->host_lock);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002401 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return FAILED;
2403 }
2404/*
2405 * Case 4: If the command is currently disconnected from the bus, and
2406 * there are no connected commands, we reconnect the I_T_L or
2407 * I_T_L_Q nexus associated with it, go into message out, and send
2408 * an abort message.
2409 *
2410 * This case is especially ugly. In order to reestablish the nexus, we
2411 * need to call NCR5380_select(). The easiest way to implement this
2412 * function was to abort if the bus was busy, and let the interrupt
2413 * handler triggered on the SEL for reselect take care of lost arbitrations
2414 * where necessary, meaning interrupts need to be enabled.
2415 *
2416 * When interrupts are enabled, the queues may change - so we
2417 * can't remove it from the disconnected queue before selecting it
2418 * because that could cause a failure in hashing the nexus if that
2419 * device reselected.
2420 *
2421 * Since the queues may change, we can't use the pointers from when we
2422 * first locate it.
2423 *
2424 * So, we must first locate the command, and if NCR5380_select()
2425 * succeeds, then issue the abort, relocate the command and remove
2426 * it from the disconnected queue.
2427 */
2428
Finn Thain710ddd02014-11-12 16:12:02 +11002429 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002431 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Finn Thain9dafbd92016-01-03 16:05:28 +11002433 if (NCR5380_select(instance, cmd)) {
2434 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return FAILED;
Finn Thain9dafbd92016-01-03 16:05:28 +11002436 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11002437 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 do_abort(instance);
2440
Finn Thain710ddd02014-11-12 16:12:02 +11002441 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 -07002442 if (cmd == tmp) {
2443 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002444 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002446 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002448 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return SUCCESS;
2450 }
2451 }
2452/*
2453 * Case 5 : If we reached this point, the command was not found in any of
2454 * the queues.
2455 *
2456 * We probably reached this point because of an unlikely race condition
2457 * between the command completing successfully and the abortion code,
2458 * so we won't panic, but we will notify the user in case something really
2459 * broke.
2460 */
Finn Thain9dafbd92016-01-03 16:05:28 +11002461 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2463 " before abortion\n", instance->host_no);
2464 return FAILED;
2465}
2466
2467
Finn Thain3be1b3e2016-01-03 16:05:12 +11002468/**
2469 * NCR5380_bus_reset - reset the SCSI bus
2470 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002472 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 */
2474
Finn Thain710ddd02014-11-12 16:12:02 +11002475static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002476{
2477 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002479 spin_lock_irq(instance->host_lock);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002480
2481#if (NDEBUG & NDEBUG_ANY)
2482 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2483 NCR5380_print_status(instance);
2484#endif
2485
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002486 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002487
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002488 spin_unlock_irq(instance->host_lock);
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return SUCCESS;
2491}