blob: 48e3d76e39e3d1942b143fb014ddec7fd24211e3 [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 Thain2f854b82016-01-03 16:05:22 +1100460 * NCR5380_poll_politely - wait for chip register value
Finn Thain636b1ec2016-01-03 16:05:10 +1100461 * @instance: controller to poll
462 * @reg: 5380 register to poll
463 * @bit: Bitmask to check
464 * @val: Value required to exit
Finn Thain2f854b82016-01-03 16:05:22 +1100465 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100466 *
Finn Thain2f854b82016-01-03 16:05:22 +1100467 * Polls the chip in a reasonably efficient manner waiting for an
468 * event to occur. After a short quick poll we begin to yield the CPU
469 * (if possible). In irq contexts the time-out is arbitrarily limited.
470 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100471 *
Finn Thain2f854b82016-01-03 16:05:22 +1100472 * Returns 0 if event occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100473 */
474
475static int NCR5380_poll_politely(struct Scsi_Host *instance,
Finn Thain2f854b82016-01-03 16:05:22 +1100476 int reg, int bit, int val, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100477{
Finn Thain2f854b82016-01-03 16:05:22 +1100478 struct NCR5380_hostdata *hostdata = shost_priv(instance);
479 unsigned long deadline = jiffies + wait;
480 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100481
Finn Thain2f854b82016-01-03 16:05:22 +1100482 /* Busy-wait for up to 10 ms */
483 n = min(10000U, jiffies_to_usecs(wait));
484 n *= hostdata->accesses_per_ms;
485 n /= 1000;
486 do {
487 if ((NCR5380_read(reg) & bit) == val)
Finn Thain636b1ec2016-01-03 16:05:10 +1100488 return 0;
489 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100490 } while (n--);
491
492 if (irqs_disabled() || in_interrupt())
493 return -ETIMEDOUT;
494
495 /* Repeatedly sleep for 1 ms until deadline */
496 while (time_is_after_jiffies(deadline)) {
497 schedule_timeout_uninterruptible(1);
498 if ((NCR5380_read(reg) & bit) == val)
499 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100500 }
501
Finn Thain636b1ec2016-01-03 16:05:10 +1100502 return -ETIMEDOUT;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505#include <linux/delay.h>
506
507#if NDEBUG
508static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200509 unsigned char mask;
510 const char *name;
511} signals[] = {
512 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
513 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
514 { SR_SEL, "SEL" }, {0, NULL}
515}, basrs[] = {
516 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
517}, icrs[] = {
518 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
519 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
520 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
521 {0, NULL}
522}, mrs[] = {
523 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
524 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
525 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
526 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
527 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
528 {0, NULL}
529};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Finn Thainff50f9e2014-11-12 16:12:18 +1100531/**
532 * NCR5380_print - print scsi bus signals
533 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100535 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
537
Roman Zippelc28bda22007-05-01 22:32:36 +0200538static void NCR5380_print(struct Scsi_Host *instance)
539{
540 unsigned char status, data, basr, mr, icr, i;
541 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Roman Zippelc28bda22007-05-01 22:32:36 +0200543 local_irq_save(flags);
544 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
545 status = NCR5380_read(STATUS_REG);
546 mr = NCR5380_read(MODE_REG);
547 icr = NCR5380_read(INITIATOR_COMMAND_REG);
548 basr = NCR5380_read(BUS_AND_STATUS_REG);
549 local_irq_restore(flags);
550 printk("STATUS_REG: %02x ", status);
551 for (i = 0; signals[i].mask; ++i)
552 if (status & signals[i].mask)
553 printk(",%s", signals[i].name);
554 printk("\nBASR: %02x ", basr);
555 for (i = 0; basrs[i].mask; ++i)
556 if (basr & basrs[i].mask)
557 printk(",%s", basrs[i].name);
558 printk("\nICR: %02x ", icr);
559 for (i = 0; icrs[i].mask; ++i)
560 if (icr & icrs[i].mask)
561 printk(",%s", icrs[i].name);
562 printk("\nMODE: %02x ", mr);
563 for (i = 0; mrs[i].mask; ++i)
564 if (mr & mrs[i].mask)
565 printk(",%s", mrs[i].name);
566 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200570 unsigned char value;
571 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200573 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
574 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
575 {PHASE_UNKNOWN, "UNKNOWN"}
576};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Finn Thainff50f9e2014-11-12 16:12:18 +1100578/**
579 * NCR5380_print_phase - show SCSI phase
580 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100582 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100584 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
586
587static void NCR5380_print_phase(struct Scsi_Host *instance)
588{
Roman Zippelc28bda22007-05-01 22:32:36 +0200589 unsigned char status;
590 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Roman Zippelc28bda22007-05-01 22:32:36 +0200592 status = NCR5380_read(STATUS_REG);
593 if (!(status & SR_REQ))
594 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
595 else {
596 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
597 (phases[i].value != (status & PHASE_MASK)); ++i)
598 ;
599 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603#endif
604
Finn Thain8c325132014-11-12 16:11:58 +1100605/**
606 * NCR58380_info - report driver and host information
607 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 *
Finn Thain8c325132014-11-12 16:11:58 +1100609 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Finn Thain8c325132014-11-12 16:11:58 +1100611 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613
Finn Thain8c325132014-11-12 16:11:58 +1100614static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Finn Thain8c325132014-11-12 16:11:58 +1100616 struct NCR5380_hostdata *hostdata = shost_priv(instance);
617
618 return hostdata->info;
619}
620
621static void prepare_info(struct Scsi_Host *instance)
622{
623 struct NCR5380_hostdata *hostdata = shost_priv(instance);
624
625 snprintf(hostdata->info, sizeof(hostdata->info),
626 "%s, io_port 0x%lx, n_io_port %d, "
627 "base 0x%lx, irq %d, "
628 "can_queue %d, cmd_per_lun %d, "
629 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100630 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100631 "options { %s} ",
632 instance->hostt->name, instance->io_port, instance->n_io_port,
633 instance->base, instance->irq,
634 instance->can_queue, instance->cmd_per_lun,
635 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100636 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100637 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100638#ifdef DIFFERENTIAL
639 "DIFFERENTIAL "
640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100642 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#endif
644#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100645 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#endif
647#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100648 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#endif
Finn Thain8c325132014-11-12 16:11:58 +1100650 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Finn Thainff50f9e2014-11-12 16:12:18 +1100653/**
654 * NCR5380_print_status - dump controller info
655 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100657 * Print commands in the various queues, called from NCR5380_abort
658 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
660
Finn Thain710ddd02014-11-12 16:12:02 +1100661static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400662{
663 int i, s;
664 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200665 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400666 H_NO(cmd), cmd->device->id, cmd->device->lun);
667 printk(KERN_CONT " command = ");
668 command = cmd->cmnd;
669 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
670 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
671 printk(KERN_CONT " %02x", command[i]);
672 printk("\n");
673}
674
Finn Thain3be1b3e2016-01-03 16:05:12 +1100675static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Al Virod89537e2013-03-31 13:24:44 -0400677 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100678 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400679 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Finn Thain8ad3a592014-03-18 11:42:19 +1100681 NCR5380_dprint(NDEBUG_ANY, instance);
682 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Roman Zippelc28bda22007-05-01 22:32:36 +0200684 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Roman Zippelc28bda22007-05-01 22:32:36 +0200686 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +0200687 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400688 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200689 else
Finn Thain710ddd02014-11-12 16:12:02 +1100690 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400691 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100692 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400693 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Al Virod89537e2013-03-31 13:24:44 -0400695 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100696 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400697 ptr = NEXT(ptr))
698 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200699
700 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400701 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Finn Thain710ddd02014-11-12 16:12:02 +1100704static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Roman Zippelc28bda22007-05-01 22:32:36 +0200706 int i, s;
707 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200708 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200709 H_NO(cmd), cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100710 seq_puts(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200711 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400712 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200713 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400714 seq_printf(m, " %02x", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100715 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Finn Thain4d3d2a52014-11-12 16:11:52 +1100718static int __maybe_unused NCR5380_show_info(struct seq_file *m,
719 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400720{
721 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100722 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400723 unsigned long flags;
724
725 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
726
Al Virod89537e2013-03-31 13:24:44 -0400727 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400728 if (!hostdata->connected)
729 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
730 else
Finn Thain710ddd02014-11-12 16:12:02 +1100731 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400732 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100733 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400734 show_Scsi_Cmnd(ptr, m);
735
736 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100737 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400738 ptr = NEXT(ptr))
739 show_Scsi_Cmnd(ptr, m);
740
741 local_irq_restore(flags);
742 return 0;
743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Finn Thainff50f9e2014-11-12 16:12:18 +1100745/**
746 * NCR5380_init - initialise an NCR5380
747 * @instance: adapter to configure
748 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100750 * Initializes *instance and corresponding 5380 chip,
751 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
753 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100754 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200755 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100756 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100759static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Roman Zippelc28bda22007-05-01 22:32:36 +0200761 int i;
762 SETUP_HOSTDATA(instance);
Finn Thain2f854b82016-01-03 16:05:22 +1100763 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Finn Thaina53a21e2014-11-12 16:12:21 +1100765 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200766 hostdata->id_mask = 1 << instance->this_id;
767 hostdata->id_higher_mask = 0;
768 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
769 if (i > hostdata->id_mask)
770 hostdata->id_higher_mask |= i;
771 for (i = 0; i < 8; ++i)
772 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100774 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#endif
776#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200777 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200779 hostdata->connected = NULL;
780 hostdata->issue_queue = NULL;
781 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100782 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Finn Thaina53a21e2014-11-12 16:12:21 +1100784 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100785 hostdata->work_q = alloc_workqueue("ncr5380_%d",
786 WQ_UNBOUND | WQ_MEM_RECLAIM,
787 1, instance->host_no);
788 if (!hostdata->work_q)
789 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Finn Thain8c325132014-11-12 16:11:58 +1100791 prepare_info(instance);
792
Roman Zippelc28bda22007-05-01 22:32:36 +0200793 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
794 NCR5380_write(MODE_REG, MR_BASE);
795 NCR5380_write(TARGET_COMMAND_REG, 0);
796 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Finn Thain2f854b82016-01-03 16:05:22 +1100798 /* Calibrate register polling loop */
799 i = 0;
800 deadline = jiffies + 1;
801 do {
802 cpu_relax();
803 } while (time_is_after_jiffies(deadline));
804 deadline += msecs_to_jiffies(256);
805 do {
806 NCR5380_read(STATUS_REG);
807 ++i;
808 cpu_relax();
809 } while (time_is_after_jiffies(deadline));
810 hostdata->accesses_per_ms = i / 256;
811
Roman Zippelc28bda22007-05-01 22:32:36 +0200812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Finn Thainff50f9e2014-11-12 16:12:18 +1100815/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100816 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
817 * @instance: adapter to check
818 *
819 * If the system crashed, it may have crashed with a connected target and
820 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
821 * currently established nexus, which we know nothing about. Failing that
822 * do a bus reset.
823 *
824 * Note that a bus reset will cause the chip to assert IRQ.
825 *
826 * Returns 0 if successful, otherwise -ENXIO.
827 */
828
829static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
830{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100831 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100832 int pass;
833
834 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
835 switch (pass) {
836 case 1:
837 case 3:
838 case 5:
839 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
840 NCR5380_poll_politely(instance,
841 STATUS_REG, SR_BSY, 0, 5 * HZ);
842 break;
843 case 2:
844 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
845 do_abort(instance);
846 break;
847 case 4:
848 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
849 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100850 /* Wait after a reset; the SCSI standard calls for
851 * 250ms, we wait 500ms to be on the safe side.
852 * But some Toshiba CD-ROMs need ten times that.
853 */
854 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
855 msleep(2500);
856 else
857 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100858 break;
859 case 6:
860 shost_printk(KERN_ERR, instance, "bus locked solid\n");
861 return -ENXIO;
862 }
863 }
864 return 0;
865}
866
867/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100868 * NCR5380_exit - remove an NCR5380
869 * @instance: adapter to remove
870 *
871 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
872 */
873
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200874static void NCR5380_exit(struct Scsi_Host *instance)
875{
Finn Thaina53a21e2014-11-12 16:12:21 +1100876 struct NCR5380_hostdata *hostdata = shost_priv(instance);
877
878 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100879 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200880}
881
Finn Thainff50f9e2014-11-12 16:12:18 +1100882/**
883 * NCR5380_queue_command - queue a command
884 * @instance: the relevant SCSI adapter
885 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 *
Finn Thain1bb40582016-01-03 16:05:29 +1100887 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100888 * twiddling done to the host specific fields of cmd. If the
889 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891
Finn Thain16b29e72014-11-12 16:12:08 +1100892static int NCR5380_queue_command(struct Scsi_Host *instance,
893 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Finn Thain16b29e72014-11-12 16:12:08 +1100895 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100896 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200897 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200900 switch (cmd->cmnd[0]) {
901 case WRITE_6:
902 case WRITE_10:
903 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
904 H_NO(cmd));
905 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100906 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200907 return 0;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
910
Roman Zippelc28bda22007-05-01 22:32:36 +0200911 /*
912 * We use the host_scribble field as a pointer to the next command
913 * in a queue
914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Roman Zippel3130d902007-05-01 22:32:37 +0200916 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200917 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Roman Zippelc28bda22007-05-01 22:32:36 +0200919 /*
920 * Insert the cmd into the issue queue. Note that REQUEST SENSE
921 * commands are added to the head of the queue since any command will
922 * clear the contingent allegiance condition that exists and the
923 * sense data is only guaranteed to be valid while the condition exists.
924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Roman Zippelc28bda22007-05-01 22:32:36 +0200926 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
927 * Otherwise a running NCR5380_main may steal the lock.
928 * Lock before actually inserting due to fairness reasons explained in
929 * atari_scsi.c. If we insert first, then it's impossible for this driver
930 * to release the lock.
931 * Stop timer for this command while waiting for the lock, or timeouts
932 * may happen (and they really do), and it's no good if the command doesn't
933 * appear in any of the queues.
934 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
935 * because also a timer int can trigger an abort or reset, which would
936 * alter queues and touch the lock.
937 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100938 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100939 return SCSI_MLQUEUE_HOST_BUSY;
940
941 local_irq_save(flags);
942
Finn Thainff50f9e2014-11-12 16:12:18 +1100943 /*
944 * Insert the cmd into the issue queue. Note that REQUEST SENSE
945 * commands are added to the head of the queue since any command will
946 * clear the contingent allegiance condition that exists and the
947 * sense data is only guaranteed to be valid while the condition exists.
948 */
949
Roman Zippelc28bda22007-05-01 22:32:36 +0200950 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
951 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200952 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200953 hostdata->issue_queue = cmd;
954 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100955 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200956 NEXT(tmp); tmp = NEXT(tmp))
957 ;
958 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200959 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200960 }
961 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Finn Thaind65e6342014-03-18 11:42:20 +1100963 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200964 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Finn Thain010e89d2016-01-03 16:05:38 +1100966 /* Kick off command processing */
967 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200968 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Finn Thaine3c3da62014-11-12 16:12:15 +1100971static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
972{
973 struct NCR5380_hostdata *hostdata = shost_priv(instance);
974
975 /* Caller does the locking needed to set & test these data atomically */
976 if (!hostdata->disconnected_queue &&
977 !hostdata->issue_queue &&
978 !hostdata->connected &&
979 !hostdata->retain_dma_intr)
980 NCR5380_release_dma_irq(instance);
981}
982
Finn Thainff50f9e2014-11-12 16:12:18 +1100983/**
984 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100986 * NCR5380_main is a coroutine that runs as long as more work can
987 * be done on the NCR5380 host adapters in a system. Both
988 * NCR5380_queue_command() and NCR5380_intr() will try to start it
989 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200990 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100991 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200992 */
993
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200994static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Finn Thaina53a21e2014-11-12 16:12:21 +1100996 struct NCR5380_hostdata *hostdata =
997 container_of(work, struct NCR5380_hostdata, main_task);
998 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100999 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02001000 int done;
1001 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Roman Zippelc28bda22007-05-01 22:32:36 +02001003 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001004 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1005 * because also a timer int can trigger an abort or reset, which can
1006 * alter queues and touch the Falcon lock.
1007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Roman Zippelc28bda22007-05-01 22:32:36 +02001009 local_save_flags(flags);
1010 do {
1011 local_irq_disable(); /* Freeze request queues */
1012 done = 1;
1013
1014 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001015 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001016 /*
1017 * Search through the issue_queue for a command destined
1018 * for a target that's not busy.
1019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001021 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001022 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1023 ;
1024 /*printk("%p ", tmp);*/
1025 if ((tmp == prev) && tmp)
1026 printk(" LOOP\n");
1027 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001029 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001030 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001031 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Finn Thaine3f463b2014-11-12 16:12:16 +11001033 dprintk(NDEBUG_LISTS,
1034 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1035 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1036 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001037 /* When we find one, remove it from the issue queue. */
1038 /* ++guenther: possible race with Falcon locking */
1039 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001041 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001043 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001044#endif
1045 ) {
1046 /* ++guenther: just to be sure, this must be atomic */
1047 local_irq_disable();
1048 if (prev) {
1049 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001050 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001051 } else {
1052 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1053 hostdata->issue_queue = NEXT(tmp);
1054 }
Roman Zippel3130d902007-05-01 22:32:37 +02001055 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001056 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001057
1058 /* reenable interrupts after finding one */
1059 local_irq_restore(flags);
1060
1061 /*
1062 * Attempt to establish an I_T_L nexus here.
1063 * On success, instance->hostdata->connected is set.
1064 * On failure, we must add the command back to the
1065 * issue queue so we can keep trying.
1066 */
Finn Thaind65e6342014-03-18 11:42:20 +11001067 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001068 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001069 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001070 /*
1071 * REQUEST SENSE commands are issued without tagged
1072 * queueing, even on SCSI-II devices because the
1073 * contingent allegiance condition exists for the
1074 * entire unit.
1075 */
1076 /* ++roman: ...and the standard also requires that
1077 * REQUEST SENSE command are untagged.
1078 */
1079
1080#ifdef SUPPORT_TAGS
1081 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1082#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001083 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001084 /* OK or bad target */
Finn Thaine3c3da62014-11-12 16:12:15 +11001085 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001086 hostdata->retain_dma_intr--;
Finn Thaine3c3da62014-11-12 16:12:15 +11001087 maybe_release_dma_irq(instance);
1088 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001089 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001090 /* Need to retry */
Roman Zippelc28bda22007-05-01 22:32:36 +02001091 local_irq_disable();
1092 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001093 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001094 hostdata->issue_queue = tmp;
1095#ifdef SUPPORT_TAGS
1096 cmd_free_tag(tmp);
1097#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001098 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001099 local_irq_restore(flags);
Finn Thain1d3db592016-01-03 16:05:24 +11001100 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001101 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001102 "returned to issue_queue\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001103 }
Finn Thain1f1b0c72016-01-03 16:05:17 +11001104 if (hostdata->connected)
1105 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001106 } /* if target/lun/target queue is not busy */
1107 } /* for issue_queue */
1108 } /* if (!hostdata->connected) */
1109
1110 if (hostdata->connected
1111#ifdef REAL_DMA
1112 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#endif
1114 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001116 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001117 HOSTNO);
1118 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001119 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001120 done = 0;
1121 }
1122 } while (!done);
Roman Zippelc28bda22007-05-01 22:32:36 +02001123 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126
1127#ifdef REAL_DMA
1128/*
1129 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1130 *
1131 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001132 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
1134 * Inputs : instance - this instance of the NCR5380.
1135 *
1136 */
1137
Roman Zippelc28bda22007-05-01 22:32:36 +02001138static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Roman Zippelc28bda22007-05-01 22:32:36 +02001140 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001141 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001142 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001143 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001144 int saved_data = 0, overrun = 0;
1145 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Roman Zippelc28bda22007-05-01 22:32:36 +02001147 if (!hostdata->connected) {
1148 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1149 "no connected cmd\n", HOSTNO);
1150 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Finn Thainef1081c2014-11-12 16:12:14 +11001153 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001154 p = hostdata->connected->SCp.phase;
1155 if (p & SR_IO) {
1156 udelay(10);
1157 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1158 (BASR_PHASE_MATCH|BASR_ACK)) ==
1159 (BASR_PHASE_MATCH|BASR_ACK)) {
1160 saved_data = NCR5380_read(INPUT_DATA_REG);
1161 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001162 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001163 }
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001166
Finn Thaind65e6342014-03-18 11:42:20 +11001167 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001168 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1169 NCR5380_read(STATUS_REG));
1170
Finn Thaine3f463b2014-11-12 16:12:16 +11001171#if defined(CONFIG_SUN3)
1172 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1173 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1174 instance->host_no);
1175 BUG();
1176 }
1177
1178 /* make sure we're not stuck in a data phase */
1179 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1180 (BASR_PHASE_MATCH | BASR_ACK)) {
1181 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1182 NCR5380_read(BUS_AND_STATUS_REG));
1183 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1184 instance->host_no);
1185 BUG();
1186 }
1187#endif
1188
Roman Zippelc28bda22007-05-01 22:32:36 +02001189 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1190 NCR5380_write(MODE_REG, MR_BASE);
1191 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1192
Finn Thain01bd9082014-11-12 16:12:23 +11001193 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001194 hostdata->dma_len = 0;
1195
1196 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1197 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001198 *data += transferred;
1199 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001200
Finn Thainef1081c2014-11-12 16:12:14 +11001201 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001202 int cnt, toPIO;
1203
Roman Zippelc28bda22007-05-01 22:32:36 +02001204 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001205 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001206 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001207 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001208 *(*data)++ = saved_data;
1209 (*count)--;
1210 cnt--;
1211 toPIO--;
1212 }
Finn Thaind65e6342014-03-18 11:42:20 +11001213 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001214 NCR5380_transfer_pio(instance, &p, &cnt, data);
1215 *count -= toPIO - cnt;
1216 }
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219#endif /* REAL_DMA */
1220
1221
Finn Thainff50f9e2014-11-12 16:12:18 +11001222/**
1223 * NCR5380_intr - generic NCR5380 irq handler
1224 * @irq: interrupt number
1225 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001226 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001227 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1228 * from the disconnected queue, and restarting NCR5380_main()
1229 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
1231
Roman Zippelc28bda22007-05-01 22:32:36 +02001232static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Finn Thaina53a21e2014-11-12 16:12:21 +11001234 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001235 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001236 int done = 1, handled = 0;
1237 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Finn Thaind65e6342014-03-18 11:42:20 +11001239 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Roman Zippelc28bda22007-05-01 22:32:36 +02001241 /* Look for pending interrupts */
1242 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001243 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001244 /* dispatch to appropriate routine if found and done=0 */
1245 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001246 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001247 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1248 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001249 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001250 NCR5380_reselect(instance);
1251 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1252 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001253 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001254 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1255 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001256 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001257 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1258 } else {
1259 /*
1260 * The rest of the interrupt conditions can occur only during a
1261 * DMA transfer
1262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001265 /*
1266 * We should only get PHASE MISMATCH and EOP interrupts if we have
1267 * DMA enabled, so do a sanity check based on the current setting
1268 * of the MODE register.
1269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Roman Zippelc28bda22007-05-01 22:32:36 +02001271 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1272 ((basr & BASR_END_DMA_TRANSFER) ||
1273 !(basr & BASR_PHASE_MATCH))) {
1274
Finn Thaind65e6342014-03-18 11:42:20 +11001275 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001276 NCR5380_dma_complete( instance );
1277 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001278 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001280 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001282 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001283 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001284 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1285 HOSTNO, basr, NCR5380_read(MODE_REG),
1286 NCR5380_read(STATUS_REG));
1287 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001288#ifdef SUN3_SCSI_VME
1289 dregs->csr |= CSR_DMA_ENABLE;
1290#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001291 }
1292 } /* if !(SELECTION || PARITY) */
1293 handled = 1;
1294 } /* BASR & IRQ */ else {
1295 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1296 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1297 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1298 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001299#ifdef SUN3_SCSI_VME
1300 dregs->csr |= CSR_DMA_ENABLE;
1301#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001302 }
1303
Finn Thain010e89d2016-01-03 16:05:38 +11001304 if (!done)
1305 queue_work(hostdata->work_q, &hostdata->main_task);
1306
Roman Zippelc28bda22007-05-01 22:32:36 +02001307 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Roman Zippelc28bda22007-05-01 22:32:36 +02001310/*
Finn Thain710ddd02014-11-12 16:12:02 +11001311 * Function : int NCR5380_select(struct Scsi_Host *instance,
1312 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 *
1314 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001315 * including ARBITRATION, SELECTION, and initial message out for
1316 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001319 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 *
Finn Thain63238762016-01-03 16:05:15 +11001321 * Returns : -1 if selection failed but should be retried.
1322 * 0 if selection failed and should not be retried.
1323 * 0 if selection succeeded completely (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001324 *
1325 * Side effects :
1326 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * with registers as they should have been on entry - ie
1328 * SELECT_ENABLE will be set appropriately, the NCR5380
1329 * will cease to drive any SCSI bus signals.
1330 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001331 * If successful : I_T_L or I_T_L_Q nexus will be established,
1332 * instance->connected will be set to cmd.
1333 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001335 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * cmd->result host byte set to DID_BAD_TARGET.
1337 */
1338
Finn Thain710ddd02014-11-12 16:12:02 +11001339static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Roman Zippelc28bda22007-05-01 22:32:36 +02001341 SETUP_HOSTDATA(instance);
1342 unsigned char tmp[3], phase;
1343 unsigned char *data;
1344 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001345 int err;
Roman Zippelc28bda22007-05-01 22:32:36 +02001346 unsigned long flags;
Finn Thain55500d92016-01-03 16:05:35 +11001347 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Finn Thain8ad3a592014-03-18 11:42:19 +11001349 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001350 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001351 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Roman Zippelc28bda22007-05-01 22:32:36 +02001353 /*
1354 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1355 * data bus during SELECTION.
1356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Roman Zippelc28bda22007-05-01 22:32:36 +02001358 local_irq_save(flags);
1359 if (hostdata->connected) {
1360 local_irq_restore(flags);
1361 return -1;
1362 }
1363 NCR5380_write(TARGET_COMMAND_REG, 0);
1364
1365 /*
1366 * Start arbitration.
1367 */
1368
1369 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1370 NCR5380_write(MODE_REG, MR_ARBITRATE);
1371
Finn Thain55500d92016-01-03 16:05:35 +11001372 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1373 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Finn Thain55500d92016-01-03 16:05:35 +11001376 local_irq_restore(flags);
1377 timeout = jiffies + HZ;
1378 while (1) {
1379 if (time_is_before_jiffies(timeout)) {
1380 NCR5380_write(MODE_REG, MR_BASE);
1381 shost_printk(KERN_ERR, instance,
1382 "select: arbitration timeout\n");
1383 return -1;
1384 }
1385 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1386 /* Reselection interrupt */
1387 return -1;
1388 }
1389 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1390 break;
1391 }
1392
1393 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001394 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Roman Zippelc28bda22007-05-01 22:32:36 +02001396 /* Check for lost arbitration */
1397 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1398 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1399 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1400 hostdata->connected) {
1401 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001402 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001403 HOSTNO);
1404 return -1;
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Finn Thaincf13b082016-01-03 16:05:18 +11001407 /* After/during arbitration, BSY should be asserted.
1408 * IBM DPES-31080 Version S31Q works now
1409 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1410 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001411 NCR5380_write(INITIATOR_COMMAND_REG,
1412 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Roman Zippelc28bda22007-05-01 22:32:36 +02001414 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1415 hostdata->connected) {
1416 NCR5380_write(MODE_REG, MR_BASE);
1417 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001418 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001419 HOSTNO);
1420 return -1;
1421 }
1422
1423 /*
1424 * Again, bus clear + bus settle time is 1.2us, however, this is
1425 * a minimum so we'll udelay ceil(1.2)
1426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Finn Thain9c3f0e22016-01-03 16:05:11 +11001428 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1429 udelay(15);
1430 else
1431 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001432
1433 if (hostdata->connected) {
1434 NCR5380_write(MODE_REG, MR_BASE);
1435 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1436 return -1;
1437 }
1438
Finn Thaind65e6342014-03-18 11:42:20 +11001439 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001440
1441 /*
1442 * Now that we have won arbitration, start Selection process, asserting
1443 * the host and target ID's on the SCSI bus.
1444 */
1445
1446 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1447
1448 /*
1449 * Raise ATN while SEL is true before BSY goes false from arbitration,
1450 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1451 * phase immediately after selection.
1452 */
1453
1454 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1455 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Roman Zippelc28bda22007-05-01 22:32:36 +02001458 /*
1459 * Reselect interrupts must be turned off prior to the dropping of BSY,
1460 * otherwise we will trigger an interrupt.
1461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 if (hostdata->connected) {
1464 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1465 return -1;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Roman Zippelc28bda22007-05-01 22:32:36 +02001470 /*
1471 * The initiator shall then wait at least two deskew delays and release
1472 * the BSY signal.
1473 */
1474 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Roman Zippelc28bda22007-05-01 22:32:36 +02001476 /* Reset BSY */
1477 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1478 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Roman Zippelc28bda22007-05-01 22:32:36 +02001480 /*
1481 * Something weird happens when we cease to drive BSY - looks
1482 * like the board/chip is letting us do another read before the
1483 * appropriate propagation delay has expired, and we're confusing
1484 * a BSY signal from ourselves as the target's response to SELECTION.
1485 *
1486 * A small delay (the 'C++' frontend breaks the pipeline with an
1487 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1488 * tighter 'C' code breaks and requires this) solves the problem -
1489 * the 1 us delay is arbitrary, and only used because this delay will
1490 * be the same on other platforms and since it works here, it should
1491 * work there.
1492 *
1493 * wingel suggests that this could be due to failing to wait
1494 * one deskew delay.
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Roman Zippelc28bda22007-05-01 22:32:36 +02001497 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Finn Thaind65e6342014-03-18 11:42:20 +11001499 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Roman Zippelc28bda22007-05-01 22:32:36 +02001501 /*
1502 * The SCSI specification calls for a 250 ms timeout for the actual
1503 * selection.
1504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Finn Thainae753a32016-01-03 16:05:23 +11001506 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1507 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Roman Zippelc28bda22007-05-01 22:32:36 +02001509 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1510 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1511 NCR5380_reselect(instance);
1512 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1513 HOSTNO);
1514 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1515 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001517
Finn Thainae753a32016-01-03 16:05:23 +11001518 if (err < 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001519 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001520 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001521#ifdef SUPPORT_TAGS
1522 cmd_free_tag(cmd);
1523#endif
1524 cmd->scsi_done(cmd);
1525 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001526 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001527 return 0;
1528 }
1529
Roman Zippelc28bda22007-05-01 22:32:36 +02001530 /*
Finn Thainae753a32016-01-03 16:05:23 +11001531 * No less than two deskew delays after the initiator detects the
1532 * BSY signal is true, it shall release the SEL signal and may
1533 * change the DATA BUS. -wingel
1534 */
1535
1536 udelay(1);
1537
1538 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1539
1540 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001541 * Since we followed the SCSI spec, and raised ATN while SEL
1542 * was true but before BSY was false during selection, the information
1543 * transfer phase should be a MESSAGE OUT phase so that we can send the
1544 * IDENTIFY message.
1545 *
1546 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1547 * message (2 bytes) with a tag ID that we increment with every command
1548 * until it wraps back to 0.
1549 *
1550 * XXX - it turns out that there are some broken SCSI-II devices,
1551 * which claim to support tagged queuing but fail when more than
1552 * some number of commands are issued at once.
1553 */
1554
1555 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001556
1557 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1558 if (err < 0) {
1559 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1560 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1561 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1562 return -1;
1563 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001564
Finn Thaind65e6342014-03-18 11:42:20 +11001565 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001566 HOSTNO, cmd->device->id);
1567 tmp[0] = IDENTIFY(1, cmd->device->lun);
1568
1569#ifdef SUPPORT_TAGS
1570 if (cmd->tag != TAG_NONE) {
1571 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1572 tmp[2] = cmd->tag;
1573 len = 3;
1574 } else
1575 len = 1;
1576#else
1577 len = 1;
1578 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579#endif /* SUPPORT_TAGS */
1580
Roman Zippelc28bda22007-05-01 22:32:36 +02001581 /* Send message(s) */
1582 data = tmp;
1583 phase = PHASE_MSGOUT;
1584 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001585 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001586 /* XXX need to handle errors here */
1587 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001589 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1590#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001591#ifdef SUN3_SCSI_VME
1592 dregs->csr |= CSR_INTR;
1593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Roman Zippelc28bda22007-05-01 22:32:36 +02001595 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Roman Zippelc28bda22007-05-01 22:32:36 +02001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Roman Zippelc28bda22007-05-01 22:32:36 +02001600/*
1601 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 * unsigned char *phase, int *count, unsigned char **data)
1603 *
1604 * Purpose : transfers data in given phase using polled I/O
1605 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001606 * Inputs : instance - instance of driver, *phase - pointer to
1607 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001609 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001611 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * is in same phase.
1613 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001614 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *
1616 * XXX Note : handling for bus free may be useful.
1617 */
1618
1619/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001620 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * IS 100% reliable, and for the actual data transfer where speed
1622 * counts, we will always do a pseudo DMA or DMA transfer.
1623 */
1624
Roman Zippelc28bda22007-05-01 22:32:36 +02001625static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1626 unsigned char *phase, int *count,
1627 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Roman Zippelc28bda22007-05-01 22:32:36 +02001629 register unsigned char p = *phase, tmp;
1630 register int c = *count;
1631 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Roman Zippelc28bda22007-05-01 22:32:36 +02001633 /*
1634 * The NCR5380 chip will only drive the SCSI bus when the
1635 * phase specified in the appropriate bits of the TARGET COMMAND
1636 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 */
1638
Roman Zippelc28bda22007-05-01 22:32:36 +02001639 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Roman Zippelc28bda22007-05-01 22:32:36 +02001641 do {
1642 /*
1643 * Wait for assertion of REQ, after which the phase bits will be
1644 * valid
1645 */
Finn Thain686f3992016-01-03 16:05:26 +11001646
1647 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Finn Thaind65e6342014-03-18 11:42:20 +11001650 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Roman Zippelc28bda22007-05-01 22:32:36 +02001652 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001653 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001654 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001655 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001656 break;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Roman Zippelc28bda22007-05-01 22:32:36 +02001659 /* Do actual transfer from SCSI bus to / from memory */
1660 if (!(p & SR_IO))
1661 NCR5380_write(OUTPUT_DATA_REG, *d);
1662 else
1663 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Roman Zippelc28bda22007-05-01 22:32:36 +02001665 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Roman Zippelc28bda22007-05-01 22:32:36 +02001667 /*
1668 * The SCSI standard suggests that in MSGOUT phase, the initiator
1669 * should drop ATN on the last byte of the message phase
1670 * after REQ has been asserted for the handshake but before
1671 * the initiator raises ACK.
1672 */
1673
1674 if (!(p & SR_IO)) {
1675 if (!((p & SR_MSG) && c > 1)) {
1676 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001677 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001678 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1679 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1680 } else {
1681 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1682 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001683 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001684 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1685 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1686 }
1687 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001688 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001689 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1690 }
1691
Finn Thaina2edc4a2016-01-03 16:05:27 +11001692 if (NCR5380_poll_politely(instance,
1693 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1694 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001695
Finn Thaind65e6342014-03-18 11:42:20 +11001696 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001697
1698 /*
1699 * We have several special cases to consider during REQ/ACK handshaking :
1700 * 1. We were in MSGOUT phase, and we are on the last byte of the
1701 * message. ATN must be dropped as ACK is dropped.
1702 *
1703 * 2. We are in a MSGIN phase, and we are on the last byte of the
1704 * message. We must exit with ACK asserted, so that the calling
1705 * code may raise ATN before dropping ACK to reject the message.
1706 *
1707 * 3. ACK and ATN are clear and the target may proceed as normal.
1708 */
1709 if (!(p == PHASE_MSGIN && c == 1)) {
1710 if (p == PHASE_MSGOUT && c > 1)
1711 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1712 else
1713 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1714 }
1715 } while (--c);
1716
Finn Thaind65e6342014-03-18 11:42:20 +11001717 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001718
1719 *count = c;
1720 *data = d;
1721 tmp = NCR5380_read(STATUS_REG);
1722 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001723 * asserted or if ACK hasn't been released yet. The latter applies if
1724 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001725 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001726 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001727 *phase = tmp & PHASE_MASK;
1728 else
1729 *phase = PHASE_UNKNOWN;
1730
1731 if (!c || (*phase == p))
1732 return 0;
1733 else
1734 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Finn Thain636b1ec2016-01-03 16:05:10 +11001737/**
1738 * do_reset - issue a reset command
1739 * @instance: adapter to reset
1740 *
1741 * Issue a reset sequence to the NCR5380 and try and get the bus
1742 * back into sane shape.
1743 *
1744 * This clears the reset interrupt flag because there may be no handler for
1745 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1746 * been installed. And when in EH we may have released the ST DMA interrupt.
1747 */
1748
1749static void do_reset(struct Scsi_Host *instance)
1750{
1751 unsigned long flags;
1752
1753 local_irq_save(flags);
1754 NCR5380_write(TARGET_COMMAND_REG,
1755 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1756 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1757 udelay(50);
1758 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1759 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1760 local_irq_restore(flags);
1761}
1762
Finn Thain80d3eb62016-01-03 16:05:34 +11001763/**
1764 * do_abort - abort the currently established nexus by going to
1765 * MESSAGE OUT phase and sending an ABORT message.
1766 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001767 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001768 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 */
1770
Finn Thaina53a21e2014-11-12 16:12:21 +11001771static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Roman Zippelc28bda22007-05-01 22:32:36 +02001773 unsigned char tmp, *msgptr, phase;
1774 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001775 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Roman Zippelc28bda22007-05-01 22:32:36 +02001777 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Roman Zippelc28bda22007-05-01 22:32:36 +02001780 /*
1781 * Wait for the target to indicate a valid phase by asserting
1782 * REQ. Once this happens, we'll have either a MSGOUT phase
1783 * and can immediately send the ABORT message, or we'll have some
1784 * other phase and will have to source/sink data.
1785 *
1786 * We really don't care what value was on the bus or what value
1787 * the target sees, so we just handshake.
1788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Finn Thain80d3eb62016-01-03 16:05:34 +11001790 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1791 if (rc < 0)
1792 goto timeout;
1793
1794 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001795
1796 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1797
Finn Thain80d3eb62016-01-03 16:05:34 +11001798 if (tmp != PHASE_MSGOUT) {
1799 NCR5380_write(INITIATOR_COMMAND_REG,
1800 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1801 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1802 if (rc < 0)
1803 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001804 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1805 }
1806
1807 tmp = ABORT;
1808 msgptr = &tmp;
1809 len = 1;
1810 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001811 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001812
1813 /*
1814 * If we got here, and the command completed successfully,
1815 * we're about to go into bus free state.
1816 */
1817
1818 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001819
1820timeout:
1821 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1822 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
1825#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001826/*
1827 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 * unsigned char *phase, int *count, unsigned char **data)
1829 *
1830 * Purpose : transfers data in given phase using either real
1831 * or pseudo DMA.
1832 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001833 * Inputs : instance - instance of driver, *phase - pointer to
1834 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001836 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001838 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 * is in same phase.
1840 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001841 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 *
1843 */
1844
1845
Roman Zippelc28bda22007-05-01 22:32:36 +02001846static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1847 unsigned char *phase, int *count,
1848 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Roman Zippelc28bda22007-05-01 22:32:36 +02001850 SETUP_HOSTDATA(instance);
1851 register int c = *count;
1852 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001853 unsigned long flags;
1854
1855#if defined(CONFIG_SUN3)
1856 /* sanity check */
1857 if (!sun3_dma_setup_done) {
1858 pr_err("scsi%d: transfer_dma without setup!\n",
1859 instance->host_no);
1860 BUG();
1861 }
1862 hostdata->dma_len = c;
1863
1864 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1865 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1866 c, (p & SR_IO) ? "to" : "from", *data);
1867
1868 /* netbsd turns off ints here, why not be safe and do it too */
1869 local_irq_save(flags);
1870
1871 /* send start chain */
1872 sun3scsi_dma_start(c, *data);
1873
1874 if (p & SR_IO) {
1875 NCR5380_write(TARGET_COMMAND_REG, 1);
1876 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1877 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1878 NCR5380_write(MODE_REG,
1879 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1880 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1881 } else {
1882 NCR5380_write(TARGET_COMMAND_REG, 0);
1883 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1884 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1885 NCR5380_write(MODE_REG,
1886 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1887 NCR5380_write(START_DMA_SEND_REG, 0);
1888 }
1889
1890#ifdef SUN3_SCSI_VME
1891 dregs->csr |= CSR_DMA_ENABLE;
1892#endif
1893
1894 local_irq_restore(flags);
1895
1896 sun3_dma_active = 1;
1897
1898#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001899 register unsigned char *d = *data;
1900 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Roman Zippelc28bda22007-05-01 22:32:36 +02001902 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1903 *phase = tmp;
1904 return -1;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Finn Thainef1081c2014-11-12 16:12:14 +11001907 if (hostdata->read_overruns && (p & SR_IO))
1908 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Finn Thaind65e6342014-03-18 11:42:20 +11001910 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001911 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1912 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Roman Zippelc28bda22007-05-01 22:32:36 +02001914 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001917 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918#endif /* def REAL_DMA */
1919
Finn Thainef1081c2014-11-12 16:12:14 +11001920 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001921 /* On the Medusa, it is a must to initialize the DMA before
1922 * starting the NCR. This is also the cleaner way for the TT.
1923 */
1924 local_irq_save(flags);
1925 hostdata->dma_len = (p & SR_IO) ?
1926 NCR5380_dma_read_setup(instance, d, c) :
1927 NCR5380_dma_write_setup(instance, d, c);
1928 local_irq_restore(flags);
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Roman Zippelc28bda22007-05-01 22:32:36 +02001931 if (p & SR_IO)
1932 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1933 else {
1934 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1935 NCR5380_write(START_DMA_SEND_REG, 0);
1936 }
1937
Finn Thainef1081c2014-11-12 16:12:14 +11001938 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001939 /* On the Falcon, the DMA setup must be done after the last */
1940 /* NCR access, else the DMA setup gets trashed!
1941 */
1942 local_irq_save(flags);
1943 hostdata->dma_len = (p & SR_IO) ?
1944 NCR5380_dma_read_setup(instance, d, c) :
1945 NCR5380_dma_write_setup(instance, d, c);
1946 local_irq_restore(flags);
1947 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001948#endif /* !defined(CONFIG_SUN3) */
1949
Roman Zippelc28bda22007-05-01 22:32:36 +02001950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952#endif /* defined(REAL_DMA) */
1953
1954/*
1955 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1956 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001957 * Purpose : run through the various SCSI phases and do as the target
1958 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * instance->connected.
1960 *
1961 * Inputs : instance, instance for which we are doing commands
1962 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001963 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 * modified if a command disconnects, *instance->connected will
1965 * change.
1966 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001967 * XXX Note : we need to watch for bus free or a reset condition here
1968 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001970
1971static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Roman Zippelc28bda22007-05-01 22:32:36 +02001973 SETUP_HOSTDATA(instance);
1974 unsigned long flags;
1975 unsigned char msgout = NOP;
1976 int sink = 0;
1977 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001979 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001981 unsigned char *data;
1982 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001983 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Finn Thaine3f463b2014-11-12 16:12:16 +11001985#ifdef SUN3_SCSI_VME
1986 dregs->csr |= CSR_INTR;
1987#endif
1988
Roman Zippelc28bda22007-05-01 22:32:36 +02001989 while (1) {
1990 tmp = NCR5380_read(STATUS_REG);
1991 /* We only have a valid SCSI phase when REQ is asserted */
1992 if (tmp & SR_REQ) {
1993 phase = (tmp & PHASE_MASK);
1994 if (phase != old_phase) {
1995 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001996 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001998#if defined(CONFIG_SUN3)
1999 if (phase == PHASE_CMDOUT) {
2000#if defined(REAL_DMA)
2001 void *d;
2002 unsigned long count;
2003
2004 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2005 count = cmd->SCp.buffer->length;
2006 d = sg_virt(cmd->SCp.buffer);
2007 } else {
2008 count = cmd->SCp.this_residual;
2009 d = cmd->SCp.ptr;
2010 }
2011 /* this command setup for dma yet? */
2012 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2013 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2014 sun3scsi_dma_setup(d, count,
2015 rq_data_dir(cmd->request));
2016 sun3_dma_setup_done = cmd;
2017 }
2018 }
2019#endif
2020#ifdef SUN3_SCSI_VME
2021 dregs->csr |= CSR_INTR;
2022#endif
2023 }
2024#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Roman Zippelc28bda22007-05-01 22:32:36 +02002026 if (sink && (phase != PHASE_MSGOUT)) {
2027 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Roman Zippelc28bda22007-05-01 22:32:36 +02002029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2030 ICR_ASSERT_ACK);
2031 while (NCR5380_read(STATUS_REG) & SR_REQ)
2032 ;
2033 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2034 ICR_ASSERT_ATN);
2035 sink = 0;
2036 continue;
2037 }
2038
2039 switch (phase) {
2040 case PHASE_DATAOUT:
2041#if (NDEBUG & NDEBUG_NO_DATAOUT)
2042 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2043 "aborted\n", HOSTNO);
2044 sink = 1;
2045 do_abort(instance);
2046 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002047 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002048 return;
2049#endif
2050 case PHASE_DATAIN:
2051 /*
2052 * If there is no room left in the current buffer in the
2053 * scatter-gather list, move onto the next one.
2054 */
2055
2056 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2057 ++cmd->SCp.buffer;
2058 --cmd->SCp.buffers_residual;
2059 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002060 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002061 /* ++roman: Try to merge some scatter-buffers if
2062 * they are at contiguous physical addresses.
2063 */
2064 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002065 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002066 HOSTNO, cmd->SCp.this_residual,
2067 cmd->SCp.buffers_residual);
2068 }
2069
2070 /*
2071 * The preferred transfer method is going to be
2072 * PSEUDO-DMA for systems that are strictly PIO,
2073 * since we can let the hardware do the handshaking.
2074 *
2075 * For this to work, we need to know the transfersize
2076 * ahead of time, since the pseudo-DMA code will sit
2077 * in an unconditional loop.
2078 */
2079
2080 /* ++roman: I suggest, this should be
2081 * #if def(REAL_DMA)
2082 * instead of leaving REAL_DMA out.
2083 */
2084
2085#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002086#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11002087 transfersize = 0;
2088 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11002089#endif
Finn Thainff3d4572016-01-03 16:05:25 +11002090 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
2091
2092 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002093 len = transfersize;
2094 cmd->SCp.phase = phase;
2095 if (NCR5380_transfer_dma(instance, &phase,
2096 &len, (unsigned char **)&cmd->SCp.ptr)) {
2097 /*
2098 * If the watchdog timer fires, all future
2099 * accesses to this device will use the
2100 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002101 scmd_printk(KERN_INFO, cmd,
2102 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002103 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02002104 sink = 1;
2105 do_abort(instance);
2106 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002107 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002108 /* XXX - need to source or sink data here, as appropriate */
2109 } else {
2110#ifdef REAL_DMA
2111 /* ++roman: When using real DMA,
2112 * information_transfer() should return after
2113 * starting DMA since it has nothing more to
2114 * do.
2115 */
2116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002118 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002120 }
2121 } else
2122#endif /* defined(REAL_DMA) */
2123 NCR5380_transfer_pio(instance, &phase,
2124 (int *)&cmd->SCp.this_residual,
2125 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002126#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2127 /* if we had intended to dma that command clear it */
2128 if (sun3_dma_setup_done == cmd)
2129 sun3_dma_setup_done = NULL;
2130#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002131 break;
2132 case PHASE_MSGIN:
2133 len = 1;
2134 data = &tmp;
2135 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2136 NCR5380_transfer_pio(instance, &phase, &len, &data);
2137 cmd->SCp.Message = tmp;
2138
2139 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002140 case ABORT:
2141 case COMMAND_COMPLETE:
2142 /* Accept message by clearing ACK */
2143 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002144 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002145 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002146
2147 local_irq_save(flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002148 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002149#ifdef SUPPORT_TAGS
2150 cmd_free_tag(cmd);
2151 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2152 /* Turn a QUEUE FULL status into BUSY, I think the
2153 * mid level cannot handle QUEUE FULL :-( (The
2154 * command is retried after BUSY). Also update our
2155 * queue size to the number of currently issued
2156 * commands now.
2157 */
2158 /* ++Andreas: the mid level code knows about
2159 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002160 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002161 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002162 "QUEUE_FULL after %d commands\n",
2163 HOSTNO, cmd->device->id, cmd->device->lun,
2164 ta->nr_allocated);
2165 if (ta->queue_size > ta->nr_allocated)
2166 ta->nr_allocated = ta->queue_size;
2167 }
2168#else
2169 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2170#endif
2171 /* Enable reselect interrupts */
2172 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2173
2174 /*
2175 * I'm not sure what the correct thing to do here is :
2176 *
2177 * If the command that just executed is NOT a request
2178 * sense, the obvious thing to do is to set the result
2179 * code to the values of the stored parameters.
2180 *
2181 * If it was a REQUEST SENSE command, we need some way to
2182 * differentiate between the failure code of the original
2183 * and the failure code of the REQUEST sense - the obvious
2184 * case is success, where we fall through and leave the
2185 * result code unchanged.
2186 *
2187 * The non-obvious place is where the REQUEST SENSE failed
2188 */
2189
2190 if (cmd->cmnd[0] != REQUEST_SENSE)
2191 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2192 else if (status_byte(cmd->SCp.Status) != GOOD)
2193 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2194
Boaz Harrosh28424d32007-09-10 22:37:45 +03002195 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2196 hostdata->ses.cmd_len) {
2197 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2198 hostdata->ses.cmd_len = 0 ;
2199 }
2200
Roman Zippelc28bda22007-05-01 22:32:36 +02002201 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2202 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002203 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002204
Finn Thaind65e6342014-03-18 11:42:20 +11002205 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002206
Roman Zippelc28bda22007-05-01 22:32:36 +02002207 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002208 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002209 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002210 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002211 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002212 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002213 cmd->scsi_done(cmd);
2214 }
2215
2216 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2217 /*
2218 * Restore phase bits to 0 so an interrupted selection,
2219 * arbitration can resume.
2220 */
2221 NCR5380_write(TARGET_COMMAND_REG, 0);
2222
Roman Zippelc28bda22007-05-01 22:32:36 +02002223 /* ++roman: For Falcon SCSI, release the lock on the
2224 * ST-DMA here if no other commands are waiting on the
2225 * disconnected queue.
2226 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002227 maybe_release_dma_irq(instance);
2228 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002229 return;
2230 case MESSAGE_REJECT:
2231 /* Accept message by clearing ACK */
2232 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2233 /* Enable reselect interrupts */
2234 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2235 switch (hostdata->last_message) {
2236 case HEAD_OF_QUEUE_TAG:
2237 case ORDERED_QUEUE_TAG:
2238 case SIMPLE_QUEUE_TAG:
2239 /* The target obviously doesn't support tagged
2240 * queuing, even though it announced this ability in
2241 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2242 * clear 'tagged_supported' and lock the LUN, since
2243 * the command is treated as untagged further on.
2244 */
2245 cmd->device->tagged_supported = 0;
2246 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2247 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002248 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002249 "QUEUE_TAG message; tagged queuing "
2250 "disabled\n",
2251 HOSTNO, cmd->device->id, cmd->device->lun);
2252 break;
2253 }
2254 break;
2255 case DISCONNECT:
2256 /* Accept message by clearing ACK */
2257 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2258 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002259 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002260 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002261 hostdata->connected = NULL;
2262 hostdata->disconnected_queue = cmd;
2263 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002264 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002265 "moved from connected to the "
2266 "disconnected_queue\n", HOSTNO,
2267 cmd->device->id, cmd->device->lun);
2268 /*
2269 * Restore phase bits to 0 so an interrupted selection,
2270 * arbitration can resume.
2271 */
2272 NCR5380_write(TARGET_COMMAND_REG, 0);
2273
2274 /* Enable reselect interrupts */
2275 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002276#ifdef SUN3_SCSI_VME
2277 dregs->csr |= CSR_DMA_ENABLE;
2278#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002279 return;
2280 /*
2281 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2282 * operation, in violation of the SCSI spec so we can safely
2283 * ignore SAVE/RESTORE pointers calls.
2284 *
2285 * Unfortunately, some disks violate the SCSI spec and
2286 * don't issue the required SAVE_POINTERS message before
2287 * disconnecting, and we have to break spec to remain
2288 * compatible.
2289 */
2290 case SAVE_POINTERS:
2291 case RESTORE_POINTERS:
2292 /* Accept message by clearing ACK */
2293 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2294 /* Enable reselect interrupts */
2295 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2296 break;
2297 case EXTENDED_MESSAGE:
2298 /*
2299 * Extended messages are sent in the following format :
2300 * Byte
2301 * 0 EXTENDED_MESSAGE == 1
2302 * 1 length (includes one byte for code, doesn't
2303 * include first two bytes)
2304 * 2 code
2305 * 3..length+1 arguments
2306 *
2307 * Start the extended message buffer with the EXTENDED_MESSAGE
2308 * byte, since spi_print_msg() wants the whole thing.
2309 */
2310 extended_msg[0] = EXTENDED_MESSAGE;
2311 /* Accept first byte by clearing ACK */
2312 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2313
Finn Thaind65e6342014-03-18 11:42:20 +11002314 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002315
2316 len = 2;
2317 data = extended_msg + 1;
2318 phase = PHASE_MSGIN;
2319 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002320 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002321 (int)extended_msg[1], (int)extended_msg[2]);
2322
2323 if (!len && extended_msg[1] <=
2324 (sizeof(extended_msg) - 1)) {
2325 /* Accept third byte by clearing ACK */
2326 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2327 len = extended_msg[1] - 1;
2328 data = extended_msg + 3;
2329 phase = PHASE_MSGIN;
2330
2331 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002332 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002333 HOSTNO, len);
2334
2335 switch (extended_msg[2]) {
2336 case EXTENDED_SDTR:
2337 case EXTENDED_WDTR:
2338 case EXTENDED_MODIFY_DATA_POINTER:
2339 case EXTENDED_EXTENDED_IDENTIFY:
2340 tmp = 0;
2341 }
2342 } else if (len) {
2343 printk(KERN_NOTICE "scsi%d: error receiving "
2344 "extended message\n", HOSTNO);
2345 tmp = 0;
2346 } else {
2347 printk(KERN_NOTICE "scsi%d: extended message "
2348 "code %02x length %d is too long\n",
2349 HOSTNO, extended_msg[2], extended_msg[1]);
2350 tmp = 0;
2351 }
2352 /* Fall through to reject message */
2353
2354 /*
2355 * If we get something weird that we aren't expecting,
2356 * reject it.
2357 */
2358 default:
2359 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002360 printk(KERN_INFO "scsi%d: rejecting message ",
2361 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002362 spi_print_msg(extended_msg);
2363 printk("\n");
2364 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002365 scmd_printk(KERN_INFO, cmd,
2366 "rejecting unknown message %02x\n",
2367 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002368 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002369 scmd_printk(KERN_INFO, cmd,
2370 "rejecting unknown extended message code %02x, length %d\n",
2371 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002372
2373 msgout = MESSAGE_REJECT;
2374 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2375 break;
2376 } /* switch (tmp) */
2377 break;
2378 case PHASE_MSGOUT:
2379 len = 1;
2380 data = &msgout;
2381 hostdata->last_message = msgout;
2382 NCR5380_transfer_pio(instance, &phase, &len, &data);
2383 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002384 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002385#ifdef SUPPORT_TAGS
2386 cmd_free_tag(cmd);
2387#else
2388 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2389#endif
2390 hostdata->connected = NULL;
2391 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002392 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002393 maybe_release_dma_irq(instance);
2394 local_irq_restore(flags);
2395 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002396 return;
2397 }
2398 msgout = NOP;
2399 break;
2400 case PHASE_CMDOUT:
2401 len = cmd->cmd_len;
2402 data = cmd->cmnd;
2403 /*
2404 * XXX for performance reasons, on machines with a
2405 * PSEUDO-DMA architecture we should probably
2406 * use the dma transfer function.
2407 */
2408 NCR5380_transfer_pio(instance, &phase, &len, &data);
2409 break;
2410 case PHASE_STATIN:
2411 len = 1;
2412 data = &tmp;
2413 NCR5380_transfer_pio(instance, &phase, &len, &data);
2414 cmd->SCp.Status = tmp;
2415 break;
2416 default:
2417 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002418 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002419 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002420 } else {
2421 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2422 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002423 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426/*
2427 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2428 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002429 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002430 * 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 -07002431 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * Inputs : instance - this instance of the NCR5380.
2434 *
2435 */
2436
2437
Finn Thaine3f463b2014-11-12 16:12:16 +11002438/* it might eventually prove necessary to do a dma setup on
2439 reselection, but it doesn't seem to be needed now -- sam */
2440
Roman Zippelc28bda22007-05-01 22:32:36 +02002441static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Roman Zippelc28bda22007-05-01 22:32:36 +02002443 SETUP_HOSTDATA(instance);
2444 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002445 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002447 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002449 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002450 int __maybe_unused len;
2451 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002452 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Roman Zippelc28bda22007-05-01 22:32:36 +02002454 /*
2455 * Disable arbitration, etc. since the host adapter obviously
2456 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Roman Zippelc28bda22007-05-01 22:32:36 +02002459 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Roman Zippelc28bda22007-05-01 22:32:36 +02002461 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2462
Finn Thaind65e6342014-03-18 11:42:20 +11002463 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002464
2465 /*
2466 * At this point, we have detected that our SCSI ID is on the bus,
2467 * SEL is true and BSY was false for at least one bus settle delay
2468 * (400 ns).
2469 *
2470 * We must assert BSY ourselves, until the target drops the SEL
2471 * signal.
2472 */
2473
2474 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2475
2476 while (NCR5380_read(STATUS_REG) & SR_SEL)
2477 ;
2478 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2479
2480 /*
2481 * Wait for target to go into MSGIN.
2482 */
2483
2484 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2485 ;
2486
Finn Thaine3f463b2014-11-12 16:12:16 +11002487#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2488 /* acknowledge toggle to MSGIN */
2489 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2490
2491 /* peek at the byte without really hitting the bus */
2492 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2493#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002494 len = 1;
2495 data = msg;
2496 phase = PHASE_MSGIN;
2497 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002498#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002499
2500 if (!(msg[0] & 0x80)) {
2501 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2502 spi_print_msg(msg);
2503 do_abort(instance);
2504 return;
2505 }
2506 lun = (msg[0] & 0x07);
2507
Finn Thaine3f463b2014-11-12 16:12:16 +11002508#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002509 /* If the phase is still MSGIN, the target wants to send some more
2510 * messages. In case it supports tagged queuing, this is probably a
2511 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2512 */
2513 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002514 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002515 /* Accept previous IDENTIFY message by clearing ACK */
2516 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2517 len = 2;
2518 data = msg + 1;
2519 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2520 msg[1] == SIMPLE_QUEUE_TAG)
2521 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002522 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002523 "reselection\n", HOSTNO, target_mask, lun, tag);
2524 }
2525#endif
2526
2527 /*
2528 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2529 * just reestablished, and remove it from the disconnected queue.
2530 */
2531
Finn Thain710ddd02014-11-12 16:12:02 +11002532 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002533 tmp; prev = tmp, tmp = NEXT(tmp)) {
2534 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2535#ifdef SUPPORT_TAGS
2536 && (tag == tmp->tag)
2537#endif
2538 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002539 if (prev) {
2540 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002541 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002542 } else {
2543 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2544 hostdata->disconnected_queue = NEXT(tmp);
2545 }
Roman Zippel3130d902007-05-01 22:32:37 +02002546 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002547 break;
2548 }
2549 }
2550
2551 if (!tmp) {
2552 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2553#ifdef SUPPORT_TAGS
2554 "tag %d "
2555#endif
2556 "not in disconnected_queue.\n",
2557 HOSTNO, target_mask, lun
2558#ifdef SUPPORT_TAGS
2559 , tag
2560#endif
2561 );
2562 /*
2563 * Since we have an established nexus that we can't do anything
2564 * with, we must abort it.
2565 */
2566 do_abort(instance);
2567 return;
2568 }
2569
Finn Thaine3f463b2014-11-12 16:12:16 +11002570#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2571 /* engage dma setup for the command we just saw */
2572 {
2573 void *d;
2574 unsigned long count;
2575
2576 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2577 count = tmp->SCp.buffer->length;
2578 d = sg_virt(tmp->SCp.buffer);
2579 } else {
2580 count = tmp->SCp.this_residual;
2581 d = tmp->SCp.ptr;
2582 }
2583 /* setup this command for dma if not already */
2584 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2585 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2586 sun3_dma_setup_done = tmp;
2587 }
2588 }
2589
2590 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2591#endif
2592
Roman Zippelc28bda22007-05-01 22:32:36 +02002593 /* Accept message by clearing ACK */
2594 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2595
Finn Thaine3f463b2014-11-12 16:12:16 +11002596#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2597 /* If the phase is still MSGIN, the target wants to send some more
2598 * messages. In case it supports tagged queuing, this is probably a
2599 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2600 */
2601 tag = TAG_NONE;
2602 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2603 /* Accept previous IDENTIFY message by clearing ACK */
2604 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2605 len = 2;
2606 data = msg + 1;
2607 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2608 msg[1] == SIMPLE_QUEUE_TAG)
2609 tag = msg[2];
2610 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2611 HOSTNO, target_mask, lun, tag);
2612 }
2613#endif
2614
Roman Zippelc28bda22007-05-01 22:32:36 +02002615 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002616 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002617 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
2620
2621/*
Finn Thain710ddd02014-11-12 16:12:02 +11002622 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 *
2624 * Purpose : abort a command
2625 *
Finn Thain710ddd02014-11-12 16:12:02 +11002626 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002627 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 * used.
2629 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002630 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002632 * XXX - there is no way to abort the command that is currently
2633 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002635 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
2637
2638static
Finn Thain710ddd02014-11-12 16:12:02 +11002639int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Roman Zippelc28bda22007-05-01 22:32:36 +02002641 struct Scsi_Host *instance = cmd->device->host;
2642 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002643 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002644 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002646 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Roman Zippelc28bda22007-05-01 22:32:36 +02002648 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Roman Zippelc28bda22007-05-01 22:32:36 +02002650 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Finn Thaind65e6342014-03-18 11:42:20 +11002652 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002653 NCR5380_read(BUS_AND_STATUS_REG),
2654 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002657 /*
2658 * Case 1 : If the command is the currently executing command,
2659 * we'll set the aborted flag and return control so that
2660 * information transfer routine can exit cleanly.
2661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Roman Zippelc28bda22007-05-01 22:32:36 +02002663 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Finn Thaind65e6342014-03-18 11:42:20 +11002665 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002666 /*
2667 * We should perform BSY checking, and make sure we haven't slipped
2668 * into BUS FREE.
2669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Roman Zippelc28bda22007-05-01 22:32:36 +02002671 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2672 /*
2673 * Since we can't change phases until we've completed the current
2674 * handshake, we have to source or sink a byte of data if the current
2675 * phase is not MSGOUT.
2676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Roman Zippelc28bda22007-05-01 22:32:36 +02002678 /*
2679 * Return control to the executing NCR drive so we can clear the
2680 * aborted flag and get back into our main loop.
2681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Roman Zippelc28bda22007-05-01 22:32:36 +02002683 if (do_abort(instance) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002684 hostdata->connected = NULL;
2685 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002687 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002689 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002691 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002692 local_irq_restore(flags);
2693 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002694 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002695 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002696 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002697 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002698 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002702
2703 /*
2704 * Case 2 : If the command hasn't been issued yet, we simply remove it
2705 * from the issue queue.
2706 */
Finn Thain710ddd02014-11-12 16:12:02 +11002707 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2708 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002709 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2710 if (cmd == tmp) {
2711 REMOVE(5, *prev, tmp, NEXT(tmp));
2712 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002713 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002714 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002715 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002716 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002717 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002718 HOSTNO);
2719 /* Tagged queuing note: no tag to free here, hasn't been assigned
2720 * yet... */
2721 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002722 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 }
2725
Roman Zippelc28bda22007-05-01 22:32:36 +02002726 /*
2727 * Case 3 : If any commands are connected, we're going to fail the abort
2728 * and let the high level SCSI driver retry at a later time or
2729 * issue a reset.
2730 *
2731 * Timeouts, and therefore aborted commands, will be highly unlikely
2732 * and handling them cleanly in this situation would make the common
2733 * case of noresets less efficient, and would pollute our code. So,
2734 * we fail.
2735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Roman Zippelc28bda22007-05-01 22:32:36 +02002737 if (hostdata->connected) {
2738 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002739 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002740 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Roman Zippelc28bda22007-05-01 22:32:36 +02002743 /*
2744 * Case 4: If the command is currently disconnected from the bus, and
2745 * there are no connected commands, we reconnect the I_T_L or
2746 * I_T_L_Q nexus associated with it, go into message out, and send
2747 * an abort message.
2748 *
2749 * This case is especially ugly. In order to reestablish the nexus, we
2750 * need to call NCR5380_select(). The easiest way to implement this
2751 * function was to abort if the bus was busy, and let the interrupt
2752 * handler triggered on the SEL for reselect take care of lost arbitrations
2753 * where necessary, meaning interrupts need to be enabled.
2754 *
2755 * When interrupts are enabled, the queues may change - so we
2756 * can't remove it from the disconnected queue before selecting it
2757 * because that could cause a failure in hashing the nexus if that
2758 * device reselected.
2759 *
2760 * Since the queues may change, we can't use the pointers from when we
2761 * first locate it.
2762 *
2763 * So, we must first locate the command, and if NCR5380_select()
2764 * succeeds, then issue the abort, relocate the command and remove
2765 * it from the disconnected queue.
2766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Finn Thain710ddd02014-11-12 16:12:02 +11002768 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002769 tmp = NEXT(tmp)) {
2770 if (cmd == tmp) {
2771 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002772 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002773
Finn Thain76f13b92014-11-12 16:11:53 +11002774 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002775 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002776
Finn Thaind65e6342014-03-18 11:42:20 +11002777 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002778
2779 do_abort(instance);
2780
2781 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002782 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2783 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002784 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2785 if (cmd == tmp) {
2786 REMOVE(5, *prev, tmp, NEXT(tmp));
2787 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002788 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002789 tmp->result = DID_ABORT << 16;
2790 /* We must unlock the tag/LUN immediately here, since the
2791 * target goes to BUS FREE and doesn't send us another
2792 * message (COMMAND_COMPLETE or the like)
2793 */
2794#ifdef SUPPORT_TAGS
2795 cmd_free_tag(tmp);
2796#else
2797 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2798#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002799 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002800 local_irq_restore(flags);
2801 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002802 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002803 }
2804 }
2805 }
2806 }
2807
Finn Thaine3c3da62014-11-12 16:12:15 +11002808 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2809 * possible at all) At least, we should check if the lock could be
2810 * released after the abort, in case it is kept due to some bug.
2811 */
2812 maybe_release_dma_irq(instance);
2813 local_irq_restore(flags);
2814
Roman Zippelc28bda22007-05-01 22:32:36 +02002815 /*
2816 * Case 5 : If we reached this point, the command was not found in any of
2817 * the queues.
2818 *
2819 * We probably reached this point because of an unlikely race condition
2820 * between the command completing successfully and the abortion code,
2821 * so we won't panic, but we will notify the user in case something really
2822 * broke.
2823 */
2824
Joe Perchesad361c92009-07-06 13:05:40 -07002825 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002826
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002827 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
2830
Finn Thain3be1b3e2016-01-03 16:05:12 +11002831/**
2832 * NCR5380_bus_reset - reset the SCSI bus
2833 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002834 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002835 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Finn Thain710ddd02014-11-12 16:12:02 +11002838static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Finn Thaine3c3da62014-11-12 16:12:15 +11002840 struct Scsi_Host *instance = cmd->device->host;
2841 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002842 int i;
2843 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Finn Thain3be1b3e2016-01-03 16:05:12 +11002845 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Finn Thain3be1b3e2016-01-03 16:05:12 +11002847#if (NDEBUG & NDEBUG_ANY)
2848 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2849 NCR5380_print_status(instance);
2850#endif
2851
2852 do_reset(instance);
2853
Roman Zippelc28bda22007-05-01 22:32:36 +02002854 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002855 NCR5380_write(MODE_REG, MR_BASE);
2856 NCR5380_write(TARGET_COMMAND_REG, 0);
2857 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Roman Zippelc28bda22007-05-01 22:32:36 +02002859 /* After the reset, there are no more connected or disconnected commands
2860 * and no busy units; so clear the low-level status here to avoid
2861 * conflicts when the mid-level code tries to wake up the affected
2862 * commands!
2863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Roman Zippelc28bda22007-05-01 22:32:36 +02002865 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002866 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002867 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002868 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002869 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002870 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Roman Zippelc28bda22007-05-01 22:32:36 +02002872 hostdata->issue_queue = NULL;
2873 hostdata->connected = NULL;
2874 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002876 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002878 for (i = 0; i < 8; ++i)
2879 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002881 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002883
2884 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002885 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002887 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}