blob: e3b362380a743061192ff76aa97b86f804db9850 [file] [log] [blame]
Roman Zippelc28bda22007-05-01 22:32:36 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Roman Zippelc28bda22007-05-01 22:32:36 +02003 * to implement 5380 SCSI drivers under Linux with a non-trantor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
Roman Zippelc28bda22007-05-01 22:32:36 +02009 * Visionary Computing
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (Unix and Linux consulting and custom programming)
Roman Zippelc28bda22007-05-01 22:32:36 +020011 * drew@colorado.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * +1 (303) 666-5836
13 *
Roman Zippelc28bda22007-05-01 22:32:36 +020014 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
28 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
29 * this file, too:
30 *
31 * - Some of the debug statements were incorrect (undefined variables and the
32 * like). I fixed that.
33 *
34 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
35 * possible DMA transfer size should also happen for REAL_DMA. I added this
36 * in the #if statement.
37 *
38 * - When using real DMA, information_transfer() should return in a DATAOUT
39 * phase after starting the DMA. It has nothing more to do.
40 *
41 * - The interrupt service routine should run main after end of DMA, too (not
42 * only after RESELECTION interrupts). Additionally, it should _not_ test
43 * for more interrupts after running main, since a DMA process may have
44 * been started and interrupts are turned on now. The new int could happen
45 * inside the execution of NCR5380_intr(), leading to recursive
46 * calls.
47 *
48 * - I've added a function merge_contiguous_buffers() that tries to
49 * merge scatter-gather buffers that are located at contiguous
50 * physical addresses and can be processed with the same DMA setup.
51 * Since most scatter-gather operations work on a page (4K) of
52 * 4 buffers (1K), in more than 90% of all cases three interrupts and
53 * DMA setup actions are saved.
54 *
55 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
56 * and USLEEP, because these were messing up readability and will never be
57 * needed for Atari SCSI.
Roman Zippelc28bda22007-05-01 22:32:36 +020058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
60 * stuff), and 'main' is executed in a bottom half if awoken by an
61 * interrupt.
62 *
63 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
64 * constructs. In my eyes, this made the source rather unreadable, so I
65 * finally replaced that by the *_PRINTK() macros.
66 *
67 */
68
Finn Thaine3f463b2014-11-12 16:12:16 +110069/* Adapted for the sun3 by Sam Creasey. */
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * Design
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
Roman Zippelc28bda22007-05-01 22:32:36 +020074 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +020076 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * memory mapped) and drops this file in their 'C' wrapper.
78 *
Roman Zippelc28bda22007-05-01 22:32:36 +020079 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +020081 * and commands that are currently executing. This means that an
82 * unlimited number of commands may be queued, letting
83 * more commands propagate from the higher driver levels giving higher
84 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
85 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * while a command is already executing.
87 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Roman Zippelc28bda22007-05-01 22:32:36 +020089 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *
Roman Zippelc28bda22007-05-01 22:32:36 +020091 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
92 * piece of hardware that requires you to sit in a loop polling for
93 * the REQ signal as long as you are connected. Some devices are
94 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110095 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * broken devices are the exception rather than the rule and I'd rather
97 * spend my time optimizing for the normal case.
98 *
99 * Architecture :
100 *
101 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +1100102 * which is started from a workqueue for each NCR5380 host in the
103 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
104 * removing the commands from the issue queue and calling
105 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * Once a nexus is established, the NCR5380_information_transfer()
108 * phase goes through the various phases as instructed by the target.
109 * if the target goes into MSG IN and sends a DISCONNECT message,
110 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +1100111 * queue, and NCR5380_main tries to find more work. If the target is
112 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
114 * If a command has disconnected, eventually an interrupt will trigger,
115 * calling NCR5380_intr() which will in turn call NCR5380_reselect
116 * to reestablish a nexus. This will run main if necessary.
117 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200118 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * appropriate.
120 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200121 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * structures, being initialized after the command is connected
123 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
124 * Note that in violation of the standard, an implicit SAVE POINTERS operation
125 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
126 */
127
128/*
129 * Using this file :
130 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200131 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * and macros and include this file in your driver.
133 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200134 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200136 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100138 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
139 * transceivers.
140 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
142 *
143 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
144 *
145 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200146 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * NCR5380_read(register) - read from the specified register
148 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200149 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100151 * NCR5380_implementation_fields - additional fields needed for this
152 * specific implementation of the NCR5380
153 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200157 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * that they were able to program the controller for.
159 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200160 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * available as NCR5380_i386_dma_write_setup,
162 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
163 *
164 * NCR5380_dma_write_setup(instance, src, count) - initialize
165 * NCR5380_dma_read_setup(instance, dst, count) - initialize
166 * NCR5380_dma_residual(instance); - residual count
167 *
168 * PSEUDO functions :
169 * NCR5380_pwrite(instance, src, count)
170 * NCR5380_pread(instance, dst, count);
171 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200173 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100175 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define HOSTNO instance->host_no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Finn Thain636b1ec2016-01-03 16:05:10 +1100180static int do_abort(struct Scsi_Host *);
181static void do_reset(struct Scsi_Host *);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#ifdef SUPPORT_TAGS
184
185/*
186 * Functions for handling tagged queuing
187 * =====================================
188 *
189 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
190 *
191 * Using consecutive numbers for the tags is no good idea in my eyes. There
192 * could be wrong re-usings if the counter (8 bit!) wraps and some early
193 * command has been preempted for a long time. My solution: a bitfield for
194 * remembering used tags.
195 *
196 * There's also the problem that each target has a certain queue size, but we
197 * cannot know it in advance :-( We just see a QUEUE_FULL status being
198 * returned. So, in this case, the driver internal queue size assumption is
199 * reduced to the number of active tags if QUEUE_FULL is returned by the
Finn Thaine42d0152016-01-03 16:05:49 +1100200 * target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
202 * We're also not allowed running tagged commands as long as an untagged
203 * command is active. And REQUEST SENSE commands after a contingent allegiance
204 * condition _must_ be untagged. To keep track whether an untagged command has
205 * been issued, the host->busy array is still employed, as it is without
206 * support for tagged queuing.
207 *
208 * One could suspect that there are possible race conditions between
209 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
210 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
211 * which already guaranteed to be running at most once. It is also the only
212 * place where tags/LUNs are allocated. So no other allocation can slip
213 * between that pair, there could only happen a reselection, which can free a
214 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
215 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
216 */
217
Finn Thainca513fc2014-11-12 16:12:19 +1100218static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Roman Zippelc28bda22007-05-01 22:32:36 +0200220 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100221 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200222
Finn Thainca513fc2014-11-12 16:12:19 +1100223 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200224 return;
225
226 for (target = 0; target < 8; ++target) {
227 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100228 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200229 bitmap_zero(ta->allocated, MAX_TAGS);
230 ta->nr_allocated = 0;
231 /* At the beginning, assume the maximum queue size we could
232 * support (MAX_TAGS). This value will be decreased if the target
233 * returns QUEUE_FULL status.
234 */
235 ta->queue_size = MAX_TAGS;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240
241/* Check if we can issue a command to this LUN: First see if the LUN is marked
242 * busy by an untagged command. If the command should use tagged queuing, also
243 * check that there is a free tag and the target's queue won't overflow. This
244 * function should be called with interrupts disabled to avoid race
245 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Finn Thain710ddd02014-11-12 16:12:02 +1100248static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200250 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100251 struct Scsi_Host *instance = cmd->device->host;
252 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200254 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200255 return 1;
256 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100257 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
258 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200259 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100260 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
261 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100262 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d: no free tags\n",
263 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200264 return 1;
265 }
266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
271 * must be called before!), or reserve the LUN in 'busy' if the command is
272 * untagged.
273 */
274
Finn Thain710ddd02014-11-12 16:12:02 +1100275static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200277 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100278 struct Scsi_Host *instance = cmd->device->host;
279 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Roman Zippelc28bda22007-05-01 22:32:36 +0200281 /* If we or the target don't support tagged queuing, allocate the LUN for
282 * an untagged command.
283 */
284 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100285 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
286 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200287 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200288 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100289 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d now allocated by untagged command\n",
290 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200291 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100292 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Roman Zippelc28bda22007-05-01 22:32:36 +0200294 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
295 set_bit(cmd->tag, ta->allocated);
296 ta->nr_allocated++;
Finn Thaindbb6b352016-01-03 16:05:53 +1100297 dsprintk(NDEBUG_TAGS, instance, "using tag %d for target %d lun %d (%d tags allocated)\n",
298 cmd->tag, scmd_id(cmd), lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302
303/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
304 * unlock the LUN.
305 */
306
Finn Thain710ddd02014-11-12 16:12:02 +1100307static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200309 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100310 struct Scsi_Host *instance = cmd->device->host;
311 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Roman Zippelc28bda22007-05-01 22:32:36 +0200313 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200314 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100315 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d untagged cmd freed\n",
316 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200317 } else if (cmd->tag >= MAX_TAGS) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100318 shost_printk(KERN_NOTICE, instance,
319 "trying to free bad tag %d!\n", cmd->tag);
Roman Zippelc28bda22007-05-01 22:32:36 +0200320 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100321 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 clear_bit(cmd->tag, ta->allocated);
323 ta->nr_allocated--;
Finn Thaindbb6b352016-01-03 16:05:53 +1100324 dsprintk(NDEBUG_TAGS, instance, "freed tag %d for target %d lun %d\n",
325 cmd->tag, scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329
Finn Thainca513fc2014-11-12 16:12:19 +1100330static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Roman Zippelc28bda22007-05-01 22:32:36 +0200332 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100333 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Finn Thainca513fc2014-11-12 16:12:19 +1100335 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200336 return;
337
338 for (target = 0; target < 8; ++target) {
339 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100340 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200341 bitmap_zero(ta->allocated, MAX_TAGS);
342 ta->nr_allocated = 0;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347#endif /* SUPPORT_TAGS */
348
349
350/*
Finn Thain710ddd02014-11-12 16:12:02 +1100351 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
353 * Purpose: Try to merge several scatter-gather requests into one DMA
354 * transfer. This is possible if the scatter buffers lie on
355 * physical contiguous addresses.
356 *
Finn Thain710ddd02014-11-12 16:12:02 +1100357 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300359 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
361
Finn Thain710ddd02014-11-12 16:12:02 +1100362static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Finn Thaine3f463b2014-11-12 16:12:16 +1100364#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200365 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200367 unsigned long oldlen = cmd->SCp.this_residual;
368 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
370
Roman Zippelc28bda22007-05-01 22:32:36 +0200371 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
372 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200373 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100374 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200375 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200377 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200379 ++cmd->SCp.buffer;
380 --cmd->SCp.buffers_residual;
381 cmd->SCp.this_residual += cmd->SCp.buffer->length;
382 endaddr += cmd->SCp.buffer->length;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200385 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100386 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200387 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100389#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Finn Thainff50f9e2014-11-12 16:12:18 +1100392/**
393 * initialize_SCp - init the scsi pointer field
394 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100396 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
398
Finn Thain710ddd02014-11-12 16:12:02 +1100399static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Roman Zippelc28bda22007-05-01 22:32:36 +0200401 /*
402 * Initialize the Scsi Pointer field so that all of the commands in the
403 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200405
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200406 if (scsi_bufflen(cmd)) {
407 cmd->SCp.buffer = scsi_sglist(cmd);
408 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200409 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200410 cmd->SCp.this_residual = cmd->SCp.buffer->length;
411 /* ++roman: Try to merge some scatter-buffers if they are at
412 * contiguous physical addresses.
413 */
414 merge_contiguous_buffers(cmd);
415 } else {
416 cmd->SCp.buffer = NULL;
417 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200418 cmd->SCp.ptr = NULL;
419 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200420 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100421
422 cmd->SCp.Status = 0;
423 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Finn Thain636b1ec2016-01-03 16:05:10 +1100426/**
Finn Thainb32ade12016-01-03 16:05:41 +1100427 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain636b1ec2016-01-03 16:05:10 +1100428 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100429 * @reg1: 5380 register to poll
430 * @bit1: Bitmask to check
431 * @val1: Expected value
432 * @reg2: Second 5380 register to poll
433 * @bit2: Second bitmask to check
434 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100435 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100436 *
Finn Thain2f854b82016-01-03 16:05:22 +1100437 * Polls the chip in a reasonably efficient manner waiting for an
438 * event to occur. After a short quick poll we begin to yield the CPU
439 * (if possible). In irq contexts the time-out is arbitrarily limited.
440 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100441 *
Finn Thainb32ade12016-01-03 16:05:41 +1100442 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100443 */
444
Finn Thainb32ade12016-01-03 16:05:41 +1100445static int NCR5380_poll_politely2(struct Scsi_Host *instance,
446 int reg1, int bit1, int val1,
447 int reg2, int bit2, int val2, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100448{
Finn Thain2f854b82016-01-03 16:05:22 +1100449 struct NCR5380_hostdata *hostdata = shost_priv(instance);
450 unsigned long deadline = jiffies + wait;
451 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100452
Finn Thain2f854b82016-01-03 16:05:22 +1100453 /* Busy-wait for up to 10 ms */
454 n = min(10000U, jiffies_to_usecs(wait));
455 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100456 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100457 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100458 if ((NCR5380_read(reg1) & bit1) == val1)
459 return 0;
460 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain636b1ec2016-01-03 16:05:10 +1100461 return 0;
462 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100463 } while (n--);
464
465 if (irqs_disabled() || in_interrupt())
466 return -ETIMEDOUT;
467
468 /* Repeatedly sleep for 1 ms until deadline */
469 while (time_is_after_jiffies(deadline)) {
470 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100471 if ((NCR5380_read(reg1) & bit1) == val1)
472 return 0;
473 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain2f854b82016-01-03 16:05:22 +1100474 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100475 }
476
Finn Thain636b1ec2016-01-03 16:05:10 +1100477 return -ETIMEDOUT;
478}
479
Finn Thainb32ade12016-01-03 16:05:41 +1100480static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
481 int reg, int bit, int val, int wait)
482{
483 return NCR5380_poll_politely2(instance, reg, bit, val,
484 reg, bit, val, wait);
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#if NDEBUG
488static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200489 unsigned char mask;
490 const char *name;
491} signals[] = {
492 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
493 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
494 { SR_SEL, "SEL" }, {0, NULL}
495}, basrs[] = {
496 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
497}, icrs[] = {
498 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
499 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
500 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
501 {0, NULL}
502}, mrs[] = {
503 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
504 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
505 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
506 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
507 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
508 {0, NULL}
509};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Finn Thainff50f9e2014-11-12 16:12:18 +1100511/**
512 * NCR5380_print - print scsi bus signals
513 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100515 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517
Roman Zippelc28bda22007-05-01 22:32:36 +0200518static void NCR5380_print(struct Scsi_Host *instance)
519{
520 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Roman Zippelc28bda22007-05-01 22:32:36 +0200522 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
523 status = NCR5380_read(STATUS_REG);
524 mr = NCR5380_read(MODE_REG);
525 icr = NCR5380_read(INITIATOR_COMMAND_REG);
526 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thain11d2f632016-01-03 16:05:51 +1100527
Roman Zippelc28bda22007-05-01 22:32:36 +0200528 printk("STATUS_REG: %02x ", status);
529 for (i = 0; signals[i].mask; ++i)
530 if (status & signals[i].mask)
531 printk(",%s", signals[i].name);
532 printk("\nBASR: %02x ", basr);
533 for (i = 0; basrs[i].mask; ++i)
534 if (basr & basrs[i].mask)
535 printk(",%s", basrs[i].name);
536 printk("\nICR: %02x ", icr);
537 for (i = 0; icrs[i].mask; ++i)
538 if (icr & icrs[i].mask)
539 printk(",%s", icrs[i].name);
540 printk("\nMODE: %02x ", mr);
541 for (i = 0; mrs[i].mask; ++i)
542 if (mr & mrs[i].mask)
543 printk(",%s", mrs[i].name);
544 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200548 unsigned char value;
549 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200551 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
552 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
553 {PHASE_UNKNOWN, "UNKNOWN"}
554};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Finn Thainff50f9e2014-11-12 16:12:18 +1100556/**
557 * NCR5380_print_phase - show SCSI phase
558 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100560 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100562 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
564
565static void NCR5380_print_phase(struct Scsi_Host *instance)
566{
Roman Zippelc28bda22007-05-01 22:32:36 +0200567 unsigned char status;
568 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Roman Zippelc28bda22007-05-01 22:32:36 +0200570 status = NCR5380_read(STATUS_REG);
571 if (!(status & SR_REQ))
572 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
573 else {
574 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
575 (phases[i].value != (status & PHASE_MASK)); ++i)
576 ;
577 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#endif
582
Finn Thain8c325132014-11-12 16:11:58 +1100583/**
584 * NCR58380_info - report driver and host information
585 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
Finn Thain8c325132014-11-12 16:11:58 +1100587 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Finn Thain8c325132014-11-12 16:11:58 +1100589 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591
Finn Thain8c325132014-11-12 16:11:58 +1100592static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Finn Thain8c325132014-11-12 16:11:58 +1100594 struct NCR5380_hostdata *hostdata = shost_priv(instance);
595
596 return hostdata->info;
597}
598
599static void prepare_info(struct Scsi_Host *instance)
600{
601 struct NCR5380_hostdata *hostdata = shost_priv(instance);
602
603 snprintf(hostdata->info, sizeof(hostdata->info),
604 "%s, io_port 0x%lx, n_io_port %d, "
605 "base 0x%lx, irq %d, "
606 "can_queue %d, cmd_per_lun %d, "
607 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100608 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100609 "options { %s} ",
610 instance->hostt->name, instance->io_port, instance->n_io_port,
611 instance->base, instance->irq,
612 instance->can_queue, instance->cmd_per_lun,
613 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100614 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100615 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100616#ifdef DIFFERENTIAL
617 "DIFFERENTIAL "
618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100620 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#endif
622#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100623 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624#endif
625#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100626 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#endif
Finn Thain8c325132014-11-12 16:11:58 +1100628 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Finn Thainff50f9e2014-11-12 16:12:18 +1100631/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100632 * NCR5380_init - initialise an NCR5380
633 * @instance: adapter to configure
634 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100636 * Initializes *instance and corresponding 5380 chip,
637 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *
639 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100640 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200641 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100642 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100645static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Finn Thaine8a60142016-01-03 16:05:54 +1100647 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +0200648 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100649 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Finn Thaina53a21e2014-11-12 16:12:21 +1100651 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200652 hostdata->id_mask = 1 << instance->this_id;
653 hostdata->id_higher_mask = 0;
654 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
655 if (i > hostdata->id_mask)
656 hostdata->id_higher_mask |= i;
657 for (i = 0; i < 8; ++i)
658 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100660 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
662#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200663 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100665 spin_lock_init(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200666 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100667 hostdata->sensing = NULL;
668 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100669 INIT_LIST_HEAD(&hostdata->unissued);
670 INIT_LIST_HEAD(&hostdata->disconnected);
671
Finn Thainef1081c2014-11-12 16:12:14 +1100672 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Finn Thaina53a21e2014-11-12 16:12:21 +1100674 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100675 hostdata->work_q = alloc_workqueue("ncr5380_%d",
676 WQ_UNBOUND | WQ_MEM_RECLAIM,
677 1, instance->host_no);
678 if (!hostdata->work_q)
679 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Finn Thain8c325132014-11-12 16:11:58 +1100681 prepare_info(instance);
682
Roman Zippelc28bda22007-05-01 22:32:36 +0200683 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
684 NCR5380_write(MODE_REG, MR_BASE);
685 NCR5380_write(TARGET_COMMAND_REG, 0);
686 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Finn Thain2f854b82016-01-03 16:05:22 +1100688 /* Calibrate register polling loop */
689 i = 0;
690 deadline = jiffies + 1;
691 do {
692 cpu_relax();
693 } while (time_is_after_jiffies(deadline));
694 deadline += msecs_to_jiffies(256);
695 do {
696 NCR5380_read(STATUS_REG);
697 ++i;
698 cpu_relax();
699 } while (time_is_after_jiffies(deadline));
700 hostdata->accesses_per_ms = i / 256;
701
Roman Zippelc28bda22007-05-01 22:32:36 +0200702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Finn Thainff50f9e2014-11-12 16:12:18 +1100705/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100706 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
707 * @instance: adapter to check
708 *
709 * If the system crashed, it may have crashed with a connected target and
710 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
711 * currently established nexus, which we know nothing about. Failing that
712 * do a bus reset.
713 *
714 * Note that a bus reset will cause the chip to assert IRQ.
715 *
716 * Returns 0 if successful, otherwise -ENXIO.
717 */
718
719static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
720{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100721 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100722 int pass;
723
724 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
725 switch (pass) {
726 case 1:
727 case 3:
728 case 5:
729 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
730 NCR5380_poll_politely(instance,
731 STATUS_REG, SR_BSY, 0, 5 * HZ);
732 break;
733 case 2:
734 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
735 do_abort(instance);
736 break;
737 case 4:
738 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
739 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100740 /* Wait after a reset; the SCSI standard calls for
741 * 250ms, we wait 500ms to be on the safe side.
742 * But some Toshiba CD-ROMs need ten times that.
743 */
744 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
745 msleep(2500);
746 else
747 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100748 break;
749 case 6:
750 shost_printk(KERN_ERR, instance, "bus locked solid\n");
751 return -ENXIO;
752 }
753 }
754 return 0;
755}
756
757/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100758 * NCR5380_exit - remove an NCR5380
759 * @instance: adapter to remove
760 *
761 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
762 */
763
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200764static void NCR5380_exit(struct Scsi_Host *instance)
765{
Finn Thaina53a21e2014-11-12 16:12:21 +1100766 struct NCR5380_hostdata *hostdata = shost_priv(instance);
767
768 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100769 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200770}
771
Finn Thainff50f9e2014-11-12 16:12:18 +1100772/**
Finn Thain677e0192016-01-03 16:05:59 +1100773 * complete_cmd - finish processing a command and return it to the SCSI ML
774 * @instance: the host instance
775 * @cmd: command to complete
776 */
777
778static void complete_cmd(struct Scsi_Host *instance,
779 struct scsi_cmnd *cmd)
780{
781 struct NCR5380_hostdata *hostdata = shost_priv(instance);
782
783 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
784
Finn Thainf27db8e2016-01-03 16:06:00 +1100785 if (hostdata->sensing == cmd) {
786 /* Autosense processing ends here */
787 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
788 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
789 set_host_byte(cmd, DID_ERROR);
790 } else
791 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
792 hostdata->sensing = NULL;
793 }
794
Finn Thain677e0192016-01-03 16:05:59 +1100795#ifdef SUPPORT_TAGS
796 cmd_free_tag(cmd);
797#else
798 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
799#endif
800 cmd->scsi_done(cmd);
801}
802
803/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100804 * NCR5380_queue_command - queue a command
805 * @instance: the relevant SCSI adapter
806 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Finn Thain1bb40582016-01-03 16:05:29 +1100808 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100809 * twiddling done to the host specific fields of cmd. If the
810 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
812
Finn Thain16b29e72014-11-12 16:12:08 +1100813static int NCR5380_queue_command(struct Scsi_Host *instance,
814 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Finn Thain16b29e72014-11-12 16:12:08 +1100816 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100817 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200818 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200821 switch (cmd->cmnd[0]) {
822 case WRITE_6:
823 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100824 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200825 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100826 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200827 return 0;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
830
Roman Zippelc28bda22007-05-01 22:32:36 +0200831 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Roman Zippelc28bda22007-05-01 22:32:36 +0200833 /*
834 * Insert the cmd into the issue queue. Note that REQUEST SENSE
835 * commands are added to the head of the queue since any command will
836 * clear the contingent allegiance condition that exists and the
837 * sense data is only guaranteed to be valid while the condition exists.
838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Roman Zippelc28bda22007-05-01 22:32:36 +0200840 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
841 * Otherwise a running NCR5380_main may steal the lock.
842 * Lock before actually inserting due to fairness reasons explained in
843 * atari_scsi.c. If we insert first, then it's impossible for this driver
844 * to release the lock.
845 * Stop timer for this command while waiting for the lock, or timeouts
846 * may happen (and they really do), and it's no good if the command doesn't
847 * appear in any of the queues.
848 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
849 * because also a timer int can trigger an abort or reset, which would
850 * alter queues and touch the lock.
851 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100852 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100853 return SCSI_MLQUEUE_HOST_BUSY;
854
Finn Thain11d2f632016-01-03 16:05:51 +1100855 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain16b29e72014-11-12 16:12:08 +1100856
Finn Thainff50f9e2014-11-12 16:12:18 +1100857 /*
858 * Insert the cmd into the issue queue. Note that REQUEST SENSE
859 * commands are added to the head of the queue since any command will
860 * clear the contingent allegiance condition that exists and the
861 * sense data is only guaranteed to be valid while the condition exists.
862 */
863
Finn Thain32b26a12016-01-03 16:05:58 +1100864 if (cmd->cmnd[0] == REQUEST_SENSE)
865 list_add(&ncmd->list, &hostdata->unissued);
866 else
867 list_add_tail(&ncmd->list, &hostdata->unissued);
868
Finn Thain11d2f632016-01-03 16:05:51 +1100869 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Finn Thaindbb6b352016-01-03 16:05:53 +1100871 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
872 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Finn Thain010e89d2016-01-03 16:05:38 +1100874 /* Kick off command processing */
875 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Finn Thaine3c3da62014-11-12 16:12:15 +1100879static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
880{
881 struct NCR5380_hostdata *hostdata = shost_priv(instance);
882
883 /* Caller does the locking needed to set & test these data atomically */
Finn Thain32b26a12016-01-03 16:05:58 +1100884 if (list_empty(&hostdata->disconnected) &&
885 list_empty(&hostdata->unissued) &&
Finn Thainf27db8e2016-01-03 16:06:00 +1100886 list_empty(&hostdata->autosense) &&
Finn Thaine3c3da62014-11-12 16:12:15 +1100887 !hostdata->connected &&
888 !hostdata->retain_dma_intr)
889 NCR5380_release_dma_irq(instance);
890}
891
Finn Thainff50f9e2014-11-12 16:12:18 +1100892/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100893 * dequeue_next_cmd - dequeue a command for processing
894 * @instance: the scsi host instance
895 *
896 * Priority is given to commands on the autosense queue. These commands
897 * need autosense because of a CHECK CONDITION result.
898 *
899 * Returns a command pointer if a command is found for a target that is
900 * not already busy. Otherwise returns NULL.
901 */
902
903static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
904{
905 struct NCR5380_hostdata *hostdata = shost_priv(instance);
906 struct NCR5380_cmd *ncmd;
907 struct scsi_cmnd *cmd;
908
909 if (list_empty(&hostdata->autosense)) {
910 list_for_each_entry(ncmd, &hostdata->unissued, list) {
911 cmd = NCR5380_to_scmd(ncmd);
912 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
913 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
914
915 if (
916#ifdef SUPPORT_TAGS
917 !is_lun_busy(cmd, 1)
918#else
919 !(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))
920#endif
921 ) {
922 list_del(&ncmd->list);
923 dsprintk(NDEBUG_QUEUES, instance,
924 "dequeue: removed %p from issue queue\n", cmd);
925 return cmd;
926 }
927 }
928 } else {
929 /* Autosense processing begins here */
930 ncmd = list_first_entry(&hostdata->autosense,
931 struct NCR5380_cmd, list);
932 list_del(&ncmd->list);
933 cmd = NCR5380_to_scmd(ncmd);
934 dsprintk(NDEBUG_QUEUES, instance,
935 "dequeue: removed %p from autosense queue\n", cmd);
936 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
937 hostdata->sensing = cmd;
938 return cmd;
939 }
940 return NULL;
941}
942
943static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
944{
945 struct NCR5380_hostdata *hostdata = shost_priv(instance);
946 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
947
948 if (hostdata->sensing) {
949 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
950 list_add(&ncmd->list, &hostdata->autosense);
951 hostdata->sensing = NULL;
952 } else
953 list_add(&ncmd->list, &hostdata->unissued);
954}
955
956/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100957 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100959 * NCR5380_main is a coroutine that runs as long as more work can
960 * be done on the NCR5380 host adapters in a system. Both
961 * NCR5380_queue_command() and NCR5380_intr() will try to start it
962 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200963 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100964 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200965 */
966
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200967static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Finn Thaina53a21e2014-11-12 16:12:21 +1100969 struct NCR5380_hostdata *hostdata =
970 container_of(work, struct NCR5380_hostdata, main_task);
971 struct Scsi_Host *instance = hostdata->host;
Finn Thainf27db8e2016-01-03 16:06:00 +1100972 struct scsi_cmnd *cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +0200973 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Roman Zippelc28bda22007-05-01 22:32:36 +0200975 /*
Roman Zippelc28bda22007-05-01 22:32:36 +0200976 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
977 * because also a timer int can trigger an abort or reset, which can
978 * alter queues and touch the Falcon lock.
979 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Finn Thain11d2f632016-01-03 16:05:51 +1100981 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200982 do {
Roman Zippelc28bda22007-05-01 22:32:36 +0200983 done = 1;
984
Finn Thainf27db8e2016-01-03 16:06:00 +1100985 while (!hostdata->connected &&
986 (cmd = dequeue_next_cmd(instance))) {
987
988 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
989
Roman Zippelc28bda22007-05-01 22:32:36 +0200990 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100991 * Attempt to establish an I_T_L nexus here.
992 * On success, instance->hostdata->connected is set.
993 * On failure, we must add the command back to the
994 * issue queue so we can keep trying.
Roman Zippelc28bda22007-05-01 22:32:36 +0200995 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100996 /*
997 * REQUEST SENSE commands are issued without tagged
998 * queueing, even on SCSI-II devices because the
999 * contingent allegiance condition exists for the
1000 * entire unit.
1001 */
1002 /* ++roman: ...and the standard also requires that
1003 * REQUEST SENSE command are untagged.
1004 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001005
1006#ifdef SUPPORT_TAGS
Finn Thainf27db8e2016-01-03 16:06:00 +11001007 cmd_get_tag(cmd, cmd->cmnd[0] != REQUEST_SENSE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001008#endif
Finn Thainf27db8e2016-01-03 16:06:00 +11001009 hostdata->retain_dma_intr++;
1010 if (!NCR5380_select(instance, cmd)) {
1011 dsprintk(NDEBUG_MAIN, instance, "main: selected target %d for command %p\n",
1012 scmd_id(cmd), cmd);
1013 hostdata->retain_dma_intr--;
1014 maybe_release_dma_irq(instance);
1015 } else {
1016 hostdata->retain_dma_intr--;
1017 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
1018 "main: select failed, returning %p to queue\n", cmd);
1019 requeue_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001020#ifdef SUPPORT_TAGS
Finn Thainf27db8e2016-01-03 16:06:00 +11001021 cmd_free_tag(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001022#endif
Finn Thainf27db8e2016-01-03 16:06:00 +11001023 }
1024 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001025 if (hostdata->connected
1026#ifdef REAL_DMA
1027 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#endif
1029 ) {
Finn Thaind65e6342014-03-18 11:42:20 +11001030 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001031 HOSTNO);
1032 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001033 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001034 done = 0;
1035 }
1036 } while (!done);
Finn Thain11d2f632016-01-03 16:05:51 +11001037 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040
1041#ifdef REAL_DMA
1042/*
1043 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1044 *
1045 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001046 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
1048 * Inputs : instance - this instance of the NCR5380.
1049 *
1050 */
1051
Roman Zippelc28bda22007-05-01 22:32:36 +02001052static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Finn Thaine8a60142016-01-03 16:05:54 +11001054 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001055 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001056 unsigned char **data;
Finn Thain5299b3c2016-01-03 16:05:57 +11001057 int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001058 int saved_data = 0, overrun = 0;
1059 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Finn Thainef1081c2014-11-12 16:12:14 +11001061 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001062 p = hostdata->connected->SCp.phase;
1063 if (p & SR_IO) {
1064 udelay(10);
1065 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1066 (BASR_PHASE_MATCH|BASR_ACK)) ==
1067 (BASR_PHASE_MATCH|BASR_ACK)) {
1068 saved_data = NCR5380_read(INPUT_DATA_REG);
1069 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +11001070 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001071 }
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001074
Finn Thaine3f463b2014-11-12 16:12:16 +11001075#if defined(CONFIG_SUN3)
1076 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1077 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1078 instance->host_no);
1079 BUG();
1080 }
1081
1082 /* make sure we're not stuck in a data phase */
1083 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1084 (BASR_PHASE_MATCH | BASR_ACK)) {
1085 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1086 NCR5380_read(BUS_AND_STATUS_REG));
1087 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1088 instance->host_no);
1089 BUG();
1090 }
1091#endif
1092
Roman Zippelc28bda22007-05-01 22:32:36 +02001093 NCR5380_write(MODE_REG, MR_BASE);
1094 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001095 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001096
Finn Thain01bd9082014-11-12 16:12:23 +11001097 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001098 hostdata->dma_len = 0;
1099
1100 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1101 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001102 *data += transferred;
1103 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001104
Finn Thainef1081c2014-11-12 16:12:14 +11001105 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001106 int cnt, toPIO;
1107
Roman Zippelc28bda22007-05-01 22:32:36 +02001108 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001109 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001110 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001111 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001112 *(*data)++ = saved_data;
1113 (*count)--;
1114 cnt--;
1115 toPIO--;
1116 }
Finn Thaind65e6342014-03-18 11:42:20 +11001117 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001118 NCR5380_transfer_pio(instance, &p, &cnt, data);
1119 *count -= toPIO - cnt;
1120 }
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123#endif /* REAL_DMA */
1124
1125
Finn Thainff50f9e2014-11-12 16:12:18 +11001126/**
1127 * NCR5380_intr - generic NCR5380 irq handler
1128 * @irq: interrupt number
1129 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001130 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001131 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1132 * from the disconnected queue, and restarting NCR5380_main()
1133 * as required.
Finn Thaincd400822016-01-03 16:05:40 +11001134 *
1135 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1136 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1137 * Three of these six conditions are latched in the Bus and Status Register:
1138 * - End of DMA (cleared by ending DMA Mode)
1139 * - Parity error (cleared by reading RPIR)
1140 * - Loss of BSY (cleared by reading RPIR)
1141 * Two conditions have flag bits that are not latched:
1142 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1143 * - Bus reset (non-maskable)
1144 * The remaining condition has no flag bit at all:
1145 * - Selection/reselection
1146 *
1147 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1148 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1149 * claimed that "the design of the [DP8490] interrupt logic ensures
1150 * interrupts will not be lost (they can be on the DP5380)."
1151 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1152 *
1153 * Checking for bus reset by reading RST is futile because of interrupt
1154 * latency, but a bus reset will reset chip logic. Checking for parity error
1155 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1156 * condition will clear DMA Mode. We can tell when this occurs because the
1157 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
1159
Roman Zippelc28bda22007-05-01 22:32:36 +02001160static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Finn Thaina53a21e2014-11-12 16:12:21 +11001162 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001163 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001164 int handled = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001165 unsigned char basr;
Finn Thain11d2f632016-01-03 16:05:51 +11001166 unsigned long flags;
1167
1168 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Roman Zippelc28bda22007-05-01 22:32:36 +02001170 basr = NCR5380_read(BUS_AND_STATUS_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001171 if (basr & BASR_IRQ) {
Finn Thaincd400822016-01-03 16:05:40 +11001172 unsigned char mr = NCR5380_read(MODE_REG);
1173 unsigned char sr = NCR5380_read(STATUS_REG);
1174
1175 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1176 HOSTNO, irq, basr, sr, mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001179 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1180 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1181 * We ack IRQ after clearing Mode Register. Workarounds
1182 * for End of DMA errata need to happen in DMA Mode.
Roman Zippelc28bda22007-05-01 22:32:36 +02001183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Finn Thaincd400822016-01-03 16:05:40 +11001185 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001186
Finn Thaincd400822016-01-03 16:05:40 +11001187 if (hostdata->connected) {
1188 NCR5380_dma_complete(instance);
1189 queue_work(hostdata->work_q, &hostdata->main_task);
1190 } else {
1191 NCR5380_write(MODE_REG, MR_BASE);
1192 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001193 }
Finn Thaincd400822016-01-03 16:05:40 +11001194 } else
1195#endif /* REAL_DMA */
1196 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1197 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1198 /* Probably reselected */
1199 NCR5380_write(SELECT_ENABLE_REG, 0);
1200 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1201
1202 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1203 HOSTNO);
1204
1205 if (!hostdata->connected) {
1206 NCR5380_reselect(instance);
1207 queue_work(hostdata->work_q, &hostdata->main_task);
1208 }
1209 if (!hostdata->connected)
1210 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1211 } else {
1212 /* Probably Bus Reset */
1213 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1214
1215 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", HOSTNO);
1216#ifdef SUN3_SCSI_VME
1217 dregs->csr |= CSR_DMA_ENABLE;
1218#endif
1219 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001220 handled = 1;
Finn Thaincd400822016-01-03 16:05:40 +11001221 } else {
1222 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine3f463b2014-11-12 16:12:16 +11001223#ifdef SUN3_SCSI_VME
1224 dregs->csr |= CSR_DMA_ENABLE;
1225#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001226 }
1227
Finn Thain11d2f632016-01-03 16:05:51 +11001228 spin_unlock_irqrestore(&hostdata->lock, flags);
1229
Roman Zippelc28bda22007-05-01 22:32:36 +02001230 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Roman Zippelc28bda22007-05-01 22:32:36 +02001233/*
Finn Thain710ddd02014-11-12 16:12:02 +11001234 * Function : int NCR5380_select(struct Scsi_Host *instance,
1235 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 *
1237 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001238 * including ARBITRATION, SELECTION, and initial message out for
1239 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001241 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001242 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
Finn Thain63238762016-01-03 16:05:15 +11001244 * Returns : -1 if selection failed but should be retried.
1245 * 0 if selection failed and should not be retried.
1246 * 0 if selection succeeded completely (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001247 *
1248 * Side effects :
1249 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * with registers as they should have been on entry - ie
1251 * SELECT_ENABLE will be set appropriately, the NCR5380
1252 * will cease to drive any SCSI bus signals.
1253 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001254 * If successful : I_T_L or I_T_L_Q nexus will be established,
1255 * instance->connected will be set to cmd.
1256 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001258 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * cmd->result host byte set to DID_BAD_TARGET.
1260 */
1261
Finn Thain710ddd02014-11-12 16:12:02 +11001262static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Finn Thaine8a60142016-01-03 16:05:54 +11001264 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001265 unsigned char tmp[3], phase;
1266 unsigned char *data;
1267 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001268 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Finn Thain8ad3a592014-03-18 11:42:19 +11001270 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001271 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001272 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Roman Zippelc28bda22007-05-01 22:32:36 +02001274 /*
1275 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1276 * data bus during SELECTION.
1277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Roman Zippelc28bda22007-05-01 22:32:36 +02001279 NCR5380_write(TARGET_COMMAND_REG, 0);
1280
1281 /*
1282 * Start arbitration.
1283 */
1284
1285 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1286 NCR5380_write(MODE_REG, MR_ARBITRATE);
1287
Finn Thain55500d92016-01-03 16:05:35 +11001288 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1289 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Finn Thain11d2f632016-01-03 16:05:51 +11001292 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001293 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1294 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1295 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001296 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001297 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1298 /* Reselection interrupt */
1299 return -1;
1300 }
1301 if (err < 0) {
1302 NCR5380_write(MODE_REG, MR_BASE);
1303 shost_printk(KERN_ERR, instance,
1304 "select: arbitration timeout\n");
1305 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001306 }
Finn Thain11d2f632016-01-03 16:05:51 +11001307 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001308
1309 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001310 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Roman Zippelc28bda22007-05-01 22:32:36 +02001312 /* Check for lost arbitration */
1313 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1314 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
Finn Thain11d2f632016-01-03 16:05:51 +11001315 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001316 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001317 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 HOSTNO);
Finn Thain11d2f632016-01-03 16:05:51 +11001319 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001320 return -1;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Finn Thaincf13b082016-01-03 16:05:18 +11001323 /* After/during arbitration, BSY should be asserted.
1324 * IBM DPES-31080 Version S31Q works now
1325 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1326 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001327 NCR5380_write(INITIATOR_COMMAND_REG,
1328 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Roman Zippelc28bda22007-05-01 22:32:36 +02001330 /*
1331 * Again, bus clear + bus settle time is 1.2us, however, this is
1332 * a minimum so we'll udelay ceil(1.2)
1333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Finn Thain9c3f0e22016-01-03 16:05:11 +11001335 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1336 udelay(15);
1337 else
1338 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001339
Finn Thain11d2f632016-01-03 16:05:51 +11001340 spin_lock_irq(&hostdata->lock);
1341
Finn Thain72064a72016-01-03 16:05:44 +11001342 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1343 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Roman Zippelc28bda22007-05-01 22:32:36 +02001344 return -1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001345
Finn Thaind65e6342014-03-18 11:42:20 +11001346 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001347
1348 /*
1349 * Now that we have won arbitration, start Selection process, asserting
1350 * the host and target ID's on the SCSI bus.
1351 */
1352
1353 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1354
1355 /*
1356 * Raise ATN while SEL is true before BSY goes false from arbitration,
1357 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1358 * phase immediately after selection.
1359 */
1360
1361 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1362 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Roman Zippelc28bda22007-05-01 22:32:36 +02001365 /*
1366 * Reselect interrupts must be turned off prior to the dropping of BSY,
1367 * otherwise we will trigger an interrupt.
1368 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001369 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Finn Thain11d2f632016-01-03 16:05:51 +11001371 spin_unlock_irq(&hostdata->lock);
1372
Roman Zippelc28bda22007-05-01 22:32:36 +02001373 /*
1374 * The initiator shall then wait at least two deskew delays and release
1375 * the BSY signal.
1376 */
1377 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Roman Zippelc28bda22007-05-01 22:32:36 +02001379 /* Reset BSY */
1380 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1381 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Roman Zippelc28bda22007-05-01 22:32:36 +02001383 /*
1384 * Something weird happens when we cease to drive BSY - looks
1385 * like the board/chip is letting us do another read before the
1386 * appropriate propagation delay has expired, and we're confusing
1387 * a BSY signal from ourselves as the target's response to SELECTION.
1388 *
1389 * A small delay (the 'C++' frontend breaks the pipeline with an
1390 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1391 * tighter 'C' code breaks and requires this) solves the problem -
1392 * the 1 us delay is arbitrary, and only used because this delay will
1393 * be the same on other platforms and since it works here, it should
1394 * work there.
1395 *
1396 * wingel suggests that this could be due to failing to wait
1397 * one deskew delay.
1398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Roman Zippelc28bda22007-05-01 22:32:36 +02001400 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Finn Thaind65e6342014-03-18 11:42:20 +11001402 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Roman Zippelc28bda22007-05-01 22:32:36 +02001404 /*
1405 * The SCSI specification calls for a 250 ms timeout for the actual
1406 * selection.
1407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Finn Thainae753a32016-01-03 16:05:23 +11001409 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1410 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Roman Zippelc28bda22007-05-01 22:32:36 +02001412 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001413 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1415 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001416 if (!hostdata->connected)
1417 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02001418 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1419 HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001420 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001422
Finn Thainae753a32016-01-03 16:05:23 +11001423 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001424 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001425 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001426 cmd->result = DID_BAD_TARGET << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001427 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001428 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001429 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001430 return 0;
1431 }
1432
Roman Zippelc28bda22007-05-01 22:32:36 +02001433 /*
Finn Thainae753a32016-01-03 16:05:23 +11001434 * No less than two deskew delays after the initiator detects the
1435 * BSY signal is true, it shall release the SEL signal and may
1436 * change the DATA BUS. -wingel
1437 */
1438
1439 udelay(1);
1440
1441 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1442
1443 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001444 * Since we followed the SCSI spec, and raised ATN while SEL
1445 * was true but before BSY was false during selection, the information
1446 * transfer phase should be a MESSAGE OUT phase so that we can send the
1447 * IDENTIFY message.
1448 *
1449 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1450 * message (2 bytes) with a tag ID that we increment with every command
1451 * until it wraps back to 0.
1452 *
1453 * XXX - it turns out that there are some broken SCSI-II devices,
1454 * which claim to support tagged queuing but fail when more than
1455 * some number of commands are issued at once.
1456 */
1457
1458 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001459
1460 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001461 spin_lock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001462 if (err < 0) {
1463 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1464 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1465 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1466 return -1;
1467 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001468
Finn Thaind65e6342014-03-18 11:42:20 +11001469 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001470 HOSTNO, cmd->device->id);
1471 tmp[0] = IDENTIFY(1, cmd->device->lun);
1472
1473#ifdef SUPPORT_TAGS
1474 if (cmd->tag != TAG_NONE) {
1475 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1476 tmp[2] = cmd->tag;
1477 len = 3;
1478 } else
1479 len = 1;
1480#else
1481 len = 1;
1482 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483#endif /* SUPPORT_TAGS */
1484
Roman Zippelc28bda22007-05-01 22:32:36 +02001485 /* Send message(s) */
1486 data = tmp;
1487 phase = PHASE_MSGOUT;
1488 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001489 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001490 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001491
Roman Zippelc28bda22007-05-01 22:32:36 +02001492 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001494 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1495#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001496#ifdef SUN3_SCSI_VME
1497 dregs->csr |= CSR_INTR;
1498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Roman Zippelc28bda22007-05-01 22:32:36 +02001500 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Roman Zippelc28bda22007-05-01 22:32:36 +02001502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Roman Zippelc28bda22007-05-01 22:32:36 +02001505/*
1506 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * unsigned char *phase, int *count, unsigned char **data)
1508 *
1509 * Purpose : transfers data in given phase using polled I/O
1510 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001511 * Inputs : instance - instance of driver, *phase - pointer to
1512 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001516 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 * is in same phase.
1518 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001519 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *
1521 * XXX Note : handling for bus free may be useful.
1522 */
1523
1524/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001525 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 * IS 100% reliable, and for the actual data transfer where speed
1527 * counts, we will always do a pseudo DMA or DMA transfer.
1528 */
1529
Roman Zippelc28bda22007-05-01 22:32:36 +02001530static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1531 unsigned char *phase, int *count,
1532 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Roman Zippelc28bda22007-05-01 22:32:36 +02001534 register unsigned char p = *phase, tmp;
1535 register int c = *count;
1536 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Roman Zippelc28bda22007-05-01 22:32:36 +02001538 /*
1539 * The NCR5380 chip will only drive the SCSI bus when the
1540 * phase specified in the appropriate bits of the TARGET COMMAND
1541 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
1543
Roman Zippelc28bda22007-05-01 22:32:36 +02001544 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Roman Zippelc28bda22007-05-01 22:32:36 +02001546 do {
1547 /*
1548 * Wait for assertion of REQ, after which the phase bits will be
1549 * valid
1550 */
Finn Thain686f3992016-01-03 16:05:26 +11001551
1552 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Finn Thaind65e6342014-03-18 11:42:20 +11001555 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Roman Zippelc28bda22007-05-01 22:32:36 +02001557 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001558 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001559 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001560 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001561 break;
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Roman Zippelc28bda22007-05-01 22:32:36 +02001564 /* Do actual transfer from SCSI bus to / from memory */
1565 if (!(p & SR_IO))
1566 NCR5380_write(OUTPUT_DATA_REG, *d);
1567 else
1568 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Roman Zippelc28bda22007-05-01 22:32:36 +02001570 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Roman Zippelc28bda22007-05-01 22:32:36 +02001572 /*
1573 * The SCSI standard suggests that in MSGOUT phase, the initiator
1574 * should drop ATN on the last byte of the message phase
1575 * after REQ has been asserted for the handshake but before
1576 * the initiator raises ACK.
1577 */
1578
1579 if (!(p & SR_IO)) {
1580 if (!((p & SR_MSG) && c > 1)) {
1581 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001582 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001583 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1584 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1585 } else {
1586 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1587 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001588 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001589 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1590 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1591 }
1592 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001593 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001594 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1595 }
1596
Finn Thaina2edc4a2016-01-03 16:05:27 +11001597 if (NCR5380_poll_politely(instance,
1598 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1599 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001600
Finn Thaind65e6342014-03-18 11:42:20 +11001601 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001602
1603 /*
1604 * We have several special cases to consider during REQ/ACK handshaking :
1605 * 1. We were in MSGOUT phase, and we are on the last byte of the
1606 * message. ATN must be dropped as ACK is dropped.
1607 *
1608 * 2. We are in a MSGIN phase, and we are on the last byte of the
1609 * message. We must exit with ACK asserted, so that the calling
1610 * code may raise ATN before dropping ACK to reject the message.
1611 *
1612 * 3. ACK and ATN are clear and the target may proceed as normal.
1613 */
1614 if (!(p == PHASE_MSGIN && c == 1)) {
1615 if (p == PHASE_MSGOUT && c > 1)
1616 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1617 else
1618 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1619 }
1620 } while (--c);
1621
Finn Thaind65e6342014-03-18 11:42:20 +11001622 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001623
1624 *count = c;
1625 *data = d;
1626 tmp = NCR5380_read(STATUS_REG);
1627 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001628 * asserted or if ACK hasn't been released yet. The latter applies if
1629 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001630 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001631 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001632 *phase = tmp & PHASE_MASK;
1633 else
1634 *phase = PHASE_UNKNOWN;
1635
1636 if (!c || (*phase == p))
1637 return 0;
1638 else
1639 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Finn Thain636b1ec2016-01-03 16:05:10 +11001642/**
1643 * do_reset - issue a reset command
1644 * @instance: adapter to reset
1645 *
1646 * Issue a reset sequence to the NCR5380 and try and get the bus
1647 * back into sane shape.
1648 *
1649 * This clears the reset interrupt flag because there may be no handler for
1650 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1651 * been installed. And when in EH we may have released the ST DMA interrupt.
1652 */
1653
1654static void do_reset(struct Scsi_Host *instance)
1655{
1656 unsigned long flags;
1657
1658 local_irq_save(flags);
1659 NCR5380_write(TARGET_COMMAND_REG,
1660 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1661 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1662 udelay(50);
1663 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1664 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1665 local_irq_restore(flags);
1666}
1667
Finn Thain80d3eb62016-01-03 16:05:34 +11001668/**
1669 * do_abort - abort the currently established nexus by going to
1670 * MESSAGE OUT phase and sending an ABORT message.
1671 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001672 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001673 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
1675
Finn Thaina53a21e2014-11-12 16:12:21 +11001676static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Roman Zippelc28bda22007-05-01 22:32:36 +02001678 unsigned char tmp, *msgptr, phase;
1679 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001680 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Roman Zippelc28bda22007-05-01 22:32:36 +02001682 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Roman Zippelc28bda22007-05-01 22:32:36 +02001685 /*
1686 * Wait for the target to indicate a valid phase by asserting
1687 * REQ. Once this happens, we'll have either a MSGOUT phase
1688 * and can immediately send the ABORT message, or we'll have some
1689 * other phase and will have to source/sink data.
1690 *
1691 * We really don't care what value was on the bus or what value
1692 * the target sees, so we just handshake.
1693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Finn Thain80d3eb62016-01-03 16:05:34 +11001695 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1696 if (rc < 0)
1697 goto timeout;
1698
1699 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001700
1701 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1702
Finn Thain80d3eb62016-01-03 16:05:34 +11001703 if (tmp != PHASE_MSGOUT) {
1704 NCR5380_write(INITIATOR_COMMAND_REG,
1705 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1706 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1707 if (rc < 0)
1708 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001709 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1710 }
1711
1712 tmp = ABORT;
1713 msgptr = &tmp;
1714 len = 1;
1715 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001716 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001717
1718 /*
1719 * If we got here, and the command completed successfully,
1720 * we're about to go into bus free state.
1721 */
1722
1723 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001724
1725timeout:
1726 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1727 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001731/*
1732 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * unsigned char *phase, int *count, unsigned char **data)
1734 *
1735 * Purpose : transfers data in given phase using either real
1736 * or pseudo DMA.
1737 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001738 * Inputs : instance - instance of driver, *phase - pointer to
1739 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001741 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001743 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * is in same phase.
1745 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001746 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *
1748 */
1749
1750
Roman Zippelc28bda22007-05-01 22:32:36 +02001751static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1752 unsigned char *phase, int *count,
1753 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Finn Thaine8a60142016-01-03 16:05:54 +11001755 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001756 register int c = *count;
1757 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001758
1759#if defined(CONFIG_SUN3)
1760 /* sanity check */
1761 if (!sun3_dma_setup_done) {
1762 pr_err("scsi%d: transfer_dma without setup!\n",
1763 instance->host_no);
1764 BUG();
1765 }
1766 hostdata->dma_len = c;
1767
1768 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1769 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1770 c, (p & SR_IO) ? "to" : "from", *data);
1771
1772 /* netbsd turns off ints here, why not be safe and do it too */
Finn Thaine3f463b2014-11-12 16:12:16 +11001773
1774 /* send start chain */
1775 sun3scsi_dma_start(c, *data);
1776
Finn Thaincd400822016-01-03 16:05:40 +11001777 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1778 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1779 MR_ENABLE_EOP_INTR);
Finn Thaine3f463b2014-11-12 16:12:16 +11001780 if (p & SR_IO) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001781 NCR5380_write(INITIATOR_COMMAND_REG, 0);
Finn Thaine3f463b2014-11-12 16:12:16 +11001782 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1783 } else {
Finn Thaine3f463b2014-11-12 16:12:16 +11001784 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
Finn Thaine3f463b2014-11-12 16:12:16 +11001785 NCR5380_write(START_DMA_SEND_REG, 0);
1786 }
1787
1788#ifdef SUN3_SCSI_VME
1789 dregs->csr |= CSR_DMA_ENABLE;
1790#endif
1791
Finn Thaine3f463b2014-11-12 16:12:16 +11001792 sun3_dma_active = 1;
1793
1794#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001795 register unsigned char *d = *data;
1796 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Roman Zippelc28bda22007-05-01 22:32:36 +02001798 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1799 *phase = tmp;
1800 return -1;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Finn Thainef1081c2014-11-12 16:12:14 +11001803 if (hostdata->read_overruns && (p & SR_IO))
1804 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Finn Thaind65e6342014-03-18 11:42:20 +11001806 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001807 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1808 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Roman Zippelc28bda22007-05-01 22:32:36 +02001810 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thaincd400822016-01-03 16:05:40 +11001811 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1812 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Finn Thainef1081c2014-11-12 16:12:14 +11001814 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001815 /* On the Medusa, it is a must to initialize the DMA before
1816 * starting the NCR. This is also the cleaner way for the TT.
1817 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001818 hostdata->dma_len = (p & SR_IO) ?
1819 NCR5380_dma_read_setup(instance, d, c) :
1820 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Roman Zippelc28bda22007-05-01 22:32:36 +02001823 if (p & SR_IO)
1824 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1825 else {
1826 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1827 NCR5380_write(START_DMA_SEND_REG, 0);
1828 }
1829
Finn Thainef1081c2014-11-12 16:12:14 +11001830 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001831 /* On the Falcon, the DMA setup must be done after the last */
1832 /* NCR access, else the DMA setup gets trashed!
1833 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001834 hostdata->dma_len = (p & SR_IO) ?
1835 NCR5380_dma_read_setup(instance, d, c) :
1836 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001837 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001838#endif /* !defined(CONFIG_SUN3) */
1839
Roman Zippelc28bda22007-05-01 22:32:36 +02001840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842#endif /* defined(REAL_DMA) */
1843
1844/*
1845 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1846 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001847 * Purpose : run through the various SCSI phases and do as the target
1848 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * instance->connected.
1850 *
1851 * Inputs : instance, instance for which we are doing commands
1852 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001853 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * modified if a command disconnects, *instance->connected will
1855 * change.
1856 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001857 * XXX Note : we need to watch for bus free or a reset condition here
1858 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001860
1861static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Finn Thaine8a60142016-01-03 16:05:54 +11001863 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001864 unsigned char msgout = NOP;
1865 int sink = 0;
1866 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001868 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001870 unsigned char *data;
1871 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001872 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Finn Thaine3f463b2014-11-12 16:12:16 +11001874#ifdef SUN3_SCSI_VME
1875 dregs->csr |= CSR_INTR;
1876#endif
1877
Finn Thain11d2f632016-01-03 16:05:51 +11001878 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001879 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1880
Roman Zippelc28bda22007-05-01 22:32:36 +02001881 tmp = NCR5380_read(STATUS_REG);
1882 /* We only have a valid SCSI phase when REQ is asserted */
1883 if (tmp & SR_REQ) {
1884 phase = (tmp & PHASE_MASK);
1885 if (phase != old_phase) {
1886 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001887 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001889#if defined(CONFIG_SUN3)
1890 if (phase == PHASE_CMDOUT) {
1891#if defined(REAL_DMA)
1892 void *d;
1893 unsigned long count;
1894
1895 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1896 count = cmd->SCp.buffer->length;
1897 d = sg_virt(cmd->SCp.buffer);
1898 } else {
1899 count = cmd->SCp.this_residual;
1900 d = cmd->SCp.ptr;
1901 }
1902 /* this command setup for dma yet? */
1903 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
1904 if (cmd->request->cmd_type == REQ_TYPE_FS) {
1905 sun3scsi_dma_setup(d, count,
1906 rq_data_dir(cmd->request));
1907 sun3_dma_setup_done = cmd;
1908 }
1909 }
1910#endif
1911#ifdef SUN3_SCSI_VME
1912 dregs->csr |= CSR_INTR;
1913#endif
1914 }
1915#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Roman Zippelc28bda22007-05-01 22:32:36 +02001917 if (sink && (phase != PHASE_MSGOUT)) {
1918 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Roman Zippelc28bda22007-05-01 22:32:36 +02001920 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1921 ICR_ASSERT_ACK);
1922 while (NCR5380_read(STATUS_REG) & SR_REQ)
1923 ;
1924 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1925 ICR_ASSERT_ATN);
1926 sink = 0;
1927 continue;
1928 }
1929
1930 switch (phase) {
1931 case PHASE_DATAOUT:
1932#if (NDEBUG & NDEBUG_NO_DATAOUT)
1933 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
1934 "aborted\n", HOSTNO);
1935 sink = 1;
1936 do_abort(instance);
1937 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001938 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001939 return;
1940#endif
1941 case PHASE_DATAIN:
1942 /*
1943 * If there is no room left in the current buffer in the
1944 * scatter-gather list, move onto the next one.
1945 */
1946
1947 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1948 ++cmd->SCp.buffer;
1949 --cmd->SCp.buffers_residual;
1950 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001951 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02001952 /* ++roman: Try to merge some scatter-buffers if
1953 * they are at contiguous physical addresses.
1954 */
1955 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11001956 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001957 HOSTNO, cmd->SCp.this_residual,
1958 cmd->SCp.buffers_residual);
1959 }
1960
1961 /*
1962 * The preferred transfer method is going to be
1963 * PSEUDO-DMA for systems that are strictly PIO,
1964 * since we can let the hardware do the handshaking.
1965 *
1966 * For this to work, we need to know the transfersize
1967 * ahead of time, since the pseudo-DMA code will sit
1968 * in an unconditional loop.
1969 */
1970
1971 /* ++roman: I suggest, this should be
1972 * #if def(REAL_DMA)
1973 * instead of leaving REAL_DMA out.
1974 */
1975
1976#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11001977#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11001978 transfersize = 0;
1979 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11001980#endif
Finn Thainff3d4572016-01-03 16:05:25 +11001981 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1982
1983 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001984 len = transfersize;
1985 cmd->SCp.phase = phase;
1986 if (NCR5380_transfer_dma(instance, &phase,
1987 &len, (unsigned char **)&cmd->SCp.ptr)) {
1988 /*
1989 * If the watchdog timer fires, all future
1990 * accesses to this device will use the
1991 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11001992 scmd_printk(KERN_INFO, cmd,
1993 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001994 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001995 sink = 1;
1996 do_abort(instance);
1997 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001998 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001999 /* XXX - need to source or sink data here, as appropriate */
2000 } else {
2001#ifdef REAL_DMA
2002 /* ++roman: When using real DMA,
2003 * information_transfer() should return after
2004 * starting DMA since it has nothing more to
2005 * do.
2006 */
2007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002009 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002011 }
2012 } else
2013#endif /* defined(REAL_DMA) */
Finn Thain11d2f632016-01-03 16:05:51 +11002014 {
2015 spin_unlock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02002016 NCR5380_transfer_pio(instance, &phase,
2017 (int *)&cmd->SCp.this_residual,
2018 (unsigned char **)&cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11002019 spin_lock_irq(&hostdata->lock);
2020 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002021#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2022 /* if we had intended to dma that command clear it */
2023 if (sun3_dma_setup_done == cmd)
2024 sun3_dma_setup_done = NULL;
2025#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002026 break;
2027 case PHASE_MSGIN:
2028 len = 1;
2029 data = &tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002030 NCR5380_transfer_pio(instance, &phase, &len, &data);
2031 cmd->SCp.Message = tmp;
2032
2033 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002034 case ABORT:
2035 case COMMAND_COMPLETE:
2036 /* Accept message by clearing ACK */
2037 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002038 dsprintk(NDEBUG_QUEUES, instance,
2039 "COMMAND COMPLETE %p target %d lun %llu\n",
2040 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002041
Finn Thaine3c3da62014-11-12 16:12:15 +11002042 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002043#ifdef SUPPORT_TAGS
2044 cmd_free_tag(cmd);
2045 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
Finn Thain61d739a2014-11-12 16:12:20 +11002046 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002047 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02002048 "QUEUE_FULL after %d commands\n",
2049 HOSTNO, cmd->device->id, cmd->device->lun,
2050 ta->nr_allocated);
2051 if (ta->queue_size > ta->nr_allocated)
Finn Thaine42d0152016-01-03 16:05:49 +11002052 ta->queue_size = ta->nr_allocated;
Roman Zippelc28bda22007-05-01 22:32:36 +02002053 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002054#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002055
Finn Thainf27db8e2016-01-03 16:06:00 +11002056 cmd->result &= ~0xffff;
2057 cmd->result |= cmd->SCp.Status;
2058 cmd->result |= cmd->SCp.Message << 8;
Roman Zippelc28bda22007-05-01 22:32:36 +02002059
Finn Thainf27db8e2016-01-03 16:06:00 +11002060 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11002061 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11002062 else {
2063 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
2064 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
2065 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
2066 cmd);
2067 list_add_tail(&ncmd->list,
2068 &hostdata->autosense);
2069 } else
2070 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002071 }
2072
Roman Zippelc28bda22007-05-01 22:32:36 +02002073 /*
2074 * Restore phase bits to 0 so an interrupted selection,
2075 * arbitration can resume.
2076 */
2077 NCR5380_write(TARGET_COMMAND_REG, 0);
2078
Finn Thain72064a72016-01-03 16:05:44 +11002079 /* Enable reselect interrupts */
2080 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2081
Roman Zippelc28bda22007-05-01 22:32:36 +02002082 /* ++roman: For Falcon SCSI, release the lock on the
2083 * ST-DMA here if no other commands are waiting on the
2084 * disconnected queue.
2085 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002086 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002087 return;
2088 case MESSAGE_REJECT:
2089 /* Accept message by clearing ACK */
2090 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002091 switch (hostdata->last_message) {
2092 case HEAD_OF_QUEUE_TAG:
2093 case ORDERED_QUEUE_TAG:
2094 case SIMPLE_QUEUE_TAG:
2095 /* The target obviously doesn't support tagged
2096 * queuing, even though it announced this ability in
2097 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2098 * clear 'tagged_supported' and lock the LUN, since
2099 * the command is treated as untagged further on.
2100 */
2101 cmd->device->tagged_supported = 0;
2102 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2103 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002104 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002105 "QUEUE_TAG message; tagged queuing "
2106 "disabled\n",
2107 HOSTNO, cmd->device->id, cmd->device->lun);
2108 break;
2109 }
2110 break;
2111 case DISCONNECT:
2112 /* Accept message by clearing ACK */
2113 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002114 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11002115 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002116 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
2117 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
2118 cmd, scmd_id(cmd), cmd->device->lun);
2119
Roman Zippelc28bda22007-05-01 22:32:36 +02002120 /*
2121 * Restore phase bits to 0 so an interrupted selection,
2122 * arbitration can resume.
2123 */
2124 NCR5380_write(TARGET_COMMAND_REG, 0);
2125
2126 /* Enable reselect interrupts */
2127 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002128#ifdef SUN3_SCSI_VME
2129 dregs->csr |= CSR_DMA_ENABLE;
2130#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002131 return;
2132 /*
2133 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2134 * operation, in violation of the SCSI spec so we can safely
2135 * ignore SAVE/RESTORE pointers calls.
2136 *
2137 * Unfortunately, some disks violate the SCSI spec and
2138 * don't issue the required SAVE_POINTERS message before
2139 * disconnecting, and we have to break spec to remain
2140 * compatible.
2141 */
2142 case SAVE_POINTERS:
2143 case RESTORE_POINTERS:
2144 /* Accept message by clearing ACK */
2145 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002146 break;
2147 case EXTENDED_MESSAGE:
2148 /*
2149 * Extended messages are sent in the following format :
2150 * Byte
2151 * 0 EXTENDED_MESSAGE == 1
2152 * 1 length (includes one byte for code, doesn't
2153 * include first two bytes)
2154 * 2 code
2155 * 3..length+1 arguments
2156 *
2157 * Start the extended message buffer with the EXTENDED_MESSAGE
2158 * byte, since spi_print_msg() wants the whole thing.
2159 */
2160 extended_msg[0] = EXTENDED_MESSAGE;
2161 /* Accept first byte by clearing ACK */
2162 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2163
Finn Thain11d2f632016-01-03 16:05:51 +11002164 spin_unlock_irq(&hostdata->lock);
2165
Finn Thaind65e6342014-03-18 11:42:20 +11002166 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002167
2168 len = 2;
2169 data = extended_msg + 1;
2170 phase = PHASE_MSGIN;
2171 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002172 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002173 (int)extended_msg[1], (int)extended_msg[2]);
2174
Finn Thaine0783ed2016-01-03 16:05:45 +11002175 if (!len && extended_msg[1] > 0 &&
2176 extended_msg[1] <= sizeof(extended_msg) - 2) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002177 /* Accept third byte by clearing ACK */
2178 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2179 len = extended_msg[1] - 1;
2180 data = extended_msg + 3;
2181 phase = PHASE_MSGIN;
2182
2183 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002184 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002185 HOSTNO, len);
2186
2187 switch (extended_msg[2]) {
2188 case EXTENDED_SDTR:
2189 case EXTENDED_WDTR:
2190 case EXTENDED_MODIFY_DATA_POINTER:
2191 case EXTENDED_EXTENDED_IDENTIFY:
2192 tmp = 0;
2193 }
2194 } else if (len) {
2195 printk(KERN_NOTICE "scsi%d: error receiving "
2196 "extended message\n", HOSTNO);
2197 tmp = 0;
2198 } else {
2199 printk(KERN_NOTICE "scsi%d: extended message "
2200 "code %02x length %d is too long\n",
2201 HOSTNO, extended_msg[2], extended_msg[1]);
2202 tmp = 0;
2203 }
Finn Thain11d2f632016-01-03 16:05:51 +11002204
2205 spin_lock_irq(&hostdata->lock);
2206 if (!hostdata->connected)
2207 return;
2208
Roman Zippelc28bda22007-05-01 22:32:36 +02002209 /* Fall through to reject message */
2210
2211 /*
2212 * If we get something weird that we aren't expecting,
2213 * reject it.
2214 */
2215 default:
2216 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002217 printk(KERN_INFO "scsi%d: rejecting message ",
2218 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002219 spi_print_msg(extended_msg);
2220 printk("\n");
2221 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002222 scmd_printk(KERN_INFO, cmd,
2223 "rejecting unknown message %02x\n",
2224 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002225 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002226 scmd_printk(KERN_INFO, cmd,
2227 "rejecting unknown extended message code %02x, length %d\n",
2228 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002229
2230 msgout = MESSAGE_REJECT;
2231 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2232 break;
2233 } /* switch (tmp) */
2234 break;
2235 case PHASE_MSGOUT:
2236 len = 1;
2237 data = &msgout;
2238 hostdata->last_message = msgout;
2239 NCR5380_transfer_pio(instance, &phase, &len, &data);
2240 if (msgout == ABORT) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002241 hostdata->connected = NULL;
2242 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002243 complete_cmd(instance, cmd);
Finn Thaine3c3da62014-11-12 16:12:15 +11002244 maybe_release_dma_irq(instance);
Finn Thaincd400822016-01-03 16:05:40 +11002245 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02002246 return;
2247 }
2248 msgout = NOP;
2249 break;
2250 case PHASE_CMDOUT:
2251 len = cmd->cmd_len;
2252 data = cmd->cmnd;
2253 /*
2254 * XXX for performance reasons, on machines with a
2255 * PSEUDO-DMA architecture we should probably
2256 * use the dma transfer function.
2257 */
2258 NCR5380_transfer_pio(instance, &phase, &len, &data);
2259 break;
2260 case PHASE_STATIN:
2261 len = 1;
2262 data = &tmp;
2263 NCR5380_transfer_pio(instance, &phase, &len, &data);
2264 cmd->SCp.Status = tmp;
2265 break;
2266 default:
2267 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002268 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002269 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002270 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002271 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002272 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002273 spin_lock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002274 }
Finn Thain11d2f632016-01-03 16:05:51 +11002275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278/*
2279 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2280 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002281 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002282 * 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 -07002283 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * Inputs : instance - this instance of the NCR5380.
2286 *
2287 */
2288
2289
Finn Thaine3f463b2014-11-12 16:12:16 +11002290/* it might eventually prove necessary to do a dma setup on
2291 reselection, but it doesn't seem to be needed now -- sam */
2292
Roman Zippelc28bda22007-05-01 22:32:36 +02002293static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Finn Thaine8a60142016-01-03 16:05:54 +11002295 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002296 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002297 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002299 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002301 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002302 int __maybe_unused len;
2303 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain32b26a12016-01-03 16:05:58 +11002304 struct NCR5380_cmd *ncmd;
2305 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Roman Zippelc28bda22007-05-01 22:32:36 +02002307 /*
2308 * Disable arbitration, etc. since the host adapter obviously
2309 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Roman Zippelc28bda22007-05-01 22:32:36 +02002312 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Roman Zippelc28bda22007-05-01 22:32:36 +02002314 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2315
Finn Thaind65e6342014-03-18 11:42:20 +11002316 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002317
2318 /*
2319 * At this point, we have detected that our SCSI ID is on the bus,
2320 * SEL is true and BSY was false for at least one bus settle delay
2321 * (400 ns).
2322 *
2323 * We must assert BSY ourselves, until the target drops the SEL
2324 * signal.
2325 */
2326
2327 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002328 if (NCR5380_poll_politely(instance,
2329 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2330 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2331 return;
2332 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002333 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2334
2335 /*
2336 * Wait for target to go into MSGIN.
2337 */
2338
Finn Thain72064a72016-01-03 16:05:44 +11002339 if (NCR5380_poll_politely(instance,
2340 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2341 do_abort(instance);
2342 return;
2343 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002344
Finn Thaine3f463b2014-11-12 16:12:16 +11002345#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2346 /* acknowledge toggle to MSGIN */
2347 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2348
2349 /* peek at the byte without really hitting the bus */
2350 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2351#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002352 len = 1;
2353 data = msg;
2354 phase = PHASE_MSGIN;
2355 NCR5380_transfer_pio(instance, &phase, &len, &data);
2356
Finn Thain72064a72016-01-03 16:05:44 +11002357 if (len) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002358 do_abort(instance);
2359 return;
2360 }
Finn Thain72064a72016-01-03 16:05:44 +11002361#endif
2362
2363 if (!(msg[0] & 0x80)) {
2364 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2365 spi_print_msg(msg);
2366 printk("\n");
2367 do_abort(instance);
2368 return;
2369 }
2370 lun = msg[0] & 0x07;
Roman Zippelc28bda22007-05-01 22:32:36 +02002371
Finn Thaine3f463b2014-11-12 16:12:16 +11002372#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002373 /* If the phase is still MSGIN, the target wants to send some more
2374 * messages. In case it supports tagged queuing, this is probably a
2375 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2376 */
2377 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002378 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002379 /* Accept previous IDENTIFY message by clearing ACK */
2380 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2381 len = 2;
2382 data = msg + 1;
2383 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2384 msg[1] == SIMPLE_QUEUE_TAG)
2385 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002386 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002387 "reselection\n", HOSTNO, target_mask, lun, tag);
2388 }
2389#endif
2390
2391 /*
2392 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2393 * just reestablished, and remove it from the disconnected queue.
2394 */
2395
Finn Thain32b26a12016-01-03 16:05:58 +11002396 tmp = NULL;
2397 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2398 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2399
2400 if (target_mask == (1 << scmd_id(cmd)) &&
2401 lun == (u8)cmd->device->lun
Roman Zippelc28bda22007-05-01 22:32:36 +02002402#ifdef SUPPORT_TAGS
Finn Thain32b26a12016-01-03 16:05:58 +11002403 && (tag == cmd->tag)
Roman Zippelc28bda22007-05-01 22:32:36 +02002404#endif
2405 ) {
Finn Thain32b26a12016-01-03 16:05:58 +11002406 list_del(&ncmd->list);
2407 tmp = cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +02002408 break;
2409 }
2410 }
2411
Finn Thain0d3d9a42016-01-03 16:05:55 +11002412 if (tmp) {
2413 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2414 "reselect: removed %p from disconnected queue\n", tmp);
2415 } else {
2416
Roman Zippelc28bda22007-05-01 22:32:36 +02002417#ifdef SUPPORT_TAGS
Finn Thain72064a72016-01-03 16:05:44 +11002418 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d tag %d not in disconnected queue.\n",
2419 target_mask, lun, tag);
2420#else
2421 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2422 target_mask, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002423#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002424 /*
2425 * Since we have an established nexus that we can't do anything
2426 * with, we must abort it.
2427 */
2428 do_abort(instance);
2429 return;
2430 }
2431
Finn Thaine3f463b2014-11-12 16:12:16 +11002432#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2433 /* engage dma setup for the command we just saw */
2434 {
2435 void *d;
2436 unsigned long count;
2437
2438 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2439 count = tmp->SCp.buffer->length;
2440 d = sg_virt(tmp->SCp.buffer);
2441 } else {
2442 count = tmp->SCp.this_residual;
2443 d = tmp->SCp.ptr;
2444 }
2445 /* setup this command for dma if not already */
2446 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2447 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2448 sun3_dma_setup_done = tmp;
2449 }
2450 }
2451
2452 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2453#endif
2454
Roman Zippelc28bda22007-05-01 22:32:36 +02002455 /* Accept message by clearing ACK */
2456 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2457
Finn Thaine3f463b2014-11-12 16:12:16 +11002458#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2459 /* If the phase is still MSGIN, the target wants to send some more
2460 * messages. In case it supports tagged queuing, this is probably a
2461 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2462 */
2463 tag = TAG_NONE;
2464 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2465 /* Accept previous IDENTIFY message by clearing ACK */
2466 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2467 len = 2;
2468 data = msg + 1;
2469 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2470 msg[1] == SIMPLE_QUEUE_TAG)
2471 tag = msg[2];
2472 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2473 HOSTNO, target_mask, lun, tag);
2474 }
2475#endif
2476
Roman Zippelc28bda22007-05-01 22:32:36 +02002477 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002478 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002479 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
2482
Finn Thain8b00c3d2016-01-03 16:06:01 +11002483/**
2484 * list_find_cmd - test for presence of a command in a linked list
2485 * @haystack: list of commands
2486 * @needle: command to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 */
2488
Finn Thain8b00c3d2016-01-03 16:06:01 +11002489static bool list_find_cmd(struct list_head *haystack,
2490 struct scsi_cmnd *needle)
2491{
2492 struct NCR5380_cmd *ncmd;
2493
2494 list_for_each_entry(ncmd, haystack, list)
2495 if (NCR5380_to_scmd(ncmd) == needle)
2496 return true;
2497 return false;
2498}
2499
2500/**
2501 * list_remove_cmd - remove a command from linked list
2502 * @haystack: list of commands
2503 * @needle: command to remove
2504 */
2505
2506static bool list_del_cmd(struct list_head *haystack,
2507 struct scsi_cmnd *needle)
2508{
2509 if (list_find_cmd(haystack, needle)) {
2510 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2511
2512 list_del(&ncmd->list);
2513 return true;
2514 }
2515 return false;
2516}
2517
2518/**
2519 * NCR5380_abort - scsi host eh_abort_handler() method
2520 * @cmd: the command to be aborted
2521 *
2522 * Try to abort a given command by removing it from queues and/or sending
2523 * the target an abort message. This may not succeed in causing a target
2524 * to abort the command. Nonetheless, the low-level driver must forget about
2525 * the command because the mid-layer reclaims it and it may be re-issued.
2526 *
2527 * The normal path taken by a command is as follows. For EH we trace this
2528 * same path to locate and abort the command.
2529 *
2530 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2531 * [disconnected -> connected ->]...
2532 * [autosense -> connected ->] done
2533 *
2534 * If cmd is unissued then just remove it.
2535 * If cmd is disconnected, try to select the target.
2536 * If cmd is connected, try to send an abort message.
2537 * If cmd is waiting for autosense, give it a chance to complete but check
2538 * that it isn't left connected.
2539 * If cmd was not found at all then presumably it has already been completed,
2540 * in which case return SUCCESS to try to avoid further EH measures.
2541 * If the command has not completed yet, we must not fail to find it.
2542 */
2543
2544static int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Roman Zippelc28bda22007-05-01 22:32:36 +02002546 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002547 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002548 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002549 int result = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Finn Thain11d2f632016-01-03 16:05:51 +11002551 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Finn Thain32b26a12016-01-03 16:05:58 +11002553#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002554 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002555#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002556 NCR5380_dprint(NDEBUG_ANY, instance);
2557 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Finn Thain8b00c3d2016-01-03 16:06:01 +11002559 if (list_del_cmd(&hostdata->unissued, cmd)) {
2560 dsprintk(NDEBUG_ABORT, instance,
2561 "abort: removed %p from issue queue\n", cmd);
2562 cmd->result = DID_ABORT << 16;
2563 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2564 }
2565
2566 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2567 dsprintk(NDEBUG_ABORT, instance,
2568 "abort: removed %p from disconnected list\n", cmd);
2569 cmd->result = DID_ERROR << 16;
2570 if (!hostdata->connected)
2571 NCR5380_select(instance, cmd);
2572 if (hostdata->connected != cmd) {
2573 complete_cmd(instance, cmd);
2574 result = FAILED;
2575 goto out;
2576 }
2577 }
2578
2579 if (hostdata->connected == cmd) {
2580 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2581 hostdata->connected = NULL;
2582 if (do_abort(instance)) {
2583 set_host_byte(cmd, DID_ERROR);
2584 complete_cmd(instance, cmd);
2585 result = FAILED;
2586 goto out;
2587 }
2588 set_host_byte(cmd, DID_ABORT);
2589#ifdef REAL_DMA
2590 hostdata->dma_len = 0;
2591#endif
2592 if (cmd->cmnd[0] == REQUEST_SENSE)
2593 complete_cmd(instance, cmd);
2594 else {
2595 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
2596
2597 /* Perform autosense for this command */
2598 list_add(&ncmd->list, &hostdata->autosense);
2599 }
2600 }
2601
2602 if (list_find_cmd(&hostdata->autosense, cmd)) {
2603 dsprintk(NDEBUG_ABORT, instance,
2604 "abort: found %p on sense queue\n", cmd);
2605 spin_unlock_irqrestore(&hostdata->lock, flags);
2606 queue_work(hostdata->work_q, &hostdata->main_task);
2607 msleep(1000);
2608 spin_lock_irqsave(&hostdata->lock, flags);
2609 if (list_del_cmd(&hostdata->autosense, cmd)) {
2610 dsprintk(NDEBUG_ABORT, instance,
2611 "abort: removed %p from sense queue\n", cmd);
2612 set_host_byte(cmd, DID_ABORT);
2613 complete_cmd(instance, cmd);
2614 goto out;
2615 }
2616 }
2617
2618 if (hostdata->connected == cmd) {
2619 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2620 hostdata->connected = NULL;
2621 if (do_abort(instance)) {
2622 set_host_byte(cmd, DID_ERROR);
2623 complete_cmd(instance, cmd);
2624 result = FAILED;
2625 goto out;
2626 }
2627 set_host_byte(cmd, DID_ABORT);
2628#ifdef REAL_DMA
2629 hostdata->dma_len = 0;
2630#endif
2631 complete_cmd(instance, cmd);
2632 }
2633
2634out:
2635 if (result == FAILED)
2636 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2637 else
2638 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2639
2640 queue_work(hostdata->work_q, &hostdata->main_task);
2641 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002642 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002643
Finn Thain8b00c3d2016-01-03 16:06:01 +11002644 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
2647
Finn Thain3be1b3e2016-01-03 16:05:12 +11002648/**
2649 * NCR5380_bus_reset - reset the SCSI bus
2650 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002651 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002652 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Finn Thain710ddd02014-11-12 16:12:02 +11002655static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Finn Thaine3c3da62014-11-12 16:12:15 +11002657 struct Scsi_Host *instance = cmd->device->host;
2658 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002659 int i;
2660 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Finn Thain11d2f632016-01-03 16:05:51 +11002662 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Finn Thain3be1b3e2016-01-03 16:05:12 +11002664#if (NDEBUG & NDEBUG_ANY)
2665 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
Finn Thain3be1b3e2016-01-03 16:05:12 +11002666#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002667 NCR5380_dprint(NDEBUG_ANY, instance);
2668 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002669
2670 do_reset(instance);
2671
Roman Zippelc28bda22007-05-01 22:32:36 +02002672 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002673 NCR5380_write(MODE_REG, MR_BASE);
2674 NCR5380_write(TARGET_COMMAND_REG, 0);
2675 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Roman Zippelc28bda22007-05-01 22:32:36 +02002677 /* After the reset, there are no more connected or disconnected commands
2678 * and no busy units; so clear the low-level status here to avoid
2679 * conflicts when the mid-level code tries to wake up the affected
2680 * commands!
2681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Roman Zippelc28bda22007-05-01 22:32:36 +02002683 if (hostdata->connected)
Finn Thaindbb6b352016-01-03 16:05:53 +11002684 dsprintk(NDEBUG_ABORT, instance, "reset aborted a connected command\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002685 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11002686
Finn Thainf27db8e2016-01-03 16:06:00 +11002687 if (hostdata->sensing) {
2688 complete_cmd(instance, hostdata->sensing);
2689 hostdata->sensing = NULL;
2690 }
2691
2692 if (!list_empty(&hostdata->autosense))
2693 dsprintk(NDEBUG_ABORT, instance, "reset aborted autosense list\n");
2694 INIT_LIST_HEAD(&hostdata->autosense);
2695
Finn Thain32b26a12016-01-03 16:05:58 +11002696 if (!list_empty(&hostdata->unissued))
2697 dsprintk(NDEBUG_ABORT, instance, "reset aborted unissued list\n");
2698 INIT_LIST_HEAD(&hostdata->unissued);
2699
2700 if (!list_empty(&hostdata->disconnected))
2701 dsprintk(NDEBUG_ABORT, instance, "reset aborted disconnected list\n");
2702 INIT_LIST_HEAD(&hostdata->disconnected);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002705 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002707 for (i = 0; i < 8; ++i)
2708 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002710 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002712
2713 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002714 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002716 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}