blob: 3cfd39c583ad54277cd43bf9a42dcc16e2d19395 [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"
686#ifdef AUTOSENSE
687 " AUTOSENSE"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688#endif
689#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200690 " REAL DMA"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691#endif
692#ifdef PARITY
Roman Zippelc28bda22007-05-01 22:32:36 +0200693 " PARITY"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#endif
695#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200696 " SCSI-2 TAGGED QUEUING"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200698 );
699 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
703 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
704 *
705 * Purpose : print commands in the various queues, called from
706 * NCR5380_abort and NCR5380_debug to aid debugging.
707 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200708 * Inputs : instance, pointer to this instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710
Al Virod89537e2013-03-31 13:24:44 -0400711static void lprint_Scsi_Cmnd(Scsi_Cmnd *cmd)
712{
713 int i, s;
714 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200715 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400716 H_NO(cmd), cmd->device->id, cmd->device->lun);
717 printk(KERN_CONT " command = ");
718 command = cmd->cmnd;
719 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
720 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
721 printk(KERN_CONT " %02x", command[i]);
722 printk("\n");
723}
724
Roman Zippelc28bda22007-05-01 22:32:36 +0200725static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Al Virod89537e2013-03-31 13:24:44 -0400727 struct NCR5380_hostdata *hostdata;
728 Scsi_Cmnd *ptr;
729 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Finn Thain8ad3a592014-03-18 11:42:19 +1100731 NCR5380_dprint(NDEBUG_ANY, instance);
732 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Roman Zippelc28bda22007-05-01 22:32:36 +0200734 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Al Virod89537e2013-03-31 13:24:44 -0400736 printk("\nNCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
Roman Zippelc28bda22007-05-01 22:32:36 +0200737 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400738 printk("NCR5380: coroutine is%s running.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200739 main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200740 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400741 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200742 else
Al Virod89537e2013-03-31 13:24:44 -0400743 lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected);
744 printk("scsi%d: issue_queue\n", HOSTNO);
745 for (ptr = (Scsi_Cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
746 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Al Virod89537e2013-03-31 13:24:44 -0400748 printk("scsi%d: disconnected_queue\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200749 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400750 ptr = NEXT(ptr))
751 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200752
753 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400754 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Al Virod89537e2013-03-31 13:24:44 -0400757static void show_Scsi_Cmnd(Scsi_Cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Roman Zippelc28bda22007-05-01 22:32:36 +0200759 int i, s;
760 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200761 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200762 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400763 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200764 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400765 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200766 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400767 seq_printf(m, " %02x", command[i]);
768 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Finn Thain4d3d2a52014-11-12 16:11:52 +1100771static int __maybe_unused NCR5380_show_info(struct seq_file *m,
772 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400773{
774 struct NCR5380_hostdata *hostdata;
775 Scsi_Cmnd *ptr;
776 unsigned long flags;
777
778 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
779
780 seq_printf(m, "NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
781 local_irq_save(flags);
782 seq_printf(m, "NCR5380: coroutine is%s running.\n",
783 main_running ? "" : "n't");
784 if (!hostdata->connected)
785 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
786 else
787 show_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, m);
788 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
789 for (ptr = (Scsi_Cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
790 show_Scsi_Cmnd(ptr, m);
791
792 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
793 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
794 ptr = NEXT(ptr))
795 show_Scsi_Cmnd(ptr, m);
796
797 local_irq_restore(flags);
798 return 0;
799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Roman Zippelc28bda22007-05-01 22:32:36 +0200801/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * Function : void NCR5380_init (struct Scsi_Host *instance)
803 *
804 * Purpose : initializes *instance and corresponding 5380 chip.
805 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200806 * Inputs : instance - instantiation of the 5380 driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
808 * Notes : I assume that the host, hostno, and id bits have been
Roman Zippelc28bda22007-05-01 22:32:36 +0200809 * set correctly. I don't care about the irq and other fields.
810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
812
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100813static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Roman Zippelc28bda22007-05-01 22:32:36 +0200815 int i;
816 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Roman Zippelc28bda22007-05-01 22:32:36 +0200818 NCR5380_all_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Roman Zippelc28bda22007-05-01 22:32:36 +0200820 hostdata->aborted = 0;
821 hostdata->id_mask = 1 << instance->this_id;
822 hostdata->id_higher_mask = 0;
823 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
824 if (i > hostdata->id_mask)
825 hostdata->id_higher_mask |= i;
826 for (i = 0; i < 8; ++i)
827 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200829 init_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#endif
831#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200832 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200834 hostdata->targets_present = 0;
835 hostdata->connected = NULL;
836 hostdata->issue_queue = NULL;
837 hostdata->disconnected_queue = NULL;
838 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Roman Zippelc28bda22007-05-01 22:32:36 +0200840 if (!the_template) {
841 the_template = instance->hostt;
842 first_instance = instance;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845#ifndef AUTOSENSE
Roman Zippelc28bda22007-05-01 22:32:36 +0200846 if ((instance->cmd_per_lun > 1) || (instance->can_queue > 1))
847 printk("scsi%d: WARNING : support for multiple outstanding commands enabled\n"
848 " without AUTOSENSE option, contingent allegiance conditions may\n"
849 " be incorrectly cleared.\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850#endif /* def AUTOSENSE */
851
Roman Zippelc28bda22007-05-01 22:32:36 +0200852 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
853 NCR5380_write(MODE_REG, MR_BASE);
854 NCR5380_write(TARGET_COMMAND_REG, 0);
855 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Roman Zippelc28bda22007-05-01 22:32:36 +0200857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200860static void NCR5380_exit(struct Scsi_Host *instance)
861{
862 /* Empty, as we didn't schedule any delayed work */
863}
864
Roman Zippelc28bda22007-05-01 22:32:36 +0200865/*
Roman Zippelc28bda22007-05-01 22:32:36 +0200866 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
867 * void (*done)(Scsi_Cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 *
869 * Purpose : enqueues a SCSI command
870 *
871 * Inputs : cmd - SCSI command, done - function called on completion, with
872 * a pointer to the command descriptor.
Roman Zippelc28bda22007-05-01 22:32:36 +0200873 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * Returns : 0
875 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200876 * Side effects :
877 * cmd is added to the per instance issue_queue, with minor
878 * twiddling done to the host specific fields of cmd. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * main coroutine is not running, it is restarted.
880 *
881 */
882
Jeff Garzikf2812332010-11-16 02:10:29 -0500883static int NCR5380_queue_command_lck(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Roman Zippelc28bda22007-05-01 22:32:36 +0200885 SETUP_HOSTDATA(cmd->device->host);
886 Scsi_Cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200887 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200890 switch (cmd->cmnd[0]) {
891 case WRITE_6:
892 case WRITE_10:
893 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
894 H_NO(cmd));
895 cmd->result = (DID_ERROR << 16);
896 done(cmd);
897 return 0;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#ifdef NCR5380_STATS
902# if 0
Roman Zippelc28bda22007-05-01 22:32:36 +0200903 if (!hostdata->connected && !hostdata->issue_queue &&
904 !hostdata->disconnected_queue) {
905 hostdata->timebase = jiffies;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907# endif
908# ifdef NCR5380_STAT_LIMIT
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200909 if (scsi_bufflen(cmd) > NCR5380_STAT_LIMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910# endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200911 switch (cmd->cmnd[0]) {
912 case WRITE:
913 case WRITE_6:
914 case WRITE_10:
915 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200916 hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200917 hostdata->pendingw++;
918 break;
919 case READ:
920 case READ_6:
921 case READ_10:
922 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200923 hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200924 hostdata->pendingr++;
925 break;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#endif
928
Roman Zippelc28bda22007-05-01 22:32:36 +0200929 /*
930 * We use the host_scribble field as a pointer to the next command
931 * in a queue
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Roman Zippel3130d902007-05-01 22:32:37 +0200934 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200935 cmd->scsi_done = done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Roman Zippelc28bda22007-05-01 22:32:36 +0200937 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Roman Zippelc28bda22007-05-01 22:32:36 +0200939 /*
940 * Insert the cmd into the issue queue. Note that REQUEST SENSE
941 * commands are added to the head of the queue since any command will
942 * clear the contingent allegiance condition that exists and the
943 * sense data is only guaranteed to be valid while the condition exists.
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Roman Zippelc28bda22007-05-01 22:32:36 +0200946 local_irq_save(flags);
947 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
948 * Otherwise a running NCR5380_main may steal the lock.
949 * Lock before actually inserting due to fairness reasons explained in
950 * atari_scsi.c. If we insert first, then it's impossible for this driver
951 * to release the lock.
952 * Stop timer for this command while waiting for the lock, or timeouts
953 * may happen (and they really do), and it's no good if the command doesn't
954 * appear in any of the queues.
955 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
956 * because also a timer int can trigger an abort or reset, which would
957 * alter queues and touch the lock.
958 */
959 if (!IS_A_TT()) {
Michael Schmitz8ce79552007-06-03 12:55:04 +0200960 /* perhaps stop command timer here */
Roman Zippelc28bda22007-05-01 22:32:36 +0200961 falcon_get_lock();
Michael Schmitz8ce79552007-06-03 12:55:04 +0200962 /* perhaps restart command timer here */
Roman Zippelc28bda22007-05-01 22:32:36 +0200963 }
964 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
965 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200966 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200967 hostdata->issue_queue = cmd;
968 } else {
969 for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
970 NEXT(tmp); tmp = NEXT(tmp))
971 ;
972 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200973 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200974 }
975 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Finn Thaind65e6342014-03-18 11:42:20 +1100977 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200978 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Roman Zippelc28bda22007-05-01 22:32:36 +0200980 /* If queue_command() is called from an interrupt (real one or bottom
981 * half), we let queue_main() do the job of taking care about main. If it
982 * is already running, this is a no-op, else main will be queued.
983 *
984 * If we're not in an interrupt, we can call NCR5380_main()
985 * unconditionally, because it cannot be already running.
986 */
987 if (in_interrupt() || ((flags >> 8) & 7) >= 6)
988 queue_main();
989 else
990 NCR5380_main(NULL);
991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Jeff Garzikf2812332010-11-16 02:10:29 -0500994static DEF_SCSI_QCMD(NCR5380_queue_command)
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
Roman Zippelc28bda22007-05-01 22:32:36 +0200997 * Function : NCR5380_main (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200999 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1000 * be done on the NCR5380 host adapters in a system. Both
1001 * NCR5380_queue_command() and NCR5380_intr() will try to start it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +02001003 *
1004 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * reenable them. This prevents reentrancy and kernel stack overflow.
Roman Zippelc28bda22007-05-01 22:32:36 +02001006 */
1007
Geert Uytterhoevenb312b382007-05-01 22:32:48 +02001008static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Roman Zippelc28bda22007-05-01 22:32:36 +02001010 Scsi_Cmnd *tmp, *prev;
1011 struct Scsi_Host *instance = first_instance;
1012 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
1013 int done;
1014 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Roman Zippelc28bda22007-05-01 22:32:36 +02001016 /*
1017 * We run (with interrupts disabled) until we're sure that none of
1018 * the host adapters have anything that can be done, at which point
1019 * we set main_running to 0 and exit.
1020 *
1021 * Interrupts are enabled before doing various other internal
1022 * instructions, after we've decided that we need to run through
1023 * the loop again.
1024 *
1025 * this should prevent any race conditions.
1026 *
1027 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1028 * because also a timer int can trigger an abort or reset, which can
1029 * alter queues and touch the Falcon lock.
1030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Roman Zippelc28bda22007-05-01 22:32:36 +02001032 /* Tell int handlers main() is now already executing. Note that
1033 no races are possible here. If an int comes in before
1034 'main_running' is set here, and queues/executes main via the
1035 task queue, it doesn't do any harm, just this instance of main
1036 won't find any work left to do. */
1037 if (main_running)
1038 return;
1039 main_running = 1;
1040
1041 local_save_flags(flags);
1042 do {
1043 local_irq_disable(); /* Freeze request queues */
1044 done = 1;
1045
1046 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001047 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001048 /*
1049 * Search through the issue_queue for a command destined
1050 * for a target that's not busy.
1051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +02001053 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
1054 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1055 ;
1056 /*printk("%p ", tmp);*/
1057 if ((tmp == prev) && tmp)
1058 printk(" LOOP\n");
1059 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001061 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1062 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001063 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +02001066 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001067 printk("MAIN tmp=%p target=%d busy=%d lun=%llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001068 tmp, tmp->device->id, hostdata->busy[tmp->device->id],
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001069 lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001071 /* When we find one, remove it from the issue queue. */
1072 /* ++guenther: possible race with Falcon locking */
1073 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001075 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001077 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001078#endif
1079 ) {
1080 /* ++guenther: just to be sure, this must be atomic */
1081 local_irq_disable();
1082 if (prev) {
1083 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001084 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001085 } else {
1086 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1087 hostdata->issue_queue = NEXT(tmp);
1088 }
Roman Zippel3130d902007-05-01 22:32:37 +02001089 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02001090 falcon_dont_release++;
1091
1092 /* reenable interrupts after finding one */
1093 local_irq_restore(flags);
1094
1095 /*
1096 * Attempt to establish an I_T_L nexus here.
1097 * On success, instance->hostdata->connected is set.
1098 * On failure, we must add the command back to the
1099 * issue queue so we can keep trying.
1100 */
Finn Thaind65e6342014-03-18 11:42:20 +11001101 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001102 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001103 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001104 /*
1105 * REQUEST SENSE commands are issued without tagged
1106 * queueing, even on SCSI-II devices because the
1107 * contingent allegiance condition exists for the
1108 * entire unit.
1109 */
1110 /* ++roman: ...and the standard also requires that
1111 * REQUEST SENSE command are untagged.
1112 */
1113
1114#ifdef SUPPORT_TAGS
1115 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1116#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001117 if (!NCR5380_select(instance, tmp)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001118 falcon_dont_release--;
1119 /* release if target did not response! */
1120 falcon_release_lock_if_possible(hostdata);
1121 break;
1122 } else {
1123 local_irq_disable();
1124 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001125 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001126 hostdata->issue_queue = tmp;
1127#ifdef SUPPORT_TAGS
1128 cmd_free_tag(tmp);
1129#endif
1130 falcon_dont_release--;
1131 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001132 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001133 "returned to issue_queue\n", HOSTNO);
1134 if (hostdata->connected)
1135 break;
1136 }
1137 } /* if target/lun/target queue is not busy */
1138 } /* for issue_queue */
1139 } /* if (!hostdata->connected) */
1140
1141 if (hostdata->connected
1142#ifdef REAL_DMA
1143 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144#endif
1145 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001147 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001148 HOSTNO);
1149 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001150 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001151 done = 0;
1152 }
1153 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Roman Zippelc28bda22007-05-01 22:32:36 +02001155 /* Better allow ints _after_ 'main_running' has been cleared, else
1156 an interrupt could believe we'll pick up the work it left for
1157 us, but we won't see it anymore here... */
1158 main_running = 0;
1159 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162
1163#ifdef REAL_DMA
1164/*
1165 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1166 *
1167 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001168 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 *
1170 * Inputs : instance - this instance of the NCR5380.
1171 *
1172 */
1173
Roman Zippelc28bda22007-05-01 22:32:36 +02001174static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Roman Zippelc28bda22007-05-01 22:32:36 +02001176 SETUP_HOSTDATA(instance);
1177 int transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1178 unsigned char **data, p;
1179 volatile int *count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Roman Zippelc28bda22007-05-01 22:32:36 +02001181 if (!hostdata->connected) {
1182 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1183 "no connected cmd\n", HOSTNO);
1184 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Roman Zippelc28bda22007-05-01 22:32:36 +02001187 if (atari_read_overruns) {
1188 p = hostdata->connected->SCp.phase;
1189 if (p & SR_IO) {
1190 udelay(10);
1191 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1192 (BASR_PHASE_MATCH|BASR_ACK)) ==
1193 (BASR_PHASE_MATCH|BASR_ACK)) {
1194 saved_data = NCR5380_read(INPUT_DATA_REG);
1195 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001196 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001197 }
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001200
Finn Thaind65e6342014-03-18 11:42:20 +11001201 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001202 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1203 NCR5380_read(STATUS_REG));
1204
1205 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1206 NCR5380_write(MODE_REG, MR_BASE);
1207 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1208
1209 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1210 hostdata->dma_len = 0;
1211
1212 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1213 count = &hostdata->connected->SCp.this_residual;
1214 *data += transfered;
1215 *count -= transfered;
1216
1217 if (atari_read_overruns) {
1218 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1219 cnt = toPIO = atari_read_overruns;
1220 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001221 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001222 *(*data)++ = saved_data;
1223 (*count)--;
1224 cnt--;
1225 toPIO--;
1226 }
Finn Thaind65e6342014-03-18 11:42:20 +11001227 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001228 NCR5380_transfer_pio(instance, &p, &cnt, data);
1229 *count -= toPIO - cnt;
1230 }
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233#endif /* REAL_DMA */
1234
1235
1236/*
1237 * Function : void NCR5380_intr (int irq)
Roman Zippelc28bda22007-05-01 22:32:36 +02001238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
Roman Zippelc28bda22007-05-01 22:32:36 +02001240 * from the disconnected queue, and restarting NCR5380_main()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * as required.
1242 *
1243 * Inputs : int irq, irq that caused this interrupt.
1244 *
1245 */
1246
Roman Zippelc28bda22007-05-01 22:32:36 +02001247static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Roman Zippelc28bda22007-05-01 22:32:36 +02001249 struct Scsi_Host *instance = first_instance;
1250 int done = 1, handled = 0;
1251 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Finn Thaind65e6342014-03-18 11:42:20 +11001253 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Roman Zippelc28bda22007-05-01 22:32:36 +02001255 /* Look for pending interrupts */
1256 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001257 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001258 /* dispatch to appropriate routine if found and done=0 */
1259 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001260 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001261 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1262 done = 0;
1263 ENABLE_IRQ();
Finn Thaind65e6342014-03-18 11:42:20 +11001264 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001265 NCR5380_reselect(instance);
1266 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1267 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001268 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001269 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1270 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001271 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001272 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1273 } else {
1274 /*
1275 * The rest of the interrupt conditions can occur only during a
1276 * DMA transfer
1277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001280 /*
1281 * We should only get PHASE MISMATCH and EOP interrupts if we have
1282 * DMA enabled, so do a sanity check based on the current setting
1283 * of the MODE register.
1284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Roman Zippelc28bda22007-05-01 22:32:36 +02001286 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1287 ((basr & BASR_END_DMA_TRANSFER) ||
1288 !(basr & BASR_PHASE_MATCH))) {
1289
Finn Thaind65e6342014-03-18 11:42:20 +11001290 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001291 NCR5380_dma_complete( instance );
1292 done = 0;
1293 ENABLE_IRQ();
1294 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001296 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001298 if (basr & BASR_PHASE_MATCH)
1299 printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1300 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1301 HOSTNO, basr, NCR5380_read(MODE_REG),
1302 NCR5380_read(STATUS_REG));
1303 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1304 }
1305 } /* if !(SELECTION || PARITY) */
1306 handled = 1;
1307 } /* BASR & IRQ */ else {
1308 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1309 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1310 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1311 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1312 }
1313
1314 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001315 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001316 /* Put a call to NCR5380_main() on the queue... */
1317 queue_main();
1318 }
1319 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322#ifdef NCR5380_STATS
Roman Zippelc28bda22007-05-01 22:32:36 +02001323static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325# ifdef NCR5380_STAT_LIMIT
Boaz Harrosh9e0fe442007-11-05 11:23:35 +02001326 if (scsi_bufflen(cmd) > NCR5380_STAT_LIMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327# endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001328 switch (cmd->cmnd[0]) {
1329 case WRITE:
1330 case WRITE_6:
1331 case WRITE_10:
1332 hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +02001333 /*hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);*/
Roman Zippelc28bda22007-05-01 22:32:36 +02001334 hostdata->pendingw--;
1335 break;
1336 case READ:
1337 case READ_6:
1338 case READ_10:
1339 hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +02001340 /*hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);*/
Roman Zippelc28bda22007-05-01 22:32:36 +02001341 hostdata->pendingr--;
1342 break;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345#endif
1346
Roman Zippelc28bda22007-05-01 22:32:36 +02001347/*
Finn Thain76f13b92014-11-12 16:11:53 +11001348 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 *
1350 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001351 * including ARBITRATION, SELECTION, and initial message out for
1352 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001354 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001355 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001357 * Returns : -1 if selection could not execute for some reason,
1358 * 0 if selection succeeded or failed because the target
1359 * did not respond.
1360 *
1361 * Side effects :
1362 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * with registers as they should have been on entry - ie
1364 * SELECT_ENABLE will be set appropriately, the NCR5380
1365 * will cease to drive any SCSI bus signals.
1366 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001367 * If successful : I_T_L or I_T_L_Q nexus will be established,
1368 * instance->connected will be set to cmd.
1369 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001371 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * cmd->result host byte set to DID_BAD_TARGET.
1373 */
1374
Finn Thain76f13b92014-11-12 16:11:53 +11001375static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Roman Zippelc28bda22007-05-01 22:32:36 +02001377 SETUP_HOSTDATA(instance);
1378 unsigned char tmp[3], phase;
1379 unsigned char *data;
1380 int len;
1381 unsigned long timeout;
1382 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Roman Zippelc28bda22007-05-01 22:32:36 +02001384 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001385 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001386 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001387 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Roman Zippelc28bda22007-05-01 22:32:36 +02001389 /*
1390 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1391 * data bus during SELECTION.
1392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Roman Zippelc28bda22007-05-01 22:32:36 +02001394 local_irq_save(flags);
1395 if (hostdata->connected) {
1396 local_irq_restore(flags);
1397 return -1;
1398 }
1399 NCR5380_write(TARGET_COMMAND_REG, 0);
1400
1401 /*
1402 * Start arbitration.
1403 */
1404
1405 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1406 NCR5380_write(MODE_REG, MR_ARBITRATE);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Roman Zippelc28bda22007-05-01 22:32:36 +02001410 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001411#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 {
1413 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Roman Zippelc28bda22007-05-01 22:32:36 +02001415 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1416 time_before(jiffies, timeout) && !hostdata->connected)
1417 ;
1418 if (time_after_eq(jiffies, timeout)) {
1419 printk("scsi : arbitration timeout at %d\n", __LINE__);
1420 NCR5380_write(MODE_REG, MR_BASE);
1421 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1422 return -1;
1423 }
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001426 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1427 !hostdata->connected)
1428 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429#endif
1430
Finn Thaind65e6342014-03-18 11:42:20 +11001431 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Roman Zippelc28bda22007-05-01 22:32:36 +02001433 if (hostdata->connected) {
1434 NCR5380_write(MODE_REG, MR_BASE);
1435 return -1;
1436 }
1437 /*
1438 * The arbitration delay is 2.2us, but this is a minimum and there is
1439 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1440 * the integral nature of udelay().
1441 *
1442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Roman Zippelc28bda22007-05-01 22:32:36 +02001444 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Roman Zippelc28bda22007-05-01 22:32:36 +02001446 /* Check for lost arbitration */
1447 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1448 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1449 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1450 hostdata->connected) {
1451 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001452 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001453 HOSTNO);
1454 return -1;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Roman Zippelc28bda22007-05-01 22:32:36 +02001457 /* after/during arbitration, BSY should be asserted.
1458 IBM DPES-31080 Version S31Q works now */
1459 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1460 NCR5380_write(INITIATOR_COMMAND_REG,
1461 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1464 hostdata->connected) {
1465 NCR5380_write(MODE_REG, MR_BASE);
1466 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001467 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 HOSTNO);
1469 return -1;
1470 }
1471
1472 /*
1473 * Again, bus clear + bus settle time is 1.2us, however, this is
1474 * a minimum so we'll udelay ceil(1.2)
1475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001478 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1479 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001481 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001483
1484 if (hostdata->connected) {
1485 NCR5380_write(MODE_REG, MR_BASE);
1486 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1487 return -1;
1488 }
1489
Finn Thaind65e6342014-03-18 11:42:20 +11001490 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001491
1492 /*
1493 * Now that we have won arbitration, start Selection process, asserting
1494 * the host and target ID's on the SCSI bus.
1495 */
1496
1497 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1498
1499 /*
1500 * Raise ATN while SEL is true before BSY goes false from arbitration,
1501 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1502 * phase immediately after selection.
1503 */
1504
1505 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1506 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Roman Zippelc28bda22007-05-01 22:32:36 +02001509 /*
1510 * Reselect interrupts must be turned off prior to the dropping of BSY,
1511 * otherwise we will trigger an interrupt.
1512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 if (hostdata->connected) {
1515 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1516 return -1;
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Roman Zippelc28bda22007-05-01 22:32:36 +02001519 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Roman Zippelc28bda22007-05-01 22:32:36 +02001521 /*
1522 * The initiator shall then wait at least two deskew delays and release
1523 * the BSY signal.
1524 */
1525 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Roman Zippelc28bda22007-05-01 22:32:36 +02001527 /* Reset BSY */
1528 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1529 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Roman Zippelc28bda22007-05-01 22:32:36 +02001531 /*
1532 * Something weird happens when we cease to drive BSY - looks
1533 * like the board/chip is letting us do another read before the
1534 * appropriate propagation delay has expired, and we're confusing
1535 * a BSY signal from ourselves as the target's response to SELECTION.
1536 *
1537 * A small delay (the 'C++' frontend breaks the pipeline with an
1538 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1539 * tighter 'C' code breaks and requires this) solves the problem -
1540 * the 1 us delay is arbitrary, and only used because this delay will
1541 * be the same on other platforms and since it works here, it should
1542 * work there.
1543 *
1544 * wingel suggests that this could be due to failing to wait
1545 * one deskew delay.
1546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Roman Zippelc28bda22007-05-01 22:32:36 +02001548 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Finn Thaind65e6342014-03-18 11:42:20 +11001550 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Roman Zippelc28bda22007-05-01 22:32:36 +02001552 /*
1553 * The SCSI specification calls for a 250 ms timeout for the actual
1554 * selection.
1555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Roman Zippelc28bda22007-05-01 22:32:36 +02001557 timeout = jiffies + 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Roman Zippelc28bda22007-05-01 22:32:36 +02001559 /*
1560 * XXX very interesting - we're seeing a bounce where the BSY we
1561 * asserted is being reflected / still asserted (propagation delay?)
1562 * and it's detecting as true. Sigh.
1563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001566 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1567 * IO while SEL is true. But again, there are some disks out the in the
1568 * world that do that nevertheless. (Somebody claimed that this announces
1569 * reselection capability of the target.) So we better skip that test and
1570 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Roman Zippelc28bda22007-05-01 22:32:36 +02001573 while (time_before(jiffies, timeout) &&
1574 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1575 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Roman Zippelc28bda22007-05-01 22:32:36 +02001577 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1578 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1579 NCR5380_reselect(instance);
1580 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1581 HOSTNO);
1582 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1583 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001586 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1587 ;
1588#endif
1589
1590 /*
1591 * No less than two deskew delays after the initiator detects the
1592 * BSY signal is true, it shall release the SEL signal and may
1593 * change the DATA BUS. -wingel
1594 */
1595
1596 udelay(1);
1597
1598 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1599
1600 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1601 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1602 if (hostdata->targets_present & (1 << cmd->device->id)) {
1603 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1604 if (hostdata->restart_select)
1605 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001606 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001607 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1608 return -1;
1609 }
1610 cmd->result = DID_BAD_TARGET << 16;
1611#ifdef NCR5380_STATS
1612 collect_stats(hostdata, cmd);
1613#endif
1614#ifdef SUPPORT_TAGS
1615 cmd_free_tag(cmd);
1616#endif
1617 cmd->scsi_done(cmd);
1618 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001619 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001620 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1621 return 0;
1622 }
1623
1624 hostdata->targets_present |= (1 << cmd->device->id);
1625
1626 /*
1627 * Since we followed the SCSI spec, and raised ATN while SEL
1628 * was true but before BSY was false during selection, the information
1629 * transfer phase should be a MESSAGE OUT phase so that we can send the
1630 * IDENTIFY message.
1631 *
1632 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1633 * message (2 bytes) with a tag ID that we increment with every command
1634 * until it wraps back to 0.
1635 *
1636 * XXX - it turns out that there are some broken SCSI-II devices,
1637 * which claim to support tagged queuing but fail when more than
1638 * some number of commands are issued at once.
1639 */
1640
1641 /* Wait for start of REQ/ACK handshake */
1642 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1643 ;
1644
Finn Thaind65e6342014-03-18 11:42:20 +11001645 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001646 HOSTNO, cmd->device->id);
1647 tmp[0] = IDENTIFY(1, cmd->device->lun);
1648
1649#ifdef SUPPORT_TAGS
1650 if (cmd->tag != TAG_NONE) {
1651 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1652 tmp[2] = cmd->tag;
1653 len = 3;
1654 } else
1655 len = 1;
1656#else
1657 len = 1;
1658 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659#endif /* SUPPORT_TAGS */
1660
Roman Zippelc28bda22007-05-01 22:32:36 +02001661 /* Send message(s) */
1662 data = tmp;
1663 phase = PHASE_MSGOUT;
1664 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001665 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001666 /* XXX need to handle errors here */
1667 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001669 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1670#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Roman Zippelc28bda22007-05-01 22:32:36 +02001672 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Roman Zippelc28bda22007-05-01 22:32:36 +02001674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Roman Zippelc28bda22007-05-01 22:32:36 +02001677/*
1678 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * unsigned char *phase, int *count, unsigned char **data)
1680 *
1681 * Purpose : transfers data in given phase using polled I/O
1682 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001683 * Inputs : instance - instance of driver, *phase - pointer to
1684 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001686 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001688 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * is in same phase.
1690 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001691 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
1693 * XXX Note : handling for bus free may be useful.
1694 */
1695
1696/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001697 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * IS 100% reliable, and for the actual data transfer where speed
1699 * counts, we will always do a pseudo DMA or DMA transfer.
1700 */
1701
Roman Zippelc28bda22007-05-01 22:32:36 +02001702static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1703 unsigned char *phase, int *count,
1704 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Roman Zippelc28bda22007-05-01 22:32:36 +02001706 register unsigned char p = *phase, tmp;
1707 register int c = *count;
1708 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Roman Zippelc28bda22007-05-01 22:32:36 +02001710 /*
1711 * The NCR5380 chip will only drive the SCSI bus when the
1712 * phase specified in the appropriate bits of the TARGET COMMAND
1713 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
1715
Roman Zippelc28bda22007-05-01 22:32:36 +02001716 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Roman Zippelc28bda22007-05-01 22:32:36 +02001718 do {
1719 /*
1720 * Wait for assertion of REQ, after which the phase bits will be
1721 * valid
1722 */
1723 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1724 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Finn Thaind65e6342014-03-18 11:42:20 +11001726 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Roman Zippelc28bda22007-05-01 22:32:36 +02001728 /* Check for phase mismatch */
1729 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001730 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001731 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001732 break;
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Roman Zippelc28bda22007-05-01 22:32:36 +02001735 /* Do actual transfer from SCSI bus to / from memory */
1736 if (!(p & SR_IO))
1737 NCR5380_write(OUTPUT_DATA_REG, *d);
1738 else
1739 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Roman Zippelc28bda22007-05-01 22:32:36 +02001741 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Roman Zippelc28bda22007-05-01 22:32:36 +02001743 /*
1744 * The SCSI standard suggests that in MSGOUT phase, the initiator
1745 * should drop ATN on the last byte of the message phase
1746 * after REQ has been asserted for the handshake but before
1747 * the initiator raises ACK.
1748 */
1749
1750 if (!(p & SR_IO)) {
1751 if (!((p & SR_MSG) && c > 1)) {
1752 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001753 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001754 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1755 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1756 } else {
1757 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1758 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001759 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001760 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1761 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1762 }
1763 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001764 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001765 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1766 }
1767
1768 while (NCR5380_read(STATUS_REG) & SR_REQ)
1769 ;
1770
Finn Thaind65e6342014-03-18 11:42:20 +11001771 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001772
1773 /*
1774 * We have several special cases to consider during REQ/ACK handshaking :
1775 * 1. We were in MSGOUT phase, and we are on the last byte of the
1776 * message. ATN must be dropped as ACK is dropped.
1777 *
1778 * 2. We are in a MSGIN phase, and we are on the last byte of the
1779 * message. We must exit with ACK asserted, so that the calling
1780 * code may raise ATN before dropping ACK to reject the message.
1781 *
1782 * 3. ACK and ATN are clear and the target may proceed as normal.
1783 */
1784 if (!(p == PHASE_MSGIN && c == 1)) {
1785 if (p == PHASE_MSGOUT && c > 1)
1786 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1787 else
1788 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1789 }
1790 } while (--c);
1791
Finn Thaind65e6342014-03-18 11:42:20 +11001792 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001793
1794 *count = c;
1795 *data = d;
1796 tmp = NCR5380_read(STATUS_REG);
1797 /* The phase read from the bus is valid if either REQ is (already)
1798 * asserted or if ACK hasn't been released yet. The latter is the case if
1799 * we're in MSGIN and all wanted bytes have been received.
1800 */
1801 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1802 *phase = tmp & PHASE_MASK;
1803 else
1804 *phase = PHASE_UNKNOWN;
1805
1806 if (!c || (*phase == p))
1807 return 0;
1808 else
1809 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812/*
1813 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001814 *
1815 * Purpose : abort the currently established nexus. Should only be
1816 * called from a routine which can drop into a
1817 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * Returns : 0 on success, -1 on failure.
1819 */
1820
Roman Zippelc28bda22007-05-01 22:32:36 +02001821static int do_abort(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Roman Zippelc28bda22007-05-01 22:32:36 +02001823 unsigned char tmp, *msgptr, phase;
1824 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Roman Zippelc28bda22007-05-01 22:32:36 +02001826 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Roman Zippelc28bda22007-05-01 22:32:36 +02001829 /*
1830 * Wait for the target to indicate a valid phase by asserting
1831 * REQ. Once this happens, we'll have either a MSGOUT phase
1832 * and can immediately send the ABORT message, or we'll have some
1833 * other phase and will have to source/sink data.
1834 *
1835 * We really don't care what value was on the bus or what value
1836 * the target sees, so we just handshake.
1837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Roel Kluin3be38e72007-11-15 21:13:46 +01001839 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001840 ;
1841
1842 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1843
1844 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1845 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1846 ICR_ASSERT_ACK);
1847 while (NCR5380_read(STATUS_REG) & SR_REQ)
1848 ;
1849 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1850 }
1851
1852 tmp = ABORT;
1853 msgptr = &tmp;
1854 len = 1;
1855 phase = PHASE_MSGOUT;
1856 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1857
1858 /*
1859 * If we got here, and the command completed successfully,
1860 * we're about to go into bus free state.
1861 */
1862
1863 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
1866#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001867/*
1868 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * unsigned char *phase, int *count, unsigned char **data)
1870 *
1871 * Purpose : transfers data in given phase using either real
1872 * or pseudo DMA.
1873 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001874 * Inputs : instance - instance of driver, *phase - pointer to
1875 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001877 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001879 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * is in same phase.
1881 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001882 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 *
1884 */
1885
1886
Roman Zippelc28bda22007-05-01 22:32:36 +02001887static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1888 unsigned char *phase, int *count,
1889 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Roman Zippelc28bda22007-05-01 22:32:36 +02001891 SETUP_HOSTDATA(instance);
1892 register int c = *count;
1893 register unsigned char p = *phase;
1894 register unsigned char *d = *data;
1895 unsigned char tmp;
1896 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Roman Zippelc28bda22007-05-01 22:32:36 +02001898 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1899 *phase = tmp;
1900 return -1;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Roman Zippelc28bda22007-05-01 22:32:36 +02001903 if (atari_read_overruns && (p & SR_IO))
1904 c -= atari_read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Finn Thaind65e6342014-03-18 11:42:20 +11001906 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001907 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1908 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Roman Zippelc28bda22007-05-01 22:32:36 +02001910 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001913 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914#endif /* def REAL_DMA */
1915
Roman Zippelc28bda22007-05-01 22:32:36 +02001916 if (IS_A_TT()) {
1917 /* On the Medusa, it is a must to initialize the DMA before
1918 * starting the NCR. This is also the cleaner way for the TT.
1919 */
1920 local_irq_save(flags);
1921 hostdata->dma_len = (p & SR_IO) ?
1922 NCR5380_dma_read_setup(instance, d, c) :
1923 NCR5380_dma_write_setup(instance, d, c);
1924 local_irq_restore(flags);
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Roman Zippelc28bda22007-05-01 22:32:36 +02001927 if (p & SR_IO)
1928 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1929 else {
1930 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1931 NCR5380_write(START_DMA_SEND_REG, 0);
1932 }
1933
1934 if (!IS_A_TT()) {
1935 /* On the Falcon, the DMA setup must be done after the last */
1936 /* NCR access, else the DMA setup gets trashed!
1937 */
1938 local_irq_save(flags);
1939 hostdata->dma_len = (p & SR_IO) ?
1940 NCR5380_dma_read_setup(instance, d, c) :
1941 NCR5380_dma_write_setup(instance, d, c);
1942 local_irq_restore(flags);
1943 }
1944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946#endif /* defined(REAL_DMA) */
1947
1948/*
1949 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1950 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001951 * Purpose : run through the various SCSI phases and do as the target
1952 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * instance->connected.
1954 *
1955 * Inputs : instance, instance for which we are doing commands
1956 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001957 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * modified if a command disconnects, *instance->connected will
1959 * change.
1960 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001961 * XXX Note : we need to watch for bus free or a reset condition here
1962 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001964
1965static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Roman Zippelc28bda22007-05-01 22:32:36 +02001967 SETUP_HOSTDATA(instance);
1968 unsigned long flags;
1969 unsigned char msgout = NOP;
1970 int sink = 0;
1971 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001973 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001975 unsigned char *data;
1976 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1977 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Roman Zippelc28bda22007-05-01 22:32:36 +02001979 while (1) {
1980 tmp = NCR5380_read(STATUS_REG);
1981 /* We only have a valid SCSI phase when REQ is asserted */
1982 if (tmp & SR_REQ) {
1983 phase = (tmp & PHASE_MASK);
1984 if (phase != old_phase) {
1985 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001986 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Roman Zippelc28bda22007-05-01 22:32:36 +02001989 if (sink && (phase != PHASE_MSGOUT)) {
1990 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Roman Zippelc28bda22007-05-01 22:32:36 +02001992 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1993 ICR_ASSERT_ACK);
1994 while (NCR5380_read(STATUS_REG) & SR_REQ)
1995 ;
1996 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1997 ICR_ASSERT_ATN);
1998 sink = 0;
1999 continue;
2000 }
2001
2002 switch (phase) {
2003 case PHASE_DATAOUT:
2004#if (NDEBUG & NDEBUG_NO_DATAOUT)
2005 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2006 "aborted\n", HOSTNO);
2007 sink = 1;
2008 do_abort(instance);
2009 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002010 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002011 return;
2012#endif
2013 case PHASE_DATAIN:
2014 /*
2015 * If there is no room left in the current buffer in the
2016 * scatter-gather list, move onto the next one.
2017 */
2018
2019 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2020 ++cmd->SCp.buffer;
2021 --cmd->SCp.buffers_residual;
2022 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002023 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002024 /* ++roman: Try to merge some scatter-buffers if
2025 * they are at contiguous physical addresses.
2026 */
2027 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002028 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002029 HOSTNO, cmd->SCp.this_residual,
2030 cmd->SCp.buffers_residual);
2031 }
2032
2033 /*
2034 * The preferred transfer method is going to be
2035 * PSEUDO-DMA for systems that are strictly PIO,
2036 * since we can let the hardware do the handshaking.
2037 *
2038 * For this to work, we need to know the transfersize
2039 * ahead of time, since the pseudo-DMA code will sit
2040 * in an unconditional loop.
2041 */
2042
2043 /* ++roman: I suggest, this should be
2044 * #if def(REAL_DMA)
2045 * instead of leaving REAL_DMA out.
2046 */
2047
2048#if defined(REAL_DMA)
2049 if (!cmd->device->borken &&
2050 (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
2051 len = transfersize;
2052 cmd->SCp.phase = phase;
2053 if (NCR5380_transfer_dma(instance, &phase,
2054 &len, (unsigned char **)&cmd->SCp.ptr)) {
2055 /*
2056 * If the watchdog timer fires, all future
2057 * accesses to this device will use the
2058 * polled-IO. */
2059 printk(KERN_NOTICE "scsi%d: switching target %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002060 "lun %llu to slow handshake\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002061 cmd->device->id, cmd->device->lun);
2062 cmd->device->borken = 1;
2063 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2064 ICR_ASSERT_ATN);
2065 sink = 1;
2066 do_abort(instance);
2067 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002068 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002069 /* XXX - need to source or sink data here, as appropriate */
2070 } else {
2071#ifdef REAL_DMA
2072 /* ++roman: When using real DMA,
2073 * information_transfer() should return after
2074 * starting DMA since it has nothing more to
2075 * do.
2076 */
2077 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002079 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002081 }
2082 } else
2083#endif /* defined(REAL_DMA) */
2084 NCR5380_transfer_pio(instance, &phase,
2085 (int *)&cmd->SCp.this_residual,
2086 (unsigned char **)&cmd->SCp.ptr);
2087 break;
2088 case PHASE_MSGIN:
2089 len = 1;
2090 data = &tmp;
2091 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2092 NCR5380_transfer_pio(instance, &phase, &len, &data);
2093 cmd->SCp.Message = tmp;
2094
2095 switch (tmp) {
2096 /*
2097 * Linking lets us reduce the time required to get the
2098 * next command out to the device, hopefully this will
2099 * mean we don't waste another revolution due to the delays
2100 * required by ARBITRATION and another SELECTION.
2101 *
2102 * In the current implementation proposal, low level drivers
2103 * merely have to start the next command, pointed to by
2104 * next_link, done() is called as with unlinked commands.
2105 */
2106#ifdef LINKED
2107 case LINKED_CMD_COMPLETE:
2108 case LINKED_FLG_CMD_COMPLETE:
2109 /* Accept message by clearing ACK */
2110 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2111
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002112 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002113 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2114
2115 /* Enable reselect interrupts */
2116 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2117 /*
2118 * Sanity check : A linked command should only terminate
2119 * with one of these messages if there are more linked
2120 * commands available.
2121 */
2122
2123 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002124 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002125 "linked command complete, no next_link\n",
2126 HOSTNO, cmd->device->id, cmd->device->lun);
2127 sink = 1;
2128 do_abort(instance);
2129 return;
2130 }
2131
2132 initialize_SCp(cmd->next_link);
2133 /* The next command is still part of this process; copy it
2134 * and don't free it! */
2135 cmd->next_link->tag = cmd->tag;
2136 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002137 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002138 "done, calling scsi_done().\n",
2139 HOSTNO, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140#ifdef NCR5380_STATS
Roman Zippelc28bda22007-05-01 22:32:36 +02002141 collect_stats(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002143 cmd->scsi_done(cmd);
2144 cmd = hostdata->connected;
2145 break;
2146#endif /* def LINKED */
2147 case ABORT:
2148 case COMMAND_COMPLETE:
2149 /* Accept message by clearing ACK */
2150 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2151 /* ++guenther: possible race with Falcon locking */
2152 falcon_dont_release++;
2153 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002154 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002155 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
2156#ifdef SUPPORT_TAGS
2157 cmd_free_tag(cmd);
2158 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2159 /* Turn a QUEUE FULL status into BUSY, I think the
2160 * mid level cannot handle QUEUE FULL :-( (The
2161 * command is retried after BUSY). Also update our
2162 * queue size to the number of currently issued
2163 * commands now.
2164 */
2165 /* ++Andreas: the mid level code knows about
2166 QUEUE_FULL now. */
2167 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002168 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002169 "QUEUE_FULL after %d commands\n",
2170 HOSTNO, cmd->device->id, cmd->device->lun,
2171 ta->nr_allocated);
2172 if (ta->queue_size > ta->nr_allocated)
2173 ta->nr_allocated = ta->queue_size;
2174 }
2175#else
2176 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2177#endif
2178 /* Enable reselect interrupts */
2179 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2180
2181 /*
2182 * I'm not sure what the correct thing to do here is :
2183 *
2184 * If the command that just executed is NOT a request
2185 * sense, the obvious thing to do is to set the result
2186 * code to the values of the stored parameters.
2187 *
2188 * If it was a REQUEST SENSE command, we need some way to
2189 * differentiate between the failure code of the original
2190 * and the failure code of the REQUEST sense - the obvious
2191 * case is success, where we fall through and leave the
2192 * result code unchanged.
2193 *
2194 * The non-obvious place is where the REQUEST SENSE failed
2195 */
2196
2197 if (cmd->cmnd[0] != REQUEST_SENSE)
2198 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2199 else if (status_byte(cmd->SCp.Status) != GOOD)
2200 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2201
2202#ifdef AUTOSENSE
Boaz Harrosh28424d32007-09-10 22:37:45 +03002203 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2204 hostdata->ses.cmd_len) {
2205 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2206 hostdata->ses.cmd_len = 0 ;
2207 }
2208
Roman Zippelc28bda22007-05-01 22:32:36 +02002209 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2210 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002211 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002212
Finn Thaind65e6342014-03-18 11:42:20 +11002213 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002214
2215 local_irq_save(flags);
2216 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002217 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002218 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2219 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002220 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002221 "issue queue\n", H_NO(cmd));
2222 } else
2223#endif /* def AUTOSENSE */
2224 {
2225#ifdef NCR5380_STATS
2226 collect_stats(hostdata, cmd);
2227#endif
2228 cmd->scsi_done(cmd);
2229 }
2230
2231 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2232 /*
2233 * Restore phase bits to 0 so an interrupted selection,
2234 * arbitration can resume.
2235 */
2236 NCR5380_write(TARGET_COMMAND_REG, 0);
2237
2238 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2239 barrier();
2240
2241 falcon_dont_release--;
2242 /* ++roman: For Falcon SCSI, release the lock on the
2243 * ST-DMA here if no other commands are waiting on the
2244 * disconnected queue.
2245 */
2246 falcon_release_lock_if_possible(hostdata);
2247 return;
2248 case MESSAGE_REJECT:
2249 /* Accept message by clearing ACK */
2250 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2251 /* Enable reselect interrupts */
2252 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2253 switch (hostdata->last_message) {
2254 case HEAD_OF_QUEUE_TAG:
2255 case ORDERED_QUEUE_TAG:
2256 case SIMPLE_QUEUE_TAG:
2257 /* The target obviously doesn't support tagged
2258 * queuing, even though it announced this ability in
2259 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2260 * clear 'tagged_supported' and lock the LUN, since
2261 * the command is treated as untagged further on.
2262 */
2263 cmd->device->tagged_supported = 0;
2264 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2265 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002266 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002267 "QUEUE_TAG message; tagged queuing "
2268 "disabled\n",
2269 HOSTNO, cmd->device->id, cmd->device->lun);
2270 break;
2271 }
2272 break;
2273 case DISCONNECT:
2274 /* Accept message by clearing ACK */
2275 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2276 local_irq_save(flags);
2277 cmd->device->disconnect = 1;
2278 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002279 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002280 hostdata->connected = NULL;
2281 hostdata->disconnected_queue = cmd;
2282 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002283 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002284 "moved from connected to the "
2285 "disconnected_queue\n", HOSTNO,
2286 cmd->device->id, cmd->device->lun);
2287 /*
2288 * Restore phase bits to 0 so an interrupted selection,
2289 * arbitration can resume.
2290 */
2291 NCR5380_write(TARGET_COMMAND_REG, 0);
2292
2293 /* Enable reselect interrupts */
2294 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2295 /* Wait for bus free to avoid nasty timeouts */
2296 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2297 barrier();
2298 return;
2299 /*
2300 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2301 * operation, in violation of the SCSI spec so we can safely
2302 * ignore SAVE/RESTORE pointers calls.
2303 *
2304 * Unfortunately, some disks violate the SCSI spec and
2305 * don't issue the required SAVE_POINTERS message before
2306 * disconnecting, and we have to break spec to remain
2307 * compatible.
2308 */
2309 case SAVE_POINTERS:
2310 case RESTORE_POINTERS:
2311 /* Accept message by clearing ACK */
2312 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2313 /* Enable reselect interrupts */
2314 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2315 break;
2316 case EXTENDED_MESSAGE:
2317 /*
2318 * Extended messages are sent in the following format :
2319 * Byte
2320 * 0 EXTENDED_MESSAGE == 1
2321 * 1 length (includes one byte for code, doesn't
2322 * include first two bytes)
2323 * 2 code
2324 * 3..length+1 arguments
2325 *
2326 * Start the extended message buffer with the EXTENDED_MESSAGE
2327 * byte, since spi_print_msg() wants the whole thing.
2328 */
2329 extended_msg[0] = EXTENDED_MESSAGE;
2330 /* Accept first byte by clearing ACK */
2331 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2332
Finn Thaind65e6342014-03-18 11:42:20 +11002333 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002334
2335 len = 2;
2336 data = extended_msg + 1;
2337 phase = PHASE_MSGIN;
2338 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002339 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002340 (int)extended_msg[1], (int)extended_msg[2]);
2341
2342 if (!len && extended_msg[1] <=
2343 (sizeof(extended_msg) - 1)) {
2344 /* Accept third byte by clearing ACK */
2345 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2346 len = extended_msg[1] - 1;
2347 data = extended_msg + 3;
2348 phase = PHASE_MSGIN;
2349
2350 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002351 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002352 HOSTNO, len);
2353
2354 switch (extended_msg[2]) {
2355 case EXTENDED_SDTR:
2356 case EXTENDED_WDTR:
2357 case EXTENDED_MODIFY_DATA_POINTER:
2358 case EXTENDED_EXTENDED_IDENTIFY:
2359 tmp = 0;
2360 }
2361 } else if (len) {
2362 printk(KERN_NOTICE "scsi%d: error receiving "
2363 "extended message\n", HOSTNO);
2364 tmp = 0;
2365 } else {
2366 printk(KERN_NOTICE "scsi%d: extended message "
2367 "code %02x length %d is too long\n",
2368 HOSTNO, extended_msg[2], extended_msg[1]);
2369 tmp = 0;
2370 }
2371 /* Fall through to reject message */
2372
2373 /*
2374 * If we get something weird that we aren't expecting,
2375 * reject it.
2376 */
2377 default:
2378 if (!tmp) {
2379 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2380 spi_print_msg(extended_msg);
2381 printk("\n");
2382 } else if (tmp != EXTENDED_MESSAGE)
2383 printk(KERN_DEBUG "scsi%d: rejecting unknown "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002384 "message %02x from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002385 HOSTNO, tmp, cmd->device->id, cmd->device->lun);
2386 else
2387 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2388 "extended message "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002389 "code %02x, length %d from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002390 HOSTNO, extended_msg[1], extended_msg[0],
2391 cmd->device->id, cmd->device->lun);
2392
2393
2394 msgout = MESSAGE_REJECT;
2395 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2396 break;
2397 } /* switch (tmp) */
2398 break;
2399 case PHASE_MSGOUT:
2400 len = 1;
2401 data = &msgout;
2402 hostdata->last_message = msgout;
2403 NCR5380_transfer_pio(instance, &phase, &len, &data);
2404 if (msgout == ABORT) {
2405#ifdef SUPPORT_TAGS
2406 cmd_free_tag(cmd);
2407#else
2408 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2409#endif
2410 hostdata->connected = NULL;
2411 cmd->result = DID_ERROR << 16;
2412#ifdef NCR5380_STATS
2413 collect_stats(hostdata, cmd);
2414#endif
2415 cmd->scsi_done(cmd);
2416 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2417 falcon_release_lock_if_possible(hostdata);
2418 return;
2419 }
2420 msgout = NOP;
2421 break;
2422 case PHASE_CMDOUT:
2423 len = cmd->cmd_len;
2424 data = cmd->cmnd;
2425 /*
2426 * XXX for performance reasons, on machines with a
2427 * PSEUDO-DMA architecture we should probably
2428 * use the dma transfer function.
2429 */
2430 NCR5380_transfer_pio(instance, &phase, &len, &data);
2431 break;
2432 case PHASE_STATIN:
2433 len = 1;
2434 data = &tmp;
2435 NCR5380_transfer_pio(instance, &phase, &len, &data);
2436 cmd->SCp.Status = tmp;
2437 break;
2438 default:
2439 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002440 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002441 } /* switch(phase) */
2442 } /* if (tmp * SR_REQ) */
2443 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446/*
2447 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2448 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002449 * Purpose : does reselection, initializing the instance->connected
2450 * 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 -07002451 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 * Inputs : instance - this instance of the NCR5380.
2454 *
2455 */
2456
2457
Roman Zippelc28bda22007-05-01 22:32:36 +02002458static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Roman Zippelc28bda22007-05-01 22:32:36 +02002460 SETUP_HOSTDATA(instance);
2461 unsigned char target_mask;
2462 unsigned char lun, phase;
2463 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002465 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002467 unsigned char msg[3];
2468 unsigned char *data;
2469 Scsi_Cmnd *tmp = NULL, *prev;
2470/* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Roman Zippelc28bda22007-05-01 22:32:36 +02002472 /*
2473 * Disable arbitration, etc. since the host adapter obviously
2474 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Roman Zippelc28bda22007-05-01 22:32:36 +02002477 NCR5380_write(MODE_REG, MR_BASE);
2478 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Roman Zippelc28bda22007-05-01 22:32:36 +02002480 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2481
Finn Thaind65e6342014-03-18 11:42:20 +11002482 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002483
2484 /*
2485 * At this point, we have detected that our SCSI ID is on the bus,
2486 * SEL is true and BSY was false for at least one bus settle delay
2487 * (400 ns).
2488 *
2489 * We must assert BSY ourselves, until the target drops the SEL
2490 * signal.
2491 */
2492
2493 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2494
2495 while (NCR5380_read(STATUS_REG) & SR_SEL)
2496 ;
2497 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2498
2499 /*
2500 * Wait for target to go into MSGIN.
2501 */
2502
2503 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2504 ;
2505
2506 len = 1;
2507 data = msg;
2508 phase = PHASE_MSGIN;
2509 NCR5380_transfer_pio(instance, &phase, &len, &data);
2510
2511 if (!(msg[0] & 0x80)) {
2512 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2513 spi_print_msg(msg);
2514 do_abort(instance);
2515 return;
2516 }
2517 lun = (msg[0] & 0x07);
2518
2519#ifdef SUPPORT_TAGS
2520 /* If the phase is still MSGIN, the target wants to send some more
2521 * messages. In case it supports tagged queuing, this is probably a
2522 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2523 */
2524 tag = TAG_NONE;
2525 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2526 /* Accept previous IDENTIFY message by clearing ACK */
2527 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2528 len = 2;
2529 data = msg + 1;
2530 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2531 msg[1] == SIMPLE_QUEUE_TAG)
2532 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002533 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002534 "reselection\n", HOSTNO, target_mask, lun, tag);
2535 }
2536#endif
2537
2538 /*
2539 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2540 * just reestablished, and remove it from the disconnected queue.
2541 */
2542
2543 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2544 tmp; prev = tmp, tmp = NEXT(tmp)) {
2545 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2546#ifdef SUPPORT_TAGS
2547 && (tag == tmp->tag)
2548#endif
2549 ) {
2550 /* ++guenther: prevent race with falcon_release_lock */
2551 falcon_dont_release++;
2552 if (prev) {
2553 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002554 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002555 } else {
2556 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2557 hostdata->disconnected_queue = NEXT(tmp);
2558 }
Roman Zippel3130d902007-05-01 22:32:37 +02002559 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002560 break;
2561 }
2562 }
2563
2564 if (!tmp) {
2565 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2566#ifdef SUPPORT_TAGS
2567 "tag %d "
2568#endif
2569 "not in disconnected_queue.\n",
2570 HOSTNO, target_mask, lun
2571#ifdef SUPPORT_TAGS
2572 , tag
2573#endif
2574 );
2575 /*
2576 * Since we have an established nexus that we can't do anything
2577 * with, we must abort it.
2578 */
2579 do_abort(instance);
2580 return;
2581 }
2582
2583 /* Accept message by clearing ACK */
2584 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2585
2586 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002587 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002588 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
2589 falcon_dont_release--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592
2593/*
2594 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2595 *
2596 * Purpose : abort a command
2597 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002598 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2599 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 * used.
2601 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002602 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002604 * XXX - there is no way to abort the command that is currently
2605 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002607 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 */
2609
2610static
Roman Zippelc28bda22007-05-01 22:32:36 +02002611int NCR5380_abort(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
Roman Zippelc28bda22007-05-01 22:32:36 +02002613 struct Scsi_Host *instance = cmd->device->host;
2614 SETUP_HOSTDATA(instance);
2615 Scsi_Cmnd *tmp, **prev;
2616 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002618 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Roman Zippelc28bda22007-05-01 22:32:36 +02002620 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Roman Zippelc28bda22007-05-01 22:32:36 +02002622 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Roman Zippelc28bda22007-05-01 22:32:36 +02002624 if (!IS_A_TT() && !falcon_got_lock)
2625 printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_abort\n",
2626 HOSTNO);
2627
Finn Thaind65e6342014-03-18 11:42:20 +11002628 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002629 NCR5380_read(BUS_AND_STATUS_REG),
2630 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002633 /*
2634 * Case 1 : If the command is the currently executing command,
2635 * we'll set the aborted flag and return control so that
2636 * information transfer routine can exit cleanly.
2637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Roman Zippelc28bda22007-05-01 22:32:36 +02002639 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Finn Thaind65e6342014-03-18 11:42:20 +11002641 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002642 /*
2643 * We should perform BSY checking, and make sure we haven't slipped
2644 * into BUS FREE.
2645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Roman Zippelc28bda22007-05-01 22:32:36 +02002647 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2648 /*
2649 * Since we can't change phases until we've completed the current
2650 * handshake, we have to source or sink a byte of data if the current
2651 * phase is not MSGOUT.
2652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Roman Zippelc28bda22007-05-01 22:32:36 +02002654 /*
2655 * Return control to the executing NCR drive so we can clear the
2656 * aborted flag and get back into our main loop.
2657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Roman Zippelc28bda22007-05-01 22:32:36 +02002659 if (do_abort(instance) == 0) {
2660 hostdata->aborted = 1;
2661 hostdata->connected = NULL;
2662 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002664 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002666 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002668 local_irq_restore(flags);
2669 cmd->scsi_done(cmd);
2670 falcon_release_lock_if_possible(hostdata);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002671 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002672 } else {
2673/* local_irq_restore(flags); */
2674 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002675 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002679
2680 /*
2681 * Case 2 : If the command hasn't been issued yet, we simply remove it
2682 * from the issue queue.
2683 */
2684 for (prev = (Scsi_Cmnd **)&(hostdata->issue_queue),
2685 tmp = (Scsi_Cmnd *)hostdata->issue_queue;
2686 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2687 if (cmd == tmp) {
2688 REMOVE(5, *prev, tmp, NEXT(tmp));
2689 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002690 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002691 tmp->result = DID_ABORT << 16;
2692 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002693 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002694 HOSTNO);
2695 /* Tagged queuing note: no tag to free here, hasn't been assigned
2696 * yet... */
2697 tmp->scsi_done(tmp);
2698 falcon_release_lock_if_possible(hostdata);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002699 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701 }
2702
Roman Zippelc28bda22007-05-01 22:32:36 +02002703 /*
2704 * Case 3 : If any commands are connected, we're going to fail the abort
2705 * and let the high level SCSI driver retry at a later time or
2706 * issue a reset.
2707 *
2708 * Timeouts, and therefore aborted commands, will be highly unlikely
2709 * and handling them cleanly in this situation would make the common
2710 * case of noresets less efficient, and would pollute our code. So,
2711 * we fail.
2712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Roman Zippelc28bda22007-05-01 22:32:36 +02002714 if (hostdata->connected) {
2715 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002716 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002717 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Roman Zippelc28bda22007-05-01 22:32:36 +02002720 /*
2721 * Case 4: If the command is currently disconnected from the bus, and
2722 * there are no connected commands, we reconnect the I_T_L or
2723 * I_T_L_Q nexus associated with it, go into message out, and send
2724 * an abort message.
2725 *
2726 * This case is especially ugly. In order to reestablish the nexus, we
2727 * need to call NCR5380_select(). The easiest way to implement this
2728 * function was to abort if the bus was busy, and let the interrupt
2729 * handler triggered on the SEL for reselect take care of lost arbitrations
2730 * where necessary, meaning interrupts need to be enabled.
2731 *
2732 * When interrupts are enabled, the queues may change - so we
2733 * can't remove it from the disconnected queue before selecting it
2734 * because that could cause a failure in hashing the nexus if that
2735 * device reselected.
2736 *
2737 * Since the queues may change, we can't use the pointers from when we
2738 * first locate it.
2739 *
2740 * So, we must first locate the command, and if NCR5380_select()
2741 * succeeds, then issue the abort, relocate the command and remove
2742 * it from the disconnected queue.
2743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Roman Zippelc28bda22007-05-01 22:32:36 +02002745 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2746 tmp = NEXT(tmp)) {
2747 if (cmd == tmp) {
2748 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002749 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002750
Finn Thain76f13b92014-11-12 16:11:53 +11002751 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002752 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002753
Finn Thaind65e6342014-03-18 11:42:20 +11002754 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002755
2756 do_abort(instance);
2757
2758 local_irq_save(flags);
2759 for (prev = (Scsi_Cmnd **)&(hostdata->disconnected_queue),
2760 tmp = (Scsi_Cmnd *)hostdata->disconnected_queue;
2761 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2762 if (cmd == tmp) {
2763 REMOVE(5, *prev, tmp, NEXT(tmp));
2764 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002765 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002766 tmp->result = DID_ABORT << 16;
2767 /* We must unlock the tag/LUN immediately here, since the
2768 * target goes to BUS FREE and doesn't send us another
2769 * message (COMMAND_COMPLETE or the like)
2770 */
2771#ifdef SUPPORT_TAGS
2772 cmd_free_tag(tmp);
2773#else
2774 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2775#endif
2776 local_irq_restore(flags);
2777 tmp->scsi_done(tmp);
2778 falcon_release_lock_if_possible(hostdata);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002779 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002780 }
2781 }
2782 }
2783 }
2784
2785 /*
2786 * Case 5 : If we reached this point, the command was not found in any of
2787 * the queues.
2788 *
2789 * We probably reached this point because of an unlikely race condition
2790 * between the command completing successfully and the abortion code,
2791 * so we won't panic, but we will notify the user in case something really
2792 * broke.
2793 */
2794
2795 local_irq_restore(flags);
Joe Perchesad361c92009-07-06 13:05:40 -07002796 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002797
2798 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2799 * possible at all) At least, we should check if the lock could be
2800 * released after the abort, in case it is kept due to some bug.
2801 */
2802 falcon_release_lock_if_possible(hostdata);
2803
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002804 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807
Roman Zippelc28bda22007-05-01 22:32:36 +02002808/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 * Purpose : reset the SCSI bus.
2812 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002813 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Roman Zippelc28bda22007-05-01 22:32:36 +02002817static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Roman Zippelc28bda22007-05-01 22:32:36 +02002819 SETUP_HOSTDATA(cmd->device->host);
2820 int i;
2821 unsigned long flags;
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002822#if defined(RESET_RUN_DONE)
Roman Zippelc28bda22007-05-01 22:32:36 +02002823 Scsi_Cmnd *connected, *disconnected_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824#endif
2825
Roman Zippelc28bda22007-05-01 22:32:36 +02002826 if (!IS_A_TT() && !falcon_got_lock)
2827 printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_reset\n",
2828 H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Roman Zippelc28bda22007-05-01 22:32:36 +02002830 NCR5380_print_status(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Roman Zippelc28bda22007-05-01 22:32:36 +02002832 /* get in phase */
2833 NCR5380_write(TARGET_COMMAND_REG,
2834 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2835 /* assert RST */
2836 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2837 udelay(40);
2838 /* reset NCR registers */
2839 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2840 NCR5380_write(MODE_REG, MR_BASE);
2841 NCR5380_write(TARGET_COMMAND_REG, 0);
2842 NCR5380_write(SELECT_ENABLE_REG, 0);
2843 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2844 * through anymore ... */
2845 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002847 /* MSch 20140115 - looking at the generic NCR5380 driver, all of this
2848 * should go.
2849 * Catch-22: if we don't clear all queues, the SCSI driver lock will
2850 * not be reset by atari_scsi_reset()!
2851 */
2852
2853#if defined(RESET_RUN_DONE)
2854 /* XXX Should now be done by midlevel code, but it's broken XXX */
Roman Zippelc28bda22007-05-01 22:32:36 +02002855 /* XXX see below XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Roman Zippelc28bda22007-05-01 22:32:36 +02002857 /* MSch: old-style reset: actually abort all command processing here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Roman Zippelc28bda22007-05-01 22:32:36 +02002859 /* After the reset, there are no more connected or disconnected commands
2860 * and no busy units; to avoid problems with re-inserting the commands
2861 * into the issue_queue (via scsi_done()), the aborted commands are
2862 * remembered in local variables first.
2863 */
2864 local_irq_save(flags);
2865 connected = (Scsi_Cmnd *)hostdata->connected;
2866 hostdata->connected = NULL;
2867 disconnected_queue = (Scsi_Cmnd *)hostdata->disconnected_queue;
2868 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002870 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002872 for (i = 0; i < 8; ++i)
2873 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002875 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002877 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Roman Zippelc28bda22007-05-01 22:32:36 +02002879 /* In order to tell the mid-level code which commands were aborted,
2880 * set the command status to DID_RESET and call scsi_done() !!!
2881 * This ultimately aborts processing of these commands in the mid-level.
2882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Roman Zippelc28bda22007-05-01 22:32:36 +02002884 if ((cmd = connected)) {
Finn Thaind65e6342014-03-18 11:42:20 +11002885 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002886 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2887 cmd->scsi_done(cmd);
2888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Roman Zippelc28bda22007-05-01 22:32:36 +02002890 for (i = 0; (cmd = disconnected_queue); ++i) {
2891 disconnected_queue = NEXT(cmd);
Roman Zippel3130d902007-05-01 22:32:37 +02002892 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002893 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2894 cmd->scsi_done(cmd);
2895 }
2896 if (i > 0)
Finn Thaind65e6342014-03-18 11:42:20 +11002897 dprintk(NDEBUG_ABORT, "scsi: reset aborted %d disconnected command(s)\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Roman Zippelc28bda22007-05-01 22:32:36 +02002899 /* The Falcon lock should be released after a reset...
2900 */
2901 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2902 * unlocking and enabling dma interrupt.
2903 */
2904/* falcon_release_lock_if_possible( hostdata );*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Roman Zippelc28bda22007-05-01 22:32:36 +02002906 /* since all commands have been explicitly terminated, we need to tell
2907 * the midlevel code that the reset was SUCCESSFUL, and there is no
2908 * need to 'wake up' the commands by a request_sense
2909 */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002910 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911#else /* 1 */
2912
Roman Zippelc28bda22007-05-01 22:32:36 +02002913 /* MSch: new-style reset handling: let the mid-level do what it can */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Roman Zippelc28bda22007-05-01 22:32:36 +02002915 /* ++guenther: MID-LEVEL IS STILL BROKEN.
2916 * Mid-level is supposed to requeue all commands that were active on the
2917 * various low-level queues. In fact it does this, but that's not enough
2918 * because all these commands are subject to timeout. And if a timeout
2919 * happens for any removed command, *_abort() is called but all queues
2920 * are now empty. Abort then gives up the falcon lock, which is fatal,
2921 * since the mid-level will queue more commands and must have the lock
2922 * (it's all happening inside timer interrupt handler!!).
2923 * Even worse, abort will return NOT_RUNNING for all those commands not
2924 * on any queue, so they won't be retried ...
2925 *
2926 * Conclusion: either scsi.c disables timeout for all resetted commands
2927 * immediately, or we lose! As of linux-2.0.20 it doesn't.
2928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Roman Zippelc28bda22007-05-01 22:32:36 +02002930 /* After the reset, there are no more connected or disconnected commands
2931 * and no busy units; so clear the low-level status here to avoid
2932 * conflicts when the mid-level code tries to wake up the affected
2933 * commands!
2934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Roman Zippelc28bda22007-05-01 22:32:36 +02002936 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002937 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002938 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002939 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002940 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002941 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Roman Zippelc28bda22007-05-01 22:32:36 +02002943 local_irq_save(flags);
2944 hostdata->issue_queue = NULL;
2945 hostdata->connected = NULL;
2946 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002948 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002950 for (i = 0; i < 8; ++i)
2951 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002953 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002955 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Roman Zippelc28bda22007-05-01 22:32:36 +02002957 /* we did no complete reset of all commands, so a wakeup is required */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002958 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959#endif /* 1 */
2960}