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