blob: 32425ac61096a934ff5aa932faaf814ffda8fdeb [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:
Douglas Gilbert65c26a02014-06-03 13:18:18 -040010 * Copyright (C) 1998 - 2014 Douglas Gilbert
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 */
18
Douglas Gilbert65c26a02014-06-03 13:18:18 -040019static int sg_version_num = 30536; /* 2 digits for each component */
20#define SG_VERSION_STR "3.5.36"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
Douglas Gilbert65c26a02014-06-03 13:18:18 -040023 * D. P. Gilbert (dgilbert@interlog.com), notes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
25 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
26 * (otherwise the macros compile to empty statements).
27 *
28 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/string.h>
35#include <linux/mm.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070036#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/errno.h>
38#include <linux/mtio.h>
39#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/fcntl.h>
42#include <linux/init.h>
43#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/cdev.h>
James Bottomley7c07d612007-08-05 13:36:11 -050046#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/seq_file.h>
48#include <linux/blkdev.h>
49#include <linux/delay.h>
Christof Schmitt6da127a2008-01-11 10:09:43 +010050#include <linux/blktrace_api.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020051#include <linux/mutex.h>
Christian Dietrich2fe038e2011-06-04 17:36:10 +020052#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "scsi.h"
db9dff32005-04-03 14:53:59 -050055#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <scsi/scsi_driver.h>
58#include <scsi/scsi_ioctl.h>
59#include <scsi/sg.h>
60
61#include "scsi_logging.h"
62
63#ifdef CONFIG_SCSI_PROC_FS
64#include <linux/proc_fs.h>
Douglas Gilbert65c26a02014-06-03 13:18:18 -040065static char *sg_version_date = "20140603";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int sg_proc_init(void);
68static void sg_proc_cleanup(void);
69#endif
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SG_ALLOW_DIO_DEF 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define SG_MAX_DEVS 32768
74
Douglas Gilbert65c26a02014-06-03 13:18:18 -040075/* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
76 * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
77 * than 16 bytes are "variable length" whose length is a multiple of 4
78 */
79#define SG_MAX_CDB_SIZE 252
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
83 * Then when using 32 bit integers x * m may overflow during the calculation.
84 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
85 * calculates the same, but prevents the overflow when both m and d
86 * are "small" numbers (like HZ and USER_HZ).
87 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
88 * in 32 bits.
89 */
90#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
91
92#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
93
94int sg_big_buff = SG_DEF_RESERVED_SIZE;
95/* N.B. This variable is readable and writeable via
96 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
97 of this size (or less if there is not enough memory) will be reserved
98 for use by this file descriptor. [Deprecated usage: this variable is also
99 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
100 the kernel (i.e. it is not a module).] */
101static int def_reserved_size = -1; /* picks up init parameter */
102static int sg_allow_dio = SG_ALLOW_DIO_DEF;
103
Douglas Gilbert6460e752006-09-20 18:20:49 -0400104static int scatter_elem_sz = SG_SCATTER_SZ;
105static int scatter_elem_sz_prev = SG_SCATTER_SZ;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define SG_SECTOR_SZ 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Tony Jonesee959b02008-02-22 00:13:36 +0100109static int sg_add(struct device *, struct class_interface *);
110static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
James Bottomley98481ff2013-10-25 10:26:38 +0100112static DEFINE_SPINLOCK(sg_open_exclusive_lock);
113
James Bottomley7c07d612007-08-05 13:36:11 -0500114static DEFINE_IDR(sg_index_idr);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100115static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
116 file descriptor list for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100119 .add_dev = sg_add,
120 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
124 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900125 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200127 struct page **pages;
128 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
130 unsigned char cmd_opcode; /* first byte of command */
131} Sg_scatter_hold;
132
133struct sg_device; /* forward declarations */
134struct sg_fd;
135
136typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct sg_request *nextrp; /* NULL -> tail request (slist) */
138 struct sg_fd *parentfp; /* NULL -> not in use */
139 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
140 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600141 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
143 char orphan; /* 1 -> drop on sight, 0 -> normal */
144 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
Jörn Engel6acddc52012-04-12 17:33:58 -0400145 /* done protected by rq_list_lock */
146 char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900147 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900148 struct bio *bio;
FUJITA Tomonoric96952e2009-02-04 11:36:27 +0900149 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150} Sg_request;
151
152typedef struct sg_fd { /* holds the state of a file descriptor */
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100153 /* sfd_siblings is protected by sg_index_lock */
154 struct list_head sfd_siblings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct sg_device *parentdp; /* owning device */
156 wait_queue_head_t read_wait; /* queue read until command done */
157 rwlock_t rq_list_lock; /* protect access to list in req_arr */
158 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
159 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
160 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
161 unsigned save_scat_len; /* original length of trunc. scat. element */
162 Sg_request *headrp; /* head of request slist, NULL->empty */
163 struct fasync_struct *async_qp; /* used by asynchronous notification */
164 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
165 char low_dma; /* as in parent but possibly overridden to 1 */
166 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
Douglas Gilbert65c26a02014-06-03 13:18:18 -0400168 unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
170 char mmap_called; /* 0 -> mmap() never called on this fd */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500171 struct kref f_ref;
172 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173} Sg_fd;
174
175typedef struct sg_device { /* holds the state of each scsi generic device */
176 struct scsi_device *device;
James Bottomley065b4a22013-10-25 10:27:02 +0100177 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500179 u32 index; /* device index number */
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100180 /* sfds is protected by sg_index_lock */
FUJITA Tomonori3442f802009-02-16 13:26:53 +0900181 struct list_head sfds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 volatile char detached; /* 0->attached, 1->detached pending removal */
James Bottomley98481ff2013-10-25 10:26:38 +0100183 /* exclude protected by sg_open_exclusive_lock */
Jörn Engelb499e522012-04-24 16:13:11 -0400184 char exclude; /* opened for exclusive access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
186 struct gendisk *disk;
187 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
Tony Battersbyc6517b792009-01-21 14:45:50 -0500188 struct kref d_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189} Sg_device;
190
Mike Christied6b10342005-11-08 04:06:41 -0600191/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900192static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900193static int sg_start_req(Sg_request *srp, unsigned char *cmd);
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +0900194static int sg_finish_rem_req(Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
197 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200198static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
199 const char __user *buf, size_t count, int blocking,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500200 int read_only, int sg_io_owned, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
202 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
204static void sg_remove_scat(Sg_scatter_hold * schp);
205static void sg_build_reserve(Sg_fd * sfp, int req_size);
206static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
207static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500209static void sg_remove_sfp(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
211static Sg_request *sg_add_request(Sg_fd * sfp);
212static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
213static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static Sg_device *sg_get_dev(int dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500215static void sg_put_dev(Sg_device *sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define SZ_SG_HEADER sizeof(struct sg_header)
218#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
219#define SZ_SG_IOVEC sizeof(sg_iovec_t)
220#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
221
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900222static int sg_allow_access(struct file *filp, unsigned char *cmd)
223{
Joe Perches35df8392010-09-04 18:52:46 -0700224 struct sg_fd *sfp = filp->private_data;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900225
226 if (sfp->parentdp->device->type == TYPE_SCANNER)
227 return 0;
228
Jens Axboe018e0442009-06-26 16:27:10 +0200229 return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900230}
231
James Bottomley98481ff2013-10-25 10:26:38 +0100232static int get_exclude(Sg_device *sdp)
233{
234 unsigned long flags;
235 int ret;
236
237 spin_lock_irqsave(&sg_open_exclusive_lock, flags);
238 ret = sdp->exclude;
239 spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
240 return ret;
241}
242
243static int set_exclude(Sg_device *sdp, char val)
244{
245 unsigned long flags;
246
247 spin_lock_irqsave(&sg_open_exclusive_lock, flags);
248 sdp->exclude = val;
249 spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
250 return val;
251}
252
Jörn Engel035d12e2012-04-25 11:17:29 -0400253static int sfds_list_empty(Sg_device *sdp)
254{
255 unsigned long flags;
256 int ret;
257
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100258 read_lock_irqsave(&sg_index_lock, flags);
Jörn Engel035d12e2012-04-25 11:17:29 -0400259 ret = list_empty(&sdp->sfds);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +0100260 read_unlock_irqrestore(&sg_index_lock, flags);
Jörn Engel035d12e2012-04-25 11:17:29 -0400261 return ret;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int
265sg_open(struct inode *inode, struct file *filp)
266{
267 int dev = iminor(inode);
268 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600269 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 Sg_device *sdp;
271 Sg_fd *sfp;
James Bottomley065b4a22013-10-25 10:27:02 +0100272 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int retval;
274
275 nonseekable_open(inode, filp);
276 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
277 sdp = sg_get_dev(dev);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500278 if (IS_ERR(sdp)) {
279 retval = PTR_ERR(sdp);
280 sdp = NULL;
281 goto sg_put;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* This driver's module count bumped by fops_get in <linux/fs.h> */
285 /* Prevent the device driver from vanishing while we sleep */
286 retval = scsi_device_get(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500287 if (retval)
288 goto sg_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Alan Sternbc4f2402010-06-17 10:41:42 -0400290 retval = scsi_autopm_get_device(sdp->device);
291 if (retval)
292 goto sdp_put;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!((flags & O_NONBLOCK) ||
295 scsi_block_when_processing_errors(sdp->device))) {
296 retval = -ENXIO;
297 /* we are in error recovery for this device */
298 goto error_out;
299 }
300
James Bottomley065b4a22013-10-25 10:27:02 +0100301 if (flags & O_EXCL) {
302 if (O_RDONLY == (flags & O_ACCMODE)) {
303 retval = -EPERM; /* Can't lock it with read only access */
304 goto error_out;
Vaughan Cao15b06f92013-08-29 10:00:36 +0800305 }
James Bottomley065b4a22013-10-25 10:27:02 +0100306 if (!sfds_list_empty(sdp) && (flags & O_NONBLOCK)) {
307 retval = -EBUSY;
308 goto error_out;
309 }
310 res = wait_event_interruptible(sdp->o_excl_wait,
311 ((!sfds_list_empty(sdp) || get_exclude(sdp)) ? 0 : set_exclude(sdp, 1)));
312 if (res) {
313 retval = res; /* -ERESTARTSYS because signal hit process */
314 goto error_out;
315 }
316 } else if (get_exclude(sdp)) { /* some other fd has an exclusive lock on dev */
317 if (flags & O_NONBLOCK) {
318 retval = -EBUSY;
319 goto error_out;
320 }
321 res = wait_event_interruptible(sdp->o_excl_wait, !get_exclude(sdp));
322 if (res) {
323 retval = res; /* -ERESTARTSYS because signal hit process */
324 goto error_out;
325 }
Vaughan Cao15b06f92013-08-29 10:00:36 +0800326 }
James Bottomleybafc8ad2013-10-25 10:25:14 +0100327 if (sdp->detached) {
328 retval = -ENODEV;
James Bottomley065b4a22013-10-25 10:27:02 +0100329 goto error_out;
James Bottomleybafc8ad2013-10-25 10:25:14 +0100330 }
Jörn Engel035d12e2012-04-25 11:17:29 -0400331 if (sfds_list_empty(sdp)) { /* no existing opens on this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600333 q = sdp->device->request_queue;
Martin K. Petersen8a783622010-02-26 00:20:39 -0500334 sdp->sg_tablesize = queue_max_segments(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
James Bottomleybafc8ad2013-10-25 10:25:14 +0100336 if ((sfp = sg_add_sfp(sdp, dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 filp->private_data = sfp;
338 else {
Tony Battersbyc6517b792009-01-21 14:45:50 -0500339 if (flags & O_EXCL) {
James Bottomley98481ff2013-10-25 10:26:38 +0100340 set_exclude(sdp, 0); /* undo if error */
James Bottomley065b4a22013-10-25 10:27:02 +0100341 wake_up_interruptible(&sdp->o_excl_wait);
342 }
343 retval = -ENOMEM;
344 goto error_out;
345 }
346 retval = 0;
Tony Battersbyc6517b792009-01-21 14:45:50 -0500347error_out:
James Bottomley065b4a22013-10-25 10:27:02 +0100348 if (retval) {
Alan Sternbc4f2402010-06-17 10:41:42 -0400349 scsi_autopm_put_device(sdp->device);
350sdp_put:
Tony Battersbyc6517b792009-01-21 14:45:50 -0500351 scsi_device_put(sdp->device);
Alan Sternbc4f2402010-06-17 10:41:42 -0400352 }
Tony Battersbyc6517b792009-01-21 14:45:50 -0500353sg_put:
354 if (sdp)
355 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return retval;
357}
358
359/* Following function was formerly called 'sg_close' */
360static int
361sg_release(struct inode *inode, struct file *filp)
362{
363 Sg_device *sdp;
364 Sg_fd *sfp;
365
366 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
367 return -ENXIO;
368 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
Tony Battersbyc6517b792009-01-21 14:45:50 -0500369
James Bottomley98481ff2013-10-25 10:26:38 +0100370 set_exclude(sdp, 0);
James Bottomley065b4a22013-10-25 10:27:02 +0100371 wake_up_interruptible(&sdp->o_excl_wait);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500372
Alan Sternbc4f2402010-06-17 10:41:42 -0400373 scsi_autopm_put_device(sdp->device);
Tony Battersbyc6517b792009-01-21 14:45:50 -0500374 kref_put(&sfp->f_ref, sg_remove_sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376}
377
378static ssize_t
379sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 Sg_device *sdp;
382 Sg_fd *sfp;
383 Sg_request *srp;
384 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600386 struct sg_header *old_hdr = NULL;
387 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
390 return -ENXIO;
391 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
392 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!access_ok(VERIFY_WRITE, buf, count))
395 return -EFAULT;
396 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600397 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
398 if (!old_hdr)
399 return -ENOMEM;
400 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
401 retval = -EFAULT;
402 goto free_old_hdr;
403 }
404 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600406 sg_io_hdr_t *new_hdr;
407 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
408 if (!new_hdr) {
409 retval = -ENOMEM;
410 goto free_old_hdr;
411 }
412 retval =__copy_from_user
413 (new_hdr, buf, SZ_SG_IO_HDR);
414 req_pack_id = new_hdr->pack_id;
415 kfree(new_hdr);
416 if (retval) {
417 retval = -EFAULT;
418 goto free_old_hdr;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 } else
cb59e842005-04-02 13:51:23 -0600422 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 srp = sg_get_rq_mark(sfp, req_pack_id);
425 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600426 if (sdp->detached) {
427 retval = -ENODEV;
428 goto free_old_hdr;
429 }
430 if (filp->f_flags & O_NONBLOCK) {
431 retval = -EAGAIN;
432 goto free_old_hdr;
433 }
Jörn Engel3f0c6ab2012-04-12 17:33:25 -0400434 retval = wait_event_interruptible(sfp->read_wait,
Jörn Engel794c10f2012-04-12 17:32:48 -0400435 (sdp->detached ||
Jörn Engel3f0c6ab2012-04-12 17:33:25 -0400436 (srp = sg_get_rq_mark(sfp, req_pack_id))));
Jörn Engel794c10f2012-04-12 17:32:48 -0400437 if (sdp->detached) {
438 retval = -ENODEV;
439 goto free_old_hdr;
440 }
441 if (retval) {
cb59e842005-04-02 13:51:23 -0600442 /* -ERESTARTSYS as signal hit process */
443 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 }
cb59e842005-04-02 13:51:23 -0600446 if (srp->header.interface_id != '\0') {
447 retval = sg_new_read(sfp, buf, count, srp);
448 goto free_old_hdr;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600452 if (old_hdr == NULL) {
453 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
454 if (! old_hdr) {
455 retval = -ENOMEM;
456 goto free_old_hdr;
457 }
458 }
459 memset(old_hdr, 0, SZ_SG_HEADER);
460 old_hdr->reply_len = (int) hp->timeout;
461 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
462 old_hdr->pack_id = hp->pack_id;
463 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600465 old_hdr->target_status = hp->masked_status;
466 old_hdr->host_status = hp->host_status;
467 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if ((CHECK_CONDITION & hp->masked_status) ||
469 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600470 memcpy(old_hdr->sense_buffer, srp->sense_b,
471 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 switch (hp->host_status) {
473 /* This setup of 'result' is for backward compatibility and is best
474 ignored by the user who should use target, host + driver status */
475 case DID_OK:
476 case DID_PASSTHROUGH:
477 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600478 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case DID_NO_CONNECT:
481 case DID_BUS_BUSY:
482 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600483 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case DID_BAD_TARGET:
486 case DID_ABORT:
487 case DID_PARITY:
488 case DID_RESET:
489 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600490 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600493 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 hp->masked_status == GOOD) ? 0 : EIO;
495 break;
496 default:
cb59e842005-04-02 13:51:23 -0600497 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 }
500
501 /* Now copy the result back to the user buffer. */
502 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600503 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
504 retval = -EFAULT;
505 goto free_old_hdr;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600508 if (count > old_hdr->reply_len)
509 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600511 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
512 retval = -EFAULT;
513 goto free_old_hdr;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 } else
cb59e842005-04-02 13:51:23 -0600517 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600519 retval = count;
520free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800521 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600522 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525static ssize_t
526sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
527{
528 sg_io_hdr_t *hp = &srp->header;
529 int err = 0;
530 int len;
531
532 if (count < SZ_SG_IO_HDR) {
533 err = -EINVAL;
534 goto err_out;
535 }
536 hp->sb_len_wr = 0;
537 if ((hp->mx_sb_len > 0) && hp->sbp) {
538 if ((CHECK_CONDITION & hp->masked_status) ||
539 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600540 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
542 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
543 len = (len > sb_len) ? sb_len : len;
544 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
545 err = -EFAULT;
546 goto err_out;
547 }
548 hp->sb_len_wr = len;
549 }
550 }
551 if (hp->masked_status || hp->host_status || hp->driver_status)
552 hp->info |= SG_INFO_CHECK;
553 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
554 err = -EFAULT;
555 goto err_out;
556 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900557err_out:
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +0900558 err = sg_finish_rem_req(srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return (0 == err) ? count : err;
560}
561
562static ssize_t
563sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
564{
565 int mxsize, cmd_size, k;
566 int input_size, blocking;
567 unsigned char opcode;
568 Sg_device *sdp;
569 Sg_fd *sfp;
570 Sg_request *srp;
571 struct sg_header old_hdr;
572 sg_io_hdr_t *hp;
Douglas Gilbert65c26a02014-06-03 13:18:18 -0400573 unsigned char cmnd[SG_MAX_CDB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
576 return -ENXIO;
577 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
578 sdp->disk->disk_name, (int) count));
579 if (sdp->detached)
580 return -ENODEV;
581 if (!((filp->f_flags & O_NONBLOCK) ||
582 scsi_block_when_processing_errors(sdp->device)))
583 return -ENXIO;
584
585 if (!access_ok(VERIFY_READ, buf, count))
586 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
587 if (count < SZ_SG_HEADER)
588 return -EIO;
589 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
590 return -EFAULT;
591 blocking = !(filp->f_flags & O_NONBLOCK);
592 if (old_hdr.reply_len < 0)
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500593 return sg_new_write(sfp, filp, buf, count,
594 blocking, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (count < (SZ_SG_HEADER + 6))
596 return -EIO; /* The minimum scsi command length is 6 bytes. */
597
598 if (!(srp = sg_add_request(sfp))) {
599 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
600 return -EDOM;
601 }
602 buf += SZ_SG_HEADER;
603 __get_user(opcode, buf);
604 if (sfp->next_cmd_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 cmd_size = sfp->next_cmd_len;
606 sfp->next_cmd_len = 0; /* reset so only this write() effected */
607 } else {
608 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
609 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
610 cmd_size = 12;
611 }
612 SCSI_LOG_TIMEOUT(4, printk(
613 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
614/* Determine buffer size. */
615 input_size = count - cmd_size;
616 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
617 mxsize -= SZ_SG_HEADER;
618 input_size -= SZ_SG_HEADER;
619 if (input_size < 0) {
620 sg_remove_request(sfp, srp);
621 return -EIO; /* User did not pass enough bytes for this command. */
622 }
623 hp = &srp->header;
624 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
625 hp->cmd_len = (unsigned char) cmd_size;
626 hp->iovec_count = 0;
627 hp->mx_sb_len = 0;
628 if (input_size > 0)
629 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
630 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
631 else
632 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
633 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900634 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
635 hp->dxferp = (char __user *)buf + cmd_size;
636 else
637 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 hp->sbp = NULL;
639 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
640 hp->flags = input_size; /* structure abuse ... */
641 hp->pack_id = old_hdr.pack_id;
642 hp->usr_ptr = NULL;
643 if (__copy_from_user(cmnd, buf, cmd_size))
644 return -EFAULT;
645 /*
646 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
647 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
648 * is a non-zero input_size, so emit a warning.
649 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100650 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
651 static char cmd[TASK_COMM_LEN];
Christian Dietrich2fe038e2011-06-04 17:36:10 +0200652 if (strcmp(current->comm, cmd)) {
653 printk_ratelimited(KERN_WARNING
654 "sg_write: data in/out %d/%d bytes "
655 "for SCSI command 0x%x-- guessing "
656 "data in;\n program %s not setting "
657 "count and/or reply_len properly\n",
658 old_hdr.reply_len - (int)SZ_SG_HEADER,
659 input_size, (unsigned int) cmnd[0],
660 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100661 strcpy(cmd, current->comm);
662 }
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
665 return (k < 0) ? k : count;
666}
667
668static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200669sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500670 size_t count, int blocking, int read_only, int sg_io_owned,
Adel Gadllah0b07de82008-06-26 13:48:27 +0200671 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int k;
674 Sg_request *srp;
675 sg_io_hdr_t *hp;
Douglas Gilbert65c26a02014-06-03 13:18:18 -0400676 unsigned char cmnd[SG_MAX_CDB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int timeout;
678 unsigned long ul_timeout;
679
680 if (count < SZ_SG_IO_HDR)
681 return -EINVAL;
682 if (!access_ok(VERIFY_READ, buf, count))
683 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
684
685 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
686 if (!(srp = sg_add_request(sfp))) {
687 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
688 return -EDOM;
689 }
Tony Battersbya2dd3b42009-01-20 17:00:09 -0500690 srp->sg_io_owned = sg_io_owned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 hp = &srp->header;
692 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
693 sg_remove_request(sfp, srp);
694 return -EFAULT;
695 }
696 if (hp->interface_id != 'S') {
697 sg_remove_request(sfp, srp);
698 return -ENOSYS;
699 }
700 if (hp->flags & SG_FLAG_MMAP_IO) {
701 if (hp->dxfer_len > sfp->reserve.bufflen) {
702 sg_remove_request(sfp, srp);
703 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
704 }
705 if (hp->flags & SG_FLAG_DIRECT_IO) {
706 sg_remove_request(sfp, srp);
707 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
708 }
709 if (sg_res_in_use(sfp)) {
710 sg_remove_request(sfp, srp);
711 return -EBUSY; /* reserve buffer already being used */
712 }
713 }
714 ul_timeout = msecs_to_jiffies(srp->header.timeout);
715 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
716 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
717 sg_remove_request(sfp, srp);
718 return -EMSGSIZE;
719 }
720 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
721 sg_remove_request(sfp, srp);
722 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
723 }
724 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
725 sg_remove_request(sfp, srp);
726 return -EFAULT;
727 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900728 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 sg_remove_request(sfp, srp);
730 return -EPERM;
731 }
732 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
733 if (k < 0)
734 return k;
735 if (o_srp)
736 *o_srp = srp;
737 return count;
738}
739
740static int
741sg_common_write(Sg_fd * sfp, Sg_request * srp,
742 unsigned char *cmnd, int timeout, int blocking)
743{
Douglas Gilbert16070cc2014-06-04 10:58:30 -0400744 int k, data_dir, at_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 Sg_device *sdp = sfp->parentdp;
746 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
749 hp->status = 0;
750 hp->masked_status = 0;
751 hp->msg_status = 0;
752 hp->info = 0;
753 hp->host_status = 0;
754 hp->driver_status = 0;
755 hp->resid = 0;
756 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
757 (int) cmnd[0], (int) hp->cmd_len));
758
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900759 k = sg_start_req(srp, cmnd);
760 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400761 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sg_finish_rem_req(srp);
763 return k; /* probably out of space --> ENOMEM */
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (sdp->detached) {
FUJITA Tomonoricaf19d32010-07-22 09:36:51 +0900766 if (srp->bio)
767 blk_end_request_all(srp->rq, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sg_finish_rem_req(srp);
769 return -ENODEV;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 switch (hp->dxfer_direction) {
773 case SG_DXFER_TO_FROM_DEV:
774 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600775 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600778 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600781 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783 default:
Mike Christied6b10342005-11-08 04:06:41 -0600784 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786 }
cb59e842005-04-02 13:51:23 -0600787 hp->duration = jiffies_to_msecs(jiffies);
Douglas Gilbert16070cc2014-06-04 10:58:30 -0400788 if (hp->interface_id != '\0' && /* v3 (or later) interface */
789 (SG_FLAG_Q_AT_TAIL & hp->flags))
790 at_head = 0;
791 else
792 at_head = 1;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200793
794 srp->rq->timeout = timeout;
Tony Battersbyc6517b792009-01-21 14:45:50 -0500795 kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200796 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
Douglas Gilbert16070cc2014-06-04 10:58:30 -0400797 srp->rq, at_head, sg_rq_end_io);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Jörn Engel6acddc52012-04-12 17:33:58 -0400801static int srp_done(Sg_fd *sfp, Sg_request *srp)
802{
803 unsigned long flags;
804 int ret;
805
806 read_lock_irqsave(&sfp->rq_list_lock, flags);
807 ret = srp->done;
808 read_unlock_irqrestore(&sfp->rq_list_lock, flags);
809 return ret;
810}
811
Akinobu Mita46f69e62014-06-02 22:56:46 +0900812static int max_sectors_bytes(struct request_queue *q)
813{
814 unsigned int max_sectors = queue_max_sectors(q);
815
816 max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
817
818 return max_sectors << 9;
819}
820
Jörn Engel37b9d1e2012-04-12 17:35:05 -0400821static long
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200822sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 void __user *p = (void __user *)arg;
825 int __user *ip = p;
826 int result, val, read_only;
827 Sg_device *sdp;
828 Sg_fd *sfp;
829 Sg_request *srp;
830 unsigned long iflags;
831
832 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
833 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
836 sdp->disk->disk_name, (int) cmd_in));
837 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
838
839 switch (cmd_in) {
840 case SG_IO:
Jörn Engeldddbf8d2012-04-12 17:32:17 -0400841 if (sdp->detached)
842 return -ENODEV;
843 if (!scsi_block_when_processing_errors(sdp->device))
844 return -ENXIO;
845 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
846 return -EFAULT;
847 result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
848 1, read_only, 1, &srp);
849 if (result < 0)
850 return result;
Jörn Engel3f0c6ab2012-04-12 17:33:25 -0400851 result = wait_event_interruptible(sfp->read_wait,
Jörn Engel6acddc52012-04-12 17:33:58 -0400852 (srp_done(sfp, srp) || sdp->detached));
Jörn Engel794c10f2012-04-12 17:32:48 -0400853 if (sdp->detached)
854 return -ENODEV;
855 write_lock_irq(&sfp->rq_list_lock);
856 if (srp->done) {
857 srp->done = 2;
Jörn Engeldddbf8d2012-04-12 17:32:17 -0400858 write_unlock_irq(&sfp->rq_list_lock);
Jörn Engel794c10f2012-04-12 17:32:48 -0400859 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
860 return (result < 0) ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Jörn Engel794c10f2012-04-12 17:32:48 -0400862 srp->orphan = 1;
863 write_unlock_irq(&sfp->rq_list_lock);
864 return result; /* -ERESTARTSYS because signal hit process */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 case SG_SET_TIMEOUT:
866 result = get_user(val, ip);
867 if (result)
868 return result;
869 if (val < 0)
870 return -EIO;
871 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
872 val = MULDIV (INT_MAX, USER_HZ, HZ);
873 sfp->timeout_user = val;
874 sfp->timeout = MULDIV (val, HZ, USER_HZ);
875
876 return 0;
877 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
878 /* strange ..., for backward compatibility */
879 return sfp->timeout_user;
880 case SG_SET_FORCE_LOW_DMA:
881 result = get_user(val, ip);
882 if (result)
883 return result;
884 if (val) {
885 sfp->low_dma = 1;
886 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
887 val = (int) sfp->reserve.bufflen;
888 sg_remove_scat(&sfp->reserve);
889 sg_build_reserve(sfp, val);
890 }
891 } else {
892 if (sdp->detached)
893 return -ENODEV;
894 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
895 }
896 return 0;
897 case SG_GET_LOW_DMA:
898 return put_user((int) sfp->low_dma, ip);
899 case SG_GET_SCSI_ID:
900 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
901 return -EFAULT;
902 else {
903 sg_scsi_id_t __user *sg_idp = p;
904
905 if (sdp->detached)
906 return -ENODEV;
907 __put_user((int) sdp->device->host->host_no,
908 &sg_idp->host_no);
909 __put_user((int) sdp->device->channel,
910 &sg_idp->channel);
911 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
912 __put_user((int) sdp->device->lun, &sg_idp->lun);
913 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
914 __put_user((short) sdp->device->host->cmd_per_lun,
915 &sg_idp->h_cmd_per_lun);
916 __put_user((short) sdp->device->queue_depth,
917 &sg_idp->d_queue_depth);
918 __put_user(0, &sg_idp->unused[0]);
919 __put_user(0, &sg_idp->unused[1]);
920 return 0;
921 }
922 case SG_SET_FORCE_PACK_ID:
923 result = get_user(val, ip);
924 if (result)
925 return result;
926 sfp->force_packid = val ? 1 : 0;
927 return 0;
928 case SG_GET_PACK_ID:
929 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
930 return -EFAULT;
931 read_lock_irqsave(&sfp->rq_list_lock, iflags);
932 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
933 if ((1 == srp->done) && (!srp->sg_io_owned)) {
934 read_unlock_irqrestore(&sfp->rq_list_lock,
935 iflags);
936 __put_user(srp->header.pack_id, ip);
937 return 0;
938 }
939 }
940 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
941 __put_user(-1, ip);
942 return 0;
943 case SG_GET_NUM_WAITING:
944 read_lock_irqsave(&sfp->rq_list_lock, iflags);
945 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
946 if ((1 == srp->done) && (!srp->sg_io_owned))
947 ++val;
948 }
949 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
950 return put_user(val, ip);
951 case SG_GET_SG_TABLESIZE:
952 return put_user(sdp->sg_tablesize, ip);
953 case SG_SET_RESERVED_SIZE:
954 result = get_user(val, ip);
955 if (result)
956 return result;
957 if (val < 0)
958 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500959 val = min_t(int, val,
Akinobu Mita46f69e62014-06-02 22:56:46 +0900960 max_sectors_bytes(sdp->device->request_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (val != sfp->reserve.bufflen) {
962 if (sg_res_in_use(sfp) || sfp->mmap_called)
963 return -EBUSY;
964 sg_remove_scat(&sfp->reserve);
965 sg_build_reserve(sfp, val);
966 }
967 return 0;
968 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500969 val = min_t(int, sfp->reserve.bufflen,
Akinobu Mita46f69e62014-06-02 22:56:46 +0900970 max_sectors_bytes(sdp->device->request_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return put_user(val, ip);
972 case SG_SET_COMMAND_Q:
973 result = get_user(val, ip);
974 if (result)
975 return result;
976 sfp->cmd_q = val ? 1 : 0;
977 return 0;
978 case SG_GET_COMMAND_Q:
979 return put_user((int) sfp->cmd_q, ip);
980 case SG_SET_KEEP_ORPHAN:
981 result = get_user(val, ip);
982 if (result)
983 return result;
984 sfp->keep_orphan = val;
985 return 0;
986 case SG_GET_KEEP_ORPHAN:
987 return put_user((int) sfp->keep_orphan, ip);
988 case SG_NEXT_CMD_LEN:
989 result = get_user(val, ip);
990 if (result)
991 return result;
992 sfp->next_cmd_len = (val > 0) ? val : 0;
993 return 0;
994 case SG_GET_VERSION_NUM:
995 return put_user(sg_version_num, ip);
996 case SG_GET_ACCESS_COUNT:
997 /* faked - we don't have a real access count anymore */
998 val = (sdp->device ? 1 : 0);
999 return put_user(val, ip);
1000 case SG_GET_REQUEST_TABLE:
1001 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
1002 return -EFAULT;
1003 else {
cb59e842005-04-02 13:51:23 -06001004 sg_req_info_t *rinfo;
1005 unsigned int ms;
1006
1007 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
1008 GFP_KERNEL);
1009 if (!rinfo)
1010 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1012 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
1013 ++val, srp = srp ? srp->nextrp : srp) {
1014 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
1015 if (srp) {
1016 rinfo[val].req_state = srp->done + 1;
1017 rinfo[val].problem =
1018 srp->header.masked_status &
1019 srp->header.host_status &
1020 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -06001021 if (srp->done)
1022 rinfo[val].duration =
1023 srp->header.duration;
1024 else {
1025 ms = jiffies_to_msecs(jiffies);
1026 rinfo[val].duration =
1027 (ms > srp->header.duration) ?
1028 (ms - srp->header.duration) : 0;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001031 rinfo[val].sg_io_owned =
1032 srp->sg_io_owned;
1033 rinfo[val].pack_id =
1034 srp->header.pack_id;
1035 rinfo[val].usr_ptr =
1036 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 }
1039 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001040 result = __copy_to_user(p, rinfo,
1041 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1042 result = result ? -EFAULT : 0;
1043 kfree(rinfo);
1044 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 case SG_EMULATED_HOST:
1047 if (sdp->detached)
1048 return -ENODEV;
1049 return put_user(sdp->device->host->hostt->emulated, ip);
1050 case SG_SCSI_RESET:
1051 if (sdp->detached)
1052 return -ENODEV;
1053 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001054 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -EBUSY;
1056 } else if (!scsi_block_when_processing_errors(sdp->device))
1057 return -EBUSY;
1058 result = get_user(val, ip);
1059 if (result)
1060 return result;
1061 if (SG_SCSI_RESET_NOTHING == val)
1062 return 0;
1063 switch (val) {
1064 case SG_SCSI_RESET_DEVICE:
1065 val = SCSI_TRY_RESET_DEVICE;
1066 break;
Brian King39120e12008-07-01 13:03:19 -05001067 case SG_SCSI_RESET_TARGET:
1068 val = SCSI_TRY_RESET_TARGET;
1069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case SG_SCSI_RESET_BUS:
1071 val = SCSI_TRY_RESET_BUS;
1072 break;
1073 case SG_SCSI_RESET_HOST:
1074 val = SCSI_TRY_RESET_HOST;
1075 break;
1076 default:
1077 return -EINVAL;
1078 }
1079 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1080 return -EACCES;
1081 return (scsi_reset_provider(sdp->device, val) ==
1082 SUCCESS) ? 0 : -EIO;
1083 case SCSI_IOCTL_SEND_COMMAND:
1084 if (sdp->detached)
1085 return -ENODEV;
1086 if (read_only) {
1087 unsigned char opcode = WRITE_6;
1088 Scsi_Ioctl_Command __user *siocp = p;
1089
1090 if (copy_from_user(&opcode, siocp->data, 1))
1091 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001092 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -EPERM;
1094 }
Al Viroe915e872008-09-02 17:16:41 -04001095 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 case SG_SET_DEBUG:
1097 result = get_user(val, ip);
1098 if (result)
1099 return result;
1100 sdp->sgdebug = (char) val;
1101 return 0;
1102 case SCSI_IOCTL_GET_IDLUN:
1103 case SCSI_IOCTL_GET_BUS_NUMBER:
1104 case SCSI_IOCTL_PROBE_HOST:
1105 case SG_GET_TRANSFORM:
1106 if (sdp->detached)
1107 return -ENODEV;
1108 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001109 case BLKSECTGET:
Akinobu Mita46f69e62014-06-02 22:56:46 +09001110 return put_user(max_sectors_bytes(sdp->device->request_queue),
Alan Stern44ec9542007-02-20 11:01:57 -05001111 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001112 case BLKTRACESETUP:
1113 return blk_trace_setup(sdp->device->request_queue,
1114 sdp->disk->disk_name,
Martin Peschke76e3a192009-01-30 15:46:23 +01001115 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
Shawn Dud0deef52009-04-14 13:58:56 +08001116 NULL,
Christof Schmitt6da127a2008-01-11 10:09:43 +01001117 (char *)arg);
1118 case BLKTRACESTART:
1119 return blk_trace_startstop(sdp->device->request_queue, 1);
1120 case BLKTRACESTOP:
1121 return blk_trace_startstop(sdp->device->request_queue, 0);
1122 case BLKTRACETEARDOWN:
1123 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 default:
1125 if (read_only)
1126 return -EPERM; /* don't know so take safe approach */
1127 return scsi_ioctl(sdp->device, cmd_in, p);
1128 }
1129}
1130
1131#ifdef CONFIG_COMPAT
1132static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1133{
1134 Sg_device *sdp;
1135 Sg_fd *sfp;
1136 struct scsi_device *sdev;
1137
1138 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1139 return -ENXIO;
1140
1141 sdev = sdp->device;
1142 if (sdev->host->hostt->compat_ioctl) {
1143 int ret;
1144
1145 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1146
1147 return ret;
1148 }
1149
1150 return -ENOIOCTLCMD;
1151}
1152#endif
1153
1154static unsigned int
1155sg_poll(struct file *filp, poll_table * wait)
1156{
1157 unsigned int res = 0;
1158 Sg_device *sdp;
1159 Sg_fd *sfp;
1160 Sg_request *srp;
1161 int count = 0;
1162 unsigned long iflags;
1163
Jörn Engelebaf4662012-04-12 17:33:39 -04001164 sfp = filp->private_data;
1165 if (!sfp)
1166 return POLLERR;
1167 sdp = sfp->parentdp;
1168 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return POLLERR;
1170 poll_wait(filp, &sfp->read_wait, wait);
1171 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1172 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1173 /* if any read waiting, flag it */
1174 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1175 res = POLLIN | POLLRDNORM;
1176 ++count;
1177 }
1178 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1179
1180 if (sdp->detached)
1181 res |= POLLHUP;
1182 else if (!sfp->cmd_q) {
1183 if (0 == count)
1184 res |= POLLOUT | POLLWRNORM;
1185 } else if (count < SG_MAX_QUEUE)
1186 res |= POLLOUT | POLLWRNORM;
1187 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1188 sdp->disk->disk_name, (int) res));
1189 return res;
1190}
1191
1192static int
1193sg_fasync(int fd, struct file *filp, int mode)
1194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 Sg_device *sdp;
1196 Sg_fd *sfp;
1197
1198 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1199 return -ENXIO;
1200 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1201 sdp->disk->disk_name, mode));
1202
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001203 return fasync_helper(fd, filp, mode, &sfp->async_qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Nick Piggina13ff0b2008-02-07 18:46:06 -08001206static int
1207sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001210 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001212 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001215 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001217 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001219 return VM_FAULT_SIGBUS;
1220 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001222 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001223 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1224 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001225 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001226 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001227 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001228 struct page *page = nth_page(rsv_schp->pages[k],
1229 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001230 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001231 vmf->page = page;
1232 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Mike Christied6b10342005-11-08 04:06:41 -06001234 sa += len;
1235 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Mike Christied6b10342005-11-08 04:06:41 -06001237
Nick Piggina13ff0b2008-02-07 18:46:06 -08001238 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001241static const struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001242 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243};
1244
1245static int
1246sg_mmap(struct file *filp, struct vm_area_struct *vma)
1247{
1248 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001249 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001251 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1254 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001255 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1257 (void *) vma->vm_start, (int) req_sz));
1258 if (vma->vm_pgoff)
1259 return -EINVAL; /* want no offset */
1260 rsv_schp = &sfp->reserve;
1261 if (req_sz > rsv_schp->bufflen)
1262 return -ENOMEM; /* cannot map more than reserved buffer */
1263
Mike Christied6b10342005-11-08 04:06:41 -06001264 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001265 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1266 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001267 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001268 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001269 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Mike Christied6b10342005-11-08 04:06:41 -06001271
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001272 sfp->mmap_called = 1;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001273 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 vma->vm_private_data = sfp;
1275 vma->vm_ops = &sg_mmap_vm_ops;
1276 return 0;
1277}
1278
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001279static void sg_rq_end_io_usercontext(struct work_struct *work)
1280{
1281 struct sg_request *srp = container_of(work, struct sg_request, ew.work);
1282 struct sg_fd *sfp = srp->parentfp;
1283
1284 sg_finish_rem_req(srp);
1285 kref_put(&sfp->f_ref, sg_remove_sfp);
1286}
1287
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001288/*
1289 * This function is a "bottom half" handler that is called by the mid
1290 * level when a command is completed (or has failed).
1291 */
1292static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001294 struct sg_request *srp = rq->end_io_data;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001295 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001298 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001299 char *sense;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001300 int result, resid, done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Tony Battersbyc6517b792009-01-21 14:45:50 -05001302 if (WARN_ON(srp->done != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 sfp = srp->parentfp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001306 if (WARN_ON(sfp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001308
1309 sdp = sfp->parentdp;
1310 if (unlikely(sdp->detached))
1311 printk(KERN_INFO "sg_rq_end_io: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001313 sense = rq->sense;
1314 result = rq->errors;
Tejun Heoc3a4d782009-05-07 22:24:37 +09001315 resid = rq->resid_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001318 sdp->disk->disk_name, srp->header.pack_id, result));
1319 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001320 ms = jiffies_to_msecs(jiffies);
1321 srp->header.duration = (ms > srp->header.duration) ?
1322 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001323 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 struct scsi_sense_hdr sshdr;
1325
Mike Christied6b10342005-11-08 04:06:41 -06001326 srp->header.status = 0xff & result;
1327 srp->header.masked_status = status_byte(result);
1328 srp->header.msg_status = msg_byte(result);
1329 srp->header.host_status = host_byte(result);
1330 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if ((sdp->sgdebug > 0) &&
1332 ((CHECK_CONDITION == srp->header.masked_status) ||
1333 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001334 __scsi_print_sense("sg_cmd_done", sense,
1335 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001338 if (driver_byte(result) != 0
1339 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 && !scsi_sense_is_deferred(&sshdr)
1341 && sshdr.sense_key == UNIT_ATTENTION
1342 && sdp->device->removable) {
1343 /* Detected possible disc change. Set the bit - this */
1344 /* may be used if there are filesystems using this device */
1345 sdp->device->changed = 1;
1346 }
1347 }
1348 /* Rely on write phase to clean out srp status values, so no "else" */
1349
Tony Battersbyc6517b792009-01-21 14:45:50 -05001350 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1351 if (unlikely(srp->orphan)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (sfp->keep_orphan)
1353 srp->sg_io_owned = 0;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001354 else
1355 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001357 srp->done = done;
1358 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1359
1360 if (likely(done)) {
1361 /* Now wake up any sg_read() that is waiting for this
1362 * packet.
1363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 wake_up_interruptible(&sfp->read_wait);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001365 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
FUJITA Tomonoric96952e2009-02-04 11:36:27 +09001366 kref_put(&sfp->f_ref, sg_remove_sfp);
FUJITA Tomonori015640e2009-04-03 19:28:06 +09001367 } else {
1368 INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
1369 schedule_work(&srp->ew.work);
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001373static const struct file_operations sg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 .owner = THIS_MODULE,
1375 .read = sg_read,
1376 .write = sg_write,
1377 .poll = sg_poll,
Jörn Engel37b9d1e2012-04-12 17:35:05 -04001378 .unlocked_ioctl = sg_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#ifdef CONFIG_COMPAT
1380 .compat_ioctl = sg_compat_ioctl,
1381#endif
1382 .open = sg_open,
1383 .mmap = sg_mmap,
1384 .release = sg_release,
1385 .fasync = sg_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001386 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387};
1388
gregkh@suse.ded2538782005-03-23 09:55:22 -08001389static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391static int sg_sysfs_valid = 0;
1392
James Bottomley7c07d612007-08-05 13:36:11 -05001393static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Mike Christied6b10342005-11-08 04:06:41 -06001395 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 Sg_device *sdp;
1397 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001398 int error;
1399 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Jes Sorensen24669f752006-01-16 10:31:18 -05001401 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (!sdp) {
1403 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001404 return ERR_PTR(-ENOMEM);
1405 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05001406
Tejun Heob98c52b2013-02-27 17:04:42 -08001407 idr_preload(GFP_KERNEL);
James Bottomley7c07d612007-08-05 13:36:11 -05001408 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Tejun Heob98c52b2013-02-27 17:04:42 -08001410 error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
1411 if (error < 0) {
1412 if (error == -ENOSPC) {
1413 sdev_printk(KERN_WARNING, scsidp,
1414 "Unable to attach sg device type=%d, minor number exceeds %d\n",
1415 scsidp->type, SG_MAX_DEVS - 1);
1416 error = -ENODEV;
1417 } else {
1418 printk(KERN_WARNING
1419 "idr allocation Sg_device failure: %d\n", error);
1420 }
1421 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Tejun Heob98c52b2013-02-27 17:04:42 -08001423 k = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1426 sprintf(disk->disk_name, "sg%d", k);
1427 disk->first_minor = k;
1428 sdp->disk = disk;
1429 sdp->device = scsidp;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09001430 INIT_LIST_HEAD(&sdp->sfds);
James Bottomley065b4a22013-10-25 10:27:02 +01001431 init_waitqueue_head(&sdp->o_excl_wait);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001432 sdp->sg_tablesize = queue_max_segments(q);
James Bottomley7c07d612007-08-05 13:36:11 -05001433 sdp->index = k;
Tony Battersbyc6517b792009-01-21 14:45:50 -05001434 kref_init(&sdp->d_ref);
James Bottomley7c07d612007-08-05 13:36:11 -05001435 error = 0;
Tejun Heob98c52b2013-02-27 17:04:42 -08001436
1437out_unlock:
1438 write_unlock_irqrestore(&sg_index_lock, iflags);
1439 idr_preload_end();
1440
James Bottomley7c07d612007-08-05 13:36:11 -05001441 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001443 return ERR_PTR(error);
1444 }
1445 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448static int
Tony Jonesee959b02008-02-22 00:13:36 +01001449sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Tony Jonesee959b02008-02-22 00:13:36 +01001451 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct gendisk *disk;
1453 Sg_device *sdp = NULL;
1454 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001455 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001456 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 disk = alloc_disk(1);
1459 if (!disk) {
1460 printk(KERN_WARNING "alloc_disk failed\n");
1461 return -ENOMEM;
1462 }
1463 disk->major = SCSI_GENERIC_MAJOR;
1464
1465 error = -ENOMEM;
1466 cdev = cdev_alloc();
1467 if (!cdev) {
1468 printk(KERN_WARNING "cdev_alloc failed\n");
1469 goto out;
1470 }
1471 cdev->owner = THIS_MODULE;
1472 cdev->ops = &sg_fops;
1473
James Bottomley7c07d612007-08-05 13:36:11 -05001474 sdp = sg_alloc(disk, scsidp);
1475 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001477 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 goto out;
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
James Bottomley7c07d612007-08-05 13:36:11 -05001481 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001482 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001483 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sdp->cdev = cdev;
1486 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001487 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Greg Kroah-Hartmand73a1a672008-07-21 20:03:34 -07001489 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1490 MKDEV(SCSI_GENERIC_MAJOR,
1491 sdp->index),
1492 sdp, "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001493 if (IS_ERR(sg_class_member)) {
1494 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001495 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001496 error = PTR_ERR(sg_class_member);
1497 goto cdev_add_err;
1498 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001499 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 &sg_class_member->kobj, "generic");
1501 if (error)
1502 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001503 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001505 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
James Bottomley9ccfc752005-10-02 11:45:08 -05001507 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001508 "Attached scsi generic sg%d type %d\n", sdp->index,
1509 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Tony Jonesee959b02008-02-22 00:13:36 +01001511 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001515cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001516 write_lock_irqsave(&sg_index_lock, iflags);
1517 idr_remove(&sg_index_idr, sdp->index);
1518 write_unlock_irqrestore(&sg_index_lock, iflags);
1519 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521out:
1522 put_disk(disk);
1523 if (cdev)
1524 cdev_del(cdev);
1525 return error;
1526}
1527
Tony Battersbyc6517b792009-01-21 14:45:50 -05001528static void sg_device_destroy(struct kref *kref)
1529{
1530 struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1531 unsigned long flags;
1532
1533 /* CAUTION! Note that the device can still be found via idr_find()
1534 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1535 * any other cleanup.
1536 */
1537
1538 write_lock_irqsave(&sg_index_lock, flags);
1539 idr_remove(&sg_index_idr, sdp->index);
1540 write_unlock_irqrestore(&sg_index_lock, flags);
1541
1542 SCSI_LOG_TIMEOUT(3,
1543 printk("sg_device_destroy: %s\n",
1544 sdp->disk->disk_name));
1545
1546 put_disk(sdp->disk);
1547 kfree(sdp);
1548}
1549
1550static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Tony Jonesee959b02008-02-22 00:13:36 +01001552 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1553 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 unsigned long iflags;
1555 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Tony Battersbyc6517b792009-01-21 14:45:50 -05001557 if (!sdp || sdp->detached)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Tony Battersbyc6517b792009-01-21 14:45:50 -05001560 SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
1561
1562 /* Need a write lock to set sdp->detached. */
James Bottomley7c07d612007-08-05 13:36:11 -05001563 write_lock_irqsave(&sg_index_lock, iflags);
Tony Battersbyc6517b792009-01-21 14:45:50 -05001564 sdp->detached = 1;
FUJITA Tomonori3442f802009-02-16 13:26:53 +09001565 list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05001566 wake_up_interruptible(&sfp->read_wait);
1567 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
James Bottomley7c07d612007-08-05 13:36:11 -05001569 write_unlock_irqrestore(&sg_index_lock, iflags);
1570
1571 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001572 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001573 cdev_del(sdp->cdev);
1574 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Tony Battersbyc6517b792009-01-21 14:45:50 -05001576 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Douglas Gilbert6460e752006-09-20 18:20:49 -04001579module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1580module_param_named(def_reserved_size, def_reserved_size, int,
1581 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1583
1584MODULE_AUTHOR("Douglas Gilbert");
1585MODULE_DESCRIPTION("SCSI generic (sg) driver");
1586MODULE_LICENSE("GPL");
1587MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001588MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Douglas Gilbert6460e752006-09-20 18:20:49 -04001590MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1591 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1593MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1594
1595static int __init
1596init_sg(void)
1597{
1598 int rc;
1599
Douglas Gilbert6460e752006-09-20 18:20:49 -04001600 if (scatter_elem_sz < PAGE_SIZE) {
1601 scatter_elem_sz = PAGE_SIZE;
1602 scatter_elem_sz_prev = scatter_elem_sz;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (def_reserved_size >= 0)
1605 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001606 else
1607 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1610 SG_MAX_DEVS, "sg");
1611 if (rc)
1612 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001613 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if ( IS_ERR(sg_sysfs_class) ) {
1615 rc = PTR_ERR(sg_sysfs_class);
1616 goto err_out;
1617 }
1618 sg_sysfs_valid = 1;
1619 rc = scsi_register_interface(&sg_interface);
1620 if (0 == rc) {
1621#ifdef CONFIG_SCSI_PROC_FS
1622 sg_proc_init();
1623#endif /* CONFIG_SCSI_PROC_FS */
1624 return 0;
1625 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001626 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627err_out:
1628 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1629 return rc;
1630}
1631
1632static void __exit
1633exit_sg(void)
1634{
1635#ifdef CONFIG_SCSI_PROC_FS
1636 sg_proc_cleanup();
1637#endif /* CONFIG_SCSI_PROC_FS */
1638 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001639 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sg_sysfs_valid = 0;
1641 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1642 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001643 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001646static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001647{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001648 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001649 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001650 Sg_fd *sfp = srp->parentfp;
1651 sg_io_hdr_t *hp = &srp->header;
1652 int dxfer_len = (int) hp->dxfer_len;
1653 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001654 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001655 Sg_scatter_hold *req_schp = &srp->data;
1656 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1657 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001658 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001659 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001660 unsigned char *long_cmdp = NULL;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001661
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001662 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1663 dxfer_len));
1664
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001665 if (hp->cmd_len > BLK_MAX_CDB) {
1666 long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
1667 if (!long_cmdp)
1668 return -ENOMEM;
1669 }
1670
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001671 rq = blk_get_request(q, rw, GFP_ATOMIC);
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001672 if (!rq) {
1673 kfree(long_cmdp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001674 return -ENOMEM;
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001675 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001676
Jens Axboef27b0872014-06-06 07:57:37 -06001677 blk_rq_set_block_pc(rq);
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001678
1679 if (hp->cmd_len > BLK_MAX_CDB)
1680 rq->cmd = long_cmdp;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001681 memcpy(rq->cmd, cmd, hp->cmd_len);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001682 rq->cmd_len = hp->cmd_len;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001683
1684 srp->rq = rq;
1685 rq->end_io_data = srp;
1686 rq->sense = srp->sense_b;
1687 rq->retries = SG_DEFAULT_RETRIES;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001690 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001691
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001692 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1693 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1694 !sfp->parentdp->device->host->unchecked_isa_dma &&
Namhyung Kim2610a252010-09-16 12:55:57 +09001695 blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001696 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001697 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001698 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001699
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001700 if (md) {
1701 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1702 sg_link_reserve(sfp, srp, dxfer_len);
1703 else {
1704 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1705 if (res)
1706 return res;
1707 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001708
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001709 md->pages = req_schp->pages;
1710 md->page_order = req_schp->page_order;
1711 md->nr_entries = req_schp->k_use_sg;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001712 md->offset = 0;
FUJITA Tomonori97ae77a2008-12-18 14:49:38 +09001713 md->null_mapped = hp->dxferp ? 0 : 1;
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001714 if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
1715 md->from_user = 1;
1716 else
1717 md->from_user = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001719
FUJITA Tomonori0fdf96b2009-04-03 09:12:20 +09001720 if (iov_count) {
1721 int len, size = sizeof(struct sg_iovec) * iov_count;
1722 struct iovec *iov;
1723
Julia Lawall30941412010-08-10 18:01:27 -07001724 iov = memdup_user(hp->dxferp, size);
1725 if (IS_ERR(iov))
1726 return PTR_ERR(iov);
FUJITA Tomonori0fdf96b2009-04-03 09:12:20 +09001727
1728 len = iov_length(iov, iov_count);
1729 if (hp->dxfer_len < len) {
1730 iov_count = iov_shorten(iov, iov_count, hp->dxfer_len);
1731 len = hp->dxfer_len;
1732 }
1733
1734 res = blk_rq_map_user_iov(q, rq, md, (struct sg_iovec *)iov,
1735 iov_count,
1736 len, GFP_ATOMIC);
1737 kfree(iov);
1738 } else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001739 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1740 hp->dxfer_len, GFP_ATOMIC);
1741
1742 if (!res) {
1743 srp->bio = rq->bio;
1744
1745 if (!md) {
1746 req_schp->dio_in_use = 1;
1747 hp->info |= SG_INFO_DIRECT_IO;
1748 }
1749 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001750 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001753static int sg_finish_rem_req(Sg_request * srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001755 int ret = 0;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 Sg_fd *sfp = srp->parentfp;
1758 Sg_scatter_hold *req_schp = &srp->data;
1759
1760 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001761 if (srp->rq) {
1762 if (srp->bio)
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001763 ret = blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001764
Douglas Gilbert65c26a02014-06-03 13:18:18 -04001765 if (srp->rq->cmd != srp->rq->__cmd)
1766 kfree(srp->rq->cmd);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001767 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001768 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001769
Christof Schmitte27168f2009-09-17 09:10:14 +02001770 if (srp->res_used)
1771 sg_unlink_reserve(sfp, srp);
1772 else
1773 sg_remove_scat(req_schp);
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 sg_remove_request(sfp, srp);
FUJITA Tomonorie7ee4cc2009-04-04 00:35:42 +09001776
1777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780static int
1781sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1782{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001783 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001784 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001786 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1787 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001790 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static int
1794sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1795{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001796 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001797 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001798 int blk_size = buff_size, order;
1799 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Eric Sesterhennfb119932007-05-23 14:41:36 -07001801 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return -EFAULT;
1803 if (0 == blk_size)
1804 ++blk_size; /* don't know why */
FUJITA Tomonorib2ed6c62009-02-12 02:42:57 +09001805 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1806 blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1808 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001809
1810 /* N.B. ret_sz carried into this block ... */
1811 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1812 if (mx_sc_elems < 0)
1813 return mx_sc_elems; /* most likely -ENOMEM */
1814
Douglas Gilbert6460e752006-09-20 18:20:49 -04001815 num = scatter_elem_sz;
1816 if (unlikely(num != scatter_elem_sz_prev)) {
1817 if (num < PAGE_SIZE) {
1818 scatter_elem_sz = PAGE_SIZE;
1819 scatter_elem_sz_prev = PAGE_SIZE;
1820 } else
1821 scatter_elem_sz_prev = num;
1822 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001823
1824 if (sfp->low_dma)
1825 gfp_mask |= GFP_DMA;
1826
1827 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1828 gfp_mask |= __GFP_ZERO;
1829
1830 order = get_order(num);
1831retry:
1832 ret_sz = 1 << (PAGE_SHIFT + order);
1833
1834 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1835 k++, rem_sz -= ret_sz) {
1836
Douglas Gilbert6460e752006-09-20 18:20:49 -04001837 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001838 scatter_elem_sz_prev : rem_sz;
1839
1840 schp->pages[k] = alloc_pages(gfp_mask, order);
1841 if (!schp->pages[k])
1842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Douglas Gilbert6460e752006-09-20 18:20:49 -04001844 if (num == scatter_elem_sz_prev) {
1845 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1846 scatter_elem_sz = ret_sz;
1847 scatter_elem_sz_prev = ret_sz;
1848 }
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001851 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1852 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001853 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001855 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001856 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001857 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1858 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001859
1860 schp->bufflen = blk_size;
1861 if (rem_sz > 0) /* must have failed */
1862 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001864out:
1865 for (i = 0; i < k; i++)
Michal Schmidte71044e2009-09-03 14:27:08 +02001866 __free_pages(schp->pages[i], order);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001867
1868 if (--order >= 0)
1869 goto retry;
1870
1871 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874static void
1875sg_remove_scat(Sg_scatter_hold * schp)
1876{
1877 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001878 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001879 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int k;
1881
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001882 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001884 "sg_remove_scat: k=%d, pg=0x%p\n",
1885 k, schp->pages[k]));
1886 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001888
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001889 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
Mike Christied6b10342005-11-08 04:06:41 -06001891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 memset(schp, 0, sizeof (*schp));
1893}
1894
1895static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1897{
1898 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001899 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1902 num_read_xfer));
1903 if ((!outp) || (num_read_xfer <= 0))
1904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001906 num = 1 << (PAGE_SHIFT + schp->page_order);
1907 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001908 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001909 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001910 num_read_xfer))
1911 return -EFAULT;
1912 break;
1913 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001914 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001915 num))
1916 return -EFAULT;
1917 num_read_xfer -= num;
1918 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 break;
Mike Christied6b10342005-11-08 04:06:41 -06001920 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Mike Christied6b10342005-11-08 04:06:41 -06001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
1927static void
1928sg_build_reserve(Sg_fd * sfp, int req_size)
1929{
1930 Sg_scatter_hold *schp = &sfp->reserve;
1931
1932 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1933 do {
1934 if (req_size < PAGE_SIZE)
1935 req_size = PAGE_SIZE;
1936 if (0 == sg_build_indirect(schp, sfp, req_size))
1937 return;
1938 else
1939 sg_remove_scat(schp);
1940 req_size >>= 1; /* divide by 2 */
1941 } while (req_size > (PAGE_SIZE / 2));
1942}
1943
1944static void
1945sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1946{
1947 Sg_scatter_hold *req_schp = &srp->data;
1948 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001949 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 srp->res_used = 1;
1952 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001953 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001955 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1956 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001957 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001958 req_schp->k_use_sg = k + 1;
1959 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001960 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001961
1962 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001963 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001964 break;
1965 } else
1966 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Mike Christied6b10342005-11-08 04:06:41 -06001968
1969 if (k >= rsv_schp->k_use_sg)
1970 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static void
1974sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1975{
1976 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1979 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 req_schp->k_use_sg = 0;
1981 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001982 req_schp->pages = NULL;
1983 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 req_schp->sglist_len = 0;
1985 sfp->save_scat_len = 0;
1986 srp->res_used = 0;
1987}
1988
1989static Sg_request *
1990sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1991{
1992 Sg_request *resp;
1993 unsigned long iflags;
1994
1995 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1996 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1997 /* look for requests that are ready + not SG_IO owned */
1998 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1999 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2000 resp->done = 2; /* guard against other readers */
2001 break;
2002 }
2003 }
2004 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2005 return resp;
2006}
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008/* always adds to end of list */
2009static Sg_request *
2010sg_add_request(Sg_fd * sfp)
2011{
2012 int k;
2013 unsigned long iflags;
2014 Sg_request *resp;
2015 Sg_request *rp = sfp->req_arr;
2016
2017 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2018 resp = sfp->headrp;
2019 if (!resp) {
2020 memset(rp, 0, sizeof (Sg_request));
2021 rp->parentfp = sfp;
2022 resp = rp;
2023 sfp->headrp = resp;
2024 } else {
2025 if (0 == sfp->cmd_q)
2026 resp = NULL; /* command queuing disallowed */
2027 else {
2028 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2029 if (!rp->parentfp)
2030 break;
2031 }
2032 if (k < SG_MAX_QUEUE) {
2033 memset(rp, 0, sizeof (Sg_request));
2034 rp->parentfp = sfp;
2035 while (resp->nextrp)
2036 resp = resp->nextrp;
2037 resp->nextrp = rp;
2038 resp = rp;
2039 } else
2040 resp = NULL;
2041 }
2042 }
2043 if (resp) {
2044 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002045 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2048 return resp;
2049}
2050
2051/* Return of 1 for found; 0 for not found */
2052static int
2053sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2054{
2055 Sg_request *prev_rp;
2056 Sg_request *rp;
2057 unsigned long iflags;
2058 int res = 0;
2059
2060 if ((!sfp) || (!srp) || (!sfp->headrp))
2061 return res;
2062 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2063 prev_rp = sfp->headrp;
2064 if (srp == prev_rp) {
2065 sfp->headrp = prev_rp->nextrp;
2066 prev_rp->parentfp = NULL;
2067 res = 1;
2068 } else {
2069 while ((rp = prev_rp->nextrp)) {
2070 if (srp == rp) {
2071 prev_rp->nextrp = rp->nextrp;
2072 rp->parentfp = NULL;
2073 res = 1;
2074 break;
2075 }
2076 prev_rp = rp;
2077 }
2078 }
2079 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2080 return res;
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static Sg_fd *
2084sg_add_sfp(Sg_device * sdp, int dev)
2085{
2086 Sg_fd *sfp;
2087 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002088 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Mike Christied6b10342005-11-08 04:06:41 -06002090 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!sfp)
James Bottomleybafc8ad2013-10-25 10:25:14 +01002092 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 init_waitqueue_head(&sfp->read_wait);
2095 rwlock_init(&sfp->rq_list_lock);
2096
Tony Battersbyc6517b792009-01-21 14:45:50 -05002097 kref_init(&sfp->f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 sfp->timeout = SG_DEFAULT_TIMEOUT;
2099 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2100 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2101 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2102 sdp->device->host->unchecked_isa_dma : 1;
2103 sfp->cmd_q = SG_DEF_COMMAND_Q;
2104 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2105 sfp->parentdp = sdp;
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002106 write_lock_irqsave(&sg_index_lock, iflags);
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002107 list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002108 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002110 if (unlikely(sg_big_buff != def_reserved_size))
2111 sg_big_buff = def_reserved_size;
2112
Alan Stern44ec9542007-02-20 11:01:57 -05002113 bufflen = min_t(int, sg_big_buff,
Akinobu Mita46f69e62014-06-02 22:56:46 +09002114 max_sectors_bytes(sdp->device->request_queue));
Alan Stern44ec9542007-02-20 11:01:57 -05002115 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2117 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
Tony Battersbyc6517b792009-01-21 14:45:50 -05002118
2119 kref_get(&sdp->d_ref);
2120 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return sfp;
2122}
2123
Tony Battersbyc6517b792009-01-21 14:45:50 -05002124static void sg_remove_sfp_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002126 struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2127 struct sg_device *sdp = sfp->parentdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Tony Battersbyc6517b792009-01-21 14:45:50 -05002129 /* Cleanup any responses which were never read(). */
2130 while (sfp->headrp)
2131 sg_finish_rem_req(sfp->headrp);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (sfp->reserve.bufflen > 0) {
Tony Battersbyc6517b792009-01-21 14:45:50 -05002134 SCSI_LOG_TIMEOUT(6,
2135 printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2136 (int) sfp->reserve.bufflen,
2137 (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 sg_remove_scat(&sfp->reserve);
2139 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002140
2141 SCSI_LOG_TIMEOUT(6,
2142 printk("sg_remove_sfp: %s, sfp=0x%p\n",
2143 sdp->disk->disk_name,
2144 sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002145 kfree(sfp);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002146
2147 scsi_device_put(sdp->device);
2148 sg_put_dev(sdp);
2149 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Tony Battersbyc6517b792009-01-21 14:45:50 -05002152static void sg_remove_sfp(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002154 struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
James Bottomley065b4a22013-10-25 10:27:02 +01002155 struct sg_device *sdp = sfp->parentdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002156 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002158 write_lock_irqsave(&sg_index_lock, iflags);
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002159 list_del(&sfp->sfd_siblings);
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002160 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley065b4a22013-10-25 10:27:02 +01002161 wake_up_interruptible(&sdp->o_excl_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
FUJITA Tomonori015640e2009-04-03 19:28:06 +09002163 INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
2164 schedule_work(&sfp->ew.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167static int
2168sg_res_in_use(Sg_fd * sfp)
2169{
2170 const Sg_request *srp;
2171 unsigned long iflags;
2172
2173 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2174 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2175 if (srp->res_used)
2176 break;
2177 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2178 return srp ? 1 : 0;
2179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181#ifdef CONFIG_SCSI_PROC_FS
2182static int
James Bottomley7c07d612007-08-05 13:36:11 -05002183sg_idr_max_id(int id, void *p, void *data)
2184{
2185 int *k = data;
2186
2187 if (*k < id)
2188 *k = id;
2189
2190 return 0;
2191}
2192
2193static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194sg_last_dev(void)
2195{
Tony Battersby53474c02008-01-22 15:25:49 -05002196 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 unsigned long iflags;
2198
James Bottomley7c07d612007-08-05 13:36:11 -05002199 read_lock_irqsave(&sg_index_lock, iflags);
2200 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2201 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return k + 1; /* origin 1 */
2203}
2204#endif
2205
Tony Battersbyc6517b792009-01-21 14:45:50 -05002206/* must be called with sg_index_lock held */
2207static Sg_device *sg_lookup_dev(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Tony Battersbyc6517b792009-01-21 14:45:50 -05002209 return idr_find(&sg_index_idr, dev);
2210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Tony Battersbyc6517b792009-01-21 14:45:50 -05002212static Sg_device *sg_get_dev(int dev)
2213{
2214 struct sg_device *sdp;
2215 unsigned long flags;
2216
2217 read_lock_irqsave(&sg_index_lock, flags);
2218 sdp = sg_lookup_dev(dev);
2219 if (!sdp)
2220 sdp = ERR_PTR(-ENXIO);
2221 else if (sdp->detached) {
2222 /* If sdp->detached, then the refcount may already be 0, in
2223 * which case it would be a bug to do kref_get().
2224 */
2225 sdp = ERR_PTR(-ENODEV);
2226 } else
2227 kref_get(&sdp->d_ref);
2228 read_unlock_irqrestore(&sg_index_lock, flags);
James Bottomley7c07d612007-08-05 13:36:11 -05002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return sdp;
2231}
2232
Tony Battersbyc6517b792009-01-21 14:45:50 -05002233static void sg_put_dev(struct sg_device *sdp)
2234{
2235 kref_put(&sdp->d_ref, sg_device_destroy);
2236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238#ifdef CONFIG_SCSI_PROC_FS
2239
2240static struct proc_dir_entry *sg_proc_sgp = NULL;
2241
2242static char sg_proc_sg_dirname[] = "scsi/sg";
2243
2244static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2245
2246static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2247static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2248 size_t count, loff_t *off);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002249static const struct file_operations adio_fops = {
2250 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 .open = sg_proc_single_open_adio,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002252 .read = seq_read,
2253 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 .write = sg_proc_write_adio,
2255 .release = single_release,
2256};
2257
2258static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2259static ssize_t sg_proc_write_dressz(struct file *filp,
2260 const char __user *buffer, size_t count, loff_t *off);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002261static const struct file_operations dressz_fops = {
2262 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 .open = sg_proc_single_open_dressz,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002264 .read = seq_read,
2265 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 .write = sg_proc_write_dressz,
2267 .release = single_release,
2268};
2269
2270static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2271static int sg_proc_single_open_version(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002272static const struct file_operations version_fops = {
2273 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .open = sg_proc_single_open_version,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002275 .read = seq_read,
2276 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 .release = single_release,
2278};
2279
2280static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2281static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002282static const struct file_operations devhdr_fops = {
2283 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 .open = sg_proc_single_open_devhdr,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002285 .read = seq_read,
2286 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 .release = single_release,
2288};
2289
2290static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2291static int sg_proc_open_dev(struct inode *inode, struct file *file);
2292static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2293static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2294static void dev_seq_stop(struct seq_file *s, void *v);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002295static const struct file_operations dev_fops = {
2296 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 .open = sg_proc_open_dev,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002298 .read = seq_read,
2299 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 .release = seq_release,
2301};
James Morris88e9d342009-09-22 16:43:43 -07002302static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 .start = dev_seq_start,
2304 .next = dev_seq_next,
2305 .stop = dev_seq_stop,
2306 .show = sg_proc_seq_show_dev,
2307};
2308
2309static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2310static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002311static const struct file_operations devstrs_fops = {
2312 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 .open = sg_proc_open_devstrs,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002314 .read = seq_read,
2315 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 .release = seq_release,
2317};
James Morris88e9d342009-09-22 16:43:43 -07002318static const struct seq_operations devstrs_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 .start = dev_seq_start,
2320 .next = dev_seq_next,
2321 .stop = dev_seq_stop,
2322 .show = sg_proc_seq_show_devstrs,
2323};
2324
2325static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2326static int sg_proc_open_debug(struct inode *inode, struct file *file);
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002327static const struct file_operations debug_fops = {
2328 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 .open = sg_proc_open_debug,
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002330 .read = seq_read,
2331 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 .release = seq_release,
2333};
James Morris88e9d342009-09-22 16:43:43 -07002334static const struct seq_operations debug_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 .start = dev_seq_start,
2336 .next = dev_seq_next,
2337 .stop = dev_seq_stop,
2338 .show = sg_proc_seq_show_debug,
2339};
2340
2341
2342struct sg_proc_leaf {
2343 const char * name;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002344 const struct file_operations * fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345};
2346
Jörn Engel18b8ba62012-04-12 17:35:25 -04002347static const struct sg_proc_leaf sg_proc_leaf_arr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 {"allow_dio", &adio_fops},
2349 {"debug", &debug_fops},
2350 {"def_reserved_size", &dressz_fops},
2351 {"device_hdr", &devhdr_fops},
2352 {"devices", &dev_fops},
2353 {"device_strs", &devstrs_fops},
2354 {"version", &version_fops}
2355};
2356
2357static int
2358sg_proc_init(void)
2359{
Tobias Klauser6391a112006-06-08 22:23:48 -07002360 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Al Virod161a132011-07-24 03:36:29 -04002361 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Al Viro66600222005-09-28 22:32:57 +01002363 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (!sg_proc_sgp)
2365 return 1;
2366 for (k = 0; k < num_leaves; ++k) {
Jörn Engel18b8ba62012-04-12 17:35:25 -04002367 const struct sg_proc_leaf *leaf = &sg_proc_leaf_arr[k];
Al Virod161a132011-07-24 03:36:29 -04002368 umode_t mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002369 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 return 0;
2372}
2373
2374static void
2375sg_proc_cleanup(void)
2376{
2377 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002378 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 if (!sg_proc_sgp)
2381 return;
2382 for (k = 0; k < num_leaves; ++k)
2383 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2384 remove_proc_entry(sg_proc_sg_dirname, NULL);
2385}
2386
2387
2388static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2389{
2390 seq_printf(s, "%d\n", *((int *)s->private));
2391 return 0;
2392}
2393
2394static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2395{
2396 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2397}
2398
2399static ssize_t
2400sg_proc_write_adio(struct file *filp, const char __user *buffer,
2401 size_t count, loff_t *off)
2402{
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002403 int err;
2404 unsigned long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2407 return -EACCES;
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002408 err = kstrtoul_from_user(buffer, count, 0, &num);
2409 if (err)
2410 return err;
2411 sg_allow_dio = num ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return count;
2413}
2414
2415static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2416{
2417 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2418}
2419
2420static ssize_t
2421sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2422 size_t count, loff_t *off)
2423{
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002424 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 unsigned long k = ULONG_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2428 return -EACCES;
Stephen Boyd7e95fff2012-01-10 15:42:34 -08002429
2430 err = kstrtoul_from_user(buffer, count, 0, &k);
2431 if (err)
2432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2434 sg_big_buff = k;
2435 return count;
2436 }
2437 return -ERANGE;
2438}
2439
2440static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2441{
2442 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2443 sg_version_date);
2444 return 0;
2445}
2446
2447static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2448{
2449 return single_open(file, sg_proc_seq_show_version, NULL);
2450}
2451
2452static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2453{
2454 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2455 "online\n");
2456 return 0;
2457}
2458
2459static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2460{
2461 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2462}
2463
2464struct sg_proc_deviter {
2465 loff_t index;
2466 size_t max;
2467};
2468
2469static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2470{
2471 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2472
Jan Blunck729d70f2005-08-27 11:07:52 -07002473 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (! it)
2475 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 it->index = *pos;
2478 it->max = sg_last_dev();
2479 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002480 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2485{
Jan Blunck729d70f2005-08-27 11:07:52 -07002486 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 *pos = ++it->index;
2489 return (it->index < it->max) ? it : NULL;
2490}
2491
2492static void dev_seq_stop(struct seq_file *s, void *v)
2493{
Jan Blunck729d70f2005-08-27 11:07:52 -07002494 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497static int sg_proc_open_dev(struct inode *inode, struct file *file)
2498{
2499 return seq_open(file, &dev_seq_ops);
2500}
2501
2502static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2503{
2504 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2505 Sg_device *sdp;
2506 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002507 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Tony Battersbyc6517b792009-01-21 14:45:50 -05002509 read_lock_irqsave(&sg_index_lock, iflags);
2510 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2512 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2513 scsidp->host->host_no, scsidp->channel,
2514 scsidp->id, scsidp->lun, (int) scsidp->type,
2515 1,
2516 (int) scsidp->queue_depth,
2517 (int) scsidp->device_busy,
2518 (int) scsi_device_online(scsidp));
2519 else
2520 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 -05002521 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return 0;
2523}
2524
2525static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2526{
2527 return seq_open(file, &devstrs_seq_ops);
2528}
2529
2530static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2531{
2532 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2533 Sg_device *sdp;
2534 struct scsi_device *scsidp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002535 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Tony Battersbyc6517b792009-01-21 14:45:50 -05002537 read_lock_irqsave(&sg_index_lock, iflags);
2538 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2540 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2541 scsidp->vendor, scsidp->model, scsidp->rev);
2542 else
2543 seq_printf(s, "<no active device>\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002544 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return 0;
2546}
2547
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002548/* must be called while holding sg_index_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2550{
2551 int k, m, new_interface, blen, usg;
2552 Sg_request *srp;
2553 Sg_fd *fp;
2554 const sg_io_hdr_t *hp;
2555 const char * cp;
cb59e842005-04-02 13:51:23 -06002556 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002558 k = 0;
2559 list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
2560 k++;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002561 read_lock(&fp->rq_list_lock); /* irqs already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
FUJITA Tomonori3442f802009-02-16 13:26:53 +09002563 "(res)sgat=%d low_dma=%d\n", k,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 jiffies_to_msecs(fp->timeout),
2565 fp->reserve.bufflen,
2566 (int) fp->reserve.k_use_sg,
2567 (int) fp->low_dma);
Jörn Engelebaf4662012-04-12 17:33:39 -04002568 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 (int) fp->cmd_q, (int) fp->force_packid,
Jörn Engelebaf4662012-04-12 17:33:39 -04002570 (int) fp->keep_orphan);
Tony Battersbyc6517b792009-01-21 14:45:50 -05002571 for (m = 0, srp = fp->headrp;
2572 srp != NULL;
2573 ++m, srp = srp->nextrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 hp = &srp->header;
2575 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2576 if (srp->res_used) {
2577 if (new_interface &&
2578 (SG_FLAG_MMAP_IO & hp->flags))
2579 cp = " mmap>> ";
2580 else
2581 cp = " rb>> ";
2582 } else {
2583 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2584 cp = " dio>> ";
2585 else
2586 cp = " ";
2587 }
2588 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002589 blen = srp->data.bufflen;
2590 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 seq_printf(s, srp->done ?
2592 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002593 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 seq_printf(s, " id=%d blen=%d",
2595 srp->header.pack_id, blen);
2596 if (srp->done)
2597 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002598 else {
2599 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002601 (new_interface ? hp->timeout :
2602 jiffies_to_msecs(fp->timeout)),
2603 (ms > hp->duration ? ms - hp->duration : 0));
2604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2606 (int) srp->data.cmd_opcode);
2607 }
2608 if (0 == m)
2609 seq_printf(s, " No requests active\n");
Tony Battersbyc6517b792009-01-21 14:45:50 -05002610 read_unlock(&fp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612}
2613
2614static int sg_proc_open_debug(struct inode *inode, struct file *file)
2615{
2616 return seq_open(file, &debug_seq_ops);
2617}
2618
2619static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2620{
2621 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2622 Sg_device *sdp;
Tony Battersbyc6517b792009-01-21 14:45:50 -05002623 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002626 seq_printf(s, "max_active_device=%d(origin 1)\n",
2627 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2629 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002630
2631 read_lock_irqsave(&sg_index_lock, iflags);
2632 sdp = it ? sg_lookup_dev(it->index) : NULL;
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002633 if (sdp && !list_empty(&sdp->sfds)) {
2634 struct scsi_device *scsidp = sdp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002636 seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2637 if (sdp->detached)
2638 seq_printf(s, "detached pending close ");
2639 else
2640 seq_printf
2641 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2642 scsidp->host->host_no,
2643 scsidp->channel, scsidp->id,
2644 scsidp->lun,
2645 scsidp->host->hostt->emulated);
2646 seq_printf(s, " sg_tablesize=%d excl=%d\n",
James Bottomley98481ff2013-10-25 10:26:38 +01002647 sdp->sg_tablesize, get_exclude(sdp));
James Bottomleyc0d3b9c2013-10-25 10:21:57 +01002648 sg_proc_debug_helper(s, sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Tony Battersbyc6517b792009-01-21 14:45:50 -05002650 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return 0;
2652}
2653
2654#endif /* CONFIG_SCSI_PROC_FS */
2655
2656module_init(init_sg);
2657module_exit(exit_sg);