Linux-2.6.12-rc2
[deliverable/linux.git] / drivers / scsi / atari_NCR5380.c
CommitLineData
1da177e4
LT
1/*
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/*
30 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
31 * this file, too:
32 *
33 * - Some of the debug statements were incorrect (undefined variables and the
34 * like). I fixed that.
35 *
36 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
37 * possible DMA transfer size should also happen for REAL_DMA. I added this
38 * in the #if statement.
39 *
40 * - When using real DMA, information_transfer() should return in a DATAOUT
41 * phase after starting the DMA. It has nothing more to do.
42 *
43 * - The interrupt service routine should run main after end of DMA, too (not
44 * only after RESELECTION interrupts). Additionally, it should _not_ test
45 * for more interrupts after running main, since a DMA process may have
46 * been started and interrupts are turned on now. The new int could happen
47 * inside the execution of NCR5380_intr(), leading to recursive
48 * calls.
49 *
50 * - I've added a function merge_contiguous_buffers() that tries to
51 * merge scatter-gather buffers that are located at contiguous
52 * physical addresses and can be processed with the same DMA setup.
53 * Since most scatter-gather operations work on a page (4K) of
54 * 4 buffers (1K), in more than 90% of all cases three interrupts and
55 * DMA setup actions are saved.
56 *
57 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
58 * and USLEEP, because these were messing up readability and will never be
59 * needed for Atari SCSI.
60 *
61 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
62 * stuff), and 'main' is executed in a bottom half if awoken by an
63 * interrupt.
64 *
65 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
66 * constructs. In my eyes, this made the source rather unreadable, so I
67 * finally replaced that by the *_PRINTK() macros.
68 *
69 */
70
71/*
72 * Further development / testing that should be done :
73 * 1. Test linked command handling code after Eric is ready with
74 * the high level code.
75 */
76
77#if (NDEBUG & NDEBUG_LISTS)
78#define LIST(x,y) \
79 { printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); \
80 if ((x)==(y)) udelay(5); }
81#define REMOVE(w,x,y,z) \
82 { printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, \
83 (void*)(w), (void*)(x), (void*)(y), (void*)(z)); \
84 if ((x)==(y)) udelay(5); }
85#else
86#define LIST(x,y)
87#define REMOVE(w,x,y,z)
88#endif
89
90#ifndef notyet
91#undef LINKED
92#endif
93
94/*
95 * Design
96 * Issues :
97 *
98 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
99 * and specifically for each board rather than each chip. This makes their
100 * adaptation to platforms like the Mac (Some of which use NCR5380's)
101 * more difficult than it has to be.
102 *
103 * Also, many of the SCSI drivers were written before the command queuing
104 * routines were implemented, meaning their implementations of queued
105 * commands were hacked on rather than designed in from the start.
106 *
107 * When I designed the Linux SCSI drivers I figured that
108 * while having two different SCSI boards in a system might be useful
109 * for debugging things, two of the same type wouldn't be used.
110 * Well, I was wrong and a number of users have mailed me about running
111 * multiple high-performance SCSI boards in a server.
112 *
113 * Finally, when I get questions from users, I have no idea what
114 * revision of my driver they are running.
115 *
116 * This driver attempts to address these problems :
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 * As far as command queueing, two queues are maintained for
123 * each 5380 in the system - commands that haven't been issued yet,
124 * and commands that are currently executing. This means that an
125 * unlimited number of commands may be queued, letting
126 * more commands propagate from the higher driver levels giving higher
127 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
128 * allowing multiple commands to propagate all the way to a SCSI-II device
129 * while a command is already executing.
130 *
131 * To solve the multiple-boards-in-the-same-system problem,
132 * there is a separate instance structure for each instance
133 * of a 5380 in the system. So, multiple NCR5380 drivers will
134 * be able to coexist with appropriate changes to the high level
135 * SCSI code.
136 *
137 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
138 * number (updated for each public release) printed by the
139 * NCR5380_print_options command, which should be called from the
140 * wrapper detect function, so that I know what release of the driver
141 * users are using.
142 *
143 * Issues specific to the NCR5380 :
144 *
145 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
146 * piece of hardware that requires you to sit in a loop polling for
147 * the REQ signal as long as you are connected. Some devices are
148 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
149 * while doing long seek operations.
150 *
151 * The workaround for this is to keep track of devices that have
152 * disconnected. If the device hasn't disconnected, for commands that
153 * should disconnect, we do something like
154 *
155 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
156 *
157 * Some tweaking of N and M needs to be done. An algorithm based
158 * on "time to data" would give the best results as long as short time
159 * to datas (ie, on the same track) were considered, however these
160 * broken devices are the exception rather than the rule and I'd rather
161 * spend my time optimizing for the normal case.
162 *
163 * Architecture :
164 *
165 * At the heart of the design is a coroutine, NCR5380_main,
166 * which is started when not running by the interrupt handler,
167 * timer, and queue command function. It attempts to establish
168 * I_T_L or I_T_L_Q nexuses by removing the commands from the
169 * issue queue and calling NCR5380_select() if a nexus
170 * is not established.
171 *
172 * Once a nexus is established, the NCR5380_information_transfer()
173 * phase goes through the various phases as instructed by the target.
174 * if the target goes into MSG IN and sends a DISCONNECT message,
175 * the command structure is placed into the per instance disconnected
176 * queue, and NCR5380_main tries to find more work. If USLEEP
177 * was defined, and the target is idle for too long, the system
178 * will try to sleep.
179 *
180 * If a command has disconnected, eventually an interrupt will trigger,
181 * calling NCR5380_intr() which will in turn call NCR5380_reselect
182 * to reestablish a nexus. This will run main if necessary.
183 *
184 * On command termination, the done function will be called as
185 * appropriate.
186 *
187 * SCSI pointers are maintained in the SCp field of SCSI command
188 * structures, being initialized after the command is connected
189 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
190 * Note that in violation of the standard, an implicit SAVE POINTERS operation
191 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
192 */
193
194/*
195 * Using this file :
196 * This file a skeleton Linux SCSI driver for the NCR 5380 series
197 * of chips. To use it, you write an architecture specific functions
198 * and macros and include this file in your driver.
199 *
200 * These macros control options :
201 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
202 * for commands that return with a CHECK CONDITION status.
203 *
204 * LINKED - if defined, linked commands are supported.
205 *
206 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
207 *
208 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
209 *
210 * These macros MUST be defined :
211 *
212 * NCR5380_read(register) - read from the specified register
213 *
214 * NCR5380_write(register, value) - write to the specific register
215 *
216 * Either real DMA *or* pseudo DMA may be implemented
217 * REAL functions :
218 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
219 * Note that the DMA setup functions should return the number of bytes
220 * that they were able to program the controller for.
221 *
222 * Also note that generic i386/PC versions of these macros are
223 * available as NCR5380_i386_dma_write_setup,
224 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
225 *
226 * NCR5380_dma_write_setup(instance, src, count) - initialize
227 * NCR5380_dma_read_setup(instance, dst, count) - initialize
228 * NCR5380_dma_residual(instance); - residual count
229 *
230 * PSEUDO functions :
231 * NCR5380_pwrite(instance, src, count)
232 * NCR5380_pread(instance, dst, count);
233 *
234 * If nothing specific to this implementation needs doing (ie, with external
235 * hardware), you must also define
236 *
237 * NCR5380_queue_command
238 * NCR5380_reset
239 * NCR5380_abort
240 * NCR5380_proc_info
241 *
242 * to be the global entry points into the specific driver, ie
243 * #define NCR5380_queue_command t128_queue_command.
244 *
245 * If this is not done, the routines will be defined as static functions
246 * with the NCR5380* names and the user must provide a globally
247 * accessible wrapper function.
248 *
249 * The generic driver is initialized by calling NCR5380_init(instance),
250 * after setting the appropriate host specific fields and ID. If the
251 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
252 * possible) function may be used. Before the specific driver initialization
253 * code finishes, NCR5380_print_options should be called.
254 */
255
256static struct Scsi_Host *first_instance = NULL;
257static Scsi_Host_Template *the_template = NULL;
258
259/* Macros ease life... :-) */
260#define SETUP_HOSTDATA(in) \
261 struct NCR5380_hostdata *hostdata = \
262 (struct NCR5380_hostdata *)(in)->hostdata
263#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
264
265#define NEXT(cmd) ((Scsi_Cmnd *)((cmd)->host_scribble))
266#define NEXTADDR(cmd) ((Scsi_Cmnd **)&((cmd)->host_scribble))
267
268#define HOSTNO instance->host_no
269#define H_NO(cmd) (cmd)->device->host->host_no
270
271#ifdef SUPPORT_TAGS
272
273/*
274 * Functions for handling tagged queuing
275 * =====================================
276 *
277 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
278 *
279 * Using consecutive numbers for the tags is no good idea in my eyes. There
280 * could be wrong re-usings if the counter (8 bit!) wraps and some early
281 * command has been preempted for a long time. My solution: a bitfield for
282 * remembering used tags.
283 *
284 * There's also the problem that each target has a certain queue size, but we
285 * cannot know it in advance :-( We just see a QUEUE_FULL status being
286 * returned. So, in this case, the driver internal queue size assumption is
287 * reduced to the number of active tags if QUEUE_FULL is returned by the
288 * target. The command is returned to the mid-level, but with status changed
289 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
290 * correctly.
291 *
292 * We're also not allowed running tagged commands as long as an untagged
293 * command is active. And REQUEST SENSE commands after a contingent allegiance
294 * condition _must_ be untagged. To keep track whether an untagged command has
295 * been issued, the host->busy array is still employed, as it is without
296 * support for tagged queuing.
297 *
298 * One could suspect that there are possible race conditions between
299 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
300 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
301 * which already guaranteed to be running at most once. It is also the only
302 * place where tags/LUNs are allocated. So no other allocation can slip
303 * between that pair, there could only happen a reselection, which can free a
304 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
305 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
306 */
307
308/* -1 for TAG_NONE is not possible with unsigned char cmd->tag */
309#undef TAG_NONE
310#define TAG_NONE 0xff
311
312typedef struct {
313 DECLARE_BITMAP(allocated, MAX_TAGS);
314 int nr_allocated;
315 int queue_size;
316} TAG_ALLOC;
317
318static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
319
320
321static void __init init_tags( void )
322{
323 int target, lun;
324 TAG_ALLOC *ta;
325
326 if (!setup_use_tagged_queuing)
327 return;
328
329 for( target = 0; target < 8; ++target ) {
330 for( lun = 0; lun < 8; ++lun ) {
331 ta = &TagAlloc[target][lun];
332 bitmap_zero(ta->allocated, MAX_TAGS);
333 ta->nr_allocated = 0;
334 /* At the beginning, assume the maximum queue size we could
335 * support (MAX_TAGS). This value will be decreased if the target
336 * returns QUEUE_FULL status.
337 */
338 ta->queue_size = MAX_TAGS;
339 }
340 }
341}
342
343
344/* Check if we can issue a command to this LUN: First see if the LUN is marked
345 * busy by an untagged command. If the command should use tagged queuing, also
346 * check that there is a free tag and the target's queue won't overflow. This
347 * function should be called with interrupts disabled to avoid race
348 * conditions.
349 */
350
351static int is_lun_busy( Scsi_Cmnd *cmd, int should_be_tagged )
352{
353 SETUP_HOSTDATA(cmd->device->host);
354
355 if (hostdata->busy[cmd->device->id] & (1 << cmd->device->lun))
356 return( 1 );
357 if (!should_be_tagged ||
358 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
359 return( 0 );
360 if (TagAlloc[cmd->device->id][cmd->device->lun].nr_allocated >=
361 TagAlloc[cmd->device->id][cmd->device->lun].queue_size ) {
362 TAG_PRINTK( "scsi%d: target %d lun %d: no free tags\n",
363 H_NO(cmd), cmd->device->id, cmd->device->lun );
364 return( 1 );
365 }
366 return( 0 );
367}
368
369
370/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
371 * must be called before!), or reserve the LUN in 'busy' if the command is
372 * untagged.
373 */
374
375static void cmd_get_tag( Scsi_Cmnd *cmd, int should_be_tagged )
376{
377 SETUP_HOSTDATA(cmd->device->host);
378
379 /* If we or the target don't support tagged queuing, allocate the LUN for
380 * an untagged command.
381 */
382 if (!should_be_tagged ||
383 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
384 cmd->tag = TAG_NONE;
385 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
386 TAG_PRINTK( "scsi%d: target %d lun %d now allocated by untagged "
387 "command\n", H_NO(cmd), cmd->device->id, cmd->device->lun );
388 }
389 else {
390 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
391
392 cmd->tag = find_first_zero_bit( ta->allocated, MAX_TAGS );
393 set_bit( cmd->tag, ta->allocated );
394 ta->nr_allocated++;
395 TAG_PRINTK( "scsi%d: using tag %d for target %d lun %d "
396 "(now %d tags in use)\n",
397 H_NO(cmd), cmd->tag, cmd->device->id, cmd->device->lun,
398 ta->nr_allocated );
399 }
400}
401
402
403/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
404 * unlock the LUN.
405 */
406
407static void cmd_free_tag( Scsi_Cmnd *cmd )
408{
409 SETUP_HOSTDATA(cmd->device->host);
410
411 if (cmd->tag == TAG_NONE) {
412 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
413 TAG_PRINTK( "scsi%d: target %d lun %d untagged cmd finished\n",
414 H_NO(cmd), cmd->device->id, cmd->device->lun );
415 }
416 else if (cmd->tag >= MAX_TAGS) {
417 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
418 H_NO(cmd), cmd->tag );
419 }
420 else {
421 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
422 clear_bit( cmd->tag, ta->allocated );
423 ta->nr_allocated--;
424 TAG_PRINTK( "scsi%d: freed tag %d for target %d lun %d\n",
425 H_NO(cmd), cmd->tag, cmd->device->id, cmd->device->lun );
426 }
427}
428
429
430static void free_all_tags( void )
431{
432 int target, lun;
433 TAG_ALLOC *ta;
434
435 if (!setup_use_tagged_queuing)
436 return;
437
438 for( target = 0; target < 8; ++target ) {
439 for( lun = 0; lun < 8; ++lun ) {
440 ta = &TagAlloc[target][lun];
441 bitmap_zero(ta->allocated, MAX_TAGS);
442 ta->nr_allocated = 0;
443 }
444 }
445}
446
447#endif /* SUPPORT_TAGS */
448
449
450/*
451 * Function: void merge_contiguous_buffers( Scsi_Cmnd *cmd )
452 *
453 * Purpose: Try to merge several scatter-gather requests into one DMA
454 * transfer. This is possible if the scatter buffers lie on
455 * physical contiguous addresses.
456 *
457 * Parameters: Scsi_Cmnd *cmd
458 * The command to work on. The first scatter buffer's data are
459 * assumed to be already transfered into ptr/this_residual.
460 */
461
462static void merge_contiguous_buffers( Scsi_Cmnd *cmd )
463{
464 unsigned long endaddr;
465#if (NDEBUG & NDEBUG_MERGING)
466 unsigned long oldlen = cmd->SCp.this_residual;
467 int cnt = 1;
468#endif
469
470 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
471 cmd->SCp.buffers_residual &&
472 virt_to_phys(page_address(cmd->SCp.buffer[1].page)+
473 cmd->SCp.buffer[1].offset) == endaddr; ) {
474 MER_PRINTK("VTOP(%p) == %08lx -> merging\n",
475 cmd->SCp.buffer[1].address, endaddr);
476#if (NDEBUG & NDEBUG_MERGING)
477 ++cnt;
478#endif
479 ++cmd->SCp.buffer;
480 --cmd->SCp.buffers_residual;
481 cmd->SCp.this_residual += cmd->SCp.buffer->length;
482 endaddr += cmd->SCp.buffer->length;
483 }
484#if (NDEBUG & NDEBUG_MERGING)
485 if (oldlen != cmd->SCp.this_residual)
486 MER_PRINTK("merged %d buffers from %p, new length %08x\n",
487 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
488#endif
489}
490
491/*
492 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
493 *
494 * Purpose : initialize the saved data pointers for cmd to point to the
495 * start of the buffer.
496 *
497 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
498 */
499
500static __inline__ void initialize_SCp(Scsi_Cmnd *cmd)
501{
502 /*
503 * Initialize the Scsi Pointer field so that all of the commands in the
504 * various queues are valid.
505 */
506
507 if (cmd->use_sg) {
508 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
509 cmd->SCp.buffers_residual = cmd->use_sg - 1;
510 cmd->SCp.ptr = (char *)page_address(cmd->SCp.buffer->page)+
511 cmd->SCp.buffer->offset;
512 cmd->SCp.this_residual = cmd->SCp.buffer->length;
513 /* ++roman: Try to merge some scatter-buffers if they are at
514 * contiguous physical addresses.
515 */
516 merge_contiguous_buffers( cmd );
517 } else {
518 cmd->SCp.buffer = NULL;
519 cmd->SCp.buffers_residual = 0;
520 cmd->SCp.ptr = (char *) cmd->request_buffer;
521 cmd->SCp.this_residual = cmd->request_bufflen;
522 }
523}
524
525#include <linux/config.h>
526#include <linux/delay.h>
527
528#if NDEBUG
529static struct {
530 unsigned char mask;
531 const char * name;}
532signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
533 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
534 { SR_SEL, "SEL" }, {0, NULL}},
535basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
536icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
537 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
538 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
539 {0, NULL}},
540mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
541 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
542 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
543 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
544 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
545 {0, NULL}};
546
547/*
548 * Function : void NCR5380_print(struct Scsi_Host *instance)
549 *
550 * Purpose : print the SCSI bus signals for debugging purposes
551 *
552 * Input : instance - which NCR5380
553 */
554
555static void NCR5380_print(struct Scsi_Host *instance) {
556 unsigned char status, data, basr, mr, icr, i;
557 unsigned long flags;
558
559 local_irq_save(flags);
560 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
561 status = NCR5380_read(STATUS_REG);
562 mr = NCR5380_read(MODE_REG);
563 icr = NCR5380_read(INITIATOR_COMMAND_REG);
564 basr = NCR5380_read(BUS_AND_STATUS_REG);
565 local_irq_restore(flags);
566 printk("STATUS_REG: %02x ", status);
567 for (i = 0; signals[i].mask ; ++i)
568 if (status & signals[i].mask)
569 printk(",%s", signals[i].name);
570 printk("\nBASR: %02x ", basr);
571 for (i = 0; basrs[i].mask ; ++i)
572 if (basr & basrs[i].mask)
573 printk(",%s", basrs[i].name);
574 printk("\nICR: %02x ", icr);
575 for (i = 0; icrs[i].mask; ++i)
576 if (icr & icrs[i].mask)
577 printk(",%s", icrs[i].name);
578 printk("\nMODE: %02x ", mr);
579 for (i = 0; mrs[i].mask; ++i)
580 if (mr & mrs[i].mask)
581 printk(",%s", mrs[i].name);
582 printk("\n");
583}
584
585static struct {
586 unsigned char value;
587 const char *name;
588} phases[] = {
589 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
590 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
591 {PHASE_UNKNOWN, "UNKNOWN"}};
592
593/*
594 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
595 *
596 * Purpose : print the current SCSI phase for debugging purposes
597 *
598 * Input : instance - which NCR5380
599 */
600
601static void NCR5380_print_phase(struct Scsi_Host *instance)
602{
603 unsigned char status;
604 int i;
605
606 status = NCR5380_read(STATUS_REG);
607 if (!(status & SR_REQ))
608 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
609 else {
610 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
611 (phases[i].value != (status & PHASE_MASK)); ++i);
612 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
613 }
614}
615
616#else /* !NDEBUG */
617
618/* dummies... */
619__inline__ void NCR5380_print(struct Scsi_Host *instance) { };
620__inline__ void NCR5380_print_phase(struct Scsi_Host *instance) { };
621
622#endif
623
624/*
625 * ++roman: New scheme of calling NCR5380_main()
626 *
627 * If we're not in an interrupt, we can call our main directly, it cannot be
628 * already running. Else, we queue it on a task queue, if not 'main_running'
629 * tells us that a lower level is already executing it. This way,
630 * 'main_running' needs not be protected in a special way.
631 *
632 * queue_main() is a utility function for putting our main onto the task
633 * queue, if main_running is false. It should be called only from a
634 * interrupt or bottom half.
635 */
636
637#include <linux/workqueue.h>
638#include <linux/interrupt.h>
639
640static volatile int main_running = 0;
641static DECLARE_WORK(NCR5380_tqueue, (void (*)(void*))NCR5380_main, NULL);
642
643static __inline__ void queue_main(void)
644{
645 if (!main_running) {
646 /* If in interrupt and NCR5380_main() not already running,
647 queue it on the 'immediate' task queue, to be processed
648 immediately after the current interrupt processing has
649 finished. */
650 schedule_work(&NCR5380_tqueue);
651 }
652 /* else: nothing to do: the running NCR5380_main() will pick up
653 any newly queued command. */
654}
655
656
657static inline void NCR5380_all_init (void)
658{
659 static int done = 0;
660 if (!done) {
661 INI_PRINTK("scsi : NCR5380_all_init()\n");
662 done = 1;
663 }
664}
665
666
667/*
668 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
669 *
670 * Purpose : called by probe code indicating the NCR5380 driver
671 * options that were selected.
672 *
673 * Inputs : instance, pointer to this instance. Unused.
674 */
675
676static void __init NCR5380_print_options (struct Scsi_Host *instance)
677{
678 printk(" generic options"
679#ifdef AUTOSENSE
680 " AUTOSENSE"
681#endif
682#ifdef REAL_DMA
683 " REAL DMA"
684#endif
685#ifdef PARITY
686 " PARITY"
687#endif
688#ifdef SUPPORT_TAGS
689 " SCSI-2 TAGGED QUEUING"
690#endif
691 );
692 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
693}
694
695/*
696 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
697 *
698 * Purpose : print commands in the various queues, called from
699 * NCR5380_abort and NCR5380_debug to aid debugging.
700 *
701 * Inputs : instance, pointer to this instance.
702 */
703
704static void NCR5380_print_status (struct Scsi_Host *instance)
705{
706 char *pr_bfr;
707 char *start;
708 int len;
709
710 NCR_PRINT(NDEBUG_ANY);
711 NCR_PRINT_PHASE(NDEBUG_ANY);
712
713 pr_bfr = (char *) __get_free_page(GFP_ATOMIC);
714 if (!pr_bfr) {
715 printk("NCR5380_print_status: no memory for print buffer\n");
716 return;
717 }
718 len = NCR5380_proc_info(pr_bfr, &start, 0, PAGE_SIZE, HOSTNO, 0);
719 pr_bfr[len] = 0;
720 printk("\n%s\n", pr_bfr);
721 free_page((unsigned long) pr_bfr);
722}
723
724
725/******************************************/
726/*
727 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
728 *
729 * *buffer: I/O buffer
730 * **start: if inout == FALSE pointer into buffer where user read should start
731 * offset: current offset
732 * length: length of buffer
733 * hostno: Scsi_Host host_no
734 * inout: TRUE - user is writing; FALSE - user is reading
735 *
736 * Return the number of bytes read from or written
737*/
738
739#undef SPRINTF
740#define SPRINTF(fmt,args...) \
741 do { if (pos + strlen(fmt) + 20 /* slop */ < buffer + length) \
742 pos += sprintf(pos, fmt , ## args); } while(0)
743static
744char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length);
745
746static
747int NCR5380_proc_info (struct Scsi_Host *instance, char *buffer, char **start, off_t offset,
748 int length, int inout)
749{
750 char *pos = buffer;
751 struct NCR5380_hostdata *hostdata;
752 Scsi_Cmnd *ptr;
753 unsigned long flags;
754 off_t begin = 0;
755#define check_offset() \
756 do { \
757 if (pos - buffer < offset - begin) { \
758 begin += pos - buffer; \
759 pos = buffer; \
760 } \
761 } while (0)
762
763 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
764
765 if (inout) { /* Has data been written to the file ? */
766 return(-ENOSYS); /* Currently this is a no-op */
767 }
768 SPRINTF("NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
769 check_offset();
770 local_irq_save(flags);
771 SPRINTF("NCR5380: coroutine is%s running.\n", main_running ? "" : "n't");
772 check_offset();
773 if (!hostdata->connected)
774 SPRINTF("scsi%d: no currently connected command\n", HOSTNO);
775 else
776 pos = lprint_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected,
777 pos, buffer, length);
778 SPRINTF("scsi%d: issue_queue\n", HOSTNO);
779 check_offset();
780 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = NEXT(ptr)) {
781 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
782 check_offset();
783 }
784
785 SPRINTF("scsi%d: disconnected_queue\n", HOSTNO);
786 check_offset();
787 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
788 ptr = NEXT(ptr)) {
789 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
790 check_offset();
791 }
792
793 local_irq_restore(flags);
794 *start = buffer + (offset - begin);
795 if (pos - buffer < offset - begin)
796 return 0;
797 else if (pos - buffer - (offset - begin) < length)
798 return pos - buffer - (offset - begin);
799 return length;
800}
801
802static char *
803lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length)
804{
805 int i, s;
806 unsigned char *command;
807 SPRINTF("scsi%d: destination target %d, lun %d\n",
808 H_NO(cmd), cmd->device->id, cmd->device->lun);
809 SPRINTF(" command = ");
810 command = cmd->cmnd;
811 SPRINTF("%2d (0x%02x)", command[0], command[0]);
812 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
813 SPRINTF(" %02x", command[i]);
814 SPRINTF("\n");
815 return pos;
816}
817
818
819/*
820 * Function : void NCR5380_init (struct Scsi_Host *instance)
821 *
822 * Purpose : initializes *instance and corresponding 5380 chip.
823 *
824 * Inputs : instance - instantiation of the 5380 driver.
825 *
826 * Notes : I assume that the host, hostno, and id bits have been
827 * set correctly. I don't care about the irq and other fields.
828 *
829 */
830
831static int NCR5380_init (struct Scsi_Host *instance, int flags)
832{
833 int i;
834 SETUP_HOSTDATA(instance);
835
836 NCR5380_all_init();
837
838 hostdata->aborted = 0;
839 hostdata->id_mask = 1 << instance->this_id;
840 hostdata->id_higher_mask = 0;
841 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
842 if (i > hostdata->id_mask)
843 hostdata->id_higher_mask |= i;
844 for (i = 0; i < 8; ++i)
845 hostdata->busy[i] = 0;
846#ifdef SUPPORT_TAGS
847 init_tags();
848#endif
849#if defined (REAL_DMA)
850 hostdata->dma_len = 0;
851#endif
852 hostdata->targets_present = 0;
853 hostdata->connected = NULL;
854 hostdata->issue_queue = NULL;
855 hostdata->disconnected_queue = NULL;
856 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
857
858 if (!the_template) {
859 the_template = instance->hostt;
860 first_instance = instance;
861 }
862
863
864#ifndef AUTOSENSE
865 if ((instance->cmd_per_lun > 1) || (instance->can_queue > 1))
866 printk("scsi%d: WARNING : support for multiple outstanding commands enabled\n"
867 " without AUTOSENSE option, contingent allegiance conditions may\n"
868 " be incorrectly cleared.\n", HOSTNO);
869#endif /* def AUTOSENSE */
870
871 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
872 NCR5380_write(MODE_REG, MR_BASE);
873 NCR5380_write(TARGET_COMMAND_REG, 0);
874 NCR5380_write(SELECT_ENABLE_REG, 0);
875
876 return 0;
877}
878
879/*
880 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
881 * void (*done)(Scsi_Cmnd *))
882 *
883 * Purpose : enqueues a SCSI command
884 *
885 * Inputs : cmd - SCSI command, done - function called on completion, with
886 * a pointer to the command descriptor.
887 *
888 * Returns : 0
889 *
890 * Side effects :
891 * cmd is added to the per instance issue_queue, with minor
892 * twiddling done to the host specific fields of cmd. If the
893 * main coroutine is not running, it is restarted.
894 *
895 */
896
897static
898int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
899{
900 SETUP_HOSTDATA(cmd->device->host);
901 Scsi_Cmnd *tmp;
902 int oldto;
903 unsigned long flags;
904 extern int update_timeout(Scsi_Cmnd * SCset, int timeout);
905
906#if (NDEBUG & NDEBUG_NO_WRITE)
907 switch (cmd->cmnd[0]) {
908 case WRITE_6:
909 case WRITE_10:
910 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
911 H_NO(cmd));
912 cmd->result = (DID_ERROR << 16);
913 done(cmd);
914 return 0;
915 }
916#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
917
918
919#ifdef NCR5380_STATS
920# if 0
921 if (!hostdata->connected && !hostdata->issue_queue &&
922 !hostdata->disconnected_queue) {
923 hostdata->timebase = jiffies;
924 }
925# endif
926# ifdef NCR5380_STAT_LIMIT
927 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
928# endif
929 switch (cmd->cmnd[0])
930 {
931 case WRITE:
932 case WRITE_6:
933 case WRITE_10:
934 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
935 hostdata->bytes_write[cmd->device->id] += cmd->request_bufflen;
936 hostdata->pendingw++;
937 break;
938 case READ:
939 case READ_6:
940 case READ_10:
941 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
942 hostdata->bytes_read[cmd->device->id] += cmd->request_bufflen;
943 hostdata->pendingr++;
944 break;
945 }
946#endif
947
948 /*
949 * We use the host_scribble field as a pointer to the next command
950 * in a queue
951 */
952
953 NEXT(cmd) = NULL;
954 cmd->scsi_done = done;
955
956 cmd->result = 0;
957
958
959 /*
960 * Insert the cmd into the issue queue. Note that REQUEST SENSE
961 * commands are added to the head of the queue since any command will
962 * clear the contingent allegiance condition that exists and the
963 * sense data is only guaranteed to be valid while the condition exists.
964 */
965
966 local_irq_save(flags);
967 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
968 * Otherwise a running NCR5380_main may steal the lock.
969 * Lock before actually inserting due to fairness reasons explained in
970 * atari_scsi.c. If we insert first, then it's impossible for this driver
971 * to release the lock.
972 * Stop timer for this command while waiting for the lock, or timeouts
973 * may happen (and they really do), and it's no good if the command doesn't
974 * appear in any of the queues.
975 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
976 * because also a timer int can trigger an abort or reset, which would
977 * alter queues and touch the lock.
978 */
979 if (!IS_A_TT()) {
980 oldto = update_timeout(cmd, 0);
981 falcon_get_lock();
982 update_timeout(cmd, oldto);
983 }
984 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
985 LIST(cmd, hostdata->issue_queue);
986 NEXT(cmd) = hostdata->issue_queue;
987 hostdata->issue_queue = cmd;
988 } else {
989 for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
990 NEXT(tmp); tmp = NEXT(tmp))
991 ;
992 LIST(cmd, tmp);
993 NEXT(tmp) = cmd;
994 }
995 local_irq_restore(flags);
996
997 QU_PRINTK("scsi%d: command added to %s of queue\n", H_NO(cmd),
998 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
999
1000 /* If queue_command() is called from an interrupt (real one or bottom
1001 * half), we let queue_main() do the job of taking care about main. If it
1002 * is already running, this is a no-op, else main will be queued.
1003 *
1004 * If we're not in an interrupt, we can call NCR5380_main()
1005 * unconditionally, because it cannot be already running.
1006 */
1007 if (in_interrupt() || ((flags >> 8) & 7) >= 6)
1008 queue_main();
1009 else
1010 NCR5380_main(NULL);
1011 return 0;
1012}
1013
1014/*
1015 * Function : NCR5380_main (void)
1016 *
1017 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1018 * be done on the NCR5380 host adapters in a system. Both
1019 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1020 * in case it is not running.
1021 *
1022 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1023 * reenable them. This prevents reentrancy and kernel stack overflow.
1024 */
1025
1026static void NCR5380_main (void *bl)
1027{
1028 Scsi_Cmnd *tmp, *prev;
1029 struct Scsi_Host *instance = first_instance;
1030 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
1031 int done;
1032 unsigned long flags;
1033
1034 /*
1035 * We run (with interrupts disabled) until we're sure that none of
1036 * the host adapters have anything that can be done, at which point
1037 * we set main_running to 0 and exit.
1038 *
1039 * Interrupts are enabled before doing various other internal
1040 * instructions, after we've decided that we need to run through
1041 * the loop again.
1042 *
1043 * this should prevent any race conditions.
1044 *
1045 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1046 * because also a timer int can trigger an abort or reset, which can
1047 * alter queues and touch the Falcon lock.
1048 */
1049
1050 /* Tell int handlers main() is now already executing. Note that
1051 no races are possible here. If an int comes in before
1052 'main_running' is set here, and queues/executes main via the
1053 task queue, it doesn't do any harm, just this instance of main
1054 won't find any work left to do. */
1055 if (main_running)
1056 return;
1057 main_running = 1;
1058
1059 local_save_flags(flags);
1060 do {
1061 local_irq_disable(); /* Freeze request queues */
1062 done = 1;
1063
1064 if (!hostdata->connected) {
1065 MAIN_PRINTK( "scsi%d: not connected\n", HOSTNO );
1066 /*
1067 * Search through the issue_queue for a command destined
1068 * for a target that's not busy.
1069 */
1070#if (NDEBUG & NDEBUG_LISTS)
1071 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
1072 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1073 ;
1074 /*printk("%p ", tmp);*/
1075 if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1076#endif
1077 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1078 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp) ) {
1079
1080#if (NDEBUG & NDEBUG_LISTS)
1081 if (prev != tmp)
1082 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n",
1083 tmp, tmp->device->id, hostdata->busy[tmp->device->id],
1084 tmp->device->lun);
1085#endif
1086 /* When we find one, remove it from the issue queue. */
1087 /* ++guenther: possible race with Falcon locking */
1088 if (
1089#ifdef SUPPORT_TAGS
1090 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
1091#else
1092 !(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))
1093#endif
1094 ) {
1095 /* ++guenther: just to be sure, this must be atomic */
1096 local_irq_disable();
1097 if (prev) {
1098 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
1099 NEXT(prev) = NEXT(tmp);
1100 } else {
1101 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1102 hostdata->issue_queue = NEXT(tmp);
1103 }
1104 NEXT(tmp) = NULL;
1105 falcon_dont_release++;
1106
1107 /* reenable interrupts after finding one */
1108 local_irq_restore(flags);
1109
1110 /*
1111 * Attempt to establish an I_T_L nexus here.
1112 * On success, instance->hostdata->connected is set.
1113 * On failure, we must add the command back to the
1114 * issue queue so we can keep trying.
1115 */
1116 MAIN_PRINTK("scsi%d: main(): command for target %d "
1117 "lun %d removed from issue_queue\n",
1118 HOSTNO, tmp->device->id, tmp->device->lun);
1119 /*
1120 * REQUEST SENSE commands are issued without tagged
1121 * queueing, even on SCSI-II devices because the
1122 * contingent allegiance condition exists for the
1123 * entire unit.
1124 */
1125 /* ++roman: ...and the standard also requires that
1126 * REQUEST SENSE command are untagged.
1127 */
1128
1129#ifdef SUPPORT_TAGS
1130 cmd_get_tag( tmp, tmp->cmnd[0] != REQUEST_SENSE );
1131#endif
1132 if (!NCR5380_select(instance, tmp,
1133 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1134 TAG_NEXT)) {
1135 falcon_dont_release--;
1136 /* release if target did not response! */
1137 falcon_release_lock_if_possible( hostdata );
1138 break;
1139 } else {
1140 local_irq_disable();
1141 LIST(tmp, hostdata->issue_queue);
1142 NEXT(tmp) = hostdata->issue_queue;
1143 hostdata->issue_queue = tmp;
1144#ifdef SUPPORT_TAGS
1145 cmd_free_tag( tmp );
1146#endif
1147 falcon_dont_release--;
1148 local_irq_restore(flags);
1149 MAIN_PRINTK("scsi%d: main(): select() failed, "
1150 "returned to issue_queue\n", HOSTNO);
1151 if (hostdata->connected)
1152 break;
1153 }
1154 } /* if target/lun/target queue is not busy */
1155 } /* for issue_queue */
1156 } /* if (!hostdata->connected) */
1157
1158 if (hostdata->connected
1159#ifdef REAL_DMA
1160 && !hostdata->dma_len
1161#endif
1162 ) {
1163 local_irq_restore(flags);
1164 MAIN_PRINTK("scsi%d: main: performing information transfer\n",
1165 HOSTNO);
1166 NCR5380_information_transfer(instance);
1167 MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO);
1168 done = 0;
1169 }
1170 } while (!done);
1171
1172 /* Better allow ints _after_ 'main_running' has been cleared, else
1173 an interrupt could believe we'll pick up the work it left for
1174 us, but we won't see it anymore here... */
1175 main_running = 0;
1176 local_irq_restore(flags);
1177}
1178
1179
1180#ifdef REAL_DMA
1181/*
1182 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1183 *
1184 * Purpose : Called by interrupt handler when DMA finishes or a phase
1185 * mismatch occurs (which would finish the DMA transfer).
1186 *
1187 * Inputs : instance - this instance of the NCR5380.
1188 *
1189 */
1190
1191static void NCR5380_dma_complete( struct Scsi_Host *instance )
1192{
1193 SETUP_HOSTDATA(instance);
1194 int transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1195 unsigned char **data, p;
1196 volatile int *count;
1197
1198 if (!hostdata->connected) {
1199 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1200 "no connected cmd\n", HOSTNO);
1201 return;
1202 }
1203
1204 if (atari_read_overruns) {
1205 p = hostdata->connected->SCp.phase;
1206 if (p & SR_IO) {
1207 udelay(10);
1208 if ((((NCR5380_read(BUS_AND_STATUS_REG)) &
1209 (BASR_PHASE_MATCH|BASR_ACK)) ==
1210 (BASR_PHASE_MATCH|BASR_ACK))) {
1211 saved_data = NCR5380_read(INPUT_DATA_REG);
1212 overrun = 1;
1213 DMA_PRINTK("scsi%d: read overrun handled\n", HOSTNO);
1214 }
1215 }
1216 }
1217
1218 DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1219 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1220 NCR5380_read(STATUS_REG));
1221
1222 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1223 NCR5380_write(MODE_REG, MR_BASE);
1224 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1225
1226 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1227 hostdata->dma_len = 0;
1228
1229 data = (unsigned char **) &(hostdata->connected->SCp.ptr);
1230 count = &(hostdata->connected->SCp.this_residual);
1231 *data += transfered;
1232 *count -= transfered;
1233
1234 if (atari_read_overruns) {
1235 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1236 cnt = toPIO = atari_read_overruns;
1237 if (overrun) {
1238 DMA_PRINTK("Got an input overrun, using saved byte\n");
1239 *(*data)++ = saved_data;
1240 (*count)--;
1241 cnt--;
1242 toPIO--;
1243 }
1244 DMA_PRINTK("Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
1245 NCR5380_transfer_pio(instance, &p, &cnt, data);
1246 *count -= toPIO - cnt;
1247 }
1248 }
1249}
1250#endif /* REAL_DMA */
1251
1252
1253/*
1254 * Function : void NCR5380_intr (int irq)
1255 *
1256 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1257 * from the disconnected queue, and restarting NCR5380_main()
1258 * as required.
1259 *
1260 * Inputs : int irq, irq that caused this interrupt.
1261 *
1262 */
1263
1264static irqreturn_t NCR5380_intr (int irq, void *dev_id, struct pt_regs *regs)
1265{
1266 struct Scsi_Host *instance = first_instance;
1267 int done = 1, handled = 0;
1268 unsigned char basr;
1269
1270 INT_PRINTK("scsi%d: NCR5380 irq triggered\n", HOSTNO);
1271
1272 /* Look for pending interrupts */
1273 basr = NCR5380_read(BUS_AND_STATUS_REG);
1274 INT_PRINTK("scsi%d: BASR=%02x\n", HOSTNO, basr);
1275 /* dispatch to appropriate routine if found and done=0 */
1276 if (basr & BASR_IRQ) {
1277 NCR_PRINT(NDEBUG_INTR);
1278 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1279 done = 0;
1280 ENABLE_IRQ();
1281 INT_PRINTK("scsi%d: SEL interrupt\n", HOSTNO);
1282 NCR5380_reselect(instance);
1283 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1284 }
1285 else if (basr & BASR_PARITY_ERROR) {
1286 INT_PRINTK("scsi%d: PARITY interrupt\n", HOSTNO);
1287 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1288 }
1289 else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1290 INT_PRINTK("scsi%d: RESET interrupt\n", HOSTNO);
1291 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1292 }
1293 else {
1294 /*
1295 * The rest of the interrupt conditions can occur only during a
1296 * DMA transfer
1297 */
1298
1299#if defined(REAL_DMA)
1300 /*
1301 * We should only get PHASE MISMATCH and EOP interrupts if we have
1302 * DMA enabled, so do a sanity check based on the current setting
1303 * of the MODE register.
1304 */
1305
1306 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1307 ((basr & BASR_END_DMA_TRANSFER) ||
1308 !(basr & BASR_PHASE_MATCH))) {
1309
1310 INT_PRINTK("scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
1311 NCR5380_dma_complete( instance );
1312 done = 0;
1313 ENABLE_IRQ();
1314 } else
1315#endif /* REAL_DMA */
1316 {
1317/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1318 if (basr & BASR_PHASE_MATCH)
1319 printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1320 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1321 HOSTNO, basr, NCR5380_read(MODE_REG),
1322 NCR5380_read(STATUS_REG));
1323 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1324 }
1325 } /* if !(SELECTION || PARITY) */
1326 handled = 1;
1327 } /* BASR & IRQ */
1328 else {
1329 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1330 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1331 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1332 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1333 }
1334
1335 if (!done) {
1336 INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO);
1337 /* Put a call to NCR5380_main() on the queue... */
1338 queue_main();
1339 }
1340 return IRQ_RETVAL(handled);
1341}
1342
1343#ifdef NCR5380_STATS
1344static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
1345{
1346# ifdef NCR5380_STAT_LIMIT
1347 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1348# endif
1349 switch (cmd->cmnd[0])
1350 {
1351 case WRITE:
1352 case WRITE_6:
1353 case WRITE_10:
1354 hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
1355 /*hostdata->bytes_write[cmd->device->id] += cmd->request_bufflen;*/
1356 hostdata->pendingw--;
1357 break;
1358 case READ:
1359 case READ_6:
1360 case READ_10:
1361 hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
1362 /*hostdata->bytes_read[cmd->device->id] += cmd->request_bufflen;*/
1363 hostdata->pendingr--;
1364 break;
1365 }
1366}
1367#endif
1368
1369/*
1370 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1371 * int tag);
1372 *
1373 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1374 * including ARBITRATION, SELECTION, and initial message out for
1375 * IDENTIFY and queue messages.
1376 *
1377 * Inputs : instance - instantiation of the 5380 driver on which this
1378 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1379 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1380 * the command that is presently connected.
1381 *
1382 * Returns : -1 if selection could not execute for some reason,
1383 * 0 if selection succeeded or failed because the target
1384 * did not respond.
1385 *
1386 * Side effects :
1387 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1388 * with registers as they should have been on entry - ie
1389 * SELECT_ENABLE will be set appropriately, the NCR5380
1390 * will cease to drive any SCSI bus signals.
1391 *
1392 * If successful : I_T_L or I_T_L_Q nexus will be established,
1393 * instance->connected will be set to cmd.
1394 * SELECT interrupt will be disabled.
1395 *
1396 * If failed (no target) : cmd->scsi_done() will be called, and the
1397 * cmd->result host byte set to DID_BAD_TARGET.
1398 */
1399
1400static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
1401{
1402 SETUP_HOSTDATA(instance);
1403 unsigned char tmp[3], phase;
1404 unsigned char *data;
1405 int len;
1406 unsigned long timeout;
1407 unsigned long flags;
1408
1409 hostdata->restart_select = 0;
1410 NCR_PRINT(NDEBUG_ARBITRATION);
1411 ARB_PRINTK("scsi%d: starting arbitration, id = %d\n", HOSTNO,
1412 instance->this_id);
1413
1414 /*
1415 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1416 * data bus during SELECTION.
1417 */
1418
1419 local_irq_save(flags);
1420 if (hostdata->connected) {
1421 local_irq_restore(flags);
1422 return -1;
1423 }
1424 NCR5380_write(TARGET_COMMAND_REG, 0);
1425
1426
1427 /*
1428 * Start arbitration.
1429 */
1430
1431 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1432 NCR5380_write(MODE_REG, MR_ARBITRATE);
1433
1434 local_irq_restore(flags);
1435
1436 /* Wait for arbitration logic to complete */
1437#if NCR_TIMEOUT
1438 {
1439 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1440
1441 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1442 && time_before(jiffies, timeout) && !hostdata->connected)
1443 ;
1444 if (time_after_eq(jiffies, timeout))
1445 {
1446 printk("scsi : arbitration timeout at %d\n", __LINE__);
1447 NCR5380_write(MODE_REG, MR_BASE);
1448 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1449 return -1;
1450 }
1451 }
1452#else /* NCR_TIMEOUT */
1453 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1454 && !hostdata->connected);
1455#endif
1456
1457 ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO);
1458
1459 if (hostdata->connected) {
1460 NCR5380_write(MODE_REG, MR_BASE);
1461 return -1;
1462 }
1463 /*
1464 * The arbitration delay is 2.2us, but this is a minimum and there is
1465 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1466 * the integral nature of udelay().
1467 *
1468 */
1469
1470 udelay(3);
1471
1472 /* Check for lost arbitration */
1473 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1474 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1475 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1476 hostdata->connected) {
1477 NCR5380_write(MODE_REG, MR_BASE);
1478 ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1479 HOSTNO);
1480 return -1;
1481 }
1482
1483 /* after/during arbitration, BSY should be asserted.
1484 IBM DPES-31080 Version S31Q works now */
1485 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1486 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL |
1487 ICR_ASSERT_BSY ) ;
1488
1489 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1490 hostdata->connected) {
1491 NCR5380_write(MODE_REG, MR_BASE);
1492 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1493 ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1494 HOSTNO);
1495 return -1;
1496 }
1497
1498 /*
1499 * Again, bus clear + bus settle time is 1.2us, however, this is
1500 * a minimum so we'll udelay ceil(1.2)
1501 */
1502
1503#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
1504 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1505 udelay(15);
1506#else
1507 udelay(2);
1508#endif
1509
1510 if (hostdata->connected) {
1511 NCR5380_write(MODE_REG, MR_BASE);
1512 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1513 return -1;
1514 }
1515
1516 ARB_PRINTK("scsi%d: won arbitration\n", HOSTNO);
1517
1518 /*
1519 * Now that we have won arbitration, start Selection process, asserting
1520 * the host and target ID's on the SCSI bus.
1521 */
1522
1523 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1524
1525 /*
1526 * Raise ATN while SEL is true before BSY goes false from arbitration,
1527 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1528 * phase immediately after selection.
1529 */
1530
1531 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1532 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1533 NCR5380_write(MODE_REG, MR_BASE);
1534
1535 /*
1536 * Reselect interrupts must be turned off prior to the dropping of BSY,
1537 * otherwise we will trigger an interrupt.
1538 */
1539
1540 if (hostdata->connected) {
1541 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1542 return -1;
1543 }
1544
1545 NCR5380_write(SELECT_ENABLE_REG, 0);
1546
1547 /*
1548 * The initiator shall then wait at least two deskew delays and release
1549 * the BSY signal.
1550 */
1551 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1552
1553 /* Reset BSY */
1554 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1555 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1556
1557 /*
1558 * Something weird happens when we cease to drive BSY - looks
1559 * like the board/chip is letting us do another read before the
1560 * appropriate propagation delay has expired, and we're confusing
1561 * a BSY signal from ourselves as the target's response to SELECTION.
1562 *
1563 * A small delay (the 'C++' frontend breaks the pipeline with an
1564 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1565 * tighter 'C' code breaks and requires this) solves the problem -
1566 * the 1 us delay is arbitrary, and only used because this delay will
1567 * be the same on other platforms and since it works here, it should
1568 * work there.
1569 *
1570 * wingel suggests that this could be due to failing to wait
1571 * one deskew delay.
1572 */
1573
1574 udelay(1);
1575
1576 SEL_PRINTK("scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
1577
1578 /*
1579 * The SCSI specification calls for a 250 ms timeout for the actual
1580 * selection.
1581 */
1582
1583 timeout = jiffies + 25;
1584
1585 /*
1586 * XXX very interesting - we're seeing a bounce where the BSY we
1587 * asserted is being reflected / still asserted (propagation delay?)
1588 * and it's detecting as true. Sigh.
1589 */
1590
1591#if 0
1592 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1593 * IO while SEL is true. But again, there are some disks out the in the
1594 * world that do that nevertheless. (Somebody claimed that this announces
1595 * reselection capability of the target.) So we better skip that test and