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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1626 jcastillo
/* aha152x.c -- Adaptec AHA-152x driver
2
 * Author: Jürgen E. Fischer, fischer@et-inf.fho-emden.de
3
 * Copyright 1993, 1994, 1995, 1996 Jürgen E. Fischer
4
 *
5
 *
6
 * This driver is based on
7
 *   fdomain.c -- Future Domain TMC-16x0 driver
8
 * which is
9
 *   Copyright 1992, 1993 Rickard E. Faith (faith@cs.unc.edu)
10
 *
11
 *
12
 * This program is free software; you can redistribute it and/or modify it
13
 * under the terms of the GNU General Public License as published by the
14
 * Free Software Foundation; either version 2, or (at your option) any
15
 * later version.
16
 *
17
 * This program is distributed in the hope that it will be useful, but
18
 * WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
 * General Public License for more details.
21
 *
22
 *
23
 * $Id: aha152x.c,v 1.1 2005-12-20 10:17:45 jcastillo Exp $
24
 *
25
 * $Log: not supported by cvs2svn $
26
 * Revision 1.1.1.1  2001/09/10 07:44:30  simons
27
 * Initial import
28
 *
29
 * Revision 1.1.1.1  2001/07/02 17:58:27  simons
30
 * Initial revision
31
 *
32
 * Revision 1.18  1996/09/07 20:10:40  fischer
33
 * - fixed can_queue handling (multiple outstanding commands working again)
34
 *
35
 * Revision 1.17  1996/08/17 16:05:14  fischer
36
 * - biosparam improved
37
 * - interrupt verification
38
 * - updated documentation
39
 * - cleanups
40
 *
41
 * Revision 1.16  1996/06/09 00:04:56  root
42
 * - added configuration symbols for insmod (aha152x/aha152x1)
43
 *
44
 * Revision 1.15  1996/04/30 14:52:06  fischer
45
 * - proc info fixed
46
 * - support for extended translation for >1GB disks
47
 *
48
 * Revision 1.14  1996/01/17  15:11:20  fischer
49
 * - fixed lockup in MESSAGE IN phase after reconnection
50
 *
51
 * Revision 1.13  1996/01/09  02:15:53  fischer
52
 * - some cleanups
53
 * - moved request_irq behind controller initialization
54
 *   (to avoid spurious interrupts)
55
 *
56
 * Revision 1.12  1995/12/16  12:26:07  fischer
57
 * - barrier()s added
58
 * - configurable RESET delay added
59
 *
60
 * Revision 1.11  1995/12/06  21:18:35  fischer
61
 * - some minor updates
62
 *
63
 * Revision 1.10  1995/07/22  19:18:45  fischer
64
 * - support for 2 controllers
65
 * - started synchronous data transfers (not working yet)
66
 *
67
 * Revision 1.9  1995/03/18  09:20:24  root
68
 * - patches for PCMCIA and modules
69
 *
70
 * Revision 1.8  1995/01/21  22:07:19  root
71
 * - snarf_region => request_region
72
 * - aha152x_intr interface change
73
 *
74
 * Revision 1.7  1995/01/02  23:19:36  root
75
 * - updated COMMAND_SIZE to cmd_len
76
 * - changed sti() to restore_flags()
77
 * - fixed some #ifdef which generated warnings
78
 *
79
 * Revision 1.6  1994/11/24  20:35:27  root
80
 * - problem with odd number of bytes in fifo fixed
81
 *
82
 * Revision 1.5  1994/10/30  14:39:56  root
83
 * - abort code fixed
84
 * - debugging improved
85
 *
86
 * Revision 1.4  1994/09/12  11:33:01  root
87
 * - irqaction to request_irq
88
 * - abortion updated
89
 *
90
 * Revision 1.3  1994/08/04  13:53:05  root
91
 * - updates for mid-level-driver changes
92
 * - accept unexpected BUSFREE phase as error condition
93
 * - parity check now configurable
94
 *
95
 * Revision 1.2  1994/07/03  12:56:36  root
96
 * - cleaned up debugging code
97
 * - more tweaking on reset delays
98
 * - updated abort/reset code (pretty untested...)
99
 *
100
 * Revision 1.1  1994/05/28  21:18:49  root
101
 * - update for mid-level interface change (abort-reset)
102
 * - delays after resets adjusted for some slow devices
103
 *
104
 * Revision 1.0  1994/03/25  12:52:00  root
105
 * - Fixed "more data than expected" problem
106
 * - added new BIOS signatures
107
 *
108
 * Revision 0.102  1994/01/31  20:44:12  root
109
 * - minor changes in insw/outsw handling
110
 *
111
 * Revision 0.101  1993/12/13  01:16:27  root
112
 * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
113
 *   fixes problems with CD-ROM sector size detection & media change)
114
 *
115
 * Revision 0.100  1993/12/10  16:58:47  root
116
 * - fix for unsuccessful selections in case of non-continuous id assignments
117
 *   on the scsi bus.
118
 *
119
 * Revision 0.99  1993/10/24  16:19:59  root
120
 * - fixed DATA IN (rare read errors gone)
121
 *
122
 * Revision 0.98  1993/10/17  12:54:44  root
123
 * - fixed some recent fixes (shame on me)
124
 * - moved initialization of scratch area to aha152x_queue
125
 *
126
 * Revision 0.97  1993/10/09  18:53:53  root
127
 * - DATA IN fixed. Rarely left data in the fifo.
128
 *
129
 * Revision 0.96  1993/10/03  00:53:59  root
130
 * - minor changes on DATA IN
131
 *
132
 * Revision 0.95  1993/09/24  10:36:01  root
133
 * - change handling of MSGI after reselection
134
 * - fixed sti/cli
135
 * - minor changes
136
 *
137
 * Revision 0.94  1993/09/18  14:08:22  root
138
 * - fixed bug in multiple outstanding command code
139
 * - changed detection
140
 * - support for kernel command line configuration
141
 * - reset corrected
142
 * - changed message handling
143
 *
144
 * Revision 0.93  1993/09/15  20:41:19  root
145
 * - fixed bugs with multiple outstanding commands
146
 *
147
 * Revision 0.92  1993/09/13  02:46:33  root
148
 * - multiple outstanding commands work (no problems with IBM drive)
149
 *
150
 * Revision 0.91  1993/09/12  20:51:46  root
151
 * added multiple outstanding commands
152
 * (some problem with this $%&? IBM device remain)
153
 *
154
 * Revision 0.9  1993/09/12  11:11:22  root
155
 * - corrected auto-configuration
156
 * - changed the auto-configuration (added some '#define's)
157
 * - added support for dis-/reconnection
158
 *
159
 * Revision 0.8  1993/09/06  23:09:39  root
160
 * - added support for the drive activity light
161
 * - minor changes
162
 *
163
 * Revision 0.7  1993/09/05  14:30:15  root
164
 * - improved phase detection
165
 * - now using the new snarf_region code of 0.99pl13
166
 *
167
 * Revision 0.6  1993/09/02  11:01:38  root
168
 * first public release; added some signatures and biosparam()
169
 *
170
 * Revision 0.5  1993/08/30  10:23:30  root
171
 * fixed timing problems with my IBM drive
172
 *
173
 * Revision 0.4  1993/08/29  14:06:52  root
174
 * fixed some problems with timeouts due incomplete commands
175
 *
176
 * Revision 0.3  1993/08/28  15:55:03  root
177
 * writing data works too.  mounted and worked on a dos partition
178
 *
179
 * Revision 0.2  1993/08/27  22:42:07  root
180
 * reading data works.  Mounted a msdos partition.
181
 *
182
 * Revision 0.1  1993/08/25  13:38:30  root
183
 * first "damn thing doesn't work" version
184
 *
185
 * Revision 0.0  1993/08/14  19:54:25  root
186
 * empty function bodies; detect() works.
187
 *
188
 *
189
 **************************************************************************
190
 
191
 
192
 
193
 DESCRIPTION:
194
 
195
 This is the Linux low-level SCSI driver for Adaptec AHA-1520/1522 SCSI
196
 host adapters.
197
 
198
 
199
 CONFIGURATION ARGUMENTS:
200
 
201
  IOPORT        base io address                           (0x340/0x140)
202
  IRQ           interrupt level                           (9-12; default 11)
203
  SCSI_ID       scsi id of controller                     (0-7; default 7)
204
  RECONNECT     allow targets to disconnect from the bus  (0/1; default 1 [on])
205
  PARITY        enable parity checking                    (0/1; default 1 [on])
206
  SYNCHRONOUS   enable synchronous transfers              (0/1; default 0 [off])
207
                (NOT WORKING YET)
208
  DELAY:        bus reset delay                           (default 100)
209
  EXT_TRANS:    enable extended translation               (0/1: default 0 [off])
210
                (see NOTES below)
211
 
212
 COMPILE TIME CONFIGURATION (put into AHA152X in drivers/scsi/Makefile):
213
 
214
 -DAUTOCONF
215
   use configuration the controller reports (AHA-152x only)
216
 
217
 -DSKIP_BIOSTEST
218
   Don't test for BIOS signature (AHA-1510 or disabled BIOS)
219
 
220
 -DSETUP0="{ IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS, DELAY, EXT_TRANS }"
221
   override for the first controller
222
 
223
 -DSETUP1="{ IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS, DELAY, EXT_TRANS }"
224
   override for the second controller
225
 
226
 
227
 LILO COMMAND LINE OPTIONS:
228
 
229
 aha152x=<IOPORT>[,<IRQ>[,<SCSI-ID>[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY> [,<EXT_TRANS]]]]]]]
230
 
231
 The normal configuration can be overridden by specifying a command line.
232
 When you do this, the BIOS test is skipped. Entered values have to be
233
 valid (known).  Don't use values that aren't supported under normal
234
 operation.  If you think that you need other values: contact me.
235
 For two controllers use the aha152x statement twice.
236
 
237
 
238
 SYMBOLS FOR MODULE CONFIGURATION:
239
 
240
  aha152x=IOPORT,IRQ,SCSI_ID,RECONNECT,PARITY,SYNCHRONOUS,DELAY,EXT_TRANS
241
    configuration override of first controller
242
 
243
 
244
  aha152x1=IOPORT,IRQ,SCSI_ID,RECONNECT,PARITY,SYNCHRONOUS,DELAY,EXT_TRANS
245
    configuration override of second controller
246
 
247
 
248
 NOTES ON EXT_TRANS:
249
 
250
 SCSI uses block numbers to address blocks/sectors on a device.
251
 The BIOS uses a cylinder/head/sector addressing scheme (C/H/S)
252
 scheme instead.  DOS expects a BIOS or driver that understands this
253
 C/H/S addressing.
254
 
255
 The number of cylinders/heads/sectors is called geometry and is required
256
 as base for requests in C/H/S adressing.  SCSI only knows about the
257
 total capacity of disks in blocks (sectors).
258
 
259
 Therefore the SCSI BIOS/DOS driver has to calculate a logical/virtual
260
 geometry just to be able to support that addressing scheme.  The geometry
261
 returned by the SCSI BIOS is a pure calculation and has nothing to
262
 do with the real/physical geometry of the disk (which is usually
263
 irrelevant anyway).
264
 
265
 Basically this has no impact at all on Linux, because it also uses block
266
 instead of C/H/S addressing.  Unfortunately C/H/S addressing is also used
267
 in the partition table and therefore every operating system has to know
268
 the right geometry to be able to interpret it.
269
 
270
 Moreover there are certain limitations to the C/H/S addressing scheme,
271
 namely the address space is limited to upto 255 heads, upto 63 sectors
272
 and a maximum of 1023 cylinders.
273
 
274
 The AHA-1522 BIOS calculates the geometry by fixing the number of heads
275
 to 64, the number of sectors to 32 and by calculating the number of
276
 cylinders by dividing the capacity reported by the disk by 64*32 (1 MB).
277
 This is considered to be the default translation.
278
 
279
 With respect to the limit of 1023 cylinders using C/H/S you can only
280
 address the first GB of your disk in the partition table.  Therefore
281
 BIOSes of some newer controllers based on the AIC-6260/6360 support
282
 extended translation.  This means that the BIOS uses 255 for heads,
283
 63 for sectors and then divides the capacity of the disk by 255*63
284
 (about 8 MB), as soon it sees a disk greater than 1 GB.  That results
285
 in a maximum of about 8 GB adressable diskspace in the partition table
286
 (but there are already bigger disks out there today).
287
 
288
 To make it even more complicated the translation mode might/might
289
 not be configurable in certain BIOS setups.
290
 
291
 This driver does some more or less failsafe guessing to get the
292
 geometry right in most cases:
293
 
294
 - for disks<1GB: use default translation (C/32/64)
295
 - for disks>1GB:
296
   - take current geometry from the partition table
297
     (using scsicam_bios_param and accept only `valid' geometries,
298
      ie. either (C/32/64) or (C/63/255)).  This can be extended
299
      translation even if it's not enabled in the driver.
300
   - if that fails, take extended translation if enabled by override,
301
     kernel or module parameter, otherwise take default translation and
302
     ask the user for verification.  This might on not yet partitioned
303
     disks or
304
 
305
 
306
 REFERENCES USED:
307
 
308
 "AIC-6260 SCSI Chip Specification", Adaptec Corporation.
309
 
310
 "SCSI COMPUTER SYSTEM INTERFACE - 2 (SCSI-2)", X3T9.2/86-109 rev. 10h
311
 
312
 "Writing a SCSI device driver for Linux", Rik Faith (faith@cs.unc.edu)
313
 
314
 "Kernel Hacker's Guide", Michael K. Johnson (johnsonm@sunsite.unc.edu)
315
 
316
 "Adaptec 1520/1522 User's Guide", Adaptec Corporation.
317
 
318
 Michael K. Johnson (johnsonm@sunsite.unc.edu)
319
 
320
 Drew Eckhardt (drew@cs.colorado.edu)
321
 
322
 Eric Youngdale (ericy@cais.com)
323
 
324
 special thanks to Eric Youngdale for the free(!) supplying the
325
 documentation on the chip.
326
 
327
 **************************************************************************/
328
 
329
#ifdef PCMCIA
330
#define MODULE
331
#endif
332
 
333
#include <linux/module.h>
334
 
335
#ifdef PCMCIA
336
#undef MODULE
337
#endif
338
 
339
#include <linux/sched.h>
340
#include <asm/io.h>
341
#include <linux/blk.h>
342
#include "scsi.h"
343
#include "sd.h"
344
#include "hosts.h"
345
#include "constants.h"
346
#include <asm/system.h>
347
#include <linux/errno.h>
348
#include <linux/string.h>
349
#include <linux/wait.h>
350
#include <linux/ioport.h>
351
#include <linux/proc_fs.h>
352
 
353
#include "aha152x.h"
354
#include <linux/stat.h>
355
 
356
#include <scsi/scsicam.h>
357
 
358
struct proc_dir_entry proc_scsi_aha152x = {
359
    PROC_SCSI_AHA152X, 7, "aha152x",
360
    S_IFDIR | S_IRUGO | S_IXUGO, 2
361
};
362
 
363
/* DEFINES */
364
 
365
/* For PCMCIA cards, always use AUTOCONF */
366
#if defined(PCMCIA) || defined(MODULE)
367
#if !defined(AUTOCONF)
368
#define AUTOCONF
369
#endif
370
#endif
371
 
372
#if !defined(AUTOCONF) && !defined(SETUP0)
373
#error define AUTOCONF or SETUP0
374
#endif
375
 
376
#if defined(DEBUG_AHA152X)
377
 
378
#undef  SKIP_PORTS              /* don't display ports */
379
 
380
#undef  DEBUG_QUEUE             /* debug queue() */
381
#undef  DEBUG_RESET             /* debug reset() */
382
#undef  DEBUG_INTR              /* debug intr() */
383
#undef  DEBUG_SELECTION         /* debug selection part in intr() */
384
#undef  DEBUG_MSGO              /* debug message out phase in intr() */
385
#undef  DEBUG_MSGI              /* debug message in phase in intr() */
386
#undef  DEBUG_STATUS            /* debug status phase in intr() */
387
#undef  DEBUG_CMD               /* debug command phase in intr() */
388
#undef  DEBUG_DATAI             /* debug data in phase in intr() */
389
#undef  DEBUG_DATAO             /* debug data out phase in intr() */
390
#undef  DEBUG_ABORT             /* debug abort() */
391
#undef  DEBUG_DONE              /* debug done() */
392
#undef  DEBUG_BIOSPARAM         /* debug biosparam() */
393
 
394
#undef  DEBUG_RACE              /* debug race conditions */
395
#undef  DEBUG_PHASES            /* debug phases (useful to trace) */
396
#undef  DEBUG_QUEUES            /* debug reselection */
397
 
398
/* recently used for debugging */
399
#if 0
400
#endif
401
 
402
#define DEBUG_SELECTION
403
#define DEBUG_PHASES
404
#define DEBUG_RESET
405
#define DEBUG_ABORT
406
 
407
#define DEBUG_DEFAULT (debug_reset|debug_abort)
408
 
409
#endif
410
 
411
/* END OF DEFINES */
412
 
413
extern unsigned long loops_per_sec;
414
 
415
#define DELAY_DEFAULT 100
416
 
417
/* some additional "phases" for getphase() */
418
#define P_BUSFREE  1
419
#define P_PARITY   2
420
 
421
/* possible irq range */
422
#define IRQ_MIN 9
423
#define IRQ_MAX 12
424
#define IRQS    IRQ_MAX-IRQ_MIN+1
425
 
426
enum {
427
  not_issued   = 0x0001,
428
  in_selection = 0x0002,
429
  disconnected = 0x0004,
430
  aborted      = 0x0008,
431
  sent_ident   = 0x0010,
432
  in_other     = 0x0020,
433
  in_sync      = 0x0040,
434
  sync_ok      = 0x0080,
435
};
436
 
437
#if defined(MODULE)
438
#if defined(DEBUG_AHA152X)
439
int aha152x[]  = { 0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0, DEBUG_DEFAULT };
440
int aha152x1[] = { 0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0, DEBUG_DEFAULT };
441
#else
442
int aha152x[]  = { 0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0 };
443
int aha152x1[] = { 0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0 };
444
#endif
445
#endif
446
 
447
/* set by aha152x_setup according to the command line */
448
static int  setup_count=0;
449
static struct aha152x_setup {
450
  int io_port;
451
  int irq;
452
  int scsiid;
453
  int reconnect;
454
  int parity;
455
  int synchronous;
456
  int delay;
457
  int ext_trans;
458
#ifdef DEBUG_AHA152X
459
  int debug;
460
#endif
461
  char *conf;
462
} setup[2];
463
 
464
static struct Scsi_Host *aha152x_host[IRQS];
465
 
466
#define HOSTDATA(shpnt)   ((struct aha152x_hostdata *) &shpnt->hostdata)
467
#define CURRENT_SC        (HOSTDATA(shpnt)->current_SC)
468
#define ISSUE_SC          (HOSTDATA(shpnt)->issue_SC)
469
#define DISCONNECTED_SC   (HOSTDATA(shpnt)->disconnected_SC)
470
#define DELAY             (HOSTDATA(shpnt)->delay)
471
#define EXT_TRANS         (HOSTDATA(shpnt)->ext_trans)
472
#define SYNCRATE          (HOSTDATA(shpnt)->syncrate[CURRENT_SC->target])
473
#define MSG(i)            (HOSTDATA(shpnt)->message[i])
474
#define MSGLEN            (HOSTDATA(shpnt)->message_len)
475
#define ADDMSG(x)         (MSG(MSGLEN++)=x)
476
 
477
struct aha152x_hostdata {
478
  Scsi_Cmnd     *issue_SC;
479
  Scsi_Cmnd     *current_SC;
480
  Scsi_Cmnd     *disconnected_SC;
481
  int           aborting;
482
  int           abortion_complete;
483
  int           abort_result;
484
  int           commands;
485
 
486
  int           reconnect;
487
  int           parity;
488
  int           synchronous;
489
  int           delay;
490
  int           ext_trans;
491
 
492
  int           swint;
493
 
494
  unsigned char syncrate[8];
495
 
496
  unsigned char message[256];
497
  int           message_len;
498
 
499
#ifdef DEBUG_AHA152X
500
  int           debug;
501
#endif
502
};
503
 
504
void aha152x_intr(int irq, void *dev_id, struct pt_regs *);
505
void aha152x_done(struct Scsi_Host *shpnt, int error);
506
void aha152x_setup(char *str, int *ints);
507
int aha152x_checksetup(struct aha152x_setup *setup);
508
 
509
static void aha152x_reset_ports(struct Scsi_Host *shpnt);
510
static void aha152x_panic(struct Scsi_Host *shpnt, char *msg);
511
 
512
static void disp_ports(struct Scsi_Host *shpnt);
513
static void show_command(Scsi_Cmnd *ptr);
514
static void show_queues(struct Scsi_Host *shpnt);
515
static void disp_enintr(struct Scsi_Host *shpnt);
516
 
517
#if defined(DEBUG_RACE)
518
static void enter_driver(const char *);
519
static void leave_driver(const char *);
520
#endif
521
 
522
/* possible i/o addresses for the AIC-6260 */
523
static unsigned short ports[] =
524
{
525
  0x340,      /* default first */
526
  0x140
527
};
528
#define PORT_COUNT (sizeof(ports) / sizeof(unsigned short))
529
 
530
#if !defined(SKIP_BIOSTEST)
531
/* possible locations for the Adaptec BIOS */
532
static void *addresses[] =
533
{
534
  (void *) 0xdc000,   /* default first */
535
  (void *) 0xc8000,
536
  (void *) 0xcc000,
537
  (void *) 0xd0000,
538
  (void *) 0xd4000,
539
  (void *) 0xd8000,
540
  (void *) 0xe0000,
541
  (void *) 0xeb800,   /* VTech Platinum SMP */
542
  (void *) 0xf0000,
543
};
544
#define ADDRESS_COUNT (sizeof(addresses) / sizeof(void *))
545
 
546
/* signatures for various AIC-6[23]60 based controllers.
547
   The point in detecting signatures is to avoid useless and maybe
548
   harmful probes on ports. I'm not sure that all listed boards pass
549
   auto-configuration. For those which fail the BIOS signature is
550
   obsolete, because user intervention to supply the configuration is
551
   needed anyway.  May be an information whether or not the BIOS supports
552
   extended translation could be also useful here. */
553
static struct signature {
554
  char *signature;
555
  int  sig_offset;
556
  int  sig_length;
557
} signatures[] =
558
{
559
  { "Adaptec AHA-1520 BIOS",      0x102e, 21 },  /* Adaptec 152x */
560
  { "Adaptec AHA-1520B",            0x0b, 17 },  /* Adaptec 152x rev B */
561
  { "Adaptec AHA-1520B/1522B",    0x3e20, 23 },  /* Adaptec 1520B/1522B */
562
  { "Adaptec ASW-B626 BIOS",      0x1029, 21 },  /* on-board controller */
563
  { "Adaptec BIOS: ASW-B626",       0x0f, 22 },  /* on-board controller */
564
  { "Adaptec ASW-B626 S2",        0x2e6c, 19 },  /* on-board controller */
565
  { "Adaptec BIOS:AIC-6360",         0xc, 21 },  /* on-board controller */
566
  { "ScsiPro SP-360 BIOS",        0x2873, 19 },  /* ScsiPro-Controller  */
567
  { "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },  /* Gigabyte Local-Bus-SCSI */
568
  { "Adaptec BIOS:AVA-282X",         0xc, 21 },  /* Adaptec 282x */
569
  { "Adaptec IBM Dock II SCSI",   0x2edd, 24 },  /* IBM Thinkpad Dock II */
570
  { "Adaptec BIOS:AHA-1532P",       0x1c, 22 },  /* IBM Thinkpad Dock II SCSI */
571
};
572
#define SIGNATURE_COUNT (sizeof(signatures) / sizeof(struct signature))
573
#endif
574
 
575
 
576
static void do_pause(unsigned amount) /* Pause for amount*10 milliseconds */
577
{
578
   unsigned long the_time = jiffies + amount; /* 0.01 seconds per jiffy */
579
 
580
   while (jiffies < the_time)
581
     barrier();
582
}
583
 
584
/*
585
 *  queue services:
586
 */
587
static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
588
{
589
  Scsi_Cmnd *end;
590
 
591
  new_SC->host_scribble = (unsigned char *) NULL;
592
  if(!*SC)
593
    *SC=new_SC;
594
  else {
595
    for(end=*SC; end->host_scribble; end = (Scsi_Cmnd *) end->host_scribble)
596
      ;
597
    end->host_scribble = (unsigned char *) new_SC;
598
  }
599
}
600
 
601
static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
602
{
603
  Scsi_Cmnd *ptr;
604
 
605
  ptr=*SC;
606
  if(ptr)
607
    *SC= (Scsi_Cmnd *) (*SC)->host_scribble;
608
  return ptr;
609
}
610
 
611
static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
612
{
613
  Scsi_Cmnd *ptr, *prev;
614
 
615
  for(ptr=*SC, prev=NULL;
616
       ptr && ((ptr->target!=target) || (ptr->lun!=lun));
617
      prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
618
    ;
619
 
620
  if(ptr){
621
    if(prev)
622
      prev->host_scribble = ptr->host_scribble;
623
    else
624
      *SC= (Scsi_Cmnd *) ptr->host_scribble;
625
  }
626
 
627
  return ptr;
628
}
629
 
630
/*
631
 * read inbound byte and wait for ACK to get low
632
 */
633
static void make_acklow(struct Scsi_Host *shpnt)
634
{
635
  SETPORT(SXFRCTL0, CH1|SPIOEN);
636
  GETPORT(SCSIDAT);
637
  SETPORT(SXFRCTL0, CH1);
638
 
639
  while(TESTHI(SCSISIG, ACKI))
640
    barrier();
641
}
642
 
643
/*
644
 * detect current phase more reliable:
645
 * phase is valid, when the target asserts REQ after we've deasserted ACK.
646
 *
647
 * return value is a valid phase or an error code.
648
 *
649
 * errorcodes:
650
 *   P_BUSFREE   BUS FREE phase detected
651
 *   P_PARITY    parity error in DATA phase
652
 */
653
static int getphase(struct Scsi_Host *shpnt)
654
{
655
  int phase, sstat1;
656
 
657
  while(1) {
658
    do {
659
      while(!((sstat1 = GETPORT(SSTAT1)) & (BUSFREE|SCSIRSTI|REQINIT)))
660
        barrier();
661
      if(sstat1 & BUSFREE)
662
        return P_BUSFREE;
663
      if(sstat1 & SCSIRSTI) {
664
        printk("aha152x: RESET IN\n");
665
        SETPORT(SSTAT1, SCSIRSTI);
666
      }
667
    } while(TESTHI(SCSISIG, ACKI) || TESTLO(SSTAT1, REQINIT));
668
 
669
    SETPORT(SSTAT1, CLRSCSIPERR);
670
 
671
    phase = GETPORT(SCSISIG) & P_MASK ;
672
 
673
    if(TESTHI(SSTAT1, SCSIPERR)) {
674
      if((phase & (CDO|MSGO))==0)                        /* DATA phase */
675
        return P_PARITY;
676
 
677
      make_acklow(shpnt);
678
    } else
679
      return phase;
680
  }
681
}
682
 
683
/* called from init/main.c */
684
void aha152x_setup(char *str, int *ints)
685
{
686
  if(setup_count>2)
687
    panic("aha152x: you can only configure up to two controllers\n");
688
 
689
  setup[setup_count].conf        = str;
690
  setup[setup_count].io_port     = ints[0] >= 1 ? ints[1] : 0x340;
691
  setup[setup_count].irq         = ints[0] >= 2 ? ints[2] : 11;
692
  setup[setup_count].scsiid      = ints[0] >= 3 ? ints[3] : 7;
693
  setup[setup_count].reconnect   = ints[0] >= 4 ? ints[4] : 1;
694
  setup[setup_count].parity      = ints[0] >= 5 ? ints[5] : 1;
695
  setup[setup_count].synchronous = ints[0] >= 6 ? ints[6] : 0 /* FIXME: 1 */;
696
  setup[setup_count].delay       = ints[0] >= 7 ? ints[7] : DELAY_DEFAULT;
697
  setup[setup_count].ext_trans   = ints[0] >= 8 ? ints[8] : 0;
698
#ifdef DEBUG_AHA152X
699
  setup[setup_count].debug       = ints[0] >= 9 ? ints[9] : DEBUG_DEFAULT;
700
  if(ints[0]>9) {
701
    printk("aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
702
           "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>[,<DEBUG>]]]]]]]]\n");
703
#else
704
  if(ints[0]>8) {
705
    printk("aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
706
           "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>]]]]]]]\n");
707
#endif
708
  } else
709
    setup_count++;
710
}
711
 
712
/*
713
 * Test, if port_base is valid.
714
 */
715
static int aha152x_porttest(int io_port)
716
{
717
  int i;
718
 
719
  if(check_region(io_port, IO_RANGE))
720
    return 0;
721
 
722
  SETPORT(io_port+O_DMACNTRL1, 0);          /* reset stack pointer */
723
  for(i=0; i<16; i++)
724
    SETPORT(io_port+O_STACK, i);
725
 
726
  SETPORT(io_port+O_DMACNTRL1, 0);          /* reset stack pointer */
727
  for(i=0; i<16 && GETPORT(io_port+O_STACK)==i; i++)
728
    ;
729
 
730
  return(i==16);
731
}
732
 
733
int aha152x_checksetup(struct aha152x_setup *setup)
734
{
735
  int i;
736
 
737
#ifndef PCMCIA
738
  for(i=0; i<PORT_COUNT && (setup->io_port != ports[i]); i++)
739
    ;
740
 
741
  if(i==PORT_COUNT)
742
    return 0;
743
#endif
744
 
745
  if(!aha152x_porttest(setup->io_port))
746
    return 0;
747
 
748
  if(setup->irq<IRQ_MIN && setup->irq>IRQ_MAX)
749
    return 0;
750
 
751
  if((setup->scsiid < 0) || (setup->scsiid > 7))
752
    return 0;
753
 
754
  if((setup->reconnect < 0) || (setup->reconnect > 1))
755
    return 0;
756
 
757
  if((setup->parity < 0) || (setup->parity > 1))
758
    return 0;
759
 
760
  if((setup->synchronous < 0) || (setup->synchronous > 1))
761
    return 0;
762
 
763
  if((setup->ext_trans < 0) || (setup->ext_trans > 1))
764
    return 0;
765
 
766
 
767
  return 1;
768
}
769
 
770
void aha152x_swintr(int irqno, void *dev_id, struct pt_regs * regs)
771
{
772
  struct Scsi_Host *shpnt = aha152x_host[irqno-IRQ_MIN];
773
 
774
  if(!shpnt)
775
    panic("aha152x: catched software interrupt for unknown controller.\n");
776
 
777
  HOSTDATA(shpnt)->swint++;
778
}
779
 
780
 
781
int aha152x_detect(Scsi_Host_Template * tpnt)
782
{
783
  int                 i, j, ok;
784
#if defined(AUTOCONF)
785
  aha152x_config      conf;
786
#endif
787
 
788
  tpnt->proc_dir = &proc_scsi_aha152x;
789
 
790
  for(i=0; i<IRQS; i++)
791
    aha152x_host[i] = (struct Scsi_Host *) NULL;
792
 
793
  if(setup_count) {
794
    printk("aha152x: processing commandline: ");
795
 
796
    for(i=0; i<setup_count; i++)
797
      if(!aha152x_checksetup(&setup[i])) {
798
        printk("\naha152x: %s\n", setup[i].conf);
799
        printk("aha152x: invalid line (controller=%d)\n", i+1);
800
      }
801
 
802
      printk("ok\n");
803
  }
804
 
805
#ifdef SETUP0
806
  if(setup_count<2) {
807
    struct aha152x_setup override = SETUP0;
808
 
809
    if(setup_count==0 || (override.io_port != setup[0].io_port))
810
      if(!aha152x_checksetup(&override)) {
811
        printk("\naha152x: invalid override SETUP0={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
812
               override.io_port,
813
               override.irq,
814
               override.scsiid,
815
               override.reconnect,
816
               override.parity,
817
               override.synchronous,
818
               override.delay,
819
               override.ext_trans);
820
      } else
821
        setup[setup_count++] = override;
822
  }
823
#endif
824
 
825
#ifdef SETUP1
826
  if(setup_count<2) {
827
    struct aha152x_setup override = SETUP1;
828
 
829
    if(setup_count==0 || (override.io_port != setup[0].io_port))
830
      if(!aha152x_checksetup(&override)) {
831
        printk("\naha152x: invalid override SETUP1={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
832
               override.io_port,
833
               override.irq,
834
               override.scsiid,
835
               override.reconnect,
836
               override.parity,
837
               override.synchronous,
838
               override.delay,
839
               override.ext_trans);
840
      } else
841
        setup[setup_count++] = override;
842
  }
843
#endif
844
 
845
#if defined(MODULE)
846
  if(setup_count<2 && aha152x[0]!=0) {
847
    setup[setup_count].conf        = "";
848
    setup[setup_count].io_port     = aha152x[0];
849
    setup[setup_count].irq         = aha152x[1];
850
    setup[setup_count].scsiid      = aha152x[2];
851
    setup[setup_count].reconnect   = aha152x[3];
852
    setup[setup_count].parity      = aha152x[4];
853
    setup[setup_count].synchronous = aha152x[5];
854
    setup[setup_count].delay       = aha152x[6];
855
    setup[setup_count].ext_trans   = aha152x[7];
856
#ifdef DEBUG_AHA152X
857
    setup[setup_count].debug       = aha152x[8];
858
#endif
859
    if(aha152x_checksetup(&setup[setup_count]))
860
      setup_count++;
861
    else
862
      printk("\naha152x: invalid module argument aha152x=0x%x,%d,%d,%d,%d,%d,%d,%d\n",
863
             setup[setup_count].io_port,
864
             setup[setup_count].irq,
865
             setup[setup_count].scsiid,
866
             setup[setup_count].reconnect,
867
             setup[setup_count].parity,
868
             setup[setup_count].synchronous,
869
             setup[setup_count].delay,
870
             setup[setup_count].ext_trans);
871
  }
872
 
873
  if(setup_count<2 && aha152x1[0]!=0) {
874
    setup[setup_count].conf        = "";
875
    setup[setup_count].io_port     = aha152x1[0];
876
    setup[setup_count].irq         = aha152x1[1];
877
    setup[setup_count].scsiid      = aha152x1[2];
878
    setup[setup_count].reconnect   = aha152x1[3];
879
    setup[setup_count].parity      = aha152x1[4];
880
    setup[setup_count].synchronous = aha152x1[5];
881
    setup[setup_count].delay       = aha152x1[6];
882
    setup[setup_count].ext_trans   = aha152x1[7];
883
#ifdef DEBUG_AHA152X
884
    setup[setup_count].debug       = aha152x1[8];
885
#endif
886
    if(aha152x_checksetup(&setup[setup_count]))
887
      setup_count++;
888
    else
889
      printk("\naha152x: invalid module argument aha152x1=0x%x,%d,%d,%d,%d,%d,%d,%d\n",
890
             setup[setup_count].io_port,
891
             setup[setup_count].irq,
892
             setup[setup_count].scsiid,
893
             setup[setup_count].reconnect,
894
             setup[setup_count].parity,
895
             setup[setup_count].synchronous,
896
             setup[setup_count].delay,
897
             setup[setup_count].ext_trans);
898
  }
899
#endif
900
 
901
#if defined(AUTOCONF)
902
  if(setup_count<2) {
903
#if !defined(SKIP_BIOSTEST)
904
    ok=0;
905
    for(i=0; i < ADDRESS_COUNT && !ok; i++)
906
      for(j=0; (j < SIGNATURE_COUNT) && !ok; j++)
907
        ok=!memcmp((void *) addresses[i]+signatures[j].sig_offset,
908
                   (void *) signatures[j].signature,
909
                   (int) signatures[j].sig_length);
910
 
911
    if(!ok && setup_count==0)
912
      return 0;
913
 
914
    printk("aha152x: BIOS test: passed, ");
915
#else
916
    printk("aha152x: ");
917
#endif /* !SKIP_BIOSTEST */
918
 
919
    ok=0;
920
    for(i=0; i<PORT_COUNT && setup_count<2; i++) {
921
      if((setup_count==1) && (setup[0].io_port == ports[i]))
922
        continue;
923
 
924
      if(aha152x_porttest(ports[i])) {
925
        ok++;
926
        setup[setup_count].io_port = ports[i];
927
 
928
        conf.cf_port =
929
          (GETPORT(ports[i]+O_PORTA)<<8) + GETPORT(ports[i]+O_PORTB);
930
 
931
        setup[setup_count].irq         = IRQ_MIN + conf.cf_irq;
932
        setup[setup_count].scsiid      = conf.cf_id;
933
        setup[setup_count].reconnect   = conf.cf_tardisc;
934
        setup[setup_count].parity      = !conf.cf_parity;
935
        setup[setup_count].synchronous = 0 /* FIXME: conf.cf_syncneg */;
936
        setup[setup_count].delay       = DELAY_DEFAULT;
937
        setup[setup_count].ext_trans   = 0;
938
#ifdef DEBUG_AHA152X
939
        setup[setup_count].debug       = DEBUG_DEFAULT;
940
#endif
941
        setup_count++;
942
      }
943
    }
944
 
945
    if(ok)
946
      printk("auto configuration: ok, ");
947
  }
948
#endif
949
 
950
  printk("detected %d controller(s)\n", setup_count);
951
 
952
  for(i=0; i<setup_count; i++) {
953
    struct Scsi_Host        *shpnt;
954
    unsigned long int       the_time;
955
 
956
    shpnt = aha152x_host[setup[i].irq-IRQ_MIN] =
957
      scsi_register(tpnt, sizeof(struct aha152x_hostdata));
958
 
959
    shpnt->io_port                     = setup[i].io_port;
960
    shpnt->n_io_port                   = IO_RANGE;
961
    shpnt->irq                         = setup[i].irq;
962
 
963
    ISSUE_SC                           = (Scsi_Cmnd *) NULL;
964
    CURRENT_SC                         = (Scsi_Cmnd *) NULL;
965
    DISCONNECTED_SC                    = (Scsi_Cmnd *) NULL;
966
 
967
    HOSTDATA(shpnt)->reconnect         = setup[i].reconnect;
968
    HOSTDATA(shpnt)->parity            = setup[i].parity;
969
    HOSTDATA(shpnt)->synchronous       = setup[i].synchronous;
970
    HOSTDATA(shpnt)->delay             = setup[i].delay;
971
    HOSTDATA(shpnt)->ext_trans         = setup[i].ext_trans;
972
#ifdef DEBUG_AHA152X
973
    HOSTDATA(shpnt)->debug             = setup[i].debug;
974
#endif
975
 
976
    HOSTDATA(shpnt)->aborting          = 0;
977
    HOSTDATA(shpnt)->abortion_complete = 0;
978
    HOSTDATA(shpnt)->abort_result      = 0;
979
    HOSTDATA(shpnt)->commands          = 0;
980
 
981
    HOSTDATA(shpnt)->message_len       = 0;
982
 
983
    for(j=0; j<8; j++)
984
      HOSTDATA(shpnt)->syncrate[j] = 0;
985
 
986
    SETPORT(SCSIID, setup[i].scsiid << 4);
987
    shpnt->this_id=setup[i].scsiid;
988
 
989
    if(setup[i].reconnect)
990
      shpnt->can_queue=AHA152X_MAXQUEUE;
991
 
992
    /* RESET OUT */
993
    SETBITS(SCSISEQ, SCSIRSTO);
994
    do_pause(30);
995
    CLRBITS(SCSISEQ, SCSIRSTO);
996
    do_pause(setup[i].delay);
997
 
998
    aha152x_reset_ports(shpnt);
999
 
1000
    printk("aha152x%d: vital data: PORTBASE=0x%03x, IRQ=%d, SCSI ID=%d,"
1001
           " reconnect=%s, parity=%s, synchronous=%s, delay=%d, extended translation=%s\n",
1002
           i,
1003
           shpnt->io_port,
1004
           shpnt->irq,
1005
           shpnt->this_id,
1006
           HOSTDATA(shpnt)->reconnect ? "enabled" : "disabled",
1007
           HOSTDATA(shpnt)->parity ? "enabled" : "disabled",
1008
           HOSTDATA(shpnt)->synchronous ? "enabled" : "disabled",
1009
           HOSTDATA(shpnt)->delay,
1010
           HOSTDATA(shpnt)->ext_trans ? "enabled" : "disabled");
1011
 
1012
    request_region(shpnt->io_port, IO_RANGE, "aha152x");  /* Register */
1013
 
1014
    /* not expecting any interrupts */
1015
    SETPORT(SIMODE0, 0);
1016
    SETPORT(SIMODE1, 0);
1017
 
1018
    SETBITS(DMACNTRL0, INTEN);
1019
 
1020
    ok = request_irq(shpnt->irq, aha152x_swintr, SA_INTERRUPT, "aha152x", NULL);
1021
    if(ok<0) {
1022
      if(ok == -EINVAL)
1023
        printk("aha152x%d: bad IRQ %d.\n", i, shpnt->irq);
1024
      else if(ok == -EBUSY)
1025
        printk("aha152x%d: IRQ %d already in use.\n", i, shpnt->irq);
1026
      else
1027
        printk("\naha152x%d: Unexpected error code %d on requesting IRQ %d.\n",                 i, ok, shpnt->irq);
1028
      printk("aha152x: driver needs an IRQ.\n");
1029
 
1030
      scsi_unregister(shpnt);
1031
      shpnt=aha152x_host[shpnt->irq-IRQ_MIN]=0;
1032
      continue;
1033
    }
1034
 
1035
    HOSTDATA(shpnt)->swint=0;
1036
 
1037
    printk("aha152x: trying software interrupt, ");
1038
    SETBITS(DMACNTRL0, SWINT);
1039
 
1040
    the_time=jiffies+100;
1041
    while(!HOSTDATA(shpnt)->swint && jiffies<the_time)
1042
      barrier();
1043
 
1044
    free_irq(shpnt->irq,0);
1045
 
1046
    if(!HOSTDATA(shpnt)->swint) {
1047
      if(TESTHI(DMASTAT, INTSTAT)) {
1048
        printk("lost.\n");
1049
      } else {
1050
        printk("failed.\n");
1051
      }
1052
 
1053
      printk("aha152x: IRQ %d possibly wrong.  Please verify.\n", shpnt->irq);
1054
 
1055
      scsi_unregister(shpnt);
1056
      shpnt=aha152x_host[shpnt->irq-IRQ_MIN]=0;
1057
      continue;
1058
    }
1059
 
1060
    printk("ok.\n");
1061
 
1062
    CLRBITS(DMACNTRL0, SWINT);
1063
 
1064
    /* clear interrupts */
1065
    SETPORT(SSTAT0, 0x7f);
1066
    SETPORT(SSTAT1, 0xef);
1067
 
1068
    if(request_irq(shpnt->irq,aha152x_intr,SA_INTERRUPT,"aha152x",NULL)<0) {
1069
      printk("aha152x: failed to reassign interrupt.\n");
1070
    }
1071
  }
1072
 
1073
  return (setup_count>0);
1074
}
1075
 
1076
/*
1077
 *  Queue a command and setup interrupts for a free bus.
1078
 */
1079
int aha152x_queue(Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
1080
{
1081
  struct Scsi_Host *shpnt = SCpnt->host;
1082
  unsigned long flags;
1083
 
1084
#if defined(DEBUG_RACE)
1085
  enter_driver("queue");
1086
#else
1087
#if defined(DEBUG_QUEUE)
1088
  if(HOSTDATA(shpnt)->debug & debug_queue)
1089
    printk("aha152x: queue(), ");
1090
#endif
1091
#endif
1092
 
1093
#if defined(DEBUG_QUEUE)
1094
  if(HOSTDATA(shpnt)->debug & debug_queue) {
1095
    printk("SCpnt (target = %d lun = %d cmnd = ",
1096
           SCpnt->target, SCpnt->lun);
1097
    print_command(SCpnt->cmnd);
1098
    printk(", cmd_len=%d, pieces = %d size = %u), ",
1099
           SCpnt->cmd_len, SCpnt->use_sg, SCpnt->request_bufflen);
1100
    disp_ports(shpnt);
1101
  }
1102
#endif
1103
 
1104
  SCpnt->scsi_done =       done;
1105
 
1106
  /* setup scratch area
1107
     SCp.ptr              : buffer pointer
1108
     SCp.this_residual    : buffer length
1109
     SCp.buffer           : next buffer
1110
     SCp.buffers_residual : left buffers in list
1111
     SCp.phase            : current state of the command */
1112
  SCpnt->SCp.phase = not_issued;
1113
  if (SCpnt->use_sg) {
1114
    SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
1115
    SCpnt->SCp.ptr              = SCpnt->SCp.buffer->address;
1116
    SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
1117
    SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
1118
  } else {
1119
    SCpnt->SCp.ptr              = (char *)SCpnt->request_buffer;
1120
    SCpnt->SCp.this_residual    = SCpnt->request_bufflen;
1121
    SCpnt->SCp.buffer           = NULL;
1122
    SCpnt->SCp.buffers_residual = 0;
1123
  }
1124
 
1125
  SCpnt->SCp.Status              = CHECK_CONDITION;
1126
  SCpnt->SCp.Message             = 0;
1127
  SCpnt->SCp.have_data_in        = 0;
1128
  SCpnt->SCp.sent_command        = 0;
1129
 
1130
  /* Turn led on, when this is the first command. */
1131
  save_flags(flags);
1132
  cli();
1133
  HOSTDATA(shpnt)->commands++;
1134
  if(HOSTDATA(shpnt)->commands==1)
1135
    SETPORT(PORTA, 1);
1136
 
1137
#if defined(DEBUG_QUEUES)
1138
  if(HOSTDATA(shpnt)->debug & debug_queues)
1139
    printk("i+ (%d), ", HOSTDATA(shpnt)->commands);
1140
#endif
1141
  append_SC(&ISSUE_SC, SCpnt);
1142
 
1143
  /* Enable bus free interrupt, when we aren't currently on the bus */
1144
  if(!CURRENT_SC) {
1145
    SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1146
    SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1147
  }
1148
  restore_flags(flags);
1149
 
1150
#if defined(DEBUG_RACE)
1151
  leave_driver("queue");
1152
#endif
1153
 
1154
  return 0;
1155
}
1156
 
1157
/*
1158
 *  We only support commands in interrupt-driven fashion
1159
 */
1160
int aha152x_command(Scsi_Cmnd *SCpnt)
1161
{
1162
  printk("aha152x: interrupt driven driver; use aha152x_queue()\n");
1163
  return -1;
1164
}
1165
 
1166
/*
1167
 *  Abort a queued command
1168
 *  (commands that are on the bus can't be aborted easily)
1169
 */
1170
int aha152x_abort(Scsi_Cmnd *SCpnt)
1171
{
1172
  struct Scsi_Host *shpnt = SCpnt->host;
1173
  unsigned long flags;
1174
  Scsi_Cmnd *ptr, *prev;
1175
 
1176
  save_flags(flags);
1177
  cli();
1178
 
1179
#if defined(DEBUG_ABORT)
1180
  if(HOSTDATA(shpnt)->debug & debug_abort) {
1181
    printk("aha152x: abort(), SCpnt=0x%08x, ", (unsigned int) SCpnt);
1182
    show_queues(shpnt);
1183
  }
1184
#endif
1185
 
1186
  /* look for command in issue queue */
1187
  for(ptr=ISSUE_SC, prev=NULL;
1188
       ptr && ptr!=SCpnt;
1189
       prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
1190
    ;
1191
 
1192
  if(ptr) {
1193
    /* dequeue */
1194
    if(prev)
1195
      prev->host_scribble = ptr->host_scribble;
1196
    else
1197
      ISSUE_SC = (Scsi_Cmnd *) ptr->host_scribble;
1198
 
1199
    HOSTDATA(shpnt)->commands--;
1200
 
1201
    restore_flags(flags);
1202
 
1203
    ptr->host_scribble = NULL;
1204
    ptr->result = DID_ABORT << 16;
1205
    ptr->scsi_done(ptr);
1206
 
1207
    return SCSI_ABORT_SUCCESS;
1208
  }
1209
 
1210
  /* if the bus is busy or a command is currently processed,
1211
     we can't do anything more */
1212
  if (TESTLO(SSTAT1, BUSFREE) || (CURRENT_SC && CURRENT_SC!=SCpnt)) {
1213
    /* fail abortion, if bus is busy */
1214
 
1215
    if(!CURRENT_SC)
1216
      printk("bus busy w/o current command, ");
1217
 
1218
    restore_flags(flags);
1219
 
1220
    return SCSI_ABORT_BUSY;
1221
  }
1222
 
1223
  /* bus is free */
1224
 
1225
  if(CURRENT_SC) {
1226
    HOSTDATA(shpnt)->commands--;
1227
 
1228
    /* target entered bus free before COMMAND COMPLETE, nothing to abort */
1229
    restore_flags(flags);
1230
    CURRENT_SC->result = DID_ERROR << 16;
1231
    CURRENT_SC->scsi_done(CURRENT_SC);
1232
    CURRENT_SC = (Scsi_Cmnd *) NULL;
1233
 
1234
    return SCSI_ABORT_SUCCESS;
1235
  }
1236
 
1237
  /* look for command in disconnected queue */
1238
  for(ptr=DISCONNECTED_SC, prev=NULL;
1239
       ptr && ptr!=SCpnt;
1240
       prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
1241
    ;
1242
 
1243
  if(!ptr) {
1244
    /* command wasn't found */
1245
    printk("command not found\n");
1246
    restore_flags(flags);
1247
 
1248
    return SCSI_ABORT_NOT_RUNNING;
1249
  }
1250
 
1251
  if(!HOSTDATA(shpnt)->aborting) {
1252
    /* dequeue */
1253
    if(prev)
1254
      prev->host_scribble = ptr->host_scribble;
1255
    else
1256
      DISCONNECTED_SC = (Scsi_Cmnd *) ptr->host_scribble;
1257
 
1258
    HOSTDATA(shpnt)->commands--;
1259
 
1260
    /* set command current and initiate selection,
1261
       let the interrupt routine take care of the abortion */
1262
    CURRENT_SC     = ptr;
1263
    ptr->SCp.phase = in_selection|aborted;
1264
    SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
1265
 
1266
    ADDMSG(ABORT);
1267
 
1268
    /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
1269
    SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1270
    SETPORT(SIMODE1, ENSELTIMO);
1271
 
1272
    /* Enable SELECTION OUT sequence */
1273
    SETBITS(SCSISEQ, ENSELO | ENAUTOATNO);
1274
 
1275
    SETBITS(DMACNTRL0, INTEN);
1276
    HOSTDATA(shpnt)->abort_result=SCSI_ABORT_SUCCESS;
1277
    HOSTDATA(shpnt)->aborting++;
1278
    HOSTDATA(shpnt)->abortion_complete=0;
1279
 
1280
    sti();  /* Hi Eric, guess what ;-) */
1281
 
1282
    /* sleep until the abortion is complete */
1283
    while(!HOSTDATA(shpnt)->abortion_complete)
1284
      barrier();
1285
    HOSTDATA(shpnt)->aborting=0;
1286
 
1287
    return HOSTDATA(shpnt)->abort_result;
1288
  } else {
1289
    /* we're already aborting a command */
1290
    restore_flags(flags);
1291
 
1292
    return SCSI_ABORT_BUSY;
1293
  }
1294
}
1295
 
1296
/*
1297
 *  Restore default values to the AIC-6260 registers and reset the fifos
1298
 */
1299
static void aha152x_reset_ports(struct Scsi_Host *shpnt)
1300
{
1301
  /* disable interrupts */
1302
  SETPORT(DMACNTRL0, RSTFIFO);
1303
 
1304
  SETPORT(SCSISEQ, 0);
1305
 
1306
  SETPORT(SXFRCTL1, 0);
1307
  SETPORT(SCSISIG, 0);
1308
  SETPORT(SCSIRATE, 0);
1309
 
1310
  /* clear all interrupt conditions */
1311
  SETPORT(SSTAT0, 0x7f);
1312
  SETPORT(SSTAT1, 0xef);
1313
 
1314
  SETPORT(SSTAT4, SYNCERR|FWERR|FRERR);
1315
 
1316
  SETPORT(DMACNTRL0, 0);
1317
  SETPORT(DMACNTRL1, 0);
1318
 
1319
  SETPORT(BRSTCNTRL, 0xf1);
1320
 
1321
  /* clear SCSI fifo and transfer count */
1322
  SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1323
  SETPORT(SXFRCTL0, CH1);
1324
 
1325
  /* enable interrupts */
1326
  SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1327
  SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1328
}
1329
 
1330
/*
1331
 *  Reset registers, reset a hanging bus and
1332
 *  kill active and disconnected commands for target w/o soft reset
1333
 */
1334
int aha152x_reset(Scsi_Cmnd *SCpnt, unsigned int unused)
1335
{
1336
  struct Scsi_Host *shpnt = SCpnt->host;
1337
  unsigned long flags;
1338
  Scsi_Cmnd *ptr, *prev, *next;
1339
 
1340
  aha152x_reset_ports(shpnt);
1341
 
1342
  /* Reset, if bus hangs */
1343
  if(TESTLO(SSTAT1, BUSFREE)) {
1344
    CLRBITS(DMACNTRL0, INTEN);
1345
 
1346
#if defined(DEBUG_RESET)
1347
    if(HOSTDATA(shpnt)->debug & debug_reset) {
1348
      printk("aha152x: reset(), bus not free: SCSI RESET OUT\n");
1349
      show_queues(shpnt);
1350
    }
1351
#endif
1352
 
1353
    ptr=CURRENT_SC;
1354
    if(ptr && !ptr->device->soft_reset) {
1355
      ptr->host_scribble = NULL;
1356
      ptr->result = DID_RESET << 16;
1357
      ptr->scsi_done(CURRENT_SC);
1358
      CURRENT_SC=NULL;
1359
    }
1360
 
1361
    save_flags(flags);
1362
    cli();
1363
    prev=NULL; ptr=DISCONNECTED_SC;
1364
    while(ptr) {
1365
      if(!ptr->device->soft_reset) {
1366
        if(prev)
1367
          prev->host_scribble = ptr->host_scribble;
1368
        else
1369
          DISCONNECTED_SC = (Scsi_Cmnd *) ptr->host_scribble;
1370
 
1371
        next = (Scsi_Cmnd *) ptr->host_scribble;
1372
 
1373
        ptr->host_scribble = NULL;
1374
        ptr->result        = DID_RESET << 16;
1375
        ptr->scsi_done(ptr);
1376
 
1377
        ptr = next;
1378
      } else {
1379
        prev=ptr;
1380
        ptr = (Scsi_Cmnd *) ptr->host_scribble;
1381
      }
1382
    }
1383
    restore_flags(flags);
1384
 
1385
#if defined(DEBUG_RESET)
1386
    if(HOSTDATA(shpnt)->debug & debug_reset) {
1387
      printk("commands on targets w/ soft-resets:\n");
1388
      show_queues(shpnt);
1389
    }
1390
#endif
1391
 
1392
    /* RESET OUT */
1393
    SETPORT(SCSISEQ, SCSIRSTO);
1394
    do_pause(30);
1395
    SETPORT(SCSISEQ, 0);
1396
    do_pause(DELAY);
1397
 
1398
    SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1399
    SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1400
 
1401
    SETPORT(DMACNTRL0, INTEN);
1402
  }
1403
 
1404
  return SCSI_RESET_SUCCESS;
1405
}
1406
 
1407
/*
1408
 * Return the "logical geometry"
1409
 */
1410
int aha152x_biosparam(Scsi_Disk * disk, kdev_t dev, int *info_array)
1411
{
1412
  struct Scsi_Host *shpnt=disk->device->host;
1413
 
1414
#if defined(DEBUG_BIOSPARAM)
1415
  if(HOSTDATA(shpnt)->debug & debug_biosparam)
1416
    printk("aha152x_biosparam: dev=%s, size=%d, ",
1417
           kdevname(dev), disk->capacity);
1418
#endif
1419
 
1420
  /* try default translation */
1421
  info_array[0]=64;
1422
  info_array[1]=32;
1423
  info_array[2]=disk->capacity / (64 * 32);
1424
 
1425
  /* for disks >1GB do some guessing */
1426
  if(info_array[2]>=1024) {
1427
    int info[3];
1428
 
1429
    /* try to figure out the geometry from the partition table */
1430
    if(scsicam_bios_param(disk, dev, info)<0 ||
1431
       !((info[0]==64 && info[1]==32) || (info[0]==255 && info[1]==63))) {
1432
      if(EXT_TRANS) {
1433
        printk("aha152x: unable to verify geometry for disk with >1GB.\n"
1434
               "         using extended translation.\n");
1435
        info_array[0] = 255;
1436
        info_array[1] = 63;
1437
        info_array[2] = disk->capacity / (255 * 63);
1438
      } else {
1439
        printk("aha152x: unable to verify geometry for disk with >1GB.\n"
1440
               "         Using default translation. Please verify yourself.\n"
1441
               "         Perhaps you need to enable extended translation in the driver.\n"
1442
               "         See /usr/src/linux/drivers/scsi/aha152x.c for details.\n");
1443
      }
1444
    } else {
1445
      info_array[0]=info[0];
1446
      info_array[1]=info[1];
1447
      info_array[2]=info[2];
1448
 
1449
      if(info[0]==255 && !EXT_TRANS) {
1450
        printk("aha152x: current partition table is using extended translation.\n"
1451
               "         using it also, although it's not explicty enabled.\n");
1452
      }
1453
    }
1454
  }
1455
 
1456
#if defined(DEBUG_BIOSPARAM)
1457
  if(HOSTDATA(shpnt)->debug & debug_biosparam) {
1458
    printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
1459
           info_array[0], info_array[1], info_array[2]);
1460
    printk("WARNING: check, if the bios geometry is correct.\n");
1461
  }
1462
#endif
1463
 
1464
  return 0;
1465
}
1466
 
1467
/*
1468
 *  Internal done function
1469
 */
1470
void aha152x_done(struct Scsi_Host *shpnt, int error)
1471
{
1472
  unsigned long flags;
1473
  Scsi_Cmnd *done_SC;
1474
 
1475
#if defined(DEBUG_DONE)
1476
  if(HOSTDATA(shpnt)->debug & debug_done) {
1477
    printk("\naha152x: done(), ");
1478
    disp_ports(shpnt);
1479
  }
1480
#endif
1481
 
1482
  if(CURRENT_SC) {
1483
#if defined(DEBUG_DONE)
1484
    if(HOSTDATA(shpnt)->debug & debug_done)
1485
      printk("done(%x), ", error);
1486
#endif
1487
 
1488
    save_flags(flags);
1489
    cli();
1490
 
1491
    done_SC = CURRENT_SC;
1492
    CURRENT_SC = NULL;
1493
 
1494
    /* turn led off, when no commands are in the driver */
1495
    HOSTDATA(shpnt)->commands--;
1496
    if(!HOSTDATA(shpnt)->commands)
1497
      SETPORT(PORTA, 0);                                  /* turn led off */
1498
 
1499
#if defined(DEBUG_QUEUES)
1500
    if(HOSTDATA(shpnt)->debug & debug_queues)
1501
      printk("ok (%d), ", HOSTDATA(shpnt)->commands);
1502
#endif
1503
    restore_flags(flags);
1504
 
1505
    SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1506
    SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1507
 
1508
#if 0
1509
/* Why poll for the BUS FREE phase, when we have setup the interrupt!? */
1510
#if defined(DEBUG_PHASES)
1511
    if(HOSTDATA(shpnt)->debug & debug_phases)
1512
      printk("BUS FREE loop, ");
1513
#endif
1514
    while(TESTLO(SSTAT1, BUSFREE))
1515
      barrier();
1516
#if defined(DEBUG_PHASES)
1517
    if(HOSTDATA(shpnt)->debug & debug_phases)
1518
      printk("BUS FREE\n");
1519
#endif
1520
#endif
1521
 
1522
    done_SC->result = error;
1523
    if(done_SC->scsi_done) {
1524
#if defined(DEBUG_DONE)
1525
      if(HOSTDATA(shpnt)->debug & debug_done)
1526
        printk("calling scsi_done, ");
1527
#endif
1528
      done_SC->scsi_done(done_SC);
1529
#if defined(DEBUG_DONE)
1530
      if(HOSTDATA(shpnt)->debug & debug_done)
1531
        printk("done returned, ");
1532
#endif
1533
    } else
1534
       panic("aha152x: current_SC->scsi_done() == NULL");
1535
  } else
1536
    aha152x_panic(shpnt, "done() called outside of command");
1537
}
1538
 
1539
/*
1540
 * Interrupts handler (main routine of the driver)
1541
 */
1542
void aha152x_intr(int irqno, void *dev_id, struct pt_regs * regs)
1543
{
1544
  struct Scsi_Host *shpnt = aha152x_host[irqno-IRQ_MIN];
1545
  unsigned int flags;
1546
  int done=0, phase;
1547
 
1548
#if defined(DEBUG_RACE)
1549
  enter_driver("intr");
1550
#else
1551
#if defined(DEBUG_INTR)
1552
  if(HOSTDATA(shpnt)->debug & debug_intr)
1553
    printk("\naha152x: intr(), ");
1554
#endif
1555
#endif
1556
 
1557
  if(!shpnt)
1558
    panic("aha152x: catched interrupt for unknown controller.\n");
1559
 
1560
  /* no more interrupts from the controller, while we're busy.
1561
     INTEN has to be restored, when we're ready to leave
1562
     intr(). To avoid race conditions, we have to return
1563
     immediately afterwards. */
1564
  CLRBITS(DMACNTRL0, INTEN);
1565
  sti();  /* Yes, sti() really needs to be here */
1566
 
1567
  /* disconnected target is trying to reconnect.
1568
     Only possible, if we have disconnected nexuses and
1569
     nothing is occupying the bus.
1570
  */
1571
  if(TESTHI(SSTAT0, SELDI) &&
1572
      DISCONNECTED_SC &&
1573
      (!CURRENT_SC || (CURRENT_SC->SCp.phase & in_selection)) ) {
1574
    int identify_msg, target, i;
1575
 
1576
    /* Avoid conflicts when a target reconnects
1577
       while we are trying to connect to another. */
1578
    if(CURRENT_SC) {
1579
#if defined(DEBUG_QUEUES)
1580
      if(HOSTDATA(shpnt)->debug & debug_queues)
1581
        printk("i+, ");
1582
#endif
1583
      save_flags(flags);
1584
      cli();
1585
      append_SC(&ISSUE_SC, CURRENT_SC);
1586
      CURRENT_SC=NULL;
1587
      restore_flags(flags);
1588
    }
1589
 
1590
    /* disable sequences */
1591
    SETPORT(SCSISEQ, 0);
1592
    SETPORT(SSTAT0, CLRSELDI);
1593
    SETPORT(SSTAT1, CLRBUSFREE);
1594
 
1595
#if defined(DEBUG_QUEUES) || defined(DEBUG_PHASES)
1596
    if(HOSTDATA(shpnt)->debug & (debug_queues|debug_phases))
1597
      printk("reselected, ");
1598
#endif
1599
 
1600
    i = GETPORT(SELID) & ~(1 << shpnt->this_id);
1601
    target=0;
1602
 
1603
    if(i==0)
1604
      aha152x_panic(shpnt, "reconnecting target unknown");
1605
 
1606
    for(; (i & 1)==0; target++, i>>=1)
1607
      ;
1608
 
1609
#if defined(DEBUG_QUEUES)
1610
    if(HOSTDATA(shpnt)->debug & debug_queues)
1611
      printk("SELID=%02x, target=%d, ", GETPORT(SELID), target);
1612
#endif
1613
    SETPORT(SCSIID, (shpnt->this_id << OID_) | target);
1614
    SETPORT(SCSISEQ, ENRESELI);
1615
 
1616
    if(TESTLO(SSTAT0, SELDI))
1617
      aha152x_panic(shpnt, "RESELI failed");
1618
 
1619
    SETPORT(SCSIRATE, HOSTDATA(shpnt)->syncrate[target]&0x7f);
1620
 
1621
    SETPORT(SCSISIG, P_MSGI);
1622
 
1623
    /* Get identify message */
1624
    if((i=getphase(shpnt))!=P_MSGI) {
1625
      printk("target doesn't enter MSGI to identify (phase=%02x)\n", i);
1626
      aha152x_panic(shpnt, "unknown lun");
1627
    }
1628
    SETPORT(SCSISEQ, 0);
1629
 
1630
    SETPORT(SXFRCTL0, CH1);
1631
 
1632
    identify_msg = GETPORT(SCSIBUS);
1633
 
1634
    if(!(identify_msg & IDENTIFY_BASE)) {
1635
      printk("target=%d, inbound message (%02x) != IDENTIFY\n",
1636
             target, identify_msg);
1637
      aha152x_panic(shpnt, "unknown lun");
1638
    }
1639
 
1640
 
1641
#if defined(DEBUG_QUEUES)
1642
    if(HOSTDATA(shpnt)->debug & debug_queues)
1643
      printk("identify=%02x, lun=%d, ", identify_msg, identify_msg & 0x3f);
1644
#endif
1645
 
1646
    save_flags(flags);
1647
    cli();
1648
 
1649
#if defined(DEBUG_QUEUES)
1650
    if(HOSTDATA(shpnt)->debug & debug_queues)
1651
      printk("d-, ");
1652
#endif
1653
    CURRENT_SC = remove_SC(&DISCONNECTED_SC, target, identify_msg & 0x3f);
1654
 
1655
    if(!CURRENT_SC) {
1656
      printk("lun=%d, ", identify_msg & 0x3f);
1657
      aha152x_panic(shpnt, "no disconnected command for that lun");
1658
    }
1659
 
1660
    CURRENT_SC->SCp.phase &= ~disconnected;
1661
    restore_flags(flags);
1662
 
1663
    make_acklow(shpnt);
1664
    if(getphase(shpnt)!=P_MSGI) {
1665
      SETPORT(SIMODE0, 0);
1666
      SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
1667
#if defined(DEBUG_RACE)
1668
      leave_driver("(reselected) intr");
1669
#endif
1670
      SETBITS(DMACNTRL0, INTEN);
1671
      return;
1672
    }
1673
  }
1674
 
1675
  /* Check, if we aren't busy with a command */
1676
  if(!CURRENT_SC) {
1677
    /* bus is free to issue a queued command */
1678
    if(TESTHI(SSTAT1, BUSFREE) && ISSUE_SC) {
1679
      save_flags(flags);
1680
      cli();
1681
#if defined(DEBUG_QUEUES)
1682
      if(HOSTDATA(shpnt)->debug & debug_queues)
1683
        printk("i-, ");
1684
#endif
1685
      CURRENT_SC = remove_first_SC(&ISSUE_SC);
1686
      restore_flags(flags);
1687
 
1688
#if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1689
      if(HOSTDATA(shpnt)->debug & (debug_intr|debug_selection|debug_phases))
1690
        printk("issuing command, ");
1691
#endif
1692
      CURRENT_SC->SCp.phase = in_selection;
1693
 
1694
#if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1695
      if(HOSTDATA(shpnt)->debug & (debug_intr|debug_selection|debug_phases))
1696
        printk("selecting %d, ", CURRENT_SC->target);
1697
#endif
1698
      SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
1699
 
1700
      /* Enable interrupts for SELECTION OUT DONE and SELECTION OUT INITIATED */
1701
      SETPORT(SXFRCTL1, HOSTDATA(shpnt)->parity ? (ENSPCHK|ENSTIMER) : ENSTIMER);
1702
 
1703
      /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
1704
      SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1705
      SETPORT(SIMODE1, ENSELTIMO);
1706
 
1707
      /* Enable SELECTION OUT sequence */
1708
      SETBITS(SCSISEQ, ENSELO | ENAUTOATNO);
1709
 
1710
    } else {
1711
      /* No command we are busy with and no new to issue */
1712
      printk("aha152x: ignoring spurious interrupt, nothing to do\n");
1713
      if(TESTHI(DMACNTRL0, SWINT)) {
1714
        printk("aha152x: SWINT is set!  Why?\n");
1715
        CLRBITS(DMACNTRL0, SWINT);
1716
      }
1717
      show_queues(shpnt);
1718
    }
1719
 
1720
#if defined(DEBUG_RACE)
1721
    leave_driver("(selecting) intr");
1722
#endif
1723
    SETBITS(DMACNTRL0, INTEN);
1724
          return;
1725
  }
1726
 
1727
  /* the bus is busy with something */
1728
 
1729
#if defined(DEBUG_INTR)
1730
  if(HOSTDATA(shpnt)->debug & debug_intr)
1731
    disp_ports(shpnt);
1732
#endif
1733
 
1734
  /* we are waiting for the result of a selection attempt */
1735
  if(CURRENT_SC->SCp.phase & in_selection) {
1736
    if(TESTLO(SSTAT1, SELTO)) {
1737
      /* no timeout */
1738
      if(TESTHI(SSTAT0, SELDO)) {
1739
        /* clear BUS FREE interrupt */
1740
        SETPORT(SSTAT1, CLRBUSFREE);
1741
 
1742
        /* Disable SELECTION OUT sequence */
1743
        CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO);
1744
 
1745
        /* Disable SELECTION OUT DONE interrupt */
1746
        CLRBITS(SIMODE0, ENSELDO);
1747
        CLRBITS(SIMODE1, ENSELTIMO);
1748
 
1749
        if(TESTLO(SSTAT0, SELDO)) {
1750
          printk("aha152x: passing bus free condition\n");
1751
 
1752
#if defined(DEBUG_RACE)
1753
          leave_driver("(passing bus free) intr");
1754
#endif
1755
          SETBITS(DMACNTRL0, INTEN);
1756
 
1757
          if(CURRENT_SC->SCp.phase & aborted) {
1758
            HOSTDATA(shpnt)->abort_result=SCSI_ABORT_ERROR;
1759
            HOSTDATA(shpnt)->abortion_complete++;
1760
          }
1761
 
1762
          aha152x_done(shpnt, DID_NO_CONNECT << 16);
1763
 
1764
          return;
1765
        }
1766
#if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1767
        if(HOSTDATA(shpnt)->debug & (debug_selection|debug_phases))
1768
          printk("SELDO (SELID=%x), ", GETPORT(SELID));
1769
#endif
1770
 
1771
        /* selection was done */
1772
        SETPORT(SSTAT0, CLRSELDO);
1773
 
1774
#if defined(DEBUG_ABORT)
1775
        if((HOSTDATA(shpnt)->debug & debug_abort) && (CURRENT_SC->SCp.phase & aborted))
1776
          printk("(ABORT) target selected, ");
1777
#endif
1778
 
1779
        CURRENT_SC->SCp.phase &= ~in_selection;
1780
        CURRENT_SC->SCp.phase |= in_other;
1781
 
1782
        ADDMSG(IDENTIFY(HOSTDATA(shpnt)->reconnect,CURRENT_SC->lun));
1783
 
1784
        if(!(SYNCRATE&0x80) && HOSTDATA(shpnt)->synchronous) {
1785
          ADDMSG(EXTENDED_MESSAGE);
1786
          ADDMSG(3);
1787
          ADDMSG(EXTENDED_SDTR);
1788
          ADDMSG(50);
1789
          ADDMSG(8);
1790
 
1791
          printk("outbound SDTR: ");
1792
          print_msg(&MSG(MSGLEN-5));
1793
 
1794
          SYNCRATE=0x80;
1795
          CURRENT_SC->SCp.phase |= in_sync;
1796
        }
1797
 
1798
#if defined(DEBUG_RACE)
1799
        leave_driver("(SELDO) intr");
1800
#endif
1801
        SETPORT(SCSIRATE, SYNCRATE&0x7f);
1802
 
1803
        SETPORT(SCSISIG, P_MSGO);
1804
 
1805
        SETPORT(SIMODE0, 0);
1806
        SETPORT(SIMODE1, ENREQINIT|ENBUSFREE);
1807
        SETBITS(DMACNTRL0, INTEN);
1808
 
1809
        return;
1810
      } else
1811
        aha152x_panic(shpnt, "neither timeout nor selection\007");
1812
    } else {
1813
#if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1814
      if(HOSTDATA(shpnt)->debug & (debug_selection|debug_phases))
1815
        printk("SELTO, ");
1816
#endif
1817
      /* end selection attempt */
1818
      CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO);
1819
 
1820
      /* timeout */
1821
      SETPORT(SSTAT1, CLRSELTIMO);
1822
 
1823
      SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1824
      SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1825
      SETBITS(DMACNTRL0, INTEN);
1826
#if defined(DEBUG_RACE)
1827
      leave_driver("(SELTO) intr");
1828
#endif
1829
 
1830
      if(CURRENT_SC->SCp.phase & aborted) {
1831
#if defined(DEBUG_ABORT)
1832
        if(HOSTDATA(shpnt)->debug & debug_abort)
1833
          printk("(ABORT) selection timeout, ");
1834
#endif
1835
        HOSTDATA(shpnt)->abort_result=SCSI_ABORT_ERROR;
1836
        HOSTDATA(shpnt)->abortion_complete++;
1837
      }
1838
 
1839
      if(TESTLO(SSTAT0, SELINGO))
1840
        /* ARBITRATION not won */
1841
        aha152x_done(shpnt, DID_BUS_BUSY << 16);
1842
      else
1843
        /* ARBITRATION won, but SELECTION failed */
1844
        aha152x_done(shpnt, DID_NO_CONNECT << 16);
1845
 
1846
      return;
1847
    }
1848
  }
1849
 
1850
  /* enable interrupt, when target leaves current phase */
1851
  phase = getphase(shpnt);
1852
  if(!(phase & ~P_MASK))                                      /* "real" phase */
1853
    SETPORT(SCSISIG, phase);
1854
  SETPORT(SSTAT1, CLRPHASECHG);
1855
  CURRENT_SC->SCp.phase =
1856
    (CURRENT_SC->SCp.phase & ~((P_MASK|1)<<16)) | (phase << 16);
1857
 
1858
  /* information transfer phase */
1859
  switch(phase) {
1860
  case P_MSGO:                                               /* MESSAGE OUT */
1861
    {
1862
      int i, identify=0, abort=0;
1863
 
1864
#if defined(DEBUG_INTR) || defined(DEBUG_MSGO) || defined(DEBUG_PHASES)
1865
      if(HOSTDATA(shpnt)->debug & (debug_intr|debug_msgo|debug_phases))
1866
        printk("MESSAGE OUT, ");
1867
#endif
1868
      if(MSGLEN==0) {
1869
        ADDMSG(MESSAGE_REJECT);
1870
#if defined(DEBUG_MSGO)
1871
        if(HOSTDATA(shpnt)->debug & debug_msgo)
1872
          printk("unexpected MESSAGE OUT phase; rejecting, ");
1873
#endif
1874
      }
1875
 
1876
      CLRBITS(SXFRCTL0, ENDMA);
1877
 
1878
      SETPORT(SIMODE0, 0);
1879
      SETPORT(SIMODE1, ENPHASEMIS|ENREQINIT|ENBUSFREE);
1880
 
1881
      /* wait for data latch to become ready or a phase change */
1882
      while(TESTLO(DMASTAT, INTSTAT))
1883
        barrier();
1884
 
1885
#if defined(DEBUG_MSGO)
1886
      if(HOSTDATA(shpnt)->debug & debug_msgo) {
1887
        int i;
1888
 
1889
        printk("messages (");
1890
        for(i=0; i<MSGLEN; i+=print_msg(&MSG(i)), printk(" "))
1891
          ;
1892
        printk("), ");
1893
      }
1894
#endif
1895
 
1896
      for(i=0; i<MSGLEN && TESTLO(SSTAT1, PHASEMIS); i++) {
1897
#if defined(DEBUG_MSGO)
1898
        if(HOSTDATA(shpnt)->debug & debug_msgo)
1899
          printk("%x ", MSG(i));
1900
#endif
1901
        if(i==MSGLEN-1) {
1902
          /* Leave MESSAGE OUT after transfer */
1903
          SETPORT(SSTAT1, CLRATNO);
1904
        }
1905
 
1906
        SETPORT(SCSIDAT, MSG(i));
1907
 
1908
        make_acklow(shpnt);
1909
        getphase(shpnt);
1910
 
1911
        if(MSG(i)==IDENTIFY(HOSTDATA(shpnt)->reconnect,CURRENT_SC->lun))
1912
          identify++;
1913
 
1914
        if(MSG(i)==ABORT)
1915
          abort++;
1916
 
1917
      }
1918
 
1919
      MSGLEN=0;
1920
 
1921
      if(identify)
1922
        CURRENT_SC->SCp.phase |= sent_ident;
1923
 
1924
      if(abort) {
1925
        /* revive abort(); abort() enables interrupts */
1926
        HOSTDATA(shpnt)->abort_result=SCSI_ABORT_SUCCESS;
1927
        HOSTDATA(shpnt)->abortion_complete++;
1928
 
1929
        CURRENT_SC->SCp.phase &= ~(P_MASK<<16);
1930
 
1931
        /* exit */
1932
        SETBITS(DMACNTRL0, INTEN);
1933
#if defined(DEBUG_RACE)
1934
        leave_driver("(ABORT) intr");
1935
#endif
1936
        aha152x_done(shpnt, DID_ABORT<<16);
1937
 
1938
        return;
1939
      }
1940
    }
1941
    break;
1942
 
1943
  case P_CMD:                                          /* COMMAND phase */
1944
#if defined(DEBUG_INTR) || defined(DEBUG_CMD) || defined(DEBUG_PHASES)
1945
    if(HOSTDATA(shpnt)->debug & (debug_intr|debug_cmd|debug_phases))
1946
      printk("COMMAND, ");
1947
#endif
1948
    if(!(CURRENT_SC->SCp.sent_command)) {
1949
      int i;
1950
 
1951
      CLRBITS(SXFRCTL0, ENDMA);
1952
 
1953
      SETPORT(SIMODE0, 0);
1954
      SETPORT(SIMODE1, ENPHASEMIS|ENREQINIT|ENBUSFREE);
1955
 
1956
      /* wait for data latch to become ready or a phase change */
1957
      while(TESTLO(DMASTAT, INTSTAT))
1958
        barrier();
1959
 
1960
      for(i=0; i<CURRENT_SC->cmd_len && TESTLO(SSTAT1, PHASEMIS); i++) {
1961
        SETPORT(SCSIDAT, CURRENT_SC->cmnd[i]);
1962
 
1963
        make_acklow(shpnt);
1964
        getphase(shpnt);
1965
      }
1966
 
1967
      if(i<CURRENT_SC->cmd_len && TESTHI(SSTAT1, PHASEMIS))
1968
        aha152x_panic(shpnt, "target left COMMAND");
1969
 
1970
      CURRENT_SC->SCp.sent_command++;
1971
    } else
1972
      aha152x_panic(shpnt, "Nothing to send while in COMMAND");
1973
      break;
1974
 
1975
  case P_MSGI:                                          /* MESSAGE IN phase */
1976
    {
1977
      int start_sync=0;
1978
 
1979
#if defined(DEBUG_INTR) || defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1980
      if(HOSTDATA(shpnt)->debug & (debug_intr|debug_msgi|debug_phases))
1981
        printk("MESSAGE IN, ");
1982
#endif
1983
      SETPORT(SXFRCTL0, CH1);
1984
 
1985
      SETPORT(SIMODE0, 0);
1986
      SETPORT(SIMODE1, ENBUSFREE);
1987
 
1988
      while(phase == P_MSGI) {
1989
        CURRENT_SC->SCp.Message = GETPORT(SCSIDAT);
1990
        switch(CURRENT_SC->SCp.Message) {
1991
        case DISCONNECT:
1992
#if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1993
          if(HOSTDATA(shpnt)->debug & (debug_msgi|debug_phases))
1994
            printk("target disconnected, ");
1995
#endif
1996
          CURRENT_SC->SCp.Message = 0;
1997
          CURRENT_SC->SCp.phase   |= disconnected;
1998
          if(!HOSTDATA(shpnt)->reconnect)
1999
            aha152x_panic(shpnt, "target was not allowed to disconnect");
2000
 
2001
          break;
2002
 
2003
        case COMMAND_COMPLETE:
2004
#if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
2005
          if(HOSTDATA(shpnt)->debug & (debug_msgi|debug_phases))
2006
            printk("inbound message (COMMAND COMPLETE), ");
2007
#endif
2008
          done++;
2009
          break;
2010
 
2011
        case MESSAGE_REJECT:
2012
          if(CURRENT_SC->SCp.phase & in_sync) {
2013
            CURRENT_SC->SCp.phase &= ~in_sync;
2014
            SYNCRATE=0x80;
2015
            printk("synchronous rejected, ");
2016
          } else
2017
            printk("inbound message (MESSAGE REJECT), ");
2018
#if defined(DEBUG_MSGI)
2019
          if(HOSTDATA(shpnt)->debug & debug_msgi)
2020
            printk("inbound message (MESSAGE REJECT), ");
2021
#endif
2022
          break;
2023
 
2024
        case SAVE_POINTERS:
2025
#if defined(DEBUG_MSGI)
2026
          if(HOSTDATA(shpnt)->debug & debug_msgi)
2027
            printk("inbound message (SAVE DATA POINTERS), ");
2028
#endif
2029
          break;
2030
 
2031
        case RESTORE_POINTERS:
2032
#if defined(DEBUG_MSGI)
2033
          if(HOSTDATA(shpnt)->debug & debug_msgi)
2034
            printk("inbound message (RESTORE DATA POINTERS), ");
2035
#endif
2036
          break;
2037
 
2038
        case EXTENDED_MESSAGE:
2039
          {
2040
            char buffer[16];
2041
            int  i;
2042
 
2043
#if defined(DEBUG_MSGI)
2044
            if(HOSTDATA(shpnt)->debug & debug_msgi)
2045
              printk("inbound message (EXTENDED MESSAGE), ");
2046
#endif
2047
            make_acklow(shpnt);
2048
            if(getphase(shpnt)!=P_MSGI)
2049
              break;
2050
 
2051
            buffer[0]=EXTENDED_MESSAGE;
2052
            buffer[1]=GETPORT(SCSIDAT);
2053
 
2054
            for(i=0; i<buffer[1] &&
2055
                     (make_acklow(shpnt), getphase(shpnt)==P_MSGI); i++)
2056
              buffer[2+i]=GETPORT(SCSIDAT);
2057
 
2058
#if defined(DEBUG_MSGI)
2059
            if(HOSTDATA(shpnt)->debug & debug_msgi)
2060
              print_msg(buffer);
2061
#endif
2062
 
2063
            switch(buffer [2]) {
2064
            case EXTENDED_SDTR:
2065
              {
2066
                long ticks;
2067
 
2068
                if(buffer[1]!=3)
2069
                  aha152x_panic(shpnt, "SDTR message length != 3");
2070
 
2071
                if(!HOSTDATA(shpnt)->synchronous)
2072
                  break;
2073
 
2074
                printk("inbound SDTR: "); print_msg(buffer);
2075
 
2076
                ticks=(buffer[3]*4+49)/50;
2077
 
2078
                if(CURRENT_SC->SCp.phase & in_sync) {
2079
                  /* we initiated SDTR */
2080
                  if(ticks>9 || buffer[4]<1 || buffer[4]>8)
2081
                    aha152x_panic(shpnt, "received SDTR invalid");
2082
 
2083
                  SYNCRATE |= ((ticks-2)<<4) + buffer[4];
2084
                } else if(ticks<=9 && buffer[4]>=1) {
2085
                  if(buffer[4]>8)
2086
                    buffer[4]=8;
2087
 
2088
                  ADDMSG(EXTENDED_MESSAGE);
2089
                  ADDMSG(3);
2090
                  ADDMSG(EXTENDED_SDTR);
2091
                  if(ticks<4) {
2092
                    ticks=4;
2093
                    ADDMSG(50);
2094
                  } else
2095
                    ADDMSG(buffer[3]);
2096
 
2097
                  ADDMSG(buffer[4]);
2098
 
2099
                  printk("outbound SDTR: ");
2100
                  print_msg(&MSG(MSGLEN-5));
2101
 
2102
                  CURRENT_SC->SCp.phase |= in_sync;
2103
 
2104
                  SYNCRATE |= ((ticks-2)<<4) + buffer[4];
2105
 
2106
                  start_sync++;
2107
                } else {
2108
                  /* requested SDTR is too slow, do it asynchronously */
2109
                  ADDMSG(MESSAGE_REJECT);
2110
                  SYNCRATE = 0;
2111
                }
2112
 
2113
                SETPORT(SCSIRATE, SYNCRATE&0x7f);
2114
              }
2115
              break;
2116
 
2117
            case EXTENDED_MODIFY_DATA_POINTER:
2118
            case EXTENDED_EXTENDED_IDENTIFY:
2119
            case EXTENDED_WDTR:
2120
            default:
2121
              ADDMSG(MESSAGE_REJECT);
2122
              break;
2123
            }
2124
          }
2125
          break;
2126
 
2127
        default:
2128
          printk("unsupported inbound message %x, ", CURRENT_SC->SCp.Message);
2129
          break;
2130
 
2131
        }
2132
 
2133
        make_acklow(shpnt);
2134
        phase=getphase(shpnt);
2135
      }
2136
 
2137
      if(start_sync)
2138
        CURRENT_SC->SCp.phase |= in_sync;
2139
      else
2140
        CURRENT_SC->SCp.phase &= ~in_sync;
2141
 
2142
      if(MSGLEN>0)
2143
        SETPORT(SCSISIG, P_MSGI|ATNO);
2144
 
2145
      /* clear SCSI fifo on BUSFREE */
2146
      if(phase==P_BUSFREE)
2147
        SETPORT(SXFRCTL0, CH1|CLRCH1);
2148
 
2149
      if(CURRENT_SC->SCp.phase & disconnected) {
2150
        save_flags(flags);
2151
        cli();
2152
#if defined(DEBUG_QUEUES)
2153
        if(HOSTDATA(shpnt)->debug & debug_queues)
2154
          printk("d+, ");
2155
#endif
2156
        append_SC(&DISCONNECTED_SC, CURRENT_SC);
2157
        CURRENT_SC->SCp.phase |= 1<<16;
2158
        CURRENT_SC = NULL;
2159
        restore_flags(flags);
2160
 
2161
        SETBITS(SCSISEQ, ENRESELI);
2162
 
2163
        SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
2164
        SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
2165
 
2166
        SETBITS(DMACNTRL0, INTEN);
2167
 
2168
        return;
2169
      }
2170
    }
2171
    break;
2172
 
2173
  case P_STATUS:                                         /* STATUS IN phase */
2174
#if defined(DEBUG_STATUS) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
2175
    if(HOSTDATA(shpnt)->debug & (debug_status|debug_intr|debug_phases))
2176
      printk("STATUS, ");
2177
#endif
2178
    SETPORT(SXFRCTL0, CH1);
2179
 
2180
    SETPORT(SIMODE0, 0);
2181
    SETPORT(SIMODE1, ENREQINIT|ENBUSFREE);
2182
 
2183
    if(TESTHI(SSTAT1, PHASEMIS))
2184
      printk("aha152x: passing STATUS phase");
2185
 
2186
    CURRENT_SC->SCp.Status = GETPORT(SCSIBUS);
2187
    make_acklow(shpnt);
2188
    getphase(shpnt);
2189
 
2190
#if defined(DEBUG_STATUS)
2191
    if(HOSTDATA(shpnt)->debug & debug_status) {
2192
      printk("inbound status ");
2193
      print_status(CURRENT_SC->SCp.Status);
2194
      printk(", ");
2195
    }
2196
#endif
2197
    break;
2198
 
2199
  case P_DATAI:                                            /* DATA IN phase */
2200
    {
2201
      int fifodata, data_count, done;
2202
 
2203
#if defined(DEBUG_DATAI) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
2204
      if(HOSTDATA(shpnt)->debug & (debug_datai|debug_intr|debug_phases))
2205
        printk("DATA IN, ");
2206
#endif
2207
 
2208
#if 0
2209
      if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
2210
        printk("aha152x: P_DATAI: %d(%d) bytes left in FIFO, resetting\n",
2211
               GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
2212
#endif
2213
 
2214
      /* reset host fifo */
2215
      SETPORT(DMACNTRL0, RSTFIFO);
2216
      SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
2217
 
2218
      SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2219
 
2220
      SETPORT(SIMODE0, 0);
2221
      SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
2222
 
2223
      /* done is set when the FIFO is empty after the target left DATA IN */
2224
      done=0;
2225
 
2226
      /* while the target stays in DATA to transfer data */
2227
      while (!done) {
2228
#if defined(DEBUG_DATAI)
2229
        if(HOSTDATA(shpnt)->debug & debug_datai)
2230
          printk("expecting data, ");
2231
#endif
2232
        /* wait for PHASEMIS or full FIFO */
2233
        while(TESTLO(DMASTAT, DFIFOFULL|INTSTAT))
2234
          barrier();
2235
 
2236
#if defined(DEBUG_DATAI)
2237
        if(HOSTDATA(shpnt)->debug & debug_datai)
2238
          printk("ok, ");
2239
#endif
2240
 
2241
        if(TESTHI(DMASTAT, DFIFOFULL))
2242
          fifodata=GETPORT(FIFOSTAT);
2243
        else {
2244
          /* wait for SCSI fifo to get empty */
2245
          while(TESTLO(SSTAT2, SEMPTY))
2246
            barrier();
2247
 
2248
          /* rest of data in FIFO */
2249
          fifodata=GETPORT(FIFOSTAT);
2250
#if defined(DEBUG_DATAI)
2251
          if(HOSTDATA(shpnt)->debug & debug_datai)
2252
            printk("last transfer, ");
2253
#endif
2254
          done=1;
2255
        }
2256
 
2257
#if defined(DEBUG_DATAI)
2258
        if(HOSTDATA(shpnt)->debug & debug_datai)
2259
          printk("fifodata=%d, ", fifodata);
2260
#endif
2261
 
2262
        while(fifodata && CURRENT_SC->SCp.this_residual) {
2263
          data_count=fifodata;
2264
 
2265
          /* limit data transfer to size of first sg buffer */
2266
          if(data_count > CURRENT_SC->SCp.this_residual)
2267
            data_count = CURRENT_SC->SCp.this_residual;
2268
 
2269
          fifodata -= data_count;
2270
 
2271
#if defined(DEBUG_DATAI)
2272
          if(HOSTDATA(shpnt)->debug & debug_datai)
2273
            printk("data_count=%d, ", data_count);
2274
#endif
2275
 
2276
          if(data_count&1) {
2277
            /* get a single byte in byte mode */
2278
            SETBITS(DMACNTRL0, _8BIT);
2279
            *CURRENT_SC->SCp.ptr++ = GETPORT(DATAPORT);
2280
            CURRENT_SC->SCp.this_residual--;
2281
          }
2282
 
2283
          if(data_count>1) {
2284
            CLRBITS(DMACNTRL0, _8BIT);
2285
            data_count >>= 1; /* Number of words */
2286
            insw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2287
#if defined(DEBUG_DATAI)
2288
            if(HOSTDATA(shpnt)->debug & debug_datai)
2289
              /* show what comes with the last transfer */
2290
              if(done) {
2291
#if 0
2292
                int           i;
2293
                unsigned char *data;
2294
#endif
2295
 
2296
                printk("data on last transfer (%d bytes) ",
2297
                       2*data_count);
2298
#if 0
2299
                printk("data on last transfer (%d bytes: ",
2300
                       2*data_count);
2301
                data = (unsigned char *) CURRENT_SC->SCp.ptr;
2302
                for(i=0; i<2*data_count; i++)
2303
                  printk("%2x ", *data++);
2304
                printk("), ");
2305
#endif
2306
              }
2307
#endif
2308
            CURRENT_SC->SCp.ptr           += 2 * data_count;
2309
            CURRENT_SC->SCp.this_residual -= 2 * data_count;
2310
          }
2311
 
2312
          /* if this buffer is full and there are more buffers left */
2313
          if(!CURRENT_SC->SCp.this_residual &&
2314
             CURRENT_SC->SCp.buffers_residual) {
2315
            /* advance to next buffer */
2316
            CURRENT_SC->SCp.buffers_residual--;
2317
            CURRENT_SC->SCp.buffer++;
2318
            CURRENT_SC->SCp.ptr           = CURRENT_SC->SCp.buffer->address;
2319
            CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2320
          }
2321
        }
2322
 
2323
        /*
2324
         * FIFO should be empty
2325
         */
2326
        if(fifodata>0) {
2327
          printk("aha152x: more data than expected (%d bytes)\n",
2328
                 GETPORT(FIFOSTAT));
2329
          SETBITS(DMACNTRL0, _8BIT);
2330
          printk("aha152x: data (");
2331
          while(fifodata--)
2332
            printk("%2x ", GETPORT(DATAPORT));
2333
            printk(")\n");
2334
        }
2335
 
2336
#if defined(DEBUG_DATAI)
2337
        if(HOSTDATA(shpnt)->debug & debug_datai)
2338
          if(!fifodata)
2339
            printk("fifo empty, ");
2340
          else
2341
           printk("something left in fifo, ");
2342
#endif
2343
      }
2344
 
2345
#if defined(DEBUG_DATAI)
2346
      if((HOSTDATA(shpnt)->debug & debug_datai) &&
2347
         (CURRENT_SC->SCp.buffers_residual ||
2348
          CURRENT_SC->SCp.this_residual))
2349
        printk("left buffers (buffers=%d, bytes=%d), ",
2350
               CURRENT_SC->SCp.buffers_residual, CURRENT_SC->SCp.this_residual);
2351
#endif
2352
      /* transfer can be considered ended, when SCSIEN reads back zero */
2353
      CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
2354
      while(TESTHI(SXFRCTL0, SCSIEN))
2355
        barrier();
2356
      CLRBITS(DMACNTRL0, ENDMA);
2357
 
2358
#if defined(DEBUG_DATAI) || defined(DEBUG_INTR)
2359
      if(HOSTDATA(shpnt)->debug & (debug_datai|debug_intr))
2360
        printk("got %d bytes, ", GETSTCNT());
2361
#endif
2362
 
2363
      CURRENT_SC->SCp.have_data_in++;
2364
    }
2365
    break;
2366
 
2367
  case P_DATAO:                                           /* DATA OUT phase */
2368
    {
2369
      int data_count;
2370
 
2371
#if defined(DEBUG_DATAO) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
2372
      if(HOSTDATA(shpnt)->debug & (debug_datao|debug_intr|debug_phases))
2373
        printk("DATA OUT, ");
2374
#endif
2375
#if defined(DEBUG_DATAO)
2376
      if(HOSTDATA(shpnt)->debug & debug_datao)
2377
        printk("got data to send (bytes=%d, buffers=%d), ",
2378
               CURRENT_SC->SCp.this_residual,
2379
               CURRENT_SC->SCp.buffers_residual);
2380
#endif
2381
 
2382
      if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT)) {
2383
        printk("%d(%d) left in FIFO, ",
2384
               GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
2385
        aha152x_panic(shpnt, "FIFO should be empty");
2386
      }
2387
 
2388
      SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1);
2389
      SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
2390
 
2391
      SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
2392
      SETPORT(DMACNTRL0, ENDMA|WRITE_READ);
2393
 
2394
      SETPORT(SIMODE0, 0);
2395
      SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
2396
 
2397
      /* while current buffer is not empty or
2398
         there are more buffers to transfer */
2399
      while(TESTLO(SSTAT1, PHASEMIS) &&
2400
            (CURRENT_SC->SCp.this_residual ||
2401
            CURRENT_SC->SCp.buffers_residual)) {
2402
#if defined(DEBUG_DATAO)
2403
        if(HOSTDATA(shpnt)->debug & debug_datao)
2404
          printk("sending data (left: bytes=%d, buffers=%d), waiting, ",
2405
                 CURRENT_SC->SCp.this_residual,
2406
                 CURRENT_SC->SCp.buffers_residual);
2407
#endif
2408
        /* transfer rest of buffer, but max. 128 byte */
2409
        data_count =
2410
          CURRENT_SC->SCp.this_residual > 128 ?
2411
          128 : CURRENT_SC->SCp.this_residual ;
2412
 
2413
#if defined(DEBUG_DATAO)
2414
        if(HOSTDATA(shpnt)->debug & debug_datao)
2415
          printk("data_count=%d, ", data_count);
2416
#endif
2417
 
2418
        if(data_count&1) {
2419
          /* put a single byte in byte mode */
2420
          SETBITS(DMACNTRL0, _8BIT);
2421
          SETPORT(DATAPORT, *CURRENT_SC->SCp.ptr++);
2422
          CURRENT_SC->SCp.this_residual--;
2423
        }
2424
        if(data_count>1) {
2425
          CLRBITS(DMACNTRL0, _8BIT);
2426
          data_count >>= 1; /* number of words */
2427
          outsw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2428
          CURRENT_SC->SCp.ptr           += 2 * data_count;
2429
          CURRENT_SC->SCp.this_residual -= 2 * data_count;
2430
        }
2431
 
2432
        /* wait for FIFO to get empty */
2433
        while(TESTLO(DMASTAT, DFIFOEMP|INTSTAT))
2434
          barrier();
2435
 
2436
#if defined(DEBUG_DATAO)
2437
        if(HOSTDATA(shpnt)->debug & debug_datao)
2438
          printk("fifo (%d bytes), transfered (%d bytes), ",
2439
                 GETPORT(FIFOSTAT), GETSTCNT());
2440
#endif
2441
 
2442
        /* if this buffer is empty and there are more buffers left */
2443
        if(TESTLO(SSTAT1, PHASEMIS) &&
2444
           !CURRENT_SC->SCp.this_residual &&
2445
           CURRENT_SC->SCp.buffers_residual) {
2446
          /* advance to next buffer */
2447
          CURRENT_SC->SCp.buffers_residual--;
2448
          CURRENT_SC->SCp.buffer++;
2449
          CURRENT_SC->SCp.ptr           = CURRENT_SC->SCp.buffer->address;
2450
          CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2451
        }
2452
      }
2453
 
2454
      if(CURRENT_SC->SCp.this_residual || CURRENT_SC->SCp.buffers_residual) {
2455
        /* target leaves DATA OUT for an other phase (perhaps disconnect) */
2456
 
2457
        /* data in fifos has to be resend */
2458
        data_count = GETPORT(SSTAT2) & (SFULL|SFCNT);
2459
 
2460
        data_count += GETPORT(FIFOSTAT) ;
2461
        CURRENT_SC->SCp.ptr           -= data_count;
2462
        CURRENT_SC->SCp.this_residual += data_count;
2463
#if defined(DEBUG_DATAO)
2464
        if(HOSTDATA(shpnt)->debug & debug_datao)
2465
          printk("left data (bytes=%d, buffers=%d), fifos (bytes=%d), "
2466
                 "transfer incomplete, resetting fifo, ",
2467
                 CURRENT_SC->SCp.this_residual,
2468
                 CURRENT_SC->SCp.buffers_residual,
2469
                 data_count);
2470
#endif
2471
        SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
2472
        CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
2473
        CLRBITS(DMACNTRL0, ENDMA);
2474
      } else {
2475
#if defined(DEBUG_DATAO)
2476
        if(HOSTDATA(shpnt)->debug & debug_datao)
2477
          printk("waiting for SCSI fifo to get empty, ");
2478
#endif
2479
        /* wait for SCSI fifo to get empty */
2480
        while(TESTLO(SSTAT2, SEMPTY))
2481
          barrier();
2482
#if defined(DEBUG_DATAO)
2483
        if(HOSTDATA(shpnt)->debug & debug_datao)
2484
          printk("ok, left data (bytes=%d, buffers=%d) ",
2485
                 CURRENT_SC->SCp.this_residual,
2486
                 CURRENT_SC->SCp.buffers_residual);
2487
#endif
2488
        CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
2489
 
2490
        /* transfer can be considered ended, when SCSIEN reads back zero */
2491
        while(TESTHI(SXFRCTL0, SCSIEN))
2492
          barrier();
2493
 
2494
        CLRBITS(DMACNTRL0, ENDMA);
2495
      }
2496
 
2497
#if defined(DEBUG_DATAO) || defined(DEBUG_INTR)
2498
      if(HOSTDATA(shpnt)->debug & (debug_datao|debug_intr))
2499
        printk("sent %d data bytes, ", GETSTCNT());
2500
#endif
2501
    }
2502
    break;
2503
 
2504
  case P_BUSFREE:                                                /* BUSFREE */
2505
#if defined(DEBUG_RACE)
2506
    leave_driver("(BUSFREE) intr");
2507
#endif
2508
#if defined(DEBUG_PHASES)
2509
    if(HOSTDATA(shpnt)->debug & debug_phases)
2510
      printk("unexpected BUS FREE, ");
2511
#endif
2512
    CURRENT_SC->SCp.phase &= ~(P_MASK<<16);
2513
 
2514
    aha152x_done(shpnt, DID_ERROR << 16);         /* Don't know any better */
2515
    return;
2516
    break;
2517
 
2518
  case P_PARITY:                              /* parity error in DATA phase */
2519
#if defined(DEBUG_RACE)
2520
    leave_driver("(DID_PARITY) intr");
2521
#endif
2522
    printk("PARITY error in DATA phase, ");
2523
 
2524
    CURRENT_SC->SCp.phase &= ~(P_MASK<<16);
2525
 
2526
    SETBITS(DMACNTRL0, INTEN);
2527
    aha152x_done(shpnt, DID_PARITY << 16);
2528
    return;
2529
    break;
2530
 
2531
  default:
2532
    printk("aha152x: unexpected phase\n");
2533
    break;
2534
  }
2535
 
2536
  if(done) {
2537
#if defined(DEBUG_INTR)
2538
    if(HOSTDATA(shpnt)->debug & debug_intr)
2539
      printk("command done.\n");
2540
#endif
2541
#if defined(DEBUG_RACE)
2542
    leave_driver("(done) intr");
2543
#endif
2544
 
2545
    SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
2546
    SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
2547
    SETPORT(SCSISEQ, DISCONNECTED_SC ? ENRESELI : 0);
2548
 
2549
    SETBITS(DMACNTRL0, INTEN);
2550
 
2551
    aha152x_done(shpnt,
2552
                 (CURRENT_SC->SCp.Status  & 0xff)
2553
                 | ((CURRENT_SC->SCp.Message & 0xff) << 8)
2554
                 | (DID_OK << 16));
2555
 
2556
#if defined(DEBUG_RACE)
2557
    printk("done returned (DID_OK: Status=%x; Message=%x).\n",
2558
           CURRENT_SC->SCp.Status, CURRENT_SC->SCp.Message);
2559
#endif
2560
    return;
2561
  }
2562
 
2563
  if(CURRENT_SC)
2564
    CURRENT_SC->SCp.phase |= 1<<16;
2565
 
2566
  SETPORT(SIMODE0, 0);
2567
  SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
2568
#if defined(DEBUG_INTR)
2569
  if(HOSTDATA(shpnt)->debug & debug_intr)
2570
    disp_enintr(shpnt);
2571
#endif
2572
#if defined(DEBUG_RACE)
2573
  leave_driver("(PHASEEND) intr");
2574
#endif
2575
 
2576
  SETBITS(DMACNTRL0, INTEN);
2577
  return;
2578
}
2579
 
2580
/*
2581
 * Dump the current driver status and panic...
2582
 */
2583
static void aha152x_panic(struct Scsi_Host *shpnt, char *msg)
2584
{
2585
  printk("\naha152x: %s\n", msg);
2586
  show_queues(shpnt);
2587
  panic("aha152x panic");
2588
}
2589
 
2590
/*
2591
 * Display registers of AIC-6260
2592
 */
2593
static void disp_ports(struct Scsi_Host *shpnt)
2594
{
2595
#ifdef DEBUG_AHA152X
2596
  int s;
2597
 
2598
#ifdef SKIP_PORTS
2599
  if(HOSTDATA(shpnt)->debug & debug_skipports)
2600
        return;
2601
#endif
2602
 
2603
  printk("\n%s: ", CURRENT_SC ? "on bus" : "waiting");
2604
 
2605
  s=GETPORT(SCSISEQ);
2606
  printk("SCSISEQ (");
2607
  if(s & TEMODEO)     printk("TARGET MODE ");
2608
  if(s & ENSELO)      printk("SELO ");
2609
  if(s & ENSELI)      printk("SELI ");
2610
  if(s & ENRESELI)    printk("RESELI ");
2611
  if(s & ENAUTOATNO)  printk("AUTOATNO ");
2612
  if(s & ENAUTOATNI)  printk("AUTOATNI ");
2613
  if(s & ENAUTOATNP)  printk("AUTOATNP ");
2614
  if(s & SCSIRSTO)    printk("SCSIRSTO ");
2615
  printk(");");
2616
 
2617
  printk(" SCSISIG (");
2618
  s=GETPORT(SCSISIG);
2619
  switch(s & P_MASK) {
2620
  case P_DATAO:
2621
    printk("DATA OUT");
2622
    break;
2623
  case P_DATAI:
2624
    printk("DATA IN");
2625
    break;
2626
  case P_CMD:
2627
    printk("COMMAND");
2628
    break;
2629
  case P_STATUS:
2630
    printk("STATUS");
2631
    break;
2632
  case P_MSGO:
2633
    printk("MESSAGE OUT");
2634
    break;
2635
  case P_MSGI:
2636
    printk("MESSAGE IN");
2637
    break;
2638
  default:
2639
    printk("*illegal*");
2640
    break;
2641
  }
2642
 
2643
  printk("); ");
2644
 
2645
  printk("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
2646
 
2647
  printk("SSTAT (");
2648
  s=GETPORT(SSTAT0);
2649
  if(s & TARGET)   printk("TARGET ");
2650
  if(s & SELDO)    printk("SELDO ");
2651
  if(s & SELDI)    printk("SELDI ");
2652
  if(s & SELINGO)  printk("SELINGO ");
2653
  if(s & SWRAP)    printk("SWRAP ");
2654
  if(s & SDONE)    printk("SDONE ");
2655
  if(s & SPIORDY)  printk("SPIORDY ");
2656
  if(s & DMADONE)  printk("DMADONE ");
2657
 
2658
  s=GETPORT(SSTAT1);
2659
  if(s & SELTO)     printk("SELTO ");
2660
  if(s & ATNTARG)   printk("ATNTARG ");
2661
  if(s & SCSIRSTI)  printk("SCSIRSTI ");
2662
  if(s & PHASEMIS)  printk("PHASEMIS ");
2663
  if(s & BUSFREE)   printk("BUSFREE ");
2664
  if(s & SCSIPERR)  printk("SCSIPERR ");
2665
  if(s & PHASECHG)  printk("PHASECHG ");
2666
  if(s & REQINIT)   printk("REQINIT ");
2667
  printk("); ");
2668
 
2669
 
2670
  printk("SSTAT (");
2671
 
2672
  s=GETPORT(SSTAT0) & GETPORT(SIMODE0);
2673
 
2674
  if(s & TARGET)    printk("TARGET ");
2675
  if(s & SELDO)     printk("SELDO ");
2676
  if(s & SELDI)     printk("SELDI ");
2677
  if(s & SELINGO)   printk("SELINGO ");
2678
  if(s & SWRAP)     printk("SWRAP ");
2679
  if(s & SDONE)     printk("SDONE ");
2680
  if(s & SPIORDY)   printk("SPIORDY ");
2681
  if(s & DMADONE)   printk("DMADONE ");
2682
 
2683
  s=GETPORT(SSTAT1) & GETPORT(SIMODE1);
2684
 
2685
  if(s & SELTO)     printk("SELTO ");
2686
  if(s & ATNTARG)   printk("ATNTARG ");
2687
  if(s & SCSIRSTI)  printk("SCSIRSTI ");
2688
  if(s & PHASEMIS)  printk("PHASEMIS ");
2689
  if(s & BUSFREE)   printk("BUSFREE ");
2690
  if(s & SCSIPERR)  printk("SCSIPERR ");
2691
  if(s & PHASECHG)  printk("PHASECHG ");
2692
  if(s & REQINIT)   printk("REQINIT ");
2693
  printk("); ");
2694
 
2695
  printk("SXFRCTL0 (");
2696
 
2697
  s=GETPORT(SXFRCTL0);
2698
  if(s & SCSIEN)    printk("SCSIEN ");
2699
  if(s & DMAEN)     printk("DMAEN ");
2700
  if(s & CH1)       printk("CH1 ");
2701
  if(s & CLRSTCNT)  printk("CLRSTCNT ");
2702
  if(s & SPIOEN)    printk("SPIOEN ");
2703
  if(s & CLRCH1)    printk("CLRCH1 ");
2704
  printk("); ");
2705
 
2706
  printk("SIGNAL (");
2707
 
2708
  s=GETPORT(SCSISIG);
2709
  if(s & ATNI)  printk("ATNI ");
2710
  if(s & SELI)  printk("SELI ");
2711
  if(s & BSYI)  printk("BSYI ");
2712
  if(s & REQI)  printk("REQI ");
2713
  if(s & ACKI)  printk("ACKI ");
2714
  printk("); ");
2715
 
2716
  printk("SELID (%02x), ", GETPORT(SELID));
2717
 
2718
  printk("SSTAT2 (");
2719
 
2720
  s=GETPORT(SSTAT2);
2721
  if(s & SOFFSET)  printk("SOFFSET ");
2722
  if(s & SEMPTY)   printk("SEMPTY ");
2723
  if(s & SFULL)    printk("SFULL ");
2724
  printk("); SFCNT (%d); ", s & (SFULL|SFCNT));
2725
 
2726
  s=GETPORT(SSTAT3);
2727
  printk("SCSICNT (%d), OFFCNT(%d), ", (s&0xf0)>>4, s&0x0f);
2728
 
2729
  printk("SSTAT4 (");
2730
  s=GETPORT(SSTAT4);
2731
  if(s & SYNCERR)   printk("SYNCERR ");
2732
  if(s & FWERR)     printk("FWERR ");
2733
  if(s & FRERR)     printk("FRERR ");
2734
  printk("); ");
2735
 
2736
  printk("DMACNTRL0 (");
2737
  s=GETPORT(DMACNTRL0);
2738
  printk("%s ", s & _8BIT      ? "8BIT"  : "16BIT");
2739
  printk("%s ", s & DMA        ? "DMA"   : "PIO"  );
2740
  printk("%s ", s & WRITE_READ ? "WRITE" : "READ" );
2741
  if(s & ENDMA)    printk("ENDMA ");
2742
  if(s & INTEN)    printk("INTEN ");
2743
  if(s & RSTFIFO)  printk("RSTFIFO ");
2744
  if(s & SWINT)    printk("SWINT ");
2745
  printk("); ");
2746
 
2747
  printk("DMASTAT (");
2748
  s=GETPORT(DMASTAT);
2749
  if(s & ATDONE)     printk("ATDONE ");
2750
  if(s & WORDRDY)    printk("WORDRDY ");
2751
  if(s & DFIFOFULL)  printk("DFIFOFULL ");
2752
  if(s & DFIFOEMP)   printk("DFIFOEMP ");
2753
  printk(")");
2754
 
2755
  printk("\n");
2756
#endif
2757
}
2758
 
2759
/*
2760
 * display enabled interrupts
2761
 */
2762
static void disp_enintr(struct Scsi_Host *shpnt)
2763
{
2764
  int s;
2765
 
2766
  printk("enabled interrupts (");
2767
 
2768
  s=GETPORT(SIMODE0);
2769
  if(s & ENSELDO)    printk("ENSELDO ");
2770
  if(s & ENSELDI)    printk("ENSELDI ");
2771
  if(s & ENSELINGO)  printk("ENSELINGO ");
2772
  if(s & ENSWRAP)    printk("ENSWRAP ");
2773
  if(s & ENSDONE)    printk("ENSDONE ");
2774
  if(s & ENSPIORDY)  printk("ENSPIORDY ");
2775
  if(s & ENDMADONE)  printk("ENDMADONE ");
2776
 
2777
  s=GETPORT(SIMODE1);
2778
  if(s & ENSELTIMO)    printk("ENSELTIMO ");
2779
  if(s & ENATNTARG)    printk("ENATNTARG ");
2780
  if(s & ENPHASEMIS)   printk("ENPHASEMIS ");
2781
  if(s & ENBUSFREE)    printk("ENBUSFREE ");
2782
  if(s & ENSCSIPERR)   printk("ENSCSIPERR ");
2783
  if(s & ENPHASECHG)   printk("ENPHASECHG ");
2784
  if(s & ENREQINIT)    printk("ENREQINIT ");
2785
  printk(")\n");
2786
}
2787
 
2788
#if defined(DEBUG_RACE)
2789
 
2790
static const char *should_leave;
2791
static int in_driver=0;
2792
 
2793
/*
2794
 * Only one routine can be in the driver at once.
2795
 */
2796
static void enter_driver(const char *func)
2797
{
2798
  unsigned long flags;
2799
 
2800
  save_flags(flags);
2801
  cli();
2802
  printk("aha152x: entering %s() (%x)\n", func, jiffies);
2803
  if(in_driver) {
2804
    printk("%s should leave first.\n", should_leave);
2805
    panic("aha152x: already in driver\n");
2806
  }
2807
 
2808
  in_driver++;
2809
  should_leave=func;
2810
  restore_flags(flags);
2811
}
2812
 
2813
static void leave_driver(const char *func)
2814
{
2815
  unsigned long flags;
2816
 
2817
  save_flags(flags);
2818
  cli();
2819
  printk("\naha152x: leaving %s() (%x)\n", func, jiffies);
2820
  if(!in_driver) {
2821
    printk("aha152x: %s already left.\n", should_leave);
2822
    panic("aha152x: %s already left driver.\n");
2823
  }
2824
 
2825
  in_driver--;
2826
  should_leave=func;
2827
  restore_flags(flags);
2828
}
2829
#endif
2830
 
2831
/*
2832
 * Show the command data of a command
2833
 */
2834
static void show_command(Scsi_Cmnd *ptr)
2835
{
2836
  printk("0x%08x: target=%d; lun=%d; cmnd=(",
2837
         (unsigned int) ptr, ptr->target, ptr->lun);
2838
 
2839
  print_command(ptr->cmnd);
2840
 
2841
  printk("); residual=%d; buffers=%d; phase |",
2842
         ptr->SCp.this_residual, ptr->SCp.buffers_residual);
2843
 
2844
  if(ptr->SCp.phase & not_issued  )  printk("not issued|");
2845
  if(ptr->SCp.phase & in_selection)  printk("in selection|");
2846
  if(ptr->SCp.phase & disconnected)  printk("disconnected|");
2847
  if(ptr->SCp.phase & aborted     )  printk("aborted|");
2848
  if(ptr->SCp.phase & sent_ident  )  printk("send_ident|");
2849
  if(ptr->SCp.phase & in_other) {
2850
    printk("; in other(");
2851
    switch((ptr->SCp.phase >> 16) & P_MASK) {
2852
    case P_DATAO:
2853
      printk("DATA OUT");
2854
      break;
2855
    case P_DATAI:
2856
      printk("DATA IN");
2857
      break;
2858
    case P_CMD:
2859
      printk("COMMAND");
2860
      break;
2861
    case P_STATUS:
2862
      printk("STATUS");
2863
      break;
2864
    case P_MSGO:
2865
      printk("MESSAGE OUT");
2866
      break;
2867
    case P_MSGI:
2868
      printk("MESSAGE IN");
2869
      break;
2870
    default:
2871
      printk("*illegal*");
2872
      break;
2873
    }
2874
    printk(")");
2875
    if(ptr->SCp.phase & (1<<16))
2876
      printk("; phaseend");
2877
  }
2878
  printk("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
2879
}
2880
 
2881
/*
2882
 * Dump the queued data
2883
 */
2884
static void show_queues(struct Scsi_Host *shpnt)
2885
{
2886
  unsigned long flags;
2887
  Scsi_Cmnd *ptr;
2888
 
2889
  save_flags(flags);
2890
  cli();
2891
  printk("QUEUE STATUS:\nissue_SC:\n");
2892
  for(ptr=ISSUE_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
2893
    show_command(ptr);
2894
 
2895
  printk("current_SC:\n");
2896
  if(CURRENT_SC)
2897
    show_command(CURRENT_SC);
2898
  else
2899
    printk("none\n");
2900
 
2901
  printk("disconnected_SC:\n");
2902
  for(ptr=DISCONNECTED_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
2903
    show_command(ptr);
2904
 
2905
  disp_ports(shpnt);
2906
  disp_enintr(shpnt);
2907
  restore_flags(flags);
2908
}
2909
 
2910
int aha152x_set_info(char *buffer, int length, struct Scsi_Host *shpnt)
2911
{
2912
  return(-ENOSYS);  /* Currently this is a no-op */
2913
}
2914
 
2915
#undef SPRINTF
2916
#define SPRINTF(args...) pos += sprintf(pos, ## args)
2917
 
2918
static int get_command(char *pos, Scsi_Cmnd *ptr)
2919
{
2920
  char *start = pos;
2921
  int i;
2922
 
2923
  SPRINTF("0x%08x: target=%d; lun=%d; cmnd=( ",
2924
          (unsigned int) ptr, ptr->target, ptr->lun);
2925
 
2926
  for(i=0; i<COMMAND_SIZE(ptr->cmnd[0]); i++)
2927
    SPRINTF("0x%02x ", ptr->cmnd[i]);
2928
 
2929
  SPRINTF("); residual=%d; buffers=%d; phase |",
2930
          ptr->SCp.this_residual, ptr->SCp.buffers_residual);
2931
 
2932
  if(ptr->SCp.phase & not_issued  )  SPRINTF("not issued|");
2933
  if(ptr->SCp.phase & in_selection)  SPRINTF("in selection|");
2934
  if(ptr->SCp.phase & disconnected)  SPRINTF("disconnected|");
2935
  if(ptr->SCp.phase & aborted     )  SPRINTF("aborted|");
2936
  if(ptr->SCp.phase & sent_ident  )  SPRINTF("send_ident|");
2937
  if(ptr->SCp.phase & in_other) {
2938
    SPRINTF("; in other(");
2939
    switch((ptr->SCp.phase >> 16) & P_MASK) {
2940
    case P_DATAO:
2941
      SPRINTF("DATA OUT");
2942
      break;
2943
    case P_DATAI:
2944
      SPRINTF("DATA IN");
2945
      break;
2946
    case P_CMD:
2947
      SPRINTF("COMMAND");
2948
      break;
2949
    case P_STATUS:
2950
      SPRINTF("STATUS");
2951
      break;
2952
    case P_MSGO:
2953
      SPRINTF("MESSAGE OUT");
2954
      break;
2955
    case P_MSGI:
2956
      SPRINTF("MESSAGE IN");
2957
      break;
2958
    default:
2959
      SPRINTF("*illegal*");
2960
      break;
2961
    }
2962
    SPRINTF(")");
2963
    if(ptr->SCp.phase & (1<<16))
2964
      SPRINTF("; phaseend");
2965
  }
2966
  SPRINTF("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
2967
 
2968
  return(pos-start);
2969
}
2970
 
2971
static int get_ports(struct Scsi_Host *shpnt, char *pos)
2972
{
2973
  char *start = pos;
2974
  int s;
2975
 
2976
#ifdef SKIP_PORTS
2977
  if(HOSTDATA(shpnt)->debug & debug_skipports)
2978
        return;
2979
#endif
2980
 
2981
  SPRINTF("\n%s: ", CURRENT_SC ? "on bus" : "waiting");
2982
 
2983
  s=GETPORT(SCSISEQ);
2984
  SPRINTF("SCSISEQ (");
2985
  if(s & TEMODEO)     SPRINTF("TARGET MODE ");
2986
  if(s & ENSELO)      SPRINTF("SELO ");
2987
  if(s & ENSELI)      SPRINTF("SELI ");
2988
  if(s & ENRESELI)    SPRINTF("RESELI ");
2989
  if(s & ENAUTOATNO)  SPRINTF("AUTOATNO ");
2990
  if(s & ENAUTOATNI)  SPRINTF("AUTOATNI ");
2991
  if(s & ENAUTOATNP)  SPRINTF("AUTOATNP ");
2992
  if(s & SCSIRSTO)    SPRINTF("SCSIRSTO ");
2993
  SPRINTF(");");
2994
 
2995
  SPRINTF(" SCSISIG (");
2996
  s=GETPORT(SCSISIG);
2997
  switch(s & P_MASK) {
2998
  case P_DATAO:
2999
    SPRINTF("DATA OUT");
3000
    break;
3001
  case P_DATAI:
3002
    SPRINTF("DATA IN");
3003
    break;
3004
  case P_CMD:
3005
    SPRINTF("COMMAND");
3006
    break;
3007
  case P_STATUS:
3008
    SPRINTF("STATUS");
3009
    break;
3010
  case P_MSGO:
3011
    SPRINTF("MESSAGE OUT");
3012
    break;
3013
  case P_MSGI:
3014
    SPRINTF("MESSAGE IN");
3015
    break;
3016
  default:
3017
    SPRINTF("*illegal*");
3018
    break;
3019
  }
3020
 
3021
  SPRINTF("); ");
3022
 
3023
  SPRINTF("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3024
 
3025
  SPRINTF("SSTAT (");
3026
  s=GETPORT(SSTAT0);
3027
  if(s & TARGET)   SPRINTF("TARGET ");
3028
  if(s & SELDO)    SPRINTF("SELDO ");
3029
  if(s & SELDI)    SPRINTF("SELDI ");
3030
  if(s & SELINGO)  SPRINTF("SELINGO ");
3031
  if(s & SWRAP)    SPRINTF("SWRAP ");
3032
  if(s & SDONE)    SPRINTF("SDONE ");
3033
  if(s & SPIORDY)  SPRINTF("SPIORDY ");
3034
  if(s & DMADONE)  SPRINTF("DMADONE ");
3035
 
3036
  s=GETPORT(SSTAT1);
3037
  if(s & SELTO)     SPRINTF("SELTO ");
3038
  if(s & ATNTARG)   SPRINTF("ATNTARG ");
3039
  if(s & SCSIRSTI)  SPRINTF("SCSIRSTI ");
3040
  if(s & PHASEMIS)  SPRINTF("PHASEMIS ");
3041
  if(s & BUSFREE)   SPRINTF("BUSFREE ");
3042
  if(s & SCSIPERR)  SPRINTF("SCSIPERR ");
3043
  if(s & PHASECHG)  SPRINTF("PHASECHG ");
3044
  if(s & REQINIT)   SPRINTF("REQINIT ");
3045
  SPRINTF("); ");
3046
 
3047
 
3048
  SPRINTF("SSTAT (");
3049
 
3050
  s=GETPORT(SSTAT0) & GETPORT(SIMODE0);
3051
 
3052
  if(s & TARGET)    SPRINTF("TARGET ");
3053
  if(s & SELDO)     SPRINTF("SELDO ");
3054
  if(s & SELDI)     SPRINTF("SELDI ");
3055
  if(s & SELINGO)   SPRINTF("SELINGO ");
3056
  if(s & SWRAP)     SPRINTF("SWRAP ");
3057
  if(s & SDONE)     SPRINTF("SDONE ");
3058
  if(s & SPIORDY)   SPRINTF("SPIORDY ");
3059
  if(s & DMADONE)   SPRINTF("DMADONE ");
3060
 
3061
  s=GETPORT(SSTAT1) & GETPORT(SIMODE1);
3062
 
3063
  if(s & SELTO)     SPRINTF("SELTO ");
3064
  if(s & ATNTARG)   SPRINTF("ATNTARG ");
3065
  if(s & SCSIRSTI)  SPRINTF("SCSIRSTI ");
3066
  if(s & PHASEMIS)  SPRINTF("PHASEMIS ");
3067
  if(s & BUSFREE)   SPRINTF("BUSFREE ");
3068
  if(s & SCSIPERR)  SPRINTF("SCSIPERR ");
3069
  if(s & PHASECHG)  SPRINTF("PHASECHG ");
3070
  if(s & REQINIT)   SPRINTF("REQINIT ");
3071
  SPRINTF("); ");
3072
 
3073
  SPRINTF("SXFRCTL0 (");
3074
 
3075
  s=GETPORT(SXFRCTL0);
3076
  if(s & SCSIEN)    SPRINTF("SCSIEN ");
3077
  if(s & DMAEN)     SPRINTF("DMAEN ");
3078
  if(s & CH1)       SPRINTF("CH1 ");
3079
  if(s & CLRSTCNT)  SPRINTF("CLRSTCNT ");
3080
  if(s & SPIOEN)    SPRINTF("SPIOEN ");
3081
  if(s & CLRCH1)    SPRINTF("CLRCH1 ");
3082
  SPRINTF("); ");
3083
 
3084
  SPRINTF("SIGNAL (");
3085
 
3086
  s=GETPORT(SCSISIG);
3087
  if(s & ATNI)  SPRINTF("ATNI ");
3088
  if(s & SELI)  SPRINTF("SELI ");
3089
  if(s & BSYI)  SPRINTF("BSYI ");
3090
  if(s & REQI)  SPRINTF("REQI ");
3091
  if(s & ACKI)  SPRINTF("ACKI ");
3092
  SPRINTF("); ");
3093
 
3094
  SPRINTF("SELID (%02x), ", GETPORT(SELID));
3095
 
3096
  SPRINTF("SSTAT2 (");
3097
 
3098
  s=GETPORT(SSTAT2);
3099
  if(s & SOFFSET)  SPRINTF("SOFFSET ");
3100
  if(s & SEMPTY)   SPRINTF("SEMPTY ");
3101
  if(s & SFULL)    SPRINTF("SFULL ");
3102
  SPRINTF("); SFCNT (%d); ", s & (SFULL|SFCNT));
3103
 
3104
  s=GETPORT(SSTAT3);
3105
  SPRINTF("SCSICNT (%d), OFFCNT(%d), ", (s&0xf0)>>4, s&0x0f);
3106
 
3107
  SPRINTF("SSTAT4 (");
3108
  s=GETPORT(SSTAT4);
3109
  if(s & SYNCERR)   SPRINTF("SYNCERR ");
3110
  if(s & FWERR)     SPRINTF("FWERR ");
3111
  if(s & FRERR)     SPRINTF("FRERR ");
3112
  SPRINTF("); ");
3113
 
3114
  SPRINTF("DMACNTRL0 (");
3115
  s=GETPORT(DMACNTRL0);
3116
  SPRINTF("%s ", s & _8BIT      ? "8BIT"  : "16BIT");
3117
  SPRINTF("%s ", s & DMA        ? "DMA"   : "PIO"  );
3118
  SPRINTF("%s ", s & WRITE_READ ? "WRITE" : "READ" );
3119
  if(s & ENDMA)    SPRINTF("ENDMA ");
3120
  if(s & INTEN)    SPRINTF("INTEN ");
3121
  if(s & RSTFIFO)  SPRINTF("RSTFIFO ");
3122
  if(s & SWINT)    SPRINTF("SWINT ");
3123
  SPRINTF("); ");
3124
 
3125
  SPRINTF("DMASTAT (");
3126
  s=GETPORT(DMASTAT);
3127
  if(s & ATDONE)     SPRINTF("ATDONE ");
3128
  if(s & WORDRDY)    SPRINTF("WORDRDY ");
3129
  if(s & DFIFOFULL)  SPRINTF("DFIFOFULL ");
3130
  if(s & DFIFOEMP)   SPRINTF("DFIFOEMP ");
3131
  SPRINTF(")\n\n");
3132
 
3133
  SPRINTF("enabled interrupts (");
3134
 
3135
  s=GETPORT(SIMODE0);
3136
  if(s & ENSELDO)    SPRINTF("ENSELDO ");
3137
  if(s & ENSELDI)    SPRINTF("ENSELDI ");
3138
  if(s & ENSELINGO)  SPRINTF("ENSELINGO ");
3139
  if(s & ENSWRAP)    SPRINTF("ENSWRAP ");
3140
  if(s & ENSDONE)    SPRINTF("ENSDONE ");
3141
  if(s & ENSPIORDY)  SPRINTF("ENSPIORDY ");
3142
  if(s & ENDMADONE)  SPRINTF("ENDMADONE ");
3143
 
3144
  s=GETPORT(SIMODE1);
3145
  if(s & ENSELTIMO)    SPRINTF("ENSELTIMO ");
3146
  if(s & ENATNTARG)    SPRINTF("ENATNTARG ");
3147
  if(s & ENPHASEMIS)   SPRINTF("ENPHASEMIS ");
3148
  if(s & ENBUSFREE)    SPRINTF("ENBUSFREE ");
3149
  if(s & ENSCSIPERR)   SPRINTF("ENSCSIPERR ");
3150
  if(s & ENPHASECHG)   SPRINTF("ENPHASECHG ");
3151
  if(s & ENREQINIT)    SPRINTF("ENREQINIT ");
3152
  SPRINTF(")\n");
3153
 
3154
  return (pos-start);
3155
}
3156
 
3157
#undef SPRINTF
3158
#define SPRINTF(args...) do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
3159
 
3160
int aha152x_proc_info(char *buffer, char **start,
3161
                      off_t offset, int length, int hostno, int inout)
3162
{
3163
  int i;
3164
  char *pos = buffer;
3165
  struct Scsi_Host *shpnt;
3166
  unsigned long flags;
3167
  Scsi_Cmnd *ptr;
3168
 
3169
  for(i=0, shpnt= (struct Scsi_Host *) NULL; i<IRQS; i++)
3170
    if(aha152x_host[i] && aha152x_host[i]->host_no == hostno)
3171
      shpnt=aha152x_host[i];
3172
 
3173
  if(!shpnt)
3174
    return(-ESRCH);
3175
 
3176
  if(inout) /* Has data been written to the file ? */
3177
    return(aha152x_set_info(buffer, length, shpnt));
3178
 
3179
  SPRINTF(AHA152X_REVID "\n");
3180
 
3181
  save_flags(flags);
3182
  cli();
3183
 
3184
  SPRINTF("ioports 0x%04x to 0x%04x\n",
3185
          shpnt->io_port, shpnt->io_port+shpnt->n_io_port-1);
3186
  SPRINTF("interrupt 0x%02x\n", shpnt->irq);
3187
  SPRINTF("disconnection/reconnection %s\n",
3188
          HOSTDATA(shpnt)->reconnect ? "enabled" : "disabled");
3189
  SPRINTF("parity checking %s\n",
3190
          HOSTDATA(shpnt)->parity ? "enabled" : "disabled");
3191
  SPRINTF("synchronous transfers %s\n",
3192
          HOSTDATA(shpnt)->synchronous ? "enabled" : "disabled");
3193
  SPRINTF("%d commands currently queued\n", HOSTDATA(shpnt)->commands);
3194
 
3195
  if(HOSTDATA(shpnt)->synchronous) {
3196
#if 0
3197
    SPRINTF("synchronously operating targets (tick=%ld ns):\n",
3198
            250000000/loops_per_sec);
3199
    for(i=0; i<8; i++)
3200
      if(HOSTDATA(shpnt)->syncrate[i]&0x7f)
3201
        SPRINTF("target %d: period %dT/%ldns; req/ack offset %d\n",
3202
                i,
3203
                (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2),
3204
                         (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2)*
3205
                         250000000/loops_per_sec,
3206
                           HOSTDATA(shpnt)->syncrate[i]&0x0f);
3207
#else
3208
    SPRINTF("synchronously operating targets (tick=50 ns):\n");
3209
    for(i=0; i<8; i++)
3210
      if(HOSTDATA(shpnt)->syncrate[i]&0x7f)
3211
        SPRINTF("target %d: period %dT/%dns; req/ack offset %d\n",
3212
                i,
3213
                (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2),
3214
                (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2)*50,
3215
                HOSTDATA(shpnt)->syncrate[i]&0x0f);
3216
#endif
3217
  }
3218
 
3219
#ifdef DEBUG_AHA152X
3220
#define PDEBUG(flags,txt) if(HOSTDATA(shpnt)->debug & flags) SPRINTF("(%s) ", txt);
3221
 
3222
  SPRINTF("enabled debugging options: ");
3223
 
3224
  PDEBUG(debug_skipports, "skip ports");
3225
  PDEBUG(debug_queue, "queue");
3226
  PDEBUG(debug_intr, "interrupt");
3227
  PDEBUG(debug_selection, "selection");
3228
  PDEBUG(debug_msgo, "message out");
3229
  PDEBUG(debug_msgi, "message in");
3230
  PDEBUG(debug_status, "status");
3231
  PDEBUG(debug_cmd, "command");
3232
  PDEBUG(debug_datai, "data in");
3233
  PDEBUG(debug_datao, "data out");
3234
  PDEBUG(debug_abort, "abort");
3235
  PDEBUG(debug_done, "done");
3236
  PDEBUG(debug_biosparam, "bios parameters");
3237
  PDEBUG(debug_phases, "phases");
3238
  PDEBUG(debug_queues, "queues");
3239
  PDEBUG(debug_reset, "reset");
3240
 
3241
  SPRINTF("\n");
3242
#endif
3243
 
3244
  SPRINTF("\nqueue status:\n");
3245
  if(ISSUE_SC) {
3246
    SPRINTF("not yet issued commands:\n");
3247
    for(ptr=ISSUE_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
3248
      pos += get_command(pos, ptr);
3249
  } else
3250
    SPRINTF("no not yet issued commands\n");
3251
 
3252
  if(CURRENT_SC) {
3253
    SPRINTF("current command:\n");
3254
    pos += get_command(pos, CURRENT_SC);
3255
  } else
3256
    SPRINTF("no current command\n");
3257
 
3258
  if(DISCONNECTED_SC) {
3259
    SPRINTF("disconnected commands:\n");
3260
    for(ptr=DISCONNECTED_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
3261
      pos += get_command(pos, ptr);
3262
  } else
3263
    SPRINTF("no disconnected commands\n");
3264
 
3265
  restore_flags(flags);
3266
 
3267
  pos += get_ports(shpnt, pos);
3268
 
3269
  *start=buffer+offset;
3270
  if (pos - buffer < offset)
3271
    return 0;
3272
  else if (pos - buffer - offset < length)
3273
    return pos - buffer - offset;
3274
  else
3275
    return length;
3276
}
3277
 
3278
#ifdef MODULE
3279
/* Eventually this will go into an include file, but this will be later */
3280
Scsi_Host_Template driver_template = AHA152X;
3281
 
3282
#include "scsi_module.c"
3283
#endif

powered by: WebSVN 2.1.0

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