blob: 59b378057198da4dda55b27afea16bca9b5bc482 [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/**
Finn Thain2f854b82016-01-03 16:05:22 +1100482 * NCR5380_poll_politely - wait for chip register value
Finn Thain636b1ec2016-01-03 16:05:10 +1100483 * @instance: controller to poll
484 * @reg: 5380 register to poll
485 * @bit: Bitmask to check
486 * @val: Value required to exit
Finn Thain2f854b82016-01-03 16:05:22 +1100487 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100488 *
Finn Thain2f854b82016-01-03 16:05:22 +1100489 * Polls the chip in a reasonably efficient manner waiting for an
490 * event to occur. After a short quick poll we begin to yield the CPU
491 * (if possible). In irq contexts the time-out is arbitrarily limited.
492 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100493 *
Finn Thain2f854b82016-01-03 16:05:22 +1100494 * Returns 0 if event occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100495 */
496
497static int NCR5380_poll_politely(struct Scsi_Host *instance,
Finn Thain2f854b82016-01-03 16:05:22 +1100498 int reg, int bit, int val, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100499{
Finn Thain2f854b82016-01-03 16:05:22 +1100500 struct NCR5380_hostdata *hostdata = shost_priv(instance);
501 unsigned long deadline = jiffies + wait;
502 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100503
Finn Thain2f854b82016-01-03 16:05:22 +1100504 /* Busy-wait for up to 10 ms */
505 n = min(10000U, jiffies_to_usecs(wait));
506 n *= hostdata->accesses_per_ms;
507 n /= 1000;
508 do {
509 if ((NCR5380_read(reg) & bit) == val)
Finn Thain636b1ec2016-01-03 16:05:10 +1100510 return 0;
511 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100512 } while (n--);
513
514 if (irqs_disabled() || in_interrupt())
515 return -ETIMEDOUT;
516
517 /* Repeatedly sleep for 1 ms until deadline */
518 while (time_is_after_jiffies(deadline)) {
519 schedule_timeout_uninterruptible(1);
520 if ((NCR5380_read(reg) & bit) == val)
521 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100522 }
523
Finn Thain636b1ec2016-01-03 16:05:10 +1100524 return -ETIMEDOUT;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#include <linux/delay.h>
528
529#if NDEBUG
530static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200531 unsigned char mask;
532 const char *name;
533} signals[] = {
534 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
535 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
536 { SR_SEL, "SEL" }, {0, NULL}
537}, basrs[] = {
538 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
539}, icrs[] = {
540 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
541 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
542 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
543 {0, NULL}
544}, mrs[] = {
545 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
546 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
547 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
548 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
549 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
550 {0, NULL}
551};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Finn Thainff50f9e2014-11-12 16:12:18 +1100553/**
554 * NCR5380_print - print scsi bus signals
555 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100557 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559
Roman Zippelc28bda22007-05-01 22:32:36 +0200560static void NCR5380_print(struct Scsi_Host *instance)
561{
562 unsigned char status, data, basr, mr, icr, i;
563 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Roman Zippelc28bda22007-05-01 22:32:36 +0200565 local_irq_save(flags);
566 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
567 status = NCR5380_read(STATUS_REG);
568 mr = NCR5380_read(MODE_REG);
569 icr = NCR5380_read(INITIATOR_COMMAND_REG);
570 basr = NCR5380_read(BUS_AND_STATUS_REG);
571 local_irq_restore(flags);
572 printk("STATUS_REG: %02x ", status);
573 for (i = 0; signals[i].mask; ++i)
574 if (status & signals[i].mask)
575 printk(",%s", signals[i].name);
576 printk("\nBASR: %02x ", basr);
577 for (i = 0; basrs[i].mask; ++i)
578 if (basr & basrs[i].mask)
579 printk(",%s", basrs[i].name);
580 printk("\nICR: %02x ", icr);
581 for (i = 0; icrs[i].mask; ++i)
582 if (icr & icrs[i].mask)
583 printk(",%s", icrs[i].name);
584 printk("\nMODE: %02x ", mr);
585 for (i = 0; mrs[i].mask; ++i)
586 if (mr & mrs[i].mask)
587 printk(",%s", mrs[i].name);
588 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200592 unsigned char value;
593 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200595 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
596 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
597 {PHASE_UNKNOWN, "UNKNOWN"}
598};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Finn Thainff50f9e2014-11-12 16:12:18 +1100600/**
601 * NCR5380_print_phase - show SCSI phase
602 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100604 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100606 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608
609static void NCR5380_print_phase(struct Scsi_Host *instance)
610{
Roman Zippelc28bda22007-05-01 22:32:36 +0200611 unsigned char status;
612 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Roman Zippelc28bda22007-05-01 22:32:36 +0200614 status = NCR5380_read(STATUS_REG);
615 if (!(status & SR_REQ))
616 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
617 else {
618 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
619 (phases[i].value != (status & PHASE_MASK)); ++i)
620 ;
621 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif
626
627/*
628 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * If we're not in an interrupt, we can call our main directly, it cannot be
631 * already running. Else, we queue it on a task queue, if not 'main_running'
632 * tells us that a lower level is already executing it. This way,
633 * 'main_running' needs not be protected in a special way.
634 *
635 * queue_main() is a utility function for putting our main onto the task
636 * queue, if main_running is false. It should be called only from a
637 * interrupt or bottom half.
638 */
639
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900640#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#include <linux/workqueue.h>
642#include <linux/interrupt.h>
643
Finn Thaina53a21e2014-11-12 16:12:21 +1100644static inline void queue_main(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Finn Thaina53a21e2014-11-12 16:12:21 +1100646 if (!hostdata->main_running) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200647 /* If in interrupt and NCR5380_main() not already running,
648 queue it on the 'immediate' task queue, to be processed
649 immediately after the current interrupt processing has
650 finished. */
Finn Thain0ad0eff2016-01-03 16:05:21 +1100651 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200652 }
653 /* else: nothing to do: the running NCR5380_main() will pick up
654 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Finn Thain8c325132014-11-12 16:11:58 +1100657/**
658 * NCR58380_info - report driver and host information
659 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
Finn Thain8c325132014-11-12 16:11:58 +1100661 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
Finn Thain8c325132014-11-12 16:11:58 +1100663 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
665
Finn Thain8c325132014-11-12 16:11:58 +1100666static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Finn Thain8c325132014-11-12 16:11:58 +1100668 struct NCR5380_hostdata *hostdata = shost_priv(instance);
669
670 return hostdata->info;
671}
672
673static void prepare_info(struct Scsi_Host *instance)
674{
675 struct NCR5380_hostdata *hostdata = shost_priv(instance);
676
677 snprintf(hostdata->info, sizeof(hostdata->info),
678 "%s, io_port 0x%lx, n_io_port %d, "
679 "base 0x%lx, irq %d, "
680 "can_queue %d, cmd_per_lun %d, "
681 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100682 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100683 "options { %s} ",
684 instance->hostt->name, instance->io_port, instance->n_io_port,
685 instance->base, instance->irq,
686 instance->can_queue, instance->cmd_per_lun,
687 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100688 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100689 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100690#ifdef DIFFERENTIAL
691 "DIFFERENTIAL "
692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100694 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#endif
696#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100697 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif
699#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100700 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701#endif
Finn Thain8c325132014-11-12 16:11:58 +1100702 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Finn Thainff50f9e2014-11-12 16:12:18 +1100705/**
706 * NCR5380_print_status - dump controller info
707 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100709 * Print commands in the various queues, called from NCR5380_abort
710 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
712
Finn Thain710ddd02014-11-12 16:12:02 +1100713static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400714{
715 int i, s;
716 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200717 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400718 H_NO(cmd), cmd->device->id, cmd->device->lun);
719 printk(KERN_CONT " command = ");
720 command = cmd->cmnd;
721 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
722 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
723 printk(KERN_CONT " %02x", command[i]);
724 printk("\n");
725}
726
Finn Thain3be1b3e2016-01-03 16:05:12 +1100727static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Al Virod89537e2013-03-31 13:24:44 -0400729 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100730 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400731 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Finn Thain8ad3a592014-03-18 11:42:19 +1100733 NCR5380_dprint(NDEBUG_ANY, instance);
734 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Roman Zippelc28bda22007-05-01 22:32:36 +0200736 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Roman Zippelc28bda22007-05-01 22:32:36 +0200738 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400739 printk("NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100740 hostdata->main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200741 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400742 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200743 else
Finn Thain710ddd02014-11-12 16:12:02 +1100744 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400745 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100746 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400747 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Al Virod89537e2013-03-31 13:24:44 -0400749 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100750 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400751 ptr = NEXT(ptr))
752 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200753
754 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400755 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Finn Thain710ddd02014-11-12 16:12:02 +1100758static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Roman Zippelc28bda22007-05-01 22:32:36 +0200760 int i, s;
761 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200762 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200763 H_NO(cmd), cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100764 seq_puts(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200765 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400766 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200767 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400768 seq_printf(m, " %02x", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100769 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Finn Thain4d3d2a52014-11-12 16:11:52 +1100772static int __maybe_unused NCR5380_show_info(struct seq_file *m,
773 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400774{
775 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100776 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400777 unsigned long flags;
778
779 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
780
Al Virod89537e2013-03-31 13:24:44 -0400781 local_irq_save(flags);
782 seq_printf(m, "NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100783 hostdata->main_running ? "" : "n't");
Al Virod89537e2013-03-31 13:24:44 -0400784 if (!hostdata->connected)
785 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
786 else
Finn Thain710ddd02014-11-12 16:12:02 +1100787 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400788 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100789 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400790 show_Scsi_Cmnd(ptr, m);
791
792 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100793 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400794 ptr = NEXT(ptr))
795 show_Scsi_Cmnd(ptr, m);
796
797 local_irq_restore(flags);
798 return 0;
799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Finn Thainff50f9e2014-11-12 16:12:18 +1100801/**
802 * NCR5380_init - initialise an NCR5380
803 * @instance: adapter to configure
804 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100806 * Initializes *instance and corresponding 5380 chip,
807 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
809 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100810 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200811 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100812 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
814
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100815static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Roman Zippelc28bda22007-05-01 22:32:36 +0200817 int i;
818 SETUP_HOSTDATA(instance);
Finn Thain2f854b82016-01-03 16:05:22 +1100819 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Finn Thaina53a21e2014-11-12 16:12:21 +1100821 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200822 hostdata->id_mask = 1 << instance->this_id;
823 hostdata->id_higher_mask = 0;
824 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
825 if (i > hostdata->id_mask)
826 hostdata->id_higher_mask |= i;
827 for (i = 0; i < 8; ++i)
828 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100830 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#endif
832#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200833 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200835 hostdata->connected = NULL;
836 hostdata->issue_queue = NULL;
837 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100838 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Finn Thaina53a21e2014-11-12 16:12:21 +1100840 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100841 hostdata->work_q = alloc_workqueue("ncr5380_%d",
842 WQ_UNBOUND | WQ_MEM_RECLAIM,
843 1, instance->host_no);
844 if (!hostdata->work_q)
845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Finn Thain8c325132014-11-12 16:11:58 +1100847 prepare_info(instance);
848
Roman Zippelc28bda22007-05-01 22:32:36 +0200849 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
850 NCR5380_write(MODE_REG, MR_BASE);
851 NCR5380_write(TARGET_COMMAND_REG, 0);
852 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Finn Thain2f854b82016-01-03 16:05:22 +1100854 /* Calibrate register polling loop */
855 i = 0;
856 deadline = jiffies + 1;
857 do {
858 cpu_relax();
859 } while (time_is_after_jiffies(deadline));
860 deadline += msecs_to_jiffies(256);
861 do {
862 NCR5380_read(STATUS_REG);
863 ++i;
864 cpu_relax();
865 } while (time_is_after_jiffies(deadline));
866 hostdata->accesses_per_ms = i / 256;
867
Roman Zippelc28bda22007-05-01 22:32:36 +0200868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Finn Thainff50f9e2014-11-12 16:12:18 +1100871/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100872 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
873 * @instance: adapter to check
874 *
875 * If the system crashed, it may have crashed with a connected target and
876 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
877 * currently established nexus, which we know nothing about. Failing that
878 * do a bus reset.
879 *
880 * Note that a bus reset will cause the chip to assert IRQ.
881 *
882 * Returns 0 if successful, otherwise -ENXIO.
883 */
884
885static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
886{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100887 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100888 int pass;
889
890 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
891 switch (pass) {
892 case 1:
893 case 3:
894 case 5:
895 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
896 NCR5380_poll_politely(instance,
897 STATUS_REG, SR_BSY, 0, 5 * HZ);
898 break;
899 case 2:
900 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
901 do_abort(instance);
902 break;
903 case 4:
904 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
905 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100906 /* Wait after a reset; the SCSI standard calls for
907 * 250ms, we wait 500ms to be on the safe side.
908 * But some Toshiba CD-ROMs need ten times that.
909 */
910 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
911 msleep(2500);
912 else
913 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100914 break;
915 case 6:
916 shost_printk(KERN_ERR, instance, "bus locked solid\n");
917 return -ENXIO;
918 }
919 }
920 return 0;
921}
922
923/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100924 * NCR5380_exit - remove an NCR5380
925 * @instance: adapter to remove
926 *
927 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
928 */
929
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200930static void NCR5380_exit(struct Scsi_Host *instance)
931{
Finn Thaina53a21e2014-11-12 16:12:21 +1100932 struct NCR5380_hostdata *hostdata = shost_priv(instance);
933
934 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100935 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200936}
937
Finn Thainff50f9e2014-11-12 16:12:18 +1100938/**
939 * NCR5380_queue_command - queue a command
940 * @instance: the relevant SCSI adapter
941 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100943 * cmd is added to the per instance issue_queue, with minor
944 * twiddling done to the host specific fields of cmd. If the
945 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
947
Finn Thain16b29e72014-11-12 16:12:08 +1100948static int NCR5380_queue_command(struct Scsi_Host *instance,
949 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Finn Thain16b29e72014-11-12 16:12:08 +1100951 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100952 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200953 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200956 switch (cmd->cmnd[0]) {
957 case WRITE_6:
958 case WRITE_10:
959 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
960 H_NO(cmd));
961 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100962 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200963 return 0;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
966
Roman Zippelc28bda22007-05-01 22:32:36 +0200967 /*
968 * We use the host_scribble field as a pointer to the next command
969 * in a queue
970 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Roman Zippel3130d902007-05-01 22:32:37 +0200972 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200973 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Roman Zippelc28bda22007-05-01 22:32:36 +0200975 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Roman Zippelc28bda22007-05-01 22:32:36 +0200982 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
983 * Otherwise a running NCR5380_main may steal the lock.
984 * Lock before actually inserting due to fairness reasons explained in
985 * atari_scsi.c. If we insert first, then it's impossible for this driver
986 * to release the lock.
987 * Stop timer for this command while waiting for the lock, or timeouts
988 * may happen (and they really do), and it's no good if the command doesn't
989 * appear in any of the queues.
990 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
991 * because also a timer int can trigger an abort or reset, which would
992 * alter queues and touch the lock.
993 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100994 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100995 return SCSI_MLQUEUE_HOST_BUSY;
996
997 local_irq_save(flags);
998
Finn Thainff50f9e2014-11-12 16:12:18 +1100999 /*
1000 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1001 * commands are added to the head of the queue since any command will
1002 * clear the contingent allegiance condition that exists and the
1003 * sense data is only guaranteed to be valid while the condition exists.
1004 */
1005
Roman Zippelc28bda22007-05-01 22:32:36 +02001006 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1007 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001008 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001009 hostdata->issue_queue = cmd;
1010 } else {
Finn Thain710ddd02014-11-12 16:12:02 +11001011 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02001012 NEXT(tmp); tmp = NEXT(tmp))
1013 ;
1014 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02001015 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001016 }
1017 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Finn Thaind65e6342014-03-18 11:42:20 +11001019 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +02001020 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Roman Zippelc28bda22007-05-01 22:32:36 +02001022 /* If queue_command() is called from an interrupt (real one or bottom
1023 * half), we let queue_main() do the job of taking care about main. If it
1024 * is already running, this is a no-op, else main will be queued.
1025 *
1026 * If we're not in an interrupt, we can call NCR5380_main()
1027 * unconditionally, because it cannot be already running.
1028 */
Finn Thain16b29e72014-11-12 16:12:08 +11001029 if (in_interrupt() || irqs_disabled())
Finn Thaina53a21e2014-11-12 16:12:21 +11001030 queue_main(hostdata);
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 else
Finn Thaina53a21e2014-11-12 16:12:21 +11001032 NCR5380_main(&hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +02001033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Finn Thaine3c3da62014-11-12 16:12:15 +11001036static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
1037{
1038 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1039
1040 /* Caller does the locking needed to set & test these data atomically */
1041 if (!hostdata->disconnected_queue &&
1042 !hostdata->issue_queue &&
1043 !hostdata->connected &&
1044 !hostdata->retain_dma_intr)
1045 NCR5380_release_dma_irq(instance);
1046}
1047
Finn Thainff50f9e2014-11-12 16:12:18 +11001048/**
1049 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001051 * NCR5380_main is a coroutine that runs as long as more work can
1052 * be done on the NCR5380 host adapters in a system. Both
1053 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1054 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +02001055 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001056 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +02001057 */
1058
Geert Uytterhoevenb312b382007-05-01 22:32:48 +02001059static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Finn Thaina53a21e2014-11-12 16:12:21 +11001061 struct NCR5380_hostdata *hostdata =
1062 container_of(work, struct NCR5380_hostdata, main_task);
1063 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +11001064 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02001065 int done;
1066 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Roman Zippelc28bda22007-05-01 22:32:36 +02001068 /*
1069 * We run (with interrupts disabled) until we're sure that none of
1070 * the host adapters have anything that can be done, at which point
1071 * we set main_running to 0 and exit.
1072 *
1073 * Interrupts are enabled before doing various other internal
1074 * instructions, after we've decided that we need to run through
1075 * the loop again.
1076 *
1077 * this should prevent any race conditions.
1078 *
1079 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1080 * because also a timer int can trigger an abort or reset, which can
1081 * alter queues and touch the Falcon lock.
1082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Roman Zippelc28bda22007-05-01 22:32:36 +02001084 /* Tell int handlers main() is now already executing. Note that
1085 no races are possible here. If an int comes in before
1086 'main_running' is set here, and queues/executes main via the
1087 task queue, it doesn't do any harm, just this instance of main
1088 won't find any work left to do. */
Finn Thaina53a21e2014-11-12 16:12:21 +11001089 if (hostdata->main_running)
Roman Zippelc28bda22007-05-01 22:32:36 +02001090 return;
Finn Thaina53a21e2014-11-12 16:12:21 +11001091 hostdata->main_running = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001092
1093 local_save_flags(flags);
1094 do {
1095 local_irq_disable(); /* Freeze request queues */
1096 done = 1;
1097
1098 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001099 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001100 /*
1101 * Search through the issue_queue for a command destined
1102 * for a target that's not busy.
1103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001105 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001106 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1107 ;
1108 /*printk("%p ", tmp);*/
1109 if ((tmp == prev) && tmp)
1110 printk(" LOOP\n");
1111 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001113 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001114 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001115 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Finn Thaine3f463b2014-11-12 16:12:16 +11001117 dprintk(NDEBUG_LISTS,
1118 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1119 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1120 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001121 /* When we find one, remove it from the issue queue. */
1122 /* ++guenther: possible race with Falcon locking */
1123 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001125 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001127 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001128#endif
1129 ) {
1130 /* ++guenther: just to be sure, this must be atomic */
1131 local_irq_disable();
1132 if (prev) {
1133 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001134 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001135 } else {
1136 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1137 hostdata->issue_queue = NEXT(tmp);
1138 }
Roman Zippel3130d902007-05-01 22:32:37 +02001139 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001140 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001141
1142 /* reenable interrupts after finding one */
1143 local_irq_restore(flags);
1144
1145 /*
1146 * Attempt to establish an I_T_L nexus here.
1147 * On success, instance->hostdata->connected is set.
1148 * On failure, we must add the command back to the
1149 * issue queue so we can keep trying.
1150 */
Finn Thaind65e6342014-03-18 11:42:20 +11001151 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001152 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001153 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001154 /*
1155 * REQUEST SENSE commands are issued without tagged
1156 * queueing, even on SCSI-II devices because the
1157 * contingent allegiance condition exists for the
1158 * entire unit.
1159 */
1160 /* ++roman: ...and the standard also requires that
1161 * REQUEST SENSE command are untagged.
1162 */
1163
1164#ifdef SUPPORT_TAGS
1165 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1166#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001167 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001168 /* OK or bad target */
Finn Thaine3c3da62014-11-12 16:12:15 +11001169 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001170 hostdata->retain_dma_intr--;
Finn Thaine3c3da62014-11-12 16:12:15 +11001171 maybe_release_dma_irq(instance);
1172 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001173 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001174 /* Need to retry */
Roman Zippelc28bda22007-05-01 22:32:36 +02001175 local_irq_disable();
1176 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001177 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001178 hostdata->issue_queue = tmp;
1179#ifdef SUPPORT_TAGS
1180 cmd_free_tag(tmp);
1181#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001182 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001183 local_irq_restore(flags);
Finn Thain1d3db592016-01-03 16:05:24 +11001184 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001185 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001186 "returned to issue_queue\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001187 }
Finn Thain1f1b0c72016-01-03 16:05:17 +11001188 if (hostdata->connected)
1189 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001190 } /* if target/lun/target queue is not busy */
1191 } /* for issue_queue */
1192 } /* if (!hostdata->connected) */
1193
1194 if (hostdata->connected
1195#ifdef REAL_DMA
1196 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197#endif
1198 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001200 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001201 HOSTNO);
1202 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001203 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001204 done = 0;
1205 }
1206 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Roman Zippelc28bda22007-05-01 22:32:36 +02001208 /* Better allow ints _after_ 'main_running' has been cleared, else
1209 an interrupt could believe we'll pick up the work it left for
1210 us, but we won't see it anymore here... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001211 hostdata->main_running = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001212 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215
1216#ifdef REAL_DMA
1217/*
1218 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1219 *
1220 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001221 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
1223 * Inputs : instance - this instance of the NCR5380.
1224 *
1225 */
1226
Roman Zippelc28bda22007-05-01 22:32:36 +02001227static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Roman Zippelc28bda22007-05-01 22:32:36 +02001229 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001230 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001231 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001232 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001233 int saved_data = 0, overrun = 0;
1234 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Roman Zippelc28bda22007-05-01 22:32:36 +02001236 if (!hostdata->connected) {
1237 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1238 "no connected cmd\n", HOSTNO);
1239 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Finn Thainef1081c2014-11-12 16:12:14 +11001242 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001243 p = hostdata->connected->SCp.phase;
1244 if (p & SR_IO) {
1245 udelay(10);
1246 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1247 (BASR_PHASE_MATCH|BASR_ACK)) ==
1248 (BASR_PHASE_MATCH|BASR_ACK)) {
1249 saved_data = NCR5380_read(INPUT_DATA_REG);
1250 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001251 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001252 }
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001255
Finn Thaind65e6342014-03-18 11:42:20 +11001256 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001257 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1258 NCR5380_read(STATUS_REG));
1259
Finn Thaine3f463b2014-11-12 16:12:16 +11001260#if defined(CONFIG_SUN3)
1261 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1262 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1263 instance->host_no);
1264 BUG();
1265 }
1266
1267 /* make sure we're not stuck in a data phase */
1268 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1269 (BASR_PHASE_MATCH | BASR_ACK)) {
1270 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1271 NCR5380_read(BUS_AND_STATUS_REG));
1272 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1273 instance->host_no);
1274 BUG();
1275 }
1276#endif
1277
Roman Zippelc28bda22007-05-01 22:32:36 +02001278 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1279 NCR5380_write(MODE_REG, MR_BASE);
1280 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1281
Finn Thain01bd9082014-11-12 16:12:23 +11001282 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001283 hostdata->dma_len = 0;
1284
1285 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1286 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001287 *data += transferred;
1288 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001289
Finn Thainef1081c2014-11-12 16:12:14 +11001290 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001291 int cnt, toPIO;
1292
Roman Zippelc28bda22007-05-01 22:32:36 +02001293 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001294 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001295 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001296 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001297 *(*data)++ = saved_data;
1298 (*count)--;
1299 cnt--;
1300 toPIO--;
1301 }
Finn Thaind65e6342014-03-18 11:42:20 +11001302 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001303 NCR5380_transfer_pio(instance, &p, &cnt, data);
1304 *count -= toPIO - cnt;
1305 }
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308#endif /* REAL_DMA */
1309
1310
Finn Thainff50f9e2014-11-12 16:12:18 +11001311/**
1312 * NCR5380_intr - generic NCR5380 irq handler
1313 * @irq: interrupt number
1314 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001315 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001316 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1317 * from the disconnected queue, and restarting NCR5380_main()
1318 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
1320
Roman Zippelc28bda22007-05-01 22:32:36 +02001321static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Finn Thaina53a21e2014-11-12 16:12:21 +11001323 struct Scsi_Host *instance = dev_id;
Roman Zippelc28bda22007-05-01 22:32:36 +02001324 int done = 1, handled = 0;
1325 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Finn Thaind65e6342014-03-18 11:42:20 +11001327 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Roman Zippelc28bda22007-05-01 22:32:36 +02001329 /* Look for pending interrupts */
1330 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001331 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001332 /* dispatch to appropriate routine if found and done=0 */
1333 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001334 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001335 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1336 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001337 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001338 NCR5380_reselect(instance);
1339 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1340 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001341 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001342 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1343 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001344 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001345 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1346 } else {
1347 /*
1348 * The rest of the interrupt conditions can occur only during a
1349 * DMA transfer
1350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001353 /*
1354 * We should only get PHASE MISMATCH and EOP interrupts if we have
1355 * DMA enabled, so do a sanity check based on the current setting
1356 * of the MODE register.
1357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Roman Zippelc28bda22007-05-01 22:32:36 +02001359 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1360 ((basr & BASR_END_DMA_TRANSFER) ||
1361 !(basr & BASR_PHASE_MATCH))) {
1362
Finn Thaind65e6342014-03-18 11:42:20 +11001363 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001364 NCR5380_dma_complete( instance );
1365 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001366 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001368 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001370 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001371 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001372 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1373 HOSTNO, basr, NCR5380_read(MODE_REG),
1374 NCR5380_read(STATUS_REG));
1375 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001376#ifdef SUN3_SCSI_VME
1377 dregs->csr |= CSR_DMA_ENABLE;
1378#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001379 }
1380 } /* if !(SELECTION || PARITY) */
1381 handled = 1;
1382 } /* BASR & IRQ */ else {
1383 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1384 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1385 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1386 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001387#ifdef SUN3_SCSI_VME
1388 dregs->csr |= CSR_DMA_ENABLE;
1389#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001390 }
1391
1392 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001393 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001394 /* Put a call to NCR5380_main() on the queue... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001395 queue_main(shost_priv(instance));
Roman Zippelc28bda22007-05-01 22:32:36 +02001396 }
1397 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Roman Zippelc28bda22007-05-01 22:32:36 +02001400/*
Finn Thain710ddd02014-11-12 16:12:02 +11001401 * Function : int NCR5380_select(struct Scsi_Host *instance,
1402 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
1404 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001405 * including ARBITRATION, SELECTION, and initial message out for
1406 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001408 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001409 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 *
Finn Thain63238762016-01-03 16:05:15 +11001411 * Returns : -1 if selection failed but should be retried.
1412 * 0 if selection failed and should not be retried.
1413 * 0 if selection succeeded completely (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001414 *
1415 * Side effects :
1416 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * with registers as they should have been on entry - ie
1418 * SELECT_ENABLE will be set appropriately, the NCR5380
1419 * will cease to drive any SCSI bus signals.
1420 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001421 * If successful : I_T_L or I_T_L_Q nexus will be established,
1422 * instance->connected will be set to cmd.
1423 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001425 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * cmd->result host byte set to DID_BAD_TARGET.
1427 */
1428
Finn Thain710ddd02014-11-12 16:12:02 +11001429static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Roman Zippelc28bda22007-05-01 22:32:36 +02001431 SETUP_HOSTDATA(instance);
1432 unsigned char tmp[3], phase;
1433 unsigned char *data;
1434 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001435 int err;
Roman Zippelc28bda22007-05-01 22:32:36 +02001436 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Finn Thain8ad3a592014-03-18 11:42:19 +11001438 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001439 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001440 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Roman Zippelc28bda22007-05-01 22:32:36 +02001442 /*
1443 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1444 * data bus during SELECTION.
1445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Roman Zippelc28bda22007-05-01 22:32:36 +02001447 local_irq_save(flags);
1448 if (hostdata->connected) {
1449 local_irq_restore(flags);
1450 return -1;
1451 }
1452 NCR5380_write(TARGET_COMMAND_REG, 0);
1453
1454 /*
1455 * Start arbitration.
1456 */
1457
1458 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1459 NCR5380_write(MODE_REG, MR_ARBITRATE);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001464#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001465 {
1466 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1469 time_before(jiffies, timeout) && !hostdata->connected)
1470 ;
1471 if (time_after_eq(jiffies, timeout)) {
1472 printk("scsi : arbitration timeout at %d\n", __LINE__);
1473 NCR5380_write(MODE_REG, MR_BASE);
1474 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1475 return -1;
1476 }
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001479 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1480 !hostdata->connected)
1481 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482#endif
1483
Finn Thaind65e6342014-03-18 11:42:20 +11001484 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Roman Zippelc28bda22007-05-01 22:32:36 +02001486 if (hostdata->connected) {
1487 NCR5380_write(MODE_REG, MR_BASE);
1488 return -1;
1489 }
1490 /*
1491 * The arbitration delay is 2.2us, but this is a minimum and there is
1492 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1493 * the integral nature of udelay().
1494 *
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Roman Zippelc28bda22007-05-01 22:32:36 +02001497 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Roman Zippelc28bda22007-05-01 22:32:36 +02001499 /* Check for lost arbitration */
1500 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1501 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1502 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1503 hostdata->connected) {
1504 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001505 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001506 HOSTNO);
1507 return -1;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Finn Thaincf13b082016-01-03 16:05:18 +11001510 /* After/during arbitration, BSY should be asserted.
1511 * IBM DPES-31080 Version S31Q works now
1512 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1513 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 NCR5380_write(INITIATOR_COMMAND_REG,
1515 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Roman Zippelc28bda22007-05-01 22:32:36 +02001517 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1518 hostdata->connected) {
1519 NCR5380_write(MODE_REG, MR_BASE);
1520 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001521 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001522 HOSTNO);
1523 return -1;
1524 }
1525
1526 /*
1527 * Again, bus clear + bus settle time is 1.2us, however, this is
1528 * a minimum so we'll udelay ceil(1.2)
1529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Finn Thain9c3f0e22016-01-03 16:05:11 +11001531 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1532 udelay(15);
1533 else
1534 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001535
1536 if (hostdata->connected) {
1537 NCR5380_write(MODE_REG, MR_BASE);
1538 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1539 return -1;
1540 }
1541
Finn Thaind65e6342014-03-18 11:42:20 +11001542 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001543
1544 /*
1545 * Now that we have won arbitration, start Selection process, asserting
1546 * the host and target ID's on the SCSI bus.
1547 */
1548
1549 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1550
1551 /*
1552 * Raise ATN while SEL is true before BSY goes false from arbitration,
1553 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1554 * phase immediately after selection.
1555 */
1556
1557 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1558 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Roman Zippelc28bda22007-05-01 22:32:36 +02001561 /*
1562 * Reselect interrupts must be turned off prior to the dropping of BSY,
1563 * otherwise we will trigger an interrupt.
1564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Roman Zippelc28bda22007-05-01 22:32:36 +02001566 if (hostdata->connected) {
1567 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1568 return -1;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Roman Zippelc28bda22007-05-01 22:32:36 +02001571 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Roman Zippelc28bda22007-05-01 22:32:36 +02001573 /*
1574 * The initiator shall then wait at least two deskew delays and release
1575 * the BSY signal.
1576 */
1577 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Roman Zippelc28bda22007-05-01 22:32:36 +02001579 /* Reset BSY */
1580 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1581 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Roman Zippelc28bda22007-05-01 22:32:36 +02001583 /*
1584 * Something weird happens when we cease to drive BSY - looks
1585 * like the board/chip is letting us do another read before the
1586 * appropriate propagation delay has expired, and we're confusing
1587 * a BSY signal from ourselves as the target's response to SELECTION.
1588 *
1589 * A small delay (the 'C++' frontend breaks the pipeline with an
1590 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1591 * tighter 'C' code breaks and requires this) solves the problem -
1592 * the 1 us delay is arbitrary, and only used because this delay will
1593 * be the same on other platforms and since it works here, it should
1594 * work there.
1595 *
1596 * wingel suggests that this could be due to failing to wait
1597 * one deskew delay.
1598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Roman Zippelc28bda22007-05-01 22:32:36 +02001600 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Finn Thaind65e6342014-03-18 11:42:20 +11001602 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Roman Zippelc28bda22007-05-01 22:32:36 +02001604 /*
1605 * The SCSI specification calls for a 250 ms timeout for the actual
1606 * selection.
1607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Finn Thainae753a32016-01-03 16:05:23 +11001609 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1610 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Roman Zippelc28bda22007-05-01 22:32:36 +02001612 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1613 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1614 NCR5380_reselect(instance);
1615 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1616 HOSTNO);
1617 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1618 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001620
Finn Thainae753a32016-01-03 16:05:23 +11001621 if (err < 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001622 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001623 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001624#ifdef SUPPORT_TAGS
1625 cmd_free_tag(cmd);
1626#endif
1627 cmd->scsi_done(cmd);
1628 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001629 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001630 return 0;
1631 }
1632
Roman Zippelc28bda22007-05-01 22:32:36 +02001633 /*
Finn Thainae753a32016-01-03 16:05:23 +11001634 * No less than two deskew delays after the initiator detects the
1635 * BSY signal is true, it shall release the SEL signal and may
1636 * change the DATA BUS. -wingel
1637 */
1638
1639 udelay(1);
1640
1641 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1642
1643 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001644 * Since we followed the SCSI spec, and raised ATN while SEL
1645 * was true but before BSY was false during selection, the information
1646 * transfer phase should be a MESSAGE OUT phase so that we can send the
1647 * IDENTIFY message.
1648 *
1649 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1650 * message (2 bytes) with a tag ID that we increment with every command
1651 * until it wraps back to 0.
1652 *
1653 * XXX - it turns out that there are some broken SCSI-II devices,
1654 * which claim to support tagged queuing but fail when more than
1655 * some number of commands are issued at once.
1656 */
1657
1658 /* Wait for start of REQ/ACK handshake */
1659 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1660 ;
1661
Finn Thaind65e6342014-03-18 11:42:20 +11001662 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001663 HOSTNO, cmd->device->id);
1664 tmp[0] = IDENTIFY(1, cmd->device->lun);
1665
1666#ifdef SUPPORT_TAGS
1667 if (cmd->tag != TAG_NONE) {
1668 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1669 tmp[2] = cmd->tag;
1670 len = 3;
1671 } else
1672 len = 1;
1673#else
1674 len = 1;
1675 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676#endif /* SUPPORT_TAGS */
1677
Roman Zippelc28bda22007-05-01 22:32:36 +02001678 /* Send message(s) */
1679 data = tmp;
1680 phase = PHASE_MSGOUT;
1681 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001682 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001683 /* XXX need to handle errors here */
1684 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001686 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1687#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001688#ifdef SUN3_SCSI_VME
1689 dregs->csr |= CSR_INTR;
1690#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Roman Zippelc28bda22007-05-01 22:32:36 +02001692 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Roman Zippelc28bda22007-05-01 22:32:36 +02001694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Roman Zippelc28bda22007-05-01 22:32:36 +02001697/*
1698 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * unsigned char *phase, int *count, unsigned char **data)
1700 *
1701 * Purpose : transfers data in given phase using polled I/O
1702 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001703 * Inputs : instance - instance of driver, *phase - pointer to
1704 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001708 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 * is in same phase.
1710 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001711 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *
1713 * XXX Note : handling for bus free may be useful.
1714 */
1715
1716/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001717 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * IS 100% reliable, and for the actual data transfer where speed
1719 * counts, we will always do a pseudo DMA or DMA transfer.
1720 */
1721
Roman Zippelc28bda22007-05-01 22:32:36 +02001722static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1723 unsigned char *phase, int *count,
1724 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Roman Zippelc28bda22007-05-01 22:32:36 +02001726 register unsigned char p = *phase, tmp;
1727 register int c = *count;
1728 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Roman Zippelc28bda22007-05-01 22:32:36 +02001730 /*
1731 * The NCR5380 chip will only drive the SCSI bus when the
1732 * phase specified in the appropriate bits of the TARGET COMMAND
1733 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
1735
Roman Zippelc28bda22007-05-01 22:32:36 +02001736 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Roman Zippelc28bda22007-05-01 22:32:36 +02001738 do {
1739 /*
1740 * Wait for assertion of REQ, after which the phase bits will be
1741 * valid
1742 */
1743 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1744 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Finn Thaind65e6342014-03-18 11:42:20 +11001746 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Roman Zippelc28bda22007-05-01 22:32:36 +02001748 /* Check for phase mismatch */
1749 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001750 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001751 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001752 break;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Roman Zippelc28bda22007-05-01 22:32:36 +02001755 /* Do actual transfer from SCSI bus to / from memory */
1756 if (!(p & SR_IO))
1757 NCR5380_write(OUTPUT_DATA_REG, *d);
1758 else
1759 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Roman Zippelc28bda22007-05-01 22:32:36 +02001761 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Roman Zippelc28bda22007-05-01 22:32:36 +02001763 /*
1764 * The SCSI standard suggests that in MSGOUT phase, the initiator
1765 * should drop ATN on the last byte of the message phase
1766 * after REQ has been asserted for the handshake but before
1767 * the initiator raises ACK.
1768 */
1769
1770 if (!(p & SR_IO)) {
1771 if (!((p & SR_MSG) && c > 1)) {
1772 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001773 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001774 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1775 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1776 } else {
1777 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1778 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001779 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001780 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1781 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1782 }
1783 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001784 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001785 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1786 }
1787
1788 while (NCR5380_read(STATUS_REG) & SR_REQ)
1789 ;
1790
Finn Thaind65e6342014-03-18 11:42:20 +11001791 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001792
1793 /*
1794 * We have several special cases to consider during REQ/ACK handshaking :
1795 * 1. We were in MSGOUT phase, and we are on the last byte of the
1796 * message. ATN must be dropped as ACK is dropped.
1797 *
1798 * 2. We are in a MSGIN phase, and we are on the last byte of the
1799 * message. We must exit with ACK asserted, so that the calling
1800 * code may raise ATN before dropping ACK to reject the message.
1801 *
1802 * 3. ACK and ATN are clear and the target may proceed as normal.
1803 */
1804 if (!(p == PHASE_MSGIN && c == 1)) {
1805 if (p == PHASE_MSGOUT && c > 1)
1806 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1807 else
1808 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1809 }
1810 } while (--c);
1811
Finn Thaind65e6342014-03-18 11:42:20 +11001812 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001813
1814 *count = c;
1815 *data = d;
1816 tmp = NCR5380_read(STATUS_REG);
1817 /* The phase read from the bus is valid if either REQ is (already)
1818 * asserted or if ACK hasn't been released yet. The latter is the case if
1819 * we're in MSGIN and all wanted bytes have been received.
1820 */
1821 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1822 *phase = tmp & PHASE_MASK;
1823 else
1824 *phase = PHASE_UNKNOWN;
1825
1826 if (!c || (*phase == p))
1827 return 0;
1828 else
1829 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Finn Thain636b1ec2016-01-03 16:05:10 +11001832/**
1833 * do_reset - issue a reset command
1834 * @instance: adapter to reset
1835 *
1836 * Issue a reset sequence to the NCR5380 and try and get the bus
1837 * back into sane shape.
1838 *
1839 * This clears the reset interrupt flag because there may be no handler for
1840 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1841 * been installed. And when in EH we may have released the ST DMA interrupt.
1842 */
1843
1844static void do_reset(struct Scsi_Host *instance)
1845{
1846 unsigned long flags;
1847
1848 local_irq_save(flags);
1849 NCR5380_write(TARGET_COMMAND_REG,
1850 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1851 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1852 udelay(50);
1853 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1854 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1855 local_irq_restore(flags);
1856}
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
1859 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001860 *
1861 * Purpose : abort the currently established nexus. Should only be
1862 * called from a routine which can drop into a
1863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * Returns : 0 on success, -1 on failure.
1865 */
1866
Finn Thaina53a21e2014-11-12 16:12:21 +11001867static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Roman Zippelc28bda22007-05-01 22:32:36 +02001869 unsigned char tmp, *msgptr, phase;
1870 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Roman Zippelc28bda22007-05-01 22:32:36 +02001872 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Roman Zippelc28bda22007-05-01 22:32:36 +02001875 /*
1876 * Wait for the target to indicate a valid phase by asserting
1877 * REQ. Once this happens, we'll have either a MSGOUT phase
1878 * and can immediately send the ABORT message, or we'll have some
1879 * other phase and will have to source/sink data.
1880 *
1881 * We really don't care what value was on the bus or what value
1882 * the target sees, so we just handshake.
1883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Roel Kluin3be38e72007-11-15 21:13:46 +01001885 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001886 ;
1887
1888 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1889
1890 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1891 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1892 ICR_ASSERT_ACK);
1893 while (NCR5380_read(STATUS_REG) & SR_REQ)
1894 ;
1895 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1896 }
1897
1898 tmp = ABORT;
1899 msgptr = &tmp;
1900 len = 1;
1901 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001902 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001903
1904 /*
1905 * If we got here, and the command completed successfully,
1906 * we're about to go into bus free state.
1907 */
1908
1909 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
1912#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001913/*
1914 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * unsigned char *phase, int *count, unsigned char **data)
1916 *
1917 * Purpose : transfers data in given phase using either real
1918 * or pseudo DMA.
1919 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001920 * Inputs : instance - instance of driver, *phase - pointer to
1921 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001923 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001925 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 * is in same phase.
1927 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001928 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 *
1930 */
1931
1932
Roman Zippelc28bda22007-05-01 22:32:36 +02001933static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1934 unsigned char *phase, int *count,
1935 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Roman Zippelc28bda22007-05-01 22:32:36 +02001937 SETUP_HOSTDATA(instance);
1938 register int c = *count;
1939 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001940 unsigned long flags;
1941
1942#if defined(CONFIG_SUN3)
1943 /* sanity check */
1944 if (!sun3_dma_setup_done) {
1945 pr_err("scsi%d: transfer_dma without setup!\n",
1946 instance->host_no);
1947 BUG();
1948 }
1949 hostdata->dma_len = c;
1950
1951 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1952 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1953 c, (p & SR_IO) ? "to" : "from", *data);
1954
1955 /* netbsd turns off ints here, why not be safe and do it too */
1956 local_irq_save(flags);
1957
1958 /* send start chain */
1959 sun3scsi_dma_start(c, *data);
1960
1961 if (p & SR_IO) {
1962 NCR5380_write(TARGET_COMMAND_REG, 1);
1963 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1964 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1965 NCR5380_write(MODE_REG,
1966 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1967 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1968 } else {
1969 NCR5380_write(TARGET_COMMAND_REG, 0);
1970 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1971 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1972 NCR5380_write(MODE_REG,
1973 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1974 NCR5380_write(START_DMA_SEND_REG, 0);
1975 }
1976
1977#ifdef SUN3_SCSI_VME
1978 dregs->csr |= CSR_DMA_ENABLE;
1979#endif
1980
1981 local_irq_restore(flags);
1982
1983 sun3_dma_active = 1;
1984
1985#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001986 register unsigned char *d = *data;
1987 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Roman Zippelc28bda22007-05-01 22:32:36 +02001989 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1990 *phase = tmp;
1991 return -1;
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Finn Thainef1081c2014-11-12 16:12:14 +11001994 if (hostdata->read_overruns && (p & SR_IO))
1995 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Finn Thaind65e6342014-03-18 11:42:20 +11001997 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001998 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1999 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Roman Zippelc28bda22007-05-01 22:32:36 +02002001 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002004 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#endif /* def REAL_DMA */
2006
Finn Thainef1081c2014-11-12 16:12:14 +11002007 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002008 /* On the Medusa, it is a must to initialize the DMA before
2009 * starting the NCR. This is also the cleaner way for the TT.
2010 */
2011 local_irq_save(flags);
2012 hostdata->dma_len = (p & SR_IO) ?
2013 NCR5380_dma_read_setup(instance, d, c) :
2014 NCR5380_dma_write_setup(instance, d, c);
2015 local_irq_restore(flags);
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Roman Zippelc28bda22007-05-01 22:32:36 +02002018 if (p & SR_IO)
2019 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2020 else {
2021 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2022 NCR5380_write(START_DMA_SEND_REG, 0);
2023 }
2024
Finn Thainef1081c2014-11-12 16:12:14 +11002025 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002026 /* On the Falcon, the DMA setup must be done after the last */
2027 /* NCR access, else the DMA setup gets trashed!
2028 */
2029 local_irq_save(flags);
2030 hostdata->dma_len = (p & SR_IO) ?
2031 NCR5380_dma_read_setup(instance, d, c) :
2032 NCR5380_dma_write_setup(instance, d, c);
2033 local_irq_restore(flags);
2034 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002035#endif /* !defined(CONFIG_SUN3) */
2036
Roman Zippelc28bda22007-05-01 22:32:36 +02002037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039#endif /* defined(REAL_DMA) */
2040
2041/*
2042 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2043 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002044 * Purpose : run through the various SCSI phases and do as the target
2045 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 * instance->connected.
2047 *
2048 * Inputs : instance, instance for which we are doing commands
2049 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002050 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * modified if a command disconnects, *instance->connected will
2052 * change.
2053 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002054 * XXX Note : we need to watch for bus free or a reset condition here
2055 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
Roman Zippelc28bda22007-05-01 22:32:36 +02002057
2058static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Roman Zippelc28bda22007-05-01 22:32:36 +02002060 SETUP_HOSTDATA(instance);
2061 unsigned long flags;
2062 unsigned char msgout = NOP;
2063 int sink = 0;
2064 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02002066 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002068 unsigned char *data;
2069 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11002070 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Finn Thaine3f463b2014-11-12 16:12:16 +11002072#ifdef SUN3_SCSI_VME
2073 dregs->csr |= CSR_INTR;
2074#endif
2075
Roman Zippelc28bda22007-05-01 22:32:36 +02002076 while (1) {
2077 tmp = NCR5380_read(STATUS_REG);
2078 /* We only have a valid SCSI phase when REQ is asserted */
2079 if (tmp & SR_REQ) {
2080 phase = (tmp & PHASE_MASK);
2081 if (phase != old_phase) {
2082 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11002083 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002085#if defined(CONFIG_SUN3)
2086 if (phase == PHASE_CMDOUT) {
2087#if defined(REAL_DMA)
2088 void *d;
2089 unsigned long count;
2090
2091 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2092 count = cmd->SCp.buffer->length;
2093 d = sg_virt(cmd->SCp.buffer);
2094 } else {
2095 count = cmd->SCp.this_residual;
2096 d = cmd->SCp.ptr;
2097 }
2098 /* this command setup for dma yet? */
2099 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2100 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2101 sun3scsi_dma_setup(d, count,
2102 rq_data_dir(cmd->request));
2103 sun3_dma_setup_done = cmd;
2104 }
2105 }
2106#endif
2107#ifdef SUN3_SCSI_VME
2108 dregs->csr |= CSR_INTR;
2109#endif
2110 }
2111#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Roman Zippelc28bda22007-05-01 22:32:36 +02002113 if (sink && (phase != PHASE_MSGOUT)) {
2114 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Roman Zippelc28bda22007-05-01 22:32:36 +02002116 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2117 ICR_ASSERT_ACK);
2118 while (NCR5380_read(STATUS_REG) & SR_REQ)
2119 ;
2120 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2121 ICR_ASSERT_ATN);
2122 sink = 0;
2123 continue;
2124 }
2125
2126 switch (phase) {
2127 case PHASE_DATAOUT:
2128#if (NDEBUG & NDEBUG_NO_DATAOUT)
2129 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2130 "aborted\n", HOSTNO);
2131 sink = 1;
2132 do_abort(instance);
2133 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002134 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002135 return;
2136#endif
2137 case PHASE_DATAIN:
2138 /*
2139 * If there is no room left in the current buffer in the
2140 * scatter-gather list, move onto the next one.
2141 */
2142
2143 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2144 ++cmd->SCp.buffer;
2145 --cmd->SCp.buffers_residual;
2146 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002147 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002148 /* ++roman: Try to merge some scatter-buffers if
2149 * they are at contiguous physical addresses.
2150 */
2151 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002152 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002153 HOSTNO, cmd->SCp.this_residual,
2154 cmd->SCp.buffers_residual);
2155 }
2156
2157 /*
2158 * The preferred transfer method is going to be
2159 * PSEUDO-DMA for systems that are strictly PIO,
2160 * since we can let the hardware do the handshaking.
2161 *
2162 * For this to work, we need to know the transfersize
2163 * ahead of time, since the pseudo-DMA code will sit
2164 * in an unconditional loop.
2165 */
2166
2167 /* ++roman: I suggest, this should be
2168 * #if def(REAL_DMA)
2169 * instead of leaving REAL_DMA out.
2170 */
2171
2172#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002173#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11002174 transfersize = 0;
2175 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11002176#endif
Finn Thainff3d4572016-01-03 16:05:25 +11002177 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
2178
2179 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002180 len = transfersize;
2181 cmd->SCp.phase = phase;
2182 if (NCR5380_transfer_dma(instance, &phase,
2183 &len, (unsigned char **)&cmd->SCp.ptr)) {
2184 /*
2185 * If the watchdog timer fires, all future
2186 * accesses to this device will use the
2187 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002188 scmd_printk(KERN_INFO, cmd,
2189 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002190 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02002191 sink = 1;
2192 do_abort(instance);
2193 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002194 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002195 /* XXX - need to source or sink data here, as appropriate */
2196 } else {
2197#ifdef REAL_DMA
2198 /* ++roman: When using real DMA,
2199 * information_transfer() should return after
2200 * starting DMA since it has nothing more to
2201 * do.
2202 */
2203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002205 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002207 }
2208 } else
2209#endif /* defined(REAL_DMA) */
2210 NCR5380_transfer_pio(instance, &phase,
2211 (int *)&cmd->SCp.this_residual,
2212 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002213#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2214 /* if we had intended to dma that command clear it */
2215 if (sun3_dma_setup_done == cmd)
2216 sun3_dma_setup_done = NULL;
2217#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002218 break;
2219 case PHASE_MSGIN:
2220 len = 1;
2221 data = &tmp;
2222 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2223 NCR5380_transfer_pio(instance, &phase, &len, &data);
2224 cmd->SCp.Message = tmp;
2225
2226 switch (tmp) {
2227 /*
2228 * Linking lets us reduce the time required to get the
2229 * next command out to the device, hopefully this will
2230 * mean we don't waste another revolution due to the delays
2231 * required by ARBITRATION and another SELECTION.
2232 *
2233 * In the current implementation proposal, low level drivers
2234 * merely have to start the next command, pointed to by
2235 * next_link, done() is called as with unlinked commands.
2236 */
2237#ifdef LINKED
2238 case LINKED_CMD_COMPLETE:
2239 case LINKED_FLG_CMD_COMPLETE:
2240 /* Accept message by clearing ACK */
2241 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2242
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002243 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002244 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2245
2246 /* Enable reselect interrupts */
2247 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2248 /*
2249 * Sanity check : A linked command should only terminate
2250 * with one of these messages if there are more linked
2251 * commands available.
2252 */
2253
2254 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002255 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002256 "linked command complete, no next_link\n",
2257 HOSTNO, cmd->device->id, cmd->device->lun);
2258 sink = 1;
2259 do_abort(instance);
2260 return;
2261 }
2262
2263 initialize_SCp(cmd->next_link);
2264 /* The next command is still part of this process; copy it
2265 * and don't free it! */
2266 cmd->next_link->tag = cmd->tag;
2267 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002268 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002269 "done, calling scsi_done().\n",
2270 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002271 cmd->scsi_done(cmd);
2272 cmd = hostdata->connected;
2273 break;
2274#endif /* def LINKED */
2275 case ABORT:
2276 case COMMAND_COMPLETE:
2277 /* Accept message by clearing ACK */
2278 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002279 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002280 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002281
2282 local_irq_save(flags);
2283 hostdata->retain_dma_intr++;
2284 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002285#ifdef SUPPORT_TAGS
2286 cmd_free_tag(cmd);
2287 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2288 /* Turn a QUEUE FULL status into BUSY, I think the
2289 * mid level cannot handle QUEUE FULL :-( (The
2290 * command is retried after BUSY). Also update our
2291 * queue size to the number of currently issued
2292 * commands now.
2293 */
2294 /* ++Andreas: the mid level code knows about
2295 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002296 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002297 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002298 "QUEUE_FULL after %d commands\n",
2299 HOSTNO, cmd->device->id, cmd->device->lun,
2300 ta->nr_allocated);
2301 if (ta->queue_size > ta->nr_allocated)
2302 ta->nr_allocated = ta->queue_size;
2303 }
2304#else
2305 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2306#endif
2307 /* Enable reselect interrupts */
2308 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2309
2310 /*
2311 * I'm not sure what the correct thing to do here is :
2312 *
2313 * If the command that just executed is NOT a request
2314 * sense, the obvious thing to do is to set the result
2315 * code to the values of the stored parameters.
2316 *
2317 * If it was a REQUEST SENSE command, we need some way to
2318 * differentiate between the failure code of the original
2319 * and the failure code of the REQUEST sense - the obvious
2320 * case is success, where we fall through and leave the
2321 * result code unchanged.
2322 *
2323 * The non-obvious place is where the REQUEST SENSE failed
2324 */
2325
2326 if (cmd->cmnd[0] != REQUEST_SENSE)
2327 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2328 else if (status_byte(cmd->SCp.Status) != GOOD)
2329 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2330
Boaz Harrosh28424d32007-09-10 22:37:45 +03002331 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2332 hostdata->ses.cmd_len) {
2333 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2334 hostdata->ses.cmd_len = 0 ;
2335 }
2336
Roman Zippelc28bda22007-05-01 22:32:36 +02002337 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2338 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002339 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002340
Finn Thaind65e6342014-03-18 11:42:20 +11002341 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002342
Roman Zippelc28bda22007-05-01 22:32:36 +02002343 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002344 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002345 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002346 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002347 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002348 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002349 cmd->scsi_done(cmd);
2350 }
2351
Finn Thaine3c3da62014-11-12 16:12:15 +11002352 local_irq_restore(flags);
2353
Roman Zippelc28bda22007-05-01 22:32:36 +02002354 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2355 /*
2356 * Restore phase bits to 0 so an interrupted selection,
2357 * arbitration can resume.
2358 */
2359 NCR5380_write(TARGET_COMMAND_REG, 0);
2360
2361 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2362 barrier();
2363
Finn Thaine3c3da62014-11-12 16:12:15 +11002364 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002365 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002366 /* ++roman: For Falcon SCSI, release the lock on the
2367 * ST-DMA here if no other commands are waiting on the
2368 * disconnected queue.
2369 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002370 maybe_release_dma_irq(instance);
2371 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002372 return;
2373 case MESSAGE_REJECT:
2374 /* Accept message by clearing ACK */
2375 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2376 /* Enable reselect interrupts */
2377 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2378 switch (hostdata->last_message) {
2379 case HEAD_OF_QUEUE_TAG:
2380 case ORDERED_QUEUE_TAG:
2381 case SIMPLE_QUEUE_TAG:
2382 /* The target obviously doesn't support tagged
2383 * queuing, even though it announced this ability in
2384 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2385 * clear 'tagged_supported' and lock the LUN, since
2386 * the command is treated as untagged further on.
2387 */
2388 cmd->device->tagged_supported = 0;
2389 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2390 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002391 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002392 "QUEUE_TAG message; tagged queuing "
2393 "disabled\n",
2394 HOSTNO, cmd->device->id, cmd->device->lun);
2395 break;
2396 }
2397 break;
2398 case DISCONNECT:
2399 /* Accept message by clearing ACK */
2400 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2401 local_irq_save(flags);
2402 cmd->device->disconnect = 1;
2403 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002404 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002405 hostdata->connected = NULL;
2406 hostdata->disconnected_queue = cmd;
2407 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002408 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002409 "moved from connected to the "
2410 "disconnected_queue\n", HOSTNO,
2411 cmd->device->id, cmd->device->lun);
2412 /*
2413 * Restore phase bits to 0 so an interrupted selection,
2414 * arbitration can resume.
2415 */
2416 NCR5380_write(TARGET_COMMAND_REG, 0);
2417
2418 /* Enable reselect interrupts */
2419 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2420 /* Wait for bus free to avoid nasty timeouts */
2421 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2422 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002423#ifdef SUN3_SCSI_VME
2424 dregs->csr |= CSR_DMA_ENABLE;
2425#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002426 return;
2427 /*
2428 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2429 * operation, in violation of the SCSI spec so we can safely
2430 * ignore SAVE/RESTORE pointers calls.
2431 *
2432 * Unfortunately, some disks violate the SCSI spec and
2433 * don't issue the required SAVE_POINTERS message before
2434 * disconnecting, and we have to break spec to remain
2435 * compatible.
2436 */
2437 case SAVE_POINTERS:
2438 case RESTORE_POINTERS:
2439 /* Accept message by clearing ACK */
2440 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2441 /* Enable reselect interrupts */
2442 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2443 break;
2444 case EXTENDED_MESSAGE:
2445 /*
2446 * Extended messages are sent in the following format :
2447 * Byte
2448 * 0 EXTENDED_MESSAGE == 1
2449 * 1 length (includes one byte for code, doesn't
2450 * include first two bytes)
2451 * 2 code
2452 * 3..length+1 arguments
2453 *
2454 * Start the extended message buffer with the EXTENDED_MESSAGE
2455 * byte, since spi_print_msg() wants the whole thing.
2456 */
2457 extended_msg[0] = EXTENDED_MESSAGE;
2458 /* Accept first byte by clearing ACK */
2459 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2460
Finn Thaind65e6342014-03-18 11:42:20 +11002461 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002462
2463 len = 2;
2464 data = extended_msg + 1;
2465 phase = PHASE_MSGIN;
2466 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002467 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002468 (int)extended_msg[1], (int)extended_msg[2]);
2469
2470 if (!len && extended_msg[1] <=
2471 (sizeof(extended_msg) - 1)) {
2472 /* Accept third byte by clearing ACK */
2473 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2474 len = extended_msg[1] - 1;
2475 data = extended_msg + 3;
2476 phase = PHASE_MSGIN;
2477
2478 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002479 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002480 HOSTNO, len);
2481
2482 switch (extended_msg[2]) {
2483 case EXTENDED_SDTR:
2484 case EXTENDED_WDTR:
2485 case EXTENDED_MODIFY_DATA_POINTER:
2486 case EXTENDED_EXTENDED_IDENTIFY:
2487 tmp = 0;
2488 }
2489 } else if (len) {
2490 printk(KERN_NOTICE "scsi%d: error receiving "
2491 "extended message\n", HOSTNO);
2492 tmp = 0;
2493 } else {
2494 printk(KERN_NOTICE "scsi%d: extended message "
2495 "code %02x length %d is too long\n",
2496 HOSTNO, extended_msg[2], extended_msg[1]);
2497 tmp = 0;
2498 }
2499 /* Fall through to reject message */
2500
2501 /*
2502 * If we get something weird that we aren't expecting,
2503 * reject it.
2504 */
2505 default:
2506 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002507 printk(KERN_INFO "scsi%d: rejecting message ",
2508 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002509 spi_print_msg(extended_msg);
2510 printk("\n");
2511 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002512 scmd_printk(KERN_INFO, cmd,
2513 "rejecting unknown message %02x\n",
2514 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002515 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002516 scmd_printk(KERN_INFO, cmd,
2517 "rejecting unknown extended message code %02x, length %d\n",
2518 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002519
2520 msgout = MESSAGE_REJECT;
2521 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2522 break;
2523 } /* switch (tmp) */
2524 break;
2525 case PHASE_MSGOUT:
2526 len = 1;
2527 data = &msgout;
2528 hostdata->last_message = msgout;
2529 NCR5380_transfer_pio(instance, &phase, &len, &data);
2530 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002531 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002532#ifdef SUPPORT_TAGS
2533 cmd_free_tag(cmd);
2534#else
2535 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2536#endif
2537 hostdata->connected = NULL;
2538 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002539 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002540 maybe_release_dma_irq(instance);
2541 local_irq_restore(flags);
2542 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002543 return;
2544 }
2545 msgout = NOP;
2546 break;
2547 case PHASE_CMDOUT:
2548 len = cmd->cmd_len;
2549 data = cmd->cmnd;
2550 /*
2551 * XXX for performance reasons, on machines with a
2552 * PSEUDO-DMA architecture we should probably
2553 * use the dma transfer function.
2554 */
2555 NCR5380_transfer_pio(instance, &phase, &len, &data);
2556 break;
2557 case PHASE_STATIN:
2558 len = 1;
2559 data = &tmp;
2560 NCR5380_transfer_pio(instance, &phase, &len, &data);
2561 cmd->SCp.Status = tmp;
2562 break;
2563 default:
2564 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002565 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002566 } /* switch(phase) */
2567 } /* if (tmp * SR_REQ) */
2568 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
2571/*
2572 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2573 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002574 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002575 * 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 -07002576 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 * Inputs : instance - this instance of the NCR5380.
2579 *
2580 */
2581
2582
Finn Thaine3f463b2014-11-12 16:12:16 +11002583/* it might eventually prove necessary to do a dma setup on
2584 reselection, but it doesn't seem to be needed now -- sam */
2585
Roman Zippelc28bda22007-05-01 22:32:36 +02002586static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Roman Zippelc28bda22007-05-01 22:32:36 +02002588 SETUP_HOSTDATA(instance);
2589 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002590 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002592 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002594 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002595 int __maybe_unused len;
2596 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002597 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Roman Zippelc28bda22007-05-01 22:32:36 +02002599 /*
2600 * Disable arbitration, etc. since the host adapter obviously
2601 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Roman Zippelc28bda22007-05-01 22:32:36 +02002604 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Roman Zippelc28bda22007-05-01 22:32:36 +02002606 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2607
Finn Thaind65e6342014-03-18 11:42:20 +11002608 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002609
2610 /*
2611 * At this point, we have detected that our SCSI ID is on the bus,
2612 * SEL is true and BSY was false for at least one bus settle delay
2613 * (400 ns).
2614 *
2615 * We must assert BSY ourselves, until the target drops the SEL
2616 * signal.
2617 */
2618
2619 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2620
2621 while (NCR5380_read(STATUS_REG) & SR_SEL)
2622 ;
2623 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2624
2625 /*
2626 * Wait for target to go into MSGIN.
2627 */
2628
2629 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2630 ;
2631
Finn Thaine3f463b2014-11-12 16:12:16 +11002632#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2633 /* acknowledge toggle to MSGIN */
2634 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2635
2636 /* peek at the byte without really hitting the bus */
2637 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2638#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002639 len = 1;
2640 data = msg;
2641 phase = PHASE_MSGIN;
2642 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002643#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002644
2645 if (!(msg[0] & 0x80)) {
2646 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2647 spi_print_msg(msg);
2648 do_abort(instance);
2649 return;
2650 }
2651 lun = (msg[0] & 0x07);
2652
Finn Thaine3f463b2014-11-12 16:12:16 +11002653#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002654 /* If the phase is still MSGIN, the target wants to send some more
2655 * messages. In case it supports tagged queuing, this is probably a
2656 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2657 */
2658 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002659 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002660 /* Accept previous IDENTIFY message by clearing ACK */
2661 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2662 len = 2;
2663 data = msg + 1;
2664 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2665 msg[1] == SIMPLE_QUEUE_TAG)
2666 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002667 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002668 "reselection\n", HOSTNO, target_mask, lun, tag);
2669 }
2670#endif
2671
2672 /*
2673 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2674 * just reestablished, and remove it from the disconnected queue.
2675 */
2676
Finn Thain710ddd02014-11-12 16:12:02 +11002677 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002678 tmp; prev = tmp, tmp = NEXT(tmp)) {
2679 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2680#ifdef SUPPORT_TAGS
2681 && (tag == tmp->tag)
2682#endif
2683 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002684 if (prev) {
2685 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002686 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002687 } else {
2688 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2689 hostdata->disconnected_queue = NEXT(tmp);
2690 }
Roman Zippel3130d902007-05-01 22:32:37 +02002691 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002692 break;
2693 }
2694 }
2695
2696 if (!tmp) {
2697 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2698#ifdef SUPPORT_TAGS
2699 "tag %d "
2700#endif
2701 "not in disconnected_queue.\n",
2702 HOSTNO, target_mask, lun
2703#ifdef SUPPORT_TAGS
2704 , tag
2705#endif
2706 );
2707 /*
2708 * Since we have an established nexus that we can't do anything
2709 * with, we must abort it.
2710 */
2711 do_abort(instance);
2712 return;
2713 }
2714
Finn Thaine3f463b2014-11-12 16:12:16 +11002715#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2716 /* engage dma setup for the command we just saw */
2717 {
2718 void *d;
2719 unsigned long count;
2720
2721 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2722 count = tmp->SCp.buffer->length;
2723 d = sg_virt(tmp->SCp.buffer);
2724 } else {
2725 count = tmp->SCp.this_residual;
2726 d = tmp->SCp.ptr;
2727 }
2728 /* setup this command for dma if not already */
2729 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2730 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2731 sun3_dma_setup_done = tmp;
2732 }
2733 }
2734
2735 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2736#endif
2737
Roman Zippelc28bda22007-05-01 22:32:36 +02002738 /* Accept message by clearing ACK */
2739 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2740
Finn Thaine3f463b2014-11-12 16:12:16 +11002741#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2742 /* If the phase is still MSGIN, the target wants to send some more
2743 * messages. In case it supports tagged queuing, this is probably a
2744 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2745 */
2746 tag = TAG_NONE;
2747 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2748 /* Accept previous IDENTIFY message by clearing ACK */
2749 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2750 len = 2;
2751 data = msg + 1;
2752 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2753 msg[1] == SIMPLE_QUEUE_TAG)
2754 tag = msg[2];
2755 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2756 HOSTNO, target_mask, lun, tag);
2757 }
2758#endif
2759
Roman Zippelc28bda22007-05-01 22:32:36 +02002760 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002761 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002762 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765
2766/*
Finn Thain710ddd02014-11-12 16:12:02 +11002767 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 *
2769 * Purpose : abort a command
2770 *
Finn Thain710ddd02014-11-12 16:12:02 +11002771 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002772 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * used.
2774 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002775 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002777 * XXX - there is no way to abort the command that is currently
2778 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002780 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 */
2782
2783static
Finn Thain710ddd02014-11-12 16:12:02 +11002784int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Roman Zippelc28bda22007-05-01 22:32:36 +02002786 struct Scsi_Host *instance = cmd->device->host;
2787 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002788 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002789 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002791 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Roman Zippelc28bda22007-05-01 22:32:36 +02002793 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Roman Zippelc28bda22007-05-01 22:32:36 +02002795 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Finn Thaind65e6342014-03-18 11:42:20 +11002797 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002798 NCR5380_read(BUS_AND_STATUS_REG),
2799 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002802 /*
2803 * Case 1 : If the command is the currently executing command,
2804 * we'll set the aborted flag and return control so that
2805 * information transfer routine can exit cleanly.
2806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Roman Zippelc28bda22007-05-01 22:32:36 +02002808 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Finn Thaind65e6342014-03-18 11:42:20 +11002810 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002811 /*
2812 * We should perform BSY checking, and make sure we haven't slipped
2813 * into BUS FREE.
2814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Roman Zippelc28bda22007-05-01 22:32:36 +02002816 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2817 /*
2818 * Since we can't change phases until we've completed the current
2819 * handshake, we have to source or sink a byte of data if the current
2820 * phase is not MSGOUT.
2821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Roman Zippelc28bda22007-05-01 22:32:36 +02002823 /*
2824 * Return control to the executing NCR drive so we can clear the
2825 * aborted flag and get back into our main loop.
2826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Roman Zippelc28bda22007-05-01 22:32:36 +02002828 if (do_abort(instance) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002829 hostdata->connected = NULL;
2830 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002832 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002834 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002836 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002837 local_irq_restore(flags);
2838 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002839 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002840 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002841 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002842 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002843 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002847
2848 /*
2849 * Case 2 : If the command hasn't been issued yet, we simply remove it
2850 * from the issue queue.
2851 */
Finn Thain710ddd02014-11-12 16:12:02 +11002852 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2853 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002854 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2855 if (cmd == tmp) {
2856 REMOVE(5, *prev, tmp, NEXT(tmp));
2857 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002858 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002859 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002860 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002861 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002862 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002863 HOSTNO);
2864 /* Tagged queuing note: no tag to free here, hasn't been assigned
2865 * yet... */
2866 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002867 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869 }
2870
Roman Zippelc28bda22007-05-01 22:32:36 +02002871 /*
2872 * Case 3 : If any commands are connected, we're going to fail the abort
2873 * and let the high level SCSI driver retry at a later time or
2874 * issue a reset.
2875 *
2876 * Timeouts, and therefore aborted commands, will be highly unlikely
2877 * and handling them cleanly in this situation would make the common
2878 * case of noresets less efficient, and would pollute our code. So,
2879 * we fail.
2880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Roman Zippelc28bda22007-05-01 22:32:36 +02002882 if (hostdata->connected) {
2883 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002884 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002885 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Roman Zippelc28bda22007-05-01 22:32:36 +02002888 /*
2889 * Case 4: If the command is currently disconnected from the bus, and
2890 * there are no connected commands, we reconnect the I_T_L or
2891 * I_T_L_Q nexus associated with it, go into message out, and send
2892 * an abort message.
2893 *
2894 * This case is especially ugly. In order to reestablish the nexus, we
2895 * need to call NCR5380_select(). The easiest way to implement this
2896 * function was to abort if the bus was busy, and let the interrupt
2897 * handler triggered on the SEL for reselect take care of lost arbitrations
2898 * where necessary, meaning interrupts need to be enabled.
2899 *
2900 * When interrupts are enabled, the queues may change - so we
2901 * can't remove it from the disconnected queue before selecting it
2902 * because that could cause a failure in hashing the nexus if that
2903 * device reselected.
2904 *
2905 * Since the queues may change, we can't use the pointers from when we
2906 * first locate it.
2907 *
2908 * So, we must first locate the command, and if NCR5380_select()
2909 * succeeds, then issue the abort, relocate the command and remove
2910 * it from the disconnected queue.
2911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Finn Thain710ddd02014-11-12 16:12:02 +11002913 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002914 tmp = NEXT(tmp)) {
2915 if (cmd == tmp) {
2916 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002917 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002918
Finn Thain76f13b92014-11-12 16:11:53 +11002919 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002920 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002921
Finn Thaind65e6342014-03-18 11:42:20 +11002922 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002923
2924 do_abort(instance);
2925
2926 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002927 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2928 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002929 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2930 if (cmd == tmp) {
2931 REMOVE(5, *prev, tmp, NEXT(tmp));
2932 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002933 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002934 tmp->result = DID_ABORT << 16;
2935 /* We must unlock the tag/LUN immediately here, since the
2936 * target goes to BUS FREE and doesn't send us another
2937 * message (COMMAND_COMPLETE or the like)
2938 */
2939#ifdef SUPPORT_TAGS
2940 cmd_free_tag(tmp);
2941#else
2942 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2943#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002944 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002945 local_irq_restore(flags);
2946 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002947 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002948 }
2949 }
2950 }
2951 }
2952
Finn Thaine3c3da62014-11-12 16:12:15 +11002953 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2954 * possible at all) At least, we should check if the lock could be
2955 * released after the abort, in case it is kept due to some bug.
2956 */
2957 maybe_release_dma_irq(instance);
2958 local_irq_restore(flags);
2959
Roman Zippelc28bda22007-05-01 22:32:36 +02002960 /*
2961 * Case 5 : If we reached this point, the command was not found in any of
2962 * the queues.
2963 *
2964 * We probably reached this point because of an unlikely race condition
2965 * between the command completing successfully and the abortion code,
2966 * so we won't panic, but we will notify the user in case something really
2967 * broke.
2968 */
2969
Joe Perchesad361c92009-07-06 13:05:40 -07002970 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002971
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002972 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975
Finn Thain3be1b3e2016-01-03 16:05:12 +11002976/**
2977 * NCR5380_bus_reset - reset the SCSI bus
2978 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002979 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002980 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Finn Thain710ddd02014-11-12 16:12:02 +11002983static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Finn Thaine3c3da62014-11-12 16:12:15 +11002985 struct Scsi_Host *instance = cmd->device->host;
2986 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002987 int i;
2988 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Finn Thain3be1b3e2016-01-03 16:05:12 +11002990 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Finn Thain3be1b3e2016-01-03 16:05:12 +11002992#if (NDEBUG & NDEBUG_ANY)
2993 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2994 NCR5380_print_status(instance);
2995#endif
2996
2997 do_reset(instance);
2998
Roman Zippelc28bda22007-05-01 22:32:36 +02002999 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02003000 NCR5380_write(MODE_REG, MR_BASE);
3001 NCR5380_write(TARGET_COMMAND_REG, 0);
3002 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Roman Zippelc28bda22007-05-01 22:32:36 +02003004 /* After the reset, there are no more connected or disconnected commands
3005 * and no busy units; so clear the low-level status here to avoid
3006 * conflicts when the mid-level code tries to wake up the affected
3007 * commands!
3008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Roman Zippelc28bda22007-05-01 22:32:36 +02003010 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003011 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003012 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11003013 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02003014 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11003015 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Roman Zippelc28bda22007-05-01 22:32:36 +02003017 hostdata->issue_queue = NULL;
3018 hostdata->connected = NULL;
3019 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11003021 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02003023 for (i = 0; i < 8; ++i)
3024 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02003026 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11003028
3029 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02003030 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Michael Schmitz2b0f8342014-05-02 20:43:01 +12003032 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}