blob: 214f43b4baad933c113a5454a33daef290c55b0c [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
Finn Thaine3f463b2014-11-12 16:12:16 +110069/* Adapted for the sun3 by Sam Creasey. */
70
db9dff32005-04-03 14:53:59 -050071#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050072#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +020075#define LIST(x, y) \
76 do { \
77 printk("LINE:%d Adding %p to %p\n", \
78 __LINE__, (void*)(x), (void*)(y)); \
79 if ((x) == (y)) \
80 udelay(5); \
81 } while (0)
82#define REMOVE(w, x, y, z) \
83 do { \
84 printk("LINE:%d Removing: %p->%p %p->%p \n", \
85 __LINE__, (void*)(w), (void*)(x), \
86 (void*)(y), (void*)(z)); \
87 if ((x) == (y)) \
88 udelay(5); \
89 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#else
91#define LIST(x,y)
92#define REMOVE(w,x,y,z)
93#endif
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Design
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Roman Zippelc28bda22007-05-01 22:32:36 +020098 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200100 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * memory mapped) and drops this file in their 'C' wrapper.
102 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200103 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200105 * and commands that are currently executing. This means that an
106 * unlimited number of commands may be queued, letting
107 * more commands propagate from the higher driver levels giving higher
108 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
109 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * while a command is already executing.
111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200113 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
116 * piece of hardware that requires you to sit in a loop polling for
117 * the REQ signal as long as you are connected. Some devices are
118 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100119 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * broken devices are the exception rather than the rule and I'd rather
121 * spend my time optimizing for the normal case.
122 *
123 * Architecture :
124 *
125 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +1100126 * which is started from a workqueue for each NCR5380 host in the
127 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
128 * removing the commands from the issue queue and calling
129 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
131 * Once a nexus is established, the NCR5380_information_transfer()
132 * phase goes through the various phases as instructed by the target.
133 * if the target goes into MSG IN and sends a DISCONNECT message,
134 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +1100135 * queue, and NCR5380_main tries to find more work. If the target is
136 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * If a command has disconnected, eventually an interrupt will trigger,
139 * calling NCR5380_intr() which will in turn call NCR5380_reselect
140 * to reestablish a nexus. This will run main if necessary.
141 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200142 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * appropriate.
144 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200145 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * structures, being initialized after the command is connected
147 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
148 * Note that in violation of the standard, an implicit SAVE POINTERS operation
149 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
150 */
151
152/*
153 * Using this file :
154 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * and macros and include this file in your driver.
157 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200158 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200160 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100162 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
163 * transceivers.
164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
166 *
167 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
168 *
169 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * NCR5380_read(register) - read from the specified register
172 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200173 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100175 * NCR5380_implementation_fields - additional fields needed for this
176 * specific implementation of the NCR5380
177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200179 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200181 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * that they were able to program the controller for.
183 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200184 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * available as NCR5380_i386_dma_write_setup,
186 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
187 *
188 * NCR5380_dma_write_setup(instance, src, count) - initialize
189 * NCR5380_dma_read_setup(instance, dst, count) - initialize
190 * NCR5380_dma_residual(instance); - residual count
191 *
192 * PSEUDO functions :
193 * NCR5380_pwrite(instance, src, count)
194 * NCR5380_pread(instance, dst, count);
195 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200197 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100199 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* Macros ease life... :-) */
203#define SETUP_HOSTDATA(in) \
204 struct NCR5380_hostdata *hostdata = \
205 (struct NCR5380_hostdata *)(in)->hostdata
206#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
207
Finn Thain710ddd02014-11-12 16:12:02 +1100208#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200209#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100210#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#define HOSTNO instance->host_no
213#define H_NO(cmd) (cmd)->device->host->host_no
214
Finn Thain636b1ec2016-01-03 16:05:10 +1100215static int do_abort(struct Scsi_Host *);
216static void do_reset(struct Scsi_Host *);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#ifdef SUPPORT_TAGS
219
220/*
221 * Functions for handling tagged queuing
222 * =====================================
223 *
224 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
225 *
226 * Using consecutive numbers for the tags is no good idea in my eyes. There
227 * could be wrong re-usings if the counter (8 bit!) wraps and some early
228 * command has been preempted for a long time. My solution: a bitfield for
229 * remembering used tags.
230 *
231 * There's also the problem that each target has a certain queue size, but we
232 * cannot know it in advance :-( We just see a QUEUE_FULL status being
233 * returned. So, in this case, the driver internal queue size assumption is
234 * reduced to the number of active tags if QUEUE_FULL is returned by the
235 * target. The command is returned to the mid-level, but with status changed
236 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
237 * correctly.
238 *
239 * We're also not allowed running tagged commands as long as an untagged
240 * command is active. And REQUEST SENSE commands after a contingent allegiance
241 * condition _must_ be untagged. To keep track whether an untagged command has
242 * been issued, the host->busy array is still employed, as it is without
243 * support for tagged queuing.
244 *
245 * One could suspect that there are possible race conditions between
246 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
247 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
248 * which already guaranteed to be running at most once. It is also the only
249 * place where tags/LUNs are allocated. So no other allocation can slip
250 * between that pair, there could only happen a reselection, which can free a
251 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
252 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
253 */
254
Finn Thainca513fc2014-11-12 16:12:19 +1100255static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Roman Zippelc28bda22007-05-01 22:32:36 +0200257 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100258 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200259
Finn Thainca513fc2014-11-12 16:12:19 +1100260 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200261 return;
262
263 for (target = 0; target < 8; ++target) {
264 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100265 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200266 bitmap_zero(ta->allocated, MAX_TAGS);
267 ta->nr_allocated = 0;
268 /* At the beginning, assume the maximum queue size we could
269 * support (MAX_TAGS). This value will be decreased if the target
270 * returns QUEUE_FULL status.
271 */
272 ta->queue_size = MAX_TAGS;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277
278/* Check if we can issue a command to this LUN: First see if the LUN is marked
279 * busy by an untagged command. If the command should use tagged queuing, also
280 * check that there is a free tag and the target's queue won't overflow. This
281 * function should be called with interrupts disabled to avoid race
282 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Finn Thain710ddd02014-11-12 16:12:02 +1100285static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200287 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200288 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200290 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200291 return 1;
292 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100293 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
294 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200295 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100296 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
297 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100298 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200299 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200300 return 1;
301 }
302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305
306/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
307 * must be called before!), or reserve the LUN in 'busy' if the command is
308 * untagged.
309 */
310
Finn Thain710ddd02014-11-12 16:12:02 +1100311static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200313 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200314 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Roman Zippelc28bda22007-05-01 22:32:36 +0200316 /* If we or the target don't support tagged queuing, allocate the LUN for
317 * an untagged command.
318 */
319 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100320 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
321 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200323 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100324 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200325 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200326 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100327 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Roman Zippelc28bda22007-05-01 22:32:36 +0200329 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
330 set_bit(cmd->tag, ta->allocated);
331 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100332 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200333 "(now %d tags in use)\n",
334 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200335 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339
340/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
341 * unlock the LUN.
342 */
343
Finn Thain710ddd02014-11-12 16:12:02 +1100344static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200346 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200347 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Roman Zippelc28bda22007-05-01 22:32:36 +0200349 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200350 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100351 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200352 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200353 } else if (cmd->tag >= MAX_TAGS) {
354 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
355 H_NO(cmd), cmd->tag);
356 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100357 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200358 clear_bit(cmd->tag, ta->allocated);
359 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100360 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200361 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365
Finn Thainca513fc2014-11-12 16:12:19 +1100366static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Roman Zippelc28bda22007-05-01 22:32:36 +0200368 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100369 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Finn Thainca513fc2014-11-12 16:12:19 +1100371 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200372 return;
373
374 for (target = 0; target < 8; ++target) {
375 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100376 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200377 bitmap_zero(ta->allocated, MAX_TAGS);
378 ta->nr_allocated = 0;
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383#endif /* SUPPORT_TAGS */
384
385
386/*
Finn Thain710ddd02014-11-12 16:12:02 +1100387 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
389 * Purpose: Try to merge several scatter-gather requests into one DMA
390 * transfer. This is possible if the scatter buffers lie on
391 * physical contiguous addresses.
392 *
Finn Thain710ddd02014-11-12 16:12:02 +1100393 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300395 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
397
Finn Thain710ddd02014-11-12 16:12:02 +1100398static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Finn Thaine3f463b2014-11-12 16:12:16 +1100400#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200401 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200403 unsigned long oldlen = cmd->SCp.this_residual;
404 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#endif
406
Roman Zippelc28bda22007-05-01 22:32:36 +0200407 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
408 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200409 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100410 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200411 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200413 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200415 ++cmd->SCp.buffer;
416 --cmd->SCp.buffers_residual;
417 cmd->SCp.this_residual += cmd->SCp.buffer->length;
418 endaddr += cmd->SCp.buffer->length;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200421 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100422 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200423 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100425#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Finn Thainff50f9e2014-11-12 16:12:18 +1100428/**
429 * initialize_SCp - init the scsi pointer field
430 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100432 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434
Finn Thain710ddd02014-11-12 16:12:02 +1100435static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Roman Zippelc28bda22007-05-01 22:32:36 +0200437 /*
438 * Initialize the Scsi Pointer field so that all of the commands in the
439 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200441
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200442 if (scsi_bufflen(cmd)) {
443 cmd->SCp.buffer = scsi_sglist(cmd);
444 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200445 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200446 cmd->SCp.this_residual = cmd->SCp.buffer->length;
447 /* ++roman: Try to merge some scatter-buffers if they are at
448 * contiguous physical addresses.
449 */
450 merge_contiguous_buffers(cmd);
451 } else {
452 cmd->SCp.buffer = NULL;
453 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200454 cmd->SCp.ptr = NULL;
455 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Finn Thain636b1ec2016-01-03 16:05:10 +1100459/**
Finn Thainb32ade12016-01-03 16:05:41 +1100460 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain636b1ec2016-01-03 16:05:10 +1100461 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100462 * @reg1: 5380 register to poll
463 * @bit1: Bitmask to check
464 * @val1: Expected value
465 * @reg2: Second 5380 register to poll
466 * @bit2: Second bitmask to check
467 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100468 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100469 *
Finn Thain2f854b82016-01-03 16:05:22 +1100470 * Polls the chip in a reasonably efficient manner waiting for an
471 * event to occur. After a short quick poll we begin to yield the CPU
472 * (if possible). In irq contexts the time-out is arbitrarily limited.
473 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100474 *
Finn Thainb32ade12016-01-03 16:05:41 +1100475 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100476 */
477
Finn Thainb32ade12016-01-03 16:05:41 +1100478static int NCR5380_poll_politely2(struct Scsi_Host *instance,
479 int reg1, int bit1, int val1,
480 int reg2, int bit2, int val2, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100481{
Finn Thain2f854b82016-01-03 16:05:22 +1100482 struct NCR5380_hostdata *hostdata = shost_priv(instance);
483 unsigned long deadline = jiffies + wait;
484 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100485
Finn Thain2f854b82016-01-03 16:05:22 +1100486 /* Busy-wait for up to 10 ms */
487 n = min(10000U, jiffies_to_usecs(wait));
488 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100489 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100490 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100491 if ((NCR5380_read(reg1) & bit1) == val1)
492 return 0;
493 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain636b1ec2016-01-03 16:05:10 +1100494 return 0;
495 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100496 } while (n--);
497
498 if (irqs_disabled() || in_interrupt())
499 return -ETIMEDOUT;
500
501 /* Repeatedly sleep for 1 ms until deadline */
502 while (time_is_after_jiffies(deadline)) {
503 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100504 if ((NCR5380_read(reg1) & bit1) == val1)
505 return 0;
506 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain2f854b82016-01-03 16:05:22 +1100507 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100508 }
509
Finn Thain636b1ec2016-01-03 16:05:10 +1100510 return -ETIMEDOUT;
511}
512
Finn Thainb32ade12016-01-03 16:05:41 +1100513static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
514 int reg, int bit, int val, int wait)
515{
516 return NCR5380_poll_politely2(instance, reg, bit, val,
517 reg, bit, val, wait);
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#include <linux/delay.h>
521
522#if NDEBUG
523static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200524 unsigned char mask;
525 const char *name;
526} signals[] = {
527 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
528 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
529 { SR_SEL, "SEL" }, {0, NULL}
530}, basrs[] = {
531 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
532}, icrs[] = {
533 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
534 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
535 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
536 {0, NULL}
537}, mrs[] = {
538 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
539 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
540 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
541 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
542 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
543 {0, NULL}
544};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Finn Thainff50f9e2014-11-12 16:12:18 +1100546/**
547 * NCR5380_print - print scsi bus signals
548 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100550 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
552
Roman Zippelc28bda22007-05-01 22:32:36 +0200553static void NCR5380_print(struct Scsi_Host *instance)
554{
555 unsigned char status, data, basr, mr, icr, i;
556 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Roman Zippelc28bda22007-05-01 22:32:36 +0200558 local_irq_save(flags);
559 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
560 status = NCR5380_read(STATUS_REG);
561 mr = NCR5380_read(MODE_REG);
562 icr = NCR5380_read(INITIATOR_COMMAND_REG);
563 basr = NCR5380_read(BUS_AND_STATUS_REG);
564 local_irq_restore(flags);
565 printk("STATUS_REG: %02x ", status);
566 for (i = 0; signals[i].mask; ++i)
567 if (status & signals[i].mask)
568 printk(",%s", signals[i].name);
569 printk("\nBASR: %02x ", basr);
570 for (i = 0; basrs[i].mask; ++i)
571 if (basr & basrs[i].mask)
572 printk(",%s", basrs[i].name);
573 printk("\nICR: %02x ", icr);
574 for (i = 0; icrs[i].mask; ++i)
575 if (icr & icrs[i].mask)
576 printk(",%s", icrs[i].name);
577 printk("\nMODE: %02x ", mr);
578 for (i = 0; mrs[i].mask; ++i)
579 if (mr & mrs[i].mask)
580 printk(",%s", mrs[i].name);
581 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200585 unsigned char value;
586 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200588 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
589 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
590 {PHASE_UNKNOWN, "UNKNOWN"}
591};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Finn Thainff50f9e2014-11-12 16:12:18 +1100593/**
594 * NCR5380_print_phase - show SCSI phase
595 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100597 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100599 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
602static void NCR5380_print_phase(struct Scsi_Host *instance)
603{
Roman Zippelc28bda22007-05-01 22:32:36 +0200604 unsigned char status;
605 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Roman Zippelc28bda22007-05-01 22:32:36 +0200607 status = NCR5380_read(STATUS_REG);
608 if (!(status & SR_REQ))
609 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
610 else {
611 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
612 (phases[i].value != (status & PHASE_MASK)); ++i)
613 ;
614 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#endif
619
Finn Thain8c325132014-11-12 16:11:58 +1100620/**
621 * NCR58380_info - report driver and host information
622 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *
Finn Thain8c325132014-11-12 16:11:58 +1100624 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
Finn Thain8c325132014-11-12 16:11:58 +1100626 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628
Finn Thain8c325132014-11-12 16:11:58 +1100629static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Finn Thain8c325132014-11-12 16:11:58 +1100631 struct NCR5380_hostdata *hostdata = shost_priv(instance);
632
633 return hostdata->info;
634}
635
636static void prepare_info(struct Scsi_Host *instance)
637{
638 struct NCR5380_hostdata *hostdata = shost_priv(instance);
639
640 snprintf(hostdata->info, sizeof(hostdata->info),
641 "%s, io_port 0x%lx, n_io_port %d, "
642 "base 0x%lx, irq %d, "
643 "can_queue %d, cmd_per_lun %d, "
644 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100645 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100646 "options { %s} ",
647 instance->hostt->name, instance->io_port, instance->n_io_port,
648 instance->base, instance->irq,
649 instance->can_queue, instance->cmd_per_lun,
650 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100651 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100652 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100653#ifdef DIFFERENTIAL
654 "DIFFERENTIAL "
655#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100657 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#endif
659#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100660 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
662#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100663 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#endif
Finn Thain8c325132014-11-12 16:11:58 +1100665 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Finn Thainff50f9e2014-11-12 16:12:18 +1100668/**
669 * NCR5380_print_status - dump controller info
670 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100672 * Print commands in the various queues, called from NCR5380_abort
673 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675
Finn Thain710ddd02014-11-12 16:12:02 +1100676static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400677{
678 int i, s;
679 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200680 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400681 H_NO(cmd), cmd->device->id, cmd->device->lun);
682 printk(KERN_CONT " command = ");
683 command = cmd->cmnd;
684 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
685 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
686 printk(KERN_CONT " %02x", command[i]);
687 printk("\n");
688}
689
Finn Thain3be1b3e2016-01-03 16:05:12 +1100690static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Al Virod89537e2013-03-31 13:24:44 -0400692 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100693 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400694 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Finn Thain8ad3a592014-03-18 11:42:19 +1100696 NCR5380_dprint(NDEBUG_ANY, instance);
697 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Roman Zippelc28bda22007-05-01 22:32:36 +0200699 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Roman Zippelc28bda22007-05-01 22:32:36 +0200701 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +0200702 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400703 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200704 else
Finn Thain710ddd02014-11-12 16:12:02 +1100705 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400706 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100707 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400708 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Al Virod89537e2013-03-31 13:24:44 -0400710 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100711 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400712 ptr = NEXT(ptr))
713 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200714
715 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400716 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Finn Thain710ddd02014-11-12 16:12:02 +1100719static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Roman Zippelc28bda22007-05-01 22:32:36 +0200721 int i, s;
722 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200723 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200724 H_NO(cmd), cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100725 seq_puts(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200726 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400727 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200728 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400729 seq_printf(m, " %02x", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100730 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Finn Thain4d3d2a52014-11-12 16:11:52 +1100733static int __maybe_unused NCR5380_show_info(struct seq_file *m,
734 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400735{
736 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100737 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400738 unsigned long flags;
739
740 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
741
Al Virod89537e2013-03-31 13:24:44 -0400742 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400743 if (!hostdata->connected)
744 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
745 else
Finn Thain710ddd02014-11-12 16:12:02 +1100746 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400747 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100748 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400749 show_Scsi_Cmnd(ptr, m);
750
751 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100752 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400753 ptr = NEXT(ptr))
754 show_Scsi_Cmnd(ptr, m);
755
756 local_irq_restore(flags);
757 return 0;
758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Finn Thainff50f9e2014-11-12 16:12:18 +1100760/**
761 * NCR5380_init - initialise an NCR5380
762 * @instance: adapter to configure
763 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100765 * Initializes *instance and corresponding 5380 chip,
766 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
768 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100769 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200770 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100771 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
773
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100774static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Roman Zippelc28bda22007-05-01 22:32:36 +0200776 int i;
777 SETUP_HOSTDATA(instance);
Finn Thain2f854b82016-01-03 16:05:22 +1100778 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Finn Thaina53a21e2014-11-12 16:12:21 +1100780 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200781 hostdata->id_mask = 1 << instance->this_id;
782 hostdata->id_higher_mask = 0;
783 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
784 if (i > hostdata->id_mask)
785 hostdata->id_higher_mask |= i;
786 for (i = 0; i < 8; ++i)
787 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100789 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif
791#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200792 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200794 hostdata->connected = NULL;
795 hostdata->issue_queue = NULL;
796 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100797 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Finn Thaina53a21e2014-11-12 16:12:21 +1100799 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100800 hostdata->work_q = alloc_workqueue("ncr5380_%d",
801 WQ_UNBOUND | WQ_MEM_RECLAIM,
802 1, instance->host_no);
803 if (!hostdata->work_q)
804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Finn Thain8c325132014-11-12 16:11:58 +1100806 prepare_info(instance);
807
Roman Zippelc28bda22007-05-01 22:32:36 +0200808 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
809 NCR5380_write(MODE_REG, MR_BASE);
810 NCR5380_write(TARGET_COMMAND_REG, 0);
811 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Finn Thain2f854b82016-01-03 16:05:22 +1100813 /* Calibrate register polling loop */
814 i = 0;
815 deadline = jiffies + 1;
816 do {
817 cpu_relax();
818 } while (time_is_after_jiffies(deadline));
819 deadline += msecs_to_jiffies(256);
820 do {
821 NCR5380_read(STATUS_REG);
822 ++i;
823 cpu_relax();
824 } while (time_is_after_jiffies(deadline));
825 hostdata->accesses_per_ms = i / 256;
826
Roman Zippelc28bda22007-05-01 22:32:36 +0200827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Finn Thainff50f9e2014-11-12 16:12:18 +1100830/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100831 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
832 * @instance: adapter to check
833 *
834 * If the system crashed, it may have crashed with a connected target and
835 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
836 * currently established nexus, which we know nothing about. Failing that
837 * do a bus reset.
838 *
839 * Note that a bus reset will cause the chip to assert IRQ.
840 *
841 * Returns 0 if successful, otherwise -ENXIO.
842 */
843
844static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
845{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100846 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100847 int pass;
848
849 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
850 switch (pass) {
851 case 1:
852 case 3:
853 case 5:
854 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
855 NCR5380_poll_politely(instance,
856 STATUS_REG, SR_BSY, 0, 5 * HZ);
857 break;
858 case 2:
859 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
860 do_abort(instance);
861 break;
862 case 4:
863 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
864 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100865 /* Wait after a reset; the SCSI standard calls for
866 * 250ms, we wait 500ms to be on the safe side.
867 * But some Toshiba CD-ROMs need ten times that.
868 */
869 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
870 msleep(2500);
871 else
872 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100873 break;
874 case 6:
875 shost_printk(KERN_ERR, instance, "bus locked solid\n");
876 return -ENXIO;
877 }
878 }
879 return 0;
880}
881
882/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100883 * NCR5380_exit - remove an NCR5380
884 * @instance: adapter to remove
885 *
886 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
887 */
888
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200889static void NCR5380_exit(struct Scsi_Host *instance)
890{
Finn Thaina53a21e2014-11-12 16:12:21 +1100891 struct NCR5380_hostdata *hostdata = shost_priv(instance);
892
893 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100894 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200895}
896
Finn Thainff50f9e2014-11-12 16:12:18 +1100897/**
898 * NCR5380_queue_command - queue a command
899 * @instance: the relevant SCSI adapter
900 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *
Finn Thain1bb40582016-01-03 16:05:29 +1100902 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100903 * twiddling done to the host specific fields of cmd. If the
904 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
906
Finn Thain16b29e72014-11-12 16:12:08 +1100907static int NCR5380_queue_command(struct Scsi_Host *instance,
908 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Finn Thain16b29e72014-11-12 16:12:08 +1100910 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100911 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200912 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200915 switch (cmd->cmnd[0]) {
916 case WRITE_6:
917 case WRITE_10:
918 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
919 H_NO(cmd));
920 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100921 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200922 return 0;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
925
Roman Zippelc28bda22007-05-01 22:32:36 +0200926 /*
927 * We use the host_scribble field as a pointer to the next command
928 * in a queue
929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Roman Zippel3130d902007-05-01 22:32:37 +0200931 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200932 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Roman Zippelc28bda22007-05-01 22:32:36 +0200934 /*
935 * Insert the cmd into the issue queue. Note that REQUEST SENSE
936 * commands are added to the head of the queue since any command will
937 * clear the contingent allegiance condition that exists and the
938 * sense data is only guaranteed to be valid while the condition exists.
939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Roman Zippelc28bda22007-05-01 22:32:36 +0200941 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
942 * Otherwise a running NCR5380_main may steal the lock.
943 * Lock before actually inserting due to fairness reasons explained in
944 * atari_scsi.c. If we insert first, then it's impossible for this driver
945 * to release the lock.
946 * Stop timer for this command while waiting for the lock, or timeouts
947 * may happen (and they really do), and it's no good if the command doesn't
948 * appear in any of the queues.
949 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
950 * because also a timer int can trigger an abort or reset, which would
951 * alter queues and touch the lock.
952 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100953 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100954 return SCSI_MLQUEUE_HOST_BUSY;
955
956 local_irq_save(flags);
957
Finn Thainff50f9e2014-11-12 16:12:18 +1100958 /*
959 * Insert the cmd into the issue queue. Note that REQUEST SENSE
960 * commands are added to the head of the queue since any command will
961 * clear the contingent allegiance condition that exists and the
962 * sense data is only guaranteed to be valid while the condition exists.
963 */
964
Roman Zippelc28bda22007-05-01 22:32:36 +0200965 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
966 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200967 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200968 hostdata->issue_queue = cmd;
969 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100970 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200971 NEXT(tmp); tmp = NEXT(tmp))
972 ;
973 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200974 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200975 }
976 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Finn Thaind65e6342014-03-18 11:42:20 +1100978 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200979 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Finn Thain010e89d2016-01-03 16:05:38 +1100981 /* Kick off command processing */
982 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Finn Thaine3c3da62014-11-12 16:12:15 +1100986static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
987{
988 struct NCR5380_hostdata *hostdata = shost_priv(instance);
989
990 /* Caller does the locking needed to set & test these data atomically */
991 if (!hostdata->disconnected_queue &&
992 !hostdata->issue_queue &&
993 !hostdata->connected &&
994 !hostdata->retain_dma_intr)
995 NCR5380_release_dma_irq(instance);
996}
997
Finn Thainff50f9e2014-11-12 16:12:18 +1100998/**
999 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001001 * NCR5380_main is a coroutine that runs as long as more work can
1002 * be done on the NCR5380 host adapters in a system. Both
1003 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1004 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +02001005 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001006 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +02001007 */
1008
Geert Uytterhoevenb312b382007-05-01 22:32:48 +02001009static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Finn Thaina53a21e2014-11-12 16:12:21 +11001011 struct NCR5380_hostdata *hostdata =
1012 container_of(work, struct NCR5380_hostdata, main_task);
1013 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +11001014 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02001015 int done;
1016 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Roman Zippelc28bda22007-05-01 22:32:36 +02001018 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001019 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1020 * because also a timer int can trigger an abort or reset, which can
1021 * alter queues and touch the Falcon lock.
1022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Roman Zippelc28bda22007-05-01 22:32:36 +02001024 local_save_flags(flags);
1025 do {
1026 local_irq_disable(); /* Freeze request queues */
1027 done = 1;
1028
1029 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001030 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 /*
1032 * Search through the issue_queue for a command destined
1033 * for a target that's not busy.
1034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001036 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001037 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1038 ;
1039 /*printk("%p ", tmp);*/
1040 if ((tmp == prev) && tmp)
1041 printk(" LOOP\n");
1042 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001044 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001045 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001046 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Finn Thaine3f463b2014-11-12 16:12:16 +11001048 dprintk(NDEBUG_LISTS,
1049 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1050 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1051 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001052 /* When we find one, remove it from the issue queue. */
1053 /* ++guenther: possible race with Falcon locking */
1054 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001056 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001058 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001059#endif
1060 ) {
1061 /* ++guenther: just to be sure, this must be atomic */
1062 local_irq_disable();
1063 if (prev) {
1064 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001065 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001066 } else {
1067 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1068 hostdata->issue_queue = NEXT(tmp);
1069 }
Roman Zippel3130d902007-05-01 22:32:37 +02001070 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001071 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001072
1073 /* reenable interrupts after finding one */
1074 local_irq_restore(flags);
1075
1076 /*
1077 * Attempt to establish an I_T_L nexus here.
1078 * On success, instance->hostdata->connected is set.
1079 * On failure, we must add the command back to the
1080 * issue queue so we can keep trying.
1081 */
Finn Thaind65e6342014-03-18 11:42:20 +11001082 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001083 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001084 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001085 /*
1086 * REQUEST SENSE commands are issued without tagged
1087 * queueing, even on SCSI-II devices because the
1088 * contingent allegiance condition exists for the
1089 * entire unit.
1090 */
1091 /* ++roman: ...and the standard also requires that
1092 * REQUEST SENSE command are untagged.
1093 */
1094
1095#ifdef SUPPORT_TAGS
1096 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1097#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001098 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001099 /* OK or bad target */
Finn Thaine3c3da62014-11-12 16:12:15 +11001100 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001101 hostdata->retain_dma_intr--;
Finn Thaine3c3da62014-11-12 16:12:15 +11001102 maybe_release_dma_irq(instance);
1103 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001104 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001105 /* Need to retry */
Roman Zippelc28bda22007-05-01 22:32:36 +02001106 local_irq_disable();
1107 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001108 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001109 hostdata->issue_queue = tmp;
1110#ifdef SUPPORT_TAGS
1111 cmd_free_tag(tmp);
1112#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001113 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001114 local_irq_restore(flags);
Finn Thain1d3db592016-01-03 16:05:24 +11001115 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001116 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001117 "returned to issue_queue\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001118 }
Finn Thain1f1b0c72016-01-03 16:05:17 +11001119 if (hostdata->connected)
1120 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001121 } /* if target/lun/target queue is not busy */
1122 } /* for issue_queue */
1123 } /* if (!hostdata->connected) */
1124
1125 if (hostdata->connected
1126#ifdef REAL_DMA
1127 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001131 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001132 HOSTNO);
1133 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001134 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001135 done = 0;
1136 }
1137 } while (!done);
Roman Zippelc28bda22007-05-01 22:32:36 +02001138 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141
1142#ifdef REAL_DMA
1143/*
1144 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1145 *
1146 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001147 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
1149 * Inputs : instance - this instance of the NCR5380.
1150 *
1151 */
1152
Roman Zippelc28bda22007-05-01 22:32:36 +02001153static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Roman Zippelc28bda22007-05-01 22:32:36 +02001155 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001156 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001157 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001158 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001159 int saved_data = 0, overrun = 0;
1160 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Finn Thainef1081c2014-11-12 16:12:14 +11001162 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001163 p = hostdata->connected->SCp.phase;
1164 if (p & SR_IO) {
1165 udelay(10);
1166 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1167 (BASR_PHASE_MATCH|BASR_ACK)) ==
1168 (BASR_PHASE_MATCH|BASR_ACK)) {
1169 saved_data = NCR5380_read(INPUT_DATA_REG);
1170 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001171 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001172 }
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001175
Finn Thaine3f463b2014-11-12 16:12:16 +11001176#if defined(CONFIG_SUN3)
1177 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1178 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1179 instance->host_no);
1180 BUG();
1181 }
1182
1183 /* make sure we're not stuck in a data phase */
1184 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1185 (BASR_PHASE_MATCH | BASR_ACK)) {
1186 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1187 NCR5380_read(BUS_AND_STATUS_REG));
1188 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1189 instance->host_no);
1190 BUG();
1191 }
1192#endif
1193
Roman Zippelc28bda22007-05-01 22:32:36 +02001194 NCR5380_write(MODE_REG, MR_BASE);
1195 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001196 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001197
Finn Thain01bd9082014-11-12 16:12:23 +11001198 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001199 hostdata->dma_len = 0;
1200
1201 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1202 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001203 *data += transferred;
1204 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001205
Finn Thainef1081c2014-11-12 16:12:14 +11001206 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001207 int cnt, toPIO;
1208
Roman Zippelc28bda22007-05-01 22:32:36 +02001209 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001210 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001211 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001212 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001213 *(*data)++ = saved_data;
1214 (*count)--;
1215 cnt--;
1216 toPIO--;
1217 }
Finn Thaind65e6342014-03-18 11:42:20 +11001218 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001219 NCR5380_transfer_pio(instance, &p, &cnt, data);
1220 *count -= toPIO - cnt;
1221 }
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224#endif /* REAL_DMA */
1225
1226
Finn Thainff50f9e2014-11-12 16:12:18 +11001227/**
1228 * NCR5380_intr - generic NCR5380 irq handler
1229 * @irq: interrupt number
1230 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001231 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001232 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1233 * from the disconnected queue, and restarting NCR5380_main()
1234 * as required.
Finn Thaincd400822016-01-03 16:05:40 +11001235 *
1236 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1237 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1238 * Three of these six conditions are latched in the Bus and Status Register:
1239 * - End of DMA (cleared by ending DMA Mode)
1240 * - Parity error (cleared by reading RPIR)
1241 * - Loss of BSY (cleared by reading RPIR)
1242 * Two conditions have flag bits that are not latched:
1243 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1244 * - Bus reset (non-maskable)
1245 * The remaining condition has no flag bit at all:
1246 * - Selection/reselection
1247 *
1248 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1249 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1250 * claimed that "the design of the [DP8490] interrupt logic ensures
1251 * interrupts will not be lost (they can be on the DP5380)."
1252 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1253 *
1254 * Checking for bus reset by reading RST is futile because of interrupt
1255 * latency, but a bus reset will reset chip logic. Checking for parity error
1256 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1257 * condition will clear DMA Mode. We can tell when this occurs because the
1258 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260
Roman Zippelc28bda22007-05-01 22:32:36 +02001261static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Finn Thaina53a21e2014-11-12 16:12:21 +11001263 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001264 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001265 int handled = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001266 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 basr = NCR5380_read(BUS_AND_STATUS_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001269 if (basr & BASR_IRQ) {
Finn Thaincd400822016-01-03 16:05:40 +11001270 unsigned char mr = NCR5380_read(MODE_REG);
1271 unsigned char sr = NCR5380_read(STATUS_REG);
1272
1273 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1274 HOSTNO, irq, basr, sr, mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001277 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1278 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1279 * We ack IRQ after clearing Mode Register. Workarounds
1280 * for End of DMA errata need to happen in DMA Mode.
Roman Zippelc28bda22007-05-01 22:32:36 +02001281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Finn Thaincd400822016-01-03 16:05:40 +11001283 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001284
Finn Thaincd400822016-01-03 16:05:40 +11001285 if (hostdata->connected) {
1286 NCR5380_dma_complete(instance);
1287 queue_work(hostdata->work_q, &hostdata->main_task);
1288 } else {
1289 NCR5380_write(MODE_REG, MR_BASE);
1290 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001291 }
Finn Thaincd400822016-01-03 16:05:40 +11001292 } else
1293#endif /* REAL_DMA */
1294 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1295 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1296 /* Probably reselected */
1297 NCR5380_write(SELECT_ENABLE_REG, 0);
1298 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1299
1300 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1301 HOSTNO);
1302
1303 if (!hostdata->connected) {
1304 NCR5380_reselect(instance);
1305 queue_work(hostdata->work_q, &hostdata->main_task);
1306 }
1307 if (!hostdata->connected)
1308 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1309 } else {
1310 /* Probably Bus Reset */
1311 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1312
1313 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", HOSTNO);
1314#ifdef SUN3_SCSI_VME
1315 dregs->csr |= CSR_DMA_ENABLE;
1316#endif
1317 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 handled = 1;
Finn Thaincd400822016-01-03 16:05:40 +11001319 } else {
1320 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine3f463b2014-11-12 16:12:16 +11001321#ifdef SUN3_SCSI_VME
1322 dregs->csr |= CSR_DMA_ENABLE;
1323#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001324 }
1325
Roman Zippelc28bda22007-05-01 22:32:36 +02001326 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Roman Zippelc28bda22007-05-01 22:32:36 +02001329/*
Finn Thain710ddd02014-11-12 16:12:02 +11001330 * Function : int NCR5380_select(struct Scsi_Host *instance,
1331 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
1333 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001334 * including ARBITRATION, SELECTION, and initial message out for
1335 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001337 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001338 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *
Finn Thain63238762016-01-03 16:05:15 +11001340 * Returns : -1 if selection failed but should be retried.
1341 * 0 if selection failed and should not be retried.
1342 * 0 if selection succeeded completely (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001343 *
1344 * Side effects :
1345 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * with registers as they should have been on entry - ie
1347 * SELECT_ENABLE will be set appropriately, the NCR5380
1348 * will cease to drive any SCSI bus signals.
1349 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001350 * If successful : I_T_L or I_T_L_Q nexus will be established,
1351 * instance->connected will be set to cmd.
1352 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001354 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * cmd->result host byte set to DID_BAD_TARGET.
1356 */
1357
Finn Thain710ddd02014-11-12 16:12:02 +11001358static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Roman Zippelc28bda22007-05-01 22:32:36 +02001360 SETUP_HOSTDATA(instance);
1361 unsigned char tmp[3], phase;
1362 unsigned char *data;
1363 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001364 int err;
Roman Zippelc28bda22007-05-01 22:32:36 +02001365 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Finn Thain8ad3a592014-03-18 11:42:19 +11001367 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001368 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001369 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Roman Zippelc28bda22007-05-01 22:32:36 +02001371 /*
1372 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1373 * data bus during SELECTION.
1374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Roman Zippelc28bda22007-05-01 22:32:36 +02001376 local_irq_save(flags);
1377 if (hostdata->connected) {
1378 local_irq_restore(flags);
1379 return -1;
1380 }
1381 NCR5380_write(TARGET_COMMAND_REG, 0);
1382
1383 /*
1384 * Start arbitration.
1385 */
1386
1387 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1388 NCR5380_write(MODE_REG, MR_ARBITRATE);
1389
Finn Thain55500d92016-01-03 16:05:35 +11001390 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1391 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Finn Thain55500d92016-01-03 16:05:35 +11001394 local_irq_restore(flags);
Finn Thainb32ade12016-01-03 16:05:41 +11001395 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1396 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1397 ICR_ARBITRATION_PROGRESS, HZ);
1398 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1399 /* Reselection interrupt */
1400 return -1;
1401 }
1402 if (err < 0) {
1403 NCR5380_write(MODE_REG, MR_BASE);
1404 shost_printk(KERN_ERR, instance,
1405 "select: arbitration timeout\n");
1406 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001407 }
1408
1409 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001410 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 /* Check for lost arbitration */
1413 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1414 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1415 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1416 hostdata->connected) {
1417 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001418 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001419 HOSTNO);
1420 return -1;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Finn Thaincf13b082016-01-03 16:05:18 +11001423 /* After/during arbitration, BSY should be asserted.
1424 * IBM DPES-31080 Version S31Q works now
1425 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1426 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001427 NCR5380_write(INITIATOR_COMMAND_REG,
1428 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Roman Zippelc28bda22007-05-01 22:32:36 +02001430 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1431 hostdata->connected) {
1432 NCR5380_write(MODE_REG, MR_BASE);
1433 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001434 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001435 HOSTNO);
1436 return -1;
1437 }
1438
1439 /*
1440 * Again, bus clear + bus settle time is 1.2us, however, this is
1441 * a minimum so we'll udelay ceil(1.2)
1442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Finn Thain9c3f0e22016-01-03 16:05:11 +11001444 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1445 udelay(15);
1446 else
1447 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001448
Finn Thain72064a72016-01-03 16:05:44 +11001449 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1450 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Roman Zippelc28bda22007-05-01 22:32:36 +02001451 return -1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001452
Finn Thaind65e6342014-03-18 11:42:20 +11001453 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001454
1455 /*
1456 * Now that we have won arbitration, start Selection process, asserting
1457 * the host and target ID's on the SCSI bus.
1458 */
1459
1460 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1461
1462 /*
1463 * Raise ATN while SEL is true before BSY goes false from arbitration,
1464 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1465 * phase immediately after selection.
1466 */
1467
1468 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1469 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Roman Zippelc28bda22007-05-01 22:32:36 +02001472 /*
1473 * Reselect interrupts must be turned off prior to the dropping of BSY,
1474 * otherwise we will trigger an interrupt.
1475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Roman Zippelc28bda22007-05-01 22:32:36 +02001477 if (hostdata->connected) {
1478 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1479 return -1;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Roman Zippelc28bda22007-05-01 22:32:36 +02001482 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Roman Zippelc28bda22007-05-01 22:32:36 +02001484 /*
1485 * The initiator shall then wait at least two deskew delays and release
1486 * the BSY signal.
1487 */
1488 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Roman Zippelc28bda22007-05-01 22:32:36 +02001490 /* Reset BSY */
1491 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1492 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Roman Zippelc28bda22007-05-01 22:32:36 +02001494 /*
1495 * Something weird happens when we cease to drive BSY - looks
1496 * like the board/chip is letting us do another read before the
1497 * appropriate propagation delay has expired, and we're confusing
1498 * a BSY signal from ourselves as the target's response to SELECTION.
1499 *
1500 * A small delay (the 'C++' frontend breaks the pipeline with an
1501 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1502 * tighter 'C' code breaks and requires this) solves the problem -
1503 * the 1 us delay is arbitrary, and only used because this delay will
1504 * be the same on other platforms and since it works here, it should
1505 * work there.
1506 *
1507 * wingel suggests that this could be due to failing to wait
1508 * one deskew delay.
1509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Roman Zippelc28bda22007-05-01 22:32:36 +02001511 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Finn Thaind65e6342014-03-18 11:42:20 +11001513 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Roman Zippelc28bda22007-05-01 22:32:36 +02001515 /*
1516 * The SCSI specification calls for a 250 ms timeout for the actual
1517 * selection.
1518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Finn Thainae753a32016-01-03 16:05:23 +11001520 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1521 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Roman Zippelc28bda22007-05-01 22:32:36 +02001523 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1524 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1525 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001526 if (!hostdata->connected)
1527 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02001528 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1529 HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001530 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001532
Finn Thainae753a32016-01-03 16:05:23 +11001533 if (err < 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001534 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001535 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001536#ifdef SUPPORT_TAGS
1537 cmd_free_tag(cmd);
1538#endif
1539 cmd->scsi_done(cmd);
1540 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001541 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001542 return 0;
1543 }
1544
Roman Zippelc28bda22007-05-01 22:32:36 +02001545 /*
Finn Thainae753a32016-01-03 16:05:23 +11001546 * No less than two deskew delays after the initiator detects the
1547 * BSY signal is true, it shall release the SEL signal and may
1548 * change the DATA BUS. -wingel
1549 */
1550
1551 udelay(1);
1552
1553 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1554
1555 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001556 * Since we followed the SCSI spec, and raised ATN while SEL
1557 * was true but before BSY was false during selection, the information
1558 * transfer phase should be a MESSAGE OUT phase so that we can send the
1559 * IDENTIFY message.
1560 *
1561 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1562 * message (2 bytes) with a tag ID that we increment with every command
1563 * until it wraps back to 0.
1564 *
1565 * XXX - it turns out that there are some broken SCSI-II devices,
1566 * which claim to support tagged queuing but fail when more than
1567 * some number of commands are issued at once.
1568 */
1569
1570 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001571
1572 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1573 if (err < 0) {
1574 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1575 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1576 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1577 return -1;
1578 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001579
Finn Thaind65e6342014-03-18 11:42:20 +11001580 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001581 HOSTNO, cmd->device->id);
1582 tmp[0] = IDENTIFY(1, cmd->device->lun);
1583
1584#ifdef SUPPORT_TAGS
1585 if (cmd->tag != TAG_NONE) {
1586 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1587 tmp[2] = cmd->tag;
1588 len = 3;
1589 } else
1590 len = 1;
1591#else
1592 len = 1;
1593 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594#endif /* SUPPORT_TAGS */
1595
Roman Zippelc28bda22007-05-01 22:32:36 +02001596 /* Send message(s) */
1597 data = tmp;
1598 phase = PHASE_MSGOUT;
1599 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001600 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001601 /* XXX need to handle errors here */
1602 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001604 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1605#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001606#ifdef SUN3_SCSI_VME
1607 dregs->csr |= CSR_INTR;
1608#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Roman Zippelc28bda22007-05-01 22:32:36 +02001610 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Roman Zippelc28bda22007-05-01 22:32:36 +02001612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Roman Zippelc28bda22007-05-01 22:32:36 +02001615/*
1616 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * unsigned char *phase, int *count, unsigned char **data)
1618 *
1619 * Purpose : transfers data in given phase using polled I/O
1620 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001621 * Inputs : instance - instance of driver, *phase - pointer to
1622 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001624 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001626 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * is in same phase.
1628 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001629 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 *
1631 * XXX Note : handling for bus free may be useful.
1632 */
1633
1634/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001635 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * IS 100% reliable, and for the actual data transfer where speed
1637 * counts, we will always do a pseudo DMA or DMA transfer.
1638 */
1639
Roman Zippelc28bda22007-05-01 22:32:36 +02001640static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1641 unsigned char *phase, int *count,
1642 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Roman Zippelc28bda22007-05-01 22:32:36 +02001644 register unsigned char p = *phase, tmp;
1645 register int c = *count;
1646 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Roman Zippelc28bda22007-05-01 22:32:36 +02001648 /*
1649 * The NCR5380 chip will only drive the SCSI bus when the
1650 * phase specified in the appropriate bits of the TARGET COMMAND
1651 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
1653
Roman Zippelc28bda22007-05-01 22:32:36 +02001654 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Roman Zippelc28bda22007-05-01 22:32:36 +02001656 do {
1657 /*
1658 * Wait for assertion of REQ, after which the phase bits will be
1659 * valid
1660 */
Finn Thain686f3992016-01-03 16:05:26 +11001661
1662 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Finn Thaind65e6342014-03-18 11:42:20 +11001665 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Roman Zippelc28bda22007-05-01 22:32:36 +02001667 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001668 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001669 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001670 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001671 break;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Roman Zippelc28bda22007-05-01 22:32:36 +02001674 /* Do actual transfer from SCSI bus to / from memory */
1675 if (!(p & SR_IO))
1676 NCR5380_write(OUTPUT_DATA_REG, *d);
1677 else
1678 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Roman Zippelc28bda22007-05-01 22:32:36 +02001680 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Roman Zippelc28bda22007-05-01 22:32:36 +02001682 /*
1683 * The SCSI standard suggests that in MSGOUT phase, the initiator
1684 * should drop ATN on the last byte of the message phase
1685 * after REQ has been asserted for the handshake but before
1686 * the initiator raises ACK.
1687 */
1688
1689 if (!(p & SR_IO)) {
1690 if (!((p & SR_MSG) && c > 1)) {
1691 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001692 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001693 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1694 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1695 } else {
1696 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1697 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001698 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001699 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1700 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1701 }
1702 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001703 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001704 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1705 }
1706
Finn Thaina2edc4a2016-01-03 16:05:27 +11001707 if (NCR5380_poll_politely(instance,
1708 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1709 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001710
Finn Thaind65e6342014-03-18 11:42:20 +11001711 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001712
1713 /*
1714 * We have several special cases to consider during REQ/ACK handshaking :
1715 * 1. We were in MSGOUT phase, and we are on the last byte of the
1716 * message. ATN must be dropped as ACK is dropped.
1717 *
1718 * 2. We are in a MSGIN phase, and we are on the last byte of the
1719 * message. We must exit with ACK asserted, so that the calling
1720 * code may raise ATN before dropping ACK to reject the message.
1721 *
1722 * 3. ACK and ATN are clear and the target may proceed as normal.
1723 */
1724 if (!(p == PHASE_MSGIN && c == 1)) {
1725 if (p == PHASE_MSGOUT && c > 1)
1726 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1727 else
1728 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1729 }
1730 } while (--c);
1731
Finn Thaind65e6342014-03-18 11:42:20 +11001732 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001733
1734 *count = c;
1735 *data = d;
1736 tmp = NCR5380_read(STATUS_REG);
1737 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001738 * asserted or if ACK hasn't been released yet. The latter applies if
1739 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001740 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001741 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001742 *phase = tmp & PHASE_MASK;
1743 else
1744 *phase = PHASE_UNKNOWN;
1745
1746 if (!c || (*phase == p))
1747 return 0;
1748 else
1749 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Finn Thain636b1ec2016-01-03 16:05:10 +11001752/**
1753 * do_reset - issue a reset command
1754 * @instance: adapter to reset
1755 *
1756 * Issue a reset sequence to the NCR5380 and try and get the bus
1757 * back into sane shape.
1758 *
1759 * This clears the reset interrupt flag because there may be no handler for
1760 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1761 * been installed. And when in EH we may have released the ST DMA interrupt.
1762 */
1763
1764static void do_reset(struct Scsi_Host *instance)
1765{
1766 unsigned long flags;
1767
1768 local_irq_save(flags);
1769 NCR5380_write(TARGET_COMMAND_REG,
1770 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1771 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1772 udelay(50);
1773 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1774 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1775 local_irq_restore(flags);
1776}
1777
Finn Thain80d3eb62016-01-03 16:05:34 +11001778/**
1779 * do_abort - abort the currently established nexus by going to
1780 * MESSAGE OUT phase and sending an ABORT message.
1781 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001782 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001783 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
1785
Finn Thaina53a21e2014-11-12 16:12:21 +11001786static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Roman Zippelc28bda22007-05-01 22:32:36 +02001788 unsigned char tmp, *msgptr, phase;
1789 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001790 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Roman Zippelc28bda22007-05-01 22:32:36 +02001792 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Roman Zippelc28bda22007-05-01 22:32:36 +02001795 /*
1796 * Wait for the target to indicate a valid phase by asserting
1797 * REQ. Once this happens, we'll have either a MSGOUT phase
1798 * and can immediately send the ABORT message, or we'll have some
1799 * other phase and will have to source/sink data.
1800 *
1801 * We really don't care what value was on the bus or what value
1802 * the target sees, so we just handshake.
1803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Finn Thain80d3eb62016-01-03 16:05:34 +11001805 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1806 if (rc < 0)
1807 goto timeout;
1808
1809 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001810
1811 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1812
Finn Thain80d3eb62016-01-03 16:05:34 +11001813 if (tmp != PHASE_MSGOUT) {
1814 NCR5380_write(INITIATOR_COMMAND_REG,
1815 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1816 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1817 if (rc < 0)
1818 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001819 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1820 }
1821
1822 tmp = ABORT;
1823 msgptr = &tmp;
1824 len = 1;
1825 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001826 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001827
1828 /*
1829 * If we got here, and the command completed successfully,
1830 * we're about to go into bus free state.
1831 */
1832
1833 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001834
1835timeout:
1836 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1837 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001841/*
1842 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 * unsigned char *phase, int *count, unsigned char **data)
1844 *
1845 * Purpose : transfers data in given phase using either real
1846 * or pseudo DMA.
1847 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001848 * Inputs : instance - instance of driver, *phase - pointer to
1849 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001851 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001853 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * is in same phase.
1855 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001856 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 *
1858 */
1859
1860
Roman Zippelc28bda22007-05-01 22:32:36 +02001861static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1862 unsigned char *phase, int *count,
1863 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Roman Zippelc28bda22007-05-01 22:32:36 +02001865 SETUP_HOSTDATA(instance);
1866 register int c = *count;
1867 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001868 unsigned long flags;
1869
1870#if defined(CONFIG_SUN3)
1871 /* sanity check */
1872 if (!sun3_dma_setup_done) {
1873 pr_err("scsi%d: transfer_dma without setup!\n",
1874 instance->host_no);
1875 BUG();
1876 }
1877 hostdata->dma_len = c;
1878
1879 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1880 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1881 c, (p & SR_IO) ? "to" : "from", *data);
1882
1883 /* netbsd turns off ints here, why not be safe and do it too */
1884 local_irq_save(flags);
1885
1886 /* send start chain */
1887 sun3scsi_dma_start(c, *data);
1888
Finn Thaincd400822016-01-03 16:05:40 +11001889 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1890 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1891 MR_ENABLE_EOP_INTR);
Finn Thaine3f463b2014-11-12 16:12:16 +11001892 if (p & SR_IO) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001893 NCR5380_write(INITIATOR_COMMAND_REG, 0);
Finn Thaine3f463b2014-11-12 16:12:16 +11001894 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1895 } else {
Finn Thaine3f463b2014-11-12 16:12:16 +11001896 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
Finn Thaine3f463b2014-11-12 16:12:16 +11001897 NCR5380_write(START_DMA_SEND_REG, 0);
1898 }
1899
1900#ifdef SUN3_SCSI_VME
1901 dregs->csr |= CSR_DMA_ENABLE;
1902#endif
1903
1904 local_irq_restore(flags);
1905
1906 sun3_dma_active = 1;
1907
1908#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001909 register unsigned char *d = *data;
1910 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Roman Zippelc28bda22007-05-01 22:32:36 +02001912 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1913 *phase = tmp;
1914 return -1;
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Finn Thainef1081c2014-11-12 16:12:14 +11001917 if (hostdata->read_overruns && (p & SR_IO))
1918 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Finn Thaind65e6342014-03-18 11:42:20 +11001920 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001921 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1922 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Roman Zippelc28bda22007-05-01 22:32:36 +02001924 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thaincd400822016-01-03 16:05:40 +11001925 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1926 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Finn Thainef1081c2014-11-12 16:12:14 +11001928 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001929 /* On the Medusa, it is a must to initialize the DMA before
1930 * starting the NCR. This is also the cleaner way for the TT.
1931 */
1932 local_irq_save(flags);
1933 hostdata->dma_len = (p & SR_IO) ?
1934 NCR5380_dma_read_setup(instance, d, c) :
1935 NCR5380_dma_write_setup(instance, d, c);
1936 local_irq_restore(flags);
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Roman Zippelc28bda22007-05-01 22:32:36 +02001939 if (p & SR_IO)
1940 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1941 else {
1942 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1943 NCR5380_write(START_DMA_SEND_REG, 0);
1944 }
1945
Finn Thainef1081c2014-11-12 16:12:14 +11001946 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001947 /* On the Falcon, the DMA setup must be done after the last */
1948 /* NCR access, else the DMA setup gets trashed!
1949 */
1950 local_irq_save(flags);
1951 hostdata->dma_len = (p & SR_IO) ?
1952 NCR5380_dma_read_setup(instance, d, c) :
1953 NCR5380_dma_write_setup(instance, d, c);
1954 local_irq_restore(flags);
1955 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001956#endif /* !defined(CONFIG_SUN3) */
1957
Roman Zippelc28bda22007-05-01 22:32:36 +02001958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960#endif /* defined(REAL_DMA) */
1961
1962/*
1963 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1964 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001965 * Purpose : run through the various SCSI phases and do as the target
1966 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * instance->connected.
1968 *
1969 * Inputs : instance, instance for which we are doing commands
1970 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001971 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 * modified if a command disconnects, *instance->connected will
1973 * change.
1974 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001975 * XXX Note : we need to watch for bus free or a reset condition here
1976 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001978
1979static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Roman Zippelc28bda22007-05-01 22:32:36 +02001981 SETUP_HOSTDATA(instance);
1982 unsigned long flags;
1983 unsigned char msgout = NOP;
1984 int sink = 0;
1985 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001987 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001989 unsigned char *data;
1990 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001991 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Finn Thaine3f463b2014-11-12 16:12:16 +11001993#ifdef SUN3_SCSI_VME
1994 dregs->csr |= CSR_INTR;
1995#endif
1996
Roman Zippelc28bda22007-05-01 22:32:36 +02001997 while (1) {
1998 tmp = NCR5380_read(STATUS_REG);
1999 /* We only have a valid SCSI phase when REQ is asserted */
2000 if (tmp & SR_REQ) {
2001 phase = (tmp & PHASE_MASK);
2002 if (phase != old_phase) {
2003 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11002004 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002006#if defined(CONFIG_SUN3)
2007 if (phase == PHASE_CMDOUT) {
2008#if defined(REAL_DMA)
2009 void *d;
2010 unsigned long count;
2011
2012 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2013 count = cmd->SCp.buffer->length;
2014 d = sg_virt(cmd->SCp.buffer);
2015 } else {
2016 count = cmd->SCp.this_residual;
2017 d = cmd->SCp.ptr;
2018 }
2019 /* this command setup for dma yet? */
2020 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2021 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2022 sun3scsi_dma_setup(d, count,
2023 rq_data_dir(cmd->request));
2024 sun3_dma_setup_done = cmd;
2025 }
2026 }
2027#endif
2028#ifdef SUN3_SCSI_VME
2029 dregs->csr |= CSR_INTR;
2030#endif
2031 }
2032#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Roman Zippelc28bda22007-05-01 22:32:36 +02002034 if (sink && (phase != PHASE_MSGOUT)) {
2035 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Roman Zippelc28bda22007-05-01 22:32:36 +02002037 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2038 ICR_ASSERT_ACK);
2039 while (NCR5380_read(STATUS_REG) & SR_REQ)
2040 ;
2041 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2042 ICR_ASSERT_ATN);
2043 sink = 0;
2044 continue;
2045 }
2046
2047 switch (phase) {
2048 case PHASE_DATAOUT:
2049#if (NDEBUG & NDEBUG_NO_DATAOUT)
2050 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2051 "aborted\n", HOSTNO);
2052 sink = 1;
2053 do_abort(instance);
2054 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002055 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002056 return;
2057#endif
2058 case PHASE_DATAIN:
2059 /*
2060 * If there is no room left in the current buffer in the
2061 * scatter-gather list, move onto the next one.
2062 */
2063
2064 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2065 ++cmd->SCp.buffer;
2066 --cmd->SCp.buffers_residual;
2067 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002068 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002069 /* ++roman: Try to merge some scatter-buffers if
2070 * they are at contiguous physical addresses.
2071 */
2072 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002073 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002074 HOSTNO, cmd->SCp.this_residual,
2075 cmd->SCp.buffers_residual);
2076 }
2077
2078 /*
2079 * The preferred transfer method is going to be
2080 * PSEUDO-DMA for systems that are strictly PIO,
2081 * since we can let the hardware do the handshaking.
2082 *
2083 * For this to work, we need to know the transfersize
2084 * ahead of time, since the pseudo-DMA code will sit
2085 * in an unconditional loop.
2086 */
2087
2088 /* ++roman: I suggest, this should be
2089 * #if def(REAL_DMA)
2090 * instead of leaving REAL_DMA out.
2091 */
2092
2093#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002094#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11002095 transfersize = 0;
2096 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11002097#endif
Finn Thainff3d4572016-01-03 16:05:25 +11002098 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
2099
2100 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002101 len = transfersize;
2102 cmd->SCp.phase = phase;
2103 if (NCR5380_transfer_dma(instance, &phase,
2104 &len, (unsigned char **)&cmd->SCp.ptr)) {
2105 /*
2106 * If the watchdog timer fires, all future
2107 * accesses to this device will use the
2108 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002109 scmd_printk(KERN_INFO, cmd,
2110 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002111 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02002112 sink = 1;
2113 do_abort(instance);
2114 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002115 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002116 /* XXX - need to source or sink data here, as appropriate */
2117 } else {
2118#ifdef REAL_DMA
2119 /* ++roman: When using real DMA,
2120 * information_transfer() should return after
2121 * starting DMA since it has nothing more to
2122 * do.
2123 */
2124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002126 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002128 }
2129 } else
2130#endif /* defined(REAL_DMA) */
2131 NCR5380_transfer_pio(instance, &phase,
2132 (int *)&cmd->SCp.this_residual,
2133 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002134#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2135 /* if we had intended to dma that command clear it */
2136 if (sun3_dma_setup_done == cmd)
2137 sun3_dma_setup_done = NULL;
2138#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002139 break;
2140 case PHASE_MSGIN:
2141 len = 1;
2142 data = &tmp;
2143 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2144 NCR5380_transfer_pio(instance, &phase, &len, &data);
2145 cmd->SCp.Message = tmp;
2146
2147 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002148 case ABORT:
2149 case COMMAND_COMPLETE:
2150 /* Accept message by clearing ACK */
2151 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002152 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002153 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002154
2155 local_irq_save(flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002156 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002157#ifdef SUPPORT_TAGS
2158 cmd_free_tag(cmd);
2159 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2160 /* Turn a QUEUE FULL status into BUSY, I think the
2161 * mid level cannot handle QUEUE FULL :-( (The
2162 * command is retried after BUSY). Also update our
2163 * queue size to the number of currently issued
2164 * commands now.
2165 */
2166 /* ++Andreas: the mid level code knows about
2167 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002168 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002169 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002170 "QUEUE_FULL after %d commands\n",
2171 HOSTNO, cmd->device->id, cmd->device->lun,
2172 ta->nr_allocated);
2173 if (ta->queue_size > ta->nr_allocated)
2174 ta->nr_allocated = ta->queue_size;
2175 }
2176#else
2177 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2178#endif
2179 /* Enable reselect interrupts */
2180 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2181
2182 /*
2183 * I'm not sure what the correct thing to do here is :
2184 *
2185 * If the command that just executed is NOT a request
2186 * sense, the obvious thing to do is to set the result
2187 * code to the values of the stored parameters.
2188 *
2189 * If it was a REQUEST SENSE command, we need some way to
2190 * differentiate between the failure code of the original
2191 * and the failure code of the REQUEST sense - the obvious
2192 * case is success, where we fall through and leave the
2193 * result code unchanged.
2194 *
2195 * The non-obvious place is where the REQUEST SENSE failed
2196 */
2197
2198 if (cmd->cmnd[0] != REQUEST_SENSE)
2199 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2200 else if (status_byte(cmd->SCp.Status) != GOOD)
2201 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2202
Boaz Harrosh28424d32007-09-10 22:37:45 +03002203 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2204 hostdata->ses.cmd_len) {
2205 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2206 hostdata->ses.cmd_len = 0 ;
2207 }
2208
Roman Zippelc28bda22007-05-01 22:32:36 +02002209 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2210 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002211 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002212
Finn Thaind65e6342014-03-18 11:42:20 +11002213 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002214
Roman Zippelc28bda22007-05-01 22:32:36 +02002215 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002216 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002217 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002218 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002219 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002220 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002221 cmd->scsi_done(cmd);
2222 }
2223
Roman Zippelc28bda22007-05-01 22:32:36 +02002224 /*
2225 * Restore phase bits to 0 so an interrupted selection,
2226 * arbitration can resume.
2227 */
2228 NCR5380_write(TARGET_COMMAND_REG, 0);
2229
Finn Thain72064a72016-01-03 16:05:44 +11002230 /* Enable reselect interrupts */
2231 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2232
Roman Zippelc28bda22007-05-01 22:32:36 +02002233 /* ++roman: For Falcon SCSI, release the lock on the
2234 * ST-DMA here if no other commands are waiting on the
2235 * disconnected queue.
2236 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002237 maybe_release_dma_irq(instance);
2238 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002239 return;
2240 case MESSAGE_REJECT:
2241 /* Accept message by clearing ACK */
2242 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2243 /* Enable reselect interrupts */
2244 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2245 switch (hostdata->last_message) {
2246 case HEAD_OF_QUEUE_TAG:
2247 case ORDERED_QUEUE_TAG:
2248 case SIMPLE_QUEUE_TAG:
2249 /* The target obviously doesn't support tagged
2250 * queuing, even though it announced this ability in
2251 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2252 * clear 'tagged_supported' and lock the LUN, since
2253 * the command is treated as untagged further on.
2254 */
2255 cmd->device->tagged_supported = 0;
2256 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2257 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002258 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002259 "QUEUE_TAG message; tagged queuing "
2260 "disabled\n",
2261 HOSTNO, cmd->device->id, cmd->device->lun);
2262 break;
2263 }
2264 break;
2265 case DISCONNECT:
2266 /* Accept message by clearing ACK */
2267 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2268 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002269 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002270 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002271 hostdata->connected = NULL;
2272 hostdata->disconnected_queue = cmd;
2273 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002274 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002275 "moved from connected to the "
2276 "disconnected_queue\n", HOSTNO,
2277 cmd->device->id, cmd->device->lun);
2278 /*
2279 * Restore phase bits to 0 so an interrupted selection,
2280 * arbitration can resume.
2281 */
2282 NCR5380_write(TARGET_COMMAND_REG, 0);
2283
2284 /* Enable reselect interrupts */
2285 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002286#ifdef SUN3_SCSI_VME
2287 dregs->csr |= CSR_DMA_ENABLE;
2288#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002289 return;
2290 /*
2291 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2292 * operation, in violation of the SCSI spec so we can safely
2293 * ignore SAVE/RESTORE pointers calls.
2294 *
2295 * Unfortunately, some disks violate the SCSI spec and
2296 * don't issue the required SAVE_POINTERS message before
2297 * disconnecting, and we have to break spec to remain
2298 * compatible.
2299 */
2300 case SAVE_POINTERS:
2301 case RESTORE_POINTERS:
2302 /* Accept message by clearing ACK */
2303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2304 /* Enable reselect interrupts */
2305 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2306 break;
2307 case EXTENDED_MESSAGE:
2308 /*
2309 * Extended messages are sent in the following format :
2310 * Byte
2311 * 0 EXTENDED_MESSAGE == 1
2312 * 1 length (includes one byte for code, doesn't
2313 * include first two bytes)
2314 * 2 code
2315 * 3..length+1 arguments
2316 *
2317 * Start the extended message buffer with the EXTENDED_MESSAGE
2318 * byte, since spi_print_msg() wants the whole thing.
2319 */
2320 extended_msg[0] = EXTENDED_MESSAGE;
2321 /* Accept first byte by clearing ACK */
2322 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2323
Finn Thaind65e6342014-03-18 11:42:20 +11002324 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002325
2326 len = 2;
2327 data = extended_msg + 1;
2328 phase = PHASE_MSGIN;
2329 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002330 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002331 (int)extended_msg[1], (int)extended_msg[2]);
2332
2333 if (!len && extended_msg[1] <=
2334 (sizeof(extended_msg) - 1)) {
2335 /* Accept third byte by clearing ACK */
2336 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2337 len = extended_msg[1] - 1;
2338 data = extended_msg + 3;
2339 phase = PHASE_MSGIN;
2340
2341 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002342 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002343 HOSTNO, len);
2344
2345 switch (extended_msg[2]) {
2346 case EXTENDED_SDTR:
2347 case EXTENDED_WDTR:
2348 case EXTENDED_MODIFY_DATA_POINTER:
2349 case EXTENDED_EXTENDED_IDENTIFY:
2350 tmp = 0;
2351 }
2352 } else if (len) {
2353 printk(KERN_NOTICE "scsi%d: error receiving "
2354 "extended message\n", HOSTNO);
2355 tmp = 0;
2356 } else {
2357 printk(KERN_NOTICE "scsi%d: extended message "
2358 "code %02x length %d is too long\n",
2359 HOSTNO, extended_msg[2], extended_msg[1]);
2360 tmp = 0;
2361 }
2362 /* Fall through to reject message */
2363
2364 /*
2365 * If we get something weird that we aren't expecting,
2366 * reject it.
2367 */
2368 default:
2369 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002370 printk(KERN_INFO "scsi%d: rejecting message ",
2371 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002372 spi_print_msg(extended_msg);
2373 printk("\n");
2374 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002375 scmd_printk(KERN_INFO, cmd,
2376 "rejecting unknown message %02x\n",
2377 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002378 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002379 scmd_printk(KERN_INFO, cmd,
2380 "rejecting unknown extended message code %02x, length %d\n",
2381 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002382
2383 msgout = MESSAGE_REJECT;
2384 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2385 break;
2386 } /* switch (tmp) */
2387 break;
2388 case PHASE_MSGOUT:
2389 len = 1;
2390 data = &msgout;
2391 hostdata->last_message = msgout;
2392 NCR5380_transfer_pio(instance, &phase, &len, &data);
2393 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002394 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002395#ifdef SUPPORT_TAGS
2396 cmd_free_tag(cmd);
2397#else
2398 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2399#endif
2400 hostdata->connected = NULL;
2401 cmd->result = DID_ERROR << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002402 maybe_release_dma_irq(instance);
2403 local_irq_restore(flags);
2404 cmd->scsi_done(cmd);
Finn Thaincd400822016-01-03 16:05:40 +11002405 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02002406 return;
2407 }
2408 msgout = NOP;
2409 break;
2410 case PHASE_CMDOUT:
2411 len = cmd->cmd_len;
2412 data = cmd->cmnd;
2413 /*
2414 * XXX for performance reasons, on machines with a
2415 * PSEUDO-DMA architecture we should probably
2416 * use the dma transfer function.
2417 */
2418 NCR5380_transfer_pio(instance, &phase, &len, &data);
2419 break;
2420 case PHASE_STATIN:
2421 len = 1;
2422 data = &tmp;
2423 NCR5380_transfer_pio(instance, &phase, &len, &data);
2424 cmd->SCp.Status = tmp;
2425 break;
2426 default:
2427 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002428 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002429 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002430 } else {
2431 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2432 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002433 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
2436/*
2437 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2438 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002439 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002440 * 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 -07002441 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002442 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 * Inputs : instance - this instance of the NCR5380.
2444 *
2445 */
2446
2447
Finn Thaine3f463b2014-11-12 16:12:16 +11002448/* it might eventually prove necessary to do a dma setup on
2449 reselection, but it doesn't seem to be needed now -- sam */
2450
Roman Zippelc28bda22007-05-01 22:32:36 +02002451static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Roman Zippelc28bda22007-05-01 22:32:36 +02002453 SETUP_HOSTDATA(instance);
2454 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002455 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002457 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002459 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002460 int __maybe_unused len;
2461 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002462 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Roman Zippelc28bda22007-05-01 22:32:36 +02002464 /*
2465 * Disable arbitration, etc. since the host adapter obviously
2466 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Roman Zippelc28bda22007-05-01 22:32:36 +02002469 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Roman Zippelc28bda22007-05-01 22:32:36 +02002471 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2472
Finn Thaind65e6342014-03-18 11:42:20 +11002473 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002474
2475 /*
2476 * At this point, we have detected that our SCSI ID is on the bus,
2477 * SEL is true and BSY was false for at least one bus settle delay
2478 * (400 ns).
2479 *
2480 * We must assert BSY ourselves, until the target drops the SEL
2481 * signal.
2482 */
2483
2484 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002485 if (NCR5380_poll_politely(instance,
2486 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2487 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2488 return;
2489 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002490 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2491
2492 /*
2493 * Wait for target to go into MSGIN.
2494 */
2495
Finn Thain72064a72016-01-03 16:05:44 +11002496 if (NCR5380_poll_politely(instance,
2497 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2498 do_abort(instance);
2499 return;
2500 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002501
Finn Thaine3f463b2014-11-12 16:12:16 +11002502#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2503 /* acknowledge toggle to MSGIN */
2504 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2505
2506 /* peek at the byte without really hitting the bus */
2507 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2508#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002509 len = 1;
2510 data = msg;
2511 phase = PHASE_MSGIN;
2512 NCR5380_transfer_pio(instance, &phase, &len, &data);
2513
Finn Thain72064a72016-01-03 16:05:44 +11002514 if (len) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002515 do_abort(instance);
2516 return;
2517 }
Finn Thain72064a72016-01-03 16:05:44 +11002518#endif
2519
2520 if (!(msg[0] & 0x80)) {
2521 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2522 spi_print_msg(msg);
2523 printk("\n");
2524 do_abort(instance);
2525 return;
2526 }
2527 lun = msg[0] & 0x07;
Roman Zippelc28bda22007-05-01 22:32:36 +02002528
Finn Thaine3f463b2014-11-12 16:12:16 +11002529#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002530 /* If the phase is still MSGIN, the target wants to send some more
2531 * messages. In case it supports tagged queuing, this is probably a
2532 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2533 */
2534 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002535 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002536 /* Accept previous IDENTIFY message by clearing ACK */
2537 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2538 len = 2;
2539 data = msg + 1;
2540 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2541 msg[1] == SIMPLE_QUEUE_TAG)
2542 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002543 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002544 "reselection\n", HOSTNO, target_mask, lun, tag);
2545 }
2546#endif
2547
2548 /*
2549 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2550 * just reestablished, and remove it from the disconnected queue.
2551 */
2552
Finn Thain710ddd02014-11-12 16:12:02 +11002553 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002554 tmp; prev = tmp, tmp = NEXT(tmp)) {
Finn Thain72064a72016-01-03 16:05:44 +11002555 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Roman Zippelc28bda22007-05-01 22:32:36 +02002556#ifdef SUPPORT_TAGS
2557 && (tag == tmp->tag)
2558#endif
2559 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002560 if (prev) {
2561 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002562 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002563 } else {
2564 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2565 hostdata->disconnected_queue = NEXT(tmp);
2566 }
Roman Zippel3130d902007-05-01 22:32:37 +02002567 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002568 break;
2569 }
2570 }
2571
2572 if (!tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002573#ifdef SUPPORT_TAGS
Finn Thain72064a72016-01-03 16:05:44 +11002574 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d tag %d not in disconnected queue.\n",
2575 target_mask, lun, tag);
2576#else
2577 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2578 target_mask, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002579#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002580 /*
2581 * Since we have an established nexus that we can't do anything
2582 * with, we must abort it.
2583 */
2584 do_abort(instance);
2585 return;
2586 }
2587
Finn Thaine3f463b2014-11-12 16:12:16 +11002588#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2589 /* engage dma setup for the command we just saw */
2590 {
2591 void *d;
2592 unsigned long count;
2593
2594 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2595 count = tmp->SCp.buffer->length;
2596 d = sg_virt(tmp->SCp.buffer);
2597 } else {
2598 count = tmp->SCp.this_residual;
2599 d = tmp->SCp.ptr;
2600 }
2601 /* setup this command for dma if not already */
2602 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2603 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2604 sun3_dma_setup_done = tmp;
2605 }
2606 }
2607
2608 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2609#endif
2610
Roman Zippelc28bda22007-05-01 22:32:36 +02002611 /* Accept message by clearing ACK */
2612 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2613
Finn Thaine3f463b2014-11-12 16:12:16 +11002614#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2615 /* If the phase is still MSGIN, the target wants to send some more
2616 * messages. In case it supports tagged queuing, this is probably a
2617 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2618 */
2619 tag = TAG_NONE;
2620 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2621 /* Accept previous IDENTIFY message by clearing ACK */
2622 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2623 len = 2;
2624 data = msg + 1;
2625 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2626 msg[1] == SIMPLE_QUEUE_TAG)
2627 tag = msg[2];
2628 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2629 HOSTNO, target_mask, lun, tag);
2630 }
2631#endif
2632
Roman Zippelc28bda22007-05-01 22:32:36 +02002633 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002634 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002635 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
2638
2639/*
Finn Thain710ddd02014-11-12 16:12:02 +11002640 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 *
2642 * Purpose : abort a command
2643 *
Finn Thain710ddd02014-11-12 16:12:02 +11002644 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002645 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 * used.
2647 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002648 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002650 * XXX - there is no way to abort the command that is currently
2651 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002653 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 */
2655
2656static
Finn Thain710ddd02014-11-12 16:12:02 +11002657int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Roman Zippelc28bda22007-05-01 22:32:36 +02002659 struct Scsi_Host *instance = cmd->device->host;
2660 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002661 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002662 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002664 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Roman Zippelc28bda22007-05-01 22:32:36 +02002666 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Roman Zippelc28bda22007-05-01 22:32:36 +02002668 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Finn Thaind65e6342014-03-18 11:42:20 +11002670 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002671 NCR5380_read(BUS_AND_STATUS_REG),
2672 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002675 /*
2676 * Case 1 : If the command is the currently executing command,
2677 * we'll set the aborted flag and return control so that
2678 * information transfer routine can exit cleanly.
2679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Roman Zippelc28bda22007-05-01 22:32:36 +02002681 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Finn Thaind65e6342014-03-18 11:42:20 +11002683 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002684 /*
2685 * We should perform BSY checking, and make sure we haven't slipped
2686 * into BUS FREE.
2687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Roman Zippelc28bda22007-05-01 22:32:36 +02002689 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2690 /*
2691 * Since we can't change phases until we've completed the current
2692 * handshake, we have to source or sink a byte of data if the current
2693 * phase is not MSGOUT.
2694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Roman Zippelc28bda22007-05-01 22:32:36 +02002696 /*
2697 * Return control to the executing NCR drive so we can clear the
2698 * aborted flag and get back into our main loop.
2699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Roman Zippelc28bda22007-05-01 22:32:36 +02002701 if (do_abort(instance) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002702 hostdata->connected = NULL;
2703 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002705 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002707 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002709 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002710 local_irq_restore(flags);
2711 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002712 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002713 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002714 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002715 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002716 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002720
2721 /*
2722 * Case 2 : If the command hasn't been issued yet, we simply remove it
2723 * from the issue queue.
2724 */
Finn Thain710ddd02014-11-12 16:12:02 +11002725 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2726 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002727 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2728 if (cmd == tmp) {
2729 REMOVE(5, *prev, tmp, NEXT(tmp));
2730 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002731 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002732 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002733 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002734 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002735 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002736 HOSTNO);
2737 /* Tagged queuing note: no tag to free here, hasn't been assigned
2738 * yet... */
2739 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002740 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742 }
2743
Roman Zippelc28bda22007-05-01 22:32:36 +02002744 /*
2745 * Case 3 : If any commands are connected, we're going to fail the abort
2746 * and let the high level SCSI driver retry at a later time or
2747 * issue a reset.
2748 *
2749 * Timeouts, and therefore aborted commands, will be highly unlikely
2750 * and handling them cleanly in this situation would make the common
2751 * case of noresets less efficient, and would pollute our code. So,
2752 * we fail.
2753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Roman Zippelc28bda22007-05-01 22:32:36 +02002755 if (hostdata->connected) {
2756 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002757 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002758 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Roman Zippelc28bda22007-05-01 22:32:36 +02002761 /*
2762 * Case 4: If the command is currently disconnected from the bus, and
2763 * there are no connected commands, we reconnect the I_T_L or
2764 * I_T_L_Q nexus associated with it, go into message out, and send
2765 * an abort message.
2766 *
2767 * This case is especially ugly. In order to reestablish the nexus, we
2768 * need to call NCR5380_select(). The easiest way to implement this
2769 * function was to abort if the bus was busy, and let the interrupt
2770 * handler triggered on the SEL for reselect take care of lost arbitrations
2771 * where necessary, meaning interrupts need to be enabled.
2772 *
2773 * When interrupts are enabled, the queues may change - so we
2774 * can't remove it from the disconnected queue before selecting it
2775 * because that could cause a failure in hashing the nexus if that
2776 * device reselected.
2777 *
2778 * Since the queues may change, we can't use the pointers from when we
2779 * first locate it.
2780 *
2781 * So, we must first locate the command, and if NCR5380_select()
2782 * succeeds, then issue the abort, relocate the command and remove
2783 * it from the disconnected queue.
2784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Finn Thain710ddd02014-11-12 16:12:02 +11002786 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002787 tmp = NEXT(tmp)) {
2788 if (cmd == tmp) {
2789 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002790 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002791
Finn Thain76f13b92014-11-12 16:11:53 +11002792 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002793 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002794
Finn Thaind65e6342014-03-18 11:42:20 +11002795 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002796
2797 do_abort(instance);
2798
2799 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002800 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2801 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002802 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2803 if (cmd == tmp) {
2804 REMOVE(5, *prev, tmp, NEXT(tmp));
2805 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002806 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002807 tmp->result = DID_ABORT << 16;
2808 /* We must unlock the tag/LUN immediately here, since the
2809 * target goes to BUS FREE and doesn't send us another
2810 * message (COMMAND_COMPLETE or the like)
2811 */
2812#ifdef SUPPORT_TAGS
2813 cmd_free_tag(tmp);
2814#else
2815 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2816#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002817 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002818 local_irq_restore(flags);
2819 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002820 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002821 }
2822 }
2823 }
2824 }
2825
Finn Thaine3c3da62014-11-12 16:12:15 +11002826 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2827 * possible at all) At least, we should check if the lock could be
2828 * released after the abort, in case it is kept due to some bug.
2829 */
2830 maybe_release_dma_irq(instance);
2831 local_irq_restore(flags);
2832
Roman Zippelc28bda22007-05-01 22:32:36 +02002833 /*
2834 * Case 5 : If we reached this point, the command was not found in any of
2835 * the queues.
2836 *
2837 * We probably reached this point because of an unlikely race condition
2838 * between the command completing successfully and the abortion code,
2839 * so we won't panic, but we will notify the user in case something really
2840 * broke.
2841 */
2842
Joe Perchesad361c92009-07-06 13:05:40 -07002843 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002844
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002845 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848
Finn Thain3be1b3e2016-01-03 16:05:12 +11002849/**
2850 * NCR5380_bus_reset - reset the SCSI bus
2851 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002852 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002853 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Finn Thain710ddd02014-11-12 16:12:02 +11002856static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Finn Thaine3c3da62014-11-12 16:12:15 +11002858 struct Scsi_Host *instance = cmd->device->host;
2859 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002860 int i;
2861 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Finn Thain3be1b3e2016-01-03 16:05:12 +11002863 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Finn Thain3be1b3e2016-01-03 16:05:12 +11002865#if (NDEBUG & NDEBUG_ANY)
2866 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2867 NCR5380_print_status(instance);
2868#endif
2869
2870 do_reset(instance);
2871
Roman Zippelc28bda22007-05-01 22:32:36 +02002872 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002873 NCR5380_write(MODE_REG, MR_BASE);
2874 NCR5380_write(TARGET_COMMAND_REG, 0);
2875 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Roman Zippelc28bda22007-05-01 22:32:36 +02002877 /* After the reset, there are no more connected or disconnected commands
2878 * and no busy units; so clear the low-level status here to avoid
2879 * conflicts when the mid-level code tries to wake up the affected
2880 * commands!
2881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Roman Zippelc28bda22007-05-01 22:32:36 +02002883 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002884 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002885 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002886 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002887 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002888 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Roman Zippelc28bda22007-05-01 22:32:36 +02002890 hostdata->issue_queue = NULL;
2891 hostdata->connected = NULL;
2892 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002894 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002896 for (i = 0; i < 8; ++i)
2897 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002899 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002901
2902 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002903 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002905 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}