blob: 795522192d36fdfee500a3c61d1116fc41cff43a [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 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
28 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
29 * this file, too:
30 *
31 * - Some of the debug statements were incorrect (undefined variables and the
32 * like). I fixed that.
33 *
34 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
35 * possible DMA transfer size should also happen for REAL_DMA. I added this
36 * in the #if statement.
37 *
38 * - When using real DMA, information_transfer() should return in a DATAOUT
39 * phase after starting the DMA. It has nothing more to do.
40 *
41 * - The interrupt service routine should run main after end of DMA, too (not
42 * only after RESELECTION interrupts). Additionally, it should _not_ test
43 * for more interrupts after running main, since a DMA process may have
44 * been started and interrupts are turned on now. The new int could happen
45 * inside the execution of NCR5380_intr(), leading to recursive
46 * calls.
47 *
48 * - I've added a function merge_contiguous_buffers() that tries to
49 * merge scatter-gather buffers that are located at contiguous
50 * physical addresses and can be processed with the same DMA setup.
51 * Since most scatter-gather operations work on a page (4K) of
52 * 4 buffers (1K), in more than 90% of all cases three interrupts and
53 * DMA setup actions are saved.
54 *
55 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
56 * and USLEEP, because these were messing up readability and will never be
57 * needed for Atari SCSI.
Roman Zippelc28bda22007-05-01 22:32:36 +020058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
60 * stuff), and 'main' is executed in a bottom half if awoken by an
61 * interrupt.
62 *
63 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
64 * constructs. In my eyes, this made the source rather unreadable, so I
65 * finally replaced that by the *_PRINTK() macros.
66 *
67 */
68
69/*
Roman Zippelc28bda22007-05-01 22:32:36 +020070 * Further development / testing that should be done :
71 * 1. Test linked command handling code after Eric is ready with
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * the high level code.
73 */
db9dff32005-04-03 14:53:59 -050074#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050075#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +020078#define LIST(x, y) \
79 do { \
80 printk("LINE:%d Adding %p to %p\n", \
81 __LINE__, (void*)(x), (void*)(y)); \
82 if ((x) == (y)) \
83 udelay(5); \
84 } while (0)
85#define REMOVE(w, x, y, z) \
86 do { \
87 printk("LINE:%d Removing: %p->%p %p->%p \n", \
88 __LINE__, (void*)(w), (void*)(x), \
89 (void*)(y), (void*)(z)); \
90 if ((x) == (y)) \
91 udelay(5); \
92 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#else
94#define LIST(x,y)
95#define REMOVE(w,x,y,z)
96#endif
97
98#ifndef notyet
99#undef LINKED
100#endif
101
102/*
103 * Design
104 * Issues :
105 *
106 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
107 * and specifically for each board rather than each chip. This makes their
108 * adaptation to platforms like the Mac (Some of which use NCR5380's)
109 * more difficult than it has to be.
110 *
111 * Also, many of the SCSI drivers were written before the command queuing
Roman Zippelc28bda22007-05-01 22:32:36 +0200112 * routines were implemented, meaning their implementations of queued
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * commands were hacked on rather than designed in from the start.
114 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * When I designed the Linux SCSI drivers I figured that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * while having two different SCSI boards in a system might be useful
117 * for debugging things, two of the same type wouldn't be used.
118 * Well, I was wrong and a number of users have mailed me about running
119 * multiple high-performance SCSI boards in a server.
120 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200121 * Finally, when I get questions from users, I have no idea what
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * revision of my driver they are running.
123 *
124 * This driver attempts to address these problems :
Roman Zippelc28bda22007-05-01 22:32:36 +0200125 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200127 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * memory mapped) and drops this file in their 'C' wrapper.
129 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200130 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200132 * and commands that are currently executing. This means that an
133 * unlimited number of commands may be queued, letting
134 * more commands propagate from the higher driver levels giving higher
135 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
136 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * while a command is already executing.
138 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200139 * To solve the multiple-boards-in-the-same-system problem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * there is a separate instance structure for each instance
141 * of a 5380 in the system. So, multiple NCR5380 drivers will
142 * be able to coexist with appropriate changes to the high level
Roman Zippelc28bda22007-05-01 22:32:36 +0200143 * SCSI code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200145 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200147 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
148 * piece of hardware that requires you to sit in a loop polling for
149 * the REQ signal as long as you are connected. Some devices are
150 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * while doing long seek operations.
Roman Zippelc28bda22007-05-01 22:32:36 +0200152 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * The workaround for this is to keep track of devices that have
154 * disconnected. If the device hasn't disconnected, for commands that
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 * should disconnect, we do something like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
157 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
Roman Zippelc28bda22007-05-01 22:32:36 +0200158 *
159 * Some tweaking of N and M needs to be done. An algorithm based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * on "time to data" would give the best results as long as short time
Roman Zippelc28bda22007-05-01 22:32:36 +0200161 * to datas (ie, on the same track) were considered, however these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * broken devices are the exception rather than the rule and I'd rather
163 * spend my time optimizing for the normal case.
164 *
165 * Architecture :
166 *
167 * At the heart of the design is a coroutine, NCR5380_main,
168 * which is started when not running by the interrupt handler,
169 * timer, and queue command function. It attempts to establish
Roman Zippelc28bda22007-05-01 22:32:36 +0200170 * I_T_L or I_T_L_Q nexuses by removing the commands from the
171 * issue queue and calling NCR5380_select() if a nexus
172 * is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * Once a nexus is established, the NCR5380_information_transfer()
175 * phase goes through the various phases as instructed by the target.
176 * if the target goes into MSG IN and sends a DISCONNECT message,
177 * the command structure is placed into the per instance disconnected
178 * queue, and NCR5380_main tries to find more work. If USLEEP
179 * was defined, and the target is idle for too long, the system
180 * will try to sleep.
181 *
182 * If a command has disconnected, eventually an interrupt will trigger,
183 * calling NCR5380_intr() which will in turn call NCR5380_reselect
184 * to reestablish a nexus. This will run main if necessary.
185 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200186 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * appropriate.
188 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200189 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * structures, being initialized after the command is connected
191 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
192 * Note that in violation of the standard, an implicit SAVE POINTERS operation
193 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
194 */
195
196/*
197 * Using this file :
198 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200199 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * and macros and include this file in your driver.
201 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200202 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200204 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
206 * LINKED - if defined, linked commands are supported.
207 *
208 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
209 *
210 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
211 *
212 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * NCR5380_read(register) - read from the specified register
215 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200216 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200219 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200221 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * that they were able to program the controller for.
223 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200224 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * available as NCR5380_i386_dma_write_setup,
226 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
227 *
228 * NCR5380_dma_write_setup(instance, src, count) - initialize
229 * NCR5380_dma_read_setup(instance, dst, count) - initialize
230 * NCR5380_dma_residual(instance); - residual count
231 *
232 * PSEUDO functions :
233 * NCR5380_pwrite(instance, src, count)
234 * NCR5380_pread(instance, dst, count);
235 *
236 * If nothing specific to this implementation needs doing (ie, with external
Roman Zippelc28bda22007-05-01 22:32:36 +0200237 * hardware), you must also define
238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * NCR5380_queue_command
240 * NCR5380_reset
241 * NCR5380_abort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200243 * to be the global entry points into the specific driver, ie
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * #define NCR5380_queue_command t128_queue_command.
245 *
246 * If this is not done, the routines will be defined as static functions
247 * with the NCR5380* names and the user must provide a globally
248 * accessible wrapper function.
249 *
250 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200251 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100253 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
255
256static struct Scsi_Host *first_instance = NULL;
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100257static struct scsi_host_template *the_template = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/* Macros ease life... :-) */
260#define SETUP_HOSTDATA(in) \
261 struct NCR5380_hostdata *hostdata = \
262 (struct NCR5380_hostdata *)(in)->hostdata
263#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
264
Finn Thain710ddd02014-11-12 16:12:02 +1100265#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200266#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100267#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269#define HOSTNO instance->host_no
270#define H_NO(cmd) (cmd)->device->host->host_no
271
272#ifdef SUPPORT_TAGS
273
274/*
275 * Functions for handling tagged queuing
276 * =====================================
277 *
278 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
279 *
280 * Using consecutive numbers for the tags is no good idea in my eyes. There
281 * could be wrong re-usings if the counter (8 bit!) wraps and some early
282 * command has been preempted for a long time. My solution: a bitfield for
283 * remembering used tags.
284 *
285 * There's also the problem that each target has a certain queue size, but we
286 * cannot know it in advance :-( We just see a QUEUE_FULL status being
287 * returned. So, in this case, the driver internal queue size assumption is
288 * reduced to the number of active tags if QUEUE_FULL is returned by the
289 * target. The command is returned to the mid-level, but with status changed
290 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
291 * correctly.
292 *
293 * We're also not allowed running tagged commands as long as an untagged
294 * command is active. And REQUEST SENSE commands after a contingent allegiance
295 * condition _must_ be untagged. To keep track whether an untagged command has
296 * been issued, the host->busy array is still employed, as it is without
297 * support for tagged queuing.
298 *
299 * One could suspect that there are possible race conditions between
300 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
301 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
302 * which already guaranteed to be running at most once. It is also the only
303 * place where tags/LUNs are allocated. So no other allocation can slip
304 * between that pair, there could only happen a reselection, which can free a
305 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
306 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
307 */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309typedef struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200310 DECLARE_BITMAP(allocated, MAX_TAGS);
311 int nr_allocated;
312 int queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313} TAG_ALLOC;
314
Roman Zippelc28bda22007-05-01 22:32:36 +0200315static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317
Roman Zippelc28bda22007-05-01 22:32:36 +0200318static void __init init_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Roman Zippelc28bda22007-05-01 22:32:36 +0200320 int target, lun;
321 TAG_ALLOC *ta;
322
323 if (!setup_use_tagged_queuing)
324 return;
325
326 for (target = 0; target < 8; ++target) {
327 for (lun = 0; lun < 8; ++lun) {
328 ta = &TagAlloc[target][lun];
329 bitmap_zero(ta->allocated, MAX_TAGS);
330 ta->nr_allocated = 0;
331 /* At the beginning, assume the maximum queue size we could
332 * support (MAX_TAGS). This value will be decreased if the target
333 * returns QUEUE_FULL status.
334 */
335 ta->queue_size = MAX_TAGS;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340
341/* Check if we can issue a command to this LUN: First see if the LUN is marked
342 * busy by an untagged command. If the command should use tagged queuing, also
343 * check that there is a free tag and the target's queue won't overflow. This
344 * function should be called with interrupts disabled to avoid race
345 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Finn Thain710ddd02014-11-12 16:12:02 +1100348static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200350 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200351 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200353 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200354 return 1;
355 if (!should_be_tagged ||
356 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
357 return 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200358 if (TagAlloc[cmd->device->id][lun].nr_allocated >=
359 TagAlloc[cmd->device->id][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100360 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200361 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200362 return 1;
363 }
364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367
368/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
369 * must be called before!), or reserve the LUN in 'busy' if the command is
370 * untagged.
371 */
372
Finn Thain710ddd02014-11-12 16:12:02 +1100373static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200375 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200376 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Roman Zippelc28bda22007-05-01 22:32:36 +0200378 /* If we or the target don't support tagged queuing, allocate the LUN for
379 * an untagged command.
380 */
381 if (!should_be_tagged ||
382 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
383 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200384 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100385 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200386 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200387 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200388 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Roman Zippelc28bda22007-05-01 22:32:36 +0200390 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
391 set_bit(cmd->tag, ta->allocated);
392 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100393 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200394 "(now %d tags in use)\n",
395 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200396 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400
401/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
402 * unlock the LUN.
403 */
404
Finn Thain710ddd02014-11-12 16:12:02 +1100405static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200407 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200408 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Roman Zippelc28bda22007-05-01 22:32:36 +0200410 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200411 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100412 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200413 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200414 } else if (cmd->tag >= MAX_TAGS) {
415 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
416 H_NO(cmd), cmd->tag);
417 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200418 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200419 clear_bit(cmd->tag, ta->allocated);
420 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100421 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200422 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426
Roman Zippelc28bda22007-05-01 22:32:36 +0200427static void free_all_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Roman Zippelc28bda22007-05-01 22:32:36 +0200429 int target, lun;
430 TAG_ALLOC *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Roman Zippelc28bda22007-05-01 22:32:36 +0200432 if (!setup_use_tagged_queuing)
433 return;
434
435 for (target = 0; target < 8; ++target) {
436 for (lun = 0; lun < 8; ++lun) {
437 ta = &TagAlloc[target][lun];
438 bitmap_zero(ta->allocated, MAX_TAGS);
439 ta->nr_allocated = 0;
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444#endif /* SUPPORT_TAGS */
445
446
447/*
Finn Thain710ddd02014-11-12 16:12:02 +1100448 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
450 * Purpose: Try to merge several scatter-gather requests into one DMA
451 * transfer. This is possible if the scatter buffers lie on
452 * physical contiguous addresses.
453 *
Finn Thain710ddd02014-11-12 16:12:02 +1100454 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300456 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458
Finn Thain710ddd02014-11-12 16:12:02 +1100459static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Roman Zippelc28bda22007-05-01 22:32:36 +0200461 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200463 unsigned long oldlen = cmd->SCp.this_residual;
464 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#endif
466
Roman Zippelc28bda22007-05-01 22:32:36 +0200467 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
468 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200469 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100470 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200471 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200473 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200475 ++cmd->SCp.buffer;
476 --cmd->SCp.buffers_residual;
477 cmd->SCp.this_residual += cmd->SCp.buffer->length;
478 endaddr += cmd->SCp.buffer->length;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200481 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100482 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200483 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#endif
485}
486
487/*
Finn Thain710ddd02014-11-12 16:12:02 +1100488 * Function : void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200490 * Purpose : initialize the saved data pointers for cmd to point to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * start of the buffer.
492 *
Finn Thain710ddd02014-11-12 16:12:02 +1100493 * Inputs : cmd - scsi_cmnd structure to have pointers reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
495
Finn Thain710ddd02014-11-12 16:12:02 +1100496static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Roman Zippelc28bda22007-05-01 22:32:36 +0200498 /*
499 * Initialize the Scsi Pointer field so that all of the commands in the
500 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200502
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200503 if (scsi_bufflen(cmd)) {
504 cmd->SCp.buffer = scsi_sglist(cmd);
505 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200506 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200507 cmd->SCp.this_residual = cmd->SCp.buffer->length;
508 /* ++roman: Try to merge some scatter-buffers if they are at
509 * contiguous physical addresses.
510 */
511 merge_contiguous_buffers(cmd);
512 } else {
513 cmd->SCp.buffer = NULL;
514 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200515 cmd->SCp.ptr = NULL;
516 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#include <linux/delay.h>
521
522#if NDEBUG
523static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200524 unsigned char mask;
525 const char *name;
526} signals[] = {
527 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
528 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
529 { SR_SEL, "SEL" }, {0, NULL}
530}, basrs[] = {
531 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
532}, icrs[] = {
533 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
534 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
535 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
536 {0, NULL}
537}, mrs[] = {
538 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
539 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
540 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
541 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
542 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
543 {0, NULL}
544};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546/*
547 * Function : void NCR5380_print(struct Scsi_Host *instance)
548 *
549 * Purpose : print the SCSI bus signals for debugging purposes
550 *
551 * Input : instance - which NCR5380
552 */
553
Roman Zippelc28bda22007-05-01 22:32:36 +0200554static void NCR5380_print(struct Scsi_Host *instance)
555{
556 unsigned char status, data, basr, mr, icr, i;
557 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Roman Zippelc28bda22007-05-01 22:32:36 +0200559 local_irq_save(flags);
560 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
561 status = NCR5380_read(STATUS_REG);
562 mr = NCR5380_read(MODE_REG);
563 icr = NCR5380_read(INITIATOR_COMMAND_REG);
564 basr = NCR5380_read(BUS_AND_STATUS_REG);
565 local_irq_restore(flags);
566 printk("STATUS_REG: %02x ", status);
567 for (i = 0; signals[i].mask; ++i)
568 if (status & signals[i].mask)
569 printk(",%s", signals[i].name);
570 printk("\nBASR: %02x ", basr);
571 for (i = 0; basrs[i].mask; ++i)
572 if (basr & basrs[i].mask)
573 printk(",%s", basrs[i].name);
574 printk("\nICR: %02x ", icr);
575 for (i = 0; icrs[i].mask; ++i)
576 if (icr & icrs[i].mask)
577 printk(",%s", icrs[i].name);
578 printk("\nMODE: %02x ", mr);
579 for (i = 0; mrs[i].mask; ++i)
580 if (mr & mrs[i].mask)
581 printk(",%s", mrs[i].name);
582 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200586 unsigned char value;
587 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200589 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
590 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
591 {PHASE_UNKNOWN, "UNKNOWN"}
592};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Roman Zippelc28bda22007-05-01 22:32:36 +0200594/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
596 *
597 * Purpose : print the current SCSI phase for debugging purposes
598 *
599 * Input : instance - which NCR5380
600 */
601
602static void NCR5380_print_phase(struct Scsi_Host *instance)
603{
Roman Zippelc28bda22007-05-01 22:32:36 +0200604 unsigned char status;
605 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Roman Zippelc28bda22007-05-01 22:32:36 +0200607 status = NCR5380_read(STATUS_REG);
608 if (!(status & SR_REQ))
609 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
610 else {
611 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
612 (phases[i].value != (status & PHASE_MASK)); ++i)
613 ;
614 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#endif
619
620/*
621 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200622 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * If we're not in an interrupt, we can call our main directly, it cannot be
624 * already running. Else, we queue it on a task queue, if not 'main_running'
625 * tells us that a lower level is already executing it. This way,
626 * 'main_running' needs not be protected in a special way.
627 *
628 * queue_main() is a utility function for putting our main onto the task
629 * queue, if main_running is false. It should be called only from a
630 * interrupt or bottom half.
631 */
632
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900633#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634#include <linux/workqueue.h>
635#include <linux/interrupt.h>
636
Roman Zippelc28bda22007-05-01 22:32:36 +0200637static volatile int main_running;
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200638static DECLARE_WORK(NCR5380_tqueue, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Roman Zippelc28bda22007-05-01 22:32:36 +0200640static inline void queue_main(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Roman Zippelc28bda22007-05-01 22:32:36 +0200642 if (!main_running) {
643 /* If in interrupt and NCR5380_main() not already running,
644 queue it on the 'immediate' task queue, to be processed
645 immediately after the current interrupt processing has
646 finished. */
647 schedule_work(&NCR5380_tqueue);
648 }
649 /* else: nothing to do: the running NCR5380_main() will pick up
650 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653
Roman Zippelc28bda22007-05-01 22:32:36 +0200654static inline void NCR5380_all_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Roman Zippelc28bda22007-05-01 22:32:36 +0200656 static int done = 0;
657 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +1100658 dprintk(NDEBUG_INIT, "scsi : NCR5380_all_init()\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200659 done = 1;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Finn Thain8c325132014-11-12 16:11:58 +1100663/**
664 * NCR58380_info - report driver and host information
665 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *
Finn Thain8c325132014-11-12 16:11:58 +1100667 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 *
Finn Thain8c325132014-11-12 16:11:58 +1100669 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
671
Finn Thain8c325132014-11-12 16:11:58 +1100672static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Finn Thain8c325132014-11-12 16:11:58 +1100674 struct NCR5380_hostdata *hostdata = shost_priv(instance);
675
676 return hostdata->info;
677}
678
679static void prepare_info(struct Scsi_Host *instance)
680{
681 struct NCR5380_hostdata *hostdata = shost_priv(instance);
682
683 snprintf(hostdata->info, sizeof(hostdata->info),
684 "%s, io_port 0x%lx, n_io_port %d, "
685 "base 0x%lx, irq %d, "
686 "can_queue %d, cmd_per_lun %d, "
687 "sg_tablesize %d, this_id %d, "
688 "options { %s} ",
689 instance->hostt->name, instance->io_port, instance->n_io_port,
690 instance->base, instance->irq,
691 instance->can_queue, instance->cmd_per_lun,
692 instance->sg_tablesize, instance->this_id,
693#ifdef DIFFERENTIAL
694 "DIFFERENTIAL "
695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100697 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif
699#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100700 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701#endif
702#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100703 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704#endif
Finn Thain8c325132014-11-12 16:11:58 +1100705 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/*
709 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
710 *
711 * Purpose : print commands in the various queues, called from
712 * NCR5380_abort and NCR5380_debug to aid debugging.
713 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200714 * Inputs : instance, pointer to this instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
716
Finn Thain710ddd02014-11-12 16:12:02 +1100717static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400718{
719 int i, s;
720 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200721 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400722 H_NO(cmd), cmd->device->id, cmd->device->lun);
723 printk(KERN_CONT " command = ");
724 command = cmd->cmnd;
725 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
726 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
727 printk(KERN_CONT " %02x", command[i]);
728 printk("\n");
729}
730
Roman Zippelc28bda22007-05-01 22:32:36 +0200731static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Al Virod89537e2013-03-31 13:24:44 -0400733 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100734 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400735 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Finn Thain8ad3a592014-03-18 11:42:19 +1100737 NCR5380_dprint(NDEBUG_ANY, instance);
738 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Roman Zippelc28bda22007-05-01 22:32:36 +0200740 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Roman Zippelc28bda22007-05-01 22:32:36 +0200742 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400743 printk("NCR5380: coroutine is%s running.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200744 main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200745 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400746 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200747 else
Finn Thain710ddd02014-11-12 16:12:02 +1100748 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400749 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100750 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400751 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Al Virod89537e2013-03-31 13:24:44 -0400753 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100754 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400755 ptr = NEXT(ptr))
756 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200757
758 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400759 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Finn Thain710ddd02014-11-12 16:12:02 +1100762static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Roman Zippelc28bda22007-05-01 22:32:36 +0200764 int i, s;
765 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200766 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200767 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400768 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200769 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400770 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200771 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400772 seq_printf(m, " %02x", command[i]);
773 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Finn Thain4d3d2a52014-11-12 16:11:52 +1100776static int __maybe_unused NCR5380_show_info(struct seq_file *m,
777 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400778{
779 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100780 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400781 unsigned long flags;
782
783 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
784
Al Virod89537e2013-03-31 13:24:44 -0400785 local_irq_save(flags);
786 seq_printf(m, "NCR5380: coroutine is%s running.\n",
787 main_running ? "" : "n't");
788 if (!hostdata->connected)
789 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
790 else
Finn Thain710ddd02014-11-12 16:12:02 +1100791 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400792 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100793 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400794 show_Scsi_Cmnd(ptr, m);
795
796 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100797 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400798 ptr = NEXT(ptr))
799 show_Scsi_Cmnd(ptr, m);
800
801 local_irq_restore(flags);
802 return 0;
803}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Roman Zippelc28bda22007-05-01 22:32:36 +0200805/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * Function : void NCR5380_init (struct Scsi_Host *instance)
807 *
808 * Purpose : initializes *instance and corresponding 5380 chip.
809 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200810 * Inputs : instance - instantiation of the 5380 driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 *
812 * Notes : I assume that the host, hostno, and id bits have been
Roman Zippelc28bda22007-05-01 22:32:36 +0200813 * set correctly. I don't care about the irq and other fields.
814 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
816
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100817static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Roman Zippelc28bda22007-05-01 22:32:36 +0200819 int i;
820 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Roman Zippelc28bda22007-05-01 22:32:36 +0200822 NCR5380_all_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Roman Zippelc28bda22007-05-01 22:32:36 +0200824 hostdata->aborted = 0;
825 hostdata->id_mask = 1 << instance->this_id;
826 hostdata->id_higher_mask = 0;
827 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
828 if (i > hostdata->id_mask)
829 hostdata->id_higher_mask |= i;
830 for (i = 0; i < 8; ++i)
831 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200833 init_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#endif
835#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200836 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200838 hostdata->targets_present = 0;
839 hostdata->connected = NULL;
840 hostdata->issue_queue = NULL;
841 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100842 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Roman Zippelc28bda22007-05-01 22:32:36 +0200844 if (!the_template) {
845 the_template = instance->hostt;
846 first_instance = instance;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Finn Thain8c325132014-11-12 16:11:58 +1100849 prepare_info(instance);
850
Roman Zippelc28bda22007-05-01 22:32:36 +0200851 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
852 NCR5380_write(MODE_REG, MR_BASE);
853 NCR5380_write(TARGET_COMMAND_REG, 0);
854 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Roman Zippelc28bda22007-05-01 22:32:36 +0200856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200859static void NCR5380_exit(struct Scsi_Host *instance)
860{
861 /* Empty, as we didn't schedule any delayed work */
862}
863
Roman Zippelc28bda22007-05-01 22:32:36 +0200864/*
Finn Thain710ddd02014-11-12 16:12:02 +1100865 * Function : int NCR5380_queue_command (struct scsi_cmnd *cmd,
866 * void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *
868 * Purpose : enqueues a SCSI command
869 *
870 * Inputs : cmd - SCSI command, done - function called on completion, with
871 * a pointer to the command descriptor.
Roman Zippelc28bda22007-05-01 22:32:36 +0200872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * Returns : 0
874 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200875 * Side effects :
876 * cmd is added to the per instance issue_queue, with minor
877 * twiddling done to the host specific fields of cmd. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * main coroutine is not running, it is restarted.
879 *
880 */
881
Finn Thain16b29e72014-11-12 16:12:08 +1100882static int NCR5380_queue_command(struct Scsi_Host *instance,
883 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Finn Thain16b29e72014-11-12 16:12:08 +1100885 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100886 struct 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);
Finn Thain16b29e72014-11-12 16:12:08 +1100896 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200897 return 0;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
900
Roman Zippelc28bda22007-05-01 22:32:36 +0200901 /*
902 * We use the host_scribble field as a pointer to the next command
903 * in a queue
904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Roman Zippel3130d902007-05-01 22:32:37 +0200906 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200907 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Roman Zippelc28bda22007-05-01 22:32:36 +0200909 /*
910 * Insert the cmd into the issue queue. Note that REQUEST SENSE
911 * commands are added to the head of the queue since any command will
912 * clear the contingent allegiance condition that exists and the
913 * sense data is only guaranteed to be valid while the condition exists.
914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Roman Zippelc28bda22007-05-01 22:32:36 +0200916 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
917 * Otherwise a running NCR5380_main may steal the lock.
918 * Lock before actually inserting due to fairness reasons explained in
919 * atari_scsi.c. If we insert first, then it's impossible for this driver
920 * to release the lock.
921 * Stop timer for this command while waiting for the lock, or timeouts
922 * may happen (and they really do), and it's no good if the command doesn't
923 * appear in any of the queues.
924 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
925 * because also a timer int can trigger an abort or reset, which would
926 * alter queues and touch the lock.
927 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100928 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100929 return SCSI_MLQUEUE_HOST_BUSY;
930
931 local_irq_save(flags);
932
Roman Zippelc28bda22007-05-01 22:32:36 +0200933 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
934 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200935 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200936 hostdata->issue_queue = cmd;
937 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100938 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200939 NEXT(tmp); tmp = NEXT(tmp))
940 ;
941 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200942 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200943 }
944 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Finn Thaind65e6342014-03-18 11:42:20 +1100946 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200947 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Roman Zippelc28bda22007-05-01 22:32:36 +0200949 /* If queue_command() is called from an interrupt (real one or bottom
950 * half), we let queue_main() do the job of taking care about main. If it
951 * is already running, this is a no-op, else main will be queued.
952 *
953 * If we're not in an interrupt, we can call NCR5380_main()
954 * unconditionally, because it cannot be already running.
955 */
Finn Thain16b29e72014-11-12 16:12:08 +1100956 if (in_interrupt() || irqs_disabled())
Roman Zippelc28bda22007-05-01 22:32:36 +0200957 queue_main();
958 else
959 NCR5380_main(NULL);
960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Finn Thaine3c3da62014-11-12 16:12:15 +1100963static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
964{
965 struct NCR5380_hostdata *hostdata = shost_priv(instance);
966
967 /* Caller does the locking needed to set & test these data atomically */
968 if (!hostdata->disconnected_queue &&
969 !hostdata->issue_queue &&
970 !hostdata->connected &&
971 !hostdata->retain_dma_intr)
972 NCR5380_release_dma_irq(instance);
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/*
Roman Zippelc28bda22007-05-01 22:32:36 +0200976 * Function : NCR5380_main (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200978 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
979 * be done on the NCR5380 host adapters in a system. Both
980 * NCR5380_queue_command() and NCR5380_intr() will try to start it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200982 *
983 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * reenable them. This prevents reentrancy and kernel stack overflow.
Roman Zippelc28bda22007-05-01 22:32:36 +0200985 */
986
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200987static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Finn Thain710ddd02014-11-12 16:12:02 +1100989 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +0200990 struct Scsi_Host *instance = first_instance;
991 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
992 int done;
993 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Roman Zippelc28bda22007-05-01 22:32:36 +0200995 /*
996 * We run (with interrupts disabled) until we're sure that none of
997 * the host adapters have anything that can be done, at which point
998 * we set main_running to 0 and exit.
999 *
1000 * Interrupts are enabled before doing various other internal
1001 * instructions, after we've decided that we need to run through
1002 * the loop again.
1003 *
1004 * this should prevent any race conditions.
1005 *
1006 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1007 * because also a timer int can trigger an abort or reset, which can
1008 * alter queues and touch the Falcon lock.
1009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Roman Zippelc28bda22007-05-01 22:32:36 +02001011 /* Tell int handlers main() is now already executing. Note that
1012 no races are possible here. If an int comes in before
1013 'main_running' is set here, and queues/executes main via the
1014 task queue, it doesn't do any harm, just this instance of main
1015 won't find any work left to do. */
1016 if (main_running)
1017 return;
1018 main_running = 1;
1019
1020 local_save_flags(flags);
1021 do {
1022 local_irq_disable(); /* Freeze request queues */
1023 done = 1;
1024
1025 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001026 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001027 /*
1028 * Search through the issue_queue for a command destined
1029 * for a target that's not busy.
1030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001032 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001033 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1034 ;
1035 /*printk("%p ", tmp);*/
1036 if ((tmp == prev) && tmp)
1037 printk(" LOOP\n");
1038 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001040 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001041 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001042 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +02001045 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001046 printk("MAIN tmp=%p target=%d busy=%d lun=%llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001047 tmp, tmp->device->id, hostdata->busy[tmp->device->id],
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001048 lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001050 /* When we find one, remove it from the issue queue. */
1051 /* ++guenther: possible race with Falcon locking */
1052 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001054 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001056 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001057#endif
1058 ) {
1059 /* ++guenther: just to be sure, this must be atomic */
1060 local_irq_disable();
1061 if (prev) {
1062 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001063 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001064 } else {
1065 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1066 hostdata->issue_queue = NEXT(tmp);
1067 }
Roman Zippel3130d902007-05-01 22:32:37 +02001068 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001069 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001070
1071 /* reenable interrupts after finding one */
1072 local_irq_restore(flags);
1073
1074 /*
1075 * Attempt to establish an I_T_L nexus here.
1076 * On success, instance->hostdata->connected is set.
1077 * On failure, we must add the command back to the
1078 * issue queue so we can keep trying.
1079 */
Finn Thaind65e6342014-03-18 11:42:20 +11001080 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001081 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001082 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001083 /*
1084 * REQUEST SENSE commands are issued without tagged
1085 * queueing, even on SCSI-II devices because the
1086 * contingent allegiance condition exists for the
1087 * entire unit.
1088 */
1089 /* ++roman: ...and the standard also requires that
1090 * REQUEST SENSE command are untagged.
1091 */
1092
1093#ifdef SUPPORT_TAGS
1094 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1095#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001096 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001097 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001098 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001099 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001100 maybe_release_dma_irq(instance);
1101 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001102 break;
1103 } else {
1104 local_irq_disable();
1105 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001106 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001107 hostdata->issue_queue = tmp;
1108#ifdef SUPPORT_TAGS
1109 cmd_free_tag(tmp);
1110#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001111 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001112 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001113 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001114 "returned to issue_queue\n", HOSTNO);
1115 if (hostdata->connected)
1116 break;
1117 }
1118 } /* if target/lun/target queue is not busy */
1119 } /* for issue_queue */
1120 } /* if (!hostdata->connected) */
1121
1122 if (hostdata->connected
1123#ifdef REAL_DMA
1124 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#endif
1126 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001128 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001129 HOSTNO);
1130 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001131 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001132 done = 0;
1133 }
1134 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Roman Zippelc28bda22007-05-01 22:32:36 +02001136 /* Better allow ints _after_ 'main_running' has been cleared, else
1137 an interrupt could believe we'll pick up the work it left for
1138 us, but we won't see it anymore here... */
1139 main_running = 0;
1140 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143
1144#ifdef REAL_DMA
1145/*
1146 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1147 *
1148 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001149 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 *
1151 * Inputs : instance - this instance of the NCR5380.
1152 *
1153 */
1154
Roman Zippelc28bda22007-05-01 22:32:36 +02001155static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Roman Zippelc28bda22007-05-01 22:32:36 +02001157 SETUP_HOSTDATA(instance);
1158 int transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1159 unsigned char **data, p;
1160 volatile int *count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Roman Zippelc28bda22007-05-01 22:32:36 +02001162 if (!hostdata->connected) {
1163 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1164 "no connected cmd\n", HOSTNO);
1165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Finn Thainef1081c2014-11-12 16:12:14 +11001168 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001169 p = hostdata->connected->SCp.phase;
1170 if (p & SR_IO) {
1171 udelay(10);
1172 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1173 (BASR_PHASE_MATCH|BASR_ACK)) ==
1174 (BASR_PHASE_MATCH|BASR_ACK)) {
1175 saved_data = NCR5380_read(INPUT_DATA_REG);
1176 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001177 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001178 }
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001181
Finn Thaind65e6342014-03-18 11:42:20 +11001182 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001183 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1184 NCR5380_read(STATUS_REG));
1185
1186 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1187 NCR5380_write(MODE_REG, MR_BASE);
1188 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1189
1190 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1191 hostdata->dma_len = 0;
1192
1193 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1194 count = &hostdata->connected->SCp.this_residual;
1195 *data += transfered;
1196 *count -= transfered;
1197
Finn Thainef1081c2014-11-12 16:12:14 +11001198 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001199 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001200 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001201 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001202 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001203 *(*data)++ = saved_data;
1204 (*count)--;
1205 cnt--;
1206 toPIO--;
1207 }
Finn Thaind65e6342014-03-18 11:42:20 +11001208 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001209 NCR5380_transfer_pio(instance, &p, &cnt, data);
1210 *count -= toPIO - cnt;
1211 }
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214#endif /* REAL_DMA */
1215
1216
1217/*
1218 * Function : void NCR5380_intr (int irq)
Roman Zippelc28bda22007-05-01 22:32:36 +02001219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
Roman Zippelc28bda22007-05-01 22:32:36 +02001221 * from the disconnected queue, and restarting NCR5380_main()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * as required.
1223 *
1224 * Inputs : int irq, irq that caused this interrupt.
1225 *
1226 */
1227
Roman Zippelc28bda22007-05-01 22:32:36 +02001228static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Roman Zippelc28bda22007-05-01 22:32:36 +02001230 struct Scsi_Host *instance = first_instance;
1231 int done = 1, handled = 0;
1232 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Finn Thaind65e6342014-03-18 11:42:20 +11001234 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Roman Zippelc28bda22007-05-01 22:32:36 +02001236 /* Look for pending interrupts */
1237 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001238 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001239 /* dispatch to appropriate routine if found and done=0 */
1240 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001241 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001242 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1243 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001244 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001245 NCR5380_reselect(instance);
1246 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1247 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001248 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001249 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1250 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001251 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001252 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1253 } else {
1254 /*
1255 * The rest of the interrupt conditions can occur only during a
1256 * DMA transfer
1257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001260 /*
1261 * We should only get PHASE MISMATCH and EOP interrupts if we have
1262 * DMA enabled, so do a sanity check based on the current setting
1263 * of the MODE register.
1264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Roman Zippelc28bda22007-05-01 22:32:36 +02001266 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1267 ((basr & BASR_END_DMA_TRANSFER) ||
1268 !(basr & BASR_PHASE_MATCH))) {
1269
Finn Thaind65e6342014-03-18 11:42:20 +11001270 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001271 NCR5380_dma_complete( instance );
1272 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001273 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001275 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001277 if (basr & BASR_PHASE_MATCH)
1278 printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1279 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1280 HOSTNO, basr, NCR5380_read(MODE_REG),
1281 NCR5380_read(STATUS_REG));
1282 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1283 }
1284 } /* if !(SELECTION || PARITY) */
1285 handled = 1;
1286 } /* BASR & IRQ */ else {
1287 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1288 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1289 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1290 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1291 }
1292
1293 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001294 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001295 /* Put a call to NCR5380_main() on the queue... */
1296 queue_main();
1297 }
1298 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Roman Zippelc28bda22007-05-01 22:32:36 +02001301/*
Finn Thain710ddd02014-11-12 16:12:02 +11001302 * Function : int NCR5380_select(struct Scsi_Host *instance,
1303 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 *
1305 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001306 * including ARBITRATION, SELECTION, and initial message out for
1307 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001309 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001310 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001312 * Returns : -1 if selection could not execute for some reason,
1313 * 0 if selection succeeded or failed because the target
1314 * did not respond.
1315 *
1316 * Side effects :
1317 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * with registers as they should have been on entry - ie
1319 * SELECT_ENABLE will be set appropriately, the NCR5380
1320 * will cease to drive any SCSI bus signals.
1321 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001322 * If successful : I_T_L or I_T_L_Q nexus will be established,
1323 * instance->connected will be set to cmd.
1324 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001326 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * cmd->result host byte set to DID_BAD_TARGET.
1328 */
1329
Finn Thain710ddd02014-11-12 16:12:02 +11001330static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Roman Zippelc28bda22007-05-01 22:32:36 +02001332 SETUP_HOSTDATA(instance);
1333 unsigned char tmp[3], phase;
1334 unsigned char *data;
1335 int len;
1336 unsigned long timeout;
1337 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Roman Zippelc28bda22007-05-01 22:32:36 +02001339 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001340 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001341 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001342 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Roman Zippelc28bda22007-05-01 22:32:36 +02001344 /*
1345 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1346 * data bus during SELECTION.
1347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Roman Zippelc28bda22007-05-01 22:32:36 +02001349 local_irq_save(flags);
1350 if (hostdata->connected) {
1351 local_irq_restore(flags);
1352 return -1;
1353 }
1354 NCR5380_write(TARGET_COMMAND_REG, 0);
1355
1356 /*
1357 * Start arbitration.
1358 */
1359
1360 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1361 NCR5380_write(MODE_REG, MR_ARBITRATE);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Roman Zippelc28bda22007-05-01 22:32:36 +02001365 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001366#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001367 {
1368 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Roman Zippelc28bda22007-05-01 22:32:36 +02001370 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1371 time_before(jiffies, timeout) && !hostdata->connected)
1372 ;
1373 if (time_after_eq(jiffies, timeout)) {
1374 printk("scsi : arbitration timeout at %d\n", __LINE__);
1375 NCR5380_write(MODE_REG, MR_BASE);
1376 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1377 return -1;
1378 }
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001381 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1382 !hostdata->connected)
1383 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif
1385
Finn Thaind65e6342014-03-18 11:42:20 +11001386 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Roman Zippelc28bda22007-05-01 22:32:36 +02001388 if (hostdata->connected) {
1389 NCR5380_write(MODE_REG, MR_BASE);
1390 return -1;
1391 }
1392 /*
1393 * The arbitration delay is 2.2us, but this is a minimum and there is
1394 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1395 * the integral nature of udelay().
1396 *
1397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Roman Zippelc28bda22007-05-01 22:32:36 +02001399 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Roman Zippelc28bda22007-05-01 22:32:36 +02001401 /* Check for lost arbitration */
1402 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1403 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1404 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1405 hostdata->connected) {
1406 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001407 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001408 HOSTNO);
1409 return -1;
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 /* after/during arbitration, BSY should be asserted.
1413 IBM DPES-31080 Version S31Q works now */
1414 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1415 NCR5380_write(INITIATOR_COMMAND_REG,
1416 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Roman Zippelc28bda22007-05-01 22:32:36 +02001418 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1419 hostdata->connected) {
1420 NCR5380_write(MODE_REG, MR_BASE);
1421 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001422 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001423 HOSTNO);
1424 return -1;
1425 }
1426
1427 /*
1428 * Again, bus clear + bus settle time is 1.2us, however, this is
1429 * a minimum so we'll udelay ceil(1.2)
1430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001433 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1434 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001436 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001438
1439 if (hostdata->connected) {
1440 NCR5380_write(MODE_REG, MR_BASE);
1441 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1442 return -1;
1443 }
1444
Finn Thaind65e6342014-03-18 11:42:20 +11001445 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001446
1447 /*
1448 * Now that we have won arbitration, start Selection process, asserting
1449 * the host and target ID's on the SCSI bus.
1450 */
1451
1452 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1453
1454 /*
1455 * Raise ATN while SEL is true before BSY goes false from arbitration,
1456 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1457 * phase immediately after selection.
1458 */
1459
1460 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1461 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Roman Zippelc28bda22007-05-01 22:32:36 +02001464 /*
1465 * Reselect interrupts must be turned off prior to the dropping of BSY,
1466 * otherwise we will trigger an interrupt.
1467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Roman Zippelc28bda22007-05-01 22:32:36 +02001469 if (hostdata->connected) {
1470 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1471 return -1;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Roman Zippelc28bda22007-05-01 22:32:36 +02001474 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Roman Zippelc28bda22007-05-01 22:32:36 +02001476 /*
1477 * The initiator shall then wait at least two deskew delays and release
1478 * the BSY signal.
1479 */
1480 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Roman Zippelc28bda22007-05-01 22:32:36 +02001482 /* Reset BSY */
1483 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1484 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Roman Zippelc28bda22007-05-01 22:32:36 +02001486 /*
1487 * Something weird happens when we cease to drive BSY - looks
1488 * like the board/chip is letting us do another read before the
1489 * appropriate propagation delay has expired, and we're confusing
1490 * a BSY signal from ourselves as the target's response to SELECTION.
1491 *
1492 * A small delay (the 'C++' frontend breaks the pipeline with an
1493 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1494 * tighter 'C' code breaks and requires this) solves the problem -
1495 * the 1 us delay is arbitrary, and only used because this delay will
1496 * be the same on other platforms and since it works here, it should
1497 * work there.
1498 *
1499 * wingel suggests that this could be due to failing to wait
1500 * one deskew delay.
1501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Roman Zippelc28bda22007-05-01 22:32:36 +02001503 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Finn Thaind65e6342014-03-18 11:42:20 +11001505 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Roman Zippelc28bda22007-05-01 22:32:36 +02001507 /*
1508 * The SCSI specification calls for a 250 ms timeout for the actual
1509 * selection.
1510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Roman Zippelc28bda22007-05-01 22:32:36 +02001512 timeout = jiffies + 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 /*
1515 * XXX very interesting - we're seeing a bounce where the BSY we
1516 * asserted is being reflected / still asserted (propagation delay?)
1517 * and it's detecting as true. Sigh.
1518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001521 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1522 * IO while SEL is true. But again, there are some disks out the in the
1523 * world that do that nevertheless. (Somebody claimed that this announces
1524 * reselection capability of the target.) So we better skip that test and
1525 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Roman Zippelc28bda22007-05-01 22:32:36 +02001528 while (time_before(jiffies, timeout) &&
1529 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1530 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Roman Zippelc28bda22007-05-01 22:32:36 +02001532 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1533 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1534 NCR5380_reselect(instance);
1535 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1536 HOSTNO);
1537 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1538 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001541 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1542 ;
1543#endif
1544
1545 /*
1546 * No less than two deskew delays after the initiator detects the
1547 * BSY signal is true, it shall release the SEL signal and may
1548 * change the DATA BUS. -wingel
1549 */
1550
1551 udelay(1);
1552
1553 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1554
1555 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1556 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1557 if (hostdata->targets_present & (1 << cmd->device->id)) {
1558 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1559 if (hostdata->restart_select)
1560 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001561 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001562 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1563 return -1;
1564 }
1565 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001566#ifdef SUPPORT_TAGS
1567 cmd_free_tag(cmd);
1568#endif
1569 cmd->scsi_done(cmd);
1570 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001571 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001572 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1573 return 0;
1574 }
1575
1576 hostdata->targets_present |= (1 << cmd->device->id);
1577
1578 /*
1579 * Since we followed the SCSI spec, and raised ATN while SEL
1580 * was true but before BSY was false during selection, the information
1581 * transfer phase should be a MESSAGE OUT phase so that we can send the
1582 * IDENTIFY message.
1583 *
1584 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1585 * message (2 bytes) with a tag ID that we increment with every command
1586 * until it wraps back to 0.
1587 *
1588 * XXX - it turns out that there are some broken SCSI-II devices,
1589 * which claim to support tagged queuing but fail when more than
1590 * some number of commands are issued at once.
1591 */
1592
1593 /* Wait for start of REQ/ACK handshake */
1594 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1595 ;
1596
Finn Thaind65e6342014-03-18 11:42:20 +11001597 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001598 HOSTNO, cmd->device->id);
1599 tmp[0] = IDENTIFY(1, cmd->device->lun);
1600
1601#ifdef SUPPORT_TAGS
1602 if (cmd->tag != TAG_NONE) {
1603 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1604 tmp[2] = cmd->tag;
1605 len = 3;
1606 } else
1607 len = 1;
1608#else
1609 len = 1;
1610 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611#endif /* SUPPORT_TAGS */
1612
Roman Zippelc28bda22007-05-01 22:32:36 +02001613 /* Send message(s) */
1614 data = tmp;
1615 phase = PHASE_MSGOUT;
1616 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001617 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001618 /* XXX need to handle errors here */
1619 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001621 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Roman Zippelc28bda22007-05-01 22:32:36 +02001624 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Roman Zippelc28bda22007-05-01 22:32:36 +02001626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Roman Zippelc28bda22007-05-01 22:32:36 +02001629/*
1630 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 * unsigned char *phase, int *count, unsigned char **data)
1632 *
1633 * Purpose : transfers data in given phase using polled I/O
1634 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001635 * Inputs : instance - instance of driver, *phase - pointer to
1636 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001638 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001640 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * is in same phase.
1642 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001643 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
1645 * XXX Note : handling for bus free may be useful.
1646 */
1647
1648/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001649 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * IS 100% reliable, and for the actual data transfer where speed
1651 * counts, we will always do a pseudo DMA or DMA transfer.
1652 */
1653
Roman Zippelc28bda22007-05-01 22:32:36 +02001654static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1655 unsigned char *phase, int *count,
1656 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Roman Zippelc28bda22007-05-01 22:32:36 +02001658 register unsigned char p = *phase, tmp;
1659 register int c = *count;
1660 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Roman Zippelc28bda22007-05-01 22:32:36 +02001662 /*
1663 * The NCR5380 chip will only drive the SCSI bus when the
1664 * phase specified in the appropriate bits of the TARGET COMMAND
1665 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
1667
Roman Zippelc28bda22007-05-01 22:32:36 +02001668 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Roman Zippelc28bda22007-05-01 22:32:36 +02001670 do {
1671 /*
1672 * Wait for assertion of REQ, after which the phase bits will be
1673 * valid
1674 */
1675 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1676 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Finn Thaind65e6342014-03-18 11:42:20 +11001678 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Roman Zippelc28bda22007-05-01 22:32:36 +02001680 /* Check for phase mismatch */
1681 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001682 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001683 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001684 break;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Roman Zippelc28bda22007-05-01 22:32:36 +02001687 /* Do actual transfer from SCSI bus to / from memory */
1688 if (!(p & SR_IO))
1689 NCR5380_write(OUTPUT_DATA_REG, *d);
1690 else
1691 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Roman Zippelc28bda22007-05-01 22:32:36 +02001693 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Roman Zippelc28bda22007-05-01 22:32:36 +02001695 /*
1696 * The SCSI standard suggests that in MSGOUT phase, the initiator
1697 * should drop ATN on the last byte of the message phase
1698 * after REQ has been asserted for the handshake but before
1699 * the initiator raises ACK.
1700 */
1701
1702 if (!(p & SR_IO)) {
1703 if (!((p & SR_MSG) && c > 1)) {
1704 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001705 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1707 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1708 } else {
1709 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1710 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001711 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001712 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1713 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1714 }
1715 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001716 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001717 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1718 }
1719
1720 while (NCR5380_read(STATUS_REG) & SR_REQ)
1721 ;
1722
Finn Thaind65e6342014-03-18 11:42:20 +11001723 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001724
1725 /*
1726 * We have several special cases to consider during REQ/ACK handshaking :
1727 * 1. We were in MSGOUT phase, and we are on the last byte of the
1728 * message. ATN must be dropped as ACK is dropped.
1729 *
1730 * 2. We are in a MSGIN phase, and we are on the last byte of the
1731 * message. We must exit with ACK asserted, so that the calling
1732 * code may raise ATN before dropping ACK to reject the message.
1733 *
1734 * 3. ACK and ATN are clear and the target may proceed as normal.
1735 */
1736 if (!(p == PHASE_MSGIN && c == 1)) {
1737 if (p == PHASE_MSGOUT && c > 1)
1738 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1739 else
1740 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1741 }
1742 } while (--c);
1743
Finn Thaind65e6342014-03-18 11:42:20 +11001744 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001745
1746 *count = c;
1747 *data = d;
1748 tmp = NCR5380_read(STATUS_REG);
1749 /* The phase read from the bus is valid if either REQ is (already)
1750 * asserted or if ACK hasn't been released yet. The latter is the case if
1751 * we're in MSGIN and all wanted bytes have been received.
1752 */
1753 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1754 *phase = tmp & PHASE_MASK;
1755 else
1756 *phase = PHASE_UNKNOWN;
1757
1758 if (!c || (*phase == p))
1759 return 0;
1760 else
1761 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
1764/*
1765 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001766 *
1767 * Purpose : abort the currently established nexus. Should only be
1768 * called from a routine which can drop into a
1769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * Returns : 0 on success, -1 on failure.
1771 */
1772
Roman Zippelc28bda22007-05-01 22:32:36 +02001773static int do_abort(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Roman Zippelc28bda22007-05-01 22:32:36 +02001775 unsigned char tmp, *msgptr, phase;
1776 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Roman Zippelc28bda22007-05-01 22:32:36 +02001778 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Roman Zippelc28bda22007-05-01 22:32:36 +02001781 /*
1782 * Wait for the target to indicate a valid phase by asserting
1783 * REQ. Once this happens, we'll have either a MSGOUT phase
1784 * and can immediately send the ABORT message, or we'll have some
1785 * other phase and will have to source/sink data.
1786 *
1787 * We really don't care what value was on the bus or what value
1788 * the target sees, so we just handshake.
1789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Roel Kluin3be38e72007-11-15 21:13:46 +01001791 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001792 ;
1793
1794 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1795
1796 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1797 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1798 ICR_ASSERT_ACK);
1799 while (NCR5380_read(STATUS_REG) & SR_REQ)
1800 ;
1801 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1802 }
1803
1804 tmp = ABORT;
1805 msgptr = &tmp;
1806 len = 1;
1807 phase = PHASE_MSGOUT;
1808 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1809
1810 /*
1811 * If we got here, and the command completed successfully,
1812 * we're about to go into bus free state.
1813 */
1814
1815 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001819/*
1820 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * unsigned char *phase, int *count, unsigned char **data)
1822 *
1823 * Purpose : transfers data in given phase using either real
1824 * or pseudo DMA.
1825 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001826 * Inputs : instance - instance of driver, *phase - pointer to
1827 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001829 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001831 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * is in same phase.
1833 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001834 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *
1836 */
1837
1838
Roman Zippelc28bda22007-05-01 22:32:36 +02001839static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1840 unsigned char *phase, int *count,
1841 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Roman Zippelc28bda22007-05-01 22:32:36 +02001843 SETUP_HOSTDATA(instance);
1844 register int c = *count;
1845 register unsigned char p = *phase;
1846 register unsigned char *d = *data;
1847 unsigned char tmp;
1848 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Roman Zippelc28bda22007-05-01 22:32:36 +02001850 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1851 *phase = tmp;
1852 return -1;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Finn Thainef1081c2014-11-12 16:12:14 +11001855 if (hostdata->read_overruns && (p & SR_IO))
1856 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Finn Thaind65e6342014-03-18 11:42:20 +11001858 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001859 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1860 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Roman Zippelc28bda22007-05-01 22:32:36 +02001862 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001865 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866#endif /* def REAL_DMA */
1867
Finn Thainef1081c2014-11-12 16:12:14 +11001868 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001869 /* On the Medusa, it is a must to initialize the DMA before
1870 * starting the NCR. This is also the cleaner way for the TT.
1871 */
1872 local_irq_save(flags);
1873 hostdata->dma_len = (p & SR_IO) ?
1874 NCR5380_dma_read_setup(instance, d, c) :
1875 NCR5380_dma_write_setup(instance, d, c);
1876 local_irq_restore(flags);
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Roman Zippelc28bda22007-05-01 22:32:36 +02001879 if (p & SR_IO)
1880 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1881 else {
1882 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1883 NCR5380_write(START_DMA_SEND_REG, 0);
1884 }
1885
Finn Thainef1081c2014-11-12 16:12:14 +11001886 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001887 /* On the Falcon, the DMA setup must be done after the last */
1888 /* NCR access, else the DMA setup gets trashed!
1889 */
1890 local_irq_save(flags);
1891 hostdata->dma_len = (p & SR_IO) ?
1892 NCR5380_dma_read_setup(instance, d, c) :
1893 NCR5380_dma_write_setup(instance, d, c);
1894 local_irq_restore(flags);
1895 }
1896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898#endif /* defined(REAL_DMA) */
1899
1900/*
1901 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1902 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001903 * Purpose : run through the various SCSI phases and do as the target
1904 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * instance->connected.
1906 *
1907 * Inputs : instance, instance for which we are doing commands
1908 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001909 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 * modified if a command disconnects, *instance->connected will
1911 * change.
1912 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001913 * XXX Note : we need to watch for bus free or a reset condition here
1914 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001916
1917static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Roman Zippelc28bda22007-05-01 22:32:36 +02001919 SETUP_HOSTDATA(instance);
1920 unsigned long flags;
1921 unsigned char msgout = NOP;
1922 int sink = 0;
1923 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001925 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001927 unsigned char *data;
1928 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001929 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Roman Zippelc28bda22007-05-01 22:32:36 +02001931 while (1) {
1932 tmp = NCR5380_read(STATUS_REG);
1933 /* We only have a valid SCSI phase when REQ is asserted */
1934 if (tmp & SR_REQ) {
1935 phase = (tmp & PHASE_MASK);
1936 if (phase != old_phase) {
1937 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001938 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Roman Zippelc28bda22007-05-01 22:32:36 +02001941 if (sink && (phase != PHASE_MSGOUT)) {
1942 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Roman Zippelc28bda22007-05-01 22:32:36 +02001944 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1945 ICR_ASSERT_ACK);
1946 while (NCR5380_read(STATUS_REG) & SR_REQ)
1947 ;
1948 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1949 ICR_ASSERT_ATN);
1950 sink = 0;
1951 continue;
1952 }
1953
1954 switch (phase) {
1955 case PHASE_DATAOUT:
1956#if (NDEBUG & NDEBUG_NO_DATAOUT)
1957 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
1958 "aborted\n", HOSTNO);
1959 sink = 1;
1960 do_abort(instance);
1961 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001962 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001963 return;
1964#endif
1965 case PHASE_DATAIN:
1966 /*
1967 * If there is no room left in the current buffer in the
1968 * scatter-gather list, move onto the next one.
1969 */
1970
1971 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1972 ++cmd->SCp.buffer;
1973 --cmd->SCp.buffers_residual;
1974 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001975 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02001976 /* ++roman: Try to merge some scatter-buffers if
1977 * they are at contiguous physical addresses.
1978 */
1979 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11001980 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001981 HOSTNO, cmd->SCp.this_residual,
1982 cmd->SCp.buffers_residual);
1983 }
1984
1985 /*
1986 * The preferred transfer method is going to be
1987 * PSEUDO-DMA for systems that are strictly PIO,
1988 * since we can let the hardware do the handshaking.
1989 *
1990 * For this to work, we need to know the transfersize
1991 * ahead of time, since the pseudo-DMA code will sit
1992 * in an unconditional loop.
1993 */
1994
1995 /* ++roman: I suggest, this should be
1996 * #if def(REAL_DMA)
1997 * instead of leaving REAL_DMA out.
1998 */
1999
2000#if defined(REAL_DMA)
2001 if (!cmd->device->borken &&
2002 (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
2003 len = transfersize;
2004 cmd->SCp.phase = phase;
2005 if (NCR5380_transfer_dma(instance, &phase,
2006 &len, (unsigned char **)&cmd->SCp.ptr)) {
2007 /*
2008 * If the watchdog timer fires, all future
2009 * accesses to this device will use the
2010 * polled-IO. */
2011 printk(KERN_NOTICE "scsi%d: switching target %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002012 "lun %llu to slow handshake\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002013 cmd->device->id, cmd->device->lun);
2014 cmd->device->borken = 1;
2015 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2016 ICR_ASSERT_ATN);
2017 sink = 1;
2018 do_abort(instance);
2019 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002020 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002021 /* XXX - need to source or sink data here, as appropriate */
2022 } else {
2023#ifdef REAL_DMA
2024 /* ++roman: When using real DMA,
2025 * information_transfer() should return after
2026 * starting DMA since it has nothing more to
2027 * do.
2028 */
2029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002031 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002033 }
2034 } else
2035#endif /* defined(REAL_DMA) */
2036 NCR5380_transfer_pio(instance, &phase,
2037 (int *)&cmd->SCp.this_residual,
2038 (unsigned char **)&cmd->SCp.ptr);
2039 break;
2040 case PHASE_MSGIN:
2041 len = 1;
2042 data = &tmp;
2043 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2044 NCR5380_transfer_pio(instance, &phase, &len, &data);
2045 cmd->SCp.Message = tmp;
2046
2047 switch (tmp) {
2048 /*
2049 * Linking lets us reduce the time required to get the
2050 * next command out to the device, hopefully this will
2051 * mean we don't waste another revolution due to the delays
2052 * required by ARBITRATION and another SELECTION.
2053 *
2054 * In the current implementation proposal, low level drivers
2055 * merely have to start the next command, pointed to by
2056 * next_link, done() is called as with unlinked commands.
2057 */
2058#ifdef LINKED
2059 case LINKED_CMD_COMPLETE:
2060 case LINKED_FLG_CMD_COMPLETE:
2061 /* Accept message by clearing ACK */
2062 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2063
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002064 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002065 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2066
2067 /* Enable reselect interrupts */
2068 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2069 /*
2070 * Sanity check : A linked command should only terminate
2071 * with one of these messages if there are more linked
2072 * commands available.
2073 */
2074
2075 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002076 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002077 "linked command complete, no next_link\n",
2078 HOSTNO, cmd->device->id, cmd->device->lun);
2079 sink = 1;
2080 do_abort(instance);
2081 return;
2082 }
2083
2084 initialize_SCp(cmd->next_link);
2085 /* The next command is still part of this process; copy it
2086 * and don't free it! */
2087 cmd->next_link->tag = cmd->tag;
2088 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002089 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002090 "done, calling scsi_done().\n",
2091 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002092 cmd->scsi_done(cmd);
2093 cmd = hostdata->connected;
2094 break;
2095#endif /* def LINKED */
2096 case ABORT:
2097 case COMMAND_COMPLETE:
2098 /* Accept message by clearing ACK */
2099 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002100 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002101 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002102
2103 local_irq_save(flags);
2104 hostdata->retain_dma_intr++;
2105 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002106#ifdef SUPPORT_TAGS
2107 cmd_free_tag(cmd);
2108 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2109 /* Turn a QUEUE FULL status into BUSY, I think the
2110 * mid level cannot handle QUEUE FULL :-( (The
2111 * command is retried after BUSY). Also update our
2112 * queue size to the number of currently issued
2113 * commands now.
2114 */
2115 /* ++Andreas: the mid level code knows about
2116 QUEUE_FULL now. */
2117 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002118 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002119 "QUEUE_FULL after %d commands\n",
2120 HOSTNO, cmd->device->id, cmd->device->lun,
2121 ta->nr_allocated);
2122 if (ta->queue_size > ta->nr_allocated)
2123 ta->nr_allocated = ta->queue_size;
2124 }
2125#else
2126 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2127#endif
2128 /* Enable reselect interrupts */
2129 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2130
2131 /*
2132 * I'm not sure what the correct thing to do here is :
2133 *
2134 * If the command that just executed is NOT a request
2135 * sense, the obvious thing to do is to set the result
2136 * code to the values of the stored parameters.
2137 *
2138 * If it was a REQUEST SENSE command, we need some way to
2139 * differentiate between the failure code of the original
2140 * and the failure code of the REQUEST sense - the obvious
2141 * case is success, where we fall through and leave the
2142 * result code unchanged.
2143 *
2144 * The non-obvious place is where the REQUEST SENSE failed
2145 */
2146
2147 if (cmd->cmnd[0] != REQUEST_SENSE)
2148 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2149 else if (status_byte(cmd->SCp.Status) != GOOD)
2150 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2151
Boaz Harrosh28424d32007-09-10 22:37:45 +03002152 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2153 hostdata->ses.cmd_len) {
2154 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2155 hostdata->ses.cmd_len = 0 ;
2156 }
2157
Roman Zippelc28bda22007-05-01 22:32:36 +02002158 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2159 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002160 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002161
Finn Thaind65e6342014-03-18 11:42:20 +11002162 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002163
Roman Zippelc28bda22007-05-01 22:32:36 +02002164 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002165 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002166 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002167 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002168 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002169 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002170 cmd->scsi_done(cmd);
2171 }
2172
Finn Thaine3c3da62014-11-12 16:12:15 +11002173 local_irq_restore(flags);
2174
Roman Zippelc28bda22007-05-01 22:32:36 +02002175 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2176 /*
2177 * Restore phase bits to 0 so an interrupted selection,
2178 * arbitration can resume.
2179 */
2180 NCR5380_write(TARGET_COMMAND_REG, 0);
2181
2182 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2183 barrier();
2184
Finn Thaine3c3da62014-11-12 16:12:15 +11002185 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002186 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002187 /* ++roman: For Falcon SCSI, release the lock on the
2188 * ST-DMA here if no other commands are waiting on the
2189 * disconnected queue.
2190 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002191 maybe_release_dma_irq(instance);
2192 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002193 return;
2194 case MESSAGE_REJECT:
2195 /* Accept message by clearing ACK */
2196 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2197 /* Enable reselect interrupts */
2198 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2199 switch (hostdata->last_message) {
2200 case HEAD_OF_QUEUE_TAG:
2201 case ORDERED_QUEUE_TAG:
2202 case SIMPLE_QUEUE_TAG:
2203 /* The target obviously doesn't support tagged
2204 * queuing, even though it announced this ability in
2205 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2206 * clear 'tagged_supported' and lock the LUN, since
2207 * the command is treated as untagged further on.
2208 */
2209 cmd->device->tagged_supported = 0;
2210 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2211 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002212 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002213 "QUEUE_TAG message; tagged queuing "
2214 "disabled\n",
2215 HOSTNO, cmd->device->id, cmd->device->lun);
2216 break;
2217 }
2218 break;
2219 case DISCONNECT:
2220 /* Accept message by clearing ACK */
2221 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2222 local_irq_save(flags);
2223 cmd->device->disconnect = 1;
2224 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002225 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002226 hostdata->connected = NULL;
2227 hostdata->disconnected_queue = cmd;
2228 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002229 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002230 "moved from connected to the "
2231 "disconnected_queue\n", HOSTNO,
2232 cmd->device->id, cmd->device->lun);
2233 /*
2234 * Restore phase bits to 0 so an interrupted selection,
2235 * arbitration can resume.
2236 */
2237 NCR5380_write(TARGET_COMMAND_REG, 0);
2238
2239 /* Enable reselect interrupts */
2240 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2241 /* Wait for bus free to avoid nasty timeouts */
2242 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2243 barrier();
2244 return;
2245 /*
2246 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2247 * operation, in violation of the SCSI spec so we can safely
2248 * ignore SAVE/RESTORE pointers calls.
2249 *
2250 * Unfortunately, some disks violate the SCSI spec and
2251 * don't issue the required SAVE_POINTERS message before
2252 * disconnecting, and we have to break spec to remain
2253 * compatible.
2254 */
2255 case SAVE_POINTERS:
2256 case RESTORE_POINTERS:
2257 /* Accept message by clearing ACK */
2258 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2259 /* Enable reselect interrupts */
2260 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2261 break;
2262 case EXTENDED_MESSAGE:
2263 /*
2264 * Extended messages are sent in the following format :
2265 * Byte
2266 * 0 EXTENDED_MESSAGE == 1
2267 * 1 length (includes one byte for code, doesn't
2268 * include first two bytes)
2269 * 2 code
2270 * 3..length+1 arguments
2271 *
2272 * Start the extended message buffer with the EXTENDED_MESSAGE
2273 * byte, since spi_print_msg() wants the whole thing.
2274 */
2275 extended_msg[0] = EXTENDED_MESSAGE;
2276 /* Accept first byte by clearing ACK */
2277 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2278
Finn Thaind65e6342014-03-18 11:42:20 +11002279 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002280
2281 len = 2;
2282 data = extended_msg + 1;
2283 phase = PHASE_MSGIN;
2284 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002285 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002286 (int)extended_msg[1], (int)extended_msg[2]);
2287
2288 if (!len && extended_msg[1] <=
2289 (sizeof(extended_msg) - 1)) {
2290 /* Accept third byte by clearing ACK */
2291 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2292 len = extended_msg[1] - 1;
2293 data = extended_msg + 3;
2294 phase = PHASE_MSGIN;
2295
2296 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002297 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002298 HOSTNO, len);
2299
2300 switch (extended_msg[2]) {
2301 case EXTENDED_SDTR:
2302 case EXTENDED_WDTR:
2303 case EXTENDED_MODIFY_DATA_POINTER:
2304 case EXTENDED_EXTENDED_IDENTIFY:
2305 tmp = 0;
2306 }
2307 } else if (len) {
2308 printk(KERN_NOTICE "scsi%d: error receiving "
2309 "extended message\n", HOSTNO);
2310 tmp = 0;
2311 } else {
2312 printk(KERN_NOTICE "scsi%d: extended message "
2313 "code %02x length %d is too long\n",
2314 HOSTNO, extended_msg[2], extended_msg[1]);
2315 tmp = 0;
2316 }
2317 /* Fall through to reject message */
2318
2319 /*
2320 * If we get something weird that we aren't expecting,
2321 * reject it.
2322 */
2323 default:
2324 if (!tmp) {
2325 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2326 spi_print_msg(extended_msg);
2327 printk("\n");
2328 } else if (tmp != EXTENDED_MESSAGE)
2329 printk(KERN_DEBUG "scsi%d: rejecting unknown "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002330 "message %02x from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002331 HOSTNO, tmp, cmd->device->id, cmd->device->lun);
2332 else
2333 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2334 "extended message "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002335 "code %02x, length %d from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002336 HOSTNO, extended_msg[1], extended_msg[0],
2337 cmd->device->id, cmd->device->lun);
2338
2339
2340 msgout = MESSAGE_REJECT;
2341 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2342 break;
2343 } /* switch (tmp) */
2344 break;
2345 case PHASE_MSGOUT:
2346 len = 1;
2347 data = &msgout;
2348 hostdata->last_message = msgout;
2349 NCR5380_transfer_pio(instance, &phase, &len, &data);
2350 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002351 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002352#ifdef SUPPORT_TAGS
2353 cmd_free_tag(cmd);
2354#else
2355 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2356#endif
2357 hostdata->connected = NULL;
2358 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002359 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002360 maybe_release_dma_irq(instance);
2361 local_irq_restore(flags);
2362 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002363 return;
2364 }
2365 msgout = NOP;
2366 break;
2367 case PHASE_CMDOUT:
2368 len = cmd->cmd_len;
2369 data = cmd->cmnd;
2370 /*
2371 * XXX for performance reasons, on machines with a
2372 * PSEUDO-DMA architecture we should probably
2373 * use the dma transfer function.
2374 */
2375 NCR5380_transfer_pio(instance, &phase, &len, &data);
2376 break;
2377 case PHASE_STATIN:
2378 len = 1;
2379 data = &tmp;
2380 NCR5380_transfer_pio(instance, &phase, &len, &data);
2381 cmd->SCp.Status = tmp;
2382 break;
2383 default:
2384 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002385 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002386 } /* switch(phase) */
2387 } /* if (tmp * SR_REQ) */
2388 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
2391/*
2392 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2393 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002394 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002395 * 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 -07002396 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002397 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 * Inputs : instance - this instance of the NCR5380.
2399 *
2400 */
2401
2402
Roman Zippelc28bda22007-05-01 22:32:36 +02002403static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Roman Zippelc28bda22007-05-01 22:32:36 +02002405 SETUP_HOSTDATA(instance);
2406 unsigned char target_mask;
2407 unsigned char lun, phase;
2408 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002410 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002412 unsigned char msg[3];
2413 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002414 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Roman Zippelc28bda22007-05-01 22:32:36 +02002416 /*
2417 * Disable arbitration, etc. since the host adapter obviously
2418 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Roman Zippelc28bda22007-05-01 22:32:36 +02002421 NCR5380_write(MODE_REG, MR_BASE);
2422 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Roman Zippelc28bda22007-05-01 22:32:36 +02002424 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2425
Finn Thaind65e6342014-03-18 11:42:20 +11002426 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002427
2428 /*
2429 * At this point, we have detected that our SCSI ID is on the bus,
2430 * SEL is true and BSY was false for at least one bus settle delay
2431 * (400 ns).
2432 *
2433 * We must assert BSY ourselves, until the target drops the SEL
2434 * signal.
2435 */
2436
2437 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2438
2439 while (NCR5380_read(STATUS_REG) & SR_SEL)
2440 ;
2441 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2442
2443 /*
2444 * Wait for target to go into MSGIN.
2445 */
2446
2447 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2448 ;
2449
2450 len = 1;
2451 data = msg;
2452 phase = PHASE_MSGIN;
2453 NCR5380_transfer_pio(instance, &phase, &len, &data);
2454
2455 if (!(msg[0] & 0x80)) {
2456 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2457 spi_print_msg(msg);
2458 do_abort(instance);
2459 return;
2460 }
2461 lun = (msg[0] & 0x07);
2462
2463#ifdef SUPPORT_TAGS
2464 /* If the phase is still MSGIN, the target wants to send some more
2465 * messages. In case it supports tagged queuing, this is probably a
2466 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2467 */
2468 tag = TAG_NONE;
2469 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2470 /* Accept previous IDENTIFY message by clearing ACK */
2471 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2472 len = 2;
2473 data = msg + 1;
2474 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2475 msg[1] == SIMPLE_QUEUE_TAG)
2476 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002477 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002478 "reselection\n", HOSTNO, target_mask, lun, tag);
2479 }
2480#endif
2481
2482 /*
2483 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2484 * just reestablished, and remove it from the disconnected queue.
2485 */
2486
Finn Thain710ddd02014-11-12 16:12:02 +11002487 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002488 tmp; prev = tmp, tmp = NEXT(tmp)) {
2489 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2490#ifdef SUPPORT_TAGS
2491 && (tag == tmp->tag)
2492#endif
2493 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002494 if (prev) {
2495 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002496 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002497 } else {
2498 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2499 hostdata->disconnected_queue = NEXT(tmp);
2500 }
Roman Zippel3130d902007-05-01 22:32:37 +02002501 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002502 break;
2503 }
2504 }
2505
2506 if (!tmp) {
2507 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2508#ifdef SUPPORT_TAGS
2509 "tag %d "
2510#endif
2511 "not in disconnected_queue.\n",
2512 HOSTNO, target_mask, lun
2513#ifdef SUPPORT_TAGS
2514 , tag
2515#endif
2516 );
2517 /*
2518 * Since we have an established nexus that we can't do anything
2519 * with, we must abort it.
2520 */
2521 do_abort(instance);
2522 return;
2523 }
2524
2525 /* Accept message by clearing ACK */
2526 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2527
2528 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002529 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002530 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533
2534/*
Finn Thain710ddd02014-11-12 16:12:02 +11002535 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 *
2537 * Purpose : abort a command
2538 *
Finn Thain710ddd02014-11-12 16:12:02 +11002539 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002540 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 * used.
2542 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002543 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002545 * XXX - there is no way to abort the command that is currently
2546 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002548 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 */
2550
2551static
Finn Thain710ddd02014-11-12 16:12:02 +11002552int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Roman Zippelc28bda22007-05-01 22:32:36 +02002554 struct Scsi_Host *instance = cmd->device->host;
2555 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002556 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002557 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002559 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Roman Zippelc28bda22007-05-01 22:32:36 +02002561 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Roman Zippelc28bda22007-05-01 22:32:36 +02002563 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Finn Thaind65e6342014-03-18 11:42:20 +11002565 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002566 NCR5380_read(BUS_AND_STATUS_REG),
2567 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002570 /*
2571 * Case 1 : If the command is the currently executing command,
2572 * we'll set the aborted flag and return control so that
2573 * information transfer routine can exit cleanly.
2574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Roman Zippelc28bda22007-05-01 22:32:36 +02002576 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Finn Thaind65e6342014-03-18 11:42:20 +11002578 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002579 /*
2580 * We should perform BSY checking, and make sure we haven't slipped
2581 * into BUS FREE.
2582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Roman Zippelc28bda22007-05-01 22:32:36 +02002584 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2585 /*
2586 * Since we can't change phases until we've completed the current
2587 * handshake, we have to source or sink a byte of data if the current
2588 * phase is not MSGOUT.
2589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Roman Zippelc28bda22007-05-01 22:32:36 +02002591 /*
2592 * Return control to the executing NCR drive so we can clear the
2593 * aborted flag and get back into our main loop.
2594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Roman Zippelc28bda22007-05-01 22:32:36 +02002596 if (do_abort(instance) == 0) {
2597 hostdata->aborted = 1;
2598 hostdata->connected = NULL;
2599 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002601 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002603 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002605 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002606 local_irq_restore(flags);
2607 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002608 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002609 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002610 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002611 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002612 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002616
2617 /*
2618 * Case 2 : If the command hasn't been issued yet, we simply remove it
2619 * from the issue queue.
2620 */
Finn Thain710ddd02014-11-12 16:12:02 +11002621 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2622 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002623 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2624 if (cmd == tmp) {
2625 REMOVE(5, *prev, tmp, NEXT(tmp));
2626 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002627 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002628 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002629 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002630 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002631 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002632 HOSTNO);
2633 /* Tagged queuing note: no tag to free here, hasn't been assigned
2634 * yet... */
2635 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002636 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638 }
2639
Roman Zippelc28bda22007-05-01 22:32:36 +02002640 /*
2641 * Case 3 : If any commands are connected, we're going to fail the abort
2642 * and let the high level SCSI driver retry at a later time or
2643 * issue a reset.
2644 *
2645 * Timeouts, and therefore aborted commands, will be highly unlikely
2646 * and handling them cleanly in this situation would make the common
2647 * case of noresets less efficient, and would pollute our code. So,
2648 * we fail.
2649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Roman Zippelc28bda22007-05-01 22:32:36 +02002651 if (hostdata->connected) {
2652 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002653 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002654 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Roman Zippelc28bda22007-05-01 22:32:36 +02002657 /*
2658 * Case 4: If the command is currently disconnected from the bus, and
2659 * there are no connected commands, we reconnect the I_T_L or
2660 * I_T_L_Q nexus associated with it, go into message out, and send
2661 * an abort message.
2662 *
2663 * This case is especially ugly. In order to reestablish the nexus, we
2664 * need to call NCR5380_select(). The easiest way to implement this
2665 * function was to abort if the bus was busy, and let the interrupt
2666 * handler triggered on the SEL for reselect take care of lost arbitrations
2667 * where necessary, meaning interrupts need to be enabled.
2668 *
2669 * When interrupts are enabled, the queues may change - so we
2670 * can't remove it from the disconnected queue before selecting it
2671 * because that could cause a failure in hashing the nexus if that
2672 * device reselected.
2673 *
2674 * Since the queues may change, we can't use the pointers from when we
2675 * first locate it.
2676 *
2677 * So, we must first locate the command, and if NCR5380_select()
2678 * succeeds, then issue the abort, relocate the command and remove
2679 * it from the disconnected queue.
2680 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Finn Thain710ddd02014-11-12 16:12:02 +11002682 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002683 tmp = NEXT(tmp)) {
2684 if (cmd == tmp) {
2685 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002686 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002687
Finn Thain76f13b92014-11-12 16:11:53 +11002688 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002689 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002690
Finn Thaind65e6342014-03-18 11:42:20 +11002691 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002692
2693 do_abort(instance);
2694
2695 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002696 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2697 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002698 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2699 if (cmd == tmp) {
2700 REMOVE(5, *prev, tmp, NEXT(tmp));
2701 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002702 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002703 tmp->result = DID_ABORT << 16;
2704 /* We must unlock the tag/LUN immediately here, since the
2705 * target goes to BUS FREE and doesn't send us another
2706 * message (COMMAND_COMPLETE or the like)
2707 */
2708#ifdef SUPPORT_TAGS
2709 cmd_free_tag(tmp);
2710#else
2711 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2712#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002713 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002714 local_irq_restore(flags);
2715 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002716 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002717 }
2718 }
2719 }
2720 }
2721
Finn Thaine3c3da62014-11-12 16:12:15 +11002722 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2723 * possible at all) At least, we should check if the lock could be
2724 * released after the abort, in case it is kept due to some bug.
2725 */
2726 maybe_release_dma_irq(instance);
2727 local_irq_restore(flags);
2728
Roman Zippelc28bda22007-05-01 22:32:36 +02002729 /*
2730 * Case 5 : If we reached this point, the command was not found in any of
2731 * the queues.
2732 *
2733 * We probably reached this point because of an unlikely race condition
2734 * between the command completing successfully and the abortion code,
2735 * so we won't panic, but we will notify the user in case something really
2736 * broke.
2737 */
2738
Joe Perchesad361c92009-07-06 13:05:40 -07002739 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002740
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002741 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
2744
Roman Zippelc28bda22007-05-01 22:32:36 +02002745/*
Finn Thain710ddd02014-11-12 16:12:02 +11002746 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002747 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 * Purpose : reset the SCSI bus.
2749 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002750 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Finn Thain710ddd02014-11-12 16:12:02 +11002754static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Finn Thaine3c3da62014-11-12 16:12:15 +11002756 struct Scsi_Host *instance = cmd->device->host;
2757 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002758 int i;
2759 unsigned long flags;
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002760#if defined(RESET_RUN_DONE)
Finn Thain710ddd02014-11-12 16:12:02 +11002761 struct scsi_cmnd *connected, *disconnected_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762#endif
2763
Finn Thaine3c3da62014-11-12 16:12:15 +11002764 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Roman Zippelc28bda22007-05-01 22:32:36 +02002766 /* get in phase */
2767 NCR5380_write(TARGET_COMMAND_REG,
2768 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2769 /* assert RST */
2770 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2771 udelay(40);
2772 /* reset NCR registers */
2773 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2774 NCR5380_write(MODE_REG, MR_BASE);
2775 NCR5380_write(TARGET_COMMAND_REG, 0);
2776 NCR5380_write(SELECT_ENABLE_REG, 0);
2777 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2778 * through anymore ... */
2779 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002781 /* MSch 20140115 - looking at the generic NCR5380 driver, all of this
2782 * should go.
2783 * Catch-22: if we don't clear all queues, the SCSI driver lock will
2784 * not be reset by atari_scsi_reset()!
2785 */
2786
2787#if defined(RESET_RUN_DONE)
2788 /* XXX Should now be done by midlevel code, but it's broken XXX */
Roman Zippelc28bda22007-05-01 22:32:36 +02002789 /* XXX see below XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Roman Zippelc28bda22007-05-01 22:32:36 +02002791 /* MSch: old-style reset: actually abort all command processing here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Roman Zippelc28bda22007-05-01 22:32:36 +02002793 /* After the reset, there are no more connected or disconnected commands
2794 * and no busy units; to avoid problems with re-inserting the commands
2795 * into the issue_queue (via scsi_done()), the aborted commands are
2796 * remembered in local variables first.
2797 */
2798 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002799 connected = (struct scsi_cmnd *)hostdata->connected;
Roman Zippelc28bda22007-05-01 22:32:36 +02002800 hostdata->connected = NULL;
Finn Thain710ddd02014-11-12 16:12:02 +11002801 disconnected_queue = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002802 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002804 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002806 for (i = 0; i < 8; ++i)
2807 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002809 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002811 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Roman Zippelc28bda22007-05-01 22:32:36 +02002813 /* In order to tell the mid-level code which commands were aborted,
2814 * set the command status to DID_RESET and call scsi_done() !!!
2815 * This ultimately aborts processing of these commands in the mid-level.
2816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Roman Zippelc28bda22007-05-01 22:32:36 +02002818 if ((cmd = connected)) {
Finn Thaind65e6342014-03-18 11:42:20 +11002819 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002820 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2821 cmd->scsi_done(cmd);
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Roman Zippelc28bda22007-05-01 22:32:36 +02002824 for (i = 0; (cmd = disconnected_queue); ++i) {
2825 disconnected_queue = NEXT(cmd);
Roman Zippel3130d902007-05-01 22:32:37 +02002826 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002827 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2828 cmd->scsi_done(cmd);
2829 }
2830 if (i > 0)
Finn Thaind65e6342014-03-18 11:42:20 +11002831 dprintk(NDEBUG_ABORT, "scsi: reset aborted %d disconnected command(s)\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Roman Zippelc28bda22007-05-01 22:32:36 +02002833 /* The Falcon lock should be released after a reset...
2834 */
2835 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2836 * unlocking and enabling dma interrupt.
2837 */
2838/* falcon_release_lock_if_possible( hostdata );*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Roman Zippelc28bda22007-05-01 22:32:36 +02002840 /* since all commands have been explicitly terminated, we need to tell
2841 * the midlevel code that the reset was SUCCESSFUL, and there is no
2842 * need to 'wake up' the commands by a request_sense
2843 */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002844 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845#else /* 1 */
2846
Roman Zippelc28bda22007-05-01 22:32:36 +02002847 /* MSch: new-style reset handling: let the mid-level do what it can */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Roman Zippelc28bda22007-05-01 22:32:36 +02002849 /* ++guenther: MID-LEVEL IS STILL BROKEN.
2850 * Mid-level is supposed to requeue all commands that were active on the
2851 * various low-level queues. In fact it does this, but that's not enough
2852 * because all these commands are subject to timeout. And if a timeout
2853 * happens for any removed command, *_abort() is called but all queues
2854 * are now empty. Abort then gives up the falcon lock, which is fatal,
2855 * since the mid-level will queue more commands and must have the lock
2856 * (it's all happening inside timer interrupt handler!!).
2857 * Even worse, abort will return NOT_RUNNING for all those commands not
2858 * on any queue, so they won't be retried ...
2859 *
2860 * Conclusion: either scsi.c disables timeout for all resetted commands
2861 * immediately, or we lose! As of linux-2.0.20 it doesn't.
2862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Roman Zippelc28bda22007-05-01 22:32:36 +02002864 /* After the reset, there are no more connected or disconnected commands
2865 * and no busy units; so clear the low-level status here to avoid
2866 * conflicts when the mid-level code tries to wake up the affected
2867 * commands!
2868 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Roman Zippelc28bda22007-05-01 22:32:36 +02002870 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002871 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002872 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002873 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002874 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002875 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Roman Zippelc28bda22007-05-01 22:32:36 +02002877 local_irq_save(flags);
2878 hostdata->issue_queue = NULL;
2879 hostdata->connected = NULL;
2880 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002882 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002884 for (i = 0; i < 8; ++i)
2885 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002887 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002889
2890 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002891 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Roman Zippelc28bda22007-05-01 22:32:36 +02002893 /* we did no complete reset of all commands, so a wakeup is required */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002894 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895#endif /* 1 */
2896}