blob: a11b5ba064fb43278d063936013b5266d0a87bd1 [file] [log] [blame]
/*
SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
file Documentation/scsi/st.txt 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 - 2002 Kai Makisara / Willem Riede
email Kai.Makisara@metla.fi / osst@riede.org
$Header: /home/cvsroot/Driver/osst.c,v 1.68 2002/12/23 16:33:36 riede Exp $
Microscopic alterations - Rik Ling, 2000/12/21
Last st.c sync: Tue Oct 15 22:01:04 2002 by makisara
Some small formal changes - aeb, 950809
*/
static const char * cvsid = "$Id: osst.c,v 1.68 2002/12/23 16:33:36 riede Exp $";
const char * osst_version = "0.99.0";
/* The "failure to reconnect" firmware bug */
#define OSST_FW_NEED_POLL_MIN 10601 /*(107A)*/
#define OSST_FW_NEED_POLL_MAX 10704 /*(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/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 <linux/blk.h>
#include <linux/devfs_fs_kernel.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
#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"
static int max_dev = 0;
static int write_threshold_kbs = 0;
static int max_sg_segs = 0;
#ifdef MODULE
MODULE_AUTHOR("Willem Riede");
MODULE_DESCRIPTION("OnStream {DI-|FW-|SC-|USB}{30|50} Tape Driver");
MODULE_LICENSE("GPL");
MODULE_PARM(max_dev, "i");
MODULE_PARM_DESC(max_dev, "Maximum number of OnStream Tape Drives to attach (4)");
MODULE_PARM(write_threshold_kbs, "i");
MODULE_PARM_DESC(write_threshold_kbs, "Asynchronous write threshold (KB; 32)");
MODULE_PARM(max_sg_segs, "i");
MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (9)");
#else
static struct osst_dev_parm {
char *name;
int *val;
} parms[] __initdata = {
{ "max_dev", &max_dev },
{ "write_threshold_kbs", &write_threshold_kbs },
{ "max_sg_segs", &max_sg_segs }
};
#endif
static char *osst_formats[ST_NBR_MODES] ={"", "l", "m", "a"};
/* 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 DEBUG
static int debugging = 1;
/* uncomment define below to test error recovery */
// #define OSST_INJECT_ERRORS 1
#endif
#define MAX_RETRIES 2
#define MAX_READ_RETRIES 0
#define MAX_WRITE_RETRIES 0
#define MAX_READY_RETRIES 0
#define NO_TAPE NOT_READY
#define OSST_WAIT_POSITION_COMPLETE (HZ > 200 ? HZ / 200 : 1)
#define OSST_WAIT_WRITE_COMPLETE (HZ / 12)
#define OSST_WAIT_LONG_WRITE_COMPLETE (HZ / 2)
#define OSST_TIMEOUT (200 * HZ)
#define OSST_LONG_TIMEOUT (1800 * HZ)
#define TAPE_NR(x) (minor(x) & ~(-1 << ST_MODE_SHIFT))
#define TAPE_MODE(x) ((minor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
#define TAPE_REWIND(x) ((minor(x) & 0x80) == 0)
#define TAPE_IS_RAW(x) (TAPE_MODE(x) & (ST_NBR_MODES >> 1))
/* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
24 bits) */
#define SET_DENS_AND_BLK 0x10001
static int osst_buffer_size = OSST_BUFFER_SIZE;
static int osst_write_threshold = OSST_WRITE_THRESHOLD;
static int osst_max_sg_segs = OSST_MAX_SG;
static int osst_max_dev = OSST_MAX_TAPES;
static int osst_nr_dev;
static OS_Scsi_Tape **os_scsi_tapes = NULL;
static rwlock_t os_scsi_tapes_lock = RW_LOCK_UNLOCKED;
static int modes_defined = FALSE;
static OSST_buffer *new_tape_buffer(int, int, int);
static int enlarge_buffer(OSST_buffer *, 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_attach(Scsi_Device *);
static void osst_detach(Scsi_Device *);
struct Scsi_Device_Template osst_template =
{
.module = THIS_MODULE,
.list = LIST_HEAD_INIT(osst_template.list),
.name = "OnStream Tape",
.scsi_type = TYPE_TAPE,
.attach = osst_attach,
.detach = osst_detach,
.scsi_driverfs_driver = {
.name = "osst",
}
};
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);
static int osst_write_error_recovery(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int pending);
static inline char *tape_name(OS_Scsi_Tape *tape)
{
return tape->drive->disk_name;
}
/* 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)
{
char *name = tape_name(STp);
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_MASK) == 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 "%s:D: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
name, 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 (scode) printk(OSST_DEB_MSG "%s:D: Sense: %02x, ASC: %02x, ASCQ: %02x\n",
name, scode, sense[12], sense[13]);
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 "%s:W: Command with sense data: ", name);
print_req_sense("osst:", SRpnt);
}
else {
static int notyetprinted = 1;
printk(KERN_WARNING
"%s:W: Warning %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
name, result, suggestion(result), driver_byte(result) & DRIVER_MASK,
host_byte(result));
if (notyetprinted) {
notyetprinted = 0;
printk(KERN_INFO
"%s:I: This warning may be caused by your scsi controller,\n", name);
printk(KERN_INFO
"%s:I: it has been reported with some Buslogic cards.\n", name);
}
}
}
STp->pos_unknown |= STp->device->was_reset;
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 "%s:D: Recovered %s error (%d).\n", name, stp,
STp->recover_count);
}
#endif
if ((sense[2] & 0xe0) == 0)
return 0;
}
return (-EIO);
}
/* Wakeup from interrupt */
static void osst_sleep_done (Scsi_Cmnd * SCpnt)
{
OS_Scsi_Tape * STp = container_of(SCpnt->request->rq_disk->private_data, OS_Scsi_Tape, driver);
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[2] & 0x0f) == VOLUME_OVERFLOW)
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
complete(SCpnt->request->waiting);
}
/* 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;
#ifdef OSST_INJECT_ERRORS
static int inject = 0;
static int repeat = 0;
#endif
if (SRpnt == NULL) {
if ((SRpnt = scsi_allocate_request(STp->device)) == NULL) {
printk(KERN_ERR "%s:E: Can't get SCSI request.\n", tape_name(STp));
if (signal_pending(current))
(STp->buffer)->syscall_result = (-EINTR);
else
(STp->buffer)->syscall_result = (-EBUSY);
return NULL;
}
}
init_completion(&STp->wait);
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->waiting = &(STp->wait);
SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
SRpnt->sr_request->rq_disk = STp->drive;
scsi_do_req(SRpnt, (void *)cmd, bp, bytes, osst_sleep_done, timeout, retries);
if (do_wait) {
wait_for_completion(SRpnt->sr_request->waiting);
SRpnt->sr_request->waiting = NULL;
STp->buffer->syscall_result = osst_chk_result(STp, SRpnt);
#ifdef OSST_INJECT_ERRORS
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 "%s:D: Injecting read error\n", tape_name(STp));
STp->buffer->last_result_fatal = 1;
}
#endif
}
return SRpnt;
}
/* Handle the write-behind checking (downs the semaphore) */
static void osst_write_behind_check(OS_Scsi_Tape *STp)
{
OSST_buffer * STbuffer;
STbuffer = STp->buffer;
#if DEBUG
if (STp->write_pending)
STp->nbr_waits++;
else
STp->nbr_finished++;
#endif
wait_for_completion(&(STp->wait));
(STp->buffer)->last_SRpnt->sr_request->waiting = 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)
printk(KERN_WARNING "osst :A: write_behind_check: something left in buffer!\n");
STbuffer->buffer_bytes -= STbuffer->writing;
STbuffer->writing = 0;
return;
}
/* Onstream specific Routines */
/*
* Initialize the OnStream AUX
*/
static void osst_init_aux(OS_Scsi_Tape * STp, int frame_type, int frame_seq_number,
int logical_blk_num, int blk_sz, int blk_cnt)
{
os_aux_t *aux = STp->buffer->aux;
os_partition_t *par = &aux->partition;
os_dat_t *dat = &aux->dat;
if (STp->raw) return;
memset(aux, 0, sizeof(*aux));
aux->format_id = htonl(0);
memcpy(aux->application_sig, "LIN4", 4);
aux->hdwr = htonl(0);
aux->frame_type = frame_type;
switch (frame_type) {
case OS_FRAME_TYPE_HEADER:
aux->update_frame_cntr = htonl(STp->update_frame_cntr);
par->partition_num = OS_CONFIG_PARTITION;
par->par_desc_ver = OS_PARTITION_VERSION;
par->wrt_pass_cntr = htons(0xffff);
/* 0-4 = reserved, 5-9 = header, 2990-2994 = header, 2995-2999 = reserved */
par->first_frame_ppos = htonl(0);
par->last_frame_ppos = htonl(0xbb7);
aux->frame_seq_num = htonl(0);
aux->logical_blk_num_high = htonl(0);
aux->logical_blk_num = htonl(0);
aux->next_mark_ppos = htonl(STp->first_mark_ppos);
break;
case OS_FRAME_TYPE_DATA:
case OS_FRAME_TYPE_MARKER:
dat->dat_sz = 8;
dat->reserved1 = 0;
dat->entry_cnt = 1;
dat->reserved3 = 0;
dat->dat_list[0].blk_sz = htonl(blk_sz);
dat->dat_list[0].blk_cnt = htons(blk_cnt);
dat->dat_list[0].flags = frame_type==OS_FRAME_TYPE_MARKER?
OS_DAT_FLAGS_MARK:OS_DAT_FLAGS_DATA;
dat->dat_list[0].reserved = 0;
case OS_FRAME_TYPE_EOD:
aux->update_frame_cntr = htonl(0);
par->partition_num = OS_DATA_PARTITION;
par->par_desc_ver = OS_PARTITION_VERSION;
par->wrt_pass_cntr = htons(STp->wrt_pass_cntr);
par->first_frame_ppos = htonl(STp->first_data_ppos);
par->last_frame_ppos = htonl(STp->capacity);
aux->frame_seq_num = htonl(frame_seq_number);
aux->logical_blk_num_high = htonl(0);
aux->logical_blk_num = htonl(logical_blk_num);
break;
default: ; /* probably FILL */
}
aux->filemark_cnt = ntohl(STp->filemark_cnt);
aux->phys_fm = ntohl(0xffffffff);
aux->last_mark_ppos = ntohl(STp->last_mark_ppos);
aux->last_mark_lbn = ntohl(STp->last_mark_lbn);
}
/*
* Verify that we have the correct tape frame
*/
static int osst_verify_frame(OS_Scsi_Tape * STp, int frame_seq_number, int quiet)
{
char * name = tape_name(STp);
os_aux_t * aux = STp->buffer->aux;
os_partition_t * par = &(aux->partition);
ST_partstat * STps = &(STp->ps[STp->partition]);
int blk_cnt, blk_sz, i;
if (STp->raw) {
if (STp->buffer->syscall_result) {
for (i=0; i < STp->buffer->sg_segs; i++)
memset(page_address(STp->buffer->sg[i].page),
0, STp->buffer->sg[i].length);
strcpy(STp->buffer->b_data, "READ ERROR ON FRAME");
} else
STp->buffer->buffer_bytes = OS_FRAME_SIZE;
return 1;
}
if (STp->buffer->syscall_result) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, read error\n", name);
#endif
return 0;
}
if (ntohl(aux->format_id) != 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, format_id %u\n", name, ntohl(aux->format_id));
#endif
goto err_out;
}
if (memcmp(aux->application_sig, STp->application_sig, 4) != 0 &&
(memcmp(aux->application_sig, "LIN3", 4) != 0 || STp->linux_media_version != 4)) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, incorrect application signature\n", name);
#endif
goto err_out;
}
if (par->partition_num != OS_DATA_PARTITION) {
if (!STp->linux_media || STp->linux_media_version != 2) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, partition num %d\n",
name, par->partition_num);
#endif
goto err_out;
}
}
if (par->par_desc_ver != OS_PARTITION_VERSION) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, partition version %d\n", name, par->par_desc_ver);
#endif
goto err_out;
}
if (ntohs(par->wrt_pass_cntr) != STp->wrt_pass_cntr) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, wrt_pass_cntr %d (expected %d)\n",
name, ntohs(par->wrt_pass_cntr), STp->wrt_pass_cntr);
#endif
goto err_out;
}
if (aux->frame_type != OS_FRAME_TYPE_DATA &&
aux->frame_type != OS_FRAME_TYPE_EOD &&
aux->frame_type != OS_FRAME_TYPE_MARKER) {
if (!quiet)
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, frame type %x\n", name, aux->frame_type);
#endif
goto err_out;
}
if (aux->frame_type == OS_FRAME_TYPE_EOD &&
STp->first_frame_position < STp->eod_frame_ppos) {
printk(KERN_INFO "%s:I: Skipping premature EOD frame %d\n", name,
STp->first_frame_position);
goto err_out;
}
if (frame_seq_number != -1 && ntohl(aux->frame_seq_num) != frame_seq_number) {
if (!quiet)
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame, sequence number %u (expected %d)\n",
name, ntohl(aux->frame_seq_num), frame_seq_number);
#endif
goto err_out;
}
if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
STps->eof = ST_FM_HIT;
i = ntohl(aux->filemark_cnt);
if (STp->header_cache != NULL && i < OS_FM_TAB_MAX && (i > STp->filemark_cnt ||
STp->first_frame_position - 1 != ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[i]))) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: %s filemark %d at frame pos %d\n", name,
STp->header_cache->dat_fm_tab.fm_tab_ent[i] == 0?"Learned":"Corrected",
i, STp->first_frame_position - 1);
#endif
STp->header_cache->dat_fm_tab.fm_tab_ent[i] = htonl(STp->first_frame_position - 1);
if (i >= STp->filemark_cnt)
STp->filemark_cnt = i+1;
}
}
if (aux->frame_type == OS_FRAME_TYPE_EOD) {
STps->eof = ST_EOD_1;
STp->frame_in_buffer = 1;
}
if (aux->frame_type == OS_FRAME_TYPE_DATA) {
blk_cnt = ntohs(aux->dat.dat_list[0].blk_cnt);
blk_sz = ntohl(aux->dat.dat_list[0].blk_sz);
STp->buffer->buffer_bytes = blk_cnt * blk_sz;
STp->buffer->read_pointer = 0;
STp->frame_in_buffer = 1;
/* See what block size was used to write file */
if (STp->block_size != blk_sz && blk_sz > 0) {
printk(KERN_INFO
"%s:I: File was written with block size %d%c, currently %d%c, adjusted to match.\n",
name, blk_sz<1024?blk_sz:blk_sz/1024,blk_sz<1024?'b':'k',
STp->block_size<1024?STp->block_size:STp->block_size/1024,
STp->block_size<1024?'b':'k');
STp->block_size = blk_sz;
STp->buffer->buffer_blocks = OS_DATA_SIZE / blk_sz;
}
STps->eof = ST_NOEOF;
}
STp->frame_seq_number = ntohl(aux->frame_seq_num);
STp->logical_blk_num = ntohl(aux->logical_blk_num);
return 1;
err_out:
if (STp->read_error_frame == 0)
STp->read_error_frame = STp->first_frame_position - 1;
return 0;
}
/*
* Wait for the unit to become Ready
*/
static int osst_wait_ready(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, unsigned timeout, int initial_delay)
{
unsigned char cmd[MAX_COMMAND_SIZE];
Scsi_Request * SRpnt;
unsigned long startwait = jiffies;
#if DEBUG
int dbg = debugging;
char * name = tape_name(STp);
printk(OSST_DEB_MSG "%s:D: Reached onstream wait ready\n", name);
#endif
if (initial_delay > 0) {
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(initial_delay);
}
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = TEST_UNIT_READY;
SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout, MAX_READY_RETRIES, TRUE);
*aSRpnt = SRpnt;
if (!SRpnt) return (-EBUSY);
while ( STp->buffer->syscall_result && time_before(jiffies, startwait + timeout*HZ) &&
(( SRpnt->sr_sense_buffer[2] == 2 && SRpnt->sr_sense_buffer[12] == 4 &&
(SRpnt->sr_sense_buffer[13] == 1 || SRpnt->sr_sense_buffer[13] == 8) ) ||
( SRpnt->sr_sense_buffer[2] == 6 && SRpnt->sr_sense_buffer[12] == 0x28 &&
SRpnt->sr_sense_buffer[13] == 0 ) )) {
#if DEBUG
if (debugging) {
printk(OSST_DEB_MSG "%s:D: Sleeping in onstream wait ready\n", name);
printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
debugging = 0;
}
#endif
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ / 10);
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = TEST_UNIT_READY;
SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout, MAX_READY_RETRIES, TRUE);
}
*aSRpnt = SRpnt;
#if DEBUG
debugging = dbg;
#endif
if ( STp->buffer->syscall_result &&
osst_write_error_recovery(STp, aSRpnt, 0) ) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Abnormal exit from onstream wait ready\n", name);
printk(OSST_DEB_MSG "%s:D: Result = %d, Sense: 0=%02x, 2=%02x, 12=%02x, 13=%02x\n", name,
STp->buffer->syscall_result, SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[2],
SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
#endif
return (-EIO);
}
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Normal exit from onstream wait ready\n", name);
#endif
return 0;
}
/*
* Wait for a tape to be inserted in the unit
*/
static int osst_wait_for_medium(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, unsigned timeout)
{
unsigned char cmd[MAX_COMMAND_SIZE];
Scsi_Request * SRpnt;
unsigned long startwait = jiffies;
#if DEBUG
int dbg = debugging;
char * name = tape_name(STp);
printk(OSST_DEB_MSG "%s:D: Reached onstream wait for medium\n", name);
#endif
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = TEST_UNIT_READY;
SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout, MAX_READY_RETRIES, TRUE);
*aSRpnt = SRpnt;
if (!SRpnt) return (-EBUSY);
while ( STp->buffer->syscall_result && time_before(jiffies, startwait + timeout*HZ) &&
SRpnt->sr_sense_buffer[2] == 2 && SRpnt->sr_sense_buffer[12] == 0x3a &&
SRpnt->sr_sense_buffer[13] == 0 ) {
#if DEBUG
if (debugging) {
printk(OSST_DEB_MSG "%s:D: Sleeping in onstream wait medium\n", name);
printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
debugging = 0;
}
#endif
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ / 10);
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = TEST_UNIT_READY;
SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout, MAX_READY_RETRIES, TRUE);
}
*aSRpnt = SRpnt;
#if DEBUG
debugging = dbg;
#endif
if ( STp->buffer->syscall_result && SRpnt->sr_sense_buffer[2] != 2 &&
SRpnt->sr_sense_buffer[12] != 4 && SRpnt->sr_sense_buffer[13] == 1) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Abnormal exit from onstream wait medium\n", name);
printk(OSST_DEB_MSG "%s:D: Result = %d, Sense: 0=%02x, 2=%02x, 12=%02x, 13=%02x\n", name,
STp->buffer->syscall_result, SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[2],
SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
#endif
return 0;
}
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Normal exit from onstream wait medium\n", name);
#endif
return 1;
}
static int osst_position_tape_and_confirm(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int frame)
{
int retval;
osst_wait_ready(STp, aSRpnt, 15 * 60, 0); /* TODO - can this catch a write error? */
retval = osst_set_frame_position(STp, aSRpnt, frame, 0);
if (retval) return (retval);
osst_wait_ready(STp, aSRpnt, 15 * 60, OSST_WAIT_POSITION_COMPLETE);
return (osst_get_frame_position(STp, aSRpnt));
}
/*
* Wait for write(s) to complete
*/
static int osst_flush_drive_buffer(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
unsigned char cmd[MAX_COMMAND_SIZE];
Scsi_Request * SRpnt;
int result = 0;
int delay = OSST_WAIT_WRITE_COMPLETE;
#if DEBUG
char * name = tape_name(STp);
printk(OSST_DEB_MSG "%s:D: Reached onstream flush drive buffer (write filemark)\n", name);
#endif
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = WRITE_FILEMARKS;
cmd[1] = 1;
SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout, MAX_WRITE_RETRIES, TRUE);
*aSRpnt = SRpnt;
if (!SRpnt) return (-EBUSY);
if (STp->buffer->syscall_result) {
if ((SRpnt->sr_sense_buffer[2] & 0x0f) == 2 && SRpnt->sr_sense_buffer[12] == 4) {
if (SRpnt->sr_sense_buffer[13] == 8) {
delay = OSST_WAIT_LONG_WRITE_COMPLETE;
}
} else
result = osst_write_error_recovery(STp, aSRpnt, 0);
}
result |= osst_wait_ready(STp, aSRpnt, 5 * 60, delay);
STp->ps[STp->partition].rw = OS_WRITING_COMPLETE;
return (result);
}
#define OSST_POLL_PER_SEC 10
static int osst_wait_frame(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int curr, int minlast, int to)
{
unsigned long startwait = jiffies;
char * name = tape_name(STp);
#if DEBUG
char notyetprinted = 1;
#endif
if (minlast >= 0 && STp->ps[STp->partition].rw != ST_READING)
printk(KERN_ERR "%s:A: Waiting for frame without having initialized read!\n", name);
while (time_before (jiffies, startwait + to*HZ))
{
int result;
result = osst_get_frame_position (STp, aSRpnt);
if (result == -EIO)
if ((result = osst_write_error_recovery(STp, aSRpnt, 0)) == 0)
return 0; /* successful recovery leaves drive ready for frame */
if (result < 0) break;
if (STp->first_frame_position == curr &&
((minlast < 0 &&
(signed)STp->last_frame_position > (signed)curr + minlast) ||
(minlast >= 0 && STp->cur_frames > minlast)
) && result >= 0)
{
#if DEBUG
if (debugging || jiffies - startwait >= 2*HZ/OSST_POLL_PER_SEC)
printk (OSST_DEB_MSG
"%s:D: Succ wait f fr %i (>%i): %i-%i %i (%i): %3li.%li s\n",
name, curr, curr+minlast, STp->first_frame_position,
STp->last_frame_position, STp->cur_frames,
result, (jiffies-startwait)/HZ,
(((jiffies-startwait)%HZ)*10)/HZ);
#endif
return 0;
}
#if DEBUG
if (jiffies - startwait >= 2*HZ/OSST_POLL_PER_SEC && notyetprinted)
{
printk (OSST_DEB_MSG "%s:D: Wait for frame %i (>%i): %i-%i %i (%i)\n",
name, curr, curr+minlast, STp->first_frame_position,
STp->last_frame_position, STp->cur_frames, result);
notyetprinted--;
}
#endif
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout (HZ / OSST_POLL_PER_SEC);
}
#if DEBUG
printk (OSST_DEB_MSG "%s:D: Fail wait f fr %i (>%i): %i-%i %i: %3li.%li s\n",
name, curr, curr+minlast, STp->first_frame_position,
STp->last_frame_position, STp->cur_frames,
(jiffies-startwait)/HZ, (((jiffies-startwait)%HZ)*10)/HZ);
#endif
return -EBUSY;
}
/*
* Read the next OnStream tape frame at the current location
*/
static int osst_read_frame(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int timeout)
{
unsigned char cmd[MAX_COMMAND_SIZE];
Scsi_Request * SRpnt;
int retval = 0;
#if DEBUG
os_aux_t * aux = STp->buffer->aux;
char * name = tape_name(STp);
#endif
/* TODO: Error handling */
if (STp->poll)
retval = osst_wait_frame (STp, aSRpnt, STp->first_frame_position, 0, timeout);
#if 0// DEBUG
printk ("osst_read: wait for frame returned %i\n", retval);
#endif
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = READ_6;
cmd[1] = 1;
cmd[4] = 1;
#if DEBUG
if (debugging)
printk(OSST_DEB_MSG "%s:D: Reading frame from OnStream tape\n", name);
#endif
SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, OS_FRAME_SIZE, SCSI_DATA_READ,
STp->timeout, MAX_READ_RETRIES, TRUE);
*aSRpnt = SRpnt;
if (!SRpnt)
return (-EBUSY);
if ((STp->buffer)->syscall_result) {
retval = 1;
if (STp->read_error_frame == 0) {
STp->read_error_frame = STp->first_frame_position;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Recording read error at %d\n", name, STp->read_error_frame);
#endif
}
#if DEBUG
if (debugging)
printk(OSST_DEB_MSG "%s:D: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
name,
SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1],
SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3],
SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5],
SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7]);
#endif
}
else
STp->first_frame_position++;
#if DEBUG
if (debugging) {
char sig[8]; int i;
for (i=0;i<4;i++)
sig[i] = aux->application_sig[i]<32?'^':aux->application_sig[i];
sig[4] = '\0';
printk(OSST_DEB_MSG
"%s:D: AUX: %s UpdFrCt#%d Wpass#%d %s FrSeq#%d LogBlk#%d Qty=%d Sz=%d\n", name, sig,
ntohl(aux->update_frame_cntr), ntohs(aux->partition.wrt_pass_cntr),
aux->frame_type==1?"EOD":aux->frame_type==2?"MARK":
aux->frame_type==8?"HEADR":aux->frame_type==0x80?"DATA":"FILL",
ntohl(aux->frame_seq_num), ntohl(aux->logical_blk_num),
ntohs(aux->dat.dat_list[0].blk_cnt), ntohl(aux->dat.dat_list[0].blk_sz) );
if (aux->frame_type==2)
printk(OSST_DEB_MSG "%s:D: mark_cnt=%d, last_mark_ppos=%d, last_mark_lbn=%d\n", name,
ntohl(aux->filemark_cnt), ntohl(aux->last_mark_ppos), ntohl(aux->last_mark_lbn));
printk(OSST_DEB_MSG "%s:D: Exit read frame from OnStream tape with code %d\n", name, retval);
}
#endif
return (retval);
}
static int osst_initiate_read(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
ST_partstat * STps = &(STp->ps[STp->partition]);
Scsi_Request * SRpnt ;
unsigned char cmd[MAX_COMMAND_SIZE];
int retval = 0;
#if DEBUG
char * name = tape_name(STp);
#endif
if (STps->rw != ST_READING) { /* Initialize read operation */
if (STps->rw == ST_WRITING || STp->dirty) {
STp->write_type = OS_WRITE_DATA;
osst_flush_write_buffer(STp, aSRpnt);
osst_flush_drive_buffer(STp, aSRpnt);
}
STps->rw = ST_READING;
STp->frame_in_buffer = 0;
/*
* Issue a read 0 command to get the OnStream drive
* read frames into its buffer.
*/
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = READ_6;
cmd[1] = 1;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Start Read Ahead on OnStream tape\n", name);
#endif
SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout, MAX_READ_RETRIES, TRUE);
*aSRpnt = SRpnt;
retval = STp->buffer->syscall_result;
}
return retval;
}
static int osst_get_logical_frame(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int frame_seq_number, int quiet)
{
ST_partstat * STps = &(STp->ps[STp->partition]);
char * name = tape_name(STp);
int cnt = 0,
bad = 0,
past = 0,
x,
position;
/*
* If we want just any frame (-1) and there is a frame in the buffer, return it
*/
if (frame_seq_number == -1 && STp->frame_in_buffer) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Frame %d still in buffer\n", name, STp->frame_seq_number);
#endif
return (STps->eof);
}
/*
* Search and wait for the next logical tape frame
*/
while (1) {
if (cnt++ > 400) {
printk(KERN_ERR "%s:E: Couldn't find logical frame %d, aborting\n",
name, frame_seq_number);
if (STp->read_error_frame) {
osst_set_frame_position(STp, aSRpnt, STp->read_error_frame, 0);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Repositioning tape to bad frame %d\n",
name, STp->read_error_frame);
#endif
STp->read_error_frame = 0;
}
return (-EIO);
}
#if DEBUG
if (debugging)
printk(OSST_DEB_MSG "%s:D: Looking for frame %d, attempt %d\n",
name, frame_seq_number, cnt);
#endif
if ( osst_initiate_read(STp, aSRpnt)
|| ( (!STp->frame_in_buffer) && osst_read_frame(STp, aSRpnt, 30) ) ) {
if (STp->raw)
return (-EIO);
position = osst_get_frame_position(STp, aSRpnt);
if (position >= 0xbae && position < 0xbb8)
position = 0xbb8;
else if (position > STp->eod_frame_ppos || ++bad == 10) {
position = STp->read_error_frame - 1;
}
else {
position += 39;
cnt += 20;
}
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Bad frame detected, positioning tape to block %d\n",
name, position);
#endif
osst_set_frame_position(STp, aSRpnt, position, 0);
continue;
}
if (osst_verify_frame(STp, frame_seq_number, quiet))
break;
if (osst_verify_frame(STp, -1, quiet)) {
x = ntohl(STp->buffer->aux->frame_seq_num);
if (STp->fast_open) {
printk(KERN_WARNING
"%s:W: Found logical frame %d instead of %d after fast open\n",
name, x, frame_seq_number);
STp->header_ok = 0;
STp->read_error_frame = 0;
return (-EIO);
}
if (x > frame_seq_number) {
if (++past > 3) {
/* positioning backwards did not bring us to the desired frame */
position = STp->read_error_frame - 1;
}
else {
position = osst_get_frame_position(STp, aSRpnt)
+ frame_seq_number - x - 1;
if (STp->first_frame_position >= 3000 && position < 3000)
position -= 10;
}
#if DEBUG
printk(OSST_DEB_MSG
"%s:D: Found logical frame %d while looking for %d: back up %d\n",
name, x, frame_seq_number,
STp->first_frame_position - position);
#endif
osst_set_frame_position(STp, aSRpnt, position, 0);
cnt += 10;
}
else
past = 0;
}
if (osst_get_frame_position(STp, aSRpnt) == 0xbaf) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping config partition\n", name);
#endif
osst_set_frame_position(STp, aSRpnt, 0xbb8, 0);
cnt--;
}
STp->frame_in_buffer = 0;
}
if (cnt > 1) {
STp->recover_count++;
STp->recover_erreg++;
printk(KERN_WARNING "%s:I: Don't worry, Read error at position %d recovered\n",
name, STp->read_error_frame);
}
STp->read_count++;
#if DEBUG
if (debugging || STps->eof)
printk(OSST_DEB_MSG
"%s:D: Exit get logical frame (%d=>%d) from OnStream tape with code %d\n",
name, frame_seq_number, STp->frame_seq_number, STps->eof);
#endif
STp->fast_open = FALSE;
STp->read_error_frame = 0;
return (STps->eof);
}
static int osst_seek_logical_blk(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int logical_blk_num)
{
ST_partstat * STps = &(STp->ps[STp->partition]);
char * name = tape_name(STp);
int retries = 0;
int frame_seq_estimate, ppos_estimate, move;
if (logical_blk_num < 0) logical_blk_num = 0;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Seeking logical block %d (now at %d, size %d%c)\n",
name, logical_blk_num, STp->logical_blk_num,
STp->block_size<1024?STp->block_size:STp->block_size/1024,
STp->block_size<1024?'b':'k');
#endif
/* Do we know where we are? */
if (STps->drv_block >= 0) {
move = logical_blk_num - STp->logical_blk_num;
if (move < 0) move -= (OS_DATA_SIZE / STp->block_size) - 1;
move /= (OS_DATA_SIZE / STp->block_size);
frame_seq_estimate = STp->frame_seq_number + move;
} else
frame_seq_estimate = logical_blk_num * STp->block_size / OS_DATA_SIZE;
if (frame_seq_estimate < 2980) ppos_estimate = frame_seq_estimate + 10;
else ppos_estimate = frame_seq_estimate + 20;
while (++retries < 10) {
if (ppos_estimate > STp->eod_frame_ppos-2) {
frame_seq_estimate += STp->eod_frame_ppos - 2 - ppos_estimate;
ppos_estimate = STp->eod_frame_ppos - 2;
}
if (frame_seq_estimate < 0) {
frame_seq_estimate = 0;
ppos_estimate = 10;
}
osst_set_frame_position(STp, aSRpnt, ppos_estimate, 0);
if (osst_get_logical_frame(STp, aSRpnt, frame_seq_estimate, 1) >= 0) {
/* we've located the estimated frame, now does it have our block? */
if (logical_blk_num < STp->logical_blk_num ||
logical_blk_num >= STp->logical_blk_num + ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt)) {
if (STps->eof == ST_FM_HIT)
move = logical_blk_num < STp->logical_blk_num? -2 : 1;
else {
move = logical_blk_num - STp->logical_blk_num;
if (move < 0) move -= (OS_DATA_SIZE / STp->block_size) - 1;
move /= (OS_DATA_SIZE / STp->block_size);
}
if (!move) move = logical_blk_num > STp->logical_blk_num ? 1 : -1;
#if DEBUG
printk(OSST_DEB_MSG
"%s:D: Seek retry %d at ppos %d fsq %d (est %d) lbn %d (need %d) move %d\n",
name, retries, ppos_estimate, STp->frame_seq_number, frame_seq_estimate,
STp->logical_blk_num, logical_blk_num, move);
#endif
frame_seq_estimate += move;
ppos_estimate += move;
continue;
} else {
STp->buffer->read_pointer = (logical_blk_num - STp->logical_blk_num) * STp->block_size;
STp->buffer->buffer_bytes -= STp->buffer->read_pointer;
STp->logical_blk_num = logical_blk_num;
#if DEBUG
printk(OSST_DEB_MSG
"%s:D: Seek success at ppos %d fsq %d in_buf %d, bytes %d, ptr %d*%d\n",
name, ppos_estimate, STp->frame_seq_number, STp->frame_in_buffer,
STp->buffer->buffer_bytes, STp->buffer->read_pointer / STp->block_size,
STp->block_size);
#endif
STps->drv_file = ntohl(STp->buffer->aux->filemark_cnt);
if (STps->eof == ST_FM_HIT) {
STps->drv_file++;
STps->drv_block = 0;
} else {
STps->drv_block = ntohl(STp->buffer->aux->last_mark_lbn)?
STp->logical_blk_num -
(STps->drv_file ? ntohl(STp->buffer->aux->last_mark_lbn) + 1 : 0):
-1;
}
STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_NOEOF;
return 0;
}
}
if (osst_get_logical_frame(STp, aSRpnt, -1, 1) < 0)
goto error;
/* we are not yet at the estimated frame, adjust our estimate of its physical position */
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Seek retry %d at ppos %d fsq %d (est %d) lbn %d (need %d)\n",
name, retries, ppos_estimate, STp->frame_seq_number, frame_seq_estimate,
STp->logical_blk_num, logical_blk_num);
#endif
if (frame_seq_estimate != STp->frame_seq_number)
ppos_estimate += frame_seq_estimate - STp->frame_seq_number;
else
break;
}
error:
printk(KERN_ERR "%s:E: Couldn't seek to logical block %d (at %d), %d retries\n",
name, logical_blk_num, STp->logical_blk_num, retries);
return (-EIO);
}
/* The values below are based on the OnStream frame payload size of 32K == 2**15,
* that is, OSST_FRAME_SHIFT + OSST_SECTOR_SHIFT must be 15. With a minimum block
* size of 512 bytes, we need to be able to resolve 32K/512 == 64 == 2**6 positions
* inside each frame. Finaly, OSST_SECTOR_MASK == 2**OSST_FRAME_SHIFT - 1.
*/
#define OSST_FRAME_SHIFT 6
#define OSST_SECTOR_SHIFT 9
#define OSST_SECTOR_MASK 0x03F
static int osst_get_sector(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
int sector;
#if DEBUG
char * name = tape_name(STp);
printk(OSST_DEB_MSG
"%s:D: Positioned at ppos %d, frame %d, lbn %d, file %d, blk %d, %cptr %d, eof %d\n",
name, STp->first_frame_position, STp->frame_seq_number, STp->logical_blk_num,
STp->ps[STp->partition].drv_file, STp->ps[STp->partition].drv_block,
STp->ps[STp->partition].rw == ST_WRITING?'w':'r',
STp->ps[STp->partition].rw == ST_WRITING?STp->buffer->buffer_bytes:
STp->buffer->read_pointer, STp->ps[STp->partition].eof);
#endif
/* do we know where we are inside a file? */
if (STp->ps[STp->partition].drv_block >= 0) {
sector = (STp->frame_in_buffer ? STp->first_frame_position-1 :
STp->first_frame_position) << OSST_FRAME_SHIFT;
if (STp->ps[STp->partition].rw == ST_WRITING)
sector |= (STp->buffer->buffer_bytes >> OSST_SECTOR_SHIFT) & OSST_SECTOR_MASK;
else
sector |= (STp->buffer->read_pointer >> OSST_SECTOR_SHIFT) & OSST_SECTOR_MASK;
} else {
sector = osst_get_frame_position(STp, aSRpnt);
if (sector > 0)
sector <<= OSST_FRAME_SHIFT;
}
return sector;
}
static int osst_seek_sector(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int sector)
{
ST_partstat * STps = &(STp->ps[STp->partition]);
int frame = sector >> OSST_FRAME_SHIFT,
offset = (sector & OSST_SECTOR_MASK) << OSST_SECTOR_SHIFT,
r;
#if DEBUG
char * name = tape_name(STp);
printk(OSST_DEB_MSG "%s:D: Seeking sector %d in frame %d at offset %d\n",
name, sector, frame, offset);
#endif
if (frame < 0 || frame >= STp->capacity) return (-ENXIO);
if (frame <= STp->first_data_ppos) {
STp->frame_seq_number = STp->logical_blk_num = STps->drv_file = STps->drv_block = 0;
return (osst_set_frame_position(STp, aSRpnt, frame, 0));
}
r = osst_set_frame_position(STp, aSRpnt, offset?frame:frame-1, 0);
if (r < 0) return r;
r = osst_get_logical_frame(STp, aSRpnt, -1, 1);
if (r < 0) return r;
if (osst_get_frame_position(STp, aSRpnt) != (offset?frame+1:frame)) return (-EIO);
if (offset) {
STp->logical_blk_num += offset / STp->block_size;
STp->buffer->read_pointer = offset;
STp->buffer->buffer_bytes -= offset;
} else {
STp->frame_seq_number++;
STp->frame_in_buffer = 0;
STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
STp->buffer->buffer_bytes = STp->buffer->read_pointer = 0;
}
STps->drv_file = ntohl(STp->buffer->aux->filemark_cnt);
if (STps->eof == ST_FM_HIT) {
STps->drv_file++;
STps->drv_block = 0;
} else {
STps->drv_block = ntohl(STp->buffer->aux->last_mark_lbn)?
STp->logical_blk_num -
(STps->drv_file ? ntohl(STp->buffer->aux->last_mark_lbn) + 1 : 0):
-1;
}
STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_NOEOF;
#if DEBUG
printk(OSST_DEB_MSG
"%s:D: Now positioned at ppos %d, frame %d, lbn %d, file %d, blk %d, rptr %d, eof %d\n",
name, STp->first_frame_position, STp->frame_seq_number, STp->logical_blk_num,
STps->drv_file, STps->drv_block, STp->buffer->read_pointer, STps->eof);
#endif
return 0;
}
/*
* Read back the drive's internal buffer contents, as a part
* of the write error recovery mechanism for old OnStream
* firmware revisions.
* Precondition for this function to work: all frames in the
* drive's buffer must be of one type (DATA, MARK or EOD)!
*/
static int osst_read_back_buffer_and_rewrite(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt,
unsigned int frame, unsigned int skip, int pending)
{
Scsi_Request * SRpnt = * aSRpnt;
unsigned char * buffer, * p;
unsigned char cmd[MAX_COMMAND_SIZE];
int flag, new_frame, i;
int nframes = STp->cur_frames;
int blks_per_frame = ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
int frame_seq_number = ntohl(STp->buffer->aux->frame_seq_num)
- (nframes + pending - 1);
int logical_blk_num = ntohl(STp->buffer->aux->logical_blk_num)
- (nframes + pending - 1) * blks_per_frame;
char * name = tape_name(STp);
unsigned long startwait = jiffies;
#if DEBUG
int dbg = debugging;
#endif
if ((buffer = (unsigned char *)vmalloc((nframes + 1) * OS_DATA_SIZE)) == NULL)
return (-EIO);
printk(KERN_INFO "%s:I: Reading back %d frames from drive buffer%s\n",
name, nframes, pending?" and one that was pending":"");
osst_copy_from_buffer(STp->buffer, (p = &buffer[nframes * OS_DATA_SIZE]));
#if DEBUG
if (pending && debugging)
printk(OSST_DEB_MSG "%s:D: Pending frame %d (lblk %d), data %02x %02x %02x %02x\n",
name, frame_seq_number + nframes,
logical_blk_num + nframes * blks_per_frame,
p[0], p[1], p[2], p[3]);
#endif
for (i = 0, p = buffer; i < nframes; i++, p += OS_DATA_SIZE) {
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = 0x3C; /* Buffer Read */
cmd[1] = 6; /* Retrieve Faulty Block */
cmd[7] = 32768 >> 8;
cmd[8] = 32768 & 0xff;
SRpnt = osst_do_scsi(SRpnt, STp, cmd, OS_FRAME_SIZE, SCSI_DATA_READ,
STp->timeout, MAX_READ_RETRIES, TRUE);
if ((STp->buffer)->syscall_result || !SRpnt) {
printk(KERN_ERR "%s:E: Failed to read frame back from OnStream buffer\n", name);
vfree((void *)buffer);
*aSRpnt = SRpnt;
return (-EIO);
}
osst_copy_from_buffer(STp->buffer, p);
#if DEBUG
if (debugging)
printk(OSST_DEB_MSG "%s:D: Read back logical frame %d, data %02x %02x %02x %02x\n",
name, frame_seq_number + i, p[0], p[1], p[2], p[3]);
#endif
}
*aSRpnt = SRpnt;
osst_get_frame_position(STp, aSRpnt);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Frames left in buffer: %d\n", name, STp->cur_frames);
#endif
/* Write synchronously so we can be sure we're OK again and don't have to recover recursively */
/* In the header we don't actually re-write the frames that fail, just the ones after them */
for (flag=1, new_frame=frame, p=buffer, i=0; i < nframes + pending; ) {
if (flag) {
if (STp->write_type == OS_WRITE_HEADER) {
i += skip;
p += skip * OS_DATA_SIZE;
}
else if (new_frame < 2990 && new_frame+skip+nframes+pending >= 2990)
new_frame = 3000-i;
else
new_frame += skip;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Position to frame %d, write fseq %d\n",
name, new_frame+i, frame_seq_number+i);
#endif
osst_set_frame_position(STp, aSRpnt, new_frame + i, 0);
osst_wait_ready(STp, aSRpnt, 60, OSST_WAIT_POSITION_COMPLETE);
osst_get_frame_position(STp, aSRpnt);
SRpnt = * aSRpnt;
if (new_frame > frame + 1000) {
printk(KERN_ERR "%s:E: Failed to find writable tape media\n", name);
vfree((void *)buffer);
return (-EIO);
}
flag = 0;
if ( i >= nframes + pending ) break;
}
osst_copy_to_buffer(STp->buffer, p);
/*
* IMPORTANT: for error recovery to work, _never_ queue frames with mixed frame type!
*/
osst_init_aux(STp, STp->buffer->aux->frame_type, frame_seq_number+i,
logical_blk_num + i*blks_per_frame,
ntohl(STp->buffer->aux->dat.dat_list[0].blk_sz), blks_per_frame);
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = WRITE_6;
cmd[1] = 1;
cmd[4] = 1;
#if DEBUG
if (debugging)
printk(OSST_DEB_MSG
"%s:D: About to write frame %d, seq %d, lbn %d, data %02x %02x %02x %02x\n",
name, new_frame+i, frame_seq_number+i, logical_blk_num + i*blks_per_frame,
p[0], p[1], p[2], p[3]);
#endif
SRpnt = osst_do_scsi(SRpnt, STp, cmd, OS_FRAME_SIZE, SCSI_DATA_WRITE,
STp->timeout, MAX_WRITE_RETRIES, TRUE);
if (STp->buffer->syscall_result)
flag = 1;
else {
p += OS_DATA_SIZE; i++;
/* if we just sent the last frame, wait till all successfully written */
if ( i == nframes + pending ) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Check re-write successful\n", name);
#endif
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = WRITE_FILEMARKS;
cmd[1] = 1;
SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, SCSI_DATA_NONE,
STp->timeout, MAX_WRITE_RETRIES, TRUE);
#if DEBUG
if (debugging) {
printk(OSST_DEB_MSG "%s:D: Sleeping in re-write wait ready\n", name);
printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
debugging = 0;
}
#endif
flag = STp->buffer->syscall_result;
while ( !flag && time_before(jiffies, startwait + 60*HZ) ) {
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = TEST_UNIT_READY;
SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, SCSI_DATA_NONE, STp->timeout,
MAX_READY_RETRIES, TRUE);
if (SRpnt->sr_sense_buffer[2] == 2 && SRpnt->sr_sense_buffer[12] == 4 &&
(SRpnt->sr_sense_buffer[13] == 1 || SRpnt->sr_sense_buffer[13] == 8)) {
/* in the process of becoming ready */
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ / 10);
continue;
}
if (STp->buffer->syscall_result)
flag = 1;
break;
}
#if DEBUG
debugging = dbg;
printk(OSST_DEB_MSG "%s:D: Wait re-write finished\n", name);
#endif
}
}
*aSRpnt = SRpnt;
if (flag) {
if ((SRpnt->sr_sense_buffer[ 2] & 0x0f) == 13 &&
SRpnt->sr_sense_buffer[12] == 0 &&
SRpnt->sr_sense_buffer[13] == 2) {
printk(KERN_ERR "%s:E: Volume overflow in write error recovery\n", name);
vfree((void *)buffer);
return (-EIO); /* hit end of tape = fail */
}
i = ((SRpnt->sr_sense_buffer[3] << 24) |
(SRpnt->sr_sense_buffer[4] << 16) |
(SRpnt->sr_sense_buffer[5] << 8) |
SRpnt->sr_sense_buffer[6] ) - new_frame;
p = &buffer[i * OS_DATA_SIZE];
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Additional write error at %d\n", name, new_frame+i);
#endif
osst_get_frame_position(STp, aSRpnt);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: reported frame positions: host = %d, tape = %d\n",
name, STp->first_frame_position, STp->last_frame_position);
#endif
}
}
if (!pending)
osst_copy_to_buffer(STp->buffer, p); /* so buffer content == at entry in all cases */
vfree((void *)buffer);
return 0;
}
static int osst_reposition_and_retry(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt,
unsigned int frame, unsigned int skip, int pending)
{
unsigned char cmd[MAX_COMMAND_SIZE];
Scsi_Request * SRpnt;
char * name = tape_name(STp);
int expected = 0;
int attempts = 1000 / skip;
int flag = 1;
unsigned long startwait = jiffies;
#if DEBUG
int dbg = debugging;
#endif
while (attempts && time_before(jiffies, startwait + 60*HZ)) {
if (flag) {
#if DEBUG
debugging = dbg;
#endif
if (frame < 2990 && frame+skip+STp->cur_frames+pending >= 2990)
frame = 3000-skip;
expected = frame+skip+STp->cur_frames+pending;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Position to fppos %d, re-write from fseq %d\n",
name, frame+skip, STp->frame_seq_number-STp->cur_frames-pending);
#endif
osst_set_frame_position(STp, aSRpnt, frame + skip, 1);
flag = 0;
attempts--;
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ / 10);
}
if (osst_get_frame_position(STp, aSRpnt) < 0) { /* additional write error */
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Addl error, host %d, tape %d, buffer %d\n",
name, STp->first_frame_position,
STp->last_frame_position, STp->cur_frames);
#endif
frame = STp->last_frame_position;
flag = 1;
continue;
}
if (pending && STp->cur_frames < 50) {
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = WRITE_6;
cmd[1] = 1;
cmd[4] = 1;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: About to write pending fseq %d at fppos %d\n",
name, STp->frame_seq_number-1, STp->first_frame_position);
#endif
SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, OS_FRAME_SIZE, SCSI_DATA_WRITE,
STp->timeout, MAX_WRITE_RETRIES, TRUE);
*aSRpnt = SRpnt;
if (STp->buffer->syscall_result) { /* additional write error */
if ((SRpnt->sr_sense_buffer[ 2] & 0x0f) == 13 &&
SRpnt->sr_sense_buffer[12] == 0 &&
SRpnt->sr_sense_buffer[13] == 2) {
printk(KERN_ERR
"%s:E: Volume overflow in write error recovery\n",
name);
break; /* hit end of tape = fail */
}
flag = 1;
}
else
pending = 0;
continue;
}
if (STp->cur_frames == 0) {
#if DEBUG
debugging = dbg;
printk(OSST_DEB_MSG "%s:D: Wait re-write finished\n", name);
#endif
if (STp->first_frame_position != expected) {
printk(KERN_ERR "%s:A: Actual position %d - expected %d\n",
name, STp->first_frame_position, expected);
return (-EIO);
}
return 0;
}
#if DEBUG
if (debugging) {
printk(OSST_DEB_MSG "%s:D: Sleeping in re-write wait ready\n", name);
printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
debugging = 0;
}
#endif
schedule_timeout(HZ / 10);
}
printk(KERN_ERR "%s:E: Failed to find valid tape media\n", name);
#if DEBUG
debugging = dbg;
#endif
return (-EIO);
}
/*
* Error recovery algorithm for the OnStream tape.
*/
static int osst_write_error_recovery(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int pending)
{
Scsi_Request * SRpnt = * aSRpnt;
ST_partstat * STps = & STp->ps[STp->partition];
char * name = tape_name(STp);
int retval = 0;
int rw_state;
unsigned int frame, skip;
rw_state = STps->rw;
if ((SRpnt->sr_sense_buffer[ 2] & 0x0f) != 3
|| SRpnt->sr_sense_buffer[12] != 12
|| SRpnt->sr_sense_buffer[13] != 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Write error recovery cannot handle %02x:%02x:%02x\n", name,
SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[12], SRpnt->sr_sense_buffer[13]);
#endif
return (-EIO);
}
frame = (SRpnt->sr_sense_buffer[3] << 24) |
(SRpnt->sr_sense_buffer[4] << 16) |
(SRpnt->sr_sense_buffer[5] << 8) |
SRpnt->sr_sense_buffer[6];
skip = SRpnt->sr_sense_buffer[9];
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Detected physical bad frame at %u, advised to skip %d\n", name, frame, skip);
#endif
osst_get_frame_position(STp, aSRpnt);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: reported frame positions: host = %d, tape = %d\n",
name, STp->first_frame_position, STp->last_frame_position);
#endif
switch (STp->write_type) {
case OS_WRITE_DATA:
case OS_WRITE_EOD:
case OS_WRITE_NEW_MARK:
printk(KERN_WARNING
"%s:I: Relocating %d buffered logical frames from position %u to %u\n",
name, STp->cur_frames, frame, (frame + skip > 3000 && frame < 3000)?3000:frame + skip);
if (STp->os_fw_rev >= 10600)
retval = osst_reposition_and_retry(STp, aSRpnt, frame, skip, pending);
else
retval = osst_read_back_buffer_and_rewrite(STp, aSRpnt, frame, skip, pending);
printk(KERN_WARNING "%s:%s: %sWrite error%srecovered\n", name,
retval?"E" :"I",
retval?"" :"Don't worry, ",
retval?" not ":" ");
break;
case OS_WRITE_LAST_MARK:
printk(KERN_ERR "%s:E: Bad frame in update last marker, fatal\n", name);
osst_set_frame_position(STp, aSRpnt, frame + STp->cur_frames + pending, 0);
retval = -EIO;
break;
case OS_WRITE_HEADER:
printk(KERN_WARNING "%s:I: Bad frame in header partition, skipped\n", name);
retval = osst_read_back_buffer_and_rewrite(STp, aSRpnt, frame, 1, pending);
break;
default:
printk(KERN_INFO "%s:I: Bad frame in filler, ignored\n", name);
osst_set_frame_position(STp, aSRpnt, frame + STp->cur_frames + pending, 0);
}
osst_get_frame_position(STp, aSRpnt);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Positioning complete, cur_frames %d, pos %d, tape pos %d\n",
name, STp->cur_frames, STp->first_frame_position, STp->last_frame_position);
printk(OSST_DEB_MSG "%s:D: next logical frame to write: %d\n", name, STp->logical_blk_num);
#endif
if (retval == 0) {
STp->recover_count++;
STp->recover_erreg++;
}
STps->rw = rw_state;
return retval;
}
static int osst_space_over_filemarks_backward(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt,
int mt_op, int mt_count)
{
char * name = tape_name(STp);
int cnt;
int last_mark_ppos = -1;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reached space_over_filemarks_backwards %d %d\n", name, mt_op, mt_count);
#endif
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks_bwd\n", name);
#endif
return -EIO;
}
if (STp->linux_media_version >= 4) {
/*
* direct lookup in header filemark list
*/
cnt = ntohl(STp->buffer->aux->filemark_cnt);
if (STp->header_ok &&
STp->header_cache != NULL &&
(cnt - mt_count) >= 0 &&
(cnt - mt_count) < OS_FM_TAB_MAX &&
(cnt - mt_count) < STp->filemark_cnt &&
STp->header_cache->dat_fm_tab.fm_tab_ent[cnt-1] == STp->buffer->aux->last_mark_ppos)
last_mark_ppos = ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt - mt_count]);
#if DEBUG
if (STp->header_cache == NULL || (cnt - mt_count) < 0 || (cnt - mt_count) >= OS_FM_TAB_MAX)
printk(OSST_DEB_MSG "%s:D: Filemark lookup fail due to %s\n", name,
STp->header_cache == NULL?"lack of header cache":"count out of range");
else
printk(OSST_DEB_MSG "%s:D: Filemark lookup: prev mark %d (%s), skip %d to %d\n",
name, cnt,
((cnt == -1 && ntohl(STp->buffer->aux->last_mark_ppos) == -1) ||
(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt-1] ==
STp->buffer->aux->last_mark_ppos))?"match":"error",
mt_count, last_mark_ppos);
#endif
if (last_mark_ppos > 10 && last_mark_ppos < STp->eod_frame_ppos) {
osst_position_tape_and_confirm(STp, aSRpnt, last_mark_ppos);
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG
"%s:D: Couldn't get logical blk num in space_filemarks\n", name);
#endif
return (-EIO);
}
if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
name, last_mark_ppos);
return (-EIO);
}
goto found;
}
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reverting to scan filemark backwards\n", name);
#endif
}
cnt = 0;
while (cnt != mt_count) {
last_mark_ppos = ntohl(STp->buffer->aux->last_mark_ppos);
if (last_mark_ppos == -1)
return (-EIO);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Positioning to last mark at %d\n", name, last_mark_ppos);
#endif
osst_position_tape_and_confirm(STp, aSRpnt, last_mark_ppos);
cnt++;
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n", name);
#endif
return (-EIO);
}
if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
name, last_mark_ppos);
return (-EIO);
}
}
found:
if (mt_op == MTBSFM) {
STp->frame_seq_number++;
STp->frame_in_buffer = 0;
STp->buffer->buffer_bytes = 0;
STp->buffer->read_pointer = 0;
STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
}
return 0;
}
/*
* ADRL 1.1 compatible "slow" space filemarks fwd version
*
* Just scans for the filemark sequentially.
*/
static int osst_space_over_filemarks_forward_slow(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt,
int mt_op, int mt_count)
{
int cnt = 0;
#if DEBUG
char * name = tape_name(STp);
printk(OSST_DEB_MSG "%s:D: Reached space_over_filemarks_forward_slow %d %d\n", name, mt_op, mt_count);
#endif
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks_fwd\n", name);
#endif
return (-EIO);
}
while (1) {
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n", name);
#endif
return (-EIO);
}
if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_MARKER)
cnt++;
if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_EOD) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: space_fwd: EOD reached\n", name);
#endif
if (STp->first_frame_position > STp->eod_frame_ppos+1) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: EOD position corrected (%d=>%d)\n",
name, STp->eod_frame_ppos, STp->first_frame_position-1);
#endif
STp->eod_frame_ppos = STp->first_frame_position-1;
}
return (-EIO);
}
if (cnt == mt_count)
break;
STp->frame_in_buffer = 0;
}
if (mt_op == MTFSF) {
STp->frame_seq_number++;
STp->frame_in_buffer = 0;
STp->buffer->buffer_bytes = 0;
STp->buffer->read_pointer = 0;
STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
}
return 0;
}
/*
* Fast linux specific version of OnStream FSF
*/
static int osst_space_over_filemarks_forward_fast(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt,
int mt_op, int mt_count)
{
char * name = tape_name(STp);
int cnt = 0,
next_mark_ppos = -1;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reached space_over_filemarks_forward_fast %d %d\n", name, mt_op, mt_count);
#endif
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks_fwd\n", name);
#endif
return (-EIO);
}
if (STp->linux_media_version >= 4) {
/*
* direct lookup in header filemark list
*/
cnt = ntohl(STp->buffer->aux->filemark_cnt) - 1;
if (STp->header_ok &&
STp->header_cache != NULL &&
(cnt + mt_count) < OS_FM_TAB_MAX &&
(cnt + mt_count) < STp->filemark_cnt &&
((cnt == -1 && ntohl(STp->buffer->aux->last_mark_ppos) == -1) ||
(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt] == STp->buffer->aux->last_mark_ppos)))
next_mark_ppos = ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt + mt_count]);
#if DEBUG
if (STp->header_cache == NULL || (cnt + mt_count) >= OS_FM_TAB_MAX)
printk(OSST_DEB_MSG "%s:D: Filemark lookup fail due to %s\n", name,
STp->header_cache == NULL?"lack of header cache":"count out of range");
else
printk(OSST_DEB_MSG "%s:D: Filemark lookup: prev mark %d (%s), skip %d to %d\n",
name, cnt,
((cnt == -1 && ntohl(STp->buffer->aux->last_mark_ppos) == -1) ||
(STp->header_cache->dat_fm_tab.fm_tab_ent[cnt] ==
STp->buffer->aux->last_mark_ppos))?"match":"error",
mt_count, next_mark_ppos);
#endif
if (next_mark_ppos <= 10 || next_mark_ppos > STp->eod_frame_ppos) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reverting to slow filemark space\n", name);
#endif
return osst_space_over_filemarks_forward_slow(STp, aSRpnt, mt_op, mt_count);
} else {
osst_position_tape_and_confirm(STp, aSRpnt, next_mark_ppos);
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n",
name);
#endif
return (-EIO);
}
if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
name, next_mark_ppos);
return (-EIO);
}
if (ntohl(STp->buffer->aux->filemark_cnt) != cnt + mt_count) {
printk(KERN_WARNING "%s:W: Expected to find marker %d at ppos %d, not %d\n",
name, cnt+mt_count, next_mark_ppos,
ntohl(STp->buffer->aux->filemark_cnt));
return (-EIO);
}
}
} else {
/*
* Find nearest (usually previous) marker, then jump from marker to marker
*/
while (1) {
if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_MARKER)
break;
if (STp->buffer->aux->frame_type == OS_FRAME_TYPE_EOD) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: space_fwd: EOD reached\n", name);
#endif
return (-EIO);
}
if (ntohl(STp->buffer->aux->filemark_cnt) == 0) {
if (STp->first_mark_ppos == -1) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reverting to slow filemark space\n", name);
#endif
return osst_space_over_filemarks_forward_slow(STp, aSRpnt, mt_op, mt_count);
}
osst_position_tape_and_confirm(STp, aSRpnt, STp->first_mark_ppos);
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG
"%s:D: Couldn't get logical blk num in space_filemarks_fwd_fast\n",
name);
#endif
return (-EIO);
}
if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
printk(KERN_WARNING "%s:W: Expected to find filemark at %d\n",
name, STp->first_mark_ppos);
return (-EIO);
}
} else {
if (osst_space_over_filemarks_backward(STp, aSRpnt, MTBSF, 1) < 0)
return (-EIO);
mt_count++;
}
}
cnt++;
while (cnt != mt_count) {
next_mark_ppos = ntohl(STp->buffer->aux->next_mark_ppos);
if (!next_mark_ppos || next_mark_ppos > STp->eod_frame_ppos) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reverting to slow filemark space\n", name);
#endif
return osst_space_over_filemarks_forward_slow(STp, aSRpnt, mt_op, mt_count - cnt);
}
#if DEBUG
else printk(OSST_DEB_MSG "%s:D: Positioning to next mark at %d\n", name, next_mark_ppos);
#endif
osst_position_tape_and_confirm(STp, aSRpnt, next_mark_ppos);
cnt++;
if (osst_get_logical_frame(STp, aSRpnt, -1, 0) < 0) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't get logical blk num in space_filemarks\n",
name);
#endif
return (-EIO);
}
if (STp->buffer->aux->frame_type != OS_FRAME_TYPE_MARKER) {
printk(KERN_WARNING "%s:W: Expected to find marker at ppos %d, not found\n",
name, next_mark_ppos);
return (-EIO);
}
}
}
if (mt_op == MTFSF) {
STp->frame_seq_number++;
STp->frame_in_buffer = 0;
STp->buffer->buffer_bytes = 0;
STp->buffer->read_pointer = 0;
STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
}
return 0;
}
/*
* In debug mode, we want to see as many errors as possible
* to test the error recovery mechanism.
*/
#if DEBUG
static void osst_set_retries(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int retries)
{
unsigned char cmd[MAX_COMMAND_SIZE];
Scsi_Request * SRpnt = * aSRpnt;
char * name = tape_name(STp);
memset(cmd, 0, MAX_COMMAND_SIZE);
cmd[0] = MODE_SELECT;
cmd[1] = 0x10;
cmd[4] = NUMBER_RETRIES_PAGE_LENGTH + MODE_HEADER_LENGTH;
(STp->buffer)->b_data[0] = cmd[4] - 1;
(STp->buffer)->b_data[1] = 0; /* Medium Type - ignoring */
(STp->buffer)->b_data[2] = 0; /* Reserved */
(STp->buffer)->b_data[3] = 0; /* Block Descriptor Length */
(STp->buffer)->b_data[MODE_HEADER_LENGTH + 0] = NUMBER_RETRIES_PAGE | (1 << 7);
(STp->buffer)->b_data[MODE_HEADER_LENGTH + 1] = 2;
(STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] = 4;
(STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] = retries;
if (debugging)
printk(OSST_DEB_MSG "%s:D: Setting number of retries on OnStream tape to %d\n", name, retries);
SRpnt = osst_do_scsi(SRpnt, STp, cmd, cmd[4], SCSI_DATA_WRITE, STp->timeout, 0, TRUE);
*aSRpnt = SRpnt;
if ((STp->buffer)->syscall_result)
printk (KERN_ERR "%s:D: Couldn't set retries to %d\n", name, retries);
}
#endif
static int osst_write_filemark(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
int result;
int this_mark_ppos = STp->first_frame_position;
int this_mark_lbn = STp->logical_blk_num;
#if DEBUG
char * name = tape_name(STp);
#endif
if (STp->raw) return 0;
STp->write_type = OS_WRITE_NEW_MARK;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Writing Filemark %i at fppos %d (fseq %d, lblk %d)\n",
name, STp->filemark_cnt, this_mark_ppos, STp->frame_seq_number, this_mark_lbn);
#endif
STp->dirty = 1;
result = osst_flush_write_buffer(STp, aSRpnt);
result |= osst_flush_drive_buffer(STp, aSRpnt);
STp->last_mark_ppos = this_mark_ppos;
STp->last_mark_lbn = this_mark_lbn;
if (STp->header_cache != NULL && STp->filemark_cnt < OS_FM_TAB_MAX)
STp->header_cache->dat_fm_tab.fm_tab_ent[STp->filemark_cnt] = htonl(this_mark_ppos);
if (STp->filemark_cnt++ == 0)
STp->first_mark_ppos = this_mark_ppos;
return result;
}
static int osst_write_eod(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
int result;
#if DEBUG
char * name = tape_name(STp);
#endif
if (STp->raw) return 0;
STp->write_type = OS_WRITE_EOD;
STp->eod_frame_ppos = STp->first_frame_position;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Writing EOD at fppos %d (fseq %d, lblk %d)\n", name,
STp->eod_frame_ppos, STp->frame_seq_number, STp->logical_blk_num);
#endif
STp->dirty = 1;
result = osst_flush_write_buffer(STp, aSRpnt);
result |= osst_flush_drive_buffer(STp, aSRpnt);
STp->eod_frame_lfa = --(STp->frame_seq_number);
return result;
}
static int osst_write_filler(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int where, int count)
{
char * name = tape_name(STp);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reached onstream write filler group %d\n", name, where);
#endif
osst_wait_ready(STp, aSRpnt, 60 * 5, 0);
osst_set_frame_position(STp, aSRpnt, where, 0);
STp->write_type = OS_WRITE_FILLER;
while (count--) {
memcpy(STp->buffer->b_data, "Filler", 6);
STp->buffer->buffer_bytes = 6;
STp->dirty = 1;
if (osst_flush_write_buffer(STp, aSRpnt)) {
printk(KERN_INFO "%s:I: Couldn't write filler frame\n", name);
return (-EIO);
}
}
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Exiting onstream write filler group\n", name);
#endif
return osst_flush_drive_buffer(STp, aSRpnt);
}
static int __osst_write_header(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int where, int count)
{
char * name = tape_name(STp);
int result;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reached onstream write header group %d\n", name, where);
#endif
osst_wait_ready(STp, aSRpnt, 60 * 5, 0);
osst_set_frame_position(STp, aSRpnt, where, 0);
STp->write_type = OS_WRITE_HEADER;
while (count--) {
osst_copy_to_buffer(STp->buffer, (unsigned char *)STp->header_cache);
STp->buffer->buffer_bytes = sizeof(os_header_t);
STp->dirty = 1;
if (osst_flush_write_buffer(STp, aSRpnt)) {
printk(KERN_INFO "%s:I: Couldn't write header frame\n", name);
return (-EIO);
}
}
result = osst_flush_drive_buffer(STp, aSRpnt);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Write onstream header group %s\n", name, result?"failed":"done");
#endif
return result;
}
static int osst_write_header(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int locate_eod)
{
os_header_t * header;
int result;
char * name = tape_name(STp);
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Writing tape header\n", name);
#endif
if (STp->raw) return 0;
if (STp->header_cache == NULL) {
if ((STp->header_cache = (os_header_t *)vmalloc(sizeof(os_header_t))) == NULL) {
printk(KERN_ERR "%s:E: Failed to allocate header cache\n", name);
return (-ENOMEM);
}
memset(STp->header_cache, 0, sizeof(os_header_t));
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Allocated and cleared memory for header cache\n", name);
#endif
}
if (STp->header_ok) STp->update_frame_cntr++;
else STp->update_frame_cntr = 0;
header = STp->header_cache;
strcpy(header->ident_str, "ADR_SEQ");
header->major_rev = 1;
header->minor_rev = 4;
header->ext_trk_tb_off = htons(17192);
header->pt_par_num = 1;
header->partition[0].partition_num = OS_DATA_PARTITION;
header->partition[0].par_desc_ver = OS_PARTITION_VERSION;
header->partition[0].wrt_pass_cntr = htons(STp->wrt_pass_cntr);
header->partition[0].first_frame_ppos = htonl(STp->first_data_ppos);
header->partition[0].last_frame_ppos = htonl(STp->capacity);
header->partition[0].eod_frame_ppos = htonl(STp->eod_frame_ppos);
header->cfg_col_width = htonl(20);
header->dat_col_width = htonl(1500);
header->qfa_col_width = htonl(0);
header->ext_track_tb.nr_stream_part = 1;
header->ext_track_tb.et_ent_sz = 32;
header->ext_track_tb.dat_ext_trk_ey.et_part_num = 0;
header->ext_track_tb.dat_ext_trk_ey.fmt = 1;
header->ext_track_tb.dat_ext_trk_ey.fm_tab_off = htons(17736);
header->ext_track_tb.dat_ext_trk_ey.last_hlb_hi = 0;
header->ext_track_tb.dat_ext_trk_ey.last_hlb = htonl(STp->eod_frame_lfa);
header->ext_track_tb.dat_ext_trk_ey.last_pp = htonl(STp->eod_frame_ppos);
header->dat_fm_tab.fm_part_num = 0;
header->dat_fm_tab.fm_tab_ent_sz = 4;
header->dat_fm_tab.fm_tab_ent_cnt = htons(STp->filemark_cnt<OS_FM_TAB_MAX?
STp->filemark_cnt:OS_FM_TAB_MAX);
result = __osst_write_header(STp, aSRpnt, 0xbae, 5);
if (STp->update_frame_cntr == 0)
osst_write_filler(STp, aSRpnt, 0xbb3, 5);
result &= __osst_write_header(STp, aSRpnt, 5, 5);
if (locate_eod) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Locating back to eod frame addr %d\n", name, STp->eod_frame_ppos);
#endif
osst_set_frame_position(STp, aSRpnt, STp->eod_frame_ppos, 0);
}
if (result)
printk(KERN_ERR "%s:E: Write header failed\n", name);
else {
memcpy(STp->application_sig, "LIN4", 4);
STp->linux_media = 1;
STp->linux_media_version = 4;
STp->header_ok = 1;
}
return result;
}
static int osst_reset_header(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
if (STp->header_cache != NULL)
memset(STp->header_cache, 0, sizeof(os_header_t));
STp->logical_blk_num = STp->frame_seq_number = 0;
STp->frame_in_buffer = 0;
STp->eod_frame_ppos = STp->first_data_ppos = 0x0000000A;
STp->filemark_cnt = 0;
STp->first_mark_ppos = STp->last_mark_ppos = STp->last_mark_lbn = -1;
return osst_write_header(STp, aSRpnt, 1);
}
static int __osst_analyze_headers(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt, int ppos)
{
char * name = tape_name(STp);
os_header_t * header;
os_aux_t * aux;
char id_string[8];
int linux_media_version,
update_frame_cntr;
if (STp->raw)
return 1;
if (ppos == 5 || ppos == 0xbae || STp->buffer->syscall_result) {
if (osst_set_frame_position(STp, aSRpnt, ppos, 0))
printk(KERN_WARNING "%s:W: Couldn't position tape\n", name);
if (osst_initiate_read (STp, aSRpnt)) {
printk(KERN_WARNING "%s:W: Couldn't initiate read\n", name);
return 0;
}
}
if (osst_read_frame(STp, aSRpnt, 180)) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Couldn't read header frame\n", name);
#endif
return 0;
}
header = (os_header_t *) STp->buffer->b_data; /* warning: only first segment addressable */
aux = STp->buffer->aux;
if (aux->frame_type != OS_FRAME_TYPE_HEADER) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping non-header frame (%d)\n", name, ppos);
#endif
return 0;
}
if (ntohl(aux->frame_seq_num) != 0 ||
ntohl(aux->logical_blk_num) != 0 ||
aux->partition.partition_num != OS_CONFIG_PARTITION ||
ntohl(aux->partition.first_frame_ppos) != 0 ||
ntohl(aux->partition.last_frame_ppos) != 0xbb7 ) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Invalid header frame (%d,%d,%d,%d,%d)\n", name,
ntohl(aux->frame_seq_num), ntohl(aux->logical_blk_num),
aux->partition.partition_num, ntohl(aux->partition.first_frame_ppos),
ntohl(aux->partition.last_frame_ppos));
#endif
return 0;
}
if (strncmp(header->ident_str, "ADR_SEQ", 7) != 0 &&
strncmp(header->ident_str, "ADR-SEQ", 7) != 0) {
strncpy(id_string, header->ident_str, 7);
id_string[7] = 0;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Invalid header identification string %s\n", name, id_string);
#endif
return 0;
}
update_frame_cntr = ntohl(aux->update_frame_cntr);
if (update_frame_cntr < STp->update_frame_cntr) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame %d with update_frame_counter %d<%d\n",
name, ppos, update_frame_cntr, STp->update_frame_cntr);
#endif
return 0;
}
if (header->major_rev != 1 || header->minor_rev != 4 ) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: %s revision %d.%d detected (1.4 supported)\n",
name, (header->major_rev != 1 || header->minor_rev < 2 ||
header->minor_rev > 4 )? "Invalid" : "Warning:",
header->major_rev, header->minor_rev);
#endif
if (header->major_rev != 1 || header->minor_rev < 2 || header->minor_rev > 4)
return 0;
}
#if DEBUG
if (header->pt_par_num != 1)
printk(KERN_INFO "%s:W: %d partitions defined, only one supported\n",
name, header->pt_par_num);
#endif
memcpy(id_string, aux->application_sig, 4);
id_string[4] = 0;
if (memcmp(id_string, "LIN", 3) == 0) {
STp->linux_media = 1;
linux_media_version = id_string[3] - '0';
if (linux_media_version != 4)
printk(KERN_INFO "%s:I: Linux media version %d detected (current 4)\n",
name, linux_media_version);
} else {
printk(KERN_WARNING "%s:W: Non Linux media detected (%s)\n", name, id_string);
return 0;
}
if (linux_media_version < STp->linux_media_version) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Skipping frame %d with linux_media_version %d\n",
name, ppos, linux_media_version);
#endif
return 0;
}
if (linux_media_version > STp->linux_media_version) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Frame %d sets linux_media_version to %d\n",
name, ppos, linux_media_version);
#endif
memcpy(STp->application_sig, id_string, 5);
STp->linux_media_version = linux_media_version;
STp->update_frame_cntr = -1;
}
if (update_frame_cntr > STp->update_frame_cntr) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Frame %d sets update_frame_counter to %d\n",
name, ppos, update_frame_cntr);
#endif
if (STp->header_cache == NULL) {
if ((STp->header_cache = (os_header_t *)vmalloc(sizeof(os_header_t))) == NULL) {
printk(KERN_ERR "%s:E: Failed to allocate header cache\n", name);
return 0;
}
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Allocated memory for header cache\n", name);
#endif
}
osst_copy_from_buffer(STp->buffer, (unsigned char *)STp->header_cache);
header = STp->header_cache; /* further accesses from cached (full) copy */
STp->wrt_pass_cntr = ntohs(header->partition[0].wrt_pass_cntr);
STp->first_data_ppos = ntohl(header->partition[0].first_frame_ppos);
STp->eod_frame_ppos = ntohl(header->partition[0].eod_frame_ppos);
STp->eod_frame_lfa = ntohl(header->ext_track_tb.dat_ext_trk_ey.last_hlb);
STp->filemark_cnt = ntohl(aux->filemark_cnt);
STp->first_mark_ppos = ntohl(aux->next_mark_ppos);
STp->last_mark_ppos = ntohl(aux->last_mark_ppos);
STp->last_mark_lbn = ntohl(aux->last_mark_lbn);
STp->update_frame_cntr = update_frame_cntr;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Detected write pass %d, update frame counter %d, filemark counter %d\n",
name, STp->wrt_pass_cntr, STp->update_frame_cntr, STp->filemark_cnt);
printk(OSST_DEB_MSG "%s:D: first data frame on tape = %d, last = %d, eod frame = %d\n", name,
STp->first_data_ppos,
ntohl(header->partition[0].last_frame_ppos),
ntohl(header->partition[0].eod_frame_ppos));
printk(OSST_DEB_MSG "%s:D: first mark on tape = %d, last = %d, eod frame = %d\n",
name, STp->first_mark_ppos, STp->last_mark_ppos, STp->eod_frame_ppos);
#endif
if (header->minor_rev < 4 && STp->linux_media_version == 4) {
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Moving filemark list to ADR 1.4 location\n", name);
#endif
memcpy((void *)header->dat_fm_tab.fm_tab_ent,
(void *)header->old_filemark_list, sizeof(header->dat_fm_tab.fm_tab_ent));
memset((void *)header->old_filemark_list, 0, sizeof(header->old_filemark_list));
}
if (header->minor_rev == 4 &&
(header->ext_trk_tb_off != htons(17192) ||
header->partition[0].partition_num != OS_DATA_PARTITION ||
header->partition[0].par_desc_ver != OS_PARTITION_VERSION ||
header->partition[0].last_frame_ppos != htonl(STp->capacity) ||
header->cfg_col_width != htonl(20) ||
header->dat_col_width != htonl(1500) ||
header->qfa_col_width != htonl(0) ||
header->ext_track_tb.nr_stream_part != 1 ||
header->ext_track_tb.et_ent_sz != 32 ||
header->ext_track_tb.dat_ext_trk_ey.et_part_num != OS_DATA_PARTITION ||
header->ext_track_tb.dat_ext_trk_ey.fmt != 1 ||
header->ext_track_tb.dat_ext_trk_ey.fm_tab_off != htons(17736) ||
header->ext_track_tb.dat_ext_trk_ey.last_hlb_hi != 0 ||
header->ext_track_tb.dat_ext_trk_ey.last_pp != htonl(STp->eod_frame_ppos) ||
header->dat_fm_tab.fm_part_num != OS_DATA_PARTITION ||
header->dat_fm_tab.fm_tab_ent_sz != 4 ||
header->dat_fm_tab.fm_tab_ent_cnt !=
htons(STp->filemark_cnt<OS_FM_TAB_MAX?STp->filemark_cnt:OS_FM_TAB_MAX)))
printk(KERN_WARNING "%s:W: Failed consistency check ADR 1.4 format\n", name);
}
return 1;
}
static int osst_analyze_headers(OS_Scsi_Tape * STp, Scsi_Request ** aSRpnt)
{
int position, ppos;
int first, last;
int valid = 0;
char * name = tape_name(STp);
position = osst_get_frame_position(STp, aSRpnt);
if (STp->raw) {
STp->header_ok = STp->linux_media = 1;
STp->linux_media_version = 0;
return 1;
}
STp->header_ok = STp->linux_media = STp->linux_media_version = 0;
STp->wrt_pass_cntr = STp->update_frame_cntr = -1;
STp->eod_frame_ppos = STp->first_data_ppos = -1;
STp->first_mark_ppos = STp->last_mark_ppos = STp->last_mark_lbn = -1;
#if DEBUG
printk(OSST_DEB_MSG "%s:D: Reading header\n", name);
#endif
/* optimization for speed - if we are positioned at ppos 10, read second group first */
/* TODO try the ADR 1.1 locations for the second group if we have no valid one yet... */
first = position==10?0xbae: 5;
last = position==10?0xbb3:10;
for (ppos = first; ppos < last; ppos++)
if (__osst_analyze_headers(STp, aSRpnt, ppos))
valid = 1;
first = position==10? 5:0xbae;
last = position==10?10:0xbb3;
for (ppos = first; ppos < last; ppos++)
if (__osst_analyze_headers(STp, aSRpnt, ppos))
valid = 1;
if (!valid) {
printk(KERN_ERR "%s:E: Failed to find valid ADRL header, new media?\n", name);