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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [scsi/] [mac_NCR5380.c] - Blame information for rev 1780

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

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

powered by: WebSVN 2.1.0

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