blob: 12b98cbe7b449695aa82e2b8a3e77d2ca5fea343 [file] [log] [blame]
Roman Zippelc28bda22007-05-01 22:32:36 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Roman Zippelc28bda22007-05-01 22:32:36 +02003 * to implement 5380 SCSI drivers under Linux with a non-trantor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
Roman Zippelc28bda22007-05-01 22:32:36 +02009 * Visionary Computing
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (Unix and Linux consulting and custom programming)
Roman Zippelc28bda22007-05-01 22:32:36 +020011 * drew@colorado.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * +1 (303) 666-5836
13 *
Roman Zippelc28bda22007-05-01 22:32:36 +020014 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
28 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
29 * this file, too:
30 *
31 * - Some of the debug statements were incorrect (undefined variables and the
32 * like). I fixed that.
33 *
34 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
35 * possible DMA transfer size should also happen for REAL_DMA. I added this
36 * in the #if statement.
37 *
38 * - When using real DMA, information_transfer() should return in a DATAOUT
39 * phase after starting the DMA. It has nothing more to do.
40 *
41 * - The interrupt service routine should run main after end of DMA, too (not
42 * only after RESELECTION interrupts). Additionally, it should _not_ test
43 * for more interrupts after running main, since a DMA process may have
44 * been started and interrupts are turned on now. The new int could happen
45 * inside the execution of NCR5380_intr(), leading to recursive
46 * calls.
47 *
48 * - I've added a function merge_contiguous_buffers() that tries to
49 * merge scatter-gather buffers that are located at contiguous
50 * physical addresses and can be processed with the same DMA setup.
51 * Since most scatter-gather operations work on a page (4K) of
52 * 4 buffers (1K), in more than 90% of all cases three interrupts and
53 * DMA setup actions are saved.
54 *
55 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
56 * and USLEEP, because these were messing up readability and will never be
57 * needed for Atari SCSI.
Roman Zippelc28bda22007-05-01 22:32:36 +020058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
60 * stuff), and 'main' is executed in a bottom half if awoken by an
61 * interrupt.
62 *
63 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
64 * constructs. In my eyes, this made the source rather unreadable, so I
65 * finally replaced that by the *_PRINTK() macros.
66 *
67 */
68
Finn Thaine3f463b2014-11-12 16:12:16 +110069/* Adapted for the sun3 by Sam Creasey. */
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * Design
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
Roman Zippelc28bda22007-05-01 22:32:36 +020074 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * one simply writes appropriate system specific macros (ie, data
Roman Zippelc28bda22007-05-01 22:32:36 +020076 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * memory mapped) and drops this file in their 'C' wrapper.
78 *
Roman Zippelc28bda22007-05-01 22:32:36 +020079 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * each 5380 in the system - commands that haven't been issued yet,
Roman Zippelc28bda22007-05-01 22:32:36 +020081 * and commands that are currently executing. This means that an
82 * unlimited number of commands may be queued, letting
83 * more commands propagate from the higher driver levels giving higher
84 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
85 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * while a command is already executing.
87 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Roman Zippelc28bda22007-05-01 22:32:36 +020089 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *
Roman Zippelc28bda22007-05-01 22:32:36 +020091 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
92 * piece of hardware that requires you to sit in a loop polling for
93 * the REQ signal as long as you are connected. Some devices are
94 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110095 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * broken devices are the exception rather than the rule and I'd rather
97 * spend my time optimizing for the normal case.
98 *
99 * Architecture :
100 *
101 * At the heart of the design is a coroutine, NCR5380_main,
Finn Thainff50f9e2014-11-12 16:12:18 +1100102 * which is started from a workqueue for each NCR5380 host in the
103 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
104 * removing the commands from the issue queue and calling
105 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * Once a nexus is established, the NCR5380_information_transfer()
108 * phase goes through the various phases as instructed by the target.
109 * if the target goes into MSG IN and sends a DISCONNECT message,
110 * the command structure is placed into the per instance disconnected
Finn Thainff50f9e2014-11-12 16:12:18 +1100111 * queue, and NCR5380_main tries to find more work. If the target is
112 * idle for too long, the system will try to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
114 * If a command has disconnected, eventually an interrupt will trigger,
115 * calling NCR5380_intr() which will in turn call NCR5380_reselect
116 * to reestablish a nexus. This will run main if necessary.
117 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200118 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * appropriate.
120 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200121 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * structures, being initialized after the command is connected
123 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
124 * Note that in violation of the standard, an implicit SAVE POINTERS operation
125 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
126 */
127
128/*
129 * Using this file :
130 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Roman Zippelc28bda22007-05-01 22:32:36 +0200131 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * and macros and include this file in your driver.
133 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200134 * These macros control options :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Roman Zippelc28bda22007-05-01 22:32:36 +0200136 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100138 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
139 * transceivers.
140 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
142 *
143 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
144 *
145 * These macros MUST be defined :
Roman Zippelc28bda22007-05-01 22:32:36 +0200146 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * NCR5380_read(register) - read from the specified register
148 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200149 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100151 * NCR5380_implementation_fields - additional fields needed for this
152 * specific implementation of the NCR5380
153 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Either real DMA *or* pseudo DMA may be implemented
Roman Zippelc28bda22007-05-01 22:32:36 +0200155 * REAL functions :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
Roman Zippelc28bda22007-05-01 22:32:36 +0200157 * Note that the DMA setup functions should return the number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * that they were able to program the controller for.
159 *
Roman Zippelc28bda22007-05-01 22:32:36 +0200160 * Also note that generic i386/PC versions of these macros are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * available as NCR5380_i386_dma_write_setup,
162 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
163 *
164 * NCR5380_dma_write_setup(instance, src, count) - initialize
165 * NCR5380_dma_read_setup(instance, dst, count) - initialize
166 * NCR5380_dma_residual(instance); - residual count
167 *
168 * PSEUDO functions :
169 * NCR5380_pwrite(instance, src, count)
170 * NCR5380_pread(instance, dst, count);
171 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * The generic driver is initialized by calling NCR5380_init(instance),
Roman Zippelc28bda22007-05-01 22:32:36 +0200173 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
Finn Thain8c325132014-11-12 16:11:58 +1100175 * possible) function may be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define HOSTNO instance->host_no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Finn Thain636b1ec2016-01-03 16:05:10 +1100180static int do_abort(struct Scsi_Host *);
181static void do_reset(struct Scsi_Host *);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#ifdef SUPPORT_TAGS
184
185/*
186 * Functions for handling tagged queuing
187 * =====================================
188 *
189 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
190 *
191 * Using consecutive numbers for the tags is no good idea in my eyes. There
192 * could be wrong re-usings if the counter (8 bit!) wraps and some early
193 * command has been preempted for a long time. My solution: a bitfield for
194 * remembering used tags.
195 *
196 * There's also the problem that each target has a certain queue size, but we
197 * cannot know it in advance :-( We just see a QUEUE_FULL status being
198 * returned. So, in this case, the driver internal queue size assumption is
199 * reduced to the number of active tags if QUEUE_FULL is returned by the
Finn Thaine42d0152016-01-03 16:05:49 +1100200 * target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
202 * We're also not allowed running tagged commands as long as an untagged
203 * command is active. And REQUEST SENSE commands after a contingent allegiance
204 * condition _must_ be untagged. To keep track whether an untagged command has
205 * been issued, the host->busy array is still employed, as it is without
206 * support for tagged queuing.
207 *
208 * One could suspect that there are possible race conditions between
209 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
210 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
211 * which already guaranteed to be running at most once. It is also the only
212 * place where tags/LUNs are allocated. So no other allocation can slip
213 * between that pair, there could only happen a reselection, which can free a
214 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
215 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
216 */
217
Finn Thainca513fc2014-11-12 16:12:19 +1100218static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Roman Zippelc28bda22007-05-01 22:32:36 +0200220 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100221 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200222
Finn Thainca513fc2014-11-12 16:12:19 +1100223 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200224 return;
225
226 for (target = 0; target < 8; ++target) {
227 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100228 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200229 bitmap_zero(ta->allocated, MAX_TAGS);
230 ta->nr_allocated = 0;
231 /* At the beginning, assume the maximum queue size we could
232 * support (MAX_TAGS). This value will be decreased if the target
233 * returns QUEUE_FULL status.
234 */
235 ta->queue_size = MAX_TAGS;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240
241/* Check if we can issue a command to this LUN: First see if the LUN is marked
242 * busy by an untagged command. If the command should use tagged queuing, also
243 * check that there is a free tag and the target's queue won't overflow. This
244 * function should be called with interrupts disabled to avoid race
245 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Finn Thain710ddd02014-11-12 16:12:02 +1100248static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200250 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100251 struct Scsi_Host *instance = cmd->device->host;
252 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200254 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200255 return 1;
256 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100257 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
258 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200259 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100260 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
261 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100262 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d: no free tags\n",
263 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200264 return 1;
265 }
266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
271 * must be called before!), or reserve the LUN in 'busy' if the command is
272 * untagged.
273 */
274
Finn Thain710ddd02014-11-12 16:12:02 +1100275static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200277 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100278 struct Scsi_Host *instance = cmd->device->host;
279 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Roman Zippelc28bda22007-05-01 22:32:36 +0200281 /* If we or the target don't support tagged queuing, allocate the LUN for
282 * an untagged command.
283 */
284 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100285 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
286 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200287 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200288 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100289 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d now allocated by untagged command\n",
290 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200291 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100292 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Roman Zippelc28bda22007-05-01 22:32:36 +0200294 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
295 set_bit(cmd->tag, ta->allocated);
296 ta->nr_allocated++;
Finn Thaindbb6b352016-01-03 16:05:53 +1100297 dsprintk(NDEBUG_TAGS, instance, "using tag %d for target %d lun %d (%d tags allocated)\n",
298 cmd->tag, scmd_id(cmd), lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302
303/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
304 * unlock the LUN.
305 */
306
Finn Thain710ddd02014-11-12 16:12:02 +1100307static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200309 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100310 struct Scsi_Host *instance = cmd->device->host;
311 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Roman Zippelc28bda22007-05-01 22:32:36 +0200313 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200314 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100315 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d untagged cmd freed\n",
316 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200317 } else if (cmd->tag >= MAX_TAGS) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100318 shost_printk(KERN_NOTICE, instance,
319 "trying to free bad tag %d!\n", cmd->tag);
Roman Zippelc28bda22007-05-01 22:32:36 +0200320 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100321 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 clear_bit(cmd->tag, ta->allocated);
323 ta->nr_allocated--;
Finn Thaindbb6b352016-01-03 16:05:53 +1100324 dsprintk(NDEBUG_TAGS, instance, "freed tag %d for target %d lun %d\n",
325 cmd->tag, scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329
Finn Thainca513fc2014-11-12 16:12:19 +1100330static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Roman Zippelc28bda22007-05-01 22:32:36 +0200332 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100333 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Finn Thainca513fc2014-11-12 16:12:19 +1100335 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200336 return;
337
338 for (target = 0; target < 8; ++target) {
339 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100340 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200341 bitmap_zero(ta->allocated, MAX_TAGS);
342 ta->nr_allocated = 0;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347#endif /* SUPPORT_TAGS */
348
349
350/*
Finn Thain710ddd02014-11-12 16:12:02 +1100351 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
353 * Purpose: Try to merge several scatter-gather requests into one DMA
354 * transfer. This is possible if the scatter buffers lie on
355 * physical contiguous addresses.
356 *
Finn Thain710ddd02014-11-12 16:12:02 +1100357 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300359 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
361
Finn Thain710ddd02014-11-12 16:12:02 +1100362static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Finn Thaine3f463b2014-11-12 16:12:16 +1100364#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200365 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200367 unsigned long oldlen = cmd->SCp.this_residual;
368 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
370
Roman Zippelc28bda22007-05-01 22:32:36 +0200371 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
372 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200373 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100374 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200375 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200377 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200379 ++cmd->SCp.buffer;
380 --cmd->SCp.buffers_residual;
381 cmd->SCp.this_residual += cmd->SCp.buffer->length;
382 endaddr += cmd->SCp.buffer->length;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200385 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100386 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200387 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100389#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Finn Thainff50f9e2014-11-12 16:12:18 +1100392/**
393 * initialize_SCp - init the scsi pointer field
394 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100396 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
398
Finn Thain710ddd02014-11-12 16:12:02 +1100399static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Roman Zippelc28bda22007-05-01 22:32:36 +0200401 /*
402 * Initialize the Scsi Pointer field so that all of the commands in the
403 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200405
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200406 if (scsi_bufflen(cmd)) {
407 cmd->SCp.buffer = scsi_sglist(cmd);
408 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200409 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200410 cmd->SCp.this_residual = cmd->SCp.buffer->length;
411 /* ++roman: Try to merge some scatter-buffers if they are at
412 * contiguous physical addresses.
413 */
414 merge_contiguous_buffers(cmd);
415 } else {
416 cmd->SCp.buffer = NULL;
417 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200418 cmd->SCp.ptr = NULL;
419 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Finn Thain636b1ec2016-01-03 16:05:10 +1100423/**
Finn Thainb32ade12016-01-03 16:05:41 +1100424 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain636b1ec2016-01-03 16:05:10 +1100425 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100426 * @reg1: 5380 register to poll
427 * @bit1: Bitmask to check
428 * @val1: Expected value
429 * @reg2: Second 5380 register to poll
430 * @bit2: Second bitmask to check
431 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100432 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100433 *
Finn Thain2f854b82016-01-03 16:05:22 +1100434 * Polls the chip in a reasonably efficient manner waiting for an
435 * event to occur. After a short quick poll we begin to yield the CPU
436 * (if possible). In irq contexts the time-out is arbitrarily limited.
437 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100438 *
Finn Thainb32ade12016-01-03 16:05:41 +1100439 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100440 */
441
Finn Thainb32ade12016-01-03 16:05:41 +1100442static int NCR5380_poll_politely2(struct Scsi_Host *instance,
443 int reg1, int bit1, int val1,
444 int reg2, int bit2, int val2, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100445{
Finn Thain2f854b82016-01-03 16:05:22 +1100446 struct NCR5380_hostdata *hostdata = shost_priv(instance);
447 unsigned long deadline = jiffies + wait;
448 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100449
Finn Thain2f854b82016-01-03 16:05:22 +1100450 /* Busy-wait for up to 10 ms */
451 n = min(10000U, jiffies_to_usecs(wait));
452 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100453 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100454 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100455 if ((NCR5380_read(reg1) & bit1) == val1)
456 return 0;
457 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain636b1ec2016-01-03 16:05:10 +1100458 return 0;
459 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100460 } while (n--);
461
462 if (irqs_disabled() || in_interrupt())
463 return -ETIMEDOUT;
464
465 /* Repeatedly sleep for 1 ms until deadline */
466 while (time_is_after_jiffies(deadline)) {
467 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100468 if ((NCR5380_read(reg1) & bit1) == val1)
469 return 0;
470 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain2f854b82016-01-03 16:05:22 +1100471 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100472 }
473
Finn Thain636b1ec2016-01-03 16:05:10 +1100474 return -ETIMEDOUT;
475}
476
Finn Thainb32ade12016-01-03 16:05:41 +1100477static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
478 int reg, int bit, int val, int wait)
479{
480 return NCR5380_poll_politely2(instance, reg, bit, val,
481 reg, bit, val, wait);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#if NDEBUG
485static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200486 unsigned char mask;
487 const char *name;
488} signals[] = {
489 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
490 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
491 { SR_SEL, "SEL" }, {0, NULL}
492}, basrs[] = {
493 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
494}, icrs[] = {
495 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
496 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
497 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
498 {0, NULL}
499}, mrs[] = {
500 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
501 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
502 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
503 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
504 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
505 {0, NULL}
506};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Finn Thainff50f9e2014-11-12 16:12:18 +1100508/**
509 * NCR5380_print - print scsi bus signals
510 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100512 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
514
Roman Zippelc28bda22007-05-01 22:32:36 +0200515static void NCR5380_print(struct Scsi_Host *instance)
516{
517 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Roman Zippelc28bda22007-05-01 22:32:36 +0200519 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
520 status = NCR5380_read(STATUS_REG);
521 mr = NCR5380_read(MODE_REG);
522 icr = NCR5380_read(INITIATOR_COMMAND_REG);
523 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thain11d2f632016-01-03 16:05:51 +1100524
Roman Zippelc28bda22007-05-01 22:32:36 +0200525 printk("STATUS_REG: %02x ", status);
526 for (i = 0; signals[i].mask; ++i)
527 if (status & signals[i].mask)
528 printk(",%s", signals[i].name);
529 printk("\nBASR: %02x ", basr);
530 for (i = 0; basrs[i].mask; ++i)
531 if (basr & basrs[i].mask)
532 printk(",%s", basrs[i].name);
533 printk("\nICR: %02x ", icr);
534 for (i = 0; icrs[i].mask; ++i)
535 if (icr & icrs[i].mask)
536 printk(",%s", icrs[i].name);
537 printk("\nMODE: %02x ", mr);
538 for (i = 0; mrs[i].mask; ++i)
539 if (mr & mrs[i].mask)
540 printk(",%s", mrs[i].name);
541 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200545 unsigned char value;
546 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200548 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
549 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
550 {PHASE_UNKNOWN, "UNKNOWN"}
551};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Finn Thainff50f9e2014-11-12 16:12:18 +1100553/**
554 * NCR5380_print_phase - show SCSI phase
555 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100557 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100559 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561
562static void NCR5380_print_phase(struct Scsi_Host *instance)
563{
Roman Zippelc28bda22007-05-01 22:32:36 +0200564 unsigned char status;
565 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Roman Zippelc28bda22007-05-01 22:32:36 +0200567 status = NCR5380_read(STATUS_REG);
568 if (!(status & SR_REQ))
569 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
570 else {
571 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
572 (phases[i].value != (status & PHASE_MASK)); ++i)
573 ;
574 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579
Finn Thain8c325132014-11-12 16:11:58 +1100580/**
581 * NCR58380_info - report driver and host information
582 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
Finn Thain8c325132014-11-12 16:11:58 +1100584 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Finn Thain8c325132014-11-12 16:11:58 +1100586 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588
Finn Thain8c325132014-11-12 16:11:58 +1100589static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Finn Thain8c325132014-11-12 16:11:58 +1100591 struct NCR5380_hostdata *hostdata = shost_priv(instance);
592
593 return hostdata->info;
594}
595
596static void prepare_info(struct Scsi_Host *instance)
597{
598 struct NCR5380_hostdata *hostdata = shost_priv(instance);
599
600 snprintf(hostdata->info, sizeof(hostdata->info),
601 "%s, io_port 0x%lx, n_io_port %d, "
602 "base 0x%lx, irq %d, "
603 "can_queue %d, cmd_per_lun %d, "
604 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100605 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100606 "options { %s} ",
607 instance->hostt->name, instance->io_port, instance->n_io_port,
608 instance->base, instance->irq,
609 instance->can_queue, instance->cmd_per_lun,
610 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100611 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100612 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100613#ifdef DIFFERENTIAL
614 "DIFFERENTIAL "
615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100617 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#endif
619#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100620 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#endif
622#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100623 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624#endif
Finn Thain8c325132014-11-12 16:11:58 +1100625 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Finn Thainff50f9e2014-11-12 16:12:18 +1100628/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100629 * NCR5380_init - initialise an NCR5380
630 * @instance: adapter to configure
631 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100633 * Initializes *instance and corresponding 5380 chip,
634 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
636 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100637 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200638 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100639 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
641
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100642static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Finn Thaine8a60142016-01-03 16:05:54 +1100644 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +0200645 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100646 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Finn Thaina53a21e2014-11-12 16:12:21 +1100648 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200649 hostdata->id_mask = 1 << instance->this_id;
650 hostdata->id_higher_mask = 0;
651 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
652 if (i > hostdata->id_mask)
653 hostdata->id_higher_mask |= i;
654 for (i = 0; i < 8; ++i)
655 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100657 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#endif
659#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200660 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100662 spin_lock_init(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200663 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +1100664 INIT_LIST_HEAD(&hostdata->unissued);
665 INIT_LIST_HEAD(&hostdata->disconnected);
666
Finn Thainef1081c2014-11-12 16:12:14 +1100667 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Finn Thaina53a21e2014-11-12 16:12:21 +1100669 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100670 hostdata->work_q = alloc_workqueue("ncr5380_%d",
671 WQ_UNBOUND | WQ_MEM_RECLAIM,
672 1, instance->host_no);
673 if (!hostdata->work_q)
674 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Finn Thain8c325132014-11-12 16:11:58 +1100676 prepare_info(instance);
677
Roman Zippelc28bda22007-05-01 22:32:36 +0200678 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
679 NCR5380_write(MODE_REG, MR_BASE);
680 NCR5380_write(TARGET_COMMAND_REG, 0);
681 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Finn Thain2f854b82016-01-03 16:05:22 +1100683 /* Calibrate register polling loop */
684 i = 0;
685 deadline = jiffies + 1;
686 do {
687 cpu_relax();
688 } while (time_is_after_jiffies(deadline));
689 deadline += msecs_to_jiffies(256);
690 do {
691 NCR5380_read(STATUS_REG);
692 ++i;
693 cpu_relax();
694 } while (time_is_after_jiffies(deadline));
695 hostdata->accesses_per_ms = i / 256;
696
Roman Zippelc28bda22007-05-01 22:32:36 +0200697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Finn Thainff50f9e2014-11-12 16:12:18 +1100700/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100701 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
702 * @instance: adapter to check
703 *
704 * If the system crashed, it may have crashed with a connected target and
705 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
706 * currently established nexus, which we know nothing about. Failing that
707 * do a bus reset.
708 *
709 * Note that a bus reset will cause the chip to assert IRQ.
710 *
711 * Returns 0 if successful, otherwise -ENXIO.
712 */
713
714static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
715{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100716 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100717 int pass;
718
719 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
720 switch (pass) {
721 case 1:
722 case 3:
723 case 5:
724 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
725 NCR5380_poll_politely(instance,
726 STATUS_REG, SR_BSY, 0, 5 * HZ);
727 break;
728 case 2:
729 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
730 do_abort(instance);
731 break;
732 case 4:
733 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
734 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100735 /* Wait after a reset; the SCSI standard calls for
736 * 250ms, we wait 500ms to be on the safe side.
737 * But some Toshiba CD-ROMs need ten times that.
738 */
739 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
740 msleep(2500);
741 else
742 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100743 break;
744 case 6:
745 shost_printk(KERN_ERR, instance, "bus locked solid\n");
746 return -ENXIO;
747 }
748 }
749 return 0;
750}
751
752/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100753 * NCR5380_exit - remove an NCR5380
754 * @instance: adapter to remove
755 *
756 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
757 */
758
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200759static void NCR5380_exit(struct Scsi_Host *instance)
760{
Finn Thaina53a21e2014-11-12 16:12:21 +1100761 struct NCR5380_hostdata *hostdata = shost_priv(instance);
762
763 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100764 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200765}
766
Finn Thainff50f9e2014-11-12 16:12:18 +1100767/**
768 * NCR5380_queue_command - queue a command
769 * @instance: the relevant SCSI adapter
770 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *
Finn Thain1bb40582016-01-03 16:05:29 +1100772 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100773 * twiddling done to the host specific fields of cmd. If the
774 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
776
Finn Thain16b29e72014-11-12 16:12:08 +1100777static int NCR5380_queue_command(struct Scsi_Host *instance,
778 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Finn Thain16b29e72014-11-12 16:12:08 +1100780 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100781 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200782 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200785 switch (cmd->cmnd[0]) {
786 case WRITE_6:
787 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100788 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200789 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100790 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200791 return 0;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
794
Roman Zippelc28bda22007-05-01 22:32:36 +0200795 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Roman Zippelc28bda22007-05-01 22:32:36 +0200797 /*
798 * Insert the cmd into the issue queue. Note that REQUEST SENSE
799 * commands are added to the head of the queue since any command will
800 * clear the contingent allegiance condition that exists and the
801 * sense data is only guaranteed to be valid while the condition exists.
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Roman Zippelc28bda22007-05-01 22:32:36 +0200804 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
805 * Otherwise a running NCR5380_main may steal the lock.
806 * Lock before actually inserting due to fairness reasons explained in
807 * atari_scsi.c. If we insert first, then it's impossible for this driver
808 * to release the lock.
809 * Stop timer for this command while waiting for the lock, or timeouts
810 * may happen (and they really do), and it's no good if the command doesn't
811 * appear in any of the queues.
812 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
813 * because also a timer int can trigger an abort or reset, which would
814 * alter queues and touch the lock.
815 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100816 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100817 return SCSI_MLQUEUE_HOST_BUSY;
818
Finn Thain11d2f632016-01-03 16:05:51 +1100819 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain16b29e72014-11-12 16:12:08 +1100820
Finn Thainff50f9e2014-11-12 16:12:18 +1100821 /*
822 * Insert the cmd into the issue queue. Note that REQUEST SENSE
823 * commands are added to the head of the queue since any command will
824 * clear the contingent allegiance condition that exists and the
825 * sense data is only guaranteed to be valid while the condition exists.
826 */
827
Finn Thain32b26a12016-01-03 16:05:58 +1100828 if (cmd->cmnd[0] == REQUEST_SENSE)
829 list_add(&ncmd->list, &hostdata->unissued);
830 else
831 list_add_tail(&ncmd->list, &hostdata->unissued);
832
Finn Thain11d2f632016-01-03 16:05:51 +1100833 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Finn Thaindbb6b352016-01-03 16:05:53 +1100835 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
836 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Finn Thain010e89d2016-01-03 16:05:38 +1100838 /* Kick off command processing */
839 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Finn Thaine3c3da62014-11-12 16:12:15 +1100843static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
844{
845 struct NCR5380_hostdata *hostdata = shost_priv(instance);
846
847 /* Caller does the locking needed to set & test these data atomically */
Finn Thain32b26a12016-01-03 16:05:58 +1100848 if (list_empty(&hostdata->disconnected) &&
849 list_empty(&hostdata->unissued) &&
Finn Thaine3c3da62014-11-12 16:12:15 +1100850 !hostdata->connected &&
851 !hostdata->retain_dma_intr)
852 NCR5380_release_dma_irq(instance);
853}
854
Finn Thainff50f9e2014-11-12 16:12:18 +1100855/**
856 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100858 * NCR5380_main is a coroutine that runs as long as more work can
859 * be done on the NCR5380 host adapters in a system. Both
860 * NCR5380_queue_command() and NCR5380_intr() will try to start it
861 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200862 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100863 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200864 */
865
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200866static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Finn Thaina53a21e2014-11-12 16:12:21 +1100868 struct NCR5380_hostdata *hostdata =
869 container_of(work, struct NCR5380_hostdata, main_task);
870 struct Scsi_Host *instance = hostdata->host;
Finn Thain32b26a12016-01-03 16:05:58 +1100871 struct NCR5380_cmd *ncmd, *n;
Roman Zippelc28bda22007-05-01 22:32:36 +0200872 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Roman Zippelc28bda22007-05-01 22:32:36 +0200874 /*
Roman Zippelc28bda22007-05-01 22:32:36 +0200875 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
876 * because also a timer int can trigger an abort or reset, which can
877 * alter queues and touch the Falcon lock.
878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Finn Thain11d2f632016-01-03 16:05:51 +1100880 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200881 do {
Roman Zippelc28bda22007-05-01 22:32:36 +0200882 done = 1;
883
884 if (!hostdata->connected) {
Finn Thaind65e6342014-03-18 11:42:20 +1100885 dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200886 /*
887 * Search through the issue_queue for a command destined
888 * for a target that's not busy.
889 */
Finn Thain32b26a12016-01-03 16:05:58 +1100890 list_for_each_entry_safe(ncmd, n, &hostdata->unissued,
891 list) {
892 struct scsi_cmnd *tmp = NCR5380_to_scmd(ncmd);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200893 u8 lun = tmp->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Finn Thain0d3d9a42016-01-03 16:05:55 +1100895 dsprintk(NDEBUG_QUEUES, instance, "main: tmp=%p target=%d busy=%d lun=%d\n",
896 tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)], lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200897 /* When we find one, remove it from the issue queue. */
Roman Zippelc28bda22007-05-01 22:32:36 +0200898 if (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +0200900 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#else
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200902 !(hostdata->busy[tmp->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200903#endif
904 ) {
Finn Thain32b26a12016-01-03 16:05:58 +1100905 list_del(&ncmd->list);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100906 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES,
Finn Thain32b26a12016-01-03 16:05:58 +1100907 instance, "main: removed %p from issue queue\n",
908 tmp);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100909
Finn Thainef1081c2014-11-12 16:12:14 +1100910 hostdata->retain_dma_intr++;
Roman Zippelc28bda22007-05-01 22:32:36 +0200911
Roman Zippelc28bda22007-05-01 22:32:36 +0200912 /*
913 * Attempt to establish an I_T_L nexus here.
914 * On success, instance->hostdata->connected is set.
915 * On failure, we must add the command back to the
916 * issue queue so we can keep trying.
917 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200918 /*
919 * REQUEST SENSE commands are issued without tagged
920 * queueing, even on SCSI-II devices because the
921 * contingent allegiance condition exists for the
922 * entire unit.
923 */
924 /* ++roman: ...and the standard also requires that
925 * REQUEST SENSE command are untagged.
926 */
927
928#ifdef SUPPORT_TAGS
929 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
930#endif
Finn Thain76f13b92014-11-12 16:11:53 +1100931 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100932 /* OK or bad target */
Finn Thainef1081c2014-11-12 16:12:14 +1100933 hostdata->retain_dma_intr--;
Finn Thaine3c3da62014-11-12 16:12:15 +1100934 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +0200935 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100936 /* Need to retry */
Finn Thain32b26a12016-01-03 16:05:58 +1100937 list_add(&ncmd->list, &hostdata->unissued);
Finn Thain0d3d9a42016-01-03 16:05:55 +1100938 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES,
939 instance, "main: select() failed, %p returned to issue queue\n",
940 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +0200941#ifdef SUPPORT_TAGS
942 cmd_free_tag(tmp);
943#endif
Finn Thainef1081c2014-11-12 16:12:14 +1100944 hostdata->retain_dma_intr--;
Finn Thain1d3db592016-01-03 16:05:24 +1100945 done = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200946 }
Finn Thain1f1b0c72016-01-03 16:05:17 +1100947 if (hostdata->connected)
948 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200949 } /* if target/lun/target queue is not busy */
950 } /* for issue_queue */
951 } /* if (!hostdata->connected) */
952
953 if (hostdata->connected
954#ifdef REAL_DMA
955 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956#endif
957 ) {
Finn Thaind65e6342014-03-18 11:42:20 +1100958 dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200959 HOSTNO);
960 NCR5380_information_transfer(instance);
Finn Thaind65e6342014-03-18 11:42:20 +1100961 dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200962 done = 0;
963 }
964 } while (!done);
Finn Thain11d2f632016-01-03 16:05:51 +1100965 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968
969#ifdef REAL_DMA
970/*
971 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
972 *
973 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +0200974 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *
976 * Inputs : instance - this instance of the NCR5380.
977 *
978 */
979
Roman Zippelc28bda22007-05-01 22:32:36 +0200980static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Finn Thaine8a60142016-01-03 16:05:54 +1100982 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain01bd9082014-11-12 16:12:23 +1100983 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +1100984 unsigned char **data;
Finn Thain5299b3c2016-01-03 16:05:57 +1100985 int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +1100986 int saved_data = 0, overrun = 0;
987 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Finn Thainef1081c2014-11-12 16:12:14 +1100989 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200990 p = hostdata->connected->SCp.phase;
991 if (p & SR_IO) {
992 udelay(10);
993 if ((NCR5380_read(BUS_AND_STATUS_REG) &
994 (BASR_PHASE_MATCH|BASR_ACK)) ==
995 (BASR_PHASE_MATCH|BASR_ACK)) {
996 saved_data = NCR5380_read(INPUT_DATA_REG);
997 overrun = 1;
Finn Thaind65e6342014-03-18 11:42:20 +1100998 dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +0200999 }
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001002
Finn Thaine3f463b2014-11-12 16:12:16 +11001003#if defined(CONFIG_SUN3)
1004 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1005 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1006 instance->host_no);
1007 BUG();
1008 }
1009
1010 /* make sure we're not stuck in a data phase */
1011 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1012 (BASR_PHASE_MATCH | BASR_ACK)) {
1013 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1014 NCR5380_read(BUS_AND_STATUS_REG));
1015 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1016 instance->host_no);
1017 BUG();
1018 }
1019#endif
1020
Roman Zippelc28bda22007-05-01 22:32:36 +02001021 NCR5380_write(MODE_REG, MR_BASE);
1022 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001023 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001024
Finn Thain01bd9082014-11-12 16:12:23 +11001025 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001026 hostdata->dma_len = 0;
1027
1028 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1029 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001030 *data += transferred;
1031 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001032
Finn Thainef1081c2014-11-12 16:12:14 +11001033 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001034 int cnt, toPIO;
1035
Roman Zippelc28bda22007-05-01 22:32:36 +02001036 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001037 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001038 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001039 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001040 *(*data)++ = saved_data;
1041 (*count)--;
1042 cnt--;
1043 toPIO--;
1044 }
Finn Thaind65e6342014-03-18 11:42:20 +11001045 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001046 NCR5380_transfer_pio(instance, &p, &cnt, data);
1047 *count -= toPIO - cnt;
1048 }
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051#endif /* REAL_DMA */
1052
1053
Finn Thainff50f9e2014-11-12 16:12:18 +11001054/**
1055 * NCR5380_intr - generic NCR5380 irq handler
1056 * @irq: interrupt number
1057 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001058 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001059 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1060 * from the disconnected queue, and restarting NCR5380_main()
1061 * as required.
Finn Thaincd400822016-01-03 16:05:40 +11001062 *
1063 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1064 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1065 * Three of these six conditions are latched in the Bus and Status Register:
1066 * - End of DMA (cleared by ending DMA Mode)
1067 * - Parity error (cleared by reading RPIR)
1068 * - Loss of BSY (cleared by reading RPIR)
1069 * Two conditions have flag bits that are not latched:
1070 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1071 * - Bus reset (non-maskable)
1072 * The remaining condition has no flag bit at all:
1073 * - Selection/reselection
1074 *
1075 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1076 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1077 * claimed that "the design of the [DP8490] interrupt logic ensures
1078 * interrupts will not be lost (they can be on the DP5380)."
1079 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1080 *
1081 * Checking for bus reset by reading RST is futile because of interrupt
1082 * latency, but a bus reset will reset chip logic. Checking for parity error
1083 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1084 * condition will clear DMA Mode. We can tell when this occurs because the
1085 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087
Roman Zippelc28bda22007-05-01 22:32:36 +02001088static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Finn Thaina53a21e2014-11-12 16:12:21 +11001090 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001091 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001092 int handled = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001093 unsigned char basr;
Finn Thain11d2f632016-01-03 16:05:51 +11001094 unsigned long flags;
1095
1096 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Roman Zippelc28bda22007-05-01 22:32:36 +02001098 basr = NCR5380_read(BUS_AND_STATUS_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001099 if (basr & BASR_IRQ) {
Finn Thaincd400822016-01-03 16:05:40 +11001100 unsigned char mr = NCR5380_read(MODE_REG);
1101 unsigned char sr = NCR5380_read(STATUS_REG);
1102
1103 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1104 HOSTNO, irq, basr, sr, mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001107 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1108 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1109 * We ack IRQ after clearing Mode Register. Workarounds
1110 * for End of DMA errata need to happen in DMA Mode.
Roman Zippelc28bda22007-05-01 22:32:36 +02001111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Finn Thaincd400822016-01-03 16:05:40 +11001113 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001114
Finn Thaincd400822016-01-03 16:05:40 +11001115 if (hostdata->connected) {
1116 NCR5380_dma_complete(instance);
1117 queue_work(hostdata->work_q, &hostdata->main_task);
1118 } else {
1119 NCR5380_write(MODE_REG, MR_BASE);
1120 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001121 }
Finn Thaincd400822016-01-03 16:05:40 +11001122 } else
1123#endif /* REAL_DMA */
1124 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1125 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1126 /* Probably reselected */
1127 NCR5380_write(SELECT_ENABLE_REG, 0);
1128 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1129
1130 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1131 HOSTNO);
1132
1133 if (!hostdata->connected) {
1134 NCR5380_reselect(instance);
1135 queue_work(hostdata->work_q, &hostdata->main_task);
1136 }
1137 if (!hostdata->connected)
1138 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1139 } else {
1140 /* Probably Bus Reset */
1141 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1142
1143 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", HOSTNO);
1144#ifdef SUN3_SCSI_VME
1145 dregs->csr |= CSR_DMA_ENABLE;
1146#endif
1147 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001148 handled = 1;
Finn Thaincd400822016-01-03 16:05:40 +11001149 } else {
1150 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine3f463b2014-11-12 16:12:16 +11001151#ifdef SUN3_SCSI_VME
1152 dregs->csr |= CSR_DMA_ENABLE;
1153#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001154 }
1155
Finn Thain11d2f632016-01-03 16:05:51 +11001156 spin_unlock_irqrestore(&hostdata->lock, flags);
1157
Roman Zippelc28bda22007-05-01 22:32:36 +02001158 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Roman Zippelc28bda22007-05-01 22:32:36 +02001161/*
Finn Thain710ddd02014-11-12 16:12:02 +11001162 * Function : int NCR5380_select(struct Scsi_Host *instance,
1163 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 *
1165 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001166 * including ARBITRATION, SELECTION, and initial message out for
1167 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001169 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001170 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *
Finn Thain63238762016-01-03 16:05:15 +11001172 * Returns : -1 if selection failed but should be retried.
1173 * 0 if selection failed and should not be retried.
1174 * 0 if selection succeeded completely (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001175 *
1176 * Side effects :
1177 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 * with registers as they should have been on entry - ie
1179 * SELECT_ENABLE will be set appropriately, the NCR5380
1180 * will cease to drive any SCSI bus signals.
1181 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001182 * If successful : I_T_L or I_T_L_Q nexus will be established,
1183 * instance->connected will be set to cmd.
1184 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001186 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * cmd->result host byte set to DID_BAD_TARGET.
1188 */
1189
Finn Thain710ddd02014-11-12 16:12:02 +11001190static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Finn Thaine8a60142016-01-03 16:05:54 +11001192 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001193 unsigned char tmp[3], phase;
1194 unsigned char *data;
1195 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001196 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Finn Thain8ad3a592014-03-18 11:42:19 +11001198 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thaind65e6342014-03-18 11:42:20 +11001199 dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02001200 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Roman Zippelc28bda22007-05-01 22:32:36 +02001202 /*
1203 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1204 * data bus during SELECTION.
1205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Roman Zippelc28bda22007-05-01 22:32:36 +02001207 NCR5380_write(TARGET_COMMAND_REG, 0);
1208
1209 /*
1210 * Start arbitration.
1211 */
1212
1213 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1214 NCR5380_write(MODE_REG, MR_ARBITRATE);
1215
Finn Thain55500d92016-01-03 16:05:35 +11001216 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1217 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Finn Thain11d2f632016-01-03 16:05:51 +11001220 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001221 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1222 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1223 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001224 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001225 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1226 /* Reselection interrupt */
1227 return -1;
1228 }
1229 if (err < 0) {
1230 NCR5380_write(MODE_REG, MR_BASE);
1231 shost_printk(KERN_ERR, instance,
1232 "select: arbitration timeout\n");
1233 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001234 }
Finn Thain11d2f632016-01-03 16:05:51 +11001235 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001236
1237 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001238 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Roman Zippelc28bda22007-05-01 22:32:36 +02001240 /* Check for lost arbitration */
1241 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1242 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
Finn Thain11d2f632016-01-03 16:05:51 +11001243 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001244 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaind65e6342014-03-18 11:42:20 +11001245 dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001246 HOSTNO);
Finn Thain11d2f632016-01-03 16:05:51 +11001247 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001248 return -1;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Finn Thaincf13b082016-01-03 16:05:18 +11001251 /* After/during arbitration, BSY should be asserted.
1252 * IBM DPES-31080 Version S31Q works now
1253 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1254 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001255 NCR5380_write(INITIATOR_COMMAND_REG,
1256 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Roman Zippelc28bda22007-05-01 22:32:36 +02001258 /*
1259 * Again, bus clear + bus settle time is 1.2us, however, this is
1260 * a minimum so we'll udelay ceil(1.2)
1261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Finn Thain9c3f0e22016-01-03 16:05:11 +11001263 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1264 udelay(15);
1265 else
1266 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001267
Finn Thain11d2f632016-01-03 16:05:51 +11001268 spin_lock_irq(&hostdata->lock);
1269
Finn Thain72064a72016-01-03 16:05:44 +11001270 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1271 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Roman Zippelc28bda22007-05-01 22:32:36 +02001272 return -1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001273
Finn Thaind65e6342014-03-18 11:42:20 +11001274 dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001275
1276 /*
1277 * Now that we have won arbitration, start Selection process, asserting
1278 * the host and target ID's on the SCSI bus.
1279 */
1280
1281 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1282
1283 /*
1284 * Raise ATN while SEL is true before BSY goes false from arbitration,
1285 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1286 * phase immediately after selection.
1287 */
1288
1289 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1290 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Roman Zippelc28bda22007-05-01 22:32:36 +02001293 /*
1294 * Reselect interrupts must be turned off prior to the dropping of BSY,
1295 * otherwise we will trigger an interrupt.
1296 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001297 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Finn Thain11d2f632016-01-03 16:05:51 +11001299 spin_unlock_irq(&hostdata->lock);
1300
Roman Zippelc28bda22007-05-01 22:32:36 +02001301 /*
1302 * The initiator shall then wait at least two deskew delays and release
1303 * the BSY signal.
1304 */
1305 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Roman Zippelc28bda22007-05-01 22:32:36 +02001307 /* Reset BSY */
1308 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1309 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Roman Zippelc28bda22007-05-01 22:32:36 +02001311 /*
1312 * Something weird happens when we cease to drive BSY - looks
1313 * like the board/chip is letting us do another read before the
1314 * appropriate propagation delay has expired, and we're confusing
1315 * a BSY signal from ourselves as the target's response to SELECTION.
1316 *
1317 * A small delay (the 'C++' frontend breaks the pipeline with an
1318 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1319 * tighter 'C' code breaks and requires this) solves the problem -
1320 * the 1 us delay is arbitrary, and only used because this delay will
1321 * be the same on other platforms and since it works here, it should
1322 * work there.
1323 *
1324 * wingel suggests that this could be due to failing to wait
1325 * one deskew delay.
1326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Roman Zippelc28bda22007-05-01 22:32:36 +02001328 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Finn Thaind65e6342014-03-18 11:42:20 +11001330 dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Roman Zippelc28bda22007-05-01 22:32:36 +02001332 /*
1333 * The SCSI specification calls for a 250 ms timeout for the actual
1334 * selection.
1335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Finn Thainae753a32016-01-03 16:05:23 +11001337 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1338 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Roman Zippelc28bda22007-05-01 22:32:36 +02001340 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001341 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001342 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1343 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001344 if (!hostdata->connected)
1345 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02001346 printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
1347 HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001348 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001350
Finn Thainae753a32016-01-03 16:05:23 +11001351 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001352 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001353 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001354 cmd->result = DID_BAD_TARGET << 16;
Roman Zippelc28bda22007-05-01 22:32:36 +02001355#ifdef SUPPORT_TAGS
1356 cmd_free_tag(cmd);
1357#endif
1358 cmd->scsi_done(cmd);
1359 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaind65e6342014-03-18 11:42:20 +11001360 dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001361 return 0;
1362 }
1363
Roman Zippelc28bda22007-05-01 22:32:36 +02001364 /*
Finn Thainae753a32016-01-03 16:05:23 +11001365 * No less than two deskew delays after the initiator detects the
1366 * BSY signal is true, it shall release the SEL signal and may
1367 * change the DATA BUS. -wingel
1368 */
1369
1370 udelay(1);
1371
1372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1373
1374 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001375 * Since we followed the SCSI spec, and raised ATN while SEL
1376 * was true but before BSY was false during selection, the information
1377 * transfer phase should be a MESSAGE OUT phase so that we can send the
1378 * IDENTIFY message.
1379 *
1380 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1381 * message (2 bytes) with a tag ID that we increment with every command
1382 * until it wraps back to 0.
1383 *
1384 * XXX - it turns out that there are some broken SCSI-II devices,
1385 * which claim to support tagged queuing but fail when more than
1386 * some number of commands are issued at once.
1387 */
1388
1389 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001390
1391 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001392 spin_lock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001393 if (err < 0) {
1394 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1395 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1396 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1397 return -1;
1398 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001399
Finn Thaind65e6342014-03-18 11:42:20 +11001400 dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001401 HOSTNO, cmd->device->id);
1402 tmp[0] = IDENTIFY(1, cmd->device->lun);
1403
1404#ifdef SUPPORT_TAGS
1405 if (cmd->tag != TAG_NONE) {
1406 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1407 tmp[2] = cmd->tag;
1408 len = 3;
1409 } else
1410 len = 1;
1411#else
1412 len = 1;
1413 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#endif /* SUPPORT_TAGS */
1415
Roman Zippelc28bda22007-05-01 22:32:36 +02001416 /* Send message(s) */
1417 data = tmp;
1418 phase = PHASE_MSGOUT;
1419 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11001420 dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001421 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001422
Roman Zippelc28bda22007-05-01 22:32:36 +02001423 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001425 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1426#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001427#ifdef SUN3_SCSI_VME
1428 dregs->csr |= CSR_INTR;
1429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Roman Zippelc28bda22007-05-01 22:32:36 +02001431 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Roman Zippelc28bda22007-05-01 22:32:36 +02001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Roman Zippelc28bda22007-05-01 22:32:36 +02001436/*
1437 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * unsigned char *phase, int *count, unsigned char **data)
1439 *
1440 * Purpose : transfers data in given phase using polled I/O
1441 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001442 * Inputs : instance - instance of driver, *phase - pointer to
1443 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001447 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * is in same phase.
1449 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001450 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *
1452 * XXX Note : handling for bus free may be useful.
1453 */
1454
1455/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001456 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 * IS 100% reliable, and for the actual data transfer where speed
1458 * counts, we will always do a pseudo DMA or DMA transfer.
1459 */
1460
Roman Zippelc28bda22007-05-01 22:32:36 +02001461static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1462 unsigned char *phase, int *count,
1463 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Roman Zippelc28bda22007-05-01 22:32:36 +02001465 register unsigned char p = *phase, tmp;
1466 register int c = *count;
1467 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Roman Zippelc28bda22007-05-01 22:32:36 +02001469 /*
1470 * The NCR5380 chip will only drive the SCSI bus when the
1471 * phase specified in the appropriate bits of the TARGET COMMAND
1472 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474
Roman Zippelc28bda22007-05-01 22:32:36 +02001475 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Roman Zippelc28bda22007-05-01 22:32:36 +02001477 do {
1478 /*
1479 * Wait for assertion of REQ, after which the phase bits will be
1480 * valid
1481 */
Finn Thain686f3992016-01-03 16:05:26 +11001482
1483 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Finn Thaind65e6342014-03-18 11:42:20 +11001486 dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Roman Zippelc28bda22007-05-01 22:32:36 +02001488 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001489 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thaind65e6342014-03-18 11:42:20 +11001490 dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11001491 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001492 break;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Roman Zippelc28bda22007-05-01 22:32:36 +02001495 /* Do actual transfer from SCSI bus to / from memory */
1496 if (!(p & SR_IO))
1497 NCR5380_write(OUTPUT_DATA_REG, *d);
1498 else
1499 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Roman Zippelc28bda22007-05-01 22:32:36 +02001501 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Roman Zippelc28bda22007-05-01 22:32:36 +02001503 /*
1504 * The SCSI standard suggests that in MSGOUT phase, the initiator
1505 * should drop ATN on the last byte of the message phase
1506 * after REQ has been asserted for the handshake but before
1507 * the initiator raises ACK.
1508 */
1509
1510 if (!(p & SR_IO)) {
1511 if (!((p & SR_MSG) && c > 1)) {
1512 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001513 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1515 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1516 } else {
1517 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1518 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001519 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001520 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1521 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1522 }
1523 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001524 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001525 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1526 }
1527
Finn Thaina2edc4a2016-01-03 16:05:27 +11001528 if (NCR5380_poll_politely(instance,
1529 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1530 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001531
Finn Thaind65e6342014-03-18 11:42:20 +11001532 dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02001533
1534 /*
1535 * We have several special cases to consider during REQ/ACK handshaking :
1536 * 1. We were in MSGOUT phase, and we are on the last byte of the
1537 * message. ATN must be dropped as ACK is dropped.
1538 *
1539 * 2. We are in a MSGIN phase, and we are on the last byte of the
1540 * message. We must exit with ACK asserted, so that the calling
1541 * code may raise ATN before dropping ACK to reject the message.
1542 *
1543 * 3. ACK and ATN are clear and the target may proceed as normal.
1544 */
1545 if (!(p == PHASE_MSGIN && c == 1)) {
1546 if (p == PHASE_MSGOUT && c > 1)
1547 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1548 else
1549 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1550 }
1551 } while (--c);
1552
Finn Thaind65e6342014-03-18 11:42:20 +11001553 dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001554
1555 *count = c;
1556 *data = d;
1557 tmp = NCR5380_read(STATUS_REG);
1558 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001559 * asserted or if ACK hasn't been released yet. The latter applies if
1560 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001561 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001562 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001563 *phase = tmp & PHASE_MASK;
1564 else
1565 *phase = PHASE_UNKNOWN;
1566
1567 if (!c || (*phase == p))
1568 return 0;
1569 else
1570 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Finn Thain636b1ec2016-01-03 16:05:10 +11001573/**
1574 * do_reset - issue a reset command
1575 * @instance: adapter to reset
1576 *
1577 * Issue a reset sequence to the NCR5380 and try and get the bus
1578 * back into sane shape.
1579 *
1580 * This clears the reset interrupt flag because there may be no handler for
1581 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1582 * been installed. And when in EH we may have released the ST DMA interrupt.
1583 */
1584
1585static void do_reset(struct Scsi_Host *instance)
1586{
1587 unsigned long flags;
1588
1589 local_irq_save(flags);
1590 NCR5380_write(TARGET_COMMAND_REG,
1591 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1592 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1593 udelay(50);
1594 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1595 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1596 local_irq_restore(flags);
1597}
1598
Finn Thain80d3eb62016-01-03 16:05:34 +11001599/**
1600 * do_abort - abort the currently established nexus by going to
1601 * MESSAGE OUT phase and sending an ABORT message.
1602 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001603 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001604 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
1606
Finn Thaina53a21e2014-11-12 16:12:21 +11001607static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Roman Zippelc28bda22007-05-01 22:32:36 +02001609 unsigned char tmp, *msgptr, phase;
1610 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001611 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Roman Zippelc28bda22007-05-01 22:32:36 +02001613 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Roman Zippelc28bda22007-05-01 22:32:36 +02001616 /*
1617 * Wait for the target to indicate a valid phase by asserting
1618 * REQ. Once this happens, we'll have either a MSGOUT phase
1619 * and can immediately send the ABORT message, or we'll have some
1620 * other phase and will have to source/sink data.
1621 *
1622 * We really don't care what value was on the bus or what value
1623 * the target sees, so we just handshake.
1624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Finn Thain80d3eb62016-01-03 16:05:34 +11001626 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1627 if (rc < 0)
1628 goto timeout;
1629
1630 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001631
1632 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1633
Finn Thain80d3eb62016-01-03 16:05:34 +11001634 if (tmp != PHASE_MSGOUT) {
1635 NCR5380_write(INITIATOR_COMMAND_REG,
1636 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1637 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1638 if (rc < 0)
1639 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001640 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1641 }
1642
1643 tmp = ABORT;
1644 msgptr = &tmp;
1645 len = 1;
1646 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001647 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001648
1649 /*
1650 * If we got here, and the command completed successfully,
1651 * we're about to go into bus free state.
1652 */
1653
1654 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001655
1656timeout:
1657 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1658 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001662/*
1663 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 * unsigned char *phase, int *count, unsigned char **data)
1665 *
1666 * Purpose : transfers data in given phase using either real
1667 * or pseudo DMA.
1668 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001669 * Inputs : instance - instance of driver, *phase - pointer to
1670 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001672 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001674 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * is in same phase.
1676 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001677 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 *
1679 */
1680
1681
Roman Zippelc28bda22007-05-01 22:32:36 +02001682static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1683 unsigned char *phase, int *count,
1684 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Finn Thaine8a60142016-01-03 16:05:54 +11001686 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001687 register int c = *count;
1688 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001689
1690#if defined(CONFIG_SUN3)
1691 /* sanity check */
1692 if (!sun3_dma_setup_done) {
1693 pr_err("scsi%d: transfer_dma without setup!\n",
1694 instance->host_no);
1695 BUG();
1696 }
1697 hostdata->dma_len = c;
1698
1699 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1700 instance->host_no, (p & SR_IO) ? "reading" : "writing",
1701 c, (p & SR_IO) ? "to" : "from", *data);
1702
1703 /* netbsd turns off ints here, why not be safe and do it too */
Finn Thaine3f463b2014-11-12 16:12:16 +11001704
1705 /* send start chain */
1706 sun3scsi_dma_start(c, *data);
1707
Finn Thaincd400822016-01-03 16:05:40 +11001708 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1709 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1710 MR_ENABLE_EOP_INTR);
Finn Thaine3f463b2014-11-12 16:12:16 +11001711 if (p & SR_IO) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001712 NCR5380_write(INITIATOR_COMMAND_REG, 0);
Finn Thaine3f463b2014-11-12 16:12:16 +11001713 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1714 } else {
Finn Thaine3f463b2014-11-12 16:12:16 +11001715 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
Finn Thaine3f463b2014-11-12 16:12:16 +11001716 NCR5380_write(START_DMA_SEND_REG, 0);
1717 }
1718
1719#ifdef SUN3_SCSI_VME
1720 dregs->csr |= CSR_DMA_ENABLE;
1721#endif
1722
Finn Thaine3f463b2014-11-12 16:12:16 +11001723 sun3_dma_active = 1;
1724
1725#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001726 register unsigned char *d = *data;
1727 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Roman Zippelc28bda22007-05-01 22:32:36 +02001729 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1730 *phase = tmp;
1731 return -1;
1732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Finn Thainef1081c2014-11-12 16:12:14 +11001734 if (hostdata->read_overruns && (p & SR_IO))
1735 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Finn Thaind65e6342014-03-18 11:42:20 +11001737 dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001738 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1739 c, (p & SR_IO) ? "to" : "from", d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Roman Zippelc28bda22007-05-01 22:32:36 +02001741 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thaincd400822016-01-03 16:05:40 +11001742 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1743 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Finn Thainef1081c2014-11-12 16:12:14 +11001745 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001746 /* On the Medusa, it is a must to initialize the DMA before
1747 * starting the NCR. This is also the cleaner way for the TT.
1748 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001749 hostdata->dma_len = (p & SR_IO) ?
1750 NCR5380_dma_read_setup(instance, d, c) :
1751 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Roman Zippelc28bda22007-05-01 22:32:36 +02001754 if (p & SR_IO)
1755 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1756 else {
1757 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1758 NCR5380_write(START_DMA_SEND_REG, 0);
1759 }
1760
Finn Thainef1081c2014-11-12 16:12:14 +11001761 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001762 /* On the Falcon, the DMA setup must be done after the last */
1763 /* NCR access, else the DMA setup gets trashed!
1764 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001765 hostdata->dma_len = (p & SR_IO) ?
1766 NCR5380_dma_read_setup(instance, d, c) :
1767 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001768 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001769#endif /* !defined(CONFIG_SUN3) */
1770
Roman Zippelc28bda22007-05-01 22:32:36 +02001771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773#endif /* defined(REAL_DMA) */
1774
1775/*
1776 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1777 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001778 * Purpose : run through the various SCSI phases and do as the target
1779 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * instance->connected.
1781 *
1782 * Inputs : instance, instance for which we are doing commands
1783 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001784 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 * modified if a command disconnects, *instance->connected will
1786 * change.
1787 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001788 * XXX Note : we need to watch for bus free or a reset condition here
1789 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001791
1792static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Finn Thaine8a60142016-01-03 16:05:54 +11001794 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001795 unsigned char msgout = NOP;
1796 int sink = 0;
1797 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001799 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001801 unsigned char *data;
1802 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001803 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Finn Thaine3f463b2014-11-12 16:12:16 +11001805#ifdef SUN3_SCSI_VME
1806 dregs->csr |= CSR_INTR;
1807#endif
1808
Finn Thain11d2f632016-01-03 16:05:51 +11001809 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001810 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1811
Roman Zippelc28bda22007-05-01 22:32:36 +02001812 tmp = NCR5380_read(STATUS_REG);
1813 /* We only have a valid SCSI phase when REQ is asserted */
1814 if (tmp & SR_REQ) {
1815 phase = (tmp & PHASE_MASK);
1816 if (phase != old_phase) {
1817 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001818 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001820#if defined(CONFIG_SUN3)
1821 if (phase == PHASE_CMDOUT) {
1822#if defined(REAL_DMA)
1823 void *d;
1824 unsigned long count;
1825
1826 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1827 count = cmd->SCp.buffer->length;
1828 d = sg_virt(cmd->SCp.buffer);
1829 } else {
1830 count = cmd->SCp.this_residual;
1831 d = cmd->SCp.ptr;
1832 }
1833 /* this command setup for dma yet? */
1834 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
1835 if (cmd->request->cmd_type == REQ_TYPE_FS) {
1836 sun3scsi_dma_setup(d, count,
1837 rq_data_dir(cmd->request));
1838 sun3_dma_setup_done = cmd;
1839 }
1840 }
1841#endif
1842#ifdef SUN3_SCSI_VME
1843 dregs->csr |= CSR_INTR;
1844#endif
1845 }
1846#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Roman Zippelc28bda22007-05-01 22:32:36 +02001848 if (sink && (phase != PHASE_MSGOUT)) {
1849 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Roman Zippelc28bda22007-05-01 22:32:36 +02001851 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1852 ICR_ASSERT_ACK);
1853 while (NCR5380_read(STATUS_REG) & SR_REQ)
1854 ;
1855 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1856 ICR_ASSERT_ATN);
1857 sink = 0;
1858 continue;
1859 }
1860
1861 switch (phase) {
1862 case PHASE_DATAOUT:
1863#if (NDEBUG & NDEBUG_NO_DATAOUT)
1864 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
1865 "aborted\n", HOSTNO);
1866 sink = 1;
1867 do_abort(instance);
1868 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001869 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001870 return;
1871#endif
1872 case PHASE_DATAIN:
1873 /*
1874 * If there is no room left in the current buffer in the
1875 * scatter-gather list, move onto the next one.
1876 */
1877
1878 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1879 ++cmd->SCp.buffer;
1880 --cmd->SCp.buffers_residual;
1881 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001882 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02001883 /* ++roman: Try to merge some scatter-buffers if
1884 * they are at contiguous physical addresses.
1885 */
1886 merge_contiguous_buffers(cmd);
Finn Thaind65e6342014-03-18 11:42:20 +11001887 dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02001888 HOSTNO, cmd->SCp.this_residual,
1889 cmd->SCp.buffers_residual);
1890 }
1891
1892 /*
1893 * The preferred transfer method is going to be
1894 * PSEUDO-DMA for systems that are strictly PIO,
1895 * since we can let the hardware do the handshaking.
1896 *
1897 * For this to work, we need to know the transfersize
1898 * ahead of time, since the pseudo-DMA code will sit
1899 * in an unconditional loop.
1900 */
1901
1902 /* ++roman: I suggest, this should be
1903 * #if def(REAL_DMA)
1904 * instead of leaving REAL_DMA out.
1905 */
1906
1907#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11001908#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11001909 transfersize = 0;
1910 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11001911#endif
Finn Thainff3d4572016-01-03 16:05:25 +11001912 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1913
1914 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001915 len = transfersize;
1916 cmd->SCp.phase = phase;
1917 if (NCR5380_transfer_dma(instance, &phase,
1918 &len, (unsigned char **)&cmd->SCp.ptr)) {
1919 /*
1920 * If the watchdog timer fires, all future
1921 * accesses to this device will use the
1922 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11001923 scmd_printk(KERN_INFO, cmd,
1924 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001925 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02001926 sink = 1;
1927 do_abort(instance);
1928 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001929 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001930 /* XXX - need to source or sink data here, as appropriate */
1931 } else {
1932#ifdef REAL_DMA
1933 /* ++roman: When using real DMA,
1934 * information_transfer() should return after
1935 * starting DMA since it has nothing more to
1936 * do.
1937 */
1938 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939#else
Roman Zippelc28bda22007-05-01 22:32:36 +02001940 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001942 }
1943 } else
1944#endif /* defined(REAL_DMA) */
Finn Thain11d2f632016-01-03 16:05:51 +11001945 {
1946 spin_unlock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001947 NCR5380_transfer_pio(instance, &phase,
1948 (int *)&cmd->SCp.this_residual,
1949 (unsigned char **)&cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11001950 spin_lock_irq(&hostdata->lock);
1951 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001952#if defined(CONFIG_SUN3) && defined(REAL_DMA)
1953 /* if we had intended to dma that command clear it */
1954 if (sun3_dma_setup_done == cmd)
1955 sun3_dma_setup_done = NULL;
1956#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001957 break;
1958 case PHASE_MSGIN:
1959 len = 1;
1960 data = &tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02001961 NCR5380_transfer_pio(instance, &phase, &len, &data);
1962 cmd->SCp.Message = tmp;
1963
1964 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001965 case ABORT:
1966 case COMMAND_COMPLETE:
1967 /* Accept message by clearing ACK */
1968 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001969 dsprintk(NDEBUG_QUEUES, instance,
1970 "COMMAND COMPLETE %p target %d lun %llu\n",
1971 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11001972
Finn Thaine3c3da62014-11-12 16:12:15 +11001973 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02001974#ifdef SUPPORT_TAGS
1975 cmd_free_tag(cmd);
1976 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
Finn Thain61d739a2014-11-12 16:12:20 +11001977 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001978 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
Roman Zippelc28bda22007-05-01 22:32:36 +02001979 "QUEUE_FULL after %d commands\n",
1980 HOSTNO, cmd->device->id, cmd->device->lun,
1981 ta->nr_allocated);
1982 if (ta->queue_size > ta->nr_allocated)
Finn Thaine42d0152016-01-03 16:05:49 +11001983 ta->queue_size = ta->nr_allocated;
Roman Zippelc28bda22007-05-01 22:32:36 +02001984 }
1985#else
1986 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1987#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001988
1989 /*
1990 * I'm not sure what the correct thing to do here is :
1991 *
1992 * If the command that just executed is NOT a request
1993 * sense, the obvious thing to do is to set the result
1994 * code to the values of the stored parameters.
1995 *
1996 * If it was a REQUEST SENSE command, we need some way to
1997 * differentiate between the failure code of the original
1998 * and the failure code of the REQUEST sense - the obvious
1999 * case is success, where we fall through and leave the
2000 * result code unchanged.
2001 *
2002 * The non-obvious place is where the REQUEST SENSE failed
2003 */
2004
2005 if (cmd->cmnd[0] != REQUEST_SENSE)
2006 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2007 else if (status_byte(cmd->SCp.Status) != GOOD)
2008 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2009
Boaz Harrosh28424d32007-09-10 22:37:45 +03002010 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2011 hostdata->ses.cmd_len) {
2012 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2013 hostdata->ses.cmd_len = 0 ;
2014 }
2015
Roman Zippelc28bda22007-05-01 22:32:36 +02002016 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2017 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
Boaz Harrosh28424d32007-09-10 22:37:45 +03002018 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
Roman Zippelc28bda22007-05-01 22:32:36 +02002019
Finn Thain32b26a12016-01-03 16:05:58 +11002020 list_add(&ncmd->list, &hostdata->unissued);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002021 dsprintk(NDEBUG_AUTOSENSE | NDEBUG_QUEUES,
2022 instance, "REQUEST SENSE cmd %p added to head of issue queue\n",
Finn Thaindbb6b352016-01-03 16:05:53 +11002023 cmd);
Finn Thain997acab2014-11-12 16:11:54 +11002024 } else {
Roman Zippelc28bda22007-05-01 22:32:36 +02002025 cmd->scsi_done(cmd);
2026 }
2027
Roman Zippelc28bda22007-05-01 22:32:36 +02002028 /*
2029 * Restore phase bits to 0 so an interrupted selection,
2030 * arbitration can resume.
2031 */
2032 NCR5380_write(TARGET_COMMAND_REG, 0);
2033
Finn Thain72064a72016-01-03 16:05:44 +11002034 /* Enable reselect interrupts */
2035 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2036
Roman Zippelc28bda22007-05-01 22:32:36 +02002037 /* ++roman: For Falcon SCSI, release the lock on the
2038 * ST-DMA here if no other commands are waiting on the
2039 * disconnected queue.
2040 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002041 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002042 return;
2043 case MESSAGE_REJECT:
2044 /* Accept message by clearing ACK */
2045 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002046 switch (hostdata->last_message) {
2047 case HEAD_OF_QUEUE_TAG:
2048 case ORDERED_QUEUE_TAG:
2049 case SIMPLE_QUEUE_TAG:
2050 /* The target obviously doesn't support tagged
2051 * queuing, even though it announced this ability in
2052 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2053 * clear 'tagged_supported' and lock the LUN, since
2054 * the command is treated as untagged further on.
2055 */
2056 cmd->device->tagged_supported = 0;
2057 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2058 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002059 dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
Roman Zippelc28bda22007-05-01 22:32:36 +02002060 "QUEUE_TAG message; tagged queuing "
2061 "disabled\n",
2062 HOSTNO, cmd->device->id, cmd->device->lun);
2063 break;
2064 }
2065 break;
2066 case DISCONNECT:
2067 /* Accept message by clearing ACK */
2068 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002069 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11002070 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002071 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
2072 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
2073 cmd, scmd_id(cmd), cmd->device->lun);
2074
Roman Zippelc28bda22007-05-01 22:32:36 +02002075 /*
2076 * Restore phase bits to 0 so an interrupted selection,
2077 * arbitration can resume.
2078 */
2079 NCR5380_write(TARGET_COMMAND_REG, 0);
2080
2081 /* Enable reselect interrupts */
2082 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002083#ifdef SUN3_SCSI_VME
2084 dregs->csr |= CSR_DMA_ENABLE;
2085#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002086 return;
2087 /*
2088 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2089 * operation, in violation of the SCSI spec so we can safely
2090 * ignore SAVE/RESTORE pointers calls.
2091 *
2092 * Unfortunately, some disks violate the SCSI spec and
2093 * don't issue the required SAVE_POINTERS message before
2094 * disconnecting, and we have to break spec to remain
2095 * compatible.
2096 */
2097 case SAVE_POINTERS:
2098 case RESTORE_POINTERS:
2099 /* Accept message by clearing ACK */
2100 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002101 break;
2102 case EXTENDED_MESSAGE:
2103 /*
2104 * Extended messages are sent in the following format :
2105 * Byte
2106 * 0 EXTENDED_MESSAGE == 1
2107 * 1 length (includes one byte for code, doesn't
2108 * include first two bytes)
2109 * 2 code
2110 * 3..length+1 arguments
2111 *
2112 * Start the extended message buffer with the EXTENDED_MESSAGE
2113 * byte, since spi_print_msg() wants the whole thing.
2114 */
2115 extended_msg[0] = EXTENDED_MESSAGE;
2116 /* Accept first byte by clearing ACK */
2117 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2118
Finn Thain11d2f632016-01-03 16:05:51 +11002119 spin_unlock_irq(&hostdata->lock);
2120
Finn Thaind65e6342014-03-18 11:42:20 +11002121 dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002122
2123 len = 2;
2124 data = extended_msg + 1;
2125 phase = PHASE_MSGIN;
2126 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002127 dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
Roman Zippelc28bda22007-05-01 22:32:36 +02002128 (int)extended_msg[1], (int)extended_msg[2]);
2129
Finn Thaine0783ed2016-01-03 16:05:45 +11002130 if (!len && extended_msg[1] > 0 &&
2131 extended_msg[1] <= sizeof(extended_msg) - 2) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002132 /* Accept third byte by clearing ACK */
2133 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2134 len = extended_msg[1] - 1;
2135 data = extended_msg + 3;
2136 phase = PHASE_MSGIN;
2137
2138 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thaind65e6342014-03-18 11:42:20 +11002139 dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002140 HOSTNO, len);
2141
2142 switch (extended_msg[2]) {
2143 case EXTENDED_SDTR:
2144 case EXTENDED_WDTR:
2145 case EXTENDED_MODIFY_DATA_POINTER:
2146 case EXTENDED_EXTENDED_IDENTIFY:
2147 tmp = 0;
2148 }
2149 } else if (len) {
2150 printk(KERN_NOTICE "scsi%d: error receiving "
2151 "extended message\n", HOSTNO);
2152 tmp = 0;
2153 } else {
2154 printk(KERN_NOTICE "scsi%d: extended message "
2155 "code %02x length %d is too long\n",
2156 HOSTNO, extended_msg[2], extended_msg[1]);
2157 tmp = 0;
2158 }
Finn Thain11d2f632016-01-03 16:05:51 +11002159
2160 spin_lock_irq(&hostdata->lock);
2161 if (!hostdata->connected)
2162 return;
2163
Roman Zippelc28bda22007-05-01 22:32:36 +02002164 /* Fall through to reject message */
2165
2166 /*
2167 * If we get something weird that we aren't expecting,
2168 * reject it.
2169 */
2170 default:
2171 if (!tmp) {
Finn Thainff50f9e2014-11-12 16:12:18 +11002172 printk(KERN_INFO "scsi%d: rejecting message ",
2173 instance->host_no);
Roman Zippelc28bda22007-05-01 22:32:36 +02002174 spi_print_msg(extended_msg);
2175 printk("\n");
2176 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002177 scmd_printk(KERN_INFO, cmd,
2178 "rejecting unknown message %02x\n",
2179 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002180 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002181 scmd_printk(KERN_INFO, cmd,
2182 "rejecting unknown extended message code %02x, length %d\n",
2183 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002184
2185 msgout = MESSAGE_REJECT;
2186 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2187 break;
2188 } /* switch (tmp) */
2189 break;
2190 case PHASE_MSGOUT:
2191 len = 1;
2192 data = &msgout;
2193 hostdata->last_message = msgout;
2194 NCR5380_transfer_pio(instance, &phase, &len, &data);
2195 if (msgout == ABORT) {
2196#ifdef SUPPORT_TAGS
2197 cmd_free_tag(cmd);
2198#else
2199 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2200#endif
2201 hostdata->connected = NULL;
2202 cmd->result = DID_ERROR << 16;
Finn Thaine3c3da62014-11-12 16:12:15 +11002203 maybe_release_dma_irq(instance);
Finn Thaine3c3da62014-11-12 16:12:15 +11002204 cmd->scsi_done(cmd);
Finn Thaincd400822016-01-03 16:05:40 +11002205 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02002206 return;
2207 }
2208 msgout = NOP;
2209 break;
2210 case PHASE_CMDOUT:
2211 len = cmd->cmd_len;
2212 data = cmd->cmnd;
2213 /*
2214 * XXX for performance reasons, on machines with a
2215 * PSEUDO-DMA architecture we should probably
2216 * use the dma transfer function.
2217 */
2218 NCR5380_transfer_pio(instance, &phase, &len, &data);
2219 break;
2220 case PHASE_STATIN:
2221 len = 1;
2222 data = &tmp;
2223 NCR5380_transfer_pio(instance, &phase, &len, &data);
2224 cmd->SCp.Status = tmp;
2225 break;
2226 default:
2227 printk("scsi%d: unknown phase\n", HOSTNO);
Finn Thain8ad3a592014-03-18 11:42:19 +11002228 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002229 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002230 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002231 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002232 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002233 spin_lock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002234 }
Finn Thain11d2f632016-01-03 16:05:51 +11002235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238/*
2239 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2240 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002241 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002242 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 * Inputs : instance - this instance of the NCR5380.
2246 *
2247 */
2248
2249
Finn Thaine3f463b2014-11-12 16:12:16 +11002250/* it might eventually prove necessary to do a dma setup on
2251 reselection, but it doesn't seem to be needed now -- sam */
2252
Roman Zippelc28bda22007-05-01 22:32:36 +02002253static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Finn Thaine8a60142016-01-03 16:05:54 +11002255 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002256 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002257 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002259 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002261 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002262 int __maybe_unused len;
2263 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain32b26a12016-01-03 16:05:58 +11002264 struct NCR5380_cmd *ncmd;
2265 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Roman Zippelc28bda22007-05-01 22:32:36 +02002267 /*
2268 * Disable arbitration, etc. since the host adapter obviously
2269 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Roman Zippelc28bda22007-05-01 22:32:36 +02002272 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Roman Zippelc28bda22007-05-01 22:32:36 +02002274 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2275
Finn Thaind65e6342014-03-18 11:42:20 +11002276 dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
Roman Zippelc28bda22007-05-01 22:32:36 +02002277
2278 /*
2279 * At this point, we have detected that our SCSI ID is on the bus,
2280 * SEL is true and BSY was false for at least one bus settle delay
2281 * (400 ns).
2282 *
2283 * We must assert BSY ourselves, until the target drops the SEL
2284 * signal.
2285 */
2286
2287 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002288 if (NCR5380_poll_politely(instance,
2289 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2290 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2291 return;
2292 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002293 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2294
2295 /*
2296 * Wait for target to go into MSGIN.
2297 */
2298
Finn Thain72064a72016-01-03 16:05:44 +11002299 if (NCR5380_poll_politely(instance,
2300 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2301 do_abort(instance);
2302 return;
2303 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002304
Finn Thaine3f463b2014-11-12 16:12:16 +11002305#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2306 /* acknowledge toggle to MSGIN */
2307 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2308
2309 /* peek at the byte without really hitting the bus */
2310 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2311#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002312 len = 1;
2313 data = msg;
2314 phase = PHASE_MSGIN;
2315 NCR5380_transfer_pio(instance, &phase, &len, &data);
2316
Finn Thain72064a72016-01-03 16:05:44 +11002317 if (len) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002318 do_abort(instance);
2319 return;
2320 }
Finn Thain72064a72016-01-03 16:05:44 +11002321#endif
2322
2323 if (!(msg[0] & 0x80)) {
2324 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2325 spi_print_msg(msg);
2326 printk("\n");
2327 do_abort(instance);
2328 return;
2329 }
2330 lun = msg[0] & 0x07;
Roman Zippelc28bda22007-05-01 22:32:36 +02002331
Finn Thaine3f463b2014-11-12 16:12:16 +11002332#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002333 /* If the phase is still MSGIN, the target wants to send some more
2334 * messages. In case it supports tagged queuing, this is probably a
2335 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2336 */
2337 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002338 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002339 /* Accept previous IDENTIFY message by clearing ACK */
2340 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2341 len = 2;
2342 data = msg + 1;
2343 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2344 msg[1] == SIMPLE_QUEUE_TAG)
2345 tag = msg[2];
Finn Thaind65e6342014-03-18 11:42:20 +11002346 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
Roman Zippelc28bda22007-05-01 22:32:36 +02002347 "reselection\n", HOSTNO, target_mask, lun, tag);
2348 }
2349#endif
2350
2351 /*
2352 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2353 * just reestablished, and remove it from the disconnected queue.
2354 */
2355
Finn Thain32b26a12016-01-03 16:05:58 +11002356 tmp = NULL;
2357 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2358 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2359
2360 if (target_mask == (1 << scmd_id(cmd)) &&
2361 lun == (u8)cmd->device->lun
Roman Zippelc28bda22007-05-01 22:32:36 +02002362#ifdef SUPPORT_TAGS
Finn Thain32b26a12016-01-03 16:05:58 +11002363 && (tag == cmd->tag)
Roman Zippelc28bda22007-05-01 22:32:36 +02002364#endif
2365 ) {
Finn Thain32b26a12016-01-03 16:05:58 +11002366 list_del(&ncmd->list);
2367 tmp = cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +02002368 break;
2369 }
2370 }
2371
Finn Thain0d3d9a42016-01-03 16:05:55 +11002372 if (tmp) {
2373 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2374 "reselect: removed %p from disconnected queue\n", tmp);
2375 } else {
2376
Roman Zippelc28bda22007-05-01 22:32:36 +02002377#ifdef SUPPORT_TAGS
Finn Thain72064a72016-01-03 16:05:44 +11002378 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d tag %d not in disconnected queue.\n",
2379 target_mask, lun, tag);
2380#else
2381 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2382 target_mask, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002383#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002384 /*
2385 * Since we have an established nexus that we can't do anything
2386 * with, we must abort it.
2387 */
2388 do_abort(instance);
2389 return;
2390 }
2391
Finn Thaine3f463b2014-11-12 16:12:16 +11002392#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2393 /* engage dma setup for the command we just saw */
2394 {
2395 void *d;
2396 unsigned long count;
2397
2398 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2399 count = tmp->SCp.buffer->length;
2400 d = sg_virt(tmp->SCp.buffer);
2401 } else {
2402 count = tmp->SCp.this_residual;
2403 d = tmp->SCp.ptr;
2404 }
2405 /* setup this command for dma if not already */
2406 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
2407 sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
2408 sun3_dma_setup_done = tmp;
2409 }
2410 }
2411
2412 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2413#endif
2414
Roman Zippelc28bda22007-05-01 22:32:36 +02002415 /* Accept message by clearing ACK */
2416 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2417
Finn Thaine3f463b2014-11-12 16:12:16 +11002418#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2419 /* If the phase is still MSGIN, the target wants to send some more
2420 * messages. In case it supports tagged queuing, this is probably a
2421 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2422 */
2423 tag = TAG_NONE;
2424 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2425 /* Accept previous IDENTIFY message by clearing ACK */
2426 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2427 len = 2;
2428 data = msg + 1;
2429 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2430 msg[1] == SIMPLE_QUEUE_TAG)
2431 tag = msg[2];
2432 dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2433 HOSTNO, target_mask, lun, tag);
2434 }
2435#endif
2436
Roman Zippelc28bda22007-05-01 22:32:36 +02002437 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002438 dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
Roman Zippelc28bda22007-05-01 22:32:36 +02002439 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442
2443/*
Finn Thain710ddd02014-11-12 16:12:02 +11002444 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 *
2446 * Purpose : abort a command
2447 *
Finn Thain710ddd02014-11-12 16:12:02 +11002448 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Roman Zippelc28bda22007-05-01 22:32:36 +02002449 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 * used.
2451 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002452 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002454 * XXX - there is no way to abort the command that is currently
2455 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 * a problem, we could implement longjmp() / setjmp(), setjmp()
Roman Zippelc28bda22007-05-01 22:32:36 +02002457 * called where the loop started in NCR5380_main().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
2459
2460static
Finn Thain710ddd02014-11-12 16:12:02 +11002461int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Roman Zippelc28bda22007-05-01 22:32:36 +02002463 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002464 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002465 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Finn Thain11d2f632016-01-03 16:05:51 +11002467 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Finn Thain32b26a12016-01-03 16:05:58 +11002469#if (NDEBUG & NDEBUG_ANY)
2470 scmd_printk(KERN_INFO, cmd, "aborting command\n");
2471#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002472 NCR5380_dprint(NDEBUG_ANY, instance);
2473 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Finn Thain11d2f632016-01-03 16:05:51 +11002475 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002476
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002477 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
2480
Finn Thain3be1b3e2016-01-03 16:05:12 +11002481/**
2482 * NCR5380_bus_reset - reset the SCSI bus
2483 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002484 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002485 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Finn Thain710ddd02014-11-12 16:12:02 +11002488static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Finn Thaine3c3da62014-11-12 16:12:15 +11002490 struct Scsi_Host *instance = cmd->device->host;
2491 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002492 int i;
2493 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Finn Thain11d2f632016-01-03 16:05:51 +11002495 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Finn Thain3be1b3e2016-01-03 16:05:12 +11002497#if (NDEBUG & NDEBUG_ANY)
2498 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
Finn Thain3be1b3e2016-01-03 16:05:12 +11002499#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002500 NCR5380_dprint(NDEBUG_ANY, instance);
2501 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002502
2503 do_reset(instance);
2504
Roman Zippelc28bda22007-05-01 22:32:36 +02002505 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002506 NCR5380_write(MODE_REG, MR_BASE);
2507 NCR5380_write(TARGET_COMMAND_REG, 0);
2508 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Roman Zippelc28bda22007-05-01 22:32:36 +02002510 /* After the reset, there are no more connected or disconnected commands
2511 * and no busy units; so clear the low-level status here to avoid
2512 * conflicts when the mid-level code tries to wake up the affected
2513 * commands!
2514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Roman Zippelc28bda22007-05-01 22:32:36 +02002516 if (hostdata->connected)
Finn Thaindbb6b352016-01-03 16:05:53 +11002517 dsprintk(NDEBUG_ABORT, instance, "reset aborted a connected command\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002518 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11002519
2520 if (!list_empty(&hostdata->unissued))
2521 dsprintk(NDEBUG_ABORT, instance, "reset aborted unissued list\n");
2522 INIT_LIST_HEAD(&hostdata->unissued);
2523
2524 if (!list_empty(&hostdata->disconnected))
2525 dsprintk(NDEBUG_ABORT, instance, "reset aborted disconnected list\n");
2526 INIT_LIST_HEAD(&hostdata->disconnected);
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002529 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002531 for (i = 0; i < 8; ++i)
2532 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002534 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002536
2537 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002538 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002540 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}