⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osst.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*  SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying  file README.st for more information.  History:  OnStream SCSI Tape support (osst) cloned from st.c by  Willem Riede (osst@riede.org) Feb 2000  Fixes ... Kurt Garloff <garloff@suse.de> Mar 2000  Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.  Contribution and ideas from several people including (in alphabetical  order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,  Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.  Copyright 1992 - 2000 Kai Makisara		 email Kai.Makisara@metla.fi  $Header: /home/cvsroot/Driver/osst.c,v 1.51 2000/12/22 20:48:27 garloff Exp $  Microscopic alterations - Rik Ling, 2000/12/21  Last modified: Wed Feb  2 22:04:05 2000 by makisara@kai.makisara.local  Some small formal changes - aeb, 950809*/static const char * cvsid = "$Id: osst.c,v 1.51 2000/12/22 20:48:27 garloff Exp $";const char * osst_version = "0.9.4.3";/* The "failure to reconnect" firmware bug */#define OSST_FW_NEED_POLL_MIN 10602 /*(107A)*/#define OSST_FW_NEED_POLL_MAX 10708 /*(108D)*/#define OSST_FW_NEED_POLL(x,d) ((x) >= OSST_FW_NEED_POLL_MIN && (x) <= OSST_FW_NEED_POLL_MAX && d->host->this_id != 7)#include <linux/config.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/init.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/mtio.h>#include <linux/ioctl.h>#include <linux/fcntl.h>#include <linux/spinlock.h>#include <linux/vmalloc.h>#include <linux/version.h>#include <asm/uaccess.h>#include <asm/dma.h>#include <asm/system.h>/* The driver prints some debugging information on the console if DEBUG   is defined and non-zero. */#define DEBUG 0/* The message level for the debug messages is currently set to KERN_NOTICE   so that people can easily see the messages. Later when the debugging messages   in the drivers are more widely classified, this may be changed to KERN_DEBUG. */#define OSST_DEB_MSG  KERN_NOTICE#define MAJOR_NR OSST_MAJOR#include <linux/blk.h>#include "scsi.h"#include "hosts.h"#include <scsi/scsi_ioctl.h>#define ST_KILOBYTE 1024#include "st.h"#include "osst.h"#include "osst_options.h"#include "osst_detect.h"#include "constants.h"static int buffer_kbs = 0;static int write_threshold_kbs = 0;static int max_buffers = 0;static int max_sg_segs = 0;#ifdef MODULEMODULE_AUTHOR("Willem Riede");MODULE_DESCRIPTION("OnStream SCSI Tape Driver");MODULE_PARM(buffer_kbs, "i");MODULE_PARM(write_threshold_kbs, "i");MODULE_PARM(max_buffers, "i");MODULE_PARM(max_sg_segs, "i");#elsestatic struct osst_dev_parm {       char   *name;       int    *val;} parms[] __initdata = {       { "buffer_kbs",          &buffer_kbs          },       { "write_threshold_kbs", &write_threshold_kbs },       { "max_buffers",         &max_buffers         },       { "max_sg_segs",         &max_sg_segs         }       };#endif/* Some default definitions have been moved to osst_options.h */#define OSST_BUFFER_SIZE (OSST_BUFFER_BLOCKS * ST_KILOBYTE)#define OSST_WRITE_THRESHOLD (OSST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)/* The buffer size should fit into the 24 bits for length in the   6-byte SCSI read and write commands. */#if OSST_BUFFER_SIZE >= (2 << 24 - 1)#error "Buffer size should not exceed (2 << 24 - 1) bytes!"#endif#if DEBUGstatic int debugging = 1;#endif#define MAX_RETRIES 0#define MAX_WRITE_RETRIES 0#define MAX_READY_RETRIES 5#define NO_TAPE  NOT_READY#define OSST_TIMEOUT (200 * HZ)#define OSST_LONG_TIMEOUT (1800 * HZ)#define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))#define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)/* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower   24 bits) */#define SET_DENS_AND_BLK 0x10001static int osst_nbr_buffers;static int osst_buffer_size       = OSST_BUFFER_SIZE;static int osst_write_threshold   = OSST_WRITE_THRESHOLD;static int osst_max_buffers       = OSST_MAX_BUFFERS;static int osst_max_sg_segs       = OSST_MAX_SG;static OS_Scsi_Tape **os_scsi_tapes = NULL;static OSST_buffer  **osst_buffers  = NULL;static int modes_defined = FALSE;static OSST_buffer *new_tape_buffer(int, int);static int enlarge_buffer(OSST_buffer *, int, int);static void normalize_buffer(OSST_buffer *);static int append_to_buffer(const char *, OSST_buffer *, int);static int from_buffer(OSST_buffer *, char *, int);static int osst_zero_buffer_tail(OSST_buffer *);static int osst_copy_to_buffer(OSST_buffer *, unsigned char *);static int osst_copy_from_buffer(OSST_buffer *, unsigned char *);static int osst_init(void);static int osst_attach(Scsi_Device *);static int osst_detect(Scsi_Device *);static void osst_detach(Scsi_Device *);struct Scsi_Device_Template osst_template ={       name:		"OnStream tape",       tag:		"osst",       scsi_type:	TYPE_TAPE,       major:		OSST_MAJOR,       detect:		osst_detect,       init:		osst_init,       attach:		osst_attach,       detach:		osst_detach};static int osst_int_ioctl(OS_Scsi_Tape *STp, Scsi_Request ** aSRpnt, unsigned int cmd_in,unsigned long arg);static int osst_set_frame_position(OS_Scsi_Tape *STp, Scsi_Request ** aSRpnt, int frame, int skip);static int osst_get_frame_position(OS_Scsi_Tape *STp, Scsi_Request ** aSRpnt);static int osst_flush_write_buffer(OS_Scsi_Tape *STp, Scsi_Request ** aSRpnt, int file_blk);static int osst_write_error_recovery(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int pending);/* Routines that handle the interaction with mid-layer SCSI routines *//* Convert the result to success code */static int osst_chk_result(OS_Scsi_Tape * STp, Scsi_Request * SRpnt){	int dev = TAPE_NR(STp->devt);	int result = SRpnt->sr_result;	unsigned char * sense = SRpnt->sr_sense_buffer, scode;#if DEBUG	const char *stp;#endif	if (!result) {		sense[0] = 0;    /* We don't have sense data if this byte is zero */		return 0;	}	if (driver_byte(result) & DRIVER_SENSE)		scode = sense[2] & 0x0f;	else {		sense[0] = 0;    /* We don't have sense data if this byte is zero */		scode = 0;	}#if DEBUG	if (debugging) {		printk(OSST_DEB_MSG "osst%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",		   dev, result,		   SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2],		   SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5],		   SRpnt->sr_bufflen);		if (driver_byte(result) & DRIVER_SENSE)			print_req_sense("osst", SRpnt);	}	else#endif	if (!(driver_byte(result) & DRIVER_SENSE) ||		((sense[0] & 0x70) == 0x70 &&		 scode != NO_SENSE &&		 scode != RECOVERED_ERROR &&/*      	 scode != UNIT_ATTENTION && */		 scode != BLANK_CHECK &&		 scode != VOLUME_OVERFLOW &&		 SRpnt->sr_cmnd[0] != MODE_SENSE &&		 SRpnt->sr_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */		if (driver_byte(result) & DRIVER_SENSE) {			printk(KERN_WARNING "osst%d: Error with sense data: ", dev);			print_req_sense("osst", SRpnt);		}		else			printk(KERN_WARNING			     "osst%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",			     dev, result, suggestion(result), driver_byte(result) & DRIVER_MASK,			     host_byte(result));	}	if ((sense[0] & 0x70) == 0x70 &&	     scode == RECOVERED_ERROR) {		STp->recover_count++;		STp->recover_erreg++;#if DEBUG		if (debugging) {			if (SRpnt->sr_cmnd[0] == READ_6)				stp = "read";			else if (SRpnt->sr_cmnd[0] == WRITE_6)				stp = "write";			else				stp = "ioctl";			printk(OSST_DEB_MSG "osst%d: Recovered %s error (%d).\n", dev, stp,					     os_scsi_tapes[dev]->recover_count);		}#endif		if ((sense[2] & 0xe0) == 0)			return 0;	}	return (-EIO);}/* Wakeup from interrupt */static void osst_sleep_done (Scsi_Cmnd * SCpnt){	unsigned int dev;	int remainder;	OS_Scsi_Tape * STp;	if ((dev = TAPE_NR(SCpnt->request.rq_dev)) < osst_template.nr_dev) {		STp = os_scsi_tapes[dev];		if ((STp->buffer)->writing &&		    (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&		    (SCpnt->sense_buffer[2] & 0x40)) {			/* EOM at write-behind, has all been written? */			if ((SCpnt->sense_buffer[0] & 0x80) != 0)				remainder = (SCpnt->sense_buffer[3] << 24) |					    (SCpnt->sense_buffer[4] << 16) |					    (SCpnt->sense_buffer[5] << 8 ) |					     SCpnt->sense_buffer[6];			else				remainder = 0;			if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||			    remainder > 0)				(STp->buffer)->midlevel_result = SCpnt->result; /* Error */			else				(STp->buffer)->midlevel_result = INT_MAX;       /* OK */		}		else			(STp->buffer)->midlevel_result = SCpnt->result;		SCpnt->request.rq_status = RQ_SCSI_DONE;		(STp->buffer)->last_SRpnt = SCpnt->sc_request;#if DEBUG		STp->write_pending = 0;#endif		up(SCpnt->request.sem);	}#if DEBUG	else if (debugging)		printk(KERN_ERR "osst?: Illegal interrupt device %x\n", dev);#endif}/* Do the scsi command. Waits until command performed if do_wait is true.   Otherwise osst_write_behind_check() is used to check that the command   has finished. */static	Scsi_Request * osst_do_scsi(Scsi_Request *SRpnt, OS_Scsi_Tape *STp, 	unsigned char *cmd, int bytes, int direction, int timeout, int retries, int do_wait){	unsigned char *bp;//static int inject = 0; /* FIXME - take out inject occasional read errors *///static int repeat = 0;	if (SRpnt == NULL) {		if ((SRpnt = scsi_allocate_request(STp->device)) == NULL) {			printk(KERN_ERR "osst%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));			if (signal_pending(current))				(STp->buffer)->syscall_result = (-EINTR);			else				(STp->buffer)->syscall_result = (-EBUSY);			return NULL;		}	}	cmd[1] |= (SRpnt->sr_device->lun << 5) & 0xe0;	init_MUTEX_LOCKED(&STp->sem);	SRpnt->sr_use_sg = (bytes > (STp->buffer)->sg[0].length) ?				    (STp->buffer)->use_sg : 0;	if (SRpnt->sr_use_sg) {		bp = (char *)&(STp->buffer->sg[0]);		if (STp->buffer->sg_segs < SRpnt->sr_use_sg)			SRpnt->sr_use_sg = STp->buffer->sg_segs;	}	else		bp = (STp->buffer)->b_data;	SRpnt->sr_data_direction = direction;	SRpnt->sr_cmd_len = 0;	SRpnt->sr_request.sem = &(STp->sem);	SRpnt->sr_request.rq_status = RQ_SCSI_BUSY;	SRpnt->sr_request.rq_dev = STp->devt;	scsi_do_req(SRpnt, (void *)cmd, bp, bytes, osst_sleep_done, timeout, retries);	if (do_wait) {		down(SRpnt->sr_request.sem);		SRpnt->sr_request.sem = NULL;		STp->buffer->syscall_result = osst_chk_result(STp, SRpnt);//if ((STp->buffer)->syscall_result == 0 &&//    cmd[0] == READ_6 && cmd[4] && ( /* (++ inject % 83) == 29  || *///     (STp->first_frame_position == 240 /* or STp->read_error_frame to fail again on the block calculated above */ && ++repeat < 3))) {//	printk(OSST_DEB_MSG "osst%d: injecting read error\n", TAPE_NR(STp->devt));//	STp->buffer->last_result_fatal = 1; /* FIXME - take out inject occasional read errors *///}	}	return SRpnt;}/* Handle the write-behind checking (downs the semaphore) */static void osst_write_behind_check(OS_Scsi_Tape *STp){	OSST_buffer * STbuffer;	ST_partstat * STps;	STbuffer = STp->buffer;#if DEBUG	if (STp->write_pending)		STp->nbr_waits++;	else		STp->nbr_finished++;#endif	down(&(STp->sem));	(STp->buffer)->last_SRpnt->sr_request.sem = NULL;	STp->buffer->syscall_result = osst_chk_result(STp, STp->buffer->last_SRpnt);	if ((STp->buffer)->syscall_result)		(STp->buffer)->syscall_result =			osst_write_error_recovery(STp, &((STp->buffer)->last_SRpnt), 1);	else		STp->first_frame_position++;	scsi_release_request((STp->buffer)->last_SRpnt);	if (STbuffer->writing < STbuffer->buffer_bytes)#if 0	memcpy(STbuffer->b_data,	   STbuffer->b_data + STbuffer->writing,	   STbuffer->buffer_bytes - STbuffer->writing);#else	printk(KERN_WARNING "osst: write_behind_check: something left in buffer!\n");#endif	STbuffer->buffer_bytes -= STbuffer->writing;	STps = &(STp->ps[STp->partition]);	if (STps->drv_block >= 0) {		if (STp->block_size == 0)			STps->drv_block++;		else			STps->drv_block += STbuffer->writing / STp->block_size;	}	STbuffer->writing = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -