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