blob: de53cda4b15310c2ef8aae72dc77416cdd1e2457 [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 Thain9c3f0e22016-01-03 16:05:11 +1100677 "flags { %s%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 Thain9c3f0e22016-01-03 16:05:11 +1100684 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100685#ifdef DIFFERENTIAL
686 "DIFFERENTIAL "
687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100689 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690#endif
691#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100692 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693#endif
694#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100695 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#endif
Finn Thain8c325132014-11-12 16:11:58 +1100697 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Finn Thainff50f9e2014-11-12 16:12:18 +1100700/**
701 * NCR5380_print_status - dump controller info
702 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100704 * Print commands in the various queues, called from NCR5380_abort
705 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707
Finn Thain710ddd02014-11-12 16:12:02 +1100708static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400709{
710 int i, s;
711 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200712 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400713 H_NO(cmd), cmd->device->id, cmd->device->lun);
714 printk(KERN_CONT " command = ");
715 command = cmd->cmnd;
716 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
717 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
718 printk(KERN_CONT " %02x", command[i]);
719 printk("\n");
720}
721
Roman Zippelc28bda22007-05-01 22:32:36 +0200722static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Al Virod89537e2013-03-31 13:24:44 -0400724 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100725 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400726 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Finn Thain8ad3a592014-03-18 11:42:19 +1100728 NCR5380_dprint(NDEBUG_ANY, instance);
729 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Roman Zippelc28bda22007-05-01 22:32:36 +0200731 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Roman Zippelc28bda22007-05-01 22:32:36 +0200733 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400734 printk("NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100735 hostdata->main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200736 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400737 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200738 else
Finn Thain710ddd02014-11-12 16:12:02 +1100739 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400740 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100741 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400742 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Al Virod89537e2013-03-31 13:24:44 -0400744 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100745 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400746 ptr = NEXT(ptr))
747 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200748
749 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400750 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Finn Thain710ddd02014-11-12 16:12:02 +1100753static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Roman Zippelc28bda22007-05-01 22:32:36 +0200755 int i, s;
756 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200757 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200758 H_NO(cmd), cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100759 seq_puts(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200760 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400761 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200762 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400763 seq_printf(m, " %02x", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100764 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Finn Thain4d3d2a52014-11-12 16:11:52 +1100767static int __maybe_unused NCR5380_show_info(struct seq_file *m,
768 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400769{
770 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100771 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400772 unsigned long flags;
773
774 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
775
Al Virod89537e2013-03-31 13:24:44 -0400776 local_irq_save(flags);
777 seq_printf(m, "NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100778 hostdata->main_running ? "" : "n't");
Al Virod89537e2013-03-31 13:24:44 -0400779 if (!hostdata->connected)
780 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
781 else
Finn Thain710ddd02014-11-12 16:12:02 +1100782 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400783 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100784 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400785 show_Scsi_Cmnd(ptr, m);
786
787 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100788 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400789 ptr = NEXT(ptr))
790 show_Scsi_Cmnd(ptr, m);
791
792 local_irq_restore(flags);
793 return 0;
794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Finn Thainff50f9e2014-11-12 16:12:18 +1100796/**
797 * NCR5380_init - initialise an NCR5380
798 * @instance: adapter to configure
799 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100801 * Initializes *instance and corresponding 5380 chip,
802 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
804 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100805 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200806 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100807 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100810static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Roman Zippelc28bda22007-05-01 22:32:36 +0200812 int i;
813 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Finn Thaina53a21e2014-11-12 16:12:21 +1100815 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200816 hostdata->aborted = 0;
817 hostdata->id_mask = 1 << instance->this_id;
818 hostdata->id_higher_mask = 0;
819 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
820 if (i > hostdata->id_mask)
821 hostdata->id_higher_mask |= i;
822 for (i = 0; i < 8; ++i)
823 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100825 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#endif
827#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200828 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200830 hostdata->targets_present = 0;
831 hostdata->connected = NULL;
832 hostdata->issue_queue = NULL;
833 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100834 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Finn Thaina53a21e2014-11-12 16:12:21 +1100836 INIT_WORK(&hostdata->main_task, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Finn Thain8c325132014-11-12 16:11:58 +1100838 prepare_info(instance);
839
Roman Zippelc28bda22007-05-01 22:32:36 +0200840 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
841 NCR5380_write(MODE_REG, MR_BASE);
842 NCR5380_write(TARGET_COMMAND_REG, 0);
843 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Roman Zippelc28bda22007-05-01 22:32:36 +0200845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Finn Thainff50f9e2014-11-12 16:12:18 +1100848/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100849 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
850 * @instance: adapter to check
851 *
852 * If the system crashed, it may have crashed with a connected target and
853 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
854 * currently established nexus, which we know nothing about. Failing that
855 * do a bus reset.
856 *
857 * Note that a bus reset will cause the chip to assert IRQ.
858 *
859 * Returns 0 if successful, otherwise -ENXIO.
860 */
861
862static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
863{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100864 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100865 int pass;
866
867 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
868 switch (pass) {
869 case 1:
870 case 3:
871 case 5:
872 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
873 NCR5380_poll_politely(instance,
874 STATUS_REG, SR_BSY, 0, 5 * HZ);
875 break;
876 case 2:
877 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
878 do_abort(instance);
879 break;
880 case 4:
881 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
882 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100883 /* Wait after a reset; the SCSI standard calls for
884 * 250ms, we wait 500ms to be on the safe side.
885 * But some Toshiba CD-ROMs need ten times that.
886 */
887 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
888 msleep(2500);
889 else
890 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100891 break;
892 case 6:
893 shost_printk(KERN_ERR, instance, "bus locked solid\n");
894 return -ENXIO;
895 }
896 }
897 return 0;
898}
899
900/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100901 * NCR5380_exit - remove an NCR5380
902 * @instance: adapter to remove
903 *
904 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
905 */
906
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200907static void NCR5380_exit(struct Scsi_Host *instance)
908{
Finn Thaina53a21e2014-11-12 16:12:21 +1100909 struct NCR5380_hostdata *hostdata = shost_priv(instance);
910
911 cancel_work_sync(&hostdata->main_task);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200912}
913
Finn Thainff50f9e2014-11-12 16:12:18 +1100914/**
915 * NCR5380_queue_command - queue a command
916 * @instance: the relevant SCSI adapter
917 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100919 * cmd is added to the per instance issue_queue, with minor
920 * twiddling done to the host specific fields of cmd. If the
921 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923
Finn Thain16b29e72014-11-12 16:12:08 +1100924static int NCR5380_queue_command(struct Scsi_Host *instance,
925 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Finn Thain16b29e72014-11-12 16:12:08 +1100927 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100928 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200929 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200932 switch (cmd->cmnd[0]) {
933 case WRITE_6:
934 case WRITE_10:
935 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
936 H_NO(cmd));
937 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100938 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200939 return 0;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
942
Roman Zippelc28bda22007-05-01 22:32:36 +0200943 /*
944 * We use the host_scribble field as a pointer to the next command
945 * in a queue
946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Roman Zippel3130d902007-05-01 22:32:37 +0200948 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200949 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Roman Zippelc28bda22007-05-01 22:32:36 +0200951 /*
952 * Insert the cmd into the issue queue. Note that REQUEST SENSE
953 * commands are added to the head of the queue since any command will
954 * clear the contingent allegiance condition that exists and the
955 * sense data is only guaranteed to be valid while the condition exists.
956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Roman Zippelc28bda22007-05-01 22:32:36 +0200958 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
959 * Otherwise a running NCR5380_main may steal the lock.
960 * Lock before actually inserting due to fairness reasons explained in
961 * atari_scsi.c. If we insert first, then it's impossible for this driver
962 * to release the lock.
963 * Stop timer for this command while waiting for the lock, or timeouts
964 * may happen (and they really do), and it's no good if the command doesn't
965 * appear in any of the queues.
966 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
967 * because also a timer int can trigger an abort or reset, which would
968 * alter queues and touch the lock.
969 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100970 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100971 return SCSI_MLQUEUE_HOST_BUSY;
972
973 local_irq_save(flags);
974
Finn Thainff50f9e2014-11-12 16:12:18 +1100975 /*
976 * Insert the cmd into the issue queue. Note that REQUEST SENSE
977 * commands are added to the head of the queue since any command will
978 * clear the contingent allegiance condition that exists and the
979 * sense data is only guaranteed to be valid while the condition exists.
980 */
981
Roman Zippelc28bda22007-05-01 22:32:36 +0200982 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
983 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200984 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200985 hostdata->issue_queue = cmd;
986 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100987 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200988 NEXT(tmp); tmp = NEXT(tmp))
989 ;
990 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200991 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200992 }
993 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Finn Thaind65e6342014-03-18 11:42:20 +1100995 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200996 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Roman Zippelc28bda22007-05-01 22:32:36 +0200998 /* If queue_command() is called from an interrupt (real one or bottom
999 * half), we let queue_main() do the job of taking care about main. If it
1000 * is already running, this is a no-op, else main will be queued.
1001 *
1002 * If we're not in an interrupt, we can call NCR5380_main()
1003 * unconditionally, because it cannot be already running.
1004 */
Finn Thain16b29e72014-11-12 16:12:08 +11001005 if (in_interrupt() || irqs_disabled())
Finn Thaina53a21e2014-11-12 16:12:21 +11001006 queue_main(hostdata);
Roman Zippelc28bda22007-05-01 22:32:36 +02001007 else
Finn Thaina53a21e2014-11-12 16:12:21 +11001008 NCR5380_main(&hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +02001009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Finn Thaine3c3da62014-11-12 16:12:15 +11001012static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
1013{
1014 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1015
1016 /* Caller does the locking needed to set & test these data atomically */
1017 if (!hostdata->disconnected_queue &&
1018 !hostdata->issue_queue &&
1019 !hostdata->connected &&
1020 !hostdata->retain_dma_intr)
1021 NCR5380_release_dma_irq(instance);
1022}
1023
Finn Thainff50f9e2014-11-12 16:12:18 +11001024/**
1025 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001027 * NCR5380_main is a coroutine that runs as long as more work can
1028 * be done on the NCR5380 host adapters in a system. Both
1029 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1030 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001032 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +02001033 */
1034
Geert Uytterhoevenb312b382007-05-01 22:32:48 +02001035static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Finn Thaina53a21e2014-11-12 16:12:21 +11001037 struct NCR5380_hostdata *hostdata =
1038 container_of(work, struct NCR5380_hostdata, main_task);
1039 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +11001040 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02001041 int done;
1042 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Roman Zippelc28bda22007-05-01 22:32:36 +02001044 /*
1045 * We run (with interrupts disabled) until we're sure that none of
1046 * the host adapters have anything that can be done, at which point
1047 * we set main_running to 0 and exit.
1048 *
1049 * Interrupts are enabled before doing various other internal
1050 * instructions, after we've decided that we need to run through
1051 * the loop again.
1052 *
1053 * this should prevent any race conditions.
1054 *
1055 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1056 * because also a timer int can trigger an abort or reset, which can
1057 * alter queues and touch the Falcon lock.
1058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Roman Zippelc28bda22007-05-01 22:32:36 +02001060 /* Tell int handlers main() is now already executing. Note that
1061 no races are possible here. If an int comes in before
1062 'main_running' is set here, and queues/executes main via the
1063 task queue, it doesn't do any harm, just this instance of main
1064 won't find any work left to do. */
Finn Thaina53a21e2014-11-12 16:12:21 +11001065 if (hostdata->main_running)
Roman Zippelc28bda22007-05-01 22:32:36 +02001066 return;
Finn Thaina53a21e2014-11-12 16:12:21 +11001067 hostdata->main_running = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001068
1069 local_save_flags(flags);
1070 do {
1071 local_irq_disable(); /* Freeze request queues */
1072 done = 1;
1073
1074 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001075 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001076 /*
1077 * Search through the issue_queue for a command destined
1078 * for a target that's not busy.
1079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001081 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001082 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1083 ;
1084 /*printk("%p ", tmp);*/
1085 if ((tmp == prev) && tmp)
1086 printk(" LOOP\n");
1087 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001089 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001090 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001091 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Finn Thaine3f463b2014-11-12 16:12:16 +11001093 dprintk(NDEBUG_LISTS,
1094 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1095 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1096 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001097 /* When we find one, remove it from the issue queue. */
1098 /* ++guenther: possible race with Falcon locking */
1099 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001101 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001103 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001104#endif
1105 ) {
1106 /* ++guenther: just to be sure, this must be atomic */
1107 local_irq_disable();
1108 if (prev) {
1109 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001110 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001111 } else {
1112 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1113 hostdata->issue_queue = NEXT(tmp);
1114 }
Roman Zippel3130d902007-05-01 22:32:37 +02001115 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001116 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001117
1118 /* reenable interrupts after finding one */
1119 local_irq_restore(flags);
1120
1121 /*
1122 * Attempt to establish an I_T_L nexus here.
1123 * On success, instance->hostdata->connected is set.
1124 * On failure, we must add the command back to the
1125 * issue queue so we can keep trying.
1126 */
Finn Thaind65e6342014-03-18 11:42:20 +11001127 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001128 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001129 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001130 /*
1131 * REQUEST SENSE commands are issued without tagged
1132 * queueing, even on SCSI-II devices because the
1133 * contingent allegiance condition exists for the
1134 * entire unit.
1135 */
1136 /* ++roman: ...and the standard also requires that
1137 * REQUEST SENSE command are untagged.
1138 */
1139
1140#ifdef SUPPORT_TAGS
1141 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1142#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001143 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001144 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001145 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001146 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001147 maybe_release_dma_irq(instance);
1148 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001149 break;
1150 } else {
1151 local_irq_disable();
1152 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001153 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001154 hostdata->issue_queue = tmp;
1155#ifdef SUPPORT_TAGS
1156 cmd_free_tag(tmp);
1157#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001158 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001159 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001160 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001161 "returned to issue_queue\n", HOSTNO);
1162 if (hostdata->connected)
1163 break;
1164 }
1165 } /* if target/lun/target queue is not busy */
1166 } /* for issue_queue */
1167 } /* if (!hostdata->connected) */
1168
1169 if (hostdata->connected
1170#ifdef REAL_DMA
1171 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#endif
1173 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001175 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001176 HOSTNO);
1177 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001178 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001179 done = 0;
1180 }
1181 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Roman Zippelc28bda22007-05-01 22:32:36 +02001183 /* Better allow ints _after_ 'main_running' has been cleared, else
1184 an interrupt could believe we'll pick up the work it left for
1185 us, but we won't see it anymore here... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001186 hostdata->main_running = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001187 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190
1191#ifdef REAL_DMA
1192/*
1193 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1194 *
1195 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001196 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 *
1198 * Inputs : instance - this instance of the NCR5380.
1199 *
1200 */
1201
Roman Zippelc28bda22007-05-01 22:32:36 +02001202static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Roman Zippelc28bda22007-05-01 22:32:36 +02001204 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001205 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001206 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001207 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001208 int saved_data = 0, overrun = 0;
1209 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Roman Zippelc28bda22007-05-01 22:32:36 +02001211 if (!hostdata->connected) {
1212 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1213 "no connected cmd\n", HOSTNO);
1214 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Finn Thainef1081c2014-11-12 16:12:14 +11001217 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001218 p = hostdata->connected->SCp.phase;
1219 if (p & SR_IO) {
1220 udelay(10);
1221 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1222 (BASR_PHASE_MATCH|BASR_ACK)) ==
1223 (BASR_PHASE_MATCH|BASR_ACK)) {
1224 saved_data = NCR5380_read(INPUT_DATA_REG);
1225 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001226 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001227 }
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001230
Finn Thaind65e6342014-03-18 11:42:20 +11001231 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001232 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1233 NCR5380_read(STATUS_REG));
1234
Finn Thaine3f463b2014-11-12 16:12:16 +11001235#if defined(CONFIG_SUN3)
1236 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1237 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1238 instance->host_no);
1239 BUG();
1240 }
1241
1242 /* make sure we're not stuck in a data phase */
1243 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1244 (BASR_PHASE_MATCH | BASR_ACK)) {
1245 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1246 NCR5380_read(BUS_AND_STATUS_REG));
1247 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1248 instance->host_no);
1249 BUG();
1250 }
1251#endif
1252
Roman Zippelc28bda22007-05-01 22:32:36 +02001253 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1254 NCR5380_write(MODE_REG, MR_BASE);
1255 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1256
Finn Thain01bd9082014-11-12 16:12:23 +11001257 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001258 hostdata->dma_len = 0;
1259
1260 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1261 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001262 *data += transferred;
1263 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001264
Finn Thainef1081c2014-11-12 16:12:14 +11001265 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001266 int cnt, toPIO;
1267
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001269 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001270 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001271 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001272 *(*data)++ = saved_data;
1273 (*count)--;
1274 cnt--;
1275 toPIO--;
1276 }
Finn Thaind65e6342014-03-18 11:42:20 +11001277 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001278 NCR5380_transfer_pio(instance, &p, &cnt, data);
1279 *count -= toPIO - cnt;
1280 }
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283#endif /* REAL_DMA */
1284
1285
Finn Thainff50f9e2014-11-12 16:12:18 +11001286/**
1287 * NCR5380_intr - generic NCR5380 irq handler
1288 * @irq: interrupt number
1289 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001290 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001291 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1292 * from the disconnected queue, and restarting NCR5380_main()
1293 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
1295
Roman Zippelc28bda22007-05-01 22:32:36 +02001296static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Finn Thaina53a21e2014-11-12 16:12:21 +11001298 struct Scsi_Host *instance = dev_id;
Roman Zippelc28bda22007-05-01 22:32:36 +02001299 int done = 1, handled = 0;
1300 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Finn Thaind65e6342014-03-18 11:42:20 +11001302 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Roman Zippelc28bda22007-05-01 22:32:36 +02001304 /* Look for pending interrupts */
1305 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001306 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001307 /* dispatch to appropriate routine if found and done=0 */
1308 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001309 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001310 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1311 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001312 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001313 NCR5380_reselect(instance);
1314 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1315 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001316 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001317 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1318 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001319 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001320 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1321 } else {
1322 /*
1323 * The rest of the interrupt conditions can occur only during a
1324 * DMA transfer
1325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001328 /*
1329 * We should only get PHASE MISMATCH and EOP interrupts if we have
1330 * DMA enabled, so do a sanity check based on the current setting
1331 * of the MODE register.
1332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Roman Zippelc28bda22007-05-01 22:32:36 +02001334 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1335 ((basr & BASR_END_DMA_TRANSFER) ||
1336 !(basr & BASR_PHASE_MATCH))) {
1337
Finn Thaind65e6342014-03-18 11:42:20 +11001338 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001339 NCR5380_dma_complete( instance );
1340 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001341 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001343 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001345 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001346 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001347 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1348 HOSTNO, basr, NCR5380_read(MODE_REG),
1349 NCR5380_read(STATUS_REG));
1350 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001351#ifdef SUN3_SCSI_VME
1352 dregs->csr |= CSR_DMA_ENABLE;
1353#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001354 }
1355 } /* if !(SELECTION || PARITY) */
1356 handled = 1;
1357 } /* BASR & IRQ */ else {
1358 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1359 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1360 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1361 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001362#ifdef SUN3_SCSI_VME
1363 dregs->csr |= CSR_DMA_ENABLE;
1364#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001365 }
1366
1367 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001368 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001369 /* Put a call to NCR5380_main() on the queue... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001370 queue_main(shost_priv(instance));
Roman Zippelc28bda22007-05-01 22:32:36 +02001371 }
1372 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Roman Zippelc28bda22007-05-01 22:32:36 +02001375/*
Finn Thain710ddd02014-11-12 16:12:02 +11001376 * Function : int NCR5380_select(struct Scsi_Host *instance,
1377 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 *
1379 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001380 * including ARBITRATION, SELECTION, and initial message out for
1381 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001383 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001384 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001386 * Returns : -1 if selection could not execute for some reason,
1387 * 0 if selection succeeded or failed because the target
1388 * did not respond.
1389 *
1390 * Side effects :
1391 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * with registers as they should have been on entry - ie
1393 * SELECT_ENABLE will be set appropriately, the NCR5380
1394 * will cease to drive any SCSI bus signals.
1395 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001396 * If successful : I_T_L or I_T_L_Q nexus will be established,
1397 * instance->connected will be set to cmd.
1398 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001400 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * cmd->result host byte set to DID_BAD_TARGET.
1402 */
1403
Finn Thain710ddd02014-11-12 16:12:02 +11001404static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Roman Zippelc28bda22007-05-01 22:32:36 +02001406 SETUP_HOSTDATA(instance);
1407 unsigned char tmp[3], phase;
1408 unsigned char *data;
1409 int len;
1410 unsigned long timeout;
1411 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Roman Zippelc28bda22007-05-01 22:32:36 +02001413 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001414 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001415 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001416 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Roman Zippelc28bda22007-05-01 22:32:36 +02001418 /*
1419 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1420 * data bus during SELECTION.
1421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Roman Zippelc28bda22007-05-01 22:32:36 +02001423 local_irq_save(flags);
1424 if (hostdata->connected) {
1425 local_irq_restore(flags);
1426 return -1;
1427 }
1428 NCR5380_write(TARGET_COMMAND_REG, 0);
1429
1430 /*
1431 * Start arbitration.
1432 */
1433
1434 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1435 NCR5380_write(MODE_REG, MR_ARBITRATE);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Roman Zippelc28bda22007-05-01 22:32:36 +02001439 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001440#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001441 {
1442 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Roman Zippelc28bda22007-05-01 22:32:36 +02001444 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1445 time_before(jiffies, timeout) && !hostdata->connected)
1446 ;
1447 if (time_after_eq(jiffies, timeout)) {
1448 printk("scsi : arbitration timeout at %d\n", __LINE__);
1449 NCR5380_write(MODE_REG, MR_BASE);
1450 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1451 return -1;
1452 }
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001455 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1456 !hostdata->connected)
1457 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458#endif
1459
Finn Thaind65e6342014-03-18 11:42:20 +11001460 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Roman Zippelc28bda22007-05-01 22:32:36 +02001462 if (hostdata->connected) {
1463 NCR5380_write(MODE_REG, MR_BASE);
1464 return -1;
1465 }
1466 /*
1467 * The arbitration delay is 2.2us, but this is a minimum and there is
1468 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1469 * the integral nature of udelay().
1470 *
1471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Roman Zippelc28bda22007-05-01 22:32:36 +02001473 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Roman Zippelc28bda22007-05-01 22:32:36 +02001475 /* Check for lost arbitration */
1476 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1477 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1478 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1479 hostdata->connected) {
1480 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001481 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001482 HOSTNO);
1483 return -1;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Roman Zippelc28bda22007-05-01 22:32:36 +02001486 /* after/during arbitration, BSY should be asserted.
1487 IBM DPES-31080 Version S31Q works now */
1488 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1489 NCR5380_write(INITIATOR_COMMAND_REG,
1490 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Roman Zippelc28bda22007-05-01 22:32:36 +02001492 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1493 hostdata->connected) {
1494 NCR5380_write(MODE_REG, MR_BASE);
1495 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001496 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001497 HOSTNO);
1498 return -1;
1499 }
1500
1501 /*
1502 * Again, bus clear + bus settle time is 1.2us, however, this is
1503 * a minimum so we'll udelay ceil(1.2)
1504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Finn Thain9c3f0e22016-01-03 16:05:11 +11001506 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1507 udelay(15);
1508 else
1509 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001510
1511 if (hostdata->connected) {
1512 NCR5380_write(MODE_REG, MR_BASE);
1513 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1514 return -1;
1515 }
1516
Finn Thaind65e6342014-03-18 11:42:20 +11001517 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001518
1519 /*
1520 * Now that we have won arbitration, start Selection process, asserting
1521 * the host and target ID's on the SCSI bus.
1522 */
1523
1524 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1525
1526 /*
1527 * Raise ATN while SEL is true before BSY goes false from arbitration,
1528 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1529 * phase immediately after selection.
1530 */
1531
1532 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1533 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Roman Zippelc28bda22007-05-01 22:32:36 +02001536 /*
1537 * Reselect interrupts must be turned off prior to the dropping of BSY,
1538 * otherwise we will trigger an interrupt.
1539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Roman Zippelc28bda22007-05-01 22:32:36 +02001541 if (hostdata->connected) {
1542 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1543 return -1;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Roman Zippelc28bda22007-05-01 22:32:36 +02001546 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Roman Zippelc28bda22007-05-01 22:32:36 +02001548 /*
1549 * The initiator shall then wait at least two deskew delays and release
1550 * the BSY signal.
1551 */
1552 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Roman Zippelc28bda22007-05-01 22:32:36 +02001554 /* Reset BSY */
1555 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1556 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Roman Zippelc28bda22007-05-01 22:32:36 +02001558 /*
1559 * Something weird happens when we cease to drive BSY - looks
1560 * like the board/chip is letting us do another read before the
1561 * appropriate propagation delay has expired, and we're confusing
1562 * a BSY signal from ourselves as the target's response to SELECTION.
1563 *
1564 * A small delay (the 'C++' frontend breaks the pipeline with an
1565 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1566 * tighter 'C' code breaks and requires this) solves the problem -
1567 * the 1 us delay is arbitrary, and only used because this delay will
1568 * be the same on other platforms and since it works here, it should
1569 * work there.
1570 *
1571 * wingel suggests that this could be due to failing to wait
1572 * one deskew delay.
1573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Roman Zippelc28bda22007-05-01 22:32:36 +02001575 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Finn Thaind65e6342014-03-18 11:42:20 +11001577 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Roman Zippelc28bda22007-05-01 22:32:36 +02001579 /*
1580 * The SCSI specification calls for a 250 ms timeout for the actual
1581 * selection.
1582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -05001584 timeout = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Roman Zippelc28bda22007-05-01 22:32:36 +02001586 /*
1587 * XXX very interesting - we're seeing a bounce where the BSY we
1588 * asserted is being reflected / still asserted (propagation delay?)
1589 * and it's detecting as true. Sigh.
1590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001593 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1594 * IO while SEL is true. But again, there are some disks out the in the
1595 * world that do that nevertheless. (Somebody claimed that this announces
1596 * reselection capability of the target.) So we better skip that test and
1597 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Roman Zippelc28bda22007-05-01 22:32:36 +02001600 while (time_before(jiffies, timeout) &&
1601 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1602 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Roman Zippelc28bda22007-05-01 22:32:36 +02001604 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1605 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1606 NCR5380_reselect(instance);
1607 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1608 HOSTNO);
1609 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1610 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001613 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1614 ;
1615#endif
1616
1617 /*
1618 * No less than two deskew delays after the initiator detects the
1619 * BSY signal is true, it shall release the SEL signal and may
1620 * change the DATA BUS. -wingel
1621 */
1622
1623 udelay(1);
1624
1625 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1626
1627 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1628 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1629 if (hostdata->targets_present & (1 << cmd->device->id)) {
1630 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1631 if (hostdata->restart_select)
1632 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001633 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001634 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1635 return -1;
1636 }
1637 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001638#ifdef SUPPORT_TAGS
1639 cmd_free_tag(cmd);
1640#endif
1641 cmd->scsi_done(cmd);
1642 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001643 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001644 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1645 return 0;
1646 }
1647
1648 hostdata->targets_present |= (1 << cmd->device->id);
1649
1650 /*
1651 * Since we followed the SCSI spec, and raised ATN while SEL
1652 * was true but before BSY was false during selection, the information
1653 * transfer phase should be a MESSAGE OUT phase so that we can send the
1654 * IDENTIFY message.
1655 *
1656 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1657 * message (2 bytes) with a tag ID that we increment with every command
1658 * until it wraps back to 0.
1659 *
1660 * XXX - it turns out that there are some broken SCSI-II devices,
1661 * which claim to support tagged queuing but fail when more than
1662 * some number of commands are issued at once.
1663 */
1664
1665 /* Wait for start of REQ/ACK handshake */
1666 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1667 ;
1668
Finn Thaind65e6342014-03-18 11:42:20 +11001669 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001670 HOSTNO, cmd->device->id);
1671 tmp[0] = IDENTIFY(1, cmd->device->lun);
1672
1673#ifdef SUPPORT_TAGS
1674 if (cmd->tag != TAG_NONE) {
1675 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1676 tmp[2] = cmd->tag;
1677 len = 3;
1678 } else
1679 len = 1;
1680#else
1681 len = 1;
1682 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683#endif /* SUPPORT_TAGS */
1684
Roman Zippelc28bda22007-05-01 22:32:36 +02001685 /* Send message(s) */
1686 data = tmp;
1687 phase = PHASE_MSGOUT;
1688 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001689 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001690 /* XXX need to handle errors here */
1691 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001693 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1694#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001695#ifdef SUN3_SCSI_VME
1696 dregs->csr |= CSR_INTR;
1697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Roman Zippelc28bda22007-05-01 22:32:36 +02001699 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Roman Zippelc28bda22007-05-01 22:32:36 +02001701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Roman Zippelc28bda22007-05-01 22:32:36 +02001704/*
1705 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * unsigned char *phase, int *count, unsigned char **data)
1707 *
1708 * Purpose : transfers data in given phase using polled I/O
1709 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001710 * Inputs : instance - instance of driver, *phase - pointer to
1711 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001713 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001715 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 * is in same phase.
1717 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001718 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *
1720 * XXX Note : handling for bus free may be useful.
1721 */
1722
1723/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001724 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * IS 100% reliable, and for the actual data transfer where speed
1726 * counts, we will always do a pseudo DMA or DMA transfer.
1727 */
1728
Roman Zippelc28bda22007-05-01 22:32:36 +02001729static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1730 unsigned char *phase, int *count,
1731 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Roman Zippelc28bda22007-05-01 22:32:36 +02001733 register unsigned char p = *phase, tmp;
1734 register int c = *count;
1735 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Roman Zippelc28bda22007-05-01 22:32:36 +02001737 /*
1738 * The NCR5380 chip will only drive the SCSI bus when the
1739 * phase specified in the appropriate bits of the TARGET COMMAND
1740 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
1742
Roman Zippelc28bda22007-05-01 22:32:36 +02001743 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Roman Zippelc28bda22007-05-01 22:32:36 +02001745 do {
1746 /*
1747 * Wait for assertion of REQ, after which the phase bits will be
1748 * valid
1749 */
1750 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1751 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Finn Thaind65e6342014-03-18 11:42:20 +11001753 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Roman Zippelc28bda22007-05-01 22:32:36 +02001755 /* Check for phase mismatch */
1756 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001757 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001758 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001759 break;
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Roman Zippelc28bda22007-05-01 22:32:36 +02001762 /* Do actual transfer from SCSI bus to / from memory */
1763 if (!(p & SR_IO))
1764 NCR5380_write(OUTPUT_DATA_REG, *d);
1765 else
1766 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Roman Zippelc28bda22007-05-01 22:32:36 +02001768 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Roman Zippelc28bda22007-05-01 22:32:36 +02001770 /*
1771 * The SCSI standard suggests that in MSGOUT phase, the initiator
1772 * should drop ATN on the last byte of the message phase
1773 * after REQ has been asserted for the handshake but before
1774 * the initiator raises ACK.
1775 */
1776
1777 if (!(p & SR_IO)) {
1778 if (!((p & SR_MSG) && c > 1)) {
1779 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001780 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001781 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1782 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1783 } else {
1784 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1785 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001786 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001787 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1788 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1789 }
1790 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001791 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001792 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1793 }
1794
1795 while (NCR5380_read(STATUS_REG) & SR_REQ)
1796 ;
1797
Finn Thaind65e6342014-03-18 11:42:20 +11001798 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001799
1800 /*
1801 * We have several special cases to consider during REQ/ACK handshaking :
1802 * 1. We were in MSGOUT phase, and we are on the last byte of the
1803 * message. ATN must be dropped as ACK is dropped.
1804 *
1805 * 2. We are in a MSGIN phase, and we are on the last byte of the
1806 * message. We must exit with ACK asserted, so that the calling
1807 * code may raise ATN before dropping ACK to reject the message.
1808 *
1809 * 3. ACK and ATN are clear and the target may proceed as normal.
1810 */
1811 if (!(p == PHASE_MSGIN && c == 1)) {
1812 if (p == PHASE_MSGOUT && c > 1)
1813 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1814 else
1815 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1816 }
1817 } while (--c);
1818
Finn Thaind65e6342014-03-18 11:42:20 +11001819 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001820
1821 *count = c;
1822 *data = d;
1823 tmp = NCR5380_read(STATUS_REG);
1824 /* The phase read from the bus is valid if either REQ is (already)
1825 * asserted or if ACK hasn't been released yet. The latter is the case if
1826 * we're in MSGIN and all wanted bytes have been received.
1827 */
1828 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1829 *phase = tmp & PHASE_MASK;
1830 else
1831 *phase = PHASE_UNKNOWN;
1832
1833 if (!c || (*phase == p))
1834 return 0;
1835 else
1836 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Finn Thain636b1ec2016-01-03 16:05:10 +11001839/**
1840 * do_reset - issue a reset command
1841 * @instance: adapter to reset
1842 *
1843 * Issue a reset sequence to the NCR5380 and try and get the bus
1844 * back into sane shape.
1845 *
1846 * This clears the reset interrupt flag because there may be no handler for
1847 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1848 * been installed. And when in EH we may have released the ST DMA interrupt.
1849 */
1850
1851static void do_reset(struct Scsi_Host *instance)
1852{
1853 unsigned long flags;
1854
1855 local_irq_save(flags);
1856 NCR5380_write(TARGET_COMMAND_REG,
1857 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1858 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1859 udelay(50);
1860 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1861 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1862 local_irq_restore(flags);
1863}
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
1866 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001867 *
1868 * Purpose : abort the currently established nexus. Should only be
1869 * called from a routine which can drop into a
1870 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * Returns : 0 on success, -1 on failure.
1872 */
1873
Finn Thaina53a21e2014-11-12 16:12:21 +11001874static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Roman Zippelc28bda22007-05-01 22:32:36 +02001876 unsigned char tmp, *msgptr, phase;
1877 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Roman Zippelc28bda22007-05-01 22:32:36 +02001879 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Roman Zippelc28bda22007-05-01 22:32:36 +02001882 /*
1883 * Wait for the target to indicate a valid phase by asserting
1884 * REQ. Once this happens, we'll have either a MSGOUT phase
1885 * and can immediately send the ABORT message, or we'll have some
1886 * other phase and will have to source/sink data.
1887 *
1888 * We really don't care what value was on the bus or what value
1889 * the target sees, so we just handshake.
1890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Roel Kluin3be38e72007-11-15 21:13:46 +01001892 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001893 ;
1894
1895 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1896
1897 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1898 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1899 ICR_ASSERT_ACK);
1900 while (NCR5380_read(STATUS_REG) & SR_REQ)
1901 ;
1902 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1903 }
1904
1905 tmp = ABORT;
1906 msgptr = &tmp;
1907 len = 1;
1908 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001909 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001910
1911 /*
1912 * If we got here, and the command completed successfully,
1913 * we're about to go into bus free state.
1914 */
1915
1916 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
1919#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001920/*
1921 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * unsigned char *phase, int *count, unsigned char **data)
1923 *
1924 * Purpose : transfers data in given phase using either real
1925 * or pseudo DMA.
1926 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001927 * Inputs : instance - instance of driver, *phase - pointer to
1928 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001930 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001932 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 * is in same phase.
1934 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001935 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 *
1937 */
1938
1939
Roman Zippelc28bda22007-05-01 22:32:36 +02001940static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1941 unsigned char *phase, int *count,
1942 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Roman Zippelc28bda22007-05-01 22:32:36 +02001944 SETUP_HOSTDATA(instance);
1945 register int c = *count;
1946 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001947 unsigned long flags;
1948
1949#if defined(CONFIG_SUN3)
1950 /* sanity check */
1951 if (!sun3_dma_setup_done) {
1952 pr_err("scsi%d: transfer_dma without setup!\n",
1953 instance->host_no);
1954 BUG();
1955 }
1956 hostdata->dma_len = c;
1957
1958 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1959 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1960 c, (p & SR_IO) ? "to" : "from", *data);
1961
1962 /* netbsd turns off ints here, why not be safe and do it too */
1963 local_irq_save(flags);
1964
1965 /* send start chain */
1966 sun3scsi_dma_start(c, *data);
1967
1968 if (p & SR_IO) {
1969 NCR5380_write(TARGET_COMMAND_REG, 1);
1970 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1971 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1972 NCR5380_write(MODE_REG,
1973 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1974 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1975 } else {
1976 NCR5380_write(TARGET_COMMAND_REG, 0);
1977 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1978 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1979 NCR5380_write(MODE_REG,
1980 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1981 NCR5380_write(START_DMA_SEND_REG, 0);
1982 }
1983
1984#ifdef SUN3_SCSI_VME
1985 dregs->csr |= CSR_DMA_ENABLE;
1986#endif
1987
1988 local_irq_restore(flags);
1989
1990 sun3_dma_active = 1;
1991
1992#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001993 register unsigned char *d = *data;
1994 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Roman Zippelc28bda22007-05-01 22:32:36 +02001996 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1997 *phase = tmp;
1998 return -1;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Finn Thainef1081c2014-11-12 16:12:14 +11002001 if (hostdata->read_overruns && (p & SR_IO))
2002 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Finn Thaind65e6342014-03-18 11:42:20 +11002004 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002005 HOSTNO, (p & SR_IO) ? "reading" : "writing",
2006 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Roman Zippelc28bda22007-05-01 22:32:36 +02002008 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002011 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012#endif /* def REAL_DMA */
2013
Finn Thainef1081c2014-11-12 16:12:14 +11002014 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002015 /* On the Medusa, it is a must to initialize the DMA before
2016 * starting the NCR. This is also the cleaner way for the TT.
2017 */
2018 local_irq_save(flags);
2019 hostdata->dma_len = (p & SR_IO) ?
2020 NCR5380_dma_read_setup(instance, d, c) :
2021 NCR5380_dma_write_setup(instance, d, c);
2022 local_irq_restore(flags);
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Roman Zippelc28bda22007-05-01 22:32:36 +02002025 if (p & SR_IO)
2026 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2027 else {
2028 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2029 NCR5380_write(START_DMA_SEND_REG, 0);
2030 }
2031
Finn Thainef1081c2014-11-12 16:12:14 +11002032 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002033 /* On the Falcon, the DMA setup must be done after the last */
2034 /* NCR access, else the DMA setup gets trashed!
2035 */
2036 local_irq_save(flags);
2037 hostdata->dma_len = (p & SR_IO) ?
2038 NCR5380_dma_read_setup(instance, d, c) :
2039 NCR5380_dma_write_setup(instance, d, c);
2040 local_irq_restore(flags);
2041 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002042#endif /* !defined(CONFIG_SUN3) */
2043
Roman Zippelc28bda22007-05-01 22:32:36 +02002044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046#endif /* defined(REAL_DMA) */
2047
2048/*
2049 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2050 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002051 * Purpose : run through the various SCSI phases and do as the target
2052 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * instance->connected.
2054 *
2055 * Inputs : instance, instance for which we are doing commands
2056 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002057 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 * modified if a command disconnects, *instance->connected will
2059 * change.
2060 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002061 * XXX Note : we need to watch for bus free or a reset condition here
2062 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Roman Zippelc28bda22007-05-01 22:32:36 +02002064
2065static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Roman Zippelc28bda22007-05-01 22:32:36 +02002067 SETUP_HOSTDATA(instance);
2068 unsigned long flags;
2069 unsigned char msgout = NOP;
2070 int sink = 0;
2071 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02002073 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002075 unsigned char *data;
2076 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11002077 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Finn Thaine3f463b2014-11-12 16:12:16 +11002079#ifdef SUN3_SCSI_VME
2080 dregs->csr |= CSR_INTR;
2081#endif
2082
Roman Zippelc28bda22007-05-01 22:32:36 +02002083 while (1) {
2084 tmp = NCR5380_read(STATUS_REG);
2085 /* We only have a valid SCSI phase when REQ is asserted */
2086 if (tmp & SR_REQ) {
2087 phase = (tmp & PHASE_MASK);
2088 if (phase != old_phase) {
2089 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11002090 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002092#if defined(CONFIG_SUN3)
2093 if (phase == PHASE_CMDOUT) {
2094#if defined(REAL_DMA)
2095 void *d;
2096 unsigned long count;
2097
2098 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2099 count = cmd->SCp.buffer->length;
2100 d = sg_virt(cmd->SCp.buffer);
2101 } else {
2102 count = cmd->SCp.this_residual;
2103 d = cmd->SCp.ptr;
2104 }
2105 /* this command setup for dma yet? */
2106 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2107 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2108 sun3scsi_dma_setup(d, count,
2109 rq_data_dir(cmd->request));
2110 sun3_dma_setup_done = cmd;
2111 }
2112 }
2113#endif
2114#ifdef SUN3_SCSI_VME
2115 dregs->csr |= CSR_INTR;
2116#endif
2117 }
2118#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Roman Zippelc28bda22007-05-01 22:32:36 +02002120 if (sink && (phase != PHASE_MSGOUT)) {
2121 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Roman Zippelc28bda22007-05-01 22:32:36 +02002123 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2124 ICR_ASSERT_ACK);
2125 while (NCR5380_read(STATUS_REG) & SR_REQ)
2126 ;
2127 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2128 ICR_ASSERT_ATN);
2129 sink = 0;
2130 continue;
2131 }
2132
2133 switch (phase) {
2134 case PHASE_DATAOUT:
2135#if (NDEBUG & NDEBUG_NO_DATAOUT)
2136 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2137 "aborted\n", HOSTNO);
2138 sink = 1;
2139 do_abort(instance);
2140 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002141 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002142 return;
2143#endif
2144 case PHASE_DATAIN:
2145 /*
2146 * If there is no room left in the current buffer in the
2147 * scatter-gather list, move onto the next one.
2148 */
2149
2150 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2151 ++cmd->SCp.buffer;
2152 --cmd->SCp.buffers_residual;
2153 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002154 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002155 /* ++roman: Try to merge some scatter-buffers if
2156 * they are at contiguous physical addresses.
2157 */
2158 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002159 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002160 HOSTNO, cmd->SCp.this_residual,
2161 cmd->SCp.buffers_residual);
2162 }
2163
2164 /*
2165 * The preferred transfer method is going to be
2166 * PSEUDO-DMA for systems that are strictly PIO,
2167 * since we can let the hardware do the handshaking.
2168 *
2169 * For this to work, we need to know the transfersize
2170 * ahead of time, since the pseudo-DMA code will sit
2171 * in an unconditional loop.
2172 */
2173
2174 /* ++roman: I suggest, this should be
2175 * #if def(REAL_DMA)
2176 * instead of leaving REAL_DMA out.
2177 */
2178
2179#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002180 if (
2181#if !defined(CONFIG_SUN3)
2182 !cmd->device->borken &&
2183#endif
2184 (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002185 len = transfersize;
2186 cmd->SCp.phase = phase;
2187 if (NCR5380_transfer_dma(instance, &phase,
2188 &len, (unsigned char **)&cmd->SCp.ptr)) {
2189 /*
2190 * If the watchdog timer fires, all future
2191 * accesses to this device will use the
2192 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002193 scmd_printk(KERN_INFO, cmd,
2194 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002195 cmd->device->borken = 1;
2196 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2197 ICR_ASSERT_ATN);
2198 sink = 1;
2199 do_abort(instance);
2200 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002201 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002202 /* XXX - need to source or sink data here, as appropriate */
2203 } else {
2204#ifdef REAL_DMA
2205 /* ++roman: When using real DMA,
2206 * information_transfer() should return after
2207 * starting DMA since it has nothing more to
2208 * do.
2209 */
2210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002212 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002214 }
2215 } else
2216#endif /* defined(REAL_DMA) */
2217 NCR5380_transfer_pio(instance, &phase,
2218 (int *)&cmd->SCp.this_residual,
2219 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002220#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2221 /* if we had intended to dma that command clear it */
2222 if (sun3_dma_setup_done == cmd)
2223 sun3_dma_setup_done = NULL;
2224#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002225 break;
2226 case PHASE_MSGIN:
2227 len = 1;
2228 data = &tmp;
2229 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2230 NCR5380_transfer_pio(instance, &phase, &len, &data);
2231 cmd->SCp.Message = tmp;
2232
2233 switch (tmp) {
2234 /*
2235 * Linking lets us reduce the time required to get the
2236 * next command out to the device, hopefully this will
2237 * mean we don't waste another revolution due to the delays
2238 * required by ARBITRATION and another SELECTION.
2239 *
2240 * In the current implementation proposal, low level drivers
2241 * merely have to start the next command, pointed to by
2242 * next_link, done() is called as with unlinked commands.
2243 */
2244#ifdef LINKED
2245 case LINKED_CMD_COMPLETE:
2246 case LINKED_FLG_CMD_COMPLETE:
2247 /* Accept message by clearing ACK */
2248 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2249
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002250 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002251 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2252
2253 /* Enable reselect interrupts */
2254 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2255 /*
2256 * Sanity check : A linked command should only terminate
2257 * with one of these messages if there are more linked
2258 * commands available.
2259 */
2260
2261 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002262 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002263 "linked command complete, no next_link\n",
2264 HOSTNO, cmd->device->id, cmd->device->lun);
2265 sink = 1;
2266 do_abort(instance);
2267 return;
2268 }
2269
2270 initialize_SCp(cmd->next_link);
2271 /* The next command is still part of this process; copy it
2272 * and don't free it! */
2273 cmd->next_link->tag = cmd->tag;
2274 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002275 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002276 "done, calling scsi_done().\n",
2277 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002278 cmd->scsi_done(cmd);
2279 cmd = hostdata->connected;
2280 break;
2281#endif /* def LINKED */
2282 case ABORT:
2283 case COMMAND_COMPLETE:
2284 /* Accept message by clearing ACK */
2285 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002286 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002287 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002288
2289 local_irq_save(flags);
2290 hostdata->retain_dma_intr++;
2291 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002292#ifdef SUPPORT_TAGS
2293 cmd_free_tag(cmd);
2294 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2295 /* Turn a QUEUE FULL status into BUSY, I think the
2296 * mid level cannot handle QUEUE FULL :-( (The
2297 * command is retried after BUSY). Also update our
2298 * queue size to the number of currently issued
2299 * commands now.
2300 */
2301 /* ++Andreas: the mid level code knows about
2302 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002303 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002304 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002305 "QUEUE_FULL after %d commands\n",
2306 HOSTNO, cmd->device->id, cmd->device->lun,
2307 ta->nr_allocated);
2308 if (ta->queue_size > ta->nr_allocated)
2309 ta->nr_allocated = ta->queue_size;
2310 }
2311#else
2312 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2313#endif
2314 /* Enable reselect interrupts */
2315 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2316
2317 /*
2318 * I'm not sure what the correct thing to do here is :
2319 *
2320 * If the command that just executed is NOT a request
2321 * sense, the obvious thing to do is to set the result
2322 * code to the values of the stored parameters.
2323 *
2324 * If it was a REQUEST SENSE command, we need some way to
2325 * differentiate between the failure code of the original
2326 * and the failure code of the REQUEST sense - the obvious
2327 * case is success, where we fall through and leave the
2328 * result code unchanged.
2329 *
2330 * The non-obvious place is where the REQUEST SENSE failed
2331 */
2332
2333 if (cmd->cmnd[0] != REQUEST_SENSE)
2334 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2335 else if (status_byte(cmd->SCp.Status) != GOOD)
2336 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2337
Boaz Harrosh28424d32007-09-10 22:37:45 +03002338 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2339 hostdata->ses.cmd_len) {
2340 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2341 hostdata->ses.cmd_len = 0 ;
2342 }
2343
Roman Zippelc28bda22007-05-01 22:32:36 +02002344 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2345 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002346 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002347
Finn Thaind65e6342014-03-18 11:42:20 +11002348 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002349
Roman Zippelc28bda22007-05-01 22:32:36 +02002350 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002351 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002352 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002353 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002354 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002355 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002356 cmd->scsi_done(cmd);
2357 }
2358
Finn Thaine3c3da62014-11-12 16:12:15 +11002359 local_irq_restore(flags);
2360
Roman Zippelc28bda22007-05-01 22:32:36 +02002361 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2362 /*
2363 * Restore phase bits to 0 so an interrupted selection,
2364 * arbitration can resume.
2365 */
2366 NCR5380_write(TARGET_COMMAND_REG, 0);
2367
2368 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2369 barrier();
2370
Finn Thaine3c3da62014-11-12 16:12:15 +11002371 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002372 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002373 /* ++roman: For Falcon SCSI, release the lock on the
2374 * ST-DMA here if no other commands are waiting on the
2375 * disconnected queue.
2376 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002377 maybe_release_dma_irq(instance);
2378 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002379 return;
2380 case MESSAGE_REJECT:
2381 /* Accept message by clearing ACK */
2382 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2383 /* Enable reselect interrupts */
2384 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2385 switch (hostdata->last_message) {
2386 case HEAD_OF_QUEUE_TAG:
2387 case ORDERED_QUEUE_TAG:
2388 case SIMPLE_QUEUE_TAG:
2389 /* The target obviously doesn't support tagged
2390 * queuing, even though it announced this ability in
2391 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2392 * clear 'tagged_supported' and lock the LUN, since
2393 * the command is treated as untagged further on.
2394 */
2395 cmd->device->tagged_supported = 0;
2396 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2397 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002398 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002399 "QUEUE_TAG message; tagged queuing "
2400 "disabled\n",
2401 HOSTNO, cmd->device->id, cmd->device->lun);
2402 break;
2403 }
2404 break;
2405 case DISCONNECT:
2406 /* Accept message by clearing ACK */
2407 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2408 local_irq_save(flags);
2409 cmd->device->disconnect = 1;
2410 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002411 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002412 hostdata->connected = NULL;
2413 hostdata->disconnected_queue = cmd;
2414 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002415 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002416 "moved from connected to the "
2417 "disconnected_queue\n", HOSTNO,
2418 cmd->device->id, cmd->device->lun);
2419 /*
2420 * Restore phase bits to 0 so an interrupted selection,
2421 * arbitration can resume.
2422 */
2423 NCR5380_write(TARGET_COMMAND_REG, 0);
2424
2425 /* Enable reselect interrupts */
2426 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2427 /* Wait for bus free to avoid nasty timeouts */
2428 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2429 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002430#ifdef SUN3_SCSI_VME
2431 dregs->csr |= CSR_DMA_ENABLE;
2432#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002433 return;
2434 /*
2435 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2436 * operation, in violation of the SCSI spec so we can safely
2437 * ignore SAVE/RESTORE pointers calls.
2438 *
2439 * Unfortunately, some disks violate the SCSI spec and
2440 * don't issue the required SAVE_POINTERS message before
2441 * disconnecting, and we have to break spec to remain
2442 * compatible.
2443 */
2444 case SAVE_POINTERS:
2445 case RESTORE_POINTERS:
2446 /* Accept message by clearing ACK */
2447 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2448 /* Enable reselect interrupts */
2449 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2450 break;
2451 case EXTENDED_MESSAGE:
2452 /*
2453 * Extended messages are sent in the following format :
2454 * Byte
2455 * 0 EXTENDED_MESSAGE == 1
2456 * 1 length (includes one byte for code, doesn't
2457 * include first two bytes)
2458 * 2 code
2459 * 3..length+1 arguments
2460 *
2461 * Start the extended message buffer with the EXTENDED_MESSAGE
2462 * byte, since spi_print_msg() wants the whole thing.
2463 */
2464 extended_msg[0] = EXTENDED_MESSAGE;
2465 /* Accept first byte by clearing ACK */
2466 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2467
Finn Thaind65e6342014-03-18 11:42:20 +11002468 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002469
2470 len = 2;
2471 data = extended_msg + 1;
2472 phase = PHASE_MSGIN;
2473 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002474 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002475 (int)extended_msg[1], (int)extended_msg[2]);
2476
2477 if (!len && extended_msg[1] <=
2478 (sizeof(extended_msg) - 1)) {
2479 /* Accept third byte by clearing ACK */
2480 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2481 len = extended_msg[1] - 1;
2482 data = extended_msg + 3;
2483 phase = PHASE_MSGIN;
2484
2485 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002486 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002487 HOSTNO, len);
2488
2489 switch (extended_msg[2]) {
2490 case EXTENDED_SDTR:
2491 case EXTENDED_WDTR:
2492 case EXTENDED_MODIFY_DATA_POINTER:
2493 case EXTENDED_EXTENDED_IDENTIFY:
2494 tmp = 0;
2495 }
2496 } else if (len) {
2497 printk(KERN_NOTICE "scsi%d: error receiving "
2498 "extended message\n", HOSTNO);
2499 tmp = 0;
2500 } else {
2501 printk(KERN_NOTICE "scsi%d: extended message "
2502 "code %02x length %d is too long\n",
2503 HOSTNO, extended_msg[2], extended_msg[1]);
2504 tmp = 0;
2505 }
2506 /* Fall through to reject message */
2507
2508 /*
2509 * If we get something weird that we aren't expecting,
2510 * reject it.
2511 */
2512 default:
2513 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002514 printk(KERN_INFO "scsi%d: rejecting message ",
2515 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002516 spi_print_msg(extended_msg);
2517 printk("\n");
2518 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002519 scmd_printk(KERN_INFO, cmd,
2520 "rejecting unknown message %02x\n",
2521 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002522 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002523 scmd_printk(KERN_INFO, cmd,
2524 "rejecting unknown extended message code %02x, length %d\n",
2525 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002526
2527 msgout = MESSAGE_REJECT;
2528 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2529 break;
2530 } /* switch (tmp) */
2531 break;
2532 case PHASE_MSGOUT:
2533 len = 1;
2534 data = &msgout;
2535 hostdata->last_message = msgout;
2536 NCR5380_transfer_pio(instance, &phase, &len, &data);
2537 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002538 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002539#ifdef SUPPORT_TAGS
2540 cmd_free_tag(cmd);
2541#else
2542 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2543#endif
2544 hostdata->connected = NULL;
2545 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002546 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002547 maybe_release_dma_irq(instance);
2548 local_irq_restore(flags);
2549 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002550 return;
2551 }
2552 msgout = NOP;
2553 break;
2554 case PHASE_CMDOUT:
2555 len = cmd->cmd_len;
2556 data = cmd->cmnd;
2557 /*
2558 * XXX for performance reasons, on machines with a
2559 * PSEUDO-DMA architecture we should probably
2560 * use the dma transfer function.
2561 */
2562 NCR5380_transfer_pio(instance, &phase, &len, &data);
2563 break;
2564 case PHASE_STATIN:
2565 len = 1;
2566 data = &tmp;
2567 NCR5380_transfer_pio(instance, &phase, &len, &data);
2568 cmd->SCp.Status = tmp;
2569 break;
2570 default:
2571 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002572 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002573 } /* switch(phase) */
2574 } /* if (tmp * SR_REQ) */
2575 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
2578/*
2579 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2580 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002581 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002582 * 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 -07002583 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002584 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 * Inputs : instance - this instance of the NCR5380.
2586 *
2587 */
2588
2589
Finn Thaine3f463b2014-11-12 16:12:16 +11002590/* it might eventually prove necessary to do a dma setup on
2591 reselection, but it doesn't seem to be needed now -- sam */
2592
Roman Zippelc28bda22007-05-01 22:32:36 +02002593static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Roman Zippelc28bda22007-05-01 22:32:36 +02002595 SETUP_HOSTDATA(instance);
2596 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002597 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002599 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002601 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002602 int __maybe_unused len;
2603 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002604 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Roman Zippelc28bda22007-05-01 22:32:36 +02002606 /*
2607 * Disable arbitration, etc. since the host adapter obviously
2608 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Roman Zippelc28bda22007-05-01 22:32:36 +02002611 NCR5380_write(MODE_REG, MR_BASE);
2612 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Roman Zippelc28bda22007-05-01 22:32:36 +02002614 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2615
Finn Thaind65e6342014-03-18 11:42:20 +11002616 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002617
2618 /*
2619 * At this point, we have detected that our SCSI ID is on the bus,
2620 * SEL is true and BSY was false for at least one bus settle delay
2621 * (400 ns).
2622 *
2623 * We must assert BSY ourselves, until the target drops the SEL
2624 * signal.
2625 */
2626
2627 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2628
2629 while (NCR5380_read(STATUS_REG) & SR_SEL)
2630 ;
2631 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2632
2633 /*
2634 * Wait for target to go into MSGIN.
2635 */
2636
2637 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2638 ;
2639
Finn Thaine3f463b2014-11-12 16:12:16 +11002640#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2641 /* acknowledge toggle to MSGIN */
2642 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2643
2644 /* peek at the byte without really hitting the bus */
2645 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2646#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002647 len = 1;
2648 data = msg;
2649 phase = PHASE_MSGIN;
2650 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002651#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002652
2653 if (!(msg[0] & 0x80)) {
2654 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2655 spi_print_msg(msg);
2656 do_abort(instance);
2657 return;
2658 }
2659 lun = (msg[0] & 0x07);
2660
Finn Thaine3f463b2014-11-12 16:12:16 +11002661#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002662 /* If the phase is still MSGIN, the target wants to send some more
2663 * messages. In case it supports tagged queuing, this is probably a
2664 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2665 */
2666 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002667 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002668 /* Accept previous IDENTIFY message by clearing ACK */
2669 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2670 len = 2;
2671 data = msg + 1;
2672 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2673 msg[1] == SIMPLE_QUEUE_TAG)
2674 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002675 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002676 "reselection\n", HOSTNO, target_mask, lun, tag);
2677 }
2678#endif
2679
2680 /*
2681 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2682 * just reestablished, and remove it from the disconnected queue.
2683 */
2684
Finn Thain710ddd02014-11-12 16:12:02 +11002685 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002686 tmp; prev = tmp, tmp = NEXT(tmp)) {
2687 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2688#ifdef SUPPORT_TAGS
2689 && (tag == tmp->tag)
2690#endif
2691 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002692 if (prev) {
2693 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002694 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002695 } else {
2696 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2697 hostdata->disconnected_queue = NEXT(tmp);
2698 }
Roman Zippel3130d902007-05-01 22:32:37 +02002699 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002700 break;
2701 }
2702 }
2703
2704 if (!tmp) {
2705 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2706#ifdef SUPPORT_TAGS
2707 "tag %d "
2708#endif
2709 "not in disconnected_queue.\n",
2710 HOSTNO, target_mask, lun
2711#ifdef SUPPORT_TAGS
2712 , tag
2713#endif
2714 );
2715 /*
2716 * Since we have an established nexus that we can't do anything
2717 * with, we must abort it.
2718 */
2719 do_abort(instance);
2720 return;
2721 }
2722
Finn Thaine3f463b2014-11-12 16:12:16 +11002723#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2724 /* engage dma setup for the command we just saw */
2725 {
2726 void *d;
2727 unsigned long count;
2728
2729 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2730 count = tmp->SCp.buffer->length;
2731 d = sg_virt(tmp->SCp.buffer);
2732 } else {
2733 count = tmp->SCp.this_residual;
2734 d = tmp->SCp.ptr;
2735 }
2736 /* setup this command for dma if not already */
2737 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2738 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2739 sun3_dma_setup_done = tmp;
2740 }
2741 }
2742
2743 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2744#endif
2745
Roman Zippelc28bda22007-05-01 22:32:36 +02002746 /* Accept message by clearing ACK */
2747 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2748
Finn Thaine3f463b2014-11-12 16:12:16 +11002749#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2750 /* If the phase is still MSGIN, the target wants to send some more
2751 * messages. In case it supports tagged queuing, this is probably a
2752 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2753 */
2754 tag = TAG_NONE;
2755 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2756 /* Accept previous IDENTIFY message by clearing ACK */
2757 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2758 len = 2;
2759 data = msg + 1;
2760 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2761 msg[1] == SIMPLE_QUEUE_TAG)
2762 tag = msg[2];
2763 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2764 HOSTNO, target_mask, lun, tag);
2765 }
2766#endif
2767
Roman Zippelc28bda22007-05-01 22:32:36 +02002768 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002769 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002770 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
2773
2774/*
Finn Thain710ddd02014-11-12 16:12:02 +11002775 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 *
2777 * Purpose : abort a command
2778 *
Finn Thain710ddd02014-11-12 16:12:02 +11002779 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002780 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 * used.
2782 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002783 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002785 * XXX - there is no way to abort the command that is currently
2786 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002788 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 */
2790
2791static
Finn Thain710ddd02014-11-12 16:12:02 +11002792int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Roman Zippelc28bda22007-05-01 22:32:36 +02002794 struct Scsi_Host *instance = cmd->device->host;
2795 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002796 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002797 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002799 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Roman Zippelc28bda22007-05-01 22:32:36 +02002801 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Roman Zippelc28bda22007-05-01 22:32:36 +02002803 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Finn Thaind65e6342014-03-18 11:42:20 +11002805 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002806 NCR5380_read(BUS_AND_STATUS_REG),
2807 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002810 /*
2811 * Case 1 : If the command is the currently executing command,
2812 * we'll set the aborted flag and return control so that
2813 * information transfer routine can exit cleanly.
2814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Roman Zippelc28bda22007-05-01 22:32:36 +02002816 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Finn Thaind65e6342014-03-18 11:42:20 +11002818 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002819 /*
2820 * We should perform BSY checking, and make sure we haven't slipped
2821 * into BUS FREE.
2822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Roman Zippelc28bda22007-05-01 22:32:36 +02002824 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2825 /*
2826 * Since we can't change phases until we've completed the current
2827 * handshake, we have to source or sink a byte of data if the current
2828 * phase is not MSGOUT.
2829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Roman Zippelc28bda22007-05-01 22:32:36 +02002831 /*
2832 * Return control to the executing NCR drive so we can clear the
2833 * aborted flag and get back into our main loop.
2834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Roman Zippelc28bda22007-05-01 22:32:36 +02002836 if (do_abort(instance) == 0) {
2837 hostdata->aborted = 1;
2838 hostdata->connected = NULL;
2839 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002841 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002843 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002845 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002846 local_irq_restore(flags);
2847 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002848 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002849 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002850 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002851 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002852 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002856
2857 /*
2858 * Case 2 : If the command hasn't been issued yet, we simply remove it
2859 * from the issue queue.
2860 */
Finn Thain710ddd02014-11-12 16:12:02 +11002861 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2862 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002863 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2864 if (cmd == tmp) {
2865 REMOVE(5, *prev, tmp, NEXT(tmp));
2866 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002867 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002868 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002869 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002870 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002871 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002872 HOSTNO);
2873 /* Tagged queuing note: no tag to free here, hasn't been assigned
2874 * yet... */
2875 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002876 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878 }
2879
Roman Zippelc28bda22007-05-01 22:32:36 +02002880 /*
2881 * Case 3 : If any commands are connected, we're going to fail the abort
2882 * and let the high level SCSI driver retry at a later time or
2883 * issue a reset.
2884 *
2885 * Timeouts, and therefore aborted commands, will be highly unlikely
2886 * and handling them cleanly in this situation would make the common
2887 * case of noresets less efficient, and would pollute our code. So,
2888 * we fail.
2889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Roman Zippelc28bda22007-05-01 22:32:36 +02002891 if (hostdata->connected) {
2892 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002893 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002894 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Roman Zippelc28bda22007-05-01 22:32:36 +02002897 /*
2898 * Case 4: If the command is currently disconnected from the bus, and
2899 * there are no connected commands, we reconnect the I_T_L or
2900 * I_T_L_Q nexus associated with it, go into message out, and send
2901 * an abort message.
2902 *
2903 * This case is especially ugly. In order to reestablish the nexus, we
2904 * need to call NCR5380_select(). The easiest way to implement this
2905 * function was to abort if the bus was busy, and let the interrupt
2906 * handler triggered on the SEL for reselect take care of lost arbitrations
2907 * where necessary, meaning interrupts need to be enabled.
2908 *
2909 * When interrupts are enabled, the queues may change - so we
2910 * can't remove it from the disconnected queue before selecting it
2911 * because that could cause a failure in hashing the nexus if that
2912 * device reselected.
2913 *
2914 * Since the queues may change, we can't use the pointers from when we
2915 * first locate it.
2916 *
2917 * So, we must first locate the command, and if NCR5380_select()
2918 * succeeds, then issue the abort, relocate the command and remove
2919 * it from the disconnected queue.
2920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Finn Thain710ddd02014-11-12 16:12:02 +11002922 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002923 tmp = NEXT(tmp)) {
2924 if (cmd == tmp) {
2925 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002926 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002927
Finn Thain76f13b92014-11-12 16:11:53 +11002928 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002929 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002930
Finn Thaind65e6342014-03-18 11:42:20 +11002931 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002932
2933 do_abort(instance);
2934
2935 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002936 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2937 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002938 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2939 if (cmd == tmp) {
2940 REMOVE(5, *prev, tmp, NEXT(tmp));
2941 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002942 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002943 tmp->result = DID_ABORT << 16;
2944 /* We must unlock the tag/LUN immediately here, since the
2945 * target goes to BUS FREE and doesn't send us another
2946 * message (COMMAND_COMPLETE or the like)
2947 */
2948#ifdef SUPPORT_TAGS
2949 cmd_free_tag(tmp);
2950#else
2951 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2952#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002953 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002954 local_irq_restore(flags);
2955 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002956 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002957 }
2958 }
2959 }
2960 }
2961
Finn Thaine3c3da62014-11-12 16:12:15 +11002962 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2963 * possible at all) At least, we should check if the lock could be
2964 * released after the abort, in case it is kept due to some bug.
2965 */
2966 maybe_release_dma_irq(instance);
2967 local_irq_restore(flags);
2968
Roman Zippelc28bda22007-05-01 22:32:36 +02002969 /*
2970 * Case 5 : If we reached this point, the command was not found in any of
2971 * the queues.
2972 *
2973 * We probably reached this point because of an unlikely race condition
2974 * between the command completing successfully and the abortion code,
2975 * so we won't panic, but we will notify the user in case something really
2976 * broke.
2977 */
2978
Joe Perchesad361c92009-07-06 13:05:40 -07002979 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002980
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002981 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984
Roman Zippelc28bda22007-05-01 22:32:36 +02002985/*
Finn Thain710ddd02014-11-12 16:12:02 +11002986 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002987 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * Purpose : reset the SCSI bus.
2989 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002990 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Finn Thain710ddd02014-11-12 16:12:02 +11002994static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Finn Thaine3c3da62014-11-12 16:12:15 +11002996 struct Scsi_Host *instance = cmd->device->host;
2997 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002998 int i;
2999 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Finn Thaine3c3da62014-11-12 16:12:15 +11003001 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Roman Zippelc28bda22007-05-01 22:32:36 +02003003 /* get in phase */
3004 NCR5380_write(TARGET_COMMAND_REG,
3005 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
3006 /* assert RST */
3007 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
3008 udelay(40);
3009 /* reset NCR registers */
3010 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3011 NCR5380_write(MODE_REG, MR_BASE);
3012 NCR5380_write(TARGET_COMMAND_REG, 0);
3013 NCR5380_write(SELECT_ENABLE_REG, 0);
3014 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
3015 * through anymore ... */
3016 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Roman Zippelc28bda22007-05-01 22:32:36 +02003018 /* After the reset, there are no more connected or disconnected commands
3019 * and no busy units; so clear the low-level status here to avoid
3020 * conflicts when the mid-level code tries to wake up the affected
3021 * commands!
3022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Roman Zippelc28bda22007-05-01 22:32:36 +02003024 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003025 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003026 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11003027 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003028 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003029 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Roman Zippelc28bda22007-05-01 22:32:36 +02003031 local_irq_save(flags);
3032 hostdata->issue_queue = NULL;
3033 hostdata->connected = NULL;
3034 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11003036 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02003038 for (i = 0; i < 8; ++i)
3039 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02003041 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11003043
3044 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02003045 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003047 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}