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