drm/ast: Rename AST_IO_CRTC_PORT to AST_IO_VGACRI
[linux-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
94b5530f 223 see Documentation/scsi/aha152x.rst for configuration details
1da177e4
LT
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 246
53555fb7
BVA
247#include <scsi/scsi.h>
248#include <scsi/scsi_cmnd.h>
db9dff36 249#include <scsi/scsi_dbg.h>
53555fb7
BVA
250#include <scsi/scsi_device.h>
251#include <scsi/scsi_eh.h>
1da177e4 252#include <scsi/scsi_host.h>
53555fb7 253#include <scsi/scsi_tcq.h>
1abfd370 254#include <scsi/scsi_transport_spi.h>
53555fb7 255#include <scsi/scsicam.h>
1da177e4
LT
256#include "aha152x.h"
257
5fcda422
JB
258static LIST_HEAD(aha152x_host_list);
259
1da177e4
LT
260
261/* DEFINES */
262
263/* For PCMCIA cards, always use AUTOCONF */
3eb2ebcb 264#if defined(AHA152X_PCMCIA) || defined(MODULE)
1da177e4
LT
265#if !defined(AUTOCONF)
266#define AUTOCONF
267#endif
268#endif
269
270#if !defined(AUTOCONF) && !defined(SETUP0)
271#error define AUTOCONF or SETUP0
272#endif
273
1da177e4
LT
274#define DO_LOCK(flags) spin_lock_irqsave(&QLOCK,flags)
275#define DO_UNLOCK(flags) spin_unlock_irqrestore(&QLOCK,flags)
1da177e4
LT
276
277#define LEAD "(scsi%d:%d:%d) "
1da177e4 278#define INFO_LEAD KERN_INFO LEAD
1da177e4
LT
279#define CMDINFO(cmd) \
280 (cmd) ? ((cmd)->device->host->host_no) : -1, \
281 (cmd) ? ((cmd)->device->id & 0x0f) : -1, \
9cb78c16 282 (cmd) ? ((u8)(cmd)->device->lun & 0x07) : -1
1da177e4 283
2338545a
BH
284static inline void
285CMD_INC_RESID(struct scsi_cmnd *cmd, int inc)
286{
287 scsi_set_resid(cmd, scsi_get_resid(cmd) + inc);
288}
289
1da177e4
LT
290#define DELAY_DEFAULT 1000
291
3eb2ebcb 292#if defined(AHA152X_PCMCIA)
1da177e4
LT
293#define IRQ_MIN 0
294#define IRQ_MAX 16
295#else
296#define IRQ_MIN 9
297#if defined(__PPC)
171ac6ae 298#define IRQ_MAX (nr_irqs-1)
1da177e4
LT
299#else
300#define IRQ_MAX 12
301#endif
302#endif
303
304enum {
305 not_issued = 0x0001, /* command not yet issued */
f75ae8ed 306 selecting = 0x0002, /* target is being selected */
1da177e4
LT
307 identified = 0x0004, /* IDENTIFY was sent */
308 disconnected = 0x0008, /* target disconnected */
f75ae8ed 309 completed = 0x0010, /* target sent COMMAND COMPLETE */
1da177e4
LT
310 aborted = 0x0020, /* ABORT was sent */
311 resetted = 0x0040, /* BUS DEVICE RESET was sent */
312 spiordy = 0x0080, /* waiting for SPIORDY to raise */
313 syncneg = 0x0100, /* synchronous negotiation in progress */
314 aborting = 0x0200, /* ABORT is pending */
315 resetting = 0x0400, /* BUS DEVICE RESET is pending */
316 check_condition = 0x0800, /* requesting sense after CHECK CONDITION */
317};
318
3ac6aba3 319struct aha152x_cmd_priv {
63221571
FT
320 char *ptr;
321 int this_residual;
322 struct scatterlist *buffer;
323 int status;
324 int message;
325 int sent_command;
326 int phase;
3ac6aba3
BVA
327};
328
63221571 329static struct aha152x_cmd_priv *aha152x_priv(struct scsi_cmnd *cmd)
3ac6aba3 330{
63221571 331 return scsi_cmd_priv(cmd);
3ac6aba3
BVA
332}
333
96de0e25 334MODULE_AUTHOR("Jürgen Fischer");
1da177e4
LT
335MODULE_DESCRIPTION(AHA152X_REVID);
336MODULE_LICENSE("GPL");
337
3eb2ebcb 338#if !defined(AHA152X_PCMCIA)
1da177e4
LT
339#if defined(MODULE)
340static int io[] = {0, 0};
88f06b76 341module_param_hw_array(io, int, ioport, NULL, 0);
1da177e4
LT
342MODULE_PARM_DESC(io,"base io address of controller");
343
344static int irq[] = {0, 0};
88f06b76 345module_param_hw_array(irq, int, irq, NULL, 0);
1da177e4
LT
346MODULE_PARM_DESC(irq,"interrupt for controller");
347
348static int scsiid[] = {7, 7};
349module_param_array(scsiid, int, NULL, 0);
350MODULE_PARM_DESC(scsiid,"scsi id of controller");
351
352static int reconnect[] = {1, 1};
353module_param_array(reconnect, int, NULL, 0);
354MODULE_PARM_DESC(reconnect,"allow targets to disconnect");
355
356static int parity[] = {1, 1};
357module_param_array(parity, int, NULL, 0);
358MODULE_PARM_DESC(parity,"use scsi parity");
359
360static int sync[] = {1, 1};
361module_param_array(sync, int, NULL, 0);
362MODULE_PARM_DESC(sync,"use synchronous transfers");
363
364static int delay[] = {DELAY_DEFAULT, DELAY_DEFAULT};
365module_param_array(delay, int, NULL, 0);
366MODULE_PARM_DESC(delay,"scsi reset delay");
367
368static int exttrans[] = {0, 0};
369module_param_array(exttrans, int, NULL, 0);
370MODULE_PARM_DESC(exttrans,"use extended translation");
371
1da177e4
LT
372static int aha152x[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
373module_param_array(aha152x, int, NULL, 0);
374MODULE_PARM_DESC(aha152x, "parameters for first controller");
375
376static int aha152x1[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
377module_param_array(aha152x1, int, NULL, 0);
378MODULE_PARM_DESC(aha152x1, "parameters for second controller");
1da177e4
LT
379#endif /* MODULE */
380
381#ifdef __ISAPNP__
6f039790 382static struct isapnp_device_id id_table[] = {
50f87f91
OZ
383 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1502), 0 },
384 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1505), 0 },
385 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1510), 0 },
386 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1515), 0 },
387 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1520), 0 },
388 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x2015), 0 },
389 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1522), 0 },
390 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x2215), 0 },
391 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1530), 0 },
392 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x3015), 0 },
393 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1532), 0 },
394 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x3215), 0 },
395 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x6360), 0 },
1da177e4
LT
396 { ISAPNP_DEVICE_SINGLE_END, }
397};
398MODULE_DEVICE_TABLE(isapnp, id_table);
399#endif /* ISAPNP */
400
3eb2ebcb 401#endif /* !AHA152X_PCMCIA */
1da177e4 402
11e58cea 403static const struct scsi_host_template aha152x_driver_template;
1da177e4
LT
404
405/*
406 * internal states of the host
407 *
f75ae8ed 408 */
1da177e4
LT
409enum aha152x_state {
410 idle=0,
411 unknown,
412 seldo,
413 seldi,
414 selto,
415 busfree,
416 msgo,
417 cmd,
418 msgi,
419 status,
420 datai,
421 datao,
422 parerr,
423 rsti,
424 maxstate
425};
426
427/*
428 * current state information of the host
429 *
430 */
431struct aha152x_hostdata {
91ebc1fa 432 struct scsi_cmnd *issue_SC;
1da177e4
LT
433 /* pending commands to issue */
434
91ebc1fa 435 struct scsi_cmnd *current_SC;
1da177e4
LT
436 /* current command on the bus */
437
91ebc1fa 438 struct scsi_cmnd *disconnected_SC;
1da177e4
LT
439 /* commands that disconnected */
440
91ebc1fa 441 struct scsi_cmnd *done_SC;
1da177e4
LT
442 /* command that was completed */
443
444 spinlock_t lock;
445 /* host lock */
446
1da177e4 447#if defined(AHA152X_STAT)
f75ae8ed 448 int total_commands;
1da177e4
LT
449 int disconnections;
450 int busfree_without_any_action;
451 int busfree_without_old_command;
452 int busfree_without_new_command;
453 int busfree_without_done_command;
454 int busfree_with_check_condition;
f75ae8ed
HR
455 int count[maxstate];
456 int count_trans[maxstate];
1da177e4
LT
457 unsigned long time[maxstate];
458#endif
459
460 int commands; /* current number of commands */
461
462 int reconnect; /* disconnection allowed */
463 int parity; /* parity checking enabled */
464 int synchronous; /* synchronous transferes enabled */
465 int delay; /* reset out delay */
466 int ext_trans; /* extended translation enabled */
467
f75ae8ed 468 int swint; /* software-interrupt was fired during detect() */
1da177e4
LT
469 int service; /* bh needs to be run */
470 int in_intr; /* bh is running */
471
472 /* current state,
473 previous state,
474 last state different from current state */
475 enum aha152x_state state, prevstate, laststate;
476
477 int target;
478 /* reconnecting target */
479
480 unsigned char syncrate[8];
481 /* current synchronous transfer agreements */
482
483 unsigned char syncneg[8];
484 /* 0: no negotiation;
485 * 1: negotiation in progress;
486 * 2: negotiation completed
487 */
488
489 int cmd_i;
490 /* number of sent bytes of current command */
491
492 int msgi_len;
493 /* number of received message bytes */
494 unsigned char msgi[256];
495 /* received message bytes */
496
f75ae8ed 497 int msgo_i, msgo_len;
1da177e4
LT
498 /* number of sent bytes and length of current messages */
499 unsigned char msgo[256];
500 /* pending messages */
501
502 int data_len;
503 /* number of sent/received bytes in dataphase */
504
505 unsigned long io_port0;
506 unsigned long io_port1;
507
508#ifdef __ISAPNP__
509 struct pnp_dev *pnpdev;
510#endif
5fcda422 511 struct list_head host_list;
1da177e4
LT
512};
513
514
515/*
516 * host specific command extension
517 *
518 */
519struct aha152x_scdata {
91ebc1fa 520 struct scsi_cmnd *next; /* next sc in queue */
0f06bb34 521 struct completion *done;/* semaphore to block on */
73d2cb16 522 struct scsi_eh_save ses;
1da177e4
LT
523};
524
1da177e4
LT
525/* access macros for hostdata */
526
527#define HOSTDATA(shpnt) ((struct aha152x_hostdata *) &shpnt->hostdata)
528
529#define HOSTNO ((shpnt)->host_no)
530
531#define CURRENT_SC (HOSTDATA(shpnt)->current_SC)
532#define DONE_SC (HOSTDATA(shpnt)->done_SC)
533#define ISSUE_SC (HOSTDATA(shpnt)->issue_SC)
534#define DISCONNECTED_SC (HOSTDATA(shpnt)->disconnected_SC)
535#define QLOCK (HOSTDATA(shpnt)->lock)
536#define QLOCKER (HOSTDATA(shpnt)->locker)
537#define QLOCKERL (HOSTDATA(shpnt)->lockerl)
538
539#define STATE (HOSTDATA(shpnt)->state)
540#define PREVSTATE (HOSTDATA(shpnt)->prevstate)
541#define LASTSTATE (HOSTDATA(shpnt)->laststate)
542
543#define RECONN_TARGET (HOSTDATA(shpnt)->target)
544
545#define CMD_I (HOSTDATA(shpnt)->cmd_i)
546
547#define MSGO(i) (HOSTDATA(shpnt)->msgo[i])
548#define MSGO_I (HOSTDATA(shpnt)->msgo_i)
549#define MSGOLEN (HOSTDATA(shpnt)->msgo_len)
550#define ADDMSGO(x) (MSGOLEN<256 ? (void)(MSGO(MSGOLEN++)=x) : aha152x_error(shpnt,"MSGO overflow"))
551
552#define MSGI(i) (HOSTDATA(shpnt)->msgi[i])
553#define MSGILEN (HOSTDATA(shpnt)->msgi_len)
554#define ADDMSGI(x) (MSGILEN<256 ? (void)(MSGI(MSGILEN++)=x) : aha152x_error(shpnt,"MSGI overflow"))
555
556#define DATA_LEN (HOSTDATA(shpnt)->data_len)
557
558#define SYNCRATE (HOSTDATA(shpnt)->syncrate[CURRENT_SC->device->id])
559#define SYNCNEG (HOSTDATA(shpnt)->syncneg[CURRENT_SC->device->id])
560
561#define DELAY (HOSTDATA(shpnt)->delay)
562#define EXT_TRANS (HOSTDATA(shpnt)->ext_trans)
563#define TC1550 (HOSTDATA(shpnt)->tc1550)
564#define RECONNECT (HOSTDATA(shpnt)->reconnect)
565#define PARITY (HOSTDATA(shpnt)->parity)
566#define SYNCHRONOUS (HOSTDATA(shpnt)->synchronous)
567
568#define HOSTIOPORT0 (HOSTDATA(shpnt)->io_port0)
569#define HOSTIOPORT1 (HOSTDATA(shpnt)->io_port1)
570
571#define SCDATA(SCpnt) ((struct aha152x_scdata *) (SCpnt)->host_scribble)
572#define SCNEXT(SCpnt) SCDATA(SCpnt)->next
0f06bb34 573#define SCSEM(SCpnt) SCDATA(SCpnt)->done
1da177e4 574
45711f1a 575#define SG_ADDRESS(buffer) ((char *) sg_virt((buffer)))
1da177e4
LT
576
577/* state handling */
578static void seldi_run(struct Scsi_Host *shpnt);
579static void seldo_run(struct Scsi_Host *shpnt);
580static void selto_run(struct Scsi_Host *shpnt);
581static void busfree_run(struct Scsi_Host *shpnt);
582
583static void msgo_init(struct Scsi_Host *shpnt);
584static void msgo_run(struct Scsi_Host *shpnt);
585static void msgo_end(struct Scsi_Host *shpnt);
586
587static void cmd_init(struct Scsi_Host *shpnt);
588static void cmd_run(struct Scsi_Host *shpnt);
589static void cmd_end(struct Scsi_Host *shpnt);
590
591static void datai_init(struct Scsi_Host *shpnt);
592static void datai_run(struct Scsi_Host *shpnt);
593static void datai_end(struct Scsi_Host *shpnt);
594
595static void datao_init(struct Scsi_Host *shpnt);
596static void datao_run(struct Scsi_Host *shpnt);
597static void datao_end(struct Scsi_Host *shpnt);
598
599static void status_run(struct Scsi_Host *shpnt);
600
601static void msgi_run(struct Scsi_Host *shpnt);
602static void msgi_end(struct Scsi_Host *shpnt);
603
604static void parerr_run(struct Scsi_Host *shpnt);
605static void rsti_run(struct Scsi_Host *shpnt);
606
607static void is_complete(struct Scsi_Host *shpnt);
608
609/*
610 * driver states
611 *
612 */
613static struct {
614 char *name;
615 void (*init)(struct Scsi_Host *);
616 void (*run)(struct Scsi_Host *);
617 void (*end)(struct Scsi_Host *);
618 int spio;
619} states[] = {
620 { "idle", NULL, NULL, NULL, 0},
621 { "unknown", NULL, NULL, NULL, 0},
622 { "seldo", NULL, seldo_run, NULL, 0},
623 { "seldi", NULL, seldi_run, NULL, 0},
624 { "selto", NULL, selto_run, NULL, 0},
625 { "busfree", NULL, busfree_run, NULL, 0},
626 { "msgo", msgo_init, msgo_run, msgo_end, 1},
627 { "cmd", cmd_init, cmd_run, cmd_end, 1},
628 { "msgi", NULL, msgi_run, msgi_end, 1},
629 { "status", NULL, status_run, NULL, 1},
630 { "datai", datai_init, datai_run, datai_end, 0},
631 { "datao", datao_init, datao_run, datao_end, 0},
632 { "parerr", NULL, parerr_run, NULL, 0},
633 { "rsti", NULL, rsti_run, NULL, 0},
634};
635
636/* setup & interrupt */
7d12e780 637static irqreturn_t intr(int irq, void *dev_id);
1da177e4
LT
638static void reset_ports(struct Scsi_Host *shpnt);
639static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
aec166fd 640static void done(struct Scsi_Host *shpnt, unsigned char status_byte,
fdabe57d 641 unsigned char host_byte);
1da177e4
LT
642
643/* diagnostics */
91ebc1fa 644static void show_command(struct scsi_cmnd * ptr);
1da177e4
LT
645static void show_queues(struct Scsi_Host *shpnt);
646static void disp_enintr(struct Scsi_Host *shpnt);
647
648
649/*
650 * queue services:
651 *
652 */
91ebc1fa 653static inline void append_SC(struct scsi_cmnd **SC, struct scsi_cmnd *new_SC)
1da177e4 654{
91ebc1fa 655 struct scsi_cmnd *end;
1da177e4
LT
656
657 SCNEXT(new_SC) = NULL;
658 if (!*SC)
659 *SC = new_SC;
660 else {
661 for (end = *SC; SCNEXT(end); end = SCNEXT(end))
662 ;
663 SCNEXT(end) = new_SC;
664 }
665}
666
91ebc1fa 667static inline struct scsi_cmnd *remove_first_SC(struct scsi_cmnd ** SC)
1da177e4 668{
91ebc1fa 669 struct scsi_cmnd *ptr;
1da177e4
LT
670
671 ptr = *SC;
672 if (ptr) {
673 *SC = SCNEXT(*SC);
674 SCNEXT(ptr)=NULL;
675 }
676 return ptr;
677}
678
91ebc1fa
JT
679static inline struct scsi_cmnd *remove_lun_SC(struct scsi_cmnd ** SC,
680 int target, int lun)
1da177e4 681{
91ebc1fa 682 struct scsi_cmnd *ptr, *prev;
1da177e4
LT
683
684 for (ptr = *SC, prev = NULL;
685 ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
686 prev = ptr, ptr = SCNEXT(ptr))
687 ;
688
689 if (ptr) {
690 if (prev)
691 SCNEXT(prev) = SCNEXT(ptr);
692 else
693 *SC = SCNEXT(ptr);
694
695 SCNEXT(ptr)=NULL;
696 }
697
698 return ptr;
699}
700
91ebc1fa
JT
701static inline struct scsi_cmnd *remove_SC(struct scsi_cmnd **SC,
702 struct scsi_cmnd *SCp)
1da177e4 703{
91ebc1fa 704 struct scsi_cmnd *ptr, *prev;
1da177e4
LT
705
706 for (ptr = *SC, prev = NULL;
707 ptr && SCp!=ptr;
708 prev = ptr, ptr = SCNEXT(ptr))
709 ;
710
711 if (ptr) {
712 if (prev)
713 SCNEXT(prev) = SCNEXT(ptr);
714 else
715 *SC = SCNEXT(ptr);
716
717 SCNEXT(ptr)=NULL;
718 }
719
720 return ptr;
721}
722
7d12e780 723static irqreturn_t swintr(int irqno, void *dev_id)
1da177e4 724{
c7bec5ab 725 struct Scsi_Host *shpnt = dev_id;
1da177e4
LT
726
727 HOSTDATA(shpnt)->swint++;
728
729 SETPORT(DMACNTRL0, INTEN);
730 return IRQ_HANDLED;
731}
732
733struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
734{
735 struct Scsi_Host *shpnt;
736
737 shpnt = scsi_host_alloc(&aha152x_driver_template, sizeof(struct aha152x_hostdata));
738 if (!shpnt) {
739 printk(KERN_ERR "aha152x: scsi_host_alloc failed\n");
740 return NULL;
741 }
742
1da177e4 743 memset(HOSTDATA(shpnt), 0, sizeof *HOSTDATA(shpnt));
5fcda422
JB
744 INIT_LIST_HEAD(&HOSTDATA(shpnt)->host_list);
745
746 /* need to have host registered before triggering any interrupt */
747 list_add_tail(&HOSTDATA(shpnt)->host_list, &aha152x_host_list);
1da177e4
LT
748
749 shpnt->io_port = setup->io_port;
750 shpnt->n_io_port = IO_RANGE;
751 shpnt->irq = setup->irq;
752
753 if (!setup->tc1550) {
754 HOSTIOPORT0 = setup->io_port;
755 HOSTIOPORT1 = setup->io_port;
756 } else {
757 HOSTIOPORT0 = setup->io_port+0x10;
758 HOSTIOPORT1 = setup->io_port-0x10;
759 }
760
761 spin_lock_init(&QLOCK);
762 RECONNECT = setup->reconnect;
763 SYNCHRONOUS = setup->synchronous;
764 PARITY = setup->parity;
765 DELAY = setup->delay;
766 EXT_TRANS = setup->ext_trans;
767
1da177e4
LT
768 SETPORT(SCSIID, setup->scsiid << 4);
769 shpnt->this_id = setup->scsiid;
770
771 if (setup->reconnect)
772 shpnt->can_queue = AHA152X_MAXQUEUE;
773
774 /* RESET OUT */
775 printk("aha152x: resetting bus...\n");
776 SETPORT(SCSISEQ, SCSIRSTO);
777 mdelay(256);
778 SETPORT(SCSISEQ, 0);
779 mdelay(DELAY);
780
781 reset_ports(shpnt);
782
783 printk(KERN_INFO
784 "aha152x%d%s: "
785 "vital data: rev=%x, "
786 "io=0x%03lx (0x%03lx/0x%03lx), "
787 "irq=%d, "
788 "scsiid=%d, "
789 "reconnect=%s, "
790 "parity=%s, "
791 "synchronous=%s, "
792 "delay=%d, "
793 "extended translation=%s\n",
794 shpnt->host_no, setup->tc1550 ? " (tc1550 mode)" : "",
795 GETPORT(REV) & 0x7,
796 shpnt->io_port, HOSTIOPORT0, HOSTIOPORT1,
797 shpnt->irq,
798 shpnt->this_id,
799 RECONNECT ? "enabled" : "disabled",
800 PARITY ? "enabled" : "disabled",
801 SYNCHRONOUS ? "enabled" : "disabled",
802 DELAY,
803 EXT_TRANS ? "enabled" : "disabled");
804
805 /* not expecting any interrupts */
806 SETPORT(SIMODE0, 0);
807 SETPORT(SIMODE1, 0);
808
4909cc2b 809 if (request_irq(shpnt->irq, swintr, IRQF_SHARED, "aha152x", shpnt)) {
1da177e4
LT
810 printk(KERN_ERR "aha152x%d: irq %d busy.\n", shpnt->host_no, shpnt->irq);
811 goto out_host_put;
812 }
813
814 HOSTDATA(shpnt)->swint = 0;
815
816 printk(KERN_INFO "aha152x%d: trying software interrupt, ", shpnt->host_no);
817
818 mb();
819 SETPORT(DMACNTRL0, SWINT|INTEN);
820 mdelay(1000);
821 free_irq(shpnt->irq, shpnt);
822
823 if (!HOSTDATA(shpnt)->swint) {
824 if (TESTHI(DMASTAT, INTSTAT)) {
825 printk("lost.\n");
826 } else {
827 printk("failed.\n");
828 }
829
830 SETPORT(DMACNTRL0, INTEN);
831
832 printk(KERN_ERR "aha152x%d: irq %d possibly wrong. "
833 "Please verify.\n", shpnt->host_no, shpnt->irq);
834 goto out_host_put;
835 }
836 printk("ok.\n");
837
838
839 /* clear interrupts */
840 SETPORT(SSTAT0, 0x7f);
841 SETPORT(SSTAT1, 0xef);
842
4909cc2b 843 if (request_irq(shpnt->irq, intr, IRQF_SHARED, "aha152x", shpnt)) {
1da177e4
LT
844 printk(KERN_ERR "aha152x%d: failed to reassign irq %d.\n", shpnt->host_no, shpnt->irq);
845 goto out_host_put;
846 }
847
848 if( scsi_add_host(shpnt, NULL) ) {
849 free_irq(shpnt->irq, shpnt);
850 printk(KERN_ERR "aha152x%d: failed to add host.\n", shpnt->host_no);
851 goto out_host_put;
852 }
853
854 scsi_scan_host(shpnt);
855
1da177e4
LT
856 return shpnt;
857
858out_host_put:
5fcda422 859 list_del(&HOSTDATA(shpnt)->host_list);
1da177e4
LT
860 scsi_host_put(shpnt);
861
862 return NULL;
863}
864
865void aha152x_release(struct Scsi_Host *shpnt)
866{
1bd40573 867 if (!shpnt)
1da177e4
LT
868 return;
869
1bd40573 870 scsi_remove_host(shpnt);
1da177e4
LT
871 if (shpnt->irq)
872 free_irq(shpnt->irq, shpnt);
873
3eb2ebcb 874#if !defined(AHA152X_PCMCIA)
1da177e4
LT
875 if (shpnt->io_port)
876 release_region(shpnt->io_port, IO_RANGE);
877#endif
878
879#ifdef __ISAPNP__
880 if (HOSTDATA(shpnt)->pnpdev)
881 pnp_device_detach(HOSTDATA(shpnt)->pnpdev);
882#endif
883
5fcda422 884 list_del(&HOSTDATA(shpnt)->host_list);
1da177e4
LT
885 scsi_host_put(shpnt);
886}
887
888
889/*
890 * setup controller to generate interrupts depending
891 * on current state (lock has to be acquired)
892 *
f75ae8ed 893 */
1da177e4
LT
894static int setup_expected_interrupts(struct Scsi_Host *shpnt)
895{
896 if(CURRENT_SC) {
63221571 897 struct aha152x_cmd_priv *acp = aha152x_priv(CURRENT_SC);
f75ae8ed 898
63221571 899 acp->phase |= 1 << 16;
3ac6aba3 900
63221571 901 if (acp->phase & selecting) {
1da177e4
LT
902 SETPORT(SSTAT1, SELTO);
903 SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
904 SETPORT(SIMODE1, ENSELTIMO);
905 } else {
63221571 906 SETPORT(SIMODE0, (acp->phase & spiordy) ? ENSPIORDY : 0);
f75ae8ed 907 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1da177e4
LT
908 }
909 } else if(STATE==seldi) {
1da177e4 910 SETPORT(SIMODE0, 0);
f75ae8ed 911 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1da177e4 912 } else {
1da177e4
LT
913 SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
914 SETPORT(SIMODE1, ENSCSIRST | ( (ISSUE_SC||DONE_SC) ? ENBUSFREE : 0));
915 }
916
917 if(!HOSTDATA(shpnt)->in_intr)
918 SETBITS(DMACNTRL0, INTEN);
919
920 return TESTHI(DMASTAT, INTSTAT);
921}
922
923
f75ae8ed 924/*
1da177e4
LT
925 * Queue a command and setup interrupts for a free bus.
926 */
91ebc1fa 927static int aha152x_internal_queue(struct scsi_cmnd *SCpnt,
3ab3b151 928 struct completion *complete, int phase)
1da177e4 929{
63221571 930 struct aha152x_cmd_priv *acp = aha152x_priv(SCpnt);
1da177e4
LT
931 struct Scsi_Host *shpnt = SCpnt->device->host;
932 unsigned long flags;
933
63221571
FT
934 acp->phase = not_issued | phase;
935 acp->status = 0x1; /* Illegal status by SCSI standard */
936 acp->message = 0;
937 acp->sent_command = 0;
1da177e4 938
63221571 939 if (acp->phase & (resetting | check_condition)) {
172c122d 940 if (!SCpnt->host_scribble || SCSEM(SCpnt) || SCNEXT(SCpnt)) {
f75ae8ed 941 scmd_printk(KERN_ERR, SCpnt, "cannot reuse command\n");
1da177e4
LT
942 return FAILED;
943 }
944 } else {
945 SCpnt->host_scribble = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
172c122d 946 if(!SCpnt->host_scribble) {
f75ae8ed 947 scmd_printk(KERN_ERR, SCpnt, "allocation failed\n");
1da177e4
LT
948 return FAILED;
949 }
950 }
951
952 SCNEXT(SCpnt) = NULL;
0f06bb34 953 SCSEM(SCpnt) = complete;
1da177e4
LT
954
955 /* setup scratch area
956 SCp.ptr : buffer pointer
957 SCp.this_residual : buffer length
958 SCp.buffer : next buffer
1da177e4 959 SCp.phase : current state of the command */
66acdb03 960
73d2cb16 961 if ((phase & resetting) || !scsi_sglist(SCpnt)) {
63221571
FT
962 acp->ptr = NULL;
963 acp->this_residual = 0;
73d2cb16 964 scsi_set_resid(SCpnt, 0);
63221571 965 acp->buffer = NULL;
66acdb03 966 } else {
2338545a 967 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
63221571
FT
968 acp->buffer = scsi_sglist(SCpnt);
969 acp->ptr = SG_ADDRESS(acp->buffer);
970 acp->this_residual = acp->buffer->length;
66acdb03 971 }
1da177e4
LT
972
973 DO_LOCK(flags);
974
975#if defined(AHA152X_STAT)
976 HOSTDATA(shpnt)->total_commands++;
977#endif
978
979 /* Turn led on, when this is the first command. */
980 HOSTDATA(shpnt)->commands++;
981 if (HOSTDATA(shpnt)->commands==1)
982 SETPORT(PORTA, 1);
983
984 append_SC(&ISSUE_SC, SCpnt);
985
986 if(!HOSTDATA(shpnt)->in_intr)
987 setup_expected_interrupts(shpnt);
988
989 DO_UNLOCK(flags);
990
991 return 0;
992}
993
994/*
995 * queue a command
996 *
997 */
af049dfd 998static int aha152x_queue_lck(struct scsi_cmnd *SCpnt)
1da177e4 999{
3ab3b151 1000 return aha152x_internal_queue(SCpnt, NULL, 0);
1da177e4
LT
1001}
1002
f281233d
JG
1003static DEF_SCSI_QCMD(aha152x_queue)
1004
1da177e4
LT
1005
1006/*
1da177e4
LT
1007 *
1008 */
91ebc1fa 1009static void reset_done(struct scsi_cmnd *SCpnt)
1da177e4 1010{
1da177e4 1011 if(SCSEM(SCpnt)) {
0f06bb34 1012 complete(SCSEM(SCpnt));
1da177e4 1013 } else {
0f06bb34 1014 printk(KERN_ERR "aha152x: reset_done w/o completion\n");
1da177e4
LT
1015 }
1016}
1017
3ab3b151
BVA
1018static void aha152x_scsi_done(struct scsi_cmnd *SCpnt)
1019{
63221571 1020 if (aha152x_priv(SCpnt)->phase & resetting)
3ab3b151
BVA
1021 reset_done(SCpnt);
1022 else
1023 scsi_done(SCpnt);
1024}
1025
1da177e4
LT
1026/*
1027 * Abort a command
1028 *
1029 */
91ebc1fa 1030static int aha152x_abort(struct scsi_cmnd *SCpnt)
1da177e4
LT
1031{
1032 struct Scsi_Host *shpnt = SCpnt->device->host;
91ebc1fa 1033 struct scsi_cmnd *ptr;
1da177e4
LT
1034 unsigned long flags;
1035
1da177e4
LT
1036 DO_LOCK(flags);
1037
1038 ptr=remove_SC(&ISSUE_SC, SCpnt);
1039
1040 if(ptr) {
1da177e4
LT
1041 HOSTDATA(shpnt)->commands--;
1042 if (!HOSTDATA(shpnt)->commands)
1043 SETPORT(PORTA, 0);
1044 DO_UNLOCK(flags);
1045
1046 kfree(SCpnt->host_scribble);
1047 SCpnt->host_scribble=NULL;
1048
1049 return SUCCESS;
f75ae8ed 1050 }
1da177e4
LT
1051
1052 DO_UNLOCK(flags);
1053
1054 /*
1055 * FIXME:
1056 * for current command: queue ABORT for message out and raise ATN
1057 * for disconnected command: pseudo SC with ABORT message or ABORT on reselection?
1058 *
1059 */
1060
f75ae8ed
HR
1061 scmd_printk(KERN_ERR, SCpnt,
1062 "cannot abort running or disconnected command\n");
1da177e4
LT
1063
1064 return FAILED;
1065}
1066
1da177e4
LT
1067/*
1068 * Reset a device
1069 *
1070 */
91ebc1fa 1071static int aha152x_device_reset(struct scsi_cmnd * SCpnt)
1da177e4
LT
1072{
1073 struct Scsi_Host *shpnt = SCpnt->device->host;
0f06bb34 1074 DECLARE_COMPLETION(done);
1da177e4 1075 int ret, issued, disconnected;
631c228c 1076 unsigned char old_cmd_len = SCpnt->cmd_len;
1da177e4 1077 unsigned long flags;
0f06bb34 1078 unsigned long timeleft;
1da177e4 1079
1da177e4 1080 if(CURRENT_SC==SCpnt) {
f75ae8ed 1081 scmd_printk(KERN_ERR, SCpnt, "cannot reset current device\n");
1da177e4
LT
1082 return FAILED;
1083 }
1084
1085 DO_LOCK(flags);
172c122d 1086 issued = remove_SC(&ISSUE_SC, SCpnt) == NULL;
1da177e4
LT
1087 disconnected = issued && remove_SC(&DISCONNECTED_SC, SCpnt);
1088 DO_UNLOCK(flags);
1089
1090 SCpnt->cmd_len = 0;
1da177e4 1091
3ab3b151 1092 aha152x_internal_queue(SCpnt, &done, resetting);
1da177e4 1093
0f06bb34
CH
1094 timeleft = wait_for_completion_timeout(&done, 100*HZ);
1095 if (!timeleft) {
1096 /* remove command from issue queue */
1097 DO_LOCK(flags);
1098 remove_SC(&ISSUE_SC, SCpnt);
1099 DO_UNLOCK(flags);
1100 }
631c228c
CH
1101
1102 SCpnt->cmd_len = old_cmd_len;
1da177e4
LT
1103
1104 DO_LOCK(flags);
1105
63221571 1106 if (aha152x_priv(SCpnt)->phase & resetted) {
1da177e4
LT
1107 HOSTDATA(shpnt)->commands--;
1108 if (!HOSTDATA(shpnt)->commands)
1109 SETPORT(PORTA, 0);
1110 kfree(SCpnt->host_scribble);
1111 SCpnt->host_scribble=NULL;
1112
1113 ret = SUCCESS;
1114 } else {
1115 /* requeue */
1116 if(!issued) {
1117 append_SC(&ISSUE_SC, SCpnt);
1118 } else if(disconnected) {
1119 append_SC(&DISCONNECTED_SC, SCpnt);
1120 }
f75ae8ed 1121
1da177e4
LT
1122 ret = FAILED;
1123 }
1124
1125 DO_UNLOCK(flags);
1da177e4
LT
1126 return ret;
1127}
1128
91ebc1fa
JT
1129static void free_hard_reset_SCs(struct Scsi_Host *shpnt,
1130 struct scsi_cmnd **SCs)
1da177e4 1131{
91ebc1fa 1132 struct scsi_cmnd *ptr;
1da177e4
LT
1133
1134 ptr=*SCs;
1135 while(ptr) {
91ebc1fa 1136 struct scsi_cmnd *next;
1da177e4
LT
1137
1138 if(SCDATA(ptr)) {
1139 next = SCNEXT(ptr);
1140 } else {
f75ae8ed
HR
1141 scmd_printk(KERN_DEBUG, ptr,
1142 "queue corrupted at %p\n", ptr);
1da177e4
LT
1143 next = NULL;
1144 }
1145
1146 if (!ptr->device->soft_reset) {
1da177e4
LT
1147 remove_SC(SCs, ptr);
1148 HOSTDATA(shpnt)->commands--;
1149 kfree(ptr->host_scribble);
1150 ptr->host_scribble=NULL;
1151 }
1152
1153 ptr = next;
1154 }
1155}
1156
1157/*
1158 * Reset the bus
1159 *
819f80c9
HR
1160 * AIC-6260 has a hard reset (MRST signal), but apparently
1161 * one cannot trigger it via software. So live with
1162 * a soft reset; no-one seemed to have cared.
1da177e4 1163 */