1 /*
2 * Copyright (C) 1993-1996 Bas Laarhoven,
3 * (C) 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-read.c,v $
21 * $Revision: 1.6 $
22 * $Date: 1997/10/21 14:39:22 $
23 *
24 * This file contains the reading code
25 * for the QIC-117 floppy-tape driver for Linux.
26 *
27 */
28
29 #include <linux/string.h>
30 #include <linux/errno.h>
31 #include <linux/mm.h>
32 #include <asm/segment.h>
33
34 #include <linux/ftape.h>
35 #include <linux/qic117.h>
36 #include "../lowlevel/ftape-tracing.h"
37 #include "../lowlevel/ftape-read.h"
38 #include "../lowlevel/ftape-io.h"
39 #include "../lowlevel/ftape-ctl.h"
40 #include "../lowlevel/ftape-rw.h"
41 #include "../lowlevel/ftape-write.h"
42 #include "../lowlevel/ftape-ecc.h"
43 #include "../lowlevel/ftape-bsm.h"
44
45 /* Global vars.
46 */
47
48 /* Local vars.
49 */
50
ftape_zap_read_buffers(void)51 void ftape_zap_read_buffers(void)
52 {
53 int i;
54
55 for (i = 0; i < ft_nr_buffers; ++i) {
56 /* changed to "fit" with dynamic allocation of tape_buffer. --khp */
57 ft_buffer[i]->status = waiting;
58 ft_buffer[i]->bytes = 0;
59 ft_buffer[i]->skip = 0;
60 ft_buffer[i]->retry = 0;
61 }
62 /* ftape_reset_buffer(); */
63 }
64
convert_sector_map(buffer_struct * buff)65 static SectorMap convert_sector_map(buffer_struct * buff)
66 {
67 int i = 0;
68 SectorMap bad_map = ftape_get_bad_sector_entry(buff->segment_id);
69 SectorMap src_map = buff->soft_error_map | buff->hard_error_map;
70 SectorMap dst_map = 0;
71 TRACE_FUN(ft_t_any);
72
73 if (bad_map || src_map) {
74 TRACE(ft_t_flow, "bad_map = 0x%08lx", (long) bad_map);
75 TRACE(ft_t_flow, "src_map = 0x%08lx", (long) src_map);
76 }
77 while (bad_map) {
78 while ((bad_map & 1) == 0) {
79 if (src_map & 1) {
80 dst_map |= (1 << i);
81 }
82 src_map >>= 1;
83 bad_map >>= 1;
84 ++i;
85 }
86 /* (bad_map & 1) == 1 */
87 src_map >>= 1;
88 bad_map >>= 1;
89 }
90 if (src_map) {
91 dst_map |= (src_map << i);
92 }
93 if (dst_map) {
94 TRACE(ft_t_flow, "dst_map = 0x%08lx", (long) dst_map);
95 }
96 TRACE_EXIT dst_map;
97 }
98
correct_and_copy_fraction(buffer_struct * buff,__u8 * destination,int start,int size)99 static int correct_and_copy_fraction(buffer_struct *buff, __u8 * destination,
100 int start, int size)
101 {
102 struct memory_segment mseg;
103 int result;
104 SectorMap read_bad;
105 TRACE_FUN(ft_t_any);
106
107 mseg.read_bad = convert_sector_map(buff);
108 mseg.marked_bad = 0; /* not used... */
109 mseg.blocks = buff->bytes / FT_SECTOR_SIZE;
110 mseg.data = buff->address;
111 /* If there are no data sectors we can skip this segment.
112 */
113 if (mseg.blocks <= 3) {
114 TRACE_ABORT(0, ft_t_noise, "empty segment");
115 }
116 read_bad = mseg.read_bad;
117 ft_history.crc_errors += count_ones(read_bad);
118 result = ftape_ecc_correct_data(&mseg);
119 if (read_bad != 0 || mseg.corrected != 0) {
120 TRACE(ft_t_noise, "crc error map: 0x%08lx", (unsigned long)read_bad);
121 TRACE(ft_t_noise, "corrected map: 0x%08lx", (unsigned long)mseg.corrected);
122 ft_history.corrected += count_ones(mseg.corrected);
123 }
124 if (result == ECC_CORRECTED || result == ECC_OK) {
125 if (result == ECC_CORRECTED) {
126 TRACE(ft_t_info, "ecc corrected segment: %d", buff->segment_id);
127 }
128 if(start < 0) {
129 start= 0;
130 }
131 if((start+size) > ((mseg.blocks - 3) * FT_SECTOR_SIZE)) {
132 size = (mseg.blocks - 3) * FT_SECTOR_SIZE - start;
133 }
134 if (size < 0) {
135 size= 0;
136 }
137 if(size > 0) {
138 memcpy(destination + start, mseg.data + start, size);
139 }
140 if ((read_bad ^ mseg.corrected) & mseg.corrected) {
141 /* sectors corrected without crc errors set */
142 ft_history.crc_failures++;
143 }
144 TRACE_EXIT size; /* (mseg.blocks - 3) * FT_SECTOR_SIZE; */
145 } else {
146 ft_history.ecc_failures++;
147 TRACE_ABORT(-EAGAIN,
148 ft_t_err, "ecc failure on segment %d",
149 buff->segment_id);
150 }
151 TRACE_EXIT 0;
152 }
153
154 /* Read given segment into buffer at address.
155 */
ftape_read_segment_fraction(const int segment_id,void * address,const ft_read_mode_t read_mode,const int start,const int size)156 int ftape_read_segment_fraction(const int segment_id,
157 void *address,
158 const ft_read_mode_t read_mode,
159 const int start,
160 const int size)
161 {
162 int result = 0;
163 int retry = 0;
164 int bytes_read = 0;
165 int read_done = 0;
166 TRACE_FUN(ft_t_flow);
167
168 ft_history.used |= 1;
169 TRACE(ft_t_data_flow, "segment_id = %d", segment_id);
170 if (ft_driver_state != reading) {
171 TRACE(ft_t_noise, "calling ftape_abort_operation");
172 TRACE_CATCH(ftape_abort_operation(),);
173 ftape_set_state(reading);
174 }
175 for(;;) {
176 buffer_struct *tail;
177 /* Allow escape from this loop on signal !
178 */
179 FT_SIGNAL_EXIT(_DONT_BLOCK);
180 /* Search all full buffers for the first matching the
181 * wanted segment. Clear other buffers on the fly.
182 */
183 tail = ftape_get_buffer(ft_queue_tail);
184 while (!read_done && tail->status == done) {
185 /* Allow escape from this loop on signal !
186 */
187 FT_SIGNAL_EXIT(_DONT_BLOCK);
188 if (tail->segment_id == segment_id) {
189 /* If out buffer is already full,
190 * return its contents.
191 */
192 TRACE(ft_t_flow, "found segment in cache: %d",
193 segment_id);
194 if (tail->deleted) {
195 /* Return a value that
196 * read_header_segment
197 * understands. As this
198 * should only occur when
199 * searching for the header
200 * segments it shouldn't be
201 * misinterpreted elsewhere.
202 */
203 TRACE_EXIT 0;
204 }
205 result = correct_and_copy_fraction(
206 tail,
207 address,
208 start,
209 size);
210 TRACE(ft_t_flow, "segment contains (bytes): %d",
211 result);
212 if (result < 0) {
213 if (result != -EAGAIN) {
214 TRACE_EXIT result;
215 }
216 /* keep read_done == 0, will
217 * trigger
218 * ftape_abort_operation
219 * because reading wrong
220 * segment.
221 */
222 TRACE(ft_t_err, "ecc failed, retry");
223 ++retry;
224 } else {
225 read_done = 1;
226 bytes_read = result;
227 }
228 } else {
229 TRACE(ft_t_flow,"zapping segment in cache: %d",
230 tail->segment_id);
231 }
232 tail->status = waiting;
233 tail = ftape_next_buffer(ft_queue_tail);
234 }
235 if (!read_done && tail->status == reading) {
236 if (tail->segment_id == segment_id) {
237 switch(ftape_wait_segment(reading)) {
238 case 0:
239 break;
240 case -EINTR:
241 TRACE_ABORT(-EINTR, ft_t_warn,
242 "interrupted by "
243 "non-blockable signal");
244 break;
245 default:
246 TRACE(ft_t_noise,
247 "wait_segment failed");
248 ftape_abort_operation();
249 ftape_set_state(reading);
250 break;
251 }
252 } else {
253 /* We're reading the wrong segment,
254 * stop runner.
255 */
256 TRACE(ft_t_noise, "reading wrong segment");
257 ftape_abort_operation();
258 ftape_set_state(reading);
259 }
260 }
261 /* should runner stop ?
262 */
263 if (ft_runner_status == aborting) {
264 buffer_struct *head = ftape_get_buffer(ft_queue_head);
265 switch(head->status) {
266 case error:
267 ft_history.defects +=
268 count_ones(head->hard_error_map);
269 case reading:
270 head->status = waiting;
271 break;
272 default:
273 break;
274 }
275 TRACE_CATCH(ftape_dumb_stop(),);
276 } else {
277 /* If just passed last segment on tape: wait
278 * for BOT or EOT mark. Sets ft_runner_status to
279 * idle if at lEOT and successful
280 */
281 TRACE_CATCH(ftape_handle_logical_eot(),);
282 }
283 /* If we got a segment: quit, or else retry up to limit.
284 *
285 * If segment to read is empty, do not start runner for it,
286 * but wait for next read call.
287 */
288 if (read_done ||
289 ftape_get_bad_sector_entry(segment_id) == EMPTY_SEGMENT ) {
290 /* bytes_read = 0; should still be zero */
291 TRACE_EXIT bytes_read;
292
293 }
294 if (retry > FT_RETRIES_ON_ECC_ERROR) {
295 ft_history.defects++;
296 TRACE_ABORT(-ENODATA, ft_t_err,
297 "too many retries on ecc failure");
298 }
299 /* Now at least one buffer is empty !
300 * Restart runner & tape if needed.
301 */
302 TRACE(ft_t_any, "head: %d, tail: %d, ft_runner_status: %d",
303 ftape_buffer_id(ft_queue_head),
304 ftape_buffer_id(ft_queue_tail),
305 ft_runner_status);
306 TRACE(ft_t_any, "buffer[].status, [head]: %d, [tail]: %d",
307 ftape_get_buffer(ft_queue_head)->status,
308 ftape_get_buffer(ft_queue_tail)->status);
309 tail = ftape_get_buffer(ft_queue_tail);
310 if (tail->status == waiting) {
311 buffer_struct *head = ftape_get_buffer(ft_queue_head);
312
313 ftape_setup_new_segment(head, segment_id, -1);
314 if (read_mode == FT_RD_SINGLE) {
315 /* disable read-ahead */
316 head->next_segment = 0;
317 }
318 ftape_calc_next_cluster(head);
319 if (ft_runner_status == idle) {
320 result = ftape_start_tape(segment_id,
321 head->sector_offset);
322 if (result < 0) {
323 TRACE_ABORT(result, ft_t_err, "Error: "
324 "segment %d unreachable",
325 segment_id);
326 }
327 }
328 head->status = reading;
329 fdc_setup_read_write(head, FDC_READ);
330 }
331 }
332 /* not reached */
333 TRACE_EXIT -EIO;
334 }
335
ftape_read_header_segment(__u8 * address)336 int ftape_read_header_segment(__u8 *address)
337 {
338 int result;
339 int header_segment;
340 int first_failed = 0;
341 int status;
342 TRACE_FUN(ft_t_flow);
343
344 ft_used_header_segment = -1;
345 TRACE_CATCH(ftape_report_drive_status(&status),);
346 TRACE(ft_t_flow, "reading...");
347 /* We're looking for the first header segment.
348 * A header segment cannot contain bad sectors, therefor at the
349 * tape start, segments with bad sectors are (according to QIC-40/80)
350 * written with deleted data marks and must be skipped.
351 */
352 memset(address, '\0', (FT_SECTORS_PER_SEGMENT - 3) * FT_SECTOR_SIZE);
353 result = 0;
354 #define HEADER_SEGMENT_BOUNDARY 68 /* why not 42? */
355 for (header_segment = 0;
356 header_segment < HEADER_SEGMENT_BOUNDARY && result == 0;
357 ++header_segment) {
358 /* Set no read-ahead, the isr will force read-ahead whenever
359 * it encounters deleted data !
360 */
361 result = ftape_read_segment(header_segment,
362 address,
363 FT_RD_SINGLE);
364 if (result < 0 && !first_failed) {
365 TRACE(ft_t_err, "header segment damaged, trying backup");
366 first_failed = 1;
367 result = 0; /* force read of next (backup) segment */
368 }
369 }
370 if (result < 0 || header_segment >= HEADER_SEGMENT_BOUNDARY) {
371 TRACE_ABORT(-EIO, ft_t_err,
372 "no readable header segment found");
373 }
374 TRACE_CATCH(ftape_abort_operation(),);
375 ft_used_header_segment = header_segment;
376 result = ftape_decode_header_segment(address);
377 TRACE_EXIT result;
378 }
379
ftape_decode_header_segment(__u8 * address)380 int ftape_decode_header_segment(__u8 *address)
381 {
382 unsigned int max_floppy_side;
383 unsigned int max_floppy_track;
384 unsigned int max_floppy_sector;
385 unsigned int new_tape_len;
386 TRACE_FUN(ft_t_flow);
387
388 if (GET4(address, FT_SIGNATURE) == FT_D2G_MAGIC) {
389 /* Ditto 2GB header segment. They encrypt the bad sector map.
390 * We decrypt it and store them in normal format.
391 * I hope this is correct.
392 */
393 int i;
394 TRACE(ft_t_warn,
395 "Found Ditto 2GB tape, "
396 "trying to decrypt bad sector map");
397 for (i=256; i < 29 * FT_SECTOR_SIZE; i++) {
398 address[i] = ~(address[i] - (i&0xff));
399 }
400 PUT4(address, 0,FT_HSEG_MAGIC);
401 } else if (GET4(address, FT_SIGNATURE) != FT_HSEG_MAGIC) {
402 TRACE_ABORT(-EIO, ft_t_err,
403 "wrong signature in header segment");
404 }
405 ft_format_code = (ft_format_type) address[FT_FMT_CODE];
406 if (ft_format_code != fmt_big) {
407 ft_header_segment_1 = GET2(address, FT_HSEG_1);
408 ft_header_segment_2 = GET2(address, FT_HSEG_2);
409 ft_first_data_segment = GET2(address, FT_FRST_SEG);
410 ft_last_data_segment = GET2(address, FT_LAST_SEG);
411 } else {
412 ft_header_segment_1 = GET4(address, FT_6_HSEG_1);
413 ft_header_segment_2 = GET4(address, FT_6_HSEG_2);
414 ft_first_data_segment = GET4(address, FT_6_FRST_SEG);
415 ft_last_data_segment = GET4(address, FT_6_LAST_SEG);
416 }
417 TRACE(ft_t_noise, "first data segment: %d", ft_first_data_segment);
418 TRACE(ft_t_noise, "last data segment: %d", ft_last_data_segment);
419 TRACE(ft_t_noise, "header segments are %d and %d",
420 ft_header_segment_1, ft_header_segment_2);
421
422 /* Verify tape parameters...
423 * QIC-40/80 spec: tape_parameters:
424 *
425 * segments-per-track segments_per_track
426 * tracks-per-cartridge tracks_per_tape
427 * max-floppy-side (segments_per_track *
428 * tracks_per_tape - 1) /
429 * ftape_segments_per_head
430 * max-floppy-track ftape_segments_per_head /
431 * ftape_segments_per_cylinder - 1
432 * max-floppy-sector ftape_segments_per_cylinder *
433 * FT_SECTORS_PER_SEGMENT
434 */
435 ft_segments_per_track = GET2(address, FT_SPT);
436 ft_tracks_per_tape = address[FT_TPC];
437 max_floppy_side = address[FT_FHM];
438 max_floppy_track = address[FT_FTM];
439 max_floppy_sector = address[FT_FSM];
440 TRACE(ft_t_noise, "(fmt/spt/tpc/fhm/ftm/fsm) = %d/%d/%d/%d/%d/%d",
441 ft_format_code, ft_segments_per_track, ft_tracks_per_tape,
442 max_floppy_side, max_floppy_track, max_floppy_sector);
443 new_tape_len = ftape_tape_len;
444 switch (ft_format_code) {
445 case fmt_425ft:
446 new_tape_len = 425;
447 break;
448 case fmt_normal:
449 if (ftape_tape_len == 0) { /* otherwise 307 ft */
450 new_tape_len = 205;
451 }
452 break;
453 case fmt_1100ft:
454 new_tape_len = 1100;
455 break;
456 case fmt_var:{
457 int segments_per_1000_inch = 1; /* non-zero default for switch */
458 switch (ft_qic_std) {
459 case QIC_TAPE_QIC40:
460 segments_per_1000_inch = 332;
461 break;
462 case QIC_TAPE_QIC80:
463 segments_per_1000_inch = 488;
464 break;
465 case QIC_TAPE_QIC3010:
466 segments_per_1000_inch = 730;
467 break;
468 case QIC_TAPE_QIC3020:
469 segments_per_1000_inch = 1430;
470 break;
471 }
472 new_tape_len = (1000 * ft_segments_per_track +
473 (segments_per_1000_inch - 1)) / segments_per_1000_inch;
474 break;
475 }
476 case fmt_big:{
477 int segments_per_1000_inch = 1; /* non-zero default for switch */
478 switch (ft_qic_std) {
479 case QIC_TAPE_QIC40:
480 segments_per_1000_inch = 332;
481 break;
482 case QIC_TAPE_QIC80:
483 segments_per_1000_inch = 488;
484 break;
485 case QIC_TAPE_QIC3010:
486 segments_per_1000_inch = 730;
487 break;
488 case QIC_TAPE_QIC3020:
489 segments_per_1000_inch = 1430;
490 break;
491 default:
492 TRACE_ABORT(-EIO, ft_t_bug,
493 "%x QIC-standard with fmt-code %d, please report",
494 ft_qic_std, ft_format_code);
495 }
496 new_tape_len = ((1000 * ft_segments_per_track +
497 (segments_per_1000_inch - 1)) /
498 segments_per_1000_inch);
499 break;
500 }
501 default:
502 TRACE_ABORT(-EIO, ft_t_err,
503 "unknown tape format, please report !");
504 }
505 if (new_tape_len != ftape_tape_len) {
506 ftape_tape_len = new_tape_len;
507 TRACE(ft_t_info, "calculated tape length is %d ft",
508 ftape_tape_len);
509 ftape_calc_timeouts(ft_qic_std, ft_data_rate, ftape_tape_len);
510 }
511 if (ft_segments_per_track == 0 && ft_tracks_per_tape == 0 &&
512 max_floppy_side == 0 && max_floppy_track == 0 &&
513 max_floppy_sector == 0) {
514 /* QIC-40 Rev E and earlier has no values in the header.
515 */
516 ft_segments_per_track = 68;
517 ft_tracks_per_tape = 20;
518 max_floppy_side = 1;
519 max_floppy_track = 169;
520 max_floppy_sector = 128;
521 }
522 /* This test will compensate for the wrong parameter on tapes
523 * formatted by Conner software.
524 */
525 if (ft_segments_per_track == 150 &&
526 ft_tracks_per_tape == 28 &&
527 max_floppy_side == 7 &&
528 max_floppy_track == 149 &&
529 max_floppy_sector == 128) {
530 TRACE(ft_t_info, "the famous CONNER bug: max_floppy_side off by one !");
531 max_floppy_side = 6;
532 }
533 /* These tests will compensate for the wrong parameter on tapes
534 * formatted by ComByte Windows software.
535 *
536 * First, for 205 foot tapes
537 */
538 if (ft_segments_per_track == 100 &&
539 ft_tracks_per_tape == 28 &&
540 max_floppy_side == 9 &&
541 max_floppy_track == 149 &&
542 max_floppy_sector == 128) {
543 TRACE(ft_t_info, "the ComByte bug: max_floppy_side incorrect!");
544 max_floppy_side = 4;
545 }
546 /* Next, for 307 foot tapes. */
547 if (ft_segments_per_track == 150 &&
548 ft_tracks_per_tape == 28 &&
549 max_floppy_side == 9 &&
550 max_floppy_track == 149 &&
551 max_floppy_sector == 128) {
552 TRACE(ft_t_info, "the ComByte bug: max_floppy_side incorrect!");
553 max_floppy_side = 6;
554 }
555 /* This test will compensate for the wrong parameter on tapes
556 * formatted by Colorado Windows software.
557 */
558 if (ft_segments_per_track == 150 &&
559 ft_tracks_per_tape == 28 &&
560 max_floppy_side == 6 &&
561 max_floppy_track == 150 &&
562 max_floppy_sector == 128) {
563 TRACE(ft_t_info, "the famous Colorado bug: max_floppy_track off by one !");
564 max_floppy_track = 149;
565 }
566 ftape_segments_per_head = ((max_floppy_sector/FT_SECTORS_PER_SEGMENT) *
567 (max_floppy_track + 1));
568 /* This test will compensate for some bug reported by Dima
569 * Brodsky. Seems to be a Colorado bug, either. (freebee
570 * Imation tape shipped together with Colorado T3000
571 */
572 if ((ft_format_code == fmt_var || ft_format_code == fmt_big) &&
573 ft_tracks_per_tape == 50 &&
574 max_floppy_side == 54 &&
575 max_floppy_track == 255 &&
576 max_floppy_sector == 128) {
577 TRACE(ft_t_info, "the famous ??? bug: max_floppy_track off by one !");
578 max_floppy_track = 254;
579 }
580 /*
581 * Verify drive_configuration with tape parameters
582 */
583 if (ftape_segments_per_head == 0 || ftape_segments_per_cylinder == 0 ||
584 ((ft_segments_per_track * ft_tracks_per_tape - 1) / ftape_segments_per_head
585 != max_floppy_side) ||
586 (ftape_segments_per_head / ftape_segments_per_cylinder - 1 != max_floppy_track) ||
587 (ftape_segments_per_cylinder * FT_SECTORS_PER_SEGMENT != max_floppy_sector)
588 #ifdef TESTING
589 || ((ft_format_code == fmt_var || ft_format_code == fmt_big) &&
590 (max_floppy_track != 254 || max_floppy_sector != 128))
591 #endif
592 ) {
593 TRACE(ft_t_err,"Tape parameters inconsistency, please report");
594 TRACE(ft_t_err, "reported = %d/%d/%d/%d/%d/%d",
595 ft_format_code,
596 ft_segments_per_track,
597 ft_tracks_per_tape,
598 max_floppy_side,
599 max_floppy_track,
600 max_floppy_sector);
601 TRACE(ft_t_err, "required = %d/%d/%d/%d/%d/%d",
602 ft_format_code,
603 ft_segments_per_track,
604 ft_tracks_per_tape,
605 ((ft_segments_per_track * ft_tracks_per_tape -1) /
606 ftape_segments_per_head ),
607 (ftape_segments_per_head /
608 ftape_segments_per_cylinder - 1 ),
609 (ftape_segments_per_cylinder * FT_SECTORS_PER_SEGMENT));
610 TRACE_EXIT -EIO;
611 }
612 ftape_extract_bad_sector_map(address);
613 TRACE_EXIT 0;
614 }
615