blob: 7bbfa1b777c302a2053ebed175bfb7c019973e21 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200108 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200110 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * memory mapped) and drops this file in their 'C' wrapper.
112 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200113 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * and commands that are currently executing. This means that an
116 * unlimited number of commands may be queued, letting
117 * more commands propagate from the higher driver levels giving higher
118 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
119 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * while a command is already executing.
121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200123 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200125 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
126 * piece of hardware that requires you to sit in a loop polling for
127 * the REQ signal as long as you are connected. Some devices are
128 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * while doing long seek operations.
Roman Zippelc28bda22007-05-01 22:32:36 +0200130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * The workaround for this is to keep track of devices that have
132 * disconnected. If the device hasn't disconnected, for commands that
Roman Zippelc28bda22007-05-01 22:32:36 +0200133 * should disconnect, we do something like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
135 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
Roman Zippelc28bda22007-05-01 22:32:36 +0200136 *
137 * Some tweaking of N and M needs to be done. An algorithm based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * on "time to data" would give the best results as long as short time
Roman Zippelc28bda22007-05-01 22:32:36 +0200139 * to datas (ie, on the same track) were considered, however these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * broken devices are the exception rather than the rule and I'd rather
141 * spend my time optimizing for the normal case.
142 *
143 * Architecture :
144 *
145 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +1100146 * which is started from a workqueue for each NCR5380 host in the
147 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
148 * removing the commands from the issue queue and calling
149 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * Once a nexus is established, the NCR5380_information_transfer()
152 * phase goes through the various phases as instructed by the target.
153 * if the target goes into MSG IN and sends a DISCONNECT message,
154 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +1100155 * queue, and NCR5380_main tries to find more work. If the target is
156 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
158 * If a command has disconnected, eventually an interrupt will trigger,
159 * calling NCR5380_intr() which will in turn call NCR5380_reselect
160 * to reestablish a nexus. This will run main if necessary.
161 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200162 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * appropriate.
164 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200165 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * structures, being initialized after the command is connected
167 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
168 * Note that in violation of the standard, an implicit SAVE POINTERS operation
169 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
170 */
171
172/*
173 * Using this file :
174 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200175 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * and macros and include this file in your driver.
177 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200178 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200180 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100182 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
183 * transceivers.
184 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * LINKED - if defined, linked commands are supported.
186 *
187 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
188 *
189 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
190 *
191 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * NCR5380_read(register) - read from the specified register
194 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200195 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100197 * NCR5380_implementation_fields - additional fields needed for this
198 * specific implementation of the NCR5380
199 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200201 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200203 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * that they were able to program the controller for.
205 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200206 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * available as NCR5380_i386_dma_write_setup,
208 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
209 *
210 * NCR5380_dma_write_setup(instance, src, count) - initialize
211 * NCR5380_dma_read_setup(instance, dst, count) - initialize
212 * NCR5380_dma_residual(instance); - residual count
213 *
214 * PSEUDO functions :
215 * NCR5380_pwrite(instance, src, count)
216 * NCR5380_pread(instance, dst, count);
217 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200219 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100221 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
223
224static struct Scsi_Host *first_instance = NULL;
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100225static struct scsi_host_template *the_template = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227/* Macros ease life... :-) */
228#define SETUP_HOSTDATA(in) \
229 struct NCR5380_hostdata *hostdata = \
230 (struct NCR5380_hostdata *)(in)->hostdata
231#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
232
Finn Thain710ddd02014-11-12 16:12:02 +1100233#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200234#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100235#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237#define HOSTNO instance->host_no
238#define H_NO(cmd) (cmd)->device->host->host_no
239
240#ifdef SUPPORT_TAGS
241
242/*
243 * Functions for handling tagged queuing
244 * =====================================
245 *
246 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
247 *
248 * Using consecutive numbers for the tags is no good idea in my eyes. There
249 * could be wrong re-usings if the counter (8 bit!) wraps and some early
250 * command has been preempted for a long time. My solution: a bitfield for
251 * remembering used tags.
252 *
253 * There's also the problem that each target has a certain queue size, but we
254 * cannot know it in advance :-( We just see a QUEUE_FULL status being
255 * returned. So, in this case, the driver internal queue size assumption is
256 * reduced to the number of active tags if QUEUE_FULL is returned by the
257 * target. The command is returned to the mid-level, but with status changed
258 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
259 * correctly.
260 *
261 * We're also not allowed running tagged commands as long as an untagged
262 * command is active. And REQUEST SENSE commands after a contingent allegiance
263 * condition _must_ be untagged. To keep track whether an untagged command has
264 * been issued, the host->busy array is still employed, as it is without
265 * support for tagged queuing.
266 *
267 * One could suspect that there are possible race conditions between
268 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
269 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
270 * which already guaranteed to be running at most once. It is also the only
271 * place where tags/LUNs are allocated. So no other allocation can slip
272 * between that pair, there could only happen a reselection, which can free a
273 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
274 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
275 */
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277typedef struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200278 DECLARE_BITMAP(allocated, MAX_TAGS);
279 int nr_allocated;
280 int queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281} TAG_ALLOC;
282
Roman Zippelc28bda22007-05-01 22:32:36 +0200283static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285
Roman Zippelc28bda22007-05-01 22:32:36 +0200286static void __init init_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Roman Zippelc28bda22007-05-01 22:32:36 +0200288 int target, lun;
289 TAG_ALLOC *ta;
290
291 if (!setup_use_tagged_queuing)
292 return;
293
294 for (target = 0; target < 8; ++target) {
295 for (lun = 0; lun < 8; ++lun) {
296 ta = &TagAlloc[target][lun];
297 bitmap_zero(ta->allocated, MAX_TAGS);
298 ta->nr_allocated = 0;
299 /* At the beginning, assume the maximum queue size we could
300 * support (MAX_TAGS). This value will be decreased if the target
301 * returns QUEUE_FULL status.
302 */
303 ta->queue_size = MAX_TAGS;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308
309/* Check if we can issue a command to this LUN: First see if the LUN is marked
310 * busy by an untagged command. If the command should use tagged queuing, also
311 * check that there is a free tag and the target's queue won't overflow. This
312 * function should be called with interrupts disabled to avoid race
313 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Finn Thain710ddd02014-11-12 16:12:02 +1100316static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200318 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200319 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200321 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 return 1;
323 if (!should_be_tagged ||
324 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
325 return 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200326 if (TagAlloc[cmd->device->id][lun].nr_allocated >=
327 TagAlloc[cmd->device->id][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100328 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200329 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200330 return 1;
331 }
332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335
336/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
337 * must be called before!), or reserve the LUN in 'busy' if the command is
338 * untagged.
339 */
340
Finn Thain710ddd02014-11-12 16:12:02 +1100341static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200343 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200344 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Roman Zippelc28bda22007-05-01 22:32:36 +0200346 /* If we or the target don't support tagged queuing, allocate the LUN for
347 * an untagged command.
348 */
349 if (!should_be_tagged ||
350 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
351 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200352 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100353 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200354 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200355 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200356 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Roman Zippelc28bda22007-05-01 22:32:36 +0200358 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
359 set_bit(cmd->tag, ta->allocated);
360 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100361 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200362 "(now %d tags in use)\n",
363 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200364 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368
369/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
370 * unlock the LUN.
371 */
372
Finn Thain710ddd02014-11-12 16:12:02 +1100373static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200375 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200376 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Roman Zippelc28bda22007-05-01 22:32:36 +0200378 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200379 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100380 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200381 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200382 } else if (cmd->tag >= MAX_TAGS) {
383 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
384 H_NO(cmd), cmd->tag);
385 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200386 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200387 clear_bit(cmd->tag, ta->allocated);
388 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100389 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200390 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394
Roman Zippelc28bda22007-05-01 22:32:36 +0200395static void free_all_tags(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Roman Zippelc28bda22007-05-01 22:32:36 +0200397 int target, lun;
398 TAG_ALLOC *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Roman Zippelc28bda22007-05-01 22:32:36 +0200400 if (!setup_use_tagged_queuing)
401 return;
402
403 for (target = 0; target < 8; ++target) {
404 for (lun = 0; lun < 8; ++lun) {
405 ta = &TagAlloc[target][lun];
406 bitmap_zero(ta->allocated, MAX_TAGS);
407 ta->nr_allocated = 0;
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412#endif /* SUPPORT_TAGS */
413
414
415/*
Finn Thain710ddd02014-11-12 16:12:02 +1100416 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * Purpose: Try to merge several scatter-gather requests into one DMA
419 * transfer. This is possible if the scatter buffers lie on
420 * physical contiguous addresses.
421 *
Finn Thain710ddd02014-11-12 16:12:02 +1100422 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300424 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
426
Finn Thain710ddd02014-11-12 16:12:02 +1100427static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Finn Thaine3f463b2014-11-12 16:12:16 +1100429#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200430 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200432 unsigned long oldlen = cmd->SCp.this_residual;
433 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#endif
435
Roman Zippelc28bda22007-05-01 22:32:36 +0200436 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
437 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200438 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100439 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200440 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200442 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200444 ++cmd->SCp.buffer;
445 --cmd->SCp.buffers_residual;
446 cmd->SCp.this_residual += cmd->SCp.buffer->length;
447 endaddr += cmd->SCp.buffer->length;
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200450 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100451 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200452 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100454#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Finn Thainff50f9e2014-11-12 16:12:18 +1100457/**
458 * initialize_SCp - init the scsi pointer field
459 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100461 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463
Finn Thain710ddd02014-11-12 16:12:02 +1100464static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Roman Zippelc28bda22007-05-01 22:32:36 +0200466 /*
467 * Initialize the Scsi Pointer field so that all of the commands in the
468 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200470
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200471 if (scsi_bufflen(cmd)) {
472 cmd->SCp.buffer = scsi_sglist(cmd);
473 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200474 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200475 cmd->SCp.this_residual = cmd->SCp.buffer->length;
476 /* ++roman: Try to merge some scatter-buffers if they are at
477 * contiguous physical addresses.
478 */
479 merge_contiguous_buffers(cmd);
480 } else {
481 cmd->SCp.buffer = NULL;
482 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200483 cmd->SCp.ptr = NULL;
484 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#include <linux/delay.h>
489
490#if NDEBUG
491static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200492 unsigned char mask;
493 const char *name;
494} signals[] = {
495 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
496 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
497 { SR_SEL, "SEL" }, {0, NULL}
498}, basrs[] = {
499 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
500}, icrs[] = {
501 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
502 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
503 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
504 {0, NULL}
505}, mrs[] = {
506 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
507 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
508 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
509 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
510 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
511 {0, NULL}
512};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Finn Thainff50f9e2014-11-12 16:12:18 +1100514/**
515 * NCR5380_print - print scsi bus signals
516 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100518 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
520
Roman Zippelc28bda22007-05-01 22:32:36 +0200521static void NCR5380_print(struct Scsi_Host *instance)
522{
523 unsigned char status, data, basr, mr, icr, i;
524 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Roman Zippelc28bda22007-05-01 22:32:36 +0200526 local_irq_save(flags);
527 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
528 status = NCR5380_read(STATUS_REG);
529 mr = NCR5380_read(MODE_REG);
530 icr = NCR5380_read(INITIATOR_COMMAND_REG);
531 basr = NCR5380_read(BUS_AND_STATUS_REG);
532 local_irq_restore(flags);
533 printk("STATUS_REG: %02x ", status);
534 for (i = 0; signals[i].mask; ++i)
535 if (status & signals[i].mask)
536 printk(",%s", signals[i].name);
537 printk("\nBASR: %02x ", basr);
538 for (i = 0; basrs[i].mask; ++i)
539 if (basr & basrs[i].mask)
540 printk(",%s", basrs[i].name);
541 printk("\nICR: %02x ", icr);
542 for (i = 0; icrs[i].mask; ++i)
543 if (icr & icrs[i].mask)
544 printk(",%s", icrs[i].name);
545 printk("\nMODE: %02x ", mr);
546 for (i = 0; mrs[i].mask; ++i)
547 if (mr & mrs[i].mask)
548 printk(",%s", mrs[i].name);
549 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200553 unsigned char value;
554 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200556 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
557 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
558 {PHASE_UNKNOWN, "UNKNOWN"}
559};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Finn Thainff50f9e2014-11-12 16:12:18 +1100561/**
562 * NCR5380_print_phase - show SCSI phase
563 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100565 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100567 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569
570static void NCR5380_print_phase(struct Scsi_Host *instance)
571{
Roman Zippelc28bda22007-05-01 22:32:36 +0200572 unsigned char status;
573 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Roman Zippelc28bda22007-05-01 22:32:36 +0200575 status = NCR5380_read(STATUS_REG);
576 if (!(status & SR_REQ))
577 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
578 else {
579 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
580 (phases[i].value != (status & PHASE_MASK)); ++i)
581 ;
582 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#endif
587
588/*
589 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200590 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * If we're not in an interrupt, we can call our main directly, it cannot be
592 * already running. Else, we queue it on a task queue, if not 'main_running'
593 * tells us that a lower level is already executing it. This way,
594 * 'main_running' needs not be protected in a special way.
595 *
596 * queue_main() is a utility function for putting our main onto the task
597 * queue, if main_running is false. It should be called only from a
598 * interrupt or bottom half.
599 */
600
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900601#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#include <linux/workqueue.h>
603#include <linux/interrupt.h>
604
Roman Zippelc28bda22007-05-01 22:32:36 +0200605static volatile int main_running;
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200606static DECLARE_WORK(NCR5380_tqueue, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Roman Zippelc28bda22007-05-01 22:32:36 +0200608static inline void queue_main(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Roman Zippelc28bda22007-05-01 22:32:36 +0200610 if (!main_running) {
611 /* If in interrupt and NCR5380_main() not already running,
612 queue it on the 'immediate' task queue, to be processed
613 immediately after the current interrupt processing has
614 finished. */
615 schedule_work(&NCR5380_tqueue);
616 }
617 /* else: nothing to do: the running NCR5380_main() will pick up
618 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621
Roman Zippelc28bda22007-05-01 22:32:36 +0200622static inline void NCR5380_all_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Roman Zippelc28bda22007-05-01 22:32:36 +0200624 static int done = 0;
625 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +1100626 dprintk(NDEBUG_INIT, "scsi : NCR5380_all_init()\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200627 done = 1;
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Finn Thain8c325132014-11-12 16:11:58 +1100631/**
632 * NCR58380_info - report driver and host information
633 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 *
Finn Thain8c325132014-11-12 16:11:58 +1100635 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
Finn Thain8c325132014-11-12 16:11:58 +1100637 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
639
Finn Thain8c325132014-11-12 16:11:58 +1100640static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Finn Thain8c325132014-11-12 16:11:58 +1100642 struct NCR5380_hostdata *hostdata = shost_priv(instance);
643
644 return hostdata->info;
645}
646
647static void prepare_info(struct Scsi_Host *instance)
648{
649 struct NCR5380_hostdata *hostdata = shost_priv(instance);
650
651 snprintf(hostdata->info, sizeof(hostdata->info),
652 "%s, io_port 0x%lx, n_io_port %d, "
653 "base 0x%lx, irq %d, "
654 "can_queue %d, cmd_per_lun %d, "
655 "sg_tablesize %d, this_id %d, "
656 "options { %s} ",
657 instance->hostt->name, instance->io_port, instance->n_io_port,
658 instance->base, instance->irq,
659 instance->can_queue, instance->cmd_per_lun,
660 instance->sg_tablesize, instance->this_id,
661#ifdef DIFFERENTIAL
662 "DIFFERENTIAL "
663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100665 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#endif
667#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100668 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#endif
670#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100671 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#endif
Finn Thain8c325132014-11-12 16:11:58 +1100673 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Finn Thainff50f9e2014-11-12 16:12:18 +1100676/**
677 * NCR5380_print_status - dump controller info
678 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100680 * Print commands in the various queues, called from NCR5380_abort
681 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
683
Finn Thain710ddd02014-11-12 16:12:02 +1100684static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400685{
686 int i, s;
687 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200688 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400689 H_NO(cmd), cmd->device->id, cmd->device->lun);
690 printk(KERN_CONT " command = ");
691 command = cmd->cmnd;
692 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
693 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
694 printk(KERN_CONT " %02x", command[i]);
695 printk("\n");
696}
697
Roman Zippelc28bda22007-05-01 22:32:36 +0200698static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Al Virod89537e2013-03-31 13:24:44 -0400700 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100701 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400702 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Finn Thain8ad3a592014-03-18 11:42:19 +1100704 NCR5380_dprint(NDEBUG_ANY, instance);
705 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Roman Zippelc28bda22007-05-01 22:32:36 +0200707 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Roman Zippelc28bda22007-05-01 22:32:36 +0200709 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400710 printk("NCR5380: coroutine is%s running.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200711 main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200712 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400713 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200714 else
Finn Thain710ddd02014-11-12 16:12:02 +1100715 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400716 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100717 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400718 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Al Virod89537e2013-03-31 13:24:44 -0400720 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100721 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400722 ptr = NEXT(ptr))
723 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200724
725 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400726 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Finn Thain710ddd02014-11-12 16:12:02 +1100729static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Roman Zippelc28bda22007-05-01 22:32:36 +0200731 int i, s;
732 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200733 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200734 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400735 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200736 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400737 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200738 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400739 seq_printf(m, " %02x", command[i]);
740 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Finn Thain4d3d2a52014-11-12 16:11:52 +1100743static int __maybe_unused NCR5380_show_info(struct seq_file *m,
744 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400745{
746 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100747 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400748 unsigned long flags;
749
750 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
751
Al Virod89537e2013-03-31 13:24:44 -0400752 local_irq_save(flags);
753 seq_printf(m, "NCR5380: coroutine is%s running.\n",
754 main_running ? "" : "n't");
755 if (!hostdata->connected)
756 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
757 else
Finn Thain710ddd02014-11-12 16:12:02 +1100758 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400759 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100760 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400761 show_Scsi_Cmnd(ptr, m);
762
763 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100764 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400765 ptr = NEXT(ptr))
766 show_Scsi_Cmnd(ptr, m);
767
768 local_irq_restore(flags);
769 return 0;
770}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Finn Thainff50f9e2014-11-12 16:12:18 +1100772/**
773 * NCR5380_init - initialise an NCR5380
774 * @instance: adapter to configure
775 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100777 * Initializes *instance and corresponding 5380 chip,
778 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
780 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100781 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200782 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100783 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100786static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Roman Zippelc28bda22007-05-01 22:32:36 +0200788 int i;
789 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Roman Zippelc28bda22007-05-01 22:32:36 +0200791 NCR5380_all_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Roman Zippelc28bda22007-05-01 22:32:36 +0200793 hostdata->aborted = 0;
794 hostdata->id_mask = 1 << instance->this_id;
795 hostdata->id_higher_mask = 0;
796 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
797 if (i > hostdata->id_mask)
798 hostdata->id_higher_mask |= i;
799 for (i = 0; i < 8; ++i)
800 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200802 init_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#endif
804#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200805 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200807 hostdata->targets_present = 0;
808 hostdata->connected = NULL;
809 hostdata->issue_queue = NULL;
810 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100811 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Roman Zippelc28bda22007-05-01 22:32:36 +0200813 if (!the_template) {
814 the_template = instance->hostt;
815 first_instance = instance;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Finn Thain8c325132014-11-12 16:11:58 +1100818 prepare_info(instance);
819
Roman Zippelc28bda22007-05-01 22:32:36 +0200820 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
821 NCR5380_write(MODE_REG, MR_BASE);
822 NCR5380_write(TARGET_COMMAND_REG, 0);
823 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Roman Zippelc28bda22007-05-01 22:32:36 +0200825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Finn Thainff50f9e2014-11-12 16:12:18 +1100828/**
829 * NCR5380_exit - remove an NCR5380
830 * @instance: adapter to remove
831 *
832 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
833 */
834
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200835static void NCR5380_exit(struct Scsi_Host *instance)
836{
Finn Thainff50f9e2014-11-12 16:12:18 +1100837 cancel_work_sync(&NCR5380_tqueue);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200838}
839
Finn Thainff50f9e2014-11-12 16:12:18 +1100840/**
841 * NCR5380_queue_command - queue a command
842 * @instance: the relevant SCSI adapter
843 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100845 * cmd is added to the per instance issue_queue, with minor
846 * twiddling done to the host specific fields of cmd. If the
847 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849
Finn Thain16b29e72014-11-12 16:12:08 +1100850static int NCR5380_queue_command(struct Scsi_Host *instance,
851 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Finn Thain16b29e72014-11-12 16:12:08 +1100853 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100854 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200855 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200858 switch (cmd->cmnd[0]) {
859 case WRITE_6:
860 case WRITE_10:
861 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
862 H_NO(cmd));
863 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100864 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200865 return 0;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
868
Roman Zippelc28bda22007-05-01 22:32:36 +0200869 /*
870 * We use the host_scribble field as a pointer to the next command
871 * in a queue
872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Roman Zippel3130d902007-05-01 22:32:37 +0200874 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200875 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Roman Zippelc28bda22007-05-01 22:32:36 +0200877 /*
878 * Insert the cmd into the issue queue. Note that REQUEST SENSE
879 * commands are added to the head of the queue since any command will
880 * clear the contingent allegiance condition that exists and the
881 * sense data is only guaranteed to be valid while the condition exists.
882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Roman Zippelc28bda22007-05-01 22:32:36 +0200884 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
885 * Otherwise a running NCR5380_main may steal the lock.
886 * Lock before actually inserting due to fairness reasons explained in
887 * atari_scsi.c. If we insert first, then it's impossible for this driver
888 * to release the lock.
889 * Stop timer for this command while waiting for the lock, or timeouts
890 * may happen (and they really do), and it's no good if the command doesn't
891 * appear in any of the queues.
892 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
893 * because also a timer int can trigger an abort or reset, which would
894 * alter queues and touch the lock.
895 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100896 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100897 return SCSI_MLQUEUE_HOST_BUSY;
898
899 local_irq_save(flags);
900
Finn Thainff50f9e2014-11-12 16:12:18 +1100901 /*
902 * Insert the cmd into the issue queue. Note that REQUEST SENSE
903 * commands are added to the head of the queue since any command will
904 * clear the contingent allegiance condition that exists and the
905 * sense data is only guaranteed to be valid while the condition exists.
906 */
907
Roman Zippelc28bda22007-05-01 22:32:36 +0200908 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
909 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200910 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200911 hostdata->issue_queue = cmd;
912 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100913 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200914 NEXT(tmp); tmp = NEXT(tmp))
915 ;
916 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200917 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200918 }
919 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Finn Thaind65e6342014-03-18 11:42:20 +1100921 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200922 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Roman Zippelc28bda22007-05-01 22:32:36 +0200924 /* If queue_command() is called from an interrupt (real one or bottom
925 * half), we let queue_main() do the job of taking care about main. If it
926 * is already running, this is a no-op, else main will be queued.
927 *
928 * If we're not in an interrupt, we can call NCR5380_main()
929 * unconditionally, because it cannot be already running.
930 */
Finn Thain16b29e72014-11-12 16:12:08 +1100931 if (in_interrupt() || irqs_disabled())
Roman Zippelc28bda22007-05-01 22:32:36 +0200932 queue_main();
933 else
934 NCR5380_main(NULL);
935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Finn Thaine3c3da62014-11-12 16:12:15 +1100938static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
939{
940 struct NCR5380_hostdata *hostdata = shost_priv(instance);
941
942 /* Caller does the locking needed to set & test these data atomically */
943 if (!hostdata->disconnected_queue &&
944 !hostdata->issue_queue &&
945 !hostdata->connected &&
946 !hostdata->retain_dma_intr)
947 NCR5380_release_dma_irq(instance);
948}
949
Finn Thainff50f9e2014-11-12 16:12:18 +1100950/**
951 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100953 * NCR5380_main is a coroutine that runs as long as more work can
954 * be done on the NCR5380 host adapters in a system. Both
955 * NCR5380_queue_command() and NCR5380_intr() will try to start it
956 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200957 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100958 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200959 */
960
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200961static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Finn Thain710ddd02014-11-12 16:12:02 +1100963 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +0200964 struct Scsi_Host *instance = first_instance;
965 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
966 int done;
967 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Roman Zippelc28bda22007-05-01 22:32:36 +0200969 /*
970 * We run (with interrupts disabled) until we're sure that none of
971 * the host adapters have anything that can be done, at which point
972 * we set main_running to 0 and exit.
973 *
974 * Interrupts are enabled before doing various other internal
975 * instructions, after we've decided that we need to run through
976 * the loop again.
977 *
978 * this should prevent any race conditions.
979 *
980 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
981 * because also a timer int can trigger an abort or reset, which can
982 * alter queues and touch the Falcon lock.
983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Roman Zippelc28bda22007-05-01 22:32:36 +0200985 /* Tell int handlers main() is now already executing. Note that
986 no races are possible here. If an int comes in before
987 'main_running' is set here, and queues/executes main via the
988 task queue, it doesn't do any harm, just this instance of main
989 won't find any work left to do. */
990 if (main_running)
991 return;
992 main_running = 1;
993
994 local_save_flags(flags);
995 do {
996 local_irq_disable(); /* Freeze request queues */
997 done = 1;
998
999 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001000 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001001 /*
1002 * Search through the issue_queue for a command destined
1003 * for a target that's not busy.
1004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001006 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001007 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1008 ;
1009 /*printk("%p ", tmp);*/
1010 if ((tmp == prev) && tmp)
1011 printk(" LOOP\n");
1012 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001014 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001015 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001016 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Finn Thaine3f463b2014-11-12 16:12:16 +11001018 dprintk(NDEBUG_LISTS,
1019 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1020 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1021 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001022 /* When we find one, remove it from the issue queue. */
1023 /* ++guenther: possible race with Falcon locking */
1024 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001026 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001028 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001029#endif
1030 ) {
1031 /* ++guenther: just to be sure, this must be atomic */
1032 local_irq_disable();
1033 if (prev) {
1034 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001035 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001036 } else {
1037 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1038 hostdata->issue_queue = NEXT(tmp);
1039 }
Roman Zippel3130d902007-05-01 22:32:37 +02001040 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001041 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001042
1043 /* reenable interrupts after finding one */
1044 local_irq_restore(flags);
1045
1046 /*
1047 * Attempt to establish an I_T_L nexus here.
1048 * On success, instance->hostdata->connected is set.
1049 * On failure, we must add the command back to the
1050 * issue queue so we can keep trying.
1051 */
Finn Thaind65e6342014-03-18 11:42:20 +11001052 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001053 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001054 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001055 /*
1056 * REQUEST SENSE commands are issued without tagged
1057 * queueing, even on SCSI-II devices because the
1058 * contingent allegiance condition exists for the
1059 * entire unit.
1060 */
1061 /* ++roman: ...and the standard also requires that
1062 * REQUEST SENSE command are untagged.
1063 */
1064
1065#ifdef SUPPORT_TAGS
1066 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1067#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001068 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001069 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001070 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001071 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001072 maybe_release_dma_irq(instance);
1073 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001074 break;
1075 } else {
1076 local_irq_disable();
1077 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001078 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001079 hostdata->issue_queue = tmp;
1080#ifdef SUPPORT_TAGS
1081 cmd_free_tag(tmp);
1082#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001083 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001084 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001085 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001086 "returned to issue_queue\n", HOSTNO);
1087 if (hostdata->connected)
1088 break;
1089 }
1090 } /* if target/lun/target queue is not busy */
1091 } /* for issue_queue */
1092 } /* if (!hostdata->connected) */
1093
1094 if (hostdata->connected
1095#ifdef REAL_DMA
1096 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097#endif
1098 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001100 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001101 HOSTNO);
1102 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001103 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001104 done = 0;
1105 }
1106 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Roman Zippelc28bda22007-05-01 22:32:36 +02001108 /* Better allow ints _after_ 'main_running' has been cleared, else
1109 an interrupt could believe we'll pick up the work it left for
1110 us, but we won't see it anymore here... */
1111 main_running = 0;
1112 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115
1116#ifdef REAL_DMA
1117/*
1118 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1119 *
1120 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001121 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
1123 * Inputs : instance - this instance of the NCR5380.
1124 *
1125 */
1126
Roman Zippelc28bda22007-05-01 22:32:36 +02001127static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Roman Zippelc28bda22007-05-01 22:32:36 +02001129 SETUP_HOSTDATA(instance);
Finn Thaine3f463b2014-11-12 16:12:16 +11001130 int transfered;
1131 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001132 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001133 int saved_data = 0, overrun = 0;
1134 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Roman Zippelc28bda22007-05-01 22:32:36 +02001136 if (!hostdata->connected) {
1137 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1138 "no connected cmd\n", HOSTNO);
1139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Finn Thainef1081c2014-11-12 16:12:14 +11001142 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001143 p = hostdata->connected->SCp.phase;
1144 if (p & SR_IO) {
1145 udelay(10);
1146 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1147 (BASR_PHASE_MATCH|BASR_ACK)) ==
1148 (BASR_PHASE_MATCH|BASR_ACK)) {
1149 saved_data = NCR5380_read(INPUT_DATA_REG);
1150 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001151 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001152 }
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001155
Finn Thaind65e6342014-03-18 11:42:20 +11001156 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001157 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1158 NCR5380_read(STATUS_REG));
1159
Finn Thaine3f463b2014-11-12 16:12:16 +11001160#if defined(CONFIG_SUN3)
1161 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1162 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1163 instance->host_no);
1164 BUG();
1165 }
1166
1167 /* make sure we're not stuck in a data phase */
1168 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1169 (BASR_PHASE_MATCH | BASR_ACK)) {
1170 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1171 NCR5380_read(BUS_AND_STATUS_REG));
1172 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1173 instance->host_no);
1174 BUG();
1175 }
1176#endif
1177
Roman Zippelc28bda22007-05-01 22:32:36 +02001178 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1179 NCR5380_write(MODE_REG, MR_BASE);
1180 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1181
1182 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1183 hostdata->dma_len = 0;
1184
1185 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1186 count = &hostdata->connected->SCp.this_residual;
1187 *data += transfered;
1188 *count -= transfered;
1189
Finn Thainef1081c2014-11-12 16:12:14 +11001190 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001191 int cnt, toPIO;
1192
Roman Zippelc28bda22007-05-01 22:32:36 +02001193 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001194 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001195 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001196 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001197 *(*data)++ = saved_data;
1198 (*count)--;
1199 cnt--;
1200 toPIO--;
1201 }
Finn Thaind65e6342014-03-18 11:42:20 +11001202 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001203 NCR5380_transfer_pio(instance, &p, &cnt, data);
1204 *count -= toPIO - cnt;
1205 }
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208#endif /* REAL_DMA */
1209
1210
Finn Thainff50f9e2014-11-12 16:12:18 +11001211/**
1212 * NCR5380_intr - generic NCR5380 irq handler
1213 * @irq: interrupt number
1214 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001215 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001216 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1217 * from the disconnected queue, and restarting NCR5380_main()
1218 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
1220
Roman Zippelc28bda22007-05-01 22:32:36 +02001221static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Roman Zippelc28bda22007-05-01 22:32:36 +02001223 struct Scsi_Host *instance = first_instance;
1224 int done = 1, handled = 0;
1225 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Finn Thaind65e6342014-03-18 11:42:20 +11001227 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Roman Zippelc28bda22007-05-01 22:32:36 +02001229 /* Look for pending interrupts */
1230 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001231 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001232 /* dispatch to appropriate routine if found and done=0 */
1233 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001234 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001235 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1236 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001237 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001238 NCR5380_reselect(instance);
1239 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1240 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001241 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001242 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1243 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001244 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001245 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1246 } else {
1247 /*
1248 * The rest of the interrupt conditions can occur only during a
1249 * DMA transfer
1250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001253 /*
1254 * We should only get PHASE MISMATCH and EOP interrupts if we have
1255 * DMA enabled, so do a sanity check based on the current setting
1256 * of the MODE register.
1257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Roman Zippelc28bda22007-05-01 22:32:36 +02001259 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1260 ((basr & BASR_END_DMA_TRANSFER) ||
1261 !(basr & BASR_PHASE_MATCH))) {
1262
Finn Thaind65e6342014-03-18 11:42:20 +11001263 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001264 NCR5380_dma_complete( instance );
1265 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001266 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001270 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001271 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001272 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1273 HOSTNO, basr, NCR5380_read(MODE_REG),
1274 NCR5380_read(STATUS_REG));
1275 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001276#ifdef SUN3_SCSI_VME
1277 dregs->csr |= CSR_DMA_ENABLE;
1278#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001279 }
1280 } /* if !(SELECTION || PARITY) */
1281 handled = 1;
1282 } /* BASR & IRQ */ else {
1283 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1284 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1285 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1286 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001287#ifdef SUN3_SCSI_VME
1288 dregs->csr |= CSR_DMA_ENABLE;
1289#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001290 }
1291
1292 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001293 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001294 /* Put a call to NCR5380_main() on the queue... */
1295 queue_main();
1296 }
1297 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Roman Zippelc28bda22007-05-01 22:32:36 +02001300/*
Finn Thain710ddd02014-11-12 16:12:02 +11001301 * Function : int NCR5380_select(struct Scsi_Host *instance,
1302 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 *
1304 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001305 * including ARBITRATION, SELECTION, and initial message out for
1306 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001308 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001309 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001311 * Returns : -1 if selection could not execute for some reason,
1312 * 0 if selection succeeded or failed because the target
1313 * did not respond.
1314 *
1315 * Side effects :
1316 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 * with registers as they should have been on entry - ie
1318 * SELECT_ENABLE will be set appropriately, the NCR5380
1319 * will cease to drive any SCSI bus signals.
1320 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001321 * If successful : I_T_L or I_T_L_Q nexus will be established,
1322 * instance->connected will be set to cmd.
1323 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001325 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 * cmd->result host byte set to DID_BAD_TARGET.
1327 */
1328
Finn Thain710ddd02014-11-12 16:12:02 +11001329static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Roman Zippelc28bda22007-05-01 22:32:36 +02001331 SETUP_HOSTDATA(instance);
1332 unsigned char tmp[3], phase;
1333 unsigned char *data;
1334 int len;
1335 unsigned long timeout;
1336 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Roman Zippelc28bda22007-05-01 22:32:36 +02001338 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001339 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001340 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001341 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Roman Zippelc28bda22007-05-01 22:32:36 +02001343 /*
1344 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1345 * data bus during SELECTION.
1346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Roman Zippelc28bda22007-05-01 22:32:36 +02001348 local_irq_save(flags);
1349 if (hostdata->connected) {
1350 local_irq_restore(flags);
1351 return -1;
1352 }
1353 NCR5380_write(TARGET_COMMAND_REG, 0);
1354
1355 /*
1356 * Start arbitration.
1357 */
1358
1359 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1360 NCR5380_write(MODE_REG, MR_ARBITRATE);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Roman Zippelc28bda22007-05-01 22:32:36 +02001364 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001365#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001366 {
1367 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Roman Zippelc28bda22007-05-01 22:32:36 +02001369 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1370 time_before(jiffies, timeout) && !hostdata->connected)
1371 ;
1372 if (time_after_eq(jiffies, timeout)) {
1373 printk("scsi : arbitration timeout at %d\n", __LINE__);
1374 NCR5380_write(MODE_REG, MR_BASE);
1375 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1376 return -1;
1377 }
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001380 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1381 !hostdata->connected)
1382 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#endif
1384
Finn Thaind65e6342014-03-18 11:42:20 +11001385 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Roman Zippelc28bda22007-05-01 22:32:36 +02001387 if (hostdata->connected) {
1388 NCR5380_write(MODE_REG, MR_BASE);
1389 return -1;
1390 }
1391 /*
1392 * The arbitration delay is 2.2us, but this is a minimum and there is
1393 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1394 * the integral nature of udelay().
1395 *
1396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Roman Zippelc28bda22007-05-01 22:32:36 +02001398 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Roman Zippelc28bda22007-05-01 22:32:36 +02001400 /* Check for lost arbitration */
1401 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1402 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1403 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1404 hostdata->connected) {
1405 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001406 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001407 HOSTNO);
1408 return -1;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Roman Zippelc28bda22007-05-01 22:32:36 +02001411 /* after/during arbitration, BSY should be asserted.
1412 IBM DPES-31080 Version S31Q works now */
1413 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1414 NCR5380_write(INITIATOR_COMMAND_REG,
1415 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Roman Zippelc28bda22007-05-01 22:32:36 +02001417 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1418 hostdata->connected) {
1419 NCR5380_write(MODE_REG, MR_BASE);
1420 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001421 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001422 HOSTNO);
1423 return -1;
1424 }
1425
1426 /*
1427 * Again, bus clear + bus settle time is 1.2us, however, this is
1428 * a minimum so we'll udelay ceil(1.2)
1429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001432 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1433 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001435 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001437
1438 if (hostdata->connected) {
1439 NCR5380_write(MODE_REG, MR_BASE);
1440 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1441 return -1;
1442 }
1443
Finn Thaind65e6342014-03-18 11:42:20 +11001444 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001445
1446 /*
1447 * Now that we have won arbitration, start Selection process, asserting
1448 * the host and target ID's on the SCSI bus.
1449 */
1450
1451 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1452
1453 /*
1454 * Raise ATN while SEL is true before BSY goes false from arbitration,
1455 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1456 * phase immediately after selection.
1457 */
1458
1459 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1460 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 /*
1464 * Reselect interrupts must be turned off prior to the dropping of BSY,
1465 * otherwise we will trigger an interrupt.
1466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 if (hostdata->connected) {
1469 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1470 return -1;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Roman Zippelc28bda22007-05-01 22:32:36 +02001473 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Roman Zippelc28bda22007-05-01 22:32:36 +02001475 /*
1476 * The initiator shall then wait at least two deskew delays and release
1477 * the BSY signal.
1478 */
1479 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Roman Zippelc28bda22007-05-01 22:32:36 +02001481 /* Reset BSY */
1482 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1483 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Roman Zippelc28bda22007-05-01 22:32:36 +02001485 /*
1486 * Something weird happens when we cease to drive BSY - looks
1487 * like the board/chip is letting us do another read before the
1488 * appropriate propagation delay has expired, and we're confusing
1489 * a BSY signal from ourselves as the target's response to SELECTION.
1490 *
1491 * A small delay (the 'C++' frontend breaks the pipeline with an
1492 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1493 * tighter 'C' code breaks and requires this) solves the problem -
1494 * the 1 us delay is arbitrary, and only used because this delay will
1495 * be the same on other platforms and since it works here, it should
1496 * work there.
1497 *
1498 * wingel suggests that this could be due to failing to wait
1499 * one deskew delay.
1500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Roman Zippelc28bda22007-05-01 22:32:36 +02001502 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Finn Thaind65e6342014-03-18 11:42:20 +11001504 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Roman Zippelc28bda22007-05-01 22:32:36 +02001506 /*
1507 * The SCSI specification calls for a 250 ms timeout for the actual
1508 * selection.
1509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Finn Thainff50f9e2014-11-12 16:12:18 +11001511 timeout = jiffies + (250 * HZ / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Roman Zippelc28bda22007-05-01 22:32:36 +02001513 /*
1514 * XXX very interesting - we're seeing a bounce where the BSY we
1515 * asserted is being reflected / still asserted (propagation delay?)
1516 * and it's detecting as true. Sigh.
1517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001520 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1521 * IO while SEL is true. But again, there are some disks out the in the
1522 * world that do that nevertheless. (Somebody claimed that this announces
1523 * reselection capability of the target.) So we better skip that test and
1524 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Roman Zippelc28bda22007-05-01 22:32:36 +02001527 while (time_before(jiffies, timeout) &&
1528 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1529 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Roman Zippelc28bda22007-05-01 22:32:36 +02001531 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1532 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1533 NCR5380_reselect(instance);
1534 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1535 HOSTNO);
1536 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1537 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001540 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1541 ;
1542#endif
1543
1544 /*
1545 * No less than two deskew delays after the initiator detects the
1546 * BSY signal is true, it shall release the SEL signal and may
1547 * change the DATA BUS. -wingel
1548 */
1549
1550 udelay(1);
1551
1552 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1553
1554 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1555 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1556 if (hostdata->targets_present & (1 << cmd->device->id)) {
1557 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1558 if (hostdata->restart_select)
1559 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001560 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001561 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1562 return -1;
1563 }
1564 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001565#ifdef SUPPORT_TAGS
1566 cmd_free_tag(cmd);
1567#endif
1568 cmd->scsi_done(cmd);
1569 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001570 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001571 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1572 return 0;
1573 }
1574
1575 hostdata->targets_present |= (1 << cmd->device->id);
1576
1577 /*
1578 * Since we followed the SCSI spec, and raised ATN while SEL
1579 * was true but before BSY was false during selection, the information
1580 * transfer phase should be a MESSAGE OUT phase so that we can send the
1581 * IDENTIFY message.
1582 *
1583 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1584 * message (2 bytes) with a tag ID that we increment with every command
1585 * until it wraps back to 0.
1586 *
1587 * XXX - it turns out that there are some broken SCSI-II devices,
1588 * which claim to support tagged queuing but fail when more than
1589 * some number of commands are issued at once.
1590 */
1591
1592 /* Wait for start of REQ/ACK handshake */
1593 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1594 ;
1595
Finn Thaind65e6342014-03-18 11:42:20 +11001596 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001597 HOSTNO, cmd->device->id);
1598 tmp[0] = IDENTIFY(1, cmd->device->lun);
1599
1600#ifdef SUPPORT_TAGS
1601 if (cmd->tag != TAG_NONE) {
1602 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1603 tmp[2] = cmd->tag;
1604 len = 3;
1605 } else
1606 len = 1;
1607#else
1608 len = 1;
1609 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610#endif /* SUPPORT_TAGS */
1611
Roman Zippelc28bda22007-05-01 22:32:36 +02001612 /* Send message(s) */
1613 data = tmp;
1614 phase = PHASE_MSGOUT;
1615 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001616 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001617 /* XXX need to handle errors here */
1618 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001620 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1621#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001622#ifdef SUN3_SCSI_VME
1623 dregs->csr |= CSR_INTR;
1624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Roman Zippelc28bda22007-05-01 22:32:36 +02001626 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Roman Zippelc28bda22007-05-01 22:32:36 +02001628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Roman Zippelc28bda22007-05-01 22:32:36 +02001631/*
1632 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * unsigned char *phase, int *count, unsigned char **data)
1634 *
1635 * Purpose : transfers data in given phase using polled I/O
1636 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001637 * Inputs : instance - instance of driver, *phase - pointer to
1638 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001640 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001642 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * is in same phase.
1644 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001645 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 *
1647 * XXX Note : handling for bus free may be useful.
1648 */
1649
1650/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001651 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * IS 100% reliable, and for the actual data transfer where speed
1653 * counts, we will always do a pseudo DMA or DMA transfer.
1654 */
1655
Roman Zippelc28bda22007-05-01 22:32:36 +02001656static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1657 unsigned char *phase, int *count,
1658 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Roman Zippelc28bda22007-05-01 22:32:36 +02001660 register unsigned char p = *phase, tmp;
1661 register int c = *count;
1662 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Roman Zippelc28bda22007-05-01 22:32:36 +02001664 /*
1665 * The NCR5380 chip will only drive the SCSI bus when the
1666 * phase specified in the appropriate bits of the TARGET COMMAND
1667 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
1669
Roman Zippelc28bda22007-05-01 22:32:36 +02001670 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Roman Zippelc28bda22007-05-01 22:32:36 +02001672 do {
1673 /*
1674 * Wait for assertion of REQ, after which the phase bits will be
1675 * valid
1676 */
1677 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1678 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Finn Thaind65e6342014-03-18 11:42:20 +11001680 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Roman Zippelc28bda22007-05-01 22:32:36 +02001682 /* Check for phase mismatch */
1683 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001684 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001685 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001686 break;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Roman Zippelc28bda22007-05-01 22:32:36 +02001689 /* Do actual transfer from SCSI bus to / from memory */
1690 if (!(p & SR_IO))
1691 NCR5380_write(OUTPUT_DATA_REG, *d);
1692 else
1693 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Roman Zippelc28bda22007-05-01 22:32:36 +02001695 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Roman Zippelc28bda22007-05-01 22:32:36 +02001697 /*
1698 * The SCSI standard suggests that in MSGOUT phase, the initiator
1699 * should drop ATN on the last byte of the message phase
1700 * after REQ has been asserted for the handshake but before
1701 * the initiator raises ACK.
1702 */
1703
1704 if (!(p & SR_IO)) {
1705 if (!((p & SR_MSG) && c > 1)) {
1706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001707 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001708 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1709 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1710 } else {
1711 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1712 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001713 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001714 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1715 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1716 }
1717 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001718 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001719 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1720 }
1721
1722 while (NCR5380_read(STATUS_REG) & SR_REQ)
1723 ;
1724
Finn Thaind65e6342014-03-18 11:42:20 +11001725 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001726
1727 /*
1728 * We have several special cases to consider during REQ/ACK handshaking :
1729 * 1. We were in MSGOUT phase, and we are on the last byte of the
1730 * message. ATN must be dropped as ACK is dropped.
1731 *
1732 * 2. We are in a MSGIN phase, and we are on the last byte of the
1733 * message. We must exit with ACK asserted, so that the calling
1734 * code may raise ATN before dropping ACK to reject the message.
1735 *
1736 * 3. ACK and ATN are clear and the target may proceed as normal.
1737 */
1738 if (!(p == PHASE_MSGIN && c == 1)) {
1739 if (p == PHASE_MSGOUT && c > 1)
1740 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1741 else
1742 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1743 }
1744 } while (--c);
1745
Finn Thaind65e6342014-03-18 11:42:20 +11001746 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001747
1748 *count = c;
1749 *data = d;
1750 tmp = NCR5380_read(STATUS_REG);
1751 /* The phase read from the bus is valid if either REQ is (already)
1752 * asserted or if ACK hasn't been released yet. The latter is the case if
1753 * we're in MSGIN and all wanted bytes have been received.
1754 */
1755 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1756 *phase = tmp & PHASE_MASK;
1757 else
1758 *phase = PHASE_UNKNOWN;
1759
1760 if (!c || (*phase == p))
1761 return 0;
1762 else
1763 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
1767 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001768 *
1769 * Purpose : abort the currently established nexus. Should only be
1770 * called from a routine which can drop into a
1771 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 * Returns : 0 on success, -1 on failure.
1773 */
1774
Roman Zippelc28bda22007-05-01 22:32:36 +02001775static int do_abort(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Roman Zippelc28bda22007-05-01 22:32:36 +02001777 unsigned char tmp, *msgptr, phase;
1778 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Roman Zippelc28bda22007-05-01 22:32:36 +02001780 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Roman Zippelc28bda22007-05-01 22:32:36 +02001783 /*
1784 * Wait for the target to indicate a valid phase by asserting
1785 * REQ. Once this happens, we'll have either a MSGOUT phase
1786 * and can immediately send the ABORT message, or we'll have some
1787 * other phase and will have to source/sink data.
1788 *
1789 * We really don't care what value was on the bus or what value
1790 * the target sees, so we just handshake.
1791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Roel Kluin3be38e72007-11-15 21:13:46 +01001793 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001794 ;
1795
1796 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1797
1798 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1799 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1800 ICR_ASSERT_ACK);
1801 while (NCR5380_read(STATUS_REG) & SR_REQ)
1802 ;
1803 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1804 }
1805
1806 tmp = ABORT;
1807 msgptr = &tmp;
1808 len = 1;
1809 phase = PHASE_MSGOUT;
1810 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1811
1812 /*
1813 * If we got here, and the command completed successfully,
1814 * we're about to go into bus free state.
1815 */
1816
1817 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001821/*
1822 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * unsigned char *phase, int *count, unsigned char **data)
1824 *
1825 * Purpose : transfers data in given phase using either real
1826 * or pseudo DMA.
1827 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001828 * Inputs : instance - instance of driver, *phase - pointer to
1829 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001831 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001833 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 * is in same phase.
1835 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001836 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 *
1838 */
1839
1840
Roman Zippelc28bda22007-05-01 22:32:36 +02001841static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1842 unsigned char *phase, int *count,
1843 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Roman Zippelc28bda22007-05-01 22:32:36 +02001845 SETUP_HOSTDATA(instance);
1846 register int c = *count;
1847 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001848 unsigned long flags;
1849
1850#if defined(CONFIG_SUN3)
1851 /* sanity check */
1852 if (!sun3_dma_setup_done) {
1853 pr_err("scsi%d: transfer_dma without setup!\n",
1854 instance->host_no);
1855 BUG();
1856 }
1857 hostdata->dma_len = c;
1858
1859 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1860 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1861 c, (p & SR_IO) ? "to" : "from", *data);
1862
1863 /* netbsd turns off ints here, why not be safe and do it too */
1864 local_irq_save(flags);
1865
1866 /* send start chain */
1867 sun3scsi_dma_start(c, *data);
1868
1869 if (p & SR_IO) {
1870 NCR5380_write(TARGET_COMMAND_REG, 1);
1871 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1872 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1873 NCR5380_write(MODE_REG,
1874 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1875 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1876 } else {
1877 NCR5380_write(TARGET_COMMAND_REG, 0);
1878 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1879 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1880 NCR5380_write(MODE_REG,
1881 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1882 NCR5380_write(START_DMA_SEND_REG, 0);
1883 }
1884
1885#ifdef SUN3_SCSI_VME
1886 dregs->csr |= CSR_DMA_ENABLE;
1887#endif
1888
1889 local_irq_restore(flags);
1890
1891 sun3_dma_active = 1;
1892
1893#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001894 register unsigned char *d = *data;
1895 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Roman Zippelc28bda22007-05-01 22:32:36 +02001897 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1898 *phase = tmp;
1899 return -1;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Finn Thainef1081c2014-11-12 16:12:14 +11001902 if (hostdata->read_overruns && (p & SR_IO))
1903 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Finn Thaind65e6342014-03-18 11:42:20 +11001905 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001906 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1907 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Roman Zippelc28bda22007-05-01 22:32:36 +02001909 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001912 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913#endif /* def REAL_DMA */
1914
Finn Thainef1081c2014-11-12 16:12:14 +11001915 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001916 /* On the Medusa, it is a must to initialize the DMA before
1917 * starting the NCR. This is also the cleaner way for the TT.
1918 */
1919 local_irq_save(flags);
1920 hostdata->dma_len = (p & SR_IO) ?
1921 NCR5380_dma_read_setup(instance, d, c) :
1922 NCR5380_dma_write_setup(instance, d, c);
1923 local_irq_restore(flags);
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Roman Zippelc28bda22007-05-01 22:32:36 +02001926 if (p & SR_IO)
1927 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1928 else {
1929 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1930 NCR5380_write(START_DMA_SEND_REG, 0);
1931 }
1932
Finn Thainef1081c2014-11-12 16:12:14 +11001933 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001934 /* On the Falcon, the DMA setup must be done after the last */
1935 /* NCR access, else the DMA setup gets trashed!
1936 */
1937 local_irq_save(flags);
1938 hostdata->dma_len = (p & SR_IO) ?
1939 NCR5380_dma_read_setup(instance, d, c) :
1940 NCR5380_dma_write_setup(instance, d, c);
1941 local_irq_restore(flags);
1942 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001943#endif /* !defined(CONFIG_SUN3) */
1944
Roman Zippelc28bda22007-05-01 22:32:36 +02001945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947#endif /* defined(REAL_DMA) */
1948
1949/*
1950 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1951 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001952 * Purpose : run through the various SCSI phases and do as the target
1953 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 * instance->connected.
1955 *
1956 * Inputs : instance, instance for which we are doing commands
1957 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001958 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * modified if a command disconnects, *instance->connected will
1960 * change.
1961 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001962 * XXX Note : we need to watch for bus free or a reset condition here
1963 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001965
1966static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Roman Zippelc28bda22007-05-01 22:32:36 +02001968 SETUP_HOSTDATA(instance);
1969 unsigned long flags;
1970 unsigned char msgout = NOP;
1971 int sink = 0;
1972 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001974 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001976 unsigned char *data;
1977 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001978 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Finn Thaine3f463b2014-11-12 16:12:16 +11001980#ifdef SUN3_SCSI_VME
1981 dregs->csr |= CSR_INTR;
1982#endif
1983
Roman Zippelc28bda22007-05-01 22:32:36 +02001984 while (1) {
1985 tmp = NCR5380_read(STATUS_REG);
1986 /* We only have a valid SCSI phase when REQ is asserted */
1987 if (tmp & SR_REQ) {
1988 phase = (tmp & PHASE_MASK);
1989 if (phase != old_phase) {
1990 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001991 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001993#if defined(CONFIG_SUN3)
1994 if (phase == PHASE_CMDOUT) {
1995#if defined(REAL_DMA)
1996 void *d;
1997 unsigned long count;
1998
1999 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2000 count = cmd->SCp.buffer->length;
2001 d = sg_virt(cmd->SCp.buffer);
2002 } else {
2003 count = cmd->SCp.this_residual;
2004 d = cmd->SCp.ptr;
2005 }
2006 /* this command setup for dma yet? */
2007 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2008 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2009 sun3scsi_dma_setup(d, count,
2010 rq_data_dir(cmd->request));
2011 sun3_dma_setup_done = cmd;
2012 }
2013 }
2014#endif
2015#ifdef SUN3_SCSI_VME
2016 dregs->csr |= CSR_INTR;
2017#endif
2018 }
2019#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Roman Zippelc28bda22007-05-01 22:32:36 +02002021 if (sink && (phase != PHASE_MSGOUT)) {
2022 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Roman Zippelc28bda22007-05-01 22:32:36 +02002024 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2025 ICR_ASSERT_ACK);
2026 while (NCR5380_read(STATUS_REG) & SR_REQ)
2027 ;
2028 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2029 ICR_ASSERT_ATN);
2030 sink = 0;
2031 continue;
2032 }
2033
2034 switch (phase) {
2035 case PHASE_DATAOUT:
2036#if (NDEBUG & NDEBUG_NO_DATAOUT)
2037 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2038 "aborted\n", HOSTNO);
2039 sink = 1;
2040 do_abort(instance);
2041 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002042 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002043 return;
2044#endif
2045 case PHASE_DATAIN:
2046 /*
2047 * If there is no room left in the current buffer in the
2048 * scatter-gather list, move onto the next one.
2049 */
2050
2051 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2052 ++cmd->SCp.buffer;
2053 --cmd->SCp.buffers_residual;
2054 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002055 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002056 /* ++roman: Try to merge some scatter-buffers if
2057 * they are at contiguous physical addresses.
2058 */
2059 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002060 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002061 HOSTNO, cmd->SCp.this_residual,
2062 cmd->SCp.buffers_residual);
2063 }
2064
2065 /*
2066 * The preferred transfer method is going to be
2067 * PSEUDO-DMA for systems that are strictly PIO,
2068 * since we can let the hardware do the handshaking.
2069 *
2070 * For this to work, we need to know the transfersize
2071 * ahead of time, since the pseudo-DMA code will sit
2072 * in an unconditional loop.
2073 */
2074
2075 /* ++roman: I suggest, this should be
2076 * #if def(REAL_DMA)
2077 * instead of leaving REAL_DMA out.
2078 */
2079
2080#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002081 if (
2082#if !defined(CONFIG_SUN3)
2083 !cmd->device->borken &&
2084#endif
2085 (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002086 len = transfersize;
2087 cmd->SCp.phase = phase;
2088 if (NCR5380_transfer_dma(instance, &phase,
2089 &len, (unsigned char **)&cmd->SCp.ptr)) {
2090 /*
2091 * If the watchdog timer fires, all future
2092 * accesses to this device will use the
2093 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002094 scmd_printk(KERN_INFO, cmd,
2095 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002096 cmd->device->borken = 1;
2097 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2098 ICR_ASSERT_ATN);
2099 sink = 1;
2100 do_abort(instance);
2101 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002102 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002103 /* XXX - need to source or sink data here, as appropriate */
2104 } else {
2105#ifdef REAL_DMA
2106 /* ++roman: When using real DMA,
2107 * information_transfer() should return after
2108 * starting DMA since it has nothing more to
2109 * do.
2110 */
2111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002113 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002115 }
2116 } else
2117#endif /* defined(REAL_DMA) */
2118 NCR5380_transfer_pio(instance, &phase,
2119 (int *)&cmd->SCp.this_residual,
2120 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002121#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2122 /* if we had intended to dma that command clear it */
2123 if (sun3_dma_setup_done == cmd)
2124 sun3_dma_setup_done = NULL;
2125#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002126 break;
2127 case PHASE_MSGIN:
2128 len = 1;
2129 data = &tmp;
2130 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2131 NCR5380_transfer_pio(instance, &phase, &len, &data);
2132 cmd->SCp.Message = tmp;
2133
2134 switch (tmp) {
2135 /*
2136 * Linking lets us reduce the time required to get the
2137 * next command out to the device, hopefully this will
2138 * mean we don't waste another revolution due to the delays
2139 * required by ARBITRATION and another SELECTION.
2140 *
2141 * In the current implementation proposal, low level drivers
2142 * merely have to start the next command, pointed to by
2143 * next_link, done() is called as with unlinked commands.
2144 */
2145#ifdef LINKED
2146 case LINKED_CMD_COMPLETE:
2147 case LINKED_FLG_CMD_COMPLETE:
2148 /* Accept message by clearing ACK */
2149 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2150
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002151 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002152 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2153
2154 /* Enable reselect interrupts */
2155 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2156 /*
2157 * Sanity check : A linked command should only terminate
2158 * with one of these messages if there are more linked
2159 * commands available.
2160 */
2161
2162 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002163 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002164 "linked command complete, no next_link\n",
2165 HOSTNO, cmd->device->id, cmd->device->lun);
2166 sink = 1;
2167 do_abort(instance);
2168 return;
2169 }
2170
2171 initialize_SCp(cmd->next_link);
2172 /* The next command is still part of this process; copy it
2173 * and don't free it! */
2174 cmd->next_link->tag = cmd->tag;
2175 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002176 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002177 "done, calling scsi_done().\n",
2178 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002179 cmd->scsi_done(cmd);
2180 cmd = hostdata->connected;
2181 break;
2182#endif /* def LINKED */
2183 case ABORT:
2184 case COMMAND_COMPLETE:
2185 /* Accept message by clearing ACK */
2186 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002187 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002188 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002189
2190 local_irq_save(flags);
2191 hostdata->retain_dma_intr++;
2192 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002193#ifdef SUPPORT_TAGS
2194 cmd_free_tag(cmd);
2195 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2196 /* Turn a QUEUE FULL status into BUSY, I think the
2197 * mid level cannot handle QUEUE FULL :-( (The
2198 * command is retried after BUSY). Also update our
2199 * queue size to the number of currently issued
2200 * commands now.
2201 */
2202 /* ++Andreas: the mid level code knows about
2203 QUEUE_FULL now. */
2204 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002205 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002206 "QUEUE_FULL after %d commands\n",
2207 HOSTNO, cmd->device->id, cmd->device->lun,
2208 ta->nr_allocated);
2209 if (ta->queue_size > ta->nr_allocated)
2210 ta->nr_allocated = ta->queue_size;
2211 }
2212#else
2213 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2214#endif
2215 /* Enable reselect interrupts */
2216 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2217
2218 /*
2219 * I'm not sure what the correct thing to do here is :
2220 *
2221 * If the command that just executed is NOT a request
2222 * sense, the obvious thing to do is to set the result
2223 * code to the values of the stored parameters.
2224 *
2225 * If it was a REQUEST SENSE command, we need some way to
2226 * differentiate between the failure code of the original
2227 * and the failure code of the REQUEST sense - the obvious
2228 * case is success, where we fall through and leave the
2229 * result code unchanged.
2230 *
2231 * The non-obvious place is where the REQUEST SENSE failed
2232 */
2233
2234 if (cmd->cmnd[0] != REQUEST_SENSE)
2235 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2236 else if (status_byte(cmd->SCp.Status) != GOOD)
2237 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2238
Boaz Harrosh28424d32007-09-10 22:37:45 +03002239 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2240 hostdata->ses.cmd_len) {
2241 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2242 hostdata->ses.cmd_len = 0 ;
2243 }
2244
Roman Zippelc28bda22007-05-01 22:32:36 +02002245 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2246 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002247 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002248
Finn Thaind65e6342014-03-18 11:42:20 +11002249 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002250
Roman Zippelc28bda22007-05-01 22:32:36 +02002251 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002252 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002253 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002254 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002255 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002256 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002257 cmd->scsi_done(cmd);
2258 }
2259
Finn Thaine3c3da62014-11-12 16:12:15 +11002260 local_irq_restore(flags);
2261
Roman Zippelc28bda22007-05-01 22:32:36 +02002262 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2263 /*
2264 * Restore phase bits to 0 so an interrupted selection,
2265 * arbitration can resume.
2266 */
2267 NCR5380_write(TARGET_COMMAND_REG, 0);
2268
2269 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2270 barrier();
2271
Finn Thaine3c3da62014-11-12 16:12:15 +11002272 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002273 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002274 /* ++roman: For Falcon SCSI, release the lock on the
2275 * ST-DMA here if no other commands are waiting on the
2276 * disconnected queue.
2277 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002278 maybe_release_dma_irq(instance);
2279 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002280 return;
2281 case MESSAGE_REJECT:
2282 /* Accept message by clearing ACK */
2283 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2284 /* Enable reselect interrupts */
2285 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2286 switch (hostdata->last_message) {
2287 case HEAD_OF_QUEUE_TAG:
2288 case ORDERED_QUEUE_TAG:
2289 case SIMPLE_QUEUE_TAG:
2290 /* The target obviously doesn't support tagged
2291 * queuing, even though it announced this ability in
2292 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2293 * clear 'tagged_supported' and lock the LUN, since
2294 * the command is treated as untagged further on.
2295 */
2296 cmd->device->tagged_supported = 0;
2297 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2298 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002299 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002300 "QUEUE_TAG message; tagged queuing "
2301 "disabled\n",
2302 HOSTNO, cmd->device->id, cmd->device->lun);
2303 break;
2304 }
2305 break;
2306 case DISCONNECT:
2307 /* Accept message by clearing ACK */
2308 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2309 local_irq_save(flags);
2310 cmd->device->disconnect = 1;
2311 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002312 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002313 hostdata->connected = NULL;
2314 hostdata->disconnected_queue = cmd;
2315 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002316 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002317 "moved from connected to the "
2318 "disconnected_queue\n", HOSTNO,
2319 cmd->device->id, cmd->device->lun);
2320 /*
2321 * Restore phase bits to 0 so an interrupted selection,
2322 * arbitration can resume.
2323 */
2324 NCR5380_write(TARGET_COMMAND_REG, 0);
2325
2326 /* Enable reselect interrupts */
2327 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2328 /* Wait for bus free to avoid nasty timeouts */
2329 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2330 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002331#ifdef SUN3_SCSI_VME
2332 dregs->csr |= CSR_DMA_ENABLE;
2333#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002334 return;
2335 /*
2336 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2337 * operation, in violation of the SCSI spec so we can safely
2338 * ignore SAVE/RESTORE pointers calls.
2339 *
2340 * Unfortunately, some disks violate the SCSI spec and
2341 * don't issue the required SAVE_POINTERS message before
2342 * disconnecting, and we have to break spec to remain
2343 * compatible.
2344 */
2345 case SAVE_POINTERS:
2346 case RESTORE_POINTERS:
2347 /* Accept message by clearing ACK */
2348 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2349 /* Enable reselect interrupts */
2350 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2351 break;
2352 case EXTENDED_MESSAGE:
2353 /*
2354 * Extended messages are sent in the following format :
2355 * Byte
2356 * 0 EXTENDED_MESSAGE == 1
2357 * 1 length (includes one byte for code, doesn't
2358 * include first two bytes)
2359 * 2 code
2360 * 3..length+1 arguments
2361 *
2362 * Start the extended message buffer with the EXTENDED_MESSAGE
2363 * byte, since spi_print_msg() wants the whole thing.
2364 */
2365 extended_msg[0] = EXTENDED_MESSAGE;
2366 /* Accept first byte by clearing ACK */
2367 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2368
Finn Thaind65e6342014-03-18 11:42:20 +11002369 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002370
2371 len = 2;
2372 data = extended_msg + 1;
2373 phase = PHASE_MSGIN;
2374 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002375 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002376 (int)extended_msg[1], (int)extended_msg[2]);
2377
2378 if (!len && extended_msg[1] <=
2379 (sizeof(extended_msg) - 1)) {
2380 /* Accept third byte by clearing ACK */
2381 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2382 len = extended_msg[1] - 1;
2383 data = extended_msg + 3;
2384 phase = PHASE_MSGIN;
2385
2386 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002387 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002388 HOSTNO, len);
2389
2390 switch (extended_msg[2]) {
2391 case EXTENDED_SDTR:
2392 case EXTENDED_WDTR:
2393 case EXTENDED_MODIFY_DATA_POINTER:
2394 case EXTENDED_EXTENDED_IDENTIFY:
2395 tmp = 0;
2396 }
2397 } else if (len) {
2398 printk(KERN_NOTICE "scsi%d: error receiving "
2399 "extended message\n", HOSTNO);
2400 tmp = 0;
2401 } else {
2402 printk(KERN_NOTICE "scsi%d: extended message "
2403 "code %02x length %d is too long\n",
2404 HOSTNO, extended_msg[2], extended_msg[1]);
2405 tmp = 0;
2406 }
2407 /* Fall through to reject message */
2408
2409 /*
2410 * If we get something weird that we aren't expecting,
2411 * reject it.
2412 */
2413 default:
2414 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002415 printk(KERN_INFO "scsi%d: rejecting message ",
2416 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002417 spi_print_msg(extended_msg);
2418 printk("\n");
2419 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002420 scmd_printk(KERN_INFO, cmd,
2421 "rejecting unknown message %02x\n",
2422 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002423 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002424 scmd_printk(KERN_INFO, cmd,
2425 "rejecting unknown extended message code %02x, length %d\n",
2426 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002427
2428 msgout = MESSAGE_REJECT;
2429 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2430 break;
2431 } /* switch (tmp) */
2432 break;
2433 case PHASE_MSGOUT:
2434 len = 1;
2435 data = &msgout;
2436 hostdata->last_message = msgout;
2437 NCR5380_transfer_pio(instance, &phase, &len, &data);
2438 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002439 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002440#ifdef SUPPORT_TAGS
2441 cmd_free_tag(cmd);
2442#else
2443 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2444#endif
2445 hostdata->connected = NULL;
2446 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002447 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002448 maybe_release_dma_irq(instance);
2449 local_irq_restore(flags);
2450 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002451 return;
2452 }
2453 msgout = NOP;
2454 break;
2455 case PHASE_CMDOUT:
2456 len = cmd->cmd_len;
2457 data = cmd->cmnd;
2458 /*
2459 * XXX for performance reasons, on machines with a
2460 * PSEUDO-DMA architecture we should probably
2461 * use the dma transfer function.
2462 */
2463 NCR5380_transfer_pio(instance, &phase, &len, &data);
2464 break;
2465 case PHASE_STATIN:
2466 len = 1;
2467 data = &tmp;
2468 NCR5380_transfer_pio(instance, &phase, &len, &data);
2469 cmd->SCp.Status = tmp;
2470 break;
2471 default:
2472 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002473 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002474 } /* switch(phase) */
2475 } /* if (tmp * SR_REQ) */
2476 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479/*
2480 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2481 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002482 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002483 * 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 -07002484 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * Inputs : instance - this instance of the NCR5380.
2487 *
2488 */
2489
2490
Finn Thaine3f463b2014-11-12 16:12:16 +11002491/* it might eventually prove necessary to do a dma setup on
2492 reselection, but it doesn't seem to be needed now -- sam */
2493
Roman Zippelc28bda22007-05-01 22:32:36 +02002494static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Roman Zippelc28bda22007-05-01 22:32:36 +02002496 SETUP_HOSTDATA(instance);
2497 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002498 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002500 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002502 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002503 int __maybe_unused len;
2504 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002505 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Roman Zippelc28bda22007-05-01 22:32:36 +02002507 /*
2508 * Disable arbitration, etc. since the host adapter obviously
2509 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Roman Zippelc28bda22007-05-01 22:32:36 +02002512 NCR5380_write(MODE_REG, MR_BASE);
2513 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Roman Zippelc28bda22007-05-01 22:32:36 +02002515 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2516
Finn Thaind65e6342014-03-18 11:42:20 +11002517 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002518
2519 /*
2520 * At this point, we have detected that our SCSI ID is on the bus,
2521 * SEL is true and BSY was false for at least one bus settle delay
2522 * (400 ns).
2523 *
2524 * We must assert BSY ourselves, until the target drops the SEL
2525 * signal.
2526 */
2527
2528 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2529
2530 while (NCR5380_read(STATUS_REG) & SR_SEL)
2531 ;
2532 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2533
2534 /*
2535 * Wait for target to go into MSGIN.
2536 */
2537
2538 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2539 ;
2540
Finn Thaine3f463b2014-11-12 16:12:16 +11002541#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2542 /* acknowledge toggle to MSGIN */
2543 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2544
2545 /* peek at the byte without really hitting the bus */
2546 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2547#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002548 len = 1;
2549 data = msg;
2550 phase = PHASE_MSGIN;
2551 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002552#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002553
2554 if (!(msg[0] & 0x80)) {
2555 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2556 spi_print_msg(msg);
2557 do_abort(instance);
2558 return;
2559 }
2560 lun = (msg[0] & 0x07);
2561
Finn Thaine3f463b2014-11-12 16:12:16 +11002562#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002563 /* If the phase is still MSGIN, the target wants to send some more
2564 * messages. In case it supports tagged queuing, this is probably a
2565 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2566 */
2567 tag = TAG_NONE;
2568 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2569 /* Accept previous IDENTIFY message by clearing ACK */
2570 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2571 len = 2;
2572 data = msg + 1;
2573 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2574 msg[1] == SIMPLE_QUEUE_TAG)
2575 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002576 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002577 "reselection\n", HOSTNO, target_mask, lun, tag);
2578 }
2579#endif
2580
2581 /*
2582 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2583 * just reestablished, and remove it from the disconnected queue.
2584 */
2585
Finn Thain710ddd02014-11-12 16:12:02 +11002586 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002587 tmp; prev = tmp, tmp = NEXT(tmp)) {
2588 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2589#ifdef SUPPORT_TAGS
2590 && (tag == tmp->tag)
2591#endif
2592 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002593 if (prev) {
2594 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002595 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002596 } else {
2597 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2598 hostdata->disconnected_queue = NEXT(tmp);
2599 }
Roman Zippel3130d902007-05-01 22:32:37 +02002600 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002601 break;
2602 }
2603 }
2604
2605 if (!tmp) {
2606 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2607#ifdef SUPPORT_TAGS
2608 "tag %d "
2609#endif
2610 "not in disconnected_queue.\n",
2611 HOSTNO, target_mask, lun
2612#ifdef SUPPORT_TAGS
2613 , tag
2614#endif
2615 );
2616 /*
2617 * Since we have an established nexus that we can't do anything
2618 * with, we must abort it.
2619 */
2620 do_abort(instance);
2621 return;
2622 }
2623
Finn Thaine3f463b2014-11-12 16:12:16 +11002624#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2625 /* engage dma setup for the command we just saw */
2626 {
2627 void *d;
2628 unsigned long count;
2629
2630 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2631 count = tmp->SCp.buffer->length;
2632 d = sg_virt(tmp->SCp.buffer);
2633 } else {
2634 count = tmp->SCp.this_residual;
2635 d = tmp->SCp.ptr;
2636 }
2637 /* setup this command for dma if not already */
2638 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2639 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2640 sun3_dma_setup_done = tmp;
2641 }
2642 }
2643
2644 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2645#endif
2646
Roman Zippelc28bda22007-05-01 22:32:36 +02002647 /* Accept message by clearing ACK */
2648 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2649
Finn Thaine3f463b2014-11-12 16:12:16 +11002650#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2651 /* If the phase is still MSGIN, the target wants to send some more
2652 * messages. In case it supports tagged queuing, this is probably a
2653 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2654 */
2655 tag = TAG_NONE;
2656 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2657 /* Accept previous IDENTIFY message by clearing ACK */
2658 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2659 len = 2;
2660 data = msg + 1;
2661 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2662 msg[1] == SIMPLE_QUEUE_TAG)
2663 tag = msg[2];
2664 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2665 HOSTNO, target_mask, lun, tag);
2666 }
2667#endif
2668
Roman Zippelc28bda22007-05-01 22:32:36 +02002669 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002670 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002671 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674
2675/*
Finn Thain710ddd02014-11-12 16:12:02 +11002676 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 *
2678 * Purpose : abort a command
2679 *
Finn Thain710ddd02014-11-12 16:12:02 +11002680 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002681 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 * used.
2683 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002684 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002686 * XXX - there is no way to abort the command that is currently
2687 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002689 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 */
2691
2692static
Finn Thain710ddd02014-11-12 16:12:02 +11002693int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Roman Zippelc28bda22007-05-01 22:32:36 +02002695 struct Scsi_Host *instance = cmd->device->host;
2696 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002697 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002698 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002700 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Roman Zippelc28bda22007-05-01 22:32:36 +02002702 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Roman Zippelc28bda22007-05-01 22:32:36 +02002704 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Finn Thaind65e6342014-03-18 11:42:20 +11002706 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002707 NCR5380_read(BUS_AND_STATUS_REG),
2708 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002711 /*
2712 * Case 1 : If the command is the currently executing command,
2713 * we'll set the aborted flag and return control so that
2714 * information transfer routine can exit cleanly.
2715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Roman Zippelc28bda22007-05-01 22:32:36 +02002717 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Finn Thaind65e6342014-03-18 11:42:20 +11002719 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002720 /*
2721 * We should perform BSY checking, and make sure we haven't slipped
2722 * into BUS FREE.
2723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Roman Zippelc28bda22007-05-01 22:32:36 +02002725 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2726 /*
2727 * Since we can't change phases until we've completed the current
2728 * handshake, we have to source or sink a byte of data if the current
2729 * phase is not MSGOUT.
2730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Roman Zippelc28bda22007-05-01 22:32:36 +02002732 /*
2733 * Return control to the executing NCR drive so we can clear the
2734 * aborted flag and get back into our main loop.
2735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Roman Zippelc28bda22007-05-01 22:32:36 +02002737 if (do_abort(instance) == 0) {
2738 hostdata->aborted = 1;
2739 hostdata->connected = NULL;
2740 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002742 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002744 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002746 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002747 local_irq_restore(flags);
2748 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002749 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002750 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002751 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002752 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002753 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002757
2758 /*
2759 * Case 2 : If the command hasn't been issued yet, we simply remove it
2760 * from the issue queue.
2761 */
Finn Thain710ddd02014-11-12 16:12:02 +11002762 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2763 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002764 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2765 if (cmd == tmp) {
2766 REMOVE(5, *prev, tmp, NEXT(tmp));
2767 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002768 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002769 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002770 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002771 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002772 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002773 HOSTNO);
2774 /* Tagged queuing note: no tag to free here, hasn't been assigned
2775 * yet... */
2776 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002777 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
2779 }
2780
Roman Zippelc28bda22007-05-01 22:32:36 +02002781 /*
2782 * Case 3 : If any commands are connected, we're going to fail the abort
2783 * and let the high level SCSI driver retry at a later time or
2784 * issue a reset.
2785 *
2786 * Timeouts, and therefore aborted commands, will be highly unlikely
2787 * and handling them cleanly in this situation would make the common
2788 * case of noresets less efficient, and would pollute our code. So,
2789 * we fail.
2790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Roman Zippelc28bda22007-05-01 22:32:36 +02002792 if (hostdata->connected) {
2793 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002794 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002795 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Roman Zippelc28bda22007-05-01 22:32:36 +02002798 /*
2799 * Case 4: If the command is currently disconnected from the bus, and
2800 * there are no connected commands, we reconnect the I_T_L or
2801 * I_T_L_Q nexus associated with it, go into message out, and send
2802 * an abort message.
2803 *
2804 * This case is especially ugly. In order to reestablish the nexus, we
2805 * need to call NCR5380_select(). The easiest way to implement this
2806 * function was to abort if the bus was busy, and let the interrupt
2807 * handler triggered on the SEL for reselect take care of lost arbitrations
2808 * where necessary, meaning interrupts need to be enabled.
2809 *
2810 * When interrupts are enabled, the queues may change - so we
2811 * can't remove it from the disconnected queue before selecting it
2812 * because that could cause a failure in hashing the nexus if that
2813 * device reselected.
2814 *
2815 * Since the queues may change, we can't use the pointers from when we
2816 * first locate it.
2817 *
2818 * So, we must first locate the command, and if NCR5380_select()
2819 * succeeds, then issue the abort, relocate the command and remove
2820 * it from the disconnected queue.
2821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Finn Thain710ddd02014-11-12 16:12:02 +11002823 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002824 tmp = NEXT(tmp)) {
2825 if (cmd == tmp) {
2826 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002827 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002828
Finn Thain76f13b92014-11-12 16:11:53 +11002829 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002830 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002831
Finn Thaind65e6342014-03-18 11:42:20 +11002832 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002833
2834 do_abort(instance);
2835
2836 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002837 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2838 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002839 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2840 if (cmd == tmp) {
2841 REMOVE(5, *prev, tmp, NEXT(tmp));
2842 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002843 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002844 tmp->result = DID_ABORT << 16;
2845 /* We must unlock the tag/LUN immediately here, since the
2846 * target goes to BUS FREE and doesn't send us another
2847 * message (COMMAND_COMPLETE or the like)
2848 */
2849#ifdef SUPPORT_TAGS
2850 cmd_free_tag(tmp);
2851#else
2852 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2853#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002854 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002855 local_irq_restore(flags);
2856 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002857 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002858 }
2859 }
2860 }
2861 }
2862
Finn Thaine3c3da62014-11-12 16:12:15 +11002863 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2864 * possible at all) At least, we should check if the lock could be
2865 * released after the abort, in case it is kept due to some bug.
2866 */
2867 maybe_release_dma_irq(instance);
2868 local_irq_restore(flags);
2869
Roman Zippelc28bda22007-05-01 22:32:36 +02002870 /*
2871 * Case 5 : If we reached this point, the command was not found in any of
2872 * the queues.
2873 *
2874 * We probably reached this point because of an unlikely race condition
2875 * between the command completing successfully and the abortion code,
2876 * so we won't panic, but we will notify the user in case something really
2877 * broke.
2878 */
2879
Joe Perchesad361c92009-07-06 13:05:40 -07002880 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002881
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002882 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
2885
Roman Zippelc28bda22007-05-01 22:32:36 +02002886/*
Finn Thain710ddd02014-11-12 16:12:02 +11002887 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002888 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 * Purpose : reset the SCSI bus.
2890 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002891 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Finn Thain710ddd02014-11-12 16:12:02 +11002895static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Finn Thaine3c3da62014-11-12 16:12:15 +11002897 struct Scsi_Host *instance = cmd->device->host;
2898 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002899 int i;
2900 unsigned long flags;
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002901#if defined(RESET_RUN_DONE)
Finn Thain710ddd02014-11-12 16:12:02 +11002902 struct scsi_cmnd *connected, *disconnected_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903#endif
2904
Finn Thaine3c3da62014-11-12 16:12:15 +11002905 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Roman Zippelc28bda22007-05-01 22:32:36 +02002907 /* get in phase */
2908 NCR5380_write(TARGET_COMMAND_REG,
2909 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2910 /* assert RST */
2911 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2912 udelay(40);
2913 /* reset NCR registers */
2914 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2915 NCR5380_write(MODE_REG, MR_BASE);
2916 NCR5380_write(TARGET_COMMAND_REG, 0);
2917 NCR5380_write(SELECT_ENABLE_REG, 0);
2918 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2919 * through anymore ... */
2920 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002922 /* MSch 20140115 - looking at the generic NCR5380 driver, all of this
2923 * should go.
2924 * Catch-22: if we don't clear all queues, the SCSI driver lock will
2925 * not be reset by atari_scsi_reset()!
2926 */
2927
2928#if defined(RESET_RUN_DONE)
2929 /* XXX Should now be done by midlevel code, but it's broken XXX */
Roman Zippelc28bda22007-05-01 22:32:36 +02002930 /* XXX see below XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Roman Zippelc28bda22007-05-01 22:32:36 +02002932 /* MSch: old-style reset: actually abort all command processing here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Roman Zippelc28bda22007-05-01 22:32:36 +02002934 /* After the reset, there are no more connected or disconnected commands
2935 * and no busy units; to avoid problems with re-inserting the commands
2936 * into the issue_queue (via scsi_done()), the aborted commands are
2937 * remembered in local variables first.
2938 */
2939 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002940 connected = (struct scsi_cmnd *)hostdata->connected;
Roman Zippelc28bda22007-05-01 22:32:36 +02002941 hostdata->connected = NULL;
Finn Thain710ddd02014-11-12 16:12:02 +11002942 disconnected_queue = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002943 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002945 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002947 for (i = 0; i < 8; ++i)
2948 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002950 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002952 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Roman Zippelc28bda22007-05-01 22:32:36 +02002954 /* In order to tell the mid-level code which commands were aborted,
2955 * set the command status to DID_RESET and call scsi_done() !!!
2956 * This ultimately aborts processing of these commands in the mid-level.
2957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Roman Zippelc28bda22007-05-01 22:32:36 +02002959 if ((cmd = connected)) {
Finn Thaind65e6342014-03-18 11:42:20 +11002960 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002961 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2962 cmd->scsi_done(cmd);
2963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Roman Zippelc28bda22007-05-01 22:32:36 +02002965 for (i = 0; (cmd = disconnected_queue); ++i) {
2966 disconnected_queue = NEXT(cmd);
Roman Zippel3130d902007-05-01 22:32:37 +02002967 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002968 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2969 cmd->scsi_done(cmd);
2970 }
2971 if (i > 0)
Finn Thaind65e6342014-03-18 11:42:20 +11002972 dprintk(NDEBUG_ABORT, "scsi: reset aborted %d disconnected command(s)\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Roman Zippelc28bda22007-05-01 22:32:36 +02002974 /* The Falcon lock should be released after a reset...
2975 */
2976 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2977 * unlocking and enabling dma interrupt.
2978 */
2979/* falcon_release_lock_if_possible( hostdata );*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Roman Zippelc28bda22007-05-01 22:32:36 +02002981 /* since all commands have been explicitly terminated, we need to tell
2982 * the midlevel code that the reset was SUCCESSFUL, and there is no
2983 * need to 'wake up' the commands by a request_sense
2984 */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002985 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986#else /* 1 */
2987
Roman Zippelc28bda22007-05-01 22:32:36 +02002988 /* MSch: new-style reset handling: let the mid-level do what it can */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Roman Zippelc28bda22007-05-01 22:32:36 +02002990 /* ++guenther: MID-LEVEL IS STILL BROKEN.
2991 * Mid-level is supposed to requeue all commands that were active on the
2992 * various low-level queues. In fact it does this, but that's not enough
2993 * because all these commands are subject to timeout. And if a timeout
2994 * happens for any removed command, *_abort() is called but all queues
2995 * are now empty. Abort then gives up the falcon lock, which is fatal,
2996 * since the mid-level will queue more commands and must have the lock
2997 * (it's all happening inside timer interrupt handler!!).
2998 * Even worse, abort will return NOT_RUNNING for all those commands not
2999 * on any queue, so they won't be retried ...
3000 *
3001 * Conclusion: either scsi.c disables timeout for all resetted commands
3002 * immediately, or we lose! As of linux-2.0.20 it doesn't.
3003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Roman Zippelc28bda22007-05-01 22:32:36 +02003005 /* After the reset, there are no more connected or disconnected commands
3006 * and no busy units; so clear the low-level status here to avoid
3007 * conflicts when the mid-level code tries to wake up the affected
3008 * commands!
3009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Roman Zippelc28bda22007-05-01 22:32:36 +02003011 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003012 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003013 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11003014 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003015 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003016 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Roman Zippelc28bda22007-05-01 22:32:36 +02003018 local_irq_save(flags);
3019 hostdata->issue_queue = NULL;
3020 hostdata->connected = NULL;
3021 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02003023 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02003025 for (i = 0; i < 8; ++i)
3026 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02003028 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11003030
3031 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02003032 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Roman Zippelc28bda22007-05-01 22:32:36 +02003034 /* we did no complete reset of all commands, so a wakeup is required */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003035 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036#endif /* 1 */
3037}