Merge master.kernel.org:/pub/scm/linux/kernel/git/chrisw/lsm-2.6
[linux-2.6] / drivers / scsi / aic7xxx / aic79xx_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/aic79xx_proc.c#19 $
41  */
42 #include "aic79xx_osm.h"
43 #include "aic79xx_inline.h"
44
45 static void     copy_mem_info(struct info_str *info, char *data, int len);
46 static int      copy_info(struct info_str *info, char *fmt, ...);
47 static void     ahd_dump_target_state(struct ahd_softc *ahd,
48                                       struct info_str *info,
49                                       u_int our_id, char channel,
50                                       u_int target_id, u_int target_offset);
51 static void     ahd_dump_device_state(struct info_str *info,
52                                       struct scsi_device *sdev);
53 static int      ahd_proc_write_seeprom(struct ahd_softc *ahd,
54                                        char *buffer, int length);
55
56 /*
57  * Table of syncrates that don't follow the "divisible by 4"
58  * rule. This table will be expanded in future SCSI specs.
59  */
60 static struct {
61         u_int period_factor;
62         u_int period;   /* in 100ths of ns */
63 } scsi_syncrates[] = {
64         { 0x08, 625 },  /* FAST-160 */
65         { 0x09, 1250 }, /* FAST-80 */
66         { 0x0a, 2500 }, /* FAST-40 40MHz */
67         { 0x0b, 3030 }, /* FAST-40 33MHz */
68         { 0x0c, 5000 }  /* FAST-20 */
69 };
70
71 /*
72  * Return the frequency in kHz corresponding to the given
73  * sync period factor.
74  */
75 static u_int
76 ahd_calc_syncsrate(u_int period_factor)
77 {
78         int i;
79         int num_syncrates;
80
81         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
82         /* See if the period is in the "exception" table */
83         for (i = 0; i < num_syncrates; i++) {
84
85                 if (period_factor == scsi_syncrates[i].period_factor) {
86                         /* Period in kHz */
87                         return (100000000 / scsi_syncrates[i].period);
88                 }
89         }
90
91         /*
92          * Wasn't in the table, so use the standard
93          * 4 times conversion.
94          */
95         return (10000000 / (period_factor * 4 * 10));
96 }
97
98
99 static void
100 copy_mem_info(struct info_str *info, char *data, int len)
101 {
102         if (info->pos + len > info->offset + info->length)
103                 len = info->offset + info->length - info->pos;
104
105         if (info->pos + len < info->offset) {
106                 info->pos += len;
107                 return;
108         }
109
110         if (info->pos < info->offset) {
111                 off_t partial;
112
113                 partial = info->offset - info->pos;
114                 data += partial;
115                 info->pos += partial;
116                 len  -= partial;
117         }
118
119         if (len > 0) {
120                 memcpy(info->buffer, data, len);
121                 info->pos += len;
122                 info->buffer += len;
123         }
124 }
125
126 static int
127 copy_info(struct info_str *info, char *fmt, ...)
128 {
129         va_list args;
130         char buf[256];
131         int len;
132
133         va_start(args, fmt);
134         len = vsprintf(buf, fmt, args);
135         va_end(args);
136
137         copy_mem_info(info, buf, len);
138         return (len);
139 }
140
141 void
142 ahd_format_transinfo(struct info_str *info, struct ahd_transinfo *tinfo)
143 {
144         u_int speed;
145         u_int freq;
146         u_int mb;
147
148         if (tinfo->period == AHD_PERIOD_UNKNOWN) {
149                 copy_info(info, "Renegotiation Pending\n");
150                 return;
151         }
152         speed = 3300;
153         freq = 0;
154         if (tinfo->offset != 0) {
155                 freq = ahd_calc_syncsrate(tinfo->period);
156                 speed = freq;
157         }
158         speed *= (0x01 << tinfo->width);
159         mb = speed / 1000;
160         if (mb > 0)
161                 copy_info(info, "%d.%03dMB/s transfers", mb, speed % 1000);
162         else
163                 copy_info(info, "%dKB/s transfers", speed);
164
165         if (freq != 0) {
166                 int     printed_options;
167
168                 printed_options = 0;
169                 copy_info(info, " (%d.%03dMHz", freq / 1000, freq % 1000);
170                 if ((tinfo->ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
171                         copy_info(info, " RDSTRM");
172                         printed_options++;
173                 }
174                 if ((tinfo->ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
175                         copy_info(info, "%s", printed_options ? "|DT" : " DT");
176                         printed_options++;
177                 }
178                 if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
179                         copy_info(info, "%s", printed_options ? "|IU" : " IU");
180                         printed_options++;
181                 }
182                 if ((tinfo->ppr_options & MSG_EXT_PPR_RTI) != 0) {
183                         copy_info(info, "%s",
184                                   printed_options ? "|RTI" : " RTI");
185                         printed_options++;
186                 }
187                 if ((tinfo->ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
188                         copy_info(info, "%s",
189                                   printed_options ? "|QAS" : " QAS");
190                         printed_options++;
191                 }
192         }
193
194         if (tinfo->width > 0) {
195                 if (freq != 0) {
196                         copy_info(info, ", ");
197                 } else {
198                         copy_info(info, " (");
199                 }
200                 copy_info(info, "%dbit)", 8 * (0x01 << tinfo->width));
201         } else if (freq != 0) {
202                 copy_info(info, ")");
203         }
204         copy_info(info, "\n");
205 }
206
207 static void
208 ahd_dump_target_state(struct ahd_softc *ahd, struct info_str *info,
209                       u_int our_id, char channel, u_int target_id,
210                       u_int target_offset)
211 {
212         struct  ahd_linux_target *targ;
213         struct  scsi_target *starget;
214         struct  ahd_initiator_tinfo *tinfo;
215         struct  ahd_tmode_tstate *tstate;
216         int     lun;
217
218         tinfo = ahd_fetch_transinfo(ahd, channel, our_id,
219                                     target_id, &tstate);
220         copy_info(info, "Target %d Negotiation Settings\n", target_id);
221         copy_info(info, "\tUser: ");
222         ahd_format_transinfo(info, &tinfo->user);
223         starget = ahd->platform_data->starget[target_offset];
224         if (starget == NULL)
225                 return;
226         targ = scsi_transport_target_data(starget);
227
228         copy_info(info, "\tGoal: ");
229         ahd_format_transinfo(info, &tinfo->goal);
230         copy_info(info, "\tCurr: ");
231         ahd_format_transinfo(info, &tinfo->curr);
232
233         for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
234                 struct scsi_device *dev;
235
236                 dev = targ->sdev[lun];
237
238                 if (dev == NULL)
239                         continue;
240
241                 ahd_dump_device_state(info, dev);
242         }
243 }
244
245 static void
246 ahd_dump_device_state(struct info_str *info, struct scsi_device *sdev)
247 {
248         struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
249
250         copy_info(info, "\tChannel %c Target %d Lun %d Settings\n",
251                   sdev->sdev_target->channel + 'A',
252                   sdev->sdev_target->id, sdev->lun);
253
254         copy_info(info, "\t\tCommands Queued %ld\n", dev->commands_issued);
255         copy_info(info, "\t\tCommands Active %d\n", dev->active);
256         copy_info(info, "\t\tCommand Openings %d\n", dev->openings);
257         copy_info(info, "\t\tMax Tagged Openings %d\n", dev->maxtags);
258         copy_info(info, "\t\tDevice Queue Frozen Count %d\n", dev->qfrozen);
259 }
260
261 static int
262 ahd_proc_write_seeprom(struct ahd_softc *ahd, char *buffer, int length)
263 {
264         ahd_mode_state saved_modes;
265         int have_seeprom;
266         u_long s;
267         int paused;
268         int written;
269
270         /* Default to failure. */
271         written = -EINVAL;
272         ahd_lock(ahd, &s);
273         paused = ahd_is_paused(ahd);
274         if (!paused)
275                 ahd_pause(ahd);
276
277         saved_modes = ahd_save_modes(ahd);
278         ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
279         if (length != sizeof(struct seeprom_config)) {
280                 printf("ahd_proc_write_seeprom: incorrect buffer size\n");
281                 goto done;
282         }
283
284         have_seeprom = ahd_verify_cksum((struct seeprom_config*)buffer);
285         if (have_seeprom == 0) {
286                 printf("ahd_proc_write_seeprom: cksum verification failed\n");
287                 goto done;
288         }
289
290         have_seeprom = ahd_acquire_seeprom(ahd);
291         if (!have_seeprom) {
292                 printf("ahd_proc_write_seeprom: No Serial EEPROM\n");
293                 goto done;
294         } else {
295                 u_int start_addr;
296
297                 if (ahd->seep_config == NULL) {
298                         ahd->seep_config = malloc(sizeof(*ahd->seep_config),
299                                                   M_DEVBUF, M_NOWAIT);
300                         if (ahd->seep_config == NULL) {
301                                 printf("aic79xx: Unable to allocate serial "
302                                        "eeprom buffer.  Write failing\n");
303                                 goto done;
304                         }
305                 }
306                 printf("aic79xx: Writing Serial EEPROM\n");
307                 start_addr = 32 * (ahd->channel - 'A');
308                 ahd_write_seeprom(ahd, (u_int16_t *)buffer, start_addr,
309                                   sizeof(struct seeprom_config)/2);
310                 ahd_read_seeprom(ahd, (uint16_t *)ahd->seep_config,
311                                  start_addr, sizeof(struct seeprom_config)/2,
312                                  /*ByteStream*/FALSE);
313                 ahd_release_seeprom(ahd);
314                 written = length;
315         }
316
317 done:
318         ahd_restore_modes(ahd, saved_modes);
319         if (!paused)
320                 ahd_unpause(ahd);
321         ahd_unlock(ahd, &s);
322         return (written);
323 }
324 /*
325  * Return information to handle /proc support for the driver.
326  */
327 int
328 ahd_linux_proc_info(struct Scsi_Host *shost, char *buffer, char **start,
329                     off_t offset, int length, int inout)
330 {
331         struct  ahd_softc *ahd = *(struct ahd_softc **)shost->hostdata;
332         struct  info_str info;
333         char    ahd_info[256];
334         u_int   max_targ;
335         u_int   i;
336         int     retval;
337
338          /* Has data been written to the file? */ 
339         if (inout == TRUE) {
340                 retval = ahd_proc_write_seeprom(ahd, buffer, length);
341                 goto done;
342         }
343
344         if (start)
345                 *start = buffer;
346
347         info.buffer     = buffer;
348         info.length     = length;
349         info.offset     = offset;
350         info.pos        = 0;
351
352         copy_info(&info, "Adaptec AIC79xx driver version: %s\n",
353                   AIC79XX_DRIVER_VERSION);
354         copy_info(&info, "%s\n", ahd->description);
355         ahd_controller_info(ahd, ahd_info);
356         copy_info(&info, "%s\n", ahd_info);
357         copy_info(&info, "Allocated SCBs: %d, SG List Length: %d\n\n",
358                   ahd->scb_data.numscbs, AHD_NSEG);
359
360         max_targ = 15;
361
362         if (ahd->seep_config == NULL)
363                 copy_info(&info, "No Serial EEPROM\n");
364         else {
365                 copy_info(&info, "Serial EEPROM:\n");
366                 for (i = 0; i < sizeof(*ahd->seep_config)/2; i++) {
367                         if (((i % 8) == 0) && (i != 0)) {
368                                 copy_info(&info, "\n");
369                         }
370                         copy_info(&info, "0x%.4x ",
371                                   ((uint16_t*)ahd->seep_config)[i]);
372                 }
373                 copy_info(&info, "\n");
374         }
375         copy_info(&info, "\n");
376
377         if ((ahd->features & AHD_WIDE) == 0)
378                 max_targ = 7;
379
380         for (i = 0; i <= max_targ; i++) {
381
382                 ahd_dump_target_state(ahd, &info, ahd->our_id, 'A',
383                                       /*target_id*/i, /*target_offset*/i);
384         }
385         retval = info.pos > info.offset ? info.pos - info.offset : 0;
386 done:
387         return (retval);
388 }