blob: 3ea8809600cc4829f9476bd4a6c91fd8e1ed3be5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * For more information, please consult
15 *
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Revision 1.10 1998/9/2 Alan Cox
Alan Coxfa195af2008-10-27 15:16:36 +000029 * (alan@lxorguk.ukuu.org.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Fixed up the timer lockups reported so far. Things still suck. Looking
31 * forward to 2.3 and per device request queues. Then it'll be possible to
32 * SMP thread this beast and improve life no end.
33
34 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
35 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
36 * (hopefully) fixed and enhanced USLEEP
37 * added support for DTC3181E card (for Mustek scanner)
38 *
39
40 * Revision 1.8 Ingmar Baumgart
41 * (ingmar@gonzo.schwaben.de)
42 * added support for NCR53C400a card
43 *
44
45 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
46 * added proc_info
47 * added support needed for DTC 3180/3280
48 * fixed a couple of bugs
49 *
50
51 * Revision 1.5 1994/01/19 09:14:57 drew
52 * Fixed udelay() hack that was being used on DATAOUT phases
53 * instead of a proper wait for the final handshake.
54 *
55 * Revision 1.4 1994/01/19 06:44:25 drew
56 * *** empty log message ***
57 *
58 * Revision 1.3 1994/01/19 05:24:40 drew
59 * Added support for TCR LAST_BYTE_SENT bit.
60 *
61 * Revision 1.2 1994/01/15 06:14:11 drew
62 * REAL DMA support, bug fixes.
63 *
64 * Revision 1.1 1994/01/15 06:00:54 drew
65 * Initial revision
66 *
67 */
68
69/*
70 * Further development / testing that should be done :
71 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
72 * code so that everything does the same thing that's done at the
73 * end of a pseudo-DMA read operation.
74 *
75 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
76 * basically, transfer size needs to be reduced by one
77 * and the last byte read as is done with PSEUDO_DMA.
78 *
79 * 4. Test SCSI-II tagged queueing (I have no devices which support
80 * tagged queueing)
81 *
82 * 5. Test linked command handling code after Eric is ready with
83 * the high level code.
84 */
db9dff32005-04-03 14:53:59 -050085#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050086#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#if (NDEBUG & NDEBUG_LISTS)
89#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
90#define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
91#else
92#define LIST(x,y)
93#define REMOVE(w,x,y,z)
94#endif
95
96#ifndef notyet
97#undef LINKED
98#undef REAL_DMA
99#endif
100
101#ifdef REAL_DMA_POLL
102#undef READ_OVERRUNS
103#define READ_OVERRUNS
104#endif
105
106#ifdef BOARD_REQUIRES_NO_DELAY
107#define io_recovery_delay(x)
108#else
109#define io_recovery_delay(x) udelay(x)
110#endif
111
112/*
113 * Design
114 *
115 * This is a generic 5380 driver. To use it on a different platform,
116 * one simply writes appropriate system specific macros (ie, data
117 * transfer - some PC's will use the I/O bus, 68K's must use
118 * memory mapped) and drops this file in their 'C' wrapper.
119 *
120 * (Note from hch: unfortunately it was not enough for the different
121 * m68k folks and instead of improving this driver they copied it
122 * and hacked it up for their needs. As a consequence they lost
123 * most updates to this driver. Maybe someone will fix all these
124 * drivers to use a common core one day..)
125 *
126 * As far as command queueing, two queues are maintained for
127 * each 5380 in the system - commands that haven't been issued yet,
128 * and commands that are currently executing. This means that an
129 * unlimited number of commands may be queued, letting
130 * more commands propagate from the higher driver levels giving higher
131 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
132 * allowing multiple commands to propagate all the way to a SCSI-II device
133 * while a command is already executing.
134 *
135 *
136 * Issues specific to the NCR5380 :
137 *
138 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
139 * piece of hardware that requires you to sit in a loop polling for
140 * the REQ signal as long as you are connected. Some devices are
141 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
142 * while doing long seek operations.
143 *
144 * The workaround for this is to keep track of devices that have
145 * disconnected. If the device hasn't disconnected, for commands that
146 * should disconnect, we do something like
147 *
148 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
149 *
150 * Some tweaking of N and M needs to be done. An algorithm based
151 * on "time to data" would give the best results as long as short time
152 * to datas (ie, on the same track) were considered, however these
153 * broken devices are the exception rather than the rule and I'd rather
154 * spend my time optimizing for the normal case.
155 *
156 * Architecture :
157 *
158 * At the heart of the design is a coroutine, NCR5380_main,
159 * which is started from a workqueue for each NCR5380 host in the
160 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
161 * removing the commands from the issue queue and calling
162 * NCR5380_select() if a nexus is not established.
163 *
164 * Once a nexus is established, the NCR5380_information_transfer()
165 * phase goes through the various phases as instructed by the target.
166 * if the target goes into MSG IN and sends a DISCONNECT message,
167 * the command structure is placed into the per instance disconnected
168 * queue, and NCR5380_main tries to find more work. If the target is
169 * idle for too long, the system will try to sleep.
170 *
171 * If a command has disconnected, eventually an interrupt will trigger,
172 * calling NCR5380_intr() which will in turn call NCR5380_reselect
173 * to reestablish a nexus. This will run main if necessary.
174 *
175 * On command termination, the done function will be called as
176 * appropriate.
177 *
178 * SCSI pointers are maintained in the SCp field of SCSI command
179 * structures, being initialized after the command is connected
180 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
181 * Note that in violation of the standard, an implicit SAVE POINTERS operation
182 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
183 */
184
185/*
186 * Using this file :
187 * This file a skeleton Linux SCSI driver for the NCR 5380 series
188 * of chips. To use it, you write an architecture specific functions
189 * and macros and include this file in your driver.
190 *
191 * These macros control options :
192 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
193 * defined.
194 *
195 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
196 * for commands that return with a CHECK CONDITION status.
197 *
198 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
199 * transceivers.
200 *
201 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
202 * override-configure an IRQ.
203 *
204 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
205 * bytes at a time. Since interrupts are disabled by default during
206 * these transfers, we might need this to give reasonable interrupt
207 * service time if the transfer size gets too large.
208 *
209 * LINKED - if defined, linked commands are supported.
210 *
211 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
212 *
213 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
214 *
215 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
216 * rely on phase mismatch and EOP interrupts to determine end
217 * of phase.
218 *
219 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
220 * only really want to use this if you're having a problem with
221 * dropped characters during high speed communications, and even
222 * then, you're going to be better off twiddling with transfersize
223 * in the high level code.
224 *
225 * Defaults for these will be provided although the user may want to adjust
226 * these to allocate CPU resources to the SCSI driver or "real" code.
227 *
228 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
229 *
230 * USLEEP_POLL - amount of time, in jiffies, to poll
231 *
232 * These macros MUST be defined :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
234 * NCR5380_read(register) - read from the specified register
235 *
236 * NCR5380_write(register, value) - write to the specific register
237 *
238 * NCR5380_implementation_fields - additional fields needed for this
239 * specific implementation of the NCR5380
240 *
241 * Either real DMA *or* pseudo DMA may be implemented
242 * REAL functions :
243 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
244 * Note that the DMA setup functions should return the number of bytes
245 * that they were able to program the controller for.
246 *
247 * Also note that generic i386/PC versions of these macros are
248 * available as NCR5380_i386_dma_write_setup,
249 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
250 *
251 * NCR5380_dma_write_setup(instance, src, count) - initialize
252 * NCR5380_dma_read_setup(instance, dst, count) - initialize
253 * NCR5380_dma_residual(instance); - residual count
254 *
255 * PSEUDO functions :
256 * NCR5380_pwrite(instance, src, count)
257 * NCR5380_pread(instance, dst, count);
258 *
259 * The generic driver is initialized by calling NCR5380_init(instance),
260 * after setting the appropriate host specific fields and ID. If the
261 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
262 * possible) function may be used.
263 */
264
Finn Thain54d8fe42016-01-03 16:05:06 +1100265static int do_abort(struct Scsi_Host *);
266static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/*
269 * initialize_SCp - init the scsi pointer field
270 * @cmd: command block to set up
271 *
272 * Set up the internal fields in the SCSI command.
273 */
274
Finn Thain710ddd02014-11-12 16:12:02 +1100275static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 /*
278 * Initialize the Scsi Pointer field so that all of the commands in the
279 * various queues are valid.
280 */
281
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200282 if (scsi_bufflen(cmd)) {
283 cmd->SCp.buffer = scsi_sglist(cmd);
284 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200285 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 cmd->SCp.this_residual = cmd->SCp.buffer->length;
287 } else {
288 cmd->SCp.buffer = NULL;
289 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200290 cmd->SCp.ptr = NULL;
291 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294
295/**
Finn Thain2f854b82016-01-03 16:05:22 +1100296 * NCR5380_poll_politely - wait for chip register value
297 * @instance: controller to poll
298 * @reg: 5380 register to poll
299 * @bit: Bitmask to check
300 * @val: Value required to exit
301 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
Finn Thain2f854b82016-01-03 16:05:22 +1100303 * Polls the chip in a reasonably efficient manner waiting for an
304 * event to occur. After a short quick poll we begin to yield the CPU
305 * (if possible). In irq contexts the time-out is arbitrarily limited.
306 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 *
Finn Thain2f854b82016-01-03 16:05:22 +1100308 * Returns 0 if event occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Finn Thain2f854b82016-01-03 16:05:22 +1100311static int NCR5380_poll_politely(struct Scsi_Host *instance,
312 int reg, int bit, int val, int wait)
313{
314 struct NCR5380_hostdata *hostdata = shost_priv(instance);
315 unsigned long deadline = jiffies + wait;
316 unsigned long n;
317
318 /* Busy-wait for up to 10 ms */
319 n = min(10000U, jiffies_to_usecs(wait));
320 n *= hostdata->accesses_per_ms;
321 n /= 1000;
322 do {
323 if ((NCR5380_read(reg) & bit) == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100326 } while (n--);
327
328 if (irqs_disabled() || in_interrupt())
329 return -ETIMEDOUT;
330
331 /* Repeatedly sleep for 1 ms until deadline */
332 while (time_is_after_jiffies(deadline)) {
333 schedule_timeout_uninterruptible(1);
334 if ((NCR5380_read(reg) & bit) == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Finn Thain2f854b82016-01-03 16:05:22 +1100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -ETIMEDOUT;
339}
340
341static struct {
342 unsigned char value;
343 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700344} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 {PHASE_DATAOUT, "DATAOUT"},
346 {PHASE_DATAIN, "DATAIN"},
347 {PHASE_CMDOUT, "CMDOUT"},
348 {PHASE_STATIN, "STATIN"},
349 {PHASE_MSGOUT, "MSGOUT"},
350 {PHASE_MSGIN, "MSGIN"},
351 {PHASE_UNKNOWN, "UNKNOWN"}
352};
353
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100354#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static struct {
356 unsigned char mask;
357 const char *name;
358} signals[] = {
359 {SR_DBP, "PARITY"},
360 {SR_RST, "RST"},
361 {SR_BSY, "BSY"},
362 {SR_REQ, "REQ"},
363 {SR_MSG, "MSG"},
364 {SR_CD, "CD"},
365 {SR_IO, "IO"},
366 {SR_SEL, "SEL"},
367 {0, NULL}
368},
369basrs[] = {
370 {BASR_ATN, "ATN"},
371 {BASR_ACK, "ACK"},
372 {0, NULL}
373},
374icrs[] = {
375 {ICR_ASSERT_RST, "ASSERT RST"},
376 {ICR_ASSERT_ACK, "ASSERT ACK"},
377 {ICR_ASSERT_BSY, "ASSERT BSY"},
378 {ICR_ASSERT_SEL, "ASSERT SEL"},
379 {ICR_ASSERT_ATN, "ASSERT ATN"},
380 {ICR_ASSERT_DATA, "ASSERT DATA"},
381 {0, NULL}
382},
383mrs[] = {
384 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
385 {MR_TARGET, "MODE TARGET"},
386 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
387 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
388 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
389 {MR_DMA_MODE, "MODE DMA"},
390 {MR_ARBITRATE, "MODE ARBITRATION"},
391 {0, NULL}
392};
393
394/**
395 * NCR5380_print - print scsi bus signals
396 * @instance: adapter state to dump
397 *
398 * Print the SCSI bus signals for debugging purposes
399 *
400 * Locks: caller holds hostdata lock (not essential)
401 */
402
403static void NCR5380_print(struct Scsi_Host *instance)
404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
408 status = NCR5380_read(STATUS_REG);
409 mr = NCR5380_read(MODE_REG);
410 icr = NCR5380_read(INITIATOR_COMMAND_REG);
411 basr = NCR5380_read(BUS_AND_STATUS_REG);
412
413 printk("STATUS_REG: %02x ", status);
414 for (i = 0; signals[i].mask; ++i)
415 if (status & signals[i].mask)
416 printk(",%s", signals[i].name);
417 printk("\nBASR: %02x ", basr);
418 for (i = 0; basrs[i].mask; ++i)
419 if (basr & basrs[i].mask)
420 printk(",%s", basrs[i].name);
421 printk("\nICR: %02x ", icr);
422 for (i = 0; icrs[i].mask; ++i)
423 if (icr & icrs[i].mask)
424 printk(",%s", icrs[i].name);
425 printk("\nMODE: %02x ", mr);
426 for (i = 0; mrs[i].mask; ++i)
427 if (mr & mrs[i].mask)
428 printk(",%s", mrs[i].name);
429 printk("\n");
430}
431
432
433/*
434 * NCR5380_print_phase - show SCSI phase
435 * @instance: adapter to dump
436 *
437 * Print the current SCSI phase for debugging purposes
438 *
439 * Locks: none
440 */
441
442static void NCR5380_print_phase(struct Scsi_Host *instance)
443{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned char status;
445 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 status = NCR5380_read(STATUS_REG);
448 if (!(status & SR_REQ))
449 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
450 else {
451 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
452 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
453 }
454}
455#endif
456
457/*
458 * These need tweaking, and would probably work best as per-device
459 * flags initialized differently for disk, tape, cd, etc devices.
460 * People with broken devices are free to experiment as to what gives
461 * the best results for them.
462 *
463 * USLEEP_SLEEP should be a minimum seek time.
464 *
465 * USLEEP_POLL should be a maximum rotational latency.
466 */
467#ifndef USLEEP_SLEEP
468/* 20 ms (reasonable hard disk speed) */
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500469#define USLEEP_SLEEP msecs_to_jiffies(20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#endif
471/* 300 RPM (floppy speed) */
472#ifndef USLEEP_POLL
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500473#define USLEEP_POLL msecs_to_jiffies(200)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/*
477 * Function : int should_disconnect (unsigned char cmd)
478 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200479 * Purpose : decide whether a command would normally disconnect or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * not, since if it won't disconnect we should go to sleep.
481 *
482 * Input : cmd - opcode of SCSI command
483 *
484 * Returns : DISCONNECT_LONG if we should disconnect for a really long
485 * time (ie always, sleep, look for REQ active, sleep),
486 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
487 * time-to-data delay, DISCONNECT_NONE if this command would return
488 * immediately.
489 *
490 * Future sleep algorithms based on time to data can exploit
491 * something like this so they can differentiate between "normal"
492 * (ie, read, write, seek) and unusual commands (ie, * format).
493 *
494 * Note : We don't deal with commands that handle an immediate disconnect,
495 *
496 */
497
498static int should_disconnect(unsigned char cmd)
499{
500 switch (cmd) {
501 case READ_6:
502 case WRITE_6:
503 case SEEK_6:
504 case READ_10:
505 case WRITE_10:
506 case SEEK_10:
507 return DISCONNECT_TIME_TO_DATA;
508 case FORMAT_UNIT:
509 case SEARCH_HIGH:
510 case SEARCH_LOW:
511 case SEARCH_EQUAL:
512 return DISCONNECT_LONG;
513 default:
514 return DISCONNECT_NONE;
515 }
516}
517
518static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
519{
520 hostdata->time_expires = jiffies + timeout;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100521 queue_delayed_work(hostdata->work_q, &hostdata->coroutine, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524
Finn Thaind5f7e652016-01-03 16:05:03 +1100525static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527/**
528 * probe_intr - helper for IRQ autoprobe
529 * @irq: interrupt number
530 * @dev_id: unused
531 * @regs: unused
532 *
533 * Set a flag to indicate the IRQ in question was received. This is
534 * used by the IRQ probe code.
535 */
536
David Howells7d12e782006-10-05 14:55:46 +0100537static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 probe_irq = irq;
540 return IRQ_HANDLED;
541}
542
543/**
544 * NCR5380_probe_irq - find the IRQ of an NCR5380
545 * @instance: NCR5380 controller
546 * @possible: bitmask of ISA IRQ lines
547 *
548 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
549 * and then looking to see what interrupt actually turned up.
550 *
551 * Locks: none, irqs must be enabled on entry
552 */
553
Andrew Morton702809c2007-05-23 14:41:56 -0700554static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
555 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
558 unsigned long timeout;
559 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Finn Thain22f5f102014-11-12 16:11:56 +1100561 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100562 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 trying_irqs |= mask;
564
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500565 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100566 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
569 * A interrupt is triggered whenever BSY = false, SEL = true
570 * and a bit set in the SELECT_ENABLE_REG is asserted on the
571 * SCSI bus.
572 *
573 * Note that the bus is only driven when the phase control signals
574 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
575 * to zero.
576 */
577
578 NCR5380_write(TARGET_COMMAND_REG, 0);
579 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
580 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
581 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
582
Finn Thain22f5f102014-11-12 16:11:56 +1100583 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800584 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 NCR5380_write(SELECT_ENABLE_REG, 0);
587 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
588
Finn Thain22f5f102014-11-12 16:11:56 +1100589 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (trying_irqs & mask)
591 free_irq(i, NULL);
592
593 return probe_irq;
594}
595
596/**
Finn Thain8c325132014-11-12 16:11:58 +1100597 * NCR58380_info - report driver and host information
598 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
Finn Thain8c325132014-11-12 16:11:58 +1100600 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Locks: none
603 */
604
Finn Thain8c325132014-11-12 16:11:58 +1100605static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Finn Thain8c325132014-11-12 16:11:58 +1100607 struct NCR5380_hostdata *hostdata = shost_priv(instance);
608
609 return hostdata->info;
610}
611
612static void prepare_info(struct Scsi_Host *instance)
613{
614 struct NCR5380_hostdata *hostdata = shost_priv(instance);
615
616 snprintf(hostdata->info, sizeof(hostdata->info),
617 "%s, io_port 0x%lx, n_io_port %d, "
618 "base 0x%lx, irq %d, "
619 "can_queue %d, cmd_per_lun %d, "
620 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100621 "flags { %s%s%s%s}, "
Finn Thain4d029e92016-01-03 16:05:19 +1100622#if defined(USLEEP_POLL) && defined(USLEEP_SLEEP)
623 "USLEEP_POLL %lu, USLEEP_SLEEP %lu, "
Finn Thain8c325132014-11-12 16:11:58 +1100624#endif
625 "options { %s} ",
626 instance->hostt->name, instance->io_port, instance->n_io_port,
627 instance->base, instance->irq,
628 instance->can_queue, instance->cmd_per_lun,
629 instance->sg_tablesize, instance->this_id,
630 hostdata->flags & FLAG_NCR53C400 ? "NCR53C400 " : "",
631 hostdata->flags & FLAG_DTC3181E ? "DTC3181E " : "",
632 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100633 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Finn Thain4d029e92016-01-03 16:05:19 +1100634#if defined(USLEEP_POLL) && defined(USLEEP_SLEEP)
635 USLEEP_POLL, USLEEP_SLEEP,
Finn Thain8c325132014-11-12 16:11:58 +1100636#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100638 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100641 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#endif
643#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100644 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#endif
646#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100647 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#endif
649#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100650 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#endif
652#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100653 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
655#ifdef UNSAFE
Finn Thain8c325132014-11-12 16:11:58 +1100656 "UNSAFE "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#endif
Finn Thain8c325132014-11-12 16:11:58 +1100658 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/**
662 * NCR5380_print_status - dump controller info
663 * @instance: controller to dump
664 *
665 * Print commands in the various queues, called from NCR5380_abort
666 * and NCR5380_debug to aid debugging.
667 *
668 * Locks: called functions disable irqs
669 */
670
Finn Thain3be1b3e2016-01-03 16:05:12 +1100671static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 NCR5380_dprint(NDEBUG_ANY, instance);
674 NCR5380_dprint_phase(NDEBUG_ANY, instance);
675}
676
Finn Thaina9c2dc42014-11-12 16:11:59 +1100677#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/******************************************/
679/*
680 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
681 *
682 * *buffer: I/O buffer
683 * **start: if inout == FALSE pointer into buffer where user read should start
684 * offset: current offset
685 * length: length of buffer
686 * hostno: Scsi_Host host_no
687 * inout: TRUE - user is writing; FALSE - user is reading
688 *
689 * Return the number of bytes read from or written
690 */
691
Al Virodd7ab712013-03-31 01:15:54 -0400692static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
693 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100695 struct NCR5380_hostdata *hostdata = shost_priv(instance);
696
697 hostdata->spin_max_r = 0;
698 hostdata->spin_max_w = 0;
699 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400700}
Finn Thaina9c2dc42014-11-12 16:11:59 +1100701#endif
Al Virodd7ab712013-03-31 01:15:54 -0400702
Al Virodd7ab712013-03-31 01:15:54 -0400703static
Finn Thain710ddd02014-11-12 16:12:02 +1100704void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m);
Al Virodd7ab712013-03-31 01:15:54 -0400705static
706void lprint_command(unsigned char *cmd, struct seq_file *m);
707static
708void lprint_opcode(int opcode, struct seq_file *m);
709
710static int __maybe_unused NCR5380_show_info(struct seq_file *m,
711 struct Scsi_Host *instance)
712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100714 struct scsi_cmnd *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
717
Finn Thaina9c2dc42014-11-12 16:11:59 +1100718#ifdef PSEUDO_DMA
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100719 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100720 hostdata->spin_max_w, hostdata->spin_max_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#endif
722 spin_lock_irq(instance->host_lock);
723 if (!hostdata->connected)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100724 seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else
Finn Thain710ddd02014-11-12 16:12:02 +1100726 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100727 seq_printf(m, "scsi%d: issue_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100728 for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400729 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100731 seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100732 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400733 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 spin_unlock_irq(instance->host_lock);
Al Virodd7ab712013-03-31 01:15:54 -0400735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Finn Thain710ddd02014-11-12 16:12:02 +1100738static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100740 seq_printf(m, "scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100741 seq_puts(m, " command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400742 lprint_command(cmd->cmnd, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Al Virodd7ab712013-03-31 01:15:54 -0400745static void lprint_command(unsigned char *command, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 int i, s;
Al Virodd7ab712013-03-31 01:15:54 -0400748 lprint_opcode(command[0], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100750 seq_printf(m, "%02x ", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100751 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Al Virodd7ab712013-03-31 01:15:54 -0400754static void lprint_opcode(int opcode, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100756 seq_printf(m, "%2d (0x%02x)", opcode, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759
760/**
761 * NCR5380_init - initialise an NCR5380
762 * @instance: adapter to configure
763 * @flags: control flags
764 *
765 * Initializes *instance and corresponding 5380 chip,
766 * with flags OR'd into the initial flags value.
767 *
768 * Notes : I assume that the host, hostno, and id bits have been
769 * set correctly. I don't care about the irq and other fields.
770 *
771 * Returns 0 for success
772 *
773 * Locks: interrupts must be enabled when we are called
774 */
775
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800776static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Finn Thainb6488f92016-01-03 16:05:08 +1100778 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain2f854b82016-01-03 16:05:22 +1100780 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if(in_interrupt())
783 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 hostdata->id_mask = 1 << instance->this_id;
786 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
787 if (i > hostdata->id_mask)
788 hostdata->id_higher_mask |= i;
789 for (i = 0; i < 8; ++i)
790 hostdata->busy[i] = 0;
791#ifdef REAL_DMA
792 hostdata->dmalen = 0;
793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 hostdata->connected = NULL;
795 hostdata->issue_queue = NULL;
796 hostdata->disconnected_queue = NULL;
797
David Howellsc4028952006-11-22 14:57:56 +0000798 INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100799 hostdata->work_q = alloc_workqueue("ncr5380_%d",
800 WQ_UNBOUND | WQ_MEM_RECLAIM,
801 1, instance->host_no);
802 if (!hostdata->work_q)
803 return -ENOMEM;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* The CHECK code seems to break the 53C400. Will check it later maybe */
806 if (flags & FLAG_NCR53C400)
807 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
808 else
809 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
810
811 hostdata->host = instance;
812 hostdata->time_expires = 0;
813
Finn Thain8c325132014-11-12 16:11:58 +1100814 prepare_info(instance);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
817 NCR5380_write(MODE_REG, MR_BASE);
818 NCR5380_write(TARGET_COMMAND_REG, 0);
819 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100820
821 /* Calibrate register polling loop */
822 i = 0;
823 deadline = jiffies + 1;
824 do {
825 cpu_relax();
826 } while (time_is_after_jiffies(deadline));
827 deadline += msecs_to_jiffies(256);
828 do {
829 NCR5380_read(STATUS_REG);
830 ++i;
831 cpu_relax();
832 } while (time_is_after_jiffies(deadline));
833 hostdata->accesses_per_ms = i / 256;
834
Finn Thainb6488f92016-01-03 16:05:08 +1100835 return 0;
836}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Finn Thainb6488f92016-01-03 16:05:08 +1100838/**
839 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
840 * @instance: adapter to check
841 *
842 * If the system crashed, it may have crashed with a connected target and
843 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
844 * currently established nexus, which we know nothing about. Failing that
845 * do a bus reset.
846 *
847 * Note that a bus reset will cause the chip to assert IRQ.
848 *
849 * Returns 0 if successful, otherwise -ENXIO.
850 */
851
852static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
853{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100854 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100855 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
858 switch (pass) {
859 case 1:
860 case 3:
861 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100862 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
863 NCR5380_poll_politely(instance,
864 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100867 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 do_abort(instance);
869 break;
870 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100871 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100873 /* Wait after a reset; the SCSI standard calls for
874 * 250ms, we wait 500ms to be on the safe side.
875 * But some Toshiba CD-ROMs need ten times that.
876 */
877 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
878 msleep(2500);
879 else
880 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100883 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return -ENXIO;
885 }
886 }
887 return 0;
888}
889
890/**
891 * NCR5380_exit - remove an NCR5380
892 * @instance: adapter to remove
893 */
894
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800895static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
898
Tejun Heoa684b8d2011-01-24 14:57:28 +0100899 cancel_delayed_work_sync(&hostdata->coroutine);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100900 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903/**
904 * NCR5380_queue_command - queue a command
905 * @cmd: SCSI command
906 * @done: completion handler
907 *
908 * cmd is added to the per instance issue_queue, with minor
909 * twiddling done to the host specific fields of cmd. If the
910 * main coroutine is not running, it is restarted.
911 *
912 * Locks: host lock taken by caller
913 */
914
Finn Thain710ddd02014-11-12 16:12:02 +1100915static int NCR5380_queue_command_lck(struct scsi_cmnd *cmd, void (*done) (struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct Scsi_Host *instance = cmd->device->host;
918 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100919 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921#if (NDEBUG & NDEBUG_NO_WRITE)
922 switch (cmd->cmnd[0]) {
923 case WRITE_6:
924 case WRITE_10:
925 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
926 cmd->result = (DID_ERROR << 16);
927 done(cmd);
928 return 0;
929 }
930#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * We use the host_scribble field as a pointer to the next command
934 * in a queue
935 */
936
937 cmd->host_scribble = NULL;
938 cmd->scsi_done = done;
939 cmd->result = 0;
940
941 /*
942 * Insert the cmd into the issue queue. Note that REQUEST SENSE
943 * commands are added to the head of the queue since any command will
944 * clear the contingent allegiance condition that exists and the
945 * sense data is only guaranteed to be valid while the condition exists.
946 */
947
948 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
949 LIST(cmd, hostdata->issue_queue);
950 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
951 hostdata->issue_queue = cmd;
952 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100953 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (struct scsi_cmnd *) tmp->host_scribble);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 LIST(cmd, tmp);
955 tmp->host_scribble = (unsigned char *) cmd;
956 }
Finn Thain52a6a1c2014-03-18 11:42:18 +1100957 dprintk(NDEBUG_QUEUES, "scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* Run the coroutine if it isn't already running. */
960 /* Kick off command processing */
Finn Thain0ad0eff2016-01-03 16:05:21 +1100961 queue_delayed_work(hostdata->work_q, &hostdata->coroutine, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
963}
964
Jeff Garzikf2812332010-11-16 02:10:29 -0500965static DEF_SCSI_QCMD(NCR5380_queue_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967/**
968 * NCR5380_main - NCR state machines
969 *
970 * NCR5380_main is a coroutine that runs as long as more work can
971 * be done on the NCR5380 host adapters in a system. Both
972 * NCR5380_queue_command() and NCR5380_intr() will try to start it
973 * in case it is not running.
974 *
975 * Locks: called as its own thread with no locks held. Takes the
976 * host lock and called routines may take the isa dma lock.
977 */
978
David Howellsc4028952006-11-22 14:57:56 +0000979static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
David Howellsc4028952006-11-22 14:57:56 +0000981 struct NCR5380_hostdata *hostdata =
982 container_of(work, struct NCR5380_hostdata, coroutine.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100984 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int done;
986
987 spin_lock_irq(instance->host_lock);
988 do {
989 /* Lock held here */
990 done = 1;
Finn Thainae753a32016-01-03 16:05:23 +1100991 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100992 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /*
994 * Search through the issue_queue for a command destined
995 * for a target that's not busy.
996 */
Finn Thain710ddd02014-11-12 16:12:02 +1100997 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 {
999 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001000 dprintk(NDEBUG_LISTS, "MAIN tmp=%p target=%d busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001002 if (!(hostdata->busy[tmp->device->id] &
1003 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (prev) {
1005 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
1006 prev->host_scribble = tmp->host_scribble;
1007 } else {
1008 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11001009 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 tmp->host_scribble = NULL;
1012
1013 /*
1014 * Attempt to establish an I_T_L nexus here.
1015 * On success, instance->hostdata->connected is set.
1016 * On failure, we must add the command back to the
1017 * issue queue so we can keep trying.
1018 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001019 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Finn Thain76f13b92014-11-12 16:11:53 +11001021 /*
1022 * REQUEST SENSE commands are issued without tagged
1023 * queueing, even on SCSI-II devices because the
1024 * contingent allegiance condition exists for the
1025 * entire unit.
1026 */
1027
1028 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001029 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +11001031 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 LIST(tmp, hostdata->issue_queue);
1033 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1034 hostdata->issue_queue = tmp;
1035 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001036 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Finn Thainae753a32016-01-03 16:05:23 +11001038 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +11001039 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* lock held here still */
1041 } /* if target/lun is not busy */
1042 } /* for */
1043 /* exited locked */
1044 } /* if (!hostdata->connected) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (hostdata->connected
1046#ifdef REAL_DMA
1047 && !hostdata->dmalen
1048#endif
1049 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1050 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001051 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001053 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 done = 0;
1055 } else
1056 break;
1057 } while (!done);
1058
1059 spin_unlock_irq(instance->host_lock);
1060}
1061
1062#ifndef DONT_USE_INTR
1063
1064/**
1065 * NCR5380_intr - generic NCR5380 irq handler
1066 * @irq: interrupt number
1067 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *
1069 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1070 * from the disconnected queue, and restarting NCR5380_main()
1071 * as required.
1072 *
1073 * Locks: takes the needed instance locks
1074 */
1075
Jeff Garzikbaa9aac2007-12-13 16:14:14 -08001076static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -08001078 struct Scsi_Host *instance = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1080 int done;
1081 unsigned char basr;
1082 unsigned long flags;
1083
Finn Thain52a6a1c2014-03-18 11:42:18 +11001084 dprintk(NDEBUG_INTR, "scsi : NCR5380 irq %d triggered\n",
1085 instance->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 do {
1088 done = 1;
1089 spin_lock_irqsave(instance->host_lock, flags);
1090 /* Look for pending interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 basr = NCR5380_read(BUS_AND_STATUS_REG);
1092 /* XXX dispatch to appropriate routine if found and done=0 */
1093 if (basr & BASR_IRQ) {
1094 NCR5380_dprint(NDEBUG_INTR, instance);
1095 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1096 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001097 dprintk(NDEBUG_INTR, "scsi%d : SEL interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 NCR5380_reselect(instance);
1099 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1100 } else if (basr & BASR_PARITY_ERROR) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001101 dprintk(NDEBUG_INTR, "scsi%d : PARITY interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1103 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001104 dprintk(NDEBUG_INTR, "scsi%d : RESET interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1106 } else {
1107#if defined(REAL_DMA)
1108 /*
1109 * We should only get PHASE MISMATCH and EOP interrupts
1110 * if we have DMA enabled, so do a sanity check based on
1111 * the current setting of the MODE register.
1112 */
1113
1114 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001115 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (!hostdata->connected)
1118 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1119
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001120 transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 hostdata->connected->SCp.this_residual -= transferred;
1122 hostdata->connected->SCp.ptr += transferred;
1123 hostdata->dmalen = 0;
1124
1125 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1126
1127 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1128 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1129
1130 NCR5380_write(MODE_REG, MR_BASE);
1131 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1132 }
1133#else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001134 dprintk(NDEBUG_INTR, "scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1136#endif
1137 }
1138 } /* if BASR_IRQ */
1139 spin_unlock_irqrestore(instance->host_lock, flags);
1140 if(!done)
Finn Thain0ad0eff2016-01-03 16:05:21 +11001141 queue_delayed_work(hostdata->work_q,
1142 &hostdata->coroutine, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 } while (!done);
1144 return IRQ_HANDLED;
1145}
1146
1147#endif
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/*
Finn Thain710ddd02014-11-12 16:12:02 +11001150 * Function : int NCR5380_select(struct Scsi_Host *instance,
1151 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 *
1153 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1154 * including ARBITRATION, SELECTION, and initial message out for
1155 * IDENTIFY and queue messages.
1156 *
1157 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001158 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *
Finn Thain63238762016-01-03 16:05:15 +11001160 * Returns : -1 if selection failed but should be retried.
1161 * 0 if selection failed and should not be retried.
1162 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 *
1164 * Side effects :
1165 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1166 * with registers as they should have been on entry - ie
1167 * SELECT_ENABLE will be set appropriately, the NCR5380
1168 * will cease to drive any SCSI bus signals.
1169 *
1170 * If successful : I_T_L or I_T_L_Q nexus will be established,
1171 * instance->connected will be set to cmd.
1172 * SELECT interrupt will be disabled.
1173 *
1174 * If failed (no target) : cmd->scsi_done() will be called, and the
1175 * cmd->result host byte set to DID_BAD_TARGET.
1176 *
1177 * Locks: caller holds hostdata lock in IRQ mode
1178 */
1179
Finn Thain710ddd02014-11-12 16:12:02 +11001180static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1183 unsigned char tmp[3], phase;
1184 unsigned char *data;
1185 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001189 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /*
1192 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1193 * data bus during SELECTION.
1194 */
1195
1196 NCR5380_write(TARGET_COMMAND_REG, 0);
1197
1198 /*
1199 * Start arbitration.
1200 */
1201
1202 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1203 NCR5380_write(MODE_REG, MR_ARBITRATE);
1204
1205
1206 /* We can be relaxed here, interrupts are on, we are
1207 in workqueue context, the birds are singing in the trees */
1208 spin_unlock_irq(instance->host_lock);
1209 err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
1210 spin_lock_irq(instance->host_lock);
1211 if (err < 0) {
1212 printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
1213 NCR5380_write(MODE_REG, MR_BASE);
1214 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001215 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Finn Thain52a6a1c2014-03-18 11:42:18 +11001218 dprintk(NDEBUG_ARBITRATION, "scsi%d : arbitration complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /*
1221 * The arbitration delay is 2.2us, but this is a minimum and there is
1222 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1223 * the integral nature of udelay().
1224 *
1225 */
1226
1227 udelay(3);
1228
1229 /* Check for lost arbitration */
1230 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1231 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001232 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001233 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Finn Thaincf13b082016-01-03 16:05:18 +11001235
1236 /* After/during arbitration, BSY should be asserted.
1237 * IBM DPES-31080 Version S31Q works now
1238 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1239 */
1240 NCR5380_write(INITIATOR_COMMAND_REG,
1241 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (!(hostdata->flags & FLAG_DTC3181E) &&
1244 /* RvC: DTC3181E has some trouble with this
1245 * so we simply removed it. Seems to work with
1246 * only Mustek scanner attached
1247 */
1248 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1249 NCR5380_write(MODE_REG, MR_BASE);
1250 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001251 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001252 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 /*
1255 * Again, bus clear + bus settle time is 1.2us, however, this is
1256 * a minimum so we'll udelay ceil(1.2)
1257 */
1258
Finn Thain9c3f0e22016-01-03 16:05:11 +11001259 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1260 udelay(15);
1261 else
1262 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Finn Thain52a6a1c2014-03-18 11:42:18 +11001264 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /*
1267 * Now that we have won arbitration, start Selection process, asserting
1268 * the host and target ID's on the SCSI bus.
1269 */
1270
Jeff Garzik422c0d62005-10-24 18:05:09 -04001271 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /*
1274 * Raise ATN while SEL is true before BSY goes false from arbitration,
1275 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1276 * phase immediately after selection.
1277 */
1278
1279 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1280 NCR5380_write(MODE_REG, MR_BASE);
1281
1282 /*
1283 * Reselect interrupts must be turned off prior to the dropping of BSY,
1284 * otherwise we will trigger an interrupt.
1285 */
1286 NCR5380_write(SELECT_ENABLE_REG, 0);
1287
1288 /*
1289 * The initiator shall then wait at least two deskew delays and release
1290 * the BSY signal.
1291 */
1292 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1293
1294 /* Reset BSY */
1295 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1296
1297 /*
1298 * Something weird happens when we cease to drive BSY - looks
1299 * like the board/chip is letting us do another read before the
1300 * appropriate propagation delay has expired, and we're confusing
1301 * a BSY signal from ourselves as the target's response to SELECTION.
1302 *
1303 * A small delay (the 'C++' frontend breaks the pipeline with an
1304 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1305 * tighter 'C' code breaks and requires this) solves the problem -
1306 * the 1 us delay is arbitrary, and only used because this delay will
1307 * be the same on other platforms and since it works here, it should
1308 * work there.
1309 *
1310 * wingel suggests that this could be due to failing to wait
1311 * one deskew delay.
1312 */
1313
1314 udelay(1);
1315
Finn Thain52a6a1c2014-03-18 11:42:18 +11001316 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /*
1319 * The SCSI specification calls for a 250 ms timeout for the actual
1320 * selection.
1321 */
1322
Finn Thainae753a32016-01-03 16:05:23 +11001323 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1324 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1327 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1328 NCR5380_reselect(instance);
1329 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1330 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1331 return -1;
1332 }
Finn Thainae753a32016-01-03 16:05:23 +11001333
1334 if (err < 0) {
1335 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1336 cmd->result = DID_BAD_TARGET << 16;
1337 cmd->scsi_done(cmd);
1338 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1339 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1340 instance->host_no);
1341 return 0;
1342 }
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
1345 * No less than two deskew delays after the initiator detects the
1346 * BSY signal is true, it shall release the SEL signal and may
1347 * change the DATA BUS. -wingel
1348 */
1349
1350 udelay(1);
1351
1352 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 * Since we followed the SCSI spec, and raised ATN while SEL
1356 * was true but before BSY was false during selection, the information
1357 * transfer phase should be a MESSAGE OUT phase so that we can send the
1358 * IDENTIFY message.
1359 *
1360 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1361 * message (2 bytes) with a tag ID that we increment with every command
1362 * until it wraps back to 0.
1363 *
1364 * XXX - it turns out that there are some broken SCSI-II devices,
1365 * which claim to support tagged queuing but fail when more than
1366 * some number of commands are issued at once.
1367 */
1368
1369 /* Wait for start of REQ/ACK handshake */
1370
1371 spin_unlock_irq(instance->host_lock);
1372 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1373 spin_lock_irq(instance->host_lock);
1374
1375 if(err) {
1376 printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1377 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001378 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Finn Thain52a6a1c2014-03-18 11:42:18 +11001381 dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
Finn Thain22f5f102014-11-12 16:11:56 +11001382 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 len = 1;
1385 cmd->tag = 0;
1386
1387 /* Send message(s) */
1388 data = tmp;
1389 phase = PHASE_MSGOUT;
1390 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001391 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* XXX need to handle errors here */
1393 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001394 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Boaz Harrosh28424d32007-09-10 22:37:45 +03001396 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401/*
1402 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1403 * unsigned char *phase, int *count, unsigned char **data)
1404 *
1405 * Purpose : transfers data in given phase using polled I/O
1406 *
1407 * Inputs : instance - instance of driver, *phase - pointer to
1408 * what phase is expected, *count - pointer to number of
1409 * bytes to transfer, **data - pointer to data pointer.
1410 *
1411 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001412 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * is in same phase.
1414 *
1415 * Also, *phase, *count, *data are modified in place.
1416 *
1417 * XXX Note : handling for bus free may be useful.
1418 */
1419
1420/*
1421 * Note : this code is not as quick as it could be, however it
1422 * IS 100% reliable, and for the actual data transfer where speed
1423 * counts, we will always do a pseudo DMA or DMA transfer.
1424 */
1425
1426static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 unsigned char p = *phase, tmp;
1428 int c = *count;
1429 unsigned char *d = *data;
1430 /*
1431 * RvC: some administrative data to process polling time
1432 */
1433 int break_allowed = 0;
1434 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001437 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001439 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
1442 * The NCR5380 chip will only drive the SCSI bus when the
1443 * phase specified in the appropriate bits of the TARGET COMMAND
1444 * REGISTER match the STATUS REGISTER
1445 */
1446
1447 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1448
1449 /* RvC: don't know if this is necessary, but other SCSI I/O is short
1450 * so breaks are not necessary there
1451 */
1452 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
1453 break_allowed = 1;
1454 }
1455 do {
1456 /*
1457 * Wait for assertion of REQ, after which the phase bits will be
1458 * valid
1459 */
1460
1461 /* RvC: we simply poll once, after that we stop temporarily
1462 * and let the device buffer fill up
1463 * if breaking is not allowed, we keep polling as long as needed
1464 */
1465
1466 /* FIXME */
1467 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
1468 if (!(tmp & SR_REQ)) {
1469 /* timeout condition */
1470 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
1471 break;
1472 }
1473
Finn Thain52a6a1c2014-03-18 11:42:18 +11001474 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* Check for phase mismatch */
1477 if ((tmp & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001478 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1480 break;
1481 }
1482 /* Do actual transfer from SCSI bus to / from memory */
1483 if (!(p & SR_IO))
1484 NCR5380_write(OUTPUT_DATA_REG, *d);
1485 else
1486 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1487
1488 ++d;
1489
1490 /*
1491 * The SCSI standard suggests that in MSGOUT phase, the initiator
1492 * should drop ATN on the last byte of the message phase
1493 * after REQ has been asserted for the handshake but before
1494 * the initiator raises ACK.
1495 */
1496
1497 if (!(p & SR_IO)) {
1498 if (!((p & SR_MSG) && c > 1)) {
1499 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1500 NCR5380_dprint(NDEBUG_PIO, instance);
1501 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1502 } else {
1503 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1504 NCR5380_dprint(NDEBUG_PIO, instance);
1505 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1506 }
1507 } else {
1508 NCR5380_dprint(NDEBUG_PIO, instance);
1509 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1510 }
1511
1512 /* FIXME - if this fails bus reset ?? */
1513 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001514 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516/*
1517 * We have several special cases to consider during REQ/ACK handshaking :
1518 * 1. We were in MSGOUT phase, and we are on the last byte of the
1519 * message. ATN must be dropped as ACK is dropped.
1520 *
1521 * 2. We are in a MSGIN phase, and we are on the last byte of the
1522 * message. We must exit with ACK asserted, so that the calling
1523 * code may raise ATN before dropping ACK to reject the message.
1524 *
1525 * 3. ACK and ATN are clear and the target may proceed as normal.
1526 */
1527 if (!(p == PHASE_MSGIN && c == 1)) {
1528 if (p == PHASE_MSGOUT && c > 1)
1529 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1530 else
1531 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1532 }
1533 } while (--c);
1534
Finn Thain52a6a1c2014-03-18 11:42:18 +11001535 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 *count = c;
1538 *data = d;
1539 tmp = NCR5380_read(STATUS_REG);
1540 if (tmp & SR_REQ)
1541 *phase = tmp & PHASE_MASK;
1542 else
1543 *phase = PHASE_UNKNOWN;
1544
1545 if (!c || (*phase == p))
1546 return 0;
1547 else
1548 return -1;
1549}
1550
1551/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001552 * do_reset - issue a reset command
1553 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001555 * Issue a reset sequence to the NCR5380 and try and get the bus
1556 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001558 * This clears the reset interrupt flag because there may be no handler for
1559 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1560 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 */
1562
Finn Thain54d8fe42016-01-03 16:05:06 +11001563static void do_reset(struct Scsi_Host *instance)
1564{
Finn Thain636b1ec2016-01-03 16:05:10 +11001565 unsigned long flags;
1566
1567 local_irq_save(flags);
1568 NCR5380_write(TARGET_COMMAND_REG,
1569 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001571 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001573 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1574 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/*
1578 * Function : do_abort (Scsi_Host *host)
1579 *
1580 * Purpose : abort the currently established nexus. Should only be
1581 * called from a routine which can drop into a
1582 *
1583 * Returns : 0 on success, -1 on failure.
1584 *
1585 * Locks: queue lock held by caller
1586 * FIXME: sort this out and get new_eh running
1587 */
1588
Finn Thain54d8fe42016-01-03 16:05:06 +11001589static int do_abort(struct Scsi_Host *instance)
1590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 unsigned char *msgptr, phase, tmp;
1592 int len;
1593 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /* Request message out phase */
1596 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1597
1598 /*
1599 * Wait for the target to indicate a valid phase by asserting
1600 * REQ. Once this happens, we'll have either a MSGOUT phase
1601 * and can immediately send the ABORT message, or we'll have some
1602 * other phase and will have to source/sink data.
1603 *
1604 * We really don't care what value was on the bus or what value
1605 * the target sees, so we just handshake.
1606 */
1607
Finn Thain54d8fe42016-01-03 16:05:06 +11001608 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 if(rc < 0)
1611 return -1;
1612
1613 tmp = (unsigned char)rc;
1614
1615 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1616
1617 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1618 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001619 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1621 if(rc == -1)
1622 return -1;
1623 }
1624 tmp = ABORT;
1625 msgptr = &tmp;
1626 len = 1;
1627 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001628 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 /*
1631 * If we got here, and the command completed successfully,
1632 * we're about to go into bus free state.
1633 */
1634
1635 return len ? -1 : 0;
1636}
1637
1638#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1639/*
1640 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1641 * unsigned char *phase, int *count, unsigned char **data)
1642 *
1643 * Purpose : transfers data in given phase using either real
1644 * or pseudo DMA.
1645 *
1646 * Inputs : instance - instance of driver, *phase - pointer to
1647 * what phase is expected, *count - pointer to number of
1648 * bytes to transfer, **data - pointer to data pointer.
1649 *
1650 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001651 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * is in same phase.
1653 *
1654 * Also, *phase, *count, *data are modified in place.
1655 *
1656 * Locks: io_request lock held by caller
1657 */
1658
1659
1660static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 register int c = *count;
1662 register unsigned char p = *phase;
1663 register unsigned char *d = *data;
1664 unsigned char tmp;
1665 int foo;
1666#if defined(REAL_DMA_POLL)
1667 int cnt, toPIO;
1668 unsigned char saved_data = 0, overrun = 0, residue;
1669#endif
1670
1671 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1674 *phase = tmp;
1675 return -1;
1676 }
1677#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1678#ifdef READ_OVERRUNS
1679 if (p & SR_IO) {
1680 c -= 2;
1681 }
1682#endif
Finn Thain52a6a1c2014-03-18 11:42:18 +11001683 dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1685#endif
1686
1687 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1688
1689#ifdef REAL_DMA
1690 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1691#elif defined(REAL_DMA_POLL)
1692 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1693#else
1694 /*
1695 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1696 * were not disabled for the duration of a single DMA transfer, from
1697 * before the setting of DMA mode to after transfer of the last byte.
1698 */
1699
1700#if defined(PSEUDO_DMA) && defined(UNSAFE)
1701 spin_unlock_irq(instance->host_lock);
1702#endif
1703 /* KLL May need eop and parity in 53c400 */
1704 if (hostdata->flags & FLAG_NCR53C400)
Roel Kluin085267a2010-08-10 18:01:11 -07001705 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1706 MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1707 MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 else
1709 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1710#endif /* def REAL_DMA */
1711
Finn Thain52a6a1c2014-03-18 11:42:18 +11001712 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 /*
1715 * On the PAS16 at least I/O recovery delays are not needed here.
1716 * Everyone else seems to want them.
1717 */
1718
1719 if (p & SR_IO) {
1720 io_recovery_delay(1);
1721 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1722 } else {
1723 io_recovery_delay(1);
1724 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1725 io_recovery_delay(1);
1726 NCR5380_write(START_DMA_SEND_REG, 0);
1727 io_recovery_delay(1);
1728 }
1729
1730#if defined(REAL_DMA_POLL)
1731 do {
1732 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1733 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1734
1735/*
1736 At this point, either we've completed DMA, or we have a phase mismatch,
1737 or we've unexpectedly lost BUSY (which is a real error).
1738
1739 For write DMAs, we want to wait until the last byte has been
1740 transferred out over the bus before we turn off DMA mode. Alas, there
1741 seems to be no terribly good way of doing this on a 5380 under all
1742 conditions. For non-scatter-gather operations, we can wait until REQ
1743 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1744 are nastier, since the device will be expecting more data than we
1745 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1746 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1747 why this signal was added to the newer chips) but on the older 538[01]
1748 this signal does not exist. The workaround for this lack is a watchdog;
1749 we bail out of the wait-loop after a modest amount of wait-time if
1750 the usual exit conditions are not met. Not a terribly clean or
1751 correct solution :-%
1752
1753 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1754 If the chip is in DMA mode for an READ, it will respond to a target's
1755 REQ by latching the SCSI data into the INPUT DATA register and asserting
1756 ACK, even if it has _already_ been notified by the DMA controller that
1757 the current DMA transfer has completed! If the NCR5380 is then taken
1758 out of DMA mode, this already-acknowledged byte is lost.
1759
1760 This is not a problem for "one DMA transfer per command" reads, because
1761 the situation will never arise... either all of the data is DMA'ed
1762 properly, or the target switches to MESSAGE IN phase to signal a
1763 disconnection (either operation bringing the DMA to a clean halt).
1764 However, in order to handle scatter-reads, we must work around the
1765 problem. The chosen fix is to DMA N-2 bytes, then check for the
1766 condition before taking the NCR5380 out of DMA mode. One or two extra
1767 bytes are transferred via PIO as necessary to fill out the original
1768 request.
1769 */
1770
1771 if (p & SR_IO) {
1772#ifdef READ_OVERRUNS
1773 udelay(10);
1774 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1775 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1776 overrun = 1;
1777 }
1778#endif
1779 } else {
1780 int limit = 100;
1781 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1782 if (!(tmp & BASR_PHASE_MATCH))
1783 break;
1784 if (--limit < 0)
1785 break;
1786 }
1787 }
1788
Finn Thain52a6a1c2014-03-18 11:42:18 +11001789 dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 NCR5380_write(MODE_REG, MR_BASE);
1792 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1793
1794 residue = NCR5380_dma_residual(instance);
1795 c -= residue;
1796 *count -= c;
1797 *data += c;
1798 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1799
1800#ifdef READ_OVERRUNS
1801 if (*phase == p && (p & SR_IO) && residue == 0) {
1802 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001803 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 **data = saved_data;
1805 *data += 1;
1806 *count -= 1;
1807 cnt = toPIO = 1;
1808 } else {
1809 printk("No overrun??\n");
1810 cnt = toPIO = 2;
1811 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001812 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 NCR5380_transfer_pio(instance, phase, &cnt, data);
1814 *count -= toPIO - cnt;
1815 }
1816#endif
1817
Finn Thain52a6a1c2014-03-18 11:42:18 +11001818 dprintk(NDEBUG_DMA, "Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
1820
1821#elif defined(REAL_DMA)
1822 return 0;
1823#else /* defined(REAL_DMA_POLL) */
1824 if (p & SR_IO) {
1825#ifdef DMA_WORKS_RIGHT
1826 foo = NCR5380_pread(instance, d, c);
1827#else
1828 int diff = 1;
1829 if (hostdata->flags & FLAG_NCR53C400) {
1830 diff = 0;
1831 }
1832 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1833 /*
1834 * We can't disable DMA mode after successfully transferring
1835 * what we plan to be the last byte, since that would open up
1836 * a race condition where if the target asserted REQ before
1837 * we got the DMA mode reset, the NCR5380 would have latched
1838 * an additional byte into the INPUT DATA register and we'd
1839 * have dropped it.
1840 *
1841 * The workaround was to transfer one fewer bytes than we
1842 * intended to with the pseudo-DMA read function, wait for
1843 * the chip to latch the last byte, read it, and then disable
1844 * pseudo-DMA mode.
1845 *
1846 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1847 * REQ is deasserted when ACK is asserted, and not reasserted
1848 * until ACK goes false. Since the NCR5380 won't lower ACK
1849 * until DACK is asserted, which won't happen unless we twiddle
1850 * the DMA port or we take the NCR5380 out of DMA mode, we
1851 * can guarantee that we won't handshake another extra
1852 * byte.
1853 */
1854
1855 if (!(hostdata->flags & FLAG_NCR53C400)) {
1856 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1857 /* Wait for clean handshake */
1858 while (NCR5380_read(STATUS_REG) & SR_REQ);
1859 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1860 }
1861 }
1862#endif
1863 } else {
1864#ifdef DMA_WORKS_RIGHT
1865 foo = NCR5380_pwrite(instance, d, c);
1866#else
1867 int timeout;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001868 dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1870 /*
1871 * Wait for the last byte to be sent. If REQ is being asserted for
1872 * the byte we're interested, we'll ACK it and it will go false.
1873 */
1874 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
1875 timeout = 20000;
1876 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
1877
1878 if (!timeout)
Finn Thain52a6a1c2014-03-18 11:42:18 +11001879 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
1882 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1883 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
1884 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001885 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 }
1888 } else {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001889 dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
Finn Thain52a6a1c2014-03-18 11:42:18 +11001891 dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893 }
1894#endif
1895 }
1896 NCR5380_write(MODE_REG, MR_BASE);
1897 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1898
1899 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001900 dprintk(NDEBUG_C400_PWRITE, "53C400w: Checking for IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001902 dprintk(NDEBUG_C400_PWRITE, "53C400w: got it, reading reset interrupt reg\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1904 } else {
1905 printk("53C400w: IRQ NOT THERE!\n");
1906 }
1907 }
1908 *data = d + c;
1909 *count = 0;
1910 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1911#if defined(PSEUDO_DMA) && defined(UNSAFE)
1912 spin_lock_irq(instance->host_lock);
1913#endif /* defined(REAL_DMA_POLL) */
1914 return foo;
1915#endif /* def REAL_DMA */
1916}
1917#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1918
1919/*
1920 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1921 *
1922 * Purpose : run through the various SCSI phases and do as the target
1923 * directs us to. Operates on the currently connected command,
1924 * instance->connected.
1925 *
1926 * Inputs : instance, instance for which we are doing commands
1927 *
1928 * Side effects : SCSI things happen, the disconnected queue will be
1929 * modified if a command disconnects, *instance->connected will
1930 * change.
1931 *
1932 * XXX Note : we need to watch for bus free or a reset condition here
1933 * to recover from an unexpected bus free condition.
1934 *
1935 * Locks: io_request_lock held by caller in IRQ mode
1936 */
1937
1938static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1940 unsigned char msgout = NOP;
1941 int sink = 0;
1942 int len;
1943#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1944 int transfersize;
1945#endif
1946 unsigned char *data;
1947 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001948 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /* RvC: we need to set the end of the polling time */
1950 unsigned long poll_time = jiffies + USLEEP_POLL;
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 while (1) {
1953 tmp = NCR5380_read(STATUS_REG);
1954 /* We only have a valid SCSI phase when REQ is asserted */
1955 if (tmp & SR_REQ) {
1956 phase = (tmp & PHASE_MASK);
1957 if (phase != old_phase) {
1958 old_phase = phase;
1959 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1960 }
1961 if (sink && (phase != PHASE_MSGOUT)) {
1962 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1963
1964 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1965 while (NCR5380_read(STATUS_REG) & SR_REQ);
1966 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1967 sink = 0;
1968 continue;
1969 }
1970 switch (phase) {
1971 case PHASE_DATAIN:
1972 case PHASE_DATAOUT:
1973#if (NDEBUG & NDEBUG_NO_DATAOUT)
1974 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1975 sink = 1;
1976 do_abort(instance);
1977 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001978 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return;
1980#endif
1981 /*
1982 * If there is no room left in the current buffer in the
1983 * scatter-gather list, move onto the next one.
1984 */
1985
1986 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1987 ++cmd->SCp.buffer;
1988 --cmd->SCp.buffers_residual;
1989 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001990 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001991 dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 /*
1994 * The preferred transfer method is going to be
1995 * PSEUDO-DMA for systems that are strictly PIO,
1996 * since we can let the hardware do the handshaking.
1997 *
1998 * For this to work, we need to know the transfersize
1999 * ahead of time, since the pseudo-DMA code will sit
2000 * in an unconditional loop.
2001 */
2002
2003#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2004 /* KLL
2005 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2006 * driver then it will always be defined, so the
2007 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2008 * NCR5380 case. I think this is a fairly clean solution.
2009 * We supplement these 2 if's with the flag.
2010 */
2011#ifdef NCR5380_dma_xfer_len
2012 if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2013#else
2014 transfersize = cmd->transfersize;
2015
2016#ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2017 if (transfersize > 512)
2018 transfersize = 512;
2019#endif /* LIMIT_TRANSFERSIZE */
2020
2021 if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
2022 /* Limit transfers to 32K, for xx400 & xx406
2023 * pseudoDMA that transfers in 128 bytes blocks. */
2024 if (transfersize > 32 * 1024)
2025 transfersize = 32 * 1024;
2026#endif
2027 len = transfersize;
2028 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
2029 /*
2030 * If the watchdog timer fires, all future accesses to this
2031 * device will use the polled-IO.
2032 */
Jeff Garzik017560f2005-10-24 18:04:36 -04002033 scmd_printk(KERN_INFO, cmd,
2034 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 sink = 1;
2037 do_abort(instance);
2038 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002039 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* XXX - need to source or sink data here, as appropriate */
2041 } else
2042 cmd->SCp.this_residual -= transfersize - len;
2043 } else
2044#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2045 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
2046 &cmd->SCp.ptr);
2047 break;
2048 case PHASE_MSGIN:
2049 len = 1;
2050 data = &tmp;
2051 NCR5380_transfer_pio(instance, &phase, &len, &data);
2052 cmd->SCp.Message = tmp;
2053
2054 switch (tmp) {
2055 /*
2056 * Linking lets us reduce the time required to get the
2057 * next command out to the device, hopefully this will
2058 * mean we don't waste another revolution due to the delays
2059 * required by ARBITRATION and another SELECTION.
2060 *
2061 * In the current implementation proposal, low level drivers
2062 * merely have to start the next command, pointed to by
2063 * next_link, done() is called as with unlinked commands.
2064 */
2065#ifdef LINKED
2066 case LINKED_CMD_COMPLETE:
2067 case LINKED_FLG_CMD_COMPLETE:
2068 /* Accept message by clearing ACK */
2069 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002070 dprintk(NDEBUG_LINKED, "scsi%d : target %d lun %llu linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /*
2072 * Sanity check : A linked command should only terminate with
2073 * one of these messages if there are more linked commands
2074 * available.
2075 */
2076 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002077 printk("scsi%d : target %d lun %llu linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 sink = 1;
2079 do_abort(instance);
2080 return;
2081 }
2082 initialize_SCp(cmd->next_link);
2083 /* The next command is still part of this process */
2084 cmd->next_link->tag = cmd->tag;
2085 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002086 dprintk(NDEBUG_LINKED, "scsi%d : target %d lun %llu linked request done, calling scsi_done().\n", instance->host_no, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 cmd->scsi_done(cmd);
2088 cmd = hostdata->connected;
2089 break;
2090#endif /* def LINKED */
2091 case ABORT:
2092 case COMMAND_COMPLETE:
2093 /* Accept message by clearing ACK */
2094 sink = 1;
2095 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2096 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002097 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
2098 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /*
2101 * I'm not sure what the correct thing to do here is :
2102 *
2103 * If the command that just executed is NOT a request
2104 * sense, the obvious thing to do is to set the result
2105 * code to the values of the stored parameters.
2106 *
2107 * If it was a REQUEST SENSE command, we need some way
2108 * to differentiate between the failure code of the original
2109 * and the failure code of the REQUEST sense - the obvious
2110 * case is success, where we fall through and leave the result
2111 * code unchanged.
2112 *
2113 * The non-obvious place is where the REQUEST SENSE failed
2114 */
2115
2116 if (cmd->cmnd[0] != REQUEST_SENSE)
2117 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2118 else if (status_byte(cmd->SCp.Status) != GOOD)
2119 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2120
Boaz Harrosh28424d32007-09-10 22:37:45 +03002121 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2122 hostdata->ses.cmd_len) {
2123 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2124 hostdata->ses.cmd_len = 0 ;
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Boaz Harrosh28424d32007-09-10 22:37:45 +03002127 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
2128 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
2129
Finn Thain52a6a1c2014-03-18 11:42:18 +11002130 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 LIST(cmd, hostdata->issue_queue);
2133 cmd->host_scribble = (unsigned char *)
2134 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11002135 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002136 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11002137 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 cmd->scsi_done(cmd);
2139 }
2140
2141 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2142 /*
2143 * Restore phase bits to 0 so an interrupted selection,
2144 * arbitration can resume.
2145 */
2146 NCR5380_write(TARGET_COMMAND_REG, 0);
2147
2148 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2149 barrier();
2150 return;
2151 case MESSAGE_REJECT:
2152 /* Accept message by clearing ACK */
2153 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2154 switch (hostdata->last_message) {
2155 case HEAD_OF_QUEUE_TAG:
2156 case ORDERED_QUEUE_TAG:
2157 case SIMPLE_QUEUE_TAG:
2158 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002159 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 break;
2161 default:
2162 break;
2163 }
2164 case DISCONNECT:{
2165 /* Accept message by clearing ACK */
2166 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2167 cmd->device->disconnect = 1;
2168 LIST(cmd, hostdata->disconnected_queue);
2169 cmd->host_scribble = (unsigned char *)
2170 hostdata->disconnected_queue;
2171 hostdata->connected = NULL;
2172 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002173 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d lun %llu was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /*
2175 * Restore phase bits to 0 so an interrupted selection,
2176 * arbitration can resume.
2177 */
2178 NCR5380_write(TARGET_COMMAND_REG, 0);
2179
2180 /* Enable reselect interrupts */
2181 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2182 /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
2183 /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
2184 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2185 barrier();
2186 return;
2187 }
2188 /*
2189 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2190 * operation, in violation of the SCSI spec so we can safely
2191 * ignore SAVE/RESTORE pointers calls.
2192 *
2193 * Unfortunately, some disks violate the SCSI spec and
2194 * don't issue the required SAVE_POINTERS message before
2195 * disconnecting, and we have to break spec to remain
2196 * compatible.
2197 */
2198 case SAVE_POINTERS:
2199 case RESTORE_POINTERS:
2200 /* Accept message by clearing ACK */
2201 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2202 break;
2203 case EXTENDED_MESSAGE:
2204/*
2205 * Extended messages are sent in the following format :
2206 * Byte
2207 * 0 EXTENDED_MESSAGE == 1
2208 * 1 length (includes one byte for code, doesn't
2209 * include first two bytes)
2210 * 2 code
2211 * 3..length+1 arguments
2212 *
2213 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002214 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
2216 extended_msg[0] = EXTENDED_MESSAGE;
2217 /* Accept first byte by clearing ACK */
2218 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002219 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 len = 2;
2222 data = extended_msg + 1;
2223 phase = PHASE_MSGIN;
2224 NCR5380_transfer_pio(instance, &phase, &len, &data);
2225
Finn Thain52a6a1c2014-03-18 11:42:18 +11002226 dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2229 /* Accept third byte by clearing ACK */
2230 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2231 len = extended_msg[1] - 1;
2232 data = extended_msg + 3;
2233 phase = PHASE_MSGIN;
2234
2235 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002236 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 switch (extended_msg[2]) {
2239 case EXTENDED_SDTR:
2240 case EXTENDED_WDTR:
2241 case EXTENDED_MODIFY_DATA_POINTER:
2242 case EXTENDED_EXTENDED_IDENTIFY:
2243 tmp = 0;
2244 }
2245 } else if (len) {
2246 printk("scsi%d: error receiving extended message\n", instance->host_no);
2247 tmp = 0;
2248 } else {
2249 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2250 tmp = 0;
2251 }
2252 /* Fall through to reject message */
2253
2254 /*
2255 * If we get something weird that we aren't expecting,
2256 * reject it.
2257 */
2258 default:
2259 if (!tmp) {
2260 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002261 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 printk("\n");
2263 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002264 scmd_printk(KERN_INFO, cmd,
2265 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002267 scmd_printk(KERN_INFO, cmd,
2268 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 msgout = MESSAGE_REJECT;
2271 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2272 break;
2273 } /* switch (tmp) */
2274 break;
2275 case PHASE_MSGOUT:
2276 len = 1;
2277 data = &msgout;
2278 hostdata->last_message = msgout;
2279 NCR5380_transfer_pio(instance, &phase, &len, &data);
2280 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002281 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 hostdata->connected = NULL;
2283 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 cmd->scsi_done(cmd);
2285 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2286 return;
2287 }
2288 msgout = NOP;
2289 break;
2290 case PHASE_CMDOUT:
2291 len = cmd->cmd_len;
2292 data = cmd->cmnd;
2293 /*
2294 * XXX for performance reasons, on machines with a
2295 * PSEUDO-DMA architecture we should probably
2296 * use the dma transfer function.
2297 */
2298 NCR5380_transfer_pio(instance, &phase, &len, &data);
2299 if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
2300 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002301 dprintk(NDEBUG_USLEEP, "scsi%d : issued command, sleeping until %lu\n", instance->host_no, hostdata->time_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return;
2303 }
2304 break;
2305 case PHASE_STATIN:
2306 len = 1;
2307 data = &tmp;
2308 NCR5380_transfer_pio(instance, &phase, &len, &data);
2309 cmd->SCp.Status = tmp;
2310 break;
2311 default:
2312 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002313 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 } /* switch(phase) */
2315 } /* if (tmp * SR_REQ) */
2316 else {
2317 /* RvC: go to sleep if polling time expired
2318 */
2319 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
2320 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002321 dprintk(NDEBUG_USLEEP, "scsi%d : poll timed out, sleeping until %lu\n", instance->host_no, hostdata->time_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return;
2323 }
2324 }
2325 } /* while (1) */
2326}
2327
2328/*
2329 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2330 *
2331 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002332 * 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 -07002333 * nexus has been reestablished,
2334 *
2335 * Inputs : instance - this instance of the NCR5380.
2336 *
2337 * Locks: io_request_lock held by caller if IRQ driven
2338 */
2339
2340static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2342 instance->hostdata;
2343 unsigned char target_mask;
2344 unsigned char lun, phase;
2345 int len;
2346 unsigned char msg[3];
2347 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002348 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 int abort = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /*
2352 * Disable arbitration, etc. since the host adapter obviously
2353 * lost, and tell an interrupted NCR5380_select() to restart.
2354 */
2355
2356 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002359 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 /*
2362 * At this point, we have detected that our SCSI ID is on the bus,
2363 * SEL is true and BSY was false for at least one bus settle delay
2364 * (400 ns).
2365 *
2366 * We must assert BSY ourselves, until the target drops the SEL
2367 * signal.
2368 */
2369
2370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2371
2372 /* FIXME: timeout too long, must fail to workqueue */
2373 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2374 abort = 1;
2375
2376 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2377
2378 /*
2379 * Wait for target to go into MSGIN.
2380 * FIXME: timeout needed and fail to work queeu
2381 */
2382
2383 if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
2384 abort = 1;
2385
2386 len = 1;
2387 data = msg;
2388 phase = PHASE_MSGIN;
2389 NCR5380_transfer_pio(instance, &phase, &len, &data);
2390
2391 if (!(msg[0] & 0x80)) {
2392 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002393 spi_print_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 abort = 1;
2395 } else {
2396 /* Accept message by clearing ACK */
2397 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2398 lun = (msg[0] & 0x07);
2399
2400 /*
2401 * We need to add code for SCSI-II to track which devices have
2402 * I_T_L_Q nexuses established, and which have simple I_T_L
2403 * nexuses so we can chose to do additional data transfer.
2404 */
2405
2406 /*
2407 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2408 * just reestablished, and remove it from the disconnected queue.
2409 */
2410
2411
Finn Thain710ddd02014-11-12 16:12:02 +11002412 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002413 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 ) {
2415 if (prev) {
2416 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2417 prev->host_scribble = tmp->host_scribble;
2418 } else {
2419 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002420 hostdata->disconnected_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 tmp->host_scribble = NULL;
2423 break;
2424 }
2425 if (!tmp) {
2426 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2427 /*
2428 * Since we have an established nexus that we can't do anything with,
2429 * we must abort it.
2430 */
2431 abort = 1;
2432 }
2433 }
2434
2435 if (abort) {
2436 do_abort(instance);
2437 } else {
2438 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002439 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n", instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441}
2442
2443/*
2444 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2445 *
2446 * Purpose : called by interrupt handler when DMA finishes or a phase
2447 * mismatch occurs (which would finish the DMA transfer).
2448 *
2449 * Inputs : instance - this instance of the NCR5380.
2450 *
Finn Thain710ddd02014-11-12 16:12:02 +11002451 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 * nexus has been reestablished, on failure NULL is returned.
2453 */
2454
2455#ifdef REAL_DMA
2456static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002457 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /*
2461 * XXX this might not be right.
2462 *
2463 * Wait for final byte to transfer, ie wait for ACK to go false.
2464 *
2465 * We should use the Last Byte Sent bit, unfortunately this is
2466 * not available on the 5380/5381 (only the various CMOS chips)
2467 *
2468 * FIXME: timeout, and need to handle long timeout/irq case
2469 */
2470
2471 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2472
2473 NCR5380_write(MODE_REG, MR_BASE);
2474 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2475
2476 /*
2477 * The only places we should see a phase mismatch and have to send
2478 * data from the same set of pointers will be the data transfer
2479 * phases. So, residual, requested length are only important here.
2480 */
2481
2482 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2483 transferred = instance->dmalen - NCR5380_dma_residual();
2484 hostdata->connected->SCp.this_residual -= transferred;
2485 hostdata->connected->SCp.ptr += transferred;
2486 }
2487}
2488#endif /* def REAL_DMA */
2489
2490/*
Finn Thain710ddd02014-11-12 16:12:02 +11002491 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 *
2493 * Purpose : abort a command
2494 *
Finn Thain710ddd02014-11-12 16:12:02 +11002495 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002496 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 * used.
2498 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002499 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002501 * XXX - there is no way to abort the command that is currently
2502 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 * a problem, we could implement longjmp() / setjmp(), setjmp()
2504 * called where the loop started in NCR5380_main().
2505 *
2506 * Locks: host lock taken by caller
2507 */
2508
Finn Thain710ddd02014-11-12 16:12:02 +11002509static int NCR5380_abort(struct scsi_cmnd *cmd)
2510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct Scsi_Host *instance = cmd->device->host;
2512 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002513 struct scsi_cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002514
2515 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 NCR5380_print_status(instance);
2518
Finn Thain52a6a1c2014-03-18 11:42:18 +11002519 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2520 dprintk(NDEBUG_ABORT, " basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522#if 0
2523/*
2524 * Case 1 : If the command is the currently executing command,
2525 * we'll set the aborted flag and return control so that
2526 * information transfer routine can exit cleanly.
2527 */
2528
2529 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002530 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531/*
2532 * We should perform BSY checking, and make sure we haven't slipped
2533 * into BUS FREE.
2534 */
2535
2536 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2537/*
2538 * Since we can't change phases until we've completed the current
2539 * handshake, we have to source or sink a byte of data if the current
2540 * phase is not MSGOUT.
2541 */
2542
2543/*
2544 * Return control to the executing NCR drive so we can clear the
2545 * aborted flag and get back into our main loop.
2546 */
2547
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002548 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550#endif
2551
2552/*
2553 * Case 2 : If the command hasn't been issued yet, we simply remove it
2554 * from the issue queue.
2555 */
2556
Finn Thain52a6a1c2014-03-18 11:42:18 +11002557 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002558 for (prev = (struct scsi_cmnd **) &(hostdata->issue_queue), tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (cmd == tmp) {
2560 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002561 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 tmp->host_scribble = NULL;
2563 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002564 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002565 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return SUCCESS;
2567 }
2568#if (NDEBUG & NDEBUG_ABORT)
2569 /* KLL */
2570 else if (prev == tmp)
2571 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2572#endif
2573
2574/*
2575 * Case 3 : If any commands are connected, we're going to fail the abort
2576 * and let the high level SCSI driver retry at a later time or
2577 * issue a reset.
2578 *
2579 * Timeouts, and therefore aborted commands, will be highly unlikely
2580 * and handling them cleanly in this situation would make the common
2581 * case of noresets less efficient, and would pollute our code. So,
2582 * we fail.
2583 */
2584
2585 if (hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002586 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return FAILED;
2588 }
2589/*
2590 * Case 4: If the command is currently disconnected from the bus, and
2591 * there are no connected commands, we reconnect the I_T_L or
2592 * I_T_L_Q nexus associated with it, go into message out, and send
2593 * an abort message.
2594 *
2595 * This case is especially ugly. In order to reestablish the nexus, we
2596 * need to call NCR5380_select(). The easiest way to implement this
2597 * function was to abort if the bus was busy, and let the interrupt
2598 * handler triggered on the SEL for reselect take care of lost arbitrations
2599 * where necessary, meaning interrupts need to be enabled.
2600 *
2601 * When interrupts are enabled, the queues may change - so we
2602 * can't remove it from the disconnected queue before selecting it
2603 * because that could cause a failure in hashing the nexus if that
2604 * device reselected.
2605 *
2606 * Since the queues may change, we can't use the pointers from when we
2607 * first locate it.
2608 *
2609 * So, we must first locate the command, and if NCR5380_select()
2610 * succeeds, then issue the abort, relocate the command and remove
2611 * it from the disconnected queue.
2612 */
2613
Finn Thain710ddd02014-11-12 16:12:02 +11002614 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002616 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Finn Thain76f13b92014-11-12 16:11:53 +11002618 if (NCR5380_select(instance, cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return FAILED;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002620 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 do_abort(instance);
2623
Finn Thain710ddd02014-11-12 16:12:02 +11002624 for (prev = (struct scsi_cmnd **) &(hostdata->disconnected_queue), tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (cmd == tmp) {
2626 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002627 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 tmp->host_scribble = NULL;
2629 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002630 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return SUCCESS;
2632 }
2633 }
2634/*
2635 * Case 5 : If we reached this point, the command was not found in any of
2636 * the queues.
2637 *
2638 * We probably reached this point because of an unlikely race condition
2639 * between the command completing successfully and the abortion code,
2640 * so we won't panic, but we will notify the user in case something really
2641 * broke.
2642 */
2643 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2644 " before abortion\n", instance->host_no);
2645 return FAILED;
2646}
2647
2648
Finn Thain3be1b3e2016-01-03 16:05:12 +11002649/**
2650 * NCR5380_bus_reset - reset the SCSI bus
2651 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002653 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 */
2655
Finn Thain710ddd02014-11-12 16:12:02 +11002656static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002657{
2658 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002660 spin_lock_irq(instance->host_lock);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002661
2662#if (NDEBUG & NDEBUG_ANY)
2663 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2664 NCR5380_print_status(instance);
2665#endif
2666
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002667 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002668
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002669 spin_unlock_irq(instance->host_lock);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return SUCCESS;
2672}