blob: a15b655aa2b018013448a6a2a8472193f006ca50 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/* Macros ease life... :-) */
225#define SETUP_HOSTDATA(in) \
226 struct NCR5380_hostdata *hostdata = \
227 (struct NCR5380_hostdata *)(in)->hostdata
228#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
229
Finn Thain710ddd02014-11-12 16:12:02 +1100230#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200231#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100232#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234#define HOSTNO instance->host_no
235#define H_NO(cmd) (cmd)->device->host->host_no
236
Finn Thain636b1ec2016-01-03 16:05:10 +1100237static int do_abort(struct Scsi_Host *);
238static void do_reset(struct Scsi_Host *);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#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
Finn Thain636b1ec2016-01-03 16:05:10 +1100481/**
482 * NCR5380_poll_politely - wait for NCR5380 status bits
483 * @instance: controller to poll
484 * @reg: 5380 register to poll
485 * @bit: Bitmask to check
486 * @val: Value required to exit
487 *
488 * Polls the NCR5380 in a reasonably efficient manner waiting for
489 * an event to occur, after a short quick poll we begin giving the
490 * CPU back in non IRQ contexts
491 *
492 * Returns the value of the register or a negative error code.
493 */
494
495static int NCR5380_poll_politely(struct Scsi_Host *instance,
496 int reg, int bit, int val, int t)
497{
498 int n = 500;
499 unsigned long end = jiffies + t;
500 int r;
501
502 while (n-- > 0) {
503 r = NCR5380_read(reg);
504 if ((r & bit) == val)
505 return 0;
506 cpu_relax();
507 }
508
509 /* t time yet ? */
510 while (time_before(jiffies, end)) {
511 r = NCR5380_read(reg);
512 if ((r & bit) == val)
513 return 0;
514 if (!in_interrupt())
515 cond_resched();
516 else
517 cpu_relax();
518 }
519 return -ETIMEDOUT;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#include <linux/delay.h>
523
524#if NDEBUG
525static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200526 unsigned char mask;
527 const char *name;
528} signals[] = {
529 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
530 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
531 { SR_SEL, "SEL" }, {0, NULL}
532}, basrs[] = {
533 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
534}, icrs[] = {
535 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
536 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
537 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
538 {0, NULL}
539}, mrs[] = {
540 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
541 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
542 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
543 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
544 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
545 {0, NULL}
546};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Finn Thainff50f9e2014-11-12 16:12:18 +1100548/**
549 * NCR5380_print - print scsi bus signals
550 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100552 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
554
Roman Zippelc28bda22007-05-01 22:32:36 +0200555static void NCR5380_print(struct Scsi_Host *instance)
556{
557 unsigned char status, data, basr, mr, icr, i;
558 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Roman Zippelc28bda22007-05-01 22:32:36 +0200560 local_irq_save(flags);
561 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
562 status = NCR5380_read(STATUS_REG);
563 mr = NCR5380_read(MODE_REG);
564 icr = NCR5380_read(INITIATOR_COMMAND_REG);
565 basr = NCR5380_read(BUS_AND_STATUS_REG);
566 local_irq_restore(flags);
567 printk("STATUS_REG: %02x ", status);
568 for (i = 0; signals[i].mask; ++i)
569 if (status & signals[i].mask)
570 printk(",%s", signals[i].name);
571 printk("\nBASR: %02x ", basr);
572 for (i = 0; basrs[i].mask; ++i)
573 if (basr & basrs[i].mask)
574 printk(",%s", basrs[i].name);
575 printk("\nICR: %02x ", icr);
576 for (i = 0; icrs[i].mask; ++i)
577 if (icr & icrs[i].mask)
578 printk(",%s", icrs[i].name);
579 printk("\nMODE: %02x ", mr);
580 for (i = 0; mrs[i].mask; ++i)
581 if (mr & mrs[i].mask)
582 printk(",%s", mrs[i].name);
583 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200587 unsigned char value;
588 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200590 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
591 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
592 {PHASE_UNKNOWN, "UNKNOWN"}
593};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Finn Thainff50f9e2014-11-12 16:12:18 +1100595/**
596 * NCR5380_print_phase - show SCSI phase
597 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100599 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100601 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
603
604static void NCR5380_print_phase(struct Scsi_Host *instance)
605{
Roman Zippelc28bda22007-05-01 22:32:36 +0200606 unsigned char status;
607 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Roman Zippelc28bda22007-05-01 22:32:36 +0200609 status = NCR5380_read(STATUS_REG);
610 if (!(status & SR_REQ))
611 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
612 else {
613 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
614 (phases[i].value != (status & PHASE_MASK)); ++i)
615 ;
616 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
621
622/*
623 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200624 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * If we're not in an interrupt, we can call our main directly, it cannot be
626 * already running. Else, we queue it on a task queue, if not 'main_running'
627 * tells us that a lower level is already executing it. This way,
628 * 'main_running' needs not be protected in a special way.
629 *
630 * queue_main() is a utility function for putting our main onto the task
631 * queue, if main_running is false. It should be called only from a
632 * interrupt or bottom half.
633 */
634
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900635#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#include <linux/workqueue.h>
637#include <linux/interrupt.h>
638
Finn Thaina53a21e2014-11-12 16:12:21 +1100639static inline void queue_main(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Finn Thaina53a21e2014-11-12 16:12:21 +1100641 if (!hostdata->main_running) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200642 /* If in interrupt and NCR5380_main() not already running,
643 queue it on the 'immediate' task queue, to be processed
644 immediately after the current interrupt processing has
645 finished. */
Finn Thaina53a21e2014-11-12 16:12:21 +1100646 schedule_work(&hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200647 }
648 /* else: nothing to do: the running NCR5380_main() will pick up
649 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Finn Thain8c325132014-11-12 16:11:58 +1100652/**
653 * NCR58380_info - report driver and host information
654 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *
Finn Thain8c325132014-11-12 16:11:58 +1100656 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 *
Finn Thain8c325132014-11-12 16:11:58 +1100658 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
660
Finn Thain8c325132014-11-12 16:11:58 +1100661static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Finn Thain8c325132014-11-12 16:11:58 +1100663 struct NCR5380_hostdata *hostdata = shost_priv(instance);
664
665 return hostdata->info;
666}
667
668static void prepare_info(struct Scsi_Host *instance)
669{
670 struct NCR5380_hostdata *hostdata = shost_priv(instance);
671
672 snprintf(hostdata->info, sizeof(hostdata->info),
673 "%s, io_port 0x%lx, n_io_port %d, "
674 "base 0x%lx, irq %d, "
675 "can_queue %d, cmd_per_lun %d, "
676 "sg_tablesize %d, this_id %d, "
Finn Thainca513fc2014-11-12 16:12:19 +1100677 "flags { %s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100678 "options { %s} ",
679 instance->hostt->name, instance->io_port, instance->n_io_port,
680 instance->base, instance->irq,
681 instance->can_queue, instance->cmd_per_lun,
682 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100683 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100684#ifdef DIFFERENTIAL
685 "DIFFERENTIAL "
686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100688 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#endif
690#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100691 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692#endif
693#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100694 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#endif
Finn Thain8c325132014-11-12 16:11:58 +1100696 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Finn Thainff50f9e2014-11-12 16:12:18 +1100699/**
700 * NCR5380_print_status - dump controller info
701 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100703 * Print commands in the various queues, called from NCR5380_abort
704 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
706
Finn Thain710ddd02014-11-12 16:12:02 +1100707static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400708{
709 int i, s;
710 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200711 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400712 H_NO(cmd), cmd->device->id, cmd->device->lun);
713 printk(KERN_CONT " command = ");
714 command = cmd->cmnd;
715 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
716 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
717 printk(KERN_CONT " %02x", command[i]);
718 printk("\n");
719}
720
Roman Zippelc28bda22007-05-01 22:32:36 +0200721static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Al Virod89537e2013-03-31 13:24:44 -0400723 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100724 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400725 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Finn Thain8ad3a592014-03-18 11:42:19 +1100727 NCR5380_dprint(NDEBUG_ANY, instance);
728 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Roman Zippelc28bda22007-05-01 22:32:36 +0200730 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Roman Zippelc28bda22007-05-01 22:32:36 +0200732 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400733 printk("NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100734 hostdata->main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200735 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400736 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200737 else
Finn Thain710ddd02014-11-12 16:12:02 +1100738 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400739 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100740 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400741 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Al Virod89537e2013-03-31 13:24:44 -0400743 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100744 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400745 ptr = NEXT(ptr))
746 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200747
748 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400749 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Finn Thain710ddd02014-11-12 16:12:02 +1100752static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Roman Zippelc28bda22007-05-01 22:32:36 +0200754 int i, s;
755 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200756 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200757 H_NO(cmd), cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100758 seq_puts(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200759 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400760 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200761 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400762 seq_printf(m, " %02x", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100763 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Finn Thain4d3d2a52014-11-12 16:11:52 +1100766static int __maybe_unused NCR5380_show_info(struct seq_file *m,
767 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400768{
769 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100770 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400771 unsigned long flags;
772
773 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
774
Al Virod89537e2013-03-31 13:24:44 -0400775 local_irq_save(flags);
776 seq_printf(m, "NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100777 hostdata->main_running ? "" : "n't");
Al Virod89537e2013-03-31 13:24:44 -0400778 if (!hostdata->connected)
779 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
780 else
Finn Thain710ddd02014-11-12 16:12:02 +1100781 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400782 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100783 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400784 show_Scsi_Cmnd(ptr, m);
785
786 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100787 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400788 ptr = NEXT(ptr))
789 show_Scsi_Cmnd(ptr, m);
790
791 local_irq_restore(flags);
792 return 0;
793}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Finn Thainff50f9e2014-11-12 16:12:18 +1100795/**
796 * NCR5380_init - initialise an NCR5380
797 * @instance: adapter to configure
798 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100800 * Initializes *instance and corresponding 5380 chip,
801 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
803 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100804 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200805 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100806 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
808
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100809static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Roman Zippelc28bda22007-05-01 22:32:36 +0200811 int i;
812 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Finn Thaina53a21e2014-11-12 16:12:21 +1100814 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200815 hostdata->aborted = 0;
816 hostdata->id_mask = 1 << instance->this_id;
817 hostdata->id_higher_mask = 0;
818 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
819 if (i > hostdata->id_mask)
820 hostdata->id_higher_mask |= i;
821 for (i = 0; i < 8; ++i)
822 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100824 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#endif
826#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200827 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200829 hostdata->targets_present = 0;
830 hostdata->connected = NULL;
831 hostdata->issue_queue = NULL;
832 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100833 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Finn Thaina53a21e2014-11-12 16:12:21 +1100835 INIT_WORK(&hostdata->main_task, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Finn Thain8c325132014-11-12 16:11:58 +1100837 prepare_info(instance);
838
Roman Zippelc28bda22007-05-01 22:32:36 +0200839 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
840 NCR5380_write(MODE_REG, MR_BASE);
841 NCR5380_write(TARGET_COMMAND_REG, 0);
842 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Roman Zippelc28bda22007-05-01 22:32:36 +0200844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Finn Thainff50f9e2014-11-12 16:12:18 +1100847/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100848 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
849 * @instance: adapter to check
850 *
851 * If the system crashed, it may have crashed with a connected target and
852 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
853 * currently established nexus, which we know nothing about. Failing that
854 * do a bus reset.
855 *
856 * Note that a bus reset will cause the chip to assert IRQ.
857 *
858 * Returns 0 if successful, otherwise -ENXIO.
859 */
860
861static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
862{
863 int pass;
864
865 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
866 switch (pass) {
867 case 1:
868 case 3:
869 case 5:
870 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
871 NCR5380_poll_politely(instance,
872 STATUS_REG, SR_BSY, 0, 5 * HZ);
873 break;
874 case 2:
875 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
876 do_abort(instance);
877 break;
878 case 4:
879 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
880 do_reset(instance);
881 break;
882 case 6:
883 shost_printk(KERN_ERR, instance, "bus locked solid\n");
884 return -ENXIO;
885 }
886 }
887 return 0;
888}
889
890/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100891 * NCR5380_exit - remove an NCR5380
892 * @instance: adapter to remove
893 *
894 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
895 */
896
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200897static void NCR5380_exit(struct Scsi_Host *instance)
898{
Finn Thaina53a21e2014-11-12 16:12:21 +1100899 struct NCR5380_hostdata *hostdata = shost_priv(instance);
900
901 cancel_work_sync(&hostdata->main_task);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200902}
903
Finn Thainff50f9e2014-11-12 16:12:18 +1100904/**
905 * NCR5380_queue_command - queue a command
906 * @instance: the relevant SCSI adapter
907 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100909 * cmd is added to the per instance issue_queue, with minor
910 * twiddling done to the host specific fields of cmd. If the
911 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
913
Finn Thain16b29e72014-11-12 16:12:08 +1100914static int NCR5380_queue_command(struct Scsi_Host *instance,
915 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Finn Thain16b29e72014-11-12 16:12:08 +1100917 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100918 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200919 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200922 switch (cmd->cmnd[0]) {
923 case WRITE_6:
924 case WRITE_10:
925 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
926 H_NO(cmd));
927 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100928 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200929 return 0;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
932
Roman Zippelc28bda22007-05-01 22:32:36 +0200933 /*
934 * We use the host_scribble field as a pointer to the next command
935 * in a queue
936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Roman Zippel3130d902007-05-01 22:32:37 +0200938 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200939 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Roman Zippelc28bda22007-05-01 22:32:36 +0200941 /*
942 * Insert the cmd into the issue queue. Note that REQUEST SENSE
943 * commands are added to the head of the queue since any command will
944 * clear the contingent allegiance condition that exists and the
945 * sense data is only guaranteed to be valid while the condition exists.
946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Roman Zippelc28bda22007-05-01 22:32:36 +0200948 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
949 * Otherwise a running NCR5380_main may steal the lock.
950 * Lock before actually inserting due to fairness reasons explained in
951 * atari_scsi.c. If we insert first, then it's impossible for this driver
952 * to release the lock.
953 * Stop timer for this command while waiting for the lock, or timeouts
954 * may happen (and they really do), and it's no good if the command doesn't
955 * appear in any of the queues.
956 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
957 * because also a timer int can trigger an abort or reset, which would
958 * alter queues and touch the lock.
959 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100960 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100961 return SCSI_MLQUEUE_HOST_BUSY;
962
963 local_irq_save(flags);
964
Finn Thainff50f9e2014-11-12 16:12:18 +1100965 /*
966 * Insert the cmd into the issue queue. Note that REQUEST SENSE
967 * commands are added to the head of the queue since any command will
968 * clear the contingent allegiance condition that exists and the
969 * sense data is only guaranteed to be valid while the condition exists.
970 */
971
Roman Zippelc28bda22007-05-01 22:32:36 +0200972 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
973 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200974 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200975 hostdata->issue_queue = cmd;
976 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100977 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200978 NEXT(tmp); tmp = NEXT(tmp))
979 ;
980 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200981 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200982 }
983 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Finn Thaind65e6342014-03-18 11:42:20 +1100985 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200986 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Roman Zippelc28bda22007-05-01 22:32:36 +0200988 /* If queue_command() is called from an interrupt (real one or bottom
989 * half), we let queue_main() do the job of taking care about main. If it
990 * is already running, this is a no-op, else main will be queued.
991 *
992 * If we're not in an interrupt, we can call NCR5380_main()
993 * unconditionally, because it cannot be already running.
994 */
Finn Thain16b29e72014-11-12 16:12:08 +1100995 if (in_interrupt() || irqs_disabled())
Finn Thaina53a21e2014-11-12 16:12:21 +1100996 queue_main(hostdata);
Roman Zippelc28bda22007-05-01 22:32:36 +0200997 else
Finn Thaina53a21e2014-11-12 16:12:21 +1100998 NCR5380_main(&hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Finn Thaine3c3da62014-11-12 16:12:15 +11001002static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
1003{
1004 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1005
1006 /* Caller does the locking needed to set & test these data atomically */
1007 if (!hostdata->disconnected_queue &&
1008 !hostdata->issue_queue &&
1009 !hostdata->connected &&
1010 !hostdata->retain_dma_intr)
1011 NCR5380_release_dma_irq(instance);
1012}
1013
Finn Thainff50f9e2014-11-12 16:12:18 +11001014/**
1015 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001017 * NCR5380_main is a coroutine that runs as long as more work can
1018 * be done on the NCR5380 host adapters in a system. Both
1019 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1020 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +02001021 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001022 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +02001023 */
1024
Geert Uytterhoevenb312b382007-05-01 22:32:48 +02001025static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Finn Thaina53a21e2014-11-12 16:12:21 +11001027 struct NCR5380_hostdata *hostdata =
1028 container_of(work, struct NCR5380_hostdata, main_task);
1029 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +11001030 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 int done;
1032 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Roman Zippelc28bda22007-05-01 22:32:36 +02001034 /*
1035 * We run (with interrupts disabled) until we're sure that none of
1036 * the host adapters have anything that can be done, at which point
1037 * we set main_running to 0 and exit.
1038 *
1039 * Interrupts are enabled before doing various other internal
1040 * instructions, after we've decided that we need to run through
1041 * the loop again.
1042 *
1043 * this should prevent any race conditions.
1044 *
1045 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1046 * because also a timer int can trigger an abort or reset, which can
1047 * alter queues and touch the Falcon lock.
1048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Roman Zippelc28bda22007-05-01 22:32:36 +02001050 /* Tell int handlers main() is now already executing. Note that
1051 no races are possible here. If an int comes in before
1052 'main_running' is set here, and queues/executes main via the
1053 task queue, it doesn't do any harm, just this instance of main
1054 won't find any work left to do. */
Finn Thaina53a21e2014-11-12 16:12:21 +11001055 if (hostdata->main_running)
Roman Zippelc28bda22007-05-01 22:32:36 +02001056 return;
Finn Thaina53a21e2014-11-12 16:12:21 +11001057 hostdata->main_running = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001058
1059 local_save_flags(flags);
1060 do {
1061 local_irq_disable(); /* Freeze request queues */
1062 done = 1;
1063
1064 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001065 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001066 /*
1067 * Search through the issue_queue for a command destined
1068 * for a target that's not busy.
1069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001071 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001072 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1073 ;
1074 /*printk("%p ", tmp);*/
1075 if ((tmp == prev) && tmp)
1076 printk(" LOOP\n");
1077 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001079 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001080 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001081 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Finn Thaine3f463b2014-11-12 16:12:16 +11001083 dprintk(NDEBUG_LISTS,
1084 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1085 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1086 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001087 /* When we find one, remove it from the issue queue. */
1088 /* ++guenther: possible race with Falcon locking */
1089 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001091 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001093 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001094#endif
1095 ) {
1096 /* ++guenther: just to be sure, this must be atomic */
1097 local_irq_disable();
1098 if (prev) {
1099 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001100 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001101 } else {
1102 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1103 hostdata->issue_queue = NEXT(tmp);
1104 }
Roman Zippel3130d902007-05-01 22:32:37 +02001105 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001106 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001107
1108 /* reenable interrupts after finding one */
1109 local_irq_restore(flags);
1110
1111 /*
1112 * Attempt to establish an I_T_L nexus here.
1113 * On success, instance->hostdata->connected is set.
1114 * On failure, we must add the command back to the
1115 * issue queue so we can keep trying.
1116 */
Finn Thaind65e6342014-03-18 11:42:20 +11001117 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001118 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001119 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001120 /*
1121 * REQUEST SENSE commands are issued without tagged
1122 * queueing, even on SCSI-II devices because the
1123 * contingent allegiance condition exists for the
1124 * entire unit.
1125 */
1126 /* ++roman: ...and the standard also requires that
1127 * REQUEST SENSE command are untagged.
1128 */
1129
1130#ifdef SUPPORT_TAGS
1131 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1132#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001133 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001134 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001135 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001136 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001137 maybe_release_dma_irq(instance);
1138 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001139 break;
1140 } else {
1141 local_irq_disable();
1142 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001143 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001144 hostdata->issue_queue = tmp;
1145#ifdef SUPPORT_TAGS
1146 cmd_free_tag(tmp);
1147#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001148 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001149 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001150 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001151 "returned to issue_queue\n", HOSTNO);
1152 if (hostdata->connected)
1153 break;
1154 }
1155 } /* if target/lun/target queue is not busy */
1156 } /* for issue_queue */
1157 } /* if (!hostdata->connected) */
1158
1159 if (hostdata->connected
1160#ifdef REAL_DMA
1161 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162#endif
1163 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001165 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001166 HOSTNO);
1167 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001168 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001169 done = 0;
1170 }
1171 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Roman Zippelc28bda22007-05-01 22:32:36 +02001173 /* Better allow ints _after_ 'main_running' has been cleared, else
1174 an interrupt could believe we'll pick up the work it left for
1175 us, but we won't see it anymore here... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001176 hostdata->main_running = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001177 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180
1181#ifdef REAL_DMA
1182/*
1183 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1184 *
1185 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001186 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 *
1188 * Inputs : instance - this instance of the NCR5380.
1189 *
1190 */
1191
Roman Zippelc28bda22007-05-01 22:32:36 +02001192static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Roman Zippelc28bda22007-05-01 22:32:36 +02001194 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001195 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001196 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001197 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001198 int saved_data = 0, overrun = 0;
1199 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Roman Zippelc28bda22007-05-01 22:32:36 +02001201 if (!hostdata->connected) {
1202 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1203 "no connected cmd\n", HOSTNO);
1204 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Finn Thainef1081c2014-11-12 16:12:14 +11001207 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001208 p = hostdata->connected->SCp.phase;
1209 if (p & SR_IO) {
1210 udelay(10);
1211 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1212 (BASR_PHASE_MATCH|BASR_ACK)) ==
1213 (BASR_PHASE_MATCH|BASR_ACK)) {
1214 saved_data = NCR5380_read(INPUT_DATA_REG);
1215 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001216 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001217 }
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001220
Finn Thaind65e6342014-03-18 11:42:20 +11001221 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001222 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1223 NCR5380_read(STATUS_REG));
1224
Finn Thaine3f463b2014-11-12 16:12:16 +11001225#if defined(CONFIG_SUN3)
1226 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1227 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1228 instance->host_no);
1229 BUG();
1230 }
1231
1232 /* make sure we're not stuck in a data phase */
1233 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1234 (BASR_PHASE_MATCH | BASR_ACK)) {
1235 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1236 NCR5380_read(BUS_AND_STATUS_REG));
1237 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1238 instance->host_no);
1239 BUG();
1240 }
1241#endif
1242
Roman Zippelc28bda22007-05-01 22:32:36 +02001243 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1244 NCR5380_write(MODE_REG, MR_BASE);
1245 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1246
Finn Thain01bd9082014-11-12 16:12:23 +11001247 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001248 hostdata->dma_len = 0;
1249
1250 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1251 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001252 *data += transferred;
1253 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001254
Finn Thainef1081c2014-11-12 16:12:14 +11001255 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001256 int cnt, toPIO;
1257
Roman Zippelc28bda22007-05-01 22:32:36 +02001258 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001259 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001260 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001261 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001262 *(*data)++ = saved_data;
1263 (*count)--;
1264 cnt--;
1265 toPIO--;
1266 }
Finn Thaind65e6342014-03-18 11:42:20 +11001267 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 NCR5380_transfer_pio(instance, &p, &cnt, data);
1269 *count -= toPIO - cnt;
1270 }
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273#endif /* REAL_DMA */
1274
1275
Finn Thainff50f9e2014-11-12 16:12:18 +11001276/**
1277 * NCR5380_intr - generic NCR5380 irq handler
1278 * @irq: interrupt number
1279 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001280 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001281 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1282 * from the disconnected queue, and restarting NCR5380_main()
1283 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285
Roman Zippelc28bda22007-05-01 22:32:36 +02001286static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Finn Thaina53a21e2014-11-12 16:12:21 +11001288 struct Scsi_Host *instance = dev_id;
Roman Zippelc28bda22007-05-01 22:32:36 +02001289 int done = 1, handled = 0;
1290 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Finn Thaind65e6342014-03-18 11:42:20 +11001292 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Roman Zippelc28bda22007-05-01 22:32:36 +02001294 /* Look for pending interrupts */
1295 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001296 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001297 /* dispatch to appropriate routine if found and done=0 */
1298 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001299 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001300 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1301 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001302 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001303 NCR5380_reselect(instance);
1304 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1305 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001306 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001307 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1308 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001309 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001310 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1311 } else {
1312 /*
1313 * The rest of the interrupt conditions can occur only during a
1314 * DMA transfer
1315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 /*
1319 * We should only get PHASE MISMATCH and EOP interrupts if we have
1320 * DMA enabled, so do a sanity check based on the current setting
1321 * of the MODE register.
1322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Roman Zippelc28bda22007-05-01 22:32:36 +02001324 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1325 ((basr & BASR_END_DMA_TRANSFER) ||
1326 !(basr & BASR_PHASE_MATCH))) {
1327
Finn Thaind65e6342014-03-18 11:42:20 +11001328 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001329 NCR5380_dma_complete( instance );
1330 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001331 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001333 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001335 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001336 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001337 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1338 HOSTNO, basr, NCR5380_read(MODE_REG),
1339 NCR5380_read(STATUS_REG));
1340 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001341#ifdef SUN3_SCSI_VME
1342 dregs->csr |= CSR_DMA_ENABLE;
1343#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001344 }
1345 } /* if !(SELECTION || PARITY) */
1346 handled = 1;
1347 } /* BASR & IRQ */ else {
1348 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1349 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1350 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1351 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001352#ifdef SUN3_SCSI_VME
1353 dregs->csr |= CSR_DMA_ENABLE;
1354#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001355 }
1356
1357 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001358 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001359 /* Put a call to NCR5380_main() on the queue... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001360 queue_main(shost_priv(instance));
Roman Zippelc28bda22007-05-01 22:32:36 +02001361 }
1362 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Roman Zippelc28bda22007-05-01 22:32:36 +02001365/*
Finn Thain710ddd02014-11-12 16:12:02 +11001366 * Function : int NCR5380_select(struct Scsi_Host *instance,
1367 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 *
1369 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001370 * including ARBITRATION, SELECTION, and initial message out for
1371 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001373 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001374 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001376 * Returns : -1 if selection could not execute for some reason,
1377 * 0 if selection succeeded or failed because the target
1378 * did not respond.
1379 *
1380 * Side effects :
1381 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * with registers as they should have been on entry - ie
1383 * SELECT_ENABLE will be set appropriately, the NCR5380
1384 * will cease to drive any SCSI bus signals.
1385 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001386 * If successful : I_T_L or I_T_L_Q nexus will be established,
1387 * instance->connected will be set to cmd.
1388 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001390 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * cmd->result host byte set to DID_BAD_TARGET.
1392 */
1393
Finn Thain710ddd02014-11-12 16:12:02 +11001394static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Roman Zippelc28bda22007-05-01 22:32:36 +02001396 SETUP_HOSTDATA(instance);
1397 unsigned char tmp[3], phase;
1398 unsigned char *data;
1399 int len;
1400 unsigned long timeout;
1401 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Roman Zippelc28bda22007-05-01 22:32:36 +02001403 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001404 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001405 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001406 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Roman Zippelc28bda22007-05-01 22:32:36 +02001408 /*
1409 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1410 * data bus during SELECTION.
1411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Roman Zippelc28bda22007-05-01 22:32:36 +02001413 local_irq_save(flags);
1414 if (hostdata->connected) {
1415 local_irq_restore(flags);
1416 return -1;
1417 }
1418 NCR5380_write(TARGET_COMMAND_REG, 0);
1419
1420 /*
1421 * Start arbitration.
1422 */
1423
1424 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1425 NCR5380_write(MODE_REG, MR_ARBITRATE);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Roman Zippelc28bda22007-05-01 22:32:36 +02001429 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001430#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001431 {
1432 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Roman Zippelc28bda22007-05-01 22:32:36 +02001434 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1435 time_before(jiffies, timeout) && !hostdata->connected)
1436 ;
1437 if (time_after_eq(jiffies, timeout)) {
1438 printk("scsi : arbitration timeout at %d\n", __LINE__);
1439 NCR5380_write(MODE_REG, MR_BASE);
1440 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1441 return -1;
1442 }
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001445 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1446 !hostdata->connected)
1447 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448#endif
1449
Finn Thaind65e6342014-03-18 11:42:20 +11001450 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Roman Zippelc28bda22007-05-01 22:32:36 +02001452 if (hostdata->connected) {
1453 NCR5380_write(MODE_REG, MR_BASE);
1454 return -1;
1455 }
1456 /*
1457 * The arbitration delay is 2.2us, but this is a minimum and there is
1458 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1459 * the integral nature of udelay().
1460 *
1461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Roman Zippelc28bda22007-05-01 22:32:36 +02001465 /* Check for lost arbitration */
1466 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1467 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1468 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1469 hostdata->connected) {
1470 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001471 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001472 HOSTNO);
1473 return -1;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Roman Zippelc28bda22007-05-01 22:32:36 +02001476 /* after/during arbitration, BSY should be asserted.
1477 IBM DPES-31080 Version S31Q works now */
1478 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1479 NCR5380_write(INITIATOR_COMMAND_REG,
1480 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Roman Zippelc28bda22007-05-01 22:32:36 +02001482 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1483 hostdata->connected) {
1484 NCR5380_write(MODE_REG, MR_BASE);
1485 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001486 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001487 HOSTNO);
1488 return -1;
1489 }
1490
1491 /*
1492 * Again, bus clear + bus settle time is 1.2us, however, this is
1493 * a minimum so we'll udelay ceil(1.2)
1494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001497 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1498 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001500 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001502
1503 if (hostdata->connected) {
1504 NCR5380_write(MODE_REG, MR_BASE);
1505 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1506 return -1;
1507 }
1508
Finn Thaind65e6342014-03-18 11:42:20 +11001509 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001510
1511 /*
1512 * Now that we have won arbitration, start Selection process, asserting
1513 * the host and target ID's on the SCSI bus.
1514 */
1515
1516 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1517
1518 /*
1519 * Raise ATN while SEL is true before BSY goes false from arbitration,
1520 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1521 * phase immediately after selection.
1522 */
1523
1524 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1525 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Roman Zippelc28bda22007-05-01 22:32:36 +02001528 /*
1529 * Reselect interrupts must be turned off prior to the dropping of BSY,
1530 * otherwise we will trigger an interrupt.
1531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Roman Zippelc28bda22007-05-01 22:32:36 +02001533 if (hostdata->connected) {
1534 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1535 return -1;
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Roman Zippelc28bda22007-05-01 22:32:36 +02001538 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Roman Zippelc28bda22007-05-01 22:32:36 +02001540 /*
1541 * The initiator shall then wait at least two deskew delays and release
1542 * the BSY signal.
1543 */
1544 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Roman Zippelc28bda22007-05-01 22:32:36 +02001546 /* Reset BSY */
1547 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1548 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Roman Zippelc28bda22007-05-01 22:32:36 +02001550 /*
1551 * Something weird happens when we cease to drive BSY - looks
1552 * like the board/chip is letting us do another read before the
1553 * appropriate propagation delay has expired, and we're confusing
1554 * a BSY signal from ourselves as the target's response to SELECTION.
1555 *
1556 * A small delay (the 'C++' frontend breaks the pipeline with an
1557 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1558 * tighter 'C' code breaks and requires this) solves the problem -
1559 * the 1 us delay is arbitrary, and only used because this delay will
1560 * be the same on other platforms and since it works here, it should
1561 * work there.
1562 *
1563 * wingel suggests that this could be due to failing to wait
1564 * one deskew delay.
1565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Roman Zippelc28bda22007-05-01 22:32:36 +02001567 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Finn Thaind65e6342014-03-18 11:42:20 +11001569 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Roman Zippelc28bda22007-05-01 22:32:36 +02001571 /*
1572 * The SCSI specification calls for a 250 ms timeout for the actual
1573 * selection.
1574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -05001576 timeout = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Roman Zippelc28bda22007-05-01 22:32:36 +02001578 /*
1579 * XXX very interesting - we're seeing a bounce where the BSY we
1580 * asserted is being reflected / still asserted (propagation delay?)
1581 * and it's detecting as true. Sigh.
1582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001585 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1586 * IO while SEL is true. But again, there are some disks out the in the
1587 * world that do that nevertheless. (Somebody claimed that this announces
1588 * reselection capability of the target.) So we better skip that test and
1589 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Roman Zippelc28bda22007-05-01 22:32:36 +02001592 while (time_before(jiffies, timeout) &&
1593 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1594 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Roman Zippelc28bda22007-05-01 22:32:36 +02001596 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1597 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1598 NCR5380_reselect(instance);
1599 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1600 HOSTNO);
1601 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1602 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001605 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1606 ;
1607#endif
1608
1609 /*
1610 * No less than two deskew delays after the initiator detects the
1611 * BSY signal is true, it shall release the SEL signal and may
1612 * change the DATA BUS. -wingel
1613 */
1614
1615 udelay(1);
1616
1617 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1618
1619 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1620 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1621 if (hostdata->targets_present & (1 << cmd->device->id)) {
1622 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1623 if (hostdata->restart_select)
1624 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001625 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001626 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1627 return -1;
1628 }
1629 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001630#ifdef SUPPORT_TAGS
1631 cmd_free_tag(cmd);
1632#endif
1633 cmd->scsi_done(cmd);
1634 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001635 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001636 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1637 return 0;
1638 }
1639
1640 hostdata->targets_present |= (1 << cmd->device->id);
1641
1642 /*
1643 * Since we followed the SCSI spec, and raised ATN while SEL
1644 * was true but before BSY was false during selection, the information
1645 * transfer phase should be a MESSAGE OUT phase so that we can send the
1646 * IDENTIFY message.
1647 *
1648 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1649 * message (2 bytes) with a tag ID that we increment with every command
1650 * until it wraps back to 0.
1651 *
1652 * XXX - it turns out that there are some broken SCSI-II devices,
1653 * which claim to support tagged queuing but fail when more than
1654 * some number of commands are issued at once.
1655 */
1656
1657 /* Wait for start of REQ/ACK handshake */
1658 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1659 ;
1660
Finn Thaind65e6342014-03-18 11:42:20 +11001661 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001662 HOSTNO, cmd->device->id);
1663 tmp[0] = IDENTIFY(1, cmd->device->lun);
1664
1665#ifdef SUPPORT_TAGS
1666 if (cmd->tag != TAG_NONE) {
1667 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1668 tmp[2] = cmd->tag;
1669 len = 3;
1670 } else
1671 len = 1;
1672#else
1673 len = 1;
1674 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675#endif /* SUPPORT_TAGS */
1676
Roman Zippelc28bda22007-05-01 22:32:36 +02001677 /* Send message(s) */
1678 data = tmp;
1679 phase = PHASE_MSGOUT;
1680 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001681 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001682 /* XXX need to handle errors here */
1683 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001685 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1686#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001687#ifdef SUN3_SCSI_VME
1688 dregs->csr |= CSR_INTR;
1689#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Roman Zippelc28bda22007-05-01 22:32:36 +02001691 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Roman Zippelc28bda22007-05-01 22:32:36 +02001693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Roman Zippelc28bda22007-05-01 22:32:36 +02001696/*
1697 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * unsigned char *phase, int *count, unsigned char **data)
1699 *
1700 * Purpose : transfers data in given phase using polled I/O
1701 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001702 * Inputs : instance - instance of driver, *phase - pointer to
1703 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001705 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001707 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * is in same phase.
1709 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001710 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *
1712 * XXX Note : handling for bus free may be useful.
1713 */
1714
1715/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001716 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * IS 100% reliable, and for the actual data transfer where speed
1718 * counts, we will always do a pseudo DMA or DMA transfer.
1719 */
1720
Roman Zippelc28bda22007-05-01 22:32:36 +02001721static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1722 unsigned char *phase, int *count,
1723 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Roman Zippelc28bda22007-05-01 22:32:36 +02001725 register unsigned char p = *phase, tmp;
1726 register int c = *count;
1727 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Roman Zippelc28bda22007-05-01 22:32:36 +02001729 /*
1730 * The NCR5380 chip will only drive the SCSI bus when the
1731 * phase specified in the appropriate bits of the TARGET COMMAND
1732 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
1734
Roman Zippelc28bda22007-05-01 22:32:36 +02001735 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Roman Zippelc28bda22007-05-01 22:32:36 +02001737 do {
1738 /*
1739 * Wait for assertion of REQ, after which the phase bits will be
1740 * valid
1741 */
1742 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1743 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Finn Thaind65e6342014-03-18 11:42:20 +11001745 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Roman Zippelc28bda22007-05-01 22:32:36 +02001747 /* Check for phase mismatch */
1748 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001749 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001750 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001751 break;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Roman Zippelc28bda22007-05-01 22:32:36 +02001754 /* Do actual transfer from SCSI bus to / from memory */
1755 if (!(p & SR_IO))
1756 NCR5380_write(OUTPUT_DATA_REG, *d);
1757 else
1758 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Roman Zippelc28bda22007-05-01 22:32:36 +02001760 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Roman Zippelc28bda22007-05-01 22:32:36 +02001762 /*
1763 * The SCSI standard suggests that in MSGOUT phase, the initiator
1764 * should drop ATN on the last byte of the message phase
1765 * after REQ has been asserted for the handshake but before
1766 * the initiator raises ACK.
1767 */
1768
1769 if (!(p & SR_IO)) {
1770 if (!((p & SR_MSG) && c > 1)) {
1771 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001772 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001773 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1774 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1775 } else {
1776 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1777 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001778 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001779 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1780 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1781 }
1782 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001783 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001784 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1785 }
1786
1787 while (NCR5380_read(STATUS_REG) & SR_REQ)
1788 ;
1789
Finn Thaind65e6342014-03-18 11:42:20 +11001790 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001791
1792 /*
1793 * We have several special cases to consider during REQ/ACK handshaking :
1794 * 1. We were in MSGOUT phase, and we are on the last byte of the
1795 * message. ATN must be dropped as ACK is dropped.
1796 *
1797 * 2. We are in a MSGIN phase, and we are on the last byte of the
1798 * message. We must exit with ACK asserted, so that the calling
1799 * code may raise ATN before dropping ACK to reject the message.
1800 *
1801 * 3. ACK and ATN are clear and the target may proceed as normal.
1802 */
1803 if (!(p == PHASE_MSGIN && c == 1)) {
1804 if (p == PHASE_MSGOUT && c > 1)
1805 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1806 else
1807 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1808 }
1809 } while (--c);
1810
Finn Thaind65e6342014-03-18 11:42:20 +11001811 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001812
1813 *count = c;
1814 *data = d;
1815 tmp = NCR5380_read(STATUS_REG);
1816 /* The phase read from the bus is valid if either REQ is (already)
1817 * asserted or if ACK hasn't been released yet. The latter is the case if
1818 * we're in MSGIN and all wanted bytes have been received.
1819 */
1820 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1821 *phase = tmp & PHASE_MASK;
1822 else
1823 *phase = PHASE_UNKNOWN;
1824
1825 if (!c || (*phase == p))
1826 return 0;
1827 else
1828 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Finn Thain636b1ec2016-01-03 16:05:10 +11001831/**
1832 * do_reset - issue a reset command
1833 * @instance: adapter to reset
1834 *
1835 * Issue a reset sequence to the NCR5380 and try and get the bus
1836 * back into sane shape.
1837 *
1838 * This clears the reset interrupt flag because there may be no handler for
1839 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1840 * been installed. And when in EH we may have released the ST DMA interrupt.
1841 */
1842
1843static void do_reset(struct Scsi_Host *instance)
1844{
1845 unsigned long flags;
1846
1847 local_irq_save(flags);
1848 NCR5380_write(TARGET_COMMAND_REG,
1849 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1850 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1851 udelay(50);
1852 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1853 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1854 local_irq_restore(flags);
1855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857/*
1858 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001859 *
1860 * Purpose : abort the currently established nexus. Should only be
1861 * called from a routine which can drop into a
1862 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * Returns : 0 on success, -1 on failure.
1864 */
1865
Finn Thaina53a21e2014-11-12 16:12:21 +11001866static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Roman Zippelc28bda22007-05-01 22:32:36 +02001868 unsigned char tmp, *msgptr, phase;
1869 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Roman Zippelc28bda22007-05-01 22:32:36 +02001871 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Roman Zippelc28bda22007-05-01 22:32:36 +02001874 /*
1875 * Wait for the target to indicate a valid phase by asserting
1876 * REQ. Once this happens, we'll have either a MSGOUT phase
1877 * and can immediately send the ABORT message, or we'll have some
1878 * other phase and will have to source/sink data.
1879 *
1880 * We really don't care what value was on the bus or what value
1881 * the target sees, so we just handshake.
1882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Roel Kluin3be38e72007-11-15 21:13:46 +01001884 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001885 ;
1886
1887 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1888
1889 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1890 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1891 ICR_ASSERT_ACK);
1892 while (NCR5380_read(STATUS_REG) & SR_REQ)
1893 ;
1894 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1895 }
1896
1897 tmp = ABORT;
1898 msgptr = &tmp;
1899 len = 1;
1900 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001901 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001902
1903 /*
1904 * If we got here, and the command completed successfully,
1905 * we're about to go into bus free state.
1906 */
1907
1908 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001912/*
1913 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 * unsigned char *phase, int *count, unsigned char **data)
1915 *
1916 * Purpose : transfers data in given phase using either real
1917 * or pseudo DMA.
1918 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001919 * Inputs : instance - instance of driver, *phase - pointer to
1920 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001922 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001924 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * is in same phase.
1926 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001927 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 *
1929 */
1930
1931
Roman Zippelc28bda22007-05-01 22:32:36 +02001932static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1933 unsigned char *phase, int *count,
1934 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Roman Zippelc28bda22007-05-01 22:32:36 +02001936 SETUP_HOSTDATA(instance);
1937 register int c = *count;
1938 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001939 unsigned long flags;
1940
1941#if defined(CONFIG_SUN3)
1942 /* sanity check */
1943 if (!sun3_dma_setup_done) {
1944 pr_err("scsi%d: transfer_dma without setup!\n",
1945 instance->host_no);
1946 BUG();
1947 }
1948 hostdata->dma_len = c;
1949
1950 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1951 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1952 c, (p & SR_IO) ? "to" : "from", *data);
1953
1954 /* netbsd turns off ints here, why not be safe and do it too */
1955 local_irq_save(flags);
1956
1957 /* send start chain */
1958 sun3scsi_dma_start(c, *data);
1959
1960 if (p & SR_IO) {
1961 NCR5380_write(TARGET_COMMAND_REG, 1);
1962 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1963 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1964 NCR5380_write(MODE_REG,
1965 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1966 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1967 } else {
1968 NCR5380_write(TARGET_COMMAND_REG, 0);
1969 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1970 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1971 NCR5380_write(MODE_REG,
1972 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1973 NCR5380_write(START_DMA_SEND_REG, 0);
1974 }
1975
1976#ifdef SUN3_SCSI_VME
1977 dregs->csr |= CSR_DMA_ENABLE;
1978#endif
1979
1980 local_irq_restore(flags);
1981
1982 sun3_dma_active = 1;
1983
1984#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001985 register unsigned char *d = *data;
1986 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Roman Zippelc28bda22007-05-01 22:32:36 +02001988 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1989 *phase = tmp;
1990 return -1;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Finn Thainef1081c2014-11-12 16:12:14 +11001993 if (hostdata->read_overruns && (p & SR_IO))
1994 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Finn Thaind65e6342014-03-18 11:42:20 +11001996 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001997 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1998 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Roman Zippelc28bda22007-05-01 22:32:36 +02002000 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002003 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#endif /* def REAL_DMA */
2005
Finn Thainef1081c2014-11-12 16:12:14 +11002006 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002007 /* On the Medusa, it is a must to initialize the DMA before
2008 * starting the NCR. This is also the cleaner way for the TT.
2009 */
2010 local_irq_save(flags);
2011 hostdata->dma_len = (p & SR_IO) ?
2012 NCR5380_dma_read_setup(instance, d, c) :
2013 NCR5380_dma_write_setup(instance, d, c);
2014 local_irq_restore(flags);
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Roman Zippelc28bda22007-05-01 22:32:36 +02002017 if (p & SR_IO)
2018 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2019 else {
2020 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2021 NCR5380_write(START_DMA_SEND_REG, 0);
2022 }
2023
Finn Thainef1081c2014-11-12 16:12:14 +11002024 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002025 /* On the Falcon, the DMA setup must be done after the last */
2026 /* NCR access, else the DMA setup gets trashed!
2027 */
2028 local_irq_save(flags);
2029 hostdata->dma_len = (p & SR_IO) ?
2030 NCR5380_dma_read_setup(instance, d, c) :
2031 NCR5380_dma_write_setup(instance, d, c);
2032 local_irq_restore(flags);
2033 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002034#endif /* !defined(CONFIG_SUN3) */
2035
Roman Zippelc28bda22007-05-01 22:32:36 +02002036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038#endif /* defined(REAL_DMA) */
2039
2040/*
2041 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2042 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002043 * Purpose : run through the various SCSI phases and do as the target
2044 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 * instance->connected.
2046 *
2047 * Inputs : instance, instance for which we are doing commands
2048 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002049 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 * modified if a command disconnects, *instance->connected will
2051 * change.
2052 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002053 * XXX Note : we need to watch for bus free or a reset condition here
2054 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
Roman Zippelc28bda22007-05-01 22:32:36 +02002056
2057static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Roman Zippelc28bda22007-05-01 22:32:36 +02002059 SETUP_HOSTDATA(instance);
2060 unsigned long flags;
2061 unsigned char msgout = NOP;
2062 int sink = 0;
2063 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02002065 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002067 unsigned char *data;
2068 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11002069 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Finn Thaine3f463b2014-11-12 16:12:16 +11002071#ifdef SUN3_SCSI_VME
2072 dregs->csr |= CSR_INTR;
2073#endif
2074
Roman Zippelc28bda22007-05-01 22:32:36 +02002075 while (1) {
2076 tmp = NCR5380_read(STATUS_REG);
2077 /* We only have a valid SCSI phase when REQ is asserted */
2078 if (tmp & SR_REQ) {
2079 phase = (tmp & PHASE_MASK);
2080 if (phase != old_phase) {
2081 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11002082 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002084#if defined(CONFIG_SUN3)
2085 if (phase == PHASE_CMDOUT) {
2086#if defined(REAL_DMA)
2087 void *d;
2088 unsigned long count;
2089
2090 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2091 count = cmd->SCp.buffer->length;
2092 d = sg_virt(cmd->SCp.buffer);
2093 } else {
2094 count = cmd->SCp.this_residual;
2095 d = cmd->SCp.ptr;
2096 }
2097 /* this command setup for dma yet? */
2098 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2099 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2100 sun3scsi_dma_setup(d, count,
2101 rq_data_dir(cmd->request));
2102 sun3_dma_setup_done = cmd;
2103 }
2104 }
2105#endif
2106#ifdef SUN3_SCSI_VME
2107 dregs->csr |= CSR_INTR;
2108#endif
2109 }
2110#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Roman Zippelc28bda22007-05-01 22:32:36 +02002112 if (sink && (phase != PHASE_MSGOUT)) {
2113 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Roman Zippelc28bda22007-05-01 22:32:36 +02002115 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2116 ICR_ASSERT_ACK);
2117 while (NCR5380_read(STATUS_REG) & SR_REQ)
2118 ;
2119 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2120 ICR_ASSERT_ATN);
2121 sink = 0;
2122 continue;
2123 }
2124
2125 switch (phase) {
2126 case PHASE_DATAOUT:
2127#if (NDEBUG & NDEBUG_NO_DATAOUT)
2128 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2129 "aborted\n", HOSTNO);
2130 sink = 1;
2131 do_abort(instance);
2132 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002133 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002134 return;
2135#endif
2136 case PHASE_DATAIN:
2137 /*
2138 * If there is no room left in the current buffer in the
2139 * scatter-gather list, move onto the next one.
2140 */
2141
2142 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2143 ++cmd->SCp.buffer;
2144 --cmd->SCp.buffers_residual;
2145 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002146 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002147 /* ++roman: Try to merge some scatter-buffers if
2148 * they are at contiguous physical addresses.
2149 */
2150 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002151 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002152 HOSTNO, cmd->SCp.this_residual,
2153 cmd->SCp.buffers_residual);
2154 }
2155
2156 /*
2157 * The preferred transfer method is going to be
2158 * PSEUDO-DMA for systems that are strictly PIO,
2159 * since we can let the hardware do the handshaking.
2160 *
2161 * For this to work, we need to know the transfersize
2162 * ahead of time, since the pseudo-DMA code will sit
2163 * in an unconditional loop.
2164 */
2165
2166 /* ++roman: I suggest, this should be
2167 * #if def(REAL_DMA)
2168 * instead of leaving REAL_DMA out.
2169 */
2170
2171#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002172 if (
2173#if !defined(CONFIG_SUN3)
2174 !cmd->device->borken &&
2175#endif
2176 (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002177 len = transfersize;
2178 cmd->SCp.phase = phase;
2179 if (NCR5380_transfer_dma(instance, &phase,
2180 &len, (unsigned char **)&cmd->SCp.ptr)) {
2181 /*
2182 * If the watchdog timer fires, all future
2183 * accesses to this device will use the
2184 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002185 scmd_printk(KERN_INFO, cmd,
2186 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002187 cmd->device->borken = 1;
2188 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2189 ICR_ASSERT_ATN);
2190 sink = 1;
2191 do_abort(instance);
2192 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002193 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002194 /* XXX - need to source or sink data here, as appropriate */
2195 } else {
2196#ifdef REAL_DMA
2197 /* ++roman: When using real DMA,
2198 * information_transfer() should return after
2199 * starting DMA since it has nothing more to
2200 * do.
2201 */
2202 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002204 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002206 }
2207 } else
2208#endif /* defined(REAL_DMA) */
2209 NCR5380_transfer_pio(instance, &phase,
2210 (int *)&cmd->SCp.this_residual,
2211 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002212#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2213 /* if we had intended to dma that command clear it */
2214 if (sun3_dma_setup_done == cmd)
2215 sun3_dma_setup_done = NULL;
2216#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002217 break;
2218 case PHASE_MSGIN:
2219 len = 1;
2220 data = &tmp;
2221 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2222 NCR5380_transfer_pio(instance, &phase, &len, &data);
2223 cmd->SCp.Message = tmp;
2224
2225 switch (tmp) {
2226 /*
2227 * Linking lets us reduce the time required to get the
2228 * next command out to the device, hopefully this will
2229 * mean we don't waste another revolution due to the delays
2230 * required by ARBITRATION and another SELECTION.
2231 *
2232 * In the current implementation proposal, low level drivers
2233 * merely have to start the next command, pointed to by
2234 * next_link, done() is called as with unlinked commands.
2235 */
2236#ifdef LINKED
2237 case LINKED_CMD_COMPLETE:
2238 case LINKED_FLG_CMD_COMPLETE:
2239 /* Accept message by clearing ACK */
2240 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2241
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002242 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002243 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2244
2245 /* Enable reselect interrupts */
2246 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2247 /*
2248 * Sanity check : A linked command should only terminate
2249 * with one of these messages if there are more linked
2250 * commands available.
2251 */
2252
2253 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002254 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002255 "linked command complete, no next_link\n",
2256 HOSTNO, cmd->device->id, cmd->device->lun);
2257 sink = 1;
2258 do_abort(instance);
2259 return;
2260 }
2261
2262 initialize_SCp(cmd->next_link);
2263 /* The next command is still part of this process; copy it
2264 * and don't free it! */
2265 cmd->next_link->tag = cmd->tag;
2266 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002267 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002268 "done, calling scsi_done().\n",
2269 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002270 cmd->scsi_done(cmd);
2271 cmd = hostdata->connected;
2272 break;
2273#endif /* def LINKED */
2274 case ABORT:
2275 case COMMAND_COMPLETE:
2276 /* Accept message by clearing ACK */
2277 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002278 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002279 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002280
2281 local_irq_save(flags);
2282 hostdata->retain_dma_intr++;
2283 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002284#ifdef SUPPORT_TAGS
2285 cmd_free_tag(cmd);
2286 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2287 /* Turn a QUEUE FULL status into BUSY, I think the
2288 * mid level cannot handle QUEUE FULL :-( (The
2289 * command is retried after BUSY). Also update our
2290 * queue size to the number of currently issued
2291 * commands now.
2292 */
2293 /* ++Andreas: the mid level code knows about
2294 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002295 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002296 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002297 "QUEUE_FULL after %d commands\n",
2298 HOSTNO, cmd->device->id, cmd->device->lun,
2299 ta->nr_allocated);
2300 if (ta->queue_size > ta->nr_allocated)
2301 ta->nr_allocated = ta->queue_size;
2302 }
2303#else
2304 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2305#endif
2306 /* Enable reselect interrupts */
2307 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2308
2309 /*
2310 * I'm not sure what the correct thing to do here is :
2311 *
2312 * If the command that just executed is NOT a request
2313 * sense, the obvious thing to do is to set the result
2314 * code to the values of the stored parameters.
2315 *
2316 * If it was a REQUEST SENSE command, we need some way to
2317 * differentiate between the failure code of the original
2318 * and the failure code of the REQUEST sense - the obvious
2319 * case is success, where we fall through and leave the
2320 * result code unchanged.
2321 *
2322 * The non-obvious place is where the REQUEST SENSE failed
2323 */
2324
2325 if (cmd->cmnd[0] != REQUEST_SENSE)
2326 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2327 else if (status_byte(cmd->SCp.Status) != GOOD)
2328 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2329
Boaz Harrosh28424d32007-09-10 22:37:45 +03002330 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2331 hostdata->ses.cmd_len) {
2332 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2333 hostdata->ses.cmd_len = 0 ;
2334 }
2335
Roman Zippelc28bda22007-05-01 22:32:36 +02002336 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2337 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002338 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002339
Finn Thaind65e6342014-03-18 11:42:20 +11002340 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002341
Roman Zippelc28bda22007-05-01 22:32:36 +02002342 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002343 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002344 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002345 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002346 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002347 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002348 cmd->scsi_done(cmd);
2349 }
2350
Finn Thaine3c3da62014-11-12 16:12:15 +11002351 local_irq_restore(flags);
2352
Roman Zippelc28bda22007-05-01 22:32:36 +02002353 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2354 /*
2355 * Restore phase bits to 0 so an interrupted selection,
2356 * arbitration can resume.
2357 */
2358 NCR5380_write(TARGET_COMMAND_REG, 0);
2359
2360 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2361 barrier();
2362
Finn Thaine3c3da62014-11-12 16:12:15 +11002363 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002364 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002365 /* ++roman: For Falcon SCSI, release the lock on the
2366 * ST-DMA here if no other commands are waiting on the
2367 * disconnected queue.
2368 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002369 maybe_release_dma_irq(instance);
2370 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002371 return;
2372 case MESSAGE_REJECT:
2373 /* Accept message by clearing ACK */
2374 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2375 /* Enable reselect interrupts */
2376 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2377 switch (hostdata->last_message) {
2378 case HEAD_OF_QUEUE_TAG:
2379 case ORDERED_QUEUE_TAG:
2380 case SIMPLE_QUEUE_TAG:
2381 /* The target obviously doesn't support tagged
2382 * queuing, even though it announced this ability in
2383 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2384 * clear 'tagged_supported' and lock the LUN, since
2385 * the command is treated as untagged further on.
2386 */
2387 cmd->device->tagged_supported = 0;
2388 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2389 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002390 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002391 "QUEUE_TAG message; tagged queuing "
2392 "disabled\n",
2393 HOSTNO, cmd->device->id, cmd->device->lun);
2394 break;
2395 }
2396 break;
2397 case DISCONNECT:
2398 /* Accept message by clearing ACK */
2399 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2400 local_irq_save(flags);
2401 cmd->device->disconnect = 1;
2402 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002403 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002404 hostdata->connected = NULL;
2405 hostdata->disconnected_queue = cmd;
2406 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002407 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002408 "moved from connected to the "
2409 "disconnected_queue\n", HOSTNO,
2410 cmd->device->id, cmd->device->lun);
2411 /*
2412 * Restore phase bits to 0 so an interrupted selection,
2413 * arbitration can resume.
2414 */
2415 NCR5380_write(TARGET_COMMAND_REG, 0);
2416
2417 /* Enable reselect interrupts */
2418 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2419 /* Wait for bus free to avoid nasty timeouts */
2420 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2421 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002422#ifdef SUN3_SCSI_VME
2423 dregs->csr |= CSR_DMA_ENABLE;
2424#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002425 return;
2426 /*
2427 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2428 * operation, in violation of the SCSI spec so we can safely
2429 * ignore SAVE/RESTORE pointers calls.
2430 *
2431 * Unfortunately, some disks violate the SCSI spec and
2432 * don't issue the required SAVE_POINTERS message before
2433 * disconnecting, and we have to break spec to remain
2434 * compatible.
2435 */
2436 case SAVE_POINTERS:
2437 case RESTORE_POINTERS:
2438 /* Accept message by clearing ACK */
2439 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2440 /* Enable reselect interrupts */
2441 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2442 break;
2443 case EXTENDED_MESSAGE:
2444 /*
2445 * Extended messages are sent in the following format :
2446 * Byte
2447 * 0 EXTENDED_MESSAGE == 1
2448 * 1 length (includes one byte for code, doesn't
2449 * include first two bytes)
2450 * 2 code
2451 * 3..length+1 arguments
2452 *
2453 * Start the extended message buffer with the EXTENDED_MESSAGE
2454 * byte, since spi_print_msg() wants the whole thing.
2455 */
2456 extended_msg[0] = EXTENDED_MESSAGE;
2457 /* Accept first byte by clearing ACK */
2458 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2459
Finn Thaind65e6342014-03-18 11:42:20 +11002460 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002461
2462 len = 2;
2463 data = extended_msg + 1;
2464 phase = PHASE_MSGIN;
2465 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002466 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002467 (int)extended_msg[1], (int)extended_msg[2]);
2468
2469 if (!len && extended_msg[1] <=
2470 (sizeof(extended_msg) - 1)) {
2471 /* Accept third byte by clearing ACK */
2472 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2473 len = extended_msg[1] - 1;
2474 data = extended_msg + 3;
2475 phase = PHASE_MSGIN;
2476
2477 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002478 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002479 HOSTNO, len);
2480
2481 switch (extended_msg[2]) {
2482 case EXTENDED_SDTR:
2483 case EXTENDED_WDTR:
2484 case EXTENDED_MODIFY_DATA_POINTER:
2485 case EXTENDED_EXTENDED_IDENTIFY:
2486 tmp = 0;
2487 }
2488 } else if (len) {
2489 printk(KERN_NOTICE "scsi%d: error receiving "
2490 "extended message\n", HOSTNO);
2491 tmp = 0;
2492 } else {
2493 printk(KERN_NOTICE "scsi%d: extended message "
2494 "code %02x length %d is too long\n",
2495 HOSTNO, extended_msg[2], extended_msg[1]);
2496 tmp = 0;
2497 }
2498 /* Fall through to reject message */
2499
2500 /*
2501 * If we get something weird that we aren't expecting,
2502 * reject it.
2503 */
2504 default:
2505 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002506 printk(KERN_INFO "scsi%d: rejecting message ",
2507 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002508 spi_print_msg(extended_msg);
2509 printk("\n");
2510 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002511 scmd_printk(KERN_INFO, cmd,
2512 "rejecting unknown message %02x\n",
2513 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002514 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002515 scmd_printk(KERN_INFO, cmd,
2516 "rejecting unknown extended message code %02x, length %d\n",
2517 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002518
2519 msgout = MESSAGE_REJECT;
2520 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2521 break;
2522 } /* switch (tmp) */
2523 break;
2524 case PHASE_MSGOUT:
2525 len = 1;
2526 data = &msgout;
2527 hostdata->last_message = msgout;
2528 NCR5380_transfer_pio(instance, &phase, &len, &data);
2529 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002530 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002531#ifdef SUPPORT_TAGS
2532 cmd_free_tag(cmd);
2533#else
2534 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2535#endif
2536 hostdata->connected = NULL;
2537 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002538 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002539 maybe_release_dma_irq(instance);
2540 local_irq_restore(flags);
2541 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002542 return;
2543 }
2544 msgout = NOP;
2545 break;
2546 case PHASE_CMDOUT:
2547 len = cmd->cmd_len;
2548 data = cmd->cmnd;
2549 /*
2550 * XXX for performance reasons, on machines with a
2551 * PSEUDO-DMA architecture we should probably
2552 * use the dma transfer function.
2553 */
2554 NCR5380_transfer_pio(instance, &phase, &len, &data);
2555 break;
2556 case PHASE_STATIN:
2557 len = 1;
2558 data = &tmp;
2559 NCR5380_transfer_pio(instance, &phase, &len, &data);
2560 cmd->SCp.Status = tmp;
2561 break;
2562 default:
2563 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002564 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002565 } /* switch(phase) */
2566 } /* if (tmp * SR_REQ) */
2567 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570/*
2571 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2572 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002573 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002574 * 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 -07002575 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002576 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * Inputs : instance - this instance of the NCR5380.
2578 *
2579 */
2580
2581
Finn Thaine3f463b2014-11-12 16:12:16 +11002582/* it might eventually prove necessary to do a dma setup on
2583 reselection, but it doesn't seem to be needed now -- sam */
2584
Roman Zippelc28bda22007-05-01 22:32:36 +02002585static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Roman Zippelc28bda22007-05-01 22:32:36 +02002587 SETUP_HOSTDATA(instance);
2588 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002589 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002591 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002593 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002594 int __maybe_unused len;
2595 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002596 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Roman Zippelc28bda22007-05-01 22:32:36 +02002598 /*
2599 * Disable arbitration, etc. since the host adapter obviously
2600 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Roman Zippelc28bda22007-05-01 22:32:36 +02002603 NCR5380_write(MODE_REG, MR_BASE);
2604 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Roman Zippelc28bda22007-05-01 22:32:36 +02002606 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2607
Finn Thaind65e6342014-03-18 11:42:20 +11002608 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002609
2610 /*
2611 * At this point, we have detected that our SCSI ID is on the bus,
2612 * SEL is true and BSY was false for at least one bus settle delay
2613 * (400 ns).
2614 *
2615 * We must assert BSY ourselves, until the target drops the SEL
2616 * signal.
2617 */
2618
2619 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2620
2621 while (NCR5380_read(STATUS_REG) & SR_SEL)
2622 ;
2623 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2624
2625 /*
2626 * Wait for target to go into MSGIN.
2627 */
2628
2629 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2630 ;
2631
Finn Thaine3f463b2014-11-12 16:12:16 +11002632#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2633 /* acknowledge toggle to MSGIN */
2634 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2635
2636 /* peek at the byte without really hitting the bus */
2637 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2638#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002639 len = 1;
2640 data = msg;
2641 phase = PHASE_MSGIN;
2642 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002643#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002644
2645 if (!(msg[0] & 0x80)) {
2646 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2647 spi_print_msg(msg);
2648 do_abort(instance);
2649 return;
2650 }
2651 lun = (msg[0] & 0x07);
2652
Finn Thaine3f463b2014-11-12 16:12:16 +11002653#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002654 /* If the phase is still MSGIN, the target wants to send some more
2655 * messages. In case it supports tagged queuing, this is probably a
2656 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2657 */
2658 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002659 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002660 /* Accept previous IDENTIFY message by clearing ACK */
2661 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2662 len = 2;
2663 data = msg + 1;
2664 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2665 msg[1] == SIMPLE_QUEUE_TAG)
2666 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002667 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002668 "reselection\n", HOSTNO, target_mask, lun, tag);
2669 }
2670#endif
2671
2672 /*
2673 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2674 * just reestablished, and remove it from the disconnected queue.
2675 */
2676
Finn Thain710ddd02014-11-12 16:12:02 +11002677 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002678 tmp; prev = tmp, tmp = NEXT(tmp)) {
2679 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2680#ifdef SUPPORT_TAGS
2681 && (tag == tmp->tag)
2682#endif
2683 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002684 if (prev) {
2685 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002686 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002687 } else {
2688 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2689 hostdata->disconnected_queue = NEXT(tmp);
2690 }
Roman Zippel3130d902007-05-01 22:32:37 +02002691 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002692 break;
2693 }
2694 }
2695
2696 if (!tmp) {
2697 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2698#ifdef SUPPORT_TAGS
2699 "tag %d "
2700#endif
2701 "not in disconnected_queue.\n",
2702 HOSTNO, target_mask, lun
2703#ifdef SUPPORT_TAGS
2704 , tag
2705#endif
2706 );
2707 /*
2708 * Since we have an established nexus that we can't do anything
2709 * with, we must abort it.
2710 */
2711 do_abort(instance);
2712 return;
2713 }
2714
Finn Thaine3f463b2014-11-12 16:12:16 +11002715#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2716 /* engage dma setup for the command we just saw */
2717 {
2718 void *d;
2719 unsigned long count;
2720
2721 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2722 count = tmp->SCp.buffer->length;
2723 d = sg_virt(tmp->SCp.buffer);
2724 } else {
2725 count = tmp->SCp.this_residual;
2726 d = tmp->SCp.ptr;
2727 }
2728 /* setup this command for dma if not already */
2729 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2730 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2731 sun3_dma_setup_done = tmp;
2732 }
2733 }
2734
2735 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2736#endif
2737
Roman Zippelc28bda22007-05-01 22:32:36 +02002738 /* Accept message by clearing ACK */
2739 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2740
Finn Thaine3f463b2014-11-12 16:12:16 +11002741#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2742 /* If the phase is still MSGIN, the target wants to send some more
2743 * messages. In case it supports tagged queuing, this is probably a
2744 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2745 */
2746 tag = TAG_NONE;
2747 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2748 /* Accept previous IDENTIFY message by clearing ACK */
2749 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2750 len = 2;
2751 data = msg + 1;
2752 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2753 msg[1] == SIMPLE_QUEUE_TAG)
2754 tag = msg[2];
2755 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2756 HOSTNO, target_mask, lun, tag);
2757 }
2758#endif
2759
Roman Zippelc28bda22007-05-01 22:32:36 +02002760 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002761 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002762 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765
2766/*
Finn Thain710ddd02014-11-12 16:12:02 +11002767 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 *
2769 * Purpose : abort a command
2770 *
Finn Thain710ddd02014-11-12 16:12:02 +11002771 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002772 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * used.
2774 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002775 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002777 * XXX - there is no way to abort the command that is currently
2778 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002780 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 */
2782
2783static
Finn Thain710ddd02014-11-12 16:12:02 +11002784int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Roman Zippelc28bda22007-05-01 22:32:36 +02002786 struct Scsi_Host *instance = cmd->device->host;
2787 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002788 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002789 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002791 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Roman Zippelc28bda22007-05-01 22:32:36 +02002793 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Roman Zippelc28bda22007-05-01 22:32:36 +02002795 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Finn Thaind65e6342014-03-18 11:42:20 +11002797 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002798 NCR5380_read(BUS_AND_STATUS_REG),
2799 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002802 /*
2803 * Case 1 : If the command is the currently executing command,
2804 * we'll set the aborted flag and return control so that
2805 * information transfer routine can exit cleanly.
2806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Roman Zippelc28bda22007-05-01 22:32:36 +02002808 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Finn Thaind65e6342014-03-18 11:42:20 +11002810 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002811 /*
2812 * We should perform BSY checking, and make sure we haven't slipped
2813 * into BUS FREE.
2814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Roman Zippelc28bda22007-05-01 22:32:36 +02002816 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2817 /*
2818 * Since we can't change phases until we've completed the current
2819 * handshake, we have to source or sink a byte of data if the current
2820 * phase is not MSGOUT.
2821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Roman Zippelc28bda22007-05-01 22:32:36 +02002823 /*
2824 * Return control to the executing NCR drive so we can clear the
2825 * aborted flag and get back into our main loop.
2826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Roman Zippelc28bda22007-05-01 22:32:36 +02002828 if (do_abort(instance) == 0) {
2829 hostdata->aborted = 1;
2830 hostdata->connected = NULL;
2831 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002833 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002835 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002837 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002838 local_irq_restore(flags);
2839 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002840 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002841 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002842 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002843 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002844 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002848
2849 /*
2850 * Case 2 : If the command hasn't been issued yet, we simply remove it
2851 * from the issue queue.
2852 */
Finn Thain710ddd02014-11-12 16:12:02 +11002853 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2854 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002855 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2856 if (cmd == tmp) {
2857 REMOVE(5, *prev, tmp, NEXT(tmp));
2858 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002859 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002860 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002861 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002862 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002863 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002864 HOSTNO);
2865 /* Tagged queuing note: no tag to free here, hasn't been assigned
2866 * yet... */
2867 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002868 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870 }
2871
Roman Zippelc28bda22007-05-01 22:32:36 +02002872 /*
2873 * Case 3 : If any commands are connected, we're going to fail the abort
2874 * and let the high level SCSI driver retry at a later time or
2875 * issue a reset.
2876 *
2877 * Timeouts, and therefore aborted commands, will be highly unlikely
2878 * and handling them cleanly in this situation would make the common
2879 * case of noresets less efficient, and would pollute our code. So,
2880 * we fail.
2881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Roman Zippelc28bda22007-05-01 22:32:36 +02002883 if (hostdata->connected) {
2884 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002885 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002886 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Roman Zippelc28bda22007-05-01 22:32:36 +02002889 /*
2890 * Case 4: If the command is currently disconnected from the bus, and
2891 * there are no connected commands, we reconnect the I_T_L or
2892 * I_T_L_Q nexus associated with it, go into message out, and send
2893 * an abort message.
2894 *
2895 * This case is especially ugly. In order to reestablish the nexus, we
2896 * need to call NCR5380_select(). The easiest way to implement this
2897 * function was to abort if the bus was busy, and let the interrupt
2898 * handler triggered on the SEL for reselect take care of lost arbitrations
2899 * where necessary, meaning interrupts need to be enabled.
2900 *
2901 * When interrupts are enabled, the queues may change - so we
2902 * can't remove it from the disconnected queue before selecting it
2903 * because that could cause a failure in hashing the nexus if that
2904 * device reselected.
2905 *
2906 * Since the queues may change, we can't use the pointers from when we
2907 * first locate it.
2908 *
2909 * So, we must first locate the command, and if NCR5380_select()
2910 * succeeds, then issue the abort, relocate the command and remove
2911 * it from the disconnected queue.
2912 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Finn Thain710ddd02014-11-12 16:12:02 +11002914 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002915 tmp = NEXT(tmp)) {
2916 if (cmd == tmp) {
2917 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002918 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002919
Finn Thain76f13b92014-11-12 16:11:53 +11002920 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002921 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002922
Finn Thaind65e6342014-03-18 11:42:20 +11002923 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002924
2925 do_abort(instance);
2926
2927 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002928 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2929 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002930 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2931 if (cmd == tmp) {
2932 REMOVE(5, *prev, tmp, NEXT(tmp));
2933 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002934 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002935 tmp->result = DID_ABORT << 16;
2936 /* We must unlock the tag/LUN immediately here, since the
2937 * target goes to BUS FREE and doesn't send us another
2938 * message (COMMAND_COMPLETE or the like)
2939 */
2940#ifdef SUPPORT_TAGS
2941 cmd_free_tag(tmp);
2942#else
2943 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2944#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002945 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002946 local_irq_restore(flags);
2947 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002948 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002949 }
2950 }
2951 }
2952 }
2953
Finn Thaine3c3da62014-11-12 16:12:15 +11002954 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2955 * possible at all) At least, we should check if the lock could be
2956 * released after the abort, in case it is kept due to some bug.
2957 */
2958 maybe_release_dma_irq(instance);
2959 local_irq_restore(flags);
2960
Roman Zippelc28bda22007-05-01 22:32:36 +02002961 /*
2962 * Case 5 : If we reached this point, the command was not found in any of
2963 * the queues.
2964 *
2965 * We probably reached this point because of an unlikely race condition
2966 * between the command completing successfully and the abortion code,
2967 * so we won't panic, but we will notify the user in case something really
2968 * broke.
2969 */
2970
Joe Perchesad361c92009-07-06 13:05:40 -07002971 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002972
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002973 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
2976
Roman Zippelc28bda22007-05-01 22:32:36 +02002977/*
Finn Thain710ddd02014-11-12 16:12:02 +11002978 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002979 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 * Purpose : reset the SCSI bus.
2981 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002982 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Finn Thain710ddd02014-11-12 16:12:02 +11002986static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Finn Thaine3c3da62014-11-12 16:12:15 +11002988 struct Scsi_Host *instance = cmd->device->host;
2989 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002990 int i;
2991 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Finn Thaine3c3da62014-11-12 16:12:15 +11002993 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Roman Zippelc28bda22007-05-01 22:32:36 +02002995 /* get in phase */
2996 NCR5380_write(TARGET_COMMAND_REG,
2997 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2998 /* assert RST */
2999 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
3000 udelay(40);
3001 /* reset NCR registers */
3002 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3003 NCR5380_write(MODE_REG, MR_BASE);
3004 NCR5380_write(TARGET_COMMAND_REG, 0);
3005 NCR5380_write(SELECT_ENABLE_REG, 0);
3006 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
3007 * through anymore ... */
3008 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Roman Zippelc28bda22007-05-01 22:32:36 +02003010 /* After the reset, there are no more connected or disconnected commands
3011 * and no busy units; so clear the low-level status here to avoid
3012 * conflicts when the mid-level code tries to wake up the affected
3013 * commands!
3014 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Roman Zippelc28bda22007-05-01 22:32:36 +02003016 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003017 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003018 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11003019 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003020 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003021 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Roman Zippelc28bda22007-05-01 22:32:36 +02003023 local_irq_save(flags);
3024 hostdata->issue_queue = NULL;
3025 hostdata->connected = NULL;
3026 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11003028 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02003030 for (i = 0; i < 8; ++i)
3031 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02003033 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11003035
3036 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02003037 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003039 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}