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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [scsi/] [NCR5380.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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