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