f27f52e651dccffa62efda9cc5d09c73fc0a15a9
[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  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38  */
39
40 #ifdef __FreeBSD__
41 #include <dev/sym/sym_glue.h>
42 #else
43 #include "sym_glue.h"
44 #endif
45
46 #ifdef  SYM_OPT_HANDLE_IO_TIMEOUT
47 /*
48  *  Optional CCB timeout handling.
49  *
50  *  This code is useful for O/Ses that allow or expect 
51  *  SIMs (low-level drivers) to handle SCSI IO timeouts.
52  *  It uses a power-of-two based algorithm of my own:) 
53  *  that avoids scanning of lists, provided that:
54  *
55  *  - The IO does complete in less than half the associated
56  *    timeout value.
57  *  - The greatest delay between the queuing of the IO and 
58  *    its completion is less than 
59  *          (1<<(SYM_CONF_TIMEOUT_ORDER_MAX-1))/2 ticks.
60  *
61  *  For example, if tick is 1 second and the max order is 8,
62  *  any IO that is completed within less than 64 seconds will 
63  *  just be put into some list at queuing and be removed 
64  *  at completion without any additionnal overhead.
65  */
66
67 /*
68  *  Set a timeout condition on a CCB.
69  */ 
70 void sym_timeout_ccb(hcb_p np, ccb_p cp, u_int ticks)
71 {
72         sym_remque(&cp->tmo_linkq);
73         cp->tmo_clock = np->tmo_clock + ticks;
74         if (!ticks) {
75                 sym_insque_head(&cp->tmo_linkq, &np->tmo0_ccbq);
76         }
77         else {
78                 int i = SYM_CONF_TIMEOUT_ORDER_MAX - 1;
79                 while (i > 0) {
80                         if (ticks >= (1<<(i+1)))
81                                 break;
82                         --i;
83                 }
84                 if (!(np->tmo_actq & (1<<i)))
85                         i += SYM_CONF_TIMEOUT_ORDER_MAX;
86                 sym_insque_head(&cp->tmo_linkq, &np->tmo_ccbq[i]);
87         }
88 }
89
90 /*
91  *  Walk a list of CCB and handle timeout conditions.
92  *  Should never be called in normal situations.
93  */
94 static void sym_walk_ccb_tmo_list(hcb_p np, SYM_QUEHEAD *tmoq)
95 {
96         SYM_QUEHEAD qtmp, *qp;
97         ccb_p cp;
98
99         sym_que_move(tmoq, &qtmp);
100         while ((qp = sym_remque_head(&qtmp)) != 0) {
101                 sym_insque_head(qp, &np->tmo0_ccbq);
102                 cp = sym_que_entry(qp, struct sym_ccb, tmo_linkq);
103                 if (cp->tmo_clock     != np->tmo_clock &&
104                     cp->tmo_clock + 1 != np->tmo_clock)
105                         sym_timeout_ccb(np, cp, cp->tmo_clock - np->tmo_clock);
106                 else
107                         sym_abort_ccb(np, cp, 1);
108         }
109 }
110
111 /*
112  * Our clock handler called from the O/S specific side.
113  */
114 void sym_clock(hcb_p np)
115 {
116         int i, j;
117         u_int tmp;
118
119         tmp = np->tmo_clock;
120         tmp ^= (++np->tmo_clock);
121
122         for (i = 0; i < SYM_CONF_TIMEOUT_ORDER_MAX; i++, tmp >>= 1) {
123                 if (!(tmp & 1))
124                         continue;
125                 j = i;
126                 if (np->tmo_actq & (1<<i))
127                         j += SYM_CONF_TIMEOUT_ORDER_MAX;
128
129                 if (!sym_que_empty(&np->tmo_ccbq[j])) {
130                         sym_walk_ccb_tmo_list(np, &np->tmo_ccbq[j]);
131                 }
132                 np->tmo_actq ^= (1<<i);
133         }
134 }
135 #endif  /* SYM_OPT_HANDLE_IO_TIMEOUT */
136
137
138 #ifdef  SYM_OPT_ANNOUNCE_TRANSFER_RATE
139 /*
140  *  Announce transfer rate if anything changed since last announcement.
141  */
142 void sym_announce_transfer_rate(hcb_p np, int target)
143 {
144         tcb_p tp = &np->target[target];
145
146 #define __tprev tp->tinfo.prev
147 #define __tcurr tp->tinfo.curr
148
149         if (__tprev.options  == __tcurr.options &&
150             __tprev.width    == __tcurr.width   &&
151             __tprev.offset   == __tcurr.offset  &&
152             !(__tprev.offset && __tprev.period != __tcurr.period))
153                 return;
154
155         __tprev.options  = __tcurr.options;
156         __tprev.width    = __tcurr.width;
157         __tprev.offset   = __tcurr.offset;
158         __tprev.period   = __tcurr.period;
159
160         if (__tcurr.offset && __tcurr.period) {
161                 u_int period, f10, mb10;
162                 char *scsi;
163
164                 period = f10 = mb10 = 0;
165                 scsi = "FAST-5";
166
167                 if (__tcurr.period <= 9) {
168                         scsi = "FAST-80";
169                         period = 125;
170                         mb10 = 1600;
171                 }
172                 else {
173                         if      (__tcurr.period <= 11) {
174                                 scsi = "FAST-40";
175                                 period = 250;
176                                 if (__tcurr.period == 11)
177                                         period = 303;
178                         }
179                         else if (__tcurr.period < 25) {
180                                 scsi = "FAST-20";
181                                 if (__tcurr.period == 12)
182                                         period = 500;
183                         }
184                         else if (__tcurr.period <= 50) {
185                                 scsi = "FAST-10";
186                         }
187                         if (!period)
188                                 period = 40 * __tcurr.period;
189                         f10 = 100000 << (__tcurr.width ? 1 : 0);
190                         mb10 = (f10 + period/2) / period;
191                 }
192                 printf_info (
193                     "%s:%d: %s %sSCSI %d.%d MB/s %s%s%s (%d.%d ns, offset %d)\n",
194                     sym_name(np), target, scsi, __tcurr.width? "WIDE " : "",
195                     mb10/10, mb10%10,
196                     (__tcurr.options & PPR_OPT_DT) ? "DT" : "ST",
197                     (__tcurr.options & PPR_OPT_IU) ? " IU" : "",
198                     (__tcurr.options & PPR_OPT_QAS) ? " QAS" : "",
199                     period/10, period%10, __tcurr.offset);
200         }
201         else
202                 printf_info ("%s:%d: %sasynchronous.\n", 
203                              sym_name(np), target, __tcurr.width? "wide " : "");
204 }
205 #undef __tprev
206 #undef __tcurr
207 #endif  /* SYM_OPT_ANNOUNCE_TRANSFER_RATE */