Merge /spare/repo/linux-2.6/
[linux-2.6] / drivers / scsi / aic7xxx / aic7xxx_proc.c
1 /*
2  * Copyright (c) 2000-2001 Adaptec Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  * 3. Neither the names of the above-listed copyright holders nor the names
17  *    of any contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * NO WARRANTY
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGES.
36  *
37  * String handling code courtesy of Gerard Roudier's <groudier@club-internet.fr>
38  * sym driver.
39  *
40  * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_proc.c#29 $
41  */
42 #include "aic7xxx_osm.h"
43 #include "aic7xxx_inline.h"
44 #include "aic7xxx_93cx6.h"
45
46 static void     copy_mem_info(struct info_str *info, char *data, int len);
47 static int      copy_info(struct info_str *info, char *fmt, ...);
48 static void     ahc_dump_target_state(struct ahc_softc *ahc,
49                                       struct info_str *info,
50                                       u_int our_id, char channel,
51                                       u_int target_id, u_int target_offset);
52 static void     ahc_dump_device_state(struct info_str *info,
53                                       struct scsi_device *dev);
54 static int      ahc_proc_write_seeprom(struct ahc_softc *ahc,
55                                        char *buffer, int length);
56
57 /*
58  * Table of syncrates that don't follow the "divisible by 4"
59  * rule. This table will be expanded in future SCSI specs.
60  */
61 static struct {
62         u_int period_factor;
63         u_int period;   /* in 100ths of ns */
64 } scsi_syncrates[] = {
65         { 0x08, 625 },  /* FAST-160 */
66         { 0x09, 1250 }, /* FAST-80 */
67         { 0x0a, 2500 }, /* FAST-40 40MHz */
68         { 0x0b, 3030 }, /* FAST-40 33MHz */
69         { 0x0c, 5000 }  /* FAST-20 */
70 };
71
72 /*
73  * Return the frequency in kHz corresponding to the given
74  * sync period factor.
75  */
76 static u_int
77 ahc_calc_syncsrate(u_int period_factor)
78 {
79         int i;
80         int num_syncrates;
81
82         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
83         /* See if the period is in the "exception" table */
84         for (i = 0; i < num_syncrates; i++) {
85
86                 if (period_factor == scsi_syncrates[i].period_factor) {
87                         /* Period in kHz */
88                         return (100000000 / scsi_syncrates[i].period);
89                 }
90         }
91
92         /*
93          * Wasn't in the table, so use the standard
94          * 4 times conversion.
95          */
96         return (10000000 / (period_factor * 4 * 10));
97 }
98
99
100 static void
101 copy_mem_info(struct info_str *info, char *data, int len)
102 {
103         if (info->pos + len > info->offset + info->length)
104                 len = info->offset + info->length - info->pos;
105
106         if (info->pos + len < info->offset) {
107                 info->pos += len;
108                 return;
109         }
110
111         if (info->pos < info->offset) {
112                 off_t partial;
113
114                 partial = info->offset - info->pos;
115                 data += partial;
116                 info->pos += partial;
117                 len  -= partial;
118         }
119
120         if (len > 0) {
121                 memcpy(info->buffer, data, len);
122                 info->pos += len;
123                 info->buffer += len;
124         }
125 }
126
127 static int
128 copy_info(struct info_str *info, char *fmt, ...)
129 {
130         va_list args;
131         char buf[256];
132         int len;
133
134         va_start(args, fmt);
135         len = vsprintf(buf, fmt, args);
136         va_end(args);
137
138         copy_mem_info(info, buf, len);
139         return (len);
140 }
141
142 void
143 ahc_format_transinfo(struct info_str *info, struct ahc_transinfo *tinfo)
144 {
145         u_int speed;
146         u_int freq;
147         u_int mb;
148
149         speed = 3300;
150         freq = 0;
151         if (tinfo->offset != 0) {
152                 freq = ahc_calc_syncsrate(tinfo->period);
153                 speed = freq;
154         }
155         speed *= (0x01 << tinfo->width);
156         mb = speed / 1000;
157         if (mb > 0)
158                 copy_info(info, "%d.%03dMB/s transfers", mb, speed % 1000);
159         else
160                 copy_info(info, "%dKB/s transfers", speed);
161
162         if (freq != 0) {
163                 copy_info(info, " (%d.%03dMHz%s, offset %d",
164                          freq / 1000, freq % 1000,
165                          (tinfo->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
166                          ? " DT" : "", tinfo->offset);
167         }
168
169         if (tinfo->width > 0) {
170                 if (freq != 0) {
171                         copy_info(info, ", ");
172                 } else {
173                         copy_info(info, " (");
174                 }
175                 copy_info(info, "%dbit)", 8 * (0x01 << tinfo->width));
176         } else if (freq != 0) {
177                 copy_info(info, ")");
178         }
179         copy_info(info, "\n");
180 }
181
182 static void
183 ahc_dump_target_state(struct ahc_softc *ahc, struct info_str *info,
184                       u_int our_id, char channel, u_int target_id,
185                       u_int target_offset)
186 {
187         struct  ahc_linux_target *targ;
188         struct  scsi_target *starget;
189         struct  ahc_initiator_tinfo *tinfo;
190         struct  ahc_tmode_tstate *tstate;
191         int     lun;
192
193         tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
194                                     target_id, &tstate);
195         if ((ahc->features & AHC_TWIN) != 0)
196                 copy_info(info, "Channel %c ", channel);
197         copy_info(info, "Target %d Negotiation Settings\n", target_id);
198         copy_info(info, "\tUser: ");
199         ahc_format_transinfo(info, &tinfo->user);
200         starget = ahc->platform_data->starget[target_offset];
201         if (!starget)
202                 return;
203         targ = scsi_transport_target_data(starget);
204
205         copy_info(info, "\tGoal: ");
206         ahc_format_transinfo(info, &tinfo->goal);
207         copy_info(info, "\tCurr: ");
208         ahc_format_transinfo(info, &tinfo->curr);
209
210         for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
211                 struct scsi_device *sdev;
212
213                 sdev = targ->sdev[lun];
214
215                 if (sdev == NULL)
216                         continue;
217
218                 ahc_dump_device_state(info, sdev);
219         }
220 }
221
222 static void
223 ahc_dump_device_state(struct info_str *info, struct scsi_device *sdev)
224 {
225         struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
226
227         copy_info(info, "\tChannel %c Target %d Lun %d Settings\n",
228                   sdev->sdev_target->channel + 'A',
229                   sdev->sdev_target->id, sdev->lun);
230
231         copy_info(info, "\t\tCommands Queued %ld\n", dev->commands_issued);
232         copy_info(info, "\t\tCommands Active %d\n", dev->active);
233         copy_info(info, "\t\tCommand Openings %d\n", dev->openings);
234         copy_info(info, "\t\tMax Tagged Openings %d\n", dev->maxtags);
235         copy_info(info, "\t\tDevice Queue Frozen Count %d\n", dev->qfrozen);
236 }
237
238 static int
239 ahc_proc_write_seeprom(struct ahc_softc *ahc, char *buffer, int length)
240 {
241         struct seeprom_descriptor sd;
242         int have_seeprom;
243         u_long s;
244         int paused;
245         int written;
246
247         /* Default to failure. */
248         written = -EINVAL;
249         ahc_lock(ahc, &s);
250         paused = ahc_is_paused(ahc);
251         if (!paused)
252                 ahc_pause(ahc);
253
254         if (length != sizeof(struct seeprom_config)) {
255                 printf("ahc_proc_write_seeprom: incorrect buffer size\n");
256                 goto done;
257         }
258
259         have_seeprom = ahc_verify_cksum((struct seeprom_config*)buffer);
260         if (have_seeprom == 0) {
261                 printf("ahc_proc_write_seeprom: cksum verification failed\n");
262                 goto done;
263         }
264
265         sd.sd_ahc = ahc;
266 #if AHC_PCI_CONFIG > 0
267         if ((ahc->chip & AHC_PCI) != 0) {
268                 sd.sd_control_offset = SEECTL;
269                 sd.sd_status_offset = SEECTL;
270                 sd.sd_dataout_offset = SEECTL;
271                 if (ahc->flags & AHC_LARGE_SEEPROM)
272                         sd.sd_chip = C56_66;
273                 else
274                         sd.sd_chip = C46;
275                 sd.sd_MS = SEEMS;
276                 sd.sd_RDY = SEERDY;
277                 sd.sd_CS = SEECS;
278                 sd.sd_CK = SEECK;
279                 sd.sd_DO = SEEDO;
280                 sd.sd_DI = SEEDI;
281                 have_seeprom = ahc_acquire_seeprom(ahc, &sd);
282         } else
283 #endif
284         if ((ahc->chip & AHC_VL) != 0) {
285                 sd.sd_control_offset = SEECTL_2840;
286                 sd.sd_status_offset = STATUS_2840;
287                 sd.sd_dataout_offset = STATUS_2840;             
288                 sd.sd_chip = C46;
289                 sd.sd_MS = 0;
290                 sd.sd_RDY = EEPROM_TF;
291                 sd.sd_CS = CS_2840;
292                 sd.sd_CK = CK_2840;
293                 sd.sd_DO = DO_2840;
294                 sd.sd_DI = DI_2840;
295                 have_seeprom = TRUE;
296         } else {
297                 printf("ahc_proc_write_seeprom: unsupported adapter type\n");
298                 goto done;
299         }
300
301         if (!have_seeprom) {
302                 printf("ahc_proc_write_seeprom: No Serial EEPROM\n");
303                 goto done;
304         } else {
305                 u_int start_addr;
306
307                 if (ahc->seep_config == NULL) {
308                         ahc->seep_config = malloc(sizeof(*ahc->seep_config),
309                                                   M_DEVBUF, M_NOWAIT);
310                         if (ahc->seep_config == NULL) {
311                                 printf("aic7xxx: Unable to allocate serial "
312                                        "eeprom buffer.  Write failing\n");
313                                 goto done;
314                         }
315                 }
316                 printf("aic7xxx: Writing Serial EEPROM\n");
317                 start_addr = 32 * (ahc->channel - 'A');
318                 ahc_write_seeprom(&sd, (u_int16_t *)buffer, start_addr,
319                                   sizeof(struct seeprom_config)/2);
320                 ahc_read_seeprom(&sd, (uint16_t *)ahc->seep_config,
321                                  start_addr, sizeof(struct seeprom_config)/2);
322 #if AHC_PCI_CONFIG > 0
323                 if ((ahc->chip & AHC_VL) == 0)
324                         ahc_release_seeprom(&sd);
325 #endif
326                 written = length;
327         }
328
329 done:
330         if (!paused)
331                 ahc_unpause(ahc);
332         ahc_unlock(ahc, &s);
333         return (written);
334 }
335
336 /*
337  * Return information to handle /proc support for the driver.
338  */
339 int
340 ahc_linux_proc_info(struct Scsi_Host *shost, char *buffer, char **start,
341                     off_t offset, int length, int inout)
342 {
343         struct  ahc_softc *ahc = *(struct ahc_softc **)shost->hostdata;
344         struct  info_str info;
345         char    ahc_info[256];
346         u_int   max_targ;
347         u_int   i;
348         int     retval;
349
350          /* Has data been written to the file? */ 
351         if (inout == TRUE) {
352                 retval = ahc_proc_write_seeprom(ahc, buffer, length);
353                 goto done;
354         }
355
356         if (start)
357                 *start = buffer;
358
359         info.buffer     = buffer;
360         info.length     = length;
361         info.offset     = offset;
362         info.pos        = 0;
363
364         copy_info(&info, "Adaptec AIC7xxx driver version: %s\n",
365                   AIC7XXX_DRIVER_VERSION);
366         copy_info(&info, "%s\n", ahc->description);
367         ahc_controller_info(ahc, ahc_info);
368         copy_info(&info, "%s\n", ahc_info);
369         copy_info(&info, "Allocated SCBs: %d, SG List Length: %d\n\n",
370                   ahc->scb_data->numscbs, AHC_NSEG);
371
372
373         if (ahc->seep_config == NULL)
374                 copy_info(&info, "No Serial EEPROM\n");
375         else {
376                 copy_info(&info, "Serial EEPROM:\n");
377                 for (i = 0; i < sizeof(*ahc->seep_config)/2; i++) {
378                         if (((i % 8) == 0) && (i != 0)) {
379                                 copy_info(&info, "\n");
380                         }
381                         copy_info(&info, "0x%.4x ",
382                                   ((uint16_t*)ahc->seep_config)[i]);
383                 }
384                 copy_info(&info, "\n");
385         }
386         copy_info(&info, "\n");
387
388         max_targ = 15;
389         if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
390                 max_targ = 7;
391
392         for (i = 0; i <= max_targ; i++) {
393                 u_int our_id;
394                 u_int target_id;
395                 char channel;
396
397                 channel = 'A';
398                 our_id = ahc->our_id;
399                 target_id = i;
400                 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
401                         channel = 'B';
402                         our_id = ahc->our_id_b;
403                         target_id = i % 8;
404                 }
405
406                 ahc_dump_target_state(ahc, &info, our_id,
407                                       channel, target_id, i);
408         }
409         retval = info.pos > info.offset ? info.pos - info.offset : 0;
410 done:
411         return (retval);
412 }