vserver 1.9.3
[linux-2.6.git] / drivers / scsi / sym53c8xx_2 / sym_misc.c
1 /*
2  * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family 
3  * of PCI-SCSI IO processors.
4  *
5  * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6  *
7  * This driver is derived from the Linux sym53c8xx driver.
8  * Copyright (C) 1998-2000  Gerard Roudier
9  *
10  * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
11  * a port of the FreeBSD ncr driver to Linux-1.2.13.
12  *
13  * The original ncr driver has been written for 386bsd and FreeBSD by
14  *         Wolfgang Stanglmeier        <wolf@cologne.de>
15  *         Stefan Esser                <se@mi.Uni-Koeln.de>
16  * Copyright (C) 1994  Wolfgang Stanglmeier
17  *
18  * Other major contributions:
19  *
20  * NVRAM detection and reading.
21  * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22  *
23  *-----------------------------------------------------------------------------
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. The name of the author may not be used to endorse or promote products
31  *    derived from this software without specific prior written permission.
32  *
33  * Where this Software is combined with software released under the terms of 
34  * the GNU Public License ("GPL") and the terms of the GPL would require the 
35  * combined work to also be released under the terms of the GPL, the terms
36  * and conditions of this License will apply in addition to those of the
37  * GPL with the exception of any terms or conditions of this License that
38  * conflict with, or are expressly prohibited by, the GPL.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
44  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52
53 #ifdef __FreeBSD__
54 #include <dev/sym/sym_glue.h>
55 #else
56 #include "sym_glue.h"
57 #endif
58
59 #ifdef  SYM_OPT_HANDLE_IO_TIMEOUT
60 /*
61  *  Optional CCB timeout handling.
62  *
63  *  This code is useful for O/Ses that allow or expect 
64  *  SIMs (low-level drivers) to handle SCSI IO timeouts.
65  *  It uses a power-of-two based algorithm of my own:) 
66  *  that avoids scanning of lists, provided that:
67  *
68  *  - The IO does complete in less than half the associated
69  *    timeout value.
70  *  - The greatest delay between the queuing of the IO and 
71  *    its completion is less than 
72  *          (1<<(SYM_CONF_TIMEOUT_ORDER_MAX-1))/2 ticks.
73  *
74  *  For example, if tick is 1 second and the max order is 8,
75  *  any IO that is completed within less than 64 seconds will 
76  *  just be put into some list at queuing and be removed 
77  *  at completion without any additionnal overhead.
78  */
79
80 /*
81  *  Set a timeout condition on a CCB.
82  */ 
83 void sym_timeout_ccb(hcb_p np, ccb_p cp, u_int ticks)
84 {
85         sym_remque(&cp->tmo_linkq);
86         cp->tmo_clock = np->tmo_clock + ticks;
87         if (!ticks) {
88                 sym_insque_head(&cp->tmo_linkq, &np->tmo0_ccbq);
89         }
90         else {
91                 int i = SYM_CONF_TIMEOUT_ORDER_MAX - 1;
92                 while (i > 0) {
93                         if (ticks >= (1<<(i+1)))
94                                 break;
95                         --i;
96                 }
97                 if (!(np->tmo_actq & (1<<i)))
98                         i += SYM_CONF_TIMEOUT_ORDER_MAX;
99                 sym_insque_head(&cp->tmo_linkq, &np->tmo_ccbq[i]);
100         }
101 }
102
103 /*
104  *  Walk a list of CCB and handle timeout conditions.
105  *  Should never be called in normal situations.
106  */
107 static void sym_walk_ccb_tmo_list(hcb_p np, SYM_QUEHEAD *tmoq)
108 {
109         SYM_QUEHEAD qtmp, *qp;
110         ccb_p cp;
111
112         sym_que_move(tmoq, &qtmp);
113         while ((qp = sym_remque_head(&qtmp)) != 0) {
114                 sym_insque_head(qp, &np->tmo0_ccbq);
115                 cp = sym_que_entry(qp, struct sym_ccb, tmo_linkq);
116                 if (cp->tmo_clock     != np->tmo_clock &&
117                     cp->tmo_clock + 1 != np->tmo_clock)
118                         sym_timeout_ccb(np, cp, cp->tmo_clock - np->tmo_clock);
119                 else
120                         sym_abort_ccb(np, cp, 1);
121         }
122 }
123
124 /*
125  * Our clock handler called from the O/S specific side.
126  */
127 void sym_clock(hcb_p np)
128 {
129         int i, j;
130         u_int tmp;
131
132         tmp = np->tmo_clock;
133         tmp ^= (++np->tmo_clock);
134
135         for (i = 0; i < SYM_CONF_TIMEOUT_ORDER_MAX; i++, tmp >>= 1) {
136                 if (!(tmp & 1))
137                         continue;
138                 j = i;
139                 if (np->tmo_actq & (1<<i))
140                         j += SYM_CONF_TIMEOUT_ORDER_MAX;
141
142                 if (!sym_que_empty(&np->tmo_ccbq[j])) {
143                         sym_walk_ccb_tmo_list(np, &np->tmo_ccbq[j]);
144                 }
145                 np->tmo_actq ^= (1<<i);
146         }
147 }
148 #endif  /* SYM_OPT_HANDLE_IO_TIMEOUT */
149
150
151 #ifdef  SYM_OPT_ANNOUNCE_TRANSFER_RATE
152 /*
153  *  Announce transfer rate if anything changed since last announcement.
154  */
155 void sym_announce_transfer_rate(hcb_p np, int target)
156 {
157         tcb_p tp = &np->target[target];
158
159 #define __tprev tp->tinfo.prev
160 #define __tcurr tp->tinfo.curr
161
162         if (__tprev.options  == __tcurr.options &&
163             __tprev.width    == __tcurr.width   &&
164             __tprev.offset   == __tcurr.offset  &&
165             !(__tprev.offset && __tprev.period != __tcurr.period))
166                 return;
167
168         __tprev.options  = __tcurr.options;
169         __tprev.width    = __tcurr.width;
170         __tprev.offset   = __tcurr.offset;
171         __tprev.period   = __tcurr.period;
172
173         if (__tcurr.offset && __tcurr.period) {
174                 u_int period, f10, mb10;
175                 char *scsi;
176
177                 period = f10 = mb10 = 0;
178                 scsi = "FAST-5";
179
180                 if (__tcurr.period <= 9) {
181                         scsi = "FAST-80";
182                         period = 125;
183                         mb10 = 1600;
184                 }
185                 else {
186                         if      (__tcurr.period <= 11) {
187                                 scsi = "FAST-40";
188                                 period = 250;
189                                 if (__tcurr.period == 11)
190                                         period = 303;
191                         }
192                         else if (__tcurr.period < 25) {
193                                 scsi = "FAST-20";
194                                 if (__tcurr.period == 12)
195                                         period = 500;
196                         }
197                         else if (__tcurr.period <= 50) {
198                                 scsi = "FAST-10";
199                         }
200                         if (!period)
201                                 period = 40 * __tcurr.period;
202                         f10 = 100000 << (__tcurr.width ? 1 : 0);
203                         mb10 = (f10 + period/2) / period;
204                 }
205                 printf_info (
206                     "%s:%d: %s %sSCSI %d.%d MB/s %s (%d.%d ns, offset %d)\n",
207                     sym_name(np), target, scsi, __tcurr.width? "WIDE " : "",
208                     mb10/10, mb10%10,
209                     (__tcurr.options & PPR_OPT_DT) ? "DT" : "ST",
210                     period/10, period%10, __tcurr.offset);
211         }
212         else
213                 printf_info ("%s:%d: %sasynchronous.\n", 
214                              sym_name(np), target, __tcurr.width? "wide " : "");
215 }
216 #undef __tprev
217 #undef __tcurr
218 #endif  /* SYM_OPT_ANNOUNCE_TRANSFER_RATE */