blob: 70398c61fffc832e9d0391c2fccf09fe2387a18e [file] [log] [blame]
Roman Zippelc28bda22007-05-01 22:32:36 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Roman Zippelc28bda22007-05-01 22:32:36 +02003 * to implement 5380 SCSI drivers under Linux with a non-trantor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
Roman Zippelc28bda22007-05-01 22:32:36 +02009 * Visionary Computing
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (Unix and Linux consulting and custom programming)
Roman Zippelc28bda22007-05-01 22:32:36 +020011 * drew@colorado.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * +1 (303) 666-5836
13 *
Roman Zippelc28bda22007-05-01 22:32:36 +020014 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
28 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
29 * this file, too:
30 *
31 * - Some of the debug statements were incorrect (undefined variables and the
32 * like). I fixed that.
33 *
34 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
35 * possible DMA transfer size should also happen for REAL_DMA. I added this
36 * in the #if statement.
37 *
38 * - When using real DMA, information_transfer() should return in a DATAOUT
39 * phase after starting the DMA. It has nothing more to do.
40 *
41 * - The interrupt service routine should run main after end of DMA, too (not
42 * only after RESELECTION interrupts). Additionally, it should _not_ test
43 * for more interrupts after running main, since a DMA process may have
44 * been started and interrupts are turned on now. The new int could happen
45 * inside the execution of NCR5380_intr(), leading to recursive
46 * calls.
47 *
48 * - I've added a function merge_contiguous_buffers() that tries to
49 * merge scatter-gather buffers that are located at contiguous
50 * physical addresses and can be processed with the same DMA setup.
51 * Since most scatter-gather operations work on a page (4K) of
52 * 4 buffers (1K), in more than 90% of all cases three interrupts and
53 * DMA setup actions are saved.
54 *
55 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
56 * and USLEEP, because these were messing up readability and will never be
57 * needed for Atari SCSI.
Roman Zippelc28bda22007-05-01 22:32:36 +020058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
60 * stuff), and 'main' is executed in a bottom half if awoken by an
61 * interrupt.
62 *
63 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
64 * constructs. In my eyes, this made the source rather unreadable, so I
65 * finally replaced that by the *_PRINTK() macros.
66 *
67 */
68
Finn Thaine3f463b2014-11-12 16:12:16 +110069/* Adapted for the sun3 by Sam Creasey. */
70
db9dff32005-04-03 14:53:59 -050071#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050072#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +020075#define LIST(x, y) \
76 do { \
77 printk("LINE:%d Adding %p to %p\n", \
78 __LINE__, (void*)(x), (void*)(y)); \
79 if ((x) == (y)) \
80 udelay(5); \
81 } while (0)
82#define REMOVE(w, x, y, z) \
83 do { \
84 printk("LINE:%d Removing: %p->%p %p->%p \n", \
85 __LINE__, (void*)(w), (void*)(x), \
86 (void*)(y), (void*)(z)); \
87 if ((x) == (y)) \
88 udelay(5); \
89 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#else
91#define LIST(x,y)
92#define REMOVE(w,x,y,z)
93#endif
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Design
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Roman Zippelc28bda22007-05-01 22:32:36 +020098 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200100 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * memory mapped) and drops this file in their 'C' wrapper.
102 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200103 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200105 * and commands that are currently executing. This means that an
106 * unlimited number of commands may be queued, letting
107 * more commands propagate from the higher driver levels giving higher
108 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
109 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * while a command is already executing.
111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200113 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
116 * piece of hardware that requires you to sit in a loop polling for
117 * the REQ signal as long as you are connected. Some devices are
118 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100119 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * broken devices are the exception rather than the rule and I'd rather
121 * spend my time optimizing for the normal case.
122 *
123 * Architecture :
124 *
125 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +1100126 * which is started from a workqueue for each NCR5380 host in the
127 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
128 * removing the commands from the issue queue and calling
129 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
131 * Once a nexus is established, the NCR5380_information_transfer()
132 * phase goes through the various phases as instructed by the target.
133 * if the target goes into MSG IN and sends a DISCONNECT message,
134 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +1100135 * queue, and NCR5380_main tries to find more work. If the target is
136 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * If a command has disconnected, eventually an interrupt will trigger,
139 * calling NCR5380_intr() which will in turn call NCR5380_reselect
140 * to reestablish a nexus. This will run main if necessary.
141 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200142 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * appropriate.
144 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200145 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * structures, being initialized after the command is connected
147 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
148 * Note that in violation of the standard, an implicit SAVE POINTERS operation
149 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
150 */
151
152/*
153 * Using this file :
154 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * and macros and include this file in your driver.
157 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200158 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200160 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100162 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
163 * transceivers.
164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
166 *
167 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
168 *
169 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * NCR5380_read(register) - read from the specified register
172 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200173 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100175 * NCR5380_implementation_fields - additional fields needed for this
176 * specific implementation of the NCR5380
177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200179 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200181 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * that they were able to program the controller for.
183 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200184 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * available as NCR5380_i386_dma_write_setup,
186 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
187 *
188 * NCR5380_dma_write_setup(instance, src, count) - initialize
189 * NCR5380_dma_read_setup(instance, dst, count) - initialize
190 * NCR5380_dma_residual(instance); - residual count
191 *
192 * PSEUDO functions :
193 * NCR5380_pwrite(instance, src, count)
194 * NCR5380_pread(instance, dst, count);
195 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200197 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100199 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* Macros ease life... :-) */
203#define SETUP_HOSTDATA(in) \
204 struct NCR5380_hostdata *hostdata = \
205 (struct NCR5380_hostdata *)(in)->hostdata
206#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
207
Finn Thain710ddd02014-11-12 16:12:02 +1100208#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200209#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100210#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#define HOSTNO instance->host_no
213#define H_NO(cmd) (cmd)->device->host->host_no
214
Finn Thain636b1ec2016-01-03 16:05:10 +1100215static int do_abort(struct Scsi_Host *);
216static void do_reset(struct Scsi_Host *);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#ifdef SUPPORT_TAGS
219
220/*
221 * Functions for handling tagged queuing
222 * =====================================
223 *
224 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
225 *
226 * Using consecutive numbers for the tags is no good idea in my eyes. There
227 * could be wrong re-usings if the counter (8 bit!) wraps and some early
228 * command has been preempted for a long time. My solution: a bitfield for
229 * remembering used tags.
230 *
231 * There's also the problem that each target has a certain queue size, but we
232 * cannot know it in advance :-( We just see a QUEUE_FULL status being
233 * returned. So, in this case, the driver internal queue size assumption is
234 * reduced to the number of active tags if QUEUE_FULL is returned by the
235 * target. The command is returned to the mid-level, but with status changed
236 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
237 * correctly.
238 *
239 * We're also not allowed running tagged commands as long as an untagged
240 * command is active. And REQUEST SENSE commands after a contingent allegiance
241 * condition _must_ be untagged. To keep track whether an untagged command has
242 * been issued, the host->busy array is still employed, as it is without
243 * support for tagged queuing.
244 *
245 * One could suspect that there are possible race conditions between
246 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
247 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
248 * which already guaranteed to be running at most once. It is also the only
249 * place where tags/LUNs are allocated. So no other allocation can slip
250 * between that pair, there could only happen a reselection, which can free a
251 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
252 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
253 */
254
Finn Thainca513fc2014-11-12 16:12:19 +1100255static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Roman Zippelc28bda22007-05-01 22:32:36 +0200257 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100258 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200259
Finn Thainca513fc2014-11-12 16:12:19 +1100260 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200261 return;
262
263 for (target = 0; target < 8; ++target) {
264 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100265 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200266 bitmap_zero(ta->allocated, MAX_TAGS);
267 ta->nr_allocated = 0;
268 /* At the beginning, assume the maximum queue size we could
269 * support (MAX_TAGS). This value will be decreased if the target
270 * returns QUEUE_FULL status.
271 */
272 ta->queue_size = MAX_TAGS;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277
278/* Check if we can issue a command to this LUN: First see if the LUN is marked
279 * busy by an untagged command. If the command should use tagged queuing, also
280 * check that there is a free tag and the target's queue won't overflow. This
281 * function should be called with interrupts disabled to avoid race
282 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Finn Thain710ddd02014-11-12 16:12:02 +1100285static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200287 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200288 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200290 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200291 return 1;
292 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100293 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
294 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200295 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100296 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
297 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100298 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200299 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200300 return 1;
301 }
302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305
306/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
307 * must be called before!), or reserve the LUN in 'busy' if the command is
308 * untagged.
309 */
310
Finn Thain710ddd02014-11-12 16:12:02 +1100311static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200313 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200314 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Roman Zippelc28bda22007-05-01 22:32:36 +0200316 /* If we or the target don't support tagged queuing, allocate the LUN for
317 * an untagged command.
318 */
319 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100320 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
321 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200323 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100324 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200325 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200326 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100327 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Roman Zippelc28bda22007-05-01 22:32:36 +0200329 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
330 set_bit(cmd->tag, ta->allocated);
331 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100332 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200333 "(now %d tags in use)\n",
334 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200335 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339
340/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
341 * unlock the LUN.
342 */
343
Finn Thain710ddd02014-11-12 16:12:02 +1100344static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200346 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200347 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Roman Zippelc28bda22007-05-01 22:32:36 +0200349 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200350 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100351 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200352 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200353 } else if (cmd->tag >= MAX_TAGS) {
354 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
355 H_NO(cmd), cmd->tag);
356 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100357 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200358 clear_bit(cmd->tag, ta->allocated);
359 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100360 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200361 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365
Finn Thainca513fc2014-11-12 16:12:19 +1100366static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Roman Zippelc28bda22007-05-01 22:32:36 +0200368 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100369 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Finn Thainca513fc2014-11-12 16:12:19 +1100371 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200372 return;
373
374 for (target = 0; target < 8; ++target) {
375 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100376 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200377 bitmap_zero(ta->allocated, MAX_TAGS);
378 ta->nr_allocated = 0;
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383#endif /* SUPPORT_TAGS */
384
385
386/*
Finn Thain710ddd02014-11-12 16:12:02 +1100387 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
389 * Purpose: Try to merge several scatter-gather requests into one DMA
390 * transfer. This is possible if the scatter buffers lie on
391 * physical contiguous addresses.
392 *
Finn Thain710ddd02014-11-12 16:12:02 +1100393 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300395 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
397
Finn Thain710ddd02014-11-12 16:12:02 +1100398static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Finn Thaine3f463b2014-11-12 16:12:16 +1100400#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200401 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200403 unsigned long oldlen = cmd->SCp.this_residual;
404 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#endif
406
Roman Zippelc28bda22007-05-01 22:32:36 +0200407 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
408 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200409 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100410 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200411 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200413 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200415 ++cmd->SCp.buffer;
416 --cmd->SCp.buffers_residual;
417 cmd->SCp.this_residual += cmd->SCp.buffer->length;
418 endaddr += cmd->SCp.buffer->length;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200421 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100422 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200423 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100425#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Finn Thainff50f9e2014-11-12 16:12:18 +1100428/**
429 * initialize_SCp - init the scsi pointer field
430 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100432 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434
Finn Thain710ddd02014-11-12 16:12:02 +1100435static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Roman Zippelc28bda22007-05-01 22:32:36 +0200437 /*
438 * Initialize the Scsi Pointer field so that all of the commands in the
439 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200441
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200442 if (scsi_bufflen(cmd)) {
443 cmd->SCp.buffer = scsi_sglist(cmd);
444 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200445 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200446 cmd->SCp.this_residual = cmd->SCp.buffer->length;
447 /* ++roman: Try to merge some scatter-buffers if they are at
448 * contiguous physical addresses.
449 */
450 merge_contiguous_buffers(cmd);
451 } else {
452 cmd->SCp.buffer = NULL;
453 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200454 cmd->SCp.ptr = NULL;
455 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Finn Thain636b1ec2016-01-03 16:05:10 +1100459/**
Finn Thainb32ade12016-01-03 16:05:41 +1100460 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain636b1ec2016-01-03 16:05:10 +1100461 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100462 * @reg1: 5380 register to poll
463 * @bit1: Bitmask to check
464 * @val1: Expected value
465 * @reg2: Second 5380 register to poll
466 * @bit2: Second bitmask to check
467 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100468 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100469 *
Finn Thain2f854b82016-01-03 16:05:22 +1100470 * Polls the chip in a reasonably efficient manner waiting for an
471 * event to occur. After a short quick poll we begin to yield the CPU
472 * (if possible). In irq contexts the time-out is arbitrarily limited.
473 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100474 *
Finn Thainb32ade12016-01-03 16:05:41 +1100475 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100476 */
477
Finn Thainb32ade12016-01-03 16:05:41 +1100478static int NCR5380_poll_politely2(struct Scsi_Host *instance,
479 int reg1, int bit1, int val1,
480 int reg2, int bit2, int val2, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100481{
Finn Thain2f854b82016-01-03 16:05:22 +1100482 struct NCR5380_hostdata *hostdata = shost_priv(instance);
483 unsigned long deadline = jiffies + wait;
484 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100485
Finn Thain2f854b82016-01-03 16:05:22 +1100486 /* Busy-wait for up to 10 ms */
487 n = min(10000U, jiffies_to_usecs(wait));
488 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100489 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100490 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100491 if ((NCR5380_read(reg1) & bit1) == val1)
492 return 0;
493 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain636b1ec2016-01-03 16:05:10 +1100494 return 0;
495 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100496 } while (n--);
497
498 if (irqs_disabled() || in_interrupt())
499 return -ETIMEDOUT;
500
501 /* Repeatedly sleep for 1 ms until deadline */
502 while (time_is_after_jiffies(deadline)) {
503 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100504 if ((NCR5380_read(reg1) & bit1) == val1)
505 return 0;
506 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain2f854b82016-01-03 16:05:22 +1100507 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100508 }
509
Finn Thain636b1ec2016-01-03 16:05:10 +1100510 return -ETIMEDOUT;
511}
512
Finn Thainb32ade12016-01-03 16:05:41 +1100513static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
514 int reg, int bit, int val, int wait)
515{
516 return NCR5380_poll_politely2(instance, reg, bit, val,
517 reg, bit, val, wait);
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#include <linux/delay.h>
521
522#if NDEBUG
523static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200524 unsigned char mask;
525 const char *name;
526} signals[] = {
527 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
528 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
529 { SR_SEL, "SEL" }, {0, NULL}
530}, basrs[] = {
531 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
532}, icrs[] = {
533 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
534 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
535 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
536 {0, NULL}
537}, mrs[] = {
538 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
539 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
540 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
541 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
542 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
543 {0, NULL}
544};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Finn Thainff50f9e2014-11-12 16:12:18 +1100546/**
547 * NCR5380_print - print scsi bus signals
548 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100550 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
552
Roman Zippelc28bda22007-05-01 22:32:36 +0200553static void NCR5380_print(struct Scsi_Host *instance)
554{
555 unsigned char status, data, basr, mr, icr, i;
556 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Roman Zippelc28bda22007-05-01 22:32:36 +0200558 local_irq_save(flags);
559 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
560 status = NCR5380_read(STATUS_REG);
561 mr = NCR5380_read(MODE_REG);
562 icr = NCR5380_read(INITIATOR_COMMAND_REG);
563 basr = NCR5380_read(BUS_AND_STATUS_REG);
564 local_irq_restore(flags);
565 printk("STATUS_REG: %02x ", status);
566 for (i = 0; signals[i].mask; ++i)
567 if (status & signals[i].mask)
568 printk(",%s", signals[i].name);
569 printk("\nBASR: %02x ", basr);
570 for (i = 0; basrs[i].mask; ++i)
571 if (basr & basrs[i].mask)
572 printk(",%s", basrs[i].name);
573 printk("\nICR: %02x ", icr);
574 for (i = 0; icrs[i].mask; ++i)
575 if (icr & icrs[i].mask)
576 printk(",%s", icrs[i].name);
577 printk("\nMODE: %02x ", mr);
578 for (i = 0; mrs[i].mask; ++i)
579 if (mr & mrs[i].mask)
580 printk(",%s", mrs[i].name);
581 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200585 unsigned char value;
586 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200588 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
589 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
590 {PHASE_UNKNOWN, "UNKNOWN"}
591};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Finn Thainff50f9e2014-11-12 16:12:18 +1100593/**
594 * NCR5380_print_phase - show SCSI phase
595 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100597 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100599 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
602static void NCR5380_print_phase(struct Scsi_Host *instance)
603{
Roman Zippelc28bda22007-05-01 22:32:36 +0200604 unsigned char status;
605 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Roman Zippelc28bda22007-05-01 22:32:36 +0200607 status = NCR5380_read(STATUS_REG);
608 if (!(status & SR_REQ))
609 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
610 else {
611 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
612 (phases[i].value != (status & PHASE_MASK)); ++i)
613 ;
614 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#endif
619
Finn Thain8c325132014-11-12 16:11:58 +1100620/**
621 * NCR58380_info - report driver and host information
622 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *
Finn Thain8c325132014-11-12 16:11:58 +1100624 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
Finn Thain8c325132014-11-12 16:11:58 +1100626 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628
Finn Thain8c325132014-11-12 16:11:58 +1100629static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Finn Thain8c325132014-11-12 16:11:58 +1100631 struct NCR5380_hostdata *hostdata = shost_priv(instance);
632
633 return hostdata->info;
634}
635
636static void prepare_info(struct Scsi_Host *instance)
637{
638 struct NCR5380_hostdata *hostdata = shost_priv(instance);
639
640 snprintf(hostdata->info, sizeof(hostdata->info),
641 "%s, io_port 0x%lx, n_io_port %d, "
642 "base 0x%lx, irq %d, "
643 "can_queue %d, cmd_per_lun %d, "
644 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100645 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100646 "options { %s} ",
647 instance->hostt->name, instance->io_port, instance->n_io_port,
648 instance->base, instance->irq,
649 instance->can_queue, instance->cmd_per_lun,
650 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100651 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100652 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100653#ifdef DIFFERENTIAL
654 "DIFFERENTIAL "
655#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100657 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#endif
659#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100660 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
662#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100663 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#endif
Finn Thain8c325132014-11-12 16:11:58 +1100665 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Finn Thainff50f9e2014-11-12 16:12:18 +1100668/**
669 * NCR5380_print_status - dump controller info
670 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100672 * Print commands in the various queues, called from NCR5380_abort
673 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675
Finn Thain710ddd02014-11-12 16:12:02 +1100676static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400677{
678 int i, s;
679 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200680 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400681 H_NO(cmd), cmd->device->id, cmd->device->lun);
682 printk(KERN_CONT " command = ");
683 command = cmd->cmnd;
684 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
685 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
686 printk(KERN_CONT " %02x", command[i]);
687 printk("\n");
688}
689
Finn Thain3be1b3e2016-01-03 16:05:12 +1100690static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Al Virod89537e2013-03-31 13:24:44 -0400692 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100693 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400694 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Finn Thain8ad3a592014-03-18 11:42:19 +1100696 NCR5380_dprint(NDEBUG_ANY, instance);
697 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Roman Zippelc28bda22007-05-01 22:32:36 +0200699 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Roman Zippelc28bda22007-05-01 22:32:36 +0200701 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +0200702 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400703 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200704 else
Finn Thain710ddd02014-11-12 16:12:02 +1100705 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400706 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100707 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400708 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Al Virod89537e2013-03-31 13:24:44 -0400710 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100711 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400712 ptr = NEXT(ptr))
713 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200714
715 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400716 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Finn Thain710ddd02014-11-12 16:12:02 +1100719static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Roman Zippelc28bda22007-05-01 22:32:36 +0200721 int i, s;
722 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200723 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200724 H_NO(cmd), cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100725 seq_puts(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200726 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400727 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200728 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400729 seq_printf(m, " %02x", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100730 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Finn Thain4d3d2a52014-11-12 16:11:52 +1100733static int __maybe_unused NCR5380_show_info(struct seq_file *m,
734 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400735{
736 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100737 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400738 unsigned long flags;
739
740 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
741
Al Virod89537e2013-03-31 13:24:44 -0400742 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400743 if (!hostdata->connected)
744 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
745 else
Finn Thain710ddd02014-11-12 16:12:02 +1100746 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400747 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100748 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400749 show_Scsi_Cmnd(ptr, m);
750
751 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100752 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400753 ptr = NEXT(ptr))
754 show_Scsi_Cmnd(ptr, m);
755
756 local_irq_restore(flags);
757 return 0;
758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Finn Thainff50f9e2014-11-12 16:12:18 +1100760/**
761 * NCR5380_init - initialise an NCR5380
762 * @instance: adapter to configure
763 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100765 * Initializes *instance and corresponding 5380 chip,
766 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
768 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100769 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200770 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100771 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
773
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100774static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Roman Zippelc28bda22007-05-01 22:32:36 +0200776 int i;
777 SETUP_HOSTDATA(instance);
Finn Thain2f854b82016-01-03 16:05:22 +1100778 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Finn Thaina53a21e2014-11-12 16:12:21 +1100780 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200781 hostdata->id_mask = 1 << instance->this_id;
782 hostdata->id_higher_mask = 0;
783 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
784 if (i > hostdata->id_mask)
785 hostdata->id_higher_mask |= i;
786 for (i = 0; i < 8; ++i)
787 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100789 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif
791#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200792 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200794 hostdata->connected = NULL;
795 hostdata->issue_queue = NULL;
796 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100797 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Finn Thaina53a21e2014-11-12 16:12:21 +1100799 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100800 hostdata->work_q = alloc_workqueue("ncr5380_%d",
801 WQ_UNBOUND | WQ_MEM_RECLAIM,
802 1, instance->host_no);
803 if (!hostdata->work_q)
804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Finn Thain8c325132014-11-12 16:11:58 +1100806 prepare_info(instance);
807
Roman Zippelc28bda22007-05-01 22:32:36 +0200808 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
809 NCR5380_write(MODE_REG, MR_BASE);
810 NCR5380_write(TARGET_COMMAND_REG, 0);
811 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Finn Thain2f854b82016-01-03 16:05:22 +1100813 /* Calibrate register polling loop */
814 i = 0;
815 deadline = jiffies + 1;
816 do {
817 cpu_relax();
818 } while (time_is_after_jiffies(deadline));
819 deadline += msecs_to_jiffies(256);
820 do {
821 NCR5380_read(STATUS_REG);
822 ++i;
823 cpu_relax();
824 } while (time_is_after_jiffies(deadline));
825 hostdata->accesses_per_ms = i / 256;
826
Roman Zippelc28bda22007-05-01 22:32:36 +0200827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Finn Thainff50f9e2014-11-12 16:12:18 +1100830/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100831 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
832 * @instance: adapter to check
833 *
834 * If the system crashed, it may have crashed with a connected target and
835 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
836 * currently established nexus, which we know nothing about. Failing that
837 * do a bus reset.
838 *
839 * Note that a bus reset will cause the chip to assert IRQ.
840 *
841 * Returns 0 if successful, otherwise -ENXIO.
842 */
843
844static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
845{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100846 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100847 int pass;
848
849 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
850 switch (pass) {
851 case 1:
852 case 3:
853 case 5:
854 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
855 NCR5380_poll_politely(instance,
856 STATUS_REG, SR_BSY, 0, 5 * HZ);
857 break;
858 case 2:
859 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
860 do_abort(instance);
861 break;
862 case 4:
863 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
864 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100865 /* Wait after a reset; the SCSI standard calls for
866 * 250ms, we wait 500ms to be on the safe side.
867 * But some Toshiba CD-ROMs need ten times that.
868 */
869 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
870 msleep(2500);
871 else
872 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100873 break;
874 case 6:
875 shost_printk(KERN_ERR, instance, "bus locked solid\n");
876 return -ENXIO;
877 }
878 }
879 return 0;
880}
881
882/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100883 * NCR5380_exit - remove an NCR5380
884 * @instance: adapter to remove
885 *
886 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
887 */
888
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200889static void NCR5380_exit(struct Scsi_Host *instance)
890{
Finn Thaina53a21e2014-11-12 16:12:21 +1100891 struct NCR5380_hostdata *hostdata = shost_priv(instance);
892
893 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100894 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200895}
896
Finn Thainff50f9e2014-11-12 16:12:18 +1100897/**
898 * NCR5380_queue_command - queue a command
899 * @instance: the relevant SCSI adapter
900 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *
Finn Thain1bb40582016-01-03 16:05:29 +1100902 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100903 * twiddling done to the host specific fields of cmd. If the
904 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
906
Finn Thain16b29e72014-11-12 16:12:08 +1100907static int NCR5380_queue_command(struct Scsi_Host *instance,
908 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Finn Thain16b29e72014-11-12 16:12:08 +1100910 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100911 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200912 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200915 switch (cmd->cmnd[0]) {
916 case WRITE_6:
917 case WRITE_10:
918 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
919 H_NO(cmd));
920 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100921 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200922 return 0;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
925
Roman Zippelc28bda22007-05-01 22:32:36 +0200926 /*
927 * We use the host_scribble field as a pointer to the next command
928 * in a queue
929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Roman Zippel3130d902007-05-01 22:32:37 +0200931 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200932 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Roman Zippelc28bda22007-05-01 22:32:36 +0200934 /*
935 * Insert the cmd into the issue queue. Note that REQUEST SENSE
936 * commands are added to the head of the queue since any command will
937 * clear the contingent allegiance condition that exists and the
938 * sense data is only guaranteed to be valid while the condition exists.
939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Roman Zippelc28bda22007-05-01 22:32:36 +0200941 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
942 * Otherwise a running NCR5380_main may steal the lock.
943 * Lock before actually inserting due to fairness reasons explained in
944 * atari_scsi.c. If we insert first, then it's impossible for this driver
945 * to release the lock.
946 * Stop timer for this command while waiting for the lock, or timeouts
947 * may happen (and they really do), and it's no good if the command doesn't
948 * appear in any of the queues.
949 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
950 * because also a timer int can trigger an abort or reset, which would
951 * alter queues and touch the lock.
952 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100953 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100954 return SCSI_MLQUEUE_HOST_BUSY;
955
956 local_irq_save(flags);
957
Finn Thainff50f9e2014-11-12 16:12:18 +1100958 /*
959 * Insert the cmd into the issue queue. Note that REQUEST SENSE
960 * commands are added to the head of the queue since any command will
961 * clear the contingent allegiance condition that exists and the
962 * sense data is only guaranteed to be valid while the condition exists.
963 */
964
Roman Zippelc28bda22007-05-01 22:32:36 +0200965 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
966 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200967 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200968 hostdata->issue_queue = cmd;
969 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100970 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200971 NEXT(tmp); tmp = NEXT(tmp))
972 ;
973 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200974 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200975 }
976 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Finn Thaind65e6342014-03-18 11:42:20 +1100978 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200979 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Finn Thain010e89d2016-01-03 16:05:38 +1100981 /* Kick off command processing */
982 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Finn Thaine3c3da62014-11-12 16:12:15 +1100986static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
987{
988 struct NCR5380_hostdata *hostdata = shost_priv(instance);
989
990 /* Caller does the locking needed to set & test these data atomically */
991 if (!hostdata->disconnected_queue &&
992 !hostdata->issue_queue &&
993 !hostdata->connected &&
994 !hostdata->retain_dma_intr)
995 NCR5380_release_dma_irq(instance);
996}
997
Finn Thainff50f9e2014-11-12 16:12:18 +1100998/**
999 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001001 * NCR5380_main is a coroutine that runs as long as more work can
1002 * be done on the NCR5380 host adapters in a system. Both
1003 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1004 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +02001005 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001006 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +02001007 */
1008
Geert Uytterhoevenb312b382007-05-01 22:32:48 +02001009static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Finn Thaina53a21e2014-11-12 16:12:21 +11001011 struct NCR5380_hostdata *hostdata =
1012 container_of(work, struct NCR5380_hostdata, main_task);
1013 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +11001014 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02001015 int done;
1016 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Roman Zippelc28bda22007-05-01 22:32:36 +02001018 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001019 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1020 * because also a timer int can trigger an abort or reset, which can
1021 * alter queues and touch the Falcon lock.
1022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Roman Zippelc28bda22007-05-01 22:32:36 +02001024 local_save_flags(flags);
1025 do {
1026 local_irq_disable(); /* Freeze request queues */
1027 done = 1;
1028
1029 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +11001030 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 /*
1032 * Search through the issue_queue for a command destined
1033 * for a target that's not busy.
1034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +11001036 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001037 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1038 ;
1039 /*printk("%p ", tmp);*/
1040 if ((tmp == prev) && tmp)
1041 printk(" LOOP\n");
1042 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#endif
Finn Thain710ddd02014-11-12 16:12:02 +11001044 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +02001045 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001046 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Finn Thaine3f463b2014-11-12 16:12:16 +11001048 dprintk(NDEBUG_LISTS,
1049 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1050 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
1051 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001052 /* When we find one, remove it from the issue queue. */
1053 /* ++guenther: possible race with Falcon locking */
1054 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001056 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001058 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001059#endif
1060 ) {
1061 /* ++guenther: just to be sure, this must be atomic */
1062 local_irq_disable();
1063 if (prev) {
1064 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001065 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001066 } else {
1067 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1068 hostdata->issue_queue = NEXT(tmp);
1069 }
Roman Zippel3130d902007-05-01 22:32:37 +02001070 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001071 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001072
1073 /* reenable interrupts after finding one */
1074 local_irq_restore(flags);
1075
1076 /*
1077 * Attempt to establish an I_T_L nexus here.
1078 * On success, instance->hostdata->connected is set.
1079 * On failure, we must add the command back to the
1080 * issue queue so we can keep trying.
1081 */
Finn Thaind65e6342014-03-18 11:42:20 +11001082 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001083 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001084 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001085 /*
1086 * REQUEST SENSE commands are issued without tagged
1087 * queueing, even on SCSI-II devices because the
1088 * contingent allegiance condition exists for the
1089 * entire unit.
1090 */
1091 /* ++roman: ...and the standard also requires that
1092 * REQUEST SENSE command are untagged.
1093 */
1094
1095#ifdef SUPPORT_TAGS
1096 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1097#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001098 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001099 /* OK or bad target */
Finn Thaine3c3da62014-11-12 16:12:15 +11001100 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001101 hostdata->retain_dma_intr--;
Finn Thaine3c3da62014-11-12 16:12:15 +11001102 maybe_release_dma_irq(instance);
1103 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001104 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001105 /* Need to retry */
Roman Zippelc28bda22007-05-01 22:32:36 +02001106 local_irq_disable();
1107 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001108 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001109 hostdata->issue_queue = tmp;
1110#ifdef SUPPORT_TAGS
1111 cmd_free_tag(tmp);
1112#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001113 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001114 local_irq_restore(flags);
Finn Thain1d3db592016-01-03 16:05:24 +11001115 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001116 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001117 "returned to issue_queue\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001118 }
Finn Thain1f1b0c72016-01-03 16:05:17 +11001119 if (hostdata->connected)
1120 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001121 } /* if target/lun/target queue is not busy */
1122 } /* for issue_queue */
1123 } /* if (!hostdata->connected) */
1124
1125 if (hostdata->connected
1126#ifdef REAL_DMA
1127 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001131 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001132 HOSTNO);
1133 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001134 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001135 done = 0;
1136 }
1137 } while (!done);
Roman Zippelc28bda22007-05-01 22:32:36 +02001138 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141
1142#ifdef REAL_DMA
1143/*
1144 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1145 *
1146 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001147 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
1149 * Inputs : instance - this instance of the NCR5380.
1150 *
1151 */
1152
Roman Zippelc28bda22007-05-01 22:32:36 +02001153static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Roman Zippelc28bda22007-05-01 22:32:36 +02001155 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001156 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001157 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001158 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001159 int saved_data = 0, overrun = 0;
1160 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Finn Thainef1081c2014-11-12 16:12:14 +11001162 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001163 p = hostdata->connected->SCp.phase;
1164 if (p & SR_IO) {
1165 udelay(10);
1166 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1167 (BASR_PHASE_MATCH|BASR_ACK)) ==
1168 (BASR_PHASE_MATCH|BASR_ACK)) {
1169 saved_data = NCR5380_read(INPUT_DATA_REG);
1170 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001171 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001172 }
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001175
Finn Thaine3f463b2014-11-12 16:12:16 +11001176#if defined(CONFIG_SUN3)
1177 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1178 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1179 instance->host_no);
1180 BUG();
1181 }
1182
1183 /* make sure we're not stuck in a data phase */
1184 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1185 (BASR_PHASE_MATCH | BASR_ACK)) {
1186 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1187 NCR5380_read(BUS_AND_STATUS_REG));
1188 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1189 instance->host_no);
1190 BUG();
1191 }
1192#endif
1193
Roman Zippelc28bda22007-05-01 22:32:36 +02001194 NCR5380_write(MODE_REG, MR_BASE);
1195 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001196 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001197
Finn Thain01bd9082014-11-12 16:12:23 +11001198 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001199 hostdata->dma_len = 0;
1200
1201 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1202 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001203 *data += transferred;
1204 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001205
Finn Thainef1081c2014-11-12 16:12:14 +11001206 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001207 int cnt, toPIO;
1208
Roman Zippelc28bda22007-05-01 22:32:36 +02001209 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001210 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001211 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001212 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001213 *(*data)++ = saved_data;
1214 (*count)--;
1215 cnt--;
1216 toPIO--;
1217 }
Finn Thaind65e6342014-03-18 11:42:20 +11001218 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001219 NCR5380_transfer_pio(instance, &p, &cnt, data);
1220 *count -= toPIO - cnt;
1221 }
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224#endif /* REAL_DMA */
1225
1226
Finn Thainff50f9e2014-11-12 16:12:18 +11001227/**
1228 * NCR5380_intr - generic NCR5380 irq handler
1229 * @irq: interrupt number
1230 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001231 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001232 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1233 * from the disconnected queue, and restarting NCR5380_main()
1234 * as required.
Finn Thaincd400822016-01-03 16:05:40 +11001235 *
1236 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1237 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1238 * Three of these six conditions are latched in the Bus and Status Register:
1239 * - End of DMA (cleared by ending DMA Mode)
1240 * - Parity error (cleared by reading RPIR)
1241 * - Loss of BSY (cleared by reading RPIR)
1242 * Two conditions have flag bits that are not latched:
1243 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1244 * - Bus reset (non-maskable)
1245 * The remaining condition has no flag bit at all:
1246 * - Selection/reselection
1247 *
1248 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1249 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1250 * claimed that "the design of the [DP8490] interrupt logic ensures
1251 * interrupts will not be lost (they can be on the DP5380)."
1252 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1253 *
1254 * Checking for bus reset by reading RST is futile because of interrupt
1255 * latency, but a bus reset will reset chip logic. Checking for parity error
1256 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1257 * condition will clear DMA Mode. We can tell when this occurs because the
1258 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260
Roman Zippelc28bda22007-05-01 22:32:36 +02001261static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Finn Thaina53a21e2014-11-12 16:12:21 +11001263 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001264 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001265 int handled = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001266 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 basr = NCR5380_read(BUS_AND_STATUS_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001269 if (basr & BASR_IRQ) {
Finn Thaincd400822016-01-03 16:05:40 +11001270 unsigned char mr = NCR5380_read(MODE_REG);
1271 unsigned char sr = NCR5380_read(STATUS_REG);
1272
1273 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1274 HOSTNO, irq, basr, sr, mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001277 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1278 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1279 * We ack IRQ after clearing Mode Register. Workarounds
1280 * for End of DMA errata need to happen in DMA Mode.
Roman Zippelc28bda22007-05-01 22:32:36 +02001281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Finn Thaincd400822016-01-03 16:05:40 +11001283 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001284
Finn Thaincd400822016-01-03 16:05:40 +11001285 if (hostdata->connected) {
1286 NCR5380_dma_complete(instance);
1287 queue_work(hostdata->work_q, &hostdata->main_task);
1288 } else {
1289 NCR5380_write(MODE_REG, MR_BASE);
1290 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001291 }
Finn Thaincd400822016-01-03 16:05:40 +11001292 } else
1293#endif /* REAL_DMA */
1294 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1295 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1296 /* Probably reselected */
1297 NCR5380_write(SELECT_ENABLE_REG, 0);
1298 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1299
1300 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1301 HOSTNO);
1302
1303 if (!hostdata->connected) {
1304 NCR5380_reselect(instance);
1305 queue_work(hostdata->work_q, &hostdata->main_task);
1306 }
1307 if (!hostdata->connected)
1308 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1309 } else {
1310 /* Probably Bus Reset */
1311 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1312
1313 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", HOSTNO);
1314#ifdef SUN3_SCSI_VME
1315 dregs->csr |= CSR_DMA_ENABLE;
1316#endif
1317 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 handled = 1;
Finn Thaincd400822016-01-03 16:05:40 +11001319 } else {
1320 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine3f463b2014-11-12 16:12:16 +11001321#ifdef SUN3_SCSI_VME
1322 dregs->csr |= CSR_DMA_ENABLE;
1323#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001324 }
1325
Roman Zippelc28bda22007-05-01 22:32:36 +02001326 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Roman Zippelc28bda22007-05-01 22:32:36 +02001329/*
Finn Thain710ddd02014-11-12 16:12:02 +11001330 * Function : int NCR5380_select(struct Scsi_Host *instance,
1331 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
1333 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001334 * including ARBITRATION, SELECTION, and initial message out for
1335 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001337 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001338 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *
Finn Thain63238762016-01-03 16:05:15 +11001340 * Returns : -1 if selection failed but should be retried.
1341 * 0 if selection failed and should not be retried.
1342 * 0 if selection succeeded completely (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001343 *
1344 * Side effects :
1345 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * with registers as they should have been on entry - ie
1347 * SELECT_ENABLE will be set appropriately, the NCR5380
1348 * will cease to drive any SCSI bus signals.
1349 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001350 * If successful : I_T_L or I_T_L_Q nexus will be established,
1351 * instance->connected will be set to cmd.
1352 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001354 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * cmd->result host byte set to DID_BAD_TARGET.
1356 */
1357
Finn Thain710ddd02014-11-12 16:12:02 +11001358static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Roman Zippelc28bda22007-05-01 22:32:36 +02001360 SETUP_HOSTDATA(instance);
1361 unsigned char tmp[3], phase;
1362 unsigned char *data;
1363 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001364 int err;
Roman Zippelc28bda22007-05-01 22:32:36 +02001365 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Finn Thain8ad3a592014-03-18 11:42:19 +11001367 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001368 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001369 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Roman Zippelc28bda22007-05-01 22:32:36 +02001371 /*
1372 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1373 * data bus during SELECTION.
1374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Roman Zippelc28bda22007-05-01 22:32:36 +02001376 local_irq_save(flags);
1377 if (hostdata->connected) {
1378 local_irq_restore(flags);
1379 return -1;
1380 }
1381 NCR5380_write(TARGET_COMMAND_REG, 0);
1382
1383 /*
1384 * Start arbitration.
1385 */
1386
1387 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1388 NCR5380_write(MODE_REG, MR_ARBITRATE);
1389
Finn Thain55500d92016-01-03 16:05:35 +11001390 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1391 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Finn Thain55500d92016-01-03 16:05:35 +11001394 local_irq_restore(flags);
Finn Thainb32ade12016-01-03 16:05:41 +11001395 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1396 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1397 ICR_ARBITRATION_PROGRESS, HZ);
1398 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1399 /* Reselection interrupt */
1400 return -1;
1401 }
1402 if (err < 0) {
1403 NCR5380_write(MODE_REG, MR_BASE);
1404 shost_printk(KERN_ERR, instance,
1405 "select: arbitration timeout\n");
1406 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001407 }
1408
1409 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001410 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 /* Check for lost arbitration */
1413 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1414 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1415 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1416 hostdata->connected) {
1417 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001418 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001419 HOSTNO);
1420 return -1;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Finn Thaincf13b082016-01-03 16:05:18 +11001423 /* After/during arbitration, BSY should be asserted.
1424 * IBM DPES-31080 Version S31Q works now
1425 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1426 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001427 NCR5380_write(INITIATOR_COMMAND_REG,
1428 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Roman Zippelc28bda22007-05-01 22:32:36 +02001430 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1431 hostdata->connected) {
1432 NCR5380_write(MODE_REG, MR_BASE);
1433 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001434 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001435 HOSTNO);
1436 return -1;
1437 }
1438
1439 /*
1440 * Again, bus clear + bus settle time is 1.2us, however, this is
1441 * a minimum so we'll udelay ceil(1.2)
1442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Finn Thain9c3f0e22016-01-03 16:05:11 +11001444 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1445 udelay(15);
1446 else
1447 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001448
1449 if (hostdata->connected) {
1450 NCR5380_write(MODE_REG, MR_BASE);
1451 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1452 return -1;
1453 }
1454
Finn Thaind65e6342014-03-18 11:42:20 +11001455 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001456
1457 /*
1458 * Now that we have won arbitration, start Selection process, asserting
1459 * the host and target ID's on the SCSI bus.
1460 */
1461
1462 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1463
1464 /*
1465 * Raise ATN while SEL is true before BSY goes false from arbitration,
1466 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1467 * phase immediately after selection.
1468 */
1469
1470 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1471 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Roman Zippelc28bda22007-05-01 22:32:36 +02001474 /*
1475 * Reselect interrupts must be turned off prior to the dropping of BSY,
1476 * otherwise we will trigger an interrupt.
1477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Roman Zippelc28bda22007-05-01 22:32:36 +02001479 if (hostdata->connected) {
1480 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1481 return -1;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Roman Zippelc28bda22007-05-01 22:32:36 +02001484 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Roman Zippelc28bda22007-05-01 22:32:36 +02001486 /*
1487 * The initiator shall then wait at least two deskew delays and release
1488 * the BSY signal.
1489 */
1490 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Roman Zippelc28bda22007-05-01 22:32:36 +02001492 /* Reset BSY */
1493 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1494 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Roman Zippelc28bda22007-05-01 22:32:36 +02001496 /*
1497 * Something weird happens when we cease to drive BSY - looks
1498 * like the board/chip is letting us do another read before the
1499 * appropriate propagation delay has expired, and we're confusing
1500 * a BSY signal from ourselves as the target's response to SELECTION.
1501 *
1502 * A small delay (the 'C++' frontend breaks the pipeline with an
1503 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1504 * tighter 'C' code breaks and requires this) solves the problem -
1505 * the 1 us delay is arbitrary, and only used because this delay will
1506 * be the same on other platforms and since it works here, it should
1507 * work there.
1508 *
1509 * wingel suggests that this could be due to failing to wait
1510 * one deskew delay.
1511 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Roman Zippelc28bda22007-05-01 22:32:36 +02001513 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Finn Thaind65e6342014-03-18 11:42:20 +11001515 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Roman Zippelc28bda22007-05-01 22:32:36 +02001517 /*
1518 * The SCSI specification calls for a 250 ms timeout for the actual
1519 * selection.
1520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Finn Thainae753a32016-01-03 16:05:23 +11001522 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1523 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Roman Zippelc28bda22007-05-01 22:32:36 +02001525 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1526 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1527 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001528 if (!hostdata->connected)
1529 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02001530 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1531 HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001532 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001534
Finn Thainae753a32016-01-03 16:05:23 +11001535 if (err < 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001536 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001537 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001538#ifdef SUPPORT_TAGS
1539 cmd_free_tag(cmd);
1540#endif
1541 cmd->scsi_done(cmd);
1542 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001543 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001544 return 0;
1545 }
1546
Roman Zippelc28bda22007-05-01 22:32:36 +02001547 /*
Finn Thainae753a32016-01-03 16:05:23 +11001548 * No less than two deskew delays after the initiator detects the
1549 * BSY signal is true, it shall release the SEL signal and may
1550 * change the DATA BUS. -wingel
1551 */
1552
1553 udelay(1);
1554
1555 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1556
1557 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001558 * Since we followed the SCSI spec, and raised ATN while SEL
1559 * was true but before BSY was false during selection, the information
1560 * transfer phase should be a MESSAGE OUT phase so that we can send the
1561 * IDENTIFY message.
1562 *
1563 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1564 * message (2 bytes) with a tag ID that we increment with every command
1565 * until it wraps back to 0.
1566 *
1567 * XXX - it turns out that there are some broken SCSI-II devices,
1568 * which claim to support tagged queuing but fail when more than
1569 * some number of commands are issued at once.
1570 */
1571
1572 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001573
1574 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1575 if (err < 0) {
1576 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1577 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1578 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1579 return -1;
1580 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001581
Finn Thaind65e6342014-03-18 11:42:20 +11001582 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001583 HOSTNO, cmd->device->id);
1584 tmp[0] = IDENTIFY(1, cmd->device->lun);
1585
1586#ifdef SUPPORT_TAGS
1587 if (cmd->tag != TAG_NONE) {
1588 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1589 tmp[2] = cmd->tag;
1590 len = 3;
1591 } else
1592 len = 1;
1593#else
1594 len = 1;
1595 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596#endif /* SUPPORT_TAGS */
1597
Roman Zippelc28bda22007-05-01 22:32:36 +02001598 /* Send message(s) */
1599 data = tmp;
1600 phase = PHASE_MSGOUT;
1601 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001602 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001603 /* XXX need to handle errors here */
1604 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001606 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1607#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001608#ifdef SUN3_SCSI_VME
1609 dregs->csr |= CSR_INTR;
1610#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Roman Zippelc28bda22007-05-01 22:32:36 +02001612 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Roman Zippelc28bda22007-05-01 22:32:36 +02001614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Roman Zippelc28bda22007-05-01 22:32:36 +02001617/*
1618 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * unsigned char *phase, int *count, unsigned char **data)
1620 *
1621 * Purpose : transfers data in given phase using polled I/O
1622 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001623 * Inputs : instance - instance of driver, *phase - pointer to
1624 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001628 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 * is in same phase.
1630 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001631 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 *
1633 * XXX Note : handling for bus free may be useful.
1634 */
1635
1636/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001637 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * IS 100% reliable, and for the actual data transfer where speed
1639 * counts, we will always do a pseudo DMA or DMA transfer.
1640 */
1641
Roman Zippelc28bda22007-05-01 22:32:36 +02001642static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1643 unsigned char *phase, int *count,
1644 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Roman Zippelc28bda22007-05-01 22:32:36 +02001646 register unsigned char p = *phase, tmp;
1647 register int c = *count;
1648 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Roman Zippelc28bda22007-05-01 22:32:36 +02001650 /*
1651 * The NCR5380 chip will only drive the SCSI bus when the
1652 * phase specified in the appropriate bits of the TARGET COMMAND
1653 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
1655
Roman Zippelc28bda22007-05-01 22:32:36 +02001656 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Roman Zippelc28bda22007-05-01 22:32:36 +02001658 do {
1659 /*
1660 * Wait for assertion of REQ, after which the phase bits will be
1661 * valid
1662 */
Finn Thain686f3992016-01-03 16:05:26 +11001663
1664 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Finn Thaind65e6342014-03-18 11:42:20 +11001667 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Roman Zippelc28bda22007-05-01 22:32:36 +02001669 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001670 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001671 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001672 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001673 break;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Roman Zippelc28bda22007-05-01 22:32:36 +02001676 /* Do actual transfer from SCSI bus to / from memory */
1677 if (!(p & SR_IO))
1678 NCR5380_write(OUTPUT_DATA_REG, *d);
1679 else
1680 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Roman Zippelc28bda22007-05-01 22:32:36 +02001682 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Roman Zippelc28bda22007-05-01 22:32:36 +02001684 /*
1685 * The SCSI standard suggests that in MSGOUT phase, the initiator
1686 * should drop ATN on the last byte of the message phase
1687 * after REQ has been asserted for the handshake but before
1688 * the initiator raises ACK.
1689 */
1690
1691 if (!(p & SR_IO)) {
1692 if (!((p & SR_MSG) && c > 1)) {
1693 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001694 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001695 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1696 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1697 } else {
1698 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1699 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001700 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001701 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1702 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1703 }
1704 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001705 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1707 }
1708
Finn Thaina2edc4a2016-01-03 16:05:27 +11001709 if (NCR5380_poll_politely(instance,
1710 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1711 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001712
Finn Thaind65e6342014-03-18 11:42:20 +11001713 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001714
1715 /*
1716 * We have several special cases to consider during REQ/ACK handshaking :
1717 * 1. We were in MSGOUT phase, and we are on the last byte of the
1718 * message. ATN must be dropped as ACK is dropped.
1719 *
1720 * 2. We are in a MSGIN phase, and we are on the last byte of the
1721 * message. We must exit with ACK asserted, so that the calling
1722 * code may raise ATN before dropping ACK to reject the message.
1723 *
1724 * 3. ACK and ATN are clear and the target may proceed as normal.
1725 */
1726 if (!(p == PHASE_MSGIN && c == 1)) {
1727 if (p == PHASE_MSGOUT && c > 1)
1728 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1729 else
1730 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1731 }
1732 } while (--c);
1733
Finn Thaind65e6342014-03-18 11:42:20 +11001734 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001735
1736 *count = c;
1737 *data = d;
1738 tmp = NCR5380_read(STATUS_REG);
1739 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001740 * asserted or if ACK hasn't been released yet. The latter applies if
1741 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001742 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001743 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001744 *phase = tmp & PHASE_MASK;
1745 else
1746 *phase = PHASE_UNKNOWN;
1747
1748 if (!c || (*phase == p))
1749 return 0;
1750 else
1751 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Finn Thain636b1ec2016-01-03 16:05:10 +11001754/**
1755 * do_reset - issue a reset command
1756 * @instance: adapter to reset
1757 *
1758 * Issue a reset sequence to the NCR5380 and try and get the bus
1759 * back into sane shape.
1760 *
1761 * This clears the reset interrupt flag because there may be no handler for
1762 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1763 * been installed. And when in EH we may have released the ST DMA interrupt.
1764 */
1765
1766static void do_reset(struct Scsi_Host *instance)
1767{
1768 unsigned long flags;
1769
1770 local_irq_save(flags);
1771 NCR5380_write(TARGET_COMMAND_REG,
1772 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1773 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1774 udelay(50);
1775 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1776 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1777 local_irq_restore(flags);
1778}
1779
Finn Thain80d3eb62016-01-03 16:05:34 +11001780/**
1781 * do_abort - abort the currently established nexus by going to
1782 * MESSAGE OUT phase and sending an ABORT message.
1783 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001784 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001785 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
1787
Finn Thaina53a21e2014-11-12 16:12:21 +11001788static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Roman Zippelc28bda22007-05-01 22:32:36 +02001790 unsigned char tmp, *msgptr, phase;
1791 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001792 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Roman Zippelc28bda22007-05-01 22:32:36 +02001794 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Roman Zippelc28bda22007-05-01 22:32:36 +02001797 /*
1798 * Wait for the target to indicate a valid phase by asserting
1799 * REQ. Once this happens, we'll have either a MSGOUT phase
1800 * and can immediately send the ABORT message, or we'll have some
1801 * other phase and will have to source/sink data.
1802 *
1803 * We really don't care what value was on the bus or what value
1804 * the target sees, so we just handshake.
1805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Finn Thain80d3eb62016-01-03 16:05:34 +11001807 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1808 if (rc < 0)
1809 goto timeout;
1810
1811 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001812
1813 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1814
Finn Thain80d3eb62016-01-03 16:05:34 +11001815 if (tmp != PHASE_MSGOUT) {
1816 NCR5380_write(INITIATOR_COMMAND_REG,
1817 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1818 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1819 if (rc < 0)
1820 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001821 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1822 }
1823
1824 tmp = ABORT;
1825 msgptr = &tmp;
1826 len = 1;
1827 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001828 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001829
1830 /*
1831 * If we got here, and the command completed successfully,
1832 * we're about to go into bus free state.
1833 */
1834
1835 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001836
1837timeout:
1838 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1839 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001843/*
1844 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 * unsigned char *phase, int *count, unsigned char **data)
1846 *
1847 * Purpose : transfers data in given phase using either real
1848 * or pseudo DMA.
1849 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001850 * Inputs : instance - instance of driver, *phase - pointer to
1851 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001853 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001855 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * is in same phase.
1857 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001858 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 *
1860 */
1861
1862
Roman Zippelc28bda22007-05-01 22:32:36 +02001863static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1864 unsigned char *phase, int *count,
1865 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Roman Zippelc28bda22007-05-01 22:32:36 +02001867 SETUP_HOSTDATA(instance);
1868 register int c = *count;
1869 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001870 unsigned long flags;
1871
1872#if defined(CONFIG_SUN3)
1873 /* sanity check */
1874 if (!sun3_dma_setup_done) {
1875 pr_err("scsi%d: transfer_dma without setup!\n",
1876 instance->host_no);
1877 BUG();
1878 }
1879 hostdata->dma_len = c;
1880
1881 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1882 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1883 c, (p & SR_IO) ? "to" : "from", *data);
1884
1885 /* netbsd turns off ints here, why not be safe and do it too */
1886 local_irq_save(flags);
1887
1888 /* send start chain */
1889 sun3scsi_dma_start(c, *data);
1890
Finn Thaincd400822016-01-03 16:05:40 +11001891 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1892 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1893 MR_ENABLE_EOP_INTR);
Finn Thaine3f463b2014-11-12 16:12:16 +11001894 if (p & SR_IO) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001895 NCR5380_write(INITIATOR_COMMAND_REG, 0);
Finn Thaine3f463b2014-11-12 16:12:16 +11001896 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1897 } else {
Finn Thaine3f463b2014-11-12 16:12:16 +11001898 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
Finn Thaine3f463b2014-11-12 16:12:16 +11001899 NCR5380_write(START_DMA_SEND_REG, 0);
1900 }
1901
1902#ifdef SUN3_SCSI_VME
1903 dregs->csr |= CSR_DMA_ENABLE;
1904#endif
1905
1906 local_irq_restore(flags);
1907
1908 sun3_dma_active = 1;
1909
1910#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001911 register unsigned char *d = *data;
1912 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Roman Zippelc28bda22007-05-01 22:32:36 +02001914 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1915 *phase = tmp;
1916 return -1;
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Finn Thainef1081c2014-11-12 16:12:14 +11001919 if (hostdata->read_overruns && (p & SR_IO))
1920 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Finn Thaind65e6342014-03-18 11:42:20 +11001922 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001923 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1924 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Roman Zippelc28bda22007-05-01 22:32:36 +02001926 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thaincd400822016-01-03 16:05:40 +11001927 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1928 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Finn Thainef1081c2014-11-12 16:12:14 +11001930 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001931 /* On the Medusa, it is a must to initialize the DMA before
1932 * starting the NCR. This is also the cleaner way for the TT.
1933 */
1934 local_irq_save(flags);
1935 hostdata->dma_len = (p & SR_IO) ?
1936 NCR5380_dma_read_setup(instance, d, c) :
1937 NCR5380_dma_write_setup(instance, d, c);
1938 local_irq_restore(flags);
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Roman Zippelc28bda22007-05-01 22:32:36 +02001941 if (p & SR_IO)
1942 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1943 else {
1944 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1945 NCR5380_write(START_DMA_SEND_REG, 0);
1946 }
1947
Finn Thainef1081c2014-11-12 16:12:14 +11001948 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001949 /* On the Falcon, the DMA setup must be done after the last */
1950 /* NCR access, else the DMA setup gets trashed!
1951 */
1952 local_irq_save(flags);
1953 hostdata->dma_len = (p & SR_IO) ?
1954 NCR5380_dma_read_setup(instance, d, c) :
1955 NCR5380_dma_write_setup(instance, d, c);
1956 local_irq_restore(flags);
1957 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001958#endif /* !defined(CONFIG_SUN3) */
1959
Roman Zippelc28bda22007-05-01 22:32:36 +02001960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962#endif /* defined(REAL_DMA) */
1963
1964/*
1965 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1966 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001967 * Purpose : run through the various SCSI phases and do as the target
1968 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 * instance->connected.
1970 *
1971 * Inputs : instance, instance for which we are doing commands
1972 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001973 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 * modified if a command disconnects, *instance->connected will
1975 * change.
1976 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001977 * XXX Note : we need to watch for bus free or a reset condition here
1978 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001980
1981static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Roman Zippelc28bda22007-05-01 22:32:36 +02001983 SETUP_HOSTDATA(instance);
1984 unsigned long flags;
1985 unsigned char msgout = NOP;
1986 int sink = 0;
1987 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001989 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001991 unsigned char *data;
1992 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001993 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Finn Thaine3f463b2014-11-12 16:12:16 +11001995#ifdef SUN3_SCSI_VME
1996 dregs->csr |= CSR_INTR;
1997#endif
1998
Roman Zippelc28bda22007-05-01 22:32:36 +02001999 while (1) {
2000 tmp = NCR5380_read(STATUS_REG);
2001 /* We only have a valid SCSI phase when REQ is asserted */
2002 if (tmp & SR_REQ) {
2003 phase = (tmp & PHASE_MASK);
2004 if (phase != old_phase) {
2005 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11002006 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002008#if defined(CONFIG_SUN3)
2009 if (phase == PHASE_CMDOUT) {
2010#if defined(REAL_DMA)
2011 void *d;
2012 unsigned long count;
2013
2014 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2015 count = cmd->SCp.buffer->length;
2016 d = sg_virt(cmd->SCp.buffer);
2017 } else {
2018 count = cmd->SCp.this_residual;
2019 d = cmd->SCp.ptr;
2020 }
2021 /* this command setup for dma yet? */
2022 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
2023 if (cmd->request->cmd_type == REQ_TYPE_FS) {
2024 sun3scsi_dma_setup(d, count,
2025 rq_data_dir(cmd->request));
2026 sun3_dma_setup_done = cmd;
2027 }
2028 }
2029#endif
2030#ifdef SUN3_SCSI_VME
2031 dregs->csr |= CSR_INTR;
2032#endif
2033 }
2034#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Roman Zippelc28bda22007-05-01 22:32:36 +02002036 if (sink && (phase != PHASE_MSGOUT)) {
2037 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Roman Zippelc28bda22007-05-01 22:32:36 +02002039 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2040 ICR_ASSERT_ACK);
2041 while (NCR5380_read(STATUS_REG) & SR_REQ)
2042 ;
2043 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2044 ICR_ASSERT_ATN);
2045 sink = 0;
2046 continue;
2047 }
2048
2049 switch (phase) {
2050 case PHASE_DATAOUT:
2051#if (NDEBUG & NDEBUG_NO_DATAOUT)
2052 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2053 "aborted\n", HOSTNO);
2054 sink = 1;
2055 do_abort(instance);
2056 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002057 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002058 return;
2059#endif
2060 case PHASE_DATAIN:
2061 /*
2062 * If there is no room left in the current buffer in the
2063 * scatter-gather list, move onto the next one.
2064 */
2065
2066 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2067 ++cmd->SCp.buffer;
2068 --cmd->SCp.buffers_residual;
2069 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002070 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002071 /* ++roman: Try to merge some scatter-buffers if
2072 * they are at contiguous physical addresses.
2073 */
2074 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002075 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002076 HOSTNO, cmd->SCp.this_residual,
2077 cmd->SCp.buffers_residual);
2078 }
2079
2080 /*
2081 * The preferred transfer method is going to be
2082 * PSEUDO-DMA for systems that are strictly PIO,
2083 * since we can let the hardware do the handshaking.
2084 *
2085 * For this to work, we need to know the transfersize
2086 * ahead of time, since the pseudo-DMA code will sit
2087 * in an unconditional loop.
2088 */
2089
2090 /* ++roman: I suggest, this should be
2091 * #if def(REAL_DMA)
2092 * instead of leaving REAL_DMA out.
2093 */
2094
2095#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002096#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11002097 transfersize = 0;
2098 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11002099#endif
Finn Thainff3d4572016-01-03 16:05:25 +11002100 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
2101
2102 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002103 len = transfersize;
2104 cmd->SCp.phase = phase;
2105 if (NCR5380_transfer_dma(instance, &phase,
2106 &len, (unsigned char **)&cmd->SCp.ptr)) {
2107 /*
2108 * If the watchdog timer fires, all future
2109 * accesses to this device will use the
2110 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002111 scmd_printk(KERN_INFO, cmd,
2112 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002113 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02002114 sink = 1;
2115 do_abort(instance);
2116 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002117 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002118 /* XXX - need to source or sink data here, as appropriate */
2119 } else {
2120#ifdef REAL_DMA
2121 /* ++roman: When using real DMA,
2122 * information_transfer() should return after
2123 * starting DMA since it has nothing more to
2124 * do.
2125 */
2126 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002128 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002130 }
2131 } else
2132#endif /* defined(REAL_DMA) */
2133 NCR5380_transfer_pio(instance, &phase,
2134 (int *)&cmd->SCp.this_residual,
2135 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002136#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2137 /* if we had intended to dma that command clear it */
2138 if (sun3_dma_setup_done == cmd)
2139 sun3_dma_setup_done = NULL;
2140#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002141 break;
2142 case PHASE_MSGIN:
2143 len = 1;
2144 data = &tmp;
2145 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2146 NCR5380_transfer_pio(instance, &phase, &len, &data);
2147 cmd->SCp.Message = tmp;
2148
2149 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002150 case ABORT:
2151 case COMMAND_COMPLETE:
2152 /* Accept message by clearing ACK */
2153 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002154 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002155 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002156
2157 local_irq_save(flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002158 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002159#ifdef SUPPORT_TAGS
2160 cmd_free_tag(cmd);
2161 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2162 /* Turn a QUEUE FULL status into BUSY, I think the
2163 * mid level cannot handle QUEUE FULL :-( (The
2164 * command is retried after BUSY). Also update our
2165 * queue size to the number of currently issued
2166 * commands now.
2167 */
2168 /* ++Andreas: the mid level code knows about
2169 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002170 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002171 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002172 "QUEUE_FULL after %d commands\n",
2173 HOSTNO, cmd->device->id, cmd->device->lun,
2174 ta->nr_allocated);
2175 if (ta->queue_size > ta->nr_allocated)
2176 ta->nr_allocated = ta->queue_size;
2177 }
2178#else
2179 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2180#endif
2181 /* Enable reselect interrupts */
2182 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2183
2184 /*
2185 * I'm not sure what the correct thing to do here is :
2186 *
2187 * If the command that just executed is NOT a request
2188 * sense, the obvious thing to do is to set the result
2189 * code to the values of the stored parameters.
2190 *
2191 * If it was a REQUEST SENSE command, we need some way to
2192 * differentiate between the failure code of the original
2193 * and the failure code of the REQUEST sense - the obvious
2194 * case is success, where we fall through and leave the
2195 * result code unchanged.
2196 *
2197 * The non-obvious place is where the REQUEST SENSE failed
2198 */
2199
2200 if (cmd->cmnd[0] != REQUEST_SENSE)
2201 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2202 else if (status_byte(cmd->SCp.Status) != GOOD)
2203 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2204
Boaz Harrosh28424d32007-09-10 22:37:45 +03002205 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2206 hostdata->ses.cmd_len) {
2207 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2208 hostdata->ses.cmd_len = 0 ;
2209 }
2210
Roman Zippelc28bda22007-05-01 22:32:36 +02002211 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2212 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002213 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002214
Finn Thaind65e6342014-03-18 11:42:20 +11002215 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002216
Roman Zippelc28bda22007-05-01 22:32:36 +02002217 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002218 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002219 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002220 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002221 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002222 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002223 cmd->scsi_done(cmd);
2224 }
2225
2226 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2227 /*
2228 * Restore phase bits to 0 so an interrupted selection,
2229 * arbitration can resume.
2230 */
2231 NCR5380_write(TARGET_COMMAND_REG, 0);
2232
Roman Zippelc28bda22007-05-01 22:32:36 +02002233 /* ++roman: For Falcon SCSI, release the lock on the
2234 * ST-DMA here if no other commands are waiting on the
2235 * disconnected queue.
2236 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002237 maybe_release_dma_irq(instance);
2238 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002239 return;
2240 case MESSAGE_REJECT:
2241 /* Accept message by clearing ACK */
2242 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2243 /* Enable reselect interrupts */
2244 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2245 switch (hostdata->last_message) {
2246 case HEAD_OF_QUEUE_TAG:
2247 case ORDERED_QUEUE_TAG:
2248 case SIMPLE_QUEUE_TAG:
2249 /* The target obviously doesn't support tagged
2250 * queuing, even though it announced this ability in
2251 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2252 * clear 'tagged_supported' and lock the LUN, since
2253 * the command is treated as untagged further on.
2254 */
2255 cmd->device->tagged_supported = 0;
2256 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2257 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002258 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002259 "QUEUE_TAG message; tagged queuing "
2260 "disabled\n",
2261 HOSTNO, cmd->device->id, cmd->device->lun);
2262 break;
2263 }
2264 break;
2265 case DISCONNECT:
2266 /* Accept message by clearing ACK */
2267 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2268 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002269 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002270 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002271 hostdata->connected = NULL;
2272 hostdata->disconnected_queue = cmd;
2273 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002274 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002275 "moved from connected to the "
2276 "disconnected_queue\n", HOSTNO,
2277 cmd->device->id, cmd->device->lun);
2278 /*
2279 * Restore phase bits to 0 so an interrupted selection,
2280 * arbitration can resume.
2281 */
2282 NCR5380_write(TARGET_COMMAND_REG, 0);
2283
2284 /* Enable reselect interrupts */
2285 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002286#ifdef SUN3_SCSI_VME
2287 dregs->csr |= CSR_DMA_ENABLE;
2288#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002289 return;
2290 /*
2291 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2292 * operation, in violation of the SCSI spec so we can safely
2293 * ignore SAVE/RESTORE pointers calls.
2294 *
2295 * Unfortunately, some disks violate the SCSI spec and
2296 * don't issue the required SAVE_POINTERS message before
2297 * disconnecting, and we have to break spec to remain
2298 * compatible.
2299 */
2300 case SAVE_POINTERS:
2301 case RESTORE_POINTERS:
2302 /* Accept message by clearing ACK */
2303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2304 /* Enable reselect interrupts */
2305 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2306 break;
2307 case EXTENDED_MESSAGE:
2308 /*
2309 * Extended messages are sent in the following format :
2310 * Byte
2311 * 0 EXTENDED_MESSAGE == 1
2312 * 1 length (includes one byte for code, doesn't
2313 * include first two bytes)
2314 * 2 code
2315 * 3..length+1 arguments
2316 *
2317 * Start the extended message buffer with the EXTENDED_MESSAGE
2318 * byte, since spi_print_msg() wants the whole thing.
2319 */
2320 extended_msg[0] = EXTENDED_MESSAGE;
2321 /* Accept first byte by clearing ACK */
2322 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2323
Finn Thaind65e6342014-03-18 11:42:20 +11002324 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002325
2326 len = 2;
2327 data = extended_msg + 1;
2328 phase = PHASE_MSGIN;
2329 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002330 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002331 (int)extended_msg[1], (int)extended_msg[2]);
2332
2333 if (!len && extended_msg[1] <=
2334 (sizeof(extended_msg) - 1)) {
2335 /* Accept third byte by clearing ACK */
2336 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2337 len = extended_msg[1] - 1;
2338 data = extended_msg + 3;
2339 phase = PHASE_MSGIN;
2340
2341 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002342 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002343 HOSTNO, len);
2344
2345 switch (extended_msg[2]) {
2346 case EXTENDED_SDTR:
2347 case EXTENDED_WDTR:
2348 case EXTENDED_MODIFY_DATA_POINTER:
2349 case EXTENDED_EXTENDED_IDENTIFY:
2350 tmp = 0;
2351 }
2352 } else if (len) {
2353 printk(KERN_NOTICE "scsi%d: error receiving "
2354 "extended message\n", HOSTNO);
2355 tmp = 0;
2356 } else {
2357 printk(KERN_NOTICE "scsi%d: extended message "
2358 "code %02x length %d is too long\n",
2359 HOSTNO, extended_msg[2], extended_msg[1]);
2360 tmp = 0;
2361 }
2362 /* Fall through to reject message */
2363
2364 /*
2365 * If we get something weird that we aren't expecting,
2366 * reject it.
2367 */
2368 default:
2369 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002370 printk(KERN_INFO "scsi%d: rejecting message ",
2371 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002372 spi_print_msg(extended_msg);
2373 printk("\n");
2374 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002375 scmd_printk(KERN_INFO, cmd,
2376 "rejecting unknown message %02x\n",
2377 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002378 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002379 scmd_printk(KERN_INFO, cmd,
2380 "rejecting unknown extended message code %02x, length %d\n",
2381 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002382
2383 msgout = MESSAGE_REJECT;
2384 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2385 break;
2386 } /* switch (tmp) */
2387 break;
2388 case PHASE_MSGOUT:
2389 len = 1;
2390 data = &msgout;
2391 hostdata->last_message = msgout;
2392 NCR5380_transfer_pio(instance, &phase, &len, &data);
2393 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002394 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002395#ifdef SUPPORT_TAGS
2396 cmd_free_tag(cmd);
2397#else
2398 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2399#endif
2400 hostdata->connected = NULL;
2401 cmd->result = DID_ERROR << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002402 maybe_release_dma_irq(instance);
2403 local_irq_restore(flags);
2404 cmd->scsi_done(cmd);
Finn Thaincd400822016-01-03 16:05:40 +11002405 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02002406 return;
2407 }
2408 msgout = NOP;
2409 break;
2410 case PHASE_CMDOUT:
2411 len = cmd->cmd_len;
2412 data = cmd->cmnd;
2413 /*
2414 * XXX for performance reasons, on machines with a
2415 * PSEUDO-DMA architecture we should probably
2416 * use the dma transfer function.
2417 */
2418 NCR5380_transfer_pio(instance, &phase, &len, &data);
2419 break;
2420 case PHASE_STATIN:
2421 len = 1;
2422 data = &tmp;
2423 NCR5380_transfer_pio(instance, &phase, &len, &data);
2424 cmd->SCp.Status = tmp;
2425 break;
2426 default:
2427 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002428 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002429 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002430 } else {
2431 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2432 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002433 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
2436/*
2437 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2438 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002439 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002440 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002442 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 * Inputs : instance - this instance of the NCR5380.
2444 *
2445 */
2446
2447
Finn Thaine3f463b2014-11-12 16:12:16 +11002448/* it might eventually prove necessary to do a dma setup on
2449 reselection, but it doesn't seem to be needed now -- sam */
2450
Roman Zippelc28bda22007-05-01 22:32:36 +02002451static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Roman Zippelc28bda22007-05-01 22:32:36 +02002453 SETUP_HOSTDATA(instance);
2454 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002455 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002457 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002459 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002460 int __maybe_unused len;
2461 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002462 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Roman Zippelc28bda22007-05-01 22:32:36 +02002464 /*
2465 * Disable arbitration, etc. since the host adapter obviously
2466 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Roman Zippelc28bda22007-05-01 22:32:36 +02002469 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Roman Zippelc28bda22007-05-01 22:32:36 +02002471 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2472
Finn Thaind65e6342014-03-18 11:42:20 +11002473 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002474
2475 /*
2476 * At this point, we have detected that our SCSI ID is on the bus,
2477 * SEL is true and BSY was false for at least one bus settle delay
2478 * (400 ns).
2479 *
2480 * We must assert BSY ourselves, until the target drops the SEL
2481 * signal.
2482 */
2483
2484 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2485
2486 while (NCR5380_read(STATUS_REG) & SR_SEL)
2487 ;
2488 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2489
2490 /*
2491 * Wait for target to go into MSGIN.
2492 */
2493
2494 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2495 ;
2496
Finn Thaine3f463b2014-11-12 16:12:16 +11002497#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2498 /* acknowledge toggle to MSGIN */
2499 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2500
2501 /* peek at the byte without really hitting the bus */
2502 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2503#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002504 len = 1;
2505 data = msg;
2506 phase = PHASE_MSGIN;
2507 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002508#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002509
2510 if (!(msg[0] & 0x80)) {
2511 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2512 spi_print_msg(msg);
2513 do_abort(instance);
2514 return;
2515 }
2516 lun = (msg[0] & 0x07);
2517
Finn Thaine3f463b2014-11-12 16:12:16 +11002518#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002519 /* If the phase is still MSGIN, the target wants to send some more
2520 * messages. In case it supports tagged queuing, this is probably a
2521 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2522 */
2523 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002524 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002525 /* Accept previous IDENTIFY message by clearing ACK */
2526 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2527 len = 2;
2528 data = msg + 1;
2529 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2530 msg[1] == SIMPLE_QUEUE_TAG)
2531 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002532 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002533 "reselection\n", HOSTNO, target_mask, lun, tag);
2534 }
2535#endif
2536
2537 /*
2538 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2539 * just reestablished, and remove it from the disconnected queue.
2540 */
2541
Finn Thain710ddd02014-11-12 16:12:02 +11002542 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002543 tmp; prev = tmp, tmp = NEXT(tmp)) {
2544 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2545#ifdef SUPPORT_TAGS
2546 && (tag == tmp->tag)
2547#endif
2548 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002549 if (prev) {
2550 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002551 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002552 } else {
2553 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2554 hostdata->disconnected_queue = NEXT(tmp);
2555 }
Roman Zippel3130d902007-05-01 22:32:37 +02002556 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002557 break;
2558 }
2559 }
2560
2561 if (!tmp) {
2562 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2563#ifdef SUPPORT_TAGS
2564 "tag %d "
2565#endif
2566 "not in disconnected_queue.\n",
2567 HOSTNO, target_mask, lun
2568#ifdef SUPPORT_TAGS
2569 , tag
2570#endif
2571 );
2572 /*
2573 * Since we have an established nexus that we can't do anything
2574 * with, we must abort it.
2575 */
2576 do_abort(instance);
2577 return;
2578 }
2579
Finn Thaine3f463b2014-11-12 16:12:16 +11002580#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2581 /* engage dma setup for the command we just saw */
2582 {
2583 void *d;
2584 unsigned long count;
2585
2586 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2587 count = tmp->SCp.buffer->length;
2588 d = sg_virt(tmp->SCp.buffer);
2589 } else {
2590 count = tmp->SCp.this_residual;
2591 d = tmp->SCp.ptr;
2592 }
2593 /* setup this command for dma if not already */
2594 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2595 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2596 sun3_dma_setup_done = tmp;
2597 }
2598 }
2599
2600 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2601#endif
2602
Roman Zippelc28bda22007-05-01 22:32:36 +02002603 /* Accept message by clearing ACK */
2604 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2605
Finn Thaine3f463b2014-11-12 16:12:16 +11002606#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2607 /* If the phase is still MSGIN, the target wants to send some more
2608 * messages. In case it supports tagged queuing, this is probably a
2609 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2610 */
2611 tag = TAG_NONE;
2612 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2613 /* Accept previous IDENTIFY message by clearing ACK */
2614 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2615 len = 2;
2616 data = msg + 1;
2617 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2618 msg[1] == SIMPLE_QUEUE_TAG)
2619 tag = msg[2];
2620 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2621 HOSTNO, target_mask, lun, tag);
2622 }
2623#endif
2624
Roman Zippelc28bda22007-05-01 22:32:36 +02002625 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002626 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002627 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
2630
2631/*
Finn Thain710ddd02014-11-12 16:12:02 +11002632 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 *
2634 * Purpose : abort a command
2635 *
Finn Thain710ddd02014-11-12 16:12:02 +11002636 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002637 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 * used.
2639 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002640 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002642 * XXX - there is no way to abort the command that is currently
2643 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002645 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 */
2647
2648static
Finn Thain710ddd02014-11-12 16:12:02 +11002649int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Roman Zippelc28bda22007-05-01 22:32:36 +02002651 struct Scsi_Host *instance = cmd->device->host;
2652 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002653 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002654 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002656 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Roman Zippelc28bda22007-05-01 22:32:36 +02002658 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Roman Zippelc28bda22007-05-01 22:32:36 +02002660 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Finn Thaind65e6342014-03-18 11:42:20 +11002662 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002663 NCR5380_read(BUS_AND_STATUS_REG),
2664 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002667 /*
2668 * Case 1 : If the command is the currently executing command,
2669 * we'll set the aborted flag and return control so that
2670 * information transfer routine can exit cleanly.
2671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Roman Zippelc28bda22007-05-01 22:32:36 +02002673 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Finn Thaind65e6342014-03-18 11:42:20 +11002675 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002676 /*
2677 * We should perform BSY checking, and make sure we haven't slipped
2678 * into BUS FREE.
2679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Roman Zippelc28bda22007-05-01 22:32:36 +02002681 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2682 /*
2683 * Since we can't change phases until we've completed the current
2684 * handshake, we have to source or sink a byte of data if the current
2685 * phase is not MSGOUT.
2686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Roman Zippelc28bda22007-05-01 22:32:36 +02002688 /*
2689 * Return control to the executing NCR drive so we can clear the
2690 * aborted flag and get back into our main loop.
2691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Roman Zippelc28bda22007-05-01 22:32:36 +02002693 if (do_abort(instance) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002694 hostdata->connected = NULL;
2695 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002697 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002699 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002701 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002702 local_irq_restore(flags);
2703 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002704 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002705 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002706 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002707 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002708 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002712
2713 /*
2714 * Case 2 : If the command hasn't been issued yet, we simply remove it
2715 * from the issue queue.
2716 */
Finn Thain710ddd02014-11-12 16:12:02 +11002717 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2718 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002719 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2720 if (cmd == tmp) {
2721 REMOVE(5, *prev, tmp, NEXT(tmp));
2722 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002723 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002724 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002725 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002726 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002727 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002728 HOSTNO);
2729 /* Tagged queuing note: no tag to free here, hasn't been assigned
2730 * yet... */
2731 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002732 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734 }
2735
Roman Zippelc28bda22007-05-01 22:32:36 +02002736 /*
2737 * Case 3 : If any commands are connected, we're going to fail the abort
2738 * and let the high level SCSI driver retry at a later time or
2739 * issue a reset.
2740 *
2741 * Timeouts, and therefore aborted commands, will be highly unlikely
2742 * and handling them cleanly in this situation would make the common
2743 * case of noresets less efficient, and would pollute our code. So,
2744 * we fail.
2745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Roman Zippelc28bda22007-05-01 22:32:36 +02002747 if (hostdata->connected) {
2748 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002749 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002750 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Roman Zippelc28bda22007-05-01 22:32:36 +02002753 /*
2754 * Case 4: If the command is currently disconnected from the bus, and
2755 * there are no connected commands, we reconnect the I_T_L or
2756 * I_T_L_Q nexus associated with it, go into message out, and send
2757 * an abort message.
2758 *
2759 * This case is especially ugly. In order to reestablish the nexus, we
2760 * need to call NCR5380_select(). The easiest way to implement this
2761 * function was to abort if the bus was busy, and let the interrupt
2762 * handler triggered on the SEL for reselect take care of lost arbitrations
2763 * where necessary, meaning interrupts need to be enabled.
2764 *
2765 * When interrupts are enabled, the queues may change - so we
2766 * can't remove it from the disconnected queue before selecting it
2767 * because that could cause a failure in hashing the nexus if that
2768 * device reselected.
2769 *
2770 * Since the queues may change, we can't use the pointers from when we
2771 * first locate it.
2772 *
2773 * So, we must first locate the command, and if NCR5380_select()
2774 * succeeds, then issue the abort, relocate the command and remove
2775 * it from the disconnected queue.
2776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Finn Thain710ddd02014-11-12 16:12:02 +11002778 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002779 tmp = NEXT(tmp)) {
2780 if (cmd == tmp) {
2781 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002782 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002783
Finn Thain76f13b92014-11-12 16:11:53 +11002784 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002785 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002786
Finn Thaind65e6342014-03-18 11:42:20 +11002787 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002788
2789 do_abort(instance);
2790
2791 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002792 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2793 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002794 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2795 if (cmd == tmp) {
2796 REMOVE(5, *prev, tmp, NEXT(tmp));
2797 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002798 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002799 tmp->result = DID_ABORT << 16;
2800 /* We must unlock the tag/LUN immediately here, since the
2801 * target goes to BUS FREE and doesn't send us another
2802 * message (COMMAND_COMPLETE or the like)
2803 */
2804#ifdef SUPPORT_TAGS
2805 cmd_free_tag(tmp);
2806#else
2807 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2808#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002809 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002810 local_irq_restore(flags);
2811 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002812 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002813 }
2814 }
2815 }
2816 }
2817
Finn Thaine3c3da62014-11-12 16:12:15 +11002818 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2819 * possible at all) At least, we should check if the lock could be
2820 * released after the abort, in case it is kept due to some bug.
2821 */
2822 maybe_release_dma_irq(instance);
2823 local_irq_restore(flags);
2824
Roman Zippelc28bda22007-05-01 22:32:36 +02002825 /*
2826 * Case 5 : If we reached this point, the command was not found in any of
2827 * the queues.
2828 *
2829 * We probably reached this point because of an unlikely race condition
2830 * between the command completing successfully and the abortion code,
2831 * so we won't panic, but we will notify the user in case something really
2832 * broke.
2833 */
2834
Joe Perchesad361c92009-07-06 13:05:40 -07002835 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002836
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002837 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840
Finn Thain3be1b3e2016-01-03 16:05:12 +11002841/**
2842 * NCR5380_bus_reset - reset the SCSI bus
2843 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002844 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002845 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Finn Thain710ddd02014-11-12 16:12:02 +11002848static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Finn Thaine3c3da62014-11-12 16:12:15 +11002850 struct Scsi_Host *instance = cmd->device->host;
2851 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002852 int i;
2853 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Finn Thain3be1b3e2016-01-03 16:05:12 +11002855 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Finn Thain3be1b3e2016-01-03 16:05:12 +11002857#if (NDEBUG & NDEBUG_ANY)
2858 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2859 NCR5380_print_status(instance);
2860#endif
2861
2862 do_reset(instance);
2863
Roman Zippelc28bda22007-05-01 22:32:36 +02002864 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002865 NCR5380_write(MODE_REG, MR_BASE);
2866 NCR5380_write(TARGET_COMMAND_REG, 0);
2867 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Roman Zippelc28bda22007-05-01 22:32:36 +02002869 /* After the reset, there are no more connected or disconnected commands
2870 * and no busy units; so clear the low-level status here to avoid
2871 * conflicts when the mid-level code tries to wake up the affected
2872 * commands!
2873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Roman Zippelc28bda22007-05-01 22:32:36 +02002875 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002876 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002877 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002878 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002879 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002880 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Roman Zippelc28bda22007-05-01 22:32:36 +02002882 hostdata->issue_queue = NULL;
2883 hostdata->connected = NULL;
2884 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002886 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002888 for (i = 0; i < 8; ++i)
2889 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002891 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002893
2894 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002895 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002897 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}