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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [scsi/] [acornscsi.c.2] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1622 jcastillo
/*
2
 * linux/arch/arm/drivers/scsi/acornscsi.c
3
 *
4
 *  Acorn SCSI 3 driver
5
 *  By R.M.King.
6
 *
7
 * Abandoned using the Select and Transfer command since there were
8
 * some nasty races between our software and the target devices that
9
 * were not easy to solve, and the device errata had a lot of entries
10
 * for this command, some of them quite nasty...
11
 *
12
 * Changelog:
13
 *  26-Sep-1997 RMK     Re-jigged to use the queue module.
14
 *                      Re-coded state machine to be based on driver
15
 *                      state not scsi state.  Should be easier to debug.
16
 *                      Added acornscsi_release to clean up properly.
17
 *                      Updated proc/scsi reporting.
18
 *  05-Oct-1997 RMK     Implemented writing to SCSI devices.
19
 *  06-Oct-1997 RMK     Corrected small (non-serious) bug with the connect/
20
 *                      reconnect race condition causing a warning message.
21
 */
22
#define DEBUG_NO_WRITE  1
23
#define DEBUG_QUEUES    2
24
#define DEBUG_DMA       4
25
#define DEBUG_ABORT     8
26
#define DEBUG_DISCON    16
27
#define DEBUG_CONNECT   32
28
#define DEBUG_PHASES    64
29
#define DEBUG_WRITE     128
30
#define DEBUG_LINK      256
31
#define DEBUG_MESSAGES  512
32
#define DEBUG_RESET     1024
33
#define DEBUG_ALL       (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
34
                         DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
35
                         DEBUG_DMA|DEBUG_QUEUES|DEBUG_NO_WRITE)
36
 
37
/* DRIVER CONFIGURATION
38
 *
39
 * SCSI-II Tagged queue support.
40
 *
41
 * I don't have any SCSI devices that support it, so it is totally untested
42
 * (except to make sure that it doesn't interfere with any non-tagging
43
 * devices).  It is not fully implemented either - what happens when a
44
 * tagging device reconnects???
45
 *
46
 * You can tell if you have a device that supports tagged queueing my
47
 * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
48
 * as '2 TAG'.
49
 */
50
#define SCSI2_TAG
51
/*
52
 * SCSI-II Linked command support.
53
 *
54
 * The higher level code doesn't support linked commands yet.
55
 */
56
#undef SCSI2_LINK
57
/*
58
 * SCSI-II Synchronous transfer support.
59
 *
60
 * Tried and tested...
61
 *
62
 * SDTR_SIZE      - maximum number of un-acknowledged bytes (0 = off, 12 = max)
63
 * SDTR_PERIOD    - period of REQ signal (min=125, max=1020)
64
 * DEFAULT_PERIOD - default REQ period.
65
 */
66
#define SCSI2_SYNC
67
#define SDTR_SIZE       12
68
#define SDTR_PERIOD     125
69
#define DEFAULT_PERIOD  500
70
 
71
/*
72
 * Debugging information
73
 *
74
 * DEBUG          - bit mask from list above
75
 * DEBUG_TARGET   - is defined to the target number if you want to debug
76
 *                  a specific target. [only recon/write/dma].
77
 */
78
#define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
79
/* only allow writing to SCSI device 0 */
80
#define NO_WRITE 0xFE
81
 
82
/*#define DEBUG_TARGET 2*/
83
/*
84
 * Select timeout time (in 10ms units)
85
 *
86
 * This is the timeout used between the start of selection and the WD33C93
87
 * chip deciding that the device isn't responding.
88
 */
89
#define TIMEOUT_TIME 10
90
/*
91
 * Define this if you want to have verbose explaination of SCSI
92
 * status/messages.
93
 */
94
#undef CONFIG_ACORNSCSI_CONSTANTS
95
/*
96
 * Define this if you want to use the on board DMAC
97
 */
98
#define USE_DMAC
99
/*
100
 * List of devices that the driver will recognise
101
 */
102
#define ACORNSCSI_LIST { MANU_ACORN, PROD_ACORN_SCSI }
103
/*
104
 * ====================================================================================
105
 */
106
 
107
#ifdef DEBUG_TARGET
108
#define DBG(cmd,xxx...) \
109
  if (cmd->target == DEBUG_TARGET) { \
110
    xxx; \
111
  }
112
#else
113
#define DBG(cmd,xxx...) xxx
114
#endif
115
 
116
#ifndef STRINGIFY
117
#define STRINGIFY(x) #x
118
#endif
119
#define STR(x) STRINGIFY(x)
120
#define NO_WRITE_STR STR(NO_WRITE)
121
 
122
#include 
123
#include 
124
#include 
125
#include 
126
#include 
127
#include 
128
#include 
129
#include 
130
#include 
131
#include 
132
#include 
133
#include 
134
#include 
135
#include 
136
#include 
137
 
138
#include "../block/blk.h"
139
#include "scsi.h"
140
#include "hosts.h"
141
#include "acornscsi.h"
142
#include "constants.h"
143
 
144
#define VER_MAJOR 2
145
#define VER_MINOR 0
146
#define VER_PATCH 3
147
 
148
#ifndef ABORT_TAG
149
#define ABORT_TAG 0xd
150
#else
151
#error "Yippee!  ABORT TAG is now defined!  Remove this error!"
152
#endif
153
 
154
#ifndef NO_IRQ
155
#define NO_IRQ 255
156
#endif
157
 
158
/*
159
 * DMAC setup parameters
160
 */
161
#define INIT_DEVCON0    (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
162
#define INIT_DEVCON1    (DEVCON1_BHLD)
163
#define DMAC_READ       (MODECON_READ)
164
#define DMAC_WRITE      (MODECON_WRITE)
165
#define INIT_SBICDMA    (CTRL_DMABURST)
166
 
167
#ifdef SCSI2_LINK
168
#error SCSI2 LINKed commands not supported (yet)!
169
#endif
170
 
171
/*
172
 * Size of on-board DMA buffer
173
 */
174
#define DMAC_BUFFER_SIZE        65536
175
 
176
/*
177
 * This is used to dump the previous states of the SBIC
178
 */
179
static struct status_entry {
180
        unsigned long   when;
181
        unsigned char   ssr;
182
        unsigned char   ph;
183
        unsigned char   irq;
184
        unsigned char   unused;
185
} status[9][16];
186
static unsigned char status_ptr[9];
187
 
188
#define ADD_STATUS(_q,_ssr,_ph,_irq) \
189
({                                                      \
190
        status[(_q)][status_ptr[(_q)]].when = jiffies;  \
191
        status[(_q)][status_ptr[(_q)]].ssr  = (_ssr);   \
192
        status[(_q)][status_ptr[(_q)]].ph   = (_ph);    \
193
        status[(_q)][status_ptr[(_q)]].irq  = (_irq);   \
194
        status_ptr[(_q)] = (status_ptr[(_q)] + 1) & 15;     \
195
})
196
 
197
unsigned int sdtr_period = SDTR_PERIOD;
198
unsigned int sdtr_size   = SDTR_SIZE;
199
 
200
static struct proc_dir_entry proc_scsi_acornscsi = {
201
        PROC_SCSI_EATA, 9, "acornscsi", S_IFDIR | S_IRUGO | S_IXUGO, 2
202
};
203
 
204
static void acornscsi_done (AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result);
205
static int acornscsi_reconnect_finish (AS_Host *host);
206
static void acornscsi_dma_cleanup (AS_Host *host);
207
static void acornscsi_abortcmd (AS_Host *host, unsigned char tag);
208
 
209
/* ====================================================================================
210
 * Miscellaneous
211
 */
212
 
213
static inline void
214
sbic_arm_write (unsigned int io_port, int reg, int value)
215
{
216
    arm_outb (reg, io_port);
217
    arm_outb (value, io_port + 4);
218
}
219
 
220
#define sbic_arm_writenext(io,val) \
221
        arm_outb ((val), (io) + 4)
222
 
223
static inline
224
int sbic_arm_read (unsigned int io_port, int reg)
225
{
226
    if(reg == ASR)
227
           return arm_inl(io_port) & 255;
228
    arm_outb(reg, io_port);
229
    return arm_inl(io_port + 4) & 255;
230
}
231
 
232
#define sbic_arm_readnext(io) \
233
        arm_inb((io) + 4)
234
 
235
#define dmac_read(io_port,reg) \
236
        inb ((io_port) + (reg))
237
 
238
#define dmac_write(io_port,reg,value) \
239
        ({ outb ((value), (io_port) + (reg)); })
240
 
241
#define dmac_clearintr(io_port) \
242
        ({ outb (0, (io_port)); })
243
 
244
static inline
245
unsigned int dmac_address (unsigned int io_port)
246
{
247
    return dmac_read (io_port, TXADRHI) << 16 |
248
           dmac_read (io_port, TXADRMD) << 8 |
249
           dmac_read (io_port, TXADRLO);
250
}
251
 
252
static
253
unsigned long acornscsi_sbic_xfcount (AS_Host *host)
254
{
255
    unsigned long length;
256
 
257
    length = sbic_arm_read (host->scsi.io_port, TRANSCNTH) << 16;
258
    length |= sbic_arm_readnext (host->scsi.io_port) << 8;
259
    length |= sbic_arm_readnext (host->scsi.io_port);
260
 
261
    return length;
262
}
263
 
264
static void
265
acornscsi_csdelay (unsigned int cs)
266
{
267
    unsigned long target_jiffies, flags;
268
 
269
    target_jiffies = jiffies + 1 + cs * HZ / 100;
270
 
271
    save_flags (flags);
272
    sti ();
273
 
274
    while (jiffies < target_jiffies) barrier();
275
 
276
    restore_flags (flags);
277
}
278
 
279
static
280
void acornscsi_resetcard (AS_Host *host)
281
{
282
    unsigned int i;
283
 
284
    /* assert reset line */
285
    host->card.page_reg = 0x80;
286
    outb (host->card.page_reg, host->card.io_page);
287
 
288
    /* wait 3 cs.  SCSI standard says 25ms. */
289
    acornscsi_csdelay (3);
290
 
291
    host->card.page_reg = 0;
292
    outb (host->card.page_reg, host->card.io_page);
293
 
294
    /*
295
     * Should get a reset from the card
296
     */
297
    while (!(inb (host->card.io_intr) & 8));
298
    sbic_arm_read (host->scsi.io_port, ASR);
299
    sbic_arm_read (host->scsi.io_port, SSR);
300
 
301
    /* setup sbic - WD33C93A */
302
    sbic_arm_write (host->scsi.io_port, OWNID, OWNID_EAF | host->host->this_id);
303
    sbic_arm_write (host->scsi.io_port, CMND, CMND_RESET);
304
 
305
    /*
306
     * Command should cause a reset interrupt
307
     */
308
    while (!(inb (host->card.io_intr) & 8));
309
    sbic_arm_read (host->scsi.io_port, ASR);
310
    if (sbic_arm_read (host->scsi.io_port, SSR) != 0x01)
311
        printk (KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
312
                host->host->host_no);
313
 
314
    sbic_arm_write (host->scsi.io_port, CTRL, INIT_SBICDMA | CTRL_IDI);
315
    sbic_arm_write (host->scsi.io_port, TIMEOUT, TIMEOUT_TIME);
316
    sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
317
    sbic_arm_write (host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
318
 
319
    host->card.page_reg = 0x40;
320
    outb (host->card.page_reg, host->card.io_page);
321
 
322
    /* setup dmac - uPC71071 */
323
    dmac_write (host->dma.io_port, INIT, 0);
324
    dmac_write (host->dma.io_port, INIT, INIT_8BIT);
325
    dmac_write (host->dma.io_port, CHANNEL, CHANNEL_0);
326
    dmac_write (host->dma.io_port, DEVCON0, INIT_DEVCON0);
327
    dmac_write (host->dma.io_port, DEVCON1, INIT_DEVCON1);
328
 
329
    host->SCpnt = NULL;
330
    host->scsi.phase = PHASE_IDLE;
331
    host->scsi.disconnectable = 0;
332
 
333
    for (i = 0; i < 8; i++) {
334
        host->busyluns[i] = 0;
335
        host->device[i].sync_state = SYNC_NEGOCIATE;
336
        host->device[i].disconnect_ok = 1;
337
    }
338
 
339
    /* wait 25 cs.  SCSI standard says 250ms. */
340
    acornscsi_csdelay (25);
341
}
342
 
343
/*=============================================================================================
344
 * Utility routines (eg. debug)
345
 */
346
#ifdef CONFIG_ACORNSCSI_CONSTANTS
347
static char *acornscsi_interrupttype[] = {
348
  "rst",  "suc",  "p/a",  "3",
349
  "term", "5",    "6",    "7",
350
  "serv", "9",    "a",    "b",
351
  "c",    "d",    "e",    "f"
352
};
353
 
354
static signed char acornscsi_map[] = {
355
  0,  1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
356
 -1,  2, -1, -1,  -1, -1,  3, -1,   4,  5,  6,  7,   8,  9, 10, 11,
357
 12, 13, 14, -1,  -1, -1, -1, -1,   4,  5,  6,  7,   8,  9, 10, 11,
358
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
359
 15, 16, 17, 18,  19, -1, -1, 20,   4,  5,  6,  7,   8,  9, 10, 11,
360
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
361
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
362
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
363
 21, 22, -1, -1,  -1, 23, -1, -1,   4,  5,  6,  7,   8,  9, 10, 11,
364
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
365
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
366
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
367
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
368
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
369
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
370
 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1
371
};
372
 
373
static char *acornscsi_interruptcode[] = {
374
    /* 0 */
375
    "reset - normal mode",      /* 00 */
376
    "reset - advanced mode",    /* 01 */
377
 
378
    /* 2 */
379
    "sel",                      /* 11 */
380
    "sel+xfer",                 /* 16 */
381
    "data-out",                 /* 18 */
382
    "data-in",                  /* 19 */
383
    "cmd",                      /* 1A */
384
    "stat",                     /* 1B */
385
    "??-out",                   /* 1C */
386
    "??-in",                    /* 1D */
387
    "msg-out",                  /* 1E */
388
    "msg-in",                   /* 1F */
389
 
390
    /* 12 */
391
    "/ACK asserted",            /* 20 */
392
    "save-data-ptr",            /* 21 */
393
    "{re}sel",                  /* 22 */
394
 
395
    /* 15 */
396
    "inv cmd",                  /* 40 */
397
    "unexpected disconnect",    /* 41 */
398
    "sel timeout",              /* 42 */
399
    "P err",                    /* 43 */
400
    "P err+ATN",                /* 44 */
401
    "bad status byte",          /* 47 */
402
 
403
    /* 21 */
404
    "resel, no id",             /* 80 */
405
    "resel",                    /* 81 */
406
    "discon",                   /* 85 */
407
};
408
 
409
static
410
void print_scsi_status (unsigned int ssr)
411
{
412
    if (acornscsi_map[ssr] != -1)
413
        printk ("%s:%s",
414
                acornscsi_interrupttype[(ssr >> 4)],
415
                acornscsi_interruptcode[acornscsi_map[ssr]]);
416
    else
417
        printk ("%X:%X", ssr >> 4, ssr & 0x0f);
418
}
419
#endif
420
 
421
static
422
void print_sbic_status (int asr, int ssr, int cmdphase)
423
{
424
#ifdef CONFIG_ACORNSCSI_CONSTANTS
425
        printk ("sbic: %c%c%c%c%c%c ",
426
                asr & ASR_INT ? 'I' : 'i',
427
                asr & ASR_LCI ? 'L' : 'l',
428
                asr & ASR_BSY ? 'B' : 'b',
429
                asr & ASR_CIP ? 'C' : 'c',
430
                asr & ASR_PE  ? 'P' : 'p',
431
                asr & ASR_DBR ? 'D' : 'd');
432
        printk ("scsi: ");
433
        print_scsi_status (ssr);
434
        printk (" ph %02X\n", cmdphase);
435
#else
436
        printk ("sbic: %02X scsi: %X:%X ph: %02X\n",
437
                asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
438
#endif
439
}
440
 
441
static
442
void acornscsi_dumplog (AS_Host *host, int target)
443
{
444
    unsigned int prev;
445
    do {
446
        signed int statptr;
447
 
448
        printk ("%c:", target == 8 ? 'H' : ('0' + target));
449
        statptr = status_ptr[target] - 10;
450
 
451
        if (statptr < 0)
452
            statptr += 16;
453
 
454
        prev = status[target][statptr].when;
455
 
456
        for (; statptr != status_ptr[target]; statptr = (statptr + 1) & 15) {
457
            if (status[target][statptr].when) {
458
#ifdef CONFIG_ACORNSCSI_CONSTANTS
459
                printk ("%c%02X:S=",
460
                        status[target][statptr].irq ? '-' : ' ',
461
                        status[target][statptr].ph);
462
                print_scsi_status (status[target][statptr].ssr);
463
#else
464
                printk ("%c%02X:%02X",
465
                        status[target][statptr].irq ? '-' : ' ',
466
                        status[target][statptr].ph,
467
                        status[target][statptr].ssr);
468
#endif
469
                printk ("+%02ld",
470
                        (status[target][statptr].when - prev) < 100 ?
471
                                (status[target][statptr].when - prev) : 99);
472
                prev = status[target][statptr].when;
473
            }
474
        }
475
        printk ("\n");
476
        if (target == 8)
477
            break;
478
        target = 8;
479
    } while (1);
480
}
481
 
482
static
483
char acornscsi_target (AS_Host *host)
484
{
485
        if (host->SCpnt)
486
                return '0' + host->SCpnt->target;
487
        return 'H';
488
}
489
 
490
static
491
void acornscsi_dumpdma (AS_Host *host, char *where)
492
{
493
        unsigned int mode, addr, len;
494
 
495
        mode = dmac_read (host->dma.io_port, MODECON);
496
        addr = dmac_address (host->dma.io_port);
497
        len  = dmac_read (host->dma.io_port, TXCNTHI) << 8 |
498
               dmac_read (host->dma.io_port, TXCNTLO);
499
 
500
        printk ("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
501
                host->host->host_no, where,
502
                mode, addr, (len + 1) & 0xffff,
503
                dmac_read (host->dma.io_port, MASKREG));
504
 
505
        printk ("DMA @%06x, ", host->dma.start_addr);
506
        printk ("BH @%p +%04x, ", host->scsi.SCp.ptr,
507
                host->scsi.SCp.this_residual);
508
        printk ("DT @+%04x ST @+%04x", host->dma.transferred,
509
                host->scsi.SCp.have_data_in);
510
        printk ("\n");
511
}
512
 
513
/*
514
 * Prototype: cmdtype_t acornscsi_cmdtype (int command)
515
 * Purpose  : differentiate READ from WRITE from other commands
516
 * Params   : command - command to interpret
517
 * Returns  : CMD_READ  - command reads data,
518
 *            CMD_WRITE - command writes data,
519
 *            CMD_MISC  - everything else
520
 */
521
static inline
522
cmdtype_t acornscsi_cmdtype (int command)
523
{
524
    switch (command) {
525
    case WRITE_6:  case WRITE_10:  case WRITE_12:
526
        return CMD_WRITE;
527
    case READ_6:   case READ_10:   case READ_12:
528
        return CMD_READ;
529
    default:
530
        return CMD_MISC;
531
    }
532
}
533
 
534
/*
535
 * Prototype: int acornscsi_datadirection (int command)
536
 * Purpose  : differentiate between commands that have a DATA IN phase
537
 *            and a DATA OUT phase
538
 * Params   : command - command to interpret
539
 * Returns  : DATADIR_OUT - data out phase expected
540
 *            DATADIR_IN  - data in phase expected
541
 */
542
static
543
datadir_t acornscsi_datadirection (int command)
544
{
545
    switch (command) {
546
    case CHANGE_DEFINITION:     case COMPARE:           case COPY:
547
    case COPY_VERIFY:           case LOG_SELECT:        case MODE_SELECT:
548
    case MODE_SELECT_10:        case SEND_DIAGNOSTIC:   case WRITE_BUFFER:
549
    case FORMAT_UNIT:           case REASSIGN_BLOCKS:   case RESERVE:
550
    case SEARCH_EQUAL:          case SEARCH_HIGH:       case SEARCH_LOW:
551
    case WRITE_6:               case WRITE_10:          case WRITE_VERIFY:
552
    case UPDATE_BLOCK:          case WRITE_LONG:        case WRITE_SAME:
553
    case SEARCH_HIGH_12:        case SEARCH_EQUAL_12:   case SEARCH_LOW_12:
554
    case WRITE_12:              case WRITE_VERIFY_12:   case SET_WINDOW:
555
    case MEDIUM_SCAN:           case SEND_VOLUME_TAG:   case 0xea:
556
        return DATADIR_OUT;
557
    default:
558
        return DATADIR_IN;
559
    }
560
}
561
 
562
/*
563
 * Purpose  : provide values for synchronous transfers with 33C93.
564
 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
565
 *      Modified by Russell King for 8MHz WD33C93A
566
 */
567
static struct sync_xfer_tbl {
568
    unsigned int period_ns;
569
    unsigned char reg_value;
570
} sync_xfer_table[] = {
571
    {   1, 0x20 },    { 249, 0x20 },    { 374, 0x30 },
572
    { 499, 0x40 },    { 624, 0x50 },    { 749, 0x60 },
573
    { 874, 0x70 },    { 999, 0x00 },    {   0,    0 }
574
};
575
 
576
/*
577
 * Prototype: int acornscsi_getperiod (unsigned char syncxfer)
578
 * Purpose  : period for the synchronous transfer setting
579
 * Params   : syncxfer SYNCXFER register value
580
 * Returns  : period in ns.
581
 */
582
static
583
int acornscsi_getperiod (unsigned char syncxfer)
584
{
585
    int i;
586
 
587
    syncxfer &= 0xf0;
588
    if (syncxfer == 0x10)
589
        syncxfer = 0;
590
 
591
    for (i = 1; sync_xfer_table[i].period_ns; i++)
592
        if (syncxfer == sync_xfer_table[i].reg_value)
593
            return sync_xfer_table[i].period_ns;
594
    return 0;
595
}
596
 
597
/*
598
 * Prototype: int round_period (unsigned int period)
599
 * Purpose  : return index into above table for a required REQ period
600
 * Params   : period - time (ns) for REQ
601
 * Returns  : table index
602
 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
603
 */
604
static inline
605
int round_period (unsigned int period)
606
{
607
    int i;
608
 
609
    for (i = 1; sync_xfer_table[i].period_ns; i++) {
610
        if ((period <= sync_xfer_table[i].period_ns) &&
611
            (period > sync_xfer_table[i - 1].period_ns))
612
            return i;
613
    }
614
    return 7;
615
}
616
 
617
/*
618
 * Prototype: unsigned char calc_sync_xfer (unsigned int period, unsigned int offset)
619
 * Purpose  : calculate value for 33c93s SYNC register
620
 * Params   : period - time (ns) for REQ
621
 *            offset - offset in bytes between REQ/ACK
622
 * Returns  : value for SYNC register
623
 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
624
 */
625
static
626
unsigned char calc_sync_xfer (unsigned int period, unsigned int offset)
627
{
628
    return sync_xfer_table[round_period(period)].reg_value |
629
                ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
630
}
631
 
632
/* ====================================================================================
633
 * Command functions
634
 */
635
/*
636
 * Function: acornscsi_kick (AS_Host *host)
637
 * Purpose : kick next command to interface
638
 * Params  : host - host to send command to
639
 * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
640
 * Notes   : interrupts are always disabled!
641
 */
642
static
643
intr_ret_t acornscsi_kick (AS_Host *host)
644
{
645
    int from_queue = 0;
646
    Scsi_Cmnd *SCpnt;
647
 
648
    /* first check to see if a command is waiting to be executed */
649
    SCpnt = host->origSCpnt;
650
    host->origSCpnt = NULL;
651
 
652
    /* retrieve next command */
653
    if (!SCpnt) {
654
        SCpnt = queue_remove_exclude (&host->queues.issue, host->busyluns);
655
        if (!SCpnt)
656
            return INTR_IDLE;
657
 
658
        from_queue = 1;
659
    }
660
 
661
    if (host->scsi.disconnectable && host->SCpnt) {
662
        queue_add_cmd_tail (&host->queues.disconnected, host->SCpnt);
663
        host->scsi.disconnectable = 0;
664
#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
665
        DBG(host->SCpnt, printk ("scsi%d.%c: moved command to disconnected queue\n",
666
                host->host->host_no, acornscsi_target (host)));
667
#endif
668
        host->SCpnt = NULL;
669
    }
670
 
671
    /*
672
     * If we have an interrupt pending, then we may have been reselected.
673
     * In this case, we don't want to write to the registers
674
     */
675
    if (!(sbic_arm_read (host->scsi.io_port, ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
676
        sbic_arm_write (host->scsi.io_port, DESTID, SCpnt->target);
677
        sbic_arm_write (host->scsi.io_port, CMND, CMND_SELWITHATN);
678
    }
679
 
680
    /*
681
     * claim host busy - all of these must happen atomically wrt
682
     * our interrupt routine.  Failure means command loss.
683
     */
684
    host->scsi.phase = PHASE_CONNECTING;
685
    host->SCpnt = SCpnt;
686
    host->scsi.SCp = SCpnt->SCp;
687
    host->dma.xfer_setup = 0;
688
    host->dma.xfer_required = 0;
689
 
690
#if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
691
    DBG(SCpnt,printk ("scsi%d.%c: starting cmd %02X\n",
692
            host->host->host_no, '0' + SCpnt->target,
693
            SCpnt->cmnd[0]));
694
#endif
695
 
696
    if (from_queue) {
697
#ifdef SCSI2_TAG
698
        /*
699
         * tagged queueing - allocate a new tag to this command
700
         */
701
        if (SCpnt->device->tagged_queue) {
702
            SCpnt->device->current_tag += 1;
703
            if (SCpnt->device->current_tag == 0)
704
                SCpnt->device->current_tag = 1;
705
            SCpnt->tag = SCpnt->device->current_tag;
706
        } else
707
#endif
708
            set_bit (SCpnt->target * 8 + SCpnt->lun, host->busyluns);
709
 
710
        host->stats.removes += 1;
711
 
712
        switch (acornscsi_cmdtype (SCpnt->cmnd[0])) {
713
        case CMD_WRITE:
714
            host->stats.writes += 1;
715
            break;
716
        case CMD_READ:
717
            host->stats.reads += 1;
718
            break;
719
        case CMD_MISC:
720
            host->stats.miscs += 1;
721
            break;
722
        }
723
    }
724
 
725
    return INTR_PROCESSING;
726
}
727
 
728
/*
729
 * Function: void acornscsi_done (AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
730
 * Purpose : complete processing for command
731
 * Params  : host   - interface that completed
732
 *           result - driver byte of result
733
 */
734
static
735
void acornscsi_done (AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
736
{
737
    Scsi_Cmnd *SCpnt = *SCpntp;
738
 
739
    /* clean up */
740
    sbic_arm_write (host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
741
 
742
    host->stats.fins += 1;
743
 
744
    if (SCpnt) {
745
        *SCpntp = NULL;
746
 
747
        acornscsi_dma_cleanup (host);
748
 
749
        SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
750
 
751
        /*
752
         * In theory, this should not happen.  In practice, it seems to.
753
         * Only trigger an error if the device attempts to report all happy
754
         * but with untransferred buffers...  If we don't do something, then
755
         * data loss will occur.  Should we check SCpnt->underflow here?
756
         * It doesn't appear to be set to something meaningful by the higher
757
         * levels all the time.
758
         */
759
        if (host->scsi.SCp.ptr && result == DID_OK &&
760
                acornscsi_cmdtype (SCpnt->cmnd[0]) != CMD_MISC) {
761
            switch (status_byte (SCpnt->result)) {
762
            case CHECK_CONDITION:
763
            case COMMAND_TERMINATED:
764
            case BUSY:
765
            case QUEUE_FULL:
766
            case RESERVATION_CONFLICT:
767
                break;
768
 
769
            default:
770
                printk (KERN_ERR "scsi%d.H: incomplete data transfer detected: result=%08X command=",
771
                        host->host->host_no, SCpnt->result);
772
                print_command (SCpnt->cmnd);
773
                acornscsi_dumpdma (host, "done");
774
                acornscsi_dumplog (host, SCpnt->target);
775
                SCpnt->result &= 0xffff;
776
                SCpnt->result |= DID_ERROR << 16;
777
            }
778
        }
779
 
780
        if (!SCpnt->scsi_done)
781
            panic ("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
782
 
783
        clear_bit (SCpnt->target * 8 + SCpnt->lun, host->busyluns);
784
 
785
        SCpnt->scsi_done (SCpnt);
786
    } else
787
        printk ("scsi%d: null command in acornscsi_done", host->host->host_no);
788
 
789
    host->scsi.phase = PHASE_IDLE;
790
}
791
 
792
/* ====================================================================================
793
 * DMA routines
794
 */
795
/*
796
 * Purpose  : update SCSI Data Pointer
797
 * Notes    : this will only be one SG entry or less
798
 */
799
static
800
void acornscsi_data_updateptr (AS_Host *host, Scsi_Pointer *SCp, unsigned int length)
801
{
802
    SCp->ptr += length;
803
    SCp->this_residual -= length;
804
 
805
    if (!SCp->this_residual) {
806
        if (SCp->buffers_residual) {
807
            SCp->buffer++;
808
            SCp->buffers_residual--;
809
            SCp->ptr = (char *)SCp->buffer->address;
810
            SCp->this_residual = SCp->buffer->length;
811
        } else
812
            SCp->ptr = NULL;
813
    }
814
}
815
 
816
/*
817
 * Prototype: void acornscsi_data_read (AS_Host *host, char *ptr,
818
 *                              unsigned int start_addr, unsigned int length)
819
 * Purpose  : read data from DMA RAM
820
 * Params   : host - host to transfer from
821
 *            ptr  - DRAM address
822
 *            start_addr - host mem address
823
 *            length - number of bytes to transfer
824
 * Notes    : this will only be one SG entry or less
825
 */
826
static
827
void acornscsi_data_read (AS_Host *host, char *ptr,
828
                                 unsigned int start_addr, unsigned int length)
829
{
830
    extern void __acornscsi_in (int port, char *buf, int len);
831
    unsigned int page, offset, len = length;
832
 
833
    page = (start_addr >> 12);
834
    offset = start_addr & ((1 << 12) - 1);
835
 
836
    outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
837
 
838
    while (len > 0) {
839
        unsigned int this_len;
840
 
841
        if (len + offset > (1 << 12))
842
            this_len = (1 << 12) - offset;
843
        else
844
            this_len = len;
845
 
846
        __acornscsi_in (host->card.io_ram + (offset << 1), ptr, this_len);
847
 
848
        offset += this_len;
849
        ptr += this_len;
850
        len -= this_len;
851
 
852
        if (offset == (1 << 12)) {
853
            offset = 0;
854
            page ++;
855
            outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
856
        }
857
    }
858
    outb (host->card.page_reg, host->card.io_page);
859
}
860
 
861
/*
862
 * Prototype: void acornscsi_data_write (AS_Host *host, char *ptr,
863
 *                              unsigned int start_addr, unsigned int length)
864
 * Purpose  : write data to DMA RAM
865
 * Params   : host - host to transfer from
866
 *            ptr  - DRAM address
867
 *            start_addr - host mem address
868
 *            length - number of bytes to transfer
869
 * Notes    : this will only be one SG entry or less
870
 */
871
static
872
void acornscsi_data_write (AS_Host *host, char *ptr,
873
                                 unsigned int start_addr, unsigned int length)
874
{
875
    extern void __acornscsi_out (int port, char *buf, int len);
876
    unsigned int page, offset, len = length;
877
 
878
    page = (start_addr >> 12);
879
    offset = start_addr & ((1 << 12) - 1);
880
 
881
    outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
882
 
883
    while (len > 0) {
884
        unsigned int this_len;
885
 
886
        if (len + offset > (1 << 12))
887
            this_len = (1 << 12) - offset;
888
        else
889
            this_len = len;
890
 
891
        __acornscsi_out (host->card.io_ram + (offset << 1), ptr, this_len);
892
 
893
        offset += this_len;
894
        ptr += this_len;
895
        len -= this_len;
896
 
897
        if (offset == (1 << 12)) {
898
            offset = 0;
899
            page ++;
900
            outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
901
        }
902
    }
903
    outb (host->card.page_reg, host->card.io_page);
904
}
905
 
906
/* =========================================================================================
907
 * On-board DMA routines
908
 */
909
/*
910
 * Prototype: void acornscsi_dmastop (AS_Host *host)
911
 * Purpose  : stop all DMA
912
 * Params   : host - host on which to stop DMA
913
 * Notes    : This is called when leaving DATA IN/OUT phase,
914
 *            or when interface is RESET
915
 */
916
static inline
917
void acornscsi_dma_stop (AS_Host *host)
918
{
919
    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
920
    dmac_clearintr (host->dma.io_intr_clear);
921
 
922
#if (DEBUG & DEBUG_DMA)
923
    DBG(host->SCpnt, acornscsi_dumpdma (host, "stop"));
924
#endif
925
}
926
 
927
/*
928
 * Function: void acornscsi_dma_setup (AS_Host *host, dmadir_t direction)
929
 * Purpose : setup DMA controller for data transfer
930
 * Params  : host - host to setup
931
 *           direction - data transfer direction
932
 * Notes   : This is called when entering DATA I/O phase, not
933
 *           while we're in a DATA I/O phase
934
 */
935
static
936
void acornscsi_dma_setup (AS_Host *host, dmadir_t direction)
937
{
938
    unsigned int address, length, mode;
939
 
940
    host->dma.direction = direction;
941
 
942
    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
943
 
944
    if (direction == DMA_OUT) {
945
#if (DEBUG & DEBUG_NO_WRITE)
946
        if (NO_WRITE & (1 << host->SCpnt->target)) {
947
            printk (KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
948
                    host->host->host_no, acornscsi_target (host));
949
            return;
950
        }
951
#endif
952
        mode = DMAC_WRITE;
953
    } else
954
        mode = DMAC_READ;
955
 
956
    /*
957
     * Allocate some buffer space, limited to half the buffer size
958
     */
959
    length = min (host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
960
    if (length) {
961
        host->dma.start_addr = address = host->dma.free_addr;
962
        host->dma.free_addr = (host->dma.free_addr + length) &
963
                                (DMAC_BUFFER_SIZE - 1);
964
 
965
        /*
966
         * Transfer data to DMA memory
967
         */
968
        if (direction == DMA_OUT)
969
            acornscsi_data_write (host, host->scsi.SCp.ptr, host->dma.start_addr,
970
                                length);
971
 
972
        length -= 1;
973
        dmac_write (host->dma.io_port, TXCNTLO, length);
974
        dmac_write (host->dma.io_port, TXCNTHI, length >> 8);
975
        dmac_write (host->dma.io_port, TXADRLO, address);
976
        dmac_write (host->dma.io_port, TXADRMD, address >> 8);
977
        dmac_write (host->dma.io_port, TXADRHI, 0);
978
        dmac_write (host->dma.io_port, MODECON, mode);
979
        dmac_write (host->dma.io_port, MASKREG, MASK_OFF);
980
 
981
#if (DEBUG & DEBUG_DMA)
982
        DBG(host->SCpnt, acornscsi_dumpdma (host, "strt"));
983
#endif
984
        host->dma.xfer_setup = 1;
985
    }
986
}
987
 
988
/*
989
 * Function: void acornscsi_dma_cleanup (AS_Host *host)
990
 * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
991
 * Params  : host - host to finish
992
 * Notes   : This is called when a command is:
993
 *              terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONECT
994
 *         : This must not return until all transfers are completed.
995
 */
996
static
997
void acornscsi_dma_cleanup (AS_Host *host)
998
{
999
    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
1000
    dmac_clearintr (host->dma.io_intr_clear);
1001
 
1002
    /*
1003
     * Check for a pending transfer
1004
     */
1005
    if (host->dma.xfer_required) {
1006
        host->dma.xfer_required = 0;
1007
        if (host->dma.direction == DMA_IN)
1008
            acornscsi_data_read (host, host->dma.xfer_ptr,
1009
                                 host->dma.xfer_start, host->dma.xfer_length);
1010
    }
1011
 
1012
    /*
1013
     * Has a transfer been setup?
1014
     */
1015
    if (host->dma.xfer_setup) {
1016
        unsigned int transferred;
1017
 
1018
        host->dma.xfer_setup = 0;
1019
 
1020
#if (DEBUG & DEBUG_DMA)
1021
        DBG(host->SCpnt, acornscsi_dumpdma (host, "clup"));
1022
#endif
1023
 
1024
        /*
1025
         * Calculate number of bytes transferred from DMA.
1026
         */
1027
        transferred = dmac_address (host->dma.io_port) - host->dma.start_addr;
1028
        host->dma.transferred += transferred;
1029
 
1030
        if (host->dma.direction == DMA_IN)
1031
            acornscsi_data_read (host, host->scsi.SCp.ptr,
1032
                                 host->dma.start_addr, transferred);
1033
 
1034
        /*
1035
         * Update SCSI pointers
1036
         */
1037
        acornscsi_data_updateptr (host, &host->scsi.SCp, transferred);
1038
    }
1039
}
1040
 
1041
/*
1042
 * Function: void acornscsi_dmacintr (AS_Host *host)
1043
 * Purpose : handle interrupts from DMAC device
1044
 * Params  : host - host to process
1045
 * Notes   : If reading, we schedule the read to main memory &
1046
 *           allow the transfer to continue.
1047
 *         : If writing, we fill the onboard DMA memory from main
1048
 *           memory.
1049
 *         : Called whenever DMAC finished it's current transfer.
1050
 */
1051
static
1052
void acornscsi_dma_intr (AS_Host *host)
1053
{
1054
    unsigned int address, length, transferred;
1055
 
1056
#if (DEBUG & DEBUG_DMA)
1057
    DBG(host->SCpnt, acornscsi_dumpdma (host, "inti"));
1058
#endif
1059
 
1060
    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
1061
    dmac_clearintr (host->dma.io_intr_clear);
1062
 
1063
    /*
1064
     * Calculate amount transferred via DMA
1065
     */
1066
    transferred = dmac_address (host->dma.io_port) - host->dma.start_addr;
1067
    host->dma.transferred += transferred;
1068
 
1069
    /*
1070
     * Schedule DMA transfer off board
1071
     */
1072
    if (host->dma.direction == DMA_IN) {
1073
        host->dma.xfer_start = host->dma.start_addr;
1074
        host->dma.xfer_length = transferred;
1075
        host->dma.xfer_ptr = host->scsi.SCp.ptr;
1076
        host->dma.xfer_required = 1;
1077
    }
1078
 
1079
    acornscsi_data_updateptr (host, &host->scsi.SCp, transferred);
1080
 
1081
    /*
1082
     * Allocate some buffer space, limited to half the buffer size
1083
     */
1084
    length = min (host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1085
    if (length) {
1086
        host->dma.start_addr = address = host->dma.free_addr;
1087
        host->dma.free_addr = (host->dma.free_addr + length) &
1088
                                (DMAC_BUFFER_SIZE - 1);
1089
 
1090
        /*
1091
         * Transfer data to DMA memory
1092
         */
1093
        if (host->dma.direction == DMA_OUT)
1094
            acornscsi_data_write (host, host->scsi.SCp.ptr, host->dma.start_addr,
1095
                                length);
1096
 
1097
        length -= 1;
1098
        dmac_write (host->dma.io_port, TXCNTLO, length);
1099
        dmac_write (host->dma.io_port, TXCNTHI, length >> 8);
1100
        dmac_write (host->dma.io_port, TXADRLO, address);
1101
        dmac_write (host->dma.io_port, TXADRMD, address >> 8);
1102
        dmac_write (host->dma.io_port, TXADRHI, 0);
1103
        dmac_write (host->dma.io_port, MASKREG, MASK_OFF);
1104
 
1105
#if (DEBUG & DEBUG_DMA)
1106
        DBG(host->SCpnt, acornscsi_dumpdma (host, "into"));
1107
#endif
1108
    } else {
1109
        host->dma.xfer_setup = 0;
1110
#if 0
1111
        /*
1112
         * If the interface still wants more, then this is an error.
1113
         * We give it another byte, but we also attempt to raise an
1114
         * attention condition.  We continue giving one byte until
1115
         * the device recognises the attention.
1116
         */
1117
        if (dmac_read (host->dma.io_port, STATUS) & STATUS_RQ0) {
1118
            acornscsi_abortcmd (host, host->SCpnt->tag);
1119
 
1120
            dmac_write (host->dma.io_port, TXCNTLO, 0);
1121
            dmac_write (host->dma.io_port, TXCNTHI, 0);
1122
            dmac_write (host->dma.io_port, TXADRLO, 0);
1123
            dmac_write (host->dma.io_port, TXADRMD, 0);
1124
            dmac_write (host->dma.io_port, TXADRHI, 0);
1125
            dmac_write (host->dma.io_port, MASKREG, MASK_OFF);
1126
        }
1127
#endif
1128
    }
1129
}
1130
 
1131
/*
1132
 * Function: void acornscsi_dma_xfer (AS_Host *host)
1133
 * Purpose : transfer data between AcornSCSI and memory
1134
 * Params  : host - host to process
1135
 */
1136
static
1137
void acornscsi_dma_xfer (AS_Host *host)
1138
{
1139
    host->dma.xfer_required = 0;
1140
 
1141
    if (host->dma.direction == DMA_IN)
1142
        acornscsi_data_read (host, host->dma.xfer_ptr,
1143
                                host->dma.xfer_start, host->dma.xfer_length);
1144
}
1145
 
1146
/*
1147
 * Function: void acornscsi_dma_adjust (AS_Host *host)
1148
 * Purpose : adjust DMA pointers & count for bytes transfered to
1149
 *           SBIC but not SCSI bus.
1150
 * Params  : host - host to adjust DMA count for
1151
 */
1152
static
1153
void acornscsi_dma_adjust (AS_Host *host)
1154
{
1155
    if (host->dma.xfer_setup) {
1156
        signed long transferred;
1157
#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1158
        DBG(host->SCpnt, acornscsi_dumpdma (host, "adji"));
1159
#endif
1160
        /*
1161
         * Calculate correct DMA address - DMA is ahead of SCSI bus while
1162
         * writing.
1163
         *  host->scsi.SCp.have_data_in is the number of bytes
1164
         *  actually transferred to/from the SCSI bus.
1165
         *  host->dma.transferred is the number of bytes transferred
1166
         *  over DMA since host->dma.start_addr was last set.
1167
         *
1168
         * real_dma_addr = host->dma.start_addr + host->scsi.SCp.have_data_in
1169
         *                 - host->dma.transferred
1170
         */
1171
        transferred = host->scsi.SCp.have_data_in - host->dma.transferred;
1172
        if (transferred < 0)
1173
            printk ("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1174
                    host->host->host_no, acornscsi_target (host), transferred);
1175
        else if (transferred == 0)
1176
            host->dma.xfer_setup = 0;
1177
        else {
1178
            transferred += host->dma.start_addr;
1179
            dmac_write (host->dma.io_port, TXADRLO, transferred);
1180
            dmac_write (host->dma.io_port, TXADRMD, transferred >> 8);
1181
            dmac_write (host->dma.io_port, TXADRHI, transferred >> 16);
1182
#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1183
            DBG(host->SCpnt, acornscsi_dumpdma (host, "adjo"));
1184
#endif
1185
        }
1186
    }
1187
}
1188
 
1189
/* =========================================================================================
1190
 * Data I/O
1191
 */
1192
/*
1193
 * Function: void acornscsi_sendcommand (AS_Host *host)
1194
 * Purpose : send a command to a target
1195
 * Params  : host - host which is connected to target
1196
 */
1197
static
1198
void acornscsi_sendcommand (AS_Host *host)
1199
{
1200
    Scsi_Cmnd *SCpnt = host->SCpnt;
1201
    unsigned int asr;
1202
    unsigned char *cmdptr, *cmdend;
1203
 
1204
    sbic_arm_write (host->scsi.io_port, TRANSCNTH, 0);
1205
    sbic_arm_writenext (host->scsi.io_port, 0);
1206
    sbic_arm_writenext (host->scsi.io_port, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1207
    sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO);
1208
 
1209
    cmdptr = SCpnt->cmnd + host->scsi.SCp.sent_command;
1210
    cmdend = SCpnt->cmnd + SCpnt->cmd_len;
1211
 
1212
    while (cmdptr < cmdend) {
1213
        asr = sbic_arm_read (host->scsi.io_port, ASR);
1214
        if (asr & ASR_DBR)
1215
            sbic_arm_write (host->scsi.io_port, DATA, *cmdptr++);
1216
        else if (asr & ASR_INT)
1217
            break;
1218
    }
1219
    if (cmdptr >= cmdend)
1220
        host->scsi.SCp.sent_command = cmdptr - SCpnt->cmnd;
1221
    host->scsi.phase = PHASE_COMMAND;
1222
}
1223
 
1224
static
1225
void acornscsi_sendmessage (AS_Host *host)
1226
{
1227
    unsigned int message_length = msgqueue_msglength (&host->scsi.msgs);
1228
    int msglen;
1229
    char *msg;
1230
 
1231
#if (DEBUG & DEBUG_MESSAGES)
1232
        printk ("scsi%d.%c: sending message ",
1233
                host->host->host_no, acornscsi_target (host));
1234
#endif
1235
 
1236
    switch (message_length) {
1237
    case 0:
1238
        sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO | CMND_SBT);
1239
        while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
1240
        sbic_arm_write (host->scsi.io_port, DATA, NOP);
1241
        host->scsi.last_message = NOP;
1242
#if (DEBUG & DEBUG_MESSAGES)
1243
        printk ("NOP");
1244
#endif
1245
        break;
1246
 
1247
    case 1:
1248
        sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO | CMND_SBT);
1249
        msg = msgqueue_getnextmsg (&host->scsi.msgs, &msglen);
1250
        while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
1251
        sbic_arm_write (host->scsi.io_port, DATA, msg[0]);
1252
        host->scsi.last_message = msg[0];
1253
#if (DEBUG & DEBUG_MESSAGES)
1254
        print_msg (msg);
1255
#endif
1256
        break;
1257
 
1258
    default:
1259
        /*
1260
         * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
1261
         * 'When a target sends this (MESSAGE_REJECT) message, it
1262
         *  shall change to MESSAGE IN phase and send this message
1263
         *  prior to requesting additional message bytes from the
1264
         *  initiator.  This provides an interlock so that the
1265
         *  initiator can determine which message byte is rejected.
1266
         */
1267
        sbic_arm_write (host->scsi.io_port, TRANSCNTH, 0);
1268
        sbic_arm_writenext (host->scsi.io_port, 0);
1269
        sbic_arm_writenext (host->scsi.io_port, message_length);
1270
        sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO);
1271
 
1272
        while ((msg = msgqueue_getnextmsg (&host->scsi.msgs, &msglen)) != NULL) {
1273
            unsigned int asr, i;
1274
#if (DEBUG & DEBUG_MESSAGES)
1275
            print_msg (msg);
1276
#endif
1277
            for (i = 0; i < msglen;) {
1278
                asr = sbic_arm_read (host->scsi.io_port, ASR);
1279
                if (asr & ASR_DBR)
1280
                    sbic_arm_write (host->scsi.io_port, DATA, msg[i++]);
1281
                if (asr & ASR_INT)
1282
                    break;
1283
            }
1284
            host->scsi.last_message = msg[0];
1285
            if (msg[0] == EXTENDED_MESSAGE)
1286
                host->scsi.last_message |= msg[2] << 8;
1287
            if (asr & ASR_INT)
1288
                break;
1289
        }
1290
        break;
1291
    }
1292
#if (DEBUG & DEBUG_MESSAGES)
1293
    printk ("\n");
1294
#endif
1295
}
1296
 
1297
/*
1298
 * Function: void acornscsi_readstatusbyte (AS_Host *host)
1299
 * Purpose : Read status byte from connected target
1300
 * Params  : host - host connected to target
1301
 */
1302
static
1303
void acornscsi_readstatusbyte (AS_Host *host)
1304
{
1305
    sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO|CMND_SBT);
1306
    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
1307
 
1308
    host->scsi.SCp.Status = sbic_arm_read (host->scsi.io_port, DATA);
1309
 
1310
    host->scsi.phase = PHASE_STATUSIN;
1311
}
1312
 
1313
/*
1314
 * Function: unsigned char acornscsi_readmessagebyte (AS_Host *host)
1315
 * Purpose : Read one message byte from connected target
1316
 * Params  : host - host connected to target
1317
 */
1318
static
1319
unsigned char acornscsi_readmessagebyte (AS_Host *host)
1320
{
1321
    unsigned char message;
1322
 
1323
    sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO | CMND_SBT);
1324
    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
1325
 
1326
    message = sbic_arm_read (host->scsi.io_port, DATA);
1327
 
1328
    /* wait for MSGIN-XFER-PAUSED */
1329
    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_INT) == 0);
1330
    sbic_arm_read (host->scsi.io_port, SSR);
1331
 
1332
    return message;
1333
}
1334
 
1335
/*
1336
 * Function: void acornscsi_message (AS_Host *host)
1337
 * Purpose : Read complete message from connected target & action message
1338
 * Params  : host - host connected to target
1339
 */
1340
static
1341
void acornscsi_message (AS_Host *host)
1342
{
1343
    unsigned char message[16];
1344
    unsigned int msgidx = 0, msglen = 1;
1345
 
1346
    do {
1347
        message[msgidx] = acornscsi_readmessagebyte (host);
1348
 
1349
        switch (msgidx) {
1350
        case 0:
1351
            if (message[0] == EXTENDED_MESSAGE ||
1352
                (message[0] >= 0x20 && message[0] <= 0x2f))
1353
                msglen = 2;
1354
            break;
1355
 
1356
        case 1:
1357
            if (message[0] == EXTENDED_MESSAGE)
1358
                msglen += message[msgidx];
1359
            break;
1360
        }
1361
        msgidx += 1;
1362
        if (msgidx < msglen) {
1363
            sbic_arm_write (host->scsi.io_port, CMND, CMND_NEGATEACK);
1364
 
1365
            /* wait for next msg-in */
1366
            while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_INT) == 0);
1367
            sbic_arm_read (host->scsi.io_port, SSR);
1368
        }
1369
    } while (msgidx < msglen);
1370
 
1371
#if (DEBUG & DEBUG_MESSAGES)
1372
    printk (KERN_DEBUG "scsi%d.%c: message in: ",
1373
            host->host->host_no, acornscsi_target (host));
1374
    print_msg (message);
1375
    printk ("\n");
1376
#endif
1377
 
1378
    if (host->scsi.phase == PHASE_RECONNECTED) {
1379
        /*
1380
         * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1381
         * 'Whenever a target reconnects to an initiator to continue
1382
         *  a tagged I/O process, the SIMPLE QUEUE TAG message shall
1383
         *  be sent immediately following the IDENTIFY message...'
1384
         */
1385
        if (message[0] == SIMPLE_QUEUE_TAG)
1386
            host->scsi.reconnected.tag = message[1];
1387
        if (acornscsi_reconnect_finish (host))
1388
            host->scsi.phase = PHASE_MSGIN;
1389
    }
1390
 
1391
    switch (message[0]) {
1392
    case ABORT:
1393
    case ABORT_TAG:
1394
    case COMMAND_COMPLETE:
1395
        if (host->scsi.phase != PHASE_STATUSIN)
1396
            printk (KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1397
                    host->host->host_no, acornscsi_target (host));
1398
        host->scsi.phase = PHASE_DONE;
1399
        host->scsi.SCp.Message = message[0];
1400
        break;
1401
 
1402
    case SAVE_POINTERS:
1403
        /*
1404
         * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
1405
         * 'The SAVE DATA POINTER message is sent from a target to
1406
         *  direct the initiator to copy the active data pointer to
1407
         *  the saved data pointer for the current I/O process.
1408
         */
1409
        acornscsi_dma_cleanup (host);
1410
        host->SCpnt->SCp = host->scsi.SCp;
1411
        host->SCpnt->SCp.sent_command = 0;
1412
        host->scsi.phase = PHASE_MSGIN;
1413
        break;
1414
 
1415
    case RESTORE_POINTERS:
1416
        /*
1417
         * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
1418
         * 'The RESTORE POINTERS message is sent from a target to
1419
         *  direct the initiator to copy the most recently saved
1420
         *  command, data, and status pointers for the I/O process
1421
         *  to the corresponding active pointers.  The command and
1422
         *  status pointers shall be restored to the beginning of
1423
         *  the present command and status areas.'
1424
         */
1425
        acornscsi_dma_cleanup (host);
1426
        host->scsi.SCp = host->SCpnt->SCp;
1427
        host->scsi.phase = PHASE_MSGIN;
1428
        break;
1429
 
1430
    case DISCONNECT:
1431
        /*
1432
         * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
1433
         * 'On those occasions when an error or exception condition occurs
1434
         *  and the target elects to repeat the information transfer, the
1435
         *  target may repeat the transfer either issuing a RESTORE POINTERS
1436
         *  message or by disconnecting without issuing a SAVE POINTERS
1437
         *  message.  When reconnection is completed, the most recent
1438
         *  saved pointer values are restored.'
1439
         */
1440
        acornscsi_dma_cleanup (host);
1441
        host->scsi.phase = PHASE_DISCONNECT;
1442
        break;
1443
 
1444
    case MESSAGE_REJECT:
1445
#if 0 /* this isn't needed any more */
1446
        /*
1447
         * If we were negociating sync transfer, we don't yet know if
1448
         * this REJECT is for the sync transfer or for the tagged queue/wide
1449
         * transfer.  Re-initiate sync transfer negociation now, and if
1450
         * we got a REJECT in response to SDTR, then it'll be set to DONE.
1451
         */
1452
        if (host->device[host->SCpnt->target].sync_state == SYNC_SENT_REQUEST)
1453
            host->device[host->SCpnt->target].sync_state = SYNC_NEGOCIATE;
1454
#endif
1455
 
1456
        /*
1457
         * If we have any messages waiting to go out, then assert ATN now
1458
         */
1459
        if (msgqueue_msglength (&host->scsi.msgs))
1460
            sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1461
 
1462
        switch (host->scsi.last_message) {
1463
#ifdef SCSI2_TAG
1464
        case HEAD_OF_QUEUE_TAG:
1465
        case ORDERED_QUEUE_TAG:
1466
        case SIMPLE_QUEUE_TAG:
1467
            /*
1468
             * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1469
             *  If a target does not implement tagged queuing and a queue tag
1470
             *  message is received, it shall respond with a MESSAGE REJECT
1471
             *  message and accept the I/O process as if it were untagged.
1472
             */
1473
            printk (KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
1474
                    host->host->host_no, acornscsi_target (host));
1475
            host->SCpnt->device->tagged_queue = 0;
1476
            set_bit (host->SCpnt->target * 8 + host->SCpnt->lun, &host->busyluns);
1477
            break;
1478
#endif
1479
        case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1480
            /*
1481
             * Target can't handle synchronous transfers
1482
             */
1483
            printk (KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1484
                    host->host->host_no, acornscsi_target (host));
1485
            host->device[host->SCpnt->target].sync_xfer = SYNCHTRANSFER_2DBA;
1486
            host->device[host->SCpnt->target].sync_state = SYNC_ASYNCHRONOUS;
1487
            sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
1488
            break;
1489
 
1490
        default:
1491
            break;
1492
        }
1493
        break;
1494
 
1495
    case QUEUE_FULL:
1496
        /* TODO: target queue is full */
1497
        break;
1498
 
1499
    case SIMPLE_QUEUE_TAG:
1500
        /* tag queue reconnect... message[1] = queue tag.  Print something to indicate something happened! */
1501
        printk ("scsi%d.%c: reconnect queue tag %02X\n",
1502
                host->host->host_no, acornscsi_target (host),
1503
                message[1]);
1504
        break;
1505
 
1506
    case EXTENDED_MESSAGE:
1507
        switch (message[2]) {
1508
#ifdef SCSI2_SYNC
1509
        case EXTENDED_SDTR:
1510
            if (host->device[host->SCpnt->target].sync_state == SYNC_SENT_REQUEST) {
1511
                /*
1512
                 * We requested synchronous transfers.  This isn't quite right...
1513
                 * We can only say if this succeeded if we proceed on to execute the
1514
                 * command from this message.  If we get a MESSAGE PARITY ERROR,
1515
                 * and the target retries fail, then we fallback to asynchronous mode
1516
                 */
1517
                host->device[host->SCpnt->target].sync_state = SYNC_COMPLETED;
1518
                printk (KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1519
                        host->host->host_no, acornscsi_target(host),
1520
                        message[4], message[3] * 4);
1521
                host->device[host->SCpnt->target].sync_xfer =
1522
                        calc_sync_xfer (message[3] * 4, message[4]);
1523
            } else {
1524
                unsigned char period, length;
1525
                /*
1526
                 * Target requested synchronous transfers.  The agreement is only
1527
                 * to be in operation AFTER the target leaves message out phase.
1528
                 */
1529
                sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1530
                period = max (message[3], sdtr_period / 4);
1531
                length = min (message[4], sdtr_size);
1532
                msgqueue_addmsg (&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1533
                                 EXTENDED_SDTR, period, length);
1534
                host->device[host->SCpnt->target].sync_xfer =
1535
                        calc_sync_xfer (period * 4, length);
1536
            }
1537
            sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
1538
            break;
1539
#else
1540
            /* We do not accept synchronous transfers.  Respond with a
1541
             * MESSAGE_REJECT.
1542
             */
1543
#endif
1544
 
1545
        case EXTENDED_WDTR:
1546
            /* The WD33C93A is only 8-bit.  We respond with a MESSAGE_REJECT
1547
             * to a wide data transfer request.
1548
             */
1549
        default:
1550
            sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1551
            msgqueue_flush (&host->scsi.msgs);
1552
            msgqueue_addmsg (&host->scsi.msgs, 1, MESSAGE_REJECT);
1553
            break;
1554
        }
1555
        break;
1556
 
1557
#ifdef SCSI2_LINK
1558
    case LINKED_CMD_COMPLETE:
1559
    case LINKED_FLG_CMD_COMPLETE:
1560
        /*
1561
         * We don't support linked commands yet
1562
         */
1563
        if (0) {
1564
#if (DEBUG & DEBUG_LINK)
1565
            printk (KERN_DEBUG "scsi%d.%c: lun %d tag %d linked command complete\n",
1566
                    host->host->host_no, acornscsi_target(host), host->SCpnt->tag);
1567
#endif
1568
            /*
1569
             * A linked command should only terminate with one of these messages
1570
             * if there are more linked commands available.
1571
             */
1572
            if (!host->SCpnt->next_link) {
1573
                printk (KERN_WARNING "scsi%d.%c: lun %d tag %d linked command complete, but no next_link\n",
1574
                        instance->host_no, acornscsi_target (host), host->SCpnt->tag);
1575
                sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1576
                msgqueue_addmsg (&host->scsi.msgs, 1, ABORT);
1577
            } else {
1578
                Scsi_Cmnd *SCpnt = host->SCpnt;
1579
 
1580
                acornscsi_dma_cleanup (host);
1581
 
1582
                host->SCpnt = host->SCpnt->next_link;
1583
                host->SCpnt->tag = SCpnt->tag;
1584
                SCpnt->result = DID_OK | host->scsi.SCp.Message << 8 | host->Scsi.SCp.Status;
1585
                SCpnt->done (SCpnt);
1586
 
1587
                /* initialise host->SCpnt->SCp */
1588
            }
1589
            break;
1590
        }
1591
#endif
1592
 
1593
    default: /* reject message */
1594
        printk (KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1595
                host->host->host_no, acornscsi_target (host),
1596
                message[0]);
1597
        sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1598
        msgqueue_flush (&host->scsi.msgs);
1599
        msgqueue_addmsg (&host->scsi.msgs, 1, MESSAGE_REJECT);
1600
        host->scsi.phase = PHASE_MSGIN;
1601
        break;
1602
    }
1603
    sbic_arm_write (host->scsi.io_port, CMND, CMND_NEGATEACK);
1604
}
1605
 
1606
/*
1607
 * Function: int acornscsi_buildmessages (AS_Host *host)
1608
 * Purpose : build the connection messages for a host
1609
 * Params  : host - host to add messages to
1610
 */
1611
static
1612
void acornscsi_buildmessages (AS_Host *host)
1613
{
1614
#if 0
1615
    /* does the device need resetting? */
1616
    if (cmd_reset) {
1617
        msgqueue_addmsg (&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1618
        return;
1619
    }
1620
#endif
1621
 
1622
    msgqueue_addmsg (&host->scsi.msgs, 1,
1623
                     IDENTIFY(host->device[host->SCpnt->target].disconnect_ok,
1624
                             host->SCpnt->lun));
1625
 
1626
#if 0
1627
    /* does the device need the current command aborted */
1628
    if (cmd_aborted) {
1629
        acornscsi_abortcmd (host->SCpnt->tag);
1630
        return;
1631
    }
1632
#endif
1633
 
1634
#ifdef SCSI2_TAG
1635
    if (host->SCpnt->tag) {
1636
        unsigned int tag_type;
1637
 
1638
        if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
1639
            host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
1640
            host->SCpnt->cmnd[0] == INQUIRY)
1641
            tag_type = HEAD_OF_QUEUE_TAG;
1642
        else
1643
            tag_type = SIMPLE_QUEUE_TAG;
1644
        msgqueue_addmsg (&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
1645
    }
1646
#endif
1647
 
1648
#ifdef SCSI2_SYNC
1649
    if (host->device[host->SCpnt->target].sync_state == SYNC_ASYNCHRONOUS ||
1650
        host->device[host->SCpnt->target].sync_state == SYNC_NEGOCIATE) {
1651
        host->device[host->SCpnt->target].sync_state = SYNC_COMPLETED;
1652
        msgqueue_addmsg (&host->scsi.msgs, 5,
1653
                         EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1654
                         sdtr_period / 4, sdtr_size);
1655
    }
1656
#endif
1657
}
1658
 
1659
/*
1660
 * Function: int acornscsi_starttransfer (AS_Host *host)
1661
 * Purpose : transfer data to/from connected target
1662
 * Params  : host - host to which target is connected
1663
 * Returns : 0 if failure
1664
 */
1665
static
1666
int acornscsi_starttransfer (AS_Host *host)
1667
{
1668
    int residual;
1669
 
1670
    if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
1671
        printk (KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1672
                host->host->host_no, acornscsi_target (host));
1673
        return 0;
1674
    }
1675
 
1676
    residual = host->SCpnt->request_bufflen - host->scsi.SCp.have_data_in;
1677
 
1678
    sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
1679
    sbic_arm_writenext (host->scsi.io_port, residual >> 16);
1680
    sbic_arm_writenext (host->scsi.io_port, residual >> 8);
1681
    sbic_arm_writenext (host->scsi.io_port, residual);
1682
    sbic_arm_write (host->scsi.io_port, CMND, CMND_XFERINFO);
1683
    return 1;
1684
}
1685
 
1686
/* =========================================================================================
1687
 * Connection & Disconnection
1688
 */
1689
/*
1690
 * Function : acornscsi_reconnect (AS_Host *host)
1691
 * Purpose  : reconnect a previously disconnected command
1692
 * Params   : host - host specific data
1693
 * Remarks  : SCSI spec says:
1694
 *              'The set of active pointers is restored from the set
1695
 *               of saved pointers upon reconnection of the I/O process'
1696
 */
1697
static
1698
int acornscsi_reconnect (AS_Host *host)
1699
{
1700
    unsigned int target, lun, ok = 0;
1701
 
1702
    target = sbic_arm_read (host->scsi.io_port, SOURCEID);
1703
 
1704
    if (!(target & 8))
1705
        printk (KERN_ERR "scsi%d: invalid source id after reselection "
1706
                "- device fault?\n",
1707
                host->host->host_no);
1708
 
1709
    target &= 7;
1710
 
1711
    if (host->SCpnt && !host->scsi.disconnectable) {
1712
        printk (KERN_ERR "scsi%d.%d: reconnected while command in "
1713
                "progress to target %d?\n",
1714
                host->host->host_no, target, host->SCpnt->target);
1715
        host->SCpnt = NULL;
1716
    }
1717
 
1718
    lun = sbic_arm_read (host->scsi.io_port, DATA) & 7;
1719
 
1720
    host->scsi.reconnected.target = target;
1721
    host->scsi.reconnected.lun = lun;
1722
    host->scsi.reconnected.tag = 0;
1723
 
1724
    if (host->scsi.disconnectable && host->SCpnt &&
1725
        host->SCpnt->target == target && host->SCpnt->lun == lun)
1726
        ok = 1;
1727
 
1728
    if (!ok && queue_probetgtlun (&host->queues.disconnected, target, lun))
1729
        ok = 1;
1730
 
1731
    ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1732
 
1733
    if (ok) {
1734
        host->scsi.phase = PHASE_RECONNECTED;
1735
    } else {
1736
        printk (KERN_ERR "scsi%d.%c: reselected with no command "
1737
                "to reconnect with\n",
1738
                host->host->host_no, '0' + target);
1739
        acornscsi_dumplog (host, target);
1740
        sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1741
        msgqueue_addmsg (&host->scsi.msgs, 1, ABORT);
1742
        host->scsi.phase = PHASE_ABORTED;
1743
    }
1744
    sbic_arm_write (host->scsi.io_port, CMND, CMND_NEGATEACK);
1745
    return !ok;
1746
}
1747
 
1748
/*
1749
 * Function: int acornscsi_reconect_finish (AS_Host *host)
1750
 * Purpose : finish reconnecting a command
1751
 * Params  : host - host to complete
1752
 * Returns : 0 if failed
1753
 */
1754
static
1755
int acornscsi_reconnect_finish (AS_Host *host)
1756
{
1757
    if (host->scsi.disconnectable && host->SCpnt) {
1758
        host->scsi.disconnectable = 0;
1759
        if (host->SCpnt->target == host->scsi.reconnected.target &&
1760
            host->SCpnt->lun    == host->scsi.reconnected.lun &&
1761
            host->SCpnt->tag    == host->scsi.reconnected.tag) {
1762
#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1763
            DBG(host->SCpnt, printk ("scsi%d.%c: reconnected",
1764
                    host->host->host_no, acornscsi_target (host)));
1765
#endif
1766
        } else {
1767
            queue_add_cmd_tail (&host->queues.disconnected, host->SCpnt);
1768
#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1769
            DBG(host->SCpnt, printk ("scsi%d.%c: had to move command "
1770
                    "to disconnected queue\n",
1771
                    host->host->host_no, acornscsi_target (host)));
1772
#endif
1773
            host->SCpnt = NULL;
1774
        }
1775
    }
1776
    if (!host->SCpnt) {
1777
        host->SCpnt = queue_remove_tgtluntag (&host->queues.disconnected,
1778
                                host->scsi.reconnected.target,
1779
                                host->scsi.reconnected.lun,
1780
                                host->scsi.reconnected.tag);
1781
#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1782
        DBG(host->SCpnt, printk ("scsi%d.%c: had to get command",
1783
                host->host->host_no, acornscsi_target (host)));
1784
#endif
1785
    }
1786
    if (!host->SCpnt) {
1787
        acornscsi_abortcmd (host, host->scsi.reconnected.tag);
1788
        host->scsi.phase = PHASE_ABORTED;
1789
    } else {
1790
        /*
1791
         * Restore data pointer from SAVED pointers.
1792
         */
1793
        host->scsi.SCp = host->SCpnt->SCp;
1794
#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1795
        printk (", data pointers: [%p, %X]",
1796
                host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1797
#endif
1798
    }
1799
#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1800
    printk ("\n");
1801
#endif
1802
 
1803
    host->dma.transferred = host->scsi.SCp.have_data_in;
1804
 
1805
    return host->SCpnt != NULL;
1806
}
1807
 
1808
/*
1809
 * Function: void acornscsi_disconnect_unexpected (AS_Host *host)
1810
 * Purpose : handle an unexpected disconnect
1811
 * Params  : host - host on which disconnect occurred
1812
 */
1813
static
1814
void acornscsi_disconnect_unexpected (AS_Host *host)
1815
{
1816
    printk (KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1817
            host->host->host_no, acornscsi_target (host));
1818
#if (DEBUG & DEBUG_ABORT)
1819
    acornscsi_dumplog (host, 8);
1820
#endif
1821
 
1822
    acornscsi_done (host, &host->SCpnt, DID_ABORT);
1823
}
1824
 
1825
/*
1826
 * Function: void acornscsi_abortcmd (AS_host *host, unsigned char tag)
1827
 * Purpose : abort a currently executing command
1828
 * Params  : host - host with connected command to abort
1829
 *           tag  - tag to abort
1830
 */
1831
static
1832
void acornscsi_abortcmd (AS_Host *host, unsigned char tag)
1833
{
1834
    sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
1835
 
1836
    msgqueue_flush (&host->scsi.msgs);
1837
#ifdef SCSI2_TAG
1838
    if (tag)
1839
        msgqueue_addmsg (&host->scsi.msgs, 2, ABORT_TAG, tag);
1840
    else
1841
#endif
1842
        msgqueue_addmsg (&host->scsi.msgs, 1, ABORT);
1843
}
1844
 
1845
/* ==========================================================================================
1846
 * Interrupt routines.
1847
 */
1848
/*
1849
 * Function: int acornscsi_sbicintr (AS_Host *host)
1850
 * Purpose : handle interrupts from SCSI device
1851
 * Params  : host - host to process
1852
 * Returns : INTR_PROCESS if expecting another SBIC interrupt
1853
 *           INTR_IDLE if no interrupt
1854
 *           INTR_NEXT_COMMAND if we have finished processing the command
1855
 */
1856
static
1857
intr_ret_t acornscsi_sbicintr (AS_Host *host, int in_irq)
1858
{
1859
    unsigned int asr, ssr;
1860
 
1861
    asr = sbic_arm_read (host->scsi.io_port, ASR);
1862
    if (!(asr & ASR_INT))
1863
        return INTR_IDLE;
1864
 
1865
    ssr = sbic_arm_read (host->scsi.io_port, SSR);
1866
 
1867
#if (DEBUG & DEBUG_PHASES)
1868
    print_sbic_status(asr, ssr, host->scsi.phase);
1869
#endif
1870
 
1871
    ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1872
 
1873
    if (host->SCpnt && !host->scsi.disconnectable)
1874
        ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, in_irq);
1875
 
1876
    switch (ssr) {
1877
    case 0x00:                          /* reset state - not advanced                   */
1878
        printk (KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1879
                host->host->host_no);
1880
        /* setup sbic - WD33C93A */
1881
        sbic_arm_write (host->scsi.io_port, OWNID, OWNID_EAF | host->host->this_id);
1882
        sbic_arm_write (host->scsi.io_port, CMND, CMND_RESET);
1883
        return INTR_IDLE;
1884
 
1885
    case 0x01:                          /* reset state - advanced                       */
1886
        sbic_arm_write (host->scsi.io_port, CTRL, INIT_SBICDMA | CTRL_IDI);
1887
        sbic_arm_write (host->scsi.io_port, TIMEOUT, TIMEOUT_TIME);
1888
        sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1889
        sbic_arm_write (host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1890
        msgqueue_flush (&host->scsi.msgs);
1891
        return INTR_IDLE;
1892
 
1893
    case 0x41:                          /* unexpected disconnect aborted command        */
1894
        acornscsi_disconnect_unexpected (host);
1895
        return INTR_NEXT_COMMAND;
1896
    }
1897
 
1898
    switch (host->scsi.phase) {
1899
    case PHASE_CONNECTING:              /* STATE: command removed from issue queue      */
1900
        switch (ssr) {
1901
        case 0x11:                      /* -> PHASE_CONNECTED                           */
1902
            /* BUS FREE -> SELECTION */
1903
            host->scsi.phase = PHASE_CONNECTED;
1904
            msgqueue_flush (&host->scsi.msgs);
1905
            host->dma.transferred = host->scsi.SCp.have_data_in;
1906
            /* 33C93 gives next interrupt indicating bus phase */
1907
            asr = sbic_arm_read (host->scsi.io_port, ASR);
1908
            if (!(asr & ASR_INT))
1909
                break;
1910
            ssr = sbic_arm_read (host->scsi.io_port, SSR);
1911
            ADD_STATUS(8, ssr, host->scsi.phase, 1);
1912
            ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, 1);
1913
            goto connected;
1914
 
1915
        case 0x42:                      /* select timed out                             */
1916
                                        /* -> PHASE_IDLE                                */
1917
            acornscsi_done (host, &host->SCpnt, DID_NO_CONNECT);
1918
            return INTR_NEXT_COMMAND;
1919
 
1920
        case 0x81:                      /* -> PHASE_RECONNECTED or PHASE_ABORTED        */
1921
            /* BUS FREE -> RESELECTION */
1922
            host->origSCpnt = host->SCpnt;
1923
            host->SCpnt = NULL;
1924
            msgqueue_flush (&host->scsi.msgs);
1925
            acornscsi_reconnect (host);
1926
            break;
1927
 
1928
        default:
1929
            printk (KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
1930
                    host->host->host_no, acornscsi_target (host), ssr);
1931
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
1932
            acornscsi_abortcmd (host, host->SCpnt->tag);
1933
        }
1934
        return INTR_PROCESSING;
1935
 
1936
    connected:
1937
    case PHASE_CONNECTED:               /* STATE: device selected ok                    */
1938
        switch (ssr) {
1939
#ifdef NONSTANDARD
1940
        case 0x8a:                      /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED        */
1941
            /* SELECTION -> COMMAND */
1942
            acornscsi_sendcommand (host);
1943
            break;
1944
 
1945
        case 0x8b:                      /* -> PHASE_STATUS                              */
1946
            /* SELECTION -> STATUS */
1947
            acornscsi_readstatusbyte (host);
1948
            break;
1949
#endif
1950
 
1951
        case 0x8e:                      /* -> PHASE_MSGOUT                              */
1952
            /* SELECTION ->MESSAGE OUT */
1953
            host->scsi.phase = PHASE_MSGOUT;
1954
            acornscsi_buildmessages (host);
1955
            acornscsi_sendmessage (host);
1956
            break;
1957
 
1958
        /* these should not happen */
1959
        case 0x85:                      /* target disconnected                          */
1960
            acornscsi_done (host, &host->SCpnt, DID_ERROR);
1961
            break;
1962
 
1963
        default:
1964
            printk (KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
1965
                    host->host->host_no, acornscsi_target (host), ssr);
1966
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
1967
            acornscsi_abortcmd (host, host->SCpnt->tag);
1968
        }
1969
        return INTR_PROCESSING;
1970
 
1971
    case PHASE_MSGOUT:                  /* STATE: connected & sent IDENTIFY message     */
1972
        /*
1973
         * SCSI standard says th at a MESSAGE OUT phases can be followed by a DATA phase
1974
         */
1975
        switch (ssr) {
1976
        case 0x8a:
1977
        case 0x1a:                      /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED        */
1978
            /* MESSAGE OUT -> COMMAND */
1979
            acornscsi_sendcommand (host);
1980
            break;
1981
 
1982
        case 0x1b:                      /* -> PHASE_STATUS                              */
1983
            /* MESSAGE OUT -> STATUS */
1984
            acornscsi_readstatusbyte (host);
1985
            break;
1986
 
1987
        case 0x4f:
1988
        case 0x1f:                      /* -> PHASE_MSGIN, PHASE_DISCONNECT             */
1989
            /* MESSAGE OUT -> MESSAGE IN */
1990
            acornscsi_message (host);
1991
            break;
1992
 
1993
        default:
1994
            printk (KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
1995
                    host->host->host_no, acornscsi_target (host), ssr);
1996
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
1997
        }
1998
        return INTR_PROCESSING;
1999
 
2000
    case PHASE_COMMAND:                 /* STATE: connected & command sent              */
2001
        switch (ssr) {
2002
        case 0x18:                      /* -> PHASE_DATAOUT                             */
2003
            /* COMMAND -> DATA OUT */
2004
            if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2005
                acornscsi_abortcmd (host, host->SCpnt->tag);
2006
            acornscsi_dma_setup (host, DMA_OUT);
2007
            if (!acornscsi_starttransfer (host))
2008
                acornscsi_abortcmd (host, host->SCpnt->tag);
2009
            host->scsi.phase = PHASE_DATAOUT;
2010
            return INTR_IDLE;
2011
 
2012
        case 0x19:                      /* -> PHASE_DATAIN                              */
2013
            /* COMMAND -> DATA IN */
2014
            if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2015
                acornscsi_abortcmd (host, host->SCpnt->tag);
2016
            acornscsi_dma_setup (host, DMA_IN);
2017
            if (!acornscsi_starttransfer (host))
2018
                acornscsi_abortcmd (host, host->SCpnt->tag);
2019
            host->scsi.phase = PHASE_DATAIN;
2020
            return INTR_IDLE;
2021
 
2022
        case 0x1b:                      /* -> PHASE_STATUS                              */
2023
            /* COMMAND -> STATUS */
2024
            acornscsi_readstatusbyte (host);
2025
            break;
2026
 
2027
        case 0x1e:                      /* -> PHASE_MSGOUT                              */
2028
            /* COMMAND -> MESSAGE OUT */
2029
            acornscsi_sendmessage (host);
2030
            break;
2031
 
2032
        case 0x1f:                      /* -> PHASE_MSGIN, PHASE_DISCONNECT             */
2033
            /* COMMAND -> MESSAGE IN */
2034
            acornscsi_message (host);
2035
            break;
2036
 
2037
        default:
2038
            printk (KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2039
                    host->host->host_no, acornscsi_target (host), ssr);
2040
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2041
        }
2042
        return INTR_PROCESSING;
2043
 
2044
    case PHASE_DISCONNECT:              /* STATE: connected, received DISCONNECT msg    */
2045
        if (ssr == 0x85) {              /* -> PHASE_IDLE                                */
2046
            host->scsi.disconnectable = 1;
2047
            host->scsi.reconnected.tag = 0;
2048
            host->scsi.phase = PHASE_IDLE;
2049
            host->stats.disconnects += 1;
2050
        } else {
2051
            printk (KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2052
                    host->host->host_no, acornscsi_target (host), ssr);
2053
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2054
        }
2055
        return INTR_NEXT_COMMAND;
2056
 
2057
    case PHASE_IDLE:                    /* STATE: disconnected                          */
2058
        if (ssr == 0x81)                /* -> PHASE_RECONNECTED or PHASE_ABORTED        */
2059
            acornscsi_reconnect (host);
2060
        else {
2061
            printk (KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2062
                    host->host->host_no, acornscsi_target (host), ssr);
2063
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2064
        }
2065
        return INTR_PROCESSING;
2066
 
2067
    case PHASE_RECONNECTED:             /* STATE: device reconnected to initiator       */
2068
        /*
2069
         * Command reconnected - if MESGIN, get message - it may be
2070
         * the tag.  If not, get command out of disconnected queue
2071
         */
2072
        /*
2073
         * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
2074
         * reconnect I_T_L command
2075
         */
2076
        if (ssr != 0x8f && !acornscsi_reconnect_finish (host))
2077
            return INTR_IDLE;
2078
        ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, in_irq);
2079
        switch (ssr) {
2080
        case 0x88:                      /* data out phase                               */
2081
                                        /* -> PHASE_DATAOUT                             */
2082
            /* MESSAGE IN -> DATA OUT */
2083
            acornscsi_dma_setup (host, DMA_OUT);
2084
            if (!acornscsi_starttransfer (host))
2085
                acornscsi_abortcmd (host, host->SCpnt->tag);
2086
            host->scsi.phase = PHASE_DATAOUT;
2087
            return INTR_IDLE;
2088
 
2089
        case 0x89:                      /* data in phase                                */
2090
                                        /* -> PHASE_DATAIN                              */
2091
            /* MESSAGE IN -> DATA IN */
2092
            acornscsi_dma_setup (host, DMA_IN);
2093
            if (!acornscsi_starttransfer (host))
2094
                acornscsi_abortcmd (host, host->SCpnt->tag);
2095
            host->scsi.phase = PHASE_DATAIN;
2096
            return INTR_IDLE;
2097
 
2098
        case 0x8a:                      /* command out                                  */
2099
            /* MESSAGE IN -> COMMAND */
2100
            acornscsi_sendcommand (host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED       */
2101
            break;
2102
 
2103
        case 0x8b:                      /* status in                                    */
2104
                                        /* -> PHASE_STATUSIN                            */
2105
            /* MESSAGE IN -> STATUS */
2106
            acornscsi_readstatusbyte (host);
2107
            break;
2108
 
2109
        case 0x8e:                      /* message out                                  */
2110
                                        /* -> PHASE_MSGOUT                              */
2111
            /* MESSAGE IN -> MESSAGE OUT */
2112
            acornscsi_sendmessage (host);
2113
            break;
2114
 
2115
        case 0x8f:                      /* message in                                   */
2116
            acornscsi_message (host);   /* -> PHASE_MSGIN, PHASE_DISCONNECT             */
2117
            break;
2118
 
2119
        default:
2120
            printk (KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2121
                    host->host->host_no, acornscsi_target (host), ssr);
2122
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2123
        }
2124
        return INTR_PROCESSING;
2125
 
2126
    case PHASE_DATAIN:                  /* STATE: transferred data in                   */
2127
        /*
2128
         * This is simple - if we disconnect then the DMA address & count is
2129
         * correct.
2130
         */
2131
        switch (ssr) {
2132
        case 0x19:                      /* -> PHASE_DATAIN                              */
2133
            acornscsi_abortcmd (host, host->SCpnt->tag);
2134
            return INTR_IDLE;
2135
 
2136
        case 0x4b:                      /* -> PHASE_STATUSIN                            */
2137
        case 0x1b:                      /* -> PHASE_STATUSIN                            */
2138
            /* DATA IN -> STATUS */
2139
            host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
2140
                                          acornscsi_sbic_xfcount (host);
2141
            acornscsi_dma_stop (host);
2142
            acornscsi_readstatusbyte (host);
2143
            break;
2144
 
2145
        case 0x1e:                      /* -> PHASE_MSGOUT                              */
2146
        case 0x4e:                      /* -> PHASE_MSGOUT                              */
2147
            /* DATA IN -> MESSAGE OUT */
2148
            host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
2149
                                          acornscsi_sbic_xfcount (host);
2150
            acornscsi_dma_stop (host);
2151
            acornscsi_sendmessage (host);
2152
            break;
2153
 
2154
        case 0x1f:                      /* message in                                   */
2155
        case 0x4f:                      /* message in                                   */
2156
            /* DATA IN -> MESSAGE IN */
2157
            host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
2158
                                          acornscsi_sbic_xfcount (host);
2159
            acornscsi_dma_stop (host);
2160
            acornscsi_message (host);   /* -> PHASE_MSGIN, PHASE_DISCONNECT             */
2161
            break;
2162
 
2163
        default:
2164
            printk (KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2165
                    host->host->host_no, acornscsi_target (host), ssr);
2166
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2167
        }
2168
        return INTR_PROCESSING;
2169
 
2170
    case PHASE_DATAOUT:                 /* STATE: transferred data out                  */
2171
        /*
2172
         * This is more complicated - if we disconnect, the DMA could be 12
2173
         * bytes ahead of us.  We need to correct this.
2174
         */
2175
        switch (ssr) {
2176
        case 0x18:                      /* -> PHASE_DATAOUT                             */
2177
            acornscsi_abortcmd (host, host->SCpnt->tag);
2178
            return INTR_IDLE;
2179
 
2180
        case 0x4b:                      /* -> PHASE_STATUSIN                            */
2181
        case 0x1b:                      /* -> PHASE_STATUSIN                            */
2182
            /* DATA OUT -> STATUS */
2183
            host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
2184
                                          acornscsi_sbic_xfcount (host);
2185
            acornscsi_dma_stop (host);
2186
            acornscsi_dma_adjust (host);
2187
            acornscsi_readstatusbyte (host);
2188
            break;
2189
 
2190
        case 0x1e:                      /* -> PHASE_MSGOUT                              */
2191
        case 0x4e:                      /* -> PHASE_MSGOUT                              */
2192
            /* DATA OUT -> MESSAGE OUT */
2193
            host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
2194
                                          acornscsi_sbic_xfcount (host);
2195
            acornscsi_dma_stop (host);
2196
            acornscsi_dma_adjust (host);
2197
            acornscsi_sendmessage (host);
2198
            break;
2199
 
2200
        case 0x1f:                      /* message in                                   */
2201
        case 0x4f:                      /* message in                                   */
2202
            /* DATA OUT -> MESSAGE IN */
2203
            host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
2204
                                          acornscsi_sbic_xfcount (host);
2205
            acornscsi_dma_stop (host);
2206
            acornscsi_dma_adjust (host);
2207
            acornscsi_message (host);   /* -> PHASE_MSGIN, PHASE_DISCONNECT             */
2208
            break;
2209
 
2210
        default:
2211
            printk (KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2212
                    host->host->host_no, acornscsi_target (host), ssr);
2213
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2214
        }
2215
        return INTR_PROCESSING;
2216
 
2217
    case PHASE_STATUSIN:                /* STATE: status in complete                    */
2218
        if (ssr == 0x1f)                /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2219
            /* STATUS -> MESSAGE IN */
2220
            acornscsi_message (host);
2221
        else if (ssr == 0x1e)           /* -> PHASE_MSGOUT                              */
2222
            /* STATUS -> MESSAGE OUT */
2223
            acornscsi_sendmessage (host);
2224
        else {
2225
            printk (KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2226
                    host->host->host_no, acornscsi_target (host), ssr);
2227
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2228
        }
2229
        return INTR_PROCESSING;
2230
 
2231
    case PHASE_MSGIN:                   /* STATE: message in                            */
2232
        switch (ssr) {
2233
        case 0x1e:                      /* -> PHASE_MSGOUT                              */
2234
        case 0x4e:                      /* -> PHASE_MSGOUT                              */
2235
            /* MESSAGE IN -> MESSAGE OUT */
2236
            acornscsi_sendmessage (host);
2237
            break;
2238
 
2239
        case 0x1f:                      /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2240
        case 0x2f:
2241
        case 0x4f:
2242
        case 0x8f:
2243
            acornscsi_message (host);
2244
            break;
2245
 
2246
        default:
2247
            printk (KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2248
                    host->host->host_no, acornscsi_target (host), ssr);
2249
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2250
        }
2251
        return INTR_PROCESSING;
2252
 
2253
    case PHASE_DONE:                    /* STATE: received status & message             */
2254
        switch (ssr) {
2255
        case 0x85:                      /* -> PHASE_IDLE                                */
2256
            acornscsi_done (host, &host->SCpnt, DID_OK);
2257
            return INTR_NEXT_COMMAND;
2258
 
2259
        case 0x8e:
2260
            acornscsi_sendmessage (host);
2261
            break;
2262
 
2263
        default:
2264
            printk (KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2265
                    host->host->host_no, acornscsi_target (host), ssr);
2266
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2267
        }
2268
        return INTR_PROCESSING;
2269
 
2270
    case PHASE_ABORTED:
2271
        switch (ssr) {
2272
        case 0x85:
2273
            acornscsi_done (host, &host->SCpnt, DID_ABORT);
2274
            return INTR_NEXT_COMMAND;
2275
 
2276
        case 0x1e:
2277
        case 0x2e:
2278
        case 0x4e:
2279
        case 0x8e:
2280
            acornscsi_sendmessage (host);
2281
            break;
2282
 
2283
        default:
2284
            printk (KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2285
                    host->host->host_no, acornscsi_target (host), ssr);
2286
            acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2287
        }
2288
        return INTR_PROCESSING;
2289
 
2290
    default:
2291
        printk (KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2292
                host->host->host_no, acornscsi_target (host), ssr);
2293
        acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
2294
    }
2295
    return INTR_PROCESSING;
2296
}
2297
 
2298
/*
2299
 * Prototype: void acornscsi_intr (int irq, void *dev_id, struct pt_regs *regs)
2300
 * Purpose  : handle interrupts from Acorn SCSI card
2301
 * Params   : irq    - interrupt number
2302
 *            dev_id - device specific data (AS_Host structure)
2303
 *            regs   - processor registers when interrupt occurred
2304
 */
2305
static
2306
void acornscsi_intr (int irq, void *dev_id, struct pt_regs *regs)
2307
{
2308
    AS_Host *host = (AS_Host *)dev_id;
2309
    intr_ret_t ret;
2310
    int iostatus;
2311
    int in_irq = 0;
2312
 
2313
    if (host->scsi.interrupt)
2314
        printk ("scsi%d: interrupt re-entered\n", host->host->host_no);
2315
    host->scsi.interrupt = 1;
2316
 
2317
    do {
2318
        ret = INTR_IDLE;
2319
 
2320
        iostatus = inb (host->card.io_intr);
2321
 
2322
        if (iostatus & 2) {
2323
            acornscsi_dma_intr (host);
2324
            iostatus = inb (host->card.io_intr);
2325
        }
2326
 
2327
        if (iostatus & 8)
2328
            ret = acornscsi_sbicintr (host, in_irq);
2329
 
2330
        /*
2331
         * If we have a transfer pending, start it.
2332
         * Only start it if the interface has already started transferring
2333
         * it's data
2334
         */
2335
        if (host->dma.xfer_required)
2336
            acornscsi_dma_xfer (host);
2337
 
2338
        if (ret == INTR_NEXT_COMMAND)
2339
            ret = acornscsi_kick (host);
2340
 
2341
        in_irq = 1;
2342
    } while (ret != INTR_IDLE);
2343
 
2344
    host->scsi.interrupt = 0;
2345
}
2346
 
2347
/*=============================================================================================
2348
 * Interfaces between interrupt handler and rest of scsi code
2349
 */
2350
 
2351
/*
2352
 * Function : acornscsi_queuecmd (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
2353
 * Purpose  : queues a SCSI command
2354
 * Params   : cmd  - SCSI command
2355
 *            done - function called on completion, with pointer to command descriptor
2356
 * Returns  : 0, or < 0 on error.
2357
 */
2358
int acornscsi_queuecmd (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2359
{
2360
    AS_Host *host = (AS_Host *)SCpnt->host->hostdata;
2361
 
2362
    if (!done) {
2363
        /* there should be some way of rejecting errors like this without panicing... */
2364
        panic ("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2365
                SCpnt->host->host_no, SCpnt);
2366
        return -EINVAL;
2367
    }
2368
 
2369
#if (DEBUG & DEBUG_NO_WRITE)
2370
    if (acornscsi_cmdtype (SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->target))) {
2371
        printk (KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2372
            SCpnt->host->host_no, '0' + SCpnt->target);
2373
        SCpnt->result = DID_NO_CONNECT << 16;
2374
        done (SCpnt);
2375
        return 0;
2376
    }
2377
#endif
2378
 
2379
    SCpnt->scsi_done = done;
2380
    SCpnt->host_scribble = NULL;
2381
    SCpnt->result = 0;
2382
    SCpnt->tag = 0;
2383
    SCpnt->SCp.phase = (int)acornscsi_datadirection (SCpnt->cmnd[0]);
2384
    SCpnt->SCp.sent_command = 0;
2385
    SCpnt->SCp.have_data_in = 0;
2386
    SCpnt->SCp.Status = 0;
2387
    SCpnt->SCp.Message = 0;
2388
 
2389
    if (SCpnt->use_sg) {
2390
        SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->buffer;
2391
        SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
2392
        SCpnt->SCp.ptr = (char *) SCpnt->SCp.buffer->address;
2393
        SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
2394
    } else {
2395
        SCpnt->SCp.buffer = NULL;
2396
        SCpnt->SCp.buffers_residual = 0;
2397
        SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
2398
        SCpnt->SCp.this_residual = SCpnt->request_bufflen;
2399
    }
2400
 
2401
    host->stats.queues += 1;
2402
 
2403
    {
2404
        unsigned long flags;
2405
 
2406
        if (!queue_add_cmd_ordered (&host->queues.issue, SCpnt)) {
2407
            SCpnt->result = DID_ERROR << 16;
2408
            done (SCpnt);
2409
            return 0;
2410
        }
2411
        save_flags_cli (flags);
2412
        if (host->scsi.phase == PHASE_IDLE)
2413
            acornscsi_kick (host);
2414
        restore_flags (flags);
2415
    }
2416
    return 0;
2417
}
2418
 
2419
/*
2420
 * Prototype: void acornscsi_reportstatus (Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
2421
 * Purpose  : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
2422
 * Params   : SCpntp1 - pointer to command to return
2423
 *            SCpntp2 - pointer to command to check
2424
 *            result  - result to pass back to mid-level done function
2425
 * Returns  : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
2426
 */
2427
static inline
2428
void acornscsi_reportstatus (Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
2429
{
2430
    Scsi_Cmnd *SCpnt = *SCpntp1;
2431
 
2432
    if (SCpnt) {
2433
        *SCpntp1 = NULL;
2434
 
2435
        SCpnt->result = result;
2436
        SCpnt->scsi_done (SCpnt);
2437
    }
2438
 
2439
    if (SCpnt == *SCpntp2)
2440
        *SCpntp2 = NULL;
2441
}
2442
 
2443
/*
2444
 * Prototype: int acornscsi_abort (Scsi_Cmnd *SCpnt)
2445
 * Purpose  : abort a command on this host
2446
 * Params   : SCpnt - command to abort
2447
 * Returns  : one of SCSI_ABORT_ macros
2448
 */
2449
int acornscsi_abort (Scsi_Cmnd *SCpnt)
2450
{
2451
    AS_Host *host = (AS_Host *) SCpnt->host->hostdata;
2452
    int result = SCSI_ABORT_NOT_RUNNING;
2453
 
2454
    host->stats.aborts += 1;
2455
 
2456
#if (DEBUG & DEBUG_ABORT)
2457
    {
2458
        int asr, ssr;
2459
        asr = sbic_arm_read (host->scsi.io_port, ASR);
2460
        ssr = sbic_arm_read (host->scsi.io_port, SSR);
2461
 
2462
        printk (KERN_WARNING "acornscsi_abort: ");
2463
        print_sbic_status(asr, ssr, host->scsi.phase);
2464
        acornscsi_dumplog (host, SCpnt->target);
2465
    }
2466
#endif
2467
 
2468
    if (queue_removecmd (&host->queues.issue, SCpnt)) {
2469
        SCpnt->result = DID_ABORT << 16;
2470
        SCpnt->scsi_done (SCpnt);
2471
#if (DEBUG & DEBUG_ABORT)
2472
        printk ("scsi%d: command on issue queue\n", host->host->host_no);
2473
#endif
2474
        result = SCSI_ABORT_SUCCESS;
2475
    } else if (queue_cmdonqueue (&host->queues.disconnected, SCpnt)) {
2476
        printk ("scsi%d: command on disconnected queue\n", host->host->host_no);
2477
        result = SCSI_ABORT_SNOOZE;
2478
    } else if (host->SCpnt == SCpnt) {
2479
        acornscsi_abortcmd (host, host->SCpnt->tag);
2480
        printk ("scsi%d: command executing\n", host->host->host_no);
2481
        result = SCSI_ABORT_SNOOZE;
2482
    } else if (host->origSCpnt == SCpnt) {
2483
        host->origSCpnt = NULL;
2484
        SCpnt->result = DID_ABORT << 16;
2485
        SCpnt->scsi_done (SCpnt);
2486
#if (DEBUG & DEBUG_ABORT)
2487
        printk ("scsi%d: command waiting for execution\n", host->host->host_no);
2488
#endif
2489
        result = SCSI_ABORT_SUCCESS;
2490
    }
2491
 
2492
    if (result == SCSI_ABORT_NOT_RUNNING) {
2493
        printk ("scsi%d: abort(): command not running\n", host->host->host_no);
2494
        acornscsi_dumplog (host, SCpnt->target);
2495
#if (DEBUG & DEBUG_ABORT)
2496
        result = SCSI_ABORT_SNOOZE;
2497
#endif
2498
    }
2499
    return result;
2500
}
2501
 
2502
/*
2503
 * Prototype: int acornscsi_reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
2504
 * Purpose  : reset a command on this host/reset this host
2505
 * Params   : SCpnt  - command causing reset
2506
 *            result - what type of reset to perform
2507
 * Returns  : one of SCSI_RESET_ macros
2508
 */
2509
int acornscsi_reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
2510
{
2511
    AS_Host *host = (AS_Host *)SCpnt->host->hostdata;
2512
    Scsi_Cmnd *SCptr;
2513
 
2514
    host->stats.resets += 1;
2515
 
2516
#if (DEBUG & DEBUG_RESET)
2517
    {
2518
        int asr, ssr;
2519
 
2520
        asr = sbic_arm_read (host->scsi.io_port, ASR);
2521
        ssr = sbic_arm_read (host->scsi.io_port, SSR);
2522
 
2523
        printk (KERN_WARNING "acornscsi_reset: ");
2524
        print_sbic_status(asr, ssr, host->scsi.phase);
2525
        acornscsi_dumplog (host, SCpnt->target);
2526
    }
2527
#endif
2528
 
2529
    acornscsi_dma_stop (host);
2530
 
2531
    SCptr = host->SCpnt;
2532
 
2533
    /*
2534
     * do hard reset.  This resets all devices on this host, and so we
2535
     * must set the reset status on all commands.
2536
     */
2537
    acornscsi_resetcard (host);
2538
 
2539
    /*
2540
     * report reset on commands current connected/disconnected
2541
     */
2542
    acornscsi_reportstatus (&host->SCpnt, &SCptr, DID_RESET);
2543
 
2544
    while ((SCptr = queue_remove (&host->queues.disconnected)) != NULL)
2545
        acornscsi_reportstatus (&SCptr, &SCpnt, DID_RESET);
2546
 
2547
    if (SCpnt) {
2548
        SCpnt->result = DID_RESET << 16;
2549
        SCpnt->scsi_done (SCpnt);
2550
    }
2551
 
2552
    return SCSI_RESET_BUS_RESET | SCSI_RESET_HOST_RESET | SCSI_RESET_SUCCESS;
2553
}
2554
 
2555
/*==============================================================================================
2556
 * initialisation & miscellaneous support
2557
 */
2558
static struct expansion_card *ecs[MAX_ECARDS];
2559
 
2560
/*
2561
 * Prototype: void acornscsi_init (AS_Host *host)
2562
 * Purpose  : initialise the AS_Host structure for one interface & setup hardware
2563
 * Params   : host - host to setup
2564
 */
2565
static
2566
void acornscsi_init (AS_Host *host)
2567
{
2568
    memset (&host->stats, 0, sizeof (host->stats));
2569
    queue_initialise (&host->queues.issue);
2570
    queue_initialise (&host->queues.disconnected);
2571
    msgqueue_initialise (&host->scsi.msgs);
2572
 
2573
    acornscsi_resetcard (host);
2574
}
2575
 
2576
int acornscsi_detect(Scsi_Host_Template * tpnt)
2577
{
2578
    static const card_ids acornscsi_cids[] = { ACORNSCSI_LIST, { 0xffff, 0xffff } };
2579
    int i, count = 0;
2580
    struct Scsi_Host *instance;
2581
    AS_Host *host;
2582
 
2583
    tpnt->proc_dir = &proc_scsi_acornscsi;
2584
 
2585
    for (i = 0; i < MAX_ECARDS; i++)
2586
        ecs[i] = NULL;
2587
 
2588
    ecard_startfind ();
2589
 
2590
    while(1) {
2591
        ecs[count] = ecard_find(0, acornscsi_cids);
2592
        if (!ecs[count])
2593
            break;
2594
 
2595
        if (ecs[count]->irq == 0xff) {
2596
            printk ("scsi: WD33C93 does not have IRQ enabled - ignoring\n");
2597
            continue;
2598
        }
2599
 
2600
        ecard_claim(ecs[count]); /* Must claim here - card produces irq on reset */
2601
 
2602
        instance = scsi_register (tpnt, sizeof(AS_Host));
2603
        host = (AS_Host *)instance->hostdata;
2604
 
2605
        instance->io_port = ecard_address (ecs[count], ECARD_MEMC, 0);
2606
        instance->irq = ecs[count]->irq;
2607
 
2608
        host->host              = instance;
2609
        host->scsi.io_port      = ioaddr (instance->io_port + 0x800);
2610
        host->scsi.irq          = instance->irq;
2611
        host->card.io_intr      = POD_SPACE(instance->io_port) + 0x800;
2612
        host->card.io_page      = POD_SPACE(instance->io_port) + 0xc00;
2613
        host->card.io_ram       = ioaddr (instance->io_port);
2614
        host->dma.io_port       = instance->io_port + 0xc00;
2615
        host->dma.io_intr_clear = POD_SPACE(instance->io_port) + 0x800;
2616
 
2617
        request_region (instance->io_port + 0x800,  2, "acornscsi(sbic)");
2618
        request_region (host->card.io_intr,  1, "acornscsi(intr)");
2619
        request_region (host->card.io_page,  1, "acornscsi(page)");
2620
        request_region (host->dma.io_port, 256, "acornscsi(dmac)");
2621
        request_region (instance->io_port, 2048, "acornscsi(ram)");
2622
 
2623
        if (request_irq(host->scsi.irq, acornscsi_intr, SA_INTERRUPT, "acornscsi", host)) {
2624
            printk(KERN_CRIT "scsi%d: IRQ%d not free, interrupts disabled\n",
2625
                instance->host_no, host->scsi.irq);
2626
            host->scsi.irq = NO_IRQ;
2627
        }
2628
 
2629
        acornscsi_init (host);
2630
 
2631
        ++count;
2632
    }
2633
    return count;
2634
}
2635
 
2636
/*
2637
 * Function: int acornscsi_release (struct Scsi_Host *host)
2638
 * Purpose : release all resources used by this adapter
2639
 * Params  : host - driver structure to release
2640
 * Returns : nothing of any consequence
2641
 */
2642
int acornscsi_release (struct Scsi_Host *instance)
2643
{
2644
    AS_Host *host = (AS_Host *)instance->hostdata;
2645
    int i;
2646
 
2647
    /*
2648
     * Put card into RESET state
2649
     */
2650
    outb (0x80, host->card.io_page);
2651
 
2652
    if (host->scsi.irq != NO_IRQ)
2653
        free_irq (host->scsi.irq, host);
2654
 
2655
    release_region (instance->io_port + 0x800, 2);
2656
    release_region (host->card.io_intr, 1);
2657
    release_region (host->card.io_page, 1);
2658
    release_region (host->dma.io_port, 256);
2659
    release_region (instance->io_port, 2048);
2660
 
2661
    for (i = 0; i < MAX_ECARDS; i++)
2662
        if (ecs[i] && instance->io_port == ecard_address (ecs[i], ECARD_MEMC, 0))
2663
            ecard_release (ecs[i]);
2664
 
2665
    msgqueue_free (&host->scsi.msgs);
2666
    queue_free (&host->queues.disconnected);
2667
    queue_free (&host->queues.issue);
2668
 
2669
    return 0;
2670
}
2671
 
2672
/*
2673
 * Function: char *acornscsi_info (struct Scsi_Host *host)
2674
 * Purpose : return a string describing this interface
2675
 * Params  : host - host to give information on
2676
 * Returns : a constant string
2677
 */
2678
const
2679
char *acornscsi_info(struct Scsi_Host *host)
2680
{
2681
    static char string[100], *p;
2682
 
2683
    p = string;
2684
 
2685
    p += sprintf (string, "%s at port %X irq %d v%d.%d.%d"
2686
#ifdef SCSI2_SYNC
2687
    " SYNC"
2688
#endif
2689
#ifdef SCSI2_TAG
2690
    " TAG"
2691
#endif
2692
#ifdef SCSI2_LINK
2693
    " LINK"
2694
#endif
2695
#if (DEBUG & DEBUG_NO_WRITE)
2696
    " NOWRITE ("NO_WRITE_STR")"
2697
#endif
2698
                , host->hostt->name, host->io_port, host->irq,
2699
                VER_MAJOR, VER_MINOR, VER_PATCH);
2700
    return string;
2701
}
2702
 
2703
int acornscsi_proc_info(char *buffer, char **start, off_t offset,
2704
                        int length, int host_no, int inout)
2705
{
2706
    int pos, begin = 0, first;
2707
    struct Scsi_Host *instance = scsi_hostlist;
2708
    Scsi_Device *scd = scsi_devices;
2709
    AS_Host *host;
2710
    char *p = buffer;
2711
 
2712
    for (instance = scsi_hostlist;
2713
            instance && instance->host_no != host_no;
2714
                instance = instance->next);
2715
 
2716
    if (inout == 1 || !instance)
2717
        return -EINVAL;
2718
 
2719
    host  = (AS_Host *)instance->hostdata;
2720
 
2721
    p += sprintf (p, "AcornSCSI driver v%d.%d.%d"
2722
#ifdef SCSI2_SYNC
2723
    " SYNC"
2724
#endif
2725
#ifdef SCSI2_TAG
2726
    " TAG"
2727
#endif
2728
#ifdef SCSI2_LINK
2729
    " LINK"
2730
#endif
2731
#if (DEBUG & DEBUG_NO_WRITE)
2732
    " NOWRITE ("NO_WRITE_STR")"
2733
#endif
2734
                "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2735
 
2736
    p += sprintf (p,    "SBIC: WD33C93A  Address: %08X  IRQ : %d\n"
2737
                        "DMAC: uPC71071  Address: %08X  IRQ : %d\n\n"
2738
                        "Statistics:\n",
2739
                        host->scsi.io_port, host->scsi.irq,
2740
                        host->dma.io_port, host->scsi.irq);
2741
 
2742
    p += sprintf (p,    "Queued commands: %-10ld    Issued commands: %-10ld\n"
2743
                        "Done commands  : %-10ld    Reads          : %-10ld\n"
2744
                        "Writes         : %-10ld    Others         : %-10ld\n"
2745
                        "Disconnects    : %-10ld    Aborts         : %-10ld\n"
2746
                        "Resets         : %-10ld\n\nLast phases:",
2747
                        host->stats.queues,             host->stats.removes,
2748
                        host->stats.fins,               host->stats.reads,
2749
                        host->stats.writes,             host->stats.miscs,
2750
                        host->stats.disconnects,        host->stats.aborts,
2751
                        host->stats.resets);
2752
 
2753
    for (first = 0; first < 9; first ++) {
2754
        unsigned int statptr, prev;
2755
 
2756
        p += sprintf (p, "\n%c:", first == 8 ? 'H' : ('0' + first));
2757
        statptr = status_ptr[first] - 10;
2758
 
2759
        if ((signed int)statptr < 0)
2760
            statptr += 16;
2761
 
2762
        prev = status[first][statptr].when;
2763
 
2764
        for (; statptr != status_ptr[first]; statptr = (statptr + 1) & 15) {
2765
            if (status[first][statptr].when) {
2766
                p += sprintf (p, "%c%02X:%02X+%2ld",
2767
                        status[first][statptr].irq ? '-' : ' ',
2768
                        status[first][statptr].ph,
2769
                        status[first][statptr].ssr,
2770
                        (status[first][statptr].when - prev) < 100 ?
2771
                                (status[first][statptr].when - prev) : 99);
2772
                prev = status[first][statptr].when;
2773
            }
2774
        }
2775
    }
2776
 
2777
    first = 1;
2778
    p += sprintf (p, "\nAttached devices:");
2779
    while (scd) {
2780
        if (scd->host == instance) {
2781
            int len;
2782
            if (first) {
2783
                p += sprintf (p, "\n");
2784
                first = 0;
2785
            }
2786
 
2787
            proc_print_scsidevice (scd, p, &len, 0);
2788
            p += len;
2789
 
2790
            p += sprintf (p, "Extensions: ");
2791
 
2792
            if (scd->tagged_supported)
2793
                p += sprintf (p, "TAG %sabled [%d] ",
2794
                        scd->tagged_queue ? "en" : "dis", scd->current_tag);
2795
            p += sprintf (p, "\nTransfers: ");
2796
            if (host->device[scd->id].sync_xfer & 15)
2797
                p += sprintf (p, "sync, offset %d, %d ns\n",
2798
                              host->device[scd->id].sync_xfer & 15,
2799
                              acornscsi_getperiod (host->device[scd->id].sync_xfer));
2800
            else
2801
                p += sprintf (p, "async\n");
2802
 
2803
            pos = p - buffer;
2804
            if (pos + begin < offset) {
2805
                begin += pos;
2806
                p = buffer;
2807
            }
2808
            pos = p - buffer;
2809
            if (pos + begin > offset + length)
2810
                break;
2811
        }
2812
        scd = scd->next;
2813
    }
2814
 
2815
    if (first)
2816
        p += sprintf(p, " none\n");
2817
 
2818
    pos = p - buffer;
2819
 
2820
    *start = buffer + (offset - begin);
2821
    pos -= offset - begin;
2822
 
2823
    if (pos > length)
2824
        pos = length;
2825
 
2826
    return pos;
2827
}
2828
 
2829
#ifdef MODULE
2830
 
2831
Scsi_Host_Template driver_template = ACORNSCSI_3;
2832
 
2833
#include "scsi_module.c"
2834
#endif

powered by: WebSVN 2.1.0

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