Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[linux-2.6-block.git] / drivers / scsi / aha152x.c
CommitLineData
3e0a4e85 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/* aha152x.c -- Adaptec AHA-152x driver
96de0e25
JE
3 * Author: Jürgen E. Fischer, fischer@norbit.de
4 * Copyright 1993-2004 Jürgen E. Fischer
1da177e4 5 *
1da177e4
LT
6 * $Id: aha152x.c,v 2.7 2004/01/24 11:42:59 fischer Exp $
7 *
8 * $Log: aha152x.c,v $
9 * Revision 2.7 2004/01/24 11:42:59 fischer
10 * - gather code that is not used by PCMCIA at the end
11 * - move request_region for !PCMCIA case to detection
12 * - migration to new scsi host api (remove legacy code)
13 * - free host scribble before scsi_done
14 * - fix error handling
15 * - one isapnp device added to id_table
16 *
17 * Revision 2.6 2003/10/30 20:52:47 fischer
18 * - interfaces changes for kernel 2.6
19 * - aha152x_probe_one introduced for pcmcia stub
20 * - fixed pnpdev handling
21 * - instead of allocation a new one, reuse command for request sense after check condition and reset
22 * - fixes race in is_complete
23 *
24 * Revision 2.5 2002/04/14 11:24:53 fischer
25 * - isapnp support
26 * - abort fixed
27 * - 2.5 support
28 *
29 * Revision 2.4 2000/12/16 12:53:56 fischer
30 * - allow REQUEST SENSE to be queued
31 * - handle shared PCI interrupts
32 *
33 * Revision 2.3 2000/11/04 16:40:26 fischer
34 * - handle data overruns
35 * - extend timeout for data phases
36 *
37 * Revision 2.2 2000/08/08 19:54:53 fischer
38 * - minor changes
39 *
40 * Revision 2.1 2000/05/17 16:23:17 fischer
41 * - signature update
42 * - fix for data out w/o scatter gather
43 *
44 * Revision 2.0 1999/12/25 15:07:32 fischer
45 * - interrupt routine completly reworked
46 * - basic support for new eh code
47 *
48 * Revision 1.21 1999/11/10 23:46:36 fischer
49 * - default to synchronous operation
50 * - synchronous negotiation fixed
51 * - added timeout to loops
52 * - debugging output can be controlled through procfs
53 *
54 * Revision 1.20 1999/11/07 18:37:31 fischer
55 * - synchronous operation works
56 * - resid support for sg driver
57 *
58 * Revision 1.19 1999/11/02 22:39:59 fischer
59 * - moved leading comments to README.aha152x
60 * - new additional module parameters
61 * - updates for 2.3
62 * - support for the Tripace TC1550 controller
63 * - interrupt handling changed
64 *
65 * Revision 1.18 1996/09/07 20:10:40 fischer
66 * - fixed can_queue handling (multiple outstanding commands working again)
67 *
68 * Revision 1.17 1996/08/17 16:05:14 fischer
69 * - biosparam improved
70 * - interrupt verification
71 * - updated documentation
72 * - cleanups
73 *
74 * Revision 1.16 1996/06/09 00:04:56 root
75 * - added configuration symbols for insmod (aha152x/aha152x1)
76 *
77 * Revision 1.15 1996/04/30 14:52:06 fischer
78 * - proc info fixed
79 * - support for extended translation for >1GB disks
80 *
81 * Revision 1.14 1996/01/17 15:11:20 fischer
82 * - fixed lockup in MESSAGE IN phase after reconnection
83 *
84 * Revision 1.13 1996/01/09 02:15:53 fischer
85 * - some cleanups
86 * - moved request_irq behind controller initialization
87 * (to avoid spurious interrupts)
88 *
89 * Revision 1.12 1995/12/16 12:26:07 fischer
90 * - barrier()s added
91 * - configurable RESET delay added
92 *
93 * Revision 1.11 1995/12/06 21:18:35 fischer
94 * - some minor updates
95 *
96 * Revision 1.10 1995/07/22 19:18:45 fischer
97 * - support for 2 controllers
98 * - started synchronous data transfers (not working yet)
99 *
100 * Revision 1.9 1995/03/18 09:20:24 root
101 * - patches for PCMCIA and modules
102 *
103 * Revision 1.8 1995/01/21 22:07:19 root
104 * - snarf_region => request_region
105 * - aha152x_intr interface change
106 *
107 * Revision 1.7 1995/01/02 23:19:36 root
108 * - updated COMMAND_SIZE to cmd_len
109 * - changed sti() to restore_flags()
110 * - fixed some #ifdef which generated warnings
111 *
112 * Revision 1.6 1994/11/24 20:35:27 root
113 * - problem with odd number of bytes in fifo fixed
114 *
115 * Revision 1.5 1994/10/30 14:39:56 root
116 * - abort code fixed
117 * - debugging improved
118 *
119 * Revision 1.4 1994/09/12 11:33:01 root
120 * - irqaction to request_irq
121 * - abortion updated
122 *
123 * Revision 1.3 1994/08/04 13:53:05 root
124 * - updates for mid-level-driver changes
125 * - accept unexpected BUSFREE phase as error condition
126 * - parity check now configurable
127 *
128 * Revision 1.2 1994/07/03 12:56:36 root
129 * - cleaned up debugging code
130 * - more tweaking on reset delays
131 * - updated abort/reset code (pretty untested...)
132 *
133 * Revision 1.1 1994/05/28 21:18:49 root
134 * - update for mid-level interface change (abort-reset)
135 * - delays after resets adjusted for some slow devices
136 *
137 * Revision 1.0 1994/03/25 12:52:00 root
138 * - Fixed "more data than expected" problem
139 * - added new BIOS signatures
140 *
141 * Revision 0.102 1994/01/31 20:44:12 root
142 * - minor changes in insw/outsw handling
143 *
144 * Revision 0.101 1993/12/13 01:16:27 root
145 * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
146 * fixes problems with CD-ROM sector size detection & media change)
147 *
148 * Revision 0.100 1993/12/10 16:58:47 root
149 * - fix for unsuccessful selections in case of non-continuous id assignments
150 * on the scsi bus.
151 *
152 * Revision 0.99 1993/10/24 16:19:59 root
153 * - fixed DATA IN (rare read errors gone)
154 *
155 * Revision 0.98 1993/10/17 12:54:44 root
156 * - fixed some recent fixes (shame on me)
157 * - moved initialization of scratch area to aha152x_queue
158 *
159 * Revision 0.97 1993/10/09 18:53:53 root
160 * - DATA IN fixed. Rarely left data in the fifo.
161 *
162 * Revision 0.96 1993/10/03 00:53:59 root
163 * - minor changes on DATA IN
164 *
165 * Revision 0.95 1993/09/24 10:36:01 root
166 * - change handling of MSGI after reselection
167 * - fixed sti/cli
168 * - minor changes
169 *
170 * Revision 0.94 1993/09/18 14:08:22 root
171 * - fixed bug in multiple outstanding command code
172 * - changed detection
173 * - support for kernel command line configuration
174 * - reset corrected
175 * - changed message handling
176 *
177 * Revision 0.93 1993/09/15 20:41:19 root
178 * - fixed bugs with multiple outstanding commands
179 *
180 * Revision 0.92 1993/09/13 02:46:33 root
181 * - multiple outstanding commands work (no problems with IBM drive)
182 *
183 * Revision 0.91 1993/09/12 20:51:46 root
184 * added multiple outstanding commands
185 * (some problem with this $%&? IBM device remain)
186 *
187 * Revision 0.9 1993/09/12 11:11:22 root
188 * - corrected auto-configuration
189 * - changed the auto-configuration (added some '#define's)
190 * - added support for dis-/reconnection
191 *
192 * Revision 0.8 1993/09/06 23:09:39 root
193 * - added support for the drive activity light
194 * - minor changes
195 *
196 * Revision 0.7 1993/09/05 14:30:15 root
197 * - improved phase detection
198 * - now using the new snarf_region code of 0.99pl13
199 *
200 * Revision 0.6 1993/09/02 11:01:38 root
201 * first public release; added some signatures and biosparam()
202 *
203 * Revision 0.5 1993/08/30 10:23:30 root
204 * fixed timing problems with my IBM drive
205 *
206 * Revision 0.4 1993/08/29 14:06:52 root
207 * fixed some problems with timeouts due incomplete commands
208 *
209 * Revision 0.3 1993/08/28 15:55:03 root
210 * writing data works too. mounted and worked on a dos partition
211 *
212 * Revision 0.2 1993/08/27 22:42:07 root
213 * reading data works. Mounted a msdos partition.
214 *
215 * Revision 0.1 1993/08/25 13:38:30 root
216 * first "damn thing doesn't work" version
217 *
218 * Revision 0.0 1993/08/14 19:54:25 root
219 * empty function bodies; detect() works.
220 *
1da177e4 221 **************************************************************************
f75ae8ed 222
1da177e4
LT
223 see Documentation/scsi/aha152x.txt for configuration details
224
225 **************************************************************************/
226
227#include <linux/module.h>
1da177e4 228#include <asm/irq.h>
53d5ed62 229#include <linux/io.h>
1da177e4 230#include <linux/blkdev.h>
0f06bb34 231#include <linux/completion.h>
1da177e4
LT
232#include <linux/errno.h>
233#include <linux/string.h>
234#include <linux/wait.h>
235#include <linux/ioport.h>
236#include <linux/delay.h>
237#include <linux/proc_fs.h>
238#include <linux/interrupt.h>
239#include <linux/init.h>
240#include <linux/kernel.h>
241#include <linux/isapnp.h>
242#include <linux/spinlock.h>
243#include <linux/workqueue.h>
5fcda422 244#include <linux/list.h>
5a0e3ad6 245#include <linux/slab.h>
1da177e4
LT
246#include <scsi/scsicam.h>
247
248#include "scsi.h"
db9dff36 249#include <scsi/scsi_dbg.h>
1da177e4 250#include <scsi/scsi_host.h>
1abfd370 251#include <scsi/scsi_transport_spi.h>
73d2cb16 252#include <scsi/scsi_eh.h>
1da177e4
LT
253#include "aha152x.h"
254
5fcda422
JB
255static LIST_HEAD(aha152x_host_list);
256
1da177e4
LT
257
258/* DEFINES */
259
260/* For PCMCIA cards, always use AUTOCONF */
3eb2ebcb 261#if defined(AHA152X_PCMCIA) || defined(MODULE)
1da177e4
LT
262#if !defined(AUTOCONF)
263#define AUTOCONF
264#endif
265#endif
266
267#if !defined(AUTOCONF) && !defined(SETUP0)
268#error define AUTOCONF or SETUP0
269#endif
270
1da177e4
LT
271#define DO_LOCK(flags) spin_lock_irqsave(&QLOCK,flags)
272#define DO_UNLOCK(flags) spin_unlock_irqrestore(&QLOCK,flags)
1da177e4
LT
273
274#define LEAD "(scsi%d:%d:%d) "
1da177e4 275#define INFO_LEAD KERN_INFO LEAD
1da177e4
LT
276#define CMDINFO(cmd) \
277 (cmd) ? ((cmd)->device->host->host_no) : -1, \
278 (cmd) ? ((cmd)->device->id & 0x0f) : -1, \
9cb78c16 279 (cmd) ? ((u8)(cmd)->device->lun & 0x07) : -1
1da177e4 280
2338545a
BH
281static inline void
282CMD_INC_RESID(struct scsi_cmnd *cmd, int inc)
283{
284 scsi_set_resid(cmd, scsi_get_resid(cmd) + inc);
285}
286
1da177e4
LT
287#define DELAY_DEFAULT 1000
288
3eb2ebcb 289#if defined(AHA152X_PCMCIA)
1da177e4
LT
290#define IRQ_MIN 0
291#define IRQ_MAX 16
292#else
293#define IRQ_MIN 9
294#if defined(__PPC)
171ac6ae 295#define IRQ_MAX (nr_irqs-1)
1da177e4
LT
296#else
297#define IRQ_MAX 12
298#endif
299#endif
300
301enum {
302 not_issued = 0x0001, /* command not yet issued */
f75ae8ed 303 selecting = 0x0002, /* target is being selected */
1da177e4
LT
304 identified = 0x0004, /* IDENTIFY was sent */
305 disconnected = 0x0008, /* target disconnected */
f75ae8ed 306 completed = 0x0010, /* target sent COMMAND COMPLETE */
1da177e4
LT
307 aborted = 0x0020, /* ABORT was sent */
308 resetted = 0x0040, /* BUS DEVICE RESET was sent */
309 spiordy = 0x0080, /* waiting for SPIORDY to raise */
310 syncneg = 0x0100, /* synchronous negotiation in progress */
311 aborting = 0x0200, /* ABORT is pending */
312 resetting = 0x0400, /* BUS DEVICE RESET is pending */
313 check_condition = 0x0800, /* requesting sense after CHECK CONDITION */
314};
315
96de0e25 316MODULE_AUTHOR("Jürgen Fischer");
1da177e4
LT
317MODULE_DESCRIPTION(AHA152X_REVID);
318MODULE_LICENSE("GPL");
319
3eb2ebcb 320#if !defined(AHA152X_PCMCIA)
1da177e4
LT
321#if defined(MODULE)
322static int io[] = {0, 0};
88f06b76 323module_param_hw_array(io, int, ioport, NULL, 0);
1da177e4
LT
324MODULE_PARM_DESC(io,"base io address of controller");
325
326static int irq[] = {0, 0};
88f06b76 327module_param_hw_array(irq, int, irq, NULL, 0);
1da177e4
LT
328MODULE_PARM_DESC(irq,"interrupt for controller");
329
330static int scsiid[] = {7, 7};
331module_param_array(scsiid, int, NULL, 0);
332MODULE_PARM_DESC(scsiid,"scsi id of controller");
333
334static int reconnect[] = {1, 1};
335module_param_array(reconnect, int, NULL, 0);
336MODULE_PARM_DESC(reconnect,"allow targets to disconnect");
337
338static int parity[] = {1, 1};
339module_param_array(parity, int, NULL, 0);
340MODULE_PARM_DESC(parity,"use scsi parity");
341
342static int sync[] = {1, 1};
343module_param_array(sync, int, NULL, 0);
344MODULE_PARM_DESC(sync,"use synchronous transfers");
345
346static int delay[] = {DELAY_DEFAULT, DELAY_DEFAULT};
347module_param_array(delay, int, NULL, 0);
348MODULE_PARM_DESC(delay,"scsi reset delay");
349
350static int exttrans[] = {0, 0};
351module_param_array(exttrans, int, NULL, 0);
352MODULE_PARM_DESC(exttrans,"use extended translation");
353
1da177e4
LT
354static int aha152x[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
355module_param_array(aha152x, int, NULL, 0);
356MODULE_PARM_DESC(aha152x, "parameters for first controller");
357
358static int aha152x1[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
359module_param_array(aha152x1, int, NULL, 0);
360MODULE_PARM_DESC(aha152x1, "parameters for second controller");
1da177e4
LT
361#endif /* MODULE */
362
363#ifdef __ISAPNP__
6f039790 364static struct isapnp_device_id id_table[] = {
50f87f91
OZ
365 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1502), 0 },
366 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1505), 0 },
367 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1510), 0 },
368 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1515), 0 },
369 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1520), 0 },
370 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x2015), 0 },
371 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1522), 0 },
372 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x2215), 0 },
373 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1530), 0 },
374 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x3015), 0 },
375 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1532), 0 },
376 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x3215), 0 },
377 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x6360), 0 },
1da177e4
LT
378 { ISAPNP_DEVICE_SINGLE_END, }
379};
380MODULE_DEVICE_TABLE(isapnp, id_table);
381#endif /* ISAPNP */
382
3eb2ebcb 383#endif /* !AHA152X_PCMCIA */
1da177e4 384
d0be4a7d 385static struct scsi_host_template aha152x_driver_template;
1da177e4
LT
386
387/*
388 * internal states of the host
389 *
f75ae8ed 390 */
1da177e4
LT
391enum aha152x_state {
392 idle=0,
393 unknown,
394 seldo,
395 seldi,
396 selto,
397 busfree,
398 msgo,
399 cmd,
400 msgi,
401 status,
402 datai,
403 datao,
404 parerr,
405 rsti,
406 maxstate
407};
408
409/*
410 * current state information of the host
411 *
412 */
413struct aha152x_hostdata {
91ebc1fa 414 struct scsi_cmnd *issue_SC;
1da177e4
LT
415 /* pending commands to issue */
416
91ebc1fa 417 struct scsi_cmnd *current_SC;
1da177e4
LT
418 /* current command on the bus */
419
91ebc1fa 420 struct scsi_cmnd *disconnected_SC;
1da177e4
LT
421 /* commands that disconnected */
422
91ebc1fa 423 struct scsi_cmnd *done_SC;
1da177e4
LT
424 /* command that was completed */
425
426 spinlock_t lock;
427 /* host lock */
428
1da177e4 429#if defined(AHA152X_STAT)
f75ae8ed 430 int total_commands;
1da177e4
LT
431 int disconnections;
432 int busfree_without_any_action;
433 int busfree_without_old_command;
434 int busfree_without_new_command;
435 int busfree_without_done_command;
436 int busfree_with_check_condition;
f75ae8ed
HR
437 int count[maxstate];
438 int count_trans[maxstate];
1da177e4
LT
439 unsigned long time[maxstate];
440#endif
441
442 int commands; /* current number of commands */
443
444 int reconnect; /* disconnection allowed */
445 int parity; /* parity checking enabled */
446 int synchronous; /* synchronous transferes enabled */
447 int delay; /* reset out delay */
448 int ext_trans; /* extended translation enabled */
449
f75ae8ed 450 int swint; /* software-interrupt was fired during detect() */
1da177e4
LT
451 int service; /* bh needs to be run */
452 int in_intr; /* bh is running */
453
454 /* current state,
455 previous state,
456 last state different from current state */
457 enum aha152x_state state, prevstate, laststate;
458
459 int target;
460 /* reconnecting target */
461
462 unsigned char syncrate[8];
463 /* current synchronous transfer agreements */
464
465 unsigned char syncneg[8];
466 /* 0: no negotiation;
467 * 1: negotiation in progress;
468 * 2: negotiation completed
469 */
470
471 int cmd_i;
472 /* number of sent bytes of current command */
473
474 int msgi_len;
475 /* number of received message bytes */
476 unsigned char msgi[256];
477 /* received message bytes */
478
f75ae8ed 479 int msgo_i, msgo_len;
1da177e4
LT
480 /* number of sent bytes and length of current messages */
481 unsigned char msgo[256];
482 /* pending messages */
483
484 int data_len;
485 /* number of sent/received bytes in dataphase */
486
487 unsigned long io_port0;
488 unsigned long io_port1;
489
490#ifdef __ISAPNP__
491 struct pnp_dev *pnpdev;
492#endif
5fcda422 493 struct list_head host_list;
1da177e4
LT
494};
495
496
497/*
498 * host specific command extension
499 *
500 */
501struct aha152x_scdata {
91ebc1fa 502 struct scsi_cmnd *next; /* next sc in queue */
0f06bb34 503 struct completion *done;/* semaphore to block on */
73d2cb16 504 struct scsi_eh_save ses;
1da177e4
LT
505};
506
1da177e4
LT
507/* access macros for hostdata */
508
509#define HOSTDATA(shpnt) ((struct aha152x_hostdata *) &shpnt->hostdata)
510
511#define HOSTNO ((shpnt)->host_no)
512
513#define CURRENT_SC (HOSTDATA(shpnt)->current_SC)
514#define DONE_SC (HOSTDATA(shpnt)->done_SC)
515#define ISSUE_SC (HOSTDATA(shpnt)->issue_SC)
516#define DISCONNECTED_SC (HOSTDATA(shpnt)->disconnected_SC)
517#define QLOCK (HOSTDATA(shpnt)->lock)
518#define QLOCKER (HOSTDATA(shpnt)->locker)
519#define QLOCKERL (HOSTDATA(shpnt)->lockerl)
520
521#define STATE (HOSTDATA(shpnt)->state)
522#define PREVSTATE (HOSTDATA(shpnt)->prevstate)
523#define LASTSTATE (HOSTDATA(shpnt)->laststate)
524
525#define RECONN_TARGET (HOSTDATA(shpnt)->target)
526
527#define CMD_I (HOSTDATA(shpnt)->cmd_i)
528
529#define MSGO(i) (HOSTDATA(shpnt)->msgo[i])
530#define MSGO_I (HOSTDATA(shpnt)->msgo_i)
531#define MSGOLEN (HOSTDATA(shpnt)->msgo_len)
532#define ADDMSGO(x) (MSGOLEN<256 ? (void)(MSGO(MSGOLEN++)=x) : aha152x_error(shpnt,"MSGO overflow"))
533
534#define MSGI(i) (HOSTDATA(shpnt)->msgi[i])
535#define MSGILEN (HOSTDATA(shpnt)->msgi_len)
536#define ADDMSGI(x) (MSGILEN<256 ? (void)(MSGI(MSGILEN++)=x) : aha152x_error(shpnt,"MSGI overflow"))
537
538#define DATA_LEN (HOSTDATA(shpnt)->data_len)
539
540#define SYNCRATE (HOSTDATA(shpnt)->syncrate[CURRENT_SC->device->id])
541#define SYNCNEG (HOSTDATA(shpnt)->syncneg[CURRENT_SC->device->id])
542
543#define DELAY (HOSTDATA(shpnt)->delay)
544#define EXT_TRANS (HOSTDATA(shpnt)->ext_trans)
545#define TC1550 (HOSTDATA(shpnt)->tc1550)
546#define RECONNECT (HOSTDATA(shpnt)->reconnect)
547#define PARITY (HOSTDATA(shpnt)->parity)
548#define SYNCHRONOUS (HOSTDATA(shpnt)->synchronous)
549
550#define HOSTIOPORT0 (HOSTDATA(shpnt)->io_port0)
551#define HOSTIOPORT1 (HOSTDATA(shpnt)->io_port1)
552
553#define SCDATA(SCpnt) ((struct aha152x_scdata *) (SCpnt)->host_scribble)
554#define SCNEXT(SCpnt) SCDATA(SCpnt)->next
0f06bb34 555#define SCSEM(SCpnt) SCDATA(SCpnt)->done
1da177e4 556
45711f1a 557#define SG_ADDRESS(buffer) ((char *) sg_virt((buffer)))
1da177e4
LT
558
559/* state handling */
560static void seldi_run(struct Scsi_Host *shpnt);
561static void seldo_run(struct Scsi_Host *shpnt);
562static void selto_run(struct Scsi_Host *shpnt);
563static void busfree_run(struct Scsi_Host *shpnt);
564
565static void msgo_init(struct Scsi_Host *shpnt);
566static void msgo_run(struct Scsi_Host *shpnt);
567static void msgo_end(struct Scsi_Host *shpnt);
568
569static void cmd_init(struct Scsi_Host *shpnt);
570static void cmd_run(struct Scsi_Host *shpnt);
571static void cmd_end(struct Scsi_Host *shpnt);
572
573static void datai_init(struct Scsi_Host *shpnt);
574static void datai_run(struct Scsi_Host *shpnt);
575static void datai_end(struct Scsi_Host *shpnt);
576
577static void datao_init(struct Scsi_Host *shpnt);
578static void datao_run(struct Scsi_Host *shpnt);
579static void datao_end(struct Scsi_Host *shpnt);
580
581static void status_run(struct Scsi_Host *shpnt);
582
583static void msgi_run(struct Scsi_Host *shpnt);
584static void msgi_end(struct Scsi_Host *shpnt);
585
586static void parerr_run(struct Scsi_Host *shpnt);
587static void rsti_run(struct Scsi_Host *shpnt);
588
589static void is_complete(struct Scsi_Host *shpnt);
590
591/*
592 * driver states
593 *
594 */
595static struct {
596 char *name;
597 void (*init)(struct Scsi_Host *);
598 void (*run)(struct Scsi_Host *);
599 void (*end)(struct Scsi_Host *);
600 int spio;
601} states[] = {
602 { "idle", NULL, NULL, NULL, 0},
603 { "unknown", NULL, NULL, NULL, 0},
604 { "seldo", NULL, seldo_run, NULL, 0},
605 { "seldi", NULL, seldi_run, NULL, 0},
606 { "selto", NULL, selto_run, NULL, 0},
607 { "busfree", NULL, busfree_run, NULL, 0},
608 { "msgo", msgo_init, msgo_run, msgo_end, 1},
609 { "cmd", cmd_init, cmd_run, cmd_end, 1},
610 { "msgi", NULL, msgi_run, msgi_end, 1},
611 { "status", NULL, status_run, NULL, 1},
612 { "datai", datai_init, datai_run, datai_end, 0},
613 { "datao", datao_init, datao_run, datao_end, 0},
614 { "parerr", NULL, parerr_run, NULL, 0},
615 { "rsti", NULL, rsti_run, NULL, 0},
616};
617
618/* setup & interrupt */
7d12e780 619static irqreturn_t intr(int irq, void *dev_id);
1da177e4
LT
620static void reset_ports(struct Scsi_Host *shpnt);
621static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
622static void done(struct Scsi_Host *shpnt, int error);
623
624/* diagnostics */
91ebc1fa 625static void show_command(struct scsi_cmnd * ptr);
1da177e4
LT
626static void show_queues(struct Scsi_Host *shpnt);
627static void disp_enintr(struct Scsi_Host *shpnt);
628
629
630/*
631 * queue services:
632 *
633 */
91ebc1fa 634static inline void append_SC(struct scsi_cmnd **SC, struct scsi_cmnd *new_SC)
1da177e4 635{
91ebc1fa 636 struct scsi_cmnd *end;
1da177e4
LT
637
638 SCNEXT(new_SC) = NULL;
639 if (!*SC)
640 *SC = new_SC;
641 else {
642 for (end = *SC; SCNEXT(end); end = SCNEXT(end))
643 ;
644 SCNEXT(end) = new_SC;
645 }
646}
647
91ebc1fa 648static inline struct scsi_cmnd *remove_first_SC(struct scsi_cmnd ** SC)
1da177e4 649{
91ebc1fa 650 struct scsi_cmnd *ptr;
1da177e4
LT
651
652 ptr = *SC;
653 if (ptr) {
654 *SC = SCNEXT(*SC);
655 SCNEXT(ptr)=NULL;
656 }
657 return ptr;
658}
659
91ebc1fa
JT
660static inline struct scsi_cmnd *remove_lun_SC(struct scsi_cmnd ** SC,
661 int target, int lun)
1da177e4 662{
91ebc1fa 663 struct scsi_cmnd *ptr, *prev;
1da177e4
LT
664
665 for (ptr = *SC, prev = NULL;
666 ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
667 prev = ptr, ptr = SCNEXT(ptr))
668 ;
669
670 if (ptr) {
671 if (prev)
672 SCNEXT(prev) = SCNEXT(ptr);
673 else
674 *SC = SCNEXT(ptr);
675
676 SCNEXT(ptr)=NULL;
677 }
678
679 return ptr;
680}
681
91ebc1fa
JT
682static inline struct scsi_cmnd *remove_SC(struct scsi_cmnd **SC,
683 struct scsi_cmnd *SCp)
1da177e4 684{
91ebc1fa 685 struct scsi_cmnd *ptr, *prev;
1da177e4
LT
686
687 for (ptr = *SC, prev = NULL;
688 ptr && SCp!=ptr;
689 prev = ptr, ptr = SCNEXT(ptr))
690 ;
691
692 if (ptr) {
693 if (prev)
694 SCNEXT(prev) = SCNEXT(ptr);
695 else
696 *SC = SCNEXT(ptr);
697
698 SCNEXT(ptr)=NULL;
699 }
700
701 return ptr;
702}
703
7d12e780 704static irqreturn_t swintr(int irqno, void *dev_id)
1da177e4 705{
c7bec5ab 706 struct Scsi_Host *shpnt = dev_id;
1da177e4
LT
707
708 HOSTDATA(shpnt)->swint++;
709
710 SETPORT(DMACNTRL0, INTEN);
711 return IRQ_HANDLED;
712}
713
714struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
715{
716 struct Scsi_Host *shpnt;
717
718 shpnt = scsi_host_alloc(&aha152x_driver_template, sizeof(struct aha152x_hostdata));
719 if (!shpnt) {
720 printk(KERN_ERR "aha152x: scsi_host_alloc failed\n");
721 return NULL;
722 }
723
1da177e4 724 memset(HOSTDATA(shpnt), 0, sizeof *HOSTDATA(shpnt));
5fcda422
JB
725 INIT_LIST_HEAD(&HOSTDATA(shpnt)->host_list);
726
727 /* need to have host registered before triggering any interrupt */
728 list_add_tail(&HOSTDATA(shpnt)->host_list, &aha152x_host_list);
1da177e4
LT
729
730 shpnt->io_port = setup->io_port;
731 shpnt->n_io_port = IO_RANGE;
732 shpnt->irq = setup->irq;
733
734 if (!setup->tc1550) {
735 HOSTIOPORT0 = setup->io_port;
736 HOSTIOPORT1 = setup->io_port;
737 } else {
738 HOSTIOPORT0 = setup->io_port+0x10;
739 HOSTIOPORT1 = setup->io_port-0x10;
740 }
741
742 spin_lock_init(&QLOCK);
743 RECONNECT = setup->reconnect;
744 SYNCHRONOUS = setup->synchronous;
745 PARITY = setup->parity;
746 DELAY = setup->delay;
747 EXT_TRANS = setup->ext_trans;
748
1da177e4
LT
749 SETPORT(SCSIID, setup->scsiid << 4);
750 shpnt->this_id = setup->scsiid;
751
752 if (setup->reconnect)
753 shpnt->can_queue = AHA152X_MAXQUEUE;
754
755 /* RESET OUT */
756 printk("aha152x: resetting bus...\n");
757 SETPORT(SCSISEQ, SCSIRSTO);
758 mdelay(256);
759 SETPORT(SCSISEQ, 0);
760 mdelay(DELAY);
761
762 reset_ports(shpnt);
763
764 printk(KERN_INFO
765 "aha152x%d%s: "
766 "vital data: rev=%x, "
767 "io=0x%03lx (0x%03lx/0x%03lx), "
768 "irq=%d, "
769 "scsiid=%d, "
770 "reconnect=%s, "
771 "parity=%s, "
772 "synchronous=%s, "
773 "delay=%d, "
774 "extended translation=%s\n",
775 shpnt->host_no, setup->tc1550 ? " (tc1550 mode)" : "",
776 GETPORT(REV) & 0x7,
777 shpnt->io_port, HOSTIOPORT0, HOSTIOPORT1,
778 shpnt->irq,
779 shpnt->this_id,
780 RECONNECT ? "enabled" : "disabled",
781 PARITY ? "enabled" : "disabled",
782 SYNCHRONOUS ? "enabled" : "disabled",
783 DELAY,
784 EXT_TRANS ? "enabled" : "disabled");
785
786 /* not expecting any interrupts */
787 SETPORT(SIMODE0, 0);
788 SETPORT(SIMODE1, 0);
789
4909cc2b 790 if (request_irq(shpnt->irq, swintr, IRQF_SHARED, "aha152x", shpnt)) {
1da177e4
LT
791 printk(KERN_ERR "aha152x%d: irq %d busy.\n", shpnt->host_no, shpnt->irq);
792 goto out_host_put;
793 }
794
795 HOSTDATA(shpnt)->swint = 0;
796
797 printk(KERN_INFO "aha152x%d: trying software interrupt, ", shpnt->host_no);
798
799 mb();
800 SETPORT(DMACNTRL0, SWINT|INTEN);
801 mdelay(1000);
802 free_irq(shpnt->irq, shpnt);
803
804 if (!HOSTDATA(shpnt)->swint) {
805 if (TESTHI(DMASTAT, INTSTAT)) {
806 printk("lost.\n");
807 } else {
808 printk("failed.\n");
809 }
810
811 SETPORT(DMACNTRL0, INTEN);
812
813 printk(KERN_ERR "aha152x%d: irq %d possibly wrong. "
814 "Please verify.\n", shpnt->host_no, shpnt->irq);
815 goto out_host_put;
816 }
817 printk("ok.\n");
818
819
820 /* clear interrupts */
821 SETPORT(SSTAT0, 0x7f);
822 SETPORT(SSTAT1, 0xef);
823
4909cc2b 824 if (request_irq(shpnt->irq, intr, IRQF_SHARED, "aha152x", shpnt)) {
1da177e4
LT
825 printk(KERN_ERR "aha152x%d: failed to reassign irq %d.\n", shpnt->host_no, shpnt->irq);
826 goto out_host_put;
827 }
828
829 if( scsi_add_host(shpnt, NULL) ) {
830 free_irq(shpnt->irq, shpnt);
831 printk(KERN_ERR "aha152x%d: failed to add host.\n", shpnt->host_no);
832 goto out_host_put;
833 }
834
835 scsi_scan_host(shpnt);
836
1da177e4
LT
837 return shpnt;
838
839out_host_put:
5fcda422 840 list_del(&HOSTDATA(shpnt)->host_list);
1da177e4
LT
841 scsi_host_put(shpnt);
842
843 return NULL;
844}
845
846void aha152x_release(struct Scsi_Host *shpnt)
847{
1bd40573 848 if (!shpnt)
1da177e4
LT
849 return;
850
1bd40573 851 scsi_remove_host(shpnt);
1da177e4
LT
852 if (shpnt->irq)
853 free_irq(shpnt->irq, shpnt);
854
3eb2ebcb 855#if !defined(AHA152X_PCMCIA)
1da177e4
LT
856 if (shpnt->io_port)
857 release_region(shpnt->io_port, IO_RANGE);
858#endif
859
860#ifdef __ISAPNP__
861 if (HOSTDATA(shpnt)->pnpdev)
862 pnp_device_detach(HOSTDATA(shpnt)->pnpdev);
863#endif
864
5fcda422 865 list_del(&HOSTDATA(shpnt)->host_list);
1da177e4
LT
866 scsi_host_put(shpnt);
867}
868
869
870/*
871 * setup controller to generate interrupts depending
872 * on current state (lock has to be acquired)
873 *
f75ae8ed 874 */
1da177e4
LT
875static int setup_expected_interrupts(struct Scsi_Host *shpnt)
876{
877 if(CURRENT_SC) {
878 CURRENT_SC->SCp.phase |= 1 << 16;
f75ae8ed 879
1da177e4 880 if(CURRENT_SC->SCp.phase & selecting) {
1da177e4
LT
881 SETPORT(SSTAT1, SELTO);
882 SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
883 SETPORT(SIMODE1, ENSELTIMO);
884 } else {
1da177e4 885 SETPORT(SIMODE0, (CURRENT_SC->SCp.phase & spiordy) ? ENSPIORDY : 0);
f75ae8ed 886 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1da177e4
LT
887 }
888 } else if(STATE==seldi) {
1da177e4 889 SETPORT(SIMODE0, 0);
f75ae8ed 890 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1da177e4 891 } else {
1da177e4
LT
892 SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
893 SETPORT(SIMODE1, ENSCSIRST | ( (ISSUE_SC||DONE_SC) ? ENBUSFREE : 0));
894 }
895
896 if(!HOSTDATA(shpnt)->in_intr)
897 SETBITS(DMACNTRL0, INTEN);
898
899 return TESTHI(DMASTAT, INTSTAT);
900}
901
902
f75ae8ed 903/*
1da177e4
LT
904 * Queue a command and setup interrupts for a free bus.
905 */
91ebc1fa
JT
906static int aha152x_internal_queue(struct scsi_cmnd *SCpnt,
907 struct completion *complete,
908 int phase, void (*done)(struct scsi_cmnd *))
1da177e4
LT
909{
910 struct Scsi_Host *shpnt = SCpnt->device->host;
911 unsigned long flags;
912
1da177e4 913 SCpnt->scsi_done = done;
1da177e4 914 SCpnt->SCp.phase = not_issued | phase;
0ceb4798 915 SCpnt->SCp.Status = 0x1; /* Ilegal status by SCSI standard */
1da177e4
LT
916 SCpnt->SCp.Message = 0;
917 SCpnt->SCp.have_data_in = 0;
918 SCpnt->SCp.sent_command = 0;
919
920 if(SCpnt->SCp.phase & (resetting|check_condition)) {
172c122d 921 if (!SCpnt->host_scribble || SCSEM(SCpnt) || SCNEXT(SCpnt)) {
f75ae8ed 922 scmd_printk(KERN_ERR, SCpnt, "cannot reuse command\n");
1da177e4
LT
923 return FAILED;
924 }
925 } else {
926 SCpnt->host_scribble = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
172c122d 927 if(!SCpnt->host_scribble) {
f75ae8ed 928 scmd_printk(KERN_ERR, SCpnt, "allocation failed\n");
1da177e4
LT
929 return FAILED;
930 }
931 }
932
933 SCNEXT(SCpnt) = NULL;
0f06bb34 934 SCSEM(SCpnt) = complete;
1da177e4
LT
935
936 /* setup scratch area
937 SCp.ptr : buffer pointer
938 SCp.this_residual : buffer length
939 SCp.buffer : next buffer
940 SCp.buffers_residual : left buffers in list
941 SCp.phase : current state of the command */
66acdb03 942
73d2cb16
BH
943 if ((phase & resetting) || !scsi_sglist(SCpnt)) {
944 SCpnt->SCp.ptr = NULL;
945 SCpnt->SCp.this_residual = 0;
946 scsi_set_resid(SCpnt, 0);
66acdb03
BH
947 SCpnt->SCp.buffer = NULL;
948 SCpnt->SCp.buffers_residual = 0;
949 } else {
2338545a
BH
950 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
951 SCpnt->SCp.buffer = scsi_sglist(SCpnt);
1da177e4
LT
952 SCpnt->SCp.ptr = SG_ADDRESS(SCpnt->SCp.buffer);
953 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
2338545a 954 SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1;
66acdb03 955 }
1da177e4
LT
956
957 DO_LOCK(flags);
958
959#if defined(AHA152X_STAT)
960 HOSTDATA(shpnt)->total_commands++;
961#endif
962
963 /* Turn led on, when this is the first command. */
964 HOSTDATA(shpnt)->commands++;
965 if (HOSTDATA(shpnt)->commands==1)
966 SETPORT(PORTA, 1);
967
968 append_SC(&ISSUE_SC, SCpnt);
969
970 if(!HOSTDATA(shpnt)->in_intr)
971 setup_expected_interrupts(shpnt);
972
973 DO_UNLOCK(flags);
974
975 return 0;
976}
977
978/*
979 * queue a command
980 *
981 */
91ebc1fa
JT
982static int aha152x_queue_lck(struct scsi_cmnd *SCpnt,
983 void (*done)(struct scsi_cmnd *))
1da177e4 984{
1da177e4
LT
985 return aha152x_internal_queue(SCpnt, NULL, 0, done);
986}
987
f281233d
JG
988static DEF_SCSI_QCMD(aha152x_queue)
989
1da177e4
LT
990
991/*
1da177e4
LT
992 *
993 */
91ebc1fa 994static void reset_done(struct scsi_cmnd *SCpnt)
1da177e4 995{
1da177e4 996 if(SCSEM(SCpnt)) {
0f06bb34 997 complete(SCSEM(SCpnt));
1da177e4 998 } else {
0f06bb34 999 printk(KERN_ERR "aha152x: reset_done w/o completion\n");
1da177e4
LT
1000 }
1001}
1002
1003/*
1004 * Abort a command
1005 *
1006 */
91ebc1fa 1007static int aha152x_abort(struct scsi_cmnd *SCpnt)
1da177e4
LT
1008{
1009 struct Scsi_Host *shpnt = SCpnt->device->host;
91ebc1fa 1010 struct scsi_cmnd *ptr;
1da177e4
LT
1011 unsigned long flags;
1012
1da177e4
LT
1013 DO_LOCK(flags);
1014
1015 ptr=remove_SC(&ISSUE_SC, SCpnt);
1016
1017 if(ptr) {
1da177e4
LT
1018 HOSTDATA(shpnt)->commands--;
1019 if (!HOSTDATA(shpnt)->commands)
1020 SETPORT(PORTA, 0);
1021 DO_UNLOCK(flags);
1022
1023 kfree(SCpnt->host_scribble);
1024 SCpnt->host_scribble=NULL;
1025
1026 return SUCCESS;
f75ae8ed 1027 }
1da177e4
LT
1028
1029 DO_UNLOCK(flags);
1030
1031 /*
1032 * FIXME:
1033 * for current command: queue ABORT for message out and raise ATN
1034 * for disconnected command: pseudo SC with ABORT message or ABORT on reselection?
1035 *
1036 */
1037
f75ae8ed
HR
1038 scmd_printk(KERN_ERR, SCpnt,
1039 "cannot abort running or disconnected command\n");
1da177e4
LT
1040
1041 return FAILED;
1042}
1043
1da177e4
LT
1044/*
1045 * Reset a device
1046 *
1047 */
91ebc1fa 1048static int aha152x_device_reset(struct scsi_cmnd * SCpnt)
1da177e4
LT
1049{
1050 struct Scsi_Host *shpnt = SCpnt->device->host;
0f06bb34 1051 DECLARE_COMPLETION(done);
1da177e4 1052 int ret, issued, disconnected;
631c228c 1053 unsigned char old_cmd_len = SCpnt->cmd_len;
1da177e4 1054 unsigned long flags;
0f06bb34 1055 unsigned long timeleft;
1da177e4 1056
1da177e4 1057 if(CURRENT_SC==SCpnt) {
f75ae8ed 1058 scmd_printk(KERN_ERR, SCpnt, "cannot reset current device\n");
1da177e4
LT
1059 return FAILED;
1060 }
1061
1062 DO_LOCK(flags);
172c122d 1063 issued = remove_SC(&ISSUE_SC, SCpnt) == NULL;
1da177e4
LT
1064 disconnected = issued && remove_SC(&DISCONNECTED_SC, SCpnt);
1065 DO_UNLOCK(flags);
1066
1067 SCpnt->cmd_len = 0;
1da177e4 1068
0f06bb34 1069 aha152x_internal_queue(SCpnt, &done, resetting, reset_done);
1da177e4 1070
0f06bb34
CH
1071 timeleft = wait_for_completion_timeout(&done, 100*HZ);
1072 if (!timeleft) {
1073 /* remove command from issue queue */
1074 DO_LOCK(flags);
1075 remove_SC(&ISSUE_SC, SCpnt);
1076 DO_UNLOCK(flags);
1077 }
631c228c
CH
1078
1079 SCpnt->cmd_len = old_cmd_len;
1da177e4
LT
1080
1081 DO_LOCK(flags);
1082
1083 if(SCpnt->SCp.phase & resetted) {
1084 HOSTDATA(shpnt)->commands--;
1085 if (!HOSTDATA(shpnt)->commands)
1086 SETPORT(PORTA, 0);
1087 kfree(SCpnt->host_scribble);
1088 SCpnt->host_scribble=NULL;
1089
1090 ret = SUCCESS;
1091 } else {
1092 /* requeue */
1093 if(!issued) {
1094 append_SC(&ISSUE_SC, SCpnt);
1095 } else if(disconnected) {
1096 append_SC(&DISCONNECTED_SC, SCpnt);
1097 }
f75ae8ed 1098
1da177e4
LT
1099 ret = FAILED;
1100 }
1101
1102 DO_UNLOCK(flags);
1da177e4
LT
1103 return ret;
1104}
1105
91ebc1fa
JT
1106static void free_hard_reset_SCs(struct Scsi_Host *shpnt,
1107 struct scsi_cmnd **SCs)
1da177e4 1108{
91ebc1fa 1109 struct scsi_cmnd *ptr;
1da177e4
LT
1110
1111 ptr=*SCs;
1112 while(ptr) {
91ebc1fa 1113 struct scsi_cmnd *next;
1da177e4
LT
1114
1115 if(SCDATA(ptr)) {
1116 next = SCNEXT(ptr);
1117 } else {
f75ae8ed
HR
1118 scmd_printk(KERN_DEBUG, ptr,
1119 "queue corrupted at %p\n", ptr);
1da177e4
LT
1120 next = NULL;
1121 }
1122
1123 if (!ptr->device->soft_reset) {
1da177e4
LT
1124 remove_SC(SCs, ptr);
1125 HOSTDATA(shpnt)->commands--;
1126 kfree(ptr->host_scribble);
1127 ptr->host_scribble=NULL;
1128 }
1129
1130 ptr = next;
1131 }
1132}
1133
1134/*
1135 * Reset the bus
1136 *
819f80c9
HR
1137 * AIC-6260 has a hard reset (MRST signal), but apparently
1138 * one cannot trigger it via software. So live with
1139 * a soft reset; no-one seemed to have cared.
1da177e4 1140 */