Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * History: |
| 3 | * Started: Aug 9 by Lawrence Foard (entropy@world.std.com), |
| 4 | * to allow user process control of SCSI devices. |
| 5 | * Development Sponsored by Killy Corp. NY NY |
| 6 | * |
| 7 | * Original driver (sg.c): |
| 8 | * Copyright (C) 1992 Lawrence Foard |
| 9 | * Version 2 and 3 extensions to driver: |
| 10 | * Copyright (C) 1998 - 2005 Douglas Gilbert |
| 11 | * |
| 12 | * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | */ |
| 20 | |
Douglas Gilbert | b2155d0 | 2006-08-19 00:11:34 -0400 | [diff] [blame] | 21 | static int sg_version_num = 30534; /* 2 digits for each component */ |
| 22 | #define SG_VERSION_STR "3.5.34" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes: |
| 26 | * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First |
| 27 | * the kernel/module needs to be built with CONFIG_SCSI_LOGGING |
| 28 | * (otherwise the macros compile to empty statements). |
| 29 | * |
| 30 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/sched.h> |
| 36 | #include <linux/string.h> |
| 37 | #include <linux/mm.h> |
| 38 | #include <linux/errno.h> |
| 39 | #include <linux/mtio.h> |
| 40 | #include <linux/ioctl.h> |
| 41 | #include <linux/fcntl.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/cdev.h> |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 46 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/seq_file.h> |
| 48 | #include <linux/blkdev.h> |
| 49 | #include <linux/delay.h> |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 50 | #include <linux/blktrace_api.h> |
Jonathan Corbet | eb09d3d | 2008-05-15 12:22:06 -0600 | [diff] [blame] | 51 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #include "scsi.h" |
| db9dff3 | 2005-04-03 14:53:59 -0500 | [diff] [blame] | 54 | #include <scsi/scsi_dbg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <scsi/scsi_host.h> |
| 56 | #include <scsi/scsi_driver.h> |
| 57 | #include <scsi/scsi_ioctl.h> |
| 58 | #include <scsi/sg.h> |
| 59 | |
| 60 | #include "scsi_logging.h" |
| 61 | |
| 62 | #ifdef CONFIG_SCSI_PROC_FS |
| 63 | #include <linux/proc_fs.h> |
Douglas Gilbert | 7ca63cb | 2006-10-27 17:47:49 -0400 | [diff] [blame] | 64 | static char *sg_version_date = "20061027"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | static int sg_proc_init(void); |
| 67 | static void sg_proc_cleanup(void); |
| 68 | #endif |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #define SG_ALLOW_DIO_DEF 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | #define SG_MAX_DEVS 32768 |
| 73 | |
| 74 | /* |
| 75 | * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d) |
| 76 | * Then when using 32 bit integers x * m may overflow during the calculation. |
| 77 | * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m |
| 78 | * calculates the same, but prevents the overflow when both m and d |
| 79 | * are "small" numbers (like HZ and USER_HZ). |
| 80 | * Of course an overflow is inavoidable if the result of muldiv doesn't fit |
| 81 | * in 32 bits. |
| 82 | */ |
| 83 | #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL)) |
| 84 | |
| 85 | #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ) |
| 86 | |
| 87 | int sg_big_buff = SG_DEF_RESERVED_SIZE; |
| 88 | /* N.B. This variable is readable and writeable via |
| 89 | /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer |
| 90 | of this size (or less if there is not enough memory) will be reserved |
| 91 | for use by this file descriptor. [Deprecated usage: this variable is also |
| 92 | readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into |
| 93 | the kernel (i.e. it is not a module).] */ |
| 94 | static int def_reserved_size = -1; /* picks up init parameter */ |
| 95 | static int sg_allow_dio = SG_ALLOW_DIO_DEF; |
| 96 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 97 | static int scatter_elem_sz = SG_SCATTER_SZ; |
| 98 | static int scatter_elem_sz_prev = SG_SCATTER_SZ; |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | #define SG_SECTOR_SZ 512 |
| 101 | #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1) |
| 102 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 103 | static int sg_add(struct device *, struct class_interface *); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 104 | static void sg_device_destroy(struct kref *kref); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 105 | static void sg_remove(struct device *, struct class_interface *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 107 | static DEFINE_IDR(sg_index_idr); |
| 108 | static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | file descriptor list for device */ |
| 110 | |
| 111 | static struct class_interface sg_interface = { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 112 | .add_dev = sg_add, |
| 113 | .remove_dev = sg_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */ |
| 117 | unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */ |
FUJITA Tomonori | ea31255 | 2007-08-06 00:31:24 +0900 | [diff] [blame] | 118 | unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | unsigned bufflen; /* Size of (aggregate) data buffer */ |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 120 | struct page **pages; |
| 121 | int page_order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */ |
| 123 | unsigned char cmd_opcode; /* first byte of command */ |
| 124 | } Sg_scatter_hold; |
| 125 | |
| 126 | struct sg_device; /* forward declarations */ |
| 127 | struct sg_fd; |
| 128 | |
| 129 | typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | struct sg_request *nextrp; /* NULL -> tail request (slist) */ |
| 131 | struct sg_fd *parentfp; /* NULL -> not in use */ |
| 132 | Sg_scatter_hold data; /* hold buffer, perhaps scatter list */ |
| 133 | sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */ |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 134 | unsigned char sense_b[SCSI_SENSE_BUFFERSIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | char res_used; /* 1 -> using reserve buffer, 0 -> not ... */ |
| 136 | char orphan; /* 1 -> drop on sight, 0 -> normal */ |
| 137 | char sg_io_owned; /* 1 -> packet belongs to SG_IO */ |
| 138 | volatile char done; /* 0->before bh, 1->before read, 2->read */ |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 139 | struct request *rq; |
FUJITA Tomonori | 6e5a30c | 2008-08-28 16:17:08 +0900 | [diff] [blame] | 140 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } Sg_request; |
| 142 | |
| 143 | typedef struct sg_fd { /* holds the state of a file descriptor */ |
| 144 | struct sg_fd *nextfp; /* NULL when last opened fd on this device */ |
| 145 | struct sg_device *parentdp; /* owning device */ |
| 146 | wait_queue_head_t read_wait; /* queue read until command done */ |
| 147 | rwlock_t rq_list_lock; /* protect access to list in req_arr */ |
| 148 | int timeout; /* defaults to SG_DEFAULT_TIMEOUT */ |
| 149 | int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */ |
| 150 | Sg_scatter_hold reserve; /* buffer held for this file descriptor */ |
| 151 | unsigned save_scat_len; /* original length of trunc. scat. element */ |
| 152 | Sg_request *headrp; /* head of request slist, NULL->empty */ |
| 153 | struct fasync_struct *async_qp; /* used by asynchronous notification */ |
| 154 | Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */ |
| 155 | char low_dma; /* as in parent but possibly overridden to 1 */ |
| 156 | char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */ |
| 157 | volatile char closed; /* 1 -> fd closed but request(s) outstanding */ |
| 158 | char cmd_q; /* 1 -> allow command queuing, 0 -> don't */ |
| 159 | char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */ |
| 160 | char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */ |
| 161 | char mmap_called; /* 0 -> mmap() never called on this fd */ |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 162 | struct kref f_ref; |
| 163 | struct execute_work ew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } Sg_fd; |
| 165 | |
| 166 | typedef struct sg_device { /* holds the state of each scsi generic device */ |
| 167 | struct scsi_device *device; |
| 168 | wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */ |
| 169 | int sg_tablesize; /* adapter's max scatter-gather table size */ |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 170 | u32 index; /* device index number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | Sg_fd *headfp; /* first open fd belonging to this device */ |
| 172 | volatile char detached; /* 0->attached, 1->detached pending removal */ |
| 173 | volatile char exclude; /* opened for exclusive access */ |
| 174 | char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */ |
| 175 | struct gendisk *disk; |
| 176 | struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */ |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 177 | struct kref d_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } Sg_device; |
| 179 | |
| 180 | static int sg_fasync(int fd, struct file *filp, int mode); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 181 | /* tasklet or soft irq callback */ |
FUJITA Tomonori | a91a3a2 | 2008-09-02 22:50:01 +0900 | [diff] [blame] | 182 | static void sg_rq_end_io(struct request *rq, int uptodate); |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 183 | static int sg_start_req(Sg_request *srp, unsigned char *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | static void sg_finish_rem_req(Sg_request * srp); |
| 185 | static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size); |
| 186 | static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, |
| 187 | int tablesize); |
| 188 | static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, |
| 189 | Sg_request * srp); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 190 | static ssize_t sg_new_write(Sg_fd *sfp, struct file *file, |
| 191 | const char __user *buf, size_t count, int blocking, |
| 192 | int read_only, Sg_request **o_srp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | static int sg_common_write(Sg_fd * sfp, Sg_request * srp, |
| 194 | unsigned char *cmnd, int timeout, int blocking); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer); |
| 196 | static void sg_remove_scat(Sg_scatter_hold * schp); |
| 197 | static void sg_build_reserve(Sg_fd * sfp, int req_size); |
| 198 | static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size); |
| 199 | static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 201 | static void sg_remove_sfp(struct kref *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id); |
| 203 | static Sg_request *sg_add_request(Sg_fd * sfp); |
| 204 | static int sg_remove_request(Sg_fd * sfp, Sg_request * srp); |
| 205 | static int sg_res_in_use(Sg_fd * sfp); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 206 | static Sg_device *sg_lookup_dev(int dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | static Sg_device *sg_get_dev(int dev); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 208 | static void sg_put_dev(Sg_device *sdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | #ifdef CONFIG_SCSI_PROC_FS |
| 210 | static int sg_last_dev(void); |
| 211 | #endif |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | #define SZ_SG_HEADER sizeof(struct sg_header) |
| 214 | #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t) |
| 215 | #define SZ_SG_IOVEC sizeof(sg_iovec_t) |
| 216 | #define SZ_SG_REQ_INFO sizeof(sg_req_info_t) |
| 217 | |
FUJITA Tomonori | 14e507b | 2008-07-26 18:03:24 +0900 | [diff] [blame] | 218 | static int sg_allow_access(struct file *filp, unsigned char *cmd) |
| 219 | { |
| 220 | struct sg_fd *sfp = (struct sg_fd *)filp->private_data; |
| 221 | struct request_queue *q = sfp->parentdp->device->request_queue; |
| 222 | |
| 223 | if (sfp->parentdp->device->type == TYPE_SCANNER) |
| 224 | return 0; |
| 225 | |
| 226 | return blk_verify_command(&q->cmd_filter, |
| 227 | cmd, filp->f_mode & FMODE_WRITE); |
| 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | static int |
| 231 | sg_open(struct inode *inode, struct file *filp) |
| 232 | { |
| 233 | int dev = iminor(inode); |
| 234 | int flags = filp->f_flags; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 235 | struct request_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | Sg_device *sdp; |
| 237 | Sg_fd *sfp; |
| 238 | int res; |
| 239 | int retval; |
| 240 | |
Jonathan Corbet | eb09d3d | 2008-05-15 12:22:06 -0600 | [diff] [blame] | 241 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | nonseekable_open(inode, filp); |
| 243 | SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags)); |
| 244 | sdp = sg_get_dev(dev); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 245 | if (IS_ERR(sdp)) { |
| 246 | retval = PTR_ERR(sdp); |
| 247 | sdp = NULL; |
| 248 | goto sg_put; |
Jonathan Corbet | eb09d3d | 2008-05-15 12:22:06 -0600 | [diff] [blame] | 249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | /* This driver's module count bumped by fops_get in <linux/fs.h> */ |
| 252 | /* Prevent the device driver from vanishing while we sleep */ |
| 253 | retval = scsi_device_get(sdp->device); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 254 | if (retval) |
| 255 | goto sg_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | if (!((flags & O_NONBLOCK) || |
| 258 | scsi_block_when_processing_errors(sdp->device))) { |
| 259 | retval = -ENXIO; |
| 260 | /* we are in error recovery for this device */ |
| 261 | goto error_out; |
| 262 | } |
| 263 | |
| 264 | if (flags & O_EXCL) { |
| 265 | if (O_RDONLY == (flags & O_ACCMODE)) { |
| 266 | retval = -EPERM; /* Can't lock it with read only access */ |
| 267 | goto error_out; |
| 268 | } |
| 269 | if (sdp->headfp && (flags & O_NONBLOCK)) { |
| 270 | retval = -EBUSY; |
| 271 | goto error_out; |
| 272 | } |
| 273 | res = 0; |
| 274 | __wait_event_interruptible(sdp->o_excl_wait, |
| 275 | ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res); |
| 276 | if (res) { |
| 277 | retval = res; /* -ERESTARTSYS because signal hit process */ |
| 278 | goto error_out; |
| 279 | } |
| 280 | } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */ |
| 281 | if (flags & O_NONBLOCK) { |
| 282 | retval = -EBUSY; |
| 283 | goto error_out; |
| 284 | } |
| 285 | res = 0; |
| 286 | __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude), |
| 287 | res); |
| 288 | if (res) { |
| 289 | retval = res; /* -ERESTARTSYS because signal hit process */ |
| 290 | goto error_out; |
| 291 | } |
| 292 | } |
| 293 | if (sdp->detached) { |
| 294 | retval = -ENODEV; |
| 295 | goto error_out; |
| 296 | } |
| 297 | if (!sdp->headfp) { /* no existing opens on this device */ |
| 298 | sdp->sgdebug = 0; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 299 | q = sdp->device->request_queue; |
| 300 | sdp->sg_tablesize = min(q->max_hw_segments, |
| 301 | q->max_phys_segments); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | if ((sfp = sg_add_sfp(sdp, dev))) |
| 304 | filp->private_data = sfp; |
| 305 | else { |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 306 | if (flags & O_EXCL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | sdp->exclude = 0; /* undo if error */ |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 308 | wake_up_interruptible(&sdp->o_excl_wait); |
| 309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | retval = -ENOMEM; |
| 311 | goto error_out; |
| 312 | } |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 313 | retval = 0; |
| 314 | error_out: |
| 315 | if (retval) |
| 316 | scsi_device_put(sdp->device); |
| 317 | sg_put: |
| 318 | if (sdp) |
| 319 | sg_put_dev(sdp); |
Jonathan Corbet | eb09d3d | 2008-05-15 12:22:06 -0600 | [diff] [blame] | 320 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | return retval; |
| 322 | } |
| 323 | |
| 324 | /* Following function was formerly called 'sg_close' */ |
| 325 | static int |
| 326 | sg_release(struct inode *inode, struct file *filp) |
| 327 | { |
| 328 | Sg_device *sdp; |
| 329 | Sg_fd *sfp; |
| 330 | |
| 331 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 332 | return -ENXIO; |
| 333 | SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name)); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 334 | |
| 335 | sfp->closed = 1; |
| 336 | |
| 337 | sdp->exclude = 0; |
| 338 | wake_up_interruptible(&sdp->o_excl_wait); |
| 339 | |
| 340 | kref_put(&sfp->f_ref, sg_remove_sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static ssize_t |
| 345 | sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos) |
| 346 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | Sg_device *sdp; |
| 348 | Sg_fd *sfp; |
| 349 | Sg_request *srp; |
| 350 | int req_pack_id = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | sg_io_hdr_t *hp; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 352 | struct sg_header *old_hdr = NULL; |
| 353 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 356 | return -ENXIO; |
| 357 | SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n", |
| 358 | sdp->disk->disk_name, (int) count)); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 361 | return -EFAULT; |
| 362 | if (sfp->force_packid && (count >= SZ_SG_HEADER)) { |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 363 | old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL); |
| 364 | if (!old_hdr) |
| 365 | return -ENOMEM; |
| 366 | if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) { |
| 367 | retval = -EFAULT; |
| 368 | goto free_old_hdr; |
| 369 | } |
| 370 | if (old_hdr->reply_len < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (count >= SZ_SG_IO_HDR) { |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 372 | sg_io_hdr_t *new_hdr; |
| 373 | new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL); |
| 374 | if (!new_hdr) { |
| 375 | retval = -ENOMEM; |
| 376 | goto free_old_hdr; |
| 377 | } |
| 378 | retval =__copy_from_user |
| 379 | (new_hdr, buf, SZ_SG_IO_HDR); |
| 380 | req_pack_id = new_hdr->pack_id; |
| 381 | kfree(new_hdr); |
| 382 | if (retval) { |
| 383 | retval = -EFAULT; |
| 384 | goto free_old_hdr; |
| 385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | } else |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 388 | req_pack_id = old_hdr->pack_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | srp = sg_get_rq_mark(sfp, req_pack_id); |
| 391 | if (!srp) { /* now wait on packet to arrive */ |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 392 | if (sdp->detached) { |
| 393 | retval = -ENODEV; |
| 394 | goto free_old_hdr; |
| 395 | } |
| 396 | if (filp->f_flags & O_NONBLOCK) { |
| 397 | retval = -EAGAIN; |
| 398 | goto free_old_hdr; |
| 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | while (1) { |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 401 | retval = 0; /* following macro beats race condition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | __wait_event_interruptible(sfp->read_wait, |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 403 | (sdp->detached || |
| 404 | (srp = sg_get_rq_mark(sfp, req_pack_id))), |
| 405 | retval); |
| 406 | if (sdp->detached) { |
| 407 | retval = -ENODEV; |
| 408 | goto free_old_hdr; |
| 409 | } |
| 410 | if (0 == retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | break; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 412 | |
| 413 | /* -ERESTARTSYS as signal hit process */ |
| 414 | goto free_old_hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 417 | if (srp->header.interface_id != '\0') { |
| 418 | retval = sg_new_read(sfp, buf, count, srp); |
| 419 | goto free_old_hdr; |
| 420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | hp = &srp->header; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 423 | if (old_hdr == NULL) { |
| 424 | old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL); |
| 425 | if (! old_hdr) { |
| 426 | retval = -ENOMEM; |
| 427 | goto free_old_hdr; |
| 428 | } |
| 429 | } |
| 430 | memset(old_hdr, 0, SZ_SG_HEADER); |
| 431 | old_hdr->reply_len = (int) hp->timeout; |
| 432 | old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */ |
| 433 | old_hdr->pack_id = hp->pack_id; |
| 434 | old_hdr->twelve_byte = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 436 | old_hdr->target_status = hp->masked_status; |
| 437 | old_hdr->host_status = hp->host_status; |
| 438 | old_hdr->driver_status = hp->driver_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if ((CHECK_CONDITION & hp->masked_status) || |
| 440 | (DRIVER_SENSE & hp->driver_status)) |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 441 | memcpy(old_hdr->sense_buffer, srp->sense_b, |
| 442 | sizeof (old_hdr->sense_buffer)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | switch (hp->host_status) { |
| 444 | /* This setup of 'result' is for backward compatibility and is best |
| 445 | ignored by the user who should use target, host + driver status */ |
| 446 | case DID_OK: |
| 447 | case DID_PASSTHROUGH: |
| 448 | case DID_SOFT_ERROR: |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 449 | old_hdr->result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | break; |
| 451 | case DID_NO_CONNECT: |
| 452 | case DID_BUS_BUSY: |
| 453 | case DID_TIME_OUT: |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 454 | old_hdr->result = EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | break; |
| 456 | case DID_BAD_TARGET: |
| 457 | case DID_ABORT: |
| 458 | case DID_PARITY: |
| 459 | case DID_RESET: |
| 460 | case DID_BAD_INTR: |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 461 | old_hdr->result = EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | break; |
| 463 | case DID_ERROR: |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 464 | old_hdr->result = (srp->sense_b[0] == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | hp->masked_status == GOOD) ? 0 : EIO; |
| 466 | break; |
| 467 | default: |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 468 | old_hdr->result = EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | |
| 472 | /* Now copy the result back to the user buffer. */ |
| 473 | if (count >= SZ_SG_HEADER) { |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 474 | if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) { |
| 475 | retval = -EFAULT; |
| 476 | goto free_old_hdr; |
| 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | buf += SZ_SG_HEADER; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 479 | if (count > old_hdr->reply_len) |
| 480 | count = old_hdr->reply_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (count > SZ_SG_HEADER) { |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 482 | if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) { |
| 483 | retval = -EFAULT; |
| 484 | goto free_old_hdr; |
| 485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | } else |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 488 | count = (old_hdr->result == 0) ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | sg_finish_rem_req(srp); |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 490 | retval = count; |
| 491 | free_old_hdr: |
Jesper Juhl | c9475cb | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 492 | kfree(old_hdr); |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 493 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static ssize_t |
| 497 | sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp) |
| 498 | { |
| 499 | sg_io_hdr_t *hp = &srp->header; |
| 500 | int err = 0; |
| 501 | int len; |
| 502 | |
| 503 | if (count < SZ_SG_IO_HDR) { |
| 504 | err = -EINVAL; |
| 505 | goto err_out; |
| 506 | } |
| 507 | hp->sb_len_wr = 0; |
| 508 | if ((hp->mx_sb_len > 0) && hp->sbp) { |
| 509 | if ((CHECK_CONDITION & hp->masked_status) || |
| 510 | (DRIVER_SENSE & hp->driver_status)) { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 511 | int sb_len = SCSI_SENSE_BUFFERSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len; |
| 513 | len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */ |
| 514 | len = (len > sb_len) ? sb_len : len; |
| 515 | if (copy_to_user(hp->sbp, srp->sense_b, len)) { |
| 516 | err = -EFAULT; |
| 517 | goto err_out; |
| 518 | } |
| 519 | hp->sb_len_wr = len; |
| 520 | } |
| 521 | } |
| 522 | if (hp->masked_status || hp->host_status || hp->driver_status) |
| 523 | hp->info |= SG_INFO_CHECK; |
| 524 | if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) { |
| 525 | err = -EFAULT; |
| 526 | goto err_out; |
| 527 | } |
FUJITA Tomonori | 0b6cb26 | 2008-09-02 22:50:07 +0900 | [diff] [blame] | 528 | err_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | sg_finish_rem_req(srp); |
| 530 | return (0 == err) ? count : err; |
| 531 | } |
| 532 | |
| 533 | static ssize_t |
| 534 | sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos) |
| 535 | { |
| 536 | int mxsize, cmd_size, k; |
| 537 | int input_size, blocking; |
| 538 | unsigned char opcode; |
| 539 | Sg_device *sdp; |
| 540 | Sg_fd *sfp; |
| 541 | Sg_request *srp; |
| 542 | struct sg_header old_hdr; |
| 543 | sg_io_hdr_t *hp; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 544 | unsigned char cmnd[MAX_COMMAND_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 547 | return -ENXIO; |
| 548 | SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n", |
| 549 | sdp->disk->disk_name, (int) count)); |
| 550 | if (sdp->detached) |
| 551 | return -ENODEV; |
| 552 | if (!((filp->f_flags & O_NONBLOCK) || |
| 553 | scsi_block_when_processing_errors(sdp->device))) |
| 554 | return -ENXIO; |
| 555 | |
| 556 | if (!access_ok(VERIFY_READ, buf, count)) |
| 557 | return -EFAULT; /* protects following copy_from_user()s + get_user()s */ |
| 558 | if (count < SZ_SG_HEADER) |
| 559 | return -EIO; |
| 560 | if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER)) |
| 561 | return -EFAULT; |
| 562 | blocking = !(filp->f_flags & O_NONBLOCK); |
| 563 | if (old_hdr.reply_len < 0) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 564 | return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (count < (SZ_SG_HEADER + 6)) |
| 566 | return -EIO; /* The minimum scsi command length is 6 bytes. */ |
| 567 | |
| 568 | if (!(srp = sg_add_request(sfp))) { |
| 569 | SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n")); |
| 570 | return -EDOM; |
| 571 | } |
| 572 | buf += SZ_SG_HEADER; |
| 573 | __get_user(opcode, buf); |
| 574 | if (sfp->next_cmd_len > 0) { |
| 575 | if (sfp->next_cmd_len > MAX_COMMAND_SIZE) { |
| 576 | SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n")); |
| 577 | sfp->next_cmd_len = 0; |
| 578 | sg_remove_request(sfp, srp); |
| 579 | return -EIO; |
| 580 | } |
| 581 | cmd_size = sfp->next_cmd_len; |
| 582 | sfp->next_cmd_len = 0; /* reset so only this write() effected */ |
| 583 | } else { |
| 584 | cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */ |
| 585 | if ((opcode >= 0xc0) && old_hdr.twelve_byte) |
| 586 | cmd_size = 12; |
| 587 | } |
| 588 | SCSI_LOG_TIMEOUT(4, printk( |
| 589 | "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size)); |
| 590 | /* Determine buffer size. */ |
| 591 | input_size = count - cmd_size; |
| 592 | mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len; |
| 593 | mxsize -= SZ_SG_HEADER; |
| 594 | input_size -= SZ_SG_HEADER; |
| 595 | if (input_size < 0) { |
| 596 | sg_remove_request(sfp, srp); |
| 597 | return -EIO; /* User did not pass enough bytes for this command. */ |
| 598 | } |
| 599 | hp = &srp->header; |
| 600 | hp->interface_id = '\0'; /* indicator of old interface tunnelled */ |
| 601 | hp->cmd_len = (unsigned char) cmd_size; |
| 602 | hp->iovec_count = 0; |
| 603 | hp->mx_sb_len = 0; |
| 604 | if (input_size > 0) |
| 605 | hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ? |
| 606 | SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV; |
| 607 | else |
| 608 | hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE; |
| 609 | hp->dxfer_len = mxsize; |
FUJITA Tomonori | fad7f01 | 2008-09-02 16:20:20 +0900 | [diff] [blame] | 610 | if (hp->dxfer_direction == SG_DXFER_TO_DEV) |
| 611 | hp->dxferp = (char __user *)buf + cmd_size; |
| 612 | else |
| 613 | hp->dxferp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | hp->sbp = NULL; |
| 615 | hp->timeout = old_hdr.reply_len; /* structure abuse ... */ |
| 616 | hp->flags = input_size; /* structure abuse ... */ |
| 617 | hp->pack_id = old_hdr.pack_id; |
| 618 | hp->usr_ptr = NULL; |
| 619 | if (__copy_from_user(cmnd, buf, cmd_size)) |
| 620 | return -EFAULT; |
| 621 | /* |
| 622 | * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV, |
| 623 | * but is is possible that the app intended SG_DXFER_TO_DEV, because there |
| 624 | * is a non-zero input_size, so emit a warning. |
| 625 | */ |
Andi Kleen | eaa3e22 | 2008-01-13 17:41:43 +0100 | [diff] [blame] | 626 | if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) { |
| 627 | static char cmd[TASK_COMM_LEN]; |
| 628 | if (strcmp(current->comm, cmd) && printk_ratelimit()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | printk(KERN_WARNING |
| 630 | "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--" |
| 631 | "guessing data in;\n" KERN_WARNING " " |
| 632 | "program %s not setting count and/or reply_len properly\n", |
| 633 | old_hdr.reply_len - (int)SZ_SG_HEADER, |
| 634 | input_size, (unsigned int) cmnd[0], |
| 635 | current->comm); |
Andi Kleen | eaa3e22 | 2008-01-13 17:41:43 +0100 | [diff] [blame] | 636 | strcpy(cmd, current->comm); |
| 637 | } |
| 638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking); |
| 640 | return (k < 0) ? k : count; |
| 641 | } |
| 642 | |
| 643 | static ssize_t |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 644 | sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf, |
| 645 | size_t count, int blocking, int read_only, |
| 646 | Sg_request **o_srp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
| 648 | int k; |
| 649 | Sg_request *srp; |
| 650 | sg_io_hdr_t *hp; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 651 | unsigned char cmnd[MAX_COMMAND_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | int timeout; |
| 653 | unsigned long ul_timeout; |
| 654 | |
| 655 | if (count < SZ_SG_IO_HDR) |
| 656 | return -EINVAL; |
| 657 | if (!access_ok(VERIFY_READ, buf, count)) |
| 658 | return -EFAULT; /* protects following copy_from_user()s + get_user()s */ |
| 659 | |
| 660 | sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */ |
| 661 | if (!(srp = sg_add_request(sfp))) { |
| 662 | SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n")); |
| 663 | return -EDOM; |
| 664 | } |
| 665 | hp = &srp->header; |
| 666 | if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) { |
| 667 | sg_remove_request(sfp, srp); |
| 668 | return -EFAULT; |
| 669 | } |
| 670 | if (hp->interface_id != 'S') { |
| 671 | sg_remove_request(sfp, srp); |
| 672 | return -ENOSYS; |
| 673 | } |
| 674 | if (hp->flags & SG_FLAG_MMAP_IO) { |
| 675 | if (hp->dxfer_len > sfp->reserve.bufflen) { |
| 676 | sg_remove_request(sfp, srp); |
| 677 | return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */ |
| 678 | } |
| 679 | if (hp->flags & SG_FLAG_DIRECT_IO) { |
| 680 | sg_remove_request(sfp, srp); |
| 681 | return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */ |
| 682 | } |
| 683 | if (sg_res_in_use(sfp)) { |
| 684 | sg_remove_request(sfp, srp); |
| 685 | return -EBUSY; /* reserve buffer already being used */ |
| 686 | } |
| 687 | } |
| 688 | ul_timeout = msecs_to_jiffies(srp->header.timeout); |
| 689 | timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX; |
| 690 | if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) { |
| 691 | sg_remove_request(sfp, srp); |
| 692 | return -EMSGSIZE; |
| 693 | } |
| 694 | if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) { |
| 695 | sg_remove_request(sfp, srp); |
| 696 | return -EFAULT; /* protects following copy_from_user()s + get_user()s */ |
| 697 | } |
| 698 | if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) { |
| 699 | sg_remove_request(sfp, srp); |
| 700 | return -EFAULT; |
| 701 | } |
FUJITA Tomonori | 14e507b | 2008-07-26 18:03:24 +0900 | [diff] [blame] | 702 | if (read_only && sg_allow_access(file, cmnd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | sg_remove_request(sfp, srp); |
| 704 | return -EPERM; |
| 705 | } |
| 706 | k = sg_common_write(sfp, srp, cmnd, timeout, blocking); |
| 707 | if (k < 0) |
| 708 | return k; |
| 709 | if (o_srp) |
| 710 | *o_srp = srp; |
| 711 | return count; |
| 712 | } |
| 713 | |
| 714 | static int |
| 715 | sg_common_write(Sg_fd * sfp, Sg_request * srp, |
| 716 | unsigned char *cmnd, int timeout, int blocking) |
| 717 | { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 718 | int k, data_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | Sg_device *sdp = sfp->parentdp; |
| 720 | sg_io_hdr_t *hp = &srp->header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */ |
| 723 | hp->status = 0; |
| 724 | hp->masked_status = 0; |
| 725 | hp->msg_status = 0; |
| 726 | hp->info = 0; |
| 727 | hp->host_status = 0; |
| 728 | hp->driver_status = 0; |
| 729 | hp->resid = 0; |
| 730 | SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n", |
| 731 | (int) cmnd[0], (int) hp->cmd_len)); |
| 732 | |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 733 | k = sg_start_req(srp, cmnd); |
| 734 | if (k) { |
Douglas Gilbert | 7ca63cb | 2006-10-27 17:47:49 -0400 | [diff] [blame] | 735 | SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | sg_finish_rem_req(srp); |
| 737 | return k; /* probably out of space --> ENOMEM */ |
| 738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | if (sdp->detached) { |
| 740 | sg_finish_rem_req(srp); |
| 741 | return -ENODEV; |
| 742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | switch (hp->dxfer_direction) { |
| 745 | case SG_DXFER_TO_FROM_DEV: |
| 746 | case SG_DXFER_FROM_DEV: |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 747 | data_dir = DMA_FROM_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | break; |
| 749 | case SG_DXFER_TO_DEV: |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 750 | data_dir = DMA_TO_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | break; |
| 752 | case SG_DXFER_UNKNOWN: |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 753 | data_dir = DMA_BIDIRECTIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | break; |
| 755 | default: |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 756 | data_dir = DMA_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | break; |
| 758 | } |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 759 | hp->duration = jiffies_to_msecs(jiffies); |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 760 | |
| 761 | srp->rq->timeout = timeout; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 762 | kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */ |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 763 | blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk, |
| 764 | srp->rq, 1, sg_rq_end_io); |
| 765 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | static int |
| 769 | sg_srp_done(Sg_request *srp, Sg_fd *sfp) |
| 770 | { |
| 771 | unsigned long iflags; |
| 772 | int done; |
| 773 | |
| 774 | read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 775 | done = srp->done; |
| 776 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 777 | return done; |
| 778 | } |
| 779 | |
| 780 | static int |
| 781 | sg_ioctl(struct inode *inode, struct file *filp, |
| 782 | unsigned int cmd_in, unsigned long arg) |
| 783 | { |
| 784 | void __user *p = (void __user *)arg; |
| 785 | int __user *ip = p; |
| 786 | int result, val, read_only; |
| 787 | Sg_device *sdp; |
| 788 | Sg_fd *sfp; |
| 789 | Sg_request *srp; |
| 790 | unsigned long iflags; |
| 791 | |
| 792 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 793 | return -ENXIO; |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n", |
| 796 | sdp->disk->disk_name, (int) cmd_in)); |
| 797 | read_only = (O_RDWR != (filp->f_flags & O_ACCMODE)); |
| 798 | |
| 799 | switch (cmd_in) { |
| 800 | case SG_IO: |
| 801 | { |
| 802 | int blocking = 1; /* ignore O_NONBLOCK flag */ |
| 803 | |
| 804 | if (sdp->detached) |
| 805 | return -ENODEV; |
| 806 | if (!scsi_block_when_processing_errors(sdp->device)) |
| 807 | return -ENXIO; |
| 808 | if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR)) |
| 809 | return -EFAULT; |
| 810 | result = |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 811 | sg_new_write(sfp, filp, p, SZ_SG_IO_HDR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | blocking, read_only, &srp); |
| 813 | if (result < 0) |
| 814 | return result; |
| 815 | srp->sg_io_owned = 1; |
| 816 | while (1) { |
| 817 | result = 0; /* following macro to beat race condition */ |
| 818 | __wait_event_interruptible(sfp->read_wait, |
| 819 | (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)), |
| 820 | result); |
| 821 | if (sdp->detached) |
| 822 | return -ENODEV; |
| 823 | if (sfp->closed) |
| 824 | return 0; /* request packet dropped already */ |
| 825 | if (0 == result) |
| 826 | break; |
| 827 | srp->orphan = 1; |
| 828 | return result; /* -ERESTARTSYS because signal hit process */ |
| 829 | } |
| 830 | write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 831 | srp->done = 2; |
| 832 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 833 | result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp); |
| 834 | return (result < 0) ? result : 0; |
| 835 | } |
| 836 | case SG_SET_TIMEOUT: |
| 837 | result = get_user(val, ip); |
| 838 | if (result) |
| 839 | return result; |
| 840 | if (val < 0) |
| 841 | return -EIO; |
| 842 | if (val >= MULDIV (INT_MAX, USER_HZ, HZ)) |
| 843 | val = MULDIV (INT_MAX, USER_HZ, HZ); |
| 844 | sfp->timeout_user = val; |
| 845 | sfp->timeout = MULDIV (val, HZ, USER_HZ); |
| 846 | |
| 847 | return 0; |
| 848 | case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */ |
| 849 | /* strange ..., for backward compatibility */ |
| 850 | return sfp->timeout_user; |
| 851 | case SG_SET_FORCE_LOW_DMA: |
| 852 | result = get_user(val, ip); |
| 853 | if (result) |
| 854 | return result; |
| 855 | if (val) { |
| 856 | sfp->low_dma = 1; |
| 857 | if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) { |
| 858 | val = (int) sfp->reserve.bufflen; |
| 859 | sg_remove_scat(&sfp->reserve); |
| 860 | sg_build_reserve(sfp, val); |
| 861 | } |
| 862 | } else { |
| 863 | if (sdp->detached) |
| 864 | return -ENODEV; |
| 865 | sfp->low_dma = sdp->device->host->unchecked_isa_dma; |
| 866 | } |
| 867 | return 0; |
| 868 | case SG_GET_LOW_DMA: |
| 869 | return put_user((int) sfp->low_dma, ip); |
| 870 | case SG_GET_SCSI_ID: |
| 871 | if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t))) |
| 872 | return -EFAULT; |
| 873 | else { |
| 874 | sg_scsi_id_t __user *sg_idp = p; |
| 875 | |
| 876 | if (sdp->detached) |
| 877 | return -ENODEV; |
| 878 | __put_user((int) sdp->device->host->host_no, |
| 879 | &sg_idp->host_no); |
| 880 | __put_user((int) sdp->device->channel, |
| 881 | &sg_idp->channel); |
| 882 | __put_user((int) sdp->device->id, &sg_idp->scsi_id); |
| 883 | __put_user((int) sdp->device->lun, &sg_idp->lun); |
| 884 | __put_user((int) sdp->device->type, &sg_idp->scsi_type); |
| 885 | __put_user((short) sdp->device->host->cmd_per_lun, |
| 886 | &sg_idp->h_cmd_per_lun); |
| 887 | __put_user((short) sdp->device->queue_depth, |
| 888 | &sg_idp->d_queue_depth); |
| 889 | __put_user(0, &sg_idp->unused[0]); |
| 890 | __put_user(0, &sg_idp->unused[1]); |
| 891 | return 0; |
| 892 | } |
| 893 | case SG_SET_FORCE_PACK_ID: |
| 894 | result = get_user(val, ip); |
| 895 | if (result) |
| 896 | return result; |
| 897 | sfp->force_packid = val ? 1 : 0; |
| 898 | return 0; |
| 899 | case SG_GET_PACK_ID: |
| 900 | if (!access_ok(VERIFY_WRITE, ip, sizeof (int))) |
| 901 | return -EFAULT; |
| 902 | read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 903 | for (srp = sfp->headrp; srp; srp = srp->nextrp) { |
| 904 | if ((1 == srp->done) && (!srp->sg_io_owned)) { |
| 905 | read_unlock_irqrestore(&sfp->rq_list_lock, |
| 906 | iflags); |
| 907 | __put_user(srp->header.pack_id, ip); |
| 908 | return 0; |
| 909 | } |
| 910 | } |
| 911 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 912 | __put_user(-1, ip); |
| 913 | return 0; |
| 914 | case SG_GET_NUM_WAITING: |
| 915 | read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 916 | for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) { |
| 917 | if ((1 == srp->done) && (!srp->sg_io_owned)) |
| 918 | ++val; |
| 919 | } |
| 920 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 921 | return put_user(val, ip); |
| 922 | case SG_GET_SG_TABLESIZE: |
| 923 | return put_user(sdp->sg_tablesize, ip); |
| 924 | case SG_SET_RESERVED_SIZE: |
| 925 | result = get_user(val, ip); |
| 926 | if (result) |
| 927 | return result; |
| 928 | if (val < 0) |
| 929 | return -EINVAL; |
Alan Stern | 44ec954 | 2007-02-20 11:01:57 -0500 | [diff] [blame] | 930 | val = min_t(int, val, |
| 931 | sdp->device->request_queue->max_sectors * 512); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | if (val != sfp->reserve.bufflen) { |
| 933 | if (sg_res_in_use(sfp) || sfp->mmap_called) |
| 934 | return -EBUSY; |
| 935 | sg_remove_scat(&sfp->reserve); |
| 936 | sg_build_reserve(sfp, val); |
| 937 | } |
| 938 | return 0; |
| 939 | case SG_GET_RESERVED_SIZE: |
Alan Stern | 44ec954 | 2007-02-20 11:01:57 -0500 | [diff] [blame] | 940 | val = min_t(int, sfp->reserve.bufflen, |
| 941 | sdp->device->request_queue->max_sectors * 512); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return put_user(val, ip); |
| 943 | case SG_SET_COMMAND_Q: |
| 944 | result = get_user(val, ip); |
| 945 | if (result) |
| 946 | return result; |
| 947 | sfp->cmd_q = val ? 1 : 0; |
| 948 | return 0; |
| 949 | case SG_GET_COMMAND_Q: |
| 950 | return put_user((int) sfp->cmd_q, ip); |
| 951 | case SG_SET_KEEP_ORPHAN: |
| 952 | result = get_user(val, ip); |
| 953 | if (result) |
| 954 | return result; |
| 955 | sfp->keep_orphan = val; |
| 956 | return 0; |
| 957 | case SG_GET_KEEP_ORPHAN: |
| 958 | return put_user((int) sfp->keep_orphan, ip); |
| 959 | case SG_NEXT_CMD_LEN: |
| 960 | result = get_user(val, ip); |
| 961 | if (result) |
| 962 | return result; |
| 963 | sfp->next_cmd_len = (val > 0) ? val : 0; |
| 964 | return 0; |
| 965 | case SG_GET_VERSION_NUM: |
| 966 | return put_user(sg_version_num, ip); |
| 967 | case SG_GET_ACCESS_COUNT: |
| 968 | /* faked - we don't have a real access count anymore */ |
| 969 | val = (sdp->device ? 1 : 0); |
| 970 | return put_user(val, ip); |
| 971 | case SG_GET_REQUEST_TABLE: |
| 972 | if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE)) |
| 973 | return -EFAULT; |
| 974 | else { |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 975 | sg_req_info_t *rinfo; |
| 976 | unsigned int ms; |
| 977 | |
| 978 | rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE, |
| 979 | GFP_KERNEL); |
| 980 | if (!rinfo) |
| 981 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 983 | for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE; |
| 984 | ++val, srp = srp ? srp->nextrp : srp) { |
| 985 | memset(&rinfo[val], 0, SZ_SG_REQ_INFO); |
| 986 | if (srp) { |
| 987 | rinfo[val].req_state = srp->done + 1; |
| 988 | rinfo[val].problem = |
| 989 | srp->header.masked_status & |
| 990 | srp->header.host_status & |
| 991 | srp->header.driver_status; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 992 | if (srp->done) |
| 993 | rinfo[val].duration = |
| 994 | srp->header.duration; |
| 995 | else { |
| 996 | ms = jiffies_to_msecs(jiffies); |
| 997 | rinfo[val].duration = |
| 998 | (ms > srp->header.duration) ? |
| 999 | (ms - srp->header.duration) : 0; |
| 1000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | rinfo[val].orphan = srp->orphan; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 1002 | rinfo[val].sg_io_owned = |
| 1003 | srp->sg_io_owned; |
| 1004 | rinfo[val].pack_id = |
| 1005 | srp->header.pack_id; |
| 1006 | rinfo[val].usr_ptr = |
| 1007 | srp->header.usr_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 1011 | result = __copy_to_user(p, rinfo, |
| 1012 | SZ_SG_REQ_INFO * SG_MAX_QUEUE); |
| 1013 | result = result ? -EFAULT : 0; |
| 1014 | kfree(rinfo); |
| 1015 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | case SG_EMULATED_HOST: |
| 1018 | if (sdp->detached) |
| 1019 | return -ENODEV; |
| 1020 | return put_user(sdp->device->host->hostt->emulated, ip); |
| 1021 | case SG_SCSI_RESET: |
| 1022 | if (sdp->detached) |
| 1023 | return -ENODEV; |
| 1024 | if (filp->f_flags & O_NONBLOCK) { |
James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 1025 | if (scsi_host_in_recovery(sdp->device->host)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | return -EBUSY; |
| 1027 | } else if (!scsi_block_when_processing_errors(sdp->device)) |
| 1028 | return -EBUSY; |
| 1029 | result = get_user(val, ip); |
| 1030 | if (result) |
| 1031 | return result; |
| 1032 | if (SG_SCSI_RESET_NOTHING == val) |
| 1033 | return 0; |
| 1034 | switch (val) { |
| 1035 | case SG_SCSI_RESET_DEVICE: |
| 1036 | val = SCSI_TRY_RESET_DEVICE; |
| 1037 | break; |
Brian King | 39120e1 | 2008-07-01 13:03:19 -0500 | [diff] [blame] | 1038 | case SG_SCSI_RESET_TARGET: |
| 1039 | val = SCSI_TRY_RESET_TARGET; |
| 1040 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | case SG_SCSI_RESET_BUS: |
| 1042 | val = SCSI_TRY_RESET_BUS; |
| 1043 | break; |
| 1044 | case SG_SCSI_RESET_HOST: |
| 1045 | val = SCSI_TRY_RESET_HOST; |
| 1046 | break; |
| 1047 | default: |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 1051 | return -EACCES; |
| 1052 | return (scsi_reset_provider(sdp->device, val) == |
| 1053 | SUCCESS) ? 0 : -EIO; |
| 1054 | case SCSI_IOCTL_SEND_COMMAND: |
| 1055 | if (sdp->detached) |
| 1056 | return -ENODEV; |
| 1057 | if (read_only) { |
| 1058 | unsigned char opcode = WRITE_6; |
| 1059 | Scsi_Ioctl_Command __user *siocp = p; |
| 1060 | |
| 1061 | if (copy_from_user(&opcode, siocp->data, 1)) |
| 1062 | return -EFAULT; |
FUJITA Tomonori | 14e507b | 2008-07-26 18:03:24 +0900 | [diff] [blame] | 1063 | if (sg_allow_access(filp, &opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | return -EPERM; |
| 1065 | } |
Al Viro | e915e87 | 2008-09-02 17:16:41 -0400 | [diff] [blame] | 1066 | return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | case SG_SET_DEBUG: |
| 1068 | result = get_user(val, ip); |
| 1069 | if (result) |
| 1070 | return result; |
| 1071 | sdp->sgdebug = (char) val; |
| 1072 | return 0; |
| 1073 | case SCSI_IOCTL_GET_IDLUN: |
| 1074 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 1075 | case SCSI_IOCTL_PROBE_HOST: |
| 1076 | case SG_GET_TRANSFORM: |
| 1077 | if (sdp->detached) |
| 1078 | return -ENODEV; |
| 1079 | return scsi_ioctl(sdp->device, cmd_in, p); |
Alan Stern | 44ec954 | 2007-02-20 11:01:57 -0500 | [diff] [blame] | 1080 | case BLKSECTGET: |
| 1081 | return put_user(sdp->device->request_queue->max_sectors * 512, |
| 1082 | ip); |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 1083 | case BLKTRACESETUP: |
| 1084 | return blk_trace_setup(sdp->device->request_queue, |
| 1085 | sdp->disk->disk_name, |
Martin Peschke | 76e3a19 | 2009-01-30 15:46:23 +0100 | [diff] [blame] | 1086 | MKDEV(SCSI_GENERIC_MAJOR, sdp->index), |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 1087 | (char *)arg); |
| 1088 | case BLKTRACESTART: |
| 1089 | return blk_trace_startstop(sdp->device->request_queue, 1); |
| 1090 | case BLKTRACESTOP: |
| 1091 | return blk_trace_startstop(sdp->device->request_queue, 0); |
| 1092 | case BLKTRACETEARDOWN: |
| 1093 | return blk_trace_remove(sdp->device->request_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | default: |
| 1095 | if (read_only) |
| 1096 | return -EPERM; /* don't know so take safe approach */ |
| 1097 | return scsi_ioctl(sdp->device, cmd_in, p); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | #ifdef CONFIG_COMPAT |
| 1102 | static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg) |
| 1103 | { |
| 1104 | Sg_device *sdp; |
| 1105 | Sg_fd *sfp; |
| 1106 | struct scsi_device *sdev; |
| 1107 | |
| 1108 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 1109 | return -ENXIO; |
| 1110 | |
| 1111 | sdev = sdp->device; |
| 1112 | if (sdev->host->hostt->compat_ioctl) { |
| 1113 | int ret; |
| 1114 | |
| 1115 | ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg); |
| 1116 | |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
| 1120 | return -ENOIOCTLCMD; |
| 1121 | } |
| 1122 | #endif |
| 1123 | |
| 1124 | static unsigned int |
| 1125 | sg_poll(struct file *filp, poll_table * wait) |
| 1126 | { |
| 1127 | unsigned int res = 0; |
| 1128 | Sg_device *sdp; |
| 1129 | Sg_fd *sfp; |
| 1130 | Sg_request *srp; |
| 1131 | int count = 0; |
| 1132 | unsigned long iflags; |
| 1133 | |
| 1134 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)) |
| 1135 | || sfp->closed) |
| 1136 | return POLLERR; |
| 1137 | poll_wait(filp, &sfp->read_wait, wait); |
| 1138 | read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1139 | for (srp = sfp->headrp; srp; srp = srp->nextrp) { |
| 1140 | /* if any read waiting, flag it */ |
| 1141 | if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned)) |
| 1142 | res = POLLIN | POLLRDNORM; |
| 1143 | ++count; |
| 1144 | } |
| 1145 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1146 | |
| 1147 | if (sdp->detached) |
| 1148 | res |= POLLHUP; |
| 1149 | else if (!sfp->cmd_q) { |
| 1150 | if (0 == count) |
| 1151 | res |= POLLOUT | POLLWRNORM; |
| 1152 | } else if (count < SG_MAX_QUEUE) |
| 1153 | res |= POLLOUT | POLLWRNORM; |
| 1154 | SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n", |
| 1155 | sdp->disk->disk_name, (int) res)); |
| 1156 | return res; |
| 1157 | } |
| 1158 | |
| 1159 | static int |
| 1160 | sg_fasync(int fd, struct file *filp, int mode) |
| 1161 | { |
| 1162 | int retval; |
| 1163 | Sg_device *sdp; |
| 1164 | Sg_fd *sfp; |
| 1165 | |
| 1166 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 1167 | return -ENXIO; |
| 1168 | SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n", |
| 1169 | sdp->disk->disk_name, mode)); |
| 1170 | |
| 1171 | retval = fasync_helper(fd, filp, mode, &sfp->async_qp); |
| 1172 | return (retval < 0) ? retval : 0; |
| 1173 | } |
| 1174 | |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1175 | static int |
| 1176 | sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | { |
| 1178 | Sg_fd *sfp; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1179 | unsigned long offset, len, sa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | Sg_scatter_hold *rsv_schp; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1181 | int k, length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
| 1183 | if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data))) |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1184 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | rsv_schp = &sfp->reserve; |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1186 | offset = vmf->pgoff << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | if (offset >= rsv_schp->bufflen) |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1188 | return VM_FAULT_SIGBUS; |
| 1189 | SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | offset, rsv_schp->k_use_sg)); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1191 | sa = vma->vm_start; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1192 | length = 1 << (PAGE_SHIFT + rsv_schp->page_order); |
| 1193 | for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1194 | len = vma->vm_end - sa; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1195 | len = (len < length) ? len : length; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1196 | if (offset < len) { |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1197 | struct page *page = nth_page(rsv_schp->pages[k], |
| 1198 | offset >> PAGE_SHIFT); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1199 | get_page(page); /* increment page count */ |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1200 | vmf->page = page; |
| 1201 | return 0; /* success */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1203 | sa += len; |
| 1204 | offset -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1206 | |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1207 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | static struct vm_operations_struct sg_mmap_vm_ops = { |
Nick Piggin | a13ff0b | 2008-02-07 18:46:06 -0800 | [diff] [blame] | 1211 | .fault = sg_vma_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | }; |
| 1213 | |
| 1214 | static int |
| 1215 | sg_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1216 | { |
| 1217 | Sg_fd *sfp; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1218 | unsigned long req_sz, len, sa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | Sg_scatter_hold *rsv_schp; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1220 | int k, length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
| 1222 | if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data))) |
| 1223 | return -ENXIO; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 1224 | req_sz = vma->vm_end - vma->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n", |
| 1226 | (void *) vma->vm_start, (int) req_sz)); |
| 1227 | if (vma->vm_pgoff) |
| 1228 | return -EINVAL; /* want no offset */ |
| 1229 | rsv_schp = &sfp->reserve; |
| 1230 | if (req_sz > rsv_schp->bufflen) |
| 1231 | return -ENOMEM; /* cannot map more than reserved buffer */ |
| 1232 | |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1233 | sa = vma->vm_start; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1234 | length = 1 << (PAGE_SHIFT + rsv_schp->page_order); |
| 1235 | for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1236 | len = vma->vm_end - sa; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1237 | len = (len < length) ? len : length; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1238 | sa += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1240 | |
Nick Piggin | f9aed0e | 2006-03-22 00:08:30 -0800 | [diff] [blame] | 1241 | sfp->mmap_called = 1; |
Douglas Gilbert | 1c8e71d | 2005-09-09 17:18:57 +1000 | [diff] [blame] | 1242 | vma->vm_flags |= VM_RESERVED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | vma->vm_private_data = sfp; |
| 1244 | vma->vm_ops = &sg_mmap_vm_ops; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
FUJITA Tomonori | a91a3a2 | 2008-09-02 22:50:01 +0900 | [diff] [blame] | 1248 | /* |
| 1249 | * This function is a "bottom half" handler that is called by the mid |
| 1250 | * level when a command is completed (or has failed). |
| 1251 | */ |
| 1252 | static void sg_rq_end_io(struct request *rq, int uptodate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
FUJITA Tomonori | a91a3a2 | 2008-09-02 22:50:01 +0900 | [diff] [blame] | 1254 | struct sg_request *srp = rq->end_io_data; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1255 | Sg_device *sdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | Sg_fd *sfp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | unsigned long iflags; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 1258 | unsigned int ms; |
FUJITA Tomonori | a91a3a2 | 2008-09-02 22:50:01 +0900 | [diff] [blame] | 1259 | char *sense; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1260 | int result, resid, done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1262 | if (WARN_ON(srp->done != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | return; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | sfp = srp->parentfp; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1266 | if (WARN_ON(sfp == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | return; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1268 | |
| 1269 | sdp = sfp->parentdp; |
| 1270 | if (unlikely(sdp->detached)) |
| 1271 | printk(KERN_INFO "sg_rq_end_io: device detached\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
FUJITA Tomonori | a91a3a2 | 2008-09-02 22:50:01 +0900 | [diff] [blame] | 1273 | sense = rq->sense; |
| 1274 | result = rq->errors; |
| 1275 | resid = rq->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
| 1277 | SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n", |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1278 | sdp->disk->disk_name, srp->header.pack_id, result)); |
| 1279 | srp->header.resid = resid; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 1280 | ms = jiffies_to_msecs(jiffies); |
| 1281 | srp->header.duration = (ms > srp->header.duration) ? |
| 1282 | (ms - srp->header.duration) : 0; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1283 | if (0 != result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | struct scsi_sense_hdr sshdr; |
| 1285 | |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1286 | srp->header.status = 0xff & result; |
| 1287 | srp->header.masked_status = status_byte(result); |
| 1288 | srp->header.msg_status = msg_byte(result); |
| 1289 | srp->header.host_status = host_byte(result); |
| 1290 | srp->header.driver_status = driver_byte(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | if ((sdp->sgdebug > 0) && |
| 1292 | ((CHECK_CONDITION == srp->header.masked_status) || |
| 1293 | (COMMAND_TERMINATED == srp->header.masked_status))) |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1294 | __scsi_print_sense("sg_cmd_done", sense, |
| 1295 | SCSI_SENSE_BUFFERSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | /* Following if statement is a patch supplied by Eric Youngdale */ |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1298 | if (driver_byte(result) != 0 |
| 1299 | && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | && !scsi_sense_is_deferred(&sshdr) |
| 1301 | && sshdr.sense_key == UNIT_ATTENTION |
| 1302 | && sdp->device->removable) { |
| 1303 | /* Detected possible disc change. Set the bit - this */ |
| 1304 | /* may be used if there are filesystems using this device */ |
| 1305 | sdp->device->changed = 1; |
| 1306 | } |
| 1307 | } |
| 1308 | /* Rely on write phase to clean out srp status values, so no "else" */ |
| 1309 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1310 | write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1311 | if (unlikely(srp->orphan)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | if (sfp->keep_orphan) |
| 1313 | srp->sg_io_owned = 0; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1314 | else |
| 1315 | done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1317 | srp->done = done; |
| 1318 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1319 | |
| 1320 | if (likely(done)) { |
| 1321 | /* Now wake up any sg_read() that is waiting for this |
| 1322 | * packet. |
| 1323 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | wake_up_interruptible(&sfp->read_wait); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1325 | kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN); |
| 1326 | } else |
| 1327 | sg_finish_rem_req(srp); /* call with srp->done == 0 */ |
| 1328 | |
| 1329 | kref_put(&sfp->f_ref, sg_remove_sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static struct file_operations sg_fops = { |
| 1333 | .owner = THIS_MODULE, |
| 1334 | .read = sg_read, |
| 1335 | .write = sg_write, |
| 1336 | .poll = sg_poll, |
| 1337 | .ioctl = sg_ioctl, |
| 1338 | #ifdef CONFIG_COMPAT |
| 1339 | .compat_ioctl = sg_compat_ioctl, |
| 1340 | #endif |
| 1341 | .open = sg_open, |
| 1342 | .mmap = sg_mmap, |
| 1343 | .release = sg_release, |
| 1344 | .fasync = sg_fasync, |
| 1345 | }; |
| 1346 | |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 1347 | static struct class *sg_sysfs_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
| 1349 | static int sg_sysfs_valid = 0; |
| 1350 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1351 | static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1353 | struct request_queue *q = scsidp->request_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | Sg_device *sdp; |
| 1355 | unsigned long iflags; |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1356 | int error; |
| 1357 | u32 k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 1359 | sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | if (!sdp) { |
| 1361 | printk(KERN_WARNING "kmalloc Sg_device failure\n"); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1362 | return ERR_PTR(-ENOMEM); |
| 1363 | } |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1364 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1365 | if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) { |
| 1366 | printk(KERN_WARNING "idr expansion Sg_device failure\n"); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1367 | error = -ENOMEM; |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1368 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1371 | write_lock_irqsave(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1373 | error = idr_get_new(&sg_index_idr, sdp, &k); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1374 | if (error) { |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1375 | write_unlock_irqrestore(&sg_index_lock, iflags); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1376 | printk(KERN_WARNING "idr allocation Sg_device failure: %d\n", |
| 1377 | error); |
| 1378 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (unlikely(k >= SG_MAX_DEVS)) |
| 1382 | goto overflow; |
| 1383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k)); |
| 1385 | sprintf(disk->disk_name, "sg%d", k); |
| 1386 | disk->first_minor = k; |
| 1387 | sdp->disk = disk; |
| 1388 | sdp->device = scsidp; |
| 1389 | init_waitqueue_head(&sdp->o_excl_wait); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1390 | sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1391 | sdp->index = k; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1392 | kref_init(&sdp->d_ref); |
| 1393 | |
| 1394 | write_unlock_irqrestore(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1396 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | out: |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1398 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | kfree(sdp); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1400 | return ERR_PTR(error); |
| 1401 | } |
| 1402 | return sdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
| 1404 | overflow: |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1405 | idr_remove(&sg_index_idr, k); |
| 1406 | write_unlock_irqrestore(&sg_index_lock, iflags); |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 1407 | sdev_printk(KERN_WARNING, scsidp, |
| 1408 | "Unable to attach sg device type=%d, minor " |
| 1409 | "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | error = -ENODEV; |
| 1411 | goto out; |
| 1412 | } |
| 1413 | |
| 1414 | static int |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1415 | sg_add(struct device *cl_dev, struct class_interface *cl_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1417 | struct scsi_device *scsidp = to_scsi_device(cl_dev->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | struct gendisk *disk; |
| 1419 | Sg_device *sdp = NULL; |
| 1420 | struct cdev * cdev = NULL; |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1421 | int error; |
Ishai Rabinovitz | 454e895 | 2006-06-29 16:39:54 +0300 | [diff] [blame] | 1422 | unsigned long iflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | |
| 1424 | disk = alloc_disk(1); |
| 1425 | if (!disk) { |
| 1426 | printk(KERN_WARNING "alloc_disk failed\n"); |
| 1427 | return -ENOMEM; |
| 1428 | } |
| 1429 | disk->major = SCSI_GENERIC_MAJOR; |
| 1430 | |
| 1431 | error = -ENOMEM; |
| 1432 | cdev = cdev_alloc(); |
| 1433 | if (!cdev) { |
| 1434 | printk(KERN_WARNING "cdev_alloc failed\n"); |
| 1435 | goto out; |
| 1436 | } |
| 1437 | cdev->owner = THIS_MODULE; |
| 1438 | cdev->ops = &sg_fops; |
| 1439 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1440 | sdp = sg_alloc(disk, scsidp); |
| 1441 | if (IS_ERR(sdp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | printk(KERN_WARNING "sg_alloc failed\n"); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1443 | error = PTR_ERR(sdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | goto out; |
| 1445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1447 | error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1); |
Greg KH | 5e3c34c | 2006-01-18 16:17:46 -0800 | [diff] [blame] | 1448 | if (error) |
Ishai Rabinovitz | 454e895 | 2006-06-29 16:39:54 +0300 | [diff] [blame] | 1449 | goto cdev_add_err; |
Greg KH | 5e3c34c | 2006-01-18 16:17:46 -0800 | [diff] [blame] | 1450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | sdp->cdev = cdev; |
| 1452 | if (sg_sysfs_valid) { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1453 | struct device *sg_class_member; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | |
Greg Kroah-Hartman | d73a1a6 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1455 | sg_class_member = device_create(sg_sysfs_class, cl_dev->parent, |
| 1456 | MKDEV(SCSI_GENERIC_MAJOR, |
| 1457 | sdp->index), |
| 1458 | sdp, "%s", disk->disk_name); |
FUJITA Tomonori | d07e036 | 2008-01-15 13:18:00 +0900 | [diff] [blame] | 1459 | if (IS_ERR(sg_class_member)) { |
| 1460 | printk(KERN_ERR "sg_add: " |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1461 | "device_create failed\n"); |
FUJITA Tomonori | d07e036 | 2008-01-15 13:18:00 +0900 | [diff] [blame] | 1462 | error = PTR_ERR(sg_class_member); |
| 1463 | goto cdev_add_err; |
| 1464 | } |
FUJITA Tomonori | d07e036 | 2008-01-15 13:18:00 +0900 | [diff] [blame] | 1465 | error = sysfs_create_link(&scsidp->sdev_gendev.kobj, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | &sg_class_member->kobj, "generic"); |
| 1467 | if (error) |
| 1468 | printk(KERN_ERR "sg_add: unable to make symlink " |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1469 | "'generic' back to sg%d\n", sdp->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } else |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1471 | printk(KERN_WARNING "sg_add: sg_sys Invalid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 1473 | sdev_printk(KERN_NOTICE, scsidp, |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1474 | "Attached scsi generic sg%d type %d\n", sdp->index, |
| 1475 | scsidp->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1477 | dev_set_drvdata(cl_dev, sdp); |
FUJITA Tomonori | a24484f | 2008-01-15 13:17:47 +0900 | [diff] [blame] | 1478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | return 0; |
| 1480 | |
Ishai Rabinovitz | 454e895 | 2006-06-29 16:39:54 +0300 | [diff] [blame] | 1481 | cdev_add_err: |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1482 | write_lock_irqsave(&sg_index_lock, iflags); |
| 1483 | idr_remove(&sg_index_idr, sdp->index); |
| 1484 | write_unlock_irqrestore(&sg_index_lock, iflags); |
| 1485 | kfree(sdp); |
Ishai Rabinovitz | 454e895 | 2006-06-29 16:39:54 +0300 | [diff] [blame] | 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | out: |
| 1488 | put_disk(disk); |
| 1489 | if (cdev) |
| 1490 | cdev_del(cdev); |
| 1491 | return error; |
| 1492 | } |
| 1493 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1494 | static void sg_device_destroy(struct kref *kref) |
| 1495 | { |
| 1496 | struct sg_device *sdp = container_of(kref, struct sg_device, d_ref); |
| 1497 | unsigned long flags; |
| 1498 | |
| 1499 | /* CAUTION! Note that the device can still be found via idr_find() |
| 1500 | * even though the refcount is 0. Therefore, do idr_remove() BEFORE |
| 1501 | * any other cleanup. |
| 1502 | */ |
| 1503 | |
| 1504 | write_lock_irqsave(&sg_index_lock, flags); |
| 1505 | idr_remove(&sg_index_idr, sdp->index); |
| 1506 | write_unlock_irqrestore(&sg_index_lock, flags); |
| 1507 | |
| 1508 | SCSI_LOG_TIMEOUT(3, |
| 1509 | printk("sg_device_destroy: %s\n", |
| 1510 | sdp->disk->disk_name)); |
| 1511 | |
| 1512 | put_disk(sdp->disk); |
| 1513 | kfree(sdp); |
| 1514 | } |
| 1515 | |
| 1516 | static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1518 | struct scsi_device *scsidp = to_scsi_device(cl_dev->parent); |
| 1519 | Sg_device *sdp = dev_get_drvdata(cl_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | unsigned long iflags; |
| 1521 | Sg_fd *sfp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1523 | if (!sdp || sdp->detached) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1526 | SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name)); |
| 1527 | |
| 1528 | /* Need a write lock to set sdp->detached. */ |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1529 | write_lock_irqsave(&sg_index_lock, iflags); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1530 | sdp->detached = 1; |
| 1531 | for (sfp = sdp->headfp; sfp; sfp = sfp->nextfp) { |
| 1532 | wake_up_interruptible(&sfp->read_wait); |
| 1533 | kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1535 | write_unlock_irqrestore(&sg_index_lock, iflags); |
| 1536 | |
| 1537 | sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic"); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1538 | device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index)); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1539 | cdev_del(sdp->cdev); |
| 1540 | sdp->cdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 1542 | sg_put_dev(sdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1545 | module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR); |
| 1546 | module_param_named(def_reserved_size, def_reserved_size, int, |
| 1547 | S_IRUGO | S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR); |
| 1549 | |
| 1550 | MODULE_AUTHOR("Douglas Gilbert"); |
| 1551 | MODULE_DESCRIPTION("SCSI generic (sg) driver"); |
| 1552 | MODULE_LICENSE("GPL"); |
| 1553 | MODULE_VERSION(SG_VERSION_STR); |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 1554 | MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1556 | MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element " |
| 1557 | "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd"); |
| 1559 | MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))"); |
| 1560 | |
| 1561 | static int __init |
| 1562 | init_sg(void) |
| 1563 | { |
| 1564 | int rc; |
| 1565 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1566 | if (scatter_elem_sz < PAGE_SIZE) { |
| 1567 | scatter_elem_sz = PAGE_SIZE; |
| 1568 | scatter_elem_sz_prev = scatter_elem_sz; |
| 1569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | if (def_reserved_size >= 0) |
| 1571 | sg_big_buff = def_reserved_size; |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1572 | else |
| 1573 | def_reserved_size = sg_big_buff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
| 1575 | rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), |
| 1576 | SG_MAX_DEVS, "sg"); |
| 1577 | if (rc) |
| 1578 | return rc; |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 1579 | sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | if ( IS_ERR(sg_sysfs_class) ) { |
| 1581 | rc = PTR_ERR(sg_sysfs_class); |
| 1582 | goto err_out; |
| 1583 | } |
| 1584 | sg_sysfs_valid = 1; |
| 1585 | rc = scsi_register_interface(&sg_interface); |
| 1586 | if (0 == rc) { |
| 1587 | #ifdef CONFIG_SCSI_PROC_FS |
| 1588 | sg_proc_init(); |
| 1589 | #endif /* CONFIG_SCSI_PROC_FS */ |
| 1590 | return 0; |
| 1591 | } |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 1592 | class_destroy(sg_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | err_out: |
| 1594 | unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS); |
| 1595 | return rc; |
| 1596 | } |
| 1597 | |
| 1598 | static void __exit |
| 1599 | exit_sg(void) |
| 1600 | { |
| 1601 | #ifdef CONFIG_SCSI_PROC_FS |
| 1602 | sg_proc_cleanup(); |
| 1603 | #endif /* CONFIG_SCSI_PROC_FS */ |
| 1604 | scsi_unregister_interface(&sg_interface); |
gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 1605 | class_destroy(sg_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | sg_sysfs_valid = 0; |
| 1607 | unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), |
| 1608 | SG_MAX_DEVS); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 1609 | idr_destroy(&sg_index_idr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
FUJITA Tomonori | 44c7b0e | 2008-09-02 22:50:04 +0900 | [diff] [blame] | 1612 | static int sg_start_req(Sg_request *srp, unsigned char *cmd) |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1613 | { |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1614 | int res; |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1615 | struct request *rq; |
FUJITA Tomonori | 44c7b0e | 2008-09-02 22:50:04 +0900 | [diff] [blame] | 1616 | Sg_fd *sfp = srp->parentfp; |
| 1617 | sg_io_hdr_t *hp = &srp->header; |
| 1618 | int dxfer_len = (int) hp->dxfer_len; |
| 1619 | int dxfer_dir = hp->dxfer_direction; |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1620 | unsigned int iov_count = hp->iovec_count; |
FUJITA Tomonori | 44c7b0e | 2008-09-02 22:50:04 +0900 | [diff] [blame] | 1621 | Sg_scatter_hold *req_schp = &srp->data; |
| 1622 | Sg_scatter_hold *rsv_schp = &sfp->reserve; |
| 1623 | struct request_queue *q = sfp->parentdp->device->request_queue; |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1624 | struct rq_map_data *md, map_data; |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1625 | int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ; |
| 1626 | |
FUJITA Tomonori | 44c7b0e | 2008-09-02 22:50:04 +0900 | [diff] [blame] | 1627 | SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n", |
| 1628 | dxfer_len)); |
| 1629 | |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1630 | rq = blk_get_request(q, rw, GFP_ATOMIC); |
| 1631 | if (!rq) |
| 1632 | return -ENOMEM; |
| 1633 | |
| 1634 | memcpy(rq->cmd, cmd, hp->cmd_len); |
| 1635 | |
| 1636 | rq->cmd_len = hp->cmd_len; |
| 1637 | rq->cmd_type = REQ_TYPE_BLOCK_PC; |
| 1638 | |
| 1639 | srp->rq = rq; |
| 1640 | rq->end_io_data = srp; |
| 1641 | rq->sense = srp->sense_b; |
| 1642 | rq->retries = SG_DEFAULT_RETRIES; |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE)) |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1645 | return 0; |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1646 | |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1647 | if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO && |
| 1648 | dxfer_dir != SG_DXFER_UNKNOWN && !iov_count && |
| 1649 | !sfp->parentdp->device->host->unchecked_isa_dma && |
FUJITA Tomonori | 01cfcdd | 2008-08-28 15:05:59 +0900 | [diff] [blame] | 1650 | blk_rq_aligned(q, hp->dxferp, dxfer_len)) |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1651 | md = NULL; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1652 | else |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1653 | md = &map_data; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1654 | |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1655 | if (md) { |
| 1656 | if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen) |
| 1657 | sg_link_reserve(sfp, srp, dxfer_len); |
| 1658 | else { |
| 1659 | res = sg_build_indirect(req_schp, sfp, dxfer_len); |
| 1660 | if (res) |
| 1661 | return res; |
| 1662 | } |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1663 | |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1664 | md->pages = req_schp->pages; |
| 1665 | md->page_order = req_schp->page_order; |
| 1666 | md->nr_entries = req_schp->k_use_sg; |
FUJITA Tomonori | 56c451f | 2008-12-18 14:49:37 +0900 | [diff] [blame] | 1667 | md->offset = 0; |
FUJITA Tomonori | 97ae77a | 2008-12-18 14:49:38 +0900 | [diff] [blame] | 1668 | md->null_mapped = hp->dxferp ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1670 | |
FUJITA Tomonori | 626710c | 2008-09-02 22:50:05 +0900 | [diff] [blame] | 1671 | if (iov_count) |
| 1672 | res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count, |
| 1673 | hp->dxfer_len, GFP_ATOMIC); |
| 1674 | else |
| 1675 | res = blk_rq_map_user(q, rq, md, hp->dxferp, |
| 1676 | hp->dxfer_len, GFP_ATOMIC); |
| 1677 | |
| 1678 | if (!res) { |
| 1679 | srp->bio = rq->bio; |
| 1680 | |
| 1681 | if (!md) { |
| 1682 | req_schp->dio_in_use = 1; |
| 1683 | hp->info |= SG_INFO_DIRECT_IO; |
| 1684 | } |
| 1685 | } |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1686 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static void |
| 1690 | sg_finish_rem_req(Sg_request * srp) |
| 1691 | { |
| 1692 | Sg_fd *sfp = srp->parentfp; |
| 1693 | Sg_scatter_hold *req_schp = &srp->data; |
| 1694 | |
| 1695 | SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used)); |
| 1696 | if (srp->res_used) |
| 1697 | sg_unlink_reserve(sfp, srp); |
| 1698 | else |
| 1699 | sg_remove_scat(req_schp); |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1700 | |
FUJITA Tomonori | 6e5a30c | 2008-08-28 16:17:08 +0900 | [diff] [blame] | 1701 | if (srp->rq) { |
| 1702 | if (srp->bio) |
| 1703 | blk_rq_unmap_user(srp->bio); |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1704 | |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1705 | blk_put_request(srp->rq); |
FUJITA Tomonori | 6e5a30c | 2008-08-28 16:17:08 +0900 | [diff] [blame] | 1706 | } |
FUJITA Tomonori | 10865df | 2008-08-28 16:17:07 +0900 | [diff] [blame] | 1707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | sg_remove_request(sfp, srp); |
| 1709 | } |
| 1710 | |
| 1711 | static int |
| 1712 | sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize) |
| 1713 | { |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1714 | int sg_bufflen = tablesize * sizeof(struct page *); |
Al Viro | 2d20eaf | 2006-02-01 06:31:40 -0500 | [diff] [blame] | 1715 | gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1717 | schp->pages = kzalloc(sg_bufflen, gfp_flags); |
| 1718 | if (!schp->pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | schp->sglist_len = sg_bufflen; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1721 | return tablesize; /* number of scat_gath elements allocated */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | static int |
| 1725 | sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size) |
| 1726 | { |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1727 | int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1728 | int sg_tablesize = sfp->parentdp->sg_tablesize; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1729 | int blk_size = buff_size, order; |
| 1730 | gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
Eric Sesterhenn | fb11993 | 2007-05-23 14:41:36 -0700 | [diff] [blame] | 1732 | if (blk_size < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | return -EFAULT; |
| 1734 | if (0 == blk_size) |
| 1735 | ++blk_size; /* don't know why */ |
| 1736 | /* round request up to next highest SG_SECTOR_SZ byte boundary */ |
| 1737 | blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK); |
| 1738 | SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n", |
| 1739 | buff_size, blk_size)); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1740 | |
| 1741 | /* N.B. ret_sz carried into this block ... */ |
| 1742 | mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize); |
| 1743 | if (mx_sc_elems < 0) |
| 1744 | return mx_sc_elems; /* most likely -ENOMEM */ |
| 1745 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1746 | num = scatter_elem_sz; |
| 1747 | if (unlikely(num != scatter_elem_sz_prev)) { |
| 1748 | if (num < PAGE_SIZE) { |
| 1749 | scatter_elem_sz = PAGE_SIZE; |
| 1750 | scatter_elem_sz_prev = PAGE_SIZE; |
| 1751 | } else |
| 1752 | scatter_elem_sz_prev = num; |
| 1753 | } |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1754 | |
| 1755 | if (sfp->low_dma) |
| 1756 | gfp_mask |= GFP_DMA; |
| 1757 | |
| 1758 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 1759 | gfp_mask |= __GFP_ZERO; |
| 1760 | |
| 1761 | order = get_order(num); |
| 1762 | retry: |
| 1763 | ret_sz = 1 << (PAGE_SHIFT + order); |
| 1764 | |
| 1765 | for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems; |
| 1766 | k++, rem_sz -= ret_sz) { |
| 1767 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1768 | num = (rem_sz > scatter_elem_sz_prev) ? |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1769 | scatter_elem_sz_prev : rem_sz; |
| 1770 | |
| 1771 | schp->pages[k] = alloc_pages(gfp_mask, order); |
| 1772 | if (!schp->pages[k]) |
| 1773 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 1775 | if (num == scatter_elem_sz_prev) { |
| 1776 | if (unlikely(ret_sz > scatter_elem_sz_prev)) { |
| 1777 | scatter_elem_sz = ret_sz; |
| 1778 | scatter_elem_sz_prev = ret_sz; |
| 1779 | } |
| 1780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | |
Douglas Gilbert | 7ca63cb | 2006-10-27 17:47:49 -0400 | [diff] [blame] | 1782 | SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, " |
| 1783 | "ret_sz=%d\n", k, num, ret_sz)); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1784 | } /* end of for loop */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1786 | schp->page_order = order; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1787 | schp->k_use_sg = k; |
Douglas Gilbert | 7ca63cb | 2006-10-27 17:47:49 -0400 | [diff] [blame] | 1788 | SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, " |
| 1789 | "rem_sz=%d\n", k, rem_sz)); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1790 | |
| 1791 | schp->bufflen = blk_size; |
| 1792 | if (rem_sz > 0) /* must have failed */ |
| 1793 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | return 0; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1795 | out: |
| 1796 | for (i = 0; i < k; i++) |
| 1797 | __free_pages(schp->pages[k], order); |
| 1798 | |
| 1799 | if (--order >= 0) |
| 1800 | goto retry; |
| 1801 | |
| 1802 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | static void |
| 1806 | sg_remove_scat(Sg_scatter_hold * schp) |
| 1807 | { |
| 1808 | SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg)); |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1809 | if (schp->pages && schp->sglist_len > 0) { |
FUJITA Tomonori | 6e5a30c | 2008-08-28 16:17:08 +0900 | [diff] [blame] | 1810 | if (!schp->dio_in_use) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | int k; |
| 1812 | |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1813 | for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | SCSI_LOG_TIMEOUT(5, printk( |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1815 | "sg_remove_scat: k=%d, pg=0x%p\n", |
| 1816 | k, schp->pages[k])); |
| 1817 | __free_pages(schp->pages[k], schp->page_order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | } |
FUJITA Tomonori | 6e5a30c | 2008-08-28 16:17:08 +0900 | [diff] [blame] | 1819 | |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1820 | kfree(schp->pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | } |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | memset(schp, 0, sizeof (*schp)); |
| 1824 | } |
| 1825 | |
| 1826 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer) |
| 1828 | { |
| 1829 | Sg_scatter_hold *schp = &srp->data; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1830 | int k, num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | |
| 1832 | SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n", |
| 1833 | num_read_xfer)); |
| 1834 | if ((!outp) || (num_read_xfer <= 0)) |
| 1835 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1837 | num = 1 << (PAGE_SHIFT + schp->page_order); |
| 1838 | for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1839 | if (num > num_read_xfer) { |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1840 | if (__copy_to_user(outp, page_address(schp->pages[k]), |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1841 | num_read_xfer)) |
| 1842 | return -EFAULT; |
| 1843 | break; |
| 1844 | } else { |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1845 | if (__copy_to_user(outp, page_address(schp->pages[k]), |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1846 | num)) |
| 1847 | return -EFAULT; |
| 1848 | num_read_xfer -= num; |
| 1849 | if (num_read_xfer <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | break; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1851 | outp += num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | } |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | return 0; |
| 1856 | } |
| 1857 | |
| 1858 | static void |
| 1859 | sg_build_reserve(Sg_fd * sfp, int req_size) |
| 1860 | { |
| 1861 | Sg_scatter_hold *schp = &sfp->reserve; |
| 1862 | |
| 1863 | SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size)); |
| 1864 | do { |
| 1865 | if (req_size < PAGE_SIZE) |
| 1866 | req_size = PAGE_SIZE; |
| 1867 | if (0 == sg_build_indirect(schp, sfp, req_size)) |
| 1868 | return; |
| 1869 | else |
| 1870 | sg_remove_scat(schp); |
| 1871 | req_size >>= 1; /* divide by 2 */ |
| 1872 | } while (req_size > (PAGE_SIZE / 2)); |
| 1873 | } |
| 1874 | |
| 1875 | static void |
| 1876 | sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size) |
| 1877 | { |
| 1878 | Sg_scatter_hold *req_schp = &srp->data; |
| 1879 | Sg_scatter_hold *rsv_schp = &sfp->reserve; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1880 | int k, num, rem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | |
| 1882 | srp->res_used = 1; |
| 1883 | SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size)); |
Brian King | eca7be5 | 2006-02-14 12:42:24 -0600 | [diff] [blame] | 1884 | rem = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1886 | num = 1 << (PAGE_SHIFT + rsv_schp->page_order); |
| 1887 | for (k = 0; k < rsv_schp->k_use_sg; k++) { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1888 | if (rem <= num) { |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1889 | req_schp->k_use_sg = k + 1; |
| 1890 | req_schp->sglist_len = rsv_schp->sglist_len; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1891 | req_schp->pages = rsv_schp->pages; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1892 | |
| 1893 | req_schp->bufflen = size; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1894 | req_schp->page_order = rsv_schp->page_order; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1895 | break; |
| 1896 | } else |
| 1897 | rem -= num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | } |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 1899 | |
| 1900 | if (k >= rsv_schp->k_use_sg) |
| 1901 | SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static void |
| 1905 | sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp) |
| 1906 | { |
| 1907 | Sg_scatter_hold *req_schp = &srp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
| 1909 | SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n", |
| 1910 | (int) req_schp->k_use_sg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | req_schp->k_use_sg = 0; |
| 1912 | req_schp->bufflen = 0; |
FUJITA Tomonori | 10db10d | 2008-08-29 12:32:18 +0200 | [diff] [blame] | 1913 | req_schp->pages = NULL; |
| 1914 | req_schp->page_order = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | req_schp->sglist_len = 0; |
| 1916 | sfp->save_scat_len = 0; |
| 1917 | srp->res_used = 0; |
| 1918 | } |
| 1919 | |
| 1920 | static Sg_request * |
| 1921 | sg_get_rq_mark(Sg_fd * sfp, int pack_id) |
| 1922 | { |
| 1923 | Sg_request *resp; |
| 1924 | unsigned long iflags; |
| 1925 | |
| 1926 | write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1927 | for (resp = sfp->headrp; resp; resp = resp->nextrp) { |
| 1928 | /* look for requests that are ready + not SG_IO owned */ |
| 1929 | if ((1 == resp->done) && (!resp->sg_io_owned) && |
| 1930 | ((-1 == pack_id) || (resp->header.pack_id == pack_id))) { |
| 1931 | resp->done = 2; /* guard against other readers */ |
| 1932 | break; |
| 1933 | } |
| 1934 | } |
| 1935 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1936 | return resp; |
| 1937 | } |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | /* always adds to end of list */ |
| 1940 | static Sg_request * |
| 1941 | sg_add_request(Sg_fd * sfp) |
| 1942 | { |
| 1943 | int k; |
| 1944 | unsigned long iflags; |
| 1945 | Sg_request *resp; |
| 1946 | Sg_request *rp = sfp->req_arr; |
| 1947 | |
| 1948 | write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1949 | resp = sfp->headrp; |
| 1950 | if (!resp) { |
| 1951 | memset(rp, 0, sizeof (Sg_request)); |
| 1952 | rp->parentfp = sfp; |
| 1953 | resp = rp; |
| 1954 | sfp->headrp = resp; |
| 1955 | } else { |
| 1956 | if (0 == sfp->cmd_q) |
| 1957 | resp = NULL; /* command queuing disallowed */ |
| 1958 | else { |
| 1959 | for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) { |
| 1960 | if (!rp->parentfp) |
| 1961 | break; |
| 1962 | } |
| 1963 | if (k < SG_MAX_QUEUE) { |
| 1964 | memset(rp, 0, sizeof (Sg_request)); |
| 1965 | rp->parentfp = sfp; |
| 1966 | while (resp->nextrp) |
| 1967 | resp = resp->nextrp; |
| 1968 | resp->nextrp = rp; |
| 1969 | resp = rp; |
| 1970 | } else |
| 1971 | resp = NULL; |
| 1972 | } |
| 1973 | } |
| 1974 | if (resp) { |
| 1975 | resp->nextrp = NULL; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 1976 | resp->header.duration = jiffies_to_msecs(jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | } |
| 1978 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1979 | return resp; |
| 1980 | } |
| 1981 | |
| 1982 | /* Return of 1 for found; 0 for not found */ |
| 1983 | static int |
| 1984 | sg_remove_request(Sg_fd * sfp, Sg_request * srp) |
| 1985 | { |
| 1986 | Sg_request *prev_rp; |
| 1987 | Sg_request *rp; |
| 1988 | unsigned long iflags; |
| 1989 | int res = 0; |
| 1990 | |
| 1991 | if ((!sfp) || (!srp) || (!sfp->headrp)) |
| 1992 | return res; |
| 1993 | write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1994 | prev_rp = sfp->headrp; |
| 1995 | if (srp == prev_rp) { |
| 1996 | sfp->headrp = prev_rp->nextrp; |
| 1997 | prev_rp->parentfp = NULL; |
| 1998 | res = 1; |
| 1999 | } else { |
| 2000 | while ((rp = prev_rp->nextrp)) { |
| 2001 | if (srp == rp) { |
| 2002 | prev_rp->nextrp = rp->nextrp; |
| 2003 | rp->parentfp = NULL; |
| 2004 | res = 1; |
| 2005 | break; |
| 2006 | } |
| 2007 | prev_rp = rp; |
| 2008 | } |
| 2009 | } |
| 2010 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2011 | return res; |
| 2012 | } |
| 2013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | static Sg_fd * |
| 2015 | sg_add_sfp(Sg_device * sdp, int dev) |
| 2016 | { |
| 2017 | Sg_fd *sfp; |
| 2018 | unsigned long iflags; |
Alan Stern | 44ec954 | 2007-02-20 11:01:57 -0500 | [diff] [blame] | 2019 | int bufflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 2021 | sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | if (!sfp) |
| 2023 | return NULL; |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 2024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | init_waitqueue_head(&sfp->read_wait); |
| 2026 | rwlock_init(&sfp->rq_list_lock); |
| 2027 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2028 | kref_init(&sfp->f_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | sfp->timeout = SG_DEFAULT_TIMEOUT; |
| 2030 | sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER; |
| 2031 | sfp->force_packid = SG_DEF_FORCE_PACK_ID; |
| 2032 | sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ? |
| 2033 | sdp->device->host->unchecked_isa_dma : 1; |
| 2034 | sfp->cmd_q = SG_DEF_COMMAND_Q; |
| 2035 | sfp->keep_orphan = SG_DEF_KEEP_ORPHAN; |
| 2036 | sfp->parentdp = sdp; |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 2037 | write_lock_irqsave(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | if (!sdp->headfp) |
| 2039 | sdp->headfp = sfp; |
| 2040 | else { /* add to tail of existing list */ |
| 2041 | Sg_fd *pfp = sdp->headfp; |
| 2042 | while (pfp->nextfp) |
| 2043 | pfp = pfp->nextfp; |
| 2044 | pfp->nextfp = sfp; |
| 2045 | } |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 2046 | write_unlock_irqrestore(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp)); |
Douglas Gilbert | 6460e75 | 2006-09-20 18:20:49 -0400 | [diff] [blame] | 2048 | if (unlikely(sg_big_buff != def_reserved_size)) |
| 2049 | sg_big_buff = def_reserved_size; |
| 2050 | |
Alan Stern | 44ec954 | 2007-02-20 11:01:57 -0500 | [diff] [blame] | 2051 | bufflen = min_t(int, sg_big_buff, |
| 2052 | sdp->device->request_queue->max_sectors * 512); |
| 2053 | sg_build_reserve(sfp, bufflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n", |
| 2055 | sfp->reserve.bufflen, sfp->reserve.k_use_sg)); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2056 | |
| 2057 | kref_get(&sdp->d_ref); |
| 2058 | __module_get(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | return sfp; |
| 2060 | } |
| 2061 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2062 | static void sg_remove_sfp_usercontext(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | { |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2064 | struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work); |
| 2065 | struct sg_device *sdp = sfp->parentdp; |
| 2066 | |
| 2067 | /* Cleanup any responses which were never read(). */ |
| 2068 | while (sfp->headrp) |
| 2069 | sg_finish_rem_req(sfp->headrp); |
| 2070 | |
| 2071 | if (sfp->reserve.bufflen > 0) { |
| 2072 | SCSI_LOG_TIMEOUT(6, |
| 2073 | printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n", |
| 2074 | (int) sfp->reserve.bufflen, |
| 2075 | (int) sfp->reserve.k_use_sg)); |
| 2076 | sg_remove_scat(&sfp->reserve); |
| 2077 | } |
| 2078 | |
| 2079 | SCSI_LOG_TIMEOUT(6, |
| 2080 | printk("sg_remove_sfp: %s, sfp=0x%p\n", |
| 2081 | sdp->disk->disk_name, |
| 2082 | sfp)); |
| 2083 | kfree(sfp); |
| 2084 | |
| 2085 | scsi_device_put(sdp->device); |
| 2086 | sg_put_dev(sdp); |
| 2087 | module_put(THIS_MODULE); |
| 2088 | } |
| 2089 | |
| 2090 | static void sg_remove_sfp(struct kref *kref) |
| 2091 | { |
| 2092 | struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref); |
| 2093 | struct sg_device *sdp = sfp->parentdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | Sg_fd *fp; |
| 2095 | Sg_fd *prev_fp; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2096 | unsigned long iflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2098 | /* CAUTION! Note that sfp can still be found by walking sdp->headfp |
| 2099 | * even though the refcount is now 0. Therefore, unlink sfp from |
| 2100 | * sdp->headfp BEFORE doing any other cleanup. |
| 2101 | */ |
| 2102 | |
| 2103 | write_lock_irqsave(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | prev_fp = sdp->headfp; |
| 2105 | if (sfp == prev_fp) |
| 2106 | sdp->headfp = prev_fp->nextfp; |
| 2107 | else { |
| 2108 | while ((fp = prev_fp->nextfp)) { |
| 2109 | if (sfp == fp) { |
| 2110 | prev_fp->nextfp = fp->nextfp; |
| 2111 | break; |
| 2112 | } |
| 2113 | prev_fp = fp; |
| 2114 | } |
| 2115 | } |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2116 | write_unlock_irqrestore(&sg_index_lock, iflags); |
| 2117 | wake_up_interruptible(&sdp->o_excl_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2119 | execute_in_process_context(sg_remove_sfp_usercontext, &sfp->ew); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | static int |
| 2123 | sg_res_in_use(Sg_fd * sfp) |
| 2124 | { |
| 2125 | const Sg_request *srp; |
| 2126 | unsigned long iflags; |
| 2127 | |
| 2128 | read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 2129 | for (srp = sfp->headrp; srp; srp = srp->nextrp) |
| 2130 | if (srp->res_used) |
| 2131 | break; |
| 2132 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2133 | return srp ? 1 : 0; |
| 2134 | } |
| 2135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | #ifdef CONFIG_SCSI_PROC_FS |
| 2137 | static int |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 2138 | sg_idr_max_id(int id, void *p, void *data) |
| 2139 | { |
| 2140 | int *k = data; |
| 2141 | |
| 2142 | if (*k < id) |
| 2143 | *k = id; |
| 2144 | |
| 2145 | return 0; |
| 2146 | } |
| 2147 | |
| 2148 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | sg_last_dev(void) |
| 2150 | { |
Tony Battersby | 53474c0 | 2008-01-22 15:25:49 -0500 | [diff] [blame] | 2151 | int k = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | unsigned long iflags; |
| 2153 | |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 2154 | read_lock_irqsave(&sg_index_lock, iflags); |
| 2155 | idr_for_each(&sg_index_idr, sg_idr_max_id, &k); |
| 2156 | read_unlock_irqrestore(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | return k + 1; /* origin 1 */ |
| 2158 | } |
| 2159 | #endif |
| 2160 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2161 | /* must be called with sg_index_lock held */ |
| 2162 | static Sg_device *sg_lookup_dev(int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | { |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2164 | return idr_find(&sg_index_idr, dev); |
| 2165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2167 | static Sg_device *sg_get_dev(int dev) |
| 2168 | { |
| 2169 | struct sg_device *sdp; |
| 2170 | unsigned long flags; |
| 2171 | |
| 2172 | read_lock_irqsave(&sg_index_lock, flags); |
| 2173 | sdp = sg_lookup_dev(dev); |
| 2174 | if (!sdp) |
| 2175 | sdp = ERR_PTR(-ENXIO); |
| 2176 | else if (sdp->detached) { |
| 2177 | /* If sdp->detached, then the refcount may already be 0, in |
| 2178 | * which case it would be a bug to do kref_get(). |
| 2179 | */ |
| 2180 | sdp = ERR_PTR(-ENODEV); |
| 2181 | } else |
| 2182 | kref_get(&sdp->d_ref); |
| 2183 | read_unlock_irqrestore(&sg_index_lock, flags); |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 2184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | return sdp; |
| 2186 | } |
| 2187 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2188 | static void sg_put_dev(struct sg_device *sdp) |
| 2189 | { |
| 2190 | kref_put(&sdp->d_ref, sg_device_destroy); |
| 2191 | } |
| 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | #ifdef CONFIG_SCSI_PROC_FS |
| 2194 | |
| 2195 | static struct proc_dir_entry *sg_proc_sgp = NULL; |
| 2196 | |
| 2197 | static char sg_proc_sg_dirname[] = "scsi/sg"; |
| 2198 | |
| 2199 | static int sg_proc_seq_show_int(struct seq_file *s, void *v); |
| 2200 | |
| 2201 | static int sg_proc_single_open_adio(struct inode *inode, struct file *file); |
| 2202 | static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer, |
| 2203 | size_t count, loff_t *off); |
| 2204 | static struct file_operations adio_fops = { |
| 2205 | /* .owner, .read and .llseek added in sg_proc_init() */ |
| 2206 | .open = sg_proc_single_open_adio, |
| 2207 | .write = sg_proc_write_adio, |
| 2208 | .release = single_release, |
| 2209 | }; |
| 2210 | |
| 2211 | static int sg_proc_single_open_dressz(struct inode *inode, struct file *file); |
| 2212 | static ssize_t sg_proc_write_dressz(struct file *filp, |
| 2213 | const char __user *buffer, size_t count, loff_t *off); |
| 2214 | static struct file_operations dressz_fops = { |
| 2215 | .open = sg_proc_single_open_dressz, |
| 2216 | .write = sg_proc_write_dressz, |
| 2217 | .release = single_release, |
| 2218 | }; |
| 2219 | |
| 2220 | static int sg_proc_seq_show_version(struct seq_file *s, void *v); |
| 2221 | static int sg_proc_single_open_version(struct inode *inode, struct file *file); |
| 2222 | static struct file_operations version_fops = { |
| 2223 | .open = sg_proc_single_open_version, |
| 2224 | .release = single_release, |
| 2225 | }; |
| 2226 | |
| 2227 | static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v); |
| 2228 | static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file); |
| 2229 | static struct file_operations devhdr_fops = { |
| 2230 | .open = sg_proc_single_open_devhdr, |
| 2231 | .release = single_release, |
| 2232 | }; |
| 2233 | |
| 2234 | static int sg_proc_seq_show_dev(struct seq_file *s, void *v); |
| 2235 | static int sg_proc_open_dev(struct inode *inode, struct file *file); |
| 2236 | static void * dev_seq_start(struct seq_file *s, loff_t *pos); |
| 2237 | static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos); |
| 2238 | static void dev_seq_stop(struct seq_file *s, void *v); |
| 2239 | static struct file_operations dev_fops = { |
| 2240 | .open = sg_proc_open_dev, |
| 2241 | .release = seq_release, |
| 2242 | }; |
| 2243 | static struct seq_operations dev_seq_ops = { |
| 2244 | .start = dev_seq_start, |
| 2245 | .next = dev_seq_next, |
| 2246 | .stop = dev_seq_stop, |
| 2247 | .show = sg_proc_seq_show_dev, |
| 2248 | }; |
| 2249 | |
| 2250 | static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v); |
| 2251 | static int sg_proc_open_devstrs(struct inode *inode, struct file *file); |
| 2252 | static struct file_operations devstrs_fops = { |
| 2253 | .open = sg_proc_open_devstrs, |
| 2254 | .release = seq_release, |
| 2255 | }; |
| 2256 | static struct seq_operations devstrs_seq_ops = { |
| 2257 | .start = dev_seq_start, |
| 2258 | .next = dev_seq_next, |
| 2259 | .stop = dev_seq_stop, |
| 2260 | .show = sg_proc_seq_show_devstrs, |
| 2261 | }; |
| 2262 | |
| 2263 | static int sg_proc_seq_show_debug(struct seq_file *s, void *v); |
| 2264 | static int sg_proc_open_debug(struct inode *inode, struct file *file); |
| 2265 | static struct file_operations debug_fops = { |
| 2266 | .open = sg_proc_open_debug, |
| 2267 | .release = seq_release, |
| 2268 | }; |
| 2269 | static struct seq_operations debug_seq_ops = { |
| 2270 | .start = dev_seq_start, |
| 2271 | .next = dev_seq_next, |
| 2272 | .stop = dev_seq_stop, |
| 2273 | .show = sg_proc_seq_show_debug, |
| 2274 | }; |
| 2275 | |
| 2276 | |
| 2277 | struct sg_proc_leaf { |
| 2278 | const char * name; |
| 2279 | struct file_operations * fops; |
| 2280 | }; |
| 2281 | |
| 2282 | static struct sg_proc_leaf sg_proc_leaf_arr[] = { |
| 2283 | {"allow_dio", &adio_fops}, |
| 2284 | {"debug", &debug_fops}, |
| 2285 | {"def_reserved_size", &dressz_fops}, |
| 2286 | {"device_hdr", &devhdr_fops}, |
| 2287 | {"devices", &dev_fops}, |
| 2288 | {"device_strs", &devstrs_fops}, |
| 2289 | {"version", &version_fops} |
| 2290 | }; |
| 2291 | |
| 2292 | static int |
| 2293 | sg_proc_init(void) |
| 2294 | { |
| 2295 | int k, mask; |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 2296 | int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | struct sg_proc_leaf * leaf; |
| 2298 | |
Al Viro | 6660022 | 2005-09-28 22:32:57 +0100 | [diff] [blame] | 2299 | sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | if (!sg_proc_sgp) |
| 2301 | return 1; |
| 2302 | for (k = 0; k < num_leaves; ++k) { |
| 2303 | leaf = &sg_proc_leaf_arr[k]; |
| 2304 | mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO; |
Denis V. Lunev | a973909 | 2008-04-29 01:02:17 -0700 | [diff] [blame] | 2305 | leaf->fops->owner = THIS_MODULE; |
| 2306 | leaf->fops->read = seq_read; |
| 2307 | leaf->fops->llseek = seq_lseek; |
| 2308 | proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | } |
| 2310 | return 0; |
| 2311 | } |
| 2312 | |
| 2313 | static void |
| 2314 | sg_proc_cleanup(void) |
| 2315 | { |
| 2316 | int k; |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 2317 | int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | |
| 2319 | if (!sg_proc_sgp) |
| 2320 | return; |
| 2321 | for (k = 0; k < num_leaves; ++k) |
| 2322 | remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp); |
| 2323 | remove_proc_entry(sg_proc_sg_dirname, NULL); |
| 2324 | } |
| 2325 | |
| 2326 | |
| 2327 | static int sg_proc_seq_show_int(struct seq_file *s, void *v) |
| 2328 | { |
| 2329 | seq_printf(s, "%d\n", *((int *)s->private)); |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
| 2333 | static int sg_proc_single_open_adio(struct inode *inode, struct file *file) |
| 2334 | { |
| 2335 | return single_open(file, sg_proc_seq_show_int, &sg_allow_dio); |
| 2336 | } |
| 2337 | |
| 2338 | static ssize_t |
| 2339 | sg_proc_write_adio(struct file *filp, const char __user *buffer, |
| 2340 | size_t count, loff_t *off) |
| 2341 | { |
| 2342 | int num; |
| 2343 | char buff[11]; |
| 2344 | |
| 2345 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 2346 | return -EACCES; |
| 2347 | num = (count < 10) ? count : 10; |
| 2348 | if (copy_from_user(buff, buffer, num)) |
| 2349 | return -EFAULT; |
| 2350 | buff[num] = '\0'; |
| 2351 | sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0; |
| 2352 | return count; |
| 2353 | } |
| 2354 | |
| 2355 | static int sg_proc_single_open_dressz(struct inode *inode, struct file *file) |
| 2356 | { |
| 2357 | return single_open(file, sg_proc_seq_show_int, &sg_big_buff); |
| 2358 | } |
| 2359 | |
| 2360 | static ssize_t |
| 2361 | sg_proc_write_dressz(struct file *filp, const char __user *buffer, |
| 2362 | size_t count, loff_t *off) |
| 2363 | { |
| 2364 | int num; |
| 2365 | unsigned long k = ULONG_MAX; |
| 2366 | char buff[11]; |
| 2367 | |
| 2368 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 2369 | return -EACCES; |
| 2370 | num = (count < 10) ? count : 10; |
| 2371 | if (copy_from_user(buff, buffer, num)) |
| 2372 | return -EFAULT; |
| 2373 | buff[num] = '\0'; |
| 2374 | k = simple_strtoul(buff, NULL, 10); |
| 2375 | if (k <= 1048576) { /* limit "big buff" to 1 MB */ |
| 2376 | sg_big_buff = k; |
| 2377 | return count; |
| 2378 | } |
| 2379 | return -ERANGE; |
| 2380 | } |
| 2381 | |
| 2382 | static int sg_proc_seq_show_version(struct seq_file *s, void *v) |
| 2383 | { |
| 2384 | seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR, |
| 2385 | sg_version_date); |
| 2386 | return 0; |
| 2387 | } |
| 2388 | |
| 2389 | static int sg_proc_single_open_version(struct inode *inode, struct file *file) |
| 2390 | { |
| 2391 | return single_open(file, sg_proc_seq_show_version, NULL); |
| 2392 | } |
| 2393 | |
| 2394 | static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v) |
| 2395 | { |
| 2396 | seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t" |
| 2397 | "online\n"); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file) |
| 2402 | { |
| 2403 | return single_open(file, sg_proc_seq_show_devhdr, NULL); |
| 2404 | } |
| 2405 | |
| 2406 | struct sg_proc_deviter { |
| 2407 | loff_t index; |
| 2408 | size_t max; |
| 2409 | }; |
| 2410 | |
| 2411 | static void * dev_seq_start(struct seq_file *s, loff_t *pos) |
| 2412 | { |
| 2413 | struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL); |
| 2414 | |
Jan Blunck | 729d70f | 2005-08-27 11:07:52 -0700 | [diff] [blame] | 2415 | s->private = it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | if (! it) |
| 2417 | return NULL; |
Jan Blunck | 729d70f | 2005-08-27 11:07:52 -0700 | [diff] [blame] | 2418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | it->index = *pos; |
| 2420 | it->max = sg_last_dev(); |
| 2421 | if (it->index >= it->max) |
Jan Blunck | 729d70f | 2005-08-27 11:07:52 -0700 | [diff] [blame] | 2422 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | return it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2427 | { |
Jan Blunck | 729d70f | 2005-08-27 11:07:52 -0700 | [diff] [blame] | 2428 | struct sg_proc_deviter * it = s->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | |
| 2430 | *pos = ++it->index; |
| 2431 | return (it->index < it->max) ? it : NULL; |
| 2432 | } |
| 2433 | |
| 2434 | static void dev_seq_stop(struct seq_file *s, void *v) |
| 2435 | { |
Jan Blunck | 729d70f | 2005-08-27 11:07:52 -0700 | [diff] [blame] | 2436 | kfree(s->private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | static int sg_proc_open_dev(struct inode *inode, struct file *file) |
| 2440 | { |
| 2441 | return seq_open(file, &dev_seq_ops); |
| 2442 | } |
| 2443 | |
| 2444 | static int sg_proc_seq_show_dev(struct seq_file *s, void *v) |
| 2445 | { |
| 2446 | struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; |
| 2447 | Sg_device *sdp; |
| 2448 | struct scsi_device *scsidp; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2449 | unsigned long iflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2451 | read_lock_irqsave(&sg_index_lock, iflags); |
| 2452 | sdp = it ? sg_lookup_dev(it->index) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | if (sdp && (scsidp = sdp->device) && (!sdp->detached)) |
| 2454 | seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", |
| 2455 | scsidp->host->host_no, scsidp->channel, |
| 2456 | scsidp->id, scsidp->lun, (int) scsidp->type, |
| 2457 | 1, |
| 2458 | (int) scsidp->queue_depth, |
| 2459 | (int) scsidp->device_busy, |
| 2460 | (int) scsi_device_online(scsidp)); |
| 2461 | else |
| 2462 | seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n"); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2463 | read_unlock_irqrestore(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | return 0; |
| 2465 | } |
| 2466 | |
| 2467 | static int sg_proc_open_devstrs(struct inode *inode, struct file *file) |
| 2468 | { |
| 2469 | return seq_open(file, &devstrs_seq_ops); |
| 2470 | } |
| 2471 | |
| 2472 | static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v) |
| 2473 | { |
| 2474 | struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; |
| 2475 | Sg_device *sdp; |
| 2476 | struct scsi_device *scsidp; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2477 | unsigned long iflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2479 | read_lock_irqsave(&sg_index_lock, iflags); |
| 2480 | sdp = it ? sg_lookup_dev(it->index) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | if (sdp && (scsidp = sdp->device) && (!sdp->detached)) |
| 2482 | seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n", |
| 2483 | scsidp->vendor, scsidp->model, scsidp->rev); |
| 2484 | else |
| 2485 | seq_printf(s, "<no active device>\n"); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2486 | read_unlock_irqrestore(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | return 0; |
| 2488 | } |
| 2489 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2490 | /* must be called while holding sg_index_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp) |
| 2492 | { |
| 2493 | int k, m, new_interface, blen, usg; |
| 2494 | Sg_request *srp; |
| 2495 | Sg_fd *fp; |
| 2496 | const sg_io_hdr_t *hp; |
| 2497 | const char * cp; |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 2498 | unsigned int ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2500 | for (k = 0, fp = sdp->headfp; fp != NULL; ++k, fp = fp->nextfp) { |
| 2501 | read_lock(&fp->rq_list_lock); /* irqs already disabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | seq_printf(s, " FD(%d): timeout=%dms bufflen=%d " |
| 2503 | "(res)sgat=%d low_dma=%d\n", k + 1, |
| 2504 | jiffies_to_msecs(fp->timeout), |
| 2505 | fp->reserve.bufflen, |
| 2506 | (int) fp->reserve.k_use_sg, |
| 2507 | (int) fp->low_dma); |
| 2508 | seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n", |
| 2509 | (int) fp->cmd_q, (int) fp->force_packid, |
| 2510 | (int) fp->keep_orphan, (int) fp->closed); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2511 | for (m = 0, srp = fp->headrp; |
| 2512 | srp != NULL; |
| 2513 | ++m, srp = srp->nextrp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | hp = &srp->header; |
| 2515 | new_interface = (hp->interface_id == '\0') ? 0 : 1; |
| 2516 | if (srp->res_used) { |
| 2517 | if (new_interface && |
| 2518 | (SG_FLAG_MMAP_IO & hp->flags)) |
| 2519 | cp = " mmap>> "; |
| 2520 | else |
| 2521 | cp = " rb>> "; |
| 2522 | } else { |
| 2523 | if (SG_INFO_DIRECT_IO_MASK & hp->info) |
| 2524 | cp = " dio>> "; |
| 2525 | else |
| 2526 | cp = " "; |
| 2527 | } |
| 2528 | seq_printf(s, cp); |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 2529 | blen = srp->data.bufflen; |
| 2530 | usg = srp->data.k_use_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | seq_printf(s, srp->done ? |
| 2532 | ((1 == srp->done) ? "rcv:" : "fin:") |
Mike Christie | d6b1034 | 2005-11-08 04:06:41 -0600 | [diff] [blame] | 2533 | : "act:"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | seq_printf(s, " id=%d blen=%d", |
| 2535 | srp->header.pack_id, blen); |
| 2536 | if (srp->done) |
| 2537 | seq_printf(s, " dur=%d", hp->duration); |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 2538 | else { |
| 2539 | ms = jiffies_to_msecs(jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | seq_printf(s, " t_o/elap=%d/%d", |
| cb59e84 | 2005-04-02 13:51:23 -0600 | [diff] [blame] | 2541 | (new_interface ? hp->timeout : |
| 2542 | jiffies_to_msecs(fp->timeout)), |
| 2543 | (ms > hp->duration ? ms - hp->duration : 0)); |
| 2544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | seq_printf(s, "ms sgat=%d op=0x%02x\n", usg, |
| 2546 | (int) srp->data.cmd_opcode); |
| 2547 | } |
| 2548 | if (0 == m) |
| 2549 | seq_printf(s, " No requests active\n"); |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2550 | read_unlock(&fp->rq_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | static int sg_proc_open_debug(struct inode *inode, struct file *file) |
| 2555 | { |
| 2556 | return seq_open(file, &debug_seq_ops); |
| 2557 | } |
| 2558 | |
| 2559 | static int sg_proc_seq_show_debug(struct seq_file *s, void *v) |
| 2560 | { |
| 2561 | struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; |
| 2562 | Sg_device *sdp; |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2563 | unsigned long iflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | |
| 2565 | if (it && (0 == it->index)) { |
James Bottomley | 7c07d61 | 2007-08-05 13:36:11 -0500 | [diff] [blame] | 2566 | seq_printf(s, "max_active_device=%d(origin 1)\n", |
| 2567 | (int)it->max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | seq_printf(s, " def_reserved_size=%d\n", sg_big_buff); |
| 2569 | } |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2570 | |
| 2571 | read_lock_irqsave(&sg_index_lock, iflags); |
| 2572 | sdp = it ? sg_lookup_dev(it->index) : NULL; |
| 2573 | if (sdp && sdp->headfp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | struct scsi_device *scsidp = sdp->device; |
| 2575 | |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2576 | seq_printf(s, " >>> device=%s ", sdp->disk->disk_name); |
| 2577 | if (sdp->detached) |
| 2578 | seq_printf(s, "detached pending close "); |
| 2579 | else |
| 2580 | seq_printf |
| 2581 | (s, "scsi%d chan=%d id=%d lun=%d em=%d", |
| 2582 | scsidp->host->host_no, |
| 2583 | scsidp->channel, scsidp->id, |
| 2584 | scsidp->lun, |
| 2585 | scsidp->host->hostt->emulated); |
| 2586 | seq_printf(s, " sg_tablesize=%d excl=%d\n", |
| 2587 | sdp->sg_tablesize, sdp->exclude); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | sg_proc_debug_helper(s, sdp); |
| 2589 | } |
Tony Battersby | c6517b7 | 2009-01-21 14:45:50 -0500 | [diff] [blame^] | 2590 | read_unlock_irqrestore(&sg_index_lock, iflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | return 0; |
| 2592 | } |
| 2593 | |
| 2594 | #endif /* CONFIG_SCSI_PROC_FS */ |
| 2595 | |
| 2596 | module_init(init_sg); |
| 2597 | module_exit(exit_sg); |