blob: b66ea632c76bf4f68b27f9ac3f1d72319c4a5c02 [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*
Finn Thain594d4ba2016-01-03 16:06:10 +11003 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Finn Thain594d4ba2016-01-03 16:06:10 +11006 * Note that these routines also work with NR53c400 family chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Copyright 1993, Drew Eckhardt
Finn Thain594d4ba2016-01-03 16:06:10 +11009 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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
Finn Thainc16df322016-01-03 16:06:08 +110027/* Ported to Atari by Roman Hodek and others. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Finn Thaine3f463b2014-11-12 16:12:16 +110029/* Adapted for the sun3 by Sam Creasey. */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
32 * Design
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
Roman Zippelc28bda22007-05-01 22:32:36 +020034 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +020036 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * memory mapped) and drops this file in their 'C' wrapper.
38 *
Roman Zippelc28bda22007-05-01 22:32:36 +020039 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +020041 * and commands that are currently executing. This means that an
42 * unlimited number of commands may be queued, letting
43 * more commands propagate from the higher driver levels giving higher
44 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
45 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * while a command is already executing.
47 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
Roman Zippelc28bda22007-05-01 22:32:36 +020049 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 *
Roman Zippelc28bda22007-05-01 22:32:36 +020051 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
52 * piece of hardware that requires you to sit in a loop polling for
53 * the REQ signal as long as you are connected. Some devices are
54 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110055 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * broken devices are the exception rather than the rule and I'd rather
57 * spend my time optimizing for the normal case.
58 *
59 * Architecture :
60 *
61 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +110062 * which is started from a workqueue for each NCR5380 host in the
63 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
64 * removing the commands from the issue queue and calling
65 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 * Once a nexus is established, the NCR5380_information_transfer()
68 * phase goes through the various phases as instructed by the target.
69 * if the target goes into MSG IN and sends a DISCONNECT message,
70 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +110071 * queue, and NCR5380_main tries to find more work. If the target is
72 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * If a command has disconnected, eventually an interrupt will trigger,
75 * calling NCR5380_intr() which will in turn call NCR5380_reselect
76 * to reestablish a nexus. This will run main if necessary.
77 *
Roman Zippelc28bda22007-05-01 22:32:36 +020078 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * appropriate.
80 *
Roman Zippelc28bda22007-05-01 22:32:36 +020081 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * structures, being initialized after the command is connected
83 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
84 * Note that in violation of the standard, an implicit SAVE POINTERS operation
85 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
86 */
87
88/*
89 * Using this file :
90 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +020091 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * and macros and include this file in your driver.
93 *
Roman Zippelc28bda22007-05-01 22:32:36 +020094 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Finn Thain594d4ba2016-01-03 16:06:10 +110096 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Finn Thainff50f9e2014-11-12 16:12:18 +110098 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
Finn Thain594d4ba2016-01-03 16:06:10 +110099 * transceivers.
Finn Thainff50f9e2014-11-12 16:12:18 +1100100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
102 *
103 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
104 *
105 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200106 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * NCR5380_read(register) - read from the specified register
108 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200109 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100111 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100112 * specific implementation of the NCR5380
Finn Thainff50f9e2014-11-12 16:12:18 +1100113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200115 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200117 * Note that the DMA setup functions should return the number of bytes
Finn Thain594d4ba2016-01-03 16:06:10 +1100118 * that they were able to program the controller for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200120 * Also note that generic i386/PC versions of these macros are
Finn Thain594d4ba2016-01-03 16:06:10 +1100121 * available as NCR5380_i386_dma_write_setup,
122 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
124 * NCR5380_dma_write_setup(instance, src, count) - initialize
125 * NCR5380_dma_read_setup(instance, dst, count) - initialize
126 * NCR5380_dma_residual(instance); - residual count
127 *
128 * PSEUDO functions :
129 * NCR5380_pwrite(instance, src, count)
130 * NCR5380_pread(instance, dst, count);
131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200133 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100135 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
137
Finn Thain636b1ec2016-01-03 16:05:10 +1100138static int do_abort(struct Scsi_Host *);
139static void do_reset(struct Scsi_Host *);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#ifdef SUPPORT_TAGS
142
143/*
144 * Functions for handling tagged queuing
145 * =====================================
146 *
147 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
148 *
149 * Using consecutive numbers for the tags is no good idea in my eyes. There
150 * could be wrong re-usings if the counter (8 bit!) wraps and some early
151 * command has been preempted for a long time. My solution: a bitfield for
152 * remembering used tags.
153 *
154 * There's also the problem that each target has a certain queue size, but we
155 * cannot know it in advance :-( We just see a QUEUE_FULL status being
156 * returned. So, in this case, the driver internal queue size assumption is
157 * reduced to the number of active tags if QUEUE_FULL is returned by the
Finn Thaine42d0152016-01-03 16:05:49 +1100158 * target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * We're also not allowed running tagged commands as long as an untagged
161 * command is active. And REQUEST SENSE commands after a contingent allegiance
162 * condition _must_ be untagged. To keep track whether an untagged command has
163 * been issued, the host->busy array is still employed, as it is without
164 * support for tagged queuing.
165 *
166 * One could suspect that there are possible race conditions between
167 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
168 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
169 * which already guaranteed to be running at most once. It is also the only
170 * place where tags/LUNs are allocated. So no other allocation can slip
171 * between that pair, there could only happen a reselection, which can free a
172 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
173 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
174 */
175
Finn Thainca513fc2014-11-12 16:12:19 +1100176static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Roman Zippelc28bda22007-05-01 22:32:36 +0200178 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100179 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200180
Finn Thainca513fc2014-11-12 16:12:19 +1100181 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200182 return;
183
184 for (target = 0; target < 8; ++target) {
185 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100186 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200187 bitmap_zero(ta->allocated, MAX_TAGS);
188 ta->nr_allocated = 0;
189 /* At the beginning, assume the maximum queue size we could
190 * support (MAX_TAGS). This value will be decreased if the target
191 * returns QUEUE_FULL status.
192 */
193 ta->queue_size = MAX_TAGS;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198
199/* Check if we can issue a command to this LUN: First see if the LUN is marked
200 * busy by an untagged command. If the command should use tagged queuing, also
201 * check that there is a free tag and the target's queue won't overflow. This
202 * function should be called with interrupts disabled to avoid race
203 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Finn Thain710ddd02014-11-12 16:12:02 +1100206static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200208 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100209 struct Scsi_Host *instance = cmd->device->host;
210 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200212 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200213 return 1;
214 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100215 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
216 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200217 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100218 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
219 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100220 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d: no free tags\n",
221 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200222 return 1;
223 }
224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227
228/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
229 * must be called before!), or reserve the LUN in 'busy' if the command is
230 * untagged.
231 */
232
Finn Thain710ddd02014-11-12 16:12:02 +1100233static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200235 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100236 struct Scsi_Host *instance = cmd->device->host;
237 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Roman Zippelc28bda22007-05-01 22:32:36 +0200239 /* If we or the target don't support tagged queuing, allocate the LUN for
240 * an untagged command.
241 */
242 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100243 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
244 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200245 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200246 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100247 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d now allocated by untagged command\n",
248 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200249 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100250 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Roman Zippelc28bda22007-05-01 22:32:36 +0200252 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
253 set_bit(cmd->tag, ta->allocated);
254 ta->nr_allocated++;
Finn Thaindbb6b352016-01-03 16:05:53 +1100255 dsprintk(NDEBUG_TAGS, instance, "using tag %d for target %d lun %d (%d tags allocated)\n",
256 cmd->tag, scmd_id(cmd), lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260
261/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
262 * unlock the LUN.
263 */
264
Finn Thain710ddd02014-11-12 16:12:02 +1100265static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200267 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100268 struct Scsi_Host *instance = cmd->device->host;
269 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Roman Zippelc28bda22007-05-01 22:32:36 +0200271 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200272 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100273 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d untagged cmd freed\n",
274 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200275 } else if (cmd->tag >= MAX_TAGS) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100276 shost_printk(KERN_NOTICE, instance,
277 "trying to free bad tag %d!\n", cmd->tag);
Roman Zippelc28bda22007-05-01 22:32:36 +0200278 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100279 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200280 clear_bit(cmd->tag, ta->allocated);
281 ta->nr_allocated--;
Finn Thaindbb6b352016-01-03 16:05:53 +1100282 dsprintk(NDEBUG_TAGS, instance, "freed tag %d for target %d lun %d\n",
283 cmd->tag, scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287
Finn Thainca513fc2014-11-12 16:12:19 +1100288static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Roman Zippelc28bda22007-05-01 22:32:36 +0200290 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100291 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Finn Thainca513fc2014-11-12 16:12:19 +1100293 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200294 return;
295
296 for (target = 0; target < 8; ++target) {
297 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100298 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200299 bitmap_zero(ta->allocated, MAX_TAGS);
300 ta->nr_allocated = 0;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305#endif /* SUPPORT_TAGS */
306
Finn Thainc16df322016-01-03 16:06:08 +1100307/**
308 * merge_contiguous_buffers - coalesce scatter-gather list entries
309 * @cmd: command requesting IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
Finn Thainc16df322016-01-03 16:06:08 +1100311 * Try to merge several scatter-gather buffers into one DMA transfer.
312 * This is possible if the scatter buffers lie on physically
313 * contiguous addresses. The first scatter-gather buffer's data are
314 * assumed to be already transferred into cmd->SCp.this_residual.
315 * Every buffer merged avoids an interrupt and a DMA setup operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317
Finn Thain710ddd02014-11-12 16:12:02 +1100318static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Finn Thaine3f463b2014-11-12 16:12:16 +1100320#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200321 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200323 unsigned long oldlen = cmd->SCp.this_residual;
324 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#endif
326
Roman Zippelc28bda22007-05-01 22:32:36 +0200327 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
328 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200329 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100330 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200331 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200333 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200335 ++cmd->SCp.buffer;
336 --cmd->SCp.buffers_residual;
337 cmd->SCp.this_residual += cmd->SCp.buffer->length;
338 endaddr += cmd->SCp.buffer->length;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200341 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100342 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200343 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100345#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Finn Thainff50f9e2014-11-12 16:12:18 +1100348/**
349 * initialize_SCp - init the scsi pointer field
350 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100352 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354
Finn Thain710ddd02014-11-12 16:12:02 +1100355static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Roman Zippelc28bda22007-05-01 22:32:36 +0200357 /*
358 * Initialize the Scsi Pointer field so that all of the commands in the
359 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200361
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200362 if (scsi_bufflen(cmd)) {
363 cmd->SCp.buffer = scsi_sglist(cmd);
364 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200365 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200366 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Finn Thainc16df322016-01-03 16:06:08 +1100367
Roman Zippelc28bda22007-05-01 22:32:36 +0200368 merge_contiguous_buffers(cmd);
369 } else {
370 cmd->SCp.buffer = NULL;
371 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200372 cmd->SCp.ptr = NULL;
373 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200374 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100375
376 cmd->SCp.Status = 0;
377 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Finn Thain636b1ec2016-01-03 16:05:10 +1100380/**
Finn Thainb32ade12016-01-03 16:05:41 +1100381 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain636b1ec2016-01-03 16:05:10 +1100382 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100383 * @reg1: 5380 register to poll
384 * @bit1: Bitmask to check
385 * @val1: Expected value
386 * @reg2: Second 5380 register to poll
387 * @bit2: Second bitmask to check
388 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100389 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100390 *
Finn Thain2f854b82016-01-03 16:05:22 +1100391 * Polls the chip in a reasonably efficient manner waiting for an
392 * event to occur. After a short quick poll we begin to yield the CPU
393 * (if possible). In irq contexts the time-out is arbitrarily limited.
394 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100395 *
Finn Thainb32ade12016-01-03 16:05:41 +1100396 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100397 */
398
Finn Thainb32ade12016-01-03 16:05:41 +1100399static int NCR5380_poll_politely2(struct Scsi_Host *instance,
400 int reg1, int bit1, int val1,
401 int reg2, int bit2, int val2, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100402{
Finn Thain2f854b82016-01-03 16:05:22 +1100403 struct NCR5380_hostdata *hostdata = shost_priv(instance);
404 unsigned long deadline = jiffies + wait;
405 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100406
Finn Thain2f854b82016-01-03 16:05:22 +1100407 /* Busy-wait for up to 10 ms */
408 n = min(10000U, jiffies_to_usecs(wait));
409 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100410 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100411 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100412 if ((NCR5380_read(reg1) & bit1) == val1)
413 return 0;
414 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain636b1ec2016-01-03 16:05:10 +1100415 return 0;
416 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100417 } while (n--);
418
419 if (irqs_disabled() || in_interrupt())
420 return -ETIMEDOUT;
421
422 /* Repeatedly sleep for 1 ms until deadline */
423 while (time_is_after_jiffies(deadline)) {
424 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100425 if ((NCR5380_read(reg1) & bit1) == val1)
426 return 0;
427 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain2f854b82016-01-03 16:05:22 +1100428 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100429 }
430
Finn Thain636b1ec2016-01-03 16:05:10 +1100431 return -ETIMEDOUT;
432}
433
Finn Thainb32ade12016-01-03 16:05:41 +1100434static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
435 int reg, int bit, int val, int wait)
436{
437 return NCR5380_poll_politely2(instance, reg, bit, val,
438 reg, bit, val, wait);
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#if NDEBUG
442static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200443 unsigned char mask;
444 const char *name;
445} signals[] = {
446 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
447 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
448 { SR_SEL, "SEL" }, {0, NULL}
449}, basrs[] = {
450 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
451}, icrs[] = {
452 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
453 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
454 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
455 {0, NULL}
456}, mrs[] = {
457 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
458 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
459 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
460 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
461 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
462 {0, NULL}
463};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Finn Thainff50f9e2014-11-12 16:12:18 +1100465/**
466 * NCR5380_print - print scsi bus signals
467 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100469 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471
Roman Zippelc28bda22007-05-01 22:32:36 +0200472static void NCR5380_print(struct Scsi_Host *instance)
473{
474 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Roman Zippelc28bda22007-05-01 22:32:36 +0200476 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
477 status = NCR5380_read(STATUS_REG);
478 mr = NCR5380_read(MODE_REG);
479 icr = NCR5380_read(INITIATOR_COMMAND_REG);
480 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thain11d2f632016-01-03 16:05:51 +1100481
Roman Zippelc28bda22007-05-01 22:32:36 +0200482 printk("STATUS_REG: %02x ", status);
483 for (i = 0; signals[i].mask; ++i)
484 if (status & signals[i].mask)
485 printk(",%s", signals[i].name);
486 printk("\nBASR: %02x ", basr);
487 for (i = 0; basrs[i].mask; ++i)
488 if (basr & basrs[i].mask)
489 printk(",%s", basrs[i].name);
490 printk("\nICR: %02x ", icr);
491 for (i = 0; icrs[i].mask; ++i)
492 if (icr & icrs[i].mask)
493 printk(",%s", icrs[i].name);
494 printk("\nMODE: %02x ", mr);
495 for (i = 0; mrs[i].mask; ++i)
496 if (mr & mrs[i].mask)
497 printk(",%s", mrs[i].name);
498 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200502 unsigned char value;
503 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200505 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
506 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
507 {PHASE_UNKNOWN, "UNKNOWN"}
508};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Finn Thainff50f9e2014-11-12 16:12:18 +1100510/**
511 * NCR5380_print_phase - show SCSI phase
512 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100514 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
516
517static void NCR5380_print_phase(struct Scsi_Host *instance)
518{
Roman Zippelc28bda22007-05-01 22:32:36 +0200519 unsigned char status;
520 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Roman Zippelc28bda22007-05-01 22:32:36 +0200522 status = NCR5380_read(STATUS_REG);
523 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100524 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200525 else {
526 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
527 (phases[i].value != (status & PHASE_MASK)); ++i)
528 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100529 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Roman Zippelc28bda22007-05-01 22:32:36 +0200530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#endif
534
Finn Thain8c325132014-11-12 16:11:58 +1100535/**
536 * NCR58380_info - report driver and host information
537 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
Finn Thain8c325132014-11-12 16:11:58 +1100539 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
541
Finn Thain8c325132014-11-12 16:11:58 +1100542static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Finn Thain8c325132014-11-12 16:11:58 +1100544 struct NCR5380_hostdata *hostdata = shost_priv(instance);
545
546 return hostdata->info;
547}
548
549static void prepare_info(struct Scsi_Host *instance)
550{
551 struct NCR5380_hostdata *hostdata = shost_priv(instance);
552
553 snprintf(hostdata->info, sizeof(hostdata->info),
554 "%s, io_port 0x%lx, n_io_port %d, "
555 "base 0x%lx, irq %d, "
556 "can_queue %d, cmd_per_lun %d, "
557 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100558 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100559 "options { %s} ",
560 instance->hostt->name, instance->io_port, instance->n_io_port,
561 instance->base, instance->irq,
562 instance->can_queue, instance->cmd_per_lun,
563 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100564 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100565 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100566#ifdef DIFFERENTIAL
567 "DIFFERENTIAL "
568#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100570 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#endif
572#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100573 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif
575#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100576 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
Finn Thain8c325132014-11-12 16:11:58 +1100578 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Finn Thainff50f9e2014-11-12 16:12:18 +1100581/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100582 * NCR5380_init - initialise an NCR5380
583 * @instance: adapter to configure
584 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100586 * Initializes *instance and corresponding 5380 chip,
587 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
589 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100590 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200591 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100592 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100595static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Finn Thaine8a60142016-01-03 16:05:54 +1100597 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +0200598 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100599 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Finn Thaina53a21e2014-11-12 16:12:21 +1100601 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200602 hostdata->id_mask = 1 << instance->this_id;
603 hostdata->id_higher_mask = 0;
604 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
605 if (i > hostdata->id_mask)
606 hostdata->id_higher_mask |= i;
607 for (i = 0; i < 8; ++i)
608 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100610 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif
612#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200613 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100615 spin_lock_init(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200616 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100617 hostdata->sensing = NULL;
618 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100619 INIT_LIST_HEAD(&hostdata->unissued);
620 INIT_LIST_HEAD(&hostdata->disconnected);
621
Finn Thainef1081c2014-11-12 16:12:14 +1100622 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Finn Thaina53a21e2014-11-12 16:12:21 +1100624 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100625 hostdata->work_q = alloc_workqueue("ncr5380_%d",
626 WQ_UNBOUND | WQ_MEM_RECLAIM,
627 1, instance->host_no);
628 if (!hostdata->work_q)
629 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Finn Thain8c325132014-11-12 16:11:58 +1100631 prepare_info(instance);
632
Roman Zippelc28bda22007-05-01 22:32:36 +0200633 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
634 NCR5380_write(MODE_REG, MR_BASE);
635 NCR5380_write(TARGET_COMMAND_REG, 0);
636 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Finn Thain2f854b82016-01-03 16:05:22 +1100638 /* Calibrate register polling loop */
639 i = 0;
640 deadline = jiffies + 1;
641 do {
642 cpu_relax();
643 } while (time_is_after_jiffies(deadline));
644 deadline += msecs_to_jiffies(256);
645 do {
646 NCR5380_read(STATUS_REG);
647 ++i;
648 cpu_relax();
649 } while (time_is_after_jiffies(deadline));
650 hostdata->accesses_per_ms = i / 256;
651
Roman Zippelc28bda22007-05-01 22:32:36 +0200652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Finn Thainff50f9e2014-11-12 16:12:18 +1100655/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100656 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
657 * @instance: adapter to check
658 *
659 * If the system crashed, it may have crashed with a connected target and
660 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
661 * currently established nexus, which we know nothing about. Failing that
662 * do a bus reset.
663 *
664 * Note that a bus reset will cause the chip to assert IRQ.
665 *
666 * Returns 0 if successful, otherwise -ENXIO.
667 */
668
669static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
670{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100671 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100672 int pass;
673
674 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
675 switch (pass) {
676 case 1:
677 case 3:
678 case 5:
679 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
680 NCR5380_poll_politely(instance,
681 STATUS_REG, SR_BSY, 0, 5 * HZ);
682 break;
683 case 2:
684 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
685 do_abort(instance);
686 break;
687 case 4:
688 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
689 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100690 /* Wait after a reset; the SCSI standard calls for
691 * 250ms, we wait 500ms to be on the safe side.
692 * But some Toshiba CD-ROMs need ten times that.
693 */
694 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
695 msleep(2500);
696 else
697 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100698 break;
699 case 6:
700 shost_printk(KERN_ERR, instance, "bus locked solid\n");
701 return -ENXIO;
702 }
703 }
704 return 0;
705}
706
707/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100708 * NCR5380_exit - remove an NCR5380
709 * @instance: adapter to remove
710 *
711 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
712 */
713
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200714static void NCR5380_exit(struct Scsi_Host *instance)
715{
Finn Thaina53a21e2014-11-12 16:12:21 +1100716 struct NCR5380_hostdata *hostdata = shost_priv(instance);
717
718 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100719 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200720}
721
Finn Thainff50f9e2014-11-12 16:12:18 +1100722/**
Finn Thain677e0192016-01-03 16:05:59 +1100723 * complete_cmd - finish processing a command and return it to the SCSI ML
724 * @instance: the host instance
725 * @cmd: command to complete
726 */
727
728static void complete_cmd(struct Scsi_Host *instance,
729 struct scsi_cmnd *cmd)
730{
731 struct NCR5380_hostdata *hostdata = shost_priv(instance);
732
733 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
734
Finn Thainf27db8e2016-01-03 16:06:00 +1100735 if (hostdata->sensing == cmd) {
736 /* Autosense processing ends here */
737 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
738 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
739 set_host_byte(cmd, DID_ERROR);
740 } else
741 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
742 hostdata->sensing = NULL;
743 }
744
Finn Thain677e0192016-01-03 16:05:59 +1100745#ifdef SUPPORT_TAGS
746 cmd_free_tag(cmd);
747#else
748 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
749#endif
750 cmd->scsi_done(cmd);
751}
752
753/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100754 * NCR5380_queue_command - queue a command
755 * @instance: the relevant SCSI adapter
756 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
Finn Thain1bb40582016-01-03 16:05:29 +1100758 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100759 * twiddling done to the host specific fields of cmd. If the
760 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
762
Finn Thain16b29e72014-11-12 16:12:08 +1100763static int NCR5380_queue_command(struct Scsi_Host *instance,
764 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Finn Thain16b29e72014-11-12 16:12:08 +1100766 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100767 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200768 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200771 switch (cmd->cmnd[0]) {
772 case WRITE_6:
773 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100774 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200775 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100776 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200777 return 0;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
780
Roman Zippelc28bda22007-05-01 22:32:36 +0200781 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Roman Zippelc28bda22007-05-01 22:32:36 +0200783 /*
Roman Zippelc28bda22007-05-01 22:32:36 +0200784 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
785 * because also a timer int can trigger an abort or reset, which would
786 * alter queues and touch the lock.
787 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100788 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100789 return SCSI_MLQUEUE_HOST_BUSY;
790
Finn Thain11d2f632016-01-03 16:05:51 +1100791 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain16b29e72014-11-12 16:12:08 +1100792
Finn Thainff50f9e2014-11-12 16:12:18 +1100793 /*
794 * Insert the cmd into the issue queue. Note that REQUEST SENSE
795 * commands are added to the head of the queue since any command will
796 * clear the contingent allegiance condition that exists and the
797 * sense data is only guaranteed to be valid while the condition exists.
798 */
799
Finn Thain32b26a12016-01-03 16:05:58 +1100800 if (cmd->cmnd[0] == REQUEST_SENSE)
801 list_add(&ncmd->list, &hostdata->unissued);
802 else
803 list_add_tail(&ncmd->list, &hostdata->unissued);
804
Finn Thain11d2f632016-01-03 16:05:51 +1100805 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Finn Thaindbb6b352016-01-03 16:05:53 +1100807 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
808 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Finn Thain010e89d2016-01-03 16:05:38 +1100810 /* Kick off command processing */
811 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Finn Thaine3c3da62014-11-12 16:12:15 +1100815static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
816{
817 struct NCR5380_hostdata *hostdata = shost_priv(instance);
818
819 /* Caller does the locking needed to set & test these data atomically */
Finn Thain32b26a12016-01-03 16:05:58 +1100820 if (list_empty(&hostdata->disconnected) &&
821 list_empty(&hostdata->unissued) &&
Finn Thainf27db8e2016-01-03 16:06:00 +1100822 list_empty(&hostdata->autosense) &&
Finn Thaine3c3da62014-11-12 16:12:15 +1100823 !hostdata->connected &&
Finn Thain707d62b2016-01-03 16:06:02 +1100824 !hostdata->selecting)
Finn Thaine3c3da62014-11-12 16:12:15 +1100825 NCR5380_release_dma_irq(instance);
826}
827
Finn Thainff50f9e2014-11-12 16:12:18 +1100828/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100829 * dequeue_next_cmd - dequeue a command for processing
830 * @instance: the scsi host instance
831 *
832 * Priority is given to commands on the autosense queue. These commands
833 * need autosense because of a CHECK CONDITION result.
834 *
835 * Returns a command pointer if a command is found for a target that is
836 * not already busy. Otherwise returns NULL.
837 */
838
839static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
840{
841 struct NCR5380_hostdata *hostdata = shost_priv(instance);
842 struct NCR5380_cmd *ncmd;
843 struct scsi_cmnd *cmd;
844
845 if (list_empty(&hostdata->autosense)) {
846 list_for_each_entry(ncmd, &hostdata->unissued, list) {
847 cmd = NCR5380_to_scmd(ncmd);
848 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
849 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
850
851 if (
852#ifdef SUPPORT_TAGS
853 !is_lun_busy(cmd, 1)
854#else
855 !(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))
856#endif
857 ) {
858 list_del(&ncmd->list);
859 dsprintk(NDEBUG_QUEUES, instance,
860 "dequeue: removed %p from issue queue\n", cmd);
861 return cmd;
862 }
863 }
864 } else {
865 /* Autosense processing begins here */
866 ncmd = list_first_entry(&hostdata->autosense,
867 struct NCR5380_cmd, list);
868 list_del(&ncmd->list);
869 cmd = NCR5380_to_scmd(ncmd);
870 dsprintk(NDEBUG_QUEUES, instance,
871 "dequeue: removed %p from autosense queue\n", cmd);
872 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
873 hostdata->sensing = cmd;
874 return cmd;
875 }
876 return NULL;
877}
878
879static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
880{
881 struct NCR5380_hostdata *hostdata = shost_priv(instance);
882 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
883
884 if (hostdata->sensing) {
885 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
886 list_add(&ncmd->list, &hostdata->autosense);
887 hostdata->sensing = NULL;
888 } else
889 list_add(&ncmd->list, &hostdata->unissued);
890}
891
892/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100893 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100895 * NCR5380_main is a coroutine that runs as long as more work can
896 * be done on the NCR5380 host adapters in a system. Both
897 * NCR5380_queue_command() and NCR5380_intr() will try to start it
898 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200899 */
900
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200901static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Finn Thaina53a21e2014-11-12 16:12:21 +1100903 struct NCR5380_hostdata *hostdata =
904 container_of(work, struct NCR5380_hostdata, main_task);
905 struct Scsi_Host *instance = hostdata->host;
Finn Thainf27db8e2016-01-03 16:06:00 +1100906 struct scsi_cmnd *cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +0200907 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Roman Zippelc28bda22007-05-01 22:32:36 +0200909 /*
Roman Zippelc28bda22007-05-01 22:32:36 +0200910 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
911 * because also a timer int can trigger an abort or reset, which can
912 * alter queues and touch the Falcon lock.
913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Roman Zippelc28bda22007-05-01 22:32:36 +0200915 do {
Roman Zippelc28bda22007-05-01 22:32:36 +0200916 done = 1;
917
Finn Thain0a4e3612016-01-03 16:06:07 +1100918 spin_lock_irq(&hostdata->lock);
Finn Thainf27db8e2016-01-03 16:06:00 +1100919 while (!hostdata->connected &&
920 (cmd = dequeue_next_cmd(instance))) {
921
922 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
923
Roman Zippelc28bda22007-05-01 22:32:36 +0200924 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100925 * Attempt to establish an I_T_L nexus here.
926 * On success, instance->hostdata->connected is set.
927 * On failure, we must add the command back to the
928 * issue queue so we can keep trying.
Roman Zippelc28bda22007-05-01 22:32:36 +0200929 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100930 /*
931 * REQUEST SENSE commands are issued without tagged
932 * queueing, even on SCSI-II devices because the
933 * contingent allegiance condition exists for the
934 * entire unit.
935 */
936 /* ++roman: ...and the standard also requires that
937 * REQUEST SENSE command are untagged.
938 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200939
940#ifdef SUPPORT_TAGS
Finn Thainf27db8e2016-01-03 16:06:00 +1100941 cmd_get_tag(cmd, cmd->cmnd[0] != REQUEST_SENSE);
Roman Zippelc28bda22007-05-01 22:32:36 +0200942#endif
Finn Thain707d62b2016-01-03 16:06:02 +1100943 cmd = NCR5380_select(instance, cmd);
944 if (!cmd) {
945 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +1100946 maybe_release_dma_irq(instance);
947 } else {
Finn Thainf27db8e2016-01-03 16:06:00 +1100948 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
949 "main: select failed, returning %p to queue\n", cmd);
950 requeue_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200951#ifdef SUPPORT_TAGS
Finn Thainf27db8e2016-01-03 16:06:00 +1100952 cmd_free_tag(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200953#endif
Finn Thainf27db8e2016-01-03 16:06:00 +1100954 }
955 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200956 if (hostdata->connected
957#ifdef REAL_DMA
958 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#endif
960 ) {
Finn Thainb7465452016-01-03 16:06:05 +1100961 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200962 NCR5380_information_transfer(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +0200963 done = 0;
964 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100965 spin_unlock_irq(&hostdata->lock);
966 if (!done)
967 cond_resched();
Roman Zippelc28bda22007-05-01 22:32:36 +0200968 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971
972#ifdef REAL_DMA
973/*
974 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
975 *
976 * Purpose : Called by interrupt handler when DMA finishes or a phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100977 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
979 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981
Roman Zippelc28bda22007-05-01 22:32:36 +0200982static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Finn Thaine8a60142016-01-03 16:05:54 +1100984 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain01bd9082014-11-12 16:12:23 +1100985 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +1100986 unsigned char **data;
Finn Thain5299b3c2016-01-03 16:05:57 +1100987 int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +1100988 int saved_data = 0, overrun = 0;
989 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Finn Thainef1081c2014-11-12 16:12:14 +1100991 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200992 p = hostdata->connected->SCp.phase;
993 if (p & SR_IO) {
994 udelay(10);
995 if ((NCR5380_read(BUS_AND_STATUS_REG) &
996 (BASR_PHASE_MATCH|BASR_ACK)) ==
997 (BASR_PHASE_MATCH|BASR_ACK)) {
998 saved_data = NCR5380_read(INPUT_DATA_REG);
999 overrun = 1;
Finn Thainb7465452016-01-03 16:06:05 +11001000 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001001 }
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001004
Finn Thaine3f463b2014-11-12 16:12:16 +11001005#if defined(CONFIG_SUN3)
1006 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1007 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1008 instance->host_no);
1009 BUG();
1010 }
1011
1012 /* make sure we're not stuck in a data phase */
1013 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1014 (BASR_PHASE_MATCH | BASR_ACK)) {
1015 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1016 NCR5380_read(BUS_AND_STATUS_REG));
1017 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1018 instance->host_no);
1019 BUG();
1020 }
1021#endif
1022
Roman Zippelc28bda22007-05-01 22:32:36 +02001023 NCR5380_write(MODE_REG, MR_BASE);
1024 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001025 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001026
Finn Thain01bd9082014-11-12 16:12:23 +11001027 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001028 hostdata->dma_len = 0;
1029
1030 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1031 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001032 *data += transferred;
1033 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001034
Finn Thainef1081c2014-11-12 16:12:14 +11001035 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001036 int cnt, toPIO;
1037
Roman Zippelc28bda22007-05-01 22:32:36 +02001038 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001039 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001040 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001041 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001042 *(*data)++ = saved_data;
1043 (*count)--;
1044 cnt--;
1045 toPIO--;
1046 }
Finn Thaind65e6342014-03-18 11:42:20 +11001047 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001048 NCR5380_transfer_pio(instance, &p, &cnt, data);
1049 *count -= toPIO - cnt;
1050 }
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053#endif /* REAL_DMA */
1054
1055
Finn Thainff50f9e2014-11-12 16:12:18 +11001056/**
1057 * NCR5380_intr - generic NCR5380 irq handler
1058 * @irq: interrupt number
1059 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001060 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001061 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1062 * from the disconnected queue, and restarting NCR5380_main()
1063 * as required.
Finn Thaincd400822016-01-03 16:05:40 +11001064 *
1065 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1066 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1067 * Three of these six conditions are latched in the Bus and Status Register:
1068 * - End of DMA (cleared by ending DMA Mode)
1069 * - Parity error (cleared by reading RPIR)
1070 * - Loss of BSY (cleared by reading RPIR)
1071 * Two conditions have flag bits that are not latched:
1072 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1073 * - Bus reset (non-maskable)
1074 * The remaining condition has no flag bit at all:
1075 * - Selection/reselection
1076 *
1077 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1078 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1079 * claimed that "the design of the [DP8490] interrupt logic ensures
1080 * interrupts will not be lost (they can be on the DP5380)."
1081 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1082 *
1083 * Checking for bus reset by reading RST is futile because of interrupt
1084 * latency, but a bus reset will reset chip logic. Checking for parity error
1085 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1086 * condition will clear DMA Mode. We can tell when this occurs because the
1087 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
1089
Roman Zippelc28bda22007-05-01 22:32:36 +02001090static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Finn Thaina53a21e2014-11-12 16:12:21 +11001092 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001093 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001094 int handled = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001095 unsigned char basr;
Finn Thain11d2f632016-01-03 16:05:51 +11001096 unsigned long flags;
1097
1098 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Roman Zippelc28bda22007-05-01 22:32:36 +02001100 basr = NCR5380_read(BUS_AND_STATUS_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001101 if (basr & BASR_IRQ) {
Finn Thaincd400822016-01-03 16:05:40 +11001102 unsigned char mr = NCR5380_read(MODE_REG);
1103 unsigned char sr = NCR5380_read(STATUS_REG);
1104
Finn Thainb7465452016-01-03 16:06:05 +11001105 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1106 irq, basr, sr, mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001109 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1110 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1111 * We ack IRQ after clearing Mode Register. Workarounds
1112 * for End of DMA errata need to happen in DMA Mode.
Roman Zippelc28bda22007-05-01 22:32:36 +02001113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Finn Thainb7465452016-01-03 16:06:05 +11001115 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001116
Finn Thaincd400822016-01-03 16:05:40 +11001117 if (hostdata->connected) {
1118 NCR5380_dma_complete(instance);
1119 queue_work(hostdata->work_q, &hostdata->main_task);
1120 } else {
1121 NCR5380_write(MODE_REG, MR_BASE);
1122 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001123 }
Finn Thaincd400822016-01-03 16:05:40 +11001124 } else
1125#endif /* REAL_DMA */
1126 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1127 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1128 /* Probably reselected */
1129 NCR5380_write(SELECT_ENABLE_REG, 0);
1130 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1131
Finn Thainb7465452016-01-03 16:06:05 +11001132 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +11001133
1134 if (!hostdata->connected) {
1135 NCR5380_reselect(instance);
1136 queue_work(hostdata->work_q, &hostdata->main_task);
1137 }
1138 if (!hostdata->connected)
1139 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1140 } else {
1141 /* Probably Bus Reset */
1142 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1143
Finn Thainb7465452016-01-03 16:06:05 +11001144 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +11001145#ifdef SUN3_SCSI_VME
1146 dregs->csr |= CSR_DMA_ENABLE;
1147#endif
1148 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001149 handled = 1;
Finn Thaincd400822016-01-03 16:05:40 +11001150 } else {
1151 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine3f463b2014-11-12 16:12:16 +11001152#ifdef SUN3_SCSI_VME
1153 dregs->csr |= CSR_DMA_ENABLE;
1154#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001155 }
1156
Finn Thain11d2f632016-01-03 16:05:51 +11001157 spin_unlock_irqrestore(&hostdata->lock, flags);
1158
Roman Zippelc28bda22007-05-01 22:32:36 +02001159 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Roman Zippelc28bda22007-05-01 22:32:36 +02001162/*
Finn Thain710ddd02014-11-12 16:12:02 +11001163 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001164 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *
1166 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +11001167 * including ARBITRATION, SELECTION, and initial message out for
1168 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001170 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +11001171 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 *
Finn Thain707d62b2016-01-03 16:06:02 +11001173 * Returns cmd if selection failed but should be retried,
1174 * NULL if selection failed and should not be retried, or
1175 * NULL if selection succeeded (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001176 *
1177 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +11001178 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1179 * with registers as they should have been on entry - ie
1180 * SELECT_ENABLE will be set appropriately, the NCR5380
1181 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001183 * If successful : I_T_L or I_T_L_Q nexus will be established,
1184 * instance->connected will be set to cmd.
1185 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001187 * If failed (no target) : cmd->scsi_done() will be called, and the
1188 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190
Finn Thain707d62b2016-01-03 16:06:02 +11001191static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1192 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Finn Thaine8a60142016-01-03 16:05:54 +11001194 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001195 unsigned char tmp[3], phase;
1196 unsigned char *data;
1197 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Finn Thain8ad3a592014-03-18 11:42:19 +11001200 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +11001201 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1202 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Roman Zippelc28bda22007-05-01 22:32:36 +02001204 /*
Finn Thain707d62b2016-01-03 16:06:02 +11001205 * Arbitration and selection phases are slow and involve dropping the
1206 * lock, so we have to watch out for EH. An exception handler may
1207 * change 'selecting' to NULL. This function will then return NULL
1208 * so that the caller will forget about 'cmd'. (During information
1209 * transfer phases, EH may change 'connected' to NULL.)
1210 */
1211 hostdata->selecting = cmd;
1212
1213 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001214 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1215 * data bus during SELECTION.
1216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Roman Zippelc28bda22007-05-01 22:32:36 +02001218 NCR5380_write(TARGET_COMMAND_REG, 0);
1219
1220 /*
1221 * Start arbitration.
1222 */
1223
1224 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1225 NCR5380_write(MODE_REG, MR_ARBITRATE);
1226
Finn Thain55500d92016-01-03 16:05:35 +11001227 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1228 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Finn Thain11d2f632016-01-03 16:05:51 +11001231 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001232 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1233 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1234 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001235 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001236 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1237 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001238 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001239 }
1240 if (err < 0) {
1241 NCR5380_write(MODE_REG, MR_BASE);
1242 shost_printk(KERN_ERR, instance,
1243 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001244 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001245 }
Finn Thain11d2f632016-01-03 16:05:51 +11001246 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001247
1248 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001249 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Roman Zippelc28bda22007-05-01 22:32:36 +02001251 /* Check for lost arbitration */
1252 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1253 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
Finn Thain11d2f632016-01-03 16:05:51 +11001254 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001255 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001256 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001257 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001258 goto out;
Roman Zippelc28bda22007-05-01 22:32:36 +02001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Finn Thaincf13b082016-01-03 16:05:18 +11001261 /* After/during arbitration, BSY should be asserted.
1262 * IBM DPES-31080 Version S31Q works now
1263 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1264 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001265 NCR5380_write(INITIATOR_COMMAND_REG,
1266 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Roman Zippelc28bda22007-05-01 22:32:36 +02001268 /*
1269 * Again, bus clear + bus settle time is 1.2us, however, this is
1270 * a minimum so we'll udelay ceil(1.2)
1271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Finn Thain9c3f0e22016-01-03 16:05:11 +11001273 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1274 udelay(15);
1275 else
1276 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001277
Finn Thain11d2f632016-01-03 16:05:51 +11001278 spin_lock_irq(&hostdata->lock);
1279
Finn Thain72064a72016-01-03 16:05:44 +11001280 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1281 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001282 goto out;
1283
1284 if (!hostdata->selecting) {
1285 NCR5380_write(MODE_REG, MR_BASE);
1286 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1287 goto out;
1288 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001289
Finn Thainb7465452016-01-03 16:06:05 +11001290 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001291
1292 /*
1293 * Now that we have won arbitration, start Selection process, asserting
1294 * the host and target ID's on the SCSI bus.
1295 */
1296
1297 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1298
1299 /*
1300 * Raise ATN while SEL is true before BSY goes false from arbitration,
1301 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1302 * phase immediately after selection.
1303 */
1304
1305 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1306 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Roman Zippelc28bda22007-05-01 22:32:36 +02001309 /*
1310 * Reselect interrupts must be turned off prior to the dropping of BSY,
1311 * otherwise we will trigger an interrupt.
1312 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001313 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Finn Thain11d2f632016-01-03 16:05:51 +11001315 spin_unlock_irq(&hostdata->lock);
1316
Roman Zippelc28bda22007-05-01 22:32:36 +02001317 /*
1318 * The initiator shall then wait at least two deskew delays and release
1319 * the BSY signal.
1320 */
1321 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Roman Zippelc28bda22007-05-01 22:32:36 +02001323 /* Reset BSY */
1324 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1325 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Roman Zippelc28bda22007-05-01 22:32:36 +02001327 /*
1328 * Something weird happens when we cease to drive BSY - looks
1329 * like the board/chip is letting us do another read before the
1330 * appropriate propagation delay has expired, and we're confusing
1331 * a BSY signal from ourselves as the target's response to SELECTION.
1332 *
1333 * A small delay (the 'C++' frontend breaks the pipeline with an
1334 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1335 * tighter 'C' code breaks and requires this) solves the problem -
1336 * the 1 us delay is arbitrary, and only used because this delay will
1337 * be the same on other platforms and since it works here, it should
1338 * work there.
1339 *
1340 * wingel suggests that this could be due to failing to wait
1341 * one deskew delay.
1342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Roman Zippelc28bda22007-05-01 22:32:36 +02001344 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Finn Thainb7465452016-01-03 16:06:05 +11001346 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Roman Zippelc28bda22007-05-01 22:32:36 +02001348 /*
1349 * The SCSI specification calls for a 250 ms timeout for the actual
1350 * selection.
1351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Finn Thainae753a32016-01-03 16:05:23 +11001353 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1354 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Roman Zippelc28bda22007-05-01 22:32:36 +02001356 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001357 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001358 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1359 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001360 if (!hostdata->connected)
1361 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001362 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001365
Finn Thainae753a32016-01-03 16:05:23 +11001366 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001367 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001368 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001369 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001370 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1371 if (hostdata->selecting) {
1372 cmd->result = DID_BAD_TARGET << 16;
1373 complete_cmd(instance, cmd);
1374 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1375 cmd = NULL;
1376 }
1377 goto out;
Roman Zippelc28bda22007-05-01 22:32:36 +02001378 }
1379
Roman Zippelc28bda22007-05-01 22:32:36 +02001380 /*
Finn Thainae753a32016-01-03 16:05:23 +11001381 * No less than two deskew delays after the initiator detects the
1382 * BSY signal is true, it shall release the SEL signal and may
1383 * change the DATA BUS. -wingel
1384 */
1385
1386 udelay(1);
1387
1388 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1389
1390 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001391 * Since we followed the SCSI spec, and raised ATN while SEL
1392 * was true but before BSY was false during selection, the information
1393 * transfer phase should be a MESSAGE OUT phase so that we can send the
1394 * IDENTIFY message.
1395 *
1396 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1397 * message (2 bytes) with a tag ID that we increment with every command
1398 * until it wraps back to 0.
1399 *
1400 * XXX - it turns out that there are some broken SCSI-II devices,
Finn Thain594d4ba2016-01-03 16:06:10 +11001401 * which claim to support tagged queuing but fail when more than
1402 * some number of commands are issued at once.
Roman Zippelc28bda22007-05-01 22:32:36 +02001403 */
1404
1405 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001406
1407 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001408 spin_lock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001409 if (err < 0) {
1410 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1412 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001413 goto out;
1414 }
1415 if (!hostdata->selecting) {
1416 do_abort(instance);
1417 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001418 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001419
Finn Thainb7465452016-01-03 16:06:05 +11001420 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1421 scmd_id(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02001422 tmp[0] = IDENTIFY(1, cmd->device->lun);
1423
1424#ifdef SUPPORT_TAGS
1425 if (cmd->tag != TAG_NONE) {
1426 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1427 tmp[2] = cmd->tag;
1428 len = 3;
1429 } else
1430 len = 1;
1431#else
1432 len = 1;
1433 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434#endif /* SUPPORT_TAGS */
1435
Roman Zippelc28bda22007-05-01 22:32:36 +02001436 /* Send message(s) */
1437 data = tmp;
1438 phase = PHASE_MSGOUT;
1439 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001440 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001441 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001442
Roman Zippelc28bda22007-05-01 22:32:36 +02001443 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001445 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1446#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001447#ifdef SUN3_SCSI_VME
1448 dregs->csr |= CSR_INTR;
1449#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Roman Zippelc28bda22007-05-01 22:32:36 +02001451 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Finn Thain707d62b2016-01-03 16:06:02 +11001453 cmd = NULL;
1454
1455out:
1456 if (!hostdata->selecting)
1457 return NULL;
1458 hostdata->selecting = NULL;
1459 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Roman Zippelc28bda22007-05-01 22:32:36 +02001462/*
1463 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001464 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
1466 * Purpose : transfers data in given phase using polled I/O
1467 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001468 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001469 * what phase is expected, *count - pointer to number of
1470 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001473 * maximum number of bytes, 0 if all bytes are transferred or exit
1474 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001476 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
1478 * XXX Note : handling for bus free may be useful.
1479 */
1480
1481/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001482 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * IS 100% reliable, and for the actual data transfer where speed
1484 * counts, we will always do a pseudo DMA or DMA transfer.
1485 */
1486
Roman Zippelc28bda22007-05-01 22:32:36 +02001487static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1488 unsigned char *phase, int *count,
1489 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Roman Zippelc28bda22007-05-01 22:32:36 +02001491 register unsigned char p = *phase, tmp;
1492 register int c = *count;
1493 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Roman Zippelc28bda22007-05-01 22:32:36 +02001495 /*
1496 * The NCR5380 chip will only drive the SCSI bus when the
1497 * phase specified in the appropriate bits of the TARGET COMMAND
1498 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
1500
Roman Zippelc28bda22007-05-01 22:32:36 +02001501 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Roman Zippelc28bda22007-05-01 22:32:36 +02001503 do {
1504 /*
1505 * Wait for assertion of REQ, after which the phase bits will be
1506 * valid
1507 */
Finn Thain686f3992016-01-03 16:05:26 +11001508
1509 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Finn Thainb7465452016-01-03 16:06:05 +11001512 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001515 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001516 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001517 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001518 break;
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Roman Zippelc28bda22007-05-01 22:32:36 +02001521 /* Do actual transfer from SCSI bus to / from memory */
1522 if (!(p & SR_IO))
1523 NCR5380_write(OUTPUT_DATA_REG, *d);
1524 else
1525 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Roman Zippelc28bda22007-05-01 22:32:36 +02001527 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Roman Zippelc28bda22007-05-01 22:32:36 +02001529 /*
1530 * The SCSI standard suggests that in MSGOUT phase, the initiator
1531 * should drop ATN on the last byte of the message phase
1532 * after REQ has been asserted for the handshake but before
1533 * the initiator raises ACK.
1534 */
1535
1536 if (!(p & SR_IO)) {
1537 if (!((p & SR_MSG) && c > 1)) {
1538 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001539 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001540 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1541 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1542 } else {
1543 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1544 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001545 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001546 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1547 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1548 }
1549 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001550 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001551 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1552 }
1553
Finn Thaina2edc4a2016-01-03 16:05:27 +11001554 if (NCR5380_poll_politely(instance,
1555 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1556 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001557
Finn Thainb7465452016-01-03 16:06:05 +11001558 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001559
1560 /*
1561 * We have several special cases to consider during REQ/ACK handshaking :
1562 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001563 * message. ATN must be dropped as ACK is dropped.
Roman Zippelc28bda22007-05-01 22:32:36 +02001564 *
1565 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001566 * message. We must exit with ACK asserted, so that the calling
1567 * code may raise ATN before dropping ACK to reject the message.
Roman Zippelc28bda22007-05-01 22:32:36 +02001568 *
1569 * 3. ACK and ATN are clear and the target may proceed as normal.
1570 */
1571 if (!(p == PHASE_MSGIN && c == 1)) {
1572 if (p == PHASE_MSGOUT && c > 1)
1573 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1574 else
1575 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1576 }
1577 } while (--c);
1578
Finn Thainb7465452016-01-03 16:06:05 +11001579 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001580
1581 *count = c;
1582 *data = d;
1583 tmp = NCR5380_read(STATUS_REG);
1584 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001585 * asserted or if ACK hasn't been released yet. The latter applies if
1586 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001587 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001588 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001589 *phase = tmp & PHASE_MASK;
1590 else
1591 *phase = PHASE_UNKNOWN;
1592
1593 if (!c || (*phase == p))
1594 return 0;
1595 else
1596 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Finn Thain636b1ec2016-01-03 16:05:10 +11001599/**
1600 * do_reset - issue a reset command
1601 * @instance: adapter to reset
1602 *
1603 * Issue a reset sequence to the NCR5380 and try and get the bus
1604 * back into sane shape.
1605 *
1606 * This clears the reset interrupt flag because there may be no handler for
1607 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1608 * been installed. And when in EH we may have released the ST DMA interrupt.
1609 */
1610
1611static void do_reset(struct Scsi_Host *instance)
1612{
1613 unsigned long flags;
1614
1615 local_irq_save(flags);
1616 NCR5380_write(TARGET_COMMAND_REG,
1617 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1618 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1619 udelay(50);
1620 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1621 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1622 local_irq_restore(flags);
1623}
1624
Finn Thain80d3eb62016-01-03 16:05:34 +11001625/**
1626 * do_abort - abort the currently established nexus by going to
1627 * MESSAGE OUT phase and sending an ABORT message.
1628 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001629 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001630 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
1632
Finn Thaina53a21e2014-11-12 16:12:21 +11001633static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Roman Zippelc28bda22007-05-01 22:32:36 +02001635 unsigned char tmp, *msgptr, phase;
1636 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001637 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Roman Zippelc28bda22007-05-01 22:32:36 +02001639 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Roman Zippelc28bda22007-05-01 22:32:36 +02001642 /*
1643 * Wait for the target to indicate a valid phase by asserting
1644 * REQ. Once this happens, we'll have either a MSGOUT phase
1645 * and can immediately send the ABORT message, or we'll have some
1646 * other phase and will have to source/sink data.
1647 *
1648 * We really don't care what value was on the bus or what value
1649 * the target sees, so we just handshake.
1650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Finn Thain80d3eb62016-01-03 16:05:34 +11001652 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1653 if (rc < 0)
1654 goto timeout;
1655
1656 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001657
1658 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1659
Finn Thain80d3eb62016-01-03 16:05:34 +11001660 if (tmp != PHASE_MSGOUT) {
1661 NCR5380_write(INITIATOR_COMMAND_REG,
1662 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1663 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1664 if (rc < 0)
1665 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001666 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1667 }
1668
1669 tmp = ABORT;
1670 msgptr = &tmp;
1671 len = 1;
1672 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001673 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001674
1675 /*
1676 * If we got here, and the command completed successfully,
1677 * we're about to go into bus free state.
1678 */
1679
1680 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001681
1682timeout:
1683 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1684 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001688/*
1689 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001690 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 *
1692 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001693 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001695 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001696 * what phase is expected, *count - pointer to number of
1697 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001700 * maximum number of bytes, 0 if all bytes or transferred or exit
1701 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001703 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
1705
1706
Roman Zippelc28bda22007-05-01 22:32:36 +02001707static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1708 unsigned char *phase, int *count,
1709 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Finn Thaine8a60142016-01-03 16:05:54 +11001711 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001712 register int c = *count;
1713 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001714
1715#if defined(CONFIG_SUN3)
1716 /* sanity check */
1717 if (!sun3_dma_setup_done) {
1718 pr_err("scsi%d: transfer_dma without setup!\n",
1719 instance->host_no);
1720 BUG();
1721 }
1722 hostdata->dma_len = c;
1723
Finn Thainb7465452016-01-03 16:06:05 +11001724 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1725 (p & SR_IO) ? "receive" : "send", c, *data);
Finn Thaine3f463b2014-11-12 16:12:16 +11001726
1727 /* netbsd turns off ints here, why not be safe and do it too */
Finn Thaine3f463b2014-11-12 16:12:16 +11001728
1729 /* send start chain */
1730 sun3scsi_dma_start(c, *data);
1731
Finn Thaincd400822016-01-03 16:05:40 +11001732 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1733 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1734 MR_ENABLE_EOP_INTR);
Finn Thaine3f463b2014-11-12 16:12:16 +11001735 if (p & SR_IO) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001736 NCR5380_write(INITIATOR_COMMAND_REG, 0);
Finn Thaine3f463b2014-11-12 16:12:16 +11001737 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1738 } else {
Finn Thaine3f463b2014-11-12 16:12:16 +11001739 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
Finn Thaine3f463b2014-11-12 16:12:16 +11001740 NCR5380_write(START_DMA_SEND_REG, 0);
1741 }
1742
1743#ifdef SUN3_SCSI_VME
1744 dregs->csr |= CSR_DMA_ENABLE;
1745#endif
1746
Finn Thaine3f463b2014-11-12 16:12:16 +11001747 sun3_dma_active = 1;
1748
1749#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001750 register unsigned char *d = *data;
1751 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Roman Zippelc28bda22007-05-01 22:32:36 +02001753 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1754 *phase = tmp;
1755 return -1;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Finn Thainef1081c2014-11-12 16:12:14 +11001758 if (hostdata->read_overruns && (p & SR_IO))
1759 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Finn Thainb7465452016-01-03 16:06:05 +11001761 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1762 (p & SR_IO) ? "receive" : "send", c, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Roman Zippelc28bda22007-05-01 22:32:36 +02001764 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thaincd400822016-01-03 16:05:40 +11001765 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1766 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Finn Thainef1081c2014-11-12 16:12:14 +11001768 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001769 /* On the Medusa, it is a must to initialize the DMA before
1770 * starting the NCR. This is also the cleaner way for the TT.
1771 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001772 hostdata->dma_len = (p & SR_IO) ?
1773 NCR5380_dma_read_setup(instance, d, c) :
1774 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Roman Zippelc28bda22007-05-01 22:32:36 +02001777 if (p & SR_IO)
1778 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1779 else {
1780 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1781 NCR5380_write(START_DMA_SEND_REG, 0);
1782 }
1783
Finn Thainef1081c2014-11-12 16:12:14 +11001784 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001785 /* On the Falcon, the DMA setup must be done after the last */
1786 /* NCR access, else the DMA setup gets trashed!
1787 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001788 hostdata->dma_len = (p & SR_IO) ?
1789 NCR5380_dma_read_setup(instance, d, c) :
1790 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001791 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001792#endif /* !defined(CONFIG_SUN3) */
1793
Roman Zippelc28bda22007-05-01 22:32:36 +02001794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796#endif /* defined(REAL_DMA) */
1797
1798/*
1799 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1800 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001801 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001802 * directs us to. Operates on the currently connected command,
1803 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 *
1805 * Inputs : instance, instance for which we are doing commands
1806 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001807 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001808 * modified if a command disconnects, *instance->connected will
1809 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001811 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001812 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001814
1815static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Finn Thaine8a60142016-01-03 16:05:54 +11001817 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001818 unsigned char msgout = NOP;
1819 int sink = 0;
1820 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001822 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001824 unsigned char *data;
1825 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001826 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Finn Thaine3f463b2014-11-12 16:12:16 +11001828#ifdef SUN3_SCSI_VME
1829 dregs->csr |= CSR_INTR;
1830#endif
1831
Finn Thain11d2f632016-01-03 16:05:51 +11001832 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001833 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1834
Roman Zippelc28bda22007-05-01 22:32:36 +02001835 tmp = NCR5380_read(STATUS_REG);
1836 /* We only have a valid SCSI phase when REQ is asserted */
1837 if (tmp & SR_REQ) {
1838 phase = (tmp & PHASE_MASK);
1839 if (phase != old_phase) {
1840 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001841 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001843#if defined(CONFIG_SUN3)
1844 if (phase == PHASE_CMDOUT) {
1845#if defined(REAL_DMA)
1846 void *d;
1847 unsigned long count;
1848
1849 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1850 count = cmd->SCp.buffer->length;
1851 d = sg_virt(cmd->SCp.buffer);
1852 } else {
1853 count = cmd->SCp.this_residual;
1854 d = cmd->SCp.ptr;
1855 }
1856 /* this command setup for dma yet? */
1857 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
1858 if (cmd->request->cmd_type == REQ_TYPE_FS) {
Finn Thaincd461402016-01-03 16:06:06 +11001859 sun3scsi_dma_setup(instance, d, count,
Finn Thaine3f463b2014-11-12 16:12:16 +11001860 rq_data_dir(cmd->request));
1861 sun3_dma_setup_done = cmd;
1862 }
1863 }
1864#endif
1865#ifdef SUN3_SCSI_VME
1866 dregs->csr |= CSR_INTR;
1867#endif
1868 }
1869#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Roman Zippelc28bda22007-05-01 22:32:36 +02001871 if (sink && (phase != PHASE_MSGOUT)) {
1872 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Roman Zippelc28bda22007-05-01 22:32:36 +02001874 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1875 ICR_ASSERT_ACK);
1876 while (NCR5380_read(STATUS_REG) & SR_REQ)
1877 ;
1878 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1879 ICR_ASSERT_ATN);
1880 sink = 0;
1881 continue;
1882 }
1883
1884 switch (phase) {
1885 case PHASE_DATAOUT:
1886#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001887 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001888 sink = 1;
1889 do_abort(instance);
1890 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001891 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001892 return;
1893#endif
1894 case PHASE_DATAIN:
1895 /*
1896 * If there is no room left in the current buffer in the
1897 * scatter-gather list, move onto the next one.
1898 */
1899
1900 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1901 ++cmd->SCp.buffer;
1902 --cmd->SCp.buffers_residual;
1903 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001904 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02001905 merge_contiguous_buffers(cmd);
Finn Thainb7465452016-01-03 16:06:05 +11001906 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1907 cmd->SCp.this_residual,
1908 cmd->SCp.buffers_residual);
Roman Zippelc28bda22007-05-01 22:32:36 +02001909 }
1910
1911 /*
1912 * The preferred transfer method is going to be
1913 * PSEUDO-DMA for systems that are strictly PIO,
1914 * since we can let the hardware do the handshaking.
1915 *
1916 * For this to work, we need to know the transfersize
1917 * ahead of time, since the pseudo-DMA code will sit
1918 * in an unconditional loop.
1919 */
1920
1921 /* ++roman: I suggest, this should be
Finn Thain594d4ba2016-01-03 16:06:10 +11001922 * #if def(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001923 * instead of leaving REAL_DMA out.
1924 */
1925
1926#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11001927#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11001928 transfersize = 0;
1929 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11001930#endif
Finn Thainff3d4572016-01-03 16:05:25 +11001931 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1932
1933 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001934 len = transfersize;
1935 cmd->SCp.phase = phase;
1936 if (NCR5380_transfer_dma(instance, &phase,
1937 &len, (unsigned char **)&cmd->SCp.ptr)) {
1938 /*
1939 * If the watchdog timer fires, all future
1940 * accesses to this device will use the
Finn Thainc16df322016-01-03 16:06:08 +11001941 * polled-IO.
1942 */
Finn Thainff50f9e2014-11-12 16:12:18 +11001943 scmd_printk(KERN_INFO, cmd,
1944 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001945 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001946 sink = 1;
1947 do_abort(instance);
1948 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001949 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001950 /* XXX - need to source or sink data here, as appropriate */
1951 } else {
1952#ifdef REAL_DMA
1953 /* ++roman: When using real DMA,
1954 * information_transfer() should return after
1955 * starting DMA since it has nothing more to
1956 * do.
1957 */
1958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001960 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001962 }
1963 } else
1964#endif /* defined(REAL_DMA) */
Finn Thain11d2f632016-01-03 16:05:51 +11001965 {
1966 spin_unlock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001967 NCR5380_transfer_pio(instance, &phase,
1968 (int *)&cmd->SCp.this_residual,
1969 (unsigned char **)&cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11001970 spin_lock_irq(&hostdata->lock);
1971 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001972#if defined(CONFIG_SUN3) && defined(REAL_DMA)
1973 /* if we had intended to dma that command clear it */
1974 if (sun3_dma_setup_done == cmd)
1975 sun3_dma_setup_done = NULL;
1976#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001977 break;
1978 case PHASE_MSGIN:
1979 len = 1;
1980 data = &tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02001981 NCR5380_transfer_pio(instance, &phase, &len, &data);
1982 cmd->SCp.Message = tmp;
1983
1984 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001985 case ABORT:
1986 case COMMAND_COMPLETE:
1987 /* Accept message by clearing ACK */
1988 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001989 dsprintk(NDEBUG_QUEUES, instance,
1990 "COMMAND COMPLETE %p target %d lun %llu\n",
1991 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11001992
Finn Thaine3c3da62014-11-12 16:12:15 +11001993 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001994#ifdef SUPPORT_TAGS
1995 cmd_free_tag(cmd);
1996 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
Finn Thainb7465452016-01-03 16:06:05 +11001997 u8 lun = cmd->device->lun;
1998 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
1999
2000 dsprintk(NDEBUG_TAGS, instance,
2001 "QUEUE_FULL %p target %d lun %d nr_allocated %d\n",
2002 cmd, scmd_id(cmd), lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +02002003 if (ta->queue_size > ta->nr_allocated)
Finn Thaine42d0152016-01-03 16:05:49 +11002004 ta->queue_size = ta->nr_allocated;
Roman Zippelc28bda22007-05-01 22:32:36 +02002005 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002006#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002007
Finn Thainf27db8e2016-01-03 16:06:00 +11002008 cmd->result &= ~0xffff;
2009 cmd->result |= cmd->SCp.Status;
2010 cmd->result |= cmd->SCp.Message << 8;
Roman Zippelc28bda22007-05-01 22:32:36 +02002011
Finn Thainf27db8e2016-01-03 16:06:00 +11002012 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11002013 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11002014 else {
2015 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
2016 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
2017 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
2018 cmd);
2019 list_add_tail(&ncmd->list,
2020 &hostdata->autosense);
2021 } else
2022 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002023 }
2024
Roman Zippelc28bda22007-05-01 22:32:36 +02002025 /*
2026 * Restore phase bits to 0 so an interrupted selection,
2027 * arbitration can resume.
2028 */
2029 NCR5380_write(TARGET_COMMAND_REG, 0);
2030
Finn Thain72064a72016-01-03 16:05:44 +11002031 /* Enable reselect interrupts */
2032 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2033
Finn Thaine3c3da62014-11-12 16:12:15 +11002034 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002035 return;
2036 case MESSAGE_REJECT:
2037 /* Accept message by clearing ACK */
2038 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002039 switch (hostdata->last_message) {
2040 case HEAD_OF_QUEUE_TAG:
2041 case ORDERED_QUEUE_TAG:
2042 case SIMPLE_QUEUE_TAG:
2043 /* The target obviously doesn't support tagged
2044 * queuing, even though it announced this ability in
2045 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2046 * clear 'tagged_supported' and lock the LUN, since
2047 * the command is treated as untagged further on.
2048 */
2049 cmd->device->tagged_supported = 0;
2050 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2051 cmd->tag = TAG_NONE;
Finn Thainb7465452016-01-03 16:06:05 +11002052 dsprintk(NDEBUG_TAGS, instance, "target %d lun %llu rejected QUEUE_TAG message; tagged queuing disabled\n",
2053 scmd_id(cmd), cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002054 break;
2055 }
2056 break;
2057 case DISCONNECT:
2058 /* Accept message by clearing ACK */
2059 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002060 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11002061 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002062 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
2063 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
2064 cmd, scmd_id(cmd), cmd->device->lun);
2065
Roman Zippelc28bda22007-05-01 22:32:36 +02002066 /*
2067 * Restore phase bits to 0 so an interrupted selection,
2068 * arbitration can resume.
2069 */
2070 NCR5380_write(TARGET_COMMAND_REG, 0);
2071
2072 /* Enable reselect interrupts */
2073 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002074#ifdef SUN3_SCSI_VME
2075 dregs->csr |= CSR_DMA_ENABLE;
2076#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002077 return;
2078 /*
2079 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2080 * operation, in violation of the SCSI spec so we can safely
2081 * ignore SAVE/RESTORE pointers calls.
2082 *
2083 * Unfortunately, some disks violate the SCSI spec and
2084 * don't issue the required SAVE_POINTERS message before
2085 * disconnecting, and we have to break spec to remain
2086 * compatible.
2087 */
2088 case SAVE_POINTERS:
2089 case RESTORE_POINTERS:
2090 /* Accept message by clearing ACK */
2091 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002092 break;
2093 case EXTENDED_MESSAGE:
2094 /*
Finn Thainc16df322016-01-03 16:06:08 +11002095 * Start the message buffer with the EXTENDED_MESSAGE
Roman Zippelc28bda22007-05-01 22:32:36 +02002096 * byte, since spi_print_msg() wants the whole thing.
2097 */
2098 extended_msg[0] = EXTENDED_MESSAGE;
2099 /* Accept first byte by clearing ACK */
2100 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2101
Finn Thain11d2f632016-01-03 16:05:51 +11002102 spin_unlock_irq(&hostdata->lock);
2103
Finn Thainb7465452016-01-03 16:06:05 +11002104 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002105
2106 len = 2;
2107 data = extended_msg + 1;
2108 phase = PHASE_MSGIN;
2109 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002110 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
2111 (int)extended_msg[1],
2112 (int)extended_msg[2]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002113
Finn Thaine0783ed2016-01-03 16:05:45 +11002114 if (!len && extended_msg[1] > 0 &&
2115 extended_msg[1] <= sizeof(extended_msg) - 2) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002116 /* Accept third byte by clearing ACK */
2117 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2118 len = extended_msg[1] - 1;
2119 data = extended_msg + 3;
2120 phase = PHASE_MSGIN;
2121
2122 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002123 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
2124 len);
Roman Zippelc28bda22007-05-01 22:32:36 +02002125
2126 switch (extended_msg[2]) {
2127 case EXTENDED_SDTR:
2128 case EXTENDED_WDTR:
2129 case EXTENDED_MODIFY_DATA_POINTER:
2130 case EXTENDED_EXTENDED_IDENTIFY:
2131 tmp = 0;
2132 }
2133 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002134 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002135 tmp = 0;
2136 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002137 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2138 extended_msg[2], extended_msg[1]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002139 tmp = 0;
2140 }
Finn Thain11d2f632016-01-03 16:05:51 +11002141
2142 spin_lock_irq(&hostdata->lock);
2143 if (!hostdata->connected)
2144 return;
2145
Roman Zippelc28bda22007-05-01 22:32:36 +02002146 /* Fall through to reject message */
2147
2148 /*
2149 * If we get something weird that we aren't expecting,
2150 * reject it.
2151 */
2152 default:
2153 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002154 shost_printk(KERN_ERR, instance, "rejecting message ");
Roman Zippelc28bda22007-05-01 22:32:36 +02002155 spi_print_msg(extended_msg);
2156 printk("\n");
2157 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002158 scmd_printk(KERN_INFO, cmd,
2159 "rejecting unknown message %02x\n",
2160 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002161 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002162 scmd_printk(KERN_INFO, cmd,
2163 "rejecting unknown extended message code %02x, length %d\n",
2164 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002165
2166 msgout = MESSAGE_REJECT;
2167 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2168 break;
2169 } /* switch (tmp) */
2170 break;
2171 case PHASE_MSGOUT:
2172 len = 1;
2173 data = &msgout;
2174 hostdata->last_message = msgout;
2175 NCR5380_transfer_pio(instance, &phase, &len, &data);
2176 if (msgout == ABORT) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002177 hostdata->connected = NULL;
2178 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002179 complete_cmd(instance, cmd);
Finn Thaine3c3da62014-11-12 16:12:15 +11002180 maybe_release_dma_irq(instance);
Finn Thaincd400822016-01-03 16:05:40 +11002181 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02002182 return;
2183 }
2184 msgout = NOP;
2185 break;
2186 case PHASE_CMDOUT:
2187 len = cmd->cmd_len;
2188 data = cmd->cmnd;
2189 /*
2190 * XXX for performance reasons, on machines with a
2191 * PSEUDO-DMA architecture we should probably
2192 * use the dma transfer function.
2193 */
2194 NCR5380_transfer_pio(instance, &phase, &len, &data);
2195 break;
2196 case PHASE_STATIN:
2197 len = 1;
2198 data = &tmp;
2199 NCR5380_transfer_pio(instance, &phase, &len, &data);
2200 cmd->SCp.Status = tmp;
2201 break;
2202 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002203 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11002204 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002205 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002206 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002207 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002208 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002209 spin_lock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002210 }
Finn Thain11d2f632016-01-03 16:05:51 +11002211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214/*
2215 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2216 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002217 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002218 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2219 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
2223
2224
Finn Thaine3f463b2014-11-12 16:12:16 +11002225/* it might eventually prove necessary to do a dma setup on
2226 reselection, but it doesn't seem to be needed now -- sam */
2227
Roman Zippelc28bda22007-05-01 22:32:36 +02002228static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Finn Thaine8a60142016-01-03 16:05:54 +11002230 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002231 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002232 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002234 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002236 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002237 int __maybe_unused len;
2238 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain32b26a12016-01-03 16:05:58 +11002239 struct NCR5380_cmd *ncmd;
2240 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Roman Zippelc28bda22007-05-01 22:32:36 +02002242 /*
2243 * Disable arbitration, etc. since the host adapter obviously
2244 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Roman Zippelc28bda22007-05-01 22:32:36 +02002247 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Roman Zippelc28bda22007-05-01 22:32:36 +02002249 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2250
Finn Thainb7465452016-01-03 16:06:05 +11002251 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002252
2253 /*
2254 * At this point, we have detected that our SCSI ID is on the bus,
2255 * SEL is true and BSY was false for at least one bus settle delay
2256 * (400 ns).
2257 *
2258 * We must assert BSY ourselves, until the target drops the SEL
2259 * signal.
2260 */
2261
2262 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002263 if (NCR5380_poll_politely(instance,
2264 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2265 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2266 return;
2267 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002268 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2269
2270 /*
2271 * Wait for target to go into MSGIN.
2272 */
2273
Finn Thain72064a72016-01-03 16:05:44 +11002274 if (NCR5380_poll_politely(instance,
2275 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2276 do_abort(instance);
2277 return;
2278 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002279
Finn Thaine3f463b2014-11-12 16:12:16 +11002280#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2281 /* acknowledge toggle to MSGIN */
2282 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2283
2284 /* peek at the byte without really hitting the bus */
2285 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2286#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002287 len = 1;
2288 data = msg;
2289 phase = PHASE_MSGIN;
2290 NCR5380_transfer_pio(instance, &phase, &len, &data);
2291
Finn Thain72064a72016-01-03 16:05:44 +11002292 if (len) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002293 do_abort(instance);
2294 return;
2295 }
Finn Thain72064a72016-01-03 16:05:44 +11002296#endif
2297
2298 if (!(msg[0] & 0x80)) {
2299 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2300 spi_print_msg(msg);
2301 printk("\n");
2302 do_abort(instance);
2303 return;
2304 }
2305 lun = msg[0] & 0x07;
Roman Zippelc28bda22007-05-01 22:32:36 +02002306
Finn Thaine3f463b2014-11-12 16:12:16 +11002307#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002308 /* If the phase is still MSGIN, the target wants to send some more
2309 * messages. In case it supports tagged queuing, this is probably a
2310 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2311 */
2312 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002313 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002314 /* Accept previous IDENTIFY message by clearing ACK */
2315 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2316 len = 2;
2317 data = msg + 1;
2318 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2319 msg[1] == SIMPLE_QUEUE_TAG)
2320 tag = msg[2];
Finn Thainb7465452016-01-03 16:06:05 +11002321 dsprintk(NDEBUG_TAGS, instance, "reselect: target mask %02x, lun %d sent tag %d\n",
2322 target_mask, lun, tag);
Roman Zippelc28bda22007-05-01 22:32:36 +02002323 }
2324#endif
2325
2326 /*
2327 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2328 * just reestablished, and remove it from the disconnected queue.
2329 */
2330
Finn Thain32b26a12016-01-03 16:05:58 +11002331 tmp = NULL;
2332 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2333 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2334
2335 if (target_mask == (1 << scmd_id(cmd)) &&
2336 lun == (u8)cmd->device->lun
Roman Zippelc28bda22007-05-01 22:32:36 +02002337#ifdef SUPPORT_TAGS
Finn Thain32b26a12016-01-03 16:05:58 +11002338 && (tag == cmd->tag)
Roman Zippelc28bda22007-05-01 22:32:36 +02002339#endif
2340 ) {
Finn Thain32b26a12016-01-03 16:05:58 +11002341 list_del(&ncmd->list);
2342 tmp = cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +02002343 break;
2344 }
2345 }
2346
Finn Thain0d3d9a42016-01-03 16:05:55 +11002347 if (tmp) {
2348 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2349 "reselect: removed %p from disconnected queue\n", tmp);
2350 } else {
2351
Roman Zippelc28bda22007-05-01 22:32:36 +02002352#ifdef SUPPORT_TAGS
Finn Thain72064a72016-01-03 16:05:44 +11002353 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d tag %d not in disconnected queue.\n",
2354 target_mask, lun, tag);
2355#else
2356 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2357 target_mask, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002358#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002359 /*
2360 * Since we have an established nexus that we can't do anything
2361 * with, we must abort it.
2362 */
2363 do_abort(instance);
2364 return;
2365 }
2366
Finn Thaine3f463b2014-11-12 16:12:16 +11002367#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2368 /* engage dma setup for the command we just saw */
2369 {
2370 void *d;
2371 unsigned long count;
2372
2373 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2374 count = tmp->SCp.buffer->length;
2375 d = sg_virt(tmp->SCp.buffer);
2376 } else {
2377 count = tmp->SCp.this_residual;
2378 d = tmp->SCp.ptr;
2379 }
2380 /* setup this command for dma if not already */
2381 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
Finn Thaincd461402016-01-03 16:06:06 +11002382 sun3scsi_dma_setup(instance, d, count,
2383 rq_data_dir(tmp->request));
Finn Thaine3f463b2014-11-12 16:12:16 +11002384 sun3_dma_setup_done = tmp;
2385 }
2386 }
2387
2388 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2389#endif
2390
Roman Zippelc28bda22007-05-01 22:32:36 +02002391 /* Accept message by clearing ACK */
2392 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2393
Finn Thaine3f463b2014-11-12 16:12:16 +11002394#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2395 /* If the phase is still MSGIN, the target wants to send some more
2396 * messages. In case it supports tagged queuing, this is probably a
2397 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2398 */
2399 tag = TAG_NONE;
2400 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2401 /* Accept previous IDENTIFY message by clearing ACK */
2402 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2403 len = 2;
2404 data = msg + 1;
2405 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2406 msg[1] == SIMPLE_QUEUE_TAG)
2407 tag = msg[2];
Finn Thainb7465452016-01-03 16:06:05 +11002408 dsprintk(NDEBUG_TAGS, instance, "reselect: target mask %02x, lun %d sent tag %d\n"
2409 target_mask, lun, tag);
Finn Thaine3f463b2014-11-12 16:12:16 +11002410 }
2411#endif
2412
Roman Zippelc28bda22007-05-01 22:32:36 +02002413 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002414 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2415 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418
Finn Thain8b00c3d2016-01-03 16:06:01 +11002419/**
2420 * list_find_cmd - test for presence of a command in a linked list
2421 * @haystack: list of commands
2422 * @needle: command to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 */
2424
Finn Thain8b00c3d2016-01-03 16:06:01 +11002425static bool list_find_cmd(struct list_head *haystack,
2426 struct scsi_cmnd *needle)
2427{
2428 struct NCR5380_cmd *ncmd;
2429
2430 list_for_each_entry(ncmd, haystack, list)
2431 if (NCR5380_to_scmd(ncmd) == needle)
2432 return true;
2433 return false;
2434}
2435
2436/**
2437 * list_remove_cmd - remove a command from linked list
2438 * @haystack: list of commands
2439 * @needle: command to remove
2440 */
2441
2442static bool list_del_cmd(struct list_head *haystack,
2443 struct scsi_cmnd *needle)
2444{
2445 if (list_find_cmd(haystack, needle)) {
2446 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2447
2448 list_del(&ncmd->list);
2449 return true;
2450 }
2451 return false;
2452}
2453
2454/**
2455 * NCR5380_abort - scsi host eh_abort_handler() method
2456 * @cmd: the command to be aborted
2457 *
2458 * Try to abort a given command by removing it from queues and/or sending
2459 * the target an abort message. This may not succeed in causing a target
2460 * to abort the command. Nonetheless, the low-level driver must forget about
2461 * the command because the mid-layer reclaims it and it may be re-issued.
2462 *
2463 * The normal path taken by a command is as follows. For EH we trace this
2464 * same path to locate and abort the command.
2465 *
2466 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2467 * [disconnected -> connected ->]...
2468 * [autosense -> connected ->] done
2469 *
2470 * If cmd is unissued then just remove it.
2471 * If cmd is disconnected, try to select the target.
2472 * If cmd is connected, try to send an abort message.
2473 * If cmd is waiting for autosense, give it a chance to complete but check
2474 * that it isn't left connected.
2475 * If cmd was not found at all then presumably it has already been completed,
2476 * in which case return SUCCESS to try to avoid further EH measures.
2477 * If the command has not completed yet, we must not fail to find it.
2478 */
2479
2480static int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Roman Zippelc28bda22007-05-01 22:32:36 +02002482 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002483 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002484 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002485 int result = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Finn Thain11d2f632016-01-03 16:05:51 +11002487 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Finn Thain32b26a12016-01-03 16:05:58 +11002489#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002490 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002491#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002492 NCR5380_dprint(NDEBUG_ANY, instance);
2493 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Finn Thain8b00c3d2016-01-03 16:06:01 +11002495 if (list_del_cmd(&hostdata->unissued, cmd)) {
2496 dsprintk(NDEBUG_ABORT, instance,
2497 "abort: removed %p from issue queue\n", cmd);
2498 cmd->result = DID_ABORT << 16;
2499 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2500 }
2501
Finn Thain707d62b2016-01-03 16:06:02 +11002502 if (hostdata->selecting == cmd) {
2503 dsprintk(NDEBUG_ABORT, instance,
2504 "abort: cmd %p == selecting\n", cmd);
2505 hostdata->selecting = NULL;
2506 cmd->result = DID_ABORT << 16;
2507 complete_cmd(instance, cmd);
2508 goto out;
2509 }
2510
Finn Thain8b00c3d2016-01-03 16:06:01 +11002511 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2512 dsprintk(NDEBUG_ABORT, instance,
2513 "abort: removed %p from disconnected list\n", cmd);
2514 cmd->result = DID_ERROR << 16;
2515 if (!hostdata->connected)
2516 NCR5380_select(instance, cmd);
2517 if (hostdata->connected != cmd) {
2518 complete_cmd(instance, cmd);
2519 result = FAILED;
2520 goto out;
2521 }
2522 }
2523
2524 if (hostdata->connected == cmd) {
2525 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2526 hostdata->connected = NULL;
2527 if (do_abort(instance)) {
2528 set_host_byte(cmd, DID_ERROR);
2529 complete_cmd(instance, cmd);
2530 result = FAILED;
2531 goto out;
2532 }
2533 set_host_byte(cmd, DID_ABORT);
2534#ifdef REAL_DMA
2535 hostdata->dma_len = 0;
2536#endif
2537 if (cmd->cmnd[0] == REQUEST_SENSE)
2538 complete_cmd(instance, cmd);
2539 else {
2540 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
2541
2542 /* Perform autosense for this command */
2543 list_add(&ncmd->list, &hostdata->autosense);
2544 }
2545 }
2546
2547 if (list_find_cmd(&hostdata->autosense, cmd)) {
2548 dsprintk(NDEBUG_ABORT, instance,
2549 "abort: found %p on sense queue\n", cmd);
2550 spin_unlock_irqrestore(&hostdata->lock, flags);
2551 queue_work(hostdata->work_q, &hostdata->main_task);
2552 msleep(1000);
2553 spin_lock_irqsave(&hostdata->lock, flags);
2554 if (list_del_cmd(&hostdata->autosense, cmd)) {
2555 dsprintk(NDEBUG_ABORT, instance,
2556 "abort: removed %p from sense queue\n", cmd);
2557 set_host_byte(cmd, DID_ABORT);
2558 complete_cmd(instance, cmd);
2559 goto out;
2560 }
2561 }
2562
2563 if (hostdata->connected == cmd) {
2564 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2565 hostdata->connected = NULL;
2566 if (do_abort(instance)) {
2567 set_host_byte(cmd, DID_ERROR);
2568 complete_cmd(instance, cmd);
2569 result = FAILED;
2570 goto out;
2571 }
2572 set_host_byte(cmd, DID_ABORT);
2573#ifdef REAL_DMA
2574 hostdata->dma_len = 0;
2575#endif
2576 complete_cmd(instance, cmd);
2577 }
2578
2579out:
2580 if (result == FAILED)
2581 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2582 else
2583 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2584
2585 queue_work(hostdata->work_q, &hostdata->main_task);
2586 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002587 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002588
Finn Thain8b00c3d2016-01-03 16:06:01 +11002589 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592
Finn Thain3be1b3e2016-01-03 16:05:12 +11002593/**
2594 * NCR5380_bus_reset - reset the SCSI bus
2595 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002596 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002597 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Finn Thain710ddd02014-11-12 16:12:02 +11002600static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Finn Thaine3c3da62014-11-12 16:12:15 +11002602 struct Scsi_Host *instance = cmd->device->host;
2603 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002604 int i;
2605 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002606 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Finn Thain11d2f632016-01-03 16:05:51 +11002608 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Finn Thain3be1b3e2016-01-03 16:05:12 +11002610#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002611 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002612#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002613 NCR5380_dprint(NDEBUG_ANY, instance);
2614 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002615
2616 do_reset(instance);
2617
Roman Zippelc28bda22007-05-01 22:32:36 +02002618 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002619 NCR5380_write(MODE_REG, MR_BASE);
2620 NCR5380_write(TARGET_COMMAND_REG, 0);
2621 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Roman Zippelc28bda22007-05-01 22:32:36 +02002623 /* After the reset, there are no more connected or disconnected commands
2624 * and no busy units; so clear the low-level status here to avoid
2625 * conflicts when the mid-level code tries to wake up the affected
2626 * commands!
2627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Finn Thain707d62b2016-01-03 16:06:02 +11002629 hostdata->selecting = NULL;
2630
Finn Thain62717f52016-01-03 16:06:03 +11002631 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2632 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2633
2634 set_host_byte(cmd, DID_RESET);
2635 cmd->scsi_done(cmd);
2636 }
2637
2638 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2639 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2640
2641 set_host_byte(cmd, DID_RESET);
2642 cmd->scsi_done(cmd);
2643 }
2644
2645 if (hostdata->connected) {
2646 set_host_byte(hostdata->connected, DID_RESET);
2647 complete_cmd(instance, hostdata->connected);
2648 hostdata->connected = NULL;
2649 }
Finn Thain32b26a12016-01-03 16:05:58 +11002650
Finn Thainf27db8e2016-01-03 16:06:00 +11002651 if (hostdata->sensing) {
Finn Thain62717f52016-01-03 16:06:03 +11002652 set_host_byte(hostdata->connected, DID_RESET);
Finn Thainf27db8e2016-01-03 16:06:00 +11002653 complete_cmd(instance, hostdata->sensing);
2654 hostdata->sensing = NULL;
2655 }
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002658 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002660 for (i = 0; i < 8; ++i)
2661 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002663 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002665
Finn Thain62717f52016-01-03 16:06:03 +11002666 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thaine3c3da62014-11-12 16:12:15 +11002667 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002668 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002670 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}