cregit-Linux how code gets into the kernel

Release 4.11 drivers/scsi/st.c

Directory: drivers/scsi
/*
   SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
   file Documentation/scsi/st.txt for more information.

   History:
   Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
   Contribution and ideas from several people including (in alphabetical
   order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk,
   Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky,
   Michael Schaefer, J"org Weule, and Eric Youngdale.

   Copyright 1992 - 2016 Kai Makisara
   email Kai.Makisara@kolumbus.fi

   Some small formal changes - aeb, 950809

   Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
 */


static const char *verstr = "20160209";

#include <linux/module.h>

#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/sched/signal.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/mtio.h>
#include <linux/cdrom.h>
#include <linux/ioctl.h>
#include <linux/fcntl.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <linux/idr.h>
#include <linux/delay.h>
#include <linux/mutex.h>

#include <linux/uaccess.h>
#include <asm/dma.h>

#include <scsi/scsi.h>
#include <scsi/scsi_dbg.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>


/* The driver prints some debugging information on the console if DEBUG
   is defined and non-zero. */

#define DEBUG 1

#define NO_DEBUG 0


#define ST_DEB_MSG  KERN_NOTICE
#if DEBUG
/* 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 DEB(a) a

#define DEBC(a) if (debugging) { a ; }
#else

#define DEB(a)

#define DEBC(a)
#endif


#define ST_KILOBYTE 1024

#include "st_options.h"
#include "st.h"


static int buffer_kbs;

static int max_sg_segs;

static int try_direct_io = TRY_DIRECT_IO;

static int try_rdio = 1;

static int try_wdio = 1;

static int debug_flag;


static struct class st_sysfs_class;

static const struct attribute_group *st_dev_groups[];

static const struct attribute_group *st_drv_groups[];

MODULE_AUTHOR("Kai Makisara");
MODULE_DESCRIPTION("SCSI tape (st) driver");
MODULE_LICENSE("GPL");

MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);

MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);

/* Set 'perm' (4th argument) to 0 to disable module_param's definition
 * of sysfs parameters (which module_param doesn't yet support).
 * Sysfs parameters defined explicitly later.
 */
module_param_named(buffer_kbs, buffer_kbs, int, 0);
MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
module_param_named(max_sg_segs, max_sg_segs, int, 0);
MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
module_param_named(try_direct_io, try_direct_io, int, 0);
MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
module_param_named(debug_flag, debug_flag, int, 0);
MODULE_PARM_DESC(debug_flag, "Enable DEBUG, same as setting debugging=1");


/* Extra parameters for testing */
module_param_named(try_rdio, try_rdio, int, 0);
MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
module_param_named(try_wdio, try_wdio, int, 0);
MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");

#ifndef MODULE

static int write_threshold_kbs;  
/* retained for compatibility */

static struct st_dev_parm {
	
char *name;
	
int *val;
} 
parms[] __initdata = {
	{
		"buffer_kbs", &buffer_kbs
	},
	{       /* Retained for compatibility with 2.4 */
		"write_threshold_kbs", &write_threshold_kbs
	},
	{
		"max_sg_segs", NULL
	},
	{
		"try_direct_io", &try_direct_io
	},
	{
		"debug_flag", &debug_flag
	}
};
#endif

/* Restrict the number of modes so that names for all are assigned */
#if ST_NBR_MODES > 16
#error "Maximum number of modes is 16"
#endif
/* Bit reversed order to get same names for same minors with all
   mode counts */

static const char *st_formats[] = {
	"",  "r", "k", "s", "l", "t", "o", "u",
	"m", "v", "p", "x", "a", "y", "q", "z"}; 

/* The default definitions have been moved to st_options.h */


#define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)

/* The buffer size should fit into the 24 bits for length in the
   6-byte SCSI read and write commands. */
#if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
#error "Buffer size should not exceed (2 << 24 - 1) bytes!"
#endif


static int debugging = DEBUG;


#define MAX_RETRIES 0

#define MAX_WRITE_RETRIES 0

#define MAX_READY_RETRIES 0

#define NO_TAPE  NOT_READY


#define ST_TIMEOUT (900 * HZ)

#define ST_LONG_TIMEOUT (14000 * HZ)

/* Remove mode bits and auto-rewind bit (7) */

#define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
    (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )

#define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)

/* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */

#define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
  (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )

/* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
   24 bits) */

#define SET_DENS_AND_BLK 0x10001


static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;

static int st_max_sg_segs = ST_MAX_SG;


static int modes_defined;

static int enlarge_buffer(struct st_buffer *, int, int);
static void clear_buffer(struct st_buffer *);
static void normalize_buffer(struct st_buffer *);
static int append_to_buffer(const char __user *, struct st_buffer *, int);
static int from_buffer(struct st_buffer *, char __user *, int);
static void move_buffer_data(struct st_buffer *, int);

static int sgl_map_user_pages(struct st_buffer *, const unsigned int,
			      unsigned long, size_t, int);
static int sgl_unmap_user_pages(struct st_buffer *, const unsigned int, int);

static int st_probe(struct device *);
static int st_remove(struct device *);


static struct scsi_driver st_template = {
	.gendrv = {
		.name		= "st",
		.owner		= THIS_MODULE,
		.probe		= st_probe,
		.remove		= st_remove,
		.groups		= st_drv_groups,
        },
};

static int st_compression(struct scsi_tape *, int);

static int find_partition(struct scsi_tape *);
static int switch_partition(struct scsi_tape *);

static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);

static void scsi_tape_release(struct kref *);


#define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)

static DEFINE_MUTEX(st_ref_mutex);
static DEFINE_SPINLOCK(st_index_lock);
static DEFINE_SPINLOCK(st_use_lock);
static DEFINE_IDR(st_index_idr);



#include "osst_detect.h"
#ifndef SIGS_FROM_OSST

#define SIGS_FROM_OSST \
	{"OnStream", "SC-", "", "osst"}, \
        {"OnStream", "DI-", "", "osst"}, \
        {"OnStream", "DP-", "", "osst"}, \
        {"OnStream", "USB", "", "osst"}, \
        {"OnStream", "FW-", "", "osst"}
#endif


static struct scsi_tape *scsi_tape_get(int dev) { struct scsi_tape *STp = NULL; mutex_lock(&st_ref_mutex); spin_lock(&st_index_lock); STp = idr_find(&st_index_idr, dev); if (!STp) goto out; kref_get(&STp->kref); if (!STp->device) goto out_put; if (scsi_device_get(STp->device)) goto out_put; goto out; out_put: kref_put(&STp->kref, scsi_tape_release); STp = NULL; out: spin_unlock(&st_index_lock); mutex_unlock(&st_ref_mutex); return STp; }

Contributors

PersonTokensPropCommitsCommitProp
Kai Mäkisara10087.72%133.33%
Jeff Mahoney108.77%133.33%
Arjan van de Ven43.51%133.33%
Total114100.00%3100.00%


static void scsi_tape_put(struct scsi_tape *STp) { struct scsi_device *sdev = STp->device; mutex_lock(&st_ref_mutex); kref_put(&STp->kref, scsi_tape_release); scsi_device_put(sdev); mutex_unlock(&st_ref_mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Kai Mäkisara4391.49%150.00%
Arjan van de Ven48.51%150.00%
Total47100.00%2100.00%

struct st_reject_data { char *vendor; char *model; char *rev; char *driver_hint; /* Name of the correct driver, NULL if unknown */ }; static struct st_reject_data reject_list[] = { /* {"XXX", "Yy-", "", NULL}, example */ SIGS_FROM_OSST, {NULL, }}; /* If the device signature is on the list of incompatible drives, the function returns a pointer to the name of the correct driver (if known) */
static char * st_incompatible(struct scsi_device* SDp) { struct st_reject_data *rp; for (rp=&(reject_list[0]); rp->vendor != NULL; rp++) if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) && !strncmp(rp->model, SDp->model, strlen(rp->model)) && !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) { if (rp->driver_hint) return rp->driver_hint; else return "unknown"; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)10690.60%133.33%
Anton Blanchard97.69%133.33%
Kai Mäkisara21.71%133.33%
Total117100.00%3100.00%


static inline char *tape_name(struct scsi_tape *tape) { return tape->disk->disk_name; }

Contributors

PersonTokensPropCommitsCommitProp
Al Viro1890.00%266.67%
Kai Mäkisara210.00%133.33%
Total20100.00%3100.00%

#define st_printk(prefix, t, fmt, a...) \ sdev_prefix_printk(prefix, (t)->device, tape_name(t), fmt, ##a) #ifdef DEBUG #define DEBC_printk(t, fmt, a...) \ if (debugging) { st_printk(ST_DEB_MSG, t, fmt, ##a ); } #else #define DEBC_printk(t, fmt, a...) #endif
static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s) { const u8 *ucp; const u8 *sense = SRpnt->sense; s->have_sense = scsi_normalize_sense(SRpnt->sense, SCSI_SENSE_BUFFERSIZE, &s->sense_hdr); s->flags = 0; if (s->have_sense) { s->deferred = 0; s->remainder_valid = scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64); switch (sense[0] & 0x7f) { case 0x71: s->deferred = 1; case 0x70: s->fixed_format = 1; s->flags = sense[2] & 0xe0; break; case 0x73: s->deferred = 1; case 0x72: s->fixed_format = 0; ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4); s->flags = ucp ? (ucp[3] & 0xe0) : 0; break; } } }

Contributors

PersonTokensPropCommitsCommitProp
Kai Mäkisara16595.93%266.67%
Mike Christie74.07%133.33%
Total172100.00%3100.00%

/* Convert the result to success code */
static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt) { int result = SRpnt->result; u8 scode; DEB(const char *stp;) char *name = tape_name(STp); struct st_cmdstatus *cmdstatp; if (!result) return 0; cmdstatp = &STp->buffer->cmdstat; st_analyze_sense(SRpnt, cmdstatp); if (cmdstatp->have_sense) scode = STp->buffer->cmdstat.sense_hdr.sense_key; else scode = 0; DEB( if (debugging) { st_printk(ST_DEB_MSG, STp, "Error: %x, cmd: %x %x %x %x %x %x\n", result, SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2], SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]); if (cmdstatp->have_sense) __scsi_print_sense(STp->device, name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE); } ) /* end DEB */ if (!debugging) { /* Abnormal conditions for tape */ if (!cmdstatp->have_sense) st_printk(KERN_WARNING, STp, "Error %x (driver bt 0x%x, host bt 0x%x).\n", result, driver_byte(result), host_byte(result)); else if (cmdstatp->have_sense && scode != NO_SENSE && scode != RECOVERED_ERROR && /* scode != UNIT_ATTENTION && */ scode != BLANK_CHECK && scode != VOLUME_OVERFLOW && SRpnt->cmd[0] != MODE_SENSE && SRpnt->cmd[0] != TEST_UNIT_READY) { __scsi_print_sense(STp->device, name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE); } } if (cmdstatp->fixed_format && STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */ if (STp->cln_sense_value) STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] & STp->cln_sense_mask) == STp->cln_sense_value); else STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] & STp->cln_sense_mask) != 0); } if (cmdstatp->have_sense && cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17) STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */ STp->pos_unknown |= STp->device->was_reset; if (cmdstatp->have_sense && scode == RECOVERED_ERROR #if ST_RECOVERED_WRITE_FATAL && SRpnt->cmd[0] != WRITE_6 && SRpnt->cmd[0] != WRITE_FILEMARKS #endif ) { STp->recover_count++; STp->recover_reg++; DEB( if (debugging) { if (SRpnt->cmd[0] == READ_6) stp = "read"; else if (SRpnt->cmd[0] == WRITE_6) stp = "write"; else stp = "ioctl"; st_printk(ST_DEB_MSG, STp, "Recovered %s error (%d).\n", stp, STp->recover_count); } ) /* end DEB */ if (cmdstatp->flags == 0) return 0; } return (-EIO); }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)15739.95%1657.14%
Kai Mäkisara10326.21%517.86%
Linus Torvalds7719.59%13.57%
Mike Christie194.83%13.57%
Hannes Reinecke164.07%27.14%
Anton Blanchard112.80%13.57%
Al Viro92.29%13.57%
Luben Tuikov10.25%13.57%
Total393100.00%28100.00%


static struct st_request *st_allocate_request(struct scsi_tape *stp) { struct st_request *streq; streq = kzalloc(sizeof(*streq), GFP_KERNEL); if (streq) streq->stp = stp; else { st_printk(KERN_ERR, stp, "Can't get SCSI request.\n"); if (signal_pending(current)) stp->buffer->syscall_result = -EINTR; else stp->buffer->syscall_result = -EBUSY; } return streq; }

Contributors

PersonTokensPropCommitsCommitProp
FUJITA Tomonori5769.51%133.33%
Mike Christie1821.95%133.33%
Hannes Reinecke78.54%133.33%
Total82100.00%3100.00%


static void st_release_request(struct st_request *streq) { kfree(streq); }

Contributors

PersonTokensPropCommitsCommitProp
Mike Christie16100.00%1100.00%
Total16100.00%1100.00%


static void st_do_stats(struct scsi_tape *STp, struct request *req) { ktime_t now; now = ktime_get(); if (scsi_req(req)->cmd[0] == WRITE_6) { now = ktime_sub(now, STp->stats->write_time); atomic64_add(ktime_to_ns(now), &STp->stats->tot_write_time); atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time); atomic64_inc(&STp->stats->write_cnt); if (req->errors) { atomic64_add(atomic_read(&STp->stats->last_write_size) - STp->buffer->cmdstat.residual, &STp->stats->write_byte_cnt); if (STp->buffer->cmdstat.residual > 0) atomic64_inc(&STp->stats->resid_cnt); } else atomic64_add(atomic_read(&STp->stats->last_write_size), &STp->stats->write_byte_cnt); } else if (scsi_req(req)->cmd[0] == READ_6) { now = ktime_sub(now, STp->stats->read_time); atomic64_add(ktime_to_ns(now), &STp->stats->tot_read_time); atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time); atomic64_inc(&STp->stats->read_cnt); if (req->errors) { atomic64_add(atomic_read(&STp->stats->last_read_size) - STp->buffer->cmdstat.residual, &STp->stats->read_byte_cnt); if (STp->buffer->cmdstat.residual > 0) atomic64_inc(&STp->stats->resid_cnt); } else atomic64_add(atomic_read(&STp->stats->last_read_size), &STp->stats->read_byte_cnt); } else { now = ktime_sub(now, STp->stats->other_time); atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time); atomic64_inc(&STp->stats->other_cnt); } atomic64_dec(&STp->stats->in_flight); }

Contributors

PersonTokensPropCommitsCommitProp
Shane M Seymour26671.51%116.67%
FUJITA Tomonori9525.54%116.67%
Christoph Hellwig61.61%116.67%
Jens Axboe20.54%116.67%
Petr Uzel20.54%116.67%
Joe Lawrence10.27%116.67%
Total372100.00%6100.00%


static void st_scsi_execute_end(struct request *req, int uptodate) { struct st_request *SRpnt = req->end_io_data; struct scsi_request *rq = scsi_req(req); struct scsi_tape *STp = SRpnt->stp; struct bio *tmp; STp->buffer->cmdstat.midlevel_result = SRpnt->result = req->errors; STp->buffer->cmdstat.residual = rq->resid_len; st_do_stats(STp, req); tmp = SRpnt->bio; if (rq->sense_len) memcpy(SRpnt->sense, rq->sense, SCSI_SENSE_BUFFERSIZE); if (SRpnt->waiting) complete(SRpnt->waiting); blk_rq_unmap_user(tmp); __blk_put_request(req->q, req); }

Contributors

PersonTokensPropCommitsCommitProp
Shane M Seymour5843.28%17.69%
Christoph Hellwig3022.39%17.69%
Kai Mäkisara2317.16%215.38%
Linus Torvalds (pre-git)1410.45%753.85%
FUJITA Tomonori96.72%215.38%
Total134100.00%13100.00%


static int st_scsi_execute(struct st_request *SRpnt, const unsigned char *cmd, int data_direction, void *buffer, unsigned bufflen, int timeout, int retries) { struct request *req; struct scsi_request *rq; struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data; int err = 0; struct scsi_tape *STp = SRpnt->stp; req = blk_get_request(SRpnt->stp->device->request_queue, data_direction == DMA_TO_DEVICE ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, GFP_KERNEL); if (IS_ERR(req)) return DRIVER_ERROR << 24; rq = scsi_req(req); scsi_req_init(req); req->rq_flags |= RQF_QUIET; mdata->null_mapped = 1; if (bufflen) { err = blk_rq_map_user(req->q, req, mdata, NULL, bufflen, GFP_KERNEL); if (err) { blk_put_request(req); return DRIVER_ERROR << 24; } } atomic64_inc(&STp->stats->in_flight); if (cmd[0] == WRITE_6) { atomic_set(&STp->stats->last_write_size, bufflen); STp->stats->write_time = ktime_get(); } else if (cmd[0] == READ_6) { atomic_set(&STp->stats->last_read_size, bufflen); STp->stats->read_time = ktime_get(); } else { STp->stats->other_time = ktime_get(); } SRpnt->bio = req->bio; rq->cmd_len = COMMAND_SIZE(cmd[0]); memset(rq->cmd, 0, BLK_MAX_CDB); memcpy(rq->cmd, cmd, rq->cmd_len); req->timeout = timeout; req->retries = retries; req->end_io_data = SRpnt; blk_execute_rq_nowait(req->q, NULL, req, 1, st_scsi_execute_end); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Shane M Seymour24171.30%15.56%
Linus Torvalds (pre-git)277.99%738.89%
Christoph Hellwig267.69%316.67%
FUJITA Tomonori257.40%316.67%
Kai Mäkisara102.96%316.67%
Mike Christie92.66%15.56%
Total338100.00%18100.00%

/* Do the scsi command. Waits until command performed if do_wait is true. Otherwise write_behind_check() is used to check that the command has finished. */
static struct st_request * st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd, int bytes, int direction, int timeout, int retries, int do_wait) { struct completion *waiting; struct rq_map_data *mdata = &STp->buffer->map_data; int ret; /* if async, make sure there's no command outstanding */ if (!do_wait && ((STp->buffer)->last_SRpnt)) { st_printk(KERN_ERR, STp, "Async command already active.\n"); if (signal_pending(current)) (STp->buffer)->syscall_result = (-EINTR); else (STp->buffer)->syscall_result = (-EBUSY); return NULL; } if (!SRpnt) { SRpnt = st_allocate_request(STp); if (!SRpnt) return NULL; } /* If async IO, set last_SRpnt. This ptr tells write_behind_check which IO is outstanding. It's nulled out when the IO completes. */ if (!do_wait) (STp->buffer)->last_SRpnt = SRpnt; waiting = &STp->wait; init_completion(waiting); SRpnt->waiting = waiting; if (STp->buffer->do_dio) { mdata->page_order = 0; mdata->nr_entries = STp->buffer->sg_segs; mdata->pages = STp->buffer->mapped_pages; } else { mdata->page_order = STp->buffer->reserved_page_order; mdata->nr_entries = DIV_ROUND_UP(bytes, PAGE_SIZE << mdata->page_order); mdata->pages = STp->buffer->reserved_pages; mdata->offset = 0; } memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd)); STp->buffer->cmdstat.have_sense = 0; STp->buffer->syscall_result = 0; ret = st_scsi_execute(SRpnt, cmd, direction,