blob: be812e09058dd1cb2c60e1d27d5b23e6b026cc47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * History:
3 * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
4 * to allow user process control of SCSI devices.
5 * Development Sponsored by Killy Corp. NY NY
6 *
7 * Original driver (sg.c):
8 * Copyright (C) 1992 Lawrence Foard
9 * Version 2 and 3 extensions to driver:
10 * Copyright (C) 1998 - 2005 Douglas Gilbert
11 *
12 * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 */
20
Douglas Gilbertb2155d02006-08-19 00:11:34 -040021static int sg_version_num = 30534; /* 2 digits for each component */
22#define SG_VERSION_STR "3.5.34"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
26 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
27 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
28 * (otherwise the macros compile to empty statements).
29 *
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32
33#include <linux/fs.h>
34#include <linux/kernel.h>
35#include <linux/sched.h>
36#include <linux/string.h>
37#include <linux/mm.h>
38#include <linux/errno.h>
39#include <linux/mtio.h>
40#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/fcntl.h>
43#include <linux/init.h>
44#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/cdev.h>
James Bottomley7c07d612007-08-05 13:36:11 -050047#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/seq_file.h>
49#include <linux/blkdev.h>
50#include <linux/delay.h>
Christof Schmitt6da127a2008-01-11 10:09:43 +010051#include <linux/blktrace_api.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020052#include <linux/mutex.h>
Christian Dietrich2fe038e2011-06-04 17:36:10 +020053#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include "scsi.h"
db9dff32005-04-03 14:53:59 -050056#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <scsi/scsi_host.h>
58#include <scsi/scsi_driver.h>
59#include <scsi/scsi_ioctl.h>
60#include <scsi/sg.h>
61
62#include "scsi_logging.h"
63
64#ifdef CONFIG_SCSI_PROC_FS
65#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040066static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static int sg_proc_init(void);
69static void sg_proc_cleanup(void);
70#endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define SG_ALLOW_DIO_DEF 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
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
89int 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).] */
96static int def_reserved_size = -1; /* picks up init parameter */
97static int sg_allow_dio = SG_ALLOW_DIO_DEF;
98
Douglas Gilbert6460e752006-09-20 18:20:49 -040099static int scatter_elem_sz = SG_SCATTER_SZ;
100static int scatter_elem_sz_prev = SG_SCATTER_SZ;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define SG_SECTOR_SZ 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Tony Jonesee959b02008-02-22 00:13:36 +0100104static int sg_add(struct device *, struct class_interface *);
105static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200107static DEFINE_MUTEX(sg_mutex);
108
James Bottomley7c07d612007-08-05 13:36:11 -0500109static DEFINE_IDR(sg_index_idr);
110static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 file descriptor list for device */
112
113static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100114 .add_dev = sg_add,
115 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
119 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900120 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200122 struct page **pages;
123 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
125 unsigned char cmd_opcode; /* first byte of command */
126} Sg_scatter_hold;
127
128struct sg_device; /* forward declarations */
129struct sg_fd;
130
131typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct sg_request *nextrp; /* NULL -> tail request (slist) */
133 struct sg_fd *parentfp; /* NULL -> not in use */
134 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
135 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600136 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
138 char orphan; /* 1 -> drop on sight, 0 -> normal */
139 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
140 volatile char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900141 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900142 struct bio *bio;
FUJITA Tomonoric96952e2009-02-04 11:36:27 +0900143 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144} Sg_request;
145
146typedef struct sg_fd { /* holds the state of a file descriptor */
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900147 struct list_head sfd_siblings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct sg_device *parentdp; /* owning device */
149 wait_queue_head_t read_wait; /* queue read until command done */
150 rwlock_t rq_list_lock; /* protect access to list in req_arr */
151 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
152 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
153 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
154 unsigned save_scat_len; /* original length of trunc. scat. element */
155 Sg_request *headrp; /* head of request slist, NULL->empty */
156 struct fasync_struct *async_qp; /* used by asynchronous notification */
157 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
158 char low_dma; /* as in parent but possibly overridden to 1 */
159 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
160 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
161 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
162 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
163 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
164 char mmap_called; /* 0 -> mmap() never called on this fd */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500165 struct kref f_ref;
166 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167} Sg_fd;
168
169typedef struct sg_device { /* holds the state of each scsi generic device */
170 struct scsi_device *device;
171 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
172 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500173 u32 index; /* device index number */
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900174 struct list_head sfds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 volatile char detached; /* 0->attached, 1->detached pending removal */
176 volatile char exclude; /* opened for exclusive access */
177 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
178 struct gendisk *disk;
179 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500180 struct kref d_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181} Sg_device;
182
Mike Christied6b10342005-11-08 04:06:41 -0600183/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900184static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900185static int sg_start_req(Sg_request *srp, unsigned char *cmd);
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +0900186static int sg_finish_rem_req(Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
189 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200190static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
191 const char __user *buf, size_t count, int blocking,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500192 int read_only, int sg_io_owned, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
194 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196static void sg_remove_scat(Sg_scatter_hold * schp);
197static void sg_build_reserve(Sg_fd * sfp, int req_size);
198static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500201static void sg_remove_sfp(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
203static Sg_request *sg_add_request(Sg_fd * sfp);
204static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
205static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static Sg_device *sg_get_dev(int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500207static void sg_put_dev(Sg_device *sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#define SZ_SG_HEADER sizeof(struct sg_header)
210#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
211#define SZ_SG_IOVEC sizeof(sg_iovec_t)
212#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
213
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900214static int sg_allow_access(struct file *filp, unsigned char *cmd)
215{
Joe Perches35df8392010-09-04 18:52:46 -0700216 struct sg_fd *sfp = filp->private_data;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900217
218 if (sfp->parentdp->device->type == TYPE_SCANNER)
219 return 0;
220
Jens Axboe018e0442009-06-26 16:27:10 +0200221 return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int
225sg_open(struct inode *inode, struct file *filp)
226{
227 int dev = iminor(inode);
228 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600229 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 Sg_device *sdp;
231 Sg_fd *sfp;
232 int res;
233 int retval;
234
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200235 mutex_lock(&sg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 nonseekable_open(inode, filp);
237 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
238 sdp = sg_get_dev(dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500239 if (IS_ERR(sdp)) {
240 retval = PTR_ERR(sdp);
241 sdp = NULL;
242 goto sg_put;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* This driver's module count bumped by fops_get in <linux/fs.h> */
246 /* Prevent the device driver from vanishing while we sleep */
247 retval = scsi_device_get(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500248 if (retval)
249 goto sg_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alan Sternbc4f2402010-06-17 10:41:42 -0400251 retval = scsi_autopm_get_device(sdp->device);
252 if (retval)
253 goto sdp_put;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!((flags & O_NONBLOCK) ||
256 scsi_block_when_processing_errors(sdp->device))) {
257 retval = -ENXIO;
258 /* we are in error recovery for this device */
259 goto error_out;
260 }
261
262 if (flags & O_EXCL) {
263 if (O_RDONLY == (flags & O_ACCMODE)) {
264 retval = -EPERM; /* Can't lock it with read only access */
265 goto error_out;
266 }
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900267 if (!list_empty(&sdp->sfds) && (flags & O_NONBLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 retval = -EBUSY;
269 goto error_out;
270 }
271 res = 0;
272 __wait_event_interruptible(sdp->o_excl_wait,
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900273 ((!list_empty(&sdp->sfds) || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (res) {
275 retval = res; /* -ERESTARTSYS because signal hit process */
276 goto error_out;
277 }
278 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
279 if (flags & O_NONBLOCK) {
280 retval = -EBUSY;
281 goto error_out;
282 }
283 res = 0;
284 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
285 res);
286 if (res) {
287 retval = res; /* -ERESTARTSYS because signal hit process */
288 goto error_out;
289 }
290 }
291 if (sdp->detached) {
292 retval = -ENODEV;
293 goto error_out;
294 }
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900295 if (list_empty(&sdp->sfds)) { /* no existing opens on this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600297 q = sdp->device->request_queue;
Martin K. Petersen8a783622010-02-26 00:20:39 -0500298 sdp->sg_tablesize = queue_max_segments(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 if ((sfp = sg_add_sfp(sdp, dev)))
301 filp->private_data = sfp;
302 else {
Tony Battersbyc6517b792009-01-21 14:45:50 -0500303 if (flags & O_EXCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 sdp->exclude = 0; /* undo if error */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500305 wake_up_interruptible(&sdp->o_excl_wait);
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 retval = -ENOMEM;
308 goto error_out;
309 }
Tony Battersbyc6517b792009-01-21 14:45:50 -0500310 retval = 0;
311error_out:
Alan Sternbc4f2402010-06-17 10:41:42 -0400312 if (retval) {
313 scsi_autopm_put_device(sdp->device);
314sdp_put:
Tony Battersbyc6517b792009-01-21 14:45:50 -0500315 scsi_device_put(sdp->device);
Alan Sternbc4f2402010-06-17 10:41:42 -0400316 }
Tony Battersbyc6517b792009-01-21 14:45:50 -0500317sg_put:
318 if (sdp)
319 sg_put_dev(sdp);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200320 mutex_unlock(&sg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return retval;
322}
323
324/* Following function was formerly called 'sg_close' */
325static int
326sg_release(struct inode *inode, struct file *filp)
327{
328 Sg_device *sdp;
329 Sg_fd *sfp;
330
331 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
332 return -ENXIO;
333 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
Tony Battersbyc6517b792009-01-21 14:45:50 -0500334
335 sfp->closed = 1;
336
337 sdp->exclude = 0;
338 wake_up_interruptible(&sdp->o_excl_wait);
339
Alan Sternbc4f2402010-06-17 10:41:42 -0400340 scsi_autopm_put_device(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500341 kref_put(&sfp->f_ref, sg_remove_sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343}
344
345static ssize_t
346sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 Sg_device *sdp;
349 Sg_fd *sfp;
350 Sg_request *srp;
351 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600353 struct sg_header *old_hdr = NULL;
354 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
357 return -ENXIO;
358 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
359 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!access_ok(VERIFY_WRITE, buf, count))
362 return -EFAULT;
363 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600364 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
365 if (!old_hdr)
366 return -ENOMEM;
367 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
368 retval = -EFAULT;
369 goto free_old_hdr;
370 }
371 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600373 sg_io_hdr_t *new_hdr;
374 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
375 if (!new_hdr) {
376 retval = -ENOMEM;
377 goto free_old_hdr;
378 }
379 retval =__copy_from_user
380 (new_hdr, buf, SZ_SG_IO_HDR);
381 req_pack_id = new_hdr->pack_id;
382 kfree(new_hdr);
383 if (retval) {
384 retval = -EFAULT;
385 goto free_old_hdr;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 } else
cb59e842005-04-02 13:51:23 -0600389 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 srp = sg_get_rq_mark(sfp, req_pack_id);
392 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600393 if (sdp->detached) {
394 retval = -ENODEV;
395 goto free_old_hdr;
396 }
397 if (filp->f_flags & O_NONBLOCK) {
398 retval = -EAGAIN;
399 goto free_old_hdr;
400 }
Jörn Engel794c10f2012-04-12 17:32:48 -0400401 retval = 0; /* following macro beats race condition */
402 __wait_event_interruptible(sfp->read_wait,
403 (sdp->detached ||
404 (srp = sg_get_rq_mark(sfp, req_pack_id))), retval);
405 if (sdp->detached) {
406 retval = -ENODEV;
407 goto free_old_hdr;
408 }
409 if (retval) {
cb59e842005-04-02 13:51:23 -0600410 /* -ERESTARTSYS as signal hit process */
411 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
cb59e842005-04-02 13:51:23 -0600414 if (srp->header.interface_id != '\0') {
415 retval = sg_new_read(sfp, buf, count, srp);
416 goto free_old_hdr;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600420 if (old_hdr == NULL) {
421 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
422 if (! old_hdr) {
423 retval = -ENOMEM;
424 goto free_old_hdr;
425 }
426 }
427 memset(old_hdr, 0, SZ_SG_HEADER);
428 old_hdr->reply_len = (int) hp->timeout;
429 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
430 old_hdr->pack_id = hp->pack_id;
431 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600433 old_hdr->target_status = hp->masked_status;
434 old_hdr->host_status = hp->host_status;
435 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if ((CHECK_CONDITION & hp->masked_status) ||
437 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600438 memcpy(old_hdr->sense_buffer, srp->sense_b,
439 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 switch (hp->host_status) {
441 /* This setup of 'result' is for backward compatibility and is best
442 ignored by the user who should use target, host + driver status */
443 case DID_OK:
444 case DID_PASSTHROUGH:
445 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case DID_NO_CONNECT:
449 case DID_BUS_BUSY:
450 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600451 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case DID_BAD_TARGET:
454 case DID_ABORT:
455 case DID_PARITY:
456 case DID_RESET:
457 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600458 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600461 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 hp->masked_status == GOOD) ? 0 : EIO;
463 break;
464 default:
cb59e842005-04-02 13:51:23 -0600465 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 }
468
469 /* Now copy the result back to the user buffer. */
470 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600471 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
472 retval = -EFAULT;
473 goto free_old_hdr;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600476 if (count > old_hdr->reply_len)
477 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600479 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
480 retval = -EFAULT;
481 goto free_old_hdr;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 } else
cb59e842005-04-02 13:51:23 -0600485 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600487 retval = count;
488free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800489 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600490 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static ssize_t
494sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
495{
496 sg_io_hdr_t *hp = &srp->header;
497 int err = 0;
498 int len;
499
500 if (count < SZ_SG_IO_HDR) {
501 err = -EINVAL;
502 goto err_out;
503 }
504 hp->sb_len_wr = 0;
505 if ((hp->mx_sb_len > 0) && hp->sbp) {
506 if ((CHECK_CONDITION & hp->masked_status) ||
507 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600508 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
510 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
511 len = (len > sb_len) ? sb_len : len;
512 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
513 err = -EFAULT;
514 goto err_out;
515 }
516 hp->sb_len_wr = len;
517 }
518 }
519 if (hp->masked_status || hp->host_status || hp->driver_status)
520 hp->info |= SG_INFO_CHECK;
521 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
522 err = -EFAULT;
523 goto err_out;
524 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900525err_out:
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +0900526 err = sg_finish_rem_req(srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return (0 == err) ? count : err;
528}
529
530static ssize_t
531sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
532{
533 int mxsize, cmd_size, k;
534 int input_size, blocking;
535 unsigned char opcode;
536 Sg_device *sdp;
537 Sg_fd *sfp;
538 Sg_request *srp;
539 struct sg_header old_hdr;
540 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600541 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
544 return -ENXIO;
545 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
546 sdp->disk->disk_name, (int) count));
547 if (sdp->detached)
548 return -ENODEV;
549 if (!((filp->f_flags & O_NONBLOCK) ||
550 scsi_block_when_processing_errors(sdp->device)))
551 return -ENXIO;
552
553 if (!access_ok(VERIFY_READ, buf, count))
554 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
555 if (count < SZ_SG_HEADER)
556 return -EIO;
557 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
558 return -EFAULT;
559 blocking = !(filp->f_flags & O_NONBLOCK);
560 if (old_hdr.reply_len < 0)
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500561 return sg_new_write(sfp, filp, buf, count,
562 blocking, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (count < (SZ_SG_HEADER + 6))
564 return -EIO; /* The minimum scsi command length is 6 bytes. */
565
566 if (!(srp = sg_add_request(sfp))) {
567 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
568 return -EDOM;
569 }
570 buf += SZ_SG_HEADER;
571 __get_user(opcode, buf);
572 if (sfp->next_cmd_len > 0) {
573 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
574 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
575 sfp->next_cmd_len = 0;
576 sg_remove_request(sfp, srp);
577 return -EIO;
578 }
579 cmd_size = sfp->next_cmd_len;
580 sfp->next_cmd_len = 0; /* reset so only this write() effected */
581 } else {
582 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
583 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
584 cmd_size = 12;
585 }
586 SCSI_LOG_TIMEOUT(4, printk(
587 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
588/* Determine buffer size. */
589 input_size = count - cmd_size;
590 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
591 mxsize -= SZ_SG_HEADER;
592 input_size -= SZ_SG_HEADER;
593 if (input_size < 0) {
594 sg_remove_request(sfp, srp);
595 return -EIO; /* User did not pass enough bytes for this command. */
596 }
597 hp = &srp->header;
598 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
599 hp->cmd_len = (unsigned char) cmd_size;
600 hp->iovec_count = 0;
601 hp->mx_sb_len = 0;
602 if (input_size > 0)
603 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
604 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
605 else
606 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
607 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900608 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
609 hp->dxferp = (char __user *)buf + cmd_size;
610 else
611 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 hp->sbp = NULL;
613 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
614 hp->flags = input_size; /* structure abuse ... */
615 hp->pack_id = old_hdr.pack_id;
616 hp->usr_ptr = NULL;
617 if (__copy_from_user(cmnd, buf, cmd_size))
618 return -EFAULT;
619 /*
620 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
621 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
622 * is a non-zero input_size, so emit a warning.
623 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100624 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
625 static char cmd[TASK_COMM_LEN];
Christian Dietrich2fe038e2011-06-04 17:36:10 +0200626 if (strcmp(current->comm, cmd)) {
627 printk_ratelimited(KERN_WARNING
628 "sg_write: data in/out %d/%d bytes "
629 "for SCSI command 0x%x-- guessing "
630 "data in;\n program %s not setting "
631 "count and/or reply_len properly\n",
632 old_hdr.reply_len - (int)SZ_SG_HEADER,
633 input_size, (unsigned int) cmnd[0],
634 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100635 strcpy(cmd, current->comm);
636 }
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
639 return (k < 0) ? k : count;
640}
641
642static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200643sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500644 size_t count, int blocking, int read_only, int sg_io_owned,
Adel Gadllah0b07de82008-06-26 13:48:27 +0200645 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 int k;
648 Sg_request *srp;
649 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600650 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int timeout;
652 unsigned long ul_timeout;
653
654 if (count < SZ_SG_IO_HDR)
655 return -EINVAL;
656 if (!access_ok(VERIFY_READ, buf, count))
657 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
658
659 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
660 if (!(srp = sg_add_request(sfp))) {
661 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
662 return -EDOM;
663 }
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500664 srp->sg_io_owned = sg_io_owned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 hp = &srp->header;
666 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
667 sg_remove_request(sfp, srp);
668 return -EFAULT;
669 }
670 if (hp->interface_id != 'S') {
671 sg_remove_request(sfp, srp);
672 return -ENOSYS;
673 }
674 if (hp->flags & SG_FLAG_MMAP_IO) {
675 if (hp->dxfer_len > sfp->reserve.bufflen) {
676 sg_remove_request(sfp, srp);
677 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
678 }
679 if (hp->flags & SG_FLAG_DIRECT_IO) {
680 sg_remove_request(sfp, srp);
681 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
682 }
683 if (sg_res_in_use(sfp)) {
684 sg_remove_request(sfp, srp);
685 return -EBUSY; /* reserve buffer already being used */
686 }
687 }
688 ul_timeout = msecs_to_jiffies(srp->header.timeout);
689 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
690 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
691 sg_remove_request(sfp, srp);
692 return -EMSGSIZE;
693 }
694 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
695 sg_remove_request(sfp, srp);
696 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
697 }
698 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
699 sg_remove_request(sfp, srp);
700 return -EFAULT;
701 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900702 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 sg_remove_request(sfp, srp);
704 return -EPERM;
705 }
706 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
707 if (k < 0)
708 return k;
709 if (o_srp)
710 *o_srp = srp;
711 return count;
712}
713
714static int
715sg_common_write(Sg_fd * sfp, Sg_request * srp,
716 unsigned char *cmnd, int timeout, int blocking)
717{
Mike Christied6b10342005-11-08 04:06:41 -0600718 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 Sg_device *sdp = sfp->parentdp;
720 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
723 hp->status = 0;
724 hp->masked_status = 0;
725 hp->msg_status = 0;
726 hp->info = 0;
727 hp->host_status = 0;
728 hp->driver_status = 0;
729 hp->resid = 0;
730 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
731 (int) cmnd[0], (int) hp->cmd_len));
732
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900733 k = sg_start_req(srp, cmnd);
734 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400735 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 sg_finish_rem_req(srp);
737 return k; /* probably out of space --> ENOMEM */
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (sdp->detached) {
FUJITA Tomonoricaf19d32010-07-22 09:36:51 +0900740 if (srp->bio)
741 blk_end_request_all(srp->rq, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 sg_finish_rem_req(srp);
743 return -ENODEV;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 switch (hp->dxfer_direction) {
747 case SG_DXFER_TO_FROM_DEV:
748 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600749 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600752 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600755 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
757 default:
Mike Christied6b10342005-11-08 04:06:41 -0600758 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760 }
cb59e842005-04-02 13:51:23 -0600761 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200762
763 srp->rq->timeout = timeout;
Tony Battersbyc6517b792009-01-21 14:45:50 -0500764 kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200765 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
766 srp->rq, 1, sg_rq_end_io);
767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static int
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200771sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
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;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
785 sdp->disk->disk_name, (int) cmd_in));
786 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
787
788 switch (cmd_in) {
789 case SG_IO:
Jörn Engeldddbf8d2012-04-12 17:32:17 -0400790 if (sdp->detached)
791 return -ENODEV;
792 if (!scsi_block_when_processing_errors(sdp->device))
793 return -ENXIO;
794 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
795 return -EFAULT;
796 result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
797 1, read_only, 1, &srp);
798 if (result < 0)
799 return result;
Jörn Engel794c10f2012-04-12 17:32:48 -0400800 result = 0; /* following macro to beat race condition */
801 __wait_event_interruptible(sfp->read_wait,
802 (srp->done || sdp->detached), result);
803 if (sdp->detached)
804 return -ENODEV;
805 write_lock_irq(&sfp->rq_list_lock);
806 if (srp->done) {
807 srp->done = 2;
Jörn Engeldddbf8d2012-04-12 17:32:17 -0400808 write_unlock_irq(&sfp->rq_list_lock);
Jörn Engel794c10f2012-04-12 17:32:48 -0400809 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
810 return (result < 0) ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Jörn Engel794c10f2012-04-12 17:32:48 -0400812 srp->orphan = 1;
813 write_unlock_irq(&sfp->rq_list_lock);
814 return result; /* -ERESTARTSYS because signal hit process */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case SG_SET_TIMEOUT:
816 result = get_user(val, ip);
817 if (result)
818 return result;
819 if (val < 0)
820 return -EIO;
821 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
822 val = MULDIV (INT_MAX, USER_HZ, HZ);
823 sfp->timeout_user = val;
824 sfp->timeout = MULDIV (val, HZ, USER_HZ);
825
826 return 0;
827 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
828 /* strange ..., for backward compatibility */
829 return sfp->timeout_user;
830 case SG_SET_FORCE_LOW_DMA:
831 result = get_user(val, ip);
832 if (result)
833 return result;
834 if (val) {
835 sfp->low_dma = 1;
836 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
837 val = (int) sfp->reserve.bufflen;
838 sg_remove_scat(&sfp->reserve);
839 sg_build_reserve(sfp, val);
840 }
841 } else {
842 if (sdp->detached)
843 return -ENODEV;
844 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
845 }
846 return 0;
847 case SG_GET_LOW_DMA:
848 return put_user((int) sfp->low_dma, ip);
849 case SG_GET_SCSI_ID:
850 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
851 return -EFAULT;
852 else {
853 sg_scsi_id_t __user *sg_idp = p;
854
855 if (sdp->detached)
856 return -ENODEV;
857 __put_user((int) sdp->device->host->host_no,
858 &sg_idp->host_no);
859 __put_user((int) sdp->device->channel,
860 &sg_idp->channel);
861 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
862 __put_user((int) sdp->device->lun, &sg_idp->lun);
863 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
864 __put_user((short) sdp->device->host->cmd_per_lun,
865 &sg_idp->h_cmd_per_lun);
866 __put_user((short) sdp->device->queue_depth,
867 &sg_idp->d_queue_depth);
868 __put_user(0, &sg_idp->unused[0]);
869 __put_user(0, &sg_idp->unused[1]);
870 return 0;
871 }
872 case SG_SET_FORCE_PACK_ID:
873 result = get_user(val, ip);
874 if (result)
875 return result;
876 sfp->force_packid = val ? 1 : 0;
877 return 0;
878 case SG_GET_PACK_ID:
879 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
880 return -EFAULT;
881 read_lock_irqsave(&sfp->rq_list_lock, iflags);
882 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
883 if ((1 == srp->done) && (!srp->sg_io_owned)) {
884 read_unlock_irqrestore(&sfp->rq_list_lock,
885 iflags);
886 __put_user(srp->header.pack_id, ip);
887 return 0;
888 }
889 }
890 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
891 __put_user(-1, ip);
892 return 0;
893 case SG_GET_NUM_WAITING:
894 read_lock_irqsave(&sfp->rq_list_lock, iflags);
895 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
896 if ((1 == srp->done) && (!srp->sg_io_owned))
897 ++val;
898 }
899 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
900 return put_user(val, ip);
901 case SG_GET_SG_TABLESIZE:
902 return put_user(sdp->sg_tablesize, ip);
903 case SG_SET_RESERVED_SIZE:
904 result = get_user(val, ip);
905 if (result)
906 return result;
907 if (val < 0)
908 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500909 val = min_t(int, val,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400910 queue_max_sectors(sdp->device->request_queue) * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (val != sfp->reserve.bufflen) {
912 if (sg_res_in_use(sfp) || sfp->mmap_called)
913 return -EBUSY;
914 sg_remove_scat(&sfp->reserve);
915 sg_build_reserve(sfp, val);
916 }
917 return 0;
918 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500919 val = min_t(int, sfp->reserve.bufflen,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400920 queue_max_sectors(sdp->device->request_queue) * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return put_user(val, ip);
922 case SG_SET_COMMAND_Q:
923 result = get_user(val, ip);
924 if (result)
925 return result;
926 sfp->cmd_q = val ? 1 : 0;
927 return 0;
928 case SG_GET_COMMAND_Q:
929 return put_user((int) sfp->cmd_q, ip);
930 case SG_SET_KEEP_ORPHAN:
931 result = get_user(val, ip);
932 if (result)
933 return result;
934 sfp->keep_orphan = val;
935 return 0;
936 case SG_GET_KEEP_ORPHAN:
937 return put_user((int) sfp->keep_orphan, ip);
938 case SG_NEXT_CMD_LEN:
939 result = get_user(val, ip);
940 if (result)
941 return result;
942 sfp->next_cmd_len = (val > 0) ? val : 0;
943 return 0;
944 case SG_GET_VERSION_NUM:
945 return put_user(sg_version_num, ip);
946 case SG_GET_ACCESS_COUNT:
947 /* faked - we don't have a real access count anymore */
948 val = (sdp->device ? 1 : 0);
949 return put_user(val, ip);
950 case SG_GET_REQUEST_TABLE:
951 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
952 return -EFAULT;
953 else {
cb59e842005-04-02 13:51:23 -0600954 sg_req_info_t *rinfo;
955 unsigned int ms;
956
957 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
958 GFP_KERNEL);
959 if (!rinfo)
960 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 read_lock_irqsave(&sfp->rq_list_lock, iflags);
962 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
963 ++val, srp = srp ? srp->nextrp : srp) {
964 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
965 if (srp) {
966 rinfo[val].req_state = srp->done + 1;
967 rinfo[val].problem =
968 srp->header.masked_status &
969 srp->header.host_status &
970 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600971 if (srp->done)
972 rinfo[val].duration =
973 srp->header.duration;
974 else {
975 ms = jiffies_to_msecs(jiffies);
976 rinfo[val].duration =
977 (ms > srp->header.duration) ?
978 (ms - srp->header.duration) : 0;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600981 rinfo[val].sg_io_owned =
982 srp->sg_io_owned;
983 rinfo[val].pack_id =
984 srp->header.pack_id;
985 rinfo[val].usr_ptr =
986 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 }
989 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -0600990 result = __copy_to_user(p, rinfo,
991 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
992 result = result ? -EFAULT : 0;
993 kfree(rinfo);
994 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 case SG_EMULATED_HOST:
997 if (sdp->detached)
998 return -ENODEV;
999 return put_user(sdp->device->host->hostt->emulated, ip);
1000 case SG_SCSI_RESET:
1001 if (sdp->detached)
1002 return -ENODEV;
1003 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001004 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EBUSY;
1006 } else if (!scsi_block_when_processing_errors(sdp->device))
1007 return -EBUSY;
1008 result = get_user(val, ip);
1009 if (result)
1010 return result;
1011 if (SG_SCSI_RESET_NOTHING == val)
1012 return 0;
1013 switch (val) {
1014 case SG_SCSI_RESET_DEVICE:
1015 val = SCSI_TRY_RESET_DEVICE;
1016 break;
Brian King39120e12008-07-01 13:03:19 -05001017 case SG_SCSI_RESET_TARGET:
1018 val = SCSI_TRY_RESET_TARGET;
1019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 case SG_SCSI_RESET_BUS:
1021 val = SCSI_TRY_RESET_BUS;
1022 break;
1023 case SG_SCSI_RESET_HOST:
1024 val = SCSI_TRY_RESET_HOST;
1025 break;
1026 default:
1027 return -EINVAL;
1028 }
1029 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1030 return -EACCES;
1031 return (scsi_reset_provider(sdp->device, val) ==
1032 SUCCESS) ? 0 : -EIO;
1033 case SCSI_IOCTL_SEND_COMMAND:
1034 if (sdp->detached)
1035 return -ENODEV;
1036 if (read_only) {
1037 unsigned char opcode = WRITE_6;
1038 Scsi_Ioctl_Command __user *siocp = p;
1039
1040 if (copy_from_user(&opcode, siocp->data, 1))
1041 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001042 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -EPERM;
1044 }
Al Viroe915e872008-09-02 17:16:41 -04001045 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 case SG_SET_DEBUG:
1047 result = get_user(val, ip);
1048 if (result)
1049 return result;
1050 sdp->sgdebug = (char) val;
1051 return 0;
1052 case SCSI_IOCTL_GET_IDLUN:
1053 case SCSI_IOCTL_GET_BUS_NUMBER:
1054 case SCSI_IOCTL_PROBE_HOST:
1055 case SG_GET_TRANSFORM:
1056 if (sdp->detached)
1057 return -ENODEV;
1058 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001059 case BLKSECTGET:
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001060 return put_user(queue_max_sectors(sdp->device->request_queue) * 512,
Alan Stern44ec9542007-02-20 11:01:57 -05001061 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001062 case BLKTRACESETUP:
1063 return blk_trace_setup(sdp->device->request_queue,
1064 sdp->disk->disk_name,
Martin Peschke76e3a192009-01-30 15:46:23 +01001065 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
Shawn Dud0deef52009-04-14 13:58:56 +08001066 NULL,
Christof Schmitt6da127a2008-01-11 10:09:43 +01001067 (char *)arg);
1068 case BLKTRACESTART:
1069 return blk_trace_startstop(sdp->device->request_queue, 1);
1070 case BLKTRACESTOP:
1071 return blk_trace_startstop(sdp->device->request_queue, 0);
1072 case BLKTRACETEARDOWN:
1073 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 default:
1075 if (read_only)
1076 return -EPERM; /* don't know so take safe approach */
1077 return scsi_ioctl(sdp->device, cmd_in, p);
1078 }
1079}
1080
Arnd Bergmannf4927c42010-04-27 00:24:01 +02001081static long
1082sg_unlocked_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1083{
1084 int ret;
1085
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02001086 mutex_lock(&sg_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02001087 ret = sg_ioctl(filp, cmd_in, arg);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02001088 mutex_unlock(&sg_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02001089
1090 return ret;
1091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#ifdef CONFIG_COMPAT
1094static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1095{
1096 Sg_device *sdp;
1097 Sg_fd *sfp;
1098 struct scsi_device *sdev;
1099
1100 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1101 return -ENXIO;
1102
1103 sdev = sdp->device;
1104 if (sdev->host->hostt->compat_ioctl) {
1105 int ret;
1106
1107 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1108
1109 return ret;
1110 }
1111
1112 return -ENOIOCTLCMD;
1113}
1114#endif
1115
1116static unsigned int
1117sg_poll(struct file *filp, poll_table * wait)
1118{
1119 unsigned int res = 0;
1120 Sg_device *sdp;
1121 Sg_fd *sfp;
1122 Sg_request *srp;
1123 int count = 0;
1124 unsigned long iflags;
1125
1126 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1127 || sfp->closed)
1128 return POLLERR;
1129 poll_wait(filp, &sfp->read_wait, wait);
1130 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1131 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1132 /* if any read waiting, flag it */
1133 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1134 res = POLLIN | POLLRDNORM;
1135 ++count;
1136 }
1137 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1138
1139 if (sdp->detached)
1140 res |= POLLHUP;
1141 else if (!sfp->cmd_q) {
1142 if (0 == count)
1143 res |= POLLOUT | POLLWRNORM;
1144 } else if (count < SG_MAX_QUEUE)
1145 res |= POLLOUT | POLLWRNORM;
1146 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1147 sdp->disk->disk_name, (int) res));
1148 return res;
1149}
1150
1151static int
1152sg_fasync(int fd, struct file *filp, int mode)
1153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 Sg_device *sdp;
1155 Sg_fd *sfp;
1156
1157 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1158 return -ENXIO;
1159 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1160 sdp->disk->disk_name, mode));
1161
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001162 return fasync_helper(fd, filp, mode, &sfp->async_qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Nick Piggina13ff0b2008-02-07 18:46:06 -08001165static int
1166sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001169 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001171 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001174 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001176 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001178 return VM_FAULT_SIGBUS;
1179 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001181 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001182 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1183 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001184 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001185 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001186 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001187 struct page *page = nth_page(rsv_schp->pages[k],
1188 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001189 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001190 vmf->page = page;
1191 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Mike Christied6b10342005-11-08 04:06:41 -06001193 sa += len;
1194 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Mike Christied6b10342005-11-08 04:06:41 -06001196
Nick Piggina13ff0b2008-02-07 18:46:06 -08001197 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001200static const struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001201 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202};
1203
1204static int
1205sg_mmap(struct file *filp, struct vm_area_struct *vma)
1206{
1207 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001208 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001210 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1213 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001214 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1216 (void *) vma->vm_start, (int) req_sz));
1217 if (vma->vm_pgoff)
1218 return -EINVAL; /* want no offset */
1219 rsv_schp = &sfp->reserve;
1220 if (req_sz > rsv_schp->bufflen)
1221 return -ENOMEM; /* cannot map more than reserved buffer */
1222
Mike Christied6b10342005-11-08 04:06:41 -06001223 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001224 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1225 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001226 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001227 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001228 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Mike Christied6b10342005-11-08 04:06:41 -06001230
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001231 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001232 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 vma->vm_private_data = sfp;
1234 vma->vm_ops = &sg_mmap_vm_ops;
1235 return 0;
1236}
1237
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001238static void sg_rq_end_io_usercontext(struct work_struct *work)
1239{
1240 struct sg_request *srp = container_of(work, struct sg_request, ew.work);
1241 struct sg_fd *sfp = srp->parentfp;
1242
1243 sg_finish_rem_req(srp);
1244 kref_put(&sfp->f_ref, sg_remove_sfp);
1245}
1246
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001247/*
1248 * This function is a "bottom half" handler that is called by the mid
1249 * level when a command is completed (or has failed).
1250 */
1251static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001253 struct sg_request *srp = rq->end_io_data;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001254 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001257 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001258 char *sense;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001259 int result, resid, done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Tony Battersbyc6517b792009-01-21 14:45:50 -05001261 if (WARN_ON(srp->done != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sfp = srp->parentfp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001265 if (WARN_ON(sfp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001267
1268 sdp = sfp->parentdp;
1269 if (unlikely(sdp->detached))
1270 printk(KERN_INFO "sg_rq_end_io: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001272 sense = rq->sense;
1273 result = rq->errors;
Tejun Heoc3a4d782009-05-07 22:24:37 +09001274 resid = rq->resid_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001277 sdp->disk->disk_name, srp->header.pack_id, result));
1278 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001279 ms = jiffies_to_msecs(jiffies);
1280 srp->header.duration = (ms > srp->header.duration) ?
1281 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001282 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct scsi_sense_hdr sshdr;
1284
Mike Christied6b10342005-11-08 04:06:41 -06001285 srp->header.status = 0xff & result;
1286 srp->header.masked_status = status_byte(result);
1287 srp->header.msg_status = msg_byte(result);
1288 srp->header.host_status = host_byte(result);
1289 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if ((sdp->sgdebug > 0) &&
1291 ((CHECK_CONDITION == srp->header.masked_status) ||
1292 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001293 __scsi_print_sense("sg_cmd_done", sense,
1294 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001297 if (driver_byte(result) != 0
1298 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 && !scsi_sense_is_deferred(&sshdr)
1300 && sshdr.sense_key == UNIT_ATTENTION
1301 && sdp->device->removable) {
1302 /* Detected possible disc change. Set the bit - this */
1303 /* may be used if there are filesystems using this device */
1304 sdp->device->changed = 1;
1305 }
1306 }
1307 /* Rely on write phase to clean out srp status values, so no "else" */
1308
Tony Battersbyc6517b792009-01-21 14:45:50 -05001309 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1310 if (unlikely(srp->orphan)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (sfp->keep_orphan)
1312 srp->sg_io_owned = 0;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001313 else
1314 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001316 srp->done = done;
1317 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1318
1319 if (likely(done)) {
1320 /* Now wake up any sg_read() that is waiting for this
1321 * packet.
1322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 wake_up_interruptible(&sfp->read_wait);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001324 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001325 kref_put(&sfp->f_ref, sg_remove_sfp);
FUJITA Tomonori015640e2009-04-03 19:28:06 +09001326 } else {
1327 INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
1328 schedule_work(&srp->ew.work);
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001332static const struct file_operations sg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 .owner = THIS_MODULE,
1334 .read = sg_read,
1335 .write = sg_write,
1336 .poll = sg_poll,
Arnd Bergmannf4927c42010-04-27 00:24:01 +02001337 .unlocked_ioctl = sg_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#ifdef CONFIG_COMPAT
1339 .compat_ioctl = sg_compat_ioctl,
1340#endif
1341 .open = sg_open,
1342 .mmap = sg_mmap,
1343 .release = sg_release,
1344 .fasync = sg_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001345 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346};
1347
gregkh@suse.ded2538782005-03-23 09:55:22 -08001348static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350static int sg_sysfs_valid = 0;
1351
James Bottomley7c07d612007-08-05 13:36:11 -05001352static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Mike Christied6b10342005-11-08 04:06:41 -06001354 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 Sg_device *sdp;
1356 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001357 int error;
1358 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Jes Sorensen24669f752006-01-16 10:31:18 -05001360 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!sdp) {
1362 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001363 return ERR_PTR(-ENOMEM);
1364 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001365
James Bottomley7c07d612007-08-05 13:36:11 -05001366 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1367 printk(KERN_WARNING "idr expansion Sg_device failure\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05001368 error = -ENOMEM;
James Bottomley7c07d612007-08-05 13:36:11 -05001369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371
James Bottomley7c07d612007-08-05 13:36:11 -05001372 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Tony Battersbyc6517b792009-01-21 14:45:50 -05001374 error = idr_get_new(&sg_index_idr, sdp, &k);
James Bottomley7c07d612007-08-05 13:36:11 -05001375 if (error) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05001376 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley7c07d612007-08-05 13:36:11 -05001377 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1378 error);
1379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (unlikely(k >= SG_MAX_DEVS))
1383 goto overflow;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1386 sprintf(disk->disk_name, "sg%d", k);
1387 disk->first_minor = k;
1388 sdp->disk = disk;
1389 sdp->device = scsidp;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09001390 INIT_LIST_HEAD(&sdp->sfds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 init_waitqueue_head(&sdp->o_excl_wait);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001392 sdp->sg_tablesize = queue_max_segments(q);
James Bottomley7c07d612007-08-05 13:36:11 -05001393 sdp->index = k;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001394 kref_init(&sdp->d_ref);
1395
1396 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
James Bottomley7c07d612007-08-05 13:36:11 -05001398 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001400 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001402 return ERR_PTR(error);
1403 }
1404 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 overflow:
Tony Battersbyc6517b792009-01-21 14:45:50 -05001407 idr_remove(&sg_index_idr, k);
1408 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley9ccfc752005-10-02 11:45:08 -05001409 sdev_printk(KERN_WARNING, scsidp,
1410 "Unable to attach sg device type=%d, minor "
1411 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 error = -ENODEV;
1413 goto out;
1414}
1415
1416static int
Tony Jonesee959b02008-02-22 00:13:36 +01001417sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Tony Jonesee959b02008-02-22 00:13:36 +01001419 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct gendisk *disk;
1421 Sg_device *sdp = NULL;
1422 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001423 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001424 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 disk = alloc_disk(1);
1427 if (!disk) {
1428 printk(KERN_WARNING "alloc_disk failed\n");
1429 return -ENOMEM;
1430 }
1431 disk->major = SCSI_GENERIC_MAJOR;
1432
1433 error = -ENOMEM;
1434 cdev = cdev_alloc();
1435 if (!cdev) {
1436 printk(KERN_WARNING "cdev_alloc failed\n");
1437 goto out;
1438 }
1439 cdev->owner = THIS_MODULE;
1440 cdev->ops = &sg_fops;
1441
James Bottomley7c07d612007-08-05 13:36:11 -05001442 sdp = sg_alloc(disk, scsidp);
1443 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001445 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
James Bottomley7c07d612007-08-05 13:36:11 -05001449 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001450 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001451 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 sdp->cdev = cdev;
1454 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001455 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Greg Kroah-Hartmand73a1a672008-07-21 20:03:34 -07001457 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1458 MKDEV(SCSI_GENERIC_MAJOR,
1459 sdp->index),
1460 sdp, "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001461 if (IS_ERR(sg_class_member)) {
1462 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001463 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001464 error = PTR_ERR(sg_class_member);
1465 goto cdev_add_err;
1466 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001467 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 &sg_class_member->kobj, "generic");
1469 if (error)
1470 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001471 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001473 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
James Bottomley9ccfc752005-10-02 11:45:08 -05001475 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001476 "Attached scsi generic sg%d type %d\n", sdp->index,
1477 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Tony Jonesee959b02008-02-22 00:13:36 +01001479 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
1482
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001483cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001484 write_lock_irqsave(&sg_index_lock, iflags);
1485 idr_remove(&sg_index_idr, sdp->index);
1486 write_unlock_irqrestore(&sg_index_lock, iflags);
1487 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489out:
1490 put_disk(disk);
1491 if (cdev)
1492 cdev_del(cdev);
1493 return error;
1494}
1495
Tony Battersbyc6517b792009-01-21 14:45:50 -05001496static void sg_device_destroy(struct kref *kref)
1497{
1498 struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1499 unsigned long flags;
1500
1501 /* CAUTION! Note that the device can still be found via idr_find()
1502 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1503 * any other cleanup.
1504 */
1505
1506 write_lock_irqsave(&sg_index_lock, flags);
1507 idr_remove(&sg_index_idr, sdp->index);
1508 write_unlock_irqrestore(&sg_index_lock, flags);
1509
1510 SCSI_LOG_TIMEOUT(3,
1511 printk("sg_device_destroy: %s\n",
1512 sdp->disk->disk_name));
1513
1514 put_disk(sdp->disk);
1515 kfree(sdp);
1516}
1517
1518static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Tony Jonesee959b02008-02-22 00:13:36 +01001520 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1521 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 unsigned long iflags;
1523 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Tony Battersbyc6517b792009-01-21 14:45:50 -05001525 if (!sdp || sdp->detached)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Tony Battersbyc6517b792009-01-21 14:45:50 -05001528 SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
1529
1530 /* Need a write lock to set sdp->detached. */
James Bottomley7c07d612007-08-05 13:36:11 -05001531 write_lock_irqsave(&sg_index_lock, iflags);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001532 sdp->detached = 1;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09001533 list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05001534 wake_up_interruptible(&sfp->read_wait);
1535 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
James Bottomley7c07d612007-08-05 13:36:11 -05001537 write_unlock_irqrestore(&sg_index_lock, iflags);
1538
1539 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001540 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001541 cdev_del(sdp->cdev);
1542 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Tony Battersbyc6517b792009-01-21 14:45:50 -05001544 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
Douglas Gilbert6460e752006-09-20 18:20:49 -04001547module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1548module_param_named(def_reserved_size, def_reserved_size, int,
1549 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1551
1552MODULE_AUTHOR("Douglas Gilbert");
1553MODULE_DESCRIPTION("SCSI generic (sg) driver");
1554MODULE_LICENSE("GPL");
1555MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001556MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Douglas Gilbert6460e752006-09-20 18:20:49 -04001558MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1559 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1561MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1562
1563static int __init
1564init_sg(void)
1565{
1566 int rc;
1567
Douglas Gilbert6460e752006-09-20 18:20:49 -04001568 if (scatter_elem_sz < PAGE_SIZE) {
1569 scatter_elem_sz = PAGE_SIZE;
1570 scatter_elem_sz_prev = scatter_elem_sz;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (def_reserved_size >= 0)
1573 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001574 else
1575 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1578 SG_MAX_DEVS, "sg");
1579 if (rc)
1580 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001581 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if ( IS_ERR(sg_sysfs_class) ) {
1583 rc = PTR_ERR(sg_sysfs_class);
1584 goto err_out;
1585 }
1586 sg_sysfs_valid = 1;
1587 rc = scsi_register_interface(&sg_interface);
1588 if (0 == rc) {
1589#ifdef CONFIG_SCSI_PROC_FS
1590 sg_proc_init();
1591#endif /* CONFIG_SCSI_PROC_FS */
1592 return 0;
1593 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001594 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595err_out:
1596 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1597 return rc;
1598}
1599
1600static void __exit
1601exit_sg(void)
1602{
1603#ifdef CONFIG_SCSI_PROC_FS
1604 sg_proc_cleanup();
1605#endif /* CONFIG_SCSI_PROC_FS */
1606 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001607 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 sg_sysfs_valid = 0;
1609 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1610 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001611 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001614static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001615{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001616 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001617 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001618 Sg_fd *sfp = srp->parentfp;
1619 sg_io_hdr_t *hp = &srp->header;
1620 int dxfer_len = (int) hp->dxfer_len;
1621 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001622 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001623 Sg_scatter_hold *req_schp = &srp->data;
1624 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1625 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001626 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001627 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1628
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001629 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1630 dxfer_len));
1631
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001632 rq = blk_get_request(q, rw, GFP_ATOMIC);
1633 if (!rq)
1634 return -ENOMEM;
1635
1636 memcpy(rq->cmd, cmd, hp->cmd_len);
1637
1638 rq->cmd_len = hp->cmd_len;
1639 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1640
1641 srp->rq = rq;
1642 rq->end_io_data = srp;
1643 rq->sense = srp->sense_b;
1644 rq->retries = SG_DEFAULT_RETRIES;
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001647 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001648
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001649 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1650 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1651 !sfp->parentdp->device->host->unchecked_isa_dma &&
Namhyung Kim2610a252010-09-16 12:55:57 +09001652 blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001653 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001654 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001655 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001656
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001657 if (md) {
1658 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1659 sg_link_reserve(sfp, srp, dxfer_len);
1660 else {
1661 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1662 if (res)
1663 return res;
1664 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001665
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001666 md->pages = req_schp->pages;
1667 md->page_order = req_schp->page_order;
1668 md->nr_entries = req_schp->k_use_sg;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001669 md->offset = 0;
FUJITA Tomonori97ae77a2008-12-18 14:49:38 +09001670 md->null_mapped = hp->dxferp ? 0 : 1;
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001671 if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
1672 md->from_user = 1;
1673 else
1674 md->from_user = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001676
FUJITA Tomonori0fdf96b2009-04-03 09:12:20 +09001677 if (iov_count) {
1678 int len, size = sizeof(struct sg_iovec) * iov_count;
1679 struct iovec *iov;
1680
Julia Lawall30941412010-08-10 18:01:27 -07001681 iov = memdup_user(hp->dxferp, size);
1682 if (IS_ERR(iov))
1683 return PTR_ERR(iov);
FUJITA Tomonori0fdf96b2009-04-03 09:12:20 +09001684
1685 len = iov_length(iov, iov_count);
1686 if (hp->dxfer_len < len) {
1687 iov_count = iov_shorten(iov, iov_count, hp->dxfer_len);
1688 len = hp->dxfer_len;
1689 }
1690
1691 res = blk_rq_map_user_iov(q, rq, md, (struct sg_iovec *)iov,
1692 iov_count,
1693 len, GFP_ATOMIC);
1694 kfree(iov);
1695 } else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001696 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1697 hp->dxfer_len, GFP_ATOMIC);
1698
1699 if (!res) {
1700 srp->bio = rq->bio;
1701
1702 if (!md) {
1703 req_schp->dio_in_use = 1;
1704 hp->info |= SG_INFO_DIRECT_IO;
1705 }
1706 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001707 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001710static int sg_finish_rem_req(Sg_request * srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001712 int ret = 0;
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 Sg_fd *sfp = srp->parentfp;
1715 Sg_scatter_hold *req_schp = &srp->data;
1716
1717 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001718 if (srp->rq) {
1719 if (srp->bio)
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001720 ret = blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001721
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001722 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001723 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001724
Christof Schmitte27168f2009-09-17 09:10:14 +02001725 if (srp->res_used)
1726 sg_unlink_reserve(sfp, srp);
1727 else
1728 sg_remove_scat(req_schp);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 sg_remove_request(sfp, srp);
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001731
1732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735static int
1736sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1737{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001738 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001739 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001741 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1742 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001745 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int
1749sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1750{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001751 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001752 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001753 int blk_size = buff_size, order;
1754 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Eric Sesterhennfb119932007-05-23 14:41:36 -07001756 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return -EFAULT;
1758 if (0 == blk_size)
1759 ++blk_size; /* don't know why */
FUJITA Tomonorib2ed6c62009-02-12 02:42:57 +09001760 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1761 blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1763 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001764
1765 /* N.B. ret_sz carried into this block ... */
1766 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1767 if (mx_sc_elems < 0)
1768 return mx_sc_elems; /* most likely -ENOMEM */
1769
Douglas Gilbert6460e752006-09-20 18:20:49 -04001770 num = scatter_elem_sz;
1771 if (unlikely(num != scatter_elem_sz_prev)) {
1772 if (num < PAGE_SIZE) {
1773 scatter_elem_sz = PAGE_SIZE;
1774 scatter_elem_sz_prev = PAGE_SIZE;
1775 } else
1776 scatter_elem_sz_prev = num;
1777 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001778
1779 if (sfp->low_dma)
1780 gfp_mask |= GFP_DMA;
1781
1782 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1783 gfp_mask |= __GFP_ZERO;
1784
1785 order = get_order(num);
1786retry:
1787 ret_sz = 1 << (PAGE_SHIFT + order);
1788
1789 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1790 k++, rem_sz -= ret_sz) {
1791
Douglas Gilbert6460e752006-09-20 18:20:49 -04001792 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001793 scatter_elem_sz_prev : rem_sz;
1794
1795 schp->pages[k] = alloc_pages(gfp_mask, order);
1796 if (!schp->pages[k])
1797 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Douglas Gilbert6460e752006-09-20 18:20:49 -04001799 if (num == scatter_elem_sz_prev) {
1800 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1801 scatter_elem_sz = ret_sz;
1802 scatter_elem_sz_prev = ret_sz;
1803 }
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001806 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1807 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001808 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001810 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001811 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001812 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1813 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001814
1815 schp->bufflen = blk_size;
1816 if (rem_sz > 0) /* must have failed */
1817 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001819out:
1820 for (i = 0; i < k; i++)
Michal Schmidte71044e2009-09-03 14:27:08 +02001821 __free_pages(schp->pages[i], order);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001822
1823 if (--order >= 0)
1824 goto retry;
1825
1826 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static void
1830sg_remove_scat(Sg_scatter_hold * schp)
1831{
1832 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001833 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001834 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int k;
1836
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001837 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001839 "sg_remove_scat: k=%d, pg=0x%p\n",
1840 k, schp->pages[k]));
1841 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001843
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001844 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Mike Christied6b10342005-11-08 04:06:41 -06001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 memset(schp, 0, sizeof (*schp));
1848}
1849
1850static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1852{
1853 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001854 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1857 num_read_xfer));
1858 if ((!outp) || (num_read_xfer <= 0))
1859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001861 num = 1 << (PAGE_SHIFT + schp->page_order);
1862 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001863 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001864 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001865 num_read_xfer))
1866 return -EFAULT;
1867 break;
1868 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001869 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001870 num))
1871 return -EFAULT;
1872 num_read_xfer -= num;
1873 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 break;
Mike Christied6b10342005-11-08 04:06:41 -06001875 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Mike Christied6b10342005-11-08 04:06:41 -06001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 0;
1880}
1881
1882static void
1883sg_build_reserve(Sg_fd * sfp, int req_size)
1884{
1885 Sg_scatter_hold *schp = &sfp->reserve;
1886
1887 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1888 do {
1889 if (req_size < PAGE_SIZE)
1890 req_size = PAGE_SIZE;
1891 if (0 == sg_build_indirect(schp, sfp, req_size))
1892 return;
1893 else
1894 sg_remove_scat(schp);
1895 req_size >>= 1; /* divide by 2 */
1896 } while (req_size > (PAGE_SIZE / 2));
1897}
1898
1899static void
1900sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1901{
1902 Sg_scatter_hold *req_schp = &srp->data;
1903 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001904 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 srp->res_used = 1;
1907 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001908 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001910 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1911 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001912 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001913 req_schp->k_use_sg = k + 1;
1914 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001915 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001916
1917 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001918 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001919 break;
1920 } else
1921 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Mike Christied6b10342005-11-08 04:06:41 -06001923
1924 if (k >= rsv_schp->k_use_sg)
1925 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
1928static void
1929sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1930{
1931 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1934 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 req_schp->k_use_sg = 0;
1936 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001937 req_schp->pages = NULL;
1938 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 req_schp->sglist_len = 0;
1940 sfp->save_scat_len = 0;
1941 srp->res_used = 0;
1942}
1943
1944static Sg_request *
1945sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1946{
1947 Sg_request *resp;
1948 unsigned long iflags;
1949
1950 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1951 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1952 /* look for requests that are ready + not SG_IO owned */
1953 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1954 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1955 resp->done = 2; /* guard against other readers */
1956 break;
1957 }
1958 }
1959 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1960 return resp;
1961}
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963/* always adds to end of list */
1964static Sg_request *
1965sg_add_request(Sg_fd * sfp)
1966{
1967 int k;
1968 unsigned long iflags;
1969 Sg_request *resp;
1970 Sg_request *rp = sfp->req_arr;
1971
1972 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1973 resp = sfp->headrp;
1974 if (!resp) {
1975 memset(rp, 0, sizeof (Sg_request));
1976 rp->parentfp = sfp;
1977 resp = rp;
1978 sfp->headrp = resp;
1979 } else {
1980 if (0 == sfp->cmd_q)
1981 resp = NULL; /* command queuing disallowed */
1982 else {
1983 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1984 if (!rp->parentfp)
1985 break;
1986 }
1987 if (k < SG_MAX_QUEUE) {
1988 memset(rp, 0, sizeof (Sg_request));
1989 rp->parentfp = sfp;
1990 while (resp->nextrp)
1991 resp = resp->nextrp;
1992 resp->nextrp = rp;
1993 resp = rp;
1994 } else
1995 resp = NULL;
1996 }
1997 }
1998 if (resp) {
1999 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002000 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2003 return resp;
2004}
2005
2006/* Return of 1 for found; 0 for not found */
2007static int
2008sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2009{
2010 Sg_request *prev_rp;
2011 Sg_request *rp;
2012 unsigned long iflags;
2013 int res = 0;
2014
2015 if ((!sfp) || (!srp) || (!sfp->headrp))
2016 return res;
2017 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2018 prev_rp = sfp->headrp;
2019 if (srp == prev_rp) {
2020 sfp->headrp = prev_rp->nextrp;
2021 prev_rp->parentfp = NULL;
2022 res = 1;
2023 } else {
2024 while ((rp = prev_rp->nextrp)) {
2025 if (srp == rp) {
2026 prev_rp->nextrp = rp->nextrp;
2027 rp->parentfp = NULL;
2028 res = 1;
2029 break;
2030 }
2031 prev_rp = rp;
2032 }
2033 }
2034 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2035 return res;
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038static Sg_fd *
2039sg_add_sfp(Sg_device * sdp, int dev)
2040{
2041 Sg_fd *sfp;
2042 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002043 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Mike Christied6b10342005-11-08 04:06:41 -06002045 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (!sfp)
2047 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 init_waitqueue_head(&sfp->read_wait);
2050 rwlock_init(&sfp->rq_list_lock);
2051
Tony Battersbyc6517b792009-01-21 14:45:50 -05002052 kref_init(&sfp->f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 sfp->timeout = SG_DEFAULT_TIMEOUT;
2054 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2055 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2056 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2057 sdp->device->host->unchecked_isa_dma : 1;
2058 sfp->cmd_q = SG_DEF_COMMAND_Q;
2059 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2060 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002061 write_lock_irqsave(&sg_index_lock, iflags);
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002062 list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
James Bottomley7c07d612007-08-05 13:36:11 -05002063 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002065 if (unlikely(sg_big_buff != def_reserved_size))
2066 sg_big_buff = def_reserved_size;
2067
Alan Stern44ec9542007-02-20 11:01:57 -05002068 bufflen = min_t(int, sg_big_buff,
Martin K. Petersenae03bf62009-05-22 17:17:50 -04002069 queue_max_sectors(sdp->device->request_queue) * 512);
Alan Stern44ec9542007-02-20 11:01:57 -05002070 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2072 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
Tony Battersbyc6517b792009-01-21 14:45:50 -05002073
2074 kref_get(&sdp->d_ref);
2075 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return sfp;
2077}
2078
Tony Battersbyc6517b792009-01-21 14:45:50 -05002079static void sg_remove_sfp_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002081 struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2082 struct sg_device *sdp = sfp->parentdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Tony Battersbyc6517b792009-01-21 14:45:50 -05002084 /* Cleanup any responses which were never read(). */
2085 while (sfp->headrp)
2086 sg_finish_rem_req(sfp->headrp);
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (sfp->reserve.bufflen > 0) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05002089 SCSI_LOG_TIMEOUT(6,
2090 printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2091 (int) sfp->reserve.bufflen,
2092 (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 sg_remove_scat(&sfp->reserve);
2094 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002095
2096 SCSI_LOG_TIMEOUT(6,
2097 printk("sg_remove_sfp: %s, sfp=0x%p\n",
2098 sdp->disk->disk_name,
2099 sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002100 kfree(sfp);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002101
2102 scsi_device_put(sdp->device);
2103 sg_put_dev(sdp);
2104 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Tony Battersbyc6517b792009-01-21 14:45:50 -05002107static void sg_remove_sfp(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002109 struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
2110 struct sg_device *sdp = sfp->parentdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002111 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Tony Battersbyc6517b792009-01-21 14:45:50 -05002113 write_lock_irqsave(&sg_index_lock, iflags);
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002114 list_del(&sfp->sfd_siblings);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002115 write_unlock_irqrestore(&sg_index_lock, iflags);
2116 wake_up_interruptible(&sdp->o_excl_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
FUJITA Tomonori015640e2009-04-03 19:28:06 +09002118 INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
2119 schedule_work(&sfp->ew.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122static int
2123sg_res_in_use(Sg_fd * sfp)
2124{
2125 const Sg_request *srp;
2126 unsigned long iflags;
2127
2128 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2129 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2130 if (srp->res_used)
2131 break;
2132 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2133 return srp ? 1 : 0;
2134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136#ifdef CONFIG_SCSI_PROC_FS
2137static int
James Bottomley7c07d612007-08-05 13:36:11 -05002138sg_idr_max_id(int id, void *p, void *data)
2139{
2140 int *k = data;
2141
2142 if (*k < id)
2143 *k = id;
2144
2145 return 0;
2146}
2147
2148static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149sg_last_dev(void)
2150{
Tony Battersby53474c02008-01-22 15:25:49 -05002151 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 unsigned long iflags;
2153
James Bottomley7c07d612007-08-05 13:36:11 -05002154 read_lock_irqsave(&sg_index_lock, iflags);
2155 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2156 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return k + 1; /* origin 1 */
2158}
2159#endif
2160
Tony Battersbyc6517b792009-01-21 14:45:50 -05002161/* must be called with sg_index_lock held */
2162static Sg_device *sg_lookup_dev(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002164 return idr_find(&sg_index_idr, dev);
2165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Tony Battersbyc6517b792009-01-21 14:45:50 -05002167static Sg_device *sg_get_dev(int dev)
2168{
2169 struct sg_device *sdp;
2170 unsigned long flags;
2171
2172 read_lock_irqsave(&sg_index_lock, flags);
2173 sdp = sg_lookup_dev(dev);
2174 if (!sdp)
2175 sdp = ERR_PTR(-ENXIO);
2176 else if (sdp->detached) {
2177 /* If sdp->detached, then the refcount may already be 0, in
2178 * which case it would be a bug to do kref_get().
2179 */
2180 sdp = ERR_PTR(-ENODEV);
2181 } else
2182 kref_get(&sdp->d_ref);
2183 read_unlock_irqrestore(&sg_index_lock, flags);
James Bottomley7c07d612007-08-05 13:36:11 -05002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return sdp;
2186}
2187
Tony Battersbyc6517b792009-01-21 14:45:50 -05002188static void sg_put_dev(struct sg_device *sdp)
2189{
2190 kref_put(&sdp->d_ref, sg_device_destroy);
2191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#ifdef CONFIG_SCSI_PROC_FS
2194
2195static struct proc_dir_entry *sg_proc_sgp = NULL;
2196
2197static char sg_proc_sg_dirname[] = "scsi/sg";
2198
2199static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2200
2201static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2202static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2203 size_t count, loff_t *off);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002204static const struct file_operations adio_fops = {
2205 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 .open = sg_proc_single_open_adio,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002207 .read = seq_read,
2208 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 .write = sg_proc_write_adio,
2210 .release = single_release,
2211};
2212
2213static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2214static ssize_t sg_proc_write_dressz(struct file *filp,
2215 const char __user *buffer, size_t count, loff_t *off);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002216static const struct file_operations dressz_fops = {
2217 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 .open = sg_proc_single_open_dressz,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002219 .read = seq_read,
2220 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 .write = sg_proc_write_dressz,
2222 .release = single_release,
2223};
2224
2225static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2226static int sg_proc_single_open_version(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002227static const struct file_operations version_fops = {
2228 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 .open = sg_proc_single_open_version,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002230 .read = seq_read,
2231 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 .release = single_release,
2233};
2234
2235static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2236static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002237static const struct file_operations devhdr_fops = {
2238 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 .open = sg_proc_single_open_devhdr,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002240 .read = seq_read,
2241 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 .release = single_release,
2243};
2244
2245static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2246static int sg_proc_open_dev(struct inode *inode, struct file *file);
2247static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2248static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2249static void dev_seq_stop(struct seq_file *s, void *v);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002250static const struct file_operations dev_fops = {
2251 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 .open = sg_proc_open_dev,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002253 .read = seq_read,
2254 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 .release = seq_release,
2256};
James Morris88e9d342009-09-22 16:43:43 -07002257static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .start = dev_seq_start,
2259 .next = dev_seq_next,
2260 .stop = dev_seq_stop,
2261 .show = sg_proc_seq_show_dev,
2262};
2263
2264static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2265static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002266static const struct file_operations devstrs_fops = {
2267 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 .open = sg_proc_open_devstrs,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002269 .read = seq_read,
2270 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 .release = seq_release,
2272};
James Morris88e9d342009-09-22 16:43:43 -07002273static const struct seq_operations devstrs_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .start = dev_seq_start,
2275 .next = dev_seq_next,
2276 .stop = dev_seq_stop,
2277 .show = sg_proc_seq_show_devstrs,
2278};
2279
2280static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2281static int sg_proc_open_debug(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002282static const struct file_operations debug_fops = {
2283 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 .open = sg_proc_open_debug,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002285 .read = seq_read,
2286 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 .release = seq_release,
2288};
James Morris88e9d342009-09-22 16:43:43 -07002289static const struct seq_operations debug_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 .start = dev_seq_start,
2291 .next = dev_seq_next,
2292 .stop = dev_seq_stop,
2293 .show = sg_proc_seq_show_debug,
2294};
2295
2296
2297struct sg_proc_leaf {
2298 const char * name;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002299 const struct file_operations * fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300};
2301
2302static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2303 {"allow_dio", &adio_fops},
2304 {"debug", &debug_fops},
2305 {"def_reserved_size", &dressz_fops},
2306 {"device_hdr", &devhdr_fops},
2307 {"devices", &dev_fops},
2308 {"device_strs", &devstrs_fops},
2309 {"version", &version_fops}
2310};
2311
2312static int
2313sg_proc_init(void)
2314{
Tobias Klauser6391a112006-06-08 22:23:48 -07002315 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Al Virod161a132011-07-24 03:36:29 -04002316 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Al Viro66600222005-09-28 22:32:57 +01002318 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!sg_proc_sgp)
2320 return 1;
2321 for (k = 0; k < num_leaves; ++k) {
Al Virod161a132011-07-24 03:36:29 -04002322 struct sg_proc_leaf *leaf = &sg_proc_leaf_arr[k];
2323 umode_t mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002324 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 return 0;
2327}
2328
2329static void
2330sg_proc_cleanup(void)
2331{
2332 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002333 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 if (!sg_proc_sgp)
2336 return;
2337 for (k = 0; k < num_leaves; ++k)
2338 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2339 remove_proc_entry(sg_proc_sg_dirname, NULL);
2340}
2341
2342
2343static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2344{
2345 seq_printf(s, "%d\n", *((int *)s->private));
2346 return 0;
2347}
2348
2349static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2350{
2351 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2352}
2353
2354static ssize_t
2355sg_proc_write_adio(struct file *filp, const char __user *buffer,
2356 size_t count, loff_t *off)
2357{
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002358 int err;
2359 unsigned long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2362 return -EACCES;
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002363 err = kstrtoul_from_user(buffer, count, 0, &num);
2364 if (err)
2365 return err;
2366 sg_allow_dio = num ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return count;
2368}
2369
2370static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2371{
2372 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2373}
2374
2375static ssize_t
2376sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2377 size_t count, loff_t *off)
2378{
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002379 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 unsigned long k = ULONG_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2383 return -EACCES;
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002384
2385 err = kstrtoul_from_user(buffer, count, 0, &k);
2386 if (err)
2387 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2389 sg_big_buff = k;
2390 return count;
2391 }
2392 return -ERANGE;
2393}
2394
2395static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2396{
2397 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2398 sg_version_date);
2399 return 0;
2400}
2401
2402static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2403{
2404 return single_open(file, sg_proc_seq_show_version, NULL);
2405}
2406
2407static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2408{
2409 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2410 "online\n");
2411 return 0;
2412}
2413
2414static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2415{
2416 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2417}
2418
2419struct sg_proc_deviter {
2420 loff_t index;
2421 size_t max;
2422};
2423
2424static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2425{
2426 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2427
Jan Blunck729d70f2005-08-27 11:07:52 -07002428 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (! it)
2430 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 it->index = *pos;
2433 it->max = sg_last_dev();
2434 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002435 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
2439static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2440{
Jan Blunck729d70f2005-08-27 11:07:52 -07002441 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 *pos = ++it->index;
2444 return (it->index < it->max) ? it : NULL;
2445}
2446
2447static void dev_seq_stop(struct seq_file *s, void *v)
2448{
Jan Blunck729d70f2005-08-27 11:07:52 -07002449 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452static int sg_proc_open_dev(struct inode *inode, struct file *file)
2453{
2454 return seq_open(file, &dev_seq_ops);
2455}
2456
2457static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2458{
2459 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2460 Sg_device *sdp;
2461 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002462 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Tony Battersbyc6517b792009-01-21 14:45:50 -05002464 read_lock_irqsave(&sg_index_lock, iflags);
2465 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2467 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2468 scsidp->host->host_no, scsidp->channel,
2469 scsidp->id, scsidp->lun, (int) scsidp->type,
2470 1,
2471 (int) scsidp->queue_depth,
2472 (int) scsidp->device_busy,
2473 (int) scsi_device_online(scsidp));
2474 else
2475 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002476 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return 0;
2478}
2479
2480static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2481{
2482 return seq_open(file, &devstrs_seq_ops);
2483}
2484
2485static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2486{
2487 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2488 Sg_device *sdp;
2489 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002490 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Tony Battersbyc6517b792009-01-21 14:45:50 -05002492 read_lock_irqsave(&sg_index_lock, iflags);
2493 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2495 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2496 scsidp->vendor, scsidp->model, scsidp->rev);
2497 else
2498 seq_printf(s, "<no active device>\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002499 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
Tony Battersbyc6517b792009-01-21 14:45:50 -05002503/* must be called while holding sg_index_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2505{
2506 int k, m, new_interface, blen, usg;
2507 Sg_request *srp;
2508 Sg_fd *fp;
2509 const sg_io_hdr_t *hp;
2510 const char * cp;
cb59e842005-04-02 13:51:23 -06002511 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002513 k = 0;
2514 list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
2515 k++;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002516 read_lock(&fp->rq_list_lock); /* irqs already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002518 "(res)sgat=%d low_dma=%d\n", k,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 jiffies_to_msecs(fp->timeout),
2520 fp->reserve.bufflen,
2521 (int) fp->reserve.k_use_sg,
2522 (int) fp->low_dma);
2523 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2524 (int) fp->cmd_q, (int) fp->force_packid,
2525 (int) fp->keep_orphan, (int) fp->closed);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002526 for (m = 0, srp = fp->headrp;
2527 srp != NULL;
2528 ++m, srp = srp->nextrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 hp = &srp->header;
2530 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2531 if (srp->res_used) {
2532 if (new_interface &&
2533 (SG_FLAG_MMAP_IO & hp->flags))
2534 cp = " mmap>> ";
2535 else
2536 cp = " rb>> ";
2537 } else {
2538 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2539 cp = " dio>> ";
2540 else
2541 cp = " ";
2542 }
2543 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002544 blen = srp->data.bufflen;
2545 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 seq_printf(s, srp->done ?
2547 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002548 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 seq_printf(s, " id=%d blen=%d",
2550 srp->header.pack_id, blen);
2551 if (srp->done)
2552 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002553 else {
2554 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002556 (new_interface ? hp->timeout :
2557 jiffies_to_msecs(fp->timeout)),
2558 (ms > hp->duration ? ms - hp->duration : 0));
2559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2561 (int) srp->data.cmd_opcode);
2562 }
2563 if (0 == m)
2564 seq_printf(s, " No requests active\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002565 read_unlock(&fp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567}
2568
2569static int sg_proc_open_debug(struct inode *inode, struct file *file)
2570{
2571 return seq_open(file, &debug_seq_ops);
2572}
2573
2574static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2575{
2576 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2577 Sg_device *sdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002578 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002581 seq_printf(s, "max_active_device=%d(origin 1)\n",
2582 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2584 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002585
2586 read_lock_irqsave(&sg_index_lock, iflags);
2587 sdp = it ? sg_lookup_dev(it->index) : NULL;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002588 if (sdp && !list_empty(&sdp->sfds)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct scsi_device *scsidp = sdp->device;
2590
Tony Battersbyc6517b792009-01-21 14:45:50 -05002591 seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2592 if (sdp->detached)
2593 seq_printf(s, "detached pending close ");
2594 else
2595 seq_printf
2596 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2597 scsidp->host->host_no,
2598 scsidp->channel, scsidp->id,
2599 scsidp->lun,
2600 scsidp->host->hostt->emulated);
2601 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2602 sdp->sg_tablesize, sdp->exclude);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 sg_proc_debug_helper(s, sdp);
2604 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002605 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return 0;
2607}
2608
2609#endif /* CONFIG_SCSI_PROC_FS */
2610
2611module_init(init_sg);
2612module_exit(exit_sg);