blob: e17e64e3e0d9657f157ecf6c50ee7a03ddb9930f [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 *
14 * DISTRIBUTION RELEASE 6.
15 *
16 * For more information, please consult
17 *
18 * NCR 5380 Family
19 * SCSI Protocol Controller
20 * Databook
21 *
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
25 * 1+ (719) 578-3400
26 * 1+ (800) 334-5454
27 */
28
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Revision 1.10 1998/9/2 Alan Cox
Alan Coxfa195af2008-10-27 15:16:36 +000031 * (alan@lxorguk.ukuu.org.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Fixed up the timer lockups reported so far. Things still suck. Looking
33 * forward to 2.3 and per device request queues. Then it'll be possible to
34 * SMP thread this beast and improve life no end.
35
36 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
37 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
38 * (hopefully) fixed and enhanced USLEEP
39 * added support for DTC3181E card (for Mustek scanner)
40 *
41
42 * Revision 1.8 Ingmar Baumgart
43 * (ingmar@gonzo.schwaben.de)
44 * added support for NCR53C400a card
45 *
46
47 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
48 * added proc_info
49 * added support needed for DTC 3180/3280
50 * fixed a couple of bugs
51 *
52
53 * Revision 1.5 1994/01/19 09:14:57 drew
54 * Fixed udelay() hack that was being used on DATAOUT phases
55 * instead of a proper wait for the final handshake.
56 *
57 * Revision 1.4 1994/01/19 06:44:25 drew
58 * *** empty log message ***
59 *
60 * Revision 1.3 1994/01/19 05:24:40 drew
61 * Added support for TCR LAST_BYTE_SENT bit.
62 *
63 * Revision 1.2 1994/01/15 06:14:11 drew
64 * REAL DMA support, bug fixes.
65 *
66 * Revision 1.1 1994/01/15 06:00:54 drew
67 * Initial revision
68 *
69 */
70
71/*
72 * Further development / testing that should be done :
73 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
74 * code so that everything does the same thing that's done at the
75 * end of a pseudo-DMA read operation.
76 *
77 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
78 * basically, transfer size needs to be reduced by one
79 * and the last byte read as is done with PSEUDO_DMA.
80 *
81 * 4. Test SCSI-II tagged queueing (I have no devices which support
82 * tagged queueing)
83 *
84 * 5. Test linked command handling code after Eric is ready with
85 * the high level code.
86 */
db9dff32005-04-03 14:53:59 -050087#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050088#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#if (NDEBUG & NDEBUG_LISTS)
91#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
92#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); }
93#else
94#define LIST(x,y)
95#define REMOVE(w,x,y,z)
96#endif
97
98#ifndef notyet
99#undef LINKED
100#undef REAL_DMA
101#endif
102
103#ifdef REAL_DMA_POLL
104#undef READ_OVERRUNS
105#define READ_OVERRUNS
106#endif
107
108#ifdef BOARD_REQUIRES_NO_DELAY
109#define io_recovery_delay(x)
110#else
111#define io_recovery_delay(x) udelay(x)
112#endif
113
114/*
115 * Design
116 *
117 * This is a generic 5380 driver. To use it on a different platform,
118 * one simply writes appropriate system specific macros (ie, data
119 * transfer - some PC's will use the I/O bus, 68K's must use
120 * memory mapped) and drops this file in their 'C' wrapper.
121 *
122 * (Note from hch: unfortunately it was not enough for the different
123 * m68k folks and instead of improving this driver they copied it
124 * and hacked it up for their needs. As a consequence they lost
125 * most updates to this driver. Maybe someone will fix all these
126 * drivers to use a common core one day..)
127 *
128 * As far as command queueing, two queues are maintained for
129 * each 5380 in the system - commands that haven't been issued yet,
130 * and commands that are currently executing. This means that an
131 * unlimited number of commands may be queued, letting
132 * more commands propagate from the higher driver levels giving higher
133 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
134 * allowing multiple commands to propagate all the way to a SCSI-II device
135 * while a command is already executing.
136 *
137 *
138 * Issues specific to the NCR5380 :
139 *
140 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
141 * piece of hardware that requires you to sit in a loop polling for
142 * the REQ signal as long as you are connected. Some devices are
143 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
144 * while doing long seek operations.
145 *
146 * The workaround for this is to keep track of devices that have
147 * disconnected. If the device hasn't disconnected, for commands that
148 * should disconnect, we do something like
149 *
150 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
151 *
152 * Some tweaking of N and M needs to be done. An algorithm based
153 * on "time to data" would give the best results as long as short time
154 * to datas (ie, on the same track) were considered, however these
155 * broken devices are the exception rather than the rule and I'd rather
156 * spend my time optimizing for the normal case.
157 *
158 * Architecture :
159 *
160 * At the heart of the design is a coroutine, NCR5380_main,
161 * which is started from a workqueue for each NCR5380 host in the
162 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
163 * removing the commands from the issue queue and calling
164 * NCR5380_select() if a nexus is not established.
165 *
166 * Once a nexus is established, the NCR5380_information_transfer()
167 * phase goes through the various phases as instructed by the target.
168 * if the target goes into MSG IN and sends a DISCONNECT message,
169 * the command structure is placed into the per instance disconnected
170 * queue, and NCR5380_main tries to find more work. If the target is
171 * idle for too long, the system will try to sleep.
172 *
173 * If a command has disconnected, eventually an interrupt will trigger,
174 * calling NCR5380_intr() which will in turn call NCR5380_reselect
175 * to reestablish a nexus. This will run main if necessary.
176 *
177 * On command termination, the done function will be called as
178 * appropriate.
179 *
180 * SCSI pointers are maintained in the SCp field of SCSI command
181 * structures, being initialized after the command is connected
182 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
183 * Note that in violation of the standard, an implicit SAVE POINTERS operation
184 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
185 */
186
187/*
188 * Using this file :
189 * This file a skeleton Linux SCSI driver for the NCR 5380 series
190 * of chips. To use it, you write an architecture specific functions
191 * and macros and include this file in your driver.
192 *
193 * These macros control options :
194 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
195 * defined.
196 *
197 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
198 * for commands that return with a CHECK CONDITION status.
199 *
200 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
201 * transceivers.
202 *
203 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
204 * override-configure an IRQ.
205 *
206 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
207 * bytes at a time. Since interrupts are disabled by default during
208 * these transfers, we might need this to give reasonable interrupt
209 * service time if the transfer size gets too large.
210 *
211 * LINKED - if defined, linked commands are supported.
212 *
213 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
214 *
215 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
216 *
217 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
218 * rely on phase mismatch and EOP interrupts to determine end
219 * of phase.
220 *
221 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
222 * only really want to use this if you're having a problem with
223 * dropped characters during high speed communications, and even
224 * then, you're going to be better off twiddling with transfersize
225 * in the high level code.
226 *
227 * Defaults for these will be provided although the user may want to adjust
228 * these to allocate CPU resources to the SCSI driver or "real" code.
229 *
230 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
231 *
232 * USLEEP_POLL - amount of time, in jiffies, to poll
233 *
234 * These macros MUST be defined :
235 * NCR5380_local_declare() - declare any local variables needed for your
236 * transfer routines.
237 *
238 * NCR5380_setup(instance) - initialize any local variables needed from a given
239 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
240 *
241 * NCR5380_read(register) - read from the specified register
242 *
243 * NCR5380_write(register, value) - write to the specific register
244 *
245 * NCR5380_implementation_fields - additional fields needed for this
246 * specific implementation of the NCR5380
247 *
248 * Either real DMA *or* pseudo DMA may be implemented
249 * REAL functions :
250 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
251 * Note that the DMA setup functions should return the number of bytes
252 * that they were able to program the controller for.
253 *
254 * Also note that generic i386/PC versions of these macros are
255 * available as NCR5380_i386_dma_write_setup,
256 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
257 *
258 * NCR5380_dma_write_setup(instance, src, count) - initialize
259 * NCR5380_dma_read_setup(instance, dst, count) - initialize
260 * NCR5380_dma_residual(instance); - residual count
261 *
262 * PSEUDO functions :
263 * NCR5380_pwrite(instance, src, count)
264 * NCR5380_pread(instance, dst, count);
265 *
266 * The generic driver is initialized by calling NCR5380_init(instance),
267 * after setting the appropriate host specific fields and ID. If the
268 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
269 * possible) function may be used.
270 */
271
272static int do_abort(struct Scsi_Host *host);
273static void do_reset(struct Scsi_Host *host);
274
275/*
276 * initialize_SCp - init the scsi pointer field
277 * @cmd: command block to set up
278 *
279 * Set up the internal fields in the SCSI command.
280 */
281
282static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
283{
284 /*
285 * Initialize the Scsi Pointer field so that all of the commands in the
286 * various queues are valid.
287 */
288
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200289 if (scsi_bufflen(cmd)) {
290 cmd->SCp.buffer = scsi_sglist(cmd);
291 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200292 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 cmd->SCp.this_residual = cmd->SCp.buffer->length;
294 } else {
295 cmd->SCp.buffer = NULL;
296 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200297 cmd->SCp.ptr = NULL;
298 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
302/**
303 * NCR5380_poll_politely - wait for NCR5380 status bits
304 * @instance: controller to poll
305 * @reg: 5380 register to poll
306 * @bit: Bitmask to check
307 * @val: Value required to exit
308 *
309 * Polls the NCR5380 in a reasonably efficient manner waiting for
310 * an event to occur, after a short quick poll we begin giving the
311 * CPU back in non IRQ contexts
312 *
313 * Returns the value of the register or a negative error code.
314 */
315
316static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
317{
318 NCR5380_local_declare();
319 int n = 500; /* At about 8uS a cycle for the cpu access */
320 unsigned long end = jiffies + t;
321 int r;
322
323 NCR5380_setup(instance);
324
325 while( n-- > 0)
326 {
327 r = NCR5380_read(reg);
328 if((r & bit) == val)
329 return 0;
330 cpu_relax();
331 }
332
333 /* t time yet ? */
334 while(time_before(jiffies, end))
335 {
336 r = NCR5380_read(reg);
337 if((r & bit) == val)
338 return 0;
339 if(!in_interrupt())
Amol Lad730a6462007-05-23 14:41:37 -0700340 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 else
342 cpu_relax();
343 }
344 return -ETIMEDOUT;
345}
346
347static struct {
348 unsigned char value;
349 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700350} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 {PHASE_DATAOUT, "DATAOUT"},
352 {PHASE_DATAIN, "DATAIN"},
353 {PHASE_CMDOUT, "CMDOUT"},
354 {PHASE_STATIN, "STATIN"},
355 {PHASE_MSGOUT, "MSGOUT"},
356 {PHASE_MSGIN, "MSGIN"},
357 {PHASE_UNKNOWN, "UNKNOWN"}
358};
359
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100360#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static struct {
362 unsigned char mask;
363 const char *name;
364} signals[] = {
365 {SR_DBP, "PARITY"},
366 {SR_RST, "RST"},
367 {SR_BSY, "BSY"},
368 {SR_REQ, "REQ"},
369 {SR_MSG, "MSG"},
370 {SR_CD, "CD"},
371 {SR_IO, "IO"},
372 {SR_SEL, "SEL"},
373 {0, NULL}
374},
375basrs[] = {
376 {BASR_ATN, "ATN"},
377 {BASR_ACK, "ACK"},
378 {0, NULL}
379},
380icrs[] = {
381 {ICR_ASSERT_RST, "ASSERT RST"},
382 {ICR_ASSERT_ACK, "ASSERT ACK"},
383 {ICR_ASSERT_BSY, "ASSERT BSY"},
384 {ICR_ASSERT_SEL, "ASSERT SEL"},
385 {ICR_ASSERT_ATN, "ASSERT ATN"},
386 {ICR_ASSERT_DATA, "ASSERT DATA"},
387 {0, NULL}
388},
389mrs[] = {
390 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
391 {MR_TARGET, "MODE TARGET"},
392 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
393 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
394 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
395 {MR_DMA_MODE, "MODE DMA"},
396 {MR_ARBITRATE, "MODE ARBITRATION"},
397 {0, NULL}
398};
399
400/**
401 * NCR5380_print - print scsi bus signals
402 * @instance: adapter state to dump
403 *
404 * Print the SCSI bus signals for debugging purposes
405 *
406 * Locks: caller holds hostdata lock (not essential)
407 */
408
409static void NCR5380_print(struct Scsi_Host *instance)
410{
411 NCR5380_local_declare();
412 unsigned char status, data, basr, mr, icr, i;
413 NCR5380_setup(instance);
414
415 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
416 status = NCR5380_read(STATUS_REG);
417 mr = NCR5380_read(MODE_REG);
418 icr = NCR5380_read(INITIATOR_COMMAND_REG);
419 basr = NCR5380_read(BUS_AND_STATUS_REG);
420
421 printk("STATUS_REG: %02x ", status);
422 for (i = 0; signals[i].mask; ++i)
423 if (status & signals[i].mask)
424 printk(",%s", signals[i].name);
425 printk("\nBASR: %02x ", basr);
426 for (i = 0; basrs[i].mask; ++i)
427 if (basr & basrs[i].mask)
428 printk(",%s", basrs[i].name);
429 printk("\nICR: %02x ", icr);
430 for (i = 0; icrs[i].mask; ++i)
431 if (icr & icrs[i].mask)
432 printk(",%s", icrs[i].name);
433 printk("\nMODE: %02x ", mr);
434 for (i = 0; mrs[i].mask; ++i)
435 if (mr & mrs[i].mask)
436 printk(",%s", mrs[i].name);
437 printk("\n");
438}
439
440
441/*
442 * NCR5380_print_phase - show SCSI phase
443 * @instance: adapter to dump
444 *
445 * Print the current SCSI phase for debugging purposes
446 *
447 * Locks: none
448 */
449
450static void NCR5380_print_phase(struct Scsi_Host *instance)
451{
452 NCR5380_local_declare();
453 unsigned char status;
454 int i;
455 NCR5380_setup(instance);
456
457 status = NCR5380_read(STATUS_REG);
458 if (!(status & SR_REQ))
459 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
460 else {
461 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
462 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
463 }
464}
465#endif
466
467/*
468 * These need tweaking, and would probably work best as per-device
469 * flags initialized differently for disk, tape, cd, etc devices.
470 * People with broken devices are free to experiment as to what gives
471 * the best results for them.
472 *
473 * USLEEP_SLEEP should be a minimum seek time.
474 *
475 * USLEEP_POLL should be a maximum rotational latency.
476 */
477#ifndef USLEEP_SLEEP
478/* 20 ms (reasonable hard disk speed) */
479#define USLEEP_SLEEP (20*HZ/1000)
480#endif
481/* 300 RPM (floppy speed) */
482#ifndef USLEEP_POLL
483#define USLEEP_POLL (200*HZ/1000)
484#endif
485#ifndef USLEEP_WAITLONG
486/* RvC: (reasonable time to wait on select error) */
487#define USLEEP_WAITLONG USLEEP_SLEEP
488#endif
489
490/*
491 * Function : int should_disconnect (unsigned char cmd)
492 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200493 * Purpose : decide whether a command would normally disconnect or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * not, since if it won't disconnect we should go to sleep.
495 *
496 * Input : cmd - opcode of SCSI command
497 *
498 * Returns : DISCONNECT_LONG if we should disconnect for a really long
499 * time (ie always, sleep, look for REQ active, sleep),
500 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
501 * time-to-data delay, DISCONNECT_NONE if this command would return
502 * immediately.
503 *
504 * Future sleep algorithms based on time to data can exploit
505 * something like this so they can differentiate between "normal"
506 * (ie, read, write, seek) and unusual commands (ie, * format).
507 *
508 * Note : We don't deal with commands that handle an immediate disconnect,
509 *
510 */
511
512static int should_disconnect(unsigned char cmd)
513{
514 switch (cmd) {
515 case READ_6:
516 case WRITE_6:
517 case SEEK_6:
518 case READ_10:
519 case WRITE_10:
520 case SEEK_10:
521 return DISCONNECT_TIME_TO_DATA;
522 case FORMAT_UNIT:
523 case SEARCH_HIGH:
524 case SEARCH_LOW:
525 case SEARCH_EQUAL:
526 return DISCONNECT_LONG;
527 default:
528 return DISCONNECT_NONE;
529 }
530}
531
532static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
533{
534 hostdata->time_expires = jiffies + timeout;
535 schedule_delayed_work(&hostdata->coroutine, timeout);
536}
537
538
539static int probe_irq __initdata = 0;
540
541/**
542 * probe_intr - helper for IRQ autoprobe
543 * @irq: interrupt number
544 * @dev_id: unused
545 * @regs: unused
546 *
547 * Set a flag to indicate the IRQ in question was received. This is
548 * used by the IRQ probe code.
549 */
550
David Howells7d12e782006-10-05 14:55:46 +0100551static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 probe_irq = irq;
554 return IRQ_HANDLED;
555}
556
557/**
558 * NCR5380_probe_irq - find the IRQ of an NCR5380
559 * @instance: NCR5380 controller
560 * @possible: bitmask of ISA IRQ lines
561 *
562 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
563 * and then looking to see what interrupt actually turned up.
564 *
565 * Locks: none, irqs must be enabled on entry
566 */
567
Andrew Morton702809c2007-05-23 14:41:56 -0700568static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
569 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 NCR5380_local_declare();
572 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
573 unsigned long timeout;
574 int trying_irqs, i, mask;
575 NCR5380_setup(instance);
576
577 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100578 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 trying_irqs |= mask;
580
581 timeout = jiffies + (250 * HZ / 1000);
582 probe_irq = SCSI_IRQ_NONE;
583
584 /*
585 * A interrupt is triggered whenever BSY = false, SEL = true
586 * and a bit set in the SELECT_ENABLE_REG is asserted on the
587 * SCSI bus.
588 *
589 * Note that the bus is only driven when the phase control signals
590 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
591 * to zero.
592 */
593
594 NCR5380_write(TARGET_COMMAND_REG, 0);
595 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
596 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
597 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
598
599 while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800600 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 NCR5380_write(SELECT_ENABLE_REG, 0);
603 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
604
605 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
606 if (trying_irqs & mask)
607 free_irq(i, NULL);
608
609 return probe_irq;
610}
611
612/**
613 * NCR58380_print_options - show options
614 * @instance: unused for now
615 *
616 * Called by probe code indicating the NCR5380 driver options that
617 * were selected. At some point this will switch to runtime options
618 * read from the adapter in question
619 *
620 * Locks: none
621 */
622
Andrew Morton702809c2007-05-23 14:41:56 -0700623static void __init __maybe_unused
624NCR5380_print_options(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 printk(" generic options"
627#ifdef AUTOPROBE_IRQ
628 " AUTOPROBE_IRQ"
629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#ifdef DIFFERENTIAL
631 " DIFFERENTIAL"
632#endif
633#ifdef REAL_DMA
634 " REAL DMA"
635#endif
636#ifdef REAL_DMA_POLL
637 " REAL DMA POLL"
638#endif
639#ifdef PARITY
640 " PARITY"
641#endif
642#ifdef PSEUDO_DMA
643 " PSEUDO DMA"
644#endif
645#ifdef UNSAFE
646 " UNSAFE "
647#endif
648 );
Finn Thain48f16c92014-11-12 16:11:50 +1100649 printk(" USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
651 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
652 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
653 }
654}
655
656/**
657 * NCR5380_print_status - dump controller info
658 * @instance: controller to dump
659 *
660 * Print commands in the various queues, called from NCR5380_abort
661 * and NCR5380_debug to aid debugging.
662 *
663 * Locks: called functions disable irqs
664 */
665
666static void NCR5380_print_status(struct Scsi_Host *instance)
667{
668 NCR5380_dprint(NDEBUG_ANY, instance);
669 NCR5380_dprint_phase(NDEBUG_ANY, instance);
670}
671
672/******************************************/
673/*
674 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
675 *
676 * *buffer: I/O buffer
677 * **start: if inout == FALSE pointer into buffer where user read should start
678 * offset: current offset
679 * length: length of buffer
680 * hostno: Scsi_Host host_no
681 * inout: TRUE - user is writing; FALSE - user is reading
682 *
683 * Return the number of bytes read from or written
684 */
685
Al Virodd7ab712013-03-31 01:15:54 -0400686static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
687 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Virodd7ab712013-03-31 01:15:54 -0400689#ifdef DTC_PUBLIC_RELEASE
690 dtc_wmaxi = dtc_maxi = 0;
691#endif
692#ifdef PAS16_PUBLIC_RELEASE
693 pas_wmaxi = pas_maxi = 0;
694#endif
695 return (-ENOSYS); /* Currently this is a no-op */
696}
697
698#undef SPRINTF
699#define SPRINTF(args...) seq_printf(m, ## args)
700static
701void lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, struct seq_file *m);
702static
703void lprint_command(unsigned char *cmd, struct seq_file *m);
704static
705void lprint_opcode(int opcode, struct seq_file *m);
706
707static int __maybe_unused NCR5380_show_info(struct seq_file *m,
708 struct Scsi_Host *instance)
709{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct NCR5380_hostdata *hostdata;
711 Scsi_Cmnd *ptr;
712
713 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
716 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
717 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
718#ifdef DTC_PUBLIC_RELEASE
719 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
720#endif
721#ifdef T128_PUBLIC_RELEASE
722 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
723#endif
724#ifdef GENERIC_NCR5380_PUBLIC_RELEASE
725 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
726#endif
727#ifdef PAS16_PUBLIC_RELEASE
728 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
729#endif
730
731 SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
732 SPRINTF("io_port: %04x ", (int) instance->io_port);
733 if (instance->irq == SCSI_IRQ_NONE)
734 SPRINTF("IRQ: None.\n");
735 else
736 SPRINTF("IRQ: %d.\n", instance->irq);
737
738#ifdef DTC_PUBLIC_RELEASE
739 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi);
740#endif
741#ifdef PAS16_PUBLIC_RELEASE
742 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi);
743#endif
744 spin_lock_irq(instance->host_lock);
745 if (!hostdata->connected)
746 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
747 else
Al Virodd7ab712013-03-31 01:15:54 -0400748 lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
750 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400751 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
754 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400755 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_unlock_irq(instance->host_lock);
Al Virodd7ab712013-03-31 01:15:54 -0400757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Al Virodd7ab712013-03-31 01:15:54 -0400760static void lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200762 SPRINTF("scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 SPRINTF(" command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400764 lprint_command(cmd->cmnd, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Al Virodd7ab712013-03-31 01:15:54 -0400767static void lprint_command(unsigned char *command, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 int i, s;
Al Virodd7ab712013-03-31 01:15:54 -0400770 lprint_opcode(command[0], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
772 SPRINTF("%02x ", command[i]);
773 SPRINTF("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Al Virodd7ab712013-03-31 01:15:54 -0400776static void lprint_opcode(int opcode, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 SPRINTF("%2d (0x%02x)", opcode, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781
782/**
783 * NCR5380_init - initialise an NCR5380
784 * @instance: adapter to configure
785 * @flags: control flags
786 *
787 * Initializes *instance and corresponding 5380 chip,
788 * with flags OR'd into the initial flags value.
789 *
790 * Notes : I assume that the host, hostno, and id bits have been
791 * set correctly. I don't care about the irq and other fields.
792 *
793 * Returns 0 for success
794 *
795 * Locks: interrupts must be enabled when we are called
796 */
797
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800798static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 NCR5380_local_declare();
801 int i, pass;
802 unsigned long timeout;
803 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
804
805 if(in_interrupt())
806 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
807 /*
808 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
809 * the base address.
810 */
811
812#ifdef NCR53C400
813 if (flags & FLAG_NCR53C400)
814 instance->NCR5380_instance_name += NCR53C400_address_adjust;
815#endif
816
817 NCR5380_setup(instance);
818
819 hostdata->aborted = 0;
820 hostdata->id_mask = 1 << instance->this_id;
821 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
822 if (i > hostdata->id_mask)
823 hostdata->id_higher_mask |= i;
824 for (i = 0; i < 8; ++i)
825 hostdata->busy[i] = 0;
826#ifdef REAL_DMA
827 hostdata->dmalen = 0;
828#endif
829 hostdata->targets_present = 0;
830 hostdata->connected = NULL;
831 hostdata->issue_queue = NULL;
832 hostdata->disconnected_queue = NULL;
833
David Howellsc4028952006-11-22 14:57:56 +0000834 INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836#ifdef NCR5380_STATS
837 for (i = 0; i < 8; ++i) {
838 hostdata->time_read[i] = 0;
839 hostdata->time_write[i] = 0;
840 hostdata->bytes_read[i] = 0;
841 hostdata->bytes_write[i] = 0;
842 }
843 hostdata->timebase = 0;
844 hostdata->pendingw = 0;
845 hostdata->pendingr = 0;
846#endif
847
848 /* The CHECK code seems to break the 53C400. Will check it later maybe */
849 if (flags & FLAG_NCR53C400)
850 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
851 else
852 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
853
854 hostdata->host = instance;
855 hostdata->time_expires = 0;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
858 NCR5380_write(MODE_REG, MR_BASE);
859 NCR5380_write(TARGET_COMMAND_REG, 0);
860 NCR5380_write(SELECT_ENABLE_REG, 0);
861
862#ifdef NCR53C400
863 if (hostdata->flags & FLAG_NCR53C400) {
864 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
865 }
866#endif
867
868 /*
869 * Detect and correct bus wedge problems.
870 *
871 * If the system crashed, it may have crashed in a state
872 * where a SCSI command was still executing, and the
873 * SCSI bus is not in a BUS FREE STATE.
874 *
875 * If this is the case, we'll try to abort the currently
876 * established nexus which we know nothing about, and that
877 * failing, do a hard reset of the SCSI bus
878 */
879
880 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
881 switch (pass) {
882 case 1:
883 case 3:
884 case 5:
885 printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
886 timeout = jiffies + 5 * HZ;
887 NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ);
888 break;
889 case 2:
890 printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no);
891 do_abort(instance);
892 break;
893 case 4:
894 printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no);
895 do_reset(instance);
896 break;
897 case 6:
898 printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no);
899 return -ENXIO;
900 }
901 }
902 return 0;
903}
904
905/**
906 * NCR5380_exit - remove an NCR5380
907 * @instance: adapter to remove
908 */
909
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800910static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
913
Tejun Heoa684b8d2011-01-24 14:57:28 +0100914 cancel_delayed_work_sync(&hostdata->coroutine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/**
918 * NCR5380_queue_command - queue a command
919 * @cmd: SCSI command
920 * @done: completion handler
921 *
922 * cmd is added to the per instance issue_queue, with minor
923 * twiddling done to the host specific fields of cmd. If the
924 * main coroutine is not running, it is restarted.
925 *
926 * Locks: host lock taken by caller
927 */
928
Jeff Garzikf2812332010-11-16 02:10:29 -0500929static int NCR5380_queue_command_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct Scsi_Host *instance = cmd->device->host;
932 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
933 Scsi_Cmnd *tmp;
934
935#if (NDEBUG & NDEBUG_NO_WRITE)
936 switch (cmd->cmnd[0]) {
937 case WRITE_6:
938 case WRITE_10:
939 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
940 cmd->result = (DID_ERROR << 16);
941 done(cmd);
942 return 0;
943 }
944#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
945
946#ifdef NCR5380_STATS
947 switch (cmd->cmnd[0]) {
948 case WRITE:
949 case WRITE_6:
950 case WRITE_10:
951 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200952 hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 hostdata->pendingw++;
954 break;
955 case READ:
956 case READ_6:
957 case READ_10:
958 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200959 hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 hostdata->pendingr++;
961 break;
962 }
963#endif
964
965 /*
966 * We use the host_scribble field as a pointer to the next command
967 * in a queue
968 */
969
970 cmd->host_scribble = NULL;
971 cmd->scsi_done = done;
972 cmd->result = 0;
973
974 /*
975 * Insert the cmd into the issue queue. Note that REQUEST SENSE
976 * commands are added to the head of the queue since any command will
977 * clear the contingent allegiance condition that exists and the
978 * sense data is only guaranteed to be valid while the condition exists.
979 */
980
981 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
982 LIST(cmd, hostdata->issue_queue);
983 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
984 hostdata->issue_queue = cmd;
985 } else {
986 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
987 LIST(cmd, tmp);
988 tmp->host_scribble = (unsigned char *) cmd;
989 }
Finn Thain52a6a1c2014-03-18 11:42:18 +1100990 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 -0700991
992 /* Run the coroutine if it isn't already running. */
993 /* Kick off command processing */
David Howellsc4028952006-11-22 14:57:56 +0000994 schedule_delayed_work(&hostdata->coroutine, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 0;
996}
997
Jeff Garzikf2812332010-11-16 02:10:29 -0500998static DEF_SCSI_QCMD(NCR5380_queue_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000/**
1001 * NCR5380_main - NCR state machines
1002 *
1003 * NCR5380_main is a coroutine that runs as long as more work can
1004 * be done on the NCR5380 host adapters in a system. Both
1005 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1006 * in case it is not running.
1007 *
1008 * Locks: called as its own thread with no locks held. Takes the
1009 * host lock and called routines may take the isa dma lock.
1010 */
1011
David Howellsc4028952006-11-22 14:57:56 +00001012static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
David Howellsc4028952006-11-22 14:57:56 +00001014 struct NCR5380_hostdata *hostdata =
1015 container_of(work, struct NCR5380_hostdata, coroutine.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct Scsi_Host *instance = hostdata->host;
1017 Scsi_Cmnd *tmp, *prev;
1018 int done;
1019
1020 spin_lock_irq(instance->host_lock);
1021 do {
1022 /* Lock held here */
1023 done = 1;
1024 if (!hostdata->connected && !hostdata->selecting) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001025 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * Search through the issue_queue for a command destined
1028 * for a target that's not busy.
1029 */
1030 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
1031 {
1032 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001033 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 -07001034 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001035 if (!(hostdata->busy[tmp->device->id] &
1036 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (prev) {
1038 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
1039 prev->host_scribble = tmp->host_scribble;
1040 } else {
1041 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
1042 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1043 }
1044 tmp->host_scribble = NULL;
1045
1046 /*
1047 * Attempt to establish an I_T_L nexus here.
1048 * On success, instance->hostdata->connected is set.
1049 * On failure, we must add the command back to the
1050 * issue queue so we can keep trying.
1051 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001052 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 -07001053
1054 /*
1055 * A successful selection is defined as one that
1056 * leaves us with the command connected and
1057 * in hostdata->connected, OR has terminated the
1058 * command.
1059 *
1060 * With successful commands, we fall through
1061 * and see if we can do an information transfer,
1062 * with failures we will restart.
1063 */
1064 hostdata->selecting = NULL;
1065 /* RvC: have to preset this to indicate a new command is being performed */
1066
Finn Thain76f13b92014-11-12 16:11:53 +11001067 /*
1068 * REQUEST SENSE commands are issued without tagged
1069 * queueing, even on SCSI-II devices because the
1070 * contingent allegiance condition exists for the
1071 * entire unit.
1072 */
1073
1074 if (!NCR5380_select(instance, tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 } else {
1077 LIST(tmp, hostdata->issue_queue);
1078 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1079 hostdata->issue_queue = tmp;
1080 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001081 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 -07001082 }
1083 /* lock held here still */
1084 } /* if target/lun is not busy */
1085 } /* for */
1086 /* exited locked */
1087 } /* if (!hostdata->connected) */
1088 if (hostdata->selecting) {
1089 tmp = (Scsi_Cmnd *) hostdata->selecting;
1090 /* Selection will drop and retake the lock */
Finn Thain76f13b92014-11-12 16:11:53 +11001091 if (!NCR5380_select(instance, tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* Ok ?? */
1093 } else {
1094 /* RvC: device failed, so we wait a long time
1095 this is needed for Mustek scanners, that
1096 do not respond to commands immediately
1097 after a scan */
1098 printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
1099 LIST(tmp, hostdata->issue_queue);
1100 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1101 hostdata->issue_queue = tmp;
1102 NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
1103 }
1104 } /* if hostdata->selecting */
1105 if (hostdata->connected
1106#ifdef REAL_DMA
1107 && !hostdata->dmalen
1108#endif
1109 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1110 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001111 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001113 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 done = 0;
1115 } else
1116 break;
1117 } while (!done);
1118
1119 spin_unlock_irq(instance->host_lock);
1120}
1121
1122#ifndef DONT_USE_INTR
1123
1124/**
1125 * NCR5380_intr - generic NCR5380 irq handler
1126 * @irq: interrupt number
1127 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
1129 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1130 * from the disconnected queue, and restarting NCR5380_main()
1131 * as required.
1132 *
1133 * Locks: takes the needed instance locks
1134 */
1135
Jeff Garzikbaa9aac2007-12-13 16:14:14 -08001136static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 NCR5380_local_declare();
Jeff Garzikbaa9aac2007-12-13 16:14:14 -08001139 struct Scsi_Host *instance = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1141 int done;
1142 unsigned char basr;
1143 unsigned long flags;
1144
Finn Thain52a6a1c2014-03-18 11:42:18 +11001145 dprintk(NDEBUG_INTR, "scsi : NCR5380 irq %d triggered\n",
1146 instance->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 do {
1149 done = 1;
1150 spin_lock_irqsave(instance->host_lock, flags);
1151 /* Look for pending interrupts */
1152 NCR5380_setup(instance);
1153 basr = NCR5380_read(BUS_AND_STATUS_REG);
1154 /* XXX dispatch to appropriate routine if found and done=0 */
1155 if (basr & BASR_IRQ) {
1156 NCR5380_dprint(NDEBUG_INTR, instance);
1157 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1158 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001159 dprintk(NDEBUG_INTR, "scsi%d : SEL interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 NCR5380_reselect(instance);
1161 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1162 } else if (basr & BASR_PARITY_ERROR) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001163 dprintk(NDEBUG_INTR, "scsi%d : PARITY interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1165 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001166 dprintk(NDEBUG_INTR, "scsi%d : RESET interrupt\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1168 } else {
1169#if defined(REAL_DMA)
1170 /*
1171 * We should only get PHASE MISMATCH and EOP interrupts
1172 * if we have DMA enabled, so do a sanity check based on
1173 * the current setting of the MODE register.
1174 */
1175
1176 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001177 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (!hostdata->connected)
1180 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1181
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001182 transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 hostdata->connected->SCp.this_residual -= transferred;
1184 hostdata->connected->SCp.ptr += transferred;
1185 hostdata->dmalen = 0;
1186
1187 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1188
1189 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1190 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1191
1192 NCR5380_write(MODE_REG, MR_BASE);
1193 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1194 }
1195#else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001196 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 -07001197 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1198#endif
1199 }
1200 } /* if BASR_IRQ */
1201 spin_unlock_irqrestore(instance->host_lock, flags);
1202 if(!done)
David Howellsc4028952006-11-22 14:57:56 +00001203 schedule_delayed_work(&hostdata->coroutine, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 } while (!done);
1205 return IRQ_HANDLED;
1206}
1207
1208#endif
1209
1210/**
1211 * collect_stats - collect stats on a scsi command
1212 * @hostdata: adapter
1213 * @cmd: command being issued
1214 *
1215 * Update the statistical data by parsing the command in question
1216 */
1217
1218static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
1219{
1220#ifdef NCR5380_STATS
1221 switch (cmd->cmnd[0]) {
1222 case WRITE:
1223 case WRITE_6:
1224 case WRITE_10:
Jeff Garzik422c0d62005-10-24 18:05:09 -04001225 hostdata->time_write[scmd_id(cmd)] += (jiffies - hostdata->timebase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 hostdata->pendingw--;
1227 break;
1228 case READ:
1229 case READ_6:
1230 case READ_10:
Jeff Garzik422c0d62005-10-24 18:05:09 -04001231 hostdata->time_read[scmd_id(cmd)] += (jiffies - hostdata->timebase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 hostdata->pendingr--;
1233 break;
1234 }
1235#endif
1236}
1237
1238
1239/*
Finn Thain76f13b92014-11-12 16:11:53 +11001240 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 *
1242 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1243 * including ARBITRATION, SELECTION, and initial message out for
1244 * IDENTIFY and queue messages.
1245 *
1246 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001247 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 *
1249 * Returns : -1 if selection could not execute for some reason,
1250 * 0 if selection succeeded or failed because the target
1251 * did not respond.
1252 *
1253 * Side effects :
1254 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1255 * with registers as they should have been on entry - ie
1256 * SELECT_ENABLE will be set appropriately, the NCR5380
1257 * will cease to drive any SCSI bus signals.
1258 *
1259 * If successful : I_T_L or I_T_L_Q nexus will be established,
1260 * instance->connected will be set to cmd.
1261 * SELECT interrupt will be disabled.
1262 *
1263 * If failed (no target) : cmd->scsi_done() will be called, and the
1264 * cmd->result host byte set to DID_BAD_TARGET.
1265 *
1266 * Locks: caller holds hostdata lock in IRQ mode
1267 */
1268
Finn Thain76f13b92014-11-12 16:11:53 +11001269static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 NCR5380_local_declare();
1272 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1273 unsigned char tmp[3], phase;
1274 unsigned char *data;
1275 int len;
1276 unsigned long timeout;
1277 unsigned char value;
1278 int err;
1279 NCR5380_setup(instance);
1280
1281 if (hostdata->selecting)
1282 goto part2;
1283
1284 hostdata->restart_select = 0;
1285
1286 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001287 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /*
1290 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1291 * data bus during SELECTION.
1292 */
1293
1294 NCR5380_write(TARGET_COMMAND_REG, 0);
1295
1296 /*
1297 * Start arbitration.
1298 */
1299
1300 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1301 NCR5380_write(MODE_REG, MR_ARBITRATE);
1302
1303
1304 /* We can be relaxed here, interrupts are on, we are
1305 in workqueue context, the birds are singing in the trees */
1306 spin_unlock_irq(instance->host_lock);
1307 err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
1308 spin_lock_irq(instance->host_lock);
1309 if (err < 0) {
1310 printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
1311 NCR5380_write(MODE_REG, MR_BASE);
1312 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1313 goto failed;
1314 }
1315
Finn Thain52a6a1c2014-03-18 11:42:18 +11001316 dprintk(NDEBUG_ARBITRATION, "scsi%d : arbitration complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /*
1319 * The arbitration delay is 2.2us, but this is a minimum and there is
1320 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1321 * the integral nature of udelay().
1322 *
1323 */
1324
1325 udelay(3);
1326
1327 /* Check for lost arbitration */
1328 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)) {
1329 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001330 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto failed;
1332 }
1333 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1334
1335 if (!(hostdata->flags & FLAG_DTC3181E) &&
1336 /* RvC: DTC3181E has some trouble with this
1337 * so we simply removed it. Seems to work with
1338 * only Mustek scanner attached
1339 */
1340 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1341 NCR5380_write(MODE_REG, MR_BASE);
1342 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001343 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto failed;
1345 }
1346 /*
1347 * Again, bus clear + bus settle time is 1.2us, however, this is
1348 * a minimum so we'll udelay ceil(1.2)
1349 */
1350
1351 udelay(2);
1352
Finn Thain52a6a1c2014-03-18 11:42:18 +11001353 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /*
1356 * Now that we have won arbitration, start Selection process, asserting
1357 * the host and target ID's on the SCSI bus.
1358 */
1359
Jeff Garzik422c0d62005-10-24 18:05:09 -04001360 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /*
1363 * Raise ATN while SEL is true before BSY goes false from arbitration,
1364 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1365 * phase immediately after selection.
1366 */
1367
1368 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1369 NCR5380_write(MODE_REG, MR_BASE);
1370
1371 /*
1372 * Reselect interrupts must be turned off prior to the dropping of BSY,
1373 * otherwise we will trigger an interrupt.
1374 */
1375 NCR5380_write(SELECT_ENABLE_REG, 0);
1376
1377 /*
1378 * The initiator shall then wait at least two deskew delays and release
1379 * the BSY signal.
1380 */
1381 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1382
1383 /* Reset BSY */
1384 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1385
1386 /*
1387 * Something weird happens when we cease to drive BSY - looks
1388 * like the board/chip is letting us do another read before the
1389 * appropriate propagation delay has expired, and we're confusing
1390 * a BSY signal from ourselves as the target's response to SELECTION.
1391 *
1392 * A small delay (the 'C++' frontend breaks the pipeline with an
1393 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1394 * tighter 'C' code breaks and requires this) solves the problem -
1395 * the 1 us delay is arbitrary, and only used because this delay will
1396 * be the same on other platforms and since it works here, it should
1397 * work there.
1398 *
1399 * wingel suggests that this could be due to failing to wait
1400 * one deskew delay.
1401 */
1402
1403 udelay(1);
1404
Finn Thain52a6a1c2014-03-18 11:42:18 +11001405 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /*
1408 * The SCSI specification calls for a 250 ms timeout for the actual
1409 * selection.
1410 */
1411
1412 timeout = jiffies + (250 * HZ / 1000);
1413
1414 /*
1415 * XXX very interesting - we're seeing a bounce where the BSY we
1416 * asserted is being reflected / still asserted (propagation delay?)
1417 * and it's detecting as true. Sigh.
1418 */
1419
1420 hostdata->select_time = 0; /* we count the clock ticks at which we polled */
1421 hostdata->selecting = cmd;
1422
1423part2:
1424 /* RvC: here we enter after a sleeping period, or immediately after
1425 execution of part 1
1426 we poll only once ech clock tick */
1427 value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
1428
1429 if (!value && (hostdata->select_time < HZ/4)) {
1430 /* RvC: we still must wait for a device response */
1431 hostdata->select_time++; /* after 25 ticks the device has failed */
1432 NCR5380_set_timer(hostdata, 1);
1433 return 0; /* RvC: we return here with hostdata->selecting set,
1434 to go to sleep */
1435 }
1436
1437 hostdata->selecting = NULL;/* clear this pointer, because we passed the
1438 waiting period */
1439 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1440 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1441 NCR5380_reselect(instance);
1442 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1443 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1444 return -1;
1445 }
1446 /*
1447 * No less than two deskew delays after the initiator detects the
1448 * BSY signal is true, it shall release the SEL signal and may
1449 * change the DATA BUS. -wingel
1450 */
1451
1452 udelay(1);
1453
1454 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1455
1456 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1457 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Jeff Garzik422c0d62005-10-24 18:05:09 -04001458 if (hostdata->targets_present & (1 << scmd_id(cmd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no);
1460 if (hostdata->restart_select)
1461 printk(KERN_DEBUG "\trestart select\n");
1462 NCR5380_dprint(NDEBUG_SELECTION, instance);
1463 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1464 return -1;
1465 }
1466 cmd->result = DID_BAD_TARGET << 16;
1467 collect_stats(hostdata, cmd);
1468 cmd->scsi_done(cmd);
1469 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001470 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1472 return 0;
1473 }
Jeff Garzik422c0d62005-10-24 18:05:09 -04001474 hostdata->targets_present |= (1 << scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /*
1477 * Since we followed the SCSI spec, and raised ATN while SEL
1478 * was true but before BSY was false during selection, the information
1479 * transfer phase should be a MESSAGE OUT phase so that we can send the
1480 * IDENTIFY message.
1481 *
1482 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1483 * message (2 bytes) with a tag ID that we increment with every command
1484 * until it wraps back to 0.
1485 *
1486 * XXX - it turns out that there are some broken SCSI-II devices,
1487 * which claim to support tagged queuing but fail when more than
1488 * some number of commands are issued at once.
1489 */
1490
1491 /* Wait for start of REQ/ACK handshake */
1492
1493 spin_unlock_irq(instance->host_lock);
1494 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1495 spin_lock_irq(instance->host_lock);
1496
1497 if(err) {
1498 printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1499 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1500 goto failed;
1501 }
1502
Finn Thain52a6a1c2014-03-18 11:42:18 +11001503 dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun);
1505
1506 len = 1;
1507 cmd->tag = 0;
1508
1509 /* Send message(s) */
1510 data = tmp;
1511 phase = PHASE_MSGOUT;
1512 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001513 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* XXX need to handle errors here */
1515 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001516 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Boaz Harrosh28424d32007-09-10 22:37:45 +03001518 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 return 0;
1521
1522 /* Selection failed */
1523failed:
1524 return -1;
1525
1526}
1527
1528/*
1529 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1530 * unsigned char *phase, int *count, unsigned char **data)
1531 *
1532 * Purpose : transfers data in given phase using polled I/O
1533 *
1534 * Inputs : instance - instance of driver, *phase - pointer to
1535 * what phase is expected, *count - pointer to number of
1536 * bytes to transfer, **data - pointer to data pointer.
1537 *
1538 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001539 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * is in same phase.
1541 *
1542 * Also, *phase, *count, *data are modified in place.
1543 *
1544 * XXX Note : handling for bus free may be useful.
1545 */
1546
1547/*
1548 * Note : this code is not as quick as it could be, however it
1549 * IS 100% reliable, and for the actual data transfer where speed
1550 * counts, we will always do a pseudo DMA or DMA transfer.
1551 */
1552
1553static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1554 NCR5380_local_declare();
1555 unsigned char p = *phase, tmp;
1556 int c = *count;
1557 unsigned char *d = *data;
1558 /*
1559 * RvC: some administrative data to process polling time
1560 */
1561 int break_allowed = 0;
1562 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1563 NCR5380_setup(instance);
1564
1565 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001566 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001568 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /*
1571 * The NCR5380 chip will only drive the SCSI bus when the
1572 * phase specified in the appropriate bits of the TARGET COMMAND
1573 * REGISTER match the STATUS REGISTER
1574 */
1575
1576 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1577
1578 /* RvC: don't know if this is necessary, but other SCSI I/O is short
1579 * so breaks are not necessary there
1580 */
1581 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
1582 break_allowed = 1;
1583 }
1584 do {
1585 /*
1586 * Wait for assertion of REQ, after which the phase bits will be
1587 * valid
1588 */
1589
1590 /* RvC: we simply poll once, after that we stop temporarily
1591 * and let the device buffer fill up
1592 * if breaking is not allowed, we keep polling as long as needed
1593 */
1594
1595 /* FIXME */
1596 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
1597 if (!(tmp & SR_REQ)) {
1598 /* timeout condition */
1599 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
1600 break;
1601 }
1602
Finn Thain52a6a1c2014-03-18 11:42:18 +11001603 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 /* Check for phase mismatch */
1606 if ((tmp & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001607 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1609 break;
1610 }
1611 /* Do actual transfer from SCSI bus to / from memory */
1612 if (!(p & SR_IO))
1613 NCR5380_write(OUTPUT_DATA_REG, *d);
1614 else
1615 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1616
1617 ++d;
1618
1619 /*
1620 * The SCSI standard suggests that in MSGOUT phase, the initiator
1621 * should drop ATN on the last byte of the message phase
1622 * after REQ has been asserted for the handshake but before
1623 * the initiator raises ACK.
1624 */
1625
1626 if (!(p & SR_IO)) {
1627 if (!((p & SR_MSG) && c > 1)) {
1628 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1629 NCR5380_dprint(NDEBUG_PIO, instance);
1630 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1631 } else {
1632 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1633 NCR5380_dprint(NDEBUG_PIO, instance);
1634 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1635 }
1636 } else {
1637 NCR5380_dprint(NDEBUG_PIO, instance);
1638 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1639 }
1640
1641 /* FIXME - if this fails bus reset ?? */
1642 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001643 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645/*
1646 * We have several special cases to consider during REQ/ACK handshaking :
1647 * 1. We were in MSGOUT phase, and we are on the last byte of the
1648 * message. ATN must be dropped as ACK is dropped.
1649 *
1650 * 2. We are in a MSGIN phase, and we are on the last byte of the
1651 * message. We must exit with ACK asserted, so that the calling
1652 * code may raise ATN before dropping ACK to reject the message.
1653 *
1654 * 3. ACK and ATN are clear and the target may proceed as normal.
1655 */
1656 if (!(p == PHASE_MSGIN && c == 1)) {
1657 if (p == PHASE_MSGOUT && c > 1)
1658 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1659 else
1660 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1661 }
1662 } while (--c);
1663
Finn Thain52a6a1c2014-03-18 11:42:18 +11001664 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 *count = c;
1667 *data = d;
1668 tmp = NCR5380_read(STATUS_REG);
1669 if (tmp & SR_REQ)
1670 *phase = tmp & PHASE_MASK;
1671 else
1672 *phase = PHASE_UNKNOWN;
1673
1674 if (!c || (*phase == p))
1675 return 0;
1676 else
1677 return -1;
1678}
1679
1680/**
1681 * do_reset - issue a reset command
1682 * @host: adapter to reset
1683 *
1684 * Issue a reset sequence to the NCR5380 and try and get the bus
1685 * back into sane shape.
1686 *
1687 * Locks: caller holds queue lock
1688 */
1689
1690static void do_reset(struct Scsi_Host *host) {
1691 NCR5380_local_declare();
1692 NCR5380_setup(host);
1693
1694 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1695 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1696 udelay(25);
1697 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1698}
1699
1700/*
1701 * Function : do_abort (Scsi_Host *host)
1702 *
1703 * Purpose : abort the currently established nexus. Should only be
1704 * called from a routine which can drop into a
1705 *
1706 * Returns : 0 on success, -1 on failure.
1707 *
1708 * Locks: queue lock held by caller
1709 * FIXME: sort this out and get new_eh running
1710 */
1711
1712static int do_abort(struct Scsi_Host *host) {
1713 NCR5380_local_declare();
1714 unsigned char *msgptr, phase, tmp;
1715 int len;
1716 int rc;
1717 NCR5380_setup(host);
1718
1719
1720 /* Request message out phase */
1721 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1722
1723 /*
1724 * Wait for the target to indicate a valid phase by asserting
1725 * REQ. Once this happens, we'll have either a MSGOUT phase
1726 * and can immediately send the ABORT message, or we'll have some
1727 * other phase and will have to source/sink data.
1728 *
1729 * We really don't care what value was on the bus or what value
1730 * the target sees, so we just handshake.
1731 */
1732
1733 rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
1734
1735 if(rc < 0)
1736 return -1;
1737
1738 tmp = (unsigned char)rc;
1739
1740 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1741
1742 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1743 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1744 rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ);
1745 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1746 if(rc == -1)
1747 return -1;
1748 }
1749 tmp = ABORT;
1750 msgptr = &tmp;
1751 len = 1;
1752 phase = PHASE_MSGOUT;
1753 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1754
1755 /*
1756 * If we got here, and the command completed successfully,
1757 * we're about to go into bus free state.
1758 */
1759
1760 return len ? -1 : 0;
1761}
1762
1763#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1764/*
1765 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1766 * unsigned char *phase, int *count, unsigned char **data)
1767 *
1768 * Purpose : transfers data in given phase using either real
1769 * or pseudo DMA.
1770 *
1771 * Inputs : instance - instance of driver, *phase - pointer to
1772 * what phase is expected, *count - pointer to number of
1773 * bytes to transfer, **data - pointer to data pointer.
1774 *
1775 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001776 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 * is in same phase.
1778 *
1779 * Also, *phase, *count, *data are modified in place.
1780 *
1781 * Locks: io_request lock held by caller
1782 */
1783
1784
1785static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1786 NCR5380_local_declare();
1787 register int c = *count;
1788 register unsigned char p = *phase;
1789 register unsigned char *d = *data;
1790 unsigned char tmp;
1791 int foo;
1792#if defined(REAL_DMA_POLL)
1793 int cnt, toPIO;
1794 unsigned char saved_data = 0, overrun = 0, residue;
1795#endif
1796
1797 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1798
1799 NCR5380_setup(instance);
1800
1801 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1802 *phase = tmp;
1803 return -1;
1804 }
1805#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1806#ifdef READ_OVERRUNS
1807 if (p & SR_IO) {
1808 c -= 2;
1809 }
1810#endif
Finn Thain52a6a1c2014-03-18 11:42:18 +11001811 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 -07001812 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1813#endif
1814
1815 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1816
1817#ifdef REAL_DMA
1818 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1819#elif defined(REAL_DMA_POLL)
1820 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1821#else
1822 /*
1823 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1824 * were not disabled for the duration of a single DMA transfer, from
1825 * before the setting of DMA mode to after transfer of the last byte.
1826 */
1827
1828#if defined(PSEUDO_DMA) && defined(UNSAFE)
1829 spin_unlock_irq(instance->host_lock);
1830#endif
1831 /* KLL May need eop and parity in 53c400 */
1832 if (hostdata->flags & FLAG_NCR53C400)
Roel Kluin085267a2010-08-10 18:01:11 -07001833 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1834 MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1835 MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 else
1837 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1838#endif /* def REAL_DMA */
1839
Finn Thain52a6a1c2014-03-18 11:42:18 +11001840 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /*
1843 * On the PAS16 at least I/O recovery delays are not needed here.
1844 * Everyone else seems to want them.
1845 */
1846
1847 if (p & SR_IO) {
1848 io_recovery_delay(1);
1849 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1850 } else {
1851 io_recovery_delay(1);
1852 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1853 io_recovery_delay(1);
1854 NCR5380_write(START_DMA_SEND_REG, 0);
1855 io_recovery_delay(1);
1856 }
1857
1858#if defined(REAL_DMA_POLL)
1859 do {
1860 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1861 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1862
1863/*
1864 At this point, either we've completed DMA, or we have a phase mismatch,
1865 or we've unexpectedly lost BUSY (which is a real error).
1866
1867 For write DMAs, we want to wait until the last byte has been
1868 transferred out over the bus before we turn off DMA mode. Alas, there
1869 seems to be no terribly good way of doing this on a 5380 under all
1870 conditions. For non-scatter-gather operations, we can wait until REQ
1871 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1872 are nastier, since the device will be expecting more data than we
1873 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1874 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1875 why this signal was added to the newer chips) but on the older 538[01]
1876 this signal does not exist. The workaround for this lack is a watchdog;
1877 we bail out of the wait-loop after a modest amount of wait-time if
1878 the usual exit conditions are not met. Not a terribly clean or
1879 correct solution :-%
1880
1881 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1882 If the chip is in DMA mode for an READ, it will respond to a target's
1883 REQ by latching the SCSI data into the INPUT DATA register and asserting
1884 ACK, even if it has _already_ been notified by the DMA controller that
1885 the current DMA transfer has completed! If the NCR5380 is then taken
1886 out of DMA mode, this already-acknowledged byte is lost.
1887
1888 This is not a problem for "one DMA transfer per command" reads, because
1889 the situation will never arise... either all of the data is DMA'ed
1890 properly, or the target switches to MESSAGE IN phase to signal a
1891 disconnection (either operation bringing the DMA to a clean halt).
1892 However, in order to handle scatter-reads, we must work around the
1893 problem. The chosen fix is to DMA N-2 bytes, then check for the
1894 condition before taking the NCR5380 out of DMA mode. One or two extra
1895 bytes are transferred via PIO as necessary to fill out the original
1896 request.
1897 */
1898
1899 if (p & SR_IO) {
1900#ifdef READ_OVERRUNS
1901 udelay(10);
1902 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1903 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1904 overrun = 1;
1905 }
1906#endif
1907 } else {
1908 int limit = 100;
1909 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1910 if (!(tmp & BASR_PHASE_MATCH))
1911 break;
1912 if (--limit < 0)
1913 break;
1914 }
1915 }
1916
Finn Thain52a6a1c2014-03-18 11:42:18 +11001917 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 -07001918
1919 NCR5380_write(MODE_REG, MR_BASE);
1920 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1921
1922 residue = NCR5380_dma_residual(instance);
1923 c -= residue;
1924 *count -= c;
1925 *data += c;
1926 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1927
1928#ifdef READ_OVERRUNS
1929 if (*phase == p && (p & SR_IO) && residue == 0) {
1930 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001931 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 **data = saved_data;
1933 *data += 1;
1934 *count -= 1;
1935 cnt = toPIO = 1;
1936 } else {
1937 printk("No overrun??\n");
1938 cnt = toPIO = 2;
1939 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001940 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 NCR5380_transfer_pio(instance, phase, &cnt, data);
1942 *count -= toPIO - cnt;
1943 }
1944#endif
1945
Finn Thain52a6a1c2014-03-18 11:42:18 +11001946 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 -07001947 return 0;
1948
1949#elif defined(REAL_DMA)
1950 return 0;
1951#else /* defined(REAL_DMA_POLL) */
1952 if (p & SR_IO) {
1953#ifdef DMA_WORKS_RIGHT
1954 foo = NCR5380_pread(instance, d, c);
1955#else
1956 int diff = 1;
1957 if (hostdata->flags & FLAG_NCR53C400) {
1958 diff = 0;
1959 }
1960 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1961 /*
1962 * We can't disable DMA mode after successfully transferring
1963 * what we plan to be the last byte, since that would open up
1964 * a race condition where if the target asserted REQ before
1965 * we got the DMA mode reset, the NCR5380 would have latched
1966 * an additional byte into the INPUT DATA register and we'd
1967 * have dropped it.
1968 *
1969 * The workaround was to transfer one fewer bytes than we
1970 * intended to with the pseudo-DMA read function, wait for
1971 * the chip to latch the last byte, read it, and then disable
1972 * pseudo-DMA mode.
1973 *
1974 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1975 * REQ is deasserted when ACK is asserted, and not reasserted
1976 * until ACK goes false. Since the NCR5380 won't lower ACK
1977 * until DACK is asserted, which won't happen unless we twiddle
1978 * the DMA port or we take the NCR5380 out of DMA mode, we
1979 * can guarantee that we won't handshake another extra
1980 * byte.
1981 */
1982
1983 if (!(hostdata->flags & FLAG_NCR53C400)) {
1984 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1985 /* Wait for clean handshake */
1986 while (NCR5380_read(STATUS_REG) & SR_REQ);
1987 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1988 }
1989 }
1990#endif
1991 } else {
1992#ifdef DMA_WORKS_RIGHT
1993 foo = NCR5380_pwrite(instance, d, c);
1994#else
1995 int timeout;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001996 dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1998 /*
1999 * Wait for the last byte to be sent. If REQ is being asserted for
2000 * the byte we're interested, we'll ACK it and it will go false.
2001 */
2002 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2003 timeout = 20000;
2004 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
2005
2006 if (!timeout)
Finn Thain52a6a1c2014-03-18 11:42:18 +11002007 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2010 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2011 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2012 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002013 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 }
2016 } else {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002017 dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
Finn Thain52a6a1c2014-03-18 11:42:18 +11002019 dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 }
2022#endif
2023 }
2024 NCR5380_write(MODE_REG, MR_BASE);
2025 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2026
2027 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002028 dprintk(NDEBUG_C400_PWRITE, "53C400w: Checking for IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002030 dprintk(NDEBUG_C400_PWRITE, "53C400w: got it, reading reset interrupt reg\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2032 } else {
2033 printk("53C400w: IRQ NOT THERE!\n");
2034 }
2035 }
2036 *data = d + c;
2037 *count = 0;
2038 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2039#if defined(PSEUDO_DMA) && defined(UNSAFE)
2040 spin_lock_irq(instance->host_lock);
2041#endif /* defined(REAL_DMA_POLL) */
2042 return foo;
2043#endif /* def REAL_DMA */
2044}
2045#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2046
2047/*
2048 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2049 *
2050 * Purpose : run through the various SCSI phases and do as the target
2051 * directs us to. Operates on the currently connected command,
2052 * instance->connected.
2053 *
2054 * Inputs : instance, instance for which we are doing commands
2055 *
2056 * Side effects : SCSI things happen, the disconnected queue will be
2057 * modified if a command disconnects, *instance->connected will
2058 * change.
2059 *
2060 * XXX Note : we need to watch for bus free or a reset condition here
2061 * to recover from an unexpected bus free condition.
2062 *
2063 * Locks: io_request_lock held by caller in IRQ mode
2064 */
2065
2066static void NCR5380_information_transfer(struct Scsi_Host *instance) {
2067 NCR5380_local_declare();
2068 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
2069 unsigned char msgout = NOP;
2070 int sink = 0;
2071 int len;
2072#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2073 int transfersize;
2074#endif
2075 unsigned char *data;
2076 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
2077 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2078 /* RvC: we need to set the end of the polling time */
2079 unsigned long poll_time = jiffies + USLEEP_POLL;
2080
2081 NCR5380_setup(instance);
2082
2083 while (1) {
2084 tmp = NCR5380_read(STATUS_REG);
2085 /* We only have a valid SCSI phase when REQ is asserted */
2086 if (tmp & SR_REQ) {
2087 phase = (tmp & PHASE_MASK);
2088 if (phase != old_phase) {
2089 old_phase = phase;
2090 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
2091 }
2092 if (sink && (phase != PHASE_MSGOUT)) {
2093 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2094
2095 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
2096 while (NCR5380_read(STATUS_REG) & SR_REQ);
2097 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2098 sink = 0;
2099 continue;
2100 }
2101 switch (phase) {
2102 case PHASE_DATAIN:
2103 case PHASE_DATAOUT:
2104#if (NDEBUG & NDEBUG_NO_DATAOUT)
2105 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
2106 sink = 1;
2107 do_abort(instance);
2108 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002109 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return;
2111#endif
2112 /*
2113 * If there is no room left in the current buffer in the
2114 * scatter-gather list, move onto the next one.
2115 */
2116
2117 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2118 ++cmd->SCp.buffer;
2119 --cmd->SCp.buffers_residual;
2120 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02002121 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002122 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 -07002123 }
2124 /*
2125 * The preferred transfer method is going to be
2126 * PSEUDO-DMA for systems that are strictly PIO,
2127 * since we can let the hardware do the handshaking.
2128 *
2129 * For this to work, we need to know the transfersize
2130 * ahead of time, since the pseudo-DMA code will sit
2131 * in an unconditional loop.
2132 */
2133
2134#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2135 /* KLL
2136 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2137 * driver then it will always be defined, so the
2138 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2139 * NCR5380 case. I think this is a fairly clean solution.
2140 * We supplement these 2 if's with the flag.
2141 */
2142#ifdef NCR5380_dma_xfer_len
2143 if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2144#else
2145 transfersize = cmd->transfersize;
2146
2147#ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2148 if (transfersize > 512)
2149 transfersize = 512;
2150#endif /* LIMIT_TRANSFERSIZE */
2151
2152 if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
2153 /* Limit transfers to 32K, for xx400 & xx406
2154 * pseudoDMA that transfers in 128 bytes blocks. */
2155 if (transfersize > 32 * 1024)
2156 transfersize = 32 * 1024;
2157#endif
2158 len = transfersize;
2159 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
2160 /*
2161 * If the watchdog timer fires, all future accesses to this
2162 * device will use the polled-IO.
2163 */
Jeff Garzik017560f2005-10-24 18:04:36 -04002164 scmd_printk(KERN_INFO, cmd,
2165 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 cmd->device->borken = 1;
2167 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2168 sink = 1;
2169 do_abort(instance);
2170 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002171 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* XXX - need to source or sink data here, as appropriate */
2173 } else
2174 cmd->SCp.this_residual -= transfersize - len;
2175 } else
2176#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2177 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
2178 &cmd->SCp.ptr);
2179 break;
2180 case PHASE_MSGIN:
2181 len = 1;
2182 data = &tmp;
2183 NCR5380_transfer_pio(instance, &phase, &len, &data);
2184 cmd->SCp.Message = tmp;
2185
2186 switch (tmp) {
2187 /*
2188 * Linking lets us reduce the time required to get the
2189 * next command out to the device, hopefully this will
2190 * mean we don't waste another revolution due to the delays
2191 * required by ARBITRATION and another SELECTION.
2192 *
2193 * In the current implementation proposal, low level drivers
2194 * merely have to start the next command, pointed to by
2195 * next_link, done() is called as with unlinked commands.
2196 */
2197#ifdef LINKED
2198 case LINKED_CMD_COMPLETE:
2199 case LINKED_FLG_CMD_COMPLETE:
2200 /* Accept message by clearing ACK */
2201 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002202 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 -07002203 /*
2204 * Sanity check : A linked command should only terminate with
2205 * one of these messages if there are more linked commands
2206 * available.
2207 */
2208 if (!cmd->next_link) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002209 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 -07002210 sink = 1;
2211 do_abort(instance);
2212 return;
2213 }
2214 initialize_SCp(cmd->next_link);
2215 /* The next command is still part of this process */
2216 cmd->next_link->tag = cmd->tag;
2217 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002218 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 -07002219 collect_stats(hostdata, cmd);
2220 cmd->scsi_done(cmd);
2221 cmd = hostdata->connected;
2222 break;
2223#endif /* def LINKED */
2224 case ABORT:
2225 case COMMAND_COMPLETE:
2226 /* Accept message by clearing ACK */
2227 sink = 1;
2228 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2229 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002230 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
2231 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 /*
2234 * I'm not sure what the correct thing to do here is :
2235 *
2236 * If the command that just executed is NOT a request
2237 * sense, the obvious thing to do is to set the result
2238 * code to the values of the stored parameters.
2239 *
2240 * If it was a REQUEST SENSE command, we need some way
2241 * to differentiate between the failure code of the original
2242 * and the failure code of the REQUEST sense - the obvious
2243 * case is success, where we fall through and leave the result
2244 * code unchanged.
2245 *
2246 * The non-obvious place is where the REQUEST SENSE failed
2247 */
2248
2249 if (cmd->cmnd[0] != REQUEST_SENSE)
2250 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2251 else if (status_byte(cmd->SCp.Status) != GOOD)
2252 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2253
Boaz Harrosh28424d32007-09-10 22:37:45 +03002254 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2255 hostdata->ses.cmd_len) {
2256 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2257 hostdata->ses.cmd_len = 0 ;
2258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Boaz Harrosh28424d32007-09-10 22:37:45 +03002260 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
2261 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
2262
Finn Thain52a6a1c2014-03-18 11:42:18 +11002263 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 LIST(cmd, hostdata->issue_queue);
2266 cmd->host_scribble = (unsigned char *)
2267 hostdata->issue_queue;
2268 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002269 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11002270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 collect_stats(hostdata, cmd);
2272 cmd->scsi_done(cmd);
2273 }
2274
2275 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2276 /*
2277 * Restore phase bits to 0 so an interrupted selection,
2278 * arbitration can resume.
2279 */
2280 NCR5380_write(TARGET_COMMAND_REG, 0);
2281
2282 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2283 barrier();
2284 return;
2285 case MESSAGE_REJECT:
2286 /* Accept message by clearing ACK */
2287 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2288 switch (hostdata->last_message) {
2289 case HEAD_OF_QUEUE_TAG:
2290 case ORDERED_QUEUE_TAG:
2291 case SIMPLE_QUEUE_TAG:
2292 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002293 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 break;
2295 default:
2296 break;
2297 }
2298 case DISCONNECT:{
2299 /* Accept message by clearing ACK */
2300 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2301 cmd->device->disconnect = 1;
2302 LIST(cmd, hostdata->disconnected_queue);
2303 cmd->host_scribble = (unsigned char *)
2304 hostdata->disconnected_queue;
2305 hostdata->connected = NULL;
2306 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002307 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 -07002308 /*
2309 * Restore phase bits to 0 so an interrupted selection,
2310 * arbitration can resume.
2311 */
2312 NCR5380_write(TARGET_COMMAND_REG, 0);
2313
2314 /* Enable reselect interrupts */
2315 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2316 /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
2317 /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
2318 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2319 barrier();
2320 return;
2321 }
2322 /*
2323 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2324 * operation, in violation of the SCSI spec so we can safely
2325 * ignore SAVE/RESTORE pointers calls.
2326 *
2327 * Unfortunately, some disks violate the SCSI spec and
2328 * don't issue the required SAVE_POINTERS message before
2329 * disconnecting, and we have to break spec to remain
2330 * compatible.
2331 */
2332 case SAVE_POINTERS:
2333 case RESTORE_POINTERS:
2334 /* Accept message by clearing ACK */
2335 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2336 break;
2337 case EXTENDED_MESSAGE:
2338/*
2339 * Extended messages are sent in the following format :
2340 * Byte
2341 * 0 EXTENDED_MESSAGE == 1
2342 * 1 length (includes one byte for code, doesn't
2343 * include first two bytes)
2344 * 2 code
2345 * 3..length+1 arguments
2346 *
2347 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002348 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 */
2350 extended_msg[0] = EXTENDED_MESSAGE;
2351 /* Accept first byte by clearing ACK */
2352 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002353 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 len = 2;
2356 data = extended_msg + 1;
2357 phase = PHASE_MSGIN;
2358 NCR5380_transfer_pio(instance, &phase, &len, &data);
2359
Finn Thain52a6a1c2014-03-18 11:42:18 +11002360 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 -07002361
2362 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2363 /* Accept third byte by clearing ACK */
2364 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2365 len = extended_msg[1] - 1;
2366 data = extended_msg + 3;
2367 phase = PHASE_MSGIN;
2368
2369 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002370 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 switch (extended_msg[2]) {
2373 case EXTENDED_SDTR:
2374 case EXTENDED_WDTR:
2375 case EXTENDED_MODIFY_DATA_POINTER:
2376 case EXTENDED_EXTENDED_IDENTIFY:
2377 tmp = 0;
2378 }
2379 } else if (len) {
2380 printk("scsi%d: error receiving extended message\n", instance->host_no);
2381 tmp = 0;
2382 } else {
2383 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2384 tmp = 0;
2385 }
2386 /* Fall through to reject message */
2387
2388 /*
2389 * If we get something weird that we aren't expecting,
2390 * reject it.
2391 */
2392 default:
2393 if (!tmp) {
2394 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002395 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 printk("\n");
2397 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002398 scmd_printk(KERN_INFO, cmd,
2399 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002401 scmd_printk(KERN_INFO, cmd,
2402 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 msgout = MESSAGE_REJECT;
2405 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2406 break;
2407 } /* switch (tmp) */
2408 break;
2409 case PHASE_MSGOUT:
2410 len = 1;
2411 data = &msgout;
2412 hostdata->last_message = msgout;
2413 NCR5380_transfer_pio(instance, &phase, &len, &data);
2414 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002415 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 hostdata->connected = NULL;
2417 cmd->result = DID_ERROR << 16;
2418 collect_stats(hostdata, cmd);
2419 cmd->scsi_done(cmd);
2420 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2421 return;
2422 }
2423 msgout = NOP;
2424 break;
2425 case PHASE_CMDOUT:
2426 len = cmd->cmd_len;
2427 data = cmd->cmnd;
2428 /*
2429 * XXX for performance reasons, on machines with a
2430 * PSEUDO-DMA architecture we should probably
2431 * use the dma transfer function.
2432 */
2433 NCR5380_transfer_pio(instance, &phase, &len, &data);
2434 if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
2435 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002436 dprintk(NDEBUG_USLEEP, "scsi%d : issued command, sleeping until %lu\n", instance->host_no, hostdata->time_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return;
2438 }
2439 break;
2440 case PHASE_STATIN:
2441 len = 1;
2442 data = &tmp;
2443 NCR5380_transfer_pio(instance, &phase, &len, &data);
2444 cmd->SCp.Status = tmp;
2445 break;
2446 default:
2447 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002448 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 } /* switch(phase) */
2450 } /* if (tmp * SR_REQ) */
2451 else {
2452 /* RvC: go to sleep if polling time expired
2453 */
2454 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
2455 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002456 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 -07002457 return;
2458 }
2459 }
2460 } /* while (1) */
2461}
2462
2463/*
2464 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2465 *
2466 * Purpose : does reselection, initializing the instance->connected
2467 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2468 * nexus has been reestablished,
2469 *
2470 * Inputs : instance - this instance of the NCR5380.
2471 *
2472 * Locks: io_request_lock held by caller if IRQ driven
2473 */
2474
2475static void NCR5380_reselect(struct Scsi_Host *instance) {
2476 NCR5380_local_declare();
2477 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2478 instance->hostdata;
2479 unsigned char target_mask;
2480 unsigned char lun, phase;
2481 int len;
2482 unsigned char msg[3];
2483 unsigned char *data;
2484 Scsi_Cmnd *tmp = NULL, *prev;
2485 int abort = 0;
2486 NCR5380_setup(instance);
2487
2488 /*
2489 * Disable arbitration, etc. since the host adapter obviously
2490 * lost, and tell an interrupted NCR5380_select() to restart.
2491 */
2492
2493 NCR5380_write(MODE_REG, MR_BASE);
2494 hostdata->restart_select = 1;
2495
2496 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002497 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /*
2500 * At this point, we have detected that our SCSI ID is on the bus,
2501 * SEL is true and BSY was false for at least one bus settle delay
2502 * (400 ns).
2503 *
2504 * We must assert BSY ourselves, until the target drops the SEL
2505 * signal.
2506 */
2507
2508 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2509
2510 /* FIXME: timeout too long, must fail to workqueue */
2511 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2512 abort = 1;
2513
2514 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2515
2516 /*
2517 * Wait for target to go into MSGIN.
2518 * FIXME: timeout needed and fail to work queeu
2519 */
2520
2521 if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
2522 abort = 1;
2523
2524 len = 1;
2525 data = msg;
2526 phase = PHASE_MSGIN;
2527 NCR5380_transfer_pio(instance, &phase, &len, &data);
2528
2529 if (!(msg[0] & 0x80)) {
2530 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002531 spi_print_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 abort = 1;
2533 } else {
2534 /* Accept message by clearing ACK */
2535 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2536 lun = (msg[0] & 0x07);
2537
2538 /*
2539 * We need to add code for SCSI-II to track which devices have
2540 * I_T_L_Q nexuses established, and which have simple I_T_L
2541 * nexuses so we can chose to do additional data transfer.
2542 */
2543
2544 /*
2545 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2546 * just reestablished, and remove it from the disconnected queue.
2547 */
2548
2549
2550 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002551 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 ) {
2553 if (prev) {
2554 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2555 prev->host_scribble = tmp->host_scribble;
2556 } else {
2557 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2558 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2559 }
2560 tmp->host_scribble = NULL;
2561 break;
2562 }
2563 if (!tmp) {
2564 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2565 /*
2566 * Since we have an established nexus that we can't do anything with,
2567 * we must abort it.
2568 */
2569 abort = 1;
2570 }
2571 }
2572
2573 if (abort) {
2574 do_abort(instance);
2575 } else {
2576 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002577 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 -07002578 }
2579}
2580
2581/*
2582 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2583 *
2584 * Purpose : called by interrupt handler when DMA finishes or a phase
2585 * mismatch occurs (which would finish the DMA transfer).
2586 *
2587 * Inputs : instance - this instance of the NCR5380.
2588 *
2589 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2590 * nexus has been reestablished, on failure NULL is returned.
2591 */
2592
2593#ifdef REAL_DMA
2594static void NCR5380_dma_complete(NCR5380_instance * instance) {
2595 NCR5380_local_declare();
Yoann Padioleauf8343682007-06-01 00:46:36 -07002596 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 int transferred;
2598 NCR5380_setup(instance);
2599
2600 /*
2601 * XXX this might not be right.
2602 *
2603 * Wait for final byte to transfer, ie wait for ACK to go false.
2604 *
2605 * We should use the Last Byte Sent bit, unfortunately this is
2606 * not available on the 5380/5381 (only the various CMOS chips)
2607 *
2608 * FIXME: timeout, and need to handle long timeout/irq case
2609 */
2610
2611 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2612
2613 NCR5380_write(MODE_REG, MR_BASE);
2614 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2615
2616 /*
2617 * The only places we should see a phase mismatch and have to send
2618 * data from the same set of pointers will be the data transfer
2619 * phases. So, residual, requested length are only important here.
2620 */
2621
2622 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2623 transferred = instance->dmalen - NCR5380_dma_residual();
2624 hostdata->connected->SCp.this_residual -= transferred;
2625 hostdata->connected->SCp.ptr += transferred;
2626 }
2627}
2628#endif /* def REAL_DMA */
2629
2630/*
2631 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2632 *
2633 * Purpose : abort a command
2634 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002635 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2636 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * used.
2638 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002639 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002641 * XXX - there is no way to abort the command that is currently
2642 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 * a problem, we could implement longjmp() / setjmp(), setjmp()
2644 * called where the loop started in NCR5380_main().
2645 *
2646 * Locks: host lock taken by caller
2647 */
2648
2649static int NCR5380_abort(Scsi_Cmnd * cmd) {
2650 NCR5380_local_declare();
2651 struct Scsi_Host *instance = cmd->device->host;
2652 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2653 Scsi_Cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002654
2655 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 NCR5380_print_status(instance);
2658
2659 NCR5380_setup(instance);
2660
Finn Thain52a6a1c2014-03-18 11:42:18 +11002661 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2662 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 -07002663
2664#if 0
2665/*
2666 * Case 1 : If the command is the currently executing command,
2667 * we'll set the aborted flag and return control so that
2668 * information transfer routine can exit cleanly.
2669 */
2670
2671 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002672 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 hostdata->aborted = 1;
2674/*
2675 * We should perform BSY checking, and make sure we haven't slipped
2676 * into BUS FREE.
2677 */
2678
2679 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2680/*
2681 * Since we can't change phases until we've completed the current
2682 * handshake, we have to source or sink a byte of data if the current
2683 * phase is not MSGOUT.
2684 */
2685
2686/*
2687 * Return control to the executing NCR drive so we can clear the
2688 * aborted flag and get back into our main loop.
2689 */
2690
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002691 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693#endif
2694
2695/*
2696 * Case 2 : If the command hasn't been issued yet, we simply remove it
2697 * from the issue queue.
2698 */
2699
Finn Thain52a6a1c2014-03-18 11:42:18 +11002700 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue), tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
2702 if (cmd == tmp) {
2703 REMOVE(5, *prev, tmp, tmp->host_scribble);
2704 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2705 tmp->host_scribble = NULL;
2706 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002707 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002708 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return SUCCESS;
2710 }
2711#if (NDEBUG & NDEBUG_ABORT)
2712 /* KLL */
2713 else if (prev == tmp)
2714 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2715#endif
2716
2717/*
2718 * Case 3 : If any commands are connected, we're going to fail the abort
2719 * and let the high level SCSI driver retry at a later time or
2720 * issue a reset.
2721 *
2722 * Timeouts, and therefore aborted commands, will be highly unlikely
2723 * and handling them cleanly in this situation would make the common
2724 * case of noresets less efficient, and would pollute our code. So,
2725 * we fail.
2726 */
2727
2728 if (hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002729 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return FAILED;
2731 }
2732/*
2733 * Case 4: If the command is currently disconnected from the bus, and
2734 * there are no connected commands, we reconnect the I_T_L or
2735 * I_T_L_Q nexus associated with it, go into message out, and send
2736 * an abort message.
2737 *
2738 * This case is especially ugly. In order to reestablish the nexus, we
2739 * need to call NCR5380_select(). The easiest way to implement this
2740 * function was to abort if the bus was busy, and let the interrupt
2741 * handler triggered on the SEL for reselect take care of lost arbitrations
2742 * where necessary, meaning interrupts need to be enabled.
2743 *
2744 * When interrupts are enabled, the queues may change - so we
2745 * can't remove it from the disconnected queue before selecting it
2746 * because that could cause a failure in hashing the nexus if that
2747 * device reselected.
2748 *
2749 * Since the queues may change, we can't use the pointers from when we
2750 * first locate it.
2751 *
2752 * So, we must first locate the command, and if NCR5380_select()
2753 * succeeds, then issue the abort, relocate the command and remove
2754 * it from the disconnected queue.
2755 */
2756
2757 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
2758 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002759 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Finn Thain76f13b92014-11-12 16:11:53 +11002761 if (NCR5380_select(instance, cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return FAILED;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002763 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 do_abort(instance);
2766
2767 for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue), tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
2768 if (cmd == tmp) {
2769 REMOVE(5, *prev, tmp, tmp->host_scribble);
2770 *prev = (Scsi_Cmnd *) tmp->host_scribble;
2771 tmp->host_scribble = NULL;
2772 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002773 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return SUCCESS;
2775 }
2776 }
2777/*
2778 * Case 5 : If we reached this point, the command was not found in any of
2779 * the queues.
2780 *
2781 * We probably reached this point because of an unlikely race condition
2782 * between the command completing successfully and the abortion code,
2783 * so we won't panic, but we will notify the user in case something really
2784 * broke.
2785 */
2786 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2787 " before abortion\n", instance->host_no);
2788 return FAILED;
2789}
2790
2791
2792/*
2793 * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
2794 *
2795 * Purpose : reset the SCSI bus.
2796 *
2797 * Returns : SUCCESS
2798 *
2799 * Locks: host lock taken by caller
2800 */
2801
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002802static int NCR5380_bus_reset(Scsi_Cmnd * cmd)
2803{
2804 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002806 NCR5380_local_declare();
2807 NCR5380_setup(instance);
2808 NCR5380_print_status(instance);
2809
2810 spin_lock_irq(instance->host_lock);
2811 do_reset(instance);
2812 spin_unlock_irq(instance->host_lock);
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return SUCCESS;
2815}