blob: 1331b8cf5fe94a0a2efa2decbbd655ab6e77eca5 [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 */
Finn Thaine3f463b2014-11-12 16:12:16 +110074
75/* Adapted for the sun3 by Sam Creasey. */
76
db9dff32005-04-03 14:53:59 -050077#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050078#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +020081#define LIST(x, y) \
82 do { \
83 printk("LINE:%d Adding %p to %p\n", \
84 __LINE__, (void*)(x), (void*)(y)); \
85 if ((x) == (y)) \
86 udelay(5); \
87 } while (0)
88#define REMOVE(w, x, y, z) \
89 do { \
90 printk("LINE:%d Removing: %p->%p %p->%p \n", \
91 __LINE__, (void*)(w), (void*)(x), \
92 (void*)(y), (void*)(z)); \
93 if ((x) == (y)) \
94 udelay(5); \
95 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#else
97#define LIST(x,y)
98#define REMOVE(w,x,y,z)
99#endif
100
101#ifndef notyet
102#undef LINKED
103#endif
104
105/*
106 * Design
107 * Issues :
108 *
109 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
110 * and specifically for each board rather than each chip. This makes their
111 * adaptation to platforms like the Mac (Some of which use NCR5380's)
112 * more difficult than it has to be.
113 *
114 * Also, many of the SCSI drivers were written before the command queuing
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * routines were implemented, meaning their implementations of queued
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * commands were hacked on rather than designed in from the start.
117 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200118 * When I designed the Linux SCSI drivers I figured that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * while having two different SCSI boards in a system might be useful
120 * for debugging things, two of the same type wouldn't be used.
121 * Well, I was wrong and a number of users have mailed me about running
122 * multiple high-performance SCSI boards in a server.
123 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200124 * Finally, when I get questions from users, I have no idea what
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * revision of my driver they are running.
126 *
127 * This driver attempts to address these problems :
Roman Zippelc28bda22007-05-01 22:32:36 +0200128 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200130 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * memory mapped) and drops this file in their 'C' wrapper.
132 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200133 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200135 * and commands that are currently executing. This means that an
136 * unlimited number of commands may be queued, letting
137 * more commands propagate from the higher driver levels giving higher
138 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
139 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * while a command is already executing.
141 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200142 * To solve the multiple-boards-in-the-same-system problem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * there is a separate instance structure for each instance
144 * of a 5380 in the system. So, multiple NCR5380 drivers will
145 * be able to coexist with appropriate changes to the high level
Roman Zippelc28bda22007-05-01 22:32:36 +0200146 * SCSI code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200148 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200150 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
151 * piece of hardware that requires you to sit in a loop polling for
152 * the REQ signal as long as you are connected. Some devices are
153 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * while doing long seek operations.
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * The workaround for this is to keep track of devices that have
157 * disconnected. If the device hasn't disconnected, for commands that
Roman Zippelc28bda22007-05-01 22:32:36 +0200158 * should disconnect, we do something like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
Roman Zippelc28bda22007-05-01 22:32:36 +0200161 *
162 * Some tweaking of N and M needs to be done. An algorithm based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * on "time to data" would give the best results as long as short time
Roman Zippelc28bda22007-05-01 22:32:36 +0200164 * to datas (ie, on the same track) were considered, however these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * broken devices are the exception rather than the rule and I'd rather
166 * spend my time optimizing for the normal case.
167 *
168 * Architecture :
169 *
170 * At the heart of the design is a coroutine, NCR5380_main,
171 * which is started when not running by the interrupt handler,
172 * timer, and queue command function. It attempts to establish
Roman Zippelc28bda22007-05-01 22:32:36 +0200173 * I_T_L or I_T_L_Q nexuses by removing the commands from the
174 * issue queue and calling NCR5380_select() if a nexus
175 * is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Once a nexus is established, the NCR5380_information_transfer()
178 * phase goes through the various phases as instructed by the target.
179 * if the target goes into MSG IN and sends a DISCONNECT message,
180 * the command structure is placed into the per instance disconnected
181 * queue, and NCR5380_main tries to find more work. If USLEEP
182 * was defined, and the target is idle for too long, the system
183 * will try to sleep.
184 *
185 * If a command has disconnected, eventually an interrupt will trigger,
186 * calling NCR5380_intr() which will in turn call NCR5380_reselect
187 * to reestablish a nexus. This will run main if necessary.
188 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200189 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * appropriate.
191 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200192 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * structures, being initialized after the command is connected
194 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
195 * Note that in violation of the standard, an implicit SAVE POINTERS operation
196 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
197 */
198
199/*
200 * Using this file :
201 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200202 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * and macros and include this file in your driver.
204 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200205 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200207 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * LINKED - if defined, linked commands are supported.
210 *
211 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
212 *
213 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
214 *
215 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * NCR5380_read(register) - read from the specified register
218 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200219 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
221 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200222 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200224 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * that they were able to program the controller for.
226 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200227 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * available as NCR5380_i386_dma_write_setup,
229 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
230 *
231 * NCR5380_dma_write_setup(instance, src, count) - initialize
232 * NCR5380_dma_read_setup(instance, dst, count) - initialize
233 * NCR5380_dma_residual(instance); - residual count
234 *
235 * PSEUDO functions :
236 * NCR5380_pwrite(instance, src, count)
237 * NCR5380_pread(instance, dst, count);
238 *
239 * If nothing specific to this implementation needs doing (ie, with external
Roman Zippelc28bda22007-05-01 22:32:36 +0200240 * hardware), you must also define
241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * NCR5380_queue_command
243 * NCR5380_reset
244 * NCR5380_abort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200246 * to be the global entry points into the specific driver, ie
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * #define NCR5380_queue_command t128_queue_command.
248 *
249 * If this is not done, the routines will be defined as static functions
250 * with the NCR5380* names and the user must provide a globally
251 * accessible wrapper function.
252 *
253 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200254 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100256 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258
259static struct Scsi_Host *first_instance = NULL;
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100260static struct scsi_host_template *the_template = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/* Macros ease life... :-) */
263#define SETUP_HOSTDATA(in) \
264 struct NCR5380_hostdata *hostdata = \
265 (struct NCR5380_hostdata *)(in)->hostdata
266#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
267
Finn Thain710ddd02014-11-12 16:12:02 +1100268#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200269#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100270#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272#define HOSTNO instance->host_no
273#define H_NO(cmd) (cmd)->device->host->host_no
274
275#ifdef SUPPORT_TAGS
276
277/*
278 * Functions for handling tagged queuing
279 * =====================================
280 *
281 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
282 *
283 * Using consecutive numbers for the tags is no good idea in my eyes. There
284 * could be wrong re-usings if the counter (8 bit!) wraps and some early
285 * command has been preempted for a long time. My solution: a bitfield for
286 * remembering used tags.
287 *
288 * There's also the problem that each target has a certain queue size, but we
289 * cannot know it in advance :-( We just see a QUEUE_FULL status being
290 * returned. So, in this case, the driver internal queue size assumption is
291 * reduced to the number of active tags if QUEUE_FULL is returned by the
292 * target. The command is returned to the mid-level, but with status changed
293 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
294 * correctly.
295 *
296 * We're also not allowed running tagged commands as long as an untagged
297 * command is active. And REQUEST SENSE commands after a contingent allegiance
298 * condition _must_ be untagged. To keep track whether an untagged command has
299 * been issued, the host->busy array is still employed, as it is without
300 * support for tagged queuing.
301 *
302 * One could suspect that there are possible race conditions between
303 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
304 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
305 * which already guaranteed to be running at most once. It is also the only
306 * place where tags/LUNs are allocated. So no other allocation can slip
307 * between that pair, there could only happen a reselection, which can free a
308 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
309 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
310 */
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312typedef struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200313 DECLARE_BITMAP(allocated, MAX_TAGS);
314 int nr_allocated;
315 int queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316} TAG_ALLOC;
317
Roman Zippelc28bda22007-05-01 22:32:36 +0200318static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320
Roman Zippelc28bda22007-05-01 22:32:36 +0200321static void __init init_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Roman Zippelc28bda22007-05-01 22:32:36 +0200323 int target, lun;
324 TAG_ALLOC *ta;
325
326 if (!setup_use_tagged_queuing)
327 return;
328
329 for (target = 0; target < 8; ++target) {
330 for (lun = 0; lun < 8; ++lun) {
331 ta = &TagAlloc[target][lun];
332 bitmap_zero(ta->allocated, MAX_TAGS);
333 ta->nr_allocated = 0;
334 /* At the beginning, assume the maximum queue size we could
335 * support (MAX_TAGS). This value will be decreased if the target
336 * returns QUEUE_FULL status.
337 */
338 ta->queue_size = MAX_TAGS;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343
344/* Check if we can issue a command to this LUN: First see if the LUN is marked
345 * busy by an untagged command. If the command should use tagged queuing, also
346 * check that there is a free tag and the target's queue won't overflow. This
347 * function should be called with interrupts disabled to avoid race
348 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Finn Thain710ddd02014-11-12 16:12:02 +1100351static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200353 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200354 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200356 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200357 return 1;
358 if (!should_be_tagged ||
359 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
360 return 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200361 if (TagAlloc[cmd->device->id][lun].nr_allocated >=
362 TagAlloc[cmd->device->id][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100363 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200364 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200365 return 1;
366 }
367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370
371/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
372 * must be called before!), or reserve the LUN in 'busy' if the command is
373 * untagged.
374 */
375
Finn Thain710ddd02014-11-12 16:12:02 +1100376static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200378 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200379 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Roman Zippelc28bda22007-05-01 22:32:36 +0200381 /* If we or the target don't support tagged queuing, allocate the LUN for
382 * an untagged command.
383 */
384 if (!should_be_tagged ||
385 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
386 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200387 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100388 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200389 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200390 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200391 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Roman Zippelc28bda22007-05-01 22:32:36 +0200393 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
394 set_bit(cmd->tag, ta->allocated);
395 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100396 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200397 "(now %d tags in use)\n",
398 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200399 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403
404/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
405 * unlock the LUN.
406 */
407
Finn Thain710ddd02014-11-12 16:12:02 +1100408static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200410 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200411 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Roman Zippelc28bda22007-05-01 22:32:36 +0200413 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200414 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100415 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200416 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200417 } else if (cmd->tag >= MAX_TAGS) {
418 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
419 H_NO(cmd), cmd->tag);
420 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200421 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200422 clear_bit(cmd->tag, ta->allocated);
423 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100424 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200425 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429
Roman Zippelc28bda22007-05-01 22:32:36 +0200430static void free_all_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Roman Zippelc28bda22007-05-01 22:32:36 +0200432 int target, lun;
433 TAG_ALLOC *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Roman Zippelc28bda22007-05-01 22:32:36 +0200435 if (!setup_use_tagged_queuing)
436 return;
437
438 for (target = 0; target < 8; ++target) {
439 for (lun = 0; lun < 8; ++lun) {
440 ta = &TagAlloc[target][lun];
441 bitmap_zero(ta->allocated, MAX_TAGS);
442 ta->nr_allocated = 0;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447#endif /* SUPPORT_TAGS */
448
449
450/*
Finn Thain710ddd02014-11-12 16:12:02 +1100451 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
453 * Purpose: Try to merge several scatter-gather requests into one DMA
454 * transfer. This is possible if the scatter buffers lie on
455 * physical contiguous addresses.
456 *
Finn Thain710ddd02014-11-12 16:12:02 +1100457 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300459 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
461
Finn Thain710ddd02014-11-12 16:12:02 +1100462static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Finn Thaine3f463b2014-11-12 16:12:16 +1100464#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200465 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200467 unsigned long oldlen = cmd->SCp.this_residual;
468 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#endif
470
Roman Zippelc28bda22007-05-01 22:32:36 +0200471 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
472 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200473 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100474 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200475 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200477 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200479 ++cmd->SCp.buffer;
480 --cmd->SCp.buffers_residual;
481 cmd->SCp.this_residual += cmd->SCp.buffer->length;
482 endaddr += cmd->SCp.buffer->length;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200485 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100486 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200487 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100489#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
Finn Thain710ddd02014-11-12 16:12:02 +1100493 * Function : void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200495 * Purpose : initialize the saved data pointers for cmd to point to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * start of the buffer.
497 *
Finn Thain710ddd02014-11-12 16:12:02 +1100498 * Inputs : cmd - scsi_cmnd structure to have pointers reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
500
Finn Thain710ddd02014-11-12 16:12:02 +1100501static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Roman Zippelc28bda22007-05-01 22:32:36 +0200503 /*
504 * Initialize the Scsi Pointer field so that all of the commands in the
505 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200507
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200508 if (scsi_bufflen(cmd)) {
509 cmd->SCp.buffer = scsi_sglist(cmd);
510 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200511 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200512 cmd->SCp.this_residual = cmd->SCp.buffer->length;
513 /* ++roman: Try to merge some scatter-buffers if they are at
514 * contiguous physical addresses.
515 */
516 merge_contiguous_buffers(cmd);
517 } else {
518 cmd->SCp.buffer = NULL;
519 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200520 cmd->SCp.ptr = NULL;
521 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#include <linux/delay.h>
526
527#if NDEBUG
528static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200529 unsigned char mask;
530 const char *name;
531} signals[] = {
532 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
533 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
534 { SR_SEL, "SEL" }, {0, NULL}
535}, basrs[] = {
536 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
537}, icrs[] = {
538 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
539 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
540 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
541 {0, NULL}
542}, mrs[] = {
543 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
544 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
545 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
546 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
547 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
548 {0, NULL}
549};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551/*
552 * Function : void NCR5380_print(struct Scsi_Host *instance)
553 *
554 * Purpose : print the SCSI bus signals for debugging purposes
555 *
556 * Input : instance - which NCR5380
557 */
558
Roman Zippelc28bda22007-05-01 22:32:36 +0200559static void NCR5380_print(struct Scsi_Host *instance)
560{
561 unsigned char status, data, basr, mr, icr, i;
562 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Roman Zippelc28bda22007-05-01 22:32:36 +0200564 local_irq_save(flags);
565 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
566 status = NCR5380_read(STATUS_REG);
567 mr = NCR5380_read(MODE_REG);
568 icr = NCR5380_read(INITIATOR_COMMAND_REG);
569 basr = NCR5380_read(BUS_AND_STATUS_REG);
570 local_irq_restore(flags);
571 printk("STATUS_REG: %02x ", status);
572 for (i = 0; signals[i].mask; ++i)
573 if (status & signals[i].mask)
574 printk(",%s", signals[i].name);
575 printk("\nBASR: %02x ", basr);
576 for (i = 0; basrs[i].mask; ++i)
577 if (basr & basrs[i].mask)
578 printk(",%s", basrs[i].name);
579 printk("\nICR: %02x ", icr);
580 for (i = 0; icrs[i].mask; ++i)
581 if (icr & icrs[i].mask)
582 printk(",%s", icrs[i].name);
583 printk("\nMODE: %02x ", mr);
584 for (i = 0; mrs[i].mask; ++i)
585 if (mr & mrs[i].mask)
586 printk(",%s", mrs[i].name);
587 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200591 unsigned char value;
592 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200594 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
595 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
596 {PHASE_UNKNOWN, "UNKNOWN"}
597};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Roman Zippelc28bda22007-05-01 22:32:36 +0200599/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
601 *
602 * Purpose : print the current SCSI phase for debugging purposes
603 *
604 * Input : instance - which NCR5380
605 */
606
607static void NCR5380_print_phase(struct Scsi_Host *instance)
608{
Roman Zippelc28bda22007-05-01 22:32:36 +0200609 unsigned char status;
610 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Roman Zippelc28bda22007-05-01 22:32:36 +0200612 status = NCR5380_read(STATUS_REG);
613 if (!(status & SR_REQ))
614 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
615 else {
616 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
617 (phases[i].value != (status & PHASE_MASK)); ++i)
618 ;
619 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623#endif
624
625/*
626 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200627 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * If we're not in an interrupt, we can call our main directly, it cannot be
629 * already running. Else, we queue it on a task queue, if not 'main_running'
630 * tells us that a lower level is already executing it. This way,
631 * 'main_running' needs not be protected in a special way.
632 *
633 * queue_main() is a utility function for putting our main onto the task
634 * queue, if main_running is false. It should be called only from a
635 * interrupt or bottom half.
636 */
637
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900638#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#include <linux/workqueue.h>
640#include <linux/interrupt.h>
641
Roman Zippelc28bda22007-05-01 22:32:36 +0200642static volatile int main_running;
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200643static DECLARE_WORK(NCR5380_tqueue, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Roman Zippelc28bda22007-05-01 22:32:36 +0200645static inline void queue_main(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Roman Zippelc28bda22007-05-01 22:32:36 +0200647 if (!main_running) {
648 /* If in interrupt and NCR5380_main() not already running,
649 queue it on the 'immediate' task queue, to be processed
650 immediately after the current interrupt processing has
651 finished. */
652 schedule_work(&NCR5380_tqueue);
653 }
654 /* else: nothing to do: the running NCR5380_main() will pick up
655 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658
Roman Zippelc28bda22007-05-01 22:32:36 +0200659static inline void NCR5380_all_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Roman Zippelc28bda22007-05-01 22:32:36 +0200661 static int done = 0;
662 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +1100663 dprintk(NDEBUG_INIT, "scsi : NCR5380_all_init()\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200664 done = 1;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Finn Thain8c325132014-11-12 16:11:58 +1100668/**
669 * NCR58380_info - report driver and host information
670 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
Finn Thain8c325132014-11-12 16:11:58 +1100672 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
Finn Thain8c325132014-11-12 16:11:58 +1100674 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
676
Finn Thain8c325132014-11-12 16:11:58 +1100677static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Finn Thain8c325132014-11-12 16:11:58 +1100679 struct NCR5380_hostdata *hostdata = shost_priv(instance);
680
681 return hostdata->info;
682}
683
684static void prepare_info(struct Scsi_Host *instance)
685{
686 struct NCR5380_hostdata *hostdata = shost_priv(instance);
687
688 snprintf(hostdata->info, sizeof(hostdata->info),
689 "%s, io_port 0x%lx, n_io_port %d, "
690 "base 0x%lx, irq %d, "
691 "can_queue %d, cmd_per_lun %d, "
692 "sg_tablesize %d, this_id %d, "
693 "options { %s} ",
694 instance->hostt->name, instance->io_port, instance->n_io_port,
695 instance->base, instance->irq,
696 instance->can_queue, instance->cmd_per_lun,
697 instance->sg_tablesize, instance->this_id,
698#ifdef DIFFERENTIAL
699 "DIFFERENTIAL "
700#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100702 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#endif
704#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100705 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706#endif
707#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100708 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#endif
Finn Thain8c325132014-11-12 16:11:58 +1100710 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713/*
714 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
715 *
716 * Purpose : print commands in the various queues, called from
717 * NCR5380_abort and NCR5380_debug to aid debugging.
718 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200719 * Inputs : instance, pointer to this instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721
Finn Thain710ddd02014-11-12 16:12:02 +1100722static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400723{
724 int i, s;
725 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200726 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400727 H_NO(cmd), cmd->device->id, cmd->device->lun);
728 printk(KERN_CONT " command = ");
729 command = cmd->cmnd;
730 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
731 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
732 printk(KERN_CONT " %02x", command[i]);
733 printk("\n");
734}
735
Roman Zippelc28bda22007-05-01 22:32:36 +0200736static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Al Virod89537e2013-03-31 13:24:44 -0400738 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100739 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400740 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Finn Thain8ad3a592014-03-18 11:42:19 +1100742 NCR5380_dprint(NDEBUG_ANY, instance);
743 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Roman Zippelc28bda22007-05-01 22:32:36 +0200745 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Roman Zippelc28bda22007-05-01 22:32:36 +0200747 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400748 printk("NCR5380: coroutine is%s running.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200749 main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200750 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400751 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200752 else
Finn Thain710ddd02014-11-12 16:12:02 +1100753 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400754 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100755 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400756 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Al Virod89537e2013-03-31 13:24:44 -0400758 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100759 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400760 ptr = NEXT(ptr))
761 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200762
763 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400764 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Finn Thain710ddd02014-11-12 16:12:02 +1100767static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Roman Zippelc28bda22007-05-01 22:32:36 +0200769 int i, s;
770 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200771 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200772 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400773 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200774 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400775 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200776 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400777 seq_printf(m, " %02x", command[i]);
778 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Finn Thain4d3d2a52014-11-12 16:11:52 +1100781static int __maybe_unused NCR5380_show_info(struct seq_file *m,
782 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400783{
784 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100785 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400786 unsigned long flags;
787
788 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
789
Al Virod89537e2013-03-31 13:24:44 -0400790 local_irq_save(flags);
791 seq_printf(m, "NCR5380: coroutine is%s running.\n",
792 main_running ? "" : "n't");
793 if (!hostdata->connected)
794 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
795 else
Finn Thain710ddd02014-11-12 16:12:02 +1100796 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400797 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100798 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400799 show_Scsi_Cmnd(ptr, m);
800
801 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100802 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400803 ptr = NEXT(ptr))
804 show_Scsi_Cmnd(ptr, m);
805
806 local_irq_restore(flags);
807 return 0;
808}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Roman Zippelc28bda22007-05-01 22:32:36 +0200810/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Function : void NCR5380_init (struct Scsi_Host *instance)
812 *
813 * Purpose : initializes *instance and corresponding 5380 chip.
814 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200815 * Inputs : instance - instantiation of the 5380 driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
817 * Notes : I assume that the host, hostno, and id bits have been
Roman Zippelc28bda22007-05-01 22:32:36 +0200818 * set correctly. I don't care about the irq and other fields.
819 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
821
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100822static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Roman Zippelc28bda22007-05-01 22:32:36 +0200824 int i;
825 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Roman Zippelc28bda22007-05-01 22:32:36 +0200827 NCR5380_all_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Roman Zippelc28bda22007-05-01 22:32:36 +0200829 hostdata->aborted = 0;
830 hostdata->id_mask = 1 << instance->this_id;
831 hostdata->id_higher_mask = 0;
832 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
833 if (i > hostdata->id_mask)
834 hostdata->id_higher_mask |= i;
835 for (i = 0; i < 8; ++i)
836 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200838 init_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839#endif
840#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200841 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200843 hostdata->targets_present = 0;
844 hostdata->connected = NULL;
845 hostdata->issue_queue = NULL;
846 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100847 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Roman Zippelc28bda22007-05-01 22:32:36 +0200849 if (!the_template) {
850 the_template = instance->hostt;
851 first_instance = instance;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Finn Thain8c325132014-11-12 16:11:58 +1100854 prepare_info(instance);
855
Roman Zippelc28bda22007-05-01 22:32:36 +0200856 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
857 NCR5380_write(MODE_REG, MR_BASE);
858 NCR5380_write(TARGET_COMMAND_REG, 0);
859 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Roman Zippelc28bda22007-05-01 22:32:36 +0200861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200864static void NCR5380_exit(struct Scsi_Host *instance)
865{
866 /* Empty, as we didn't schedule any delayed work */
867}
868
Roman Zippelc28bda22007-05-01 22:32:36 +0200869/*
Finn Thain710ddd02014-11-12 16:12:02 +1100870 * Function : int NCR5380_queue_command (struct scsi_cmnd *cmd,
871 * void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
873 * Purpose : enqueues a SCSI command
874 *
875 * Inputs : cmd - SCSI command, done - function called on completion, with
876 * a pointer to the command descriptor.
Roman Zippelc28bda22007-05-01 22:32:36 +0200877 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * Returns : 0
879 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200880 * Side effects :
881 * cmd is added to the per instance issue_queue, with minor
882 * twiddling done to the host specific fields of cmd. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * main coroutine is not running, it is restarted.
884 *
885 */
886
Finn Thain16b29e72014-11-12 16:12:08 +1100887static int NCR5380_queue_command(struct Scsi_Host *instance,
888 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Finn Thain16b29e72014-11-12 16:12:08 +1100890 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100891 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200892 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200895 switch (cmd->cmnd[0]) {
896 case WRITE_6:
897 case WRITE_10:
898 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
899 H_NO(cmd));
900 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100901 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200902 return 0;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
905
Roman Zippelc28bda22007-05-01 22:32:36 +0200906 /*
907 * We use the host_scribble field as a pointer to the next command
908 * in a queue
909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Roman Zippel3130d902007-05-01 22:32:37 +0200911 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200912 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Roman Zippelc28bda22007-05-01 22:32:36 +0200914 /*
915 * Insert the cmd into the issue queue. Note that REQUEST SENSE
916 * commands are added to the head of the queue since any command will
917 * clear the contingent allegiance condition that exists and the
918 * sense data is only guaranteed to be valid while the condition exists.
919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Roman Zippelc28bda22007-05-01 22:32:36 +0200921 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
922 * Otherwise a running NCR5380_main may steal the lock.
923 * Lock before actually inserting due to fairness reasons explained in
924 * atari_scsi.c. If we insert first, then it's impossible for this driver
925 * to release the lock.
926 * Stop timer for this command while waiting for the lock, or timeouts
927 * may happen (and they really do), and it's no good if the command doesn't
928 * appear in any of the queues.
929 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
930 * because also a timer int can trigger an abort or reset, which would
931 * alter queues and touch the lock.
932 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100933 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100934 return SCSI_MLQUEUE_HOST_BUSY;
935
936 local_irq_save(flags);
937
Roman Zippelc28bda22007-05-01 22:32:36 +0200938 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
939 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200940 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200941 hostdata->issue_queue = cmd;
942 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100943 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200944 NEXT(tmp); tmp = NEXT(tmp))
945 ;
946 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200947 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200948 }
949 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Finn Thaind65e6342014-03-18 11:42:20 +1100951 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200952 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Roman Zippelc28bda22007-05-01 22:32:36 +0200954 /* If queue_command() is called from an interrupt (real one or bottom
955 * half), we let queue_main() do the job of taking care about main. If it
956 * is already running, this is a no-op, else main will be queued.
957 *
958 * If we're not in an interrupt, we can call NCR5380_main()
959 * unconditionally, because it cannot be already running.
960 */
Finn Thain16b29e72014-11-12 16:12:08 +1100961 if (in_interrupt() || irqs_disabled())
Roman Zippelc28bda22007-05-01 22:32:36 +0200962 queue_main();
963 else
964 NCR5380_main(NULL);
965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Finn Thaine3c3da62014-11-12 16:12:15 +1100968static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
969{
970 struct NCR5380_hostdata *hostdata = shost_priv(instance);
971
972 /* Caller does the locking needed to set & test these data atomically */
973 if (!hostdata->disconnected_queue &&
974 !hostdata->issue_queue &&
975 !hostdata->connected &&
976 !hostdata->retain_dma_intr)
977 NCR5380_release_dma_irq(instance);
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
Roman Zippelc28bda22007-05-01 22:32:36 +0200981 * Function : NCR5380_main (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200983 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
984 * be done on the NCR5380 host adapters in a system. Both
985 * NCR5380_queue_command() and NCR5380_intr() will try to start it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200987 *
988 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * reenable them. This prevents reentrancy and kernel stack overflow.
Roman Zippelc28bda22007-05-01 22:32:36 +0200990 */
991
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200992static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Finn Thain710ddd02014-11-12 16:12:02 +1100994 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +0200995 struct Scsi_Host *instance = first_instance;
996 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
997 int done;
998 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Roman Zippelc28bda22007-05-01 22:32:36 +02001000 /*
1001 * We run (with interrupts disabled) until we're sure that none of
1002 * the host adapters have anything that can be done, at which point
1003 * we set main_running to 0 and exit.
1004 *
1005 * Interrupts are enabled before doing various other internal
1006 * instructions, after we've decided that we need to run through
1007 * the loop again.
1008 *
1009 * this should prevent any race conditions.
1010 *
1011 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1012 * because also a timer int can trigger an abort or reset, which can
1013 * alter queues and touch the Falcon lock.
1014 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Roman Zippelc28bda22007-05-01 22:32:36 +02001016 /* Tell int handlers main() is now already executing. Note that
1017 no races are possible here. If an int comes in before
1018 'main_running' is set here, and queues/executes main via the
1019 task queue, it doesn't do any harm, just this instance of main
1020 won't find any work left to do. */
1021 if (main_running)
1022 return;
1023 main_running = 1;
1024
1025 local_save_flags(flags);
1026 do {
1027 local_irq_disable(); /* Freeze request queues */
1028 done = 1;
1029
1030 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001031 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001032 /*
1033 * Search through the issue_queue for a command destined
1034 * for a target that's not busy.
1035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001037 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001038 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1039 ;
1040 /*printk("%p ", tmp);*/
1041 if ((tmp == prev) && tmp)
1042 printk(" LOOP\n");
1043 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001045 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001046 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001047 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Finn Thaine3f463b2014-11-12 16:12:16 +11001049 dprintk(NDEBUG_LISTS,
1050 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1051 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1052 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001053 /* When we find one, remove it from the issue queue. */
1054 /* ++guenther: possible race with Falcon locking */
1055 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001057 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001059 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001060#endif
1061 ) {
1062 /* ++guenther: just to be sure, this must be atomic */
1063 local_irq_disable();
1064 if (prev) {
1065 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001066 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001067 } else {
1068 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1069 hostdata->issue_queue = NEXT(tmp);
1070 }
Roman Zippel3130d902007-05-01 22:32:37 +02001071 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001072 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001073
1074 /* reenable interrupts after finding one */
1075 local_irq_restore(flags);
1076
1077 /*
1078 * Attempt to establish an I_T_L nexus here.
1079 * On success, instance->hostdata->connected is set.
1080 * On failure, we must add the command back to the
1081 * issue queue so we can keep trying.
1082 */
Finn Thaind65e6342014-03-18 11:42:20 +11001083 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001084 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001085 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001086 /*
1087 * REQUEST SENSE commands are issued without tagged
1088 * queueing, even on SCSI-II devices because the
1089 * contingent allegiance condition exists for the
1090 * entire unit.
1091 */
1092 /* ++roman: ...and the standard also requires that
1093 * REQUEST SENSE command are untagged.
1094 */
1095
1096#ifdef SUPPORT_TAGS
1097 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1098#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001099 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001100 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001101 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001102 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001103 maybe_release_dma_irq(instance);
1104 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001105 break;
1106 } else {
1107 local_irq_disable();
1108 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001109 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001110 hostdata->issue_queue = tmp;
1111#ifdef SUPPORT_TAGS
1112 cmd_free_tag(tmp);
1113#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001114 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001115 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001116 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001117 "returned to issue_queue\n", HOSTNO);
1118 if (hostdata->connected)
1119 break;
1120 }
1121 } /* if target/lun/target queue is not busy */
1122 } /* for issue_queue */
1123 } /* if (!hostdata->connected) */
1124
1125 if (hostdata->connected
1126#ifdef REAL_DMA
1127 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001131 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001132 HOSTNO);
1133 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001134 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001135 done = 0;
1136 }
1137 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Roman Zippelc28bda22007-05-01 22:32:36 +02001139 /* Better allow ints _after_ 'main_running' has been cleared, else
1140 an interrupt could believe we'll pick up the work it left for
1141 us, but we won't see it anymore here... */
1142 main_running = 0;
1143 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146
1147#ifdef REAL_DMA
1148/*
1149 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1150 *
1151 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001152 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 *
1154 * Inputs : instance - this instance of the NCR5380.
1155 *
1156 */
1157
Roman Zippelc28bda22007-05-01 22:32:36 +02001158static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Roman Zippelc28bda22007-05-01 22:32:36 +02001160 SETUP_HOSTDATA(instance);
Finn Thaine3f463b2014-11-12 16:12:16 +11001161 int transfered;
1162 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001163 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001164 int saved_data = 0, overrun = 0;
1165 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Roman Zippelc28bda22007-05-01 22:32:36 +02001167 if (!hostdata->connected) {
1168 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1169 "no connected cmd\n", HOSTNO);
1170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Finn Thainef1081c2014-11-12 16:12:14 +11001173 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001174 p = hostdata->connected->SCp.phase;
1175 if (p & SR_IO) {
1176 udelay(10);
1177 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1178 (BASR_PHASE_MATCH|BASR_ACK)) ==
1179 (BASR_PHASE_MATCH|BASR_ACK)) {
1180 saved_data = NCR5380_read(INPUT_DATA_REG);
1181 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001182 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001183 }
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001186
Finn Thaind65e6342014-03-18 11:42:20 +11001187 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001188 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1189 NCR5380_read(STATUS_REG));
1190
Finn Thaine3f463b2014-11-12 16:12:16 +11001191#if defined(CONFIG_SUN3)
1192 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1193 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1194 instance->host_no);
1195 BUG();
1196 }
1197
1198 /* make sure we're not stuck in a data phase */
1199 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1200 (BASR_PHASE_MATCH | BASR_ACK)) {
1201 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1202 NCR5380_read(BUS_AND_STATUS_REG));
1203 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1204 instance->host_no);
1205 BUG();
1206 }
1207#endif
1208
Roman Zippelc28bda22007-05-01 22:32:36 +02001209 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1210 NCR5380_write(MODE_REG, MR_BASE);
1211 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1212
1213 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1214 hostdata->dma_len = 0;
1215
1216 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1217 count = &hostdata->connected->SCp.this_residual;
1218 *data += transfered;
1219 *count -= transfered;
1220
Finn Thainef1081c2014-11-12 16:12:14 +11001221 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001222 int cnt, toPIO;
1223
Roman Zippelc28bda22007-05-01 22:32:36 +02001224 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001225 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001226 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001227 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001228 *(*data)++ = saved_data;
1229 (*count)--;
1230 cnt--;
1231 toPIO--;
1232 }
Finn Thaind65e6342014-03-18 11:42:20 +11001233 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001234 NCR5380_transfer_pio(instance, &p, &cnt, data);
1235 *count -= toPIO - cnt;
1236 }
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239#endif /* REAL_DMA */
1240
1241
1242/*
1243 * Function : void NCR5380_intr (int irq)
Roman Zippelc28bda22007-05-01 22:32:36 +02001244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
Roman Zippelc28bda22007-05-01 22:32:36 +02001246 * from the disconnected queue, and restarting NCR5380_main()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * as required.
1248 *
1249 * Inputs : int irq, irq that caused this interrupt.
1250 *
1251 */
1252
Roman Zippelc28bda22007-05-01 22:32:36 +02001253static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Roman Zippelc28bda22007-05-01 22:32:36 +02001255 struct Scsi_Host *instance = first_instance;
1256 int done = 1, handled = 0;
1257 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Finn Thaind65e6342014-03-18 11:42:20 +11001259 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Roman Zippelc28bda22007-05-01 22:32:36 +02001261 /* Look for pending interrupts */
1262 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001263 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001264 /* dispatch to appropriate routine if found and done=0 */
1265 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001266 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001267 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1268 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001269 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001270 NCR5380_reselect(instance);
1271 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1272 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001273 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001274 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1275 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001276 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001277 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1278 } else {
1279 /*
1280 * The rest of the interrupt conditions can occur only during a
1281 * DMA transfer
1282 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001285 /*
1286 * We should only get PHASE MISMATCH and EOP interrupts if we have
1287 * DMA enabled, so do a sanity check based on the current setting
1288 * of the MODE register.
1289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Roman Zippelc28bda22007-05-01 22:32:36 +02001291 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1292 ((basr & BASR_END_DMA_TRANSFER) ||
1293 !(basr & BASR_PHASE_MATCH))) {
1294
Finn Thaind65e6342014-03-18 11:42:20 +11001295 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001296 NCR5380_dma_complete( instance );
1297 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001298 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001300 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001302 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001303 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001304 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1305 HOSTNO, basr, NCR5380_read(MODE_REG),
1306 NCR5380_read(STATUS_REG));
1307 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001308#ifdef SUN3_SCSI_VME
1309 dregs->csr |= CSR_DMA_ENABLE;
1310#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001311 }
1312 } /* if !(SELECTION || PARITY) */
1313 handled = 1;
1314 } /* BASR & IRQ */ else {
1315 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1316 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1317 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1318 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001319#ifdef SUN3_SCSI_VME
1320 dregs->csr |= CSR_DMA_ENABLE;
1321#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001322 }
1323
1324 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001325 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001326 /* Put a call to NCR5380_main() on the queue... */
1327 queue_main();
1328 }
1329 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Roman Zippelc28bda22007-05-01 22:32:36 +02001332/*
Finn Thain710ddd02014-11-12 16:12:02 +11001333 * Function : int NCR5380_select(struct Scsi_Host *instance,
1334 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
1336 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001337 * including ARBITRATION, SELECTION, and initial message out for
1338 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001340 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001341 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001343 * Returns : -1 if selection could not execute for some reason,
1344 * 0 if selection succeeded or failed because the target
1345 * did not respond.
1346 *
1347 * Side effects :
1348 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * with registers as they should have been on entry - ie
1350 * SELECT_ENABLE will be set appropriately, the NCR5380
1351 * will cease to drive any SCSI bus signals.
1352 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001353 * If successful : I_T_L or I_T_L_Q nexus will be established,
1354 * instance->connected will be set to cmd.
1355 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001357 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * cmd->result host byte set to DID_BAD_TARGET.
1359 */
1360
Finn Thain710ddd02014-11-12 16:12:02 +11001361static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Roman Zippelc28bda22007-05-01 22:32:36 +02001363 SETUP_HOSTDATA(instance);
1364 unsigned char tmp[3], phase;
1365 unsigned char *data;
1366 int len;
1367 unsigned long timeout;
1368 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Roman Zippelc28bda22007-05-01 22:32:36 +02001370 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001371 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001372 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001373 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Roman Zippelc28bda22007-05-01 22:32:36 +02001375 /*
1376 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1377 * data bus during SELECTION.
1378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Roman Zippelc28bda22007-05-01 22:32:36 +02001380 local_irq_save(flags);
1381 if (hostdata->connected) {
1382 local_irq_restore(flags);
1383 return -1;
1384 }
1385 NCR5380_write(TARGET_COMMAND_REG, 0);
1386
1387 /*
1388 * Start arbitration.
1389 */
1390
1391 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1392 NCR5380_write(MODE_REG, MR_ARBITRATE);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Roman Zippelc28bda22007-05-01 22:32:36 +02001396 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001397#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001398 {
1399 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Roman Zippelc28bda22007-05-01 22:32:36 +02001401 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1402 time_before(jiffies, timeout) && !hostdata->connected)
1403 ;
1404 if (time_after_eq(jiffies, timeout)) {
1405 printk("scsi : arbitration timeout at %d\n", __LINE__);
1406 NCR5380_write(MODE_REG, MR_BASE);
1407 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1408 return -1;
1409 }
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1413 !hostdata->connected)
1414 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415#endif
1416
Finn Thaind65e6342014-03-18 11:42:20 +11001417 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Roman Zippelc28bda22007-05-01 22:32:36 +02001419 if (hostdata->connected) {
1420 NCR5380_write(MODE_REG, MR_BASE);
1421 return -1;
1422 }
1423 /*
1424 * The arbitration delay is 2.2us, but this is a minimum and there is
1425 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1426 * the integral nature of udelay().
1427 *
1428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Roman Zippelc28bda22007-05-01 22:32:36 +02001430 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Roman Zippelc28bda22007-05-01 22:32:36 +02001432 /* Check for lost arbitration */
1433 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1434 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1435 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1436 hostdata->connected) {
1437 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001438 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001439 HOSTNO);
1440 return -1;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Roman Zippelc28bda22007-05-01 22:32:36 +02001443 /* after/during arbitration, BSY should be asserted.
1444 IBM DPES-31080 Version S31Q works now */
1445 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1446 NCR5380_write(INITIATOR_COMMAND_REG,
1447 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Roman Zippelc28bda22007-05-01 22:32:36 +02001449 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1450 hostdata->connected) {
1451 NCR5380_write(MODE_REG, MR_BASE);
1452 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001453 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001454 HOSTNO);
1455 return -1;
1456 }
1457
1458 /*
1459 * Again, bus clear + bus settle time is 1.2us, however, this is
1460 * a minimum so we'll udelay ceil(1.2)
1461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001464 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1465 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001467 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001469
1470 if (hostdata->connected) {
1471 NCR5380_write(MODE_REG, MR_BASE);
1472 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1473 return -1;
1474 }
1475
Finn Thaind65e6342014-03-18 11:42:20 +11001476 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001477
1478 /*
1479 * Now that we have won arbitration, start Selection process, asserting
1480 * the host and target ID's on the SCSI bus.
1481 */
1482
1483 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1484
1485 /*
1486 * Raise ATN while SEL is true before BSY goes false from arbitration,
1487 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1488 * phase immediately after selection.
1489 */
1490
1491 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1492 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Roman Zippelc28bda22007-05-01 22:32:36 +02001495 /*
1496 * Reselect interrupts must be turned off prior to the dropping of BSY,
1497 * otherwise we will trigger an interrupt.
1498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Roman Zippelc28bda22007-05-01 22:32:36 +02001500 if (hostdata->connected) {
1501 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1502 return -1;
1503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Roman Zippelc28bda22007-05-01 22:32:36 +02001505 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Roman Zippelc28bda22007-05-01 22:32:36 +02001507 /*
1508 * The initiator shall then wait at least two deskew delays and release
1509 * the BSY signal.
1510 */
1511 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Roman Zippelc28bda22007-05-01 22:32:36 +02001513 /* Reset BSY */
1514 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1515 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Roman Zippelc28bda22007-05-01 22:32:36 +02001517 /*
1518 * Something weird happens when we cease to drive BSY - looks
1519 * like the board/chip is letting us do another read before the
1520 * appropriate propagation delay has expired, and we're confusing
1521 * a BSY signal from ourselves as the target's response to SELECTION.
1522 *
1523 * A small delay (the 'C++' frontend breaks the pipeline with an
1524 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1525 * tighter 'C' code breaks and requires this) solves the problem -
1526 * the 1 us delay is arbitrary, and only used because this delay will
1527 * be the same on other platforms and since it works here, it should
1528 * work there.
1529 *
1530 * wingel suggests that this could be due to failing to wait
1531 * one deskew delay.
1532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Roman Zippelc28bda22007-05-01 22:32:36 +02001534 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Finn Thaind65e6342014-03-18 11:42:20 +11001536 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Roman Zippelc28bda22007-05-01 22:32:36 +02001538 /*
1539 * The SCSI specification calls for a 250 ms timeout for the actual
1540 * selection.
1541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Roman Zippelc28bda22007-05-01 22:32:36 +02001543 timeout = jiffies + 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Roman Zippelc28bda22007-05-01 22:32:36 +02001545 /*
1546 * XXX very interesting - we're seeing a bounce where the BSY we
1547 * asserted is being reflected / still asserted (propagation delay?)
1548 * and it's detecting as true. Sigh.
1549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001552 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1553 * IO while SEL is true. But again, there are some disks out the in the
1554 * world that do that nevertheless. (Somebody claimed that this announces
1555 * reselection capability of the target.) So we better skip that test and
1556 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Roman Zippelc28bda22007-05-01 22:32:36 +02001559 while (time_before(jiffies, timeout) &&
1560 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1561 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Roman Zippelc28bda22007-05-01 22:32:36 +02001563 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1564 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1565 NCR5380_reselect(instance);
1566 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1567 HOSTNO);
1568 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1569 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001572 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1573 ;
1574#endif
1575
1576 /*
1577 * No less than two deskew delays after the initiator detects the
1578 * BSY signal is true, it shall release the SEL signal and may
1579 * change the DATA BUS. -wingel
1580 */
1581
1582 udelay(1);
1583
1584 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1585
1586 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1587 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1588 if (hostdata->targets_present & (1 << cmd->device->id)) {
1589 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1590 if (hostdata->restart_select)
1591 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001592 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001593 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1594 return -1;
1595 }
1596 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001597#ifdef SUPPORT_TAGS
1598 cmd_free_tag(cmd);
1599#endif
1600 cmd->scsi_done(cmd);
1601 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001602 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001603 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1604 return 0;
1605 }
1606
1607 hostdata->targets_present |= (1 << cmd->device->id);
1608
1609 /*
1610 * Since we followed the SCSI spec, and raised ATN while SEL
1611 * was true but before BSY was false during selection, the information
1612 * transfer phase should be a MESSAGE OUT phase so that we can send the
1613 * IDENTIFY message.
1614 *
1615 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1616 * message (2 bytes) with a tag ID that we increment with every command
1617 * until it wraps back to 0.
1618 *
1619 * XXX - it turns out that there are some broken SCSI-II devices,
1620 * which claim to support tagged queuing but fail when more than
1621 * some number of commands are issued at once.
1622 */
1623
1624 /* Wait for start of REQ/ACK handshake */
1625 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1626 ;
1627
Finn Thaind65e6342014-03-18 11:42:20 +11001628 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001629 HOSTNO, cmd->device->id);
1630 tmp[0] = IDENTIFY(1, cmd->device->lun);
1631
1632#ifdef SUPPORT_TAGS
1633 if (cmd->tag != TAG_NONE) {
1634 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1635 tmp[2] = cmd->tag;
1636 len = 3;
1637 } else
1638 len = 1;
1639#else
1640 len = 1;
1641 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642#endif /* SUPPORT_TAGS */
1643
Roman Zippelc28bda22007-05-01 22:32:36 +02001644 /* Send message(s) */
1645 data = tmp;
1646 phase = PHASE_MSGOUT;
1647 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001648 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001649 /* XXX need to handle errors here */
1650 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001652 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1653#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001654#ifdef SUN3_SCSI_VME
1655 dregs->csr |= CSR_INTR;
1656#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Roman Zippelc28bda22007-05-01 22:32:36 +02001658 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Roman Zippelc28bda22007-05-01 22:32:36 +02001660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Roman Zippelc28bda22007-05-01 22:32:36 +02001663/*
1664 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 * unsigned char *phase, int *count, unsigned char **data)
1666 *
1667 * Purpose : transfers data in given phase using polled I/O
1668 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001669 * Inputs : instance - instance of driver, *phase - pointer to
1670 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001672 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001674 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * is in same phase.
1676 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001677 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 *
1679 * XXX Note : handling for bus free may be useful.
1680 */
1681
1682/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001683 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 * IS 100% reliable, and for the actual data transfer where speed
1685 * counts, we will always do a pseudo DMA or DMA transfer.
1686 */
1687
Roman Zippelc28bda22007-05-01 22:32:36 +02001688static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1689 unsigned char *phase, int *count,
1690 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Roman Zippelc28bda22007-05-01 22:32:36 +02001692 register unsigned char p = *phase, tmp;
1693 register int c = *count;
1694 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Roman Zippelc28bda22007-05-01 22:32:36 +02001696 /*
1697 * The NCR5380 chip will only drive the SCSI bus when the
1698 * phase specified in the appropriate bits of the TARGET COMMAND
1699 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 */
1701
Roman Zippelc28bda22007-05-01 22:32:36 +02001702 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Roman Zippelc28bda22007-05-01 22:32:36 +02001704 do {
1705 /*
1706 * Wait for assertion of REQ, after which the phase bits will be
1707 * valid
1708 */
1709 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1710 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Finn Thaind65e6342014-03-18 11:42:20 +11001712 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Roman Zippelc28bda22007-05-01 22:32:36 +02001714 /* Check for phase mismatch */
1715 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001716 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001717 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001718 break;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Roman Zippelc28bda22007-05-01 22:32:36 +02001721 /* Do actual transfer from SCSI bus to / from memory */
1722 if (!(p & SR_IO))
1723 NCR5380_write(OUTPUT_DATA_REG, *d);
1724 else
1725 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Roman Zippelc28bda22007-05-01 22:32:36 +02001727 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Roman Zippelc28bda22007-05-01 22:32:36 +02001729 /*
1730 * The SCSI standard suggests that in MSGOUT phase, the initiator
1731 * should drop ATN on the last byte of the message phase
1732 * after REQ has been asserted for the handshake but before
1733 * the initiator raises ACK.
1734 */
1735
1736 if (!(p & SR_IO)) {
1737 if (!((p & SR_MSG) && c > 1)) {
1738 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001739 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001740 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1741 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1742 } else {
1743 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1744 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001745 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001746 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1747 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1748 }
1749 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001750 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001751 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1752 }
1753
1754 while (NCR5380_read(STATUS_REG) & SR_REQ)
1755 ;
1756
Finn Thaind65e6342014-03-18 11:42:20 +11001757 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001758
1759 /*
1760 * We have several special cases to consider during REQ/ACK handshaking :
1761 * 1. We were in MSGOUT phase, and we are on the last byte of the
1762 * message. ATN must be dropped as ACK is dropped.
1763 *
1764 * 2. We are in a MSGIN phase, and we are on the last byte of the
1765 * message. We must exit with ACK asserted, so that the calling
1766 * code may raise ATN before dropping ACK to reject the message.
1767 *
1768 * 3. ACK and ATN are clear and the target may proceed as normal.
1769 */
1770 if (!(p == PHASE_MSGIN && c == 1)) {
1771 if (p == PHASE_MSGOUT && c > 1)
1772 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1773 else
1774 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1775 }
1776 } while (--c);
1777
Finn Thaind65e6342014-03-18 11:42:20 +11001778 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001779
1780 *count = c;
1781 *data = d;
1782 tmp = NCR5380_read(STATUS_REG);
1783 /* The phase read from the bus is valid if either REQ is (already)
1784 * asserted or if ACK hasn't been released yet. The latter is the case if
1785 * we're in MSGIN and all wanted bytes have been received.
1786 */
1787 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1788 *phase = tmp & PHASE_MASK;
1789 else
1790 *phase = PHASE_UNKNOWN;
1791
1792 if (!c || (*phase == p))
1793 return 0;
1794 else
1795 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798/*
1799 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001800 *
1801 * Purpose : abort the currently established nexus. Should only be
1802 * called from a routine which can drop into a
1803 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * Returns : 0 on success, -1 on failure.
1805 */
1806
Roman Zippelc28bda22007-05-01 22:32:36 +02001807static int do_abort(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Roman Zippelc28bda22007-05-01 22:32:36 +02001809 unsigned char tmp, *msgptr, phase;
1810 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Roman Zippelc28bda22007-05-01 22:32:36 +02001812 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Roman Zippelc28bda22007-05-01 22:32:36 +02001815 /*
1816 * Wait for the target to indicate a valid phase by asserting
1817 * REQ. Once this happens, we'll have either a MSGOUT phase
1818 * and can immediately send the ABORT message, or we'll have some
1819 * other phase and will have to source/sink data.
1820 *
1821 * We really don't care what value was on the bus or what value
1822 * the target sees, so we just handshake.
1823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Roel Kluin3be38e72007-11-15 21:13:46 +01001825 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001826 ;
1827
1828 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1829
1830 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1831 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1832 ICR_ASSERT_ACK);
1833 while (NCR5380_read(STATUS_REG) & SR_REQ)
1834 ;
1835 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1836 }
1837
1838 tmp = ABORT;
1839 msgptr = &tmp;
1840 len = 1;
1841 phase = PHASE_MSGOUT;
1842 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1843
1844 /*
1845 * If we got here, and the command completed successfully,
1846 * we're about to go into bus free state.
1847 */
1848
1849 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001853/*
1854 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 * unsigned char *phase, int *count, unsigned char **data)
1856 *
1857 * Purpose : transfers data in given phase using either real
1858 * or pseudo DMA.
1859 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001860 * Inputs : instance - instance of driver, *phase - pointer to
1861 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001865 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * is in same phase.
1867 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001868 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
1870 */
1871
1872
Roman Zippelc28bda22007-05-01 22:32:36 +02001873static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1874 unsigned char *phase, int *count,
1875 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Roman Zippelc28bda22007-05-01 22:32:36 +02001877 SETUP_HOSTDATA(instance);
1878 register int c = *count;
1879 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001880 unsigned long flags;
1881
1882#if defined(CONFIG_SUN3)
1883 /* sanity check */
1884 if (!sun3_dma_setup_done) {
1885 pr_err("scsi%d: transfer_dma without setup!\n",
1886 instance->host_no);
1887 BUG();
1888 }
1889 hostdata->dma_len = c;
1890
1891 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1892 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1893 c, (p & SR_IO) ? "to" : "from", *data);
1894
1895 /* netbsd turns off ints here, why not be safe and do it too */
1896 local_irq_save(flags);
1897
1898 /* send start chain */
1899 sun3scsi_dma_start(c, *data);
1900
1901 if (p & SR_IO) {
1902 NCR5380_write(TARGET_COMMAND_REG, 1);
1903 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1904 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1905 NCR5380_write(MODE_REG,
1906 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1907 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1908 } else {
1909 NCR5380_write(TARGET_COMMAND_REG, 0);
1910 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1911 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1912 NCR5380_write(MODE_REG,
1913 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1914 NCR5380_write(START_DMA_SEND_REG, 0);
1915 }
1916
1917#ifdef SUN3_SCSI_VME
1918 dregs->csr |= CSR_DMA_ENABLE;
1919#endif
1920
1921 local_irq_restore(flags);
1922
1923 sun3_dma_active = 1;
1924
1925#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001926 register unsigned char *d = *data;
1927 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Roman Zippelc28bda22007-05-01 22:32:36 +02001929 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1930 *phase = tmp;
1931 return -1;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Finn Thainef1081c2014-11-12 16:12:14 +11001934 if (hostdata->read_overruns && (p & SR_IO))
1935 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Finn Thaind65e6342014-03-18 11:42:20 +11001937 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001938 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1939 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Roman Zippelc28bda22007-05-01 22:32:36 +02001941 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001944 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945#endif /* def REAL_DMA */
1946
Finn Thainef1081c2014-11-12 16:12:14 +11001947 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001948 /* On the Medusa, it is a must to initialize the DMA before
1949 * starting the NCR. This is also the cleaner way for the TT.
1950 */
1951 local_irq_save(flags);
1952 hostdata->dma_len = (p & SR_IO) ?
1953 NCR5380_dma_read_setup(instance, d, c) :
1954 NCR5380_dma_write_setup(instance, d, c);
1955 local_irq_restore(flags);
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Roman Zippelc28bda22007-05-01 22:32:36 +02001958 if (p & SR_IO)
1959 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1960 else {
1961 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1962 NCR5380_write(START_DMA_SEND_REG, 0);
1963 }
1964
Finn Thainef1081c2014-11-12 16:12:14 +11001965 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001966 /* On the Falcon, the DMA setup must be done after the last */
1967 /* NCR access, else the DMA setup gets trashed!
1968 */
1969 local_irq_save(flags);
1970 hostdata->dma_len = (p & SR_IO) ?
1971 NCR5380_dma_read_setup(instance, d, c) :
1972 NCR5380_dma_write_setup(instance, d, c);
1973 local_irq_restore(flags);
1974 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001975#endif /* !defined(CONFIG_SUN3) */
1976
Roman Zippelc28bda22007-05-01 22:32:36 +02001977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979#endif /* defined(REAL_DMA) */
1980
1981/*
1982 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1983 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001984 * Purpose : run through the various SCSI phases and do as the target
1985 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 * instance->connected.
1987 *
1988 * Inputs : instance, instance for which we are doing commands
1989 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001990 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 * modified if a command disconnects, *instance->connected will
1992 * change.
1993 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001994 * XXX Note : we need to watch for bus free or a reset condition here
1995 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001997
1998static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Roman Zippelc28bda22007-05-01 22:32:36 +02002000 SETUP_HOSTDATA(instance);
2001 unsigned long flags;
2002 unsigned char msgout = NOP;
2003 int sink = 0;
2004 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02002006 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002008 unsigned char *data;
2009 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11002010 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Finn Thaine3f463b2014-11-12 16:12:16 +11002012#ifdef SUN3_SCSI_VME
2013 dregs->csr |= CSR_INTR;
2014#endif
2015
Roman Zippelc28bda22007-05-01 22:32:36 +02002016 while (1) {
2017 tmp = NCR5380_read(STATUS_REG);
2018 /* We only have a valid SCSI phase when REQ is asserted */
2019 if (tmp & SR_REQ) {
2020 phase = (tmp & PHASE_MASK);
2021 if (phase != old_phase) {
2022 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11002023 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002025#if defined(CONFIG_SUN3)
2026 if (phase == PHASE_CMDOUT) {
2027#if defined(REAL_DMA)
2028 void *d;
2029 unsigned long count;
2030
2031 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2032 count = cmd->SCp.buffer->length;
2033 d = sg_virt(cmd->SCp.buffer);
2034 } else {
2035 count = cmd->SCp.this_residual;
2036 d = cmd->SCp.ptr;
2037 }
2038 /* this command setup for dma yet? */
2039 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2040 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2041 sun3scsi_dma_setup(d, count,
2042 rq_data_dir(cmd->request));
2043 sun3_dma_setup_done = cmd;
2044 }
2045 }
2046#endif
2047#ifdef SUN3_SCSI_VME
2048 dregs->csr |= CSR_INTR;
2049#endif
2050 }
2051#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Roman Zippelc28bda22007-05-01 22:32:36 +02002053 if (sink && (phase != PHASE_MSGOUT)) {
2054 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Roman Zippelc28bda22007-05-01 22:32:36 +02002056 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2057 ICR_ASSERT_ACK);
2058 while (NCR5380_read(STATUS_REG) & SR_REQ)
2059 ;
2060 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2061 ICR_ASSERT_ATN);
2062 sink = 0;
2063 continue;
2064 }
2065
2066 switch (phase) {
2067 case PHASE_DATAOUT:
2068#if (NDEBUG & NDEBUG_NO_DATAOUT)
2069 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2070 "aborted\n", HOSTNO);
2071 sink = 1;
2072 do_abort(instance);
2073 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002074 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002075 return;
2076#endif
2077 case PHASE_DATAIN:
2078 /*
2079 * If there is no room left in the current buffer in the
2080 * scatter-gather list, move onto the next one.
2081 */
2082
2083 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2084 ++cmd->SCp.buffer;
2085 --cmd->SCp.buffers_residual;
2086 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002087 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002088 /* ++roman: Try to merge some scatter-buffers if
2089 * they are at contiguous physical addresses.
2090 */
2091 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002092 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002093 HOSTNO, cmd->SCp.this_residual,
2094 cmd->SCp.buffers_residual);
2095 }
2096
2097 /*
2098 * The preferred transfer method is going to be
2099 * PSEUDO-DMA for systems that are strictly PIO,
2100 * since we can let the hardware do the handshaking.
2101 *
2102 * For this to work, we need to know the transfersize
2103 * ahead of time, since the pseudo-DMA code will sit
2104 * in an unconditional loop.
2105 */
2106
2107 /* ++roman: I suggest, this should be
2108 * #if def(REAL_DMA)
2109 * instead of leaving REAL_DMA out.
2110 */
2111
2112#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002113 if (
2114#if !defined(CONFIG_SUN3)
2115 !cmd->device->borken &&
2116#endif
2117 (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002118 len = transfersize;
2119 cmd->SCp.phase = phase;
2120 if (NCR5380_transfer_dma(instance, &phase,
2121 &len, (unsigned char **)&cmd->SCp.ptr)) {
2122 /*
2123 * If the watchdog timer fires, all future
2124 * accesses to this device will use the
2125 * polled-IO. */
2126 printk(KERN_NOTICE "scsi%d: switching target %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002127 "lun %llu to slow handshake\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002128 cmd->device->id, cmd->device->lun);
2129 cmd->device->borken = 1;
2130 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2131 ICR_ASSERT_ATN);
2132 sink = 1;
2133 do_abort(instance);
2134 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002135 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002136 /* XXX - need to source or sink data here, as appropriate */
2137 } else {
2138#ifdef REAL_DMA
2139 /* ++roman: When using real DMA,
2140 * information_transfer() should return after
2141 * starting DMA since it has nothing more to
2142 * do.
2143 */
2144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002146 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002148 }
2149 } else
2150#endif /* defined(REAL_DMA) */
2151 NCR5380_transfer_pio(instance, &phase,
2152 (int *)&cmd->SCp.this_residual,
2153 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002154#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2155 /* if we had intended to dma that command clear it */
2156 if (sun3_dma_setup_done == cmd)
2157 sun3_dma_setup_done = NULL;
2158#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002159 break;
2160 case PHASE_MSGIN:
2161 len = 1;
2162 data = &tmp;
2163 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2164 NCR5380_transfer_pio(instance, &phase, &len, &data);
2165 cmd->SCp.Message = tmp;
2166
2167 switch (tmp) {
2168 /*
2169 * Linking lets us reduce the time required to get the
2170 * next command out to the device, hopefully this will
2171 * mean we don't waste another revolution due to the delays
2172 * required by ARBITRATION and another SELECTION.
2173 *
2174 * In the current implementation proposal, low level drivers
2175 * merely have to start the next command, pointed to by
2176 * next_link, done() is called as with unlinked commands.
2177 */
2178#ifdef LINKED
2179 case LINKED_CMD_COMPLETE:
2180 case LINKED_FLG_CMD_COMPLETE:
2181 /* Accept message by clearing ACK */
2182 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2183
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002184 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002185 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2186
2187 /* Enable reselect interrupts */
2188 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2189 /*
2190 * Sanity check : A linked command should only terminate
2191 * with one of these messages if there are more linked
2192 * commands available.
2193 */
2194
2195 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002196 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002197 "linked command complete, no next_link\n",
2198 HOSTNO, cmd->device->id, cmd->device->lun);
2199 sink = 1;
2200 do_abort(instance);
2201 return;
2202 }
2203
2204 initialize_SCp(cmd->next_link);
2205 /* The next command is still part of this process; copy it
2206 * and don't free it! */
2207 cmd->next_link->tag = cmd->tag;
2208 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002209 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002210 "done, calling scsi_done().\n",
2211 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002212 cmd->scsi_done(cmd);
2213 cmd = hostdata->connected;
2214 break;
2215#endif /* def LINKED */
2216 case ABORT:
2217 case COMMAND_COMPLETE:
2218 /* Accept message by clearing ACK */
2219 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002220 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002221 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002222
2223 local_irq_save(flags);
2224 hostdata->retain_dma_intr++;
2225 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002226#ifdef SUPPORT_TAGS
2227 cmd_free_tag(cmd);
2228 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2229 /* Turn a QUEUE FULL status into BUSY, I think the
2230 * mid level cannot handle QUEUE FULL :-( (The
2231 * command is retried after BUSY). Also update our
2232 * queue size to the number of currently issued
2233 * commands now.
2234 */
2235 /* ++Andreas: the mid level code knows about
2236 QUEUE_FULL now. */
2237 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002238 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002239 "QUEUE_FULL after %d commands\n",
2240 HOSTNO, cmd->device->id, cmd->device->lun,
2241 ta->nr_allocated);
2242 if (ta->queue_size > ta->nr_allocated)
2243 ta->nr_allocated = ta->queue_size;
2244 }
2245#else
2246 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2247#endif
2248 /* Enable reselect interrupts */
2249 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2250
2251 /*
2252 * I'm not sure what the correct thing to do here is :
2253 *
2254 * If the command that just executed is NOT a request
2255 * sense, the obvious thing to do is to set the result
2256 * code to the values of the stored parameters.
2257 *
2258 * If it was a REQUEST SENSE command, we need some way to
2259 * differentiate between the failure code of the original
2260 * and the failure code of the REQUEST sense - the obvious
2261 * case is success, where we fall through and leave the
2262 * result code unchanged.
2263 *
2264 * The non-obvious place is where the REQUEST SENSE failed
2265 */
2266
2267 if (cmd->cmnd[0] != REQUEST_SENSE)
2268 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2269 else if (status_byte(cmd->SCp.Status) != GOOD)
2270 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2271
Boaz Harrosh28424d32007-09-10 22:37:45 +03002272 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2273 hostdata->ses.cmd_len) {
2274 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2275 hostdata->ses.cmd_len = 0 ;
2276 }
2277
Roman Zippelc28bda22007-05-01 22:32:36 +02002278 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2279 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002280 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002281
Finn Thaind65e6342014-03-18 11:42:20 +11002282 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002283
Roman Zippelc28bda22007-05-01 22:32:36 +02002284 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002285 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002286 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002287 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002288 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002289 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002290 cmd->scsi_done(cmd);
2291 }
2292
Finn Thaine3c3da62014-11-12 16:12:15 +11002293 local_irq_restore(flags);
2294
Roman Zippelc28bda22007-05-01 22:32:36 +02002295 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2296 /*
2297 * Restore phase bits to 0 so an interrupted selection,
2298 * arbitration can resume.
2299 */
2300 NCR5380_write(TARGET_COMMAND_REG, 0);
2301
2302 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2303 barrier();
2304
Finn Thaine3c3da62014-11-12 16:12:15 +11002305 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002306 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002307 /* ++roman: For Falcon SCSI, release the lock on the
2308 * ST-DMA here if no other commands are waiting on the
2309 * disconnected queue.
2310 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002311 maybe_release_dma_irq(instance);
2312 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002313 return;
2314 case MESSAGE_REJECT:
2315 /* Accept message by clearing ACK */
2316 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2317 /* Enable reselect interrupts */
2318 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2319 switch (hostdata->last_message) {
2320 case HEAD_OF_QUEUE_TAG:
2321 case ORDERED_QUEUE_TAG:
2322 case SIMPLE_QUEUE_TAG:
2323 /* The target obviously doesn't support tagged
2324 * queuing, even though it announced this ability in
2325 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2326 * clear 'tagged_supported' and lock the LUN, since
2327 * the command is treated as untagged further on.
2328 */
2329 cmd->device->tagged_supported = 0;
2330 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2331 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002332 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002333 "QUEUE_TAG message; tagged queuing "
2334 "disabled\n",
2335 HOSTNO, cmd->device->id, cmd->device->lun);
2336 break;
2337 }
2338 break;
2339 case DISCONNECT:
2340 /* Accept message by clearing ACK */
2341 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2342 local_irq_save(flags);
2343 cmd->device->disconnect = 1;
2344 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002345 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002346 hostdata->connected = NULL;
2347 hostdata->disconnected_queue = cmd;
2348 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002349 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002350 "moved from connected to the "
2351 "disconnected_queue\n", HOSTNO,
2352 cmd->device->id, cmd->device->lun);
2353 /*
2354 * Restore phase bits to 0 so an interrupted selection,
2355 * arbitration can resume.
2356 */
2357 NCR5380_write(TARGET_COMMAND_REG, 0);
2358
2359 /* Enable reselect interrupts */
2360 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2361 /* Wait for bus free to avoid nasty timeouts */
2362 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2363 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002364#ifdef SUN3_SCSI_VME
2365 dregs->csr |= CSR_DMA_ENABLE;
2366#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002367 return;
2368 /*
2369 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2370 * operation, in violation of the SCSI spec so we can safely
2371 * ignore SAVE/RESTORE pointers calls.
2372 *
2373 * Unfortunately, some disks violate the SCSI spec and
2374 * don't issue the required SAVE_POINTERS message before
2375 * disconnecting, and we have to break spec to remain
2376 * compatible.
2377 */
2378 case SAVE_POINTERS:
2379 case RESTORE_POINTERS:
2380 /* Accept message by clearing ACK */
2381 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2382 /* Enable reselect interrupts */
2383 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2384 break;
2385 case EXTENDED_MESSAGE:
2386 /*
2387 * Extended messages are sent in the following format :
2388 * Byte
2389 * 0 EXTENDED_MESSAGE == 1
2390 * 1 length (includes one byte for code, doesn't
2391 * include first two bytes)
2392 * 2 code
2393 * 3..length+1 arguments
2394 *
2395 * Start the extended message buffer with the EXTENDED_MESSAGE
2396 * byte, since spi_print_msg() wants the whole thing.
2397 */
2398 extended_msg[0] = EXTENDED_MESSAGE;
2399 /* Accept first byte by clearing ACK */
2400 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2401
Finn Thaind65e6342014-03-18 11:42:20 +11002402 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002403
2404 len = 2;
2405 data = extended_msg + 1;
2406 phase = PHASE_MSGIN;
2407 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002408 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002409 (int)extended_msg[1], (int)extended_msg[2]);
2410
2411 if (!len && extended_msg[1] <=
2412 (sizeof(extended_msg) - 1)) {
2413 /* Accept third byte by clearing ACK */
2414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2415 len = extended_msg[1] - 1;
2416 data = extended_msg + 3;
2417 phase = PHASE_MSGIN;
2418
2419 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002420 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002421 HOSTNO, len);
2422
2423 switch (extended_msg[2]) {
2424 case EXTENDED_SDTR:
2425 case EXTENDED_WDTR:
2426 case EXTENDED_MODIFY_DATA_POINTER:
2427 case EXTENDED_EXTENDED_IDENTIFY:
2428 tmp = 0;
2429 }
2430 } else if (len) {
2431 printk(KERN_NOTICE "scsi%d: error receiving "
2432 "extended message\n", HOSTNO);
2433 tmp = 0;
2434 } else {
2435 printk(KERN_NOTICE "scsi%d: extended message "
2436 "code %02x length %d is too long\n",
2437 HOSTNO, extended_msg[2], extended_msg[1]);
2438 tmp = 0;
2439 }
2440 /* Fall through to reject message */
2441
2442 /*
2443 * If we get something weird that we aren't expecting,
2444 * reject it.
2445 */
2446 default:
2447 if (!tmp) {
2448 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2449 spi_print_msg(extended_msg);
2450 printk("\n");
2451 } else if (tmp != EXTENDED_MESSAGE)
2452 printk(KERN_DEBUG "scsi%d: rejecting unknown "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002453 "message %02x from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002454 HOSTNO, tmp, cmd->device->id, cmd->device->lun);
2455 else
2456 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2457 "extended message "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002458 "code %02x, length %d from target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002459 HOSTNO, extended_msg[1], extended_msg[0],
2460 cmd->device->id, cmd->device->lun);
2461
2462
2463 msgout = MESSAGE_REJECT;
2464 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2465 break;
2466 } /* switch (tmp) */
2467 break;
2468 case PHASE_MSGOUT:
2469 len = 1;
2470 data = &msgout;
2471 hostdata->last_message = msgout;
2472 NCR5380_transfer_pio(instance, &phase, &len, &data);
2473 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002474 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002475#ifdef SUPPORT_TAGS
2476 cmd_free_tag(cmd);
2477#else
2478 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2479#endif
2480 hostdata->connected = NULL;
2481 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002482 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002483 maybe_release_dma_irq(instance);
2484 local_irq_restore(flags);
2485 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002486 return;
2487 }
2488 msgout = NOP;
2489 break;
2490 case PHASE_CMDOUT:
2491 len = cmd->cmd_len;
2492 data = cmd->cmnd;
2493 /*
2494 * XXX for performance reasons, on machines with a
2495 * PSEUDO-DMA architecture we should probably
2496 * use the dma transfer function.
2497 */
2498 NCR5380_transfer_pio(instance, &phase, &len, &data);
2499 break;
2500 case PHASE_STATIN:
2501 len = 1;
2502 data = &tmp;
2503 NCR5380_transfer_pio(instance, &phase, &len, &data);
2504 cmd->SCp.Status = tmp;
2505 break;
2506 default:
2507 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002508 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002509 } /* switch(phase) */
2510 } /* if (tmp * SR_REQ) */
2511 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514/*
2515 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2516 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002517 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002518 * 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 -07002519 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002520 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 * Inputs : instance - this instance of the NCR5380.
2522 *
2523 */
2524
2525
Finn Thaine3f463b2014-11-12 16:12:16 +11002526/* it might eventually prove necessary to do a dma setup on
2527 reselection, but it doesn't seem to be needed now -- sam */
2528
Roman Zippelc28bda22007-05-01 22:32:36 +02002529static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Roman Zippelc28bda22007-05-01 22:32:36 +02002531 SETUP_HOSTDATA(instance);
2532 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002533 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002535 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002537 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002538 int __maybe_unused len;
2539 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002540 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Roman Zippelc28bda22007-05-01 22:32:36 +02002542 /*
2543 * Disable arbitration, etc. since the host adapter obviously
2544 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Roman Zippelc28bda22007-05-01 22:32:36 +02002547 NCR5380_write(MODE_REG, MR_BASE);
2548 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Roman Zippelc28bda22007-05-01 22:32:36 +02002550 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2551
Finn Thaind65e6342014-03-18 11:42:20 +11002552 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002553
2554 /*
2555 * At this point, we have detected that our SCSI ID is on the bus,
2556 * SEL is true and BSY was false for at least one bus settle delay
2557 * (400 ns).
2558 *
2559 * We must assert BSY ourselves, until the target drops the SEL
2560 * signal.
2561 */
2562
2563 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2564
2565 while (NCR5380_read(STATUS_REG) & SR_SEL)
2566 ;
2567 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2568
2569 /*
2570 * Wait for target to go into MSGIN.
2571 */
2572
2573 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2574 ;
2575
Finn Thaine3f463b2014-11-12 16:12:16 +11002576#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2577 /* acknowledge toggle to MSGIN */
2578 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2579
2580 /* peek at the byte without really hitting the bus */
2581 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2582#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002583 len = 1;
2584 data = msg;
2585 phase = PHASE_MSGIN;
2586 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002587#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002588
2589 if (!(msg[0] & 0x80)) {
2590 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2591 spi_print_msg(msg);
2592 do_abort(instance);
2593 return;
2594 }
2595 lun = (msg[0] & 0x07);
2596
Finn Thaine3f463b2014-11-12 16:12:16 +11002597#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002598 /* If the phase is still MSGIN, the target wants to send some more
2599 * messages. In case it supports tagged queuing, this is probably a
2600 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2601 */
2602 tag = TAG_NONE;
2603 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2604 /* Accept previous IDENTIFY message by clearing ACK */
2605 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2606 len = 2;
2607 data = msg + 1;
2608 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2609 msg[1] == SIMPLE_QUEUE_TAG)
2610 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002611 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002612 "reselection\n", HOSTNO, target_mask, lun, tag);
2613 }
2614#endif
2615
2616 /*
2617 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2618 * just reestablished, and remove it from the disconnected queue.
2619 */
2620
Finn Thain710ddd02014-11-12 16:12:02 +11002621 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002622 tmp; prev = tmp, tmp = NEXT(tmp)) {
2623 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2624#ifdef SUPPORT_TAGS
2625 && (tag == tmp->tag)
2626#endif
2627 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002628 if (prev) {
2629 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002630 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002631 } else {
2632 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2633 hostdata->disconnected_queue = NEXT(tmp);
2634 }
Roman Zippel3130d902007-05-01 22:32:37 +02002635 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002636 break;
2637 }
2638 }
2639
2640 if (!tmp) {
2641 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2642#ifdef SUPPORT_TAGS
2643 "tag %d "
2644#endif
2645 "not in disconnected_queue.\n",
2646 HOSTNO, target_mask, lun
2647#ifdef SUPPORT_TAGS
2648 , tag
2649#endif
2650 );
2651 /*
2652 * Since we have an established nexus that we can't do anything
2653 * with, we must abort it.
2654 */
2655 do_abort(instance);
2656 return;
2657 }
2658
Finn Thaine3f463b2014-11-12 16:12:16 +11002659#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2660 /* engage dma setup for the command we just saw */
2661 {
2662 void *d;
2663 unsigned long count;
2664
2665 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2666 count = tmp->SCp.buffer->length;
2667 d = sg_virt(tmp->SCp.buffer);
2668 } else {
2669 count = tmp->SCp.this_residual;
2670 d = tmp->SCp.ptr;
2671 }
2672 /* setup this command for dma if not already */
2673 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2674 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2675 sun3_dma_setup_done = tmp;
2676 }
2677 }
2678
2679 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2680#endif
2681
Roman Zippelc28bda22007-05-01 22:32:36 +02002682 /* Accept message by clearing ACK */
2683 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2684
Finn Thaine3f463b2014-11-12 16:12:16 +11002685#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2686 /* If the phase is still MSGIN, the target wants to send some more
2687 * messages. In case it supports tagged queuing, this is probably a
2688 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2689 */
2690 tag = TAG_NONE;
2691 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2692 /* Accept previous IDENTIFY message by clearing ACK */
2693 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2694 len = 2;
2695 data = msg + 1;
2696 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2697 msg[1] == SIMPLE_QUEUE_TAG)
2698 tag = msg[2];
2699 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2700 HOSTNO, target_mask, lun, tag);
2701 }
2702#endif
2703
Roman Zippelc28bda22007-05-01 22:32:36 +02002704 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002705 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002706 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
2709
2710/*
Finn Thain710ddd02014-11-12 16:12:02 +11002711 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 *
2713 * Purpose : abort a command
2714 *
Finn Thain710ddd02014-11-12 16:12:02 +11002715 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002716 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 * used.
2718 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002719 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002721 * XXX - there is no way to abort the command that is currently
2722 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002724 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
2726
2727static
Finn Thain710ddd02014-11-12 16:12:02 +11002728int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Roman Zippelc28bda22007-05-01 22:32:36 +02002730 struct Scsi_Host *instance = cmd->device->host;
2731 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002732 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002733 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002735 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Roman Zippelc28bda22007-05-01 22:32:36 +02002737 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Roman Zippelc28bda22007-05-01 22:32:36 +02002739 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Finn Thaind65e6342014-03-18 11:42:20 +11002741 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002742 NCR5380_read(BUS_AND_STATUS_REG),
2743 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002746 /*
2747 * Case 1 : If the command is the currently executing command,
2748 * we'll set the aborted flag and return control so that
2749 * information transfer routine can exit cleanly.
2750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Roman Zippelc28bda22007-05-01 22:32:36 +02002752 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Finn Thaind65e6342014-03-18 11:42:20 +11002754 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002755 /*
2756 * We should perform BSY checking, and make sure we haven't slipped
2757 * into BUS FREE.
2758 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Roman Zippelc28bda22007-05-01 22:32:36 +02002760 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2761 /*
2762 * Since we can't change phases until we've completed the current
2763 * handshake, we have to source or sink a byte of data if the current
2764 * phase is not MSGOUT.
2765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Roman Zippelc28bda22007-05-01 22:32:36 +02002767 /*
2768 * Return control to the executing NCR drive so we can clear the
2769 * aborted flag and get back into our main loop.
2770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Roman Zippelc28bda22007-05-01 22:32:36 +02002772 if (do_abort(instance) == 0) {
2773 hostdata->aborted = 1;
2774 hostdata->connected = NULL;
2775 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002777 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002779 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002781 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002782 local_irq_restore(flags);
2783 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002784 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002785 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002786 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002787 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002788 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002792
2793 /*
2794 * Case 2 : If the command hasn't been issued yet, we simply remove it
2795 * from the issue queue.
2796 */
Finn Thain710ddd02014-11-12 16:12:02 +11002797 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2798 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002799 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2800 if (cmd == tmp) {
2801 REMOVE(5, *prev, tmp, NEXT(tmp));
2802 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002803 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002804 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002805 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002806 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002807 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002808 HOSTNO);
2809 /* Tagged queuing note: no tag to free here, hasn't been assigned
2810 * yet... */
2811 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002812 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
2814 }
2815
Roman Zippelc28bda22007-05-01 22:32:36 +02002816 /*
2817 * Case 3 : If any commands are connected, we're going to fail the abort
2818 * and let the high level SCSI driver retry at a later time or
2819 * issue a reset.
2820 *
2821 * Timeouts, and therefore aborted commands, will be highly unlikely
2822 * and handling them cleanly in this situation would make the common
2823 * case of noresets less efficient, and would pollute our code. So,
2824 * we fail.
2825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Roman Zippelc28bda22007-05-01 22:32:36 +02002827 if (hostdata->connected) {
2828 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002829 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002830 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Roman Zippelc28bda22007-05-01 22:32:36 +02002833 /*
2834 * Case 4: If the command is currently disconnected from the bus, and
2835 * there are no connected commands, we reconnect the I_T_L or
2836 * I_T_L_Q nexus associated with it, go into message out, and send
2837 * an abort message.
2838 *
2839 * This case is especially ugly. In order to reestablish the nexus, we
2840 * need to call NCR5380_select(). The easiest way to implement this
2841 * function was to abort if the bus was busy, and let the interrupt
2842 * handler triggered on the SEL for reselect take care of lost arbitrations
2843 * where necessary, meaning interrupts need to be enabled.
2844 *
2845 * When interrupts are enabled, the queues may change - so we
2846 * can't remove it from the disconnected queue before selecting it
2847 * because that could cause a failure in hashing the nexus if that
2848 * device reselected.
2849 *
2850 * Since the queues may change, we can't use the pointers from when we
2851 * first locate it.
2852 *
2853 * So, we must first locate the command, and if NCR5380_select()
2854 * succeeds, then issue the abort, relocate the command and remove
2855 * it from the disconnected queue.
2856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Finn Thain710ddd02014-11-12 16:12:02 +11002858 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002859 tmp = NEXT(tmp)) {
2860 if (cmd == tmp) {
2861 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002862 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002863
Finn Thain76f13b92014-11-12 16:11:53 +11002864 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002865 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002866
Finn Thaind65e6342014-03-18 11:42:20 +11002867 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002868
2869 do_abort(instance);
2870
2871 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002872 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2873 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002874 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2875 if (cmd == tmp) {
2876 REMOVE(5, *prev, tmp, NEXT(tmp));
2877 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002878 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002879 tmp->result = DID_ABORT << 16;
2880 /* We must unlock the tag/LUN immediately here, since the
2881 * target goes to BUS FREE and doesn't send us another
2882 * message (COMMAND_COMPLETE or the like)
2883 */
2884#ifdef SUPPORT_TAGS
2885 cmd_free_tag(tmp);
2886#else
2887 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2888#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002889 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002890 local_irq_restore(flags);
2891 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002892 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002893 }
2894 }
2895 }
2896 }
2897
Finn Thaine3c3da62014-11-12 16:12:15 +11002898 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2899 * possible at all) At least, we should check if the lock could be
2900 * released after the abort, in case it is kept due to some bug.
2901 */
2902 maybe_release_dma_irq(instance);
2903 local_irq_restore(flags);
2904
Roman Zippelc28bda22007-05-01 22:32:36 +02002905 /*
2906 * Case 5 : If we reached this point, the command was not found in any of
2907 * the queues.
2908 *
2909 * We probably reached this point because of an unlikely race condition
2910 * between the command completing successfully and the abortion code,
2911 * so we won't panic, but we will notify the user in case something really
2912 * broke.
2913 */
2914
Joe Perchesad361c92009-07-06 13:05:40 -07002915 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002916
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002917 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
2920
Roman Zippelc28bda22007-05-01 22:32:36 +02002921/*
Finn Thain710ddd02014-11-12 16:12:02 +11002922 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002923 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 * Purpose : reset the SCSI bus.
2925 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002926 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Finn Thain710ddd02014-11-12 16:12:02 +11002930static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Finn Thaine3c3da62014-11-12 16:12:15 +11002932 struct Scsi_Host *instance = cmd->device->host;
2933 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002934 int i;
2935 unsigned long flags;
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002936#if defined(RESET_RUN_DONE)
Finn Thain710ddd02014-11-12 16:12:02 +11002937 struct scsi_cmnd *connected, *disconnected_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938#endif
2939
Finn Thaine3c3da62014-11-12 16:12:15 +11002940 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Roman Zippelc28bda22007-05-01 22:32:36 +02002942 /* get in phase */
2943 NCR5380_write(TARGET_COMMAND_REG,
2944 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2945 /* assert RST */
2946 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2947 udelay(40);
2948 /* reset NCR registers */
2949 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2950 NCR5380_write(MODE_REG, MR_BASE);
2951 NCR5380_write(TARGET_COMMAND_REG, 0);
2952 NCR5380_write(SELECT_ENABLE_REG, 0);
2953 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2954 * through anymore ... */
2955 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002957 /* MSch 20140115 - looking at the generic NCR5380 driver, all of this
2958 * should go.
2959 * Catch-22: if we don't clear all queues, the SCSI driver lock will
2960 * not be reset by atari_scsi_reset()!
2961 */
2962
2963#if defined(RESET_RUN_DONE)
2964 /* XXX Should now be done by midlevel code, but it's broken XXX */
Roman Zippelc28bda22007-05-01 22:32:36 +02002965 /* XXX see below XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Roman Zippelc28bda22007-05-01 22:32:36 +02002967 /* MSch: old-style reset: actually abort all command processing here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Roman Zippelc28bda22007-05-01 22:32:36 +02002969 /* After the reset, there are no more connected or disconnected commands
2970 * and no busy units; to avoid problems with re-inserting the commands
2971 * into the issue_queue (via scsi_done()), the aborted commands are
2972 * remembered in local variables first.
2973 */
2974 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002975 connected = (struct scsi_cmnd *)hostdata->connected;
Roman Zippelc28bda22007-05-01 22:32:36 +02002976 hostdata->connected = NULL;
Finn Thain710ddd02014-11-12 16:12:02 +11002977 disconnected_queue = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002978 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002980 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002982 for (i = 0; i < 8; ++i)
2983 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002985 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002987 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Roman Zippelc28bda22007-05-01 22:32:36 +02002989 /* In order to tell the mid-level code which commands were aborted,
2990 * set the command status to DID_RESET and call scsi_done() !!!
2991 * This ultimately aborts processing of these commands in the mid-level.
2992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Roman Zippelc28bda22007-05-01 22:32:36 +02002994 if ((cmd = connected)) {
Finn Thaind65e6342014-03-18 11:42:20 +11002995 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002996 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2997 cmd->scsi_done(cmd);
2998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Roman Zippelc28bda22007-05-01 22:32:36 +02003000 for (i = 0; (cmd = disconnected_queue); ++i) {
3001 disconnected_queue = NEXT(cmd);
Roman Zippel3130d902007-05-01 22:32:37 +02003002 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02003003 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
3004 cmd->scsi_done(cmd);
3005 }
3006 if (i > 0)
Finn Thaind65e6342014-03-18 11:42:20 +11003007 dprintk(NDEBUG_ABORT, "scsi: reset aborted %d disconnected command(s)\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Roman Zippelc28bda22007-05-01 22:32:36 +02003009 /* The Falcon lock should be released after a reset...
3010 */
3011 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
3012 * unlocking and enabling dma interrupt.
3013 */
3014/* falcon_release_lock_if_possible( hostdata );*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Roman Zippelc28bda22007-05-01 22:32:36 +02003016 /* since all commands have been explicitly terminated, we need to tell
3017 * the midlevel code that the reset was SUCCESSFUL, and there is no
3018 * need to 'wake up' the commands by a request_sense
3019 */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003020 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021#else /* 1 */
3022
Roman Zippelc28bda22007-05-01 22:32:36 +02003023 /* MSch: new-style reset handling: let the mid-level do what it can */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Roman Zippelc28bda22007-05-01 22:32:36 +02003025 /* ++guenther: MID-LEVEL IS STILL BROKEN.
3026 * Mid-level is supposed to requeue all commands that were active on the
3027 * various low-level queues. In fact it does this, but that's not enough
3028 * because all these commands are subject to timeout. And if a timeout
3029 * happens for any removed command, *_abort() is called but all queues
3030 * are now empty. Abort then gives up the falcon lock, which is fatal,
3031 * since the mid-level will queue more commands and must have the lock
3032 * (it's all happening inside timer interrupt handler!!).
3033 * Even worse, abort will return NOT_RUNNING for all those commands not
3034 * on any queue, so they won't be retried ...
3035 *
3036 * Conclusion: either scsi.c disables timeout for all resetted commands
3037 * immediately, or we lose! As of linux-2.0.20 it doesn't.
3038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Roman Zippelc28bda22007-05-01 22:32:36 +02003040 /* After the reset, there are no more connected or disconnected commands
3041 * and no busy units; so clear the low-level status here to avoid
3042 * conflicts when the mid-level code tries to wake up the affected
3043 * commands!
3044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Roman Zippelc28bda22007-05-01 22:32:36 +02003046 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003047 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003048 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11003049 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003050 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003051 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Roman Zippelc28bda22007-05-01 22:32:36 +02003053 local_irq_save(flags);
3054 hostdata->issue_queue = NULL;
3055 hostdata->connected = NULL;
3056 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02003058 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02003060 for (i = 0; i < 8; ++i)
3061 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02003063 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11003065
3066 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02003067 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Roman Zippelc28bda22007-05-01 22:32:36 +02003069 /* we did no complete reset of all commands, so a wakeup is required */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003070 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071#endif /* 1 */
3072}