blob: f69d36327a7d0884db4ce7db66cadf0d1eb52979 [file] [log] [blame]
Roman Zippelc28bda22007-05-01 22:32:36 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Roman Zippelc28bda22007-05-01 22:32:36 +02003 * to implement 5380 SCSI drivers under Linux with a non-trantor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
Roman Zippelc28bda22007-05-01 22:32:36 +02009 * Visionary Computing
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (Unix and Linux consulting and custom programming)
Roman Zippelc28bda22007-05-01 22:32:36 +020011 * drew@colorado.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * +1 (303) 666-5836
13 *
Roman Zippelc28bda22007-05-01 22:32:36 +020014 * DISTRIBUTION RELEASE 6.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Roman Zippelc28bda22007-05-01 22:32:36 +020016 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * NCR 5380 Family
19 * SCSI Protocol Controller
20 * Databook
21 *
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
25 * 1+ (719) 578-3400
26 * 1+ (800) 334-5454
27 */
28
29/*
30 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
31 * this file, too:
32 *
33 * - Some of the debug statements were incorrect (undefined variables and the
34 * like). I fixed that.
35 *
36 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
37 * possible DMA transfer size should also happen for REAL_DMA. I added this
38 * in the #if statement.
39 *
40 * - When using real DMA, information_transfer() should return in a DATAOUT
41 * phase after starting the DMA. It has nothing more to do.
42 *
43 * - The interrupt service routine should run main after end of DMA, too (not
44 * only after RESELECTION interrupts). Additionally, it should _not_ test
45 * for more interrupts after running main, since a DMA process may have
46 * been started and interrupts are turned on now. The new int could happen
47 * inside the execution of NCR5380_intr(), leading to recursive
48 * calls.
49 *
50 * - I've added a function merge_contiguous_buffers() that tries to
51 * merge scatter-gather buffers that are located at contiguous
52 * physical addresses and can be processed with the same DMA setup.
53 * Since most scatter-gather operations work on a page (4K) of
54 * 4 buffers (1K), in more than 90% of all cases three interrupts and
55 * DMA setup actions are saved.
56 *
57 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
58 * and USLEEP, because these were messing up readability and will never be
59 * needed for Atari SCSI.
Roman Zippelc28bda22007-05-01 22:32:36 +020060 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
62 * stuff), and 'main' is executed in a bottom half if awoken by an
63 * interrupt.
64 *
65 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
66 * constructs. In my eyes, this made the source rather unreadable, so I
67 * finally replaced that by the *_PRINTK() macros.
68 *
69 */
70
71/*
Roman Zippelc28bda22007-05-01 22:32:36 +020072 * Further development / testing that should be done :
73 * 1. Test linked command handling code after Eric is ready with
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * the high level code.
75 */
db9dff32005-04-03 14:53:59 -050076#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050077#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +020080#define LIST(x, y) \
81 do { \
82 printk("LINE:%d Adding %p to %p\n", \
83 __LINE__, (void*)(x), (void*)(y)); \
84 if ((x) == (y)) \
85 udelay(5); \
86 } while (0)
87#define REMOVE(w, x, y, z) \
88 do { \
89 printk("LINE:%d Removing: %p->%p %p->%p \n", \
90 __LINE__, (void*)(w), (void*)(x), \
91 (void*)(y), (void*)(z)); \
92 if ((x) == (y)) \
93 udelay(5); \
94 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#else
96#define LIST(x,y)
97#define REMOVE(w,x,y,z)
98#endif
99
100#ifndef notyet
101#undef LINKED
102#endif
103
104/*
105 * Design
106 * Issues :
107 *
108 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
109 * and specifically for each board rather than each chip. This makes their
110 * adaptation to platforms like the Mac (Some of which use NCR5380's)
111 * more difficult than it has to be.
112 *
113 * Also, many of the SCSI drivers were written before the command queuing
Roman Zippelc28bda22007-05-01 22:32:36 +0200114 * routines were implemented, meaning their implementations of queued
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * commands were hacked on rather than designed in from the start.
116 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200117 * When I designed the Linux SCSI drivers I figured that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * while having two different SCSI boards in a system might be useful
119 * for debugging things, two of the same type wouldn't be used.
120 * Well, I was wrong and a number of users have mailed me about running
121 * multiple high-performance SCSI boards in a server.
122 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200123 * Finally, when I get questions from users, I have no idea what
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * revision of my driver they are running.
125 *
126 * This driver attempts to address these problems :
Roman Zippelc28bda22007-05-01 22:32:36 +0200127 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200129 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * memory mapped) and drops this file in their 'C' wrapper.
131 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200132 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200134 * and commands that are currently executing. This means that an
135 * unlimited number of commands may be queued, letting
136 * more commands propagate from the higher driver levels giving higher
137 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
138 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * while a command is already executing.
140 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200141 * To solve the multiple-boards-in-the-same-system problem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * there is a separate instance structure for each instance
143 * of a 5380 in the system. So, multiple NCR5380 drivers will
144 * be able to coexist with appropriate changes to the high level
Roman Zippelc28bda22007-05-01 22:32:36 +0200145 * SCSI code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
147 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
Roman Zippelc28bda22007-05-01 22:32:36 +0200148 * number (updated for each public release) printed by the
149 * NCR5380_print_options command, which should be called from the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * wrapper detect function, so that I know what release of the driver
151 * users are using.
152 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200153 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
156 * piece of hardware that requires you to sit in a loop polling for
157 * the REQ signal as long as you are connected. Some devices are
158 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * while doing long seek operations.
Roman Zippelc28bda22007-05-01 22:32:36 +0200160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * The workaround for this is to keep track of devices that have
162 * disconnected. If the device hasn't disconnected, for commands that
Roman Zippelc28bda22007-05-01 22:32:36 +0200163 * should disconnect, we do something like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
Roman Zippelc28bda22007-05-01 22:32:36 +0200166 *
167 * Some tweaking of N and M needs to be done. An algorithm based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * on "time to data" would give the best results as long as short time
Roman Zippelc28bda22007-05-01 22:32:36 +0200169 * to datas (ie, on the same track) were considered, however these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * broken devices are the exception rather than the rule and I'd rather
171 * spend my time optimizing for the normal case.
172 *
173 * Architecture :
174 *
175 * At the heart of the design is a coroutine, NCR5380_main,
176 * which is started when not running by the interrupt handler,
177 * timer, and queue command function. It attempts to establish
Roman Zippelc28bda22007-05-01 22:32:36 +0200178 * I_T_L or I_T_L_Q nexuses by removing the commands from the
179 * issue queue and calling NCR5380_select() if a nexus
180 * is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 *
182 * Once a nexus is established, the NCR5380_information_transfer()
183 * phase goes through the various phases as instructed by the target.
184 * if the target goes into MSG IN and sends a DISCONNECT message,
185 * the command structure is placed into the per instance disconnected
186 * queue, and NCR5380_main tries to find more work. If USLEEP
187 * was defined, and the target is idle for too long, the system
188 * will try to sleep.
189 *
190 * If a command has disconnected, eventually an interrupt will trigger,
191 * calling NCR5380_intr() which will in turn call NCR5380_reselect
192 * to reestablish a nexus. This will run main if necessary.
193 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200194 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * appropriate.
196 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200197 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * structures, being initialized after the command is connected
199 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
200 * Note that in violation of the standard, an implicit SAVE POINTERS operation
201 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
202 */
203
204/*
205 * Using this file :
206 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200207 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * and macros and include this file in your driver.
209 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200210 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200212 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
214 * LINKED - if defined, linked commands are supported.
215 *
216 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
217 *
218 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
219 *
220 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * NCR5380_read(register) - read from the specified register
223 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200224 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
226 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200227 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200229 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * that they were able to program the controller for.
231 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200232 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * available as NCR5380_i386_dma_write_setup,
234 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
235 *
236 * NCR5380_dma_write_setup(instance, src, count) - initialize
237 * NCR5380_dma_read_setup(instance, dst, count) - initialize
238 * NCR5380_dma_residual(instance); - residual count
239 *
240 * PSEUDO functions :
241 * NCR5380_pwrite(instance, src, count)
242 * NCR5380_pread(instance, dst, count);
243 *
244 * If nothing specific to this implementation needs doing (ie, with external
Roman Zippelc28bda22007-05-01 22:32:36 +0200245 * hardware), you must also define
246 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * NCR5380_queue_command
248 * NCR5380_reset
249 * NCR5380_abort
250 * NCR5380_proc_info
251 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200252 * to be the global entry points into the specific driver, ie
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * #define NCR5380_queue_command t128_queue_command.
254 *
255 * If this is not done, the routines will be defined as static functions
256 * with the NCR5380* names and the user must provide a globally
257 * accessible wrapper function.
258 *
259 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200260 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
262 * possible) function may be used. Before the specific driver initialization
263 * code finishes, NCR5380_print_options should be called.
264 */
265
266static struct Scsi_Host *first_instance = NULL;
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100267static struct scsi_host_template *the_template = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/* Macros ease life... :-) */
270#define SETUP_HOSTDATA(in) \
271 struct NCR5380_hostdata *hostdata = \
272 (struct NCR5380_hostdata *)(in)->hostdata
273#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
274
Roman Zippel3130d902007-05-01 22:32:37 +0200275#define NEXT(cmd) ((Scsi_Cmnd *)(cmd)->host_scribble)
276#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
277#define NEXTADDR(cmd) ((Scsi_Cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279#define HOSTNO instance->host_no
280#define H_NO(cmd) (cmd)->device->host->host_no
281
282#ifdef SUPPORT_TAGS
283
284/*
285 * Functions for handling tagged queuing
286 * =====================================
287 *
288 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
289 *
290 * Using consecutive numbers for the tags is no good idea in my eyes. There
291 * could be wrong re-usings if the counter (8 bit!) wraps and some early
292 * command has been preempted for a long time. My solution: a bitfield for
293 * remembering used tags.
294 *
295 * There's also the problem that each target has a certain queue size, but we
296 * cannot know it in advance :-( We just see a QUEUE_FULL status being
297 * returned. So, in this case, the driver internal queue size assumption is
298 * reduced to the number of active tags if QUEUE_FULL is returned by the
299 * target. The command is returned to the mid-level, but with status changed
300 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
301 * correctly.
302 *
303 * We're also not allowed running tagged commands as long as an untagged
304 * command is active. And REQUEST SENSE commands after a contingent allegiance
305 * condition _must_ be untagged. To keep track whether an untagged command has
306 * been issued, the host->busy array is still employed, as it is without
307 * support for tagged queuing.
308 *
309 * One could suspect that there are possible race conditions between
310 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
311 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
312 * which already guaranteed to be running at most once. It is also the only
313 * place where tags/LUNs are allocated. So no other allocation can slip
314 * between that pair, there could only happen a reselection, which can free a
315 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
316 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
317 */
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319typedef struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200320 DECLARE_BITMAP(allocated, MAX_TAGS);
321 int nr_allocated;
322 int queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323} TAG_ALLOC;
324
Roman Zippelc28bda22007-05-01 22:32:36 +0200325static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327
Roman Zippelc28bda22007-05-01 22:32:36 +0200328static void __init init_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Roman Zippelc28bda22007-05-01 22:32:36 +0200330 int target, lun;
331 TAG_ALLOC *ta;
332
333 if (!setup_use_tagged_queuing)
334 return;
335
336 for (target = 0; target < 8; ++target) {
337 for (lun = 0; lun < 8; ++lun) {
338 ta = &TagAlloc[target][lun];
339 bitmap_zero(ta->allocated, MAX_TAGS);
340 ta->nr_allocated = 0;
341 /* At the beginning, assume the maximum queue size we could
342 * support (MAX_TAGS). This value will be decreased if the target
343 * returns QUEUE_FULL status.
344 */
345 ta->queue_size = MAX_TAGS;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350
351/* Check if we can issue a command to this LUN: First see if the LUN is marked
352 * busy by an untagged command. If the command should use tagged queuing, also
353 * check that there is a free tag and the target's queue won't overflow. This
354 * function should be called with interrupts disabled to avoid race
355 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Roman Zippelc28bda22007-05-01 22:32:36 +0200358static int is_lun_busy(Scsi_Cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200360 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200361 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200363 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200364 return 1;
365 if (!should_be_tagged ||
366 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
367 return 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200368 if (TagAlloc[cmd->device->id][lun].nr_allocated >=
369 TagAlloc[cmd->device->id][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100370 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200371 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200372 return 1;
373 }
374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377
378/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
379 * must be called before!), or reserve the LUN in 'busy' if the command is
380 * untagged.
381 */
382
Roman Zippelc28bda22007-05-01 22:32:36 +0200383static void cmd_get_tag(Scsi_Cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200385 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200386 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Roman Zippelc28bda22007-05-01 22:32:36 +0200388 /* If we or the target don't support tagged queuing, allocate the LUN for
389 * an untagged command.
390 */
391 if (!should_be_tagged ||
392 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
393 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200394 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100395 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200396 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200397 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200398 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Roman Zippelc28bda22007-05-01 22:32:36 +0200400 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
401 set_bit(cmd->tag, ta->allocated);
402 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100403 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200404 "(now %d tags in use)\n",
405 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200406 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410
411/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
412 * unlock the LUN.
413 */
414
Roman Zippelc28bda22007-05-01 22:32:36 +0200415static void cmd_free_tag(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200417 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200418 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Roman Zippelc28bda22007-05-01 22:32:36 +0200420 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200421 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100422 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200423 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200424 } else if (cmd->tag >= MAX_TAGS) {
425 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
426 H_NO(cmd), cmd->tag);
427 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200428 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200429 clear_bit(cmd->tag, ta->allocated);
430 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100431 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200432 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436
Roman Zippelc28bda22007-05-01 22:32:36 +0200437static void free_all_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Roman Zippelc28bda22007-05-01 22:32:36 +0200439 int target, lun;
440 TAG_ALLOC *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Roman Zippelc28bda22007-05-01 22:32:36 +0200442 if (!setup_use_tagged_queuing)
443 return;
444
445 for (target = 0; target < 8; ++target) {
446 for (lun = 0; lun < 8; ++lun) {
447 ta = &TagAlloc[target][lun];
448 bitmap_zero(ta->allocated, MAX_TAGS);
449 ta->nr_allocated = 0;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454#endif /* SUPPORT_TAGS */
455
456
457/*
458 * Function: void merge_contiguous_buffers( Scsi_Cmnd *cmd )
459 *
460 * Purpose: Try to merge several scatter-gather requests into one DMA
461 * transfer. This is possible if the scatter buffers lie on
462 * physical contiguous addresses.
463 *
464 * Parameters: Scsi_Cmnd *cmd
465 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300466 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468
Roman Zippelc28bda22007-05-01 22:32:36 +0200469static void merge_contiguous_buffers(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Roman Zippelc28bda22007-05-01 22:32:36 +0200471 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200473 unsigned long oldlen = cmd->SCp.this_residual;
474 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475#endif
476
Roman Zippelc28bda22007-05-01 22:32:36 +0200477 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
478 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200479 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100480 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200481 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200483 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200485 ++cmd->SCp.buffer;
486 --cmd->SCp.buffers_residual;
487 cmd->SCp.this_residual += cmd->SCp.buffer->length;
488 endaddr += cmd->SCp.buffer->length;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200491 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100492 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200493 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#endif
495}
496
497/*
498 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
499 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200500 * Purpose : initialize the saved data pointers for cmd to point to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * start of the buffer.
502 *
503 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
504 */
505
Roman Zippelc28bda22007-05-01 22:32:36 +0200506static inline void initialize_SCp(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Roman Zippelc28bda22007-05-01 22:32:36 +0200508 /*
509 * Initialize the Scsi Pointer field so that all of the commands in the
510 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200512
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200513 if (scsi_bufflen(cmd)) {
514 cmd->SCp.buffer = scsi_sglist(cmd);
515 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200516 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200517 cmd->SCp.this_residual = cmd->SCp.buffer->length;
518 /* ++roman: Try to merge some scatter-buffers if they are at
519 * contiguous physical addresses.
520 */
521 merge_contiguous_buffers(cmd);
522 } else {
523 cmd->SCp.buffer = NULL;
524 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200525 cmd->SCp.ptr = NULL;
526 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#include <linux/delay.h>
531
532#if NDEBUG
533static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200534 unsigned char mask;
535 const char *name;
536} signals[] = {
537 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
538 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
539 { SR_SEL, "SEL" }, {0, NULL}
540}, basrs[] = {
541 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
542}, icrs[] = {
543 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
544 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
545 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
546 {0, NULL}
547}, mrs[] = {
548 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
549 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
550 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
551 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
552 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
553 {0, NULL}
554};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556/*
557 * Function : void NCR5380_print(struct Scsi_Host *instance)
558 *
559 * Purpose : print the SCSI bus signals for debugging purposes
560 *
561 * Input : instance - which NCR5380
562 */
563
Roman Zippelc28bda22007-05-01 22:32:36 +0200564static void NCR5380_print(struct Scsi_Host *instance)
565{
566 unsigned char status, data, basr, mr, icr, i;
567 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Roman Zippelc28bda22007-05-01 22:32:36 +0200569 local_irq_save(flags);
570 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
571 status = NCR5380_read(STATUS_REG);
572 mr = NCR5380_read(MODE_REG);
573 icr = NCR5380_read(INITIATOR_COMMAND_REG);
574 basr = NCR5380_read(BUS_AND_STATUS_REG);
575 local_irq_restore(flags);
576 printk("STATUS_REG: %02x ", status);
577 for (i = 0; signals[i].mask; ++i)
578 if (status & signals[i].mask)
579 printk(",%s", signals[i].name);
580 printk("\nBASR: %02x ", basr);
581 for (i = 0; basrs[i].mask; ++i)
582 if (basr & basrs[i].mask)
583 printk(",%s", basrs[i].name);
584 printk("\nICR: %02x ", icr);
585 for (i = 0; icrs[i].mask; ++i)
586 if (icr & icrs[i].mask)
587 printk(",%s", icrs[i].name);
588 printk("\nMODE: %02x ", mr);
589 for (i = 0; mrs[i].mask; ++i)
590 if (mr & mrs[i].mask)
591 printk(",%s", mrs[i].name);
592 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200596 unsigned char value;
597 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200599 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
600 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
601 {PHASE_UNKNOWN, "UNKNOWN"}
602};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Roman Zippelc28bda22007-05-01 22:32:36 +0200604/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
606 *
607 * Purpose : print the current SCSI phase for debugging purposes
608 *
609 * Input : instance - which NCR5380
610 */
611
612static void NCR5380_print_phase(struct Scsi_Host *instance)
613{
Roman Zippelc28bda22007-05-01 22:32:36 +0200614 unsigned char status;
615 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Roman Zippelc28bda22007-05-01 22:32:36 +0200617 status = NCR5380_read(STATUS_REG);
618 if (!(status & SR_REQ))
619 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
620 else {
621 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
622 (phases[i].value != (status & PHASE_MASK)); ++i)
623 ;
624 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#endif
629
630/*
631 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200632 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * If we're not in an interrupt, we can call our main directly, it cannot be
634 * already running. Else, we queue it on a task queue, if not 'main_running'
635 * tells us that a lower level is already executing it. This way,
636 * 'main_running' needs not be protected in a special way.
637 *
638 * queue_main() is a utility function for putting our main onto the task
639 * queue, if main_running is false. It should be called only from a
640 * interrupt or bottom half.
641 */
642
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900643#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644#include <linux/workqueue.h>
645#include <linux/interrupt.h>
646
Roman Zippelc28bda22007-05-01 22:32:36 +0200647static volatile int main_running;
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200648static DECLARE_WORK(NCR5380_tqueue, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Roman Zippelc28bda22007-05-01 22:32:36 +0200650static inline void queue_main(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Roman Zippelc28bda22007-05-01 22:32:36 +0200652 if (!main_running) {
653 /* If in interrupt and NCR5380_main() not already running,
654 queue it on the 'immediate' task queue, to be processed
655 immediately after the current interrupt processing has
656 finished. */
657 schedule_work(&NCR5380_tqueue);
658 }
659 /* else: nothing to do: the running NCR5380_main() will pick up
660 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663
Roman Zippelc28bda22007-05-01 22:32:36 +0200664static inline void NCR5380_all_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Roman Zippelc28bda22007-05-01 22:32:36 +0200666 static int done = 0;
667 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +1100668 dprintk(NDEBUG_INIT, "scsi : NCR5380_all_init()\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200669 done = 1;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Roman Zippelc28bda22007-05-01 22:32:36 +0200673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/*
675 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
676 *
677 * Purpose : called by probe code indicating the NCR5380 driver
678 * options that were selected.
679 *
680 * Inputs : instance, pointer to this instance. Unused.
681 */
682
Roman Zippelc28bda22007-05-01 22:32:36 +0200683static void __init NCR5380_print_options(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Roman Zippelc28bda22007-05-01 22:32:36 +0200685 printk(" generic options"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200687 " REAL DMA"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688#endif
689#ifdef PARITY
Roman Zippelc28bda22007-05-01 22:32:36 +0200690 " PARITY"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691#endif
692#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200693 " SCSI-2 TAGGED QUEUING"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200695 );
696 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
700 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
701 *
702 * Purpose : print commands in the various queues, called from
703 * NCR5380_abort and NCR5380_debug to aid debugging.
704 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200705 * Inputs : instance, pointer to this instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707
Al Virod89537e2013-03-31 13:24:44 -0400708static void lprint_Scsi_Cmnd(Scsi_Cmnd *cmd)
709{
710 int i, s;
711 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200712 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400713 H_NO(cmd), cmd->device->id, cmd->device->lun);
714 printk(KERN_CONT " command = ");
715 command = cmd->cmnd;
716 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
717 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
718 printk(KERN_CONT " %02x", command[i]);
719 printk("\n");
720}
721
Roman Zippelc28bda22007-05-01 22:32:36 +0200722static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Al Virod89537e2013-03-31 13:24:44 -0400724 struct NCR5380_hostdata *hostdata;
725 Scsi_Cmnd *ptr;
726 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Finn Thain8ad3a592014-03-18 11:42:19 +1100728 NCR5380_dprint(NDEBUG_ANY, instance);
729 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Roman Zippelc28bda22007-05-01 22:32:36 +0200731 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Al Virod89537e2013-03-31 13:24:44 -0400733 printk("\nNCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
Roman Zippelc28bda22007-05-01 22:32:36 +0200734 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400735 printk("NCR5380: coroutine is%s running.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200736 main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200737 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400738 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200739 else
Al Virod89537e2013-03-31 13:24:44 -0400740 lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected);
741 printk("scsi%d: issue_queue\n", HOSTNO);
742 for (ptr = (Scsi_Cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
743 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Al Virod89537e2013-03-31 13:24:44 -0400745 printk("scsi%d: disconnected_queue\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200746 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400747 ptr = NEXT(ptr))
748 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200749
750 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400751 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Al Virod89537e2013-03-31 13:24:44 -0400754static void show_Scsi_Cmnd(Scsi_Cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Roman Zippelc28bda22007-05-01 22:32:36 +0200756 int i, s;
757 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200758 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200759 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400760 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200761 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400762 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200763 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400764 seq_printf(m, " %02x", command[i]);
765 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Finn Thain4d3d2a52014-11-12 16:11:52 +1100768static int __maybe_unused NCR5380_show_info(struct seq_file *m,
769 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400770{
771 struct NCR5380_hostdata *hostdata;
772 Scsi_Cmnd *ptr;
773 unsigned long flags;
774
775 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
776
777 seq_printf(m, "NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
778 local_irq_save(flags);
779 seq_printf(m, "NCR5380: coroutine is%s running.\n",
780 main_running ? "" : "n't");
781 if (!hostdata->connected)
782 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
783 else
784 show_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, m);
785 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
786 for (ptr = (Scsi_Cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
787 show_Scsi_Cmnd(ptr, m);
788
789 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
790 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
791 ptr = NEXT(ptr))
792 show_Scsi_Cmnd(ptr, m);
793
794 local_irq_restore(flags);
795 return 0;
796}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Roman Zippelc28bda22007-05-01 22:32:36 +0200798/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * Function : void NCR5380_init (struct Scsi_Host *instance)
800 *
801 * Purpose : initializes *instance and corresponding 5380 chip.
802 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200803 * Inputs : instance - instantiation of the 5380 driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
805 * Notes : I assume that the host, hostno, and id bits have been
Roman Zippelc28bda22007-05-01 22:32:36 +0200806 * set correctly. I don't care about the irq and other fields.
807 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100810static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Roman Zippelc28bda22007-05-01 22:32:36 +0200812 int i;
813 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Roman Zippelc28bda22007-05-01 22:32:36 +0200815 NCR5380_all_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Roman Zippelc28bda22007-05-01 22:32:36 +0200817 hostdata->aborted = 0;
818 hostdata->id_mask = 1 << instance->this_id;
819 hostdata->id_higher_mask = 0;
820 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
821 if (i > hostdata->id_mask)
822 hostdata->id_higher_mask |= i;
823 for (i = 0; i < 8; ++i)
824 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200826 init_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif
828#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200829 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200831 hostdata->targets_present = 0;
832 hostdata->connected = NULL;
833 hostdata->issue_queue = NULL;
834 hostdata->disconnected_queue = NULL;
835 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Roman Zippelc28bda22007-05-01 22:32:36 +0200837 if (!the_template) {
838 the_template = instance->hostt;
839 first_instance = instance;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Roman Zippelc28bda22007-05-01 22:32:36 +0200842 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
843 NCR5380_write(MODE_REG, MR_BASE);
844 NCR5380_write(TARGET_COMMAND_REG, 0);
845 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Roman Zippelc28bda22007-05-01 22:32:36 +0200847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200850static void NCR5380_exit(struct Scsi_Host *instance)
851{
852 /* Empty, as we didn't schedule any delayed work */
853}
854
Roman Zippelc28bda22007-05-01 22:32:36 +0200855/*
Roman Zippelc28bda22007-05-01 22:32:36 +0200856 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
857 * void (*done)(Scsi_Cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
859 * Purpose : enqueues a SCSI command
860 *
861 * Inputs : cmd - SCSI command, done - function called on completion, with
862 * a pointer to the command descriptor.
Roman Zippelc28bda22007-05-01 22:32:36 +0200863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * Returns : 0
865 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200866 * Side effects :
867 * cmd is added to the per instance issue_queue, with minor
868 * twiddling done to the host specific fields of cmd. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * main coroutine is not running, it is restarted.
870 *
871 */
872
Jeff Garzikf2812332010-11-16 02:10:29 -0500873static int NCR5380_queue_command_lck(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Roman Zippelc28bda22007-05-01 22:32:36 +0200875 SETUP_HOSTDATA(cmd->device->host);
876 Scsi_Cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200877 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200880 switch (cmd->cmnd[0]) {
881 case WRITE_6:
882 case WRITE_10:
883 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
884 H_NO(cmd));
885 cmd->result = (DID_ERROR << 16);
886 done(cmd);
887 return 0;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#ifdef NCR5380_STATS
892# if 0
Roman Zippelc28bda22007-05-01 22:32:36 +0200893 if (!hostdata->connected && !hostdata->issue_queue &&
894 !hostdata->disconnected_queue) {
895 hostdata->timebase = jiffies;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897# endif
898# ifdef NCR5380_STAT_LIMIT
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200899 if (scsi_bufflen(cmd) > NCR5380_STAT_LIMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900# endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200901 switch (cmd->cmnd[0]) {
902 case WRITE:
903 case WRITE_6:
904 case WRITE_10:
905 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200906 hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200907 hostdata->pendingw++;
908 break;
909 case READ:
910 case READ_6:
911 case READ_10:
912 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200913 hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200914 hostdata->pendingr++;
915 break;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917#endif
918
Roman Zippelc28bda22007-05-01 22:32:36 +0200919 /*
920 * We use the host_scribble field as a pointer to the next command
921 * in a queue
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Roman Zippel3130d902007-05-01 22:32:37 +0200924 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200925 cmd->scsi_done = done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Roman Zippelc28bda22007-05-01 22:32:36 +0200927 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Roman Zippelc28bda22007-05-01 22:32:36 +0200929 /*
930 * Insert the cmd into the issue queue. Note that REQUEST SENSE
931 * commands are added to the head of the queue since any command will
932 * clear the contingent allegiance condition that exists and the
933 * sense data is only guaranteed to be valid while the condition exists.
934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Roman Zippelc28bda22007-05-01 22:32:36 +0200936 local_irq_save(flags);
937 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
938 * Otherwise a running NCR5380_main may steal the lock.
939 * Lock before actually inserting due to fairness reasons explained in
940 * atari_scsi.c. If we insert first, then it's impossible for this driver
941 * to release the lock.
942 * Stop timer for this command while waiting for the lock, or timeouts
943 * may happen (and they really do), and it's no good if the command doesn't
944 * appear in any of the queues.
945 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
946 * because also a timer int can trigger an abort or reset, which would
947 * alter queues and touch the lock.
948 */
949 if (!IS_A_TT()) {
Michael Schmitz8ce79552007-06-03 12:55:04 +0200950 /* perhaps stop command timer here */
Roman Zippelc28bda22007-05-01 22:32:36 +0200951 falcon_get_lock();
Michael Schmitz8ce79552007-06-03 12:55:04 +0200952 /* perhaps restart command timer here */
Roman Zippelc28bda22007-05-01 22:32:36 +0200953 }
954 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
955 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200956 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200957 hostdata->issue_queue = cmd;
958 } else {
959 for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
960 NEXT(tmp); tmp = NEXT(tmp))
961 ;
962 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200963 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200964 }
965 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Finn Thaind65e6342014-03-18 11:42:20 +1100967 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200968 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Roman Zippelc28bda22007-05-01 22:32:36 +0200970 /* If queue_command() is called from an interrupt (real one or bottom
971 * half), we let queue_main() do the job of taking care about main. If it
972 * is already running, this is a no-op, else main will be queued.
973 *
974 * If we're not in an interrupt, we can call NCR5380_main()
975 * unconditionally, because it cannot be already running.
976 */
977 if (in_interrupt() || ((flags >> 8) & 7) >= 6)
978 queue_main();
979 else
980 NCR5380_main(NULL);
981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Jeff Garzikf2812332010-11-16 02:10:29 -0500984static DEF_SCSI_QCMD(NCR5380_queue_command)
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
Roman Zippelc28bda22007-05-01 22:32:36 +0200987 * Function : NCR5380_main (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200989 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
990 * be done on the NCR5380 host adapters in a system. Both
991 * NCR5380_queue_command() and NCR5380_intr() will try to start it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200993 *
994 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * reenable them. This prevents reentrancy and kernel stack overflow.
Roman Zippelc28bda22007-05-01 22:32:36 +0200996 */
997
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200998static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Roman Zippelc28bda22007-05-01 22:32:36 +02001000 Scsi_Cmnd *tmp, *prev;
1001 struct Scsi_Host *instance = first_instance;
1002 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
1003 int done;
1004 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Roman Zippelc28bda22007-05-01 22:32:36 +02001006 /*
1007 * We run (with interrupts disabled) until we're sure that none of
1008 * the host adapters have anything that can be done, at which point
1009 * we set main_running to 0 and exit.
1010 *
1011 * Interrupts are enabled before doing various other internal
1012 * instructions, after we've decided that we need to run through
1013 * the loop again.
1014 *
1015 * this should prevent any race conditions.
1016 *
1017 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1018 * because also a timer int can trigger an abort or reset, which can
1019 * alter queues and touch the Falcon lock.
1020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Roman Zippelc28bda22007-05-01 22:32:36 +02001022 /* Tell int handlers main() is now already executing. Note that
1023 no races are possible here. If an int comes in before
1024 'main_running' is set here, and queues/executes main via the
1025 task queue, it doesn't do any harm, just this instance of main
1026 won't find any work left to do. */
1027 if (main_running)
1028 return;
1029 main_running = 1;
1030
1031 local_save_flags(flags);
1032 do {
1033 local_irq_disable(); /* Freeze request queues */
1034 done = 1;
1035
1036 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001037 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001038 /*
1039 * Search through the issue_queue for a command destined
1040 * for a target that's not busy.
1041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +02001043 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
1044 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1045 ;
1046 /*printk("%p ", tmp);*/
1047 if ((tmp == prev) && tmp)
1048 printk(" LOOP\n");
1049 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001051 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1052 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001053 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +02001056 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001057 printk("MAIN tmp=%p target=%d busy=%d lun=%llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001058 tmp, tmp->device->id, hostdata->busy[tmp->device->id],
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001059 lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001061 /* When we find one, remove it from the issue queue. */
1062 /* ++guenther: possible race with Falcon locking */
1063 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001065 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001067 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001068#endif
1069 ) {
1070 /* ++guenther: just to be sure, this must be atomic */
1071 local_irq_disable();
1072 if (prev) {
1073 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001074 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001075 } else {
1076 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1077 hostdata->issue_queue = NEXT(tmp);
1078 }
Roman Zippel3130d902007-05-01 22:32:37 +02001079 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02001080 falcon_dont_release++;
1081
1082 /* reenable interrupts after finding one */
1083 local_irq_restore(flags);
1084
1085 /*
1086 * Attempt to establish an I_T_L nexus here.
1087 * On success, instance->hostdata->connected is set.
1088 * On failure, we must add the command back to the
1089 * issue queue so we can keep trying.
1090 */
Finn Thaind65e6342014-03-18 11:42:20 +11001091 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001092 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001093 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001094 /*
1095 * REQUEST SENSE commands are issued without tagged
1096 * queueing, even on SCSI-II devices because the
1097 * contingent allegiance condition exists for the
1098 * entire unit.
1099 */
1100 /* ++roman: ...and the standard also requires that
1101 * REQUEST SENSE command are untagged.
1102 */
1103
1104#ifdef SUPPORT_TAGS
1105 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1106#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001107 if (!NCR5380_select(instance, tmp)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001108 falcon_dont_release--;
1109 /* release if target did not response! */
1110 falcon_release_lock_if_possible(hostdata);
1111 break;
1112 } else {
1113 local_irq_disable();
1114 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001115 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001116 hostdata->issue_queue = tmp;
1117#ifdef SUPPORT_TAGS
1118 cmd_free_tag(tmp);
1119#endif
1120 falcon_dont_release--;
1121 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001122 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001123 "returned to issue_queue\n", HOSTNO);
1124 if (hostdata->connected)
1125 break;
1126 }
1127 } /* if target/lun/target queue is not busy */
1128 } /* for issue_queue */
1129 } /* if (!hostdata->connected) */
1130
1131 if (hostdata->connected
1132#ifdef REAL_DMA
1133 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134#endif
1135 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001137 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001138 HOSTNO);
1139 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001140 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001141 done = 0;
1142 }
1143 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Roman Zippelc28bda22007-05-01 22:32:36 +02001145 /* Better allow ints _after_ 'main_running' has been cleared, else
1146 an interrupt could believe we'll pick up the work it left for
1147 us, but we won't see it anymore here... */
1148 main_running = 0;
1149 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152
1153#ifdef REAL_DMA
1154/*
1155 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1156 *
1157 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001158 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *
1160 * Inputs : instance - this instance of the NCR5380.
1161 *
1162 */
1163
Roman Zippelc28bda22007-05-01 22:32:36 +02001164static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Roman Zippelc28bda22007-05-01 22:32:36 +02001166 SETUP_HOSTDATA(instance);
1167 int transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1168 unsigned char **data, p;
1169 volatile int *count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Roman Zippelc28bda22007-05-01 22:32:36 +02001171 if (!hostdata->connected) {
1172 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1173 "no connected cmd\n", HOSTNO);
1174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Roman Zippelc28bda22007-05-01 22:32:36 +02001177 if (atari_read_overruns) {
1178 p = hostdata->connected->SCp.phase;
1179 if (p & SR_IO) {
1180 udelay(10);
1181 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1182 (BASR_PHASE_MATCH|BASR_ACK)) ==
1183 (BASR_PHASE_MATCH|BASR_ACK)) {
1184 saved_data = NCR5380_read(INPUT_DATA_REG);
1185 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001186 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001187 }
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001190
Finn Thaind65e6342014-03-18 11:42:20 +11001191 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001192 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1193 NCR5380_read(STATUS_REG));
1194
1195 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1196 NCR5380_write(MODE_REG, MR_BASE);
1197 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1198
1199 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1200 hostdata->dma_len = 0;
1201
1202 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1203 count = &hostdata->connected->SCp.this_residual;
1204 *data += transfered;
1205 *count -= transfered;
1206
1207 if (atari_read_overruns) {
1208 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1209 cnt = toPIO = atari_read_overruns;
1210 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001211 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001212 *(*data)++ = saved_data;
1213 (*count)--;
1214 cnt--;
1215 toPIO--;
1216 }
Finn Thaind65e6342014-03-18 11:42:20 +11001217 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001218 NCR5380_transfer_pio(instance, &p, &cnt, data);
1219 *count -= toPIO - cnt;
1220 }
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223#endif /* REAL_DMA */
1224
1225
1226/*
1227 * Function : void NCR5380_intr (int irq)
Roman Zippelc28bda22007-05-01 22:32:36 +02001228 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
Roman Zippelc28bda22007-05-01 22:32:36 +02001230 * from the disconnected queue, and restarting NCR5380_main()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * as required.
1232 *
1233 * Inputs : int irq, irq that caused this interrupt.
1234 *
1235 */
1236
Roman Zippelc28bda22007-05-01 22:32:36 +02001237static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Roman Zippelc28bda22007-05-01 22:32:36 +02001239 struct Scsi_Host *instance = first_instance;
1240 int done = 1, handled = 0;
1241 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Finn Thaind65e6342014-03-18 11:42:20 +11001243 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Roman Zippelc28bda22007-05-01 22:32:36 +02001245 /* Look for pending interrupts */
1246 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001247 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001248 /* dispatch to appropriate routine if found and done=0 */
1249 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001250 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001251 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1252 done = 0;
1253 ENABLE_IRQ();
Finn Thaind65e6342014-03-18 11:42:20 +11001254 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001255 NCR5380_reselect(instance);
1256 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1257 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001258 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001259 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1260 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001261 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001262 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1263 } else {
1264 /*
1265 * The rest of the interrupt conditions can occur only during a
1266 * DMA transfer
1267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001270 /*
1271 * We should only get PHASE MISMATCH and EOP interrupts if we have
1272 * DMA enabled, so do a sanity check based on the current setting
1273 * of the MODE register.
1274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Roman Zippelc28bda22007-05-01 22:32:36 +02001276 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1277 ((basr & BASR_END_DMA_TRANSFER) ||
1278 !(basr & BASR_PHASE_MATCH))) {
1279
Finn Thaind65e6342014-03-18 11:42:20 +11001280 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001281 NCR5380_dma_complete( instance );
1282 done = 0;
1283 ENABLE_IRQ();
1284 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001286 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001288 if (basr & BASR_PHASE_MATCH)
1289 printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1290 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1291 HOSTNO, basr, NCR5380_read(MODE_REG),
1292 NCR5380_read(STATUS_REG));
1293 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1294 }
1295 } /* if !(SELECTION || PARITY) */
1296 handled = 1;
1297 } /* BASR & IRQ */ else {
1298 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1299 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1300 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1301 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1302 }
1303
1304 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001305 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001306 /* Put a call to NCR5380_main() on the queue... */
1307 queue_main();
1308 }
1309 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312#ifdef NCR5380_STATS
Roman Zippelc28bda22007-05-01 22:32:36 +02001313static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315# ifdef NCR5380_STAT_LIMIT
Boaz Harrosh9e0fe442007-11-05 11:23:35 +02001316 if (scsi_bufflen(cmd) > NCR5380_STAT_LIMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317# endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 switch (cmd->cmnd[0]) {
1319 case WRITE:
1320 case WRITE_6:
1321 case WRITE_10:
1322 hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +02001323 /*hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);*/
Roman Zippelc28bda22007-05-01 22:32:36 +02001324 hostdata->pendingw--;
1325 break;
1326 case READ:
1327 case READ_6:
1328 case READ_10:
1329 hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +02001330 /*hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);*/
Roman Zippelc28bda22007-05-01 22:32:36 +02001331 hostdata->pendingr--;
1332 break;
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335#endif
1336
Roman Zippelc28bda22007-05-01 22:32:36 +02001337/*
Finn Thain76f13b92014-11-12 16:11:53 +11001338 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *
1340 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001341 * including ARBITRATION, SELECTION, and initial message out for
1342 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001344 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001345 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001347 * Returns : -1 if selection could not execute for some reason,
1348 * 0 if selection succeeded or failed because the target
1349 * did not respond.
1350 *
1351 * Side effects :
1352 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * with registers as they should have been on entry - ie
1354 * SELECT_ENABLE will be set appropriately, the NCR5380
1355 * will cease to drive any SCSI bus signals.
1356 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001357 * If successful : I_T_L or I_T_L_Q nexus will be established,
1358 * instance->connected will be set to cmd.
1359 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001361 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * cmd->result host byte set to DID_BAD_TARGET.
1363 */
1364
Finn Thain76f13b92014-11-12 16:11:53 +11001365static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Roman Zippelc28bda22007-05-01 22:32:36 +02001367 SETUP_HOSTDATA(instance);
1368 unsigned char tmp[3], phase;
1369 unsigned char *data;
1370 int len;
1371 unsigned long timeout;
1372 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Roman Zippelc28bda22007-05-01 22:32:36 +02001374 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001375 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001376 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001377 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Roman Zippelc28bda22007-05-01 22:32:36 +02001379 /*
1380 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1381 * data bus during SELECTION.
1382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Roman Zippelc28bda22007-05-01 22:32:36 +02001384 local_irq_save(flags);
1385 if (hostdata->connected) {
1386 local_irq_restore(flags);
1387 return -1;
1388 }
1389 NCR5380_write(TARGET_COMMAND_REG, 0);
1390
1391 /*
1392 * Start arbitration.
1393 */
1394
1395 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1396 NCR5380_write(MODE_REG, MR_ARBITRATE);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Roman Zippelc28bda22007-05-01 22:32:36 +02001400 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001401#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001402 {
1403 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Roman Zippelc28bda22007-05-01 22:32:36 +02001405 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1406 time_before(jiffies, timeout) && !hostdata->connected)
1407 ;
1408 if (time_after_eq(jiffies, timeout)) {
1409 printk("scsi : arbitration timeout at %d\n", __LINE__);
1410 NCR5380_write(MODE_REG, MR_BASE);
1411 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1412 return -1;
1413 }
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001416 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1417 !hostdata->connected)
1418 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419#endif
1420
Finn Thaind65e6342014-03-18 11:42:20 +11001421 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Roman Zippelc28bda22007-05-01 22:32:36 +02001423 if (hostdata->connected) {
1424 NCR5380_write(MODE_REG, MR_BASE);
1425 return -1;
1426 }
1427 /*
1428 * The arbitration delay is 2.2us, but this is a minimum and there is
1429 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1430 * the integral nature of udelay().
1431 *
1432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Roman Zippelc28bda22007-05-01 22:32:36 +02001434 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Roman Zippelc28bda22007-05-01 22:32:36 +02001436 /* Check for lost arbitration */
1437 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1438 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1439 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1440 hostdata->connected) {
1441 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001442 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001443 HOSTNO);
1444 return -1;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Roman Zippelc28bda22007-05-01 22:32:36 +02001447 /* after/during arbitration, BSY should be asserted.
1448 IBM DPES-31080 Version S31Q works now */
1449 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1450 NCR5380_write(INITIATOR_COMMAND_REG,
1451 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Roman Zippelc28bda22007-05-01 22:32:36 +02001453 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1454 hostdata->connected) {
1455 NCR5380_write(MODE_REG, MR_BASE);
1456 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001457 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001458 HOSTNO);
1459 return -1;
1460 }
1461
1462 /*
1463 * Again, bus clear + bus settle time is 1.2us, however, this is
1464 * a minimum so we'll udelay ceil(1.2)
1465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1469 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001471 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001473
1474 if (hostdata->connected) {
1475 NCR5380_write(MODE_REG, MR_BASE);
1476 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1477 return -1;
1478 }
1479
Finn Thaind65e6342014-03-18 11:42:20 +11001480 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001481
1482 /*
1483 * Now that we have won arbitration, start Selection process, asserting
1484 * the host and target ID's on the SCSI bus.
1485 */
1486
1487 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1488
1489 /*
1490 * Raise ATN while SEL is true before BSY goes false from arbitration,
1491 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1492 * phase immediately after selection.
1493 */
1494
1495 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1496 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Roman Zippelc28bda22007-05-01 22:32:36 +02001499 /*
1500 * Reselect interrupts must be turned off prior to the dropping of BSY,
1501 * otherwise we will trigger an interrupt.
1502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Roman Zippelc28bda22007-05-01 22:32:36 +02001504 if (hostdata->connected) {
1505 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1506 return -1;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Roman Zippelc28bda22007-05-01 22:32:36 +02001509 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Roman Zippelc28bda22007-05-01 22:32:36 +02001511 /*
1512 * The initiator shall then wait at least two deskew delays and release
1513 * the BSY signal.
1514 */
1515 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Roman Zippelc28bda22007-05-01 22:32:36 +02001517 /* Reset BSY */
1518 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1519 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Roman Zippelc28bda22007-05-01 22:32:36 +02001521 /*
1522 * Something weird happens when we cease to drive BSY - looks
1523 * like the board/chip is letting us do another read before the
1524 * appropriate propagation delay has expired, and we're confusing
1525 * a BSY signal from ourselves as the target's response to SELECTION.
1526 *
1527 * A small delay (the 'C++' frontend breaks the pipeline with an
1528 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1529 * tighter 'C' code breaks and requires this) solves the problem -
1530 * the 1 us delay is arbitrary, and only used because this delay will
1531 * be the same on other platforms and since it works here, it should
1532 * work there.
1533 *
1534 * wingel suggests that this could be due to failing to wait
1535 * one deskew delay.
1536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Roman Zippelc28bda22007-05-01 22:32:36 +02001538 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Finn Thaind65e6342014-03-18 11:42:20 +11001540 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Roman Zippelc28bda22007-05-01 22:32:36 +02001542 /*
1543 * The SCSI specification calls for a 250 ms timeout for the actual
1544 * selection.
1545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Roman Zippelc28bda22007-05-01 22:32:36 +02001547 timeout = jiffies + 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Roman Zippelc28bda22007-05-01 22:32:36 +02001549 /*
1550 * XXX very interesting - we're seeing a bounce where the BSY we
1551 * asserted is being reflected / still asserted (propagation delay?)
1552 * and it's detecting as true. Sigh.
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001556 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1557 * IO while SEL is true. But again, there are some disks out the in the
1558 * world that do that nevertheless. (Somebody claimed that this announces
1559 * reselection capability of the target.) So we better skip that test and
1560 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Roman Zippelc28bda22007-05-01 22:32:36 +02001563 while (time_before(jiffies, timeout) &&
1564 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1565 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Roman Zippelc28bda22007-05-01 22:32:36 +02001567 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1568 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1569 NCR5380_reselect(instance);
1570 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1571 HOSTNO);
1572 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1573 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001576 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1577 ;
1578#endif
1579
1580 /*
1581 * No less than two deskew delays after the initiator detects the
1582 * BSY signal is true, it shall release the SEL signal and may
1583 * change the DATA BUS. -wingel
1584 */
1585
1586 udelay(1);
1587
1588 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1589
1590 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1591 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1592 if (hostdata->targets_present & (1 << cmd->device->id)) {
1593 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1594 if (hostdata->restart_select)
1595 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001596 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001597 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1598 return -1;
1599 }
1600 cmd->result = DID_BAD_TARGET << 16;
1601#ifdef NCR5380_STATS
1602 collect_stats(hostdata, cmd);
1603#endif
1604#ifdef SUPPORT_TAGS
1605 cmd_free_tag(cmd);
1606#endif
1607 cmd->scsi_done(cmd);
1608 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001609 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001610 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1611 return 0;
1612 }
1613
1614 hostdata->targets_present |= (1 << cmd->device->id);
1615
1616 /*
1617 * Since we followed the SCSI spec, and raised ATN while SEL
1618 * was true but before BSY was false during selection, the information
1619 * transfer phase should be a MESSAGE OUT phase so that we can send the
1620 * IDENTIFY message.
1621 *
1622 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1623 * message (2 bytes) with a tag ID that we increment with every command
1624 * until it wraps back to 0.
1625 *
1626 * XXX - it turns out that there are some broken SCSI-II devices,
1627 * which claim to support tagged queuing but fail when more than
1628 * some number of commands are issued at once.
1629 */
1630
1631 /* Wait for start of REQ/ACK handshake */
1632 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1633 ;
1634
Finn Thaind65e6342014-03-18 11:42:20 +11001635 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001636 HOSTNO, cmd->device->id);
1637 tmp[0] = IDENTIFY(1, cmd->device->lun);
1638
1639#ifdef SUPPORT_TAGS
1640 if (cmd->tag != TAG_NONE) {
1641 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1642 tmp[2] = cmd->tag;
1643 len = 3;
1644 } else
1645 len = 1;
1646#else
1647 len = 1;
1648 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649#endif /* SUPPORT_TAGS */
1650
Roman Zippelc28bda22007-05-01 22:32:36 +02001651 /* Send message(s) */
1652 data = tmp;
1653 phase = PHASE_MSGOUT;
1654 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001655 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001656 /* XXX need to handle errors here */
1657 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001659 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1660#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Roman Zippelc28bda22007-05-01 22:32:36 +02001662 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Roman Zippelc28bda22007-05-01 22:32:36 +02001664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Roman Zippelc28bda22007-05-01 22:32:36 +02001667/*
1668 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * unsigned char *phase, int *count, unsigned char **data)
1670 *
1671 * Purpose : transfers data in given phase using polled I/O
1672 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001673 * Inputs : instance - instance of driver, *phase - pointer to
1674 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001676 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001678 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * is in same phase.
1680 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001681 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 *
1683 * XXX Note : handling for bus free may be useful.
1684 */
1685
1686/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001687 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 * IS 100% reliable, and for the actual data transfer where speed
1689 * counts, we will always do a pseudo DMA or DMA transfer.
1690 */
1691
Roman Zippelc28bda22007-05-01 22:32:36 +02001692static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1693 unsigned char *phase, int *count,
1694 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Roman Zippelc28bda22007-05-01 22:32:36 +02001696 register unsigned char p = *phase, tmp;
1697 register int c = *count;
1698 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Roman Zippelc28bda22007-05-01 22:32:36 +02001700 /*
1701 * The NCR5380 chip will only drive the SCSI bus when the
1702 * phase specified in the appropriate bits of the TARGET COMMAND
1703 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
1705
Roman Zippelc28bda22007-05-01 22:32:36 +02001706 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Roman Zippelc28bda22007-05-01 22:32:36 +02001708 do {
1709 /*
1710 * Wait for assertion of REQ, after which the phase bits will be
1711 * valid
1712 */
1713 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1714 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Finn Thaind65e6342014-03-18 11:42:20 +11001716 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Roman Zippelc28bda22007-05-01 22:32:36 +02001718 /* Check for phase mismatch */
1719 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001720 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001721 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001722 break;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Roman Zippelc28bda22007-05-01 22:32:36 +02001725 /* Do actual transfer from SCSI bus to / from memory */
1726 if (!(p & SR_IO))
1727 NCR5380_write(OUTPUT_DATA_REG, *d);
1728 else
1729 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Roman Zippelc28bda22007-05-01 22:32:36 +02001731 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Roman Zippelc28bda22007-05-01 22:32:36 +02001733 /*
1734 * The SCSI standard suggests that in MSGOUT phase, the initiator
1735 * should drop ATN on the last byte of the message phase
1736 * after REQ has been asserted for the handshake but before
1737 * the initiator raises ACK.
1738 */
1739
1740 if (!(p & SR_IO)) {
1741 if (!((p & SR_MSG) && c > 1)) {
1742 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001743 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001744 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1745 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1746 } else {
1747 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1748 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001749 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001750 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1751 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1752 }
1753 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001754 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001755 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1756 }
1757
1758 while (NCR5380_read(STATUS_REG) & SR_REQ)
1759 ;
1760
Finn Thaind65e6342014-03-18 11:42:20 +11001761 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001762
1763 /*
1764 * We have several special cases to consider during REQ/ACK handshaking :
1765 * 1. We were in MSGOUT phase, and we are on the last byte of the
1766 * message. ATN must be dropped as ACK is dropped.
1767 *
1768 * 2. We are in a MSGIN phase, and we are on the last byte of the
1769 * message. We must exit with ACK asserted, so that the calling
1770 * code may raise ATN before dropping ACK to reject the message.
1771 *
1772 * 3. ACK and ATN are clear and the target may proceed as normal.
1773 */
1774 if (!(p == PHASE_MSGIN && c == 1)) {
1775 if (p == PHASE_MSGOUT && c > 1)
1776 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1777 else
1778 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1779 }
1780 } while (--c);
1781
Finn Thaind65e6342014-03-18 11:42:20 +11001782 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001783
1784 *count = c;
1785 *data = d;
1786 tmp = NCR5380_read(STATUS_REG);
1787 /* The phase read from the bus is valid if either REQ is (already)
1788 * asserted or if ACK hasn't been released yet. The latter is the case if
1789 * we're in MSGIN and all wanted bytes have been received.
1790 */
1791 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1792 *phase = tmp & PHASE_MASK;
1793 else
1794 *phase = PHASE_UNKNOWN;
1795
1796 if (!c || (*phase == p))
1797 return 0;
1798 else
1799 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
1802/*
1803 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001804 *
1805 * Purpose : abort the currently established nexus. Should only be
1806 * called from a routine which can drop into a
1807 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * Returns : 0 on success, -1 on failure.
1809 */
1810
Roman Zippelc28bda22007-05-01 22:32:36 +02001811static int do_abort(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Roman Zippelc28bda22007-05-01 22:32:36 +02001813 unsigned char tmp, *msgptr, phase;
1814 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Roman Zippelc28bda22007-05-01 22:32:36 +02001816 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Roman Zippelc28bda22007-05-01 22:32:36 +02001819 /*
1820 * Wait for the target to indicate a valid phase by asserting
1821 * REQ. Once this happens, we'll have either a MSGOUT phase
1822 * and can immediately send the ABORT message, or we'll have some
1823 * other phase and will have to source/sink data.
1824 *
1825 * We really don't care what value was on the bus or what value
1826 * the target sees, so we just handshake.
1827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Roel Kluin3be38e72007-11-15 21:13:46 +01001829 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001830 ;
1831
1832 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1833
1834 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1835 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1836 ICR_ASSERT_ACK);
1837 while (NCR5380_read(STATUS_REG) & SR_REQ)
1838 ;
1839 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1840 }
1841
1842 tmp = ABORT;
1843 msgptr = &tmp;
1844 len = 1;
1845 phase = PHASE_MSGOUT;
1846 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1847
1848 /*
1849 * If we got here, and the command completed successfully,
1850 * we're about to go into bus free state.
1851 */
1852
1853 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001857/*
1858 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * unsigned char *phase, int *count, unsigned char **data)
1860 *
1861 * Purpose : transfers data in given phase using either real
1862 * or pseudo DMA.
1863 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001864 * Inputs : instance - instance of driver, *phase - pointer to
1865 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001867 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001869 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 * is in same phase.
1871 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001872 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 *
1874 */
1875
1876
Roman Zippelc28bda22007-05-01 22:32:36 +02001877static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1878 unsigned char *phase, int *count,
1879 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Roman Zippelc28bda22007-05-01 22:32:36 +02001881 SETUP_HOSTDATA(instance);
1882 register int c = *count;
1883 register unsigned char p = *phase;
1884 register unsigned char *d = *data;
1885 unsigned char tmp;
1886 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Roman Zippelc28bda22007-05-01 22:32:36 +02001888 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1889 *phase = tmp;
1890 return -1;
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Roman Zippelc28bda22007-05-01 22:32:36 +02001893 if (atari_read_overruns && (p & SR_IO))
1894 c -= atari_read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Finn Thaind65e6342014-03-18 11:42:20 +11001896 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001897 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1898 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Roman Zippelc28bda22007-05-01 22:32:36 +02001900 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001903 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904#endif /* def REAL_DMA */
1905
Roman Zippelc28bda22007-05-01 22:32:36 +02001906 if (IS_A_TT()) {
1907 /* On the Medusa, it is a must to initialize the DMA before
1908 * starting the NCR. This is also the cleaner way for the TT.
1909 */
1910 local_irq_save(flags);
1911 hostdata->dma_len = (p & SR_IO) ?
1912 NCR5380_dma_read_setup(instance, d, c) :
1913 NCR5380_dma_write_setup(instance, d, c);
1914 local_irq_restore(flags);
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Roman Zippelc28bda22007-05-01 22:32:36 +02001917 if (p & SR_IO)
1918 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1919 else {
1920 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1921 NCR5380_write(START_DMA_SEND_REG, 0);
1922 }
1923
1924 if (!IS_A_TT()) {
1925 /* On the Falcon, the DMA setup must be done after the last */
1926 /* NCR access, else the DMA setup gets trashed!
1927 */
1928 local_irq_save(flags);
1929 hostdata->dma_len = (p & SR_IO) ?
1930 NCR5380_dma_read_setup(instance, d, c) :
1931 NCR5380_dma_write_setup(instance, d, c);
1932 local_irq_restore(flags);
1933 }
1934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936#endif /* defined(REAL_DMA) */
1937
1938/*
1939 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1940 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001941 * Purpose : run through the various SCSI phases and do as the target
1942 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * instance->connected.
1944 *
1945 * Inputs : instance, instance for which we are doing commands
1946 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001947 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * modified if a command disconnects, *instance->connected will
1949 * change.
1950 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001951 * XXX Note : we need to watch for bus free or a reset condition here
1952 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001954
1955static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Roman Zippelc28bda22007-05-01 22:32:36 +02001957 SETUP_HOSTDATA(instance);
1958 unsigned long flags;
1959 unsigned char msgout = NOP;
1960 int sink = 0;
1961 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001963 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001965 unsigned char *data;
1966 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1967 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Roman Zippelc28bda22007-05-01 22:32:36 +02001969 while (1) {
1970 tmp = NCR5380_read(STATUS_REG);
1971 /* We only have a valid SCSI phase when REQ is asserted */
1972 if (tmp & SR_REQ) {
1973 phase = (tmp & PHASE_MASK);
1974 if (phase != old_phase) {
1975 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001976 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Roman Zippelc28bda22007-05-01 22:32:36 +02001979 if (sink && (phase != PHASE_MSGOUT)) {
1980 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Roman Zippelc28bda22007-05-01 22:32:36 +02001982 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1983 ICR_ASSERT_ACK);
1984 while (NCR5380_read(STATUS_REG) & SR_REQ)
1985 ;
1986 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1987 ICR_ASSERT_ATN);
1988 sink = 0;
1989 continue;
1990 }
1991
1992 switch (phase) {
1993 case PHASE_DATAOUT:
1994#if (NDEBUG & NDEBUG_NO_DATAOUT)
1995 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
1996 "aborted\n", HOSTNO);
1997 sink = 1;
1998 do_abort(instance);
1999 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002000 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002001 return;
2002#endif
2003 case PHASE_DATAIN:
2004 /*
2005 * If there is no room left in the current buffer in the
2006 * scatter-gather list, move onto the next one.
2007 */
2008
2009 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2010 ++cmd->SCp.buffer;
2011 --cmd->SCp.buffers_residual;
2012 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002013 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002014 /* ++roman: Try to merge some scatter-buffers if
2015 * they are at contiguous physical addresses.
2016 */
2017 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002018 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002019 HOSTNO, cmd->SCp.this_residual,
2020 cmd->SCp.buffers_residual);
2021 }
2022
2023 /*
2024 * The preferred transfer method is going to be
2025 * PSEUDO-DMA for systems that are strictly PIO,
2026 * since we can let the hardware do the handshaking.
2027 *
2028 * For this to work, we need to know the transfersize
2029 * ahead of time, since the pseudo-DMA code will sit
2030 * in an unconditional loop.
2031 */
2032
2033 /* ++roman: I suggest, this should be
2034 * #if def(REAL_DMA)
2035 * instead of leaving REAL_DMA out.
2036 */
2037
2038#if defined(REAL_DMA)
2039 if (!cmd->device->borken &&
2040 (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
2041 len = transfersize;
2042 cmd->SCp.phase = phase;
2043 if (NCR5380_transfer_dma(instance, &phase,
2044 &len, (unsigned char **)&cmd->SCp.ptr)) {
2045 /*
2046 * If the watchdog timer fires, all future
2047 * accesses to this device will use the
2048 * polled-IO. */
2049 printk(KERN_NOTICE "scsi%d: switching target %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002050 "lun %llu to slow handshake\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002051 cmd->device->id, cmd->device->lun);
2052 cmd->device->borken = 1;
2053 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2054 ICR_ASSERT_ATN);
2055 sink = 1;
2056 do_abort(instance);
2057 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002058 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002059 /* XXX - need to source or sink data here, as appropriate */
2060 } else {
2061#ifdef REAL_DMA
2062 /* ++roman: When using real DMA,
2063 * information_transfer() should return after
2064 * starting DMA since it has nothing more to
2065 * do.
2066 */
2067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002069 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002071 }
2072 } else
2073#endif /* defined(REAL_DMA) */
2074 NCR5380_transfer_pio(instance, &phase,
2075 (int *)&cmd->SCp.this_residual,
2076 (unsigned char **)&cmd->SCp.ptr);
2077 break;
2078 case PHASE_MSGIN:
2079 len = 1;
2080 data = &tmp;
2081 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2082 NCR5380_transfer_pio(instance, &phase, &len, &data);
2083 cmd->SCp.Message = tmp;
2084
2085 switch (tmp) {
2086 /*
2087 * Linking lets us reduce the time required to get the
2088 * next command out to the device, hopefully this will
2089 * mean we don't waste another revolution due to the delays
2090 * required by ARBITRATION and another SELECTION.
2091 *
2092 * In the current implementation proposal, low level drivers
2093 * merely have to start the next command, pointed to by
2094 * next_link, done() is called as with unlinked commands.
2095 */
2096#ifdef LINKED
2097 case LINKED_CMD_COMPLETE:
2098 case LINKED_FLG_CMD_COMPLETE:
2099 /* Accept message by clearing ACK */
2100 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2101
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002102 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002103 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2104
2105 /* Enable reselect interrupts */
2106 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2107 /*
2108 * Sanity check : A linked command should only terminate
2109 * with one of these messages if there are more linked
2110 * commands available.
2111 */
2112
2113 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002114 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002115 "linked command complete, no next_link\n",
2116 HOSTNO, cmd->device->id, cmd->device->lun);
2117 sink = 1;
2118 do_abort(instance);
2119 return;
2120 }
2121
2122 initialize_SCp(cmd->next_link);
2123 /* The next command is still part of this process; copy it
2124 * and don't free it! */
2125 cmd->next_link->tag = cmd->tag;
2126 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002127 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002128 "done, calling scsi_done().\n",
2129 HOSTNO, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130#ifdef NCR5380_STATS
Roman Zippelc28bda22007-05-01 22:32:36 +02002131 collect_stats(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002133 cmd->scsi_done(cmd);
2134 cmd = hostdata->connected;
2135 break;
2136#endif /* def LINKED */
2137 case ABORT:
2138 case COMMAND_COMPLETE:
2139 /* Accept message by clearing ACK */
2140 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2141 /* ++guenther: possible race with Falcon locking */
2142 falcon_dont_release++;
2143 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002144 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002145 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
2146#ifdef SUPPORT_TAGS
2147 cmd_free_tag(cmd);
2148 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2149 /* Turn a QUEUE FULL status into BUSY, I think the
2150 * mid level cannot handle QUEUE FULL :-( (The
2151 * command is retried after BUSY). Also update our
2152 * queue size to the number of currently issued
2153 * commands now.
2154 */
2155 /* ++Andreas: the mid level code knows about
2156 QUEUE_FULL now. */
2157 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002158 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002159 "QUEUE_FULL after %d commands\n",
2160 HOSTNO, cmd->device->id, cmd->device->lun,
2161 ta->nr_allocated);
2162 if (ta->queue_size > ta->nr_allocated)
2163 ta->nr_allocated = ta->queue_size;
2164 }
2165#else
2166 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2167#endif
2168 /* Enable reselect interrupts */
2169 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2170
2171 /*
2172 * I'm not sure what the correct thing to do here is :
2173 *
2174 * If the command that just executed is NOT a request
2175 * sense, the obvious thing to do is to set the result
2176 * code to the values of the stored parameters.
2177 *
2178 * If it was a REQUEST SENSE command, we need some way to
2179 * differentiate between the failure code of the original
2180 * and the failure code of the REQUEST sense - the obvious
2181 * case is success, where we fall through and leave the
2182 * result code unchanged.
2183 *
2184 * The non-obvious place is where the REQUEST SENSE failed
2185 */
2186
2187 if (cmd->cmnd[0] != REQUEST_SENSE)
2188 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2189 else if (status_byte(cmd->SCp.Status) != GOOD)
2190 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2191
Boaz Harrosh28424d32007-09-10 22:37:45 +03002192 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2193 hostdata->ses.cmd_len) {
2194 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2195 hostdata->ses.cmd_len = 0 ;
2196 }
2197
Roman Zippelc28bda22007-05-01 22:32:36 +02002198 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2199 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002200 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002201
Finn Thaind65e6342014-03-18 11:42:20 +11002202 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002203
2204 local_irq_save(flags);
2205 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002206 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002207 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2208 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002209 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002210 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002211 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002212#ifdef NCR5380_STATS
2213 collect_stats(hostdata, cmd);
2214#endif
2215 cmd->scsi_done(cmd);
2216 }
2217
2218 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2219 /*
2220 * Restore phase bits to 0 so an interrupted selection,
2221 * arbitration can resume.
2222 */
2223 NCR5380_write(TARGET_COMMAND_REG, 0);
2224
2225 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2226 barrier();
2227
2228 falcon_dont_release--;
2229 /* ++roman: For Falcon SCSI, release the lock on the
2230 * ST-DMA here if no other commands are waiting on the
2231 * disconnected queue.
2232 */
2233 falcon_release_lock_if_possible(hostdata);
2234 return;
2235 case MESSAGE_REJECT:
2236 /* Accept message by clearing ACK */
2237 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2238 /* Enable reselect interrupts */
2239 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2240 switch (hostdata->last_message) {
2241 case HEAD_OF_QUEUE_TAG:
2242 case ORDERED_QUEUE_TAG:
2243 case SIMPLE_QUEUE_TAG:
2244 /* The target obviously doesn't support tagged
2245 * queuing, even though it announced this ability in
2246 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2247 * clear 'tagged_supported' and lock the LUN, since
2248 * the command is treated as untagged further on.
2249 */
2250 cmd->device->tagged_supported = 0;
2251 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2252 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002253 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002254 "QUEUE_TAG message; tagged queuing "
2255 "disabled\n",
2256 HOSTNO, cmd->device->id, cmd->device->lun);
2257 break;
2258 }
2259 break;
2260 case DISCONNECT:
2261 /* Accept message by clearing ACK */
2262 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2263 local_irq_save(flags);
2264 cmd->device->disconnect = 1;
2265 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002266 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002267 hostdata->connected = NULL;
2268 hostdata->disconnected_queue = cmd;
2269 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002270 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002271 "moved from connected to the "
2272 "disconnected_queue\n", HOSTNO,
2273 cmd->device->id, cmd->device->lun);
2274 /*
2275 * Restore phase bits to 0 so an interrupted selection,
2276 * arbitration can resume.
2277 */
2278 NCR5380_write(TARGET_COMMAND_REG, 0);
2279
2280 /* Enable reselect interrupts */
2281 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2282 /* Wait for bus free to avoid nasty timeouts */
2283 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2284 barrier();
2285 return;
2286 /*
2287 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2288 * operation, in violation of the SCSI spec so we can safely
2289 * ignore SAVE/RESTORE pointers calls.
2290 *
2291 * Unfortunately, some disks violate the SCSI spec and
2292 * don't issue the required SAVE_POINTERS message before
2293 * disconnecting, and we have to break spec to remain
2294 * compatible.
2295 */
2296 case SAVE_POINTERS:
2297 case RESTORE_POINTERS:
2298 /* Accept message by clearing ACK */
2299 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2300 /* Enable reselect interrupts */
2301 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2302 break;
2303 case EXTENDED_MESSAGE:
2304 /*
2305 * Extended messages are sent in the following format :
2306 * Byte
2307 * 0 EXTENDED_MESSAGE == 1
2308 * 1 length (includes one byte for code, doesn't
2309 * include first two bytes)
2310 * 2 code
2311 * 3..length+1 arguments
2312 *
2313 * Start the extended message buffer with the EXTENDED_MESSAGE
2314 * byte, since spi_print_msg() wants the whole thing.
2315 */
2316 extended_msg[0] = EXTENDED_MESSAGE;
2317 /* Accept first byte by clearing ACK */
2318 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2319
Finn Thaind65e6342014-03-18 11:42:20 +11002320 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002321
2322 len = 2;
2323 data = extended_msg + 1;
2324 phase = PHASE_MSGIN;
2325 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002326 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002327 (int)extended_msg[1], (int)extended_msg[2]);
2328
2329 if (!len && extended_msg[1] <=
2330 (sizeof(extended_msg) - 1)) {
2331 /* Accept third byte by clearing ACK */
2332 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2333 len = extended_msg[1] - 1;
2334 data = extended_msg + 3;
2335 phase = PHASE_MSGIN;
2336
2337 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002338 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002339 HOSTNO, len);
2340
2341 switch (extended_msg[2]) {
2342 case EXTENDED_SDTR:
2343 case EXTENDED_WDTR:
2344 case EXTENDED_MODIFY_DATA_POINTER:
2345 case EXTENDED_EXTENDED_IDENTIFY:
2346 tmp = 0;
2347 }
2348 } else if (len) {
2349 printk(KERN_NOTICE "scsi%d: error receiving "
2350 "extended message\n", HOSTNO);
2351 tmp = 0;
2352 } else {
2353 printk(KERN_NOTICE "scsi%d: extended message "
2354 "code %02x length %d is too long\n",
2355 HOSTNO, extended_msg[2], extended_msg[1]);
2356 tmp = 0;
2357 }
2358 /* Fall through to reject message */
2359
2360 /*
2361 * If we get something weird that we aren't expecting,
2362 * reject it.
2363 */
2364 default:
2365 if (!tmp) {
2366 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2367 spi_print_msg(extended_msg);
2368 printk("\n");
2369 } else if (tmp != EXTENDED_MESSAGE)
2370 printk(KERN_DEBUG "scsi%d: rejecting unknown "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002371 "message %02x from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002372 HOSTNO, tmp, cmd->device->id, cmd->device->lun);
2373 else
2374 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2375 "extended message "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002376 "code %02x, length %d from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002377 HOSTNO, extended_msg[1], extended_msg[0],
2378 cmd->device->id, cmd->device->lun);
2379
2380
2381 msgout = MESSAGE_REJECT;
2382 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2383 break;
2384 } /* switch (tmp) */
2385 break;
2386 case PHASE_MSGOUT:
2387 len = 1;
2388 data = &msgout;
2389 hostdata->last_message = msgout;
2390 NCR5380_transfer_pio(instance, &phase, &len, &data);
2391 if (msgout == ABORT) {
2392#ifdef SUPPORT_TAGS
2393 cmd_free_tag(cmd);
2394#else
2395 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2396#endif
2397 hostdata->connected = NULL;
2398 cmd->result = DID_ERROR << 16;
2399#ifdef NCR5380_STATS
2400 collect_stats(hostdata, cmd);
2401#endif
2402 cmd->scsi_done(cmd);
2403 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2404 falcon_release_lock_if_possible(hostdata);
2405 return;
2406 }
2407 msgout = NOP;
2408 break;
2409 case PHASE_CMDOUT:
2410 len = cmd->cmd_len;
2411 data = cmd->cmnd;
2412 /*
2413 * XXX for performance reasons, on machines with a
2414 * PSEUDO-DMA architecture we should probably
2415 * use the dma transfer function.
2416 */
2417 NCR5380_transfer_pio(instance, &phase, &len, &data);
2418 break;
2419 case PHASE_STATIN:
2420 len = 1;
2421 data = &tmp;
2422 NCR5380_transfer_pio(instance, &phase, &len, &data);
2423 cmd->SCp.Status = tmp;
2424 break;
2425 default:
2426 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002427 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002428 } /* switch(phase) */
2429 } /* if (tmp * SR_REQ) */
2430 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433/*
2434 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2435 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002436 * Purpose : does reselection, initializing the instance->connected
2437 * 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 -07002438 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002439 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 * Inputs : instance - this instance of the NCR5380.
2441 *
2442 */
2443
2444
Roman Zippelc28bda22007-05-01 22:32:36 +02002445static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Roman Zippelc28bda22007-05-01 22:32:36 +02002447 SETUP_HOSTDATA(instance);
2448 unsigned char target_mask;
2449 unsigned char lun, phase;
2450 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002452 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002454 unsigned char msg[3];
2455 unsigned char *data;
2456 Scsi_Cmnd *tmp = NULL, *prev;
2457/* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Roman Zippelc28bda22007-05-01 22:32:36 +02002459 /*
2460 * Disable arbitration, etc. since the host adapter obviously
2461 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Roman Zippelc28bda22007-05-01 22:32:36 +02002464 NCR5380_write(MODE_REG, MR_BASE);
2465 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Roman Zippelc28bda22007-05-01 22:32:36 +02002467 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2468
Finn Thaind65e6342014-03-18 11:42:20 +11002469 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002470
2471 /*
2472 * At this point, we have detected that our SCSI ID is on the bus,
2473 * SEL is true and BSY was false for at least one bus settle delay
2474 * (400 ns).
2475 *
2476 * We must assert BSY ourselves, until the target drops the SEL
2477 * signal.
2478 */
2479
2480 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2481
2482 while (NCR5380_read(STATUS_REG) & SR_SEL)
2483 ;
2484 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2485
2486 /*
2487 * Wait for target to go into MSGIN.
2488 */
2489
2490 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2491 ;
2492
2493 len = 1;
2494 data = msg;
2495 phase = PHASE_MSGIN;
2496 NCR5380_transfer_pio(instance, &phase, &len, &data);
2497
2498 if (!(msg[0] & 0x80)) {
2499 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2500 spi_print_msg(msg);
2501 do_abort(instance);
2502 return;
2503 }
2504 lun = (msg[0] & 0x07);
2505
2506#ifdef SUPPORT_TAGS
2507 /* If the phase is still MSGIN, the target wants to send some more
2508 * messages. In case it supports tagged queuing, this is probably a
2509 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2510 */
2511 tag = TAG_NONE;
2512 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2513 /* Accept previous IDENTIFY message by clearing ACK */
2514 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2515 len = 2;
2516 data = msg + 1;
2517 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2518 msg[1] == SIMPLE_QUEUE_TAG)
2519 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002520 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002521 "reselection\n", HOSTNO, target_mask, lun, tag);
2522 }
2523#endif
2524
2525 /*
2526 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2527 * just reestablished, and remove it from the disconnected queue.
2528 */
2529
2530 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2531 tmp; prev = tmp, tmp = NEXT(tmp)) {
2532 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2533#ifdef SUPPORT_TAGS
2534 && (tag == tmp->tag)
2535#endif
2536 ) {
2537 /* ++guenther: prevent race with falcon_release_lock */
2538 falcon_dont_release++;
2539 if (prev) {
2540 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002541 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002542 } else {
2543 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2544 hostdata->disconnected_queue = NEXT(tmp);
2545 }
Roman Zippel3130d902007-05-01 22:32:37 +02002546 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002547 break;
2548 }
2549 }
2550
2551 if (!tmp) {
2552 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2553#ifdef SUPPORT_TAGS
2554 "tag %d "
2555#endif
2556 "not in disconnected_queue.\n",
2557 HOSTNO, target_mask, lun
2558#ifdef SUPPORT_TAGS
2559 , tag
2560#endif
2561 );
2562 /*
2563 * Since we have an established nexus that we can't do anything
2564 * with, we must abort it.
2565 */
2566 do_abort(instance);
2567 return;
2568 }
2569
2570 /* Accept message by clearing ACK */
2571 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2572
2573 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002574 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002575 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
2576 falcon_dont_release--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579
2580/*
2581 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2582 *
2583 * Purpose : abort a command
2584 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002585 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2586 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 * used.
2588 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002589 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002591 * XXX - there is no way to abort the command that is currently
2592 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002594 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 */
2596
2597static
Roman Zippelc28bda22007-05-01 22:32:36 +02002598int NCR5380_abort(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Roman Zippelc28bda22007-05-01 22:32:36 +02002600 struct Scsi_Host *instance = cmd->device->host;
2601 SETUP_HOSTDATA(instance);
2602 Scsi_Cmnd *tmp, **prev;
2603 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002605 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Roman Zippelc28bda22007-05-01 22:32:36 +02002607 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Roman Zippelc28bda22007-05-01 22:32:36 +02002609 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Roman Zippelc28bda22007-05-01 22:32:36 +02002611 if (!IS_A_TT() && !falcon_got_lock)
2612 printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_abort\n",
2613 HOSTNO);
2614
Finn Thaind65e6342014-03-18 11:42:20 +11002615 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002616 NCR5380_read(BUS_AND_STATUS_REG),
2617 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002620 /*
2621 * Case 1 : If the command is the currently executing command,
2622 * we'll set the aborted flag and return control so that
2623 * information transfer routine can exit cleanly.
2624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Roman Zippelc28bda22007-05-01 22:32:36 +02002626 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Finn Thaind65e6342014-03-18 11:42:20 +11002628 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002629 /*
2630 * We should perform BSY checking, and make sure we haven't slipped
2631 * into BUS FREE.
2632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Roman Zippelc28bda22007-05-01 22:32:36 +02002634 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2635 /*
2636 * Since we can't change phases until we've completed the current
2637 * handshake, we have to source or sink a byte of data if the current
2638 * phase is not MSGOUT.
2639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Roman Zippelc28bda22007-05-01 22:32:36 +02002641 /*
2642 * Return control to the executing NCR drive so we can clear the
2643 * aborted flag and get back into our main loop.
2644 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Roman Zippelc28bda22007-05-01 22:32:36 +02002646 if (do_abort(instance) == 0) {
2647 hostdata->aborted = 1;
2648 hostdata->connected = NULL;
2649 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002651 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002653 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002655 local_irq_restore(flags);
2656 cmd->scsi_done(cmd);
2657 falcon_release_lock_if_possible(hostdata);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002658 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002659 } else {
2660/* local_irq_restore(flags); */
2661 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002662 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002666
2667 /*
2668 * Case 2 : If the command hasn't been issued yet, we simply remove it
2669 * from the issue queue.
2670 */
2671 for (prev = (Scsi_Cmnd **)&(hostdata->issue_queue),
2672 tmp = (Scsi_Cmnd *)hostdata->issue_queue;
2673 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2674 if (cmd == tmp) {
2675 REMOVE(5, *prev, tmp, NEXT(tmp));
2676 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002677 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002678 tmp->result = DID_ABORT << 16;
2679 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002680 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002681 HOSTNO);
2682 /* Tagged queuing note: no tag to free here, hasn't been assigned
2683 * yet... */
2684 tmp->scsi_done(tmp);
2685 falcon_release_lock_if_possible(hostdata);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002686 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688 }
2689
Roman Zippelc28bda22007-05-01 22:32:36 +02002690 /*
2691 * Case 3 : If any commands are connected, we're going to fail the abort
2692 * and let the high level SCSI driver retry at a later time or
2693 * issue a reset.
2694 *
2695 * Timeouts, and therefore aborted commands, will be highly unlikely
2696 * and handling them cleanly in this situation would make the common
2697 * case of noresets less efficient, and would pollute our code. So,
2698 * we fail.
2699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Roman Zippelc28bda22007-05-01 22:32:36 +02002701 if (hostdata->connected) {
2702 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002703 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002704 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Roman Zippelc28bda22007-05-01 22:32:36 +02002707 /*
2708 * Case 4: If the command is currently disconnected from the bus, and
2709 * there are no connected commands, we reconnect the I_T_L or
2710 * I_T_L_Q nexus associated with it, go into message out, and send
2711 * an abort message.
2712 *
2713 * This case is especially ugly. In order to reestablish the nexus, we
2714 * need to call NCR5380_select(). The easiest way to implement this
2715 * function was to abort if the bus was busy, and let the interrupt
2716 * handler triggered on the SEL for reselect take care of lost arbitrations
2717 * where necessary, meaning interrupts need to be enabled.
2718 *
2719 * When interrupts are enabled, the queues may change - so we
2720 * can't remove it from the disconnected queue before selecting it
2721 * because that could cause a failure in hashing the nexus if that
2722 * device reselected.
2723 *
2724 * Since the queues may change, we can't use the pointers from when we
2725 * first locate it.
2726 *
2727 * So, we must first locate the command, and if NCR5380_select()
2728 * succeeds, then issue the abort, relocate the command and remove
2729 * it from the disconnected queue.
2730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Roman Zippelc28bda22007-05-01 22:32:36 +02002732 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2733 tmp = NEXT(tmp)) {
2734 if (cmd == tmp) {
2735 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002736 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002737
Finn Thain76f13b92014-11-12 16:11:53 +11002738 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002739 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002740
Finn Thaind65e6342014-03-18 11:42:20 +11002741 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002742
2743 do_abort(instance);
2744
2745 local_irq_save(flags);
2746 for (prev = (Scsi_Cmnd **)&(hostdata->disconnected_queue),
2747 tmp = (Scsi_Cmnd *)hostdata->disconnected_queue;
2748 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2749 if (cmd == tmp) {
2750 REMOVE(5, *prev, tmp, NEXT(tmp));
2751 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002752 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002753 tmp->result = DID_ABORT << 16;
2754 /* We must unlock the tag/LUN immediately here, since the
2755 * target goes to BUS FREE and doesn't send us another
2756 * message (COMMAND_COMPLETE or the like)
2757 */
2758#ifdef SUPPORT_TAGS
2759 cmd_free_tag(tmp);
2760#else
2761 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2762#endif
2763 local_irq_restore(flags);
2764 tmp->scsi_done(tmp);
2765 falcon_release_lock_if_possible(hostdata);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002766 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002767 }
2768 }
2769 }
2770 }
2771
2772 /*
2773 * Case 5 : If we reached this point, the command was not found in any of
2774 * the queues.
2775 *
2776 * We probably reached this point because of an unlikely race condition
2777 * between the command completing successfully and the abortion code,
2778 * so we won't panic, but we will notify the user in case something really
2779 * broke.
2780 */
2781
2782 local_irq_restore(flags);
Joe Perchesad361c92009-07-06 13:05:40 -07002783 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002784
2785 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2786 * possible at all) At least, we should check if the lock could be
2787 * released after the abort, in case it is kept due to some bug.
2788 */
2789 falcon_release_lock_if_possible(hostdata);
2790
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002791 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794
Roman Zippelc28bda22007-05-01 22:32:36 +02002795/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002797 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 * Purpose : reset the SCSI bus.
2799 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002800 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Roman Zippelc28bda22007-05-01 22:32:36 +02002804static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Roman Zippelc28bda22007-05-01 22:32:36 +02002806 SETUP_HOSTDATA(cmd->device->host);
2807 int i;
2808 unsigned long flags;
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002809#if defined(RESET_RUN_DONE)
Roman Zippelc28bda22007-05-01 22:32:36 +02002810 Scsi_Cmnd *connected, *disconnected_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811#endif
2812
Roman Zippelc28bda22007-05-01 22:32:36 +02002813 if (!IS_A_TT() && !falcon_got_lock)
2814 printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_reset\n",
2815 H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Roman Zippelc28bda22007-05-01 22:32:36 +02002817 NCR5380_print_status(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Roman Zippelc28bda22007-05-01 22:32:36 +02002819 /* get in phase */
2820 NCR5380_write(TARGET_COMMAND_REG,
2821 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2822 /* assert RST */
2823 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2824 udelay(40);
2825 /* reset NCR registers */
2826 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2827 NCR5380_write(MODE_REG, MR_BASE);
2828 NCR5380_write(TARGET_COMMAND_REG, 0);
2829 NCR5380_write(SELECT_ENABLE_REG, 0);
2830 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2831 * through anymore ... */
2832 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002834 /* MSch 20140115 - looking at the generic NCR5380 driver, all of this
2835 * should go.
2836 * Catch-22: if we don't clear all queues, the SCSI driver lock will
2837 * not be reset by atari_scsi_reset()!
2838 */
2839
2840#if defined(RESET_RUN_DONE)
2841 /* XXX Should now be done by midlevel code, but it's broken XXX */
Roman Zippelc28bda22007-05-01 22:32:36 +02002842 /* XXX see below XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Roman Zippelc28bda22007-05-01 22:32:36 +02002844 /* MSch: old-style reset: actually abort all command processing here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Roman Zippelc28bda22007-05-01 22:32:36 +02002846 /* After the reset, there are no more connected or disconnected commands
2847 * and no busy units; to avoid problems with re-inserting the commands
2848 * into the issue_queue (via scsi_done()), the aborted commands are
2849 * remembered in local variables first.
2850 */
2851 local_irq_save(flags);
2852 connected = (Scsi_Cmnd *)hostdata->connected;
2853 hostdata->connected = NULL;
2854 disconnected_queue = (Scsi_Cmnd *)hostdata->disconnected_queue;
2855 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002857 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002859 for (i = 0; i < 8; ++i)
2860 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002862 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002864 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Roman Zippelc28bda22007-05-01 22:32:36 +02002866 /* In order to tell the mid-level code which commands were aborted,
2867 * set the command status to DID_RESET and call scsi_done() !!!
2868 * This ultimately aborts processing of these commands in the mid-level.
2869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Roman Zippelc28bda22007-05-01 22:32:36 +02002871 if ((cmd = connected)) {
Finn Thaind65e6342014-03-18 11:42:20 +11002872 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002873 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2874 cmd->scsi_done(cmd);
2875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Roman Zippelc28bda22007-05-01 22:32:36 +02002877 for (i = 0; (cmd = disconnected_queue); ++i) {
2878 disconnected_queue = NEXT(cmd);
Roman Zippel3130d902007-05-01 22:32:37 +02002879 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002880 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2881 cmd->scsi_done(cmd);
2882 }
2883 if (i > 0)
Finn Thaind65e6342014-03-18 11:42:20 +11002884 dprintk(NDEBUG_ABORT, "scsi: reset aborted %d disconnected command(s)\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Roman Zippelc28bda22007-05-01 22:32:36 +02002886 /* The Falcon lock should be released after a reset...
2887 */
2888 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2889 * unlocking and enabling dma interrupt.
2890 */
2891/* falcon_release_lock_if_possible( hostdata );*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Roman Zippelc28bda22007-05-01 22:32:36 +02002893 /* since all commands have been explicitly terminated, we need to tell
2894 * the midlevel code that the reset was SUCCESSFUL, and there is no
2895 * need to 'wake up' the commands by a request_sense
2896 */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002897 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898#else /* 1 */
2899
Roman Zippelc28bda22007-05-01 22:32:36 +02002900 /* MSch: new-style reset handling: let the mid-level do what it can */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Roman Zippelc28bda22007-05-01 22:32:36 +02002902 /* ++guenther: MID-LEVEL IS STILL BROKEN.
2903 * Mid-level is supposed to requeue all commands that were active on the
2904 * various low-level queues. In fact it does this, but that's not enough
2905 * because all these commands are subject to timeout. And if a timeout
2906 * happens for any removed command, *_abort() is called but all queues
2907 * are now empty. Abort then gives up the falcon lock, which is fatal,
2908 * since the mid-level will queue more commands and must have the lock
2909 * (it's all happening inside timer interrupt handler!!).
2910 * Even worse, abort will return NOT_RUNNING for all those commands not
2911 * on any queue, so they won't be retried ...
2912 *
2913 * Conclusion: either scsi.c disables timeout for all resetted commands
2914 * immediately, or we lose! As of linux-2.0.20 it doesn't.
2915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Roman Zippelc28bda22007-05-01 22:32:36 +02002917 /* After the reset, there are no more connected or disconnected commands
2918 * and no busy units; so clear the low-level status here to avoid
2919 * conflicts when the mid-level code tries to wake up the affected
2920 * commands!
2921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Roman Zippelc28bda22007-05-01 22:32:36 +02002923 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002924 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002925 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002926 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002927 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002928 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Roman Zippelc28bda22007-05-01 22:32:36 +02002930 local_irq_save(flags);
2931 hostdata->issue_queue = NULL;
2932 hostdata->connected = NULL;
2933 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002935 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002937 for (i = 0; i < 8; ++i)
2938 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002940 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002942 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Roman Zippelc28bda22007-05-01 22:32:36 +02002944 /* we did no complete reset of all commands, so a wakeup is required */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002945 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946#endif /* 1 */
2947}