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

Subversion Repositories or1k

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

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

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

powered by: WebSVN 2.1.0

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