ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 */
219
220
221 #ifdef  SYM_OPT_SNIFF_INQUIRY
222 /*
223  *  Update transfer settings according to user settings 
224  *  and bits sniffed out from INQUIRY response.
225  */
226 void sym_update_trans_settings(hcb_p np, tcb_p tp)
227 {
228         memcpy(&tp->tinfo.goal, &tp->tinfo.user, sizeof(tp->tinfo.goal));
229
230         if (tp->inq_version >= 4) {
231                 switch(tp->inq_byte56 & INQ56_CLOCKING) {
232                 case INQ56_ST_ONLY:
233                         tp->tinfo.goal.options = 0;
234                         break;
235                 case INQ56_DT_ONLY:
236                 case INQ56_ST_DT:
237                 default:
238                         break;
239                 }
240         }
241
242         if (!((tp->inq_byte7 & tp->inq_byte7_valid) & INQ7_WIDE16)) {
243                 tp->tinfo.goal.width   = 0;
244                 tp->tinfo.goal.options = 0;
245         }
246
247         if (!((tp->inq_byte7 & tp->inq_byte7_valid) & INQ7_SYNC)) {
248                 tp->tinfo.goal.offset  = 0;
249                 tp->tinfo.goal.options = 0;
250         }
251
252         if (tp->tinfo.goal.options & PPR_OPT_DT) {
253                 if (tp->tinfo.goal.offset > np->maxoffs_dt)
254                         tp->tinfo.goal.offset = np->maxoffs_dt;
255         }
256         else {
257                 if (tp->tinfo.goal.offset > np->maxoffs)
258                         tp->tinfo.goal.offset = np->maxoffs;
259         }
260 }
261
262 /*
263  *  Snoop target capabilities from INQUIRY response.
264  *  We only believe device versions >= SCSI-2 that use 
265  *  appropriate response data format (2). But it seems 
266  *  that some CCS devices also support SYNC (?).
267  */
268 int 
269 __sym_sniff_inquiry(hcb_p np, u_char tn, u_char ln,
270                     u_char *inq_data, int inq_len)
271 {
272         tcb_p tp = &np->target[tn];
273         u_char inq_version;
274         u_char inq_byte7;
275         u_char inq_byte56;
276
277         if (!inq_data || inq_len < 2)
278                 return -1;
279
280         /*
281          *  Check device type and qualifier.
282          */
283         if ((inq_data[0] & 0xe0) == 0x60)
284                 return -1;
285
286         /*
287          *  Get SPC version.
288          */
289         if (inq_len <= 2)
290                 return -1;
291         inq_version = inq_data[2] & 0x7;
292
293         /*
294          *  Get SYNC/WIDE16 capabilities.
295          */
296         inq_byte7 = tp->inq_byte7;
297         if (inq_version >= 2 && (inq_data[3] & 0xf) == 2) {
298                 if (inq_len > 7)
299                         inq_byte7 = inq_data[7];
300         }
301         else if (inq_version == 1 && (inq_data[3] & 0xf) == 1)
302                 inq_byte7 = INQ7_SYNC;
303
304         /*
305          *  Get Tagged Command Queuing capability.
306          */
307         if (inq_byte7 & INQ7_CMDQ)
308                 sym_set_bit(tp->cmdq_map, ln);
309         else
310                 sym_clr_bit(tp->cmdq_map, ln);
311         inq_byte7 &= ~INQ7_CMDQ;
312
313         /*
314          *  Get CLOCKING capability.
315          */
316         inq_byte56 = tp->inq_byte56;
317         if (inq_version >= 4 && inq_len > 56)
318                 inq_byte56 = inq_data[56];
319 #if 0
320 printf("XXXXXX [%d] inq_version=%x inq_byte7=%x inq_byte56=%x XXXXX\n",
321         inq_len, inq_version, inq_byte7, inq_byte56);
322 #endif
323         /*
324          *  Trigger a negotiation if needed.
325          */
326         if (tp->inq_version != inq_version ||
327             tp->inq_byte7   != inq_byte7   ||
328             tp->inq_byte56  != inq_byte56) {
329                 tp->inq_version = inq_version;
330                 tp->inq_byte7   = inq_byte7;
331                 tp->inq_byte56  = inq_byte56;
332                 return 1;
333         }
334         return 0;
335 }
336 #endif  /* SYM_OPT_SNIFF_INQUIRY */