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