1 /*
2  *      Copyright (C) 1993-1996 Bas Laarhoven,
3  *                    1996-1997 Claus-Justus Heine.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2, or (at your option)
8  any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING.  If not, write to
17  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19  *
20  * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-ctl.c,v $
21  * $Revision: 1.4 $
22  * $Date: 1997/11/11 14:37:44 $
23  *
24  *      This file contains the non-read/write ftape functions for the
25  *      QIC-40/80/3010/3020 floppy-tape driver "ftape" for Linux.
26  */
27 
28 #include <linux/config.h>
29 #include <linux/errno.h>
30 #include <linux/mm.h>
31 #include <linux/mman.h>
32 #include <linux/wrapper.h>
33 
34 #include <linux/ftape.h>
35 #include <linux/qic117.h>
36 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,6)
37 #include <asm/uaccess.h>
38 #else
39 #include <asm/segment.h>
40 #endif
41 #include <asm/io.h>
42 
43 /* ease porting between pre-2.4.x and later kernels */
44 #define vma_get_pgoff(v)      ((v)->vm_pgoff)
45 
46 #include "../lowlevel/ftape-tracing.h"
47 #include "../lowlevel/ftape-io.h"
48 #include "../lowlevel/ftape-ctl.h"
49 #include "../lowlevel/ftape-write.h"
50 #include "../lowlevel/ftape-read.h"
51 #include "../lowlevel/ftape-rw.h"
52 #include "../lowlevel/ftape-bsm.h"
53 
54 /*      Global vars.
55  */
56 ftape_info ftape_status = {
57 /*  vendor information */
58 	{ 0, },     /* drive type */
59 /*  data rates */
60 	500,        /* used data rate */
61 	500,        /* drive max rate */
62 	500,        /* fdc max rate   */
63 /*  drive selection, either FTAPE_SEL_A/B/C/D */
64 	-1,     /* drive selection */
65 /*  flags set after decode the drive and tape status   */
66 	0,          /* formatted */
67 	1,          /* no tape */
68 	1,          /* write protected */
69 	1,          /* new tape */
70 /*  values of last queried drive/tape status and error */
71 	{{0,}},     /* last error code */
72 	{{0,}},     /* drive status, configuration, tape status */
73 /*  cartridge geometry */
74         20,         /* tracks_per_tape */
75         102,        /* segments_per_track */
76 /*  location of header segments, etc. */
77 	-1,     /* used_header_segment */
78 	-1,     /* header_segment_1 */
79 	-1,     /* header_segment_2 */
80 	-1,     /* first_data_segment */
81         -1,     /* last_data_segment */
82 /*  the format code as stored in the header segment  */
83 	fmt_normal, /* format code */
84 /*  the default for the qic std: unknown */
85 	-1,
86 /*  is tape running? */
87 	idle,       /* runner_state */
88 /*  is tape reading/writing/verifying/formatting/deleting */
89 	idle,       /* driver state */
90 /*  flags fatal hardware error */
91 	1,          /* failure */
92 /*  history record */
93 	{ 0, }      /* history record */
94 };
95 
96 int ftape_segments_per_head     = 1020;
97 int ftape_segments_per_cylinder = 4;
98 int ftape_init_drive_needed = 1; /* need to be global for ftape_reset_drive()
99 				  * in ftape-io.c
100 				  */
101 
102 /*      Local vars.
103  */
104 static const vendor_struct vendors[] = QIC117_VENDORS;
105 static const wakeup_method methods[] = WAKEUP_METHODS;
106 
ftape_get_status(void)107 const ftape_info *ftape_get_status(void)
108 {
109 #if defined(STATUS_PARANOYA)
110 	static ftape_info get_status;
111 
112 	get_status = ftape_status;
113 	return &get_status;
114 #else
115 	return &ftape_status; /*  maybe return only a copy of it to assure
116 			       *  read only access
117 			       */
118 #endif
119 }
120 
ftape_set_status(const ftape_info * status)121 void ftape_set_status(const ftape_info *status)
122 {
123 	ftape_status = *status;
124 }
125 
ftape_not_operational(int status)126 static int ftape_not_operational(int status)
127 {
128 	/* return true if status indicates tape can not be used.
129 	 */
130 	return ((status ^ QIC_STATUS_CARTRIDGE_PRESENT) &
131 		(QIC_STATUS_ERROR |
132 		 QIC_STATUS_CARTRIDGE_PRESENT |
133 		 QIC_STATUS_NEW_CARTRIDGE));
134 }
135 
ftape_seek_to_eot(void)136 int ftape_seek_to_eot(void)
137 {
138 	int status;
139 	TRACE_FUN(ft_t_any);
140 
141 	TRACE_CATCH(ftape_ready_wait(ftape_timeout.pause, &status),);
142 	while ((status & QIC_STATUS_AT_EOT) == 0) {
143 		if (ftape_not_operational(status)) {
144 			TRACE_EXIT -EIO;
145 		}
146 		TRACE_CATCH(ftape_command_wait(QIC_PHYSICAL_FORWARD,
147 					       ftape_timeout.rewind,&status),);
148 	}
149 	TRACE_EXIT 0;
150 }
151 
ftape_seek_to_bot(void)152 int ftape_seek_to_bot(void)
153 {
154 	int status;
155 	TRACE_FUN(ft_t_any);
156 
157 	TRACE_CATCH(ftape_ready_wait(ftape_timeout.pause, &status),);
158 	while ((status & QIC_STATUS_AT_BOT) == 0) {
159 		if (ftape_not_operational(status)) {
160 			TRACE_EXIT -EIO;
161 		}
162 		TRACE_CATCH(ftape_command_wait(QIC_PHYSICAL_REVERSE,
163 					       ftape_timeout.rewind,&status),);
164 	}
165 	TRACE_EXIT 0;
166 }
167 
ftape_new_cartridge(void)168 static int ftape_new_cartridge(void)
169 {
170 	ft_location.track = -1; /* force seek on first access */
171 	ftape_zap_read_buffers();
172 	ftape_zap_write_buffers();
173 	return 0;
174 }
175 
ftape_abort_operation(void)176 int ftape_abort_operation(void)
177 {
178 	int result = 0;
179 	int status;
180 	TRACE_FUN(ft_t_flow);
181 
182 	if (ft_runner_status == running) {
183 		TRACE(ft_t_noise, "aborting runner, waiting");
184 
185 		ft_runner_status = do_abort;
186 		/* set timeout so that the tape will run to logical EOT
187 		 * if we missed the last sector and there are no queue pulses.
188 		 */
189 		result = ftape_dumb_stop();
190 	}
191 	if (ft_runner_status != idle) {
192 		if (ft_runner_status == do_abort) {
193 			TRACE(ft_t_noise, "forcing runner abort");
194 		}
195 		TRACE(ft_t_noise, "stopping tape");
196 		result = ftape_stop_tape(&status);
197 		ft_location.known = 0;
198 		ft_runner_status  = idle;
199 	}
200 	ftape_reset_buffer();
201 	ftape_zap_read_buffers();
202 	ftape_set_state(idle);
203 	TRACE_EXIT result;
204 }
205 
lookup_vendor_id(unsigned int vendor_id)206 static int lookup_vendor_id(unsigned int vendor_id)
207 {
208 	int i = 0;
209 
210 	while (vendors[i].vendor_id != vendor_id) {
211 		if (++i >= NR_ITEMS(vendors)) {
212 			return -1;
213 		}
214 	}
215 	return i;
216 }
217 
ftape_detach_drive(void)218 void ftape_detach_drive(void)
219 {
220 	TRACE_FUN(ft_t_any);
221 
222 	TRACE(ft_t_flow, "disabling tape drive and fdc");
223 	ftape_put_drive_to_sleep(ft_drive_type.wake_up);
224 	fdc_catch_stray_interrupts(1);	/* one always comes */
225 	fdc_disable();
226 	fdc_release_irq_and_dma();
227 	fdc_release_regions();
228 	TRACE_EXIT;
229 }
230 
clear_history(void)231 static void clear_history(void)
232 {
233 	ft_history.used = 0;
234 	ft_history.id_am_errors =
235 		ft_history.id_crc_errors =
236 		ft_history.data_am_errors =
237 		ft_history.data_crc_errors =
238 		ft_history.overrun_errors =
239 		ft_history.no_data_errors =
240 		ft_history.retries =
241 		ft_history.crc_errors =
242 		ft_history.crc_failures =
243 		ft_history.ecc_failures =
244 		ft_history.corrected =
245 		ft_history.defects =
246 		ft_history.rewinds = 0;
247 }
248 
ftape_activate_drive(vendor_struct * drive_type)249 int ftape_activate_drive(vendor_struct * drive_type)
250 {
251 	int result = 0;
252 	TRACE_FUN(ft_t_flow);
253 
254 	/* If we already know the drive type, wake it up.
255 	 * Else try to find out what kind of drive is attached.
256 	 */
257 	if (drive_type->wake_up != unknown_wake_up) {
258 		TRACE(ft_t_flow, "enabling tape drive and fdc");
259 		result = ftape_wakeup_drive(drive_type->wake_up);
260 		if (result < 0) {
261 			TRACE(ft_t_err, "known wakeup method failed");
262 		}
263 	} else {
264 		wake_up_types method;
265 		const ft_trace_t old_tracing = TRACE_LEVEL;
266 		if (TRACE_LEVEL < ft_t_flow) {
267 			SET_TRACE_LEVEL(ft_t_bug);
268 		}
269 
270 		/*  Try to awaken the drive using all known methods.
271 		 *  Lower tracing for a while.
272 		 */
273 		for (method=no_wake_up; method < NR_ITEMS(methods); ++method) {
274 			drive_type->wake_up = method;
275 #ifdef CONFIG_FT_TWO_DRIVES
276 			/*  Test setup for dual drive configuration.
277 			 *  /dev/rft2 uses mountain wakeup
278 			 *  /dev/rft3 uses colorado wakeup
279 			 *  Other systems will use the normal scheme.
280 			 */
281 			if ((ft_drive_sel < 2)                            ||
282 			    (ft_drive_sel == 2 && method == FT_WAKE_UP_1) ||
283 			    (ft_drive_sel == 3 && method == FT_WAKE_UP_2)) {
284 				result=ftape_wakeup_drive(drive_type->wake_up);
285 			} else {
286 				result = -EIO;
287 			}
288 #else
289 			result = ftape_wakeup_drive(drive_type->wake_up);
290 #endif
291 			if (result >= 0) {
292 				TRACE(ft_t_warn, "drive wakeup method: %s",
293 				      methods[drive_type->wake_up].name);
294 				break;
295 			}
296 		}
297 		SET_TRACE_LEVEL(old_tracing);
298 
299 		if (method >= NR_ITEMS(methods)) {
300 			/* no response at all, cannot open this drive */
301 			drive_type->wake_up = unknown_wake_up;
302 			TRACE(ft_t_err, "no tape drive found !");
303 			result = -ENODEV;
304 		}
305 	}
306 	TRACE_EXIT result;
307 }
308 
ftape_get_drive_status(void)309 int ftape_get_drive_status(void)
310 {
311 	int result;
312 	int status;
313 	TRACE_FUN(ft_t_flow);
314 
315 	ft_no_tape = ft_write_protected = 0;
316 	/*    Tape drive is activated now.
317 	 *    First clear error status if present.
318 	 */
319 	do {
320 		result = ftape_ready_wait(ftape_timeout.reset, &status);
321 		if (result < 0) {
322 			if (result == -ETIME) {
323 				TRACE(ft_t_err, "ftape_ready_wait timeout");
324 			} else if (result == -EINTR) {
325 				TRACE(ft_t_err, "ftape_ready_wait aborted");
326 			} else {
327 				TRACE(ft_t_err, "ftape_ready_wait failed");
328 			}
329 			TRACE_EXIT -EIO;
330 		}
331 		/*  Clear error condition (drive is ready !)
332 		 */
333 		if (status & QIC_STATUS_ERROR) {
334 			unsigned int error;
335 			qic117_cmd_t command;
336 
337 			TRACE(ft_t_err, "error status set");
338 			result = ftape_report_error(&error, &command, 1);
339 			if (result < 0) {
340 				TRACE(ft_t_err,
341 				      "report_error_code failed: %d", result);
342 				/* hope it's working next time */
343 				ftape_reset_drive();
344 				TRACE_EXIT -EIO;
345 			} else if (error != 0) {
346 				TRACE(ft_t_noise, "error code   : %d", error);
347 				TRACE(ft_t_noise, "error command: %d", command);
348 			}
349 		}
350 		if (status & QIC_STATUS_NEW_CARTRIDGE) {
351 			unsigned int error;
352 			qic117_cmd_t command;
353 			const ft_trace_t old_tracing = TRACE_LEVEL;
354 			SET_TRACE_LEVEL(ft_t_bug);
355 
356 			/*  Undocumented feature: Must clear (not present!)
357 			 *  error here or we'll fail later.
358 			 */
359 			ftape_report_error(&error, &command, 1);
360 
361 			SET_TRACE_LEVEL(old_tracing);
362 			TRACE(ft_t_info, "status: new cartridge");
363 			ft_new_tape = 1;
364 		} else {
365 			ft_new_tape = 0;
366 		}
367 		FT_SIGNAL_EXIT(_DONT_BLOCK);
368 	} while (status & QIC_STATUS_ERROR);
369 
370 	ft_no_tape = !(status & QIC_STATUS_CARTRIDGE_PRESENT);
371 	ft_write_protected = (status & QIC_STATUS_WRITE_PROTECT) != 0;
372 	if (ft_no_tape) {
373 		TRACE(ft_t_warn, "no cartridge present");
374 	} else {
375 		if (ft_write_protected) {
376 			TRACE(ft_t_noise, "Write protected cartridge");
377 		}
378 	}
379 	TRACE_EXIT 0;
380 }
381 
ftape_log_vendor_id(void)382 void ftape_log_vendor_id(void)
383 {
384 	int vendor_index;
385 	TRACE_FUN(ft_t_flow);
386 
387 	ftape_report_vendor_id(&ft_drive_type.vendor_id);
388 	vendor_index = lookup_vendor_id(ft_drive_type.vendor_id);
389 	if (ft_drive_type.vendor_id == UNKNOWN_VENDOR &&
390 	    ft_drive_type.wake_up == wake_up_colorado) {
391 		vendor_index = 0;
392 		/* hack to get rid of all this mail */
393 		ft_drive_type.vendor_id = 0;
394 	}
395 	if (vendor_index < 0) {
396 		/* Unknown vendor id, first time opening device.  The
397 		 * drive_type remains set to type found at wakeup
398 		 * time, this will probably keep the driver operating
399 		 * for this new vendor.
400 		 */
401 		TRACE(ft_t_warn, "\n"
402 		      KERN_INFO "============ unknown vendor id ===========\n"
403 		      KERN_INFO "A new, yet unsupported tape drive is found\n"
404 		      KERN_INFO "Please report the following values:\n"
405 		      KERN_INFO "   Vendor id     : 0x%04x\n"
406 		      KERN_INFO "   Wakeup method : %s\n"
407 		      KERN_INFO "And a description of your tape drive\n"
408 		      KERN_INFO "to "THE_FTAPE_MAINTAINER"\n"
409 		      KERN_INFO "==========================================",
410 		      ft_drive_type.vendor_id,
411 		      methods[ft_drive_type.wake_up].name);
412 		ft_drive_type.speed = 0;		/* unknown */
413 	} else {
414 		ft_drive_type.name  = vendors[vendor_index].name;
415 		ft_drive_type.speed = vendors[vendor_index].speed;
416 		TRACE(ft_t_info, "tape drive type: %s", ft_drive_type.name);
417 		/* scan all methods for this vendor_id in table */
418 		while(ft_drive_type.wake_up != vendors[vendor_index].wake_up) {
419 			if (vendor_index < NR_ITEMS(vendors) - 1 &&
420 			    vendors[vendor_index + 1].vendor_id
421 			    ==
422 			    ft_drive_type.vendor_id) {
423 				++vendor_index;
424 			} else {
425 				break;
426 			}
427 		}
428 		if (ft_drive_type.wake_up != vendors[vendor_index].wake_up) {
429 			TRACE(ft_t_warn, "\n"
430 		     KERN_INFO "==========================================\n"
431 		     KERN_INFO "wakeup type mismatch:\n"
432 		     KERN_INFO "found: %s, expected: %s\n"
433 		     KERN_INFO "please report this to "THE_FTAPE_MAINTAINER"\n"
434 		     KERN_INFO "==========================================",
435 			      methods[ft_drive_type.wake_up].name,
436 			      methods[vendors[vendor_index].wake_up].name);
437 		}
438 	}
439 	TRACE_EXIT;
440 }
441 
ftape_calc_timeouts(unsigned int qic_std,unsigned int data_rate,unsigned int tape_len)442 void ftape_calc_timeouts(unsigned int qic_std,
443 			 unsigned int data_rate,
444 			 unsigned int tape_len)
445 {
446 	int speed;		/* deci-ips ! */
447 	int ff_speed;
448 	int length;
449 	TRACE_FUN(ft_t_any);
450 
451 	/*                           tape transport speed
452 	 *  data rate:        QIC-40   QIC-80   QIC-3010 QIC-3020
453 	 *
454 	 *    250 Kbps        25 ips     n/a      n/a      n/a
455 	 *    500 Kbps        50 ips   34 ips   22.6 ips   n/a
456 	 *      1 Mbps          n/a    68 ips   45.2 ips 22.6 ips
457 	 *      2 Mbps          n/a      n/a      n/a    45.2 ips
458 	 *
459 	 *  fast tape transport speed is at least 68 ips.
460 	 */
461 	switch (qic_std) {
462 	case QIC_TAPE_QIC40:
463 		speed = (data_rate == 250) ? 250 : 500;
464 		break;
465 	case QIC_TAPE_QIC80:
466 		speed = (data_rate == 500) ? 340 : 680;
467 		break;
468 	case QIC_TAPE_QIC3010:
469 		speed = (data_rate == 500) ? 226 : 452;
470 		break;
471 	case QIC_TAPE_QIC3020:
472 		speed = (data_rate == 1000) ? 226 : 452;
473 		break;
474 	default:
475 		TRACE(ft_t_bug, "Unknown qic_std (bug) ?");
476 		speed = 500;
477 		break;
478 	}
479 	if (ft_drive_type.speed == 0) {
480 		unsigned long t0;
481 		static int dt = 0;     /* keep gcc from complaining */
482 		static int first_time = 1;
483 
484 		/*  Measure the time it takes to wind to EOT and back to BOT.
485 		 *  If the tape length is known, calculate the rewind speed.
486 		 *  Else keep the time value for calculation of the rewind
487 		 *  speed later on, when the length _is_ known.
488 		 *  Ask for a report only when length and speed are both known.
489 		 */
490 		if (first_time) {
491 			ftape_seek_to_bot();
492 			t0 = jiffies;
493 			ftape_seek_to_eot();
494 			ftape_seek_to_bot();
495 			dt = (int) (((jiffies - t0) * FT_USPT) / 1000);
496 			if (dt < 1) {
497 				dt = 1;	/* prevent div by zero on failures */
498 			}
499 			first_time = 0;
500 			TRACE(ft_t_info,
501 			      "trying to determine seek timeout, got %d msec",
502 			      dt);
503 		}
504 		if (tape_len != 0) {
505 			ft_drive_type.speed =
506 				(2 * 12 * tape_len * 1000) / dt;
507 			TRACE(ft_t_warn, "\n"
508 		     KERN_INFO "==========================================\n"
509 		     KERN_INFO "drive type: %s\n"
510 		     KERN_INFO "delta time = %d ms, length = %d ft\n"
511 		     KERN_INFO "has a maximum tape speed of %d ips\n"
512 		     KERN_INFO "please report this to "THE_FTAPE_MAINTAINER"\n"
513 		     KERN_INFO "==========================================",
514 			      ft_drive_type.name, dt, tape_len,
515 			      ft_drive_type.speed);
516 		}
517 	}
518 	/*  Handle unknown length tapes as very long ones. We'll
519 	 *  determine the actual length from a header segment later.
520 	 *  This is normal for all modern (Wide,TR1/2/3) formats.
521 	 */
522 	if (tape_len <= 0) {
523 		TRACE(ft_t_noise,
524 		      "Unknown tape length, using maximal timeouts");
525 		length = QIC_TOP_TAPE_LEN;	/* use worst case values */
526 	} else {
527 		length = tape_len;		/* use actual values */
528 	}
529 	if (ft_drive_type.speed == 0) {
530 		ff_speed = speed;
531 	} else {
532 		ff_speed = ft_drive_type.speed;
533 	}
534 	/*  time to go from bot to eot at normal speed (data rate):
535 	 *  time = (1+delta) * length (ft) * 12 (inch/ft) / speed (ips)
536 	 *  delta = 10 % for seek speed, 20 % for rewind speed.
537 	 */
538 	ftape_timeout.seek = (length * 132 * FT_SECOND) / speed;
539 	ftape_timeout.rewind = (length * 144 * FT_SECOND) / (10 * ff_speed);
540 	ftape_timeout.reset = 20 * FT_SECOND + ftape_timeout.rewind;
541 	TRACE(ft_t_noise, "timeouts for speed = %d, length = %d\n"
542 	      KERN_INFO "seek timeout  : %d sec\n"
543 	      KERN_INFO "rewind timeout: %d sec\n"
544 	      KERN_INFO "reset timeout : %d sec",
545 	      speed, length,
546 	      (ftape_timeout.seek + 500) / 1000,
547 	      (ftape_timeout.rewind + 500) / 1000,
548 	      (ftape_timeout.reset + 500) / 1000);
549 	TRACE_EXIT;
550 }
551 
552 /* This function calibrates the datarate (i.e. determines the maximal
553  * usable data rate) and sets the global variable ft_qic_std to qic_std
554  *
555  */
ftape_calibrate_data_rate(unsigned int qic_std)556 int ftape_calibrate_data_rate(unsigned int qic_std)
557 {
558 	int rate = ft_fdc_rate_limit;
559 	int result;
560 	TRACE_FUN(ft_t_flow);
561 
562 	ft_qic_std = qic_std;
563 
564 	if (ft_qic_std == -1) {
565 		TRACE_ABORT(-EIO, ft_t_err,
566 		"Unable to determine data rate if QIC standard is unknown");
567 	}
568 
569 	/*  Select highest rate supported by both fdc and drive.
570 	 *  Start with highest rate supported by the fdc.
571 	 */
572 	while (fdc_set_data_rate(rate) < 0 && rate > 250) {
573 		rate /= 2;
574 	}
575 	TRACE(ft_t_info,
576 	      "Highest FDC supported data rate: %d Kbps", rate);
577 	ft_fdc_max_rate = rate;
578 	do {
579 		result = ftape_set_data_rate(rate, ft_qic_std);
580 	} while (result == -EINVAL && (rate /= 2) > 250);
581 	if (result < 0) {
582 		TRACE_ABORT(-EIO, ft_t_err, "set datarate failed");
583 	}
584 	ft_data_rate = rate;
585 	TRACE_EXIT 0;
586 }
587 
ftape_init_drive(void)588 int ftape_init_drive(void)
589 {
590 	int status;
591 	qic_model model;
592 	unsigned int qic_std;
593 	unsigned int data_rate;
594 	TRACE_FUN(ft_t_flow);
595 
596 	ftape_init_drive_needed = 0; /* don't retry if this fails ? */
597 	TRACE_CATCH(ftape_report_raw_drive_status(&status),);
598 	if (status & QIC_STATUS_CARTRIDGE_PRESENT) {
599 		if (!(status & QIC_STATUS_AT_BOT)) {
600 			/*  Antique drives will get here after a soft reset,
601 			 *  modern ones only if the driver is loaded when the
602 			 *  tape wasn't rewound properly.
603 			 */
604 			/* Tape should be at bot if new cartridge ! */
605 			ftape_seek_to_bot();
606 		}
607 		if (!(status & QIC_STATUS_REFERENCED)) {
608 			TRACE(ft_t_flow, "starting seek_load_point");
609 			TRACE_CATCH(ftape_command_wait(QIC_SEEK_LOAD_POINT,
610 						       ftape_timeout.reset,
611 						       &status),);
612 		}
613 	}
614 	ft_formatted = (status & QIC_STATUS_REFERENCED) != 0;
615 	if (!ft_formatted) {
616 		TRACE(ft_t_warn, "Warning: tape is not formatted !");
617 	}
618 
619 	/*  report configuration aborts when ftape_tape_len == -1
620 	 *  unknown qic_std is okay if not formatted.
621 	 */
622 	TRACE_CATCH(ftape_report_configuration(&model,
623 					       &data_rate,
624 					       &qic_std,
625 					       &ftape_tape_len),);
626 
627 	/*  Maybe add the following to the /proc entry
628 	 */
629 	TRACE(ft_t_info, "%s drive @ %d Kbps",
630 	      (model == prehistoric) ? "prehistoric" :
631 	      ((model == pre_qic117c) ? "pre QIC-117C" :
632 	       ((model == post_qic117b) ? "post QIC-117B" :
633 		"post QIC-117D")), data_rate);
634 
635 	if (ft_formatted) {
636 		/*  initialize ft_used_data_rate to maximum value
637 		 *  and set ft_qic_std
638 		 */
639 		TRACE_CATCH(ftape_calibrate_data_rate(qic_std),);
640 		if (ftape_tape_len == 0) {
641 			TRACE(ft_t_info, "unknown length QIC-%s tape",
642 			      (ft_qic_std == QIC_TAPE_QIC40) ? "40" :
643 			      ((ft_qic_std == QIC_TAPE_QIC80) ? "80" :
644 			       ((ft_qic_std == QIC_TAPE_QIC3010)
645 				? "3010" : "3020")));
646 		} else {
647 			TRACE(ft_t_info, "%d ft. QIC-%s tape", ftape_tape_len,
648 			      (ft_qic_std == QIC_TAPE_QIC40) ? "40" :
649 			      ((ft_qic_std == QIC_TAPE_QIC80) ? "80" :
650 			       ((ft_qic_std == QIC_TAPE_QIC3010)
651 				? "3010" : "3020")));
652 		}
653 		ftape_calc_timeouts(ft_qic_std, ft_data_rate, ftape_tape_len);
654 		/* soft write-protect QIC-40/QIC-80 cartridges used with a
655 		 * Colorado T3000 drive. Buggy hardware!
656 		 */
657 		if ((ft_drive_type.vendor_id == 0x011c6) &&
658 		    ((ft_qic_std == QIC_TAPE_QIC40 ||
659 		      ft_qic_std == QIC_TAPE_QIC80) &&
660 		     !ft_write_protected)) {
661 			TRACE(ft_t_warn, "\n"
662 	KERN_INFO "The famous Colorado T3000 bug:\n"
663 	KERN_INFO "%s drives can't write QIC40 and QIC80\n"
664 	KERN_INFO "cartridges but don't set the write-protect flag!",
665 			      ft_drive_type.name);
666 			ft_write_protected = 1;
667 		}
668 	} else {
669 		/*  Doesn't make too much sense to set the data rate
670 		 *  because we don't know what to use for the write
671 		 *  precompensation.
672 		 *  Need to do this again when formatting the cartridge.
673 		 */
674 		ft_data_rate = data_rate;
675 		ftape_calc_timeouts(QIC_TAPE_QIC40,
676 				    data_rate,
677 				    ftape_tape_len);
678 	}
679 	ftape_new_cartridge();
680 	TRACE_EXIT 0;
681 }
682 
ftape_munmap(void)683 static void ftape_munmap(void)
684 {
685 	int i;
686 	TRACE_FUN(ft_t_flow);
687 
688 	for (i = 0; i < ft_nr_buffers; i++) {
689 		ft_buffer[i]->mmapped = 0;
690 	}
691 	TRACE_EXIT;
692 }
693 
694 /*   Map the dma buffers into the virtual address range given by vma.
695  *   We only check the caller doesn't map non-existent buffers. We
696  *   don't check for multiple mappings.
697  */
ftape_mmap(struct vm_area_struct * vma)698 int ftape_mmap(struct vm_area_struct *vma)
699 {
700 	int num_buffers;
701 	int i;
702 	TRACE_FUN(ft_t_flow);
703 
704 	if (ft_failure) {
705 		TRACE_EXIT -ENODEV;
706 	}
707 	if (!(vma->vm_flags & (VM_READ|VM_WRITE))) {
708 		TRACE_ABORT(-EINVAL, ft_t_err, "Undefined mmap() access");
709 	}
710 	if (vma_get_pgoff(vma) != 0) {
711 		TRACE_ABORT(-EINVAL, ft_t_err, "page offset must be 0");
712 	}
713 	if ((vma->vm_end - vma->vm_start) % FT_BUFF_SIZE != 0) {
714 		TRACE_ABORT(-EINVAL, ft_t_err,
715 			    "size = %ld, should be a multiple of %d",
716 			    vma->vm_end - vma->vm_start,
717 			    FT_BUFF_SIZE);
718 	}
719 	num_buffers = (vma->vm_end - vma->vm_start) / FT_BUFF_SIZE;
720 	if (num_buffers > ft_nr_buffers) {
721 		TRACE_ABORT(-EINVAL,
722 			    ft_t_err, "size = %ld, should be less than %d",
723 			    vma->vm_end - vma->vm_start,
724 			    ft_nr_buffers * FT_BUFF_SIZE);
725 	}
726 	if (ft_driver_state != idle) {
727 		/* this also clears the buffer states
728 		 */
729 		ftape_abort_operation();
730 	} else {
731 		ftape_reset_buffer();
732 	}
733 	for (i = 0; i < num_buffers; i++) {
734 		TRACE_CATCH(remap_page_range(vma->vm_start +
735 					     i * FT_BUFF_SIZE,
736 					     virt_to_phys(ft_buffer[i]->address),
737 					     FT_BUFF_SIZE,
738 					     vma->vm_page_prot),
739 			    _res = -EAGAIN);
740 		TRACE(ft_t_noise, "remapped dma buffer @ %p to location @ %p",
741 		      ft_buffer[i]->address,
742 		      (void *)(vma->vm_start + i * FT_BUFF_SIZE));
743 	}
744 	for (i = 0; i < num_buffers; i++) {
745 		memset(ft_buffer[i]->address, 0xAA, FT_BUFF_SIZE);
746 		ft_buffer[i]->mmapped++;
747 	}
748 	TRACE_EXIT 0;
749 }
750 
751 static void ftape_init_driver(void); /* forward declaration */
752 
753 /*      OPEN routine called by kernel-interface code
754  */
ftape_enable(int drive_selection)755 int ftape_enable(int drive_selection)
756 {
757 	TRACE_FUN(ft_t_any);
758 
759 	if (ft_drive_sel == -1 || ft_drive_sel != drive_selection) {
760 		/* Other selection than last time
761 		 */
762 		ftape_init_driver();
763 	}
764 	ft_drive_sel = FTAPE_SEL(drive_selection);
765 	ft_failure = 0;
766 	TRACE_CATCH(fdc_init(),); /* init & detect fdc */
767 	TRACE_CATCH(ftape_activate_drive(&ft_drive_type),
768 		    fdc_disable();
769 		    fdc_release_irq_and_dma();
770 		    fdc_release_regions());
771 	TRACE_CATCH(ftape_get_drive_status(), ftape_detach_drive());
772 	if (ft_drive_type.vendor_id == UNKNOWN_VENDOR) {
773 		ftape_log_vendor_id();
774 	}
775 	if (ft_new_tape) {
776 		ftape_init_drive_needed = 1;
777 	}
778 	if (!ft_no_tape && ftape_init_drive_needed) {
779 		TRACE_CATCH(ftape_init_drive(), ftape_detach_drive());
780 	}
781 	ftape_munmap(); /* clear the mmap flag */
782 	clear_history();
783 	TRACE_EXIT 0;
784 }
785 
786 /*   release routine called by the high level interface modules
787  *   zftape or sftape.
788  */
ftape_disable(void)789 void ftape_disable(void)
790 {
791 	int i;
792 	TRACE_FUN(ft_t_any);
793 
794 	for (i = 0; i < ft_nr_buffers; i++) {
795 		if (ft_buffer[i]->mmapped) {
796 			TRACE(ft_t_noise, "first byte of buffer %d: 0x%02x",
797 			      i, *ft_buffer[i]->address);
798 		}
799 	}
800 	if (sigtestsetmask(&current->pending.signal, _DONT_BLOCK) &&
801 	    !(sigtestsetmask(&current->pending.signal, _NEVER_BLOCK)) &&
802 	    ftape_tape_running) {
803 		TRACE(ft_t_warn,
804 		      "Interrupted by fatal signal and tape still running");
805 		ftape_dumb_stop();
806 		ftape_abort_operation(); /* it's annoying */
807 	} else {
808 		ftape_set_state(idle);
809 	}
810 	ftape_detach_drive();
811 	if (ft_history.used) {
812 		TRACE(ft_t_info, "== Non-fatal errors this run: ==");
813 		TRACE(ft_t_info, "fdc isr statistics:\n"
814 		      KERN_INFO " id_am_errors     : %3d\n"
815 		      KERN_INFO " id_crc_errors    : %3d\n"
816 		      KERN_INFO " data_am_errors   : %3d\n"
817 		      KERN_INFO " data_crc_errors  : %3d\n"
818 		      KERN_INFO " overrun_errors   : %3d\n"
819 		      KERN_INFO " no_data_errors   : %3d\n"
820 		      KERN_INFO " retries          : %3d",
821 		      ft_history.id_am_errors,   ft_history.id_crc_errors,
822 		      ft_history.data_am_errors, ft_history.data_crc_errors,
823 		      ft_history.overrun_errors, ft_history.no_data_errors,
824 		      ft_history.retries);
825 		if (ft_history.used & 1) {
826 			TRACE(ft_t_info, "ecc statistics:\n"
827 			      KERN_INFO " crc_errors       : %3d\n"
828 			      KERN_INFO " crc_failures     : %3d\n"
829 			      KERN_INFO " ecc_failures     : %3d\n"
830 			      KERN_INFO " sectors corrected: %3d",
831 			      ft_history.crc_errors,   ft_history.crc_failures,
832 			      ft_history.ecc_failures, ft_history.corrected);
833 		}
834 		if (ft_history.defects > 0) {
835 			TRACE(ft_t_warn, "Warning: %d media defects!",
836 			      ft_history.defects);
837 		}
838 		if (ft_history.rewinds > 0) {
839 			TRACE(ft_t_info, "tape motion statistics:\n"
840 			      KERN_INFO "repositions       : %3d",
841 			      ft_history.rewinds);
842 		}
843 	}
844 	ft_failure = 1;
845 	TRACE_EXIT;
846 }
847 
ftape_init_driver(void)848 static void ftape_init_driver(void)
849 {
850 	TRACE_FUN(ft_t_flow);
851 
852 	ft_drive_type.vendor_id = UNKNOWN_VENDOR;
853 	ft_drive_type.speed     = 0;
854 	ft_drive_type.wake_up   = unknown_wake_up;
855 	ft_drive_type.name      = "Unknown";
856 
857 	ftape_timeout.seek      = 650 * FT_SECOND;
858 	ftape_timeout.reset     = 670 * FT_SECOND;
859 	ftape_timeout.rewind    = 650 * FT_SECOND;
860 	ftape_timeout.head_seek =  15 * FT_SECOND;
861 	ftape_timeout.stop      =   5 * FT_SECOND;
862 	ftape_timeout.pause     =  16 * FT_SECOND;
863 
864 	ft_qic_std             = -1;
865 	ftape_tape_len         = 0;  /* unknown */
866 	ftape_current_command  = 0;
867 	ftape_current_cylinder = -1;
868 
869 	ft_segments_per_track       = 102;
870 	ftape_segments_per_head     = 1020;
871 	ftape_segments_per_cylinder = 4;
872 	ft_tracks_per_tape          = 20;
873 
874 	ft_failure = 1;
875 
876 	ft_formatted       = 0;
877 	ft_no_tape         = 1;
878 	ft_write_protected = 1;
879 	ft_new_tape        = 1;
880 
881 	ft_driver_state = idle;
882 
883 	ft_data_rate =
884 		ft_fdc_max_rate   = 500;
885 	ft_drive_max_rate = 0; /* triggers set_rate_test() */
886 
887 	ftape_init_drive_needed = 1;
888 
889 	ft_header_segment_1    = -1;
890 	ft_header_segment_2    = -1;
891 	ft_used_header_segment = -1;
892 	ft_first_data_segment  = -1;
893 	ft_last_data_segment   = -1;
894 
895 	ft_location.track = -1;
896 	ft_location.known = 0;
897 
898 	ftape_tape_running = 0;
899 	ftape_might_be_off_track = 1;
900 
901 	ftape_new_cartridge();	/* init some tape related variables */
902 	ftape_init_bsm();
903 	TRACE_EXIT;
904 }
905