📄 ftape-io.c
字号:
/* * Copyright (C) 1993-1996 Bas Laarhoven, * (C) 1996 Kai Harrekilde-Petersen, * (C) 1997 Claus-Justus Heine. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-io.c,v $ * $Revision: 1.4 $ * $Date: 1997/11/11 14:02:36 $ * * This file contains the general control functions for the * QIC-40/80/3010/3020 floppy-tape driver "ftape" for Linux. */#include <linux/errno.h>#include <linux/sched.h>#include <linux/mm.h>#include <asm/system.h>#include <linux/ioctl.h>#include <linux/mtio.h>#include <linux/delay.h>#include <linux/ftape.h>#include <linux/qic117.h>#include "../lowlevel/ftape-tracing.h"#include "../lowlevel/fdc-io.h"#include "../lowlevel/ftape-io.h"#include "../lowlevel/ftape-ctl.h"#include "../lowlevel/ftape-rw.h"#include "../lowlevel/ftape-write.h"#include "../lowlevel/ftape-read.h"#include "../lowlevel/ftape-init.h"#include "../lowlevel/ftape-calibr.h"/* Global vars. *//* NOTE: sectors start numbering at 1, all others at 0 ! */ft_timeout_table ftape_timeout;unsigned int ftape_tape_len;volatile qic117_cmd_t ftape_current_command;const struct qic117_command_table qic117_cmds[] = QIC117_COMMANDS;int ftape_might_be_off_track;/* Local vars. */static int diagnostic_mode;static unsigned int ftape_udelay_count;static unsigned int ftape_udelay_time;void ftape_udelay(unsigned int usecs){ volatile int count = (ftape_udelay_count * usecs + ftape_udelay_count - 1) / ftape_udelay_time; volatile int i; while (count-- > 0) { for (i = 0; i < 20; ++i); }}void ftape_udelay_calibrate(void){ ftape_calibrate("ftape_udelay", ftape_udelay, &ftape_udelay_count, &ftape_udelay_time);}/* Delay (msec) routine. */void ftape_sleep(unsigned int time){ TRACE_FUN(ft_t_any); time *= 1000; /* msecs -> usecs */ if (time < FT_USPT) { /* Time too small for scheduler, do a busy wait ! */ ftape_udelay(time); } else { long timeout; unsigned long flags; unsigned int ticks = (time + FT_USPT - 1) / FT_USPT; TRACE(ft_t_any, "%d msec, %d ticks", time/1000, ticks); timeout = ticks; save_flags(flags); sti(); msleep_interruptible(jiffies_to_msecs(timeout)); /* Mmm. Isn't current->blocked == 0xffffffff ? */ if (signal_pending(current)) { TRACE(ft_t_err, "awoken by non-blocked signal :-("); } restore_flags(flags); } TRACE_EXIT;}/* send a command or parameter to the drive * Generates # of step pulses. */static inline int ft_send_to_drive(int arg){ /* Always wait for a command_timeout period to separate * individuals commands and/or parameters. */ ftape_sleep(3 * FT_MILLISECOND); /* Keep cylinder nr within range, step towards home if possible. */ if (ftape_current_cylinder >= arg) { return fdc_seek(ftape_current_cylinder - arg); } else { return fdc_seek(ftape_current_cylinder + arg); }}/* forward */ int ftape_report_raw_drive_status(int *status);static int ft_check_cmd_restrictions(qic117_cmd_t command){ int status = -1; TRACE_FUN(ft_t_any); TRACE(ft_t_flow, "%s", qic117_cmds[command].name); /* A new motion command during an uninterruptible (motion) * command requires a ready status before the new command can * be issued. Otherwise a new motion command needs to be * checked against required status. */ if (qic117_cmds[command].cmd_type == motion && qic117_cmds[ftape_current_command].non_intr) { ftape_report_raw_drive_status(&status); if ((status & QIC_STATUS_READY) == 0) { TRACE(ft_t_noise, "motion cmd (%d) during non-intr cmd (%d)", command, ftape_current_command); TRACE(ft_t_noise, "waiting until drive gets ready"); ftape_ready_wait(ftape_timeout.seek, &status); } } if (qic117_cmds[command].mask != 0) { __u8 difference; /* Some commands do require a certain status: */ if (status == -1) { /* not yet set */ ftape_report_raw_drive_status(&status); } difference = ((status ^ qic117_cmds[command].state) & qic117_cmds[command].mask); /* Wait until the drive gets * ready. This may last forever if * the drive never gets ready... */ while ((difference & QIC_STATUS_READY) != 0) { TRACE(ft_t_noise, "command %d issued while not ready", command); TRACE(ft_t_noise, "waiting until drive gets ready"); if (ftape_ready_wait(ftape_timeout.seek, &status) == -EINTR) { /* Bail out on signal ! */ TRACE_ABORT(-EINTR, ft_t_warn, "interrupted by non-blockable signal"); } difference = ((status ^ qic117_cmds[command].state) & qic117_cmds[command].mask); } while ((difference & QIC_STATUS_ERROR) != 0) { int err; qic117_cmd_t cmd; TRACE(ft_t_noise, "command %d issued while error pending", command); TRACE(ft_t_noise, "clearing error status"); ftape_report_error(&err, &cmd, 1); ftape_report_raw_drive_status(&status); difference = ((status ^ qic117_cmds[command].state) & qic117_cmds[command].mask); if ((difference & QIC_STATUS_ERROR) != 0) { /* Bail out on fatal signal ! */ FT_SIGNAL_EXIT(_NEVER_BLOCK); } } if (difference) { /* Any remaining difference can't be solved * here. */ if (difference & (QIC_STATUS_CARTRIDGE_PRESENT | QIC_STATUS_NEW_CARTRIDGE | QIC_STATUS_REFERENCED)) { TRACE(ft_t_warn, "Fatal: tape removed or reinserted !"); ft_failure = 1; } else { TRACE(ft_t_err, "wrong state: 0x%02x should be: 0x%02x", status & qic117_cmds[command].mask, qic117_cmds[command].state); } TRACE_EXIT -EIO; } if (~status & QIC_STATUS_READY & qic117_cmds[command].mask) { TRACE_ABORT(-EBUSY, ft_t_err, "Bad: still busy!"); } } TRACE_EXIT 0;}/* Issue a tape command: */int ftape_command(qic117_cmd_t command){ int result = 0; static int level; TRACE_FUN(ft_t_any); if ((unsigned int)command > NR_ITEMS(qic117_cmds)) { /* This is a bug we'll want to know about too. */ TRACE_ABORT(-EIO, ft_t_bug, "bug - bad command: %d", command); } if (++level > 5) { /* This is a bug we'll want to know about. */ --level; TRACE_ABORT(-EIO, ft_t_bug, "bug - recursion for command: %d", command); } /* disable logging and restriction check for some commands, * check all other commands that have a prescribed starting * status. */ if (diagnostic_mode) { TRACE(ft_t_flow, "diagnostic command %d", command); } else if (command == QIC_REPORT_DRIVE_STATUS || command == QIC_REPORT_NEXT_BIT) { TRACE(ft_t_any, "%s", qic117_cmds[command].name); } else { TRACE_CATCH(ft_check_cmd_restrictions(command), --level); } /* Now all conditions are met or result was < 0. */ result = ft_send_to_drive((unsigned int)command); if (qic117_cmds[command].cmd_type == motion && command != QIC_LOGICAL_FORWARD && command != QIC_STOP_TAPE) { ft_location.known = 0; } ftape_current_command = command; --level; TRACE_EXIT result;}/* Send a tape command parameter: * Generates command # of step pulses. * Skips tape-status call ! */int ftape_parameter(unsigned int parameter){ TRACE_FUN(ft_t_any); TRACE(ft_t_flow, "called with parameter = %d", parameter); TRACE_EXIT ft_send_to_drive(parameter + 2);}/* Wait for the drive to get ready. * timeout time in milli-seconds * Returned status is valid if result != -EIO * * Should we allow to be killed by SIGINT? (^C) * Would be nice at least for large timeouts. */int ftape_ready_wait(unsigned int timeout, int *status){ unsigned long t0; unsigned int poll_delay; int signal_retries; TRACE_FUN(ft_t_any); /* the following ** REALLY ** reduces the system load when * e.g. one simply rewinds or retensions. The tape is slow * anyway. It is really not necessary to detect error * conditions with 1/10 seconds granularity * * On my AMD 133MHZ 486: 100 ms: 23% system load * 1 sec: 5% * 5 sec: 0.6%, yeah */ if (timeout <= FT_SECOND) { poll_delay = 100 * FT_MILLISECOND; signal_retries = 20; /* two seconds */ } else if (timeout < 20 * FT_SECOND) { TRACE(ft_t_flow, "setting poll delay to 1 second"); poll_delay = FT_SECOND; signal_retries = 2; /* two seconds */ } else { TRACE(ft_t_flow, "setting poll delay to 5 seconds"); poll_delay = 5 * FT_SECOND; signal_retries = 1; /* five seconds */ } for (;;) { t0 = jiffies; TRACE_CATCH(ftape_report_raw_drive_status(status),); if (*status & QIC_STATUS_READY) { TRACE_EXIT 0; } if (!signal_retries--) { FT_SIGNAL_EXIT(_NEVER_BLOCK); } if ((int)timeout >= 0) { /* this will fail when jiffies wraps around about * once every year :-) */ timeout -= ((jiffies - t0) * FT_SECOND) / HZ; if (timeout <= 0) { TRACE_ABORT(-ETIME, ft_t_err, "timeout"); } ftape_sleep(poll_delay); timeout -= poll_delay; } else { ftape_sleep(poll_delay); } } TRACE_EXIT -ETIME;}/* Issue command and wait up to timeout milli seconds for drive ready */int ftape_command_wait(qic117_cmd_t command, unsigned int timeout, int *status){ int result; /* Drive should be ready, issue command */ result = ftape_command(command); if (result >= 0) { result = ftape_ready_wait(timeout, status); } return result;}static int ftape_parameter_wait(unsigned int parm, unsigned int timeout, int *status){ int result; /* Drive should be ready, issue command */ result = ftape_parameter(parm); if (result >= 0) { result = ftape_ready_wait(timeout, status); } return result;}/*-------------------------------------------------------------------------- * Report operations *//* Query the drive about its status. The command is sent and result_length bits of status are returned (2 extra bits are read for start and stop). */int ftape_report_operation(int *status, qic117_cmd_t command, int result_length){ int i, st3; unsigned int t0; unsigned int dt; TRACE_FUN(ft_t_any); TRACE_CATCH(ftape_command(command),); t0 = ftape_timestamp(); i = 0; do { ++i; ftape_sleep(3 * FT_MILLISECOND); /* see remark below */ TRACE_CATCH(fdc_sense_drive_status(&st3),); dt = ftape_timediff(t0, ftape_timestamp()); /* Ack should be asserted within Ttimout + Tack = 6 msec. * Looks like some drives fail to do this so extend this * period to 300 msec. */ } while (!(st3 & ST3_TRACK_0) && dt < 300000); if (!(st3 & ST3_TRACK_0)) { TRACE(ft_t_err, "No acknowledge after %u msec. (%i iter)", dt / 1000, i); TRACE_ABORT(-EIO, ft_t_err, "timeout on Acknowledge"); } /* dt may be larger than expected because of other tasks * scheduled while we were sleeping. */ if (i > 1 && dt > 6000) { TRACE(ft_t_err, "Acknowledge after %u msec. (%i iter)", dt / 1000, i); } *status = 0; for (i = 0; i < result_length + 1; i++) { TRACE_CATCH(ftape_command(QIC_REPORT_NEXT_BIT),); TRACE_CATCH(fdc_sense_drive_status(&st3),); if (i < result_length) { *status |= ((st3 & ST3_TRACK_0) ? 1 : 0) << i; } else if ((st3 & ST3_TRACK_0) == 0) { TRACE_ABORT(-EIO, ft_t_err, "missing status stop bit"); } } /* this command will put track zero and index back into normal state */ (void)ftape_command(QIC_REPORT_NEXT_BIT); TRACE_EXIT 0;}/* Report the current drive status. */int ftape_report_raw_drive_status(int *status){ int result; int count = 0; TRACE_FUN(ft_t_any); do { result = ftape_report_operation(status, QIC_REPORT_DRIVE_STATUS, 8); } while (result < 0 && ++count <= 3); if (result < 0) { TRACE_ABORT(-EIO, ft_t_err, "report_operation failed after %d trials", count); } if ((*status & 0xff) == 0xff) { TRACE_ABORT(-EIO, ft_t_err, "impossible drive status 0xff"); } if (*status & QIC_STATUS_READY) { ftape_current_command = QIC_NO_COMMAND; /* completed */ } ft_last_status.status.drive_status = (__u8)(*status & 0xff); TRACE_EXIT 0;}int ftape_report_drive_status(int *status){ TRACE_FUN(ft_t_any); TRACE_CATCH(ftape_report_raw_drive_status(status),); if (*status & QIC_STATUS_NEW_CARTRIDGE || !(*status & QIC_STATUS_CARTRIDGE_PRESENT)) { ft_failure = 1; /* will inhibit further operations */ TRACE_EXIT -EIO; } if (*status & QIC_STATUS_READY && *status & QIC_STATUS_ERROR) { /* Let caller handle all errors */ TRACE_ABORT(1, ft_t_warn, "warning: error status set!"); } TRACE_EXIT 0;}int ftape_report_error(unsigned int *error, qic117_cmd_t *command, int report){ static const ftape_error ftape_errors[] = QIC117_ERRORS; int code; TRACE_FUN(ft_t_any); TRACE_CATCH(ftape_report_operation(&code, QIC_REPORT_ERROR_CODE, 16),); *error = (unsigned int)(code & 0xff); *command = (qic117_cmd_t)((code>>8)&0xff); /* remember hardware status, maybe useful for status ioctls */ ft_last_error.error.command = (__u8)*command; ft_last_error.error.error = (__u8)*error; if (!report) { TRACE_EXIT 0; } if (*error == 0) { TRACE_ABORT(0, ft_t_info, "No error"); } TRACE(ft_t_info, "errorcode: %d", *error); if (*error < NR_ITEMS(ftape_errors)) { TRACE(ft_t_noise, "%sFatal ERROR:", (ftape_errors[*error].fatal ? "" : "Non-")); TRACE(ft_t_noise, "%s ...", ftape_errors[*error].message); } else { TRACE(ft_t_noise, "Unknown ERROR !"); } if ((unsigned int)*command < NR_ITEMS(qic117_cmds) && qic117_cmds[*command].name != NULL) { TRACE(ft_t_noise, "... caused by command \'%s\'",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -