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