OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [scsi/] [atari_NCR5380.c] - Blame information for rev 1626

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
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_consecutive_buffers() that tries to
51
 *    merge scatter-gather buffers that are located at consecutive
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 a 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
 
256
static struct Scsi_Host *first_instance = NULL;
257
static 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)->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
 
312
/* For the m68k, the number of bits in 'allocated' must be a multiple of 32! */
313
#if (MAX_TAGS % 32) != 0
314
#error "MAX_TAGS must be a multiple of 32!"
315
#endif
316
 
317
typedef struct {
318
    char        allocated[MAX_TAGS/8];
319
    int         nr_allocated;
320
    int         queue_size;
321
} TAG_ALLOC;
322
 
323
static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
324
 
325
 
326
static void init_tags( void )
327
{
328
    int target, lun;
329
    TAG_ALLOC *ta;
330
 
331
    if (!setup_use_tagged_queuing)
332
        return;
333
 
334
    for( target = 0; target < 8; ++target ) {
335
        for( lun = 0; lun < 8; ++lun ) {
336
            ta = &TagAlloc[target][lun];
337
            memset( &ta->allocated, 0, MAX_TAGS/8 );
338
            ta->nr_allocated = 0;
339
            /* At the beginning, assume the maximum queue size we could
340
             * support (MAX_TAGS). This value will be decreased if the target
341
             * returns QUEUE_FULL status.
342
             */
343
            ta->queue_size = MAX_TAGS;
344
        }
345
    }
346
}
347
 
348
 
349
/* Check if we can issue a command to this LUN: First see if the LUN is marked
350
 * busy by an untagged command. If the command should use tagged queuing, also
351
 * check that there is a free tag and the target's queue won't overflow. This
352
 * function should be called with interrupts disabled to avoid race
353
 * conditions.
354
 */
355
 
356
static int is_lun_busy( Scsi_Cmnd *cmd, int should_be_tagged )
357
{
358
    SETUP_HOSTDATA(cmd->host);
359
 
360
    if (hostdata->busy[cmd->target] & (1 << cmd->lun))
361
        return( 1 );
362
    if (!should_be_tagged ||
363
        !setup_use_tagged_queuing || !cmd->device->tagged_supported)
364
        return( 0 );
365
    if (TagAlloc[cmd->target][cmd->lun].nr_allocated >=
366
        TagAlloc[cmd->target][cmd->lun].queue_size ) {
367
        TAG_PRINTK( "scsi%d: target %d lun %d: no free tags\n",
368
                    H_NO(cmd), cmd->target, cmd->lun );
369
        return( 1 );
370
    }
371
    return( 0 );
372
}
373
 
374
 
375
/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
376
 * must be called before!), or reserve the LUN in 'busy' if the command is
377
 * untagged.
378
 */
379
 
380
static void cmd_get_tag( Scsi_Cmnd *cmd, int should_be_tagged )
381
{
382
    SETUP_HOSTDATA(cmd->host);
383
 
384
    /* If we or the target don't support tagged queuing, allocate the LUN for
385
     * an untagged command.
386
     */
387
    if (!should_be_tagged ||
388
        !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
389
        cmd->tag = TAG_NONE;
390
        hostdata->busy[cmd->target] |= (1 << cmd->lun);
391
        TAG_PRINTK( "scsi%d: target %d lun %d now allocated by untagged "
392
                    "command\n", H_NO(cmd), cmd->target, cmd->lun );
393
    }
394
    else {
395
        TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
396
 
397
        cmd->tag = find_first_zero_bit( &ta->allocated, MAX_TAGS );
398
        set_bit( cmd->tag, &ta->allocated );
399
        ta->nr_allocated++;
400
        TAG_PRINTK( "scsi%d: using tag %d for target %d lun %d "
401
                    "(now %d tags in use)\n",
402
                    H_NO(cmd), cmd->tag, cmd->target, cmd->lun,
403
                    ta->nr_allocated );
404
    }
405
}
406
 
407
 
408
/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
409
 * unlock the LUN.
410
 */
411
 
412
static void cmd_free_tag( Scsi_Cmnd *cmd )
413
{
414
    SETUP_HOSTDATA(cmd->host);
415
 
416
    if (cmd->tag == TAG_NONE) {
417
        hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
418
        TAG_PRINTK( "scsi%d: target %d lun %d untagged cmd finished\n",
419
                    H_NO(cmd), cmd->target, cmd->lun );
420
    }
421
    else if (cmd->tag >= MAX_TAGS) {
422
        printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
423
                H_NO(cmd), cmd->tag );
424
    }
425
    else {
426
        TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
427
        clear_bit( cmd->tag, &ta->allocated );
428
        ta->nr_allocated--;
429
        TAG_PRINTK( "scsi%d: freed tag %d for target %d lun %d\n",
430
                    H_NO(cmd), cmd->tag, cmd->target, cmd->lun );
431
    }
432
}
433
 
434
 
435
#if 1
436
static void free_all_tags( void )
437
{
438
    int target, lun;
439
    TAG_ALLOC *ta;
440
 
441
    if (!setup_use_tagged_queuing)
442
        return;
443
 
444
    for( target = 0; target < 8; ++target ) {
445
        for( lun = 0; lun < 8; ++lun ) {
446
            ta = &TagAlloc[target][lun];
447
            memset( &ta->allocated, 0, MAX_TAGS/8 );
448
            ta->nr_allocated = 0;
449
        }
450
    }
451
}
452
#endif
453
 
454
#endif /* SUPPORT_TAGS */
455
 
456
 
457
/*
458
 * Function: void merge_consecutive_buffers( Scsi_Cmnd *cmd )
459
 *
460
 * Purpose: Try to merge several scatter-gather requests into one DMA
461
 *    transfer. This is possible if the scatter buffers lie on
462
 *    physical consecutive addresses.
463
 *
464
 * Parameters: Scsi_Cmnd *cmd
465
 *    The command to work on. The first scatter buffer's data are
466
 *    assumed to be already transfered into ptr/this_residual.
467
 */
468
 
469
/* A special issue is when the buffer is exactly at the end of the
470
 * last physical memory chunk: VTOP would have to calculate the
471
 * physical address just 1 byte behind the end of physical memory. But
472
 * it will panic if given this address :-( So we need to avoid calling
473
 * VTOP on addresses that don't exist. This is done by keeping
474
 * 'endadr' to be the real end address of the buffer, not one byte
475
 * more (which would be easier).
476
 */
477
 
478
static void merge_consecutive_buffers( Scsi_Cmnd *cmd )
479
{
480
    unsigned long endadr;
481
#if (NDEBUG & NDEBUG_MERGING)
482
    unsigned long oldlen = cmd->SCp.this_residual;
483
    int           cnt = 1;
484
#endif
485
 
486
    for( endadr = VTOP(cmd->SCp.ptr + cmd->SCp.this_residual - 1);
487
         cmd->SCp.buffers_residual &&
488
         VTOP( (cmd->SCp.buffer+1)->address ) == endadr + 1; ) {
489
 
490
        MER_PRINTK( "%08lx == %08lx -> merging\n",
491
                    VTOP( (cmd->SCp.buffer+1)->address ), endadr );
492
#if (NDEBUG & NDEBUG_MERGING)
493
        ++cnt;
494
#endif
495
        ++cmd->SCp.buffer;
496
        --cmd->SCp.buffers_residual;
497
        cmd->SCp.this_residual += cmd->SCp.buffer->length;
498
        endadr += cmd->SCp.buffer->length;
499
    }
500
#if (NDEBUG & NDEBUG_MERGING)
501
    if (oldlen != cmd->SCp.this_residual)
502
        MER_PRINTK( "merged %d buffers from %08lx, new length %08lx\n",
503
                    cnt, (long)(cmd->SCp.ptr), cmd->SCp.this_residual );
504
#endif
505
}
506
 
507
/*
508
 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
509
 *
510
 * Purpose : initialize the saved data pointers for cmd to point to the
511
 *      start of the buffer.
512
 *
513
 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
514
 */
515
 
516
static __inline__ void initialize_SCp(Scsi_Cmnd *cmd)
517
{
518
    /*
519
     * Initialize the Scsi Pointer field so that all of the commands in the
520
     * various queues are valid.
521
     */
522
 
523
    if (cmd->use_sg) {
524
        cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
525
        cmd->SCp.buffers_residual = cmd->use_sg - 1;
526
        cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
527
        cmd->SCp.this_residual = cmd->SCp.buffer->length;
528
        /* ++roman: Try to merge some scatter-buffers if they are at
529
         * consecutive physical addresses.
530
         */
531
        merge_consecutive_buffers( cmd );
532
    } else {
533
        cmd->SCp.buffer = NULL;
534
        cmd->SCp.buffers_residual = 0;
535
        cmd->SCp.ptr = (char *) cmd->request_buffer;
536
        cmd->SCp.this_residual = cmd->request_bufflen;
537
    }
538
}
539
 
540
#include <linux/config.h>
541
#include <linux/delay.h>
542
 
543
#if NDEBUG
544
static struct {
545
    unsigned char mask;
546
    const char * name;}
547
signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
548
    { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD,  "CD" }, { SR_IO, "IO" },
549
    { SR_SEL, "SEL" }, {0, NULL}},
550
basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
551
icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
552
    {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
553
    {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
554
    {0, NULL}},
555
mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
556
    {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
557
    "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
558
    {MR_MONITOR_BSY, "MODE MONITOR BSY"},
559
    {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
560
    {0, NULL}};
561
 
562
/*
563
 * Function : void NCR5380_print(struct Scsi_Host *instance)
564
 *
565
 * Purpose : print the SCSI bus signals for debugging purposes
566
 *
567
 * Input : instance - which NCR5380
568
 */
569
 
570
static void NCR5380_print(struct Scsi_Host *instance) {
571
    unsigned char status, data, basr, mr, icr, i;
572
    unsigned long flags;
573
 
574
    save_flags(flags);
575
    cli();
576
    data = NCR5380_read(CURRENT_SCSI_DATA_REG);
577
    status = NCR5380_read(STATUS_REG);
578
    mr = NCR5380_read(MODE_REG);
579
    icr = NCR5380_read(INITIATOR_COMMAND_REG);
580
    basr = NCR5380_read(BUS_AND_STATUS_REG);
581
    restore_flags(flags);
582
    printk("STATUS_REG: %02x ", status);
583
    for (i = 0; signals[i].mask ; ++i)
584
        if (status & signals[i].mask)
585
            printk(",%s", signals[i].name);
586
    printk("\nBASR: %02x ", basr);
587
    for (i = 0; basrs[i].mask ; ++i)
588
        if (basr & basrs[i].mask)
589
            printk(",%s", basrs[i].name);
590
    printk("\nICR: %02x ", icr);
591
    for (i = 0; icrs[i].mask; ++i)
592
        if (icr & icrs[i].mask)
593
            printk(",%s", icrs[i].name);
594
    printk("\nMODE: %02x ", mr);
595
    for (i = 0; mrs[i].mask; ++i)
596
        if (mr & mrs[i].mask)
597
            printk(",%s", mrs[i].name);
598
    printk("\n");
599
}
600
 
601
static struct {
602
    unsigned char value;
603
    const char *name;
604
} phases[] = {
605
    {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
606
    {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
607
    {PHASE_UNKNOWN, "UNKNOWN"}};
608
 
609
/*
610
 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
611
 *
612
 * Purpose : print the current SCSI phase for debugging purposes
613
 *
614
 * Input : instance - which NCR5380
615
 */
616
 
617
static void NCR5380_print_phase(struct Scsi_Host *instance)
618
{
619
    unsigned char status;
620
    int i;
621
 
622
    status = NCR5380_read(STATUS_REG);
623
    if (!(status & SR_REQ))
624
        printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
625
    else {
626
        for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
627
            (phases[i].value != (status & PHASE_MASK)); ++i);
628
        printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
629
    }
630
}
631
 
632
#else /* !NDEBUG */
633
 
634
/* dummies... */
635
__inline__ void NCR5380_print(struct Scsi_Host *instance) { };
636
__inline__ void NCR5380_print_phase(struct Scsi_Host *instance) { };
637
 
638
#endif
639
 
640
/*
641
 * ++roman: New scheme of calling NCR5380_main()
642
 *
643
 * If we're not in an interrupt, we can call our main directly, it cannot be
644
 * already running. Else, we queue it on a task queue, if not 'main_running'
645
 * tells us that a lower level is already executing it. This way,
646
 * 'main_running' needs not be protected in a special way.
647
 *
648
 * queue_main() is a utility function for putting our main onto the task
649
 * queue, if main_running is false. It should be called only from a
650
 * interrupt or bottom half.
651
 */
652
 
653
#include <linux/tqueue.h>
654
#include <linux/interrupt.h>
655
 
656
static volatile int main_running = 0;
657
static struct tq_struct NCR5380_tqueue = {
658
    NULL,               /* next */
659
    0,                   /* sync */
660
    (void (*)(void*))NCR5380_main,  /* routine, must have (void *) arg... */
661
    NULL                /* data */
662
};
663
 
664
static __inline__ void queue_main(void)
665
{
666
    if (!main_running) {
667
        /* If in interrupt and NCR5380_main() not already running,
668
           queue it on the 'immediate' task queue, to be processed
669
           immediately after the current interrupt processing has
670
           finished. */
671
        queue_task_irq(&NCR5380_tqueue, &tq_immediate);
672
        mark_bh(IMMEDIATE_BH);
673
    }
674
    /* else: nothing to do: the running NCR5380_main() will pick up
675
       any newly queued command. */
676
}
677
 
678
 
679
static void NCR5380_all_init (void)
680
{
681
    static int done = 0;
682
    if (!done) {
683
        INI_PRINTK("scsi : NCR5380_all_init()\n");
684
        done = 1;
685
    }
686
}
687
 
688
 
689
/*
690
 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
691
 *
692
 * Purpose : called by probe code indicating the NCR5380 driver
693
 *           options that were selected.
694
 *
695
 * Inputs : instance, pointer to this instance.  Unused.
696
 */
697
 
698
static void NCR5380_print_options (struct Scsi_Host *instance)
699
{
700
    printk(" generic options"
701
#ifdef AUTOSENSE 
702
    " AUTOSENSE"
703
#endif
704
#ifdef REAL_DMA
705
    " REAL DMA"
706
#endif
707
#ifdef PARITY
708
    " PARITY"
709
#endif
710
#ifdef SUPPORT_TAGS
711
    " SCSI-2 TAGGED QUEUING"
712
#endif
713
    );
714
    printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
715
}
716
 
717
/*
718
 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
719
 *
720
 * Purpose : print commands in the various queues, called from
721
 *      NCR5380_abort and NCR5380_debug to aid debugging.
722
 *
723
 * Inputs : instance, pointer to this instance.
724
 */
725
 
726
static void NCR5380_print_status (struct Scsi_Host *instance)
727
{
728
    char pr_bfr[256];
729
    char *start;
730
    int len;
731
 
732
    NCR_PRINT(NDEBUG_ANY);
733
    NCR_PRINT_PHASE(NDEBUG_ANY);
734
 
735
    len = NCR5380_proc_info(pr_bfr, &start, 0, sizeof(pr_bfr), HOSTNO, 0);
736
    pr_bfr[len] = 0;
737
    printk("\n%s\n", pr_bfr);
738
}
739
 
740
 
741
/******************************************/
742
/*
743
 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
744
 *
745
 * *buffer: I/O buffer
746
 * **start: if inout == FALSE pointer into buffer where user read should start
747
 * offset: current offset
748
 * length: length of buffer
749
 * hostno: Scsi_Host host_no
750
 * inout: TRUE - user is writing; FALSE - user is reading
751
 *
752
 * Return the number of bytes read from or written
753
*/
754
 
755
#undef SPRINTF
756
#define SPRINTF(args...) do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
757
static
758
char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length);
759
static
760
char *lprint_command (unsigned char *cmd, char *pos, char *buffer, int len);
761
static
762
char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
763
 
764
#ifndef NCR5380_proc_info
765
static
766
#endif
767
int NCR5380_proc_info (char *buffer, char **start, off_t offset,
768
                       int length, int hostno, int inout)
769
{
770
    char *pos = buffer;
771
    struct Scsi_Host *instance;
772
    struct NCR5380_hostdata *hostdata;
773
    Scsi_Cmnd *ptr;
774
    unsigned long flags;
775
 
776
    for (instance = first_instance; instance && HOSTNO != hostno;
777
         instance = instance->next)
778
        ;
779
    if (!instance)
780
        return(-ESRCH);
781
    hostdata = (struct NCR5380_hostdata *)instance->hostdata;
782
 
783
    if (inout) { /* Has data been written to the file ? */
784
        return(-ENOSYS);  /* Currently this is a no-op */
785
    }
786
    SPRINTF("NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
787
    save_flags(flags);
788
    cli();
789
    SPRINTF("NCR5380: coroutine is%s running.\n", main_running ? "" : "n't");
790
    if (!hostdata->connected)
791
        SPRINTF("scsi%d: no currently connected command\n", HOSTNO);
792
    else
793
        pos = lprint_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected,
794
                                pos, buffer, length);
795
    SPRINTF("scsi%d: issue_queue\n", HOSTNO);
796
    for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = NEXT(ptr))
797
        pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
798
 
799
    SPRINTF("scsi%d: disconnected_queue\n", HOSTNO);
800
    for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
801
         ptr = NEXT(ptr))
802
        pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
803
 
804
    restore_flags(flags);
805
    *start = buffer;
806
    if (pos - buffer < offset)
807
        return 0;
808
    else if (pos - buffer - offset < length)
809
        return pos - buffer - offset;
810
    return length;
811
}
812
 
813
static char *
814
lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length)
815
{
816
    SPRINTF("scsi%d: destination target %d, lun %d\n",
817
            H_NO(cmd), cmd->target, cmd->lun);
818
    SPRINTF("        command = ");
819
    pos = lprint_command (cmd->cmnd, pos, buffer, length);
820
    return (pos);
821
}
822
 
823
static char *
824
lprint_command (unsigned char *command, char *pos, char *buffer, int length)
825
{
826
    int i, s;
827
    pos = lprint_opcode(command[0], pos, buffer, length);
828
    for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
829
        SPRINTF("%02x ", command[i]);
830
    SPRINTF("\n");
831
    return(pos);
832
}
833
 
834
static
835
char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
836
{
837
    SPRINTF("%2d (0x%02x)", opcode, opcode);
838
    return(pos);
839
}
840
 
841
 
842
/*
843
 * Function : void NCR5380_init (struct Scsi_Host *instance)
844
 *
845
 * Purpose : initializes *instance and corresponding 5380 chip.
846
 *
847
 * Inputs : instance - instantiation of the 5380 driver.
848
 *
849
 * Notes : I assume that the host, hostno, and id bits have been
850
 *      set correctly.  I don't care about the irq and other fields.
851
 *
852
 */
853
 
854
static void NCR5380_init (struct Scsi_Host *instance, int flags)
855
{
856
    int i;
857
    SETUP_HOSTDATA(instance);
858
 
859
    NCR5380_all_init();
860
 
861
    hostdata->aborted = 0;
862
    hostdata->id_mask = 1 << instance->this_id;
863
    hostdata->id_higher_mask = 0;
864
    for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
865
        if (i > hostdata->id_mask)
866
            hostdata->id_higher_mask |= i;
867
    for (i = 0; i < 8; ++i)
868
        hostdata->busy[i] = 0;
869
#ifdef SUPPORT_TAGS
870
    init_tags();
871
#endif
872
#if defined (REAL_DMA)
873
    hostdata->dma_len = 0;
874
#endif
875
    hostdata->targets_present = 0;
876
    hostdata->connected = NULL;
877
    hostdata->issue_queue = NULL;
878
    hostdata->disconnected_queue = NULL;
879
    hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
880
 
881
    if (!the_template) {
882
        the_template = instance->hostt;
883
        first_instance = instance;
884
    }
885
 
886
 
887
#ifndef AUTOSENSE
888
    if ((instance->cmd_per_lun > 1) || instance->can_queue > 1))
889
         printk("scsi%d: WARNING : support for multiple outstanding commands enabled\n"
890
                "        without AUTOSENSE option, contingent allegiance conditions may\n"
891
                "        be incorrectly cleared.\n", HOSTNO);
892
#endif /* def AUTOSENSE */
893
 
894
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
895
    NCR5380_write(MODE_REG, MR_BASE);
896
    NCR5380_write(TARGET_COMMAND_REG, 0);
897
    NCR5380_write(SELECT_ENABLE_REG, 0);
898
}
899
 
900
/*
901
 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
902
 *      void (*done)(Scsi_Cmnd *))
903
 *
904
 * Purpose :  enqueues a SCSI command
905
 *
906
 * Inputs : cmd - SCSI command, done - function called on completion, with
907
 *      a pointer to the command descriptor.
908
 *
909
 * Returns : 0
910
 *
911
 * Side effects :
912
 *      cmd is added to the per instance issue_queue, with minor
913
 *      twiddling done to the host specific fields of cmd.  If the
914
 *      main coroutine is not running, it is restarted.
915
 *
916
 */
917
 
918
/* Only make static if a wrapper function is used */
919
#ifndef NCR5380_queue_command
920
static
921
#endif
922
int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
923
{
924
    SETUP_HOSTDATA(cmd->host);
925
    Scsi_Cmnd *tmp;
926
    int oldto;
927
    unsigned long flags;
928
    extern int scsi_update_timeout(Scsi_Cmnd * SCset, int timeout);
929
 
930
#if (NDEBUG & NDEBUG_NO_WRITE)
931
    switch (cmd->cmnd[0]) {
932
    case WRITE_6:
933
    case WRITE_10:
934
        printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
935
               H_NO(cmd));
936
        cmd->result = (DID_ERROR << 16);
937
        done(cmd);
938
        return 0;
939
    }
940
#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
941
 
942
 
943
#ifdef NCR5380_STATS
944
# if 0
945
    if (!hostdata->connected && !hostdata->issue_queue &&
946
        !hostdata->disconnected_queue) {
947
        hostdata->timebase = jiffies;
948
    }
949
# endif
950
# ifdef NCR5380_STAT_LIMIT
951
    if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
952
# endif
953
        switch (cmd->cmnd[0])
954
        {
955
            case WRITE:
956
            case WRITE_6:
957
            case WRITE_10:
958
                hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
959
                hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
960
                hostdata->pendingw++;
961
                break;
962
            case READ:
963
            case READ_6:
964
            case READ_10:
965
                hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
966
                hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
967
                hostdata->pendingr++;
968
                break;
969
        }
970
#endif
971
 
972
    /*
973
     * We use the host_scribble field as a pointer to the next command
974
     * in a queue
975
     */
976
 
977
    NEXT(cmd) = NULL;
978
    cmd->scsi_done = done;
979
 
980
    cmd->result = 0;
981
 
982
 
983
    /*
984
     * Insert the cmd into the issue queue. Note that REQUEST SENSE
985
     * commands are added to the head of the queue since any command will
986
     * clear the contingent allegiance condition that exists and the
987
     * sense data is only guaranteed to be valid while the condition exists.
988
     */
989
 
990
    save_flags(flags);
991
    cli();
992
    /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
993
     * Otherwise a running NCR5380_main may steal the lock.
994
     * Lock before actually inserting due to fairness reasons explained in
995
     * atari_scsi.c. If we insert first, then it's impossible for this driver
996
     * to release the lock.
997
     * Stop timer for this command while waiting for the lock, or timeouts
998
     * may happen (and they really do), and it's no good if the command doesn't
999
     * appear in any of the queues.
1000
     * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1001
     * because also a timer int can trigger an abort or reset, which would
1002
     * alter queues and touch the lock.
1003
     */
1004
    if (!IS_A_TT()) {
1005
        oldto = scsi_update_timeout(cmd, 0);
1006
        falcon_get_lock();
1007
        scsi_update_timeout(cmd, oldto);
1008
    }
1009
    if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1010
        LIST(cmd, hostdata->issue_queue);
1011
        NEXT(cmd) = hostdata->issue_queue;
1012
        hostdata->issue_queue = cmd;
1013
    } else {
1014
        for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
1015
             NEXT(tmp); tmp = NEXT(tmp))
1016
            ;
1017
        LIST(cmd, tmp);
1018
        NEXT(tmp) = cmd;
1019
    }
1020
    restore_flags(flags);
1021
 
1022
    QU_PRINTK("scsi%d: command added to %s of queue\n", H_NO(cmd),
1023
              (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1024
 
1025
    /* If queue_command() is called from an interrupt (real one or bottom
1026
     * half), we let queue_main() do the job of taking care about main. If it
1027
     * is already running, this is a no-op, else main will be queued.
1028
     *
1029
     * If we're not in an interrupt, we can call NCR5380_main()
1030
     * unconditionally, because it cannot be already running.
1031
     */
1032
    if (intr_count > 0)
1033
        queue_main();
1034
    else
1035
        NCR5380_main();
1036
    return 0;
1037
}
1038
 
1039
/*
1040
 * Function : NCR5380_main (void)
1041
 *
1042
 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1043
 *      be done on the NCR5380 host adapters in a system.  Both
1044
 *      NCR5380_queue_command() and NCR5380_intr() will try to start it
1045
 *      in case it is not running.
1046
 *
1047
 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1048
 *  reenable them.  This prevents reentrancy and kernel stack overflow.
1049
 */
1050
 
1051
static void NCR5380_main (void)
1052
{
1053
    Scsi_Cmnd *tmp, *prev;
1054
    struct Scsi_Host *instance = first_instance;
1055
    struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
1056
    int done;
1057
    unsigned long flags;
1058
 
1059
    /*
1060
     * We run (with interrupts disabled) until we're sure that none of
1061
     * the host adapters have anything that can be done, at which point
1062
     * we set main_running to 0 and exit.
1063
     *
1064
     * Interrupts are enabled before doing various other internal
1065
     * instructions, after we've decided that we need to run through
1066
     * the loop again.
1067
     *
1068
     * this should prevent any race conditions.
1069
     *
1070
     * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1071
     * because also a timer int can trigger an abort or reset, which can
1072
     * alter queues and touch the Falcon lock.
1073
     */
1074
 
1075
    /* Tell int handlers main() is now already executing.  Note that
1076
       no races are possible here. If an int comes in before
1077
       'main_running' is set here, and queues/executes main via the
1078
       task queue, it doesn't do any harm, just this instance of main
1079
       won't find any work left to do. */
1080
    main_running = 1;
1081
 
1082
    save_flags(flags);
1083
    do {
1084
        cli(); /* Freeze request queues */
1085
        done = 1;
1086
 
1087
        if (!hostdata->connected) {
1088
            MAIN_PRINTK( "scsi%d: not connected\n", HOSTNO );
1089
            /*
1090
             * Search through the issue_queue for a command destined
1091
             * for a target that's not busy.
1092
             */
1093
#if (NDEBUG & NDEBUG_LISTS)
1094
            for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
1095
                 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1096
                ;
1097
                /*printk("%p  ", tmp);*/
1098
            if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1099
#endif
1100
            for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1101
                 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp) ) {
1102
 
1103
#if (NDEBUG & NDEBUG_LISTS)
1104
                if (prev != tmp)
1105
                    printk("MAIN tmp=%p   target=%d   busy=%d lun=%d\n",
1106
                           tmp, tmp->target, hostdata->busy[tmp->target],
1107
                           tmp->lun);
1108
#endif
1109
                /*  When we find one, remove it from the issue queue. */
1110
                /* ++guenther: possible race with Falcon locking */
1111
                if (
1112
#ifdef SUPPORT_TAGS
1113
                    !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
1114
#else
1115
                    !(hostdata->busy[tmp->target] & (1 << tmp->lun))
1116
#endif
1117
                    ) {
1118
                    cli(); /* ++guenther: just to be sure, this must be atomic */
1119
                    if (prev) {
1120
                        REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
1121
                        NEXT(prev) = NEXT(tmp);
1122
                    } else {
1123
                        REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1124
                        hostdata->issue_queue = NEXT(tmp);
1125
                    }
1126
                    NEXT(tmp) = NULL;
1127
                    falcon_dont_release++;
1128
 
1129
                    /* reenable interrupts after finding one */
1130
                    restore_flags(flags);
1131
 
1132
                    /*
1133
                     * Attempt to establish an I_T_L nexus here.
1134
                     * On success, instance->hostdata->connected is set.
1135
                     * On failure, we must add the command back to the
1136
                     *   issue queue so we can keep trying.
1137
                     */
1138
                    DPRINTK(NDEBUG_MAIN | NDEBUG_QUEUES,
1139
                            "scsi%d: main(): command for target %d lun %d "
1140
                            "removed from issue_queue\n",
1141
                            HOSTNO, tmp->target, tmp->lun);
1142
                    /*
1143
                     * REQUEST SENSE commands are issued without tagged
1144
                     * queueing, even on SCSI-II devices because the
1145
                     * contingent allegiance condition exists for the
1146
                     * entire unit.
1147
                     */
1148
                    /* ++roman: ...and the standard also requires that
1149
                     * REQUEST SENSE command are untagged.
1150
                     */
1151
 
1152
#ifdef SUPPORT_TAGS
1153
                    cmd_get_tag( tmp, tmp->cmnd[0] != REQUEST_SENSE );
1154
#endif
1155
                    if (!NCR5380_select(instance, tmp,
1156
                            (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1157
                            TAG_NEXT)) {
1158
                        falcon_dont_release--;
1159
                        /* release if target did not response! */
1160
                        falcon_release_lock_if_possible( hostdata );
1161
                        break;
1162
                    } else {
1163
                        cli();
1164
                        LIST(tmp, hostdata->issue_queue);
1165
                        NEXT(tmp) = hostdata->issue_queue;
1166
                        hostdata->issue_queue = tmp;
1167
#ifdef SUPPORT_TAGS
1168
                        cmd_free_tag( tmp );
1169
#endif
1170
                        falcon_dont_release--;
1171
                        restore_flags(flags);
1172
                        DPRINTK(NDEBUG_MAIN | NDEBUG_QUEUES,
1173
                                "scsi%d: main(): select() failed, "
1174
                                "returned to issue_queue\n", HOSTNO);
1175
                        if (hostdata->connected)
1176
                            break;
1177
                    }
1178
                } /* if target/lun/target queue is not busy */
1179
            } /* for issue_queue */
1180
        } /* if (!hostdata->connected) */
1181
 
1182
        if (hostdata->connected
1183
#ifdef REAL_DMA
1184
            && !hostdata->dma_len
1185
#endif
1186
            ) {
1187
            restore_flags(flags);
1188
            MAIN_PRINTK("scsi%d: main: performing information transfer\n",
1189
                        HOSTNO);
1190
            NCR5380_information_transfer(instance);
1191
            MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO);
1192
            done = 0;
1193
        }
1194
    } while (!done);
1195
 
1196
    /* Better allow ints _after_ 'main_running' has been cleared, else
1197
       an interrupt could believe we'll pick up the work it left for
1198
       us, but we won't see it anymore here... */
1199
    main_running = 0;
1200
    restore_flags(flags);
1201
}
1202
 
1203
 
1204
#ifdef REAL_DMA
1205
/*
1206
 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1207
 *
1208
 * Purpose : Called by interrupt handler when DMA finishes or a phase
1209
 *      mismatch occurs (which would finish the DMA transfer).
1210
 *
1211
 * Inputs : instance - this instance of the NCR5380.
1212
 *
1213
 */
1214
 
1215
static void NCR5380_dma_complete( struct Scsi_Host *instance )
1216
{
1217
    SETUP_HOSTDATA(instance);
1218
    int           transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1219
    unsigned char **data, p;
1220
    volatile int  *count;
1221
 
1222
    if (!hostdata->connected) {
1223
        printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1224
               "no connected cmd\n", HOSTNO);
1225
        return;
1226
    }
1227
 
1228
    if (atari_read_overruns) {
1229
        p = hostdata->connected->SCp.phase;
1230
        if (p & SR_IO) {
1231
            udelay(10);
1232
            if ((((NCR5380_read(BUS_AND_STATUS_REG)) &
1233
                  (BASR_PHASE_MATCH|BASR_ACK)) ==
1234
                 (BASR_PHASE_MATCH|BASR_ACK))) {
1235
                saved_data = NCR5380_read(INPUT_DATA_REG);
1236
                overrun = 1;
1237
                DMA_PRINTK("scsi%d: read overrun handled\n", HOSTNO);
1238
            }
1239
        }
1240
    }
1241
 
1242
    DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1243
               HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1244
               NCR5380_read(STATUS_REG));
1245
 
1246
    (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1247
    NCR5380_write(MODE_REG, MR_BASE);
1248
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1249
 
1250
    transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1251
    hostdata->dma_len = 0;
1252
 
1253
    data = (unsigned char **) &(hostdata->connected->SCp.ptr);
1254
    count = &(hostdata->connected->SCp.this_residual);
1255
    *data += transfered;
1256
    *count -= transfered;
1257
 
1258
    if (atari_read_overruns) {
1259
        if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1260
            cnt = toPIO = atari_read_overruns;
1261
            if (overrun) {
1262
                DMA_PRINTK("Got an input overrun, using saved byte\n");
1263
                *(*data)++ = saved_data;
1264
                (*count)--;
1265
                cnt--;
1266
                toPIO--;
1267
            }
1268
            DMA_PRINTK("Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
1269
            NCR5380_transfer_pio(instance, &p, &cnt, data);
1270
            *count -= toPIO - cnt;
1271
        }
1272
    }
1273
}
1274
#endif /* REAL_DMA */
1275
 
1276
 
1277
/*
1278
 * Function : void NCR5380_intr (int irq)
1279
 *
1280
 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1281
 *      from the disconnected queue, and restarting NCR5380_main()
1282
 *      as required.
1283
 *
1284
 * Inputs : int irq, irq that caused this interrupt.
1285
 *
1286
 */
1287
 
1288
static void NCR5380_intr (int irq, void *dev_id, struct pt_regs *regs)
1289
{
1290
    struct Scsi_Host *instance = first_instance;
1291
    int done = 1;
1292
    unsigned char basr;
1293
 
1294
    INT_PRINTK("scsi%d: NCR5380 irq triggered\n", HOSTNO);
1295
 
1296
    /* Look for pending interrupts */
1297
    basr = NCR5380_read(BUS_AND_STATUS_REG);
1298
    INT_PRINTK("scsi%d: BASR=%02x\n", HOSTNO, basr);
1299
    /* dispatch to appropriate routine if found and done=0 */
1300
    if (basr & BASR_IRQ) {
1301
        NCR_PRINT(NDEBUG_INTR);
1302
        if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1303
            done = 0;
1304
            ENABLE_IRQ();
1305
            INT_PRINTK("scsi%d: SEL interrupt\n", HOSTNO);
1306
            NCR5380_reselect(instance);
1307
            (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1308
        }
1309
        else if (basr & BASR_PARITY_ERROR) {
1310
            INT_PRINTK("scsi%d: PARITY interrupt\n", HOSTNO);
1311
            (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1312
        }
1313
        else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1314
            INT_PRINTK("scsi%d: RESET interrupt\n", HOSTNO);
1315
            (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1316
        }
1317
        else {
1318
            /*
1319
             * The rest of the interrupt conditions can occur only during a
1320
             * DMA transfer
1321
             */
1322
 
1323
#if defined(REAL_DMA)
1324
            /*
1325
             * We should only get PHASE MISMATCH and EOP interrupts if we have
1326
             * DMA enabled, so do a sanity check based on the current setting
1327
             * of the MODE register.
1328
             */
1329
 
1330
            if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1331
                ((basr & BASR_END_DMA_TRANSFER) ||
1332
                 !(basr & BASR_PHASE_MATCH))) {
1333
 
1334
                INT_PRINTK("scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
1335
                NCR5380_dma_complete( instance );
1336
                done = 0;
1337
                ENABLE_IRQ();
1338
            } else
1339
#endif /* REAL_DMA */
1340
            {
1341
/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1342
                if (basr & BASR_PHASE_MATCH)
1343
                    printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1344
                           "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1345
                           HOSTNO, basr, NCR5380_read(MODE_REG),
1346
                           NCR5380_read(STATUS_REG));
1347
                (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1348
            }
1349
        } /* if !(SELECTION || PARITY) */
1350
    } /* BASR & IRQ */
1351
    else {
1352
        printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1353
               "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1354
               NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1355
        (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1356
    }
1357
 
1358
    if (!done) {
1359
        INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO);
1360
        /* Put a call to NCR5380_main() on the queue... */
1361
        queue_main();
1362
    }
1363
}
1364
 
1365
#ifdef NCR5380_STATS
1366
static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
1367
{
1368
# ifdef NCR5380_STAT_LIMIT
1369
    if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1370
# endif
1371
        switch (cmd->cmnd[0])
1372
        {
1373
            case WRITE:
1374
            case WRITE_6:
1375
            case WRITE_10:
1376
                hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1377
                /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen;*/
1378
                hostdata->pendingw--;
1379
                break;
1380
            case READ:
1381
            case READ_6:
1382
            case READ_10:
1383
                hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1384
                /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen;*/
1385
                hostdata->pendingr--;
1386
                break;
1387
        }
1388
}
1389
#endif
1390
 
1391
/*
1392
 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1393
 *      int tag);
1394
 *
1395
 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1396
 *      including ARBITRATION, SELECTION, and initial message out for
1397
 *      IDENTIFY and queue messages.
1398
 *
1399
 * Inputs : instance - instantiation of the 5380 driver on which this
1400
 *      target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1401
 *      new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1402
 *      the command that is presently connected.
1403
 *
1404
 * Returns : -1 if selection could not execute for some reason,
1405
 *      0 if selection succeeded or failed because the target
1406
 *      did not respond.
1407
 *
1408
 * Side effects :
1409
 *      If bus busy, arbitration failed, etc, NCR5380_select() will exit
1410
 *              with registers as they should have been on entry - ie
1411
 *              SELECT_ENABLE will be set appropriately, the NCR5380
1412
 *              will cease to drive any SCSI bus signals.
1413
 *
1414
 *      If successful : I_T_L or I_T_L_Q nexus will be established,
1415
 *              instance->connected will be set to cmd.
1416
 *              SELECT interrupt will be disabled.
1417
 *
1418
 *      If failed (no target) : cmd->scsi_done() will be called, and the
1419
 *              cmd->result host byte set to DID_BAD_TARGET.
1420
 */
1421
 
1422
static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
1423
{
1424
    SETUP_HOSTDATA(instance);
1425
    unsigned char tmp[3], phase;
1426
    unsigned char *data;
1427
    int len;
1428
    unsigned long timeout;
1429
    unsigned long flags;
1430
 
1431
    hostdata->restart_select = 0;
1432
    NCR_PRINT(NDEBUG_ARBITRATION);
1433
    ARB_PRINTK("scsi%d: starting arbitration, id = %d\n", HOSTNO,
1434
               instance->this_id);
1435
 
1436
    /*
1437
     * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1438
     * data bus during SELECTION.
1439
     */
1440
 
1441
    save_flags(flags);
1442
    cli();
1443
    if (hostdata->connected) {
1444
        restore_flags(flags);
1445
        return -1;
1446
    }
1447
    NCR5380_write(TARGET_COMMAND_REG, 0);
1448
 
1449
 
1450
    /*
1451
     * Start arbitration.
1452
     */
1453
 
1454
    NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1455
    NCR5380_write(MODE_REG, MR_ARBITRATE);
1456
 
1457
    restore_flags(flags);
1458
 
1459
    /* Wait for arbitration logic to complete */
1460
#if NCR_TIMEOUT
1461
    {
1462
      unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1463
 
1464
      while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1465
           && jiffies < timeout && !hostdata->connected)
1466
        ;
1467
      if (jiffies >= timeout)
1468
      {
1469
        printk("scsi : arbitration timeout at %d\n", __LINE__);
1470
        NCR5380_write(MODE_REG, MR_BASE);
1471
        NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1472
        return -1;
1473
      }
1474
    }
1475
#else /* NCR_TIMEOUT */
1476
    while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1477
         && !hostdata->connected);
1478
#endif
1479
 
1480
    ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO);
1481
 
1482
    if (hostdata->connected) {
1483
        NCR5380_write(MODE_REG, MR_BASE);
1484
        return -1;
1485
    }
1486
    /*
1487
     * The arbitration delay is 2.2us, but this is a minimum and there is
1488
     * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1489
     * the integral nature of udelay().
1490
     *
1491
     */
1492
 
1493
    udelay(3);
1494
 
1495
    /* Check for lost arbitration */
1496
    if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1497
        (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1498
        (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1499
        hostdata->connected) {
1500
        NCR5380_write(MODE_REG, MR_BASE);
1501
        ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1502
                   HOSTNO);
1503
        return -1;
1504
    }
1505
 
1506
     /* after/during arbitration, BSY should be asserted.
1507
        IBM DPES-31080 Version S31Q works now */
1508
     /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1509
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL |
1510
                                         ICR_ASSERT_BSY ) ;
1511
 
1512
    if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1513
        hostdata->connected) {
1514
        NCR5380_write(MODE_REG, MR_BASE);
1515
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1516
        ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1517
                   HOSTNO);
1518
        return -1;
1519
    }
1520
 
1521
    /*
1522
     * Again, bus clear + bus settle time is 1.2us, however, this is
1523
     * a minimum so we'll udelay ceil(1.2)
1524
     */
1525
 
1526
#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
1527
    /* ++roman: But some targets (see above :-) seem to need a bit more... */
1528
    udelay(15);
1529
#else
1530
    udelay(2);
1531
#endif
1532
 
1533
    if (hostdata->connected) {
1534
        NCR5380_write(MODE_REG, MR_BASE);
1535
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1536
        return -1;
1537
    }
1538
 
1539
    ARB_PRINTK("scsi%d: won arbitration\n", HOSTNO);
1540
 
1541
    /*
1542
     * Now that we have won arbitration, start Selection process, asserting
1543
     * the host and target ID's on the SCSI bus.
1544
     */
1545
 
1546
    NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1547
 
1548
    /*
1549
     * Raise ATN while SEL is true before BSY goes false from arbitration,
1550
     * since this is the only way to guarantee that we'll get a MESSAGE OUT
1551
     * phase immediately after selection.
1552
     */
1553
 
1554
    NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1555
        ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1556
    NCR5380_write(MODE_REG, MR_BASE);
1557
 
1558
    /*
1559
     * Reselect interrupts must be turned off prior to the dropping of BSY,
1560
     * otherwise we will trigger an interrupt.
1561
     */
1562
 
1563
    if (hostdata->connected) {
1564
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1565
        return -1;
1566
    }
1567
 
1568
    NCR5380_write(SELECT_ENABLE_REG, 0);
1569
 
1570
    /*
1571
     * The initiator shall then wait at least two deskew delays and release
1572
     * the BSY signal.
1573
     */
1574
    udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */
1575
 
1576
    /* Reset BSY */
1577
    NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1578
        ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1579
 
1580
    /*
1581
     * Something weird happens when we cease to drive BSY - looks
1582
     * like the board/chip is letting us do another read before the
1583
     * appropriate propagation delay has expired, and we're confusing
1584
     * a BSY signal from ourselves as the target's response to SELECTION.
1585
     *
1586
     * A small delay (the 'C++' frontend breaks the pipeline with an
1587
     * unnecessary jump, making it work on my 386-33/Trantor T128, the
1588
     * tighter 'C' code breaks and requires this) solves the problem -
1589
     * the 1 us delay is arbitrary, and only used because this delay will
1590
     * be the same on other platforms and since it works here, it should
1591
     * work there.
1592
     *
1593
     * wingel suggests that this could be due to failing to wait
1594
     * one deskew delay.
1595
     */
1596
 
1597
    udelay(1);
1598
 
1599
    SEL_PRINTK("scsi%d: selecting target %d\n", HOSTNO, cmd->target);
1600
 
1601
    /*
1602
     * The SCSI specification calls for a 250 ms timeout for the actual
1603
     * selection.
1604
     */
1605
 
1606
    timeout = jiffies + 25;
1607
 
1608
    /*
1609
     * XXX very interesting - we're seeing a bounce where the BSY we
1610
     * asserted is being reflected / still asserted (propagation delay?)
1611
     * and it's detecting as true.  Sigh.
1612
     */
1613
 
1614
#if 0
1615
    /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1616
     * IO while SEL is true. But again, there are some disks out the in the
1617
     * world that do that nevertheless. (Somebody claimed that this announces
1618
     * reselection capability of the target.) So we better skip that test and
1619
     * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1620
     */
1621
 
1622
    while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) &
1623
        (SR_BSY | SR_IO)));
1624
 
1625
    if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1626
            (SR_SEL | SR_IO)) {
1627
            NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1628
            NCR5380_reselect(instance);
1629
            printk (KERN_ERR "scsi%d: reselection after won arbitration?\n",
1630
                    HOSTNO);
1631
            NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1632
            return -1;
1633
    }
1634
#else
1635
    while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
1636
#endif
1637
 
1638
    /*
1639
     * No less than two deskew delays after the initiator detects the
1640
     * BSY signal is true, it shall release the SEL signal and may
1641
     * change the DATA BUS.                                     -wingel
1642
     */
1643
 
1644
    udelay(1);
1645
 
1646
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1647
 
1648
    if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1649
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1650
        if (hostdata->targets_present & (1 << cmd->target)) {
1651
            printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1652
            if (hostdata->restart_select)
1653
                printk(KERN_NOTICE "\trestart select\n");
1654
            NCR_PRINT(NDEBUG_ANY);
1655
            NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1656
            return -1;
1657
        }
1658
        cmd->result = DID_BAD_TARGET << 16;
1659
#ifdef NCR5380_STATS
1660
        collect_stats(hostdata, cmd);
1661
#endif
1662
#ifdef SUPPORT_TAGS
1663
        cmd_free_tag( cmd );
1664
#endif
1665
        cmd->scsi_done(cmd);
1666
        NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1667
        SEL_PRINTK("scsi%d: target did not respond within 250ms\n", HOSTNO);
1668
        NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1669
        return 0;
1670
    }
1671
 
1672
    hostdata->targets_present |= (1 << cmd->target);
1673
 
1674
    /*
1675
     * Since we followed the SCSI spec, and raised ATN while SEL
1676
     * was true but before BSY was false during selection, the information
1677
     * transfer phase should be a MESSAGE OUT phase so that we can send the
1678
     * IDENTIFY message.
1679
     *
1680
     * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1681
     * message (2 bytes) with a tag ID that we increment with every command
1682
     * until it wraps back to 0.
1683
     *
1684
     * XXX - it turns out that there are some broken SCSI-II devices,
1685
     *       which claim to support tagged queuing but fail when more than
1686
     *       some number of commands are issued at once.
1687
     */
1688
 
1689
    /* Wait for start of REQ/ACK handshake */
1690
    while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1691
 
1692
    SEL_PRINTK("scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
1693
               HOSTNO, cmd->target);
1694
    tmp[0] = IDENTIFY(1, cmd->lun);
1695
 
1696
#ifdef SUPPORT_TAGS
1697
    if (cmd->tag != TAG_NONE) {
1698
        tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1699
        tmp[2] = cmd->tag;
1700
        len = 3;
1701
    } else
1702
        len = 1;
1703
#else
1704
    len = 1;
1705
    cmd->tag=0;
1706
#endif /* SUPPORT_TAGS */
1707
 
1708
    /* Send message(s) */
1709
    data = tmp;
1710
    phase = PHASE_MSGOUT;
1711
    NCR5380_transfer_pio(instance, &phase, &len, &data);
1712
    SEL_PRINTK("scsi%d: nexus established.\n", HOSTNO);
1713
    /* XXX need to handle errors here */
1714
    hostdata->connected = cmd;
1715
#ifndef SUPPORT_TAGS
1716
    hostdata->busy[cmd->target] |= (1 << cmd->lun);
1717
#endif    
1718
 
1719
    initialize_SCp(cmd);
1720
 
1721
 
1722
    return 0;
1723
}
1724
 
1725
/*
1726
 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1727
 *      unsigned char *phase, int *count, unsigned char **data)
1728
 *
1729
 * Purpose : transfers data in given phase using polled I/O
1730
 *
1731
 * Inputs : instance - instance of driver, *phase - pointer to
1732
 *      what phase is expected, *count - pointer to number of
1733
 *      bytes to transfer, **data - pointer to data pointer.
1734
 *
1735
 * Returns : -1 when different phase is entered without transferring
1736
 *      maximum number of bytes, 0 if all bytes are transfered or exit
1737
 *      is in same phase.
1738
 *
1739
 *      Also, *phase, *count, *data are modified in place.
1740
 *
1741
 * XXX Note : handling for bus free may be useful.
1742
 */
1743
 
1744
/*
1745
 * Note : this code is not as quick as it could be, however it
1746
 * IS 100% reliable, and for the actual data transfer where speed
1747
 * counts, we will always do a pseudo DMA or DMA transfer.
1748
 */
1749
 
1750
static int NCR5380_transfer_pio( struct Scsi_Host *instance,
1751
                                 unsigned char *phase, int *count,
1752
                                 unsigned char **data)
1753
{
1754
    register unsigned char p = *phase, tmp;
1755
    register int c = *count;
1756
    register unsigned char *d = *data;
1757
 
1758
    /*
1759
     * The NCR5380 chip will only drive the SCSI bus when the
1760
     * phase specified in the appropriate bits of the TARGET COMMAND
1761
     * REGISTER match the STATUS REGISTER
1762
     */
1763
 
1764
    NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1765
 
1766
    do {
1767
        /*
1768
         * Wait for assertion of REQ, after which the phase bits will be
1769
         * valid
1770
         */
1771
        while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1772
 
1773
        HSH_PRINTK("scsi%d: REQ detected\n", HOSTNO);
1774
 
1775
        /* Check for phase mismatch */
1776
        if ((tmp & PHASE_MASK) != p) {
1777
            PIO_PRINTK("scsi%d: phase mismatch\n", HOSTNO);
1778
            NCR_PRINT_PHASE(NDEBUG_PIO);
1779
            break;
1780
        }
1781
 
1782
        /* Do actual transfer from SCSI bus to / from memory */
1783
        if (!(p & SR_IO))
1784
            NCR5380_write(OUTPUT_DATA_REG, *d);
1785
        else
1786
            *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1787
 
1788
        ++d;
1789
 
1790
        /*
1791
         * The SCSI standard suggests that in MSGOUT phase, the initiator
1792
         * should drop ATN on the last byte of the message phase
1793
         * after REQ has been asserted for the handshake but before
1794
         * the initiator raises ACK.
1795
         */
1796
 
1797
        if (!(p & SR_IO)) {
1798
            if (!((p & SR_MSG) && c > 1)) {
1799
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1800
                    ICR_ASSERT_DATA);
1801
                NCR_PRINT(NDEBUG_PIO);
1802
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1803
                        ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1804
            } else {
1805
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1806
                    ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1807
                NCR_PRINT(NDEBUG_PIO);
1808
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1809
                    ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1810
            }
1811
        } else {
1812
            NCR_PRINT(NDEBUG_PIO);
1813
            NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1814
        }
1815
 
1816
        while (NCR5380_read(STATUS_REG) & SR_REQ);
1817
 
1818
        HSH_PRINTK("scsi%d: req false, handshake complete\n", HOSTNO);
1819
 
1820
/*
1821
 * We have several special cases to consider during REQ/ACK handshaking :
1822
 * 1.  We were in MSGOUT phase, and we are on the last byte of the
1823
 *      message.  ATN must be dropped as ACK is dropped.
1824
 *
1825
 * 2.  We are in a MSGIN phase, and we are on the last byte of the
1826
 *      message.  We must exit with ACK asserted, so that the calling
1827
 *      code may raise ATN before dropping ACK to reject the message.
1828
 *
1829
 * 3.  ACK and ATN are clear and the target may proceed as normal.
1830
 */
1831
        if (!(p == PHASE_MSGIN && c == 1)) {
1832
            if (p == PHASE_MSGOUT && c > 1)
1833
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1834
            else
1835
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1836
        }
1837
    } while (--c);
1838
 
1839
    PIO_PRINTK("scsi%d: residual %d\n", HOSTNO, c);
1840
 
1841
    *count = c;
1842
    *data = d;
1843
    tmp = NCR5380_read(STATUS_REG);
1844
    /* The phase read from the bus is valid if either REQ is (already)
1845
     * asserted or if ACK hasn't been released yet. The latter is the case if
1846
     * we're in MSGIN and all wanted bytes have been received. */
1847
    if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1848
        *phase = tmp & PHASE_MASK;
1849
    else
1850
        *phase = PHASE_UNKNOWN;
1851
 
1852
    if (!c || (*phase == p))
1853
        return 0;
1854
    else
1855
        return -1;
1856
}
1857
 
1858
/*
1859
 * Function : do_abort (Scsi_Host *host)
1860
 *
1861
 * Purpose : abort the currently established nexus.  Should only be
1862
 *      called from a routine which can drop into a
1863
 *
1864
 * Returns : 0 on success, -1 on failure.
1865
 */
1866
 
1867
static int do_abort (struct Scsi_Host *host)
1868
{
1869
    unsigned char tmp, *msgptr, phase;
1870
    int len;
1871
 
1872
    /* Request message out phase */
1873
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1874
 
1875
    /*
1876
     * Wait for the target to indicate a valid phase by asserting
1877
     * REQ.  Once this happens, we'll have either a MSGOUT phase
1878
     * and can immediately send the ABORT message, or we'll have some
1879
     * other phase and will have to source/sink data.
1880
     *
1881
     * We really don't care what value was on the bus or what value
1882
     * the target sees, so we just handshake.
1883
     */
1884
 
1885
    while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1886
 
1887
    NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1888
 
1889
    if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1890
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1891
                      ICR_ASSERT_ACK);
1892
        while (NCR5380_read(STATUS_REG) & SR_REQ);
1893
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1894
    }
1895
 
1896
    tmp = ABORT;
1897
    msgptr = &tmp;
1898
    len = 1;
1899
    phase = PHASE_MSGOUT;
1900
    NCR5380_transfer_pio (host, &phase, &len, &msgptr);
1901
 
1902
    /*
1903
     * If we got here, and the command completed successfully,
1904
     * we're about to go into bus free state.
1905
     */
1906
 
1907
    return len ? -1 : 0;
1908
}
1909
 
1910
#if defined(REAL_DMA)
1911
/*
1912
 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1913
 *      unsigned char *phase, int *count, unsigned char **data)
1914
 *
1915
 * Purpose : transfers data in given phase using either real
1916
 *      or pseudo DMA.
1917
 *
1918
 * Inputs : instance - instance of driver, *phase - pointer to
1919
 *      what phase is expected, *count - pointer to number of
1920
 *      bytes to transfer, **data - pointer to data pointer.
1921
 *
1922
 * Returns : -1 when different phase is entered without transferring
1923
 *      maximum number of bytes, 0 if all bytes or transfered or exit
1924
 *      is in same phase.
1925
 *
1926
 *      Also, *phase, *count, *data are modified in place.
1927
 *
1928
 */
1929
 
1930
 
1931
static int NCR5380_transfer_dma( struct Scsi_Host *instance,
1932
                                 unsigned char *phase, int *count,
1933
                                 unsigned char **data)
1934
{
1935
    SETUP_HOSTDATA(instance);
1936
    register int c = *count;
1937
    register unsigned char p = *phase;
1938
    register unsigned char *d = *data;
1939
    unsigned char tmp;
1940
    unsigned long flags;
1941
 
1942
    if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1943
        *phase = tmp;
1944
        return -1;
1945
    }
1946
 
1947
    if (atari_read_overruns && (p & SR_IO)) {
1948
        c -= atari_read_overruns;
1949
    }
1950
 
1951
    DMA_PRINTK("scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1952
               HOSTNO, (p & SR_IO) ? "reading" : "writing",
1953
               c, (p & SR_IO) ? "to" : "from", d);
1954
 
1955
    NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1956
 
1957
#ifdef REAL_DMA
1958
    NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1959
#endif /* def REAL_DMA  */
1960
 
1961
    if (IS_A_TT()) {
1962
        /* On the Medusa, it is a must to initialize the DMA before
1963
         * starting the NCR. This is also the cleaner way for the TT.
1964
         */
1965
        save_flags(flags);
1966
        cli();
1967
        hostdata->dma_len = (p & SR_IO) ?
1968
            NCR5380_dma_read_setup(instance, d, c) :
1969
            NCR5380_dma_write_setup(instance, d, c);
1970
        restore_flags(flags);
1971
    }
1972
 
1973
    if (p & SR_IO)
1974
        NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1975
    else {
1976
        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1977
        NCR5380_write(START_DMA_SEND_REG, 0);
1978
    }
1979
 
1980
    if (!IS_A_TT()) {
1981
        /* On the Falcon, the DMA setup must be done after the last */
1982
        /* NCR access, else the DMA setup gets trashed!
1983
         */
1984
        save_flags(flags);
1985
        cli();
1986
        hostdata->dma_len = (p & SR_IO) ?
1987
            NCR5380_dma_read_setup(instance, d, c) :
1988
            NCR5380_dma_write_setup(instance, d, c);
1989
        restore_flags(flags);
1990
    }
1991
    return 0;
1992
}
1993
#endif /* defined(REAL_DMA) */
1994
 
1995
/*
1996
 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1997
 *
1998
 * Purpose : run through the various SCSI phases and do as the target
1999
 *      directs us to.  Operates on the currently connected command,
2000
 *      instance->connected.
2001
 *
2002
 * Inputs : instance, instance for which we are doing commands
2003
 *
2004
 * Side effects : SCSI things happen, the disconnected queue will be
2005
 *      modified if a command disconnects, *instance->connected will
2006
 *      change.
2007
 *
2008
 * XXX Note : we need to watch for bus free or a reset condition here
2009
 *      to recover from an unexpected bus free condition.
2010
 */
2011
 
2012
static void NCR5380_information_transfer (struct Scsi_Host *instance)
2013
{
2014
    SETUP_HOSTDATA(instance);
2015
    unsigned long flags;
2016
    unsigned char msgout = NOP;
2017
    int sink = 0;
2018
    int len;
2019
#if defined(REAL_DMA)
2020
    int transfersize;
2021
#endif
2022
    unsigned char *data;
2023
    unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
2024
    Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2025
 
2026
    while (1) {
2027
        tmp = NCR5380_read(STATUS_REG);
2028
        /* We only have a valid SCSI phase when REQ is asserted */
2029
        if (tmp & SR_REQ) {
2030
            phase = (tmp & PHASE_MASK);
2031
            if (phase != old_phase) {
2032
                old_phase = phase;
2033
                NCR_PRINT_PHASE(NDEBUG_INFORMATION);
2034
            }
2035
 
2036
            if (sink && (phase != PHASE_MSGOUT)) {
2037
                NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2038
 
2039
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2040
                    ICR_ASSERT_ACK);
2041
                while (NCR5380_read(STATUS_REG) & SR_REQ);
2042
                NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2043
                    ICR_ASSERT_ATN);
2044
                sink = 0;
2045
                continue;
2046
            }
2047
 
2048
            switch (phase) {
2049
            case PHASE_DATAOUT:
2050
#if (NDEBUG & NDEBUG_NO_DATAOUT)
2051
                printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2052
                       "aborted\n", HOSTNO);
2053
                sink = 1;
2054
                do_abort(instance);
2055
                cmd->result = DID_ERROR  << 16;
2056
                cmd->done(cmd);
2057
                return;
2058
#endif
2059
            case PHASE_DATAIN:
2060
                /*
2061
                 * If there is no room left in the current buffer in the
2062
                 * scatter-gather list, move onto the next one.
2063
                 */
2064
 
2065
                if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2066
                    ++cmd->SCp.buffer;
2067
                    --cmd->SCp.buffers_residual;
2068
                    cmd->SCp.this_residual = cmd->SCp.buffer->length;
2069
                    cmd->SCp.ptr = cmd->SCp.buffer->address;
2070
                    /* ++roman: Try to merge some scatter-buffers if
2071
                     * they are at consecutive physical addresses.
2072
                     */
2073
                    merge_consecutive_buffers( cmd );
2074
                    INF_PRINTK("scsi%d: %d bytes and %d buffers left\n",
2075
                               HOSTNO, cmd->SCp.this_residual,
2076
                               cmd->SCp.buffers_residual);
2077
                }
2078
 
2079
                /*
2080
                 * The preferred transfer method is going to be
2081
                 * PSEUDO-DMA for systems that are strictly PIO,
2082
                 * since we can let the hardware do the handshaking.
2083
                 *
2084
                 * For this to work, we need to know the transfersize
2085
                 * ahead of time, since the pseudo-DMA code will sit
2086
                 * in an unconditional loop.
2087
                 */
2088
 
2089
/* ++roman: I suggest, this should be
2090
 *   #if def(REAL_DMA)
2091
 * instead of leaving REAL_DMA out.
2092
 */
2093
 
2094
#if defined(REAL_DMA)
2095
                if (!cmd->device->borken &&
2096
                    (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
2097
                    len = transfersize;
2098
                    cmd->SCp.phase = phase;
2099
                    if (NCR5380_transfer_dma(instance, &phase,
2100
                        &len, (unsigned char **) &cmd->SCp.ptr)) {
2101
                        /*
2102
                         * If the watchdog timer fires, all future
2103
                         * accesses to this device will use the
2104
                         * polled-IO. */
2105
                        printk(KERN_NOTICE "scsi%d: switching target %d "
2106
                               "lun %d to slow handshake\n", HOSTNO,
2107
                               cmd->target, cmd->lun);
2108
                        cmd->device->borken = 1;
2109
                        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2110
                            ICR_ASSERT_ATN);
2111
                        sink = 1;
2112
                        do_abort(instance);
2113
                        cmd->result = DID_ERROR  << 16;
2114
                        cmd->done(cmd);
2115
                        /* XXX - need to source or sink data here, as appropriate */
2116
                    } else {
2117
#ifdef REAL_DMA
2118
                        /* ++roman: When using real DMA,
2119
                         * information_transfer() should return after
2120
                         * starting DMA since it has nothing more to
2121
                         * do.
2122
                         */
2123
                        return;
2124
#else                   
2125
                        cmd->SCp.this_residual -= transfersize - len;
2126
#endif
2127
                    }
2128
                } else
2129
#endif /* defined(REAL_DMA) */
2130
                  NCR5380_transfer_pio(instance, &phase,
2131
                    (int *) &cmd->SCp.this_residual, (unsigned char **)
2132
                    &cmd->SCp.ptr);
2133
                break;
2134
            case PHASE_MSGIN:
2135
                len = 1;
2136
                data = &tmp;
2137
                NCR5380_write(SELECT_ENABLE_REG, 0);     /* disable reselects */
2138
                NCR5380_transfer_pio(instance, &phase, &len, &data);
2139
                cmd->SCp.Message = tmp;
2140
 
2141
                switch (tmp) {
2142
                /*
2143
                 * Linking lets us reduce the time required to get the
2144
                 * next command out to the device, hopefully this will
2145
                 * mean we don't waste another revolution due to the delays
2146
                 * required by ARBITRATION and another SELECTION.
2147
                 *
2148
                 * In the current implementation proposal, low level drivers
2149
                 * merely have to start the next command, pointed to by
2150
                 * next_link, done() is called as with unlinked commands.
2151
                 */
2152
#ifdef LINKED
2153
                case LINKED_CMD_COMPLETE:
2154
                case LINKED_FLG_CMD_COMPLETE:
2155
                    /* Accept message by clearing ACK */
2156
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2157
 
2158
                    LNK_PRINTK("scsi%d: target %d lun %d linked command "
2159
                               "complete.\n", HOSTNO, cmd->target, cmd->lun);
2160
 
2161
                    /* Enable reselect interrupts */
2162
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2163
                    /*
2164
                     * Sanity check : A linked command should only terminate
2165
                     * with one of these messages if there are more linked
2166
                     * commands available.
2167
                     */
2168
 
2169
                    if (!cmd->next_link) {
2170
                         printk(KERN_NOTICE "scsi%d: target %d lun %d "
2171
                                "linked command complete, no next_link\n",
2172
                                HOSTNO, cmd->target, cmd->lun);
2173
                            sink = 1;
2174
                            do_abort (instance);
2175
                            return;
2176
                    }
2177
 
2178
                    initialize_SCp(cmd->next_link);
2179
                    /* The next command is still part of this process; copy it
2180
                     * and don't free it! */
2181
                    cmd->next_link->tag = cmd->tag;
2182
                    cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2183
                    LNK_PRINTK("scsi%d: target %d lun %d linked request "
2184
                               "done, calling scsi_done().\n",
2185
                               HOSTNO, cmd->target, cmd->lun);
2186
#ifdef NCR5380_STATS
2187
                    collect_stats(hostdata, cmd);
2188
#endif
2189
                    cmd->scsi_done(cmd);
2190
                    cmd = hostdata->connected;
2191
                    break;
2192
#endif /* def LINKED */
2193
                case ABORT:
2194
                case COMMAND_COMPLETE:
2195
                    /* Accept message by clearing ACK */
2196
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2197
                    /* ++guenther: possible race with Falcon locking */
2198
                    falcon_dont_release++;
2199
                    hostdata->connected = NULL;
2200
                    QU_PRINTK("scsi%d: command for target %d, lun %d "
2201
                              "completed\n", HOSTNO, cmd->target, cmd->lun);
2202
#ifdef SUPPORT_TAGS
2203
                    cmd_free_tag( cmd );
2204
                    if (cmd->SCp.Status == QUEUE_FULL) {
2205
                        /* Turn a QUEUE FULL status into BUSY, I think the
2206
                         * mid level cannot handle QUEUE FULL :-( (The
2207
                         * command is retried after BUSY). Also update our
2208
                         * queue size to the number of currently issued
2209
                         * commands now.
2210
                         */
2211
                        TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
2212
                        TAG_PRINTK("scsi%d: target %d lun %d returned "
2213
                                   "QUEUE_FULL after %d commands\n",
2214
                                   HOSTNO, cmd->target, cmd->lun,
2215
                                   ta->nr_allocated);
2216
                        if (ta->queue_size > ta->nr_allocated)
2217
                            ta->nr_allocated = ta->queue_size;
2218
                        cmd->SCp.Status = BUSY;
2219
                    }
2220
#else
2221
                    hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2222
#endif
2223
                    /* Enable reselect interrupts */
2224
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2225
 
2226
                    /*
2227
                     * I'm not sure what the correct thing to do here is :
2228
                     *
2229
                     * If the command that just executed is NOT a request
2230
                     * sense, the obvious thing to do is to set the result
2231
                     * code to the values of the stored parameters.
2232
                     *
2233
                     * If it was a REQUEST SENSE command, we need some way to
2234
                     * differentiate between the failure code of the original
2235
                     * and the failure code of the REQUEST sense - the obvious
2236
                     * case is success, where we fall through and leave the
2237
                     * result code unchanged.
2238
                     *
2239
                     * The non-obvious place is where the REQUEST SENSE failed
2240
                     */
2241
 
2242
                    if (cmd->cmnd[0] != REQUEST_SENSE)
2243
                        cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2244
                    else if (cmd->SCp.Status != GOOD)
2245
                        cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2246
 
2247
#ifdef AUTOSENSE
2248
                    if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2249
                        (cmd->SCp.Status == CHECK_CONDITION)) {
2250
                        ASEN_PRINTK("scsi%d: performing request sense\n",
2251
                                    HOSTNO);
2252
                        cmd->cmnd[0] = REQUEST_SENSE;
2253
                        cmd->cmnd[1] &= 0xe0;
2254
                        cmd->cmnd[2] = 0;
2255
                        cmd->cmnd[3] = 0;
2256
                        cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2257
                        cmd->cmnd[5] = 0;
2258
 
2259
                        cmd->SCp.buffer = NULL;
2260
                        cmd->SCp.buffers_residual = 0;
2261
                        cmd->SCp.ptr = (char *) cmd->sense_buffer;
2262
                        cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2263
 
2264
                        save_flags(flags);
2265
                        cli();
2266
                        LIST(cmd,hostdata->issue_queue);
2267
                        NEXT(cmd) = hostdata->issue_queue;
2268
                        hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2269
                        restore_flags(flags);
2270
                        QU_PRINTK("scsi%d: REQUEST SENSE added to head of "
2271
                                  "issue queue\n", H_NO(cmd));
2272
                   } else
2273
#endif /* def AUTOSENSE */
2274
                   {
2275
#ifdef NCR5380_STATS
2276
                       collect_stats(hostdata, cmd);
2277
#endif
2278
                       cmd->scsi_done(cmd);
2279
                    }
2280
 
2281
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2282
                    /*
2283
                     * Restore phase bits to 0 so an interrupted selection,
2284
                     * arbitration can resume.
2285
                     */
2286
                    NCR5380_write(TARGET_COMMAND_REG, 0);
2287
 
2288
                    while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2289
                        barrier();
2290
 
2291
                    falcon_dont_release--;
2292
                    /* ++roman: For Falcon SCSI, release the lock on the
2293
                     * ST-DMA here if no other commands are waiting on the
2294
                     * disconnected queue.
2295
                     */
2296
                    falcon_release_lock_if_possible( hostdata );
2297
                    return;
2298
                case MESSAGE_REJECT:
2299
                    /* Accept message by clearing ACK */
2300
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2301
                    /* Enable reselect interrupts */
2302
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2303
                    switch (hostdata->last_message) {
2304
                    case HEAD_OF_QUEUE_TAG:
2305
                    case ORDERED_QUEUE_TAG:
2306
                    case SIMPLE_QUEUE_TAG:
2307
                        /* The target obviously doesn't support tagged
2308
                         * queuing, even though it announced this ability in
2309
                         * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2310
                         * clear 'tagged_supported' and lock the LUN, since
2311
                         * the command is treated as untagged further on.
2312
                         */
2313
                        cmd->device->tagged_supported = 0;
2314
                        hostdata->busy[cmd->target] |= (1 << cmd->lun);
2315
                        cmd->tag = TAG_NONE;
2316
                        TAG_PRINTK("scsi%d: target %d lun %d rejected "
2317
                                   "QUEUE_TAG message; tagged queuing "
2318
                                   "disabled\n",
2319
                                   HOSTNO, cmd->target, cmd->lun);
2320
                        break;
2321
                    }
2322
                    break;
2323
                case DISCONNECT:
2324
                    /* Accept message by clearing ACK */
2325
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2326
                    save_flags(flags);
2327
                    cli();
2328
                    cmd->device->disconnect = 1;
2329
                    LIST(cmd,hostdata->disconnected_queue);
2330
                    NEXT(cmd) = hostdata->disconnected_queue;
2331
                    hostdata->connected = NULL;
2332
                    hostdata->disconnected_queue = cmd;
2333
                    restore_flags(flags);
2334
                    QU_PRINTK("scsi%d: command for target %d lun %d was "
2335
                              "moved from connected to the "
2336
                              "disconnected_queue\n", HOSTNO,
2337
                              cmd->target, cmd->lun);
2338
                    /*
2339
                     * Restore phase bits to 0 so an interrupted selection,
2340
                     * arbitration can resume.
2341
                     */
2342
                    NCR5380_write(TARGET_COMMAND_REG, 0);
2343
 
2344
                    /* Enable reselect interrupts */
2345
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2346
                    /* Wait for bus free to avoid nasty timeouts */
2347
                    while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2348
                        barrier();
2349
                    return;
2350
                /*
2351
                 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2352
                 * operation, in violation of the SCSI spec so we can safely
2353
                 * ignore SAVE/RESTORE pointers calls.
2354
                 *
2355
                 * Unfortunately, some disks violate the SCSI spec and
2356
                 * don't issue the required SAVE_POINTERS message before
2357
                 * disconnecting, and we have to break spec to remain
2358
                 * compatible.
2359
                 */
2360
                case SAVE_POINTERS:
2361
                case RESTORE_POINTERS:
2362
                    /* Accept message by clearing ACK */
2363
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2364
                    /* Enable reselect interrupts */
2365
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2366
                    break;
2367
                case EXTENDED_MESSAGE:
2368
/*
2369
 * Extended messages are sent in the following format :
2370
 * Byte
2371
 * 0            EXTENDED_MESSAGE == 1
2372
 * 1            length (includes one byte for code, doesn't
2373
 *              include first two bytes)
2374
 * 2            code
2375
 * 3..length+1  arguments
2376
 *
2377
 * Start the extended message buffer with the EXTENDED_MESSAGE
2378
 * byte, since print_msg() wants the whole thing.
2379
 */
2380
                    extended_msg[0] = EXTENDED_MESSAGE;
2381
                    /* Accept first byte by clearing ACK */
2382
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2383
 
2384
                    EXT_PRINTK("scsi%d: receiving extended message\n", HOSTNO);
2385
 
2386
                    len = 2;
2387
                    data = extended_msg + 1;
2388
                    phase = PHASE_MSGIN;
2389
                    NCR5380_transfer_pio(instance, &phase, &len, &data);
2390
                    EXT_PRINTK("scsi%d: length=%d, code=0x%02x\n", HOSTNO,
2391
                               (int)extended_msg[1], (int)extended_msg[2]);
2392
 
2393
                    if (!len && extended_msg[1] <=
2394
                        (sizeof (extended_msg) - 1)) {
2395
                        /* Accept third byte by clearing ACK */
2396
                        NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2397
                        len = extended_msg[1] - 1;
2398
                        data = extended_msg + 3;
2399
                        phase = PHASE_MSGIN;
2400
 
2401
                        NCR5380_transfer_pio(instance, &phase, &len, &data);
2402
                        EXT_PRINTK("scsi%d: message received, residual %d\n",
2403
                                   HOSTNO, len);
2404
 
2405
                        switch (extended_msg[2]) {
2406
                        case EXTENDED_SDTR:
2407
                        case EXTENDED_WDTR:
2408
                        case EXTENDED_MODIFY_DATA_POINTER:
2409
                        case EXTENDED_EXTENDED_IDENTIFY:
2410
                            tmp = 0;
2411
                        }
2412
                    } else if (len) {
2413
                        printk(KERN_NOTICE "scsi%d: error receiving "
2414
                               "extended message\n", HOSTNO);
2415
                        tmp = 0;
2416
                    } else {
2417
                        printk(KERN_NOTICE "scsi%d: extended message "
2418
                               "code %02x length %d is too long\n",
2419
                               HOSTNO, extended_msg[2], extended_msg[1]);
2420
                        tmp = 0;
2421
                    }
2422
                /* Fall through to reject message */
2423
 
2424
                /*
2425
                 * If we get something weird that we aren't expecting,
2426
                 * reject it.
2427
                 */
2428
                default:
2429
                    if (!tmp) {
2430
                        printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2431
                        print_msg (extended_msg);
2432
                        printk("\n");
2433
                    } else if (tmp != EXTENDED_MESSAGE)
2434
                        printk(KERN_DEBUG "scsi%d: rejecting unknown "
2435
                               "message %02x from target %d, lun %d\n",
2436
                               HOSTNO, tmp, cmd->target, cmd->lun);
2437
                    else
2438
                        printk(KERN_DEBUG "scsi%d: rejecting unknown "
2439
                               "extended message "
2440
                               "code %02x, length %d from target %d, lun %d\n",
2441
                               HOSTNO, extended_msg[1], extended_msg[0],
2442
                               cmd->target, cmd->lun);
2443
 
2444
 
2445
                    msgout = MESSAGE_REJECT;
2446
                    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2447
                        ICR_ASSERT_ATN);
2448
                    break;
2449
                } /* switch (tmp) */
2450
                break;
2451
            case PHASE_MSGOUT:
2452
                len = 1;
2453
                data = &msgout;
2454
                hostdata->last_message = msgout;
2455
                NCR5380_transfer_pio(instance, &phase, &len, &data);
2456
                if (msgout == ABORT) {
2457
#ifdef SUPPORT_TAGS
2458
                    cmd_free_tag( cmd );
2459
#else
2460
                    hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2461
#endif
2462
                    hostdata->connected = NULL;
2463
                    cmd->result = DID_ERROR << 16;
2464
#ifdef NCR5380_STATS
2465
                    collect_stats(hostdata, cmd);
2466
#endif
2467
                    cmd->scsi_done(cmd);
2468
                    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2469
                    falcon_release_lock_if_possible( hostdata );
2470
                    return;
2471
                }
2472
                msgout = NOP;
2473
                break;
2474
            case PHASE_CMDOUT:
2475
                len = cmd->cmd_len;
2476
                data = cmd->cmnd;
2477
                /*
2478
                 * XXX for performance reasons, on machines with a
2479
                 * PSEUDO-DMA architecture we should probably
2480
                 * use the dma transfer function.
2481
                 */
2482
                NCR5380_transfer_pio(instance, &phase, &len,
2483
                    &data);
2484
                break;
2485
            case PHASE_STATIN:
2486
                len = 1;
2487
                data = &tmp;
2488
                NCR5380_transfer_pio(instance, &phase, &len, &data);
2489
                cmd->SCp.Status = tmp;
2490
                break;
2491
            default:
2492
                printk("scsi%d: unknown phase\n", HOSTNO);
2493
                NCR_PRINT(NDEBUG_ANY);
2494
            } /* switch(phase) */
2495
        } /* if (tmp * SR_REQ) */
2496
    } /* while (1) */
2497
}
2498
 
2499
/*
2500
 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2501
 *
2502
 * Purpose : does reselection, initializing the instance->connected
2503
 *      field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2504
 *      nexus has been reestablished,
2505
 *
2506
 * Inputs : instance - this instance of the NCR5380.
2507
 *
2508
 */
2509
 
2510
 
2511
static void NCR5380_reselect (struct Scsi_Host *instance)
2512
{
2513
    SETUP_HOSTDATA(instance);
2514
    unsigned char target_mask;
2515
    unsigned char lun, phase;
2516
    int len;
2517
#ifdef SUPPORT_TAGS
2518
    unsigned char tag;
2519
#endif
2520
    unsigned char msg[3];
2521
    unsigned char *data;
2522
    Scsi_Cmnd *tmp = NULL, *prev;
2523
/*    unsigned long flags; */
2524
 
2525
    /*
2526
     * Disable arbitration, etc. since the host adapter obviously
2527
     * lost, and tell an interrupted NCR5380_select() to restart.
2528
     */
2529
 
2530
    NCR5380_write(MODE_REG, MR_BASE);
2531
    hostdata->restart_select = 1;
2532
 
2533
    target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2534
 
2535
    RSL_PRINTK("scsi%d: reselect\n", HOSTNO);
2536
 
2537
    /*
2538
     * At this point, we have detected that our SCSI ID is on the bus,
2539
     * SEL is true and BSY was false for at least one bus settle delay
2540
     * (400 ns).
2541
     *
2542
     * We must assert BSY ourselves, until the target drops the SEL
2543
     * signal.
2544
     */
2545
 
2546
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2547
 
2548
    while (NCR5380_read(STATUS_REG) & SR_SEL);
2549
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2550
 
2551
    /*
2552
     * Wait for target to go into MSGIN.
2553
     */
2554
 
2555
    while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2556
 
2557
    len = 1;
2558
    data = msg;
2559
    phase = PHASE_MSGIN;
2560
    NCR5380_transfer_pio(instance, &phase, &len, &data);
2561
 
2562
    if (!msg[0] & 0x80) {
2563
        printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2564
        print_msg(msg);
2565
        do_abort(instance);
2566
        return;
2567
    }
2568
    lun = (msg[0] & 0x07);
2569
 
2570
#ifdef SUPPORT_TAGS
2571
    /* If the phase is still MSGIN, the target wants to send some more
2572
     * messages. In case it supports tagged queuing, this is probably a
2573
     * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2574
     */
2575
    tag = TAG_NONE;
2576
    if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2577
        /* Accept previous IDENTIFY message by clearing ACK */
2578
        NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
2579
        len = 2;
2580
        data = msg+1;
2581
        if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2582
            msg[1] == SIMPLE_QUEUE_TAG)
2583
            tag = msg[2];
2584
        TAG_PRINTK("scsi%d: target mask %02x, lun %d sent tag %d at "
2585
                   "reselection\n", HOSTNO, target_mask, lun, tag);
2586
    }
2587
#endif
2588
 
2589
    /*
2590
     * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we
2591
     * just reestablished, and remove it from the disconnected queue.
2592
     */
2593
 
2594
    for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2595
         tmp; prev = tmp, tmp = NEXT(tmp) ) {
2596
        if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2597
#ifdef SUPPORT_TAGS
2598
            && (tag == tmp->tag)
2599
#endif
2600
            ) {
2601
            /* ++guenther: prevent race with falcon_release_lock */
2602
            falcon_dont_release++;
2603
            if (prev) {
2604
                REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
2605
                NEXT(prev) = NEXT(tmp);
2606
            } else {
2607
                REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2608
                hostdata->disconnected_queue = NEXT(tmp);
2609
            }
2610
            NEXT(tmp) = NULL;
2611
            break;
2612
        }
2613
    }
2614
 
2615
    if (!tmp) {
2616
        printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2617
#ifdef SUPPORT_TAGS
2618
                "tag %d "
2619
#endif
2620
                "not in disconnect_queue.\n",
2621
                HOSTNO, target_mask, lun
2622
#ifdef SUPPORT_TAGS
2623
                , tag
2624
#endif
2625
                );
2626
        /*
2627
         * Since we have an established nexus that we can't do anything
2628
         * with, we must abort it.
2629
         */
2630
        do_abort(instance);
2631
        return;
2632
    }
2633
 
2634
    /* Accept message by clearing ACK */
2635
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2636
 
2637
    hostdata->connected = tmp;
2638
    RSL_PRINTK("scsi%d: nexus established, target = %d, lun = %d, tag = %d\n",
2639
               HOSTNO, tmp->target, tmp->lun, tmp->tag);
2640
    falcon_dont_release--;
2641
}
2642
 
2643
 
2644
/*
2645
 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2646
 *
2647
 * Purpose : abort a command
2648
 *
2649
 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2650
 *      host byte of the result field to, if zero DID_ABORTED is
2651
 *      used.
2652
 *
2653
 * Returns : 0 - success, -1 on failure.
2654
 *
2655
 * XXX - there is no way to abort the command that is currently
2656
 *       connected, you have to wait for it to complete.  If this is
2657
 *       a problem, we could implement longjmp() / setjmp(), setjmp()
2658
 *       called where the loop started in NCR5380_main().
2659
 */
2660
 
2661
#ifndef NCR5380_abort
2662
static
2663
#endif
2664
int NCR5380_abort (Scsi_Cmnd *cmd)
2665
{
2666
    struct Scsi_Host *instance = cmd->host;
2667
    SETUP_HOSTDATA(instance);
2668
    Scsi_Cmnd *tmp, **prev;
2669
    unsigned long flags;
2670
 
2671
    printk(KERN_NOTICE "scsi%d: aborting command\n", HOSTNO);
2672
    print_Scsi_Cmnd (cmd);
2673
 
2674
    NCR5380_print_status (instance);
2675
 
2676
    save_flags(flags);
2677
    cli();
2678
 
2679
    if (!IS_A_TT() && !falcon_got_lock)
2680
        printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_abort\n",
2681
               HOSTNO);
2682
 
2683
    ABRT_PRINTK("scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
2684
                NCR5380_read(BUS_AND_STATUS_REG),
2685
                NCR5380_read(STATUS_REG));
2686
 
2687
#if 1
2688
/*
2689
 * Case 1 : If the command is the currently executing command,
2690
 * we'll set the aborted flag and return control so that
2691
 * information transfer routine can exit cleanly.
2692
 */
2693
 
2694
    if (hostdata->connected == cmd) {
2695
        int rv;
2696
 
2697
        ABRT_PRINTK("scsi%d: aborting connected command\n", HOSTNO);
2698
        hostdata->aborted = 1;
2699
/*
2700
 * We should perform BSY checking, and make sure we haven't slipped
2701
 * into BUS FREE.
2702
 */
2703
 
2704
/*      NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2705
/*
2706
 * Since we can't change phases until we've completed the current
2707
 * handshake, we have to source or sink a byte of data if the current
2708
 * phase is not MSGOUT.
2709
 */
2710
 
2711
/*
2712
 * MSch: use the do_abort function instead ... unless there is need to wait
2713
 * a longer period for the target to go into MSGOUT.
2714
 */
2715
        rv = do_abort(instance);
2716
 
2717
        save_flags(flags);
2718
        cli();
2719
 
2720
        cmd->result = DID_ABORT << 16;
2721
#ifdef SUPPORT_TAGS
2722
        cmd_free_tag( cmd );
2723
#else
2724
        hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2725
#endif
2726
        restore_flags(flags);
2727
        cmd->done(cmd);
2728
        falcon_release_lock_if_possible( hostdata );
2729
 
2730
/*
2731
 * Return control to the executing NCR drive so we can clear the
2732
 * aborted flag and get back into our main loop.
2733
 */
2734
 
2735
        return rv ? SCSI_ABORT_SUCCESS : SCSI_ABORT_ERROR;
2736
    }
2737
#endif
2738
 
2739
/*
2740
 * Case 2 : If the command hasn't been issued yet, we simply remove it
2741
 *          from the issue queue.
2742
 */
2743
    for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue),
2744
        tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2745
        tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
2746
        if (cmd == tmp) {
2747
            REMOVE(5, *prev, tmp, NEXT(tmp));
2748
            (*prev) = NEXT(tmp);
2749
            NEXT(tmp) = NULL;
2750
            tmp->result = DID_ABORT << 16;
2751
            restore_flags(flags);
2752
            ABRT_PRINTK("scsi%d: abort removed command from issue queue.\n",
2753
                        HOSTNO);
2754
            /* Tagged queuing note: no tag to free here, hasn't been assigned
2755
             * yet... */
2756
            tmp->done(tmp);
2757
            falcon_release_lock_if_possible( hostdata );
2758
            return SCSI_ABORT_SUCCESS;
2759
        }
2760
 
2761
/*
2762
 * Case 3 : If any commands are connected, we're going to fail the abort
2763
 *          and let the high level SCSI driver retry at a later time or
2764
 *          issue a reset.
2765
 *
2766
 *          Timeouts, and therefore aborted commands, will be highly unlikely
2767
 *          and handling them cleanly in this situation would make the common
2768
 *          case of noresets less efficient, and would pollute our code.  So,
2769
 *          we fail.
2770
 */
2771
 
2772
    if (hostdata->connected) {
2773
        restore_flags(flags);
2774
        ABRT_PRINTK("scsi%d: abort failed, command connected.\n", HOSTNO);
2775
        return SCSI_ABORT_SNOOZE;
2776
    }
2777
 
2778
/*
2779
 * Case 4: If the command is currently disconnected from the bus, and
2780
 *      there are no connected commands, we reconnect the I_T_L or
2781
 *      I_T_L_Q nexus associated with it, go into message out, and send
2782
 *      an abort message.
2783
 *
2784
 * This case is especially ugly. In order to reestablish the nexus, we
2785
 * need to call NCR5380_select().  The easiest way to implement this
2786
 * function was to abort if the bus was busy, and let the interrupt
2787
 * handler triggered on the SEL for reselect take care of lost arbitrations
2788
 * where necessary, meaning interrupts need to be enabled.
2789
 *
2790
 * When interrupts are enabled, the queues may change - so we
2791
 * can't remove it from the disconnected queue before selecting it
2792
 * because that could cause a failure in hashing the nexus if that
2793
 * device reselected.
2794
 *
2795
 * Since the queues may change, we can't use the pointers from when we
2796
 * first locate it.
2797
 *
2798
 * So, we must first locate the command, and if NCR5380_select()
2799
 * succeeds, then issue the abort, relocate the command and remove
2800
 * it from the disconnected queue.
2801
 */
2802
 
2803
    for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2804
         tmp = NEXT(tmp))
2805
        if (cmd == tmp) {
2806
            restore_flags(flags);
2807
            ABRT_PRINTK("scsi%d: aborting disconnected command.\n", HOSTNO);
2808
 
2809
            if (NCR5380_select (instance, cmd, (int) cmd->tag))
2810
                return SCSI_ABORT_BUSY;
2811
 
2812
            ABRT_PRINTK("scsi%d: nexus reestablished.\n", HOSTNO);
2813
 
2814
            do_abort (instance);
2815
 
2816
            save_flags(flags);
2817
            cli();
2818
            for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue),
2819
                tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
2820
                tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
2821
                    if (cmd == tmp) {
2822
                    REMOVE(5, *prev, tmp, NEXT(tmp));
2823
                    *prev = NEXT(tmp);
2824
                    NEXT(tmp) = NULL;
2825
                    tmp->result = DID_ABORT << 16;
2826
                    /* We must unlock the tag/LUN immediately here, since the
2827
                     * target goes to BUS FREE and doesn't send us another
2828
                     * message (COMMAND_COMPLETE or the like)
2829
                     */
2830
#ifdef SUPPORT_TAGS
2831
                    cmd_free_tag( tmp );
2832
#else
2833
                    hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2834
#endif
2835
                    restore_flags(flags);
2836
                    tmp->done(tmp);
2837
                    falcon_release_lock_if_possible( hostdata );
2838
                    return SCSI_ABORT_SUCCESS;
2839
                }
2840
        }
2841
 
2842
/*
2843
 * Case 5 : If we reached this point, the command was not found in any of
2844
 *          the queues.
2845
 *
2846
 * We probably reached this point because of an unlikely race condition
2847
 * between the command completing successfully and the abortion code,
2848
 * so we won't panic, but we will notify the user in case something really
2849
 * broke.
2850
 */
2851
 
2852
    restore_flags(flags);
2853
    printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully\n"
2854
           KERN_INFO "        before abortion\n", HOSTNO);
2855
 
2856
/* Maybe it is sufficient just to release the ST-DMA lock... (if
2857
 * possible at all) At least, we should check if the lock could be
2858
 * released after the abort, in case it is kept due to some bug.
2859
 */
2860
    falcon_release_lock_if_possible( hostdata );
2861
 
2862
    return SCSI_ABORT_NOT_RUNNING;
2863
}
2864
 
2865
 
2866
/*
2867
 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
2868
 *
2869
 * Purpose : reset the SCSI bus.
2870
 *
2871
 * Returns : SCSI_RESET_WAKEUP
2872
 *
2873
 */
2874
 
2875
static int NCR5380_reset( Scsi_Cmnd *cmd, unsigned int reset_flags)
2876
{
2877
    SETUP_HOSTDATA(cmd->host);
2878
    int           i;
2879
    unsigned long flags;
2880
    Scsi_Cmnd *connected, *disconnected_queue;
2881
 
2882
    if (!IS_A_TT() && !falcon_got_lock)
2883
        printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_reset\n",
2884
               H_NO(cmd) );
2885
 
2886
    NCR5380_print_status (cmd->host);
2887
 
2888
    /* get in phase */
2889
    NCR5380_write( TARGET_COMMAND_REG,
2890
                   PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
2891
    /* assert RST */
2892
    NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
2893
    udelay (40);
2894
    /* reset NCR registers */
2895
    NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
2896
    NCR5380_write( MODE_REG, MR_BASE );
2897
    NCR5380_write( TARGET_COMMAND_REG, 0 );
2898
    NCR5380_write( SELECT_ENABLE_REG, 0 );
2899
    /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2900
     * through anymore ... */
2901
    (void)NCR5380_read( RESET_PARITY_INTERRUPT_REG );
2902
 
2903
#if 1 /* XXX Should now be done by midlevel code, bug isn't XXX */
2904
 
2905
    /* After the reset, there are no more connected or disconnected commands
2906
     * and no busy units; to avoid problems with re-inserting the commands
2907
     * into the issue_queue (via scsi_done()), the aborted commands are
2908
     * remembered in local variables first.
2909
     */
2910
    save_flags(flags);
2911
    cli();
2912
    connected = (Scsi_Cmnd *)hostdata->connected;
2913
    hostdata->connected = NULL;
2914
    disconnected_queue = (Scsi_Cmnd *)hostdata->disconnected_queue;
2915
    hostdata->disconnected_queue = NULL;
2916
#ifdef SUPPORT_TAGS
2917
    free_all_tags();
2918
#endif
2919
    for( i = 0; i < 8; ++i )
2920
        hostdata->busy[i] = 0;
2921
#ifdef REAL_DMA
2922
    hostdata->dma_len = 0;
2923
#endif
2924
    restore_flags(flags);
2925
 
2926
    if ((cmd = connected)) {
2927
        ABRT_PRINTK("scsi%d: reset aborted a connected command\n", H_NO(cmd));
2928
        cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2929
        cmd->scsi_done( cmd );
2930
    }
2931
 
2932
    for (i = 0; (cmd = disconnected_queue); ++i) {
2933
        disconnected_queue = NEXT(cmd);
2934
        NEXT(cmd) = NULL;
2935
        cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2936
        cmd->scsi_done( cmd );
2937
    }
2938
    if (i > 0)
2939
        ABRT_PRINTK("scsi: reset aborted %d disconnected command(s)\n", i);
2940
 
2941
/* The Falcon lock should be released after a reset...
2942
 */
2943
/* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2944
 * unlocking and enabling dma interrupt.
2945
 */
2946
/*    falcon_release_lock_if_possible( hostdata );*/
2947
 
2948
#endif /* 1 */
2949
 
2950
    return SCSI_RESET_WAKEUP;
2951
}
2952
 
2953
/* Local Variables: */
2954
/* tab-width: 8     */
2955
/* End:             */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.