blob: 6daed6b386d4c8b47cb3094754d59b162039802c [file] [log] [blame]
Roman Zippelc28bda22007-05-01 22:32:36 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Roman Zippelc28bda22007-05-01 22:32:36 +02003 * to implement 5380 SCSI drivers under Linux with a non-trantor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
Roman Zippelc28bda22007-05-01 22:32:36 +02009 * Visionary Computing
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (Unix and Linux consulting and custom programming)
Roman Zippelc28bda22007-05-01 22:32:36 +020011 * drew@colorado.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * +1 (303) 666-5836
13 *
Roman Zippelc28bda22007-05-01 22:32:36 +020014 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
28 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
29 * this file, too:
30 *
31 * - Some of the debug statements were incorrect (undefined variables and the
32 * like). I fixed that.
33 *
34 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
35 * possible DMA transfer size should also happen for REAL_DMA. I added this
36 * in the #if statement.
37 *
38 * - When using real DMA, information_transfer() should return in a DATAOUT
39 * phase after starting the DMA. It has nothing more to do.
40 *
41 * - The interrupt service routine should run main after end of DMA, too (not
42 * only after RESELECTION interrupts). Additionally, it should _not_ test
43 * for more interrupts after running main, since a DMA process may have
44 * been started and interrupts are turned on now. The new int could happen
45 * inside the execution of NCR5380_intr(), leading to recursive
46 * calls.
47 *
48 * - I've added a function merge_contiguous_buffers() that tries to
49 * merge scatter-gather buffers that are located at contiguous
50 * physical addresses and can be processed with the same DMA setup.
51 * Since most scatter-gather operations work on a page (4K) of
52 * 4 buffers (1K), in more than 90% of all cases three interrupts and
53 * DMA setup actions are saved.
54 *
55 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
56 * and USLEEP, because these were messing up readability and will never be
57 * needed for Atari SCSI.
Roman Zippelc28bda22007-05-01 22:32:36 +020058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
60 * stuff), and 'main' is executed in a bottom half if awoken by an
61 * interrupt.
62 *
63 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
64 * constructs. In my eyes, this made the source rather unreadable, so I
65 * finally replaced that by the *_PRINTK() macros.
66 *
67 */
68
69/*
Roman Zippelc28bda22007-05-01 22:32:36 +020070 * Further development / testing that should be done :
71 * 1. Test linked command handling code after Eric is ready with
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * the high level code.
73 */
Finn Thaine3f463b2014-11-12 16:12:16 +110074
75/* Adapted for the sun3 by Sam Creasey. */
76
db9dff32005-04-03 14:53:59 -050077#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050078#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#if (NDEBUG & NDEBUG_LISTS)
Roman Zippelc28bda22007-05-01 22:32:36 +020081#define LIST(x, y) \
82 do { \
83 printk("LINE:%d Adding %p to %p\n", \
84 __LINE__, (void*)(x), (void*)(y)); \
85 if ((x) == (y)) \
86 udelay(5); \
87 } while (0)
88#define REMOVE(w, x, y, z) \
89 do { \
90 printk("LINE:%d Removing: %p->%p %p->%p \n", \
91 __LINE__, (void*)(w), (void*)(x), \
92 (void*)(y), (void*)(z)); \
93 if ((x) == (y)) \
94 udelay(5); \
95 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#else
97#define LIST(x,y)
98#define REMOVE(w,x,y,z)
99#endif
100
101#ifndef notyet
102#undef LINKED
103#endif
104
105/*
106 * Design
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200108 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +0200110 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * memory mapped) and drops this file in their 'C' wrapper.
112 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200113 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * and commands that are currently executing. This means that an
116 * unlimited number of commands may be queued, letting
117 * more commands propagate from the higher driver levels giving higher
118 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
119 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * while a command is already executing.
121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200123 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200125 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
126 * piece of hardware that requires you to sit in a loop polling for
127 * the REQ signal as long as you are connected. Some devices are
128 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * while doing long seek operations.
Roman Zippelc28bda22007-05-01 22:32:36 +0200130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * The workaround for this is to keep track of devices that have
132 * disconnected. If the device hasn't disconnected, for commands that
Roman Zippelc28bda22007-05-01 22:32:36 +0200133 * should disconnect, we do something like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
135 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
Roman Zippelc28bda22007-05-01 22:32:36 +0200136 *
137 * Some tweaking of N and M needs to be done. An algorithm based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * on "time to data" would give the best results as long as short time
Roman Zippelc28bda22007-05-01 22:32:36 +0200139 * to datas (ie, on the same track) were considered, however these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * broken devices are the exception rather than the rule and I'd rather
141 * spend my time optimizing for the normal case.
142 *
143 * Architecture :
144 *
145 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +1100146 * which is started from a workqueue for each NCR5380 host in the
147 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
148 * removing the commands from the issue queue and calling
149 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * Once a nexus is established, the NCR5380_information_transfer()
152 * phase goes through the various phases as instructed by the target.
153 * if the target goes into MSG IN and sends a DISCONNECT message,
154 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +1100155 * queue, and NCR5380_main tries to find more work. If the target is
156 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
158 * If a command has disconnected, eventually an interrupt will trigger,
159 * calling NCR5380_intr() which will in turn call NCR5380_reselect
160 * to reestablish a nexus. This will run main if necessary.
161 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200162 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * appropriate.
164 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200165 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * structures, being initialized after the command is connected
167 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
168 * Note that in violation of the standard, an implicit SAVE POINTERS operation
169 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
170 */
171
172/*
173 * Using this file :
174 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200175 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * and macros and include this file in your driver.
177 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200178 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200180 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100182 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
183 * transceivers.
184 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * LINKED - if defined, linked commands are supported.
186 *
187 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
188 *
189 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
190 *
191 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * NCR5380_read(register) - read from the specified register
194 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200195 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100197 * NCR5380_implementation_fields - additional fields needed for this
198 * specific implementation of the NCR5380
199 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200201 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200203 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * that they were able to program the controller for.
205 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200206 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * available as NCR5380_i386_dma_write_setup,
208 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
209 *
210 * NCR5380_dma_write_setup(instance, src, count) - initialize
211 * NCR5380_dma_read_setup(instance, dst, count) - initialize
212 * NCR5380_dma_residual(instance); - residual count
213 *
214 * PSEUDO functions :
215 * NCR5380_pwrite(instance, src, count)
216 * NCR5380_pread(instance, dst, count);
217 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200219 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100221 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/* Macros ease life... :-) */
225#define SETUP_HOSTDATA(in) \
226 struct NCR5380_hostdata *hostdata = \
227 (struct NCR5380_hostdata *)(in)->hostdata
228#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
229
Finn Thain710ddd02014-11-12 16:12:02 +1100230#define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
Roman Zippel3130d902007-05-01 22:32:37 +0200231#define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
Finn Thain710ddd02014-11-12 16:12:02 +1100232#define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234#define HOSTNO instance->host_no
235#define H_NO(cmd) (cmd)->device->host->host_no
236
237#ifdef SUPPORT_TAGS
238
239/*
240 * Functions for handling tagged queuing
241 * =====================================
242 *
243 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
244 *
245 * Using consecutive numbers for the tags is no good idea in my eyes. There
246 * could be wrong re-usings if the counter (8 bit!) wraps and some early
247 * command has been preempted for a long time. My solution: a bitfield for
248 * remembering used tags.
249 *
250 * There's also the problem that each target has a certain queue size, but we
251 * cannot know it in advance :-( We just see a QUEUE_FULL status being
252 * returned. So, in this case, the driver internal queue size assumption is
253 * reduced to the number of active tags if QUEUE_FULL is returned by the
254 * target. The command is returned to the mid-level, but with status changed
255 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
256 * correctly.
257 *
258 * We're also not allowed running tagged commands as long as an untagged
259 * command is active. And REQUEST SENSE commands after a contingent allegiance
260 * condition _must_ be untagged. To keep track whether an untagged command has
261 * been issued, the host->busy array is still employed, as it is without
262 * support for tagged queuing.
263 *
264 * One could suspect that there are possible race conditions between
265 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
266 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
267 * which already guaranteed to be running at most once. It is also the only
268 * place where tags/LUNs are allocated. So no other allocation can slip
269 * between that pair, there could only happen a reselection, which can free a
270 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
271 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
272 */
273
Finn Thainca513fc2014-11-12 16:12:19 +1100274static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Roman Zippelc28bda22007-05-01 22:32:36 +0200276 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100277 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200278
Finn Thainca513fc2014-11-12 16:12:19 +1100279 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200280 return;
281
282 for (target = 0; target < 8; ++target) {
283 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100284 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200285 bitmap_zero(ta->allocated, MAX_TAGS);
286 ta->nr_allocated = 0;
287 /* At the beginning, assume the maximum queue size we could
288 * support (MAX_TAGS). This value will be decreased if the target
289 * returns QUEUE_FULL status.
290 */
291 ta->queue_size = MAX_TAGS;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296
297/* Check if we can issue a command to this LUN: First see if the LUN is marked
298 * busy by an untagged command. If the command should use tagged queuing, also
299 * check that there is a free tag and the target's queue won't overflow. This
300 * function should be called with interrupts disabled to avoid race
301 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Finn Thain710ddd02014-11-12 16:12:02 +1100304static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200306 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200307 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200309 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200310 return 1;
311 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100312 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
313 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200314 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100315 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
316 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaind65e6342014-03-18 11:42:20 +1100317 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200318 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200319 return 1;
320 }
321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324
325/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
326 * must be called before!), or reserve the LUN in 'busy' if the command is
327 * untagged.
328 */
329
Finn Thain710ddd02014-11-12 16:12:02 +1100330static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200332 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200333 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Roman Zippelc28bda22007-05-01 22:32:36 +0200335 /* If we or the target don't support tagged queuing, allocate the LUN for
336 * an untagged command.
337 */
338 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100339 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
340 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200341 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200342 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100343 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200344 "command\n", H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200345 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100346 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Roman Zippelc28bda22007-05-01 22:32:36 +0200348 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
349 set_bit(cmd->tag, ta->allocated);
350 ta->nr_allocated++;
Finn Thaind65e6342014-03-18 11:42:20 +1100351 dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
Roman Zippelc28bda22007-05-01 22:32:36 +0200352 "(now %d tags in use)\n",
353 H_NO(cmd), cmd->tag, cmd->device->id,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200354 lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358
359/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
360 * unlock the LUN.
361 */
362
Finn Thain710ddd02014-11-12 16:12:02 +1100363static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200365 u8 lun = cmd->device->lun;
Roman Zippelc28bda22007-05-01 22:32:36 +0200366 SETUP_HOSTDATA(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Roman Zippelc28bda22007-05-01 22:32:36 +0200368 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200369 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaind65e6342014-03-18 11:42:20 +1100370 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200371 H_NO(cmd), cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200372 } else if (cmd->tag >= MAX_TAGS) {
373 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
374 H_NO(cmd), cmd->tag);
375 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100376 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200377 clear_bit(cmd->tag, ta->allocated);
378 ta->nr_allocated--;
Finn Thaind65e6342014-03-18 11:42:20 +1100379 dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200380 H_NO(cmd), cmd->tag, cmd->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384
Finn Thainca513fc2014-11-12 16:12:19 +1100385static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Roman Zippelc28bda22007-05-01 22:32:36 +0200387 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100388 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Finn Thainca513fc2014-11-12 16:12:19 +1100390 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200391 return;
392
393 for (target = 0; target < 8; ++target) {
394 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100395 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200396 bitmap_zero(ta->allocated, MAX_TAGS);
397 ta->nr_allocated = 0;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402#endif /* SUPPORT_TAGS */
403
404
405/*
Finn Thain710ddd02014-11-12 16:12:02 +1100406 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
408 * Purpose: Try to merge several scatter-gather requests into one DMA
409 * transfer. This is possible if the scatter buffers lie on
410 * physical contiguous addresses.
411 *
Finn Thain710ddd02014-11-12 16:12:02 +1100412 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300414 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
416
Finn Thain710ddd02014-11-12 16:12:02 +1100417static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Finn Thaine3f463b2014-11-12 16:12:16 +1100419#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200420 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200422 unsigned long oldlen = cmd->SCp.this_residual;
423 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
425
Roman Zippelc28bda22007-05-01 22:32:36 +0200426 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
427 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200428 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100429 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200430 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200432 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200434 ++cmd->SCp.buffer;
435 --cmd->SCp.buffers_residual;
436 cmd->SCp.this_residual += cmd->SCp.buffer->length;
437 endaddr += cmd->SCp.buffer->length;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200440 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100441 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200442 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100444#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Finn Thainff50f9e2014-11-12 16:12:18 +1100447/**
448 * initialize_SCp - init the scsi pointer field
449 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100451 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453
Finn Thain710ddd02014-11-12 16:12:02 +1100454static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Roman Zippelc28bda22007-05-01 22:32:36 +0200456 /*
457 * Initialize the Scsi Pointer field so that all of the commands in the
458 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200460
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200461 if (scsi_bufflen(cmd)) {
462 cmd->SCp.buffer = scsi_sglist(cmd);
463 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200464 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200465 cmd->SCp.this_residual = cmd->SCp.buffer->length;
466 /* ++roman: Try to merge some scatter-buffers if they are at
467 * contiguous physical addresses.
468 */
469 merge_contiguous_buffers(cmd);
470 } else {
471 cmd->SCp.buffer = NULL;
472 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200473 cmd->SCp.ptr = NULL;
474 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#include <linux/delay.h>
479
480#if NDEBUG
481static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200482 unsigned char mask;
483 const char *name;
484} signals[] = {
485 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
486 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
487 { SR_SEL, "SEL" }, {0, NULL}
488}, basrs[] = {
489 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
490}, icrs[] = {
491 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
492 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
493 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
494 {0, NULL}
495}, mrs[] = {
496 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
497 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
498 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
499 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
500 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
501 {0, NULL}
502};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Finn Thainff50f9e2014-11-12 16:12:18 +1100504/**
505 * NCR5380_print - print scsi bus signals
506 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100508 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
510
Roman Zippelc28bda22007-05-01 22:32:36 +0200511static void NCR5380_print(struct Scsi_Host *instance)
512{
513 unsigned char status, data, basr, mr, icr, i;
514 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Roman Zippelc28bda22007-05-01 22:32:36 +0200516 local_irq_save(flags);
517 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
518 status = NCR5380_read(STATUS_REG);
519 mr = NCR5380_read(MODE_REG);
520 icr = NCR5380_read(INITIATOR_COMMAND_REG);
521 basr = NCR5380_read(BUS_AND_STATUS_REG);
522 local_irq_restore(flags);
523 printk("STATUS_REG: %02x ", status);
524 for (i = 0; signals[i].mask; ++i)
525 if (status & signals[i].mask)
526 printk(",%s", signals[i].name);
527 printk("\nBASR: %02x ", basr);
528 for (i = 0; basrs[i].mask; ++i)
529 if (basr & basrs[i].mask)
530 printk(",%s", basrs[i].name);
531 printk("\nICR: %02x ", icr);
532 for (i = 0; icrs[i].mask; ++i)
533 if (icr & icrs[i].mask)
534 printk(",%s", icrs[i].name);
535 printk("\nMODE: %02x ", mr);
536 for (i = 0; mrs[i].mask; ++i)
537 if (mr & mrs[i].mask)
538 printk(",%s", mrs[i].name);
539 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200543 unsigned char value;
544 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200546 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
547 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
548 {PHASE_UNKNOWN, "UNKNOWN"}
549};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Finn Thainff50f9e2014-11-12 16:12:18 +1100551/**
552 * NCR5380_print_phase - show SCSI phase
553 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100555 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100557 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559
560static void NCR5380_print_phase(struct Scsi_Host *instance)
561{
Roman Zippelc28bda22007-05-01 22:32:36 +0200562 unsigned char status;
563 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Roman Zippelc28bda22007-05-01 22:32:36 +0200565 status = NCR5380_read(STATUS_REG);
566 if (!(status & SR_REQ))
567 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
568 else {
569 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
570 (phases[i].value != (status & PHASE_MASK)); ++i)
571 ;
572 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#endif
577
578/*
579 * ++roman: New scheme of calling NCR5380_main()
Roman Zippelc28bda22007-05-01 22:32:36 +0200580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * If we're not in an interrupt, we can call our main directly, it cannot be
582 * already running. Else, we queue it on a task queue, if not 'main_running'
583 * tells us that a lower level is already executing it. This way,
584 * 'main_running' needs not be protected in a special way.
585 *
586 * queue_main() is a utility function for putting our main onto the task
587 * queue, if main_running is false. It should be called only from a
588 * interrupt or bottom half.
589 */
590
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900591#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#include <linux/workqueue.h>
593#include <linux/interrupt.h>
594
Finn Thaina53a21e2014-11-12 16:12:21 +1100595static inline void queue_main(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Finn Thaina53a21e2014-11-12 16:12:21 +1100597 if (!hostdata->main_running) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200598 /* If in interrupt and NCR5380_main() not already running,
599 queue it on the 'immediate' task queue, to be processed
600 immediately after the current interrupt processing has
601 finished. */
Finn Thaina53a21e2014-11-12 16:12:21 +1100602 schedule_work(&hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200603 }
604 /* else: nothing to do: the running NCR5380_main() will pick up
605 any newly queued command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Finn Thain8c325132014-11-12 16:11:58 +1100608/**
609 * NCR58380_info - report driver and host information
610 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *
Finn Thain8c325132014-11-12 16:11:58 +1100612 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
Finn Thain8c325132014-11-12 16:11:58 +1100614 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616
Finn Thain8c325132014-11-12 16:11:58 +1100617static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Finn Thain8c325132014-11-12 16:11:58 +1100619 struct NCR5380_hostdata *hostdata = shost_priv(instance);
620
621 return hostdata->info;
622}
623
624static void prepare_info(struct Scsi_Host *instance)
625{
626 struct NCR5380_hostdata *hostdata = shost_priv(instance);
627
628 snprintf(hostdata->info, sizeof(hostdata->info),
629 "%s, io_port 0x%lx, n_io_port %d, "
630 "base 0x%lx, irq %d, "
631 "can_queue %d, cmd_per_lun %d, "
632 "sg_tablesize %d, this_id %d, "
Finn Thainca513fc2014-11-12 16:12:19 +1100633 "flags { %s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100634 "options { %s} ",
635 instance->hostt->name, instance->io_port, instance->n_io_port,
636 instance->base, instance->irq,
637 instance->can_queue, instance->cmd_per_lun,
638 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100639 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100640#ifdef DIFFERENTIAL
641 "DIFFERENTIAL "
642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100644 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#endif
646#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100647 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#endif
649#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100650 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#endif
Finn Thain8c325132014-11-12 16:11:58 +1100652 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Finn Thainff50f9e2014-11-12 16:12:18 +1100655/**
656 * NCR5380_print_status - dump controller info
657 * @instance: controller to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100659 * Print commands in the various queues, called from NCR5380_abort
660 * to aid debugging.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
662
Finn Thain710ddd02014-11-12 16:12:02 +1100663static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
Al Virod89537e2013-03-31 13:24:44 -0400664{
665 int i, s;
666 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200667 printk("scsi%d: destination target %d, lun %llu\n",
Al Virod89537e2013-03-31 13:24:44 -0400668 H_NO(cmd), cmd->device->id, cmd->device->lun);
669 printk(KERN_CONT " command = ");
670 command = cmd->cmnd;
671 printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
672 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
673 printk(KERN_CONT " %02x", command[i]);
674 printk("\n");
675}
676
Roman Zippelc28bda22007-05-01 22:32:36 +0200677static void NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Al Virod89537e2013-03-31 13:24:44 -0400679 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100680 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400681 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Finn Thain8ad3a592014-03-18 11:42:19 +1100683 NCR5380_dprint(NDEBUG_ANY, instance);
684 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Roman Zippelc28bda22007-05-01 22:32:36 +0200686 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Roman Zippelc28bda22007-05-01 22:32:36 +0200688 local_irq_save(flags);
Al Virod89537e2013-03-31 13:24:44 -0400689 printk("NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100690 hostdata->main_running ? "" : "n't");
Roman Zippelc28bda22007-05-01 22:32:36 +0200691 if (!hostdata->connected)
Al Virod89537e2013-03-31 13:24:44 -0400692 printk("scsi%d: no currently connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200693 else
Finn Thain710ddd02014-11-12 16:12:02 +1100694 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
Al Virod89537e2013-03-31 13:24:44 -0400695 printk("scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100696 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400697 lprint_Scsi_Cmnd(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Al Virod89537e2013-03-31 13:24:44 -0400699 printk("scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100700 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400701 ptr = NEXT(ptr))
702 lprint_Scsi_Cmnd(ptr);
Roman Zippelc28bda22007-05-01 22:32:36 +0200703
704 local_irq_restore(flags);
Al Virod89537e2013-03-31 13:24:44 -0400705 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Finn Thain710ddd02014-11-12 16:12:02 +1100708static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Roman Zippelc28bda22007-05-01 22:32:36 +0200710 int i, s;
711 unsigned char *command;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200712 seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200713 H_NO(cmd), cmd->device->id, cmd->device->lun);
Al Virod89537e2013-03-31 13:24:44 -0400714 seq_printf(m, " command = ");
Roman Zippelc28bda22007-05-01 22:32:36 +0200715 command = cmd->cmnd;
Al Virod89537e2013-03-31 13:24:44 -0400716 seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +0200717 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Al Virod89537e2013-03-31 13:24:44 -0400718 seq_printf(m, " %02x", command[i]);
719 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Finn Thain4d3d2a52014-11-12 16:11:52 +1100722static int __maybe_unused NCR5380_show_info(struct seq_file *m,
723 struct Scsi_Host *instance)
Al Virod89537e2013-03-31 13:24:44 -0400724{
725 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100726 struct scsi_cmnd *ptr;
Al Virod89537e2013-03-31 13:24:44 -0400727 unsigned long flags;
728
729 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
730
Al Virod89537e2013-03-31 13:24:44 -0400731 local_irq_save(flags);
732 seq_printf(m, "NCR5380: coroutine is%s running.\n",
Finn Thaina53a21e2014-11-12 16:12:21 +1100733 hostdata->main_running ? "" : "n't");
Al Virod89537e2013-03-31 13:24:44 -0400734 if (!hostdata->connected)
735 seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
736 else
Finn Thain710ddd02014-11-12 16:12:02 +1100737 show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Al Virod89537e2013-03-31 13:24:44 -0400738 seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100739 for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
Al Virod89537e2013-03-31 13:24:44 -0400740 show_Scsi_Cmnd(ptr, m);
741
742 seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
Finn Thain710ddd02014-11-12 16:12:02 +1100743 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
Al Virod89537e2013-03-31 13:24:44 -0400744 ptr = NEXT(ptr))
745 show_Scsi_Cmnd(ptr, m);
746
747 local_irq_restore(flags);
748 return 0;
749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Finn Thainff50f9e2014-11-12 16:12:18 +1100751/**
752 * NCR5380_init - initialise an NCR5380
753 * @instance: adapter to configure
754 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100756 * Initializes *instance and corresponding 5380 chip,
757 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
759 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100760 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200761 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100762 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
764
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100765static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Roman Zippelc28bda22007-05-01 22:32:36 +0200767 int i;
768 SETUP_HOSTDATA(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Finn Thaina53a21e2014-11-12 16:12:21 +1100770 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200771 hostdata->aborted = 0;
772 hostdata->id_mask = 1 << instance->this_id;
773 hostdata->id_higher_mask = 0;
774 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
775 if (i > hostdata->id_mask)
776 hostdata->id_higher_mask |= i;
777 for (i = 0; i < 8; ++i)
778 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100780 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781#endif
782#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200783 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200785 hostdata->targets_present = 0;
786 hostdata->connected = NULL;
787 hostdata->issue_queue = NULL;
788 hostdata->disconnected_queue = NULL;
Finn Thainef1081c2014-11-12 16:12:14 +1100789 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Finn Thaina53a21e2014-11-12 16:12:21 +1100791 INIT_WORK(&hostdata->main_task, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Finn Thain8c325132014-11-12 16:11:58 +1100793 prepare_info(instance);
794
Roman Zippelc28bda22007-05-01 22:32:36 +0200795 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
796 NCR5380_write(MODE_REG, MR_BASE);
797 NCR5380_write(TARGET_COMMAND_REG, 0);
798 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Roman Zippelc28bda22007-05-01 22:32:36 +0200800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Finn Thainff50f9e2014-11-12 16:12:18 +1100803/**
804 * NCR5380_exit - remove an NCR5380
805 * @instance: adapter to remove
806 *
807 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
808 */
809
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200810static void NCR5380_exit(struct Scsi_Host *instance)
811{
Finn Thaina53a21e2014-11-12 16:12:21 +1100812 struct NCR5380_hostdata *hostdata = shost_priv(instance);
813
814 cancel_work_sync(&hostdata->main_task);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200815}
816
Finn Thainff50f9e2014-11-12 16:12:18 +1100817/**
818 * NCR5380_queue_command - queue a command
819 * @instance: the relevant SCSI adapter
820 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100822 * cmd is added to the per instance issue_queue, with minor
823 * twiddling done to the host specific fields of cmd. If the
824 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826
Finn Thain16b29e72014-11-12 16:12:08 +1100827static int NCR5380_queue_command(struct Scsi_Host *instance,
828 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Finn Thain16b29e72014-11-12 16:12:08 +1100830 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100831 struct scsi_cmnd *tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +0200832 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200835 switch (cmd->cmnd[0]) {
836 case WRITE_6:
837 case WRITE_10:
838 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
839 H_NO(cmd));
840 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100841 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200842 return 0;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
845
Roman Zippelc28bda22007-05-01 22:32:36 +0200846 /*
847 * We use the host_scribble field as a pointer to the next command
848 * in a queue
849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Roman Zippel3130d902007-05-01 22:32:37 +0200851 SET_NEXT(cmd, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +0200852 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Roman Zippelc28bda22007-05-01 22:32:36 +0200854 /*
855 * Insert the cmd into the issue queue. Note that REQUEST SENSE
856 * commands are added to the head of the queue since any command will
857 * clear the contingent allegiance condition that exists and the
858 * sense data is only guaranteed to be valid while the condition exists.
859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Roman Zippelc28bda22007-05-01 22:32:36 +0200861 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
862 * Otherwise a running NCR5380_main may steal the lock.
863 * Lock before actually inserting due to fairness reasons explained in
864 * atari_scsi.c. If we insert first, then it's impossible for this driver
865 * to release the lock.
866 * Stop timer for this command while waiting for the lock, or timeouts
867 * may happen (and they really do), and it's no good if the command doesn't
868 * appear in any of the queues.
869 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
870 * because also a timer int can trigger an abort or reset, which would
871 * alter queues and touch the lock.
872 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100873 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100874 return SCSI_MLQUEUE_HOST_BUSY;
875
876 local_irq_save(flags);
877
Finn Thainff50f9e2014-11-12 16:12:18 +1100878 /*
879 * Insert the cmd into the issue queue. Note that REQUEST SENSE
880 * commands are added to the head of the queue since any command will
881 * clear the contingent allegiance condition that exists and the
882 * sense data is only guaranteed to be valid while the condition exists.
883 */
884
Roman Zippelc28bda22007-05-01 22:32:36 +0200885 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
886 LIST(cmd, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +0200887 SET_NEXT(cmd, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +0200888 hostdata->issue_queue = cmd;
889 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100890 for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +0200891 NEXT(tmp); tmp = NEXT(tmp))
892 ;
893 LIST(cmd, tmp);
Roman Zippel3130d902007-05-01 22:32:37 +0200894 SET_NEXT(tmp, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200895 }
896 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Finn Thaind65e6342014-03-18 11:42:20 +1100898 dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
Roman Zippelc28bda22007-05-01 22:32:36 +0200899 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Roman Zippelc28bda22007-05-01 22:32:36 +0200901 /* If queue_command() is called from an interrupt (real one or bottom
902 * half), we let queue_main() do the job of taking care about main. If it
903 * is already running, this is a no-op, else main will be queued.
904 *
905 * If we're not in an interrupt, we can call NCR5380_main()
906 * unconditionally, because it cannot be already running.
907 */
Finn Thain16b29e72014-11-12 16:12:08 +1100908 if (in_interrupt() || irqs_disabled())
Finn Thaina53a21e2014-11-12 16:12:21 +1100909 queue_main(hostdata);
Roman Zippelc28bda22007-05-01 22:32:36 +0200910 else
Finn Thaina53a21e2014-11-12 16:12:21 +1100911 NCR5380_main(&hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Finn Thaine3c3da62014-11-12 16:12:15 +1100915static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
916{
917 struct NCR5380_hostdata *hostdata = shost_priv(instance);
918
919 /* Caller does the locking needed to set & test these data atomically */
920 if (!hostdata->disconnected_queue &&
921 !hostdata->issue_queue &&
922 !hostdata->connected &&
923 !hostdata->retain_dma_intr)
924 NCR5380_release_dma_irq(instance);
925}
926
Finn Thainff50f9e2014-11-12 16:12:18 +1100927/**
928 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100930 * NCR5380_main is a coroutine that runs as long as more work can
931 * be done on the NCR5380 host adapters in a system. Both
932 * NCR5380_queue_command() and NCR5380_intr() will try to start it
933 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200934 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100935 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200936 */
937
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200938static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Finn Thaina53a21e2014-11-12 16:12:21 +1100940 struct NCR5380_hostdata *hostdata =
941 container_of(work, struct NCR5380_hostdata, main_task);
942 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100943 struct scsi_cmnd *tmp, *prev;
Roman Zippelc28bda22007-05-01 22:32:36 +0200944 int done;
945 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Roman Zippelc28bda22007-05-01 22:32:36 +0200947 /*
948 * We run (with interrupts disabled) until we're sure that none of
949 * the host adapters have anything that can be done, at which point
950 * we set main_running to 0 and exit.
951 *
952 * Interrupts are enabled before doing various other internal
953 * instructions, after we've decided that we need to run through
954 * the loop again.
955 *
956 * this should prevent any race conditions.
957 *
958 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
959 * because also a timer int can trigger an abort or reset, which can
960 * alter queues and touch the Falcon lock.
961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Roman Zippelc28bda22007-05-01 22:32:36 +0200963 /* Tell int handlers main() is now already executing. Note that
964 no races are possible here. If an int comes in before
965 'main_running' is set here, and queues/executes main via the
966 task queue, it doesn't do any harm, just this instance of main
967 won't find any work left to do. */
Finn Thaina53a21e2014-11-12 16:12:21 +1100968 if (hostdata->main_running)
Roman Zippelc28bda22007-05-01 22:32:36 +0200969 return;
Finn Thaina53a21e2014-11-12 16:12:21 +1100970 hostdata->main_running = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +0200971
972 local_save_flags(flags);
973 do {
974 local_irq_disable(); /* Freeze request queues */
975 done = 1;
976
977 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +1100978 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200979 /*
980 * Search through the issue_queue for a command destined
981 * for a target that's not busy.
982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983#if (NDEBUG & NDEBUG_LISTS)
Finn Thain710ddd02014-11-12 16:12:02 +1100984 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +0200985 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
986 ;
987 /*printk("%p ", tmp);*/
988 if ((tmp == prev) && tmp)
989 printk(" LOOP\n");
990 /* else printk("\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
Finn Thain710ddd02014-11-12 16:12:02 +1100992 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
Roman Zippelc28bda22007-05-01 22:32:36 +0200993 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200994 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Finn Thaine3f463b2014-11-12 16:12:16 +1100996 dprintk(NDEBUG_LISTS,
997 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
998 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
999 lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001000 /* When we find one, remove it from the issue queue. */
1001 /* ++guenther: possible race with Falcon locking */
1002 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001004 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001006 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +02001007#endif
1008 ) {
1009 /* ++guenther: just to be sure, this must be atomic */
1010 local_irq_disable();
1011 if (prev) {
1012 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02001013 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02001014 } else {
1015 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1016 hostdata->issue_queue = NEXT(tmp);
1017 }
Roman Zippel3130d902007-05-01 22:32:37 +02001018 SET_NEXT(tmp, NULL);
Finn Thainef1081c2014-11-12 16:12:14 +11001019 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +02001020
1021 /* reenable interrupts after finding one */
1022 local_irq_restore(flags);
1023
1024 /*
1025 * Attempt to establish an I_T_L nexus here.
1026 * On success, instance->hostdata->connected is set.
1027 * On failure, we must add the command back to the
1028 * issue queue so we can keep trying.
1029 */
Finn Thaind65e6342014-03-18 11:42:20 +11001030 dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 "lun %d removed from issue_queue\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001032 HOSTNO, tmp->device->id, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02001033 /*
1034 * REQUEST SENSE commands are issued without tagged
1035 * queueing, even on SCSI-II devices because the
1036 * contingent allegiance condition exists for the
1037 * entire unit.
1038 */
1039 /* ++roman: ...and the standard also requires that
1040 * REQUEST SENSE command are untagged.
1041 */
1042
1043#ifdef SUPPORT_TAGS
1044 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1045#endif
Finn Thain76f13b92014-11-12 16:11:53 +11001046 if (!NCR5380_select(instance, tmp)) {
Finn Thaine3c3da62014-11-12 16:12:15 +11001047 local_irq_disable();
Finn Thainef1081c2014-11-12 16:12:14 +11001048 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001049 /* release if target did not response! */
Finn Thaine3c3da62014-11-12 16:12:15 +11001050 maybe_release_dma_irq(instance);
1051 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02001052 break;
1053 } else {
1054 local_irq_disable();
1055 LIST(tmp, hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02001056 SET_NEXT(tmp, hostdata->issue_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02001057 hostdata->issue_queue = tmp;
1058#ifdef SUPPORT_TAGS
1059 cmd_free_tag(tmp);
1060#endif
Finn Thainef1081c2014-11-12 16:12:14 +11001061 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02001062 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001063 dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001064 "returned to issue_queue\n", HOSTNO);
1065 if (hostdata->connected)
1066 break;
1067 }
1068 } /* if target/lun/target queue is not busy */
1069 } /* for issue_queue */
1070 } /* if (!hostdata->connected) */
1071
1072 if (hostdata->connected
1073#ifdef REAL_DMA
1074 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075#endif
1076 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11001078 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001079 HOSTNO);
1080 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001081 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001082 done = 0;
1083 }
1084 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Roman Zippelc28bda22007-05-01 22:32:36 +02001086 /* Better allow ints _after_ 'main_running' has been cleared, else
1087 an interrupt could believe we'll pick up the work it left for
1088 us, but we won't see it anymore here... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001089 hostdata->main_running = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001090 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093
1094#ifdef REAL_DMA
1095/*
1096 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1097 *
1098 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001099 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
1101 * Inputs : instance - this instance of the NCR5380.
1102 *
1103 */
1104
Roman Zippelc28bda22007-05-01 22:32:36 +02001105static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Roman Zippelc28bda22007-05-01 22:32:36 +02001107 SETUP_HOSTDATA(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001108 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001109 unsigned char **data;
Roman Zippelc28bda22007-05-01 22:32:36 +02001110 volatile int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001111 int saved_data = 0, overrun = 0;
1112 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Roman Zippelc28bda22007-05-01 22:32:36 +02001114 if (!hostdata->connected) {
1115 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1116 "no connected cmd\n", HOSTNO);
1117 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Finn Thainef1081c2014-11-12 16:12:14 +11001120 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001121 p = hostdata->connected->SCp.phase;
1122 if (p & SR_IO) {
1123 udelay(10);
1124 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1125 (BASR_PHASE_MATCH|BASR_ACK)) ==
1126 (BASR_PHASE_MATCH|BASR_ACK)) {
1127 saved_data = NCR5380_read(INPUT_DATA_REG);
1128 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001129 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001130 }
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001133
Finn Thaind65e6342014-03-18 11:42:20 +11001134 dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001135 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1136 NCR5380_read(STATUS_REG));
1137
Finn Thaine3f463b2014-11-12 16:12:16 +11001138#if defined(CONFIG_SUN3)
1139 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1140 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1141 instance->host_no);
1142 BUG();
1143 }
1144
1145 /* make sure we're not stuck in a data phase */
1146 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1147 (BASR_PHASE_MATCH | BASR_ACK)) {
1148 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1149 NCR5380_read(BUS_AND_STATUS_REG));
1150 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1151 instance->host_no);
1152 BUG();
1153 }
1154#endif
1155
Roman Zippelc28bda22007-05-01 22:32:36 +02001156 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1157 NCR5380_write(MODE_REG, MR_BASE);
1158 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1159
Finn Thain01bd9082014-11-12 16:12:23 +11001160 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001161 hostdata->dma_len = 0;
1162
1163 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1164 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001165 *data += transferred;
1166 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001167
Finn Thainef1081c2014-11-12 16:12:14 +11001168 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001169 int cnt, toPIO;
1170
Roman Zippelc28bda22007-05-01 22:32:36 +02001171 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001172 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001173 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001174 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001175 *(*data)++ = saved_data;
1176 (*count)--;
1177 cnt--;
1178 toPIO--;
1179 }
Finn Thaind65e6342014-03-18 11:42:20 +11001180 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001181 NCR5380_transfer_pio(instance, &p, &cnt, data);
1182 *count -= toPIO - cnt;
1183 }
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186#endif /* REAL_DMA */
1187
1188
Finn Thainff50f9e2014-11-12 16:12:18 +11001189/**
1190 * NCR5380_intr - generic NCR5380 irq handler
1191 * @irq: interrupt number
1192 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001193 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001194 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1195 * from the disconnected queue, and restarting NCR5380_main()
1196 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
1198
Roman Zippelc28bda22007-05-01 22:32:36 +02001199static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Finn Thaina53a21e2014-11-12 16:12:21 +11001201 struct Scsi_Host *instance = dev_id;
Roman Zippelc28bda22007-05-01 22:32:36 +02001202 int done = 1, handled = 0;
1203 unsigned char basr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Finn Thaind65e6342014-03-18 11:42:20 +11001205 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Roman Zippelc28bda22007-05-01 22:32:36 +02001207 /* Look for pending interrupts */
1208 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thaind65e6342014-03-18 11:42:20 +11001209 dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001210 /* dispatch to appropriate routine if found and done=0 */
1211 if (basr & BASR_IRQ) {
Finn Thain8ad3a592014-03-18 11:42:19 +11001212 NCR5380_dprint(NDEBUG_INTR, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001213 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1214 done = 0;
Finn Thaind65e6342014-03-18 11:42:20 +11001215 dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001216 NCR5380_reselect(instance);
1217 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1218 } else if (basr & BASR_PARITY_ERROR) {
Finn Thaind65e6342014-03-18 11:42:20 +11001219 dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001220 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1221 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thaind65e6342014-03-18 11:42:20 +11001222 dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001223 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1224 } else {
1225 /*
1226 * The rest of the interrupt conditions can occur only during a
1227 * DMA transfer
1228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001231 /*
1232 * We should only get PHASE MISMATCH and EOP interrupts if we have
1233 * DMA enabled, so do a sanity check based on the current setting
1234 * of the MODE register.
1235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Roman Zippelc28bda22007-05-01 22:32:36 +02001237 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1238 ((basr & BASR_END_DMA_TRANSFER) ||
1239 !(basr & BASR_PHASE_MATCH))) {
1240
Finn Thaind65e6342014-03-18 11:42:20 +11001241 dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001242 NCR5380_dma_complete( instance );
1243 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001244 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +02001246 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001248 if (basr & BASR_PHASE_MATCH)
Finn Thaine3f463b2014-11-12 16:12:16 +11001249 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
Roman Zippelc28bda22007-05-01 22:32:36 +02001250 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1251 HOSTNO, basr, NCR5380_read(MODE_REG),
1252 NCR5380_read(STATUS_REG));
1253 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001254#ifdef SUN3_SCSI_VME
1255 dregs->csr |= CSR_DMA_ENABLE;
1256#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001257 }
1258 } /* if !(SELECTION || PARITY) */
1259 handled = 1;
1260 } /* BASR & IRQ */ else {
1261 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1262 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1263 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1264 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Finn Thaine3f463b2014-11-12 16:12:16 +11001265#ifdef SUN3_SCSI_VME
1266 dregs->csr |= CSR_DMA_ENABLE;
1267#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 }
1269
1270 if (!done) {
Finn Thaind65e6342014-03-18 11:42:20 +11001271 dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001272 /* Put a call to NCR5380_main() on the queue... */
Finn Thaina53a21e2014-11-12 16:12:21 +11001273 queue_main(shost_priv(instance));
Roman Zippelc28bda22007-05-01 22:32:36 +02001274 }
1275 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Roman Zippelc28bda22007-05-01 22:32:36 +02001278/*
Finn Thain710ddd02014-11-12 16:12:02 +11001279 * Function : int NCR5380_select(struct Scsi_Host *instance,
1280 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 *
1282 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001283 * including ARBITRATION, SELECTION, and initial message out for
1284 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001286 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001287 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001289 * Returns : -1 if selection could not execute for some reason,
1290 * 0 if selection succeeded or failed because the target
1291 * did not respond.
1292 *
1293 * Side effects :
1294 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * with registers as they should have been on entry - ie
1296 * SELECT_ENABLE will be set appropriately, the NCR5380
1297 * will cease to drive any SCSI bus signals.
1298 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001299 * If successful : I_T_L or I_T_L_Q nexus will be established,
1300 * instance->connected will be set to cmd.
1301 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001303 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * cmd->result host byte set to DID_BAD_TARGET.
1305 */
1306
Finn Thain710ddd02014-11-12 16:12:02 +11001307static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Roman Zippelc28bda22007-05-01 22:32:36 +02001309 SETUP_HOSTDATA(instance);
1310 unsigned char tmp[3], phase;
1311 unsigned char *data;
1312 int len;
1313 unsigned long timeout;
1314 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Roman Zippelc28bda22007-05-01 22:32:36 +02001316 hostdata->restart_select = 0;
Finn Thain8ad3a592014-03-18 11:42:19 +11001317 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001318 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001319 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Roman Zippelc28bda22007-05-01 22:32:36 +02001321 /*
1322 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1323 * data bus during SELECTION.
1324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Roman Zippelc28bda22007-05-01 22:32:36 +02001326 local_irq_save(flags);
1327 if (hostdata->connected) {
1328 local_irq_restore(flags);
1329 return -1;
1330 }
1331 NCR5380_write(TARGET_COMMAND_REG, 0);
1332
1333 /*
1334 * Start arbitration.
1335 */
1336
1337 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1338 NCR5380_write(MODE_REG, MR_ARBITRATE);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Roman Zippelc28bda22007-05-01 22:32:36 +02001342 /* Wait for arbitration logic to complete */
Michael Schmitzfb810d12007-05-01 22:32:35 +02001343#if defined(NCR_TIMEOUT)
Roman Zippelc28bda22007-05-01 22:32:36 +02001344 {
1345 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Roman Zippelc28bda22007-05-01 22:32:36 +02001347 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1348 time_before(jiffies, timeout) && !hostdata->connected)
1349 ;
1350 if (time_after_eq(jiffies, timeout)) {
1351 printk("scsi : arbitration timeout at %d\n", __LINE__);
1352 NCR5380_write(MODE_REG, MR_BASE);
1353 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1354 return -1;
1355 }
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357#else /* NCR_TIMEOUT */
Roman Zippelc28bda22007-05-01 22:32:36 +02001358 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1359 !hostdata->connected)
1360 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361#endif
1362
Finn Thaind65e6342014-03-18 11:42:20 +11001363 dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Roman Zippelc28bda22007-05-01 22:32:36 +02001365 if (hostdata->connected) {
1366 NCR5380_write(MODE_REG, MR_BASE);
1367 return -1;
1368 }
1369 /*
1370 * The arbitration delay is 2.2us, but this is a minimum and there is
1371 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1372 * the integral nature of udelay().
1373 *
1374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Roman Zippelc28bda22007-05-01 22:32:36 +02001376 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Roman Zippelc28bda22007-05-01 22:32:36 +02001378 /* Check for lost arbitration */
1379 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1380 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1381 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1382 hostdata->connected) {
1383 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001384 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001385 HOSTNO);
1386 return -1;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Roman Zippelc28bda22007-05-01 22:32:36 +02001389 /* after/during arbitration, BSY should be asserted.
1390 IBM DPES-31080 Version S31Q works now */
1391 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1392 NCR5380_write(INITIATOR_COMMAND_REG,
1393 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Roman Zippelc28bda22007-05-01 22:32:36 +02001395 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1396 hostdata->connected) {
1397 NCR5380_write(MODE_REG, MR_BASE);
1398 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001399 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001400 HOSTNO);
1401 return -1;
1402 }
1403
1404 /*
1405 * Again, bus clear + bus settle time is 1.2us, however, this is
1406 * a minimum so we'll udelay ceil(1.2)
1407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
Roman Zippelc28bda22007-05-01 22:32:36 +02001410 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1411 udelay(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001413 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001415
1416 if (hostdata->connected) {
1417 NCR5380_write(MODE_REG, MR_BASE);
1418 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1419 return -1;
1420 }
1421
Finn Thaind65e6342014-03-18 11:42:20 +11001422 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001423
1424 /*
1425 * Now that we have won arbitration, start Selection process, asserting
1426 * the host and target ID's on the SCSI bus.
1427 */
1428
1429 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1430
1431 /*
1432 * Raise ATN while SEL is true before BSY goes false from arbitration,
1433 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1434 * phase immediately after selection.
1435 */
1436
1437 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1438 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Roman Zippelc28bda22007-05-01 22:32:36 +02001441 /*
1442 * Reselect interrupts must be turned off prior to the dropping of BSY,
1443 * otherwise we will trigger an interrupt.
1444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Roman Zippelc28bda22007-05-01 22:32:36 +02001446 if (hostdata->connected) {
1447 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1448 return -1;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Roman Zippelc28bda22007-05-01 22:32:36 +02001451 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Roman Zippelc28bda22007-05-01 22:32:36 +02001453 /*
1454 * The initiator shall then wait at least two deskew delays and release
1455 * the BSY signal.
1456 */
1457 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Roman Zippelc28bda22007-05-01 22:32:36 +02001459 /* Reset BSY */
1460 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1461 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Roman Zippelc28bda22007-05-01 22:32:36 +02001463 /*
1464 * Something weird happens when we cease to drive BSY - looks
1465 * like the board/chip is letting us do another read before the
1466 * appropriate propagation delay has expired, and we're confusing
1467 * a BSY signal from ourselves as the target's response to SELECTION.
1468 *
1469 * A small delay (the 'C++' frontend breaks the pipeline with an
1470 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1471 * tighter 'C' code breaks and requires this) solves the problem -
1472 * the 1 us delay is arbitrary, and only used because this delay will
1473 * be the same on other platforms and since it works here, it should
1474 * work there.
1475 *
1476 * wingel suggests that this could be due to failing to wait
1477 * one deskew delay.
1478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Roman Zippelc28bda22007-05-01 22:32:36 +02001480 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Finn Thaind65e6342014-03-18 11:42:20 +11001482 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Roman Zippelc28bda22007-05-01 22:32:36 +02001484 /*
1485 * The SCSI specification calls for a 250 ms timeout for the actual
1486 * selection.
1487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Finn Thainff50f9e2014-11-12 16:12:18 +11001489 timeout = jiffies + (250 * HZ / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Roman Zippelc28bda22007-05-01 22:32:36 +02001491 /*
1492 * XXX very interesting - we're seeing a bounce where the BSY we
1493 * asserted is being reflected / still asserted (propagation delay?)
1494 * and it's detecting as true. Sigh.
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497#if 0
Roman Zippelc28bda22007-05-01 22:32:36 +02001498 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1499 * IO while SEL is true. But again, there are some disks out the in the
1500 * world that do that nevertheless. (Somebody claimed that this announces
1501 * reselection capability of the target.) So we better skip that test and
1502 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Roman Zippelc28bda22007-05-01 22:32:36 +02001505 while (time_before(jiffies, timeout) &&
1506 !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
1507 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Roman Zippelc28bda22007-05-01 22:32:36 +02001509 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1510 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1511 NCR5380_reselect(instance);
1512 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1513 HOSTNO);
1514 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1515 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001518 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
1519 ;
1520#endif
1521
1522 /*
1523 * No less than two deskew delays after the initiator detects the
1524 * BSY signal is true, it shall release the SEL signal and may
1525 * change the DATA BUS. -wingel
1526 */
1527
1528 udelay(1);
1529
1530 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1531
1532 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1533 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1534 if (hostdata->targets_present & (1 << cmd->device->id)) {
1535 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1536 if (hostdata->restart_select)
1537 printk(KERN_NOTICE "\trestart select\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001538 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001539 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1540 return -1;
1541 }
1542 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001543#ifdef SUPPORT_TAGS
1544 cmd_free_tag(cmd);
1545#endif
1546 cmd->scsi_done(cmd);
1547 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001548 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001549 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1550 return 0;
1551 }
1552
1553 hostdata->targets_present |= (1 << cmd->device->id);
1554
1555 /*
1556 * Since we followed the SCSI spec, and raised ATN while SEL
1557 * was true but before BSY was false during selection, the information
1558 * transfer phase should be a MESSAGE OUT phase so that we can send the
1559 * IDENTIFY message.
1560 *
1561 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1562 * message (2 bytes) with a tag ID that we increment with every command
1563 * until it wraps back to 0.
1564 *
1565 * XXX - it turns out that there are some broken SCSI-II devices,
1566 * which claim to support tagged queuing but fail when more than
1567 * some number of commands are issued at once.
1568 */
1569
1570 /* Wait for start of REQ/ACK handshake */
1571 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
1572 ;
1573
Finn Thaind65e6342014-03-18 11:42:20 +11001574 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001575 HOSTNO, cmd->device->id);
1576 tmp[0] = IDENTIFY(1, cmd->device->lun);
1577
1578#ifdef SUPPORT_TAGS
1579 if (cmd->tag != TAG_NONE) {
1580 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1581 tmp[2] = cmd->tag;
1582 len = 3;
1583 } else
1584 len = 1;
1585#else
1586 len = 1;
1587 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588#endif /* SUPPORT_TAGS */
1589
Roman Zippelc28bda22007-05-01 22:32:36 +02001590 /* Send message(s) */
1591 data = tmp;
1592 phase = PHASE_MSGOUT;
1593 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001594 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001595 /* XXX need to handle errors here */
1596 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001598 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1599#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001600#ifdef SUN3_SCSI_VME
1601 dregs->csr |= CSR_INTR;
1602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Roman Zippelc28bda22007-05-01 22:32:36 +02001604 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Roman Zippelc28bda22007-05-01 22:32:36 +02001606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Roman Zippelc28bda22007-05-01 22:32:36 +02001609/*
1610 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 * unsigned char *phase, int *count, unsigned char **data)
1612 *
1613 * Purpose : transfers data in given phase using polled I/O
1614 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001615 * Inputs : instance - instance of driver, *phase - pointer to
1616 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001618 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001620 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * is in same phase.
1622 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001623 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 *
1625 * XXX Note : handling for bus free may be useful.
1626 */
1627
1628/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001629 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * IS 100% reliable, and for the actual data transfer where speed
1631 * counts, we will always do a pseudo DMA or DMA transfer.
1632 */
1633
Roman Zippelc28bda22007-05-01 22:32:36 +02001634static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1635 unsigned char *phase, int *count,
1636 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Roman Zippelc28bda22007-05-01 22:32:36 +02001638 register unsigned char p = *phase, tmp;
1639 register int c = *count;
1640 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Roman Zippelc28bda22007-05-01 22:32:36 +02001642 /*
1643 * The NCR5380 chip will only drive the SCSI bus when the
1644 * phase specified in the appropriate bits of the TARGET COMMAND
1645 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
1647
Roman Zippelc28bda22007-05-01 22:32:36 +02001648 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Roman Zippelc28bda22007-05-01 22:32:36 +02001650 do {
1651 /*
1652 * Wait for assertion of REQ, after which the phase bits will be
1653 * valid
1654 */
1655 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
1656 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Finn Thaind65e6342014-03-18 11:42:20 +11001658 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Roman Zippelc28bda22007-05-01 22:32:36 +02001660 /* Check for phase mismatch */
1661 if ((tmp & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001662 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001663 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001664 break;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Roman Zippelc28bda22007-05-01 22:32:36 +02001667 /* Do actual transfer from SCSI bus to / from memory */
1668 if (!(p & SR_IO))
1669 NCR5380_write(OUTPUT_DATA_REG, *d);
1670 else
1671 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Roman Zippelc28bda22007-05-01 22:32:36 +02001673 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Roman Zippelc28bda22007-05-01 22:32:36 +02001675 /*
1676 * The SCSI standard suggests that in MSGOUT phase, the initiator
1677 * should drop ATN on the last byte of the message phase
1678 * after REQ has been asserted for the handshake but before
1679 * the initiator raises ACK.
1680 */
1681
1682 if (!(p & SR_IO)) {
1683 if (!((p & SR_MSG) && c > 1)) {
1684 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001685 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001686 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1687 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1688 } else {
1689 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1690 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001691 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001692 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1693 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1694 }
1695 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001696 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001697 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1698 }
1699
1700 while (NCR5380_read(STATUS_REG) & SR_REQ)
1701 ;
1702
Finn Thaind65e6342014-03-18 11:42:20 +11001703 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001704
1705 /*
1706 * We have several special cases to consider during REQ/ACK handshaking :
1707 * 1. We were in MSGOUT phase, and we are on the last byte of the
1708 * message. ATN must be dropped as ACK is dropped.
1709 *
1710 * 2. We are in a MSGIN phase, and we are on the last byte of the
1711 * message. We must exit with ACK asserted, so that the calling
1712 * code may raise ATN before dropping ACK to reject the message.
1713 *
1714 * 3. ACK and ATN are clear and the target may proceed as normal.
1715 */
1716 if (!(p == PHASE_MSGIN && c == 1)) {
1717 if (p == PHASE_MSGOUT && c > 1)
1718 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1719 else
1720 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1721 }
1722 } while (--c);
1723
Finn Thaind65e6342014-03-18 11:42:20 +11001724 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001725
1726 *count = c;
1727 *data = d;
1728 tmp = NCR5380_read(STATUS_REG);
1729 /* The phase read from the bus is valid if either REQ is (already)
1730 * asserted or if ACK hasn't been released yet. The latter is the case if
1731 * we're in MSGIN and all wanted bytes have been received.
1732 */
1733 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1734 *phase = tmp & PHASE_MASK;
1735 else
1736 *phase = PHASE_UNKNOWN;
1737
1738 if (!c || (*phase == p))
1739 return 0;
1740 else
1741 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
1744/*
1745 * Function : do_abort (Scsi_Host *host)
Roman Zippelc28bda22007-05-01 22:32:36 +02001746 *
1747 * Purpose : abort the currently established nexus. Should only be
1748 * called from a routine which can drop into a
1749 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * Returns : 0 on success, -1 on failure.
1751 */
1752
Finn Thaina53a21e2014-11-12 16:12:21 +11001753static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Roman Zippelc28bda22007-05-01 22:32:36 +02001755 unsigned char tmp, *msgptr, phase;
1756 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Roman Zippelc28bda22007-05-01 22:32:36 +02001758 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Roman Zippelc28bda22007-05-01 22:32:36 +02001761 /*
1762 * Wait for the target to indicate a valid phase by asserting
1763 * REQ. Once this happens, we'll have either a MSGOUT phase
1764 * and can immediately send the ABORT message, or we'll have some
1765 * other phase and will have to source/sink data.
1766 *
1767 * We really don't care what value was on the bus or what value
1768 * the target sees, so we just handshake.
1769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Roel Kluin3be38e72007-11-15 21:13:46 +01001771 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
Roman Zippelc28bda22007-05-01 22:32:36 +02001772 ;
1773
1774 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1775
1776 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1777 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1778 ICR_ASSERT_ACK);
1779 while (NCR5380_read(STATUS_REG) & SR_REQ)
1780 ;
1781 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1782 }
1783
1784 tmp = ABORT;
1785 msgptr = &tmp;
1786 len = 1;
1787 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001788 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001789
1790 /*
1791 * If we got here, and the command completed successfully,
1792 * we're about to go into bus free state.
1793 */
1794
1795 return len ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001799/*
1800 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 * unsigned char *phase, int *count, unsigned char **data)
1802 *
1803 * Purpose : transfers data in given phase using either real
1804 * or pseudo DMA.
1805 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001806 * Inputs : instance - instance of driver, *phase - pointer to
1807 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001809 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001811 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * is in same phase.
1813 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001814 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 *
1816 */
1817
1818
Roman Zippelc28bda22007-05-01 22:32:36 +02001819static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1820 unsigned char *phase, int *count,
1821 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Roman Zippelc28bda22007-05-01 22:32:36 +02001823 SETUP_HOSTDATA(instance);
1824 register int c = *count;
1825 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001826 unsigned long flags;
1827
1828#if defined(CONFIG_SUN3)
1829 /* sanity check */
1830 if (!sun3_dma_setup_done) {
1831 pr_err("scsi%d: transfer_dma without setup!\n",
1832 instance->host_no);
1833 BUG();
1834 }
1835 hostdata->dma_len = c;
1836
1837 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1838 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1839 c, (p & SR_IO) ? "to" : "from", *data);
1840
1841 /* netbsd turns off ints here, why not be safe and do it too */
1842 local_irq_save(flags);
1843
1844 /* send start chain */
1845 sun3scsi_dma_start(c, *data);
1846
1847 if (p & SR_IO) {
1848 NCR5380_write(TARGET_COMMAND_REG, 1);
1849 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1850 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1851 NCR5380_write(MODE_REG,
1852 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1853 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1854 } else {
1855 NCR5380_write(TARGET_COMMAND_REG, 0);
1856 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1857 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1858 NCR5380_write(MODE_REG,
1859 (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
1860 NCR5380_write(START_DMA_SEND_REG, 0);
1861 }
1862
1863#ifdef SUN3_SCSI_VME
1864 dregs->csr |= CSR_DMA_ENABLE;
1865#endif
1866
1867 local_irq_restore(flags);
1868
1869 sun3_dma_active = 1;
1870
1871#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001872 register unsigned char *d = *data;
1873 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Roman Zippelc28bda22007-05-01 22:32:36 +02001875 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1876 *phase = tmp;
1877 return -1;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Finn Thainef1081c2014-11-12 16:12:14 +11001880 if (hostdata->read_overruns && (p & SR_IO))
1881 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Finn Thaind65e6342014-03-18 11:42:20 +11001883 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001884 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1885 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Roman Zippelc28bda22007-05-01 22:32:36 +02001887 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02001890 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891#endif /* def REAL_DMA */
1892
Finn Thainef1081c2014-11-12 16:12:14 +11001893 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001894 /* On the Medusa, it is a must to initialize the DMA before
1895 * starting the NCR. This is also the cleaner way for the TT.
1896 */
1897 local_irq_save(flags);
1898 hostdata->dma_len = (p & SR_IO) ?
1899 NCR5380_dma_read_setup(instance, d, c) :
1900 NCR5380_dma_write_setup(instance, d, c);
1901 local_irq_restore(flags);
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Roman Zippelc28bda22007-05-01 22:32:36 +02001904 if (p & SR_IO)
1905 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1906 else {
1907 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1908 NCR5380_write(START_DMA_SEND_REG, 0);
1909 }
1910
Finn Thainef1081c2014-11-12 16:12:14 +11001911 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001912 /* On the Falcon, the DMA setup must be done after the last */
1913 /* NCR access, else the DMA setup gets trashed!
1914 */
1915 local_irq_save(flags);
1916 hostdata->dma_len = (p & SR_IO) ?
1917 NCR5380_dma_read_setup(instance, d, c) :
1918 NCR5380_dma_write_setup(instance, d, c);
1919 local_irq_restore(flags);
1920 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001921#endif /* !defined(CONFIG_SUN3) */
1922
Roman Zippelc28bda22007-05-01 22:32:36 +02001923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925#endif /* defined(REAL_DMA) */
1926
1927/*
1928 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1929 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001930 * Purpose : run through the various SCSI phases and do as the target
1931 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * instance->connected.
1933 *
1934 * Inputs : instance, instance for which we are doing commands
1935 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001936 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * modified if a command disconnects, *instance->connected will
1938 * change.
1939 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001940 * XXX Note : we need to watch for bus free or a reset condition here
1941 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001943
1944static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Roman Zippelc28bda22007-05-01 22:32:36 +02001946 SETUP_HOSTDATA(instance);
1947 unsigned long flags;
1948 unsigned char msgout = NOP;
1949 int sink = 0;
1950 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001952 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001954 unsigned char *data;
1955 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001956 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Finn Thaine3f463b2014-11-12 16:12:16 +11001958#ifdef SUN3_SCSI_VME
1959 dregs->csr |= CSR_INTR;
1960#endif
1961
Roman Zippelc28bda22007-05-01 22:32:36 +02001962 while (1) {
1963 tmp = NCR5380_read(STATUS_REG);
1964 /* We only have a valid SCSI phase when REQ is asserted */
1965 if (tmp & SR_REQ) {
1966 phase = (tmp & PHASE_MASK);
1967 if (phase != old_phase) {
1968 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001969 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001971#if defined(CONFIG_SUN3)
1972 if (phase == PHASE_CMDOUT) {
1973#if defined(REAL_DMA)
1974 void *d;
1975 unsigned long count;
1976
1977 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1978 count = cmd->SCp.buffer->length;
1979 d = sg_virt(cmd->SCp.buffer);
1980 } else {
1981 count = cmd->SCp.this_residual;
1982 d = cmd->SCp.ptr;
1983 }
1984 /* this command setup for dma yet? */
1985 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
1986 if (cmd->request->cmd_type == REQ_TYPE_FS) {
1987 sun3scsi_dma_setup(d, count,
1988 rq_data_dir(cmd->request));
1989 sun3_dma_setup_done = cmd;
1990 }
1991 }
1992#endif
1993#ifdef SUN3_SCSI_VME
1994 dregs->csr |= CSR_INTR;
1995#endif
1996 }
1997#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Roman Zippelc28bda22007-05-01 22:32:36 +02001999 if (sink && (phase != PHASE_MSGOUT)) {
2000 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Roman Zippelc28bda22007-05-01 22:32:36 +02002002 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2003 ICR_ASSERT_ACK);
2004 while (NCR5380_read(STATUS_REG) & SR_REQ)
2005 ;
2006 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2007 ICR_ASSERT_ATN);
2008 sink = 0;
2009 continue;
2010 }
2011
2012 switch (phase) {
2013 case PHASE_DATAOUT:
2014#if (NDEBUG & NDEBUG_NO_DATAOUT)
2015 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2016 "aborted\n", HOSTNO);
2017 sink = 1;
2018 do_abort(instance);
2019 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002020 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002021 return;
2022#endif
2023 case PHASE_DATAIN:
2024 /*
2025 * If there is no room left in the current buffer in the
2026 * scatter-gather list, move onto the next one.
2027 */
2028
2029 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2030 ++cmd->SCp.buffer;
2031 --cmd->SCp.buffers_residual;
2032 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002033 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02002034 /* ++roman: Try to merge some scatter-buffers if
2035 * they are at contiguous physical addresses.
2036 */
2037 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11002038 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002039 HOSTNO, cmd->SCp.this_residual,
2040 cmd->SCp.buffers_residual);
2041 }
2042
2043 /*
2044 * The preferred transfer method is going to be
2045 * PSEUDO-DMA for systems that are strictly PIO,
2046 * since we can let the hardware do the handshaking.
2047 *
2048 * For this to work, we need to know the transfersize
2049 * ahead of time, since the pseudo-DMA code will sit
2050 * in an unconditional loop.
2051 */
2052
2053 /* ++roman: I suggest, this should be
2054 * #if def(REAL_DMA)
2055 * instead of leaving REAL_DMA out.
2056 */
2057
2058#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11002059 if (
2060#if !defined(CONFIG_SUN3)
2061 !cmd->device->borken &&
2062#endif
2063 (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002064 len = transfersize;
2065 cmd->SCp.phase = phase;
2066 if (NCR5380_transfer_dma(instance, &phase,
2067 &len, (unsigned char **)&cmd->SCp.ptr)) {
2068 /*
2069 * If the watchdog timer fires, all future
2070 * accesses to this device will use the
2071 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002072 scmd_printk(KERN_INFO, cmd,
2073 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002074 cmd->device->borken = 1;
2075 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2076 ICR_ASSERT_ATN);
2077 sink = 1;
2078 do_abort(instance);
2079 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002080 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002081 /* XXX - need to source or sink data here, as appropriate */
2082 } else {
2083#ifdef REAL_DMA
2084 /* ++roman: When using real DMA,
2085 * information_transfer() should return after
2086 * starting DMA since it has nothing more to
2087 * do.
2088 */
2089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002091 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002093 }
2094 } else
2095#endif /* defined(REAL_DMA) */
2096 NCR5380_transfer_pio(instance, &phase,
2097 (int *)&cmd->SCp.this_residual,
2098 (unsigned char **)&cmd->SCp.ptr);
Finn Thaine3f463b2014-11-12 16:12:16 +11002099#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2100 /* if we had intended to dma that command clear it */
2101 if (sun3_dma_setup_done == cmd)
2102 sun3_dma_setup_done = NULL;
2103#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002104 break;
2105 case PHASE_MSGIN:
2106 len = 1;
2107 data = &tmp;
2108 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2109 NCR5380_transfer_pio(instance, &phase, &len, &data);
2110 cmd->SCp.Message = tmp;
2111
2112 switch (tmp) {
2113 /*
2114 * Linking lets us reduce the time required to get the
2115 * next command out to the device, hopefully this will
2116 * mean we don't waste another revolution due to the delays
2117 * required by ARBITRATION and another SELECTION.
2118 *
2119 * In the current implementation proposal, low level drivers
2120 * merely have to start the next command, pointed to by
2121 * next_link, done() is called as with unlinked commands.
2122 */
2123#ifdef LINKED
2124 case LINKED_CMD_COMPLETE:
2125 case LINKED_FLG_CMD_COMPLETE:
2126 /* Accept message by clearing ACK */
2127 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2128
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002129 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
Roman Zippelc28bda22007-05-01 22:32:36 +02002130 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2131
2132 /* Enable reselect interrupts */
2133 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2134 /*
2135 * Sanity check : A linked command should only terminate
2136 * with one of these messages if there are more linked
2137 * commands available.
2138 */
2139
2140 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002141 printk(KERN_NOTICE "scsi%d: target %d lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002142 "linked command complete, no next_link\n",
2143 HOSTNO, cmd->device->id, cmd->device->lun);
2144 sink = 1;
2145 do_abort(instance);
2146 return;
2147 }
2148
2149 initialize_SCp(cmd->next_link);
2150 /* The next command is still part of this process; copy it
2151 * and don't free it! */
2152 cmd->next_link->tag = cmd->tag;
2153 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002154 dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
Roman Zippelc28bda22007-05-01 22:32:36 +02002155 "done, calling scsi_done().\n",
2156 HOSTNO, cmd->device->id, cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002157 cmd->scsi_done(cmd);
2158 cmd = hostdata->connected;
2159 break;
2160#endif /* def LINKED */
2161 case ABORT:
2162 case COMMAND_COMPLETE:
2163 /* Accept message by clearing ACK */
2164 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002165 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
Roman Zippelc28bda22007-05-01 22:32:36 +02002166 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002167
2168 local_irq_save(flags);
2169 hostdata->retain_dma_intr++;
2170 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002171#ifdef SUPPORT_TAGS
2172 cmd_free_tag(cmd);
2173 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2174 /* Turn a QUEUE FULL status into BUSY, I think the
2175 * mid level cannot handle QUEUE FULL :-( (The
2176 * command is retried after BUSY). Also update our
2177 * queue size to the number of currently issued
2178 * commands now.
2179 */
2180 /* ++Andreas: the mid level code knows about
2181 QUEUE_FULL now. */
Finn Thain61d739a2014-11-12 16:12:20 +11002182 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002183 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002184 "QUEUE_FULL after %d commands\n",
2185 HOSTNO, cmd->device->id, cmd->device->lun,
2186 ta->nr_allocated);
2187 if (ta->queue_size > ta->nr_allocated)
2188 ta->nr_allocated = ta->queue_size;
2189 }
2190#else
2191 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2192#endif
2193 /* Enable reselect interrupts */
2194 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2195
2196 /*
2197 * I'm not sure what the correct thing to do here is :
2198 *
2199 * If the command that just executed is NOT a request
2200 * sense, the obvious thing to do is to set the result
2201 * code to the values of the stored parameters.
2202 *
2203 * If it was a REQUEST SENSE command, we need some way to
2204 * differentiate between the failure code of the original
2205 * and the failure code of the REQUEST sense - the obvious
2206 * case is success, where we fall through and leave the
2207 * result code unchanged.
2208 *
2209 * The non-obvious place is where the REQUEST SENSE failed
2210 */
2211
2212 if (cmd->cmnd[0] != REQUEST_SENSE)
2213 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2214 else if (status_byte(cmd->SCp.Status) != GOOD)
2215 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2216
Boaz Harrosh28424d32007-09-10 22:37:45 +03002217 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2218 hostdata->ses.cmd_len) {
2219 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2220 hostdata->ses.cmd_len = 0 ;
2221 }
2222
Roman Zippelc28bda22007-05-01 22:32:36 +02002223 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2224 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002225 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002226
Finn Thaind65e6342014-03-18 11:42:20 +11002227 dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002228
Roman Zippelc28bda22007-05-01 22:32:36 +02002229 LIST(cmd,hostdata->issue_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002230 SET_NEXT(cmd, hostdata->issue_queue);
Finn Thain710ddd02014-11-12 16:12:02 +11002231 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thaind65e6342014-03-18 11:42:20 +11002232 dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
Roman Zippelc28bda22007-05-01 22:32:36 +02002233 "issue queue\n", H_NO(cmd));
Finn Thain997acab2014-11-12 16:11:54 +11002234 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002235 cmd->scsi_done(cmd);
2236 }
2237
Finn Thaine3c3da62014-11-12 16:12:15 +11002238 local_irq_restore(flags);
2239
Roman Zippelc28bda22007-05-01 22:32:36 +02002240 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2241 /*
2242 * Restore phase bits to 0 so an interrupted selection,
2243 * arbitration can resume.
2244 */
2245 NCR5380_write(TARGET_COMMAND_REG, 0);
2246
2247 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2248 barrier();
2249
Finn Thaine3c3da62014-11-12 16:12:15 +11002250 local_irq_save(flags);
Finn Thainef1081c2014-11-12 16:12:14 +11002251 hostdata->retain_dma_intr--;
Roman Zippelc28bda22007-05-01 22:32:36 +02002252 /* ++roman: For Falcon SCSI, release the lock on the
2253 * ST-DMA here if no other commands are waiting on the
2254 * disconnected queue.
2255 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002256 maybe_release_dma_irq(instance);
2257 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002258 return;
2259 case MESSAGE_REJECT:
2260 /* Accept message by clearing ACK */
2261 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2262 /* Enable reselect interrupts */
2263 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2264 switch (hostdata->last_message) {
2265 case HEAD_OF_QUEUE_TAG:
2266 case ORDERED_QUEUE_TAG:
2267 case SIMPLE_QUEUE_TAG:
2268 /* The target obviously doesn't support tagged
2269 * queuing, even though it announced this ability in
2270 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2271 * clear 'tagged_supported' and lock the LUN, since
2272 * the command is treated as untagged further on.
2273 */
2274 cmd->device->tagged_supported = 0;
2275 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2276 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002277 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002278 "QUEUE_TAG message; tagged queuing "
2279 "disabled\n",
2280 HOSTNO, cmd->device->id, cmd->device->lun);
2281 break;
2282 }
2283 break;
2284 case DISCONNECT:
2285 /* Accept message by clearing ACK */
2286 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2287 local_irq_save(flags);
2288 cmd->device->disconnect = 1;
2289 LIST(cmd,hostdata->disconnected_queue);
Roman Zippel3130d902007-05-01 22:32:37 +02002290 SET_NEXT(cmd, hostdata->disconnected_queue);
Roman Zippelc28bda22007-05-01 22:32:36 +02002291 hostdata->connected = NULL;
2292 hostdata->disconnected_queue = cmd;
2293 local_irq_restore(flags);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002294 dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
Roman Zippelc28bda22007-05-01 22:32:36 +02002295 "moved from connected to the "
2296 "disconnected_queue\n", HOSTNO,
2297 cmd->device->id, cmd->device->lun);
2298 /*
2299 * Restore phase bits to 0 so an interrupted selection,
2300 * arbitration can resume.
2301 */
2302 NCR5380_write(TARGET_COMMAND_REG, 0);
2303
2304 /* Enable reselect interrupts */
2305 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2306 /* Wait for bus free to avoid nasty timeouts */
2307 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2308 barrier();
Finn Thaine3f463b2014-11-12 16:12:16 +11002309#ifdef SUN3_SCSI_VME
2310 dregs->csr |= CSR_DMA_ENABLE;
2311#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002312 return;
2313 /*
2314 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2315 * operation, in violation of the SCSI spec so we can safely
2316 * ignore SAVE/RESTORE pointers calls.
2317 *
2318 * Unfortunately, some disks violate the SCSI spec and
2319 * don't issue the required SAVE_POINTERS message before
2320 * disconnecting, and we have to break spec to remain
2321 * compatible.
2322 */
2323 case SAVE_POINTERS:
2324 case RESTORE_POINTERS:
2325 /* Accept message by clearing ACK */
2326 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2327 /* Enable reselect interrupts */
2328 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2329 break;
2330 case EXTENDED_MESSAGE:
2331 /*
2332 * Extended messages are sent in the following format :
2333 * Byte
2334 * 0 EXTENDED_MESSAGE == 1
2335 * 1 length (includes one byte for code, doesn't
2336 * include first two bytes)
2337 * 2 code
2338 * 3..length+1 arguments
2339 *
2340 * Start the extended message buffer with the EXTENDED_MESSAGE
2341 * byte, since spi_print_msg() wants the whole thing.
2342 */
2343 extended_msg[0] = EXTENDED_MESSAGE;
2344 /* Accept first byte by clearing ACK */
2345 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2346
Finn Thaind65e6342014-03-18 11:42:20 +11002347 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002348
2349 len = 2;
2350 data = extended_msg + 1;
2351 phase = PHASE_MSGIN;
2352 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002353 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002354 (int)extended_msg[1], (int)extended_msg[2]);
2355
2356 if (!len && extended_msg[1] <=
2357 (sizeof(extended_msg) - 1)) {
2358 /* Accept third byte by clearing ACK */
2359 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2360 len = extended_msg[1] - 1;
2361 data = extended_msg + 3;
2362 phase = PHASE_MSGIN;
2363
2364 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002365 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002366 HOSTNO, len);
2367
2368 switch (extended_msg[2]) {
2369 case EXTENDED_SDTR:
2370 case EXTENDED_WDTR:
2371 case EXTENDED_MODIFY_DATA_POINTER:
2372 case EXTENDED_EXTENDED_IDENTIFY:
2373 tmp = 0;
2374 }
2375 } else if (len) {
2376 printk(KERN_NOTICE "scsi%d: error receiving "
2377 "extended message\n", HOSTNO);
2378 tmp = 0;
2379 } else {
2380 printk(KERN_NOTICE "scsi%d: extended message "
2381 "code %02x length %d is too long\n",
2382 HOSTNO, extended_msg[2], extended_msg[1]);
2383 tmp = 0;
2384 }
2385 /* Fall through to reject message */
2386
2387 /*
2388 * If we get something weird that we aren't expecting,
2389 * reject it.
2390 */
2391 default:
2392 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002393 printk(KERN_INFO "scsi%d: rejecting message ",
2394 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002395 spi_print_msg(extended_msg);
2396 printk("\n");
2397 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002398 scmd_printk(KERN_INFO, cmd,
2399 "rejecting unknown message %02x\n",
2400 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002401 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002402 scmd_printk(KERN_INFO, cmd,
2403 "rejecting unknown extended message code %02x, length %d\n",
2404 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002405
2406 msgout = MESSAGE_REJECT;
2407 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2408 break;
2409 } /* switch (tmp) */
2410 break;
2411 case PHASE_MSGOUT:
2412 len = 1;
2413 data = &msgout;
2414 hostdata->last_message = msgout;
2415 NCR5380_transfer_pio(instance, &phase, &len, &data);
2416 if (msgout == ABORT) {
Finn Thaine3c3da62014-11-12 16:12:15 +11002417 local_irq_save(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002418#ifdef SUPPORT_TAGS
2419 cmd_free_tag(cmd);
2420#else
2421 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2422#endif
2423 hostdata->connected = NULL;
2424 cmd->result = DID_ERROR << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02002425 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3c3da62014-11-12 16:12:15 +11002426 maybe_release_dma_irq(instance);
2427 local_irq_restore(flags);
2428 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002429 return;
2430 }
2431 msgout = NOP;
2432 break;
2433 case PHASE_CMDOUT:
2434 len = cmd->cmd_len;
2435 data = cmd->cmnd;
2436 /*
2437 * XXX for performance reasons, on machines with a
2438 * PSEUDO-DMA architecture we should probably
2439 * use the dma transfer function.
2440 */
2441 NCR5380_transfer_pio(instance, &phase, &len, &data);
2442 break;
2443 case PHASE_STATIN:
2444 len = 1;
2445 data = &tmp;
2446 NCR5380_transfer_pio(instance, &phase, &len, &data);
2447 cmd->SCp.Status = tmp;
2448 break;
2449 default:
2450 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002451 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002452 } /* switch(phase) */
2453 } /* if (tmp * SR_REQ) */
2454 } /* while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
2457/*
2458 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2459 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002460 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002461 * 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 -07002462 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 * Inputs : instance - this instance of the NCR5380.
2465 *
2466 */
2467
2468
Finn Thaine3f463b2014-11-12 16:12:16 +11002469/* it might eventually prove necessary to do a dma setup on
2470 reselection, but it doesn't seem to be needed now -- sam */
2471
Roman Zippelc28bda22007-05-01 22:32:36 +02002472static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Roman Zippelc28bda22007-05-01 22:32:36 +02002474 SETUP_HOSTDATA(instance);
2475 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002476 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002478 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002480 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002481 int __maybe_unused len;
2482 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain710ddd02014-11-12 16:12:02 +11002483 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Roman Zippelc28bda22007-05-01 22:32:36 +02002485 /*
2486 * Disable arbitration, etc. since the host adapter obviously
2487 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Roman Zippelc28bda22007-05-01 22:32:36 +02002490 NCR5380_write(MODE_REG, MR_BASE);
2491 hostdata->restart_select = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Roman Zippelc28bda22007-05-01 22:32:36 +02002493 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2494
Finn Thaind65e6342014-03-18 11:42:20 +11002495 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002496
2497 /*
2498 * At this point, we have detected that our SCSI ID is on the bus,
2499 * SEL is true and BSY was false for at least one bus settle delay
2500 * (400 ns).
2501 *
2502 * We must assert BSY ourselves, until the target drops the SEL
2503 * signal.
2504 */
2505
2506 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2507
2508 while (NCR5380_read(STATUS_REG) & SR_SEL)
2509 ;
2510 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2511
2512 /*
2513 * Wait for target to go into MSGIN.
2514 */
2515
2516 while (!(NCR5380_read(STATUS_REG) & SR_REQ))
2517 ;
2518
Finn Thaine3f463b2014-11-12 16:12:16 +11002519#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2520 /* acknowledge toggle to MSGIN */
2521 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2522
2523 /* peek at the byte without really hitting the bus */
2524 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2525#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002526 len = 1;
2527 data = msg;
2528 phase = PHASE_MSGIN;
2529 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaine3f463b2014-11-12 16:12:16 +11002530#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002531
2532 if (!(msg[0] & 0x80)) {
2533 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2534 spi_print_msg(msg);
2535 do_abort(instance);
2536 return;
2537 }
2538 lun = (msg[0] & 0x07);
2539
Finn Thaine3f463b2014-11-12 16:12:16 +11002540#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002541 /* If the phase is still MSGIN, the target wants to send some more
2542 * messages. In case it supports tagged queuing, this is probably a
2543 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2544 */
2545 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002546 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002547 /* Accept previous IDENTIFY message by clearing ACK */
2548 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2549 len = 2;
2550 data = msg + 1;
2551 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2552 msg[1] == SIMPLE_QUEUE_TAG)
2553 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002554 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002555 "reselection\n", HOSTNO, target_mask, lun, tag);
2556 }
2557#endif
2558
2559 /*
2560 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2561 * just reestablished, and remove it from the disconnected queue.
2562 */
2563
Finn Thain710ddd02014-11-12 16:12:02 +11002564 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002565 tmp; prev = tmp, tmp = NEXT(tmp)) {
2566 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2567#ifdef SUPPORT_TAGS
2568 && (tag == tmp->tag)
2569#endif
2570 ) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002571 if (prev) {
2572 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
Roman Zippel3130d902007-05-01 22:32:37 +02002573 SET_NEXT(prev, NEXT(tmp));
Roman Zippelc28bda22007-05-01 22:32:36 +02002574 } else {
2575 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2576 hostdata->disconnected_queue = NEXT(tmp);
2577 }
Roman Zippel3130d902007-05-01 22:32:37 +02002578 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002579 break;
2580 }
2581 }
2582
2583 if (!tmp) {
2584 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2585#ifdef SUPPORT_TAGS
2586 "tag %d "
2587#endif
2588 "not in disconnected_queue.\n",
2589 HOSTNO, target_mask, lun
2590#ifdef SUPPORT_TAGS
2591 , tag
2592#endif
2593 );
2594 /*
2595 * Since we have an established nexus that we can't do anything
2596 * with, we must abort it.
2597 */
2598 do_abort(instance);
2599 return;
2600 }
2601
Finn Thaine3f463b2014-11-12 16:12:16 +11002602#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2603 /* engage dma setup for the command we just saw */
2604 {
2605 void *d;
2606 unsigned long count;
2607
2608 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2609 count = tmp->SCp.buffer->length;
2610 d = sg_virt(tmp->SCp.buffer);
2611 } else {
2612 count = tmp->SCp.this_residual;
2613 d = tmp->SCp.ptr;
2614 }
2615 /* setup this command for dma if not already */
2616 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2617 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2618 sun3_dma_setup_done = tmp;
2619 }
2620 }
2621
2622 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2623#endif
2624
Roman Zippelc28bda22007-05-01 22:32:36 +02002625 /* Accept message by clearing ACK */
2626 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2627
Finn Thaine3f463b2014-11-12 16:12:16 +11002628#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2629 /* If the phase is still MSGIN, the target wants to send some more
2630 * messages. In case it supports tagged queuing, this is probably a
2631 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2632 */
2633 tag = TAG_NONE;
2634 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2635 /* Accept previous IDENTIFY message by clearing ACK */
2636 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2637 len = 2;
2638 data = msg + 1;
2639 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2640 msg[1] == SIMPLE_QUEUE_TAG)
2641 tag = msg[2];
2642 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2643 HOSTNO, target_mask, lun, tag);
2644 }
2645#endif
2646
Roman Zippelc28bda22007-05-01 22:32:36 +02002647 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002648 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002649 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
2652
2653/*
Finn Thain710ddd02014-11-12 16:12:02 +11002654 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 *
2656 * Purpose : abort a command
2657 *
Finn Thain710ddd02014-11-12 16:12:02 +11002658 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002659 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 * used.
2661 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002662 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002664 * XXX - there is no way to abort the command that is currently
2665 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002667 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 */
2669
2670static
Finn Thain710ddd02014-11-12 16:12:02 +11002671int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Roman Zippelc28bda22007-05-01 22:32:36 +02002673 struct Scsi_Host *instance = cmd->device->host;
2674 SETUP_HOSTDATA(instance);
Finn Thain710ddd02014-11-12 16:12:02 +11002675 struct scsi_cmnd *tmp, **prev;
Roman Zippelc28bda22007-05-01 22:32:36 +02002676 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002678 scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Roman Zippelc28bda22007-05-01 22:32:36 +02002680 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Roman Zippelc28bda22007-05-01 22:32:36 +02002682 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Finn Thaind65e6342014-03-18 11:42:20 +11002684 dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002685 NCR5380_read(BUS_AND_STATUS_REG),
2686 NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688#if 1
Roman Zippelc28bda22007-05-01 22:32:36 +02002689 /*
2690 * Case 1 : If the command is the currently executing command,
2691 * we'll set the aborted flag and return control so that
2692 * information transfer routine can exit cleanly.
2693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Roman Zippelc28bda22007-05-01 22:32:36 +02002695 if (hostdata->connected == cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Finn Thaind65e6342014-03-18 11:42:20 +11002697 dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002698 /*
2699 * We should perform BSY checking, and make sure we haven't slipped
2700 * into BUS FREE.
2701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Roman Zippelc28bda22007-05-01 22:32:36 +02002703 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2704 /*
2705 * Since we can't change phases until we've completed the current
2706 * handshake, we have to source or sink a byte of data if the current
2707 * phase is not MSGOUT.
2708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Roman Zippelc28bda22007-05-01 22:32:36 +02002710 /*
2711 * Return control to the executing NCR drive so we can clear the
2712 * aborted flag and get back into our main loop.
2713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Roman Zippelc28bda22007-05-01 22:32:36 +02002715 if (do_abort(instance) == 0) {
2716 hostdata->aborted = 1;
2717 hostdata->connected = NULL;
2718 cmd->result = DID_ABORT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002720 cmd_free_tag(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002722 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002724 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002725 local_irq_restore(flags);
2726 cmd->scsi_done(cmd);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002727 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002728 } else {
Finn Thaine3c3da62014-11-12 16:12:15 +11002729 local_irq_restore(flags);
Roman Zippelc28bda22007-05-01 22:32:36 +02002730 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002731 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002735
2736 /*
2737 * Case 2 : If the command hasn't been issued yet, we simply remove it
2738 * from the issue queue.
2739 */
Finn Thain710ddd02014-11-12 16:12:02 +11002740 for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
2741 tmp = (struct scsi_cmnd *)hostdata->issue_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002742 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2743 if (cmd == tmp) {
2744 REMOVE(5, *prev, tmp, NEXT(tmp));
2745 (*prev) = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002746 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002747 tmp->result = DID_ABORT << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002748 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002749 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002750 dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002751 HOSTNO);
2752 /* Tagged queuing note: no tag to free here, hasn't been assigned
2753 * yet... */
2754 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002755 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
2757 }
2758
Roman Zippelc28bda22007-05-01 22:32:36 +02002759 /*
2760 * Case 3 : If any commands are connected, we're going to fail the abort
2761 * and let the high level SCSI driver retry at a later time or
2762 * issue a reset.
2763 *
2764 * Timeouts, and therefore aborted commands, will be highly unlikely
2765 * and handling them cleanly in this situation would make the common
2766 * case of noresets less efficient, and would pollute our code. So,
2767 * we fail.
2768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Roman Zippelc28bda22007-05-01 22:32:36 +02002770 if (hostdata->connected) {
2771 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002772 dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002773 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Roman Zippelc28bda22007-05-01 22:32:36 +02002776 /*
2777 * Case 4: If the command is currently disconnected from the bus, and
2778 * there are no connected commands, we reconnect the I_T_L or
2779 * I_T_L_Q nexus associated with it, go into message out, and send
2780 * an abort message.
2781 *
2782 * This case is especially ugly. In order to reestablish the nexus, we
2783 * need to call NCR5380_select(). The easiest way to implement this
2784 * function was to abort if the bus was busy, and let the interrupt
2785 * handler triggered on the SEL for reselect take care of lost arbitrations
2786 * where necessary, meaning interrupts need to be enabled.
2787 *
2788 * When interrupts are enabled, the queues may change - so we
2789 * can't remove it from the disconnected queue before selecting it
2790 * because that could cause a failure in hashing the nexus if that
2791 * device reselected.
2792 *
2793 * Since the queues may change, we can't use the pointers from when we
2794 * first locate it.
2795 *
2796 * So, we must first locate the command, and if NCR5380_select()
2797 * succeeds, then issue the abort, relocate the command and remove
2798 * it from the disconnected queue.
2799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Finn Thain710ddd02014-11-12 16:12:02 +11002801 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002802 tmp = NEXT(tmp)) {
2803 if (cmd == tmp) {
2804 local_irq_restore(flags);
Finn Thaind65e6342014-03-18 11:42:20 +11002805 dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002806
Finn Thain76f13b92014-11-12 16:11:53 +11002807 if (NCR5380_select(instance, cmd))
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002808 return FAILED;
Roman Zippelc28bda22007-05-01 22:32:36 +02002809
Finn Thaind65e6342014-03-18 11:42:20 +11002810 dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002811
2812 do_abort(instance);
2813
2814 local_irq_save(flags);
Finn Thain710ddd02014-11-12 16:12:02 +11002815 for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
2816 tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
Roman Zippelc28bda22007-05-01 22:32:36 +02002817 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
2818 if (cmd == tmp) {
2819 REMOVE(5, *prev, tmp, NEXT(tmp));
2820 *prev = NEXT(tmp);
Roman Zippel3130d902007-05-01 22:32:37 +02002821 SET_NEXT(tmp, NULL);
Roman Zippelc28bda22007-05-01 22:32:36 +02002822 tmp->result = DID_ABORT << 16;
2823 /* We must unlock the tag/LUN immediately here, since the
2824 * target goes to BUS FREE and doesn't send us another
2825 * message (COMMAND_COMPLETE or the like)
2826 */
2827#ifdef SUPPORT_TAGS
2828 cmd_free_tag(tmp);
2829#else
2830 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2831#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002832 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002833 local_irq_restore(flags);
2834 tmp->scsi_done(tmp);
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002835 return SUCCESS;
Roman Zippelc28bda22007-05-01 22:32:36 +02002836 }
2837 }
2838 }
2839 }
2840
Finn Thaine3c3da62014-11-12 16:12:15 +11002841 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2842 * possible at all) At least, we should check if the lock could be
2843 * released after the abort, in case it is kept due to some bug.
2844 */
2845 maybe_release_dma_irq(instance);
2846 local_irq_restore(flags);
2847
Roman Zippelc28bda22007-05-01 22:32:36 +02002848 /*
2849 * Case 5 : If we reached this point, the command was not found in any of
2850 * the queues.
2851 *
2852 * We probably reached this point because of an unlikely race condition
2853 * between the command completing successfully and the abortion code,
2854 * so we won't panic, but we will notify the user in case something really
2855 * broke.
2856 */
2857
Joe Perchesad361c92009-07-06 13:05:40 -07002858 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002859
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002860 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
2862
2863
Roman Zippelc28bda22007-05-01 22:32:36 +02002864/*
Finn Thain710ddd02014-11-12 16:12:02 +11002865 * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
Roman Zippelc28bda22007-05-01 22:32:36 +02002866 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 * Purpose : reset the SCSI bus.
2868 *
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002869 * Returns : SUCCESS or FAILURE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Finn Thain710ddd02014-11-12 16:12:02 +11002873static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Finn Thaine3c3da62014-11-12 16:12:15 +11002875 struct Scsi_Host *instance = cmd->device->host;
2876 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002877 int i;
2878 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Finn Thaine3c3da62014-11-12 16:12:15 +11002880 NCR5380_print_status(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Roman Zippelc28bda22007-05-01 22:32:36 +02002882 /* get in phase */
2883 NCR5380_write(TARGET_COMMAND_REG,
2884 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2885 /* assert RST */
2886 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2887 udelay(40);
2888 /* reset NCR registers */
2889 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2890 NCR5380_write(MODE_REG, MR_BASE);
2891 NCR5380_write(TARGET_COMMAND_REG, 0);
2892 NCR5380_write(SELECT_ENABLE_REG, 0);
2893 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2894 * through anymore ... */
2895 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Roman Zippelc28bda22007-05-01 22:32:36 +02002897 /* After the reset, there are no more connected or disconnected commands
2898 * and no busy units; so clear the low-level status here to avoid
2899 * conflicts when the mid-level code tries to wake up the affected
2900 * commands!
2901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Roman Zippelc28bda22007-05-01 22:32:36 +02002903 if (hostdata->issue_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002904 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002905 if (hostdata->connected)
Finn Thaind65e6342014-03-18 11:42:20 +11002906 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02002907 if (hostdata->disconnected_queue)
Finn Thaind65e6342014-03-18 11:42:20 +11002908 dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Roman Zippelc28bda22007-05-01 22:32:36 +02002910 local_irq_save(flags);
2911 hostdata->issue_queue = NULL;
2912 hostdata->connected = NULL;
2913 hostdata->disconnected_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002915 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002917 for (i = 0; i < 8; ++i)
2918 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002920 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002922
2923 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002924 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002926 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}