blob: 590035f194e33aa43ea879d3d2a3e78d27ea6a8a [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
Finn Thainca513fc2014-11-12 16:12:19 +1100277static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Roman Zippelc28bda22007-05-01 22:32:36 +0200279 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100280 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200281
Finn Thainca513fc2014-11-12 16:12:19 +1100282 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200283 return;
284
285 for (target = 0; target < 8; ++target) {
286 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100287 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200288 bitmap_zero(ta->allocated, MAX_TAGS);
289 ta->nr_allocated = 0;
290 /* At the beginning, assume the maximum queue size we could
291 * support (MAX_TAGS). This value will be decreased if the target
292 * returns QUEUE_FULL status.
293 */
294 ta->queue_size = MAX_TAGS;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299
300/* Check if we can issue a command to this LUN: First see if the LUN is marked
301 * busy by an untagged command. If the command should use tagged queuing, also
302 * check that there is a free tag and the target's queue won't overflow. This
303 * function should be called with interrupts disabled to avoid race
304 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Finn Thain710ddd02014-11-12 16:12:02 +1100307static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200309 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200310 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200312 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200313 return 1;
314 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100315 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
316 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200317 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100318 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
319 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100320 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200321 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 return 1;
323 }
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327
328/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
329 * must be called before!), or reserve the LUN in 'busy' if the command is
330 * untagged.
331 */
332
Finn Thain710ddd02014-11-12 16:12:02 +1100333static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200335 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200336 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Roman Zippelc28bda22007-05-01 22:32:36 +0200338 /* If we or the target don't support tagged queuing, allocate the LUN for
339 * an untagged command.
340 */
341 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100342 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
343 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200344 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200345 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100346 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200347 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200348 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100349 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Roman Zippelc28bda22007-05-01 22:32:36 +0200351 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
352 set_bit(cmd->tag, ta->allocated);
353 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100354 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200355 "(now %d tags in use)\n",
356 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200357 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361
362/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
363 * unlock the LUN.
364 */
365
Finn Thain710ddd02014-11-12 16:12:02 +1100366static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200368 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200369 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Roman Zippelc28bda22007-05-01 22:32:36 +0200371 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200372 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100373 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200374 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200375 } else if (cmd->tag >= MAX_TAGS) {
376 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
377 H_NO(cmd), cmd->tag);
378 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100379 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200380 clear_bit(cmd->tag, ta->allocated);
381 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100382 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200383 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387
Finn Thainca513fc2014-11-12 16:12:19 +1100388static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Roman Zippelc28bda22007-05-01 22:32:36 +0200390 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100391 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Finn Thainca513fc2014-11-12 16:12:19 +1100393 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200394 return;
395
396 for (target = 0; target < 8; ++target) {
397 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100398 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200399 bitmap_zero(ta->allocated, MAX_TAGS);
400 ta->nr_allocated = 0;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405#endif /* SUPPORT_TAGS */
406
407
408/*
Finn Thain710ddd02014-11-12 16:12:02 +1100409 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
411 * Purpose: Try to merge several scatter-gather requests into one DMA
412 * transfer. This is possible if the scatter buffers lie on
413 * physical contiguous addresses.
414 *
Finn Thain710ddd02014-11-12 16:12:02 +1100415 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300417 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419
Finn Thain710ddd02014-11-12 16:12:02 +1100420static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Finn Thaine3f463b2014-11-12 16:12:16 +1100422#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200423 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200425 unsigned long oldlen = cmd->SCp.this_residual;
426 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#endif
428
Roman Zippelc28bda22007-05-01 22:32:36 +0200429 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
430 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200431 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100432 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200433 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200435 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200437 ++cmd->SCp.buffer;
438 --cmd->SCp.buffers_residual;
439 cmd->SCp.this_residual += cmd->SCp.buffer->length;
440 endaddr += cmd->SCp.buffer->length;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200443 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100444 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200445 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100447#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Finn Thainff50f9e2014-11-12 16:12:18 +1100450/**
451 * initialize_SCp - init the scsi pointer field
452 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100454 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
456
Finn Thain710ddd02014-11-12 16:12:02 +1100457static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Roman Zippelc28bda22007-05-01 22:32:36 +0200459 /*
460 * Initialize the Scsi Pointer field so that all of the commands in the
461 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200463
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200464 if (scsi_bufflen(cmd)) {
465 cmd->SCp.buffer = scsi_sglist(cmd);
466 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200467 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200468 cmd->SCp.this_residual = cmd->SCp.buffer->length;
469 /* ++roman: Try to merge some scatter-buffers if they are at
470 * contiguous physical addresses.
471 */
472 merge_contiguous_buffers(cmd);
473 } else {
474 cmd->SCp.buffer = NULL;
475 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200476 cmd->SCp.ptr = NULL;
477 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#include <linux/delay.h>
482
483#if NDEBUG
484static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200485 unsigned char mask;
486 const char *name;
487} signals[] = {
488 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
489 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
490 { SR_SEL, "SEL" }, {0, NULL}
491}, basrs[] = {
492 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
493}, icrs[] = {
494 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
495 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
496 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
497 {0, NULL}
498}, mrs[] = {
499 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
500 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
501 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
502 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
503 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
504 {0, NULL}
505};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Finn Thainff50f9e2014-11-12 16:12:18 +1100507/**
508 * NCR5380_print - print scsi bus signals
509 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100511 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513
Roman Zippelc28bda22007-05-01 22:32:36 +0200514static void NCR5380_print(struct Scsi_Host *instance)
515{
516 unsigned char status, data, basr, mr, icr, i;
517 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Roman Zippelc28bda22007-05-01 22:32:36 +0200519 local_irq_save(flags);
520 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
521 status = NCR5380_read(STATUS_REG);
522 mr = NCR5380_read(MODE_REG);
523 icr = NCR5380_read(INITIATOR_COMMAND_REG);
524 basr = NCR5380_read(BUS_AND_STATUS_REG);
525 local_irq_restore(flags);
526 printk("STATUS_REG: %02x ", status);
527 for (i = 0; signals[i].mask; ++i)
528 if (status & signals[i].mask)
529 printk(",%s", signals[i].name);
530 printk("\nBASR: %02x ", basr);
531 for (i = 0; basrs[i].mask; ++i)
532 if (basr & basrs[i].mask)
533 printk(",%s", basrs[i].name);
534 printk("\nICR: %02x ", icr);
535 for (i = 0; icrs[i].mask; ++i)
536 if (icr & icrs[i].mask)
537 printk(",%s", icrs[i].name);
538 printk("\nMODE: %02x ", mr);
539 for (i = 0; mrs[i].mask; ++i)
540 if (mr & mrs[i].mask)
541 printk(",%s", mrs[i].name);
542 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200546 unsigned char value;
547 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200549 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
550 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
551 {PHASE_UNKNOWN, "UNKNOWN"}
552};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Finn Thainff50f9e2014-11-12 16:12:18 +1100554/**
555 * NCR5380_print_phase - show SCSI phase
556 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100558 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100560 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
562
563static void NCR5380_print_phase(struct Scsi_Host *instance)
564{
Roman Zippelc28bda22007-05-01 22:32:36 +0200565 unsigned char status;
566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Roman Zippelc28bda22007-05-01 22:32:36 +0200568 status = NCR5380_read(STATUS_REG);
569 if (!(status & SR_REQ))
570 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
571 else {
572 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
573 (phases[i].value != (status & PHASE_MASK)); ++i)
574 ;
575 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580
581/*
582 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * If we're not in an interrupt, we can call our main directly, it cannot be
585 * already running. Else, we queue it on a task queue, if not 'main_running'
586 * tells us that a lower level is already executing it. This way,
587 * 'main_running' needs not be protected in a special way.
588 *
589 * queue_main() is a utility function for putting our main onto the task
590 * queue, if main_running is false. It should be called only from a
591 * interrupt or bottom half.
592 */
593
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900594#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#include <linux/workqueue.h>
596#include <linux/interrupt.h>
597
Roman Zippelc28bda22007-05-01 22:32:36 +0200598static volatile int main_running;
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200599static DECLARE_WORK(NCR5380_tqueue, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Roman Zippelc28bda22007-05-01 22:32:36 +0200601static inline void queue_main(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Roman Zippelc28bda22007-05-01 22:32:36 +0200603 if (!main_running) {
604 /* If in interrupt and NCR5380_main() not already running,
605 queue it on the 'immediate' task queue, to be processed
606 immediately after the current interrupt processing has
607 finished. */
608 schedule_work(&NCR5380_tqueue);
609 }
610 /* else: nothing to do: the running NCR5380_main() will pick up
611 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614
Roman Zippelc28bda22007-05-01 22:32:36 +0200615static inline void NCR5380_all_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Roman Zippelc28bda22007-05-01 22:32:36 +0200617 static int done = 0;
618 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +1100619 dprintk(NDEBUG_INIT, "scsi : NCR5380_all_init()\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200620 done = 1;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Finn Thain8c325132014-11-12 16:11:58 +1100624/**
625 * NCR58380_info - report driver and host information
626 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *
Finn Thain8c325132014-11-12 16:11:58 +1100628 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
Finn Thain8c325132014-11-12 16:11:58 +1100630 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632
Finn Thain8c325132014-11-12 16:11:58 +1100633static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Finn Thain8c325132014-11-12 16:11:58 +1100635 struct NCR5380_hostdata *hostdata = shost_priv(instance);
636
637 return hostdata->info;
638}
639
640static void prepare_info(struct Scsi_Host *instance)
641{
642 struct NCR5380_hostdata *hostdata = shost_priv(instance);
643
644 snprintf(hostdata->info, sizeof(hostdata->info),
645 "%s, io_port 0x%lx, n_io_port %d, "
646 "base 0x%lx, irq %d, "
647 "can_queue %d, cmd_per_lun %d, "
648 "sg_tablesize %d, this_id %d, "
Finn Thainca513fc2014-11-12 16:12:19 +1100649 "flags { %s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100650 "options { %s} ",
651 instance->hostt->name, instance->io_port, instance->n_io_port,
652 instance->base, instance->irq,
653 instance->can_queue, instance->cmd_per_lun,
654 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100655 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100656#ifdef DIFFERENTIAL
657 "DIFFERENTIAL "
658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100660 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
662#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100663 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#endif
665#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100666 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
Finn Thain8c325132014-11-12 16:11:58 +1100668 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Finn Thainff50f9e2014-11-12 16:12:18 +1100671/**
672 * NCR5380_print_status - dump controller info
673 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100675 * Print commands in the various queues, called from NCR5380_abort
676 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
678
Finn Thain710ddd02014-11-12 16:12:02 +1100679static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400680{
681 int i, s;
682 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200683 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400684 H_NO(cmd), cmd->device->id, cmd->device->lun);
685 printk(KERN_CONT " command = ");
686 command = cmd->cmnd;
687 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
688 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
689 printk(KERN_CONT " %02x", command[i]);
690 printk("\n");
691}
692
Roman Zippelc28bda22007-05-01 22:32:36 +0200693static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Al Virod89537e2013-03-31 13:24:44 -0400695 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100696 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400697 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Finn Thain8ad3a592014-03-18 11:42:19 +1100699 NCR5380_dprint(NDEBUG_ANY, instance);
700 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Roman Zippelc28bda22007-05-01 22:32:36 +0200702 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Roman Zippelc28bda22007-05-01 22:32:36 +0200704 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400705 printk("NCR5380: coroutine is%s running.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200706 main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200707 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400708 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200709 else
Finn Thain710ddd02014-11-12 16:12:02 +1100710 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400711 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100712 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400713 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Al Virod89537e2013-03-31 13:24:44 -0400715 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100716 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400717 ptr = NEXT(ptr))
718 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200719
720 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400721 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Finn Thain710ddd02014-11-12 16:12:02 +1100724static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Roman Zippelc28bda22007-05-01 22:32:36 +0200726 int i, s;
727 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200728 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200729 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400730 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200731 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400732 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200733 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400734 seq_printf(m, " %02x", command[i]);
735 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Finn Thain4d3d2a52014-11-12 16:11:52 +1100738static int __maybe_unused NCR5380_show_info(struct seq_file *m,
739 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400740{
741 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100742 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400743 unsigned long flags;
744
745 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
746
Al Virod89537e2013-03-31 13:24:44 -0400747 local_irq_save(flags);
748 seq_printf(m, "NCR5380: coroutine is%s running.\n",
749 main_running ? "" : "n't");
750 if (!hostdata->connected)
751 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
752 else
Finn Thain710ddd02014-11-12 16:12:02 +1100753 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400754 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100755 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400756 show_Scsi_Cmnd(ptr, m);
757
758 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100759 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400760 ptr = NEXT(ptr))
761 show_Scsi_Cmnd(ptr, m);
762
763 local_irq_restore(flags);
764 return 0;
765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Finn Thainff50f9e2014-11-12 16:12:18 +1100767/**
768 * NCR5380_init - initialise an NCR5380
769 * @instance: adapter to configure
770 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100772 * Initializes *instance and corresponding 5380 chip,
773 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
775 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100776 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200777 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100778 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
780
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100781static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Roman Zippelc28bda22007-05-01 22:32:36 +0200783 int i;
784 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Roman Zippelc28bda22007-05-01 22:32:36 +0200786 NCR5380_all_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Roman Zippelc28bda22007-05-01 22:32:36 +0200788 hostdata->aborted = 0;
789 hostdata->id_mask = 1 << instance->this_id;
790 hostdata->id_higher_mask = 0;
791 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
792 if (i > hostdata->id_mask)
793 hostdata->id_higher_mask |= i;
794 for (i = 0; i < 8; ++i)
795 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100797 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798#endif
799#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200800 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200802 hostdata->targets_present = 0;
803 hostdata->connected = NULL;
804 hostdata->issue_queue = NULL;
805 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100806 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Roman Zippelc28bda22007-05-01 22:32:36 +0200808 if (!the_template) {
809 the_template = instance->hostt;
810 first_instance = instance;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Finn Thain8c325132014-11-12 16:11:58 +1100813 prepare_info(instance);
814
Roman Zippelc28bda22007-05-01 22:32:36 +0200815 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
816 NCR5380_write(MODE_REG, MR_BASE);
817 NCR5380_write(TARGET_COMMAND_REG, 0);
818 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Roman Zippelc28bda22007-05-01 22:32:36 +0200820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Finn Thainff50f9e2014-11-12 16:12:18 +1100823/**
824 * NCR5380_exit - remove an NCR5380
825 * @instance: adapter to remove
826 *
827 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
828 */
829
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200830static void NCR5380_exit(struct Scsi_Host *instance)
831{
Finn Thainff50f9e2014-11-12 16:12:18 +1100832 cancel_work_sync(&NCR5380_tqueue);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200833}
834
Finn Thainff50f9e2014-11-12 16:12:18 +1100835/**
836 * NCR5380_queue_command - queue a command
837 * @instance: the relevant SCSI adapter
838 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100840 * cmd is added to the per instance issue_queue, with minor
841 * twiddling done to the host specific fields of cmd. If the
842 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844
Finn Thain16b29e72014-11-12 16:12:08 +1100845static int NCR5380_queue_command(struct Scsi_Host *instance,
846 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Finn Thain16b29e72014-11-12 16:12:08 +1100848 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100849 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200850 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200853 switch (cmd->cmnd[0]) {
854 case WRITE_6:
855 case WRITE_10:
856 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
857 H_NO(cmd));
858 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100859 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200860 return 0;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
863
Roman Zippelc28bda22007-05-01 22:32:36 +0200864 /*
865 * We use the host_scribble field as a pointer to the next command
866 * in a queue
867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Roman Zippel3130d902007-05-01 22:32:37 +0200869 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200870 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Roman Zippelc28bda22007-05-01 22:32:36 +0200872 /*
873 * Insert the cmd into the issue queue. Note that REQUEST SENSE
874 * commands are added to the head of the queue since any command will
875 * clear the contingent allegiance condition that exists and the
876 * sense data is only guaranteed to be valid while the condition exists.
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Roman Zippelc28bda22007-05-01 22:32:36 +0200879 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
880 * Otherwise a running NCR5380_main may steal the lock.
881 * Lock before actually inserting due to fairness reasons explained in
882 * atari_scsi.c. If we insert first, then it's impossible for this driver
883 * to release the lock.
884 * Stop timer for this command while waiting for the lock, or timeouts
885 * may happen (and they really do), and it's no good if the command doesn't
886 * appear in any of the queues.
887 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
888 * because also a timer int can trigger an abort or reset, which would
889 * alter queues and touch the lock.
890 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100891 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100892 return SCSI_MLQUEUE_HOST_BUSY;
893
894 local_irq_save(flags);
895
Finn Thainff50f9e2014-11-12 16:12:18 +1100896 /*
897 * Insert the cmd into the issue queue. Note that REQUEST SENSE
898 * commands are added to the head of the queue since any command will
899 * clear the contingent allegiance condition that exists and the
900 * sense data is only guaranteed to be valid while the condition exists.
901 */
902
Roman Zippelc28bda22007-05-01 22:32:36 +0200903 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
904 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200905 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200906 hostdata->issue_queue = cmd;
907 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100908 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200909 NEXT(tmp); tmp = NEXT(tmp))
910 ;
911 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200912 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200913 }
914 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Finn Thaind65e6342014-03-18 11:42:20 +1100916 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200917 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Roman Zippelc28bda22007-05-01 22:32:36 +0200919 /* If queue_command() is called from an interrupt (real one or bottom
920 * half), we let queue_main() do the job of taking care about main. If it
921 * is already running, this is a no-op, else main will be queued.
922 *
923 * If we're not in an interrupt, we can call NCR5380_main()
924 * unconditionally, because it cannot be already running.
925 */
Finn Thain16b29e72014-11-12 16:12:08 +1100926 if (in_interrupt() || irqs_disabled())
Roman Zippelc28bda22007-05-01 22:32:36 +0200927 queue_main();
928 else
929 NCR5380_main(NULL);
930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Finn Thaine3c3da62014-11-12 16:12:15 +1100933static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
934{
935 struct NCR5380_hostdata *hostdata = shost_priv(instance);
936
937 /* Caller does the locking needed to set & test these data atomically */
938 if (!hostdata->disconnected_queue &&
939 !hostdata->issue_queue &&
940 !hostdata->connected &&
941 !hostdata->retain_dma_intr)
942 NCR5380_release_dma_irq(instance);
943}
944
Finn Thainff50f9e2014-11-12 16:12:18 +1100945/**
946 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100948 * NCR5380_main is a coroutine that runs as long as more work can
949 * be done on the NCR5380 host adapters in a system. Both
950 * NCR5380_queue_command() and NCR5380_intr() will try to start it
951 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200952 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100953 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200954 */
955
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200956static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Finn Thain710ddd02014-11-12 16:12:02 +1100958 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +0200959 struct Scsi_Host *instance = first_instance;
960 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
961 int done;
962 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Roman Zippelc28bda22007-05-01 22:32:36 +0200964 /*
965 * We run (with interrupts disabled) until we're sure that none of
966 * the host adapters have anything that can be done, at which point
967 * we set main_running to 0 and exit.
968 *
969 * Interrupts are enabled before doing various other internal
970 * instructions, after we've decided that we need to run through
971 * the loop again.
972 *
973 * this should prevent any race conditions.
974 *
975 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
976 * because also a timer int can trigger an abort or reset, which can
977 * alter queues and touch the Falcon lock.
978 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Roman Zippelc28bda22007-05-01 22:32:36 +0200980 /* Tell int handlers main() is now already executing. Note that
981 no races are possible here. If an int comes in before
982 'main_running' is set here, and queues/executes main via the
983 task queue, it doesn't do any harm, just this instance of main
984 won't find any work left to do. */
985 if (main_running)
986 return;
987 main_running = 1;
988
989 local_save_flags(flags);
990 do {
991 local_irq_disable(); /* Freeze request queues */
992 done = 1;
993
994 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +1100995 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200996 /*
997 * Search through the issue_queue for a command destined
998 * for a target that's not busy.
999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001001 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001002 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1003 ;
1004 /*printk("%p ", tmp);*/
1005 if ((tmp == prev) && tmp)
1006 printk(" LOOP\n");
1007 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001009 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001010 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001011 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Finn Thaine3f463b2014-11-12 16:12:16 +11001013 dprintk(NDEBUG_LISTS,
1014 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1015 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1016 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001017 /* When we find one, remove it from the issue queue. */
1018 /* ++guenther: possible race with Falcon locking */
1019 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001021 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001023 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001024#endif
1025 ) {
1026 /* ++guenther: just to be sure, this must be atomic */
1027 local_irq_disable();
1028 if (prev) {
1029 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001030 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 } else {
1032 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1033 hostdata->issue_queue = NEXT(tmp);
1034 }
Roman Zippel3130d902007-05-01 22:32:37 +02001035 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001036 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001037
1038 /* reenable interrupts after finding one */
1039 local_irq_restore(flags);
1040
1041 /*
1042 * Attempt to establish an I_T_L nexus here.
1043 * On success, instance->hostdata->connected is set.
1044 * On failure, we must add the command back to the
1045 * issue queue so we can keep trying.
1046 */
Finn Thaind65e6342014-03-18 11:42:20 +11001047 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001048 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001049 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001050 /*
1051 * REQUEST SENSE commands are issued without tagged
1052 * queueing, even on SCSI-II devices because the
1053 * contingent allegiance condition exists for the
1054 * entire unit.
1055 */
1056 /* ++roman: ...and the standard also requires that
1057 * REQUEST SENSE command are untagged.
1058 */
1059
1060#ifdef SUPPORT_TAGS
1061 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1062#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001063 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001064 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001065 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001066 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001067 maybe_release_dma_irq(instance);
1068 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001069 break;
1070 } else {
1071 local_irq_disable();
1072 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001073 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001074 hostdata->issue_queue = tmp;
1075#ifdef SUPPORT_TAGS
1076 cmd_free_tag(tmp);
1077#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001078 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001079 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001080 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001081 "returned to issue_queue\n", HOSTNO);
1082 if (hostdata->connected)
1083 break;
1084 }
1085 } /* if target/lun/target queue is not busy */
1086 } /* for issue_queue */
1087 } /* if (!hostdata->connected) */
1088
1089 if (hostdata->connected
1090#ifdef REAL_DMA
1091 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092#endif
1093 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001095 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001096 HOSTNO);
1097 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001098 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001099 done = 0;
1100 }
1101 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Roman Zippelc28bda22007-05-01 22:32:36 +02001103 /* Better allow ints _after_ 'main_running' has been cleared, else
1104 an interrupt could believe we'll pick up the work it left for
1105 us, but we won't see it anymore here... */
1106 main_running = 0;
1107 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110
1111#ifdef REAL_DMA
1112/*
1113 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1114 *
1115 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001116 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *
1118 * Inputs : instance - this instance of the NCR5380.
1119 *
1120 */
1121
Roman Zippelc28bda22007-05-01 22:32:36 +02001122static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Roman Zippelc28bda22007-05-01 22:32:36 +02001124 SETUP_HOSTDATA(instance);
Finn Thaine3f463b2014-11-12 16:12:16 +11001125 int transfered;
1126 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001127 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001128 int saved_data = 0, overrun = 0;
1129 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Roman Zippelc28bda22007-05-01 22:32:36 +02001131 if (!hostdata->connected) {
1132 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1133 "no connected cmd\n", HOSTNO);
1134 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Finn Thainef1081c2014-11-12 16:12:14 +11001137 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001138 p = hostdata->connected->SCp.phase;
1139 if (p & SR_IO) {
1140 udelay(10);
1141 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1142 (BASR_PHASE_MATCH|BASR_ACK)) ==
1143 (BASR_PHASE_MATCH|BASR_ACK)) {
1144 saved_data = NCR5380_read(INPUT_DATA_REG);
1145 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001146 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001147 }
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001150
Finn Thaind65e6342014-03-18 11:42:20 +11001151 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001152 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1153 NCR5380_read(STATUS_REG));
1154
Finn Thaine3f463b2014-11-12 16:12:16 +11001155#if defined(CONFIG_SUN3)
1156 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1157 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1158 instance->host_no);
1159 BUG();
1160 }
1161
1162 /* make sure we're not stuck in a data phase */
1163 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1164 (BASR_PHASE_MATCH | BASR_ACK)) {
1165 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1166 NCR5380_read(BUS_AND_STATUS_REG));
1167 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1168 instance->host_no);
1169 BUG();
1170 }
1171#endif
1172
Roman Zippelc28bda22007-05-01 22:32:36 +02001173 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1174 NCR5380_write(MODE_REG, MR_BASE);
1175 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1176
1177 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1178 hostdata->dma_len = 0;
1179
1180 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1181 count = &hostdata->connected->SCp.this_residual;
1182 *data += transfered;
1183 *count -= transfered;
1184
Finn Thainef1081c2014-11-12 16:12:14 +11001185 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001186 int cnt, toPIO;
1187
Roman Zippelc28bda22007-05-01 22:32:36 +02001188 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001189 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001190 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001191 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001192 *(*data)++ = saved_data;
1193 (*count)--;
1194 cnt--;
1195 toPIO--;
1196 }
Finn Thaind65e6342014-03-18 11:42:20 +11001197 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001198 NCR5380_transfer_pio(instance, &p, &cnt, data);
1199 *count -= toPIO - cnt;
1200 }
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203#endif /* REAL_DMA */
1204
1205
Finn Thainff50f9e2014-11-12 16:12:18 +11001206/**
1207 * NCR5380_intr - generic NCR5380 irq handler
1208 * @irq: interrupt number
1209 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001210 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001211 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1212 * from the disconnected queue, and restarting NCR5380_main()
1213 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
1215
Roman Zippelc28bda22007-05-01 22:32:36 +02001216static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Roman Zippelc28bda22007-05-01 22:32:36 +02001218 struct Scsi_Host *instance = first_instance;
1219 int done = 1, handled = 0;
1220 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Finn Thaind65e6342014-03-18 11:42:20 +11001222 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Roman Zippelc28bda22007-05-01 22:32:36 +02001224 /* Look for pending interrupts */
1225 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001226 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001227 /* dispatch to appropriate routine if found and done=0 */
1228 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001229 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001230 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1231 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001232 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001233 NCR5380_reselect(instance);
1234 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1235 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001236 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001237 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1238 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001239 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001240 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1241 } else {
1242 /*
1243 * The rest of the interrupt conditions can occur only during a
1244 * DMA transfer
1245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001248 /*
1249 * We should only get PHASE MISMATCH and EOP interrupts if we have
1250 * DMA enabled, so do a sanity check based on the current setting
1251 * of the MODE register.
1252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Roman Zippelc28bda22007-05-01 22:32:36 +02001254 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1255 ((basr & BASR_END_DMA_TRANSFER) ||
1256 !(basr & BASR_PHASE_MATCH))) {
1257
Finn Thaind65e6342014-03-18 11:42:20 +11001258 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001259 NCR5380_dma_complete( instance );
1260 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001261 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001263 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001265 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001266 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001267 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1268 HOSTNO, basr, NCR5380_read(MODE_REG),
1269 NCR5380_read(STATUS_REG));
1270 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001271#ifdef SUN3_SCSI_VME
1272 dregs->csr |= CSR_DMA_ENABLE;
1273#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001274 }
1275 } /* if !(SELECTION || PARITY) */
1276 handled = 1;
1277 } /* BASR & IRQ */ else {
1278 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1279 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1280 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1281 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001282#ifdef SUN3_SCSI_VME
1283 dregs->csr |= CSR_DMA_ENABLE;
1284#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001285 }
1286
1287 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001288 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001289 /* Put a call to NCR5380_main() on the queue... */
1290 queue_main();
1291 }
1292 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Roman Zippelc28bda22007-05-01 22:32:36 +02001295/*
Finn Thain710ddd02014-11-12 16:12:02 +11001296 * Function : int NCR5380_select(struct Scsi_Host *instance,
1297 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
1299 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001300 * including ARBITRATION, SELECTION, and initial message out for
1301 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001303 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001304 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001306 * Returns : -1 if selection could not execute for some reason,
1307 * 0 if selection succeeded or failed because the target
1308 * did not respond.
1309 *
1310 * Side effects :
1311 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * with registers as they should have been on entry - ie
1313 * SELECT_ENABLE will be set appropriately, the NCR5380
1314 * will cease to drive any SCSI bus signals.
1315 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001316 * If successful : I_T_L or I_T_L_Q nexus will be established,
1317 * instance->connected will be set to cmd.
1318 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001320 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 * cmd->result host byte set to DID_BAD_TARGET.
1322 */
1323
Finn Thain710ddd02014-11-12 16:12:02 +11001324static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Roman Zippelc28bda22007-05-01 22:32:36 +02001326 SETUP_HOSTDATA(instance);
1327 unsigned char tmp[3], phase;
1328 unsigned char *data;
1329 int len;
1330 unsigned long timeout;
1331 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Roman Zippelc28bda22007-05-01 22:32:36 +02001333 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001334 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001335 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001336 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Roman Zippelc28bda22007-05-01 22:32:36 +02001338 /*
1339 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1340 * data bus during SELECTION.
1341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Roman Zippelc28bda22007-05-01 22:32:36 +02001343 local_irq_save(flags);
1344 if (hostdata->connected) {
1345 local_irq_restore(flags);
1346 return -1;
1347 }
1348 NCR5380_write(TARGET_COMMAND_REG, 0);
1349
1350 /*
1351 * Start arbitration.
1352 */
1353
1354 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1355 NCR5380_write(MODE_REG, MR_ARBITRATE);
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Roman Zippelc28bda22007-05-01 22:32:36 +02001359 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001360#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001361 {
1362 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Roman Zippelc28bda22007-05-01 22:32:36 +02001364 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1365 time_before(jiffies, timeout) && !hostdata->connected)
1366 ;
1367 if (time_after_eq(jiffies, timeout)) {
1368 printk("scsi : arbitration timeout at %d\n", __LINE__);
1369 NCR5380_write(MODE_REG, MR_BASE);
1370 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1371 return -1;
1372 }
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001375 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1376 !hostdata->connected)
1377 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378#endif
1379
Finn Thaind65e6342014-03-18 11:42:20 +11001380 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Roman Zippelc28bda22007-05-01 22:32:36 +02001382 if (hostdata->connected) {
1383 NCR5380_write(MODE_REG, MR_BASE);
1384 return -1;
1385 }
1386 /*
1387 * The arbitration delay is 2.2us, but this is a minimum and there is
1388 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1389 * the integral nature of udelay().
1390 *
1391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Roman Zippelc28bda22007-05-01 22:32:36 +02001393 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Roman Zippelc28bda22007-05-01 22:32:36 +02001395 /* Check for lost arbitration */
1396 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1397 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1398 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1399 hostdata->connected) {
1400 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001401 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001402 HOSTNO);
1403 return -1;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Roman Zippelc28bda22007-05-01 22:32:36 +02001406 /* after/during arbitration, BSY should be asserted.
1407 IBM DPES-31080 Version S31Q works now */
1408 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1409 NCR5380_write(INITIATOR_COMMAND_REG,
1410 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1413 hostdata->connected) {
1414 NCR5380_write(MODE_REG, MR_BASE);
1415 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001416 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001417 HOSTNO);
1418 return -1;
1419 }
1420
1421 /*
1422 * Again, bus clear + bus settle time is 1.2us, however, this is
1423 * a minimum so we'll udelay ceil(1.2)
1424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001427 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1428 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001430 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001432
1433 if (hostdata->connected) {
1434 NCR5380_write(MODE_REG, MR_BASE);
1435 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1436 return -1;
1437 }
1438
Finn Thaind65e6342014-03-18 11:42:20 +11001439 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001440
1441 /*
1442 * Now that we have won arbitration, start Selection process, asserting
1443 * the host and target ID's on the SCSI bus.
1444 */
1445
1446 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1447
1448 /*
1449 * Raise ATN while SEL is true before BSY goes false from arbitration,
1450 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1451 * phase immediately after selection.
1452 */
1453
1454 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1455 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Roman Zippelc28bda22007-05-01 22:32:36 +02001458 /*
1459 * Reselect interrupts must be turned off prior to the dropping of BSY,
1460 * otherwise we will trigger an interrupt.
1461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 if (hostdata->connected) {
1464 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1465 return -1;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Roman Zippelc28bda22007-05-01 22:32:36 +02001470 /*
1471 * The initiator shall then wait at least two deskew delays and release
1472 * the BSY signal.
1473 */
1474 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Roman Zippelc28bda22007-05-01 22:32:36 +02001476 /* Reset BSY */
1477 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1478 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Roman Zippelc28bda22007-05-01 22:32:36 +02001480 /*
1481 * Something weird happens when we cease to drive BSY - looks
1482 * like the board/chip is letting us do another read before the
1483 * appropriate propagation delay has expired, and we're confusing
1484 * a BSY signal from ourselves as the target's response to SELECTION.
1485 *
1486 * A small delay (the 'C++' frontend breaks the pipeline with an
1487 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1488 * tighter 'C' code breaks and requires this) solves the problem -
1489 * the 1 us delay is arbitrary, and only used because this delay will
1490 * be the same on other platforms and since it works here, it should
1491 * work there.
1492 *
1493 * wingel suggests that this could be due to failing to wait
1494 * one deskew delay.
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Roman Zippelc28bda22007-05-01 22:32:36 +02001497 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Finn Thaind65e6342014-03-18 11:42:20 +11001499 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Roman Zippelc28bda22007-05-01 22:32:36 +02001501 /*
1502 * The SCSI specification calls for a 250 ms timeout for the actual
1503 * selection.
1504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Finn Thainff50f9e2014-11-12 16:12:18 +11001506 timeout = jiffies + (250 * HZ / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Roman Zippelc28bda22007-05-01 22:32:36 +02001508 /*
1509 * XXX very interesting - we're seeing a bounce where the BSY we
1510 * asserted is being reflected / still asserted (propagation delay?)
1511 * and it's detecting as true. Sigh.
1512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001515 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1516 * IO while SEL is true. But again, there are some disks out the in the
1517 * world that do that nevertheless. (Somebody claimed that this announces
1518 * reselection capability of the target.) So we better skip that test and
1519 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Roman Zippelc28bda22007-05-01 22:32:36 +02001522 while (time_before(jiffies, timeout) &&
1523 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1524 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Roman Zippelc28bda22007-05-01 22:32:36 +02001526 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1527 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1528 NCR5380_reselect(instance);
1529 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1530 HOSTNO);
1531 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1532 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001535 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1536 ;
1537#endif
1538
1539 /*
1540 * No less than two deskew delays after the initiator detects the
1541 * BSY signal is true, it shall release the SEL signal and may
1542 * change the DATA BUS. -wingel
1543 */
1544
1545 udelay(1);
1546
1547 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1548
1549 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1550 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1551 if (hostdata->targets_present & (1 << cmd->device->id)) {
1552 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1553 if (hostdata->restart_select)
1554 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001555 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001556 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1557 return -1;
1558 }
1559 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001560#ifdef SUPPORT_TAGS
1561 cmd_free_tag(cmd);
1562#endif
1563 cmd->scsi_done(cmd);
1564 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001565 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001566 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1567 return 0;
1568 }
1569
1570 hostdata->targets_present |= (1 << cmd->device->id);
1571
1572 /*
1573 * Since we followed the SCSI spec, and raised ATN while SEL
1574 * was true but before BSY was false during selection, the information
1575 * transfer phase should be a MESSAGE OUT phase so that we can send the
1576 * IDENTIFY message.
1577 *
1578 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1579 * message (2 bytes) with a tag ID that we increment with every command
1580 * until it wraps back to 0.
1581 *
1582 * XXX - it turns out that there are some broken SCSI-II devices,
1583 * which claim to support tagged queuing but fail when more than
1584 * some number of commands are issued at once.
1585 */
1586
1587 /* Wait for start of REQ/ACK handshake */
1588 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1589 ;
1590
Finn Thaind65e6342014-03-18 11:42:20 +11001591 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001592 HOSTNO, cmd->device->id);
1593 tmp[0] = IDENTIFY(1, cmd->device->lun);
1594
1595#ifdef SUPPORT_TAGS
1596 if (cmd->tag != TAG_NONE) {
1597 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1598 tmp[2] = cmd->tag;
1599 len = 3;
1600 } else
1601 len = 1;
1602#else
1603 len = 1;
1604 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605#endif /* SUPPORT_TAGS */
1606
Roman Zippelc28bda22007-05-01 22:32:36 +02001607 /* Send message(s) */
1608 data = tmp;
1609 phase = PHASE_MSGOUT;
1610 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001611 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001612 /* XXX need to handle errors here */
1613 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001615 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1616#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001617#ifdef SUN3_SCSI_VME
1618 dregs->csr |= CSR_INTR;
1619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Roman Zippelc28bda22007-05-01 22:32:36 +02001621 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Roman Zippelc28bda22007-05-01 22:32:36 +02001623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Roman Zippelc28bda22007-05-01 22:32:36 +02001626/*
1627 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * unsigned char *phase, int *count, unsigned char **data)
1629 *
1630 * Purpose : transfers data in given phase using polled I/O
1631 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001632 * Inputs : instance - instance of driver, *phase - pointer to
1633 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001637 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * is in same phase.
1639 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001640 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 *
1642 * XXX Note : handling for bus free may be useful.
1643 */
1644
1645/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001646 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * IS 100% reliable, and for the actual data transfer where speed
1648 * counts, we will always do a pseudo DMA or DMA transfer.
1649 */
1650
Roman Zippelc28bda22007-05-01 22:32:36 +02001651static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1652 unsigned char *phase, int *count,
1653 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Roman Zippelc28bda22007-05-01 22:32:36 +02001655 register unsigned char p = *phase, tmp;
1656 register int c = *count;
1657 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Roman Zippelc28bda22007-05-01 22:32:36 +02001659 /*
1660 * The NCR5380 chip will only drive the SCSI bus when the
1661 * phase specified in the appropriate bits of the TARGET COMMAND
1662 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
1664
Roman Zippelc28bda22007-05-01 22:32:36 +02001665 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Roman Zippelc28bda22007-05-01 22:32:36 +02001667 do {
1668 /*
1669 * Wait for assertion of REQ, after which the phase bits will be
1670 * valid
1671 */
1672 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1673 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Finn Thaind65e6342014-03-18 11:42:20 +11001675 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Roman Zippelc28bda22007-05-01 22:32:36 +02001677 /* Check for phase mismatch */
1678 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001679 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001680 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001681 break;
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Roman Zippelc28bda22007-05-01 22:32:36 +02001684 /* Do actual transfer from SCSI bus to / from memory */
1685 if (!(p & SR_IO))
1686 NCR5380_write(OUTPUT_DATA_REG, *d);
1687 else
1688 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Roman Zippelc28bda22007-05-01 22:32:36 +02001690 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Roman Zippelc28bda22007-05-01 22:32:36 +02001692 /*
1693 * The SCSI standard suggests that in MSGOUT phase, the initiator
1694 * should drop ATN on the last byte of the message phase
1695 * after REQ has been asserted for the handshake but before
1696 * the initiator raises ACK.
1697 */
1698
1699 if (!(p & SR_IO)) {
1700 if (!((p & SR_MSG) && c > 1)) {
1701 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001702 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001703 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1704 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1705 } else {
1706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1707 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001708 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001709 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1710 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1711 }
1712 } else {
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 | ICR_ASSERT_ACK);
1715 }
1716
1717 while (NCR5380_read(STATUS_REG) & SR_REQ)
1718 ;
1719
Finn Thaind65e6342014-03-18 11:42:20 +11001720 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001721
1722 /*
1723 * We have several special cases to consider during REQ/ACK handshaking :
1724 * 1. We were in MSGOUT phase, and we are on the last byte of the
1725 * message. ATN must be dropped as ACK is dropped.
1726 *
1727 * 2. We are in a MSGIN phase, and we are on the last byte of the
1728 * message. We must exit with ACK asserted, so that the calling
1729 * code may raise ATN before dropping ACK to reject the message.
1730 *
1731 * 3. ACK and ATN are clear and the target may proceed as normal.
1732 */
1733 if (!(p == PHASE_MSGIN && c == 1)) {
1734 if (p == PHASE_MSGOUT && c > 1)
1735 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1736 else
1737 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1738 }
1739 } while (--c);
1740
Finn Thaind65e6342014-03-18 11:42:20 +11001741 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001742
1743 *count = c;
1744 *data = d;
1745 tmp = NCR5380_read(STATUS_REG);
1746 /* The phase read from the bus is valid if either REQ is (already)
1747 * asserted or if ACK hasn't been released yet. The latter is the case if
1748 * we're in MSGIN and all wanted bytes have been received.
1749 */
1750 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1751 *phase = tmp & PHASE_MASK;
1752 else
1753 *phase = PHASE_UNKNOWN;
1754
1755 if (!c || (*phase == p))
1756 return 0;
1757 else
1758 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
1761/*
1762 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001763 *
1764 * Purpose : abort the currently established nexus. Should only be
1765 * called from a routine which can drop into a
1766 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * Returns : 0 on success, -1 on failure.
1768 */
1769
Roman Zippelc28bda22007-05-01 22:32:36 +02001770static int do_abort(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Roman Zippelc28bda22007-05-01 22:32:36 +02001772 unsigned char tmp, *msgptr, phase;
1773 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Roman Zippelc28bda22007-05-01 22:32:36 +02001775 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Roman Zippelc28bda22007-05-01 22:32:36 +02001778 /*
1779 * Wait for the target to indicate a valid phase by asserting
1780 * REQ. Once this happens, we'll have either a MSGOUT phase
1781 * and can immediately send the ABORT message, or we'll have some
1782 * other phase and will have to source/sink data.
1783 *
1784 * We really don't care what value was on the bus or what value
1785 * the target sees, so we just handshake.
1786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Roel Kluin3be38e72007-11-15 21:13:46 +01001788 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001789 ;
1790
1791 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1792
1793 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1794 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1795 ICR_ASSERT_ACK);
1796 while (NCR5380_read(STATUS_REG) & SR_REQ)
1797 ;
1798 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1799 }
1800
1801 tmp = ABORT;
1802 msgptr = &tmp;
1803 len = 1;
1804 phase = PHASE_MSGOUT;
1805 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1806
1807 /*
1808 * If we got here, and the command completed successfully,
1809 * we're about to go into bus free state.
1810 */
1811
1812 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001816/*
1817 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * unsigned char *phase, int *count, unsigned char **data)
1819 *
1820 * Purpose : transfers data in given phase using either real
1821 * or pseudo DMA.
1822 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001823 * Inputs : instance - instance of driver, *phase - pointer to
1824 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001826 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001828 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * is in same phase.
1830 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001831 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 *
1833 */
1834
1835
Roman Zippelc28bda22007-05-01 22:32:36 +02001836static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1837 unsigned char *phase, int *count,
1838 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Roman Zippelc28bda22007-05-01 22:32:36 +02001840 SETUP_HOSTDATA(instance);
1841 register int c = *count;
1842 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001843 unsigned long flags;
1844
1845#if defined(CONFIG_SUN3)
1846 /* sanity check */
1847 if (!sun3_dma_setup_done) {
1848 pr_err("scsi%d: transfer_dma without setup!\n",
1849 instance->host_no);
1850 BUG();
1851 }
1852 hostdata->dma_len = c;
1853
1854 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1855 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1856 c, (p & SR_IO) ? "to" : "from", *data);
1857
1858 /* netbsd turns off ints here, why not be safe and do it too */
1859 local_irq_save(flags);
1860
1861 /* send start chain */
1862 sun3scsi_dma_start(c, *data);
1863
1864 if (p & SR_IO) {
1865 NCR5380_write(TARGET_COMMAND_REG, 1);
1866 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1867 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1868 NCR5380_write(MODE_REG,
1869 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1870 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1871 } else {
1872 NCR5380_write(TARGET_COMMAND_REG, 0);
1873 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1874 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1875 NCR5380_write(MODE_REG,
1876 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1877 NCR5380_write(START_DMA_SEND_REG, 0);
1878 }
1879
1880#ifdef SUN3_SCSI_VME
1881 dregs->csr |= CSR_DMA_ENABLE;
1882#endif
1883
1884 local_irq_restore(flags);
1885
1886 sun3_dma_active = 1;
1887
1888#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001889 register unsigned char *d = *data;
1890 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Roman Zippelc28bda22007-05-01 22:32:36 +02001892 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1893 *phase = tmp;
1894 return -1;
1895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Finn Thainef1081c2014-11-12 16:12:14 +11001897 if (hostdata->read_overruns && (p & SR_IO))
1898 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Finn Thaind65e6342014-03-18 11:42:20 +11001900 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001901 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1902 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Roman Zippelc28bda22007-05-01 22:32:36 +02001904 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001907 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908#endif /* def REAL_DMA */
1909
Finn Thainef1081c2014-11-12 16:12:14 +11001910 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001911 /* On the Medusa, it is a must to initialize the DMA before
1912 * starting the NCR. This is also the cleaner way for the TT.
1913 */
1914 local_irq_save(flags);
1915 hostdata->dma_len = (p & SR_IO) ?
1916 NCR5380_dma_read_setup(instance, d, c) :
1917 NCR5380_dma_write_setup(instance, d, c);
1918 local_irq_restore(flags);
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Roman Zippelc28bda22007-05-01 22:32:36 +02001921 if (p & SR_IO)
1922 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1923 else {
1924 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1925 NCR5380_write(START_DMA_SEND_REG, 0);
1926 }
1927
Finn Thainef1081c2014-11-12 16:12:14 +11001928 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001929 /* On the Falcon, the DMA setup must be done after the last */
1930 /* NCR access, else the DMA setup gets trashed!
1931 */
1932 local_irq_save(flags);
1933 hostdata->dma_len = (p & SR_IO) ?
1934 NCR5380_dma_read_setup(instance, d, c) :
1935 NCR5380_dma_write_setup(instance, d, c);
1936 local_irq_restore(flags);
1937 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001938#endif /* !defined(CONFIG_SUN3) */
1939
Roman Zippelc28bda22007-05-01 22:32:36 +02001940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942#endif /* defined(REAL_DMA) */
1943
1944/*
1945 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1946 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001947 * Purpose : run through the various SCSI phases and do as the target
1948 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 * instance->connected.
1950 *
1951 * Inputs : instance, instance for which we are doing commands
1952 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001953 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 * modified if a command disconnects, *instance->connected will
1955 * change.
1956 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001957 * XXX Note : we need to watch for bus free or a reset condition here
1958 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001960
1961static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Roman Zippelc28bda22007-05-01 22:32:36 +02001963 SETUP_HOSTDATA(instance);
1964 unsigned long flags;
1965 unsigned char msgout = NOP;
1966 int sink = 0;
1967 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001969 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001971 unsigned char *data;
1972 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001973 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Finn Thaine3f463b2014-11-12 16:12:16 +11001975#ifdef SUN3_SCSI_VME
1976 dregs->csr |= CSR_INTR;
1977#endif
1978
Roman Zippelc28bda22007-05-01 22:32:36 +02001979 while (1) {
1980 tmp = NCR5380_read(STATUS_REG);
1981 /* We only have a valid SCSI phase when REQ is asserted */
1982 if (tmp & SR_REQ) {
1983 phase = (tmp & PHASE_MASK);
1984 if (phase != old_phase) {
1985 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001986 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001988#if defined(CONFIG_SUN3)
1989 if (phase == PHASE_CMDOUT) {
1990#if defined(REAL_DMA)
1991 void *d;
1992 unsigned long count;
1993
1994 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1995 count = cmd->SCp.buffer->length;
1996 d = sg_virt(cmd->SCp.buffer);
1997 } else {
1998 count = cmd->SCp.this_residual;
1999 d = cmd->SCp.ptr;
2000 }
2001 /* this command setup for dma yet? */
2002 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2003 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2004 sun3scsi_dma_setup(d, count,
2005 rq_data_dir(cmd->request));
2006 sun3_dma_setup_done = cmd;
2007 }
2008 }
2009#endif
2010#ifdef SUN3_SCSI_VME
2011 dregs->csr |= CSR_INTR;
2012#endif
2013 }
2014#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Roman Zippelc28bda22007-05-01 22:32:36 +02002016 if (sink && (phase != PHASE_MSGOUT)) {
2017 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Roman Zippelc28bda22007-05-01 22:32:36 +02002019 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2020 ICR_ASSERT_ACK);
2021 while (NCR5380_read(STATUS_REG) & SR_REQ)
2022 ;
2023 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2024 ICR_ASSERT_ATN);
2025 sink = 0;
2026 continue;
2027 }
2028
2029 switch (phase) {
2030 case PHASE_DATAOUT:
2031#if (NDEBUG & NDEBUG_NO_DATAOUT)
2032 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2033 "aborted\n", HOSTNO);
2034 sink = 1;
2035 do_abort(instance);
2036 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002037 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002038 return;
2039#endif
2040 case PHASE_DATAIN:
2041 /*
2042 * If there is no room left in the current buffer in the
2043 * scatter-gather list, move onto the next one.
2044 */
2045
2046 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2047 ++cmd->SCp.buffer;
2048 --cmd->SCp.buffers_residual;
2049 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002050 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002051 /* ++roman: Try to merge some scatter-buffers if
2052 * they are at contiguous physical addresses.
2053 */
2054 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002055 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002056 HOSTNO, cmd->SCp.this_residual,
2057 cmd->SCp.buffers_residual);
2058 }
2059
2060 /*
2061 * The preferred transfer method is going to be
2062 * PSEUDO-DMA for systems that are strictly PIO,
2063 * since we can let the hardware do the handshaking.
2064 *
2065 * For this to work, we need to know the transfersize
2066 * ahead of time, since the pseudo-DMA code will sit
2067 * in an unconditional loop.
2068 */
2069
2070 /* ++roman: I suggest, this should be
2071 * #if def(REAL_DMA)
2072 * instead of leaving REAL_DMA out.
2073 */
2074
2075#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002076 if (
2077#if !defined(CONFIG_SUN3)
2078 !cmd->device->borken &&
2079#endif
2080 (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002081 len = transfersize;
2082 cmd->SCp.phase = phase;
2083 if (NCR5380_transfer_dma(instance, &phase,
2084 &len, (unsigned char **)&cmd->SCp.ptr)) {
2085 /*
2086 * If the watchdog timer fires, all future
2087 * accesses to this device will use the
2088 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002089 scmd_printk(KERN_INFO, cmd,
2090 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002091 cmd->device->borken = 1;
2092 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2093 ICR_ASSERT_ATN);
2094 sink = 1;
2095 do_abort(instance);
2096 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002097 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002098 /* XXX - need to source or sink data here, as appropriate */
2099 } else {
2100#ifdef REAL_DMA
2101 /* ++roman: When using real DMA,
2102 * information_transfer() should return after
2103 * starting DMA since it has nothing more to
2104 * do.
2105 */
2106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002108 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002110 }
2111 } else
2112#endif /* defined(REAL_DMA) */
2113 NCR5380_transfer_pio(instance, &phase,
2114 (int *)&cmd->SCp.this_residual,
2115 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002116#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2117 /* if we had intended to dma that command clear it */
2118 if (sun3_dma_setup_done == cmd)
2119 sun3_dma_setup_done = NULL;
2120#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002121 break;
2122 case PHASE_MSGIN:
2123 len = 1;
2124 data = &tmp;
2125 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2126 NCR5380_transfer_pio(instance, &phase, &len, &data);
2127 cmd->SCp.Message = tmp;
2128
2129 switch (tmp) {
2130 /*
2131 * Linking lets us reduce the time required to get the
2132 * next command out to the device, hopefully this will
2133 * mean we don't waste another revolution due to the delays
2134 * required by ARBITRATION and another SELECTION.
2135 *
2136 * In the current implementation proposal, low level drivers
2137 * merely have to start the next command, pointed to by
2138 * next_link, done() is called as with unlinked commands.
2139 */
2140#ifdef LINKED
2141 case LINKED_CMD_COMPLETE:
2142 case LINKED_FLG_CMD_COMPLETE:
2143 /* Accept message by clearing ACK */
2144 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2145
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002146 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002147 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2148
2149 /* Enable reselect interrupts */
2150 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2151 /*
2152 * Sanity check : A linked command should only terminate
2153 * with one of these messages if there are more linked
2154 * commands available.
2155 */
2156
2157 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002158 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002159 "linked command complete, no next_link\n",
2160 HOSTNO, cmd->device->id, cmd->device->lun);
2161 sink = 1;
2162 do_abort(instance);
2163 return;
2164 }
2165
2166 initialize_SCp(cmd->next_link);
2167 /* The next command is still part of this process; copy it
2168 * and don't free it! */
2169 cmd->next_link->tag = cmd->tag;
2170 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002171 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002172 "done, calling scsi_done().\n",
2173 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002174 cmd->scsi_done(cmd);
2175 cmd = hostdata->connected;
2176 break;
2177#endif /* def LINKED */
2178 case ABORT:
2179 case COMMAND_COMPLETE:
2180 /* Accept message by clearing ACK */
2181 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002182 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002183 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002184
2185 local_irq_save(flags);
2186 hostdata->retain_dma_intr++;
2187 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002188#ifdef SUPPORT_TAGS
2189 cmd_free_tag(cmd);
2190 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2191 /* Turn a QUEUE FULL status into BUSY, I think the
2192 * mid level cannot handle QUEUE FULL :-( (The
2193 * command is retried after BUSY). Also update our
2194 * queue size to the number of currently issued
2195 * commands now.
2196 */
2197 /* ++Andreas: the mid level code knows about
2198 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002199 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002200 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002201 "QUEUE_FULL after %d commands\n",
2202 HOSTNO, cmd->device->id, cmd->device->lun,
2203 ta->nr_allocated);
2204 if (ta->queue_size > ta->nr_allocated)
2205 ta->nr_allocated = ta->queue_size;
2206 }
2207#else
2208 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2209#endif
2210 /* Enable reselect interrupts */
2211 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2212
2213 /*
2214 * I'm not sure what the correct thing to do here is :
2215 *
2216 * If the command that just executed is NOT a request
2217 * sense, the obvious thing to do is to set the result
2218 * code to the values of the stored parameters.
2219 *
2220 * If it was a REQUEST SENSE command, we need some way to
2221 * differentiate between the failure code of the original
2222 * and the failure code of the REQUEST sense - the obvious
2223 * case is success, where we fall through and leave the
2224 * result code unchanged.
2225 *
2226 * The non-obvious place is where the REQUEST SENSE failed
2227 */
2228
2229 if (cmd->cmnd[0] != REQUEST_SENSE)
2230 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2231 else if (status_byte(cmd->SCp.Status) != GOOD)
2232 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2233
Boaz Harrosh28424d32007-09-10 22:37:45 +03002234 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2235 hostdata->ses.cmd_len) {
2236 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2237 hostdata->ses.cmd_len = 0 ;
2238 }
2239
Roman Zippelc28bda22007-05-01 22:32:36 +02002240 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2241 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002242 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002243
Finn Thaind65e6342014-03-18 11:42:20 +11002244 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002245
Roman Zippelc28bda22007-05-01 22:32:36 +02002246 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002247 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002248 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002249 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002250 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002251 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002252 cmd->scsi_done(cmd);
2253 }
2254
Finn Thaine3c3da62014-11-12 16:12:15 +11002255 local_irq_restore(flags);
2256
Roman Zippelc28bda22007-05-01 22:32:36 +02002257 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2258 /*
2259 * Restore phase bits to 0 so an interrupted selection,
2260 * arbitration can resume.
2261 */
2262 NCR5380_write(TARGET_COMMAND_REG, 0);
2263
2264 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2265 barrier();
2266
Finn Thaine3c3da62014-11-12 16:12:15 +11002267 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002268 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002269 /* ++roman: For Falcon SCSI, release the lock on the
2270 * ST-DMA here if no other commands are waiting on the
2271 * disconnected queue.
2272 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002273 maybe_release_dma_irq(instance);
2274 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002275 return;
2276 case MESSAGE_REJECT:
2277 /* Accept message by clearing ACK */
2278 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2279 /* Enable reselect interrupts */
2280 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2281 switch (hostdata->last_message) {
2282 case HEAD_OF_QUEUE_TAG:
2283 case ORDERED_QUEUE_TAG:
2284 case SIMPLE_QUEUE_TAG:
2285 /* The target obviously doesn't support tagged
2286 * queuing, even though it announced this ability in
2287 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2288 * clear 'tagged_supported' and lock the LUN, since
2289 * the command is treated as untagged further on.
2290 */
2291 cmd->device->tagged_supported = 0;
2292 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2293 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002294 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002295 "QUEUE_TAG message; tagged queuing "
2296 "disabled\n",
2297 HOSTNO, cmd->device->id, cmd->device->lun);
2298 break;
2299 }
2300 break;
2301 case DISCONNECT:
2302 /* Accept message by clearing ACK */
2303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2304 local_irq_save(flags);
2305 cmd->device->disconnect = 1;
2306 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002307 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002308 hostdata->connected = NULL;
2309 hostdata->disconnected_queue = cmd;
2310 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002311 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002312 "moved from connected to the "
2313 "disconnected_queue\n", HOSTNO,
2314 cmd->device->id, cmd->device->lun);
2315 /*
2316 * Restore phase bits to 0 so an interrupted selection,
2317 * arbitration can resume.
2318 */
2319 NCR5380_write(TARGET_COMMAND_REG, 0);
2320
2321 /* Enable reselect interrupts */
2322 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2323 /* Wait for bus free to avoid nasty timeouts */
2324 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2325 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002326#ifdef SUN3_SCSI_VME
2327 dregs->csr |= CSR_DMA_ENABLE;
2328#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002329 return;
2330 /*
2331 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2332 * operation, in violation of the SCSI spec so we can safely
2333 * ignore SAVE/RESTORE pointers calls.
2334 *
2335 * Unfortunately, some disks violate the SCSI spec and
2336 * don't issue the required SAVE_POINTERS message before
2337 * disconnecting, and we have to break spec to remain
2338 * compatible.
2339 */
2340 case SAVE_POINTERS:
2341 case RESTORE_POINTERS:
2342 /* Accept message by clearing ACK */
2343 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2344 /* Enable reselect interrupts */
2345 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2346 break;
2347 case EXTENDED_MESSAGE:
2348 /*
2349 * Extended messages are sent in the following format :
2350 * Byte
2351 * 0 EXTENDED_MESSAGE == 1
2352 * 1 length (includes one byte for code, doesn't
2353 * include first two bytes)
2354 * 2 code
2355 * 3..length+1 arguments
2356 *
2357 * Start the extended message buffer with the EXTENDED_MESSAGE
2358 * byte, since spi_print_msg() wants the whole thing.
2359 */
2360 extended_msg[0] = EXTENDED_MESSAGE;
2361 /* Accept first byte by clearing ACK */
2362 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2363
Finn Thaind65e6342014-03-18 11:42:20 +11002364 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002365
2366 len = 2;
2367 data = extended_msg + 1;
2368 phase = PHASE_MSGIN;
2369 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002370 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002371 (int)extended_msg[1], (int)extended_msg[2]);
2372
2373 if (!len && extended_msg[1] <=
2374 (sizeof(extended_msg) - 1)) {
2375 /* Accept third byte by clearing ACK */
2376 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2377 len = extended_msg[1] - 1;
2378 data = extended_msg + 3;
2379 phase = PHASE_MSGIN;
2380
2381 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002382 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002383 HOSTNO, len);
2384
2385 switch (extended_msg[2]) {
2386 case EXTENDED_SDTR:
2387 case EXTENDED_WDTR:
2388 case EXTENDED_MODIFY_DATA_POINTER:
2389 case EXTENDED_EXTENDED_IDENTIFY:
2390 tmp = 0;
2391 }
2392 } else if (len) {
2393 printk(KERN_NOTICE "scsi%d: error receiving "
2394 "extended message\n", HOSTNO);
2395 tmp = 0;
2396 } else {
2397 printk(KERN_NOTICE "scsi%d: extended message "
2398 "code %02x length %d is too long\n",
2399 HOSTNO, extended_msg[2], extended_msg[1]);
2400 tmp = 0;
2401 }
2402 /* Fall through to reject message */
2403
2404 /*
2405 * If we get something weird that we aren't expecting,
2406 * reject it.
2407 */
2408 default:
2409 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002410 printk(KERN_INFO "scsi%d: rejecting message ",
2411 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002412 spi_print_msg(extended_msg);
2413 printk("\n");
2414 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002415 scmd_printk(KERN_INFO, cmd,
2416 "rejecting unknown message %02x\n",
2417 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002418 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002419 scmd_printk(KERN_INFO, cmd,
2420 "rejecting unknown extended message code %02x, length %d\n",
2421 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002422
2423 msgout = MESSAGE_REJECT;
2424 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2425 break;
2426 } /* switch (tmp) */
2427 break;
2428 case PHASE_MSGOUT:
2429 len = 1;
2430 data = &msgout;
2431 hostdata->last_message = msgout;
2432 NCR5380_transfer_pio(instance, &phase, &len, &data);
2433 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002434 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002435#ifdef SUPPORT_TAGS
2436 cmd_free_tag(cmd);
2437#else
2438 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2439#endif
2440 hostdata->connected = NULL;
2441 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002442 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002443 maybe_release_dma_irq(instance);
2444 local_irq_restore(flags);
2445 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002446 return;
2447 }
2448 msgout = NOP;
2449 break;
2450 case PHASE_CMDOUT:
2451 len = cmd->cmd_len;
2452 data = cmd->cmnd;
2453 /*
2454 * XXX for performance reasons, on machines with a
2455 * PSEUDO-DMA architecture we should probably
2456 * use the dma transfer function.
2457 */
2458 NCR5380_transfer_pio(instance, &phase, &len, &data);
2459 break;
2460 case PHASE_STATIN:
2461 len = 1;
2462 data = &tmp;
2463 NCR5380_transfer_pio(instance, &phase, &len, &data);
2464 cmd->SCp.Status = tmp;
2465 break;
2466 default:
2467 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002468 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002469 } /* switch(phase) */
2470 } /* if (tmp * SR_REQ) */
2471 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474/*
2475 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2476 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002477 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002478 * 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 -07002479 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002480 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 * Inputs : instance - this instance of the NCR5380.
2482 *
2483 */
2484
2485
Finn Thaine3f463b2014-11-12 16:12:16 +11002486/* it might eventually prove necessary to do a dma setup on
2487 reselection, but it doesn't seem to be needed now -- sam */
2488
Roman Zippelc28bda22007-05-01 22:32:36 +02002489static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Roman Zippelc28bda22007-05-01 22:32:36 +02002491 SETUP_HOSTDATA(instance);
2492 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002493 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002495 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002497 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002498 int __maybe_unused len;
2499 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002500 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Roman Zippelc28bda22007-05-01 22:32:36 +02002502 /*
2503 * Disable arbitration, etc. since the host adapter obviously
2504 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Roman Zippelc28bda22007-05-01 22:32:36 +02002507 NCR5380_write(MODE_REG, MR_BASE);
2508 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Roman Zippelc28bda22007-05-01 22:32:36 +02002510 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2511
Finn Thaind65e6342014-03-18 11:42:20 +11002512 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002513
2514 /*
2515 * At this point, we have detected that our SCSI ID is on the bus,
2516 * SEL is true and BSY was false for at least one bus settle delay
2517 * (400 ns).
2518 *
2519 * We must assert BSY ourselves, until the target drops the SEL
2520 * signal.
2521 */
2522
2523 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2524
2525 while (NCR5380_read(STATUS_REG) & SR_SEL)
2526 ;
2527 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2528
2529 /*
2530 * Wait for target to go into MSGIN.
2531 */
2532
2533 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2534 ;
2535
Finn Thaine3f463b2014-11-12 16:12:16 +11002536#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2537 /* acknowledge toggle to MSGIN */
2538 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2539
2540 /* peek at the byte without really hitting the bus */
2541 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2542#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002543 len = 1;
2544 data = msg;
2545 phase = PHASE_MSGIN;
2546 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002547#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002548
2549 if (!(msg[0] & 0x80)) {
2550 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2551 spi_print_msg(msg);
2552 do_abort(instance);
2553 return;
2554 }
2555 lun = (msg[0] & 0x07);
2556
Finn Thaine3f463b2014-11-12 16:12:16 +11002557#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002558 /* If the phase is still MSGIN, the target wants to send some more
2559 * messages. In case it supports tagged queuing, this is probably a
2560 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2561 */
2562 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002563 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002564 /* Accept previous IDENTIFY message by clearing ACK */
2565 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2566 len = 2;
2567 data = msg + 1;
2568 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2569 msg[1] == SIMPLE_QUEUE_TAG)
2570 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002571 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002572 "reselection\n", HOSTNO, target_mask, lun, tag);
2573 }
2574#endif
2575
2576 /*
2577 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2578 * just reestablished, and remove it from the disconnected queue.
2579 */
2580
Finn Thain710ddd02014-11-12 16:12:02 +11002581 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002582 tmp; prev = tmp, tmp = NEXT(tmp)) {
2583 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2584#ifdef SUPPORT_TAGS
2585 && (tag == tmp->tag)
2586#endif
2587 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002588 if (prev) {
2589 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002590 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002591 } else {
2592 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2593 hostdata->disconnected_queue = NEXT(tmp);
2594 }
Roman Zippel3130d902007-05-01 22:32:37 +02002595 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002596 break;
2597 }
2598 }
2599
2600 if (!tmp) {
2601 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2602#ifdef SUPPORT_TAGS
2603 "tag %d "
2604#endif
2605 "not in disconnected_queue.\n",
2606 HOSTNO, target_mask, lun
2607#ifdef SUPPORT_TAGS
2608 , tag
2609#endif
2610 );
2611 /*
2612 * Since we have an established nexus that we can't do anything
2613 * with, we must abort it.
2614 */
2615 do_abort(instance);
2616 return;
2617 }
2618
Finn Thaine3f463b2014-11-12 16:12:16 +11002619#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2620 /* engage dma setup for the command we just saw */
2621 {
2622 void *d;
2623 unsigned long count;
2624
2625 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2626 count = tmp->SCp.buffer->length;
2627 d = sg_virt(tmp->SCp.buffer);
2628 } else {
2629 count = tmp->SCp.this_residual;
2630 d = tmp->SCp.ptr;
2631 }
2632 /* setup this command for dma if not already */
2633 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2634 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2635 sun3_dma_setup_done = tmp;
2636 }
2637 }
2638
2639 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2640#endif
2641
Roman Zippelc28bda22007-05-01 22:32:36 +02002642 /* Accept message by clearing ACK */
2643 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2644
Finn Thaine3f463b2014-11-12 16:12:16 +11002645#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2646 /* If the phase is still MSGIN, the target wants to send some more
2647 * messages. In case it supports tagged queuing, this is probably a
2648 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2649 */
2650 tag = TAG_NONE;
2651 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2652 /* Accept previous IDENTIFY message by clearing ACK */
2653 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2654 len = 2;
2655 data = msg + 1;
2656 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2657 msg[1] == SIMPLE_QUEUE_TAG)
2658 tag = msg[2];
2659 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2660 HOSTNO, target_mask, lun, tag);
2661 }
2662#endif
2663
Roman Zippelc28bda22007-05-01 22:32:36 +02002664 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002665 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002666 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669
2670/*
Finn Thain710ddd02014-11-12 16:12:02 +11002671 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 *
2673 * Purpose : abort a command
2674 *
Finn Thain710ddd02014-11-12 16:12:02 +11002675 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002676 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 * used.
2678 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002679 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002681 * XXX - there is no way to abort the command that is currently
2682 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002684 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 */
2686
2687static
Finn Thain710ddd02014-11-12 16:12:02 +11002688int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
Roman Zippelc28bda22007-05-01 22:32:36 +02002690 struct Scsi_Host *instance = cmd->device->host;
2691 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002692 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002693 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002695 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Roman Zippelc28bda22007-05-01 22:32:36 +02002697 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Roman Zippelc28bda22007-05-01 22:32:36 +02002699 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Finn Thaind65e6342014-03-18 11:42:20 +11002701 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002702 NCR5380_read(BUS_AND_STATUS_REG),
2703 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002706 /*
2707 * Case 1 : If the command is the currently executing command,
2708 * we'll set the aborted flag and return control so that
2709 * information transfer routine can exit cleanly.
2710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Roman Zippelc28bda22007-05-01 22:32:36 +02002712 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Finn Thaind65e6342014-03-18 11:42:20 +11002714 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002715 /*
2716 * We should perform BSY checking, and make sure we haven't slipped
2717 * into BUS FREE.
2718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Roman Zippelc28bda22007-05-01 22:32:36 +02002720 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2721 /*
2722 * Since we can't change phases until we've completed the current
2723 * handshake, we have to source or sink a byte of data if the current
2724 * phase is not MSGOUT.
2725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Roman Zippelc28bda22007-05-01 22:32:36 +02002727 /*
2728 * Return control to the executing NCR drive so we can clear the
2729 * aborted flag and get back into our main loop.
2730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Roman Zippelc28bda22007-05-01 22:32:36 +02002732 if (do_abort(instance) == 0) {
2733 hostdata->aborted = 1;
2734 hostdata->connected = NULL;
2735 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002737 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002739 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002741 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002742 local_irq_restore(flags);
2743 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002744 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002745 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002746 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002747 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002748 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002752
2753 /*
2754 * Case 2 : If the command hasn't been issued yet, we simply remove it
2755 * from the issue queue.
2756 */
Finn Thain710ddd02014-11-12 16:12:02 +11002757 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2758 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002759 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2760 if (cmd == tmp) {
2761 REMOVE(5, *prev, tmp, NEXT(tmp));
2762 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002763 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002764 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002765 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002766 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002767 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002768 HOSTNO);
2769 /* Tagged queuing note: no tag to free here, hasn't been assigned
2770 * yet... */
2771 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002772 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774 }
2775
Roman Zippelc28bda22007-05-01 22:32:36 +02002776 /*
2777 * Case 3 : If any commands are connected, we're going to fail the abort
2778 * and let the high level SCSI driver retry at a later time or
2779 * issue a reset.
2780 *
2781 * Timeouts, and therefore aborted commands, will be highly unlikely
2782 * and handling them cleanly in this situation would make the common
2783 * case of noresets less efficient, and would pollute our code. So,
2784 * we fail.
2785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Roman Zippelc28bda22007-05-01 22:32:36 +02002787 if (hostdata->connected) {
2788 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002789 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002790 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Roman Zippelc28bda22007-05-01 22:32:36 +02002793 /*
2794 * Case 4: If the command is currently disconnected from the bus, and
2795 * there are no connected commands, we reconnect the I_T_L or
2796 * I_T_L_Q nexus associated with it, go into message out, and send
2797 * an abort message.
2798 *
2799 * This case is especially ugly. In order to reestablish the nexus, we
2800 * need to call NCR5380_select(). The easiest way to implement this
2801 * function was to abort if the bus was busy, and let the interrupt
2802 * handler triggered on the SEL for reselect take care of lost arbitrations
2803 * where necessary, meaning interrupts need to be enabled.
2804 *
2805 * When interrupts are enabled, the queues may change - so we
2806 * can't remove it from the disconnected queue before selecting it
2807 * because that could cause a failure in hashing the nexus if that
2808 * device reselected.
2809 *
2810 * Since the queues may change, we can't use the pointers from when we
2811 * first locate it.
2812 *
2813 * So, we must first locate the command, and if NCR5380_select()
2814 * succeeds, then issue the abort, relocate the command and remove
2815 * it from the disconnected queue.
2816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Finn Thain710ddd02014-11-12 16:12:02 +11002818 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002819 tmp = NEXT(tmp)) {
2820 if (cmd == tmp) {
2821 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002822 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002823
Finn Thain76f13b92014-11-12 16:11:53 +11002824 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002825 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002826
Finn Thaind65e6342014-03-18 11:42:20 +11002827 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002828
2829 do_abort(instance);
2830
2831 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002832 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2833 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002834 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2835 if (cmd == tmp) {
2836 REMOVE(5, *prev, tmp, NEXT(tmp));
2837 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002838 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002839 tmp->result = DID_ABORT << 16;
2840 /* We must unlock the tag/LUN immediately here, since the
2841 * target goes to BUS FREE and doesn't send us another
2842 * message (COMMAND_COMPLETE or the like)
2843 */
2844#ifdef SUPPORT_TAGS
2845 cmd_free_tag(tmp);
2846#else
2847 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2848#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002849 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002850 local_irq_restore(flags);
2851 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002852 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002853 }
2854 }
2855 }
2856 }
2857
Finn Thaine3c3da62014-11-12 16:12:15 +11002858 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2859 * possible at all) At least, we should check if the lock could be
2860 * released after the abort, in case it is kept due to some bug.
2861 */
2862 maybe_release_dma_irq(instance);
2863 local_irq_restore(flags);
2864
Roman Zippelc28bda22007-05-01 22:32:36 +02002865 /*
2866 * Case 5 : If we reached this point, the command was not found in any of
2867 * the queues.
2868 *
2869 * We probably reached this point because of an unlikely race condition
2870 * between the command completing successfully and the abortion code,
2871 * so we won't panic, but we will notify the user in case something really
2872 * broke.
2873 */
2874
Joe Perchesad361c92009-07-06 13:05:40 -07002875 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002876
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002877 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880
Roman Zippelc28bda22007-05-01 22:32:36 +02002881/*
Finn Thain710ddd02014-11-12 16:12:02 +11002882 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002883 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * Purpose : reset the SCSI bus.
2885 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002886 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Finn Thain710ddd02014-11-12 16:12:02 +11002890static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Finn Thaine3c3da62014-11-12 16:12:15 +11002892 struct Scsi_Host *instance = cmd->device->host;
2893 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002894 int i;
2895 unsigned long flags;
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002896#if defined(RESET_RUN_DONE)
Finn Thain710ddd02014-11-12 16:12:02 +11002897 struct scsi_cmnd *connected, *disconnected_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898#endif
2899
Finn Thaine3c3da62014-11-12 16:12:15 +11002900 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Roman Zippelc28bda22007-05-01 22:32:36 +02002902 /* get in phase */
2903 NCR5380_write(TARGET_COMMAND_REG,
2904 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2905 /* assert RST */
2906 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2907 udelay(40);
2908 /* reset NCR registers */
2909 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2910 NCR5380_write(MODE_REG, MR_BASE);
2911 NCR5380_write(TARGET_COMMAND_REG, 0);
2912 NCR5380_write(SELECT_ENABLE_REG, 0);
2913 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2914 * through anymore ... */
2915 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002917 /* MSch 20140115 - looking at the generic NCR5380 driver, all of this
2918 * should go.
2919 * Catch-22: if we don't clear all queues, the SCSI driver lock will
2920 * not be reset by atari_scsi_reset()!
2921 */
2922
2923#if defined(RESET_RUN_DONE)
2924 /* XXX Should now be done by midlevel code, but it's broken XXX */
Roman Zippelc28bda22007-05-01 22:32:36 +02002925 /* XXX see below XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Roman Zippelc28bda22007-05-01 22:32:36 +02002927 /* MSch: old-style reset: actually abort all command processing here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Roman Zippelc28bda22007-05-01 22:32:36 +02002929 /* After the reset, there are no more connected or disconnected commands
2930 * and no busy units; to avoid problems with re-inserting the commands
2931 * into the issue_queue (via scsi_done()), the aborted commands are
2932 * remembered in local variables first.
2933 */
2934 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002935 connected = (struct scsi_cmnd *)hostdata->connected;
Roman Zippelc28bda22007-05-01 22:32:36 +02002936 hostdata->connected = NULL;
Finn Thain710ddd02014-11-12 16:12:02 +11002937 disconnected_queue = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002938 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002940 free_all_tags();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002942 for (i = 0; i < 8; ++i)
2943 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002945 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002947 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Roman Zippelc28bda22007-05-01 22:32:36 +02002949 /* In order to tell the mid-level code which commands were aborted,
2950 * set the command status to DID_RESET and call scsi_done() !!!
2951 * This ultimately aborts processing of these commands in the mid-level.
2952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Roman Zippelc28bda22007-05-01 22:32:36 +02002954 if ((cmd = connected)) {
Finn Thaind65e6342014-03-18 11:42:20 +11002955 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002956 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2957 cmd->scsi_done(cmd);
2958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Roman Zippelc28bda22007-05-01 22:32:36 +02002960 for (i = 0; (cmd = disconnected_queue); ++i) {
2961 disconnected_queue = NEXT(cmd);
Roman Zippel3130d902007-05-01 22:32:37 +02002962 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002963 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2964 cmd->scsi_done(cmd);
2965 }
2966 if (i > 0)
Finn Thaind65e6342014-03-18 11:42:20 +11002967 dprintk(NDEBUG_ABORT, "scsi: reset aborted %d disconnected command(s)\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Roman Zippelc28bda22007-05-01 22:32:36 +02002969 /* The Falcon lock should be released after a reset...
2970 */
2971 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2972 * unlocking and enabling dma interrupt.
2973 */
2974/* falcon_release_lock_if_possible( hostdata );*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Roman Zippelc28bda22007-05-01 22:32:36 +02002976 /* since all commands have been explicitly terminated, we need to tell
2977 * the midlevel code that the reset was SUCCESSFUL, and there is no
2978 * need to 'wake up' the commands by a request_sense
2979 */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002980 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981#else /* 1 */
2982
Roman Zippelc28bda22007-05-01 22:32:36 +02002983 /* MSch: new-style reset handling: let the mid-level do what it can */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Roman Zippelc28bda22007-05-01 22:32:36 +02002985 /* ++guenther: MID-LEVEL IS STILL BROKEN.
2986 * Mid-level is supposed to requeue all commands that were active on the
2987 * various low-level queues. In fact it does this, but that's not enough
2988 * because all these commands are subject to timeout. And if a timeout
2989 * happens for any removed command, *_abort() is called but all queues
2990 * are now empty. Abort then gives up the falcon lock, which is fatal,
2991 * since the mid-level will queue more commands and must have the lock
2992 * (it's all happening inside timer interrupt handler!!).
2993 * Even worse, abort will return NOT_RUNNING for all those commands not
2994 * on any queue, so they won't be retried ...
2995 *
2996 * Conclusion: either scsi.c disables timeout for all resetted commands
2997 * immediately, or we lose! As of linux-2.0.20 it doesn't.
2998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Roman Zippelc28bda22007-05-01 22:32:36 +02003000 /* After the reset, there are no more connected or disconnected commands
3001 * and no busy units; so clear the low-level status here to avoid
3002 * conflicts when the mid-level code tries to wake up the affected
3003 * commands!
3004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Roman Zippelc28bda22007-05-01 22:32:36 +02003006 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003007 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003008 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11003009 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003010 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003011 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Roman Zippelc28bda22007-05-01 22:32:36 +02003013 local_irq_save(flags);
3014 hostdata->issue_queue = NULL;
3015 hostdata->connected = NULL;
3016 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11003018 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02003020 for (i = 0; i < 8; ++i)
3021 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02003023 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11003025
3026 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02003027 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Roman Zippelc28bda22007-05-01 22:32:36 +02003029 /* we did no complete reset of all commands, so a wakeup is required */
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003030 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031#endif /* 1 */
3032}