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