blob: b36fadab422b5fffd4f0dcc69ca363b03fd6ad08 [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
Finn Thain636b1ec2016-01-03 16:05:10 +1100178static int do_abort(struct Scsi_Host *);
179static void do_reset(struct Scsi_Host *);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef SUPPORT_TAGS
182
183/*
184 * Functions for handling tagged queuing
185 * =====================================
186 *
187 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
188 *
189 * Using consecutive numbers for the tags is no good idea in my eyes. There
190 * could be wrong re-usings if the counter (8 bit!) wraps and some early
191 * command has been preempted for a long time. My solution: a bitfield for
192 * remembering used tags.
193 *
194 * There's also the problem that each target has a certain queue size, but we
195 * cannot know it in advance :-( We just see a QUEUE_FULL status being
196 * returned. So, in this case, the driver internal queue size assumption is
197 * reduced to the number of active tags if QUEUE_FULL is returned by the
Finn Thaine42d0152016-01-03 16:05:49 +1100198 * target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * We're also not allowed running tagged commands as long as an untagged
201 * command is active. And REQUEST SENSE commands after a contingent allegiance
202 * condition _must_ be untagged. To keep track whether an untagged command has
203 * been issued, the host->busy array is still employed, as it is without
204 * support for tagged queuing.
205 *
206 * One could suspect that there are possible race conditions between
207 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
208 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
209 * which already guaranteed to be running at most once. It is also the only
210 * place where tags/LUNs are allocated. So no other allocation can slip
211 * between that pair, there could only happen a reselection, which can free a
212 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
213 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
214 */
215
Finn Thainca513fc2014-11-12 16:12:19 +1100216static void __init init_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Roman Zippelc28bda22007-05-01 22:32:36 +0200218 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100219 struct tag_alloc *ta;
Roman Zippelc28bda22007-05-01 22:32:36 +0200220
Finn Thainca513fc2014-11-12 16:12:19 +1100221 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200222 return;
223
224 for (target = 0; target < 8; ++target) {
225 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100226 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200227 bitmap_zero(ta->allocated, MAX_TAGS);
228 ta->nr_allocated = 0;
229 /* At the beginning, assume the maximum queue size we could
230 * support (MAX_TAGS). This value will be decreased if the target
231 * returns QUEUE_FULL status.
232 */
233 ta->queue_size = MAX_TAGS;
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238
239/* Check if we can issue a command to this LUN: First see if the LUN is marked
240 * busy by an untagged command. If the command should use tagged queuing, also
241 * check that there is a free tag and the target's queue won't overflow. This
242 * function should be called with interrupts disabled to avoid race
243 * conditions.
Roman Zippelc28bda22007-05-01 22:32:36 +0200244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Finn Thain710ddd02014-11-12 16:12:02 +1100246static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200248 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100249 struct Scsi_Host *instance = cmd->device->host;
250 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200252 if (hostdata->busy[cmd->device->id] & (1 << lun))
Roman Zippelc28bda22007-05-01 22:32:36 +0200253 return 1;
254 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100255 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
256 !cmd->device->tagged_supported)
Roman Zippelc28bda22007-05-01 22:32:36 +0200257 return 0;
Finn Thain61d739a2014-11-12 16:12:20 +1100258 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
259 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100260 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d: no free tags\n",
261 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200262 return 1;
263 }
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267
268/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
269 * must be called before!), or reserve the LUN in 'busy' if the command is
270 * untagged.
271 */
272
Finn Thain710ddd02014-11-12 16:12:02 +1100273static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200275 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100276 struct Scsi_Host *instance = cmd->device->host;
277 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Roman Zippelc28bda22007-05-01 22:32:36 +0200279 /* If we or the target don't support tagged queuing, allocate the LUN for
280 * an untagged command.
281 */
282 if (!should_be_tagged ||
Finn Thainca513fc2014-11-12 16:12:19 +1100283 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
284 !cmd->device->tagged_supported) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200285 cmd->tag = TAG_NONE;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200286 hostdata->busy[cmd->device->id] |= (1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100287 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d now allocated by untagged command\n",
288 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200289 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100290 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Roman Zippelc28bda22007-05-01 22:32:36 +0200292 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
293 set_bit(cmd->tag, ta->allocated);
294 ta->nr_allocated++;
Finn Thaindbb6b352016-01-03 16:05:53 +1100295 dsprintk(NDEBUG_TAGS, instance, "using tag %d for target %d lun %d (%d tags allocated)\n",
296 cmd->tag, scmd_id(cmd), lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +0200297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300
301/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
302 * unlock the LUN.
303 */
304
Finn Thain710ddd02014-11-12 16:12:02 +1100305static void cmd_free_tag(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200307 u8 lun = cmd->device->lun;
Finn Thaindbb6b352016-01-03 16:05:53 +1100308 struct Scsi_Host *instance = cmd->device->host;
309 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Roman Zippelc28bda22007-05-01 22:32:36 +0200311 if (cmd->tag == TAG_NONE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200312 hostdata->busy[cmd->device->id] &= ~(1 << lun);
Finn Thaindbb6b352016-01-03 16:05:53 +1100313 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d untagged cmd freed\n",
314 scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200315 } else if (cmd->tag >= MAX_TAGS) {
Finn Thaindbb6b352016-01-03 16:05:53 +1100316 shost_printk(KERN_NOTICE, instance,
317 "trying to free bad tag %d!\n", cmd->tag);
Roman Zippelc28bda22007-05-01 22:32:36 +0200318 } else {
Finn Thain61d739a2014-11-12 16:12:20 +1100319 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200320 clear_bit(cmd->tag, ta->allocated);
321 ta->nr_allocated--;
Finn Thaindbb6b352016-01-03 16:05:53 +1100322 dsprintk(NDEBUG_TAGS, instance, "freed tag %d for target %d lun %d\n",
323 cmd->tag, scmd_id(cmd), lun);
Roman Zippelc28bda22007-05-01 22:32:36 +0200324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327
Finn Thainca513fc2014-11-12 16:12:19 +1100328static void free_all_tags(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Roman Zippelc28bda22007-05-01 22:32:36 +0200330 int target, lun;
Finn Thain61d739a2014-11-12 16:12:20 +1100331 struct tag_alloc *ta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Finn Thainca513fc2014-11-12 16:12:19 +1100333 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
Roman Zippelc28bda22007-05-01 22:32:36 +0200334 return;
335
336 for (target = 0; target < 8; ++target) {
337 for (lun = 0; lun < 8; ++lun) {
Finn Thain61d739a2014-11-12 16:12:20 +1100338 ta = &hostdata->TagAlloc[target][lun];
Roman Zippelc28bda22007-05-01 22:32:36 +0200339 bitmap_zero(ta->allocated, MAX_TAGS);
340 ta->nr_allocated = 0;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345#endif /* SUPPORT_TAGS */
346
347
348/*
Finn Thain710ddd02014-11-12 16:12:02 +1100349 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
351 * Purpose: Try to merge several scatter-gather requests into one DMA
352 * transfer. This is possible if the scatter buffers lie on
353 * physical contiguous addresses.
354 *
Finn Thain710ddd02014-11-12 16:12:02 +1100355 * Parameters: struct scsi_cmnd *cmd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * The command to work on. The first scatter buffer's data are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300357 * assumed to be already transferred into ptr/this_residual.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
359
Finn Thain710ddd02014-11-12 16:12:02 +1100360static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Finn Thaine3f463b2014-11-12 16:12:16 +1100362#if !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +0200363 unsigned long endaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200365 unsigned long oldlen = cmd->SCp.this_residual;
366 int cnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#endif
368
Roman Zippelc28bda22007-05-01 22:32:36 +0200369 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
370 cmd->SCp.buffers_residual &&
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200371 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
Finn Thaind65e6342014-03-18 11:42:20 +1100372 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
Geert Uytterhoeven5a1cb472007-10-24 08:55:40 +0200373 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200375 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200377 ++cmd->SCp.buffer;
378 --cmd->SCp.buffers_residual;
379 cmd->SCp.this_residual += cmd->SCp.buffer->length;
380 endaddr += cmd->SCp.buffer->length;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#if (NDEBUG & NDEBUG_MERGING)
Roman Zippelc28bda22007-05-01 22:32:36 +0200383 if (oldlen != cmd->SCp.this_residual)
Finn Thaind65e6342014-03-18 11:42:20 +1100384 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
Roman Zippelc28bda22007-05-01 22:32:36 +0200385 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#endif
Finn Thaine3f463b2014-11-12 16:12:16 +1100387#endif /* !defined(CONFIG_SUN3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Finn Thainff50f9e2014-11-12 16:12:18 +1100390/**
391 * initialize_SCp - init the scsi pointer field
392 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100394 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396
Finn Thain710ddd02014-11-12 16:12:02 +1100397static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Roman Zippelc28bda22007-05-01 22:32:36 +0200399 /*
400 * Initialize the Scsi Pointer field so that all of the commands in the
401 * various queues are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200403
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200404 if (scsi_bufflen(cmd)) {
405 cmd->SCp.buffer = scsi_sglist(cmd);
406 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200407 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +0200408 cmd->SCp.this_residual = cmd->SCp.buffer->length;
409 /* ++roman: Try to merge some scatter-buffers if they are at
410 * contiguous physical addresses.
411 */
412 merge_contiguous_buffers(cmd);
413 } else {
414 cmd->SCp.buffer = NULL;
415 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200416 cmd->SCp.ptr = NULL;
417 cmd->SCp.this_residual = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200418 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100419
420 cmd->SCp.Status = 0;
421 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Finn Thain636b1ec2016-01-03 16:05:10 +1100424/**
Finn Thainb32ade12016-01-03 16:05:41 +1100425 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain636b1ec2016-01-03 16:05:10 +1100426 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100427 * @reg1: 5380 register to poll
428 * @bit1: Bitmask to check
429 * @val1: Expected value
430 * @reg2: Second 5380 register to poll
431 * @bit2: Second bitmask to check
432 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100433 * @wait: Time-out in jiffies
Finn Thain636b1ec2016-01-03 16:05:10 +1100434 *
Finn Thain2f854b82016-01-03 16:05:22 +1100435 * Polls the chip in a reasonably efficient manner waiting for an
436 * event to occur. After a short quick poll we begin to yield the CPU
437 * (if possible). In irq contexts the time-out is arbitrarily limited.
438 * Callers may hold locks as long as they are held in irq mode.
Finn Thain636b1ec2016-01-03 16:05:10 +1100439 *
Finn Thainb32ade12016-01-03 16:05:41 +1100440 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Finn Thain636b1ec2016-01-03 16:05:10 +1100441 */
442
Finn Thainb32ade12016-01-03 16:05:41 +1100443static int NCR5380_poll_politely2(struct Scsi_Host *instance,
444 int reg1, int bit1, int val1,
445 int reg2, int bit2, int val2, int wait)
Finn Thain636b1ec2016-01-03 16:05:10 +1100446{
Finn Thain2f854b82016-01-03 16:05:22 +1100447 struct NCR5380_hostdata *hostdata = shost_priv(instance);
448 unsigned long deadline = jiffies + wait;
449 unsigned long n;
Finn Thain636b1ec2016-01-03 16:05:10 +1100450
Finn Thain2f854b82016-01-03 16:05:22 +1100451 /* Busy-wait for up to 10 ms */
452 n = min(10000U, jiffies_to_usecs(wait));
453 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100454 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100455 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100456 if ((NCR5380_read(reg1) & bit1) == val1)
457 return 0;
458 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain636b1ec2016-01-03 16:05:10 +1100459 return 0;
460 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100461 } while (n--);
462
463 if (irqs_disabled() || in_interrupt())
464 return -ETIMEDOUT;
465
466 /* Repeatedly sleep for 1 ms until deadline */
467 while (time_is_after_jiffies(deadline)) {
468 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100469 if ((NCR5380_read(reg1) & bit1) == val1)
470 return 0;
471 if ((NCR5380_read(reg2) & bit2) == val2)
Finn Thain2f854b82016-01-03 16:05:22 +1100472 return 0;
Finn Thain636b1ec2016-01-03 16:05:10 +1100473 }
474
Finn Thain636b1ec2016-01-03 16:05:10 +1100475 return -ETIMEDOUT;
476}
477
Finn Thainb32ade12016-01-03 16:05:41 +1100478static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
479 int reg, int bit, int val, int wait)
480{
481 return NCR5380_poll_politely2(instance, reg, bit, val,
482 reg, bit, val, wait);
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#if NDEBUG
486static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200487 unsigned char mask;
488 const char *name;
489} signals[] = {
490 { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
491 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
492 { SR_SEL, "SEL" }, {0, NULL}
493}, basrs[] = {
494 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
495}, icrs[] = {
496 {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
497 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
498 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
499 {0, NULL}
500}, mrs[] = {
501 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
502 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
503 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
504 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
505 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
506 {0, NULL}
507};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Finn Thainff50f9e2014-11-12 16:12:18 +1100509/**
510 * NCR5380_print - print scsi bus signals
511 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100513 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515
Roman Zippelc28bda22007-05-01 22:32:36 +0200516static void NCR5380_print(struct Scsi_Host *instance)
517{
518 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Roman Zippelc28bda22007-05-01 22:32:36 +0200520 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
521 status = NCR5380_read(STATUS_REG);
522 mr = NCR5380_read(MODE_REG);
523 icr = NCR5380_read(INITIATOR_COMMAND_REG);
524 basr = NCR5380_read(BUS_AND_STATUS_REG);
Finn Thain11d2f632016-01-03 16:05:51 +1100525
Roman Zippelc28bda22007-05-01 22:32:36 +0200526 printk("STATUS_REG: %02x ", status);
527 for (i = 0; signals[i].mask; ++i)
528 if (status & signals[i].mask)
529 printk(",%s", signals[i].name);
530 printk("\nBASR: %02x ", basr);
531 for (i = 0; basrs[i].mask; ++i)
532 if (basr & basrs[i].mask)
533 printk(",%s", basrs[i].name);
534 printk("\nICR: %02x ", icr);
535 for (i = 0; icrs[i].mask; ++i)
536 if (icr & icrs[i].mask)
537 printk(",%s", icrs[i].name);
538 printk("\nMODE: %02x ", mr);
539 for (i = 0; mrs[i].mask; ++i)
540 if (mr & mrs[i].mask)
541 printk(",%s", mrs[i].name);
542 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static struct {
Roman Zippelc28bda22007-05-01 22:32:36 +0200546 unsigned char value;
547 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548} phases[] = {
Roman Zippelc28bda22007-05-01 22:32:36 +0200549 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
550 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
551 {PHASE_UNKNOWN, "UNKNOWN"}
552};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Finn Thainff50f9e2014-11-12 16:12:18 +1100554/**
555 * NCR5380_print_phase - show SCSI phase
556 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100558 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100560 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
562
563static void NCR5380_print_phase(struct Scsi_Host *instance)
564{
Roman Zippelc28bda22007-05-01 22:32:36 +0200565 unsigned char status;
566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Roman Zippelc28bda22007-05-01 22:32:36 +0200568 status = NCR5380_read(STATUS_REG);
569 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100570 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200571 else {
572 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
573 (phases[i].value != (status & PHASE_MASK)); ++i)
574 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100575 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Roman Zippelc28bda22007-05-01 22:32:36 +0200576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580
Finn Thain8c325132014-11-12 16:11:58 +1100581/**
582 * NCR58380_info - report driver and host information
583 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 *
Finn Thain8c325132014-11-12 16:11:58 +1100585 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
Finn Thain8c325132014-11-12 16:11:58 +1100587 * Locks: none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
589
Finn Thain8c325132014-11-12 16:11:58 +1100590static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Finn Thain8c325132014-11-12 16:11:58 +1100592 struct NCR5380_hostdata *hostdata = shost_priv(instance);
593
594 return hostdata->info;
595}
596
597static void prepare_info(struct Scsi_Host *instance)
598{
599 struct NCR5380_hostdata *hostdata = shost_priv(instance);
600
601 snprintf(hostdata->info, sizeof(hostdata->info),
602 "%s, io_port 0x%lx, n_io_port %d, "
603 "base 0x%lx, irq %d, "
604 "can_queue %d, cmd_per_lun %d, "
605 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100606 "flags { %s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100607 "options { %s} ",
608 instance->hostt->name, instance->io_port, instance->n_io_port,
609 instance->base, instance->irq,
610 instance->can_queue, instance->cmd_per_lun,
611 instance->sg_tablesize, instance->this_id,
Finn Thainca513fc2014-11-12 16:12:19 +1100612 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100613 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100614#ifdef DIFFERENTIAL
615 "DIFFERENTIAL "
616#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100618 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#endif
620#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100621 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622#endif
623#ifdef SUPPORT_TAGS
Finn Thain8c325132014-11-12 16:11:58 +1100624 "SUPPORT_TAGS "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif
Finn Thain8c325132014-11-12 16:11:58 +1100626 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Finn Thainff50f9e2014-11-12 16:12:18 +1100629/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100630 * NCR5380_init - initialise an NCR5380
631 * @instance: adapter to configure
632 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100634 * Initializes *instance and corresponding 5380 chip,
635 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
637 * Notes : I assume that the host, hostno, and id bits have been
Finn Thainff50f9e2014-11-12 16:12:18 +1100638 * set correctly. I don't care about the irq and other fields.
Roman Zippelc28bda22007-05-01 22:32:36 +0200639 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100640 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
642
Michael Schmitz95fde7a2009-01-18 03:22:15 +0100643static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Finn Thaine8a60142016-01-03 16:05:54 +1100645 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +0200646 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100647 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Finn Thaina53a21e2014-11-12 16:12:21 +1100649 hostdata->host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200650 hostdata->id_mask = 1 << instance->this_id;
651 hostdata->id_higher_mask = 0;
652 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
653 if (i > hostdata->id_mask)
654 hostdata->id_higher_mask |= i;
655 for (i = 0; i < 8; ++i)
656 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +1100658 init_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#endif
660#if defined (REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +0200661 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662#endif
Finn Thain11d2f632016-01-03 16:05:51 +1100663 spin_lock_init(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200664 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100665 hostdata->sensing = NULL;
666 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100667 INIT_LIST_HEAD(&hostdata->unissued);
668 INIT_LIST_HEAD(&hostdata->disconnected);
669
Finn Thainef1081c2014-11-12 16:12:14 +1100670 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Finn Thaina53a21e2014-11-12 16:12:21 +1100672 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100673 hostdata->work_q = alloc_workqueue("ncr5380_%d",
674 WQ_UNBOUND | WQ_MEM_RECLAIM,
675 1, instance->host_no);
676 if (!hostdata->work_q)
677 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Finn Thain8c325132014-11-12 16:11:58 +1100679 prepare_info(instance);
680
Roman Zippelc28bda22007-05-01 22:32:36 +0200681 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
682 NCR5380_write(MODE_REG, MR_BASE);
683 NCR5380_write(TARGET_COMMAND_REG, 0);
684 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Finn Thain2f854b82016-01-03 16:05:22 +1100686 /* Calibrate register polling loop */
687 i = 0;
688 deadline = jiffies + 1;
689 do {
690 cpu_relax();
691 } while (time_is_after_jiffies(deadline));
692 deadline += msecs_to_jiffies(256);
693 do {
694 NCR5380_read(STATUS_REG);
695 ++i;
696 cpu_relax();
697 } while (time_is_after_jiffies(deadline));
698 hostdata->accesses_per_ms = i / 256;
699
Roman Zippelc28bda22007-05-01 22:32:36 +0200700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Finn Thainff50f9e2014-11-12 16:12:18 +1100703/**
Finn Thain636b1ec2016-01-03 16:05:10 +1100704 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
705 * @instance: adapter to check
706 *
707 * If the system crashed, it may have crashed with a connected target and
708 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
709 * currently established nexus, which we know nothing about. Failing that
710 * do a bus reset.
711 *
712 * Note that a bus reset will cause the chip to assert IRQ.
713 *
714 * Returns 0 if successful, otherwise -ENXIO.
715 */
716
717static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
718{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100719 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +1100720 int pass;
721
722 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
723 switch (pass) {
724 case 1:
725 case 3:
726 case 5:
727 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
728 NCR5380_poll_politely(instance,
729 STATUS_REG, SR_BSY, 0, 5 * HZ);
730 break;
731 case 2:
732 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
733 do_abort(instance);
734 break;
735 case 4:
736 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
737 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100738 /* Wait after a reset; the SCSI standard calls for
739 * 250ms, we wait 500ms to be on the safe side.
740 * But some Toshiba CD-ROMs need ten times that.
741 */
742 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
743 msleep(2500);
744 else
745 msleep(500);
Finn Thain636b1ec2016-01-03 16:05:10 +1100746 break;
747 case 6:
748 shost_printk(KERN_ERR, instance, "bus locked solid\n");
749 return -ENXIO;
750 }
751 }
752 return 0;
753}
754
755/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100756 * NCR5380_exit - remove an NCR5380
757 * @instance: adapter to remove
758 *
759 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
760 */
761
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200762static void NCR5380_exit(struct Scsi_Host *instance)
763{
Finn Thaina53a21e2014-11-12 16:12:21 +1100764 struct NCR5380_hostdata *hostdata = shost_priv(instance);
765
766 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100767 destroy_workqueue(hostdata->work_q);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200768}
769
Finn Thainff50f9e2014-11-12 16:12:18 +1100770/**
Finn Thain677e0192016-01-03 16:05:59 +1100771 * complete_cmd - finish processing a command and return it to the SCSI ML
772 * @instance: the host instance
773 * @cmd: command to complete
774 */
775
776static void complete_cmd(struct Scsi_Host *instance,
777 struct scsi_cmnd *cmd)
778{
779 struct NCR5380_hostdata *hostdata = shost_priv(instance);
780
781 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
782
Finn Thainf27db8e2016-01-03 16:06:00 +1100783 if (hostdata->sensing == cmd) {
784 /* Autosense processing ends here */
785 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
786 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
787 set_host_byte(cmd, DID_ERROR);
788 } else
789 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
790 hostdata->sensing = NULL;
791 }
792
Finn Thain677e0192016-01-03 16:05:59 +1100793#ifdef SUPPORT_TAGS
794 cmd_free_tag(cmd);
795#else
796 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
797#endif
798 cmd->scsi_done(cmd);
799}
800
801/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100802 * NCR5380_queue_command - queue a command
803 * @instance: the relevant SCSI adapter
804 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Finn Thain1bb40582016-01-03 16:05:29 +1100806 * cmd is added to the per-instance issue queue, with minor
Finn Thainff50f9e2014-11-12 16:12:18 +1100807 * twiddling done to the host specific fields of cmd. If the
808 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
810
Finn Thain16b29e72014-11-12 16:12:08 +1100811static int NCR5380_queue_command(struct Scsi_Host *instance,
812 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Finn Thain16b29e72014-11-12 16:12:08 +1100814 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100815 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200816 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818#if (NDEBUG & NDEBUG_NO_WRITE)
Roman Zippelc28bda22007-05-01 22:32:36 +0200819 switch (cmd->cmnd[0]) {
820 case WRITE_6:
821 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100822 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Roman Zippelc28bda22007-05-01 22:32:36 +0200823 cmd->result = (DID_ERROR << 16);
Finn Thain16b29e72014-11-12 16:12:08 +1100824 cmd->scsi_done(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +0200825 return 0;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
828
Roman Zippelc28bda22007-05-01 22:32:36 +0200829 cmd->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Roman Zippelc28bda22007-05-01 22:32:36 +0200831 /*
832 * Insert the cmd into the issue queue. Note that REQUEST SENSE
833 * commands are added to the head of the queue since any command will
834 * clear the contingent allegiance condition that exists and the
835 * sense data is only guaranteed to be valid while the condition exists.
836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Roman Zippelc28bda22007-05-01 22:32:36 +0200838 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
839 * Otherwise a running NCR5380_main may steal the lock.
840 * Lock before actually inserting due to fairness reasons explained in
841 * atari_scsi.c. If we insert first, then it's impossible for this driver
842 * to release the lock.
843 * Stop timer for this command while waiting for the lock, or timeouts
844 * may happen (and they really do), and it's no good if the command doesn't
845 * appear in any of the queues.
846 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
847 * because also a timer int can trigger an abort or reset, which would
848 * alter queues and touch the lock.
849 */
Finn Thaine3c3da62014-11-12 16:12:15 +1100850 if (!NCR5380_acquire_dma_irq(instance))
Finn Thain16b29e72014-11-12 16:12:08 +1100851 return SCSI_MLQUEUE_HOST_BUSY;
852
Finn Thain11d2f632016-01-03 16:05:51 +1100853 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain16b29e72014-11-12 16:12:08 +1100854
Finn Thainff50f9e2014-11-12 16:12:18 +1100855 /*
856 * Insert the cmd into the issue queue. Note that REQUEST SENSE
857 * commands are added to the head of the queue since any command will
858 * clear the contingent allegiance condition that exists and the
859 * sense data is only guaranteed to be valid while the condition exists.
860 */
861
Finn Thain32b26a12016-01-03 16:05:58 +1100862 if (cmd->cmnd[0] == REQUEST_SENSE)
863 list_add(&ncmd->list, &hostdata->unissued);
864 else
865 list_add_tail(&ncmd->list, &hostdata->unissued);
866
Finn Thain11d2f632016-01-03 16:05:51 +1100867 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Finn Thaindbb6b352016-01-03 16:05:53 +1100869 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
870 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Finn Thain010e89d2016-01-03 16:05:38 +1100872 /* Kick off command processing */
873 queue_work(hostdata->work_q, &hostdata->main_task);
Roman Zippelc28bda22007-05-01 22:32:36 +0200874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Finn Thaine3c3da62014-11-12 16:12:15 +1100877static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
878{
879 struct NCR5380_hostdata *hostdata = shost_priv(instance);
880
881 /* Caller does the locking needed to set & test these data atomically */
Finn Thain32b26a12016-01-03 16:05:58 +1100882 if (list_empty(&hostdata->disconnected) &&
883 list_empty(&hostdata->unissued) &&
Finn Thainf27db8e2016-01-03 16:06:00 +1100884 list_empty(&hostdata->autosense) &&
Finn Thaine3c3da62014-11-12 16:12:15 +1100885 !hostdata->connected &&
Finn Thain707d62b2016-01-03 16:06:02 +1100886 !hostdata->selecting)
Finn Thaine3c3da62014-11-12 16:12:15 +1100887 NCR5380_release_dma_irq(instance);
888}
889
Finn Thainff50f9e2014-11-12 16:12:18 +1100890/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100891 * dequeue_next_cmd - dequeue a command for processing
892 * @instance: the scsi host instance
893 *
894 * Priority is given to commands on the autosense queue. These commands
895 * need autosense because of a CHECK CONDITION result.
896 *
897 * Returns a command pointer if a command is found for a target that is
898 * not already busy. Otherwise returns NULL.
899 */
900
901static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
902{
903 struct NCR5380_hostdata *hostdata = shost_priv(instance);
904 struct NCR5380_cmd *ncmd;
905 struct scsi_cmnd *cmd;
906
907 if (list_empty(&hostdata->autosense)) {
908 list_for_each_entry(ncmd, &hostdata->unissued, list) {
909 cmd = NCR5380_to_scmd(ncmd);
910 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
911 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
912
913 if (
914#ifdef SUPPORT_TAGS
915 !is_lun_busy(cmd, 1)
916#else
917 !(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))
918#endif
919 ) {
920 list_del(&ncmd->list);
921 dsprintk(NDEBUG_QUEUES, instance,
922 "dequeue: removed %p from issue queue\n", cmd);
923 return cmd;
924 }
925 }
926 } else {
927 /* Autosense processing begins here */
928 ncmd = list_first_entry(&hostdata->autosense,
929 struct NCR5380_cmd, list);
930 list_del(&ncmd->list);
931 cmd = NCR5380_to_scmd(ncmd);
932 dsprintk(NDEBUG_QUEUES, instance,
933 "dequeue: removed %p from autosense queue\n", cmd);
934 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
935 hostdata->sensing = cmd;
936 return cmd;
937 }
938 return NULL;
939}
940
941static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
942{
943 struct NCR5380_hostdata *hostdata = shost_priv(instance);
944 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
945
946 if (hostdata->sensing) {
947 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
948 list_add(&ncmd->list, &hostdata->autosense);
949 hostdata->sensing = NULL;
950 } else
951 list_add(&ncmd->list, &hostdata->unissued);
952}
953
954/**
Finn Thainff50f9e2014-11-12 16:12:18 +1100955 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100957 * NCR5380_main is a coroutine that runs as long as more work can
958 * be done on the NCR5380 host adapters in a system. Both
959 * NCR5380_queue_command() and NCR5380_intr() will try to start it
960 * in case it is not running.
Roman Zippelc28bda22007-05-01 22:32:36 +0200961 *
Finn Thainff50f9e2014-11-12 16:12:18 +1100962 * Locks: called as its own thread with no locks held.
Roman Zippelc28bda22007-05-01 22:32:36 +0200963 */
964
Geert Uytterhoevenb312b382007-05-01 22:32:48 +0200965static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Finn Thaina53a21e2014-11-12 16:12:21 +1100967 struct NCR5380_hostdata *hostdata =
968 container_of(work, struct NCR5380_hostdata, main_task);
969 struct Scsi_Host *instance = hostdata->host;
Finn Thainf27db8e2016-01-03 16:06:00 +1100970 struct scsi_cmnd *cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +0200971 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Roman Zippelc28bda22007-05-01 22:32:36 +0200973 /*
Roman Zippelc28bda22007-05-01 22:32:36 +0200974 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
975 * because also a timer int can trigger an abort or reset, which can
976 * alter queues and touch the Falcon lock.
977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Finn Thain11d2f632016-01-03 16:05:51 +1100979 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +0200980 do {
Roman Zippelc28bda22007-05-01 22:32:36 +0200981 done = 1;
982
Finn Thainf27db8e2016-01-03 16:06:00 +1100983 while (!hostdata->connected &&
984 (cmd = dequeue_next_cmd(instance))) {
985
986 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
987
Roman Zippelc28bda22007-05-01 22:32:36 +0200988 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100989 * Attempt to establish an I_T_L nexus here.
990 * On success, instance->hostdata->connected is set.
991 * On failure, we must add the command back to the
992 * issue queue so we can keep trying.
Roman Zippelc28bda22007-05-01 22:32:36 +0200993 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100994 /*
995 * REQUEST SENSE commands are issued without tagged
996 * queueing, even on SCSI-II devices because the
997 * contingent allegiance condition exists for the
998 * entire unit.
999 */
1000 /* ++roman: ...and the standard also requires that
1001 * REQUEST SENSE command are untagged.
1002 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001003
1004#ifdef SUPPORT_TAGS
Finn Thainf27db8e2016-01-03 16:06:00 +11001005 cmd_get_tag(cmd, cmd->cmnd[0] != REQUEST_SENSE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001006#endif
Finn Thain707d62b2016-01-03 16:06:02 +11001007 cmd = NCR5380_select(instance, cmd);
1008 if (!cmd) {
1009 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +11001010 maybe_release_dma_irq(instance);
1011 } else {
Finn Thainf27db8e2016-01-03 16:06:00 +11001012 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
1013 "main: select failed, returning %p to queue\n", cmd);
1014 requeue_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001015#ifdef SUPPORT_TAGS
Finn Thainf27db8e2016-01-03 16:06:00 +11001016 cmd_free_tag(cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001017#endif
Finn Thainf27db8e2016-01-03 16:06:00 +11001018 }
1019 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001020 if (hostdata->connected
1021#ifdef REAL_DMA
1022 && !hostdata->dma_len
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023#endif
1024 ) {
Finn Thainb7465452016-01-03 16:06:05 +11001025 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001026 NCR5380_information_transfer(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001027 done = 0;
1028 }
1029 } while (!done);
Finn Thain11d2f632016-01-03 16:05:51 +11001030 spin_unlock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033
1034#ifdef REAL_DMA
1035/*
1036 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1037 *
1038 * Purpose : Called by interrupt handler when DMA finishes or a phase
Roman Zippelc28bda22007-05-01 22:32:36 +02001039 * mismatch occurs (which would finish the DMA transfer).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 *
1041 * Inputs : instance - this instance of the NCR5380.
1042 *
1043 */
1044
Roman Zippelc28bda22007-05-01 22:32:36 +02001045static void NCR5380_dma_complete(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Finn Thaine8a60142016-01-03 16:05:54 +11001047 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain01bd9082014-11-12 16:12:23 +11001048 int transferred;
Finn Thaine3f463b2014-11-12 16:12:16 +11001049 unsigned char **data;
Finn Thain5299b3c2016-01-03 16:05:57 +11001050 int *count;
Finn Thaine3f463b2014-11-12 16:12:16 +11001051 int saved_data = 0, overrun = 0;
1052 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Finn Thainef1081c2014-11-12 16:12:14 +11001054 if (hostdata->read_overruns) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001055 p = hostdata->connected->SCp.phase;
1056 if (p & SR_IO) {
1057 udelay(10);
1058 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1059 (BASR_PHASE_MATCH|BASR_ACK)) ==
1060 (BASR_PHASE_MATCH|BASR_ACK)) {
1061 saved_data = NCR5380_read(INPUT_DATA_REG);
1062 overrun = 1;
Finn Thainb7465452016-01-03 16:06:05 +11001063 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001064 }
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001067
Finn Thaine3f463b2014-11-12 16:12:16 +11001068#if defined(CONFIG_SUN3)
1069 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1070 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1071 instance->host_no);
1072 BUG();
1073 }
1074
1075 /* make sure we're not stuck in a data phase */
1076 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1077 (BASR_PHASE_MATCH | BASR_ACK)) {
1078 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1079 NCR5380_read(BUS_AND_STATUS_REG));
1080 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1081 instance->host_no);
1082 BUG();
1083 }
1084#endif
1085
Roman Zippelc28bda22007-05-01 22:32:36 +02001086 NCR5380_write(MODE_REG, MR_BASE);
1087 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001088 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001089
Finn Thain01bd9082014-11-12 16:12:23 +11001090 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001091 hostdata->dma_len = 0;
1092
1093 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1094 count = &hostdata->connected->SCp.this_residual;
Finn Thain01bd9082014-11-12 16:12:23 +11001095 *data += transferred;
1096 *count -= transferred;
Roman Zippelc28bda22007-05-01 22:32:36 +02001097
Finn Thainef1081c2014-11-12 16:12:14 +11001098 if (hostdata->read_overruns) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001099 int cnt, toPIO;
1100
Roman Zippelc28bda22007-05-01 22:32:36 +02001101 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
Finn Thainef1081c2014-11-12 16:12:14 +11001102 cnt = toPIO = hostdata->read_overruns;
Roman Zippelc28bda22007-05-01 22:32:36 +02001103 if (overrun) {
Finn Thaind65e6342014-03-18 11:42:20 +11001104 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001105 *(*data)++ = saved_data;
1106 (*count)--;
1107 cnt--;
1108 toPIO--;
1109 }
Finn Thaind65e6342014-03-18 11:42:20 +11001110 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
Roman Zippelc28bda22007-05-01 22:32:36 +02001111 NCR5380_transfer_pio(instance, &p, &cnt, data);
1112 *count -= toPIO - cnt;
1113 }
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116#endif /* REAL_DMA */
1117
1118
Finn Thainff50f9e2014-11-12 16:12:18 +11001119/**
1120 * NCR5380_intr - generic NCR5380 irq handler
1121 * @irq: interrupt number
1122 * @dev_id: device info
Roman Zippelc28bda22007-05-01 22:32:36 +02001123 *
Finn Thainff50f9e2014-11-12 16:12:18 +11001124 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1125 * from the disconnected queue, and restarting NCR5380_main()
1126 * as required.
Finn Thaincd400822016-01-03 16:05:40 +11001127 *
1128 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1129 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1130 * Three of these six conditions are latched in the Bus and Status Register:
1131 * - End of DMA (cleared by ending DMA Mode)
1132 * - Parity error (cleared by reading RPIR)
1133 * - Loss of BSY (cleared by reading RPIR)
1134 * Two conditions have flag bits that are not latched:
1135 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1136 * - Bus reset (non-maskable)
1137 * The remaining condition has no flag bit at all:
1138 * - Selection/reselection
1139 *
1140 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1141 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1142 * claimed that "the design of the [DP8490] interrupt logic ensures
1143 * interrupts will not be lost (they can be on the DP5380)."
1144 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1145 *
1146 * Checking for bus reset by reading RST is futile because of interrupt
1147 * latency, but a bus reset will reset chip logic. Checking for parity error
1148 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1149 * condition will clear DMA Mode. We can tell when this occurs because the
1150 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
1152
Roman Zippelc28bda22007-05-01 22:32:36 +02001153static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Finn Thaina53a21e2014-11-12 16:12:21 +11001155 struct Scsi_Host *instance = dev_id;
Finn Thain010e89d2016-01-03 16:05:38 +11001156 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001157 int handled = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +02001158 unsigned char basr;
Finn Thain11d2f632016-01-03 16:05:51 +11001159 unsigned long flags;
1160
1161 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Roman Zippelc28bda22007-05-01 22:32:36 +02001163 basr = NCR5380_read(BUS_AND_STATUS_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001164 if (basr & BASR_IRQ) {
Finn Thaincd400822016-01-03 16:05:40 +11001165 unsigned char mr = NCR5380_read(MODE_REG);
1166 unsigned char sr = NCR5380_read(STATUS_REG);
1167
Finn Thainb7465452016-01-03 16:06:05 +11001168 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1169 irq, basr, sr, mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001172 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1173 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1174 * We ack IRQ after clearing Mode Register. Workarounds
1175 * for End of DMA errata need to happen in DMA Mode.
Roman Zippelc28bda22007-05-01 22:32:36 +02001176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Finn Thainb7465452016-01-03 16:06:05 +11001178 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001179
Finn Thaincd400822016-01-03 16:05:40 +11001180 if (hostdata->connected) {
1181 NCR5380_dma_complete(instance);
1182 queue_work(hostdata->work_q, &hostdata->main_task);
1183 } else {
1184 NCR5380_write(MODE_REG, MR_BASE);
1185 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Roman Zippelc28bda22007-05-01 22:32:36 +02001186 }
Finn Thaincd400822016-01-03 16:05:40 +11001187 } else
1188#endif /* REAL_DMA */
1189 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1190 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1191 /* Probably reselected */
1192 NCR5380_write(SELECT_ENABLE_REG, 0);
1193 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1194
Finn Thainb7465452016-01-03 16:06:05 +11001195 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +11001196
1197 if (!hostdata->connected) {
1198 NCR5380_reselect(instance);
1199 queue_work(hostdata->work_q, &hostdata->main_task);
1200 }
1201 if (!hostdata->connected)
1202 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1203 } else {
1204 /* Probably Bus Reset */
1205 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1206
Finn Thainb7465452016-01-03 16:06:05 +11001207 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +11001208#ifdef SUN3_SCSI_VME
1209 dregs->csr |= CSR_DMA_ENABLE;
1210#endif
1211 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001212 handled = 1;
Finn Thaincd400822016-01-03 16:05:40 +11001213 } else {
1214 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine3f463b2014-11-12 16:12:16 +11001215#ifdef SUN3_SCSI_VME
1216 dregs->csr |= CSR_DMA_ENABLE;
1217#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001218 }
1219
Finn Thain11d2f632016-01-03 16:05:51 +11001220 spin_unlock_irqrestore(&hostdata->lock, flags);
1221
Roman Zippelc28bda22007-05-01 22:32:36 +02001222 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Roman Zippelc28bda22007-05-01 22:32:36 +02001225/*
Finn Thain710ddd02014-11-12 16:12:02 +11001226 * Function : int NCR5380_select(struct Scsi_Host *instance,
1227 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 *
1229 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Roman Zippelc28bda22007-05-01 22:32:36 +02001230 * including ARBITRATION, SELECTION, and initial message out for
1231 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001233 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001234 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 *
Finn Thain707d62b2016-01-03 16:06:02 +11001236 * Returns cmd if selection failed but should be retried,
1237 * NULL if selection failed and should not be retried, or
1238 * NULL if selection succeeded (hostdata->connected == cmd).
Roman Zippelc28bda22007-05-01 22:32:36 +02001239 *
1240 * Side effects :
1241 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * with registers as they should have been on entry - ie
1243 * SELECT_ENABLE will be set appropriately, the NCR5380
1244 * will cease to drive any SCSI bus signals.
1245 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001246 * If successful : I_T_L or I_T_L_Q nexus will be established,
1247 * instance->connected will be set to cmd.
1248 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001250 * If failed (no target) : cmd->scsi_done() will be called, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * cmd->result host byte set to DID_BAD_TARGET.
1252 */
1253
Finn Thain707d62b2016-01-03 16:06:02 +11001254static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1255 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Finn Thaine8a60142016-01-03 16:05:54 +11001257 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001258 unsigned char tmp[3], phase;
1259 unsigned char *data;
1260 int len;
Finn Thainae753a32016-01-03 16:05:23 +11001261 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Finn Thain8ad3a592014-03-18 11:42:19 +11001263 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +11001264 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1265 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Roman Zippelc28bda22007-05-01 22:32:36 +02001267 /*
Finn Thain707d62b2016-01-03 16:06:02 +11001268 * Arbitration and selection phases are slow and involve dropping the
1269 * lock, so we have to watch out for EH. An exception handler may
1270 * change 'selecting' to NULL. This function will then return NULL
1271 * so that the caller will forget about 'cmd'. (During information
1272 * transfer phases, EH may change 'connected' to NULL.)
1273 */
1274 hostdata->selecting = cmd;
1275
1276 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001277 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1278 * data bus during SELECTION.
1279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Roman Zippelc28bda22007-05-01 22:32:36 +02001281 NCR5380_write(TARGET_COMMAND_REG, 0);
1282
1283 /*
1284 * Start arbitration.
1285 */
1286
1287 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1288 NCR5380_write(MODE_REG, MR_ARBITRATE);
1289
Finn Thain55500d92016-01-03 16:05:35 +11001290 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1291 * Bus Free Delay, arbitration will begin.
Roman Zippelc28bda22007-05-01 22:32:36 +02001292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Finn Thain11d2f632016-01-03 16:05:51 +11001294 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001295 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1296 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1297 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001298 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001299 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1300 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001301 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001302 }
1303 if (err < 0) {
1304 NCR5380_write(MODE_REG, MR_BASE);
1305 shost_printk(KERN_ERR, instance,
1306 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001307 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001308 }
Finn Thain11d2f632016-01-03 16:05:51 +11001309 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001310
1311 /* The SCSI-2 arbitration delay is 2.4 us */
Roman Zippelc28bda22007-05-01 22:32:36 +02001312 udelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Roman Zippelc28bda22007-05-01 22:32:36 +02001314 /* Check for lost arbitration */
1315 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1316 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
Finn Thain11d2f632016-01-03 16:05:51 +11001317 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001318 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001319 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001320 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001321 goto out;
Roman Zippelc28bda22007-05-01 22:32:36 +02001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Finn Thaincf13b082016-01-03 16:05:18 +11001324 /* After/during arbitration, BSY should be asserted.
1325 * IBM DPES-31080 Version S31Q works now
1326 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1327 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001328 NCR5380_write(INITIATOR_COMMAND_REG,
1329 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Roman Zippelc28bda22007-05-01 22:32:36 +02001331 /*
1332 * Again, bus clear + bus settle time is 1.2us, however, this is
1333 * a minimum so we'll udelay ceil(1.2)
1334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Finn Thain9c3f0e22016-01-03 16:05:11 +11001336 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1337 udelay(15);
1338 else
1339 udelay(2);
Roman Zippelc28bda22007-05-01 22:32:36 +02001340
Finn Thain11d2f632016-01-03 16:05:51 +11001341 spin_lock_irq(&hostdata->lock);
1342
Finn Thain72064a72016-01-03 16:05:44 +11001343 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1344 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001345 goto out;
1346
1347 if (!hostdata->selecting) {
1348 NCR5380_write(MODE_REG, MR_BASE);
1349 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1350 goto out;
1351 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001352
Finn Thainb7465452016-01-03 16:06:05 +11001353 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001354
1355 /*
1356 * Now that we have won arbitration, start Selection process, asserting
1357 * the host and target ID's on the SCSI bus.
1358 */
1359
1360 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1361
1362 /*
1363 * Raise ATN while SEL is true before BSY goes false from arbitration,
1364 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1365 * phase immediately after selection.
1366 */
1367
1368 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1369 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Roman Zippelc28bda22007-05-01 22:32:36 +02001372 /*
1373 * Reselect interrupts must be turned off prior to the dropping of BSY,
1374 * otherwise we will trigger an interrupt.
1375 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001376 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Finn Thain11d2f632016-01-03 16:05:51 +11001378 spin_unlock_irq(&hostdata->lock);
1379
Roman Zippelc28bda22007-05-01 22:32:36 +02001380 /*
1381 * The initiator shall then wait at least two deskew delays and release
1382 * the BSY signal.
1383 */
1384 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Roman Zippelc28bda22007-05-01 22:32:36 +02001386 /* Reset BSY */
1387 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1388 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Roman Zippelc28bda22007-05-01 22:32:36 +02001390 /*
1391 * Something weird happens when we cease to drive BSY - looks
1392 * like the board/chip is letting us do another read before the
1393 * appropriate propagation delay has expired, and we're confusing
1394 * a BSY signal from ourselves as the target's response to SELECTION.
1395 *
1396 * A small delay (the 'C++' frontend breaks the pipeline with an
1397 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1398 * tighter 'C' code breaks and requires this) solves the problem -
1399 * the 1 us delay is arbitrary, and only used because this delay will
1400 * be the same on other platforms and since it works here, it should
1401 * work there.
1402 *
1403 * wingel suggests that this could be due to failing to wait
1404 * one deskew delay.
1405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Roman Zippelc28bda22007-05-01 22:32:36 +02001407 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Finn Thainb7465452016-01-03 16:06:05 +11001409 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Roman Zippelc28bda22007-05-01 22:32:36 +02001411 /*
1412 * The SCSI specification calls for a 250 ms timeout for the actual
1413 * selection.
1414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Finn Thainae753a32016-01-03 16:05:23 +11001416 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1417 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Roman Zippelc28bda22007-05-01 22:32:36 +02001419 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001420 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001421 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1422 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001423 if (!hostdata->connected)
1424 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001425 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001426 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001428
Finn Thainae753a32016-01-03 16:05:23 +11001429 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001430 spin_lock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02001431 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02001432 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001433 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1434 if (hostdata->selecting) {
1435 cmd->result = DID_BAD_TARGET << 16;
1436 complete_cmd(instance, cmd);
1437 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1438 cmd = NULL;
1439 }
1440 goto out;
Roman Zippelc28bda22007-05-01 22:32:36 +02001441 }
1442
Roman Zippelc28bda22007-05-01 22:32:36 +02001443 /*
Finn Thainae753a32016-01-03 16:05:23 +11001444 * No less than two deskew delays after the initiator detects the
1445 * BSY signal is true, it shall release the SEL signal and may
1446 * change the DATA BUS. -wingel
1447 */
1448
1449 udelay(1);
1450
1451 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1452
1453 /*
Roman Zippelc28bda22007-05-01 22:32:36 +02001454 * Since we followed the SCSI spec, and raised ATN while SEL
1455 * was true but before BSY was false during selection, the information
1456 * transfer phase should be a MESSAGE OUT phase so that we can send the
1457 * IDENTIFY message.
1458 *
1459 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1460 * message (2 bytes) with a tag ID that we increment with every command
1461 * until it wraps back to 0.
1462 *
1463 * XXX - it turns out that there are some broken SCSI-II devices,
1464 * which claim to support tagged queuing but fail when more than
1465 * some number of commands are issued at once.
1466 */
1467
1468 /* Wait for start of REQ/ACK handshake */
Finn Thain55500d92016-01-03 16:05:35 +11001469
1470 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001471 spin_lock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001472 if (err < 0) {
1473 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1474 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1475 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001476 goto out;
1477 }
1478 if (!hostdata->selecting) {
1479 do_abort(instance);
1480 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001481 }
Roman Zippelc28bda22007-05-01 22:32:36 +02001482
Finn Thainb7465452016-01-03 16:06:05 +11001483 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1484 scmd_id(cmd));
Roman Zippelc28bda22007-05-01 22:32:36 +02001485 tmp[0] = IDENTIFY(1, cmd->device->lun);
1486
1487#ifdef SUPPORT_TAGS
1488 if (cmd->tag != TAG_NONE) {
1489 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1490 tmp[2] = cmd->tag;
1491 len = 3;
1492 } else
1493 len = 1;
1494#else
1495 len = 1;
1496 cmd->tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497#endif /* SUPPORT_TAGS */
1498
Roman Zippelc28bda22007-05-01 22:32:36 +02001499 /* Send message(s) */
1500 data = tmp;
1501 phase = PHASE_MSGOUT;
1502 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001503 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001504 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001505
Roman Zippelc28bda22007-05-01 22:32:36 +02001506 hostdata->connected = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507#ifndef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02001508 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1509#endif
Finn Thaine3f463b2014-11-12 16:12:16 +11001510#ifdef SUN3_SCSI_VME
1511 dregs->csr |= CSR_INTR;
1512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Roman Zippelc28bda22007-05-01 22:32:36 +02001514 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Finn Thain707d62b2016-01-03 16:06:02 +11001516 cmd = NULL;
1517
1518out:
1519 if (!hostdata->selecting)
1520 return NULL;
1521 hostdata->selecting = NULL;
1522 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Roman Zippelc28bda22007-05-01 22:32:36 +02001525/*
1526 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 * unsigned char *phase, int *count, unsigned char **data)
1528 *
1529 * Purpose : transfers data in given phase using polled I/O
1530 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001531 * Inputs : instance - instance of driver, *phase - pointer to
1532 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001534 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001536 * maximum number of bytes, 0 if all bytes are transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * is in same phase.
1538 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001539 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *
1541 * XXX Note : handling for bus free may be useful.
1542 */
1543
1544/*
Roman Zippelc28bda22007-05-01 22:32:36 +02001545 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * IS 100% reliable, and for the actual data transfer where speed
1547 * counts, we will always do a pseudo DMA or DMA transfer.
1548 */
1549
Roman Zippelc28bda22007-05-01 22:32:36 +02001550static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1551 unsigned char *phase, int *count,
1552 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Roman Zippelc28bda22007-05-01 22:32:36 +02001554 register unsigned char p = *phase, tmp;
1555 register int c = *count;
1556 register unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Roman Zippelc28bda22007-05-01 22:32:36 +02001558 /*
1559 * The NCR5380 chip will only drive the SCSI bus when the
1560 * phase specified in the appropriate bits of the TARGET COMMAND
1561 * REGISTER match the STATUS REGISTER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 */
1563
Roman Zippelc28bda22007-05-01 22:32:36 +02001564 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Roman Zippelc28bda22007-05-01 22:32:36 +02001566 do {
1567 /*
1568 * Wait for assertion of REQ, after which the phase bits will be
1569 * valid
1570 */
Finn Thain686f3992016-01-03 16:05:26 +11001571
1572 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Finn Thainb7465452016-01-03 16:06:05 +11001575 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Roman Zippelc28bda22007-05-01 22:32:36 +02001577 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001578 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001579 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11001580 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001581 break;
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Roman Zippelc28bda22007-05-01 22:32:36 +02001584 /* Do actual transfer from SCSI bus to / from memory */
1585 if (!(p & SR_IO))
1586 NCR5380_write(OUTPUT_DATA_REG, *d);
1587 else
1588 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Roman Zippelc28bda22007-05-01 22:32:36 +02001590 ++d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Roman Zippelc28bda22007-05-01 22:32:36 +02001592 /*
1593 * The SCSI standard suggests that in MSGOUT phase, the initiator
1594 * should drop ATN on the last byte of the message phase
1595 * after REQ has been asserted for the handshake but before
1596 * the initiator raises ACK.
1597 */
1598
1599 if (!(p & SR_IO)) {
1600 if (!((p & SR_MSG) && c > 1)) {
1601 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thain8ad3a592014-03-18 11:42:19 +11001602 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001603 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1604 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1605 } else {
1606 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1607 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Finn Thain8ad3a592014-03-18 11:42:19 +11001608 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001609 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1610 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1611 }
1612 } else {
Finn Thain8ad3a592014-03-18 11:42:19 +11001613 NCR5380_dprint(NDEBUG_PIO, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001614 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1615 }
1616
Finn Thaina2edc4a2016-01-03 16:05:27 +11001617 if (NCR5380_poll_politely(instance,
1618 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1619 break;
Roman Zippelc28bda22007-05-01 22:32:36 +02001620
Finn Thainb7465452016-01-03 16:06:05 +11001621 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001622
1623 /*
1624 * We have several special cases to consider during REQ/ACK handshaking :
1625 * 1. We were in MSGOUT phase, and we are on the last byte of the
1626 * message. ATN must be dropped as ACK is dropped.
1627 *
1628 * 2. We are in a MSGIN phase, and we are on the last byte of the
1629 * message. We must exit with ACK asserted, so that the calling
1630 * code may raise ATN before dropping ACK to reject the message.
1631 *
1632 * 3. ACK and ATN are clear and the target may proceed as normal.
1633 */
1634 if (!(p == PHASE_MSGIN && c == 1)) {
1635 if (p == PHASE_MSGOUT && c > 1)
1636 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1637 else
1638 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1639 }
1640 } while (--c);
1641
Finn Thainb7465452016-01-03 16:06:05 +11001642 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001643
1644 *count = c;
1645 *data = d;
1646 tmp = NCR5380_read(STATUS_REG);
1647 /* The phase read from the bus is valid if either REQ is (already)
Finn Thaina2edc4a2016-01-03 16:05:27 +11001648 * asserted or if ACK hasn't been released yet. The latter applies if
1649 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
Roman Zippelc28bda22007-05-01 22:32:36 +02001650 */
Finn Thaina2edc4a2016-01-03 16:05:27 +11001651 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Roman Zippelc28bda22007-05-01 22:32:36 +02001652 *phase = tmp & PHASE_MASK;
1653 else
1654 *phase = PHASE_UNKNOWN;
1655
1656 if (!c || (*phase == p))
1657 return 0;
1658 else
1659 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Finn Thain636b1ec2016-01-03 16:05:10 +11001662/**
1663 * do_reset - issue a reset command
1664 * @instance: adapter to reset
1665 *
1666 * Issue a reset sequence to the NCR5380 and try and get the bus
1667 * back into sane shape.
1668 *
1669 * This clears the reset interrupt flag because there may be no handler for
1670 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1671 * been installed. And when in EH we may have released the ST DMA interrupt.
1672 */
1673
1674static void do_reset(struct Scsi_Host *instance)
1675{
1676 unsigned long flags;
1677
1678 local_irq_save(flags);
1679 NCR5380_write(TARGET_COMMAND_REG,
1680 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1681 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1682 udelay(50);
1683 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1684 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1685 local_irq_restore(flags);
1686}
1687
Finn Thain80d3eb62016-01-03 16:05:34 +11001688/**
1689 * do_abort - abort the currently established nexus by going to
1690 * MESSAGE OUT phase and sending an ABORT message.
1691 * @instance: relevant scsi host instance
Roman Zippelc28bda22007-05-01 22:32:36 +02001692 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001693 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
1695
Finn Thaina53a21e2014-11-12 16:12:21 +11001696static int do_abort(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Roman Zippelc28bda22007-05-01 22:32:36 +02001698 unsigned char tmp, *msgptr, phase;
1699 int len;
Finn Thain80d3eb62016-01-03 16:05:34 +11001700 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Roman Zippelc28bda22007-05-01 22:32:36 +02001702 /* Request message out phase */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Roman Zippelc28bda22007-05-01 22:32:36 +02001705 /*
1706 * Wait for the target to indicate a valid phase by asserting
1707 * REQ. Once this happens, we'll have either a MSGOUT phase
1708 * and can immediately send the ABORT message, or we'll have some
1709 * other phase and will have to source/sink data.
1710 *
1711 * We really don't care what value was on the bus or what value
1712 * the target sees, so we just handshake.
1713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Finn Thain80d3eb62016-01-03 16:05:34 +11001715 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1716 if (rc < 0)
1717 goto timeout;
1718
1719 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Roman Zippelc28bda22007-05-01 22:32:36 +02001720
1721 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1722
Finn Thain80d3eb62016-01-03 16:05:34 +11001723 if (tmp != PHASE_MSGOUT) {
1724 NCR5380_write(INITIATOR_COMMAND_REG,
1725 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1726 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1727 if (rc < 0)
1728 goto timeout;
Roman Zippelc28bda22007-05-01 22:32:36 +02001729 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1730 }
1731
1732 tmp = ABORT;
1733 msgptr = &tmp;
1734 len = 1;
1735 phase = PHASE_MSGOUT;
Finn Thaina53a21e2014-11-12 16:12:21 +11001736 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Roman Zippelc28bda22007-05-01 22:32:36 +02001737
1738 /*
1739 * If we got here, and the command completed successfully,
1740 * we're about to go into bus free state.
1741 */
1742
1743 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001744
1745timeout:
1746 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1747 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001751/*
1752 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * unsigned char *phase, int *count, unsigned char **data)
1754 *
1755 * Purpose : transfers data in given phase using either real
1756 * or pseudo DMA.
1757 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001758 * Inputs : instance - instance of driver, *phase - pointer to
1759 * what phase is expected, *count - pointer to number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * bytes to transfer, **data - pointer to data pointer.
Roman Zippelc28bda22007-05-01 22:32:36 +02001761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001763 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * is in same phase.
1765 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001766 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 *
1768 */
1769
1770
Roman Zippelc28bda22007-05-01 22:32:36 +02001771static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1772 unsigned char *phase, int *count,
1773 unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Finn Thaine8a60142016-01-03 16:05:54 +11001775 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001776 register int c = *count;
1777 register unsigned char p = *phase;
Finn Thaine3f463b2014-11-12 16:12:16 +11001778
1779#if defined(CONFIG_SUN3)
1780 /* sanity check */
1781 if (!sun3_dma_setup_done) {
1782 pr_err("scsi%d: transfer_dma without setup!\n",
1783 instance->host_no);
1784 BUG();
1785 }
1786 hostdata->dma_len = c;
1787
Finn Thainb7465452016-01-03 16:06:05 +11001788 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1789 (p & SR_IO) ? "receive" : "send", c, *data);
Finn Thaine3f463b2014-11-12 16:12:16 +11001790
1791 /* netbsd turns off ints here, why not be safe and do it too */
Finn Thaine3f463b2014-11-12 16:12:16 +11001792
1793 /* send start chain */
1794 sun3scsi_dma_start(c, *data);
1795
Finn Thaincd400822016-01-03 16:05:40 +11001796 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1797 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1798 MR_ENABLE_EOP_INTR);
Finn Thaine3f463b2014-11-12 16:12:16 +11001799 if (p & SR_IO) {
Finn Thaine3f463b2014-11-12 16:12:16 +11001800 NCR5380_write(INITIATOR_COMMAND_REG, 0);
Finn Thaine3f463b2014-11-12 16:12:16 +11001801 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1802 } else {
Finn Thaine3f463b2014-11-12 16:12:16 +11001803 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
Finn Thaine3f463b2014-11-12 16:12:16 +11001804 NCR5380_write(START_DMA_SEND_REG, 0);
1805 }
1806
1807#ifdef SUN3_SCSI_VME
1808 dregs->csr |= CSR_DMA_ENABLE;
1809#endif
1810
Finn Thaine3f463b2014-11-12 16:12:16 +11001811 sun3_dma_active = 1;
1812
1813#else /* !defined(CONFIG_SUN3) */
Roman Zippelc28bda22007-05-01 22:32:36 +02001814 register unsigned char *d = *data;
1815 unsigned char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Roman Zippelc28bda22007-05-01 22:32:36 +02001817 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1818 *phase = tmp;
1819 return -1;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Finn Thainef1081c2014-11-12 16:12:14 +11001822 if (hostdata->read_overruns && (p & SR_IO))
1823 c -= hostdata->read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Finn Thainb7465452016-01-03 16:06:05 +11001825 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1826 (p & SR_IO) ? "receive" : "send", c, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Roman Zippelc28bda22007-05-01 22:32:36 +02001828 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thaincd400822016-01-03 16:05:40 +11001829 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1830 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Finn Thainef1081c2014-11-12 16:12:14 +11001832 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001833 /* On the Medusa, it is a must to initialize the DMA before
1834 * starting the NCR. This is also the cleaner way for the TT.
1835 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001836 hostdata->dma_len = (p & SR_IO) ?
1837 NCR5380_dma_read_setup(instance, d, c) :
1838 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Roman Zippelc28bda22007-05-01 22:32:36 +02001841 if (p & SR_IO)
1842 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1843 else {
1844 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1845 NCR5380_write(START_DMA_SEND_REG, 0);
1846 }
1847
Finn Thainef1081c2014-11-12 16:12:14 +11001848 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
Roman Zippelc28bda22007-05-01 22:32:36 +02001849 /* On the Falcon, the DMA setup must be done after the last */
1850 /* NCR access, else the DMA setup gets trashed!
1851 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001852 hostdata->dma_len = (p & SR_IO) ?
1853 NCR5380_dma_read_setup(instance, d, c) :
1854 NCR5380_dma_write_setup(instance, d, c);
Roman Zippelc28bda22007-05-01 22:32:36 +02001855 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001856#endif /* !defined(CONFIG_SUN3) */
1857
Roman Zippelc28bda22007-05-01 22:32:36 +02001858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860#endif /* defined(REAL_DMA) */
1861
1862/*
1863 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1864 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001865 * Purpose : run through the various SCSI phases and do as the target
1866 * directs us to. Operates on the currently connected command,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * instance->connected.
1868 *
1869 * Inputs : instance, instance for which we are doing commands
1870 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001871 * Side effects : SCSI things happen, the disconnected queue will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * modified if a command disconnects, *instance->connected will
1873 * change.
1874 *
Roman Zippelc28bda22007-05-01 22:32:36 +02001875 * XXX Note : we need to watch for bus free or a reset condition here
1876 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 */
Roman Zippelc28bda22007-05-01 22:32:36 +02001878
1879static void NCR5380_information_transfer(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Finn Thaine8a60142016-01-03 16:05:54 +11001881 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02001882 unsigned char msgout = NOP;
1883 int sink = 0;
1884 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885#if defined(REAL_DMA)
Roman Zippelc28bda22007-05-01 22:32:36 +02001886 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02001888 unsigned char *data;
1889 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001890 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Finn Thaine3f463b2014-11-12 16:12:16 +11001892#ifdef SUN3_SCSI_VME
1893 dregs->csr |= CSR_INTR;
1894#endif
1895
Finn Thain11d2f632016-01-03 16:05:51 +11001896 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001897 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1898
Roman Zippelc28bda22007-05-01 22:32:36 +02001899 tmp = NCR5380_read(STATUS_REG);
1900 /* We only have a valid SCSI phase when REQ is asserted */
1901 if (tmp & SR_REQ) {
1902 phase = (tmp & PHASE_MASK);
1903 if (phase != old_phase) {
1904 old_phase = phase;
Finn Thain8ad3a592014-03-18 11:42:19 +11001905 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Finn Thaine3f463b2014-11-12 16:12:16 +11001907#if defined(CONFIG_SUN3)
1908 if (phase == PHASE_CMDOUT) {
1909#if defined(REAL_DMA)
1910 void *d;
1911 unsigned long count;
1912
1913 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1914 count = cmd->SCp.buffer->length;
1915 d = sg_virt(cmd->SCp.buffer);
1916 } else {
1917 count = cmd->SCp.this_residual;
1918 d = cmd->SCp.ptr;
1919 }
1920 /* this command setup for dma yet? */
1921 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
1922 if (cmd->request->cmd_type == REQ_TYPE_FS) {
Finn Thaincd461402016-01-03 16:06:06 +11001923 sun3scsi_dma_setup(instance, d, count,
Finn Thaine3f463b2014-11-12 16:12:16 +11001924 rq_data_dir(cmd->request));
1925 sun3_dma_setup_done = cmd;
1926 }
1927 }
1928#endif
1929#ifdef SUN3_SCSI_VME
1930 dregs->csr |= CSR_INTR;
1931#endif
1932 }
1933#endif /* CONFIG_SUN3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Roman Zippelc28bda22007-05-01 22:32:36 +02001935 if (sink && (phase != PHASE_MSGOUT)) {
1936 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Roman Zippelc28bda22007-05-01 22:32:36 +02001938 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1939 ICR_ASSERT_ACK);
1940 while (NCR5380_read(STATUS_REG) & SR_REQ)
1941 ;
1942 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1943 ICR_ASSERT_ATN);
1944 sink = 0;
1945 continue;
1946 }
1947
1948 switch (phase) {
1949 case PHASE_DATAOUT:
1950#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001951 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02001952 sink = 1;
1953 do_abort(instance);
1954 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001955 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02001956 return;
1957#endif
1958 case PHASE_DATAIN:
1959 /*
1960 * If there is no room left in the current buffer in the
1961 * scatter-gather list, move onto the next one.
1962 */
1963
1964 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1965 ++cmd->SCp.buffer;
1966 --cmd->SCp.buffers_residual;
1967 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001968 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Roman Zippelc28bda22007-05-01 22:32:36 +02001969 /* ++roman: Try to merge some scatter-buffers if
1970 * they are at contiguous physical addresses.
1971 */
1972 merge_contiguous_buffers(cmd);
Finn Thainb7465452016-01-03 16:06:05 +11001973 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1974 cmd->SCp.this_residual,
1975 cmd->SCp.buffers_residual);
Roman Zippelc28bda22007-05-01 22:32:36 +02001976 }
1977
1978 /*
1979 * The preferred transfer method is going to be
1980 * PSEUDO-DMA for systems that are strictly PIO,
1981 * since we can let the hardware do the handshaking.
1982 *
1983 * For this to work, we need to know the transfersize
1984 * ahead of time, since the pseudo-DMA code will sit
1985 * in an unconditional loop.
1986 */
1987
1988 /* ++roman: I suggest, this should be
1989 * #if def(REAL_DMA)
1990 * instead of leaving REAL_DMA out.
1991 */
1992
1993#if defined(REAL_DMA)
Finn Thaine3f463b2014-11-12 16:12:16 +11001994#if !defined(CONFIG_SUN3)
Finn Thainff3d4572016-01-03 16:05:25 +11001995 transfersize = 0;
1996 if (!cmd->device->borken)
Finn Thaine3f463b2014-11-12 16:12:16 +11001997#endif
Finn Thainff3d4572016-01-03 16:05:25 +11001998 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1999
2000 if (transfersize >= DMA_MIN_SIZE) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002001 len = transfersize;
2002 cmd->SCp.phase = phase;
2003 if (NCR5380_transfer_dma(instance, &phase,
2004 &len, (unsigned char **)&cmd->SCp.ptr)) {
2005 /*
2006 * If the watchdog timer fires, all future
2007 * accesses to this device will use the
2008 * polled-IO. */
Finn Thainff50f9e2014-11-12 16:12:18 +11002009 scmd_printk(KERN_INFO, cmd,
2010 "switching to slow handshake\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002011 cmd->device->borken = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +02002012 sink = 1;
2013 do_abort(instance);
2014 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002015 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002016 /* XXX - need to source or sink data here, as appropriate */
2017 } else {
2018#ifdef REAL_DMA
2019 /* ++roman: When using real DMA,
2020 * information_transfer() should return after
2021 * starting DMA since it has nothing more to
2022 * do.
2023 */
2024 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002026 cmd->SCp.this_residual -= transfersize - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002028 }
2029 } else
2030#endif /* defined(REAL_DMA) */
Finn Thain11d2f632016-01-03 16:05:51 +11002031 {
2032 spin_unlock_irq(&hostdata->lock);
Roman Zippelc28bda22007-05-01 22:32:36 +02002033 NCR5380_transfer_pio(instance, &phase,
2034 (int *)&cmd->SCp.this_residual,
2035 (unsigned char **)&cmd->SCp.ptr);
Finn Thain11d2f632016-01-03 16:05:51 +11002036 spin_lock_irq(&hostdata->lock);
2037 }
Finn Thaine3f463b2014-11-12 16:12:16 +11002038#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2039 /* if we had intended to dma that command clear it */
2040 if (sun3_dma_setup_done == cmd)
2041 sun3_dma_setup_done = NULL;
2042#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002043 break;
2044 case PHASE_MSGIN:
2045 len = 1;
2046 data = &tmp;
Roman Zippelc28bda22007-05-01 22:32:36 +02002047 NCR5380_transfer_pio(instance, &phase, &len, &data);
2048 cmd->SCp.Message = tmp;
2049
2050 switch (tmp) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002051 case ABORT:
2052 case COMMAND_COMPLETE:
2053 /* Accept message by clearing ACK */
2054 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002055 dsprintk(NDEBUG_QUEUES, instance,
2056 "COMMAND COMPLETE %p target %d lun %llu\n",
2057 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thaine3c3da62014-11-12 16:12:15 +11002058
Finn Thaine3c3da62014-11-12 16:12:15 +11002059 hostdata->connected = NULL;
Roman Zippelc28bda22007-05-01 22:32:36 +02002060#ifdef SUPPORT_TAGS
2061 cmd_free_tag(cmd);
2062 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
Finn Thainb7465452016-01-03 16:06:05 +11002063 u8 lun = cmd->device->lun;
2064 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
2065
2066 dsprintk(NDEBUG_TAGS, instance,
2067 "QUEUE_FULL %p target %d lun %d nr_allocated %d\n",
2068 cmd, scmd_id(cmd), lun, ta->nr_allocated);
Roman Zippelc28bda22007-05-01 22:32:36 +02002069 if (ta->queue_size > ta->nr_allocated)
Finn Thaine42d0152016-01-03 16:05:49 +11002070 ta->queue_size = ta->nr_allocated;
Roman Zippelc28bda22007-05-01 22:32:36 +02002071 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002072#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002073
Finn Thainf27db8e2016-01-03 16:06:00 +11002074 cmd->result &= ~0xffff;
2075 cmd->result |= cmd->SCp.Status;
2076 cmd->result |= cmd->SCp.Message << 8;
Roman Zippelc28bda22007-05-01 22:32:36 +02002077
Finn Thainf27db8e2016-01-03 16:06:00 +11002078 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11002079 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11002080 else {
2081 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
2082 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
2083 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
2084 cmd);
2085 list_add_tail(&ncmd->list,
2086 &hostdata->autosense);
2087 } else
2088 complete_cmd(instance, cmd);
Roman Zippelc28bda22007-05-01 22:32:36 +02002089 }
2090
Roman Zippelc28bda22007-05-01 22:32:36 +02002091 /*
2092 * Restore phase bits to 0 so an interrupted selection,
2093 * arbitration can resume.
2094 */
2095 NCR5380_write(TARGET_COMMAND_REG, 0);
2096
Finn Thain72064a72016-01-03 16:05:44 +11002097 /* Enable reselect interrupts */
2098 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2099
Roman Zippelc28bda22007-05-01 22:32:36 +02002100 /* ++roman: For Falcon SCSI, release the lock on the
2101 * ST-DMA here if no other commands are waiting on the
2102 * disconnected queue.
2103 */
Finn Thaine3c3da62014-11-12 16:12:15 +11002104 maybe_release_dma_irq(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002105 return;
2106 case MESSAGE_REJECT:
2107 /* Accept message by clearing ACK */
2108 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002109 switch (hostdata->last_message) {
2110 case HEAD_OF_QUEUE_TAG:
2111 case ORDERED_QUEUE_TAG:
2112 case SIMPLE_QUEUE_TAG:
2113 /* The target obviously doesn't support tagged
2114 * queuing, even though it announced this ability in
2115 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2116 * clear 'tagged_supported' and lock the LUN, since
2117 * the command is treated as untagged further on.
2118 */
2119 cmd->device->tagged_supported = 0;
2120 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2121 cmd->tag = TAG_NONE;
Finn Thainb7465452016-01-03 16:06:05 +11002122 dsprintk(NDEBUG_TAGS, instance, "target %d lun %llu rejected QUEUE_TAG message; tagged queuing disabled\n",
2123 scmd_id(cmd), cmd->device->lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002124 break;
2125 }
2126 break;
2127 case DISCONNECT:
2128 /* Accept message by clearing ACK */
2129 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002130 hostdata->connected = NULL;
Finn Thain32b26a12016-01-03 16:05:58 +11002131 list_add(&ncmd->list, &hostdata->disconnected);
Finn Thain0d3d9a42016-01-03 16:05:55 +11002132 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
2133 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
2134 cmd, scmd_id(cmd), cmd->device->lun);
2135
Roman Zippelc28bda22007-05-01 22:32:36 +02002136 /*
2137 * Restore phase bits to 0 so an interrupted selection,
2138 * arbitration can resume.
2139 */
2140 NCR5380_write(TARGET_COMMAND_REG, 0);
2141
2142 /* Enable reselect interrupts */
2143 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine3f463b2014-11-12 16:12:16 +11002144#ifdef SUN3_SCSI_VME
2145 dregs->csr |= CSR_DMA_ENABLE;
2146#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002147 return;
2148 /*
2149 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2150 * operation, in violation of the SCSI spec so we can safely
2151 * ignore SAVE/RESTORE pointers calls.
2152 *
2153 * Unfortunately, some disks violate the SCSI spec and
2154 * don't issue the required SAVE_POINTERS message before
2155 * disconnecting, and we have to break spec to remain
2156 * compatible.
2157 */
2158 case SAVE_POINTERS:
2159 case RESTORE_POINTERS:
2160 /* Accept message by clearing ACK */
2161 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Roman Zippelc28bda22007-05-01 22:32:36 +02002162 break;
2163 case EXTENDED_MESSAGE:
2164 /*
2165 * Extended messages are sent in the following format :
2166 * Byte
2167 * 0 EXTENDED_MESSAGE == 1
2168 * 1 length (includes one byte for code, doesn't
2169 * include first two bytes)
2170 * 2 code
2171 * 3..length+1 arguments
2172 *
2173 * Start the extended message buffer with the EXTENDED_MESSAGE
2174 * byte, since spi_print_msg() wants the whole thing.
2175 */
2176 extended_msg[0] = EXTENDED_MESSAGE;
2177 /* Accept first byte by clearing ACK */
2178 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2179
Finn Thain11d2f632016-01-03 16:05:51 +11002180 spin_unlock_irq(&hostdata->lock);
2181
Finn Thainb7465452016-01-03 16:06:05 +11002182 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002183
2184 len = 2;
2185 data = extended_msg + 1;
2186 phase = PHASE_MSGIN;
2187 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002188 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
2189 (int)extended_msg[1],
2190 (int)extended_msg[2]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002191
Finn Thaine0783ed2016-01-03 16:05:45 +11002192 if (!len && extended_msg[1] > 0 &&
2193 extended_msg[1] <= sizeof(extended_msg) - 2) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002194 /* Accept third byte by clearing ACK */
2195 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2196 len = extended_msg[1] - 1;
2197 data = extended_msg + 3;
2198 phase = PHASE_MSGIN;
2199
2200 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002201 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
2202 len);
Roman Zippelc28bda22007-05-01 22:32:36 +02002203
2204 switch (extended_msg[2]) {
2205 case EXTENDED_SDTR:
2206 case EXTENDED_WDTR:
2207 case EXTENDED_MODIFY_DATA_POINTER:
2208 case EXTENDED_EXTENDED_IDENTIFY:
2209 tmp = 0;
2210 }
2211 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002212 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002213 tmp = 0;
2214 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002215 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2216 extended_msg[2], extended_msg[1]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002217 tmp = 0;
2218 }
Finn Thain11d2f632016-01-03 16:05:51 +11002219
2220 spin_lock_irq(&hostdata->lock);
2221 if (!hostdata->connected)
2222 return;
2223
Roman Zippelc28bda22007-05-01 22:32:36 +02002224 /* Fall through to reject message */
2225
2226 /*
2227 * If we get something weird that we aren't expecting,
2228 * reject it.
2229 */
2230 default:
2231 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002232 shost_printk(KERN_ERR, instance, "rejecting message ");
Roman Zippelc28bda22007-05-01 22:32:36 +02002233 spi_print_msg(extended_msg);
2234 printk("\n");
2235 } else if (tmp != EXTENDED_MESSAGE)
Finn Thainff50f9e2014-11-12 16:12:18 +11002236 scmd_printk(KERN_INFO, cmd,
2237 "rejecting unknown message %02x\n",
2238 tmp);
Roman Zippelc28bda22007-05-01 22:32:36 +02002239 else
Finn Thainff50f9e2014-11-12 16:12:18 +11002240 scmd_printk(KERN_INFO, cmd,
2241 "rejecting unknown extended message code %02x, length %d\n",
2242 extended_msg[1], extended_msg[0]);
Roman Zippelc28bda22007-05-01 22:32:36 +02002243
2244 msgout = MESSAGE_REJECT;
2245 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2246 break;
2247 } /* switch (tmp) */
2248 break;
2249 case PHASE_MSGOUT:
2250 len = 1;
2251 data = &msgout;
2252 hostdata->last_message = msgout;
2253 NCR5380_transfer_pio(instance, &phase, &len, &data);
2254 if (msgout == ABORT) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002255 hostdata->connected = NULL;
2256 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002257 complete_cmd(instance, cmd);
Finn Thaine3c3da62014-11-12 16:12:15 +11002258 maybe_release_dma_irq(instance);
Finn Thaincd400822016-01-03 16:05:40 +11002259 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Roman Zippelc28bda22007-05-01 22:32:36 +02002260 return;
2261 }
2262 msgout = NOP;
2263 break;
2264 case PHASE_CMDOUT:
2265 len = cmd->cmd_len;
2266 data = cmd->cmnd;
2267 /*
2268 * XXX for performance reasons, on machines with a
2269 * PSEUDO-DMA architecture we should probably
2270 * use the dma transfer function.
2271 */
2272 NCR5380_transfer_pio(instance, &phase, &len, &data);
2273 break;
2274 case PHASE_STATIN:
2275 len = 1;
2276 data = &tmp;
2277 NCR5380_transfer_pio(instance, &phase, &len, &data);
2278 cmd->SCp.Status = tmp;
2279 break;
2280 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002281 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain8ad3a592014-03-18 11:42:19 +11002282 NCR5380_dprint(NDEBUG_ANY, instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002283 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002284 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002285 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002286 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002287 spin_lock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002288 }
Finn Thain11d2f632016-01-03 16:05:51 +11002289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292/*
2293 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2294 *
Roman Zippelc28bda22007-05-01 22:32:36 +02002295 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002296 * 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 -07002297 * nexus has been reestablished,
Roman Zippelc28bda22007-05-01 22:32:36 +02002298 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * Inputs : instance - this instance of the NCR5380.
2300 *
2301 */
2302
2303
Finn Thaine3f463b2014-11-12 16:12:16 +11002304/* it might eventually prove necessary to do a dma setup on
2305 reselection, but it doesn't seem to be needed now -- sam */
2306
Roman Zippelc28bda22007-05-01 22:32:36 +02002307static void NCR5380_reselect(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Finn Thaine8a60142016-01-03 16:05:54 +11002309 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002310 unsigned char target_mask;
Finn Thaine3f463b2014-11-12 16:12:16 +11002311 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312#ifdef SUPPORT_TAGS
Roman Zippelc28bda22007-05-01 22:32:36 +02002313 unsigned char tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002315 unsigned char msg[3];
Finn Thaine3f463b2014-11-12 16:12:16 +11002316 int __maybe_unused len;
2317 unsigned char __maybe_unused *data, __maybe_unused phase;
Finn Thain32b26a12016-01-03 16:05:58 +11002318 struct NCR5380_cmd *ncmd;
2319 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Roman Zippelc28bda22007-05-01 22:32:36 +02002321 /*
2322 * Disable arbitration, etc. since the host adapter obviously
2323 * lost, and tell an interrupted NCR5380_select() to restart.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Roman Zippelc28bda22007-05-01 22:32:36 +02002326 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Roman Zippelc28bda22007-05-01 22:32:36 +02002328 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2329
Finn Thainb7465452016-01-03 16:06:05 +11002330 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Roman Zippelc28bda22007-05-01 22:32:36 +02002331
2332 /*
2333 * At this point, we have detected that our SCSI ID is on the bus,
2334 * SEL is true and BSY was false for at least one bus settle delay
2335 * (400 ns).
2336 *
2337 * We must assert BSY ourselves, until the target drops the SEL
2338 * signal.
2339 */
2340
2341 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002342 if (NCR5380_poll_politely(instance,
2343 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2344 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2345 return;
2346 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002347 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2348
2349 /*
2350 * Wait for target to go into MSGIN.
2351 */
2352
Finn Thain72064a72016-01-03 16:05:44 +11002353 if (NCR5380_poll_politely(instance,
2354 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2355 do_abort(instance);
2356 return;
2357 }
Roman Zippelc28bda22007-05-01 22:32:36 +02002358
Finn Thaine3f463b2014-11-12 16:12:16 +11002359#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2360 /* acknowledge toggle to MSGIN */
2361 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2362
2363 /* peek at the byte without really hitting the bus */
2364 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2365#else
Roman Zippelc28bda22007-05-01 22:32:36 +02002366 len = 1;
2367 data = msg;
2368 phase = PHASE_MSGIN;
2369 NCR5380_transfer_pio(instance, &phase, &len, &data);
2370
Finn Thain72064a72016-01-03 16:05:44 +11002371 if (len) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002372 do_abort(instance);
2373 return;
2374 }
Finn Thain72064a72016-01-03 16:05:44 +11002375#endif
2376
2377 if (!(msg[0] & 0x80)) {
2378 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2379 spi_print_msg(msg);
2380 printk("\n");
2381 do_abort(instance);
2382 return;
2383 }
2384 lun = msg[0] & 0x07;
Roman Zippelc28bda22007-05-01 22:32:36 +02002385
Finn Thaine3f463b2014-11-12 16:12:16 +11002386#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
Roman Zippelc28bda22007-05-01 22:32:36 +02002387 /* If the phase is still MSGIN, the target wants to send some more
2388 * messages. In case it supports tagged queuing, this is probably a
2389 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2390 */
2391 tag = TAG_NONE;
Finn Thainca513fc2014-11-12 16:12:19 +11002392 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
Roman Zippelc28bda22007-05-01 22:32:36 +02002393 /* Accept previous IDENTIFY message by clearing ACK */
2394 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2395 len = 2;
2396 data = msg + 1;
2397 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2398 msg[1] == SIMPLE_QUEUE_TAG)
2399 tag = msg[2];
Finn Thainb7465452016-01-03 16:06:05 +11002400 dsprintk(NDEBUG_TAGS, instance, "reselect: target mask %02x, lun %d sent tag %d\n",
2401 target_mask, lun, tag);
Roman Zippelc28bda22007-05-01 22:32:36 +02002402 }
2403#endif
2404
2405 /*
2406 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2407 * just reestablished, and remove it from the disconnected queue.
2408 */
2409
Finn Thain32b26a12016-01-03 16:05:58 +11002410 tmp = NULL;
2411 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2412 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2413
2414 if (target_mask == (1 << scmd_id(cmd)) &&
2415 lun == (u8)cmd->device->lun
Roman Zippelc28bda22007-05-01 22:32:36 +02002416#ifdef SUPPORT_TAGS
Finn Thain32b26a12016-01-03 16:05:58 +11002417 && (tag == cmd->tag)
Roman Zippelc28bda22007-05-01 22:32:36 +02002418#endif
2419 ) {
Finn Thain32b26a12016-01-03 16:05:58 +11002420 list_del(&ncmd->list);
2421 tmp = cmd;
Roman Zippelc28bda22007-05-01 22:32:36 +02002422 break;
2423 }
2424 }
2425
Finn Thain0d3d9a42016-01-03 16:05:55 +11002426 if (tmp) {
2427 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2428 "reselect: removed %p from disconnected queue\n", tmp);
2429 } else {
2430
Roman Zippelc28bda22007-05-01 22:32:36 +02002431#ifdef SUPPORT_TAGS
Finn Thain72064a72016-01-03 16:05:44 +11002432 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d tag %d not in disconnected queue.\n",
2433 target_mask, lun, tag);
2434#else
2435 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2436 target_mask, lun);
Roman Zippelc28bda22007-05-01 22:32:36 +02002437#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002438 /*
2439 * Since we have an established nexus that we can't do anything
2440 * with, we must abort it.
2441 */
2442 do_abort(instance);
2443 return;
2444 }
2445
Finn Thaine3f463b2014-11-12 16:12:16 +11002446#if defined(CONFIG_SUN3) && defined(REAL_DMA)
2447 /* engage dma setup for the command we just saw */
2448 {
2449 void *d;
2450 unsigned long count;
2451
2452 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2453 count = tmp->SCp.buffer->length;
2454 d = sg_virt(tmp->SCp.buffer);
2455 } else {
2456 count = tmp->SCp.this_residual;
2457 d = tmp->SCp.ptr;
2458 }
2459 /* setup this command for dma if not already */
2460 if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
Finn Thaincd461402016-01-03 16:06:06 +11002461 sun3scsi_dma_setup(instance, d, count,
2462 rq_data_dir(tmp->request));
Finn Thaine3f463b2014-11-12 16:12:16 +11002463 sun3_dma_setup_done = tmp;
2464 }
2465 }
2466
2467 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2468#endif
2469
Roman Zippelc28bda22007-05-01 22:32:36 +02002470 /* Accept message by clearing ACK */
2471 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2472
Finn Thaine3f463b2014-11-12 16:12:16 +11002473#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2474 /* If the phase is still MSGIN, the target wants to send some more
2475 * messages. In case it supports tagged queuing, this is probably a
2476 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2477 */
2478 tag = TAG_NONE;
2479 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2480 /* Accept previous IDENTIFY message by clearing ACK */
2481 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2482 len = 2;
2483 data = msg + 1;
2484 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2485 msg[1] == SIMPLE_QUEUE_TAG)
2486 tag = msg[2];
Finn Thainb7465452016-01-03 16:06:05 +11002487 dsprintk(NDEBUG_TAGS, instance, "reselect: target mask %02x, lun %d sent tag %d\n"
2488 target_mask, lun, tag);
Finn Thaine3f463b2014-11-12 16:12:16 +11002489 }
2490#endif
2491
Roman Zippelc28bda22007-05-01 22:32:36 +02002492 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002493 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2494 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497
Finn Thain8b00c3d2016-01-03 16:06:01 +11002498/**
2499 * list_find_cmd - test for presence of a command in a linked list
2500 * @haystack: list of commands
2501 * @needle: command to search for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 */
2503
Finn Thain8b00c3d2016-01-03 16:06:01 +11002504static bool list_find_cmd(struct list_head *haystack,
2505 struct scsi_cmnd *needle)
2506{
2507 struct NCR5380_cmd *ncmd;
2508
2509 list_for_each_entry(ncmd, haystack, list)
2510 if (NCR5380_to_scmd(ncmd) == needle)
2511 return true;
2512 return false;
2513}
2514
2515/**
2516 * list_remove_cmd - remove a command from linked list
2517 * @haystack: list of commands
2518 * @needle: command to remove
2519 */
2520
2521static bool list_del_cmd(struct list_head *haystack,
2522 struct scsi_cmnd *needle)
2523{
2524 if (list_find_cmd(haystack, needle)) {
2525 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2526
2527 list_del(&ncmd->list);
2528 return true;
2529 }
2530 return false;
2531}
2532
2533/**
2534 * NCR5380_abort - scsi host eh_abort_handler() method
2535 * @cmd: the command to be aborted
2536 *
2537 * Try to abort a given command by removing it from queues and/or sending
2538 * the target an abort message. This may not succeed in causing a target
2539 * to abort the command. Nonetheless, the low-level driver must forget about
2540 * the command because the mid-layer reclaims it and it may be re-issued.
2541 *
2542 * The normal path taken by a command is as follows. For EH we trace this
2543 * same path to locate and abort the command.
2544 *
2545 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2546 * [disconnected -> connected ->]...
2547 * [autosense -> connected ->] done
2548 *
2549 * If cmd is unissued then just remove it.
2550 * If cmd is disconnected, try to select the target.
2551 * If cmd is connected, try to send an abort message.
2552 * If cmd is waiting for autosense, give it a chance to complete but check
2553 * that it isn't left connected.
2554 * If cmd was not found at all then presumably it has already been completed,
2555 * in which case return SUCCESS to try to avoid further EH measures.
2556 * If the command has not completed yet, we must not fail to find it.
2557 */
2558
2559static int NCR5380_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Roman Zippelc28bda22007-05-01 22:32:36 +02002561 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002562 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002563 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002564 int result = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Finn Thain11d2f632016-01-03 16:05:51 +11002566 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Finn Thain32b26a12016-01-03 16:05:58 +11002568#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002569 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002570#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002571 NCR5380_dprint(NDEBUG_ANY, instance);
2572 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Finn Thain8b00c3d2016-01-03 16:06:01 +11002574 if (list_del_cmd(&hostdata->unissued, cmd)) {
2575 dsprintk(NDEBUG_ABORT, instance,
2576 "abort: removed %p from issue queue\n", cmd);
2577 cmd->result = DID_ABORT << 16;
2578 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2579 }
2580
Finn Thain707d62b2016-01-03 16:06:02 +11002581 if (hostdata->selecting == cmd) {
2582 dsprintk(NDEBUG_ABORT, instance,
2583 "abort: cmd %p == selecting\n", cmd);
2584 hostdata->selecting = NULL;
2585 cmd->result = DID_ABORT << 16;
2586 complete_cmd(instance, cmd);
2587 goto out;
2588 }
2589
Finn Thain8b00c3d2016-01-03 16:06:01 +11002590 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2591 dsprintk(NDEBUG_ABORT, instance,
2592 "abort: removed %p from disconnected list\n", cmd);
2593 cmd->result = DID_ERROR << 16;
2594 if (!hostdata->connected)
2595 NCR5380_select(instance, cmd);
2596 if (hostdata->connected != cmd) {
2597 complete_cmd(instance, cmd);
2598 result = FAILED;
2599 goto out;
2600 }
2601 }
2602
2603 if (hostdata->connected == cmd) {
2604 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2605 hostdata->connected = NULL;
2606 if (do_abort(instance)) {
2607 set_host_byte(cmd, DID_ERROR);
2608 complete_cmd(instance, cmd);
2609 result = FAILED;
2610 goto out;
2611 }
2612 set_host_byte(cmd, DID_ABORT);
2613#ifdef REAL_DMA
2614 hostdata->dma_len = 0;
2615#endif
2616 if (cmd->cmnd[0] == REQUEST_SENSE)
2617 complete_cmd(instance, cmd);
2618 else {
2619 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
2620
2621 /* Perform autosense for this command */
2622 list_add(&ncmd->list, &hostdata->autosense);
2623 }
2624 }
2625
2626 if (list_find_cmd(&hostdata->autosense, cmd)) {
2627 dsprintk(NDEBUG_ABORT, instance,
2628 "abort: found %p on sense queue\n", cmd);
2629 spin_unlock_irqrestore(&hostdata->lock, flags);
2630 queue_work(hostdata->work_q, &hostdata->main_task);
2631 msleep(1000);
2632 spin_lock_irqsave(&hostdata->lock, flags);
2633 if (list_del_cmd(&hostdata->autosense, cmd)) {
2634 dsprintk(NDEBUG_ABORT, instance,
2635 "abort: removed %p from sense queue\n", cmd);
2636 set_host_byte(cmd, DID_ABORT);
2637 complete_cmd(instance, cmd);
2638 goto out;
2639 }
2640 }
2641
2642 if (hostdata->connected == cmd) {
2643 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2644 hostdata->connected = NULL;
2645 if (do_abort(instance)) {
2646 set_host_byte(cmd, DID_ERROR);
2647 complete_cmd(instance, cmd);
2648 result = FAILED;
2649 goto out;
2650 }
2651 set_host_byte(cmd, DID_ABORT);
2652#ifdef REAL_DMA
2653 hostdata->dma_len = 0;
2654#endif
2655 complete_cmd(instance, cmd);
2656 }
2657
2658out:
2659 if (result == FAILED)
2660 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2661 else
2662 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2663
2664 queue_work(hostdata->work_q, &hostdata->main_task);
2665 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002666 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaine3c3da62014-11-12 16:12:15 +11002667
Finn Thain8b00c3d2016-01-03 16:06:01 +11002668 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
2671
Finn Thain3be1b3e2016-01-03 16:05:12 +11002672/**
2673 * NCR5380_bus_reset - reset the SCSI bus
2674 * @cmd: SCSI command undergoing EH
Roman Zippelc28bda22007-05-01 22:32:36 +02002675 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002676 * Returns SUCCESS
Roman Zippelc28bda22007-05-01 22:32:36 +02002677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Finn Thain710ddd02014-11-12 16:12:02 +11002679static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Finn Thaine3c3da62014-11-12 16:12:15 +11002681 struct Scsi_Host *instance = cmd->device->host;
2682 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Roman Zippelc28bda22007-05-01 22:32:36 +02002683 int i;
2684 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002685 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Finn Thain11d2f632016-01-03 16:05:51 +11002687 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Finn Thain3be1b3e2016-01-03 16:05:12 +11002689#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002690 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002691#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002692 NCR5380_dprint(NDEBUG_ANY, instance);
2693 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002694
2695 do_reset(instance);
2696
Roman Zippelc28bda22007-05-01 22:32:36 +02002697 /* reset NCR registers */
Roman Zippelc28bda22007-05-01 22:32:36 +02002698 NCR5380_write(MODE_REG, MR_BASE);
2699 NCR5380_write(TARGET_COMMAND_REG, 0);
2700 NCR5380_write(SELECT_ENABLE_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Roman Zippelc28bda22007-05-01 22:32:36 +02002702 /* After the reset, there are no more connected or disconnected commands
2703 * and no busy units; so clear the low-level status here to avoid
2704 * conflicts when the mid-level code tries to wake up the affected
2705 * commands!
2706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Finn Thain707d62b2016-01-03 16:06:02 +11002708 hostdata->selecting = NULL;
2709
Finn Thain62717f52016-01-03 16:06:03 +11002710 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2711 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2712
2713 set_host_byte(cmd, DID_RESET);
2714 cmd->scsi_done(cmd);
2715 }
2716
2717 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2718 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2719
2720 set_host_byte(cmd, DID_RESET);
2721 cmd->scsi_done(cmd);
2722 }
2723
2724 if (hostdata->connected) {
2725 set_host_byte(hostdata->connected, DID_RESET);
2726 complete_cmd(instance, hostdata->connected);
2727 hostdata->connected = NULL;
2728 }
Finn Thain32b26a12016-01-03 16:05:58 +11002729
Finn Thainf27db8e2016-01-03 16:06:00 +11002730 if (hostdata->sensing) {
Finn Thain62717f52016-01-03 16:06:03 +11002731 set_host_byte(hostdata->connected, DID_RESET);
Finn Thainf27db8e2016-01-03 16:06:00 +11002732 complete_cmd(instance, hostdata->sensing);
2733 hostdata->sensing = NULL;
2734 }
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736#ifdef SUPPORT_TAGS
Finn Thainca513fc2014-11-12 16:12:19 +11002737 free_all_tags(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738#endif
Roman Zippelc28bda22007-05-01 22:32:36 +02002739 for (i = 0; i < 8; ++i)
2740 hostdata->busy[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +02002742 hostdata->dma_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743#endif
Finn Thaine3c3da62014-11-12 16:12:15 +11002744
Finn Thain62717f52016-01-03 16:06:03 +11002745 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thaine3c3da62014-11-12 16:12:15 +11002746 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002747 spin_unlock_irqrestore(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Michael Schmitz2b0f8342014-05-02 20:43:01 +12002749 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}