blob: b447527555a70c438a1c5c5d9c123bbed6067c7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * History:
3 * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
4 * to allow user process control of SCSI devices.
5 * Development Sponsored by Killy Corp. NY NY
6 *
7 * Original driver (sg.c):
8 * Copyright (C) 1992 Lawrence Foard
9 * Version 2 and 3 extensions to driver:
10 * Copyright (C) 1998 - 2005 Douglas Gilbert
11 *
12 * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 */
20
Douglas Gilbertb2155d02006-08-19 00:11:34 -040021static int sg_version_num = 30534; /* 2 digits for each component */
22#define SG_VERSION_STR "3.5.34"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
26 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
27 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
28 * (otherwise the macros compile to empty statements).
29 *
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32
33#include <linux/fs.h>
34#include <linux/kernel.h>
35#include <linux/sched.h>
36#include <linux/string.h>
37#include <linux/mm.h>
38#include <linux/errno.h>
39#include <linux/mtio.h>
40#include <linux/ioctl.h>
41#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>
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -060051#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "scsi.h"
db9dff32005-04-03 14:53:59 -050054#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <scsi/scsi_host.h>
56#include <scsi/scsi_driver.h>
57#include <scsi/scsi_ioctl.h>
58#include <scsi/sg.h>
59
60#include "scsi_logging.h"
61
62#ifdef CONFIG_SCSI_PROC_FS
63#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040064static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static int sg_proc_init(void);
67static void sg_proc_cleanup(void);
68#endif
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define SG_ALLOW_DIO_DEF 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define SG_MAX_DEVS 32768
73
74/*
75 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
76 * Then when using 32 bit integers x * m may overflow during the calculation.
77 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
78 * calculates the same, but prevents the overflow when both m and d
79 * are "small" numbers (like HZ and USER_HZ).
80 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
81 * in 32 bits.
82 */
83#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
84
85#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
86
87int sg_big_buff = SG_DEF_RESERVED_SIZE;
88/* N.B. This variable is readable and writeable via
89 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
90 of this size (or less if there is not enough memory) will be reserved
91 for use by this file descriptor. [Deprecated usage: this variable is also
92 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
93 the kernel (i.e. it is not a module).] */
94static int def_reserved_size = -1; /* picks up init parameter */
95static int sg_allow_dio = SG_ALLOW_DIO_DEF;
96
Douglas Gilbert6460e752006-09-20 18:20:49 -040097static int scatter_elem_sz = SG_SCATTER_SZ;
98static int scatter_elem_sz_prev = SG_SCATTER_SZ;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define SG_SECTOR_SZ 512
101#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
102
Tony Jonesee959b02008-02-22 00:13:36 +0100103static int sg_add(struct device *, struct class_interface *);
Tony Battersbyc6517b72009-01-21 14:45:50 -0500104static void sg_device_destroy(struct kref *kref);
Tony Jonesee959b02008-02-22 00:13:36 +0100105static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
James Bottomley7c07d612007-08-05 13:36:11 -0500107static DEFINE_IDR(sg_index_idr);
108static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 file descriptor list for device */
110
111static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100112 .add_dev = sg_add,
113 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
117 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900118 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200120 struct page **pages;
121 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
123 unsigned char cmd_opcode; /* first byte of command */
124} Sg_scatter_hold;
125
126struct sg_device; /* forward declarations */
127struct sg_fd;
128
129typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct sg_request *nextrp; /* NULL -> tail request (slist) */
131 struct sg_fd *parentfp; /* NULL -> not in use */
132 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
133 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600134 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
136 char orphan; /* 1 -> drop on sight, 0 -> normal */
137 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
138 volatile char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900139 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900140 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141} Sg_request;
142
143typedef struct sg_fd { /* holds the state of a file descriptor */
144 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
145 struct sg_device *parentdp; /* owning device */
146 wait_queue_head_t read_wait; /* queue read until command done */
147 rwlock_t rq_list_lock; /* protect access to list in req_arr */
148 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
149 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
150 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
151 unsigned save_scat_len; /* original length of trunc. scat. element */
152 Sg_request *headrp; /* head of request slist, NULL->empty */
153 struct fasync_struct *async_qp; /* used by asynchronous notification */
154 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
155 char low_dma; /* as in parent but possibly overridden to 1 */
156 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
157 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
158 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
159 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
160 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
161 char mmap_called; /* 0 -> mmap() never called on this fd */
Tony Battersbyc6517b72009-01-21 14:45:50 -0500162 struct kref f_ref;
163 struct execute_work ew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164} Sg_fd;
165
166typedef struct sg_device { /* holds the state of each scsi generic device */
167 struct scsi_device *device;
168 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
169 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500170 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 Sg_fd *headfp; /* first open fd belonging to this device */
172 volatile char detached; /* 0->attached, 1->detached pending removal */
173 volatile char exclude; /* opened for exclusive access */
174 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
175 struct gendisk *disk;
176 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
Tony Battersbyc6517b72009-01-21 14:45:50 -0500177 struct kref d_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178} Sg_device;
179
180static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600181/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900182static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900183static int sg_start_req(Sg_request *srp, unsigned char *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static void sg_finish_rem_req(Sg_request * srp);
185static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
186static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
187 int tablesize);
188static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
189 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200190static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
191 const char __user *buf, size_t count, int blocking,
192 int read_only, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
194 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196static void sg_remove_scat(Sg_scatter_hold * schp);
197static void sg_build_reserve(Sg_fd * sfp, int req_size);
198static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
Tony Battersbyc6517b72009-01-21 14:45:50 -0500201static void sg_remove_sfp(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
203static Sg_request *sg_add_request(Sg_fd * sfp);
204static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
205static int sg_res_in_use(Sg_fd * sfp);
Tony Battersbyc6517b72009-01-21 14:45:50 -0500206static Sg_device *sg_lookup_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static Sg_device *sg_get_dev(int dev);
Tony Battersbyc6517b72009-01-21 14:45:50 -0500208static void sg_put_dev(Sg_device *sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#ifdef CONFIG_SCSI_PROC_FS
210static int sg_last_dev(void);
211#endif
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define SZ_SG_HEADER sizeof(struct sg_header)
214#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
215#define SZ_SG_IOVEC sizeof(sg_iovec_t)
216#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
217
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900218static int sg_allow_access(struct file *filp, unsigned char *cmd)
219{
220 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
221 struct request_queue *q = sfp->parentdp->device->request_queue;
222
223 if (sfp->parentdp->device->type == TYPE_SCANNER)
224 return 0;
225
226 return blk_verify_command(&q->cmd_filter,
227 cmd, filp->f_mode & FMODE_WRITE);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int
231sg_open(struct inode *inode, struct file *filp)
232{
233 int dev = iminor(inode);
234 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600235 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 Sg_device *sdp;
237 Sg_fd *sfp;
238 int res;
239 int retval;
240
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600241 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 nonseekable_open(inode, filp);
243 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
244 sdp = sg_get_dev(dev);
Tony Battersbyc6517b72009-01-21 14:45:50 -0500245 if (IS_ERR(sdp)) {
246 retval = PTR_ERR(sdp);
247 sdp = NULL;
248 goto sg_put;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* This driver's module count bumped by fops_get in <linux/fs.h> */
252 /* Prevent the device driver from vanishing while we sleep */
253 retval = scsi_device_get(sdp->device);
Tony Battersbyc6517b72009-01-21 14:45:50 -0500254 if (retval)
255 goto sg_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (!((flags & O_NONBLOCK) ||
258 scsi_block_when_processing_errors(sdp->device))) {
259 retval = -ENXIO;
260 /* we are in error recovery for this device */
261 goto error_out;
262 }
263
264 if (flags & O_EXCL) {
265 if (O_RDONLY == (flags & O_ACCMODE)) {
266 retval = -EPERM; /* Can't lock it with read only access */
267 goto error_out;
268 }
269 if (sdp->headfp && (flags & O_NONBLOCK)) {
270 retval = -EBUSY;
271 goto error_out;
272 }
273 res = 0;
274 __wait_event_interruptible(sdp->o_excl_wait,
275 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
276 if (res) {
277 retval = res; /* -ERESTARTSYS because signal hit process */
278 goto error_out;
279 }
280 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
281 if (flags & O_NONBLOCK) {
282 retval = -EBUSY;
283 goto error_out;
284 }
285 res = 0;
286 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
287 res);
288 if (res) {
289 retval = res; /* -ERESTARTSYS because signal hit process */
290 goto error_out;
291 }
292 }
293 if (sdp->detached) {
294 retval = -ENODEV;
295 goto error_out;
296 }
297 if (!sdp->headfp) { /* no existing opens on this device */
298 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600299 q = sdp->device->request_queue;
300 sdp->sg_tablesize = min(q->max_hw_segments,
301 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 if ((sfp = sg_add_sfp(sdp, dev)))
304 filp->private_data = sfp;
305 else {
Tony Battersbyc6517b72009-01-21 14:45:50 -0500306 if (flags & O_EXCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 sdp->exclude = 0; /* undo if error */
Tony Battersbyc6517b72009-01-21 14:45:50 -0500308 wake_up_interruptible(&sdp->o_excl_wait);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 retval = -ENOMEM;
311 goto error_out;
312 }
Tony Battersbyc6517b72009-01-21 14:45:50 -0500313 retval = 0;
314error_out:
315 if (retval)
316 scsi_device_put(sdp->device);
317sg_put:
318 if (sdp)
319 sg_put_dev(sdp);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600320 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return retval;
322}
323
324/* Following function was formerly called 'sg_close' */
325static int
326sg_release(struct inode *inode, struct file *filp)
327{
328 Sg_device *sdp;
329 Sg_fd *sfp;
330
331 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
332 return -ENXIO;
333 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
Tony Battersbyc6517b72009-01-21 14:45:50 -0500334
335 sfp->closed = 1;
336
337 sdp->exclude = 0;
338 wake_up_interruptible(&sdp->o_excl_wait);
339
340 kref_put(&sfp->f_ref, sg_remove_sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342}
343
344static ssize_t
345sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 Sg_device *sdp;
348 Sg_fd *sfp;
349 Sg_request *srp;
350 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600352 struct sg_header *old_hdr = NULL;
353 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
356 return -ENXIO;
357 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
358 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!access_ok(VERIFY_WRITE, buf, count))
361 return -EFAULT;
362 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600363 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
364 if (!old_hdr)
365 return -ENOMEM;
366 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
367 retval = -EFAULT;
368 goto free_old_hdr;
369 }
370 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600372 sg_io_hdr_t *new_hdr;
373 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
374 if (!new_hdr) {
375 retval = -ENOMEM;
376 goto free_old_hdr;
377 }
378 retval =__copy_from_user
379 (new_hdr, buf, SZ_SG_IO_HDR);
380 req_pack_id = new_hdr->pack_id;
381 kfree(new_hdr);
382 if (retval) {
383 retval = -EFAULT;
384 goto free_old_hdr;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 } else
cb59e842005-04-02 13:51:23 -0600388 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 srp = sg_get_rq_mark(sfp, req_pack_id);
391 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600392 if (sdp->detached) {
393 retval = -ENODEV;
394 goto free_old_hdr;
395 }
396 if (filp->f_flags & O_NONBLOCK) {
397 retval = -EAGAIN;
398 goto free_old_hdr;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 while (1) {
cb59e842005-04-02 13:51:23 -0600401 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600403 (sdp->detached ||
404 (srp = sg_get_rq_mark(sfp, req_pack_id))),
405 retval);
406 if (sdp->detached) {
407 retval = -ENODEV;
408 goto free_old_hdr;
409 }
410 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
cb59e842005-04-02 13:51:23 -0600412
413 /* -ERESTARTSYS as signal hit process */
414 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 }
cb59e842005-04-02 13:51:23 -0600417 if (srp->header.interface_id != '\0') {
418 retval = sg_new_read(sfp, buf, count, srp);
419 goto free_old_hdr;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600423 if (old_hdr == NULL) {
424 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
425 if (! old_hdr) {
426 retval = -ENOMEM;
427 goto free_old_hdr;
428 }
429 }
430 memset(old_hdr, 0, SZ_SG_HEADER);
431 old_hdr->reply_len = (int) hp->timeout;
432 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
433 old_hdr->pack_id = hp->pack_id;
434 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600436 old_hdr->target_status = hp->masked_status;
437 old_hdr->host_status = hp->host_status;
438 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if ((CHECK_CONDITION & hp->masked_status) ||
440 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600441 memcpy(old_hdr->sense_buffer, srp->sense_b,
442 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 switch (hp->host_status) {
444 /* This setup of 'result' is for backward compatibility and is best
445 ignored by the user who should use target, host + driver status */
446 case DID_OK:
447 case DID_PASSTHROUGH:
448 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600449 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 case DID_NO_CONNECT:
452 case DID_BUS_BUSY:
453 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600454 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
456 case DID_BAD_TARGET:
457 case DID_ABORT:
458 case DID_PARITY:
459 case DID_RESET:
460 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600461 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
463 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600464 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 hp->masked_status == GOOD) ? 0 : EIO;
466 break;
467 default:
cb59e842005-04-02 13:51:23 -0600468 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 }
471
472 /* Now copy the result back to the user buffer. */
473 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600474 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
475 retval = -EFAULT;
476 goto free_old_hdr;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600479 if (count > old_hdr->reply_len)
480 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600482 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
483 retval = -EFAULT;
484 goto free_old_hdr;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 } else
cb59e842005-04-02 13:51:23 -0600488 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600490 retval = count;
491free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800492 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600493 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496static ssize_t
497sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
498{
499 sg_io_hdr_t *hp = &srp->header;
500 int err = 0;
501 int len;
502
503 if (count < SZ_SG_IO_HDR) {
504 err = -EINVAL;
505 goto err_out;
506 }
507 hp->sb_len_wr = 0;
508 if ((hp->mx_sb_len > 0) && hp->sbp) {
509 if ((CHECK_CONDITION & hp->masked_status) ||
510 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600511 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
513 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
514 len = (len > sb_len) ? sb_len : len;
515 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
516 err = -EFAULT;
517 goto err_out;
518 }
519 hp->sb_len_wr = len;
520 }
521 }
522 if (hp->masked_status || hp->host_status || hp->driver_status)
523 hp->info |= SG_INFO_CHECK;
524 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
525 err = -EFAULT;
526 goto err_out;
527 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900528err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 sg_finish_rem_req(srp);
530 return (0 == err) ? count : err;
531}
532
533static ssize_t
534sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
535{
536 int mxsize, cmd_size, k;
537 int input_size, blocking;
538 unsigned char opcode;
539 Sg_device *sdp;
540 Sg_fd *sfp;
541 Sg_request *srp;
542 struct sg_header old_hdr;
543 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600544 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
547 return -ENXIO;
548 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
549 sdp->disk->disk_name, (int) count));
550 if (sdp->detached)
551 return -ENODEV;
552 if (!((filp->f_flags & O_NONBLOCK) ||
553 scsi_block_when_processing_errors(sdp->device)))
554 return -ENXIO;
555
556 if (!access_ok(VERIFY_READ, buf, count))
557 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
558 if (count < SZ_SG_HEADER)
559 return -EIO;
560 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
561 return -EFAULT;
562 blocking = !(filp->f_flags & O_NONBLOCK);
563 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200564 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (count < (SZ_SG_HEADER + 6))
566 return -EIO; /* The minimum scsi command length is 6 bytes. */
567
568 if (!(srp = sg_add_request(sfp))) {
569 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
570 return -EDOM;
571 }
572 buf += SZ_SG_HEADER;
573 __get_user(opcode, buf);
574 if (sfp->next_cmd_len > 0) {
575 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
576 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
577 sfp->next_cmd_len = 0;
578 sg_remove_request(sfp, srp);
579 return -EIO;
580 }
581 cmd_size = sfp->next_cmd_len;
582 sfp->next_cmd_len = 0; /* reset so only this write() effected */
583 } else {
584 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
585 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
586 cmd_size = 12;
587 }
588 SCSI_LOG_TIMEOUT(4, printk(
589 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
590/* Determine buffer size. */
591 input_size = count - cmd_size;
592 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
593 mxsize -= SZ_SG_HEADER;
594 input_size -= SZ_SG_HEADER;
595 if (input_size < 0) {
596 sg_remove_request(sfp, srp);
597 return -EIO; /* User did not pass enough bytes for this command. */
598 }
599 hp = &srp->header;
600 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
601 hp->cmd_len = (unsigned char) cmd_size;
602 hp->iovec_count = 0;
603 hp->mx_sb_len = 0;
604 if (input_size > 0)
605 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
606 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
607 else
608 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
609 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900610 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
611 hp->dxferp = (char __user *)buf + cmd_size;
612 else
613 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 hp->sbp = NULL;
615 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
616 hp->flags = input_size; /* structure abuse ... */
617 hp->pack_id = old_hdr.pack_id;
618 hp->usr_ptr = NULL;
619 if (__copy_from_user(cmnd, buf, cmd_size))
620 return -EFAULT;
621 /*
622 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
623 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
624 * is a non-zero input_size, so emit a warning.
625 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100626 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
627 static char cmd[TASK_COMM_LEN];
628 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 printk(KERN_WARNING
630 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
631 "guessing data in;\n" KERN_WARNING " "
632 "program %s not setting count and/or reply_len properly\n",
633 old_hdr.reply_len - (int)SZ_SG_HEADER,
634 input_size, (unsigned int) cmnd[0],
635 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100636 strcpy(cmd, current->comm);
637 }
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
640 return (k < 0) ? k : count;
641}
642
643static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200644sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
645 size_t count, int blocking, int read_only,
646 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int k;
649 Sg_request *srp;
650 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600651 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int timeout;
653 unsigned long ul_timeout;
654
655 if (count < SZ_SG_IO_HDR)
656 return -EINVAL;
657 if (!access_ok(VERIFY_READ, buf, count))
658 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
659
660 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
661 if (!(srp = sg_add_request(sfp))) {
662 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
663 return -EDOM;
664 }
665 hp = &srp->header;
666 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
667 sg_remove_request(sfp, srp);
668 return -EFAULT;
669 }
670 if (hp->interface_id != 'S') {
671 sg_remove_request(sfp, srp);
672 return -ENOSYS;
673 }
674 if (hp->flags & SG_FLAG_MMAP_IO) {
675 if (hp->dxfer_len > sfp->reserve.bufflen) {
676 sg_remove_request(sfp, srp);
677 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
678 }
679 if (hp->flags & SG_FLAG_DIRECT_IO) {
680 sg_remove_request(sfp, srp);
681 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
682 }
683 if (sg_res_in_use(sfp)) {
684 sg_remove_request(sfp, srp);
685 return -EBUSY; /* reserve buffer already being used */
686 }
687 }
688 ul_timeout = msecs_to_jiffies(srp->header.timeout);
689 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
690 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
691 sg_remove_request(sfp, srp);
692 return -EMSGSIZE;
693 }
694 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
695 sg_remove_request(sfp, srp);
696 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
697 }
698 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
699 sg_remove_request(sfp, srp);
700 return -EFAULT;
701 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900702 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 sg_remove_request(sfp, srp);
704 return -EPERM;
705 }
706 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
707 if (k < 0)
708 return k;
709 if (o_srp)
710 *o_srp = srp;
711 return count;
712}
713
714static int
715sg_common_write(Sg_fd * sfp, Sg_request * srp,
716 unsigned char *cmnd, int timeout, int blocking)
717{
Mike Christied6b10342005-11-08 04:06:41 -0600718 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 Sg_device *sdp = sfp->parentdp;
720 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
723 hp->status = 0;
724 hp->masked_status = 0;
725 hp->msg_status = 0;
726 hp->info = 0;
727 hp->host_status = 0;
728 hp->driver_status = 0;
729 hp->resid = 0;
730 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
731 (int) cmnd[0], (int) hp->cmd_len));
732
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900733 k = sg_start_req(srp, cmnd);
734 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400735 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 sg_finish_rem_req(srp);
737 return k; /* probably out of space --> ENOMEM */
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (sdp->detached) {
740 sg_finish_rem_req(srp);
741 return -ENODEV;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 switch (hp->dxfer_direction) {
745 case SG_DXFER_TO_FROM_DEV:
746 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600747 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600750 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600753 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 default:
Mike Christied6b10342005-11-08 04:06:41 -0600756 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 }
cb59e842005-04-02 13:51:23 -0600759 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200760
761 srp->rq->timeout = timeout;
Tony Battersbyc6517b72009-01-21 14:45:50 -0500762 kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200763 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
764 srp->rq, 1, sg_rq_end_io);
765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static int
769sg_srp_done(Sg_request *srp, Sg_fd *sfp)
770{
771 unsigned long iflags;
772 int done;
773
774 read_lock_irqsave(&sfp->rq_list_lock, iflags);
775 done = srp->done;
776 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
777 return done;
778}
779
780static int
781sg_ioctl(struct inode *inode, struct file *filp,
782 unsigned int cmd_in, unsigned long arg)
783{
784 void __user *p = (void __user *)arg;
785 int __user *ip = p;
786 int result, val, read_only;
787 Sg_device *sdp;
788 Sg_fd *sfp;
789 Sg_request *srp;
790 unsigned long iflags;
791
792 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
793 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
796 sdp->disk->disk_name, (int) cmd_in));
797 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
798
799 switch (cmd_in) {
800 case SG_IO:
801 {
802 int blocking = 1; /* ignore O_NONBLOCK flag */
803
804 if (sdp->detached)
805 return -ENODEV;
806 if (!scsi_block_when_processing_errors(sdp->device))
807 return -ENXIO;
808 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
809 return -EFAULT;
810 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200811 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 blocking, read_only, &srp);
813 if (result < 0)
814 return result;
815 srp->sg_io_owned = 1;
816 while (1) {
817 result = 0; /* following macro to beat race condition */
818 __wait_event_interruptible(sfp->read_wait,
819 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
820 result);
821 if (sdp->detached)
822 return -ENODEV;
823 if (sfp->closed)
824 return 0; /* request packet dropped already */
825 if (0 == result)
826 break;
827 srp->orphan = 1;
828 return result; /* -ERESTARTSYS because signal hit process */
829 }
830 write_lock_irqsave(&sfp->rq_list_lock, iflags);
831 srp->done = 2;
832 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
833 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
834 return (result < 0) ? result : 0;
835 }
836 case SG_SET_TIMEOUT:
837 result = get_user(val, ip);
838 if (result)
839 return result;
840 if (val < 0)
841 return -EIO;
842 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
843 val = MULDIV (INT_MAX, USER_HZ, HZ);
844 sfp->timeout_user = val;
845 sfp->timeout = MULDIV (val, HZ, USER_HZ);
846
847 return 0;
848 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
849 /* strange ..., for backward compatibility */
850 return sfp->timeout_user;
851 case SG_SET_FORCE_LOW_DMA:
852 result = get_user(val, ip);
853 if (result)
854 return result;
855 if (val) {
856 sfp->low_dma = 1;
857 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
858 val = (int) sfp->reserve.bufflen;
859 sg_remove_scat(&sfp->reserve);
860 sg_build_reserve(sfp, val);
861 }
862 } else {
863 if (sdp->detached)
864 return -ENODEV;
865 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
866 }
867 return 0;
868 case SG_GET_LOW_DMA:
869 return put_user((int) sfp->low_dma, ip);
870 case SG_GET_SCSI_ID:
871 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
872 return -EFAULT;
873 else {
874 sg_scsi_id_t __user *sg_idp = p;
875
876 if (sdp->detached)
877 return -ENODEV;
878 __put_user((int) sdp->device->host->host_no,
879 &sg_idp->host_no);
880 __put_user((int) sdp->device->channel,
881 &sg_idp->channel);
882 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
883 __put_user((int) sdp->device->lun, &sg_idp->lun);
884 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
885 __put_user((short) sdp->device->host->cmd_per_lun,
886 &sg_idp->h_cmd_per_lun);
887 __put_user((short) sdp->device->queue_depth,
888 &sg_idp->d_queue_depth);
889 __put_user(0, &sg_idp->unused[0]);
890 __put_user(0, &sg_idp->unused[1]);
891 return 0;
892 }
893 case SG_SET_FORCE_PACK_ID:
894 result = get_user(val, ip);
895 if (result)
896 return result;
897 sfp->force_packid = val ? 1 : 0;
898 return 0;
899 case SG_GET_PACK_ID:
900 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
901 return -EFAULT;
902 read_lock_irqsave(&sfp->rq_list_lock, iflags);
903 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
904 if ((1 == srp->done) && (!srp->sg_io_owned)) {
905 read_unlock_irqrestore(&sfp->rq_list_lock,
906 iflags);
907 __put_user(srp->header.pack_id, ip);
908 return 0;
909 }
910 }
911 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
912 __put_user(-1, ip);
913 return 0;
914 case SG_GET_NUM_WAITING:
915 read_lock_irqsave(&sfp->rq_list_lock, iflags);
916 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
917 if ((1 == srp->done) && (!srp->sg_io_owned))
918 ++val;
919 }
920 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
921 return put_user(val, ip);
922 case SG_GET_SG_TABLESIZE:
923 return put_user(sdp->sg_tablesize, ip);
924 case SG_SET_RESERVED_SIZE:
925 result = get_user(val, ip);
926 if (result)
927 return result;
928 if (val < 0)
929 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500930 val = min_t(int, val,
931 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (val != sfp->reserve.bufflen) {
933 if (sg_res_in_use(sfp) || sfp->mmap_called)
934 return -EBUSY;
935 sg_remove_scat(&sfp->reserve);
936 sg_build_reserve(sfp, val);
937 }
938 return 0;
939 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500940 val = min_t(int, sfp->reserve.bufflen,
941 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return put_user(val, ip);
943 case SG_SET_COMMAND_Q:
944 result = get_user(val, ip);
945 if (result)
946 return result;
947 sfp->cmd_q = val ? 1 : 0;
948 return 0;
949 case SG_GET_COMMAND_Q:
950 return put_user((int) sfp->cmd_q, ip);
951 case SG_SET_KEEP_ORPHAN:
952 result = get_user(val, ip);
953 if (result)
954 return result;
955 sfp->keep_orphan = val;
956 return 0;
957 case SG_GET_KEEP_ORPHAN:
958 return put_user((int) sfp->keep_orphan, ip);
959 case SG_NEXT_CMD_LEN:
960 result = get_user(val, ip);
961 if (result)
962 return result;
963 sfp->next_cmd_len = (val > 0) ? val : 0;
964 return 0;
965 case SG_GET_VERSION_NUM:
966 return put_user(sg_version_num, ip);
967 case SG_GET_ACCESS_COUNT:
968 /* faked - we don't have a real access count anymore */
969 val = (sdp->device ? 1 : 0);
970 return put_user(val, ip);
971 case SG_GET_REQUEST_TABLE:
972 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
973 return -EFAULT;
974 else {
cb59e842005-04-02 13:51:23 -0600975 sg_req_info_t *rinfo;
976 unsigned int ms;
977
978 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
979 GFP_KERNEL);
980 if (!rinfo)
981 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 read_lock_irqsave(&sfp->rq_list_lock, iflags);
983 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
984 ++val, srp = srp ? srp->nextrp : srp) {
985 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
986 if (srp) {
987 rinfo[val].req_state = srp->done + 1;
988 rinfo[val].problem =
989 srp->header.masked_status &
990 srp->header.host_status &
991 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600992 if (srp->done)
993 rinfo[val].duration =
994 srp->header.duration;
995 else {
996 ms = jiffies_to_msecs(jiffies);
997 rinfo[val].duration =
998 (ms > srp->header.duration) ?
999 (ms - srp->header.duration) : 0;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001002 rinfo[val].sg_io_owned =
1003 srp->sg_io_owned;
1004 rinfo[val].pack_id =
1005 srp->header.pack_id;
1006 rinfo[val].usr_ptr =
1007 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 }
1010 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001011 result = __copy_to_user(p, rinfo,
1012 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1013 result = result ? -EFAULT : 0;
1014 kfree(rinfo);
1015 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 case SG_EMULATED_HOST:
1018 if (sdp->detached)
1019 return -ENODEV;
1020 return put_user(sdp->device->host->hostt->emulated, ip);
1021 case SG_SCSI_RESET:
1022 if (sdp->detached)
1023 return -ENODEV;
1024 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001025 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EBUSY;
1027 } else if (!scsi_block_when_processing_errors(sdp->device))
1028 return -EBUSY;
1029 result = get_user(val, ip);
1030 if (result)
1031 return result;
1032 if (SG_SCSI_RESET_NOTHING == val)
1033 return 0;
1034 switch (val) {
1035 case SG_SCSI_RESET_DEVICE:
1036 val = SCSI_TRY_RESET_DEVICE;
1037 break;
Brian King39120e12008-07-01 13:03:19 -05001038 case SG_SCSI_RESET_TARGET:
1039 val = SCSI_TRY_RESET_TARGET;
1040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 case SG_SCSI_RESET_BUS:
1042 val = SCSI_TRY_RESET_BUS;
1043 break;
1044 case SG_SCSI_RESET_HOST:
1045 val = SCSI_TRY_RESET_HOST;
1046 break;
1047 default:
1048 return -EINVAL;
1049 }
1050 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1051 return -EACCES;
1052 return (scsi_reset_provider(sdp->device, val) ==
1053 SUCCESS) ? 0 : -EIO;
1054 case SCSI_IOCTL_SEND_COMMAND:
1055 if (sdp->detached)
1056 return -ENODEV;
1057 if (read_only) {
1058 unsigned char opcode = WRITE_6;
1059 Scsi_Ioctl_Command __user *siocp = p;
1060
1061 if (copy_from_user(&opcode, siocp->data, 1))
1062 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001063 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -EPERM;
1065 }
Al Viroe915e872008-09-02 17:16:41 -04001066 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 case SG_SET_DEBUG:
1068 result = get_user(val, ip);
1069 if (result)
1070 return result;
1071 sdp->sgdebug = (char) val;
1072 return 0;
1073 case SCSI_IOCTL_GET_IDLUN:
1074 case SCSI_IOCTL_GET_BUS_NUMBER:
1075 case SCSI_IOCTL_PROBE_HOST:
1076 case SG_GET_TRANSFORM:
1077 if (sdp->detached)
1078 return -ENODEV;
1079 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001080 case BLKSECTGET:
1081 return put_user(sdp->device->request_queue->max_sectors * 512,
1082 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001083 case BLKTRACESETUP:
1084 return blk_trace_setup(sdp->device->request_queue,
1085 sdp->disk->disk_name,
Martin Peschke76e3a192009-01-30 15:46:23 +01001086 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
Christof Schmitt6da127a2008-01-11 10:09:43 +01001087 (char *)arg);
1088 case BLKTRACESTART:
1089 return blk_trace_startstop(sdp->device->request_queue, 1);
1090 case BLKTRACESTOP:
1091 return blk_trace_startstop(sdp->device->request_queue, 0);
1092 case BLKTRACETEARDOWN:
1093 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 default:
1095 if (read_only)
1096 return -EPERM; /* don't know so take safe approach */
1097 return scsi_ioctl(sdp->device, cmd_in, p);
1098 }
1099}
1100
1101#ifdef CONFIG_COMPAT
1102static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1103{
1104 Sg_device *sdp;
1105 Sg_fd *sfp;
1106 struct scsi_device *sdev;
1107
1108 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1109 return -ENXIO;
1110
1111 sdev = sdp->device;
1112 if (sdev->host->hostt->compat_ioctl) {
1113 int ret;
1114
1115 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1116
1117 return ret;
1118 }
1119
1120 return -ENOIOCTLCMD;
1121}
1122#endif
1123
1124static unsigned int
1125sg_poll(struct file *filp, poll_table * wait)
1126{
1127 unsigned int res = 0;
1128 Sg_device *sdp;
1129 Sg_fd *sfp;
1130 Sg_request *srp;
1131 int count = 0;
1132 unsigned long iflags;
1133
1134 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1135 || sfp->closed)
1136 return POLLERR;
1137 poll_wait(filp, &sfp->read_wait, wait);
1138 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1139 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1140 /* if any read waiting, flag it */
1141 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1142 res = POLLIN | POLLRDNORM;
1143 ++count;
1144 }
1145 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1146
1147 if (sdp->detached)
1148 res |= POLLHUP;
1149 else if (!sfp->cmd_q) {
1150 if (0 == count)
1151 res |= POLLOUT | POLLWRNORM;
1152 } else if (count < SG_MAX_QUEUE)
1153 res |= POLLOUT | POLLWRNORM;
1154 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1155 sdp->disk->disk_name, (int) res));
1156 return res;
1157}
1158
1159static int
1160sg_fasync(int fd, struct file *filp, int mode)
1161{
1162 int retval;
1163 Sg_device *sdp;
1164 Sg_fd *sfp;
1165
1166 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1167 return -ENXIO;
1168 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1169 sdp->disk->disk_name, mode));
1170
1171 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1172 return (retval < 0) ? retval : 0;
1173}
1174
Nick Piggina13ff0b2008-02-07 18:46:06 -08001175static int
1176sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001179 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001181 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001184 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001186 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001188 return VM_FAULT_SIGBUS;
1189 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001191 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001192 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1193 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001194 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001195 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001196 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001197 struct page *page = nth_page(rsv_schp->pages[k],
1198 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001199 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001200 vmf->page = page;
1201 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Mike Christied6b10342005-11-08 04:06:41 -06001203 sa += len;
1204 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Mike Christied6b10342005-11-08 04:06:41 -06001206
Nick Piggina13ff0b2008-02-07 18:46:06 -08001207 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001211 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212};
1213
1214static int
1215sg_mmap(struct file *filp, struct vm_area_struct *vma)
1216{
1217 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001218 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001220 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1223 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001224 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1226 (void *) vma->vm_start, (int) req_sz));
1227 if (vma->vm_pgoff)
1228 return -EINVAL; /* want no offset */
1229 rsv_schp = &sfp->reserve;
1230 if (req_sz > rsv_schp->bufflen)
1231 return -ENOMEM; /* cannot map more than reserved buffer */
1232
Mike Christied6b10342005-11-08 04:06:41 -06001233 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001234 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1235 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001236 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001237 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001238 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Mike Christied6b10342005-11-08 04:06:41 -06001240
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001241 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001242 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 vma->vm_private_data = sfp;
1244 vma->vm_ops = &sg_mmap_vm_ops;
1245 return 0;
1246}
1247
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001248/*
1249 * This function is a "bottom half" handler that is called by the mid
1250 * level when a command is completed (or has failed).
1251 */
1252static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001254 struct sg_request *srp = rq->end_io_data;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001255 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001258 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001259 char *sense;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001260 int result, resid, done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Tony Battersbyc6517b72009-01-21 14:45:50 -05001262 if (WARN_ON(srp->done != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sfp = srp->parentfp;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001266 if (WARN_ON(sfp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001268
1269 sdp = sfp->parentdp;
1270 if (unlikely(sdp->detached))
1271 printk(KERN_INFO "sg_rq_end_io: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001273 sense = rq->sense;
1274 result = rq->errors;
1275 resid = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001278 sdp->disk->disk_name, srp->header.pack_id, result));
1279 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001280 ms = jiffies_to_msecs(jiffies);
1281 srp->header.duration = (ms > srp->header.duration) ?
1282 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001283 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct scsi_sense_hdr sshdr;
1285
Mike Christied6b10342005-11-08 04:06:41 -06001286 srp->header.status = 0xff & result;
1287 srp->header.masked_status = status_byte(result);
1288 srp->header.msg_status = msg_byte(result);
1289 srp->header.host_status = host_byte(result);
1290 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if ((sdp->sgdebug > 0) &&
1292 ((CHECK_CONDITION == srp->header.masked_status) ||
1293 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001294 __scsi_print_sense("sg_cmd_done", sense,
1295 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001298 if (driver_byte(result) != 0
1299 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 && !scsi_sense_is_deferred(&sshdr)
1301 && sshdr.sense_key == UNIT_ATTENTION
1302 && sdp->device->removable) {
1303 /* Detected possible disc change. Set the bit - this */
1304 /* may be used if there are filesystems using this device */
1305 sdp->device->changed = 1;
1306 }
1307 }
1308 /* Rely on write phase to clean out srp status values, so no "else" */
1309
Tony Battersbyc6517b72009-01-21 14:45:50 -05001310 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1311 if (unlikely(srp->orphan)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (sfp->keep_orphan)
1313 srp->sg_io_owned = 0;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001314 else
1315 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Tony Battersbyc6517b72009-01-21 14:45:50 -05001317 srp->done = done;
1318 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1319
1320 if (likely(done)) {
1321 /* Now wake up any sg_read() that is waiting for this
1322 * packet.
1323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 wake_up_interruptible(&sfp->read_wait);
Tony Battersbyc6517b72009-01-21 14:45:50 -05001325 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1326 } else
1327 sg_finish_rem_req(srp); /* call with srp->done == 0 */
1328
1329 kref_put(&sfp->f_ref, sg_remove_sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332static struct file_operations sg_fops = {
1333 .owner = THIS_MODULE,
1334 .read = sg_read,
1335 .write = sg_write,
1336 .poll = sg_poll,
1337 .ioctl = sg_ioctl,
1338#ifdef CONFIG_COMPAT
1339 .compat_ioctl = sg_compat_ioctl,
1340#endif
1341 .open = sg_open,
1342 .mmap = sg_mmap,
1343 .release = sg_release,
1344 .fasync = sg_fasync,
1345};
1346
gregkh@suse.ded2538782005-03-23 09:55:22 -08001347static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349static int sg_sysfs_valid = 0;
1350
James Bottomley7c07d612007-08-05 13:36:11 -05001351static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Mike Christied6b10342005-11-08 04:06:41 -06001353 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 Sg_device *sdp;
1355 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001356 int error;
1357 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Jes Sorensen24669f752006-01-16 10:31:18 -05001359 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!sdp) {
1361 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001362 return ERR_PTR(-ENOMEM);
1363 }
Tony Battersbyc6517b72009-01-21 14:45:50 -05001364
James Bottomley7c07d612007-08-05 13:36:11 -05001365 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1366 printk(KERN_WARNING "idr expansion Sg_device failure\n");
Tony Battersbyc6517b72009-01-21 14:45:50 -05001367 error = -ENOMEM;
James Bottomley7c07d612007-08-05 13:36:11 -05001368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
James Bottomley7c07d612007-08-05 13:36:11 -05001371 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Tony Battersbyc6517b72009-01-21 14:45:50 -05001373 error = idr_get_new(&sg_index_idr, sdp, &k);
James Bottomley7c07d612007-08-05 13:36:11 -05001374 if (error) {
Tony Battersbyc6517b72009-01-21 14:45:50 -05001375 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley7c07d612007-08-05 13:36:11 -05001376 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1377 error);
1378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (unlikely(k >= SG_MAX_DEVS))
1382 goto overflow;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1385 sprintf(disk->disk_name, "sg%d", k);
1386 disk->first_minor = k;
1387 sdp->disk = disk;
1388 sdp->device = scsidp;
1389 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001390 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001391 sdp->index = k;
Tony Battersbyc6517b72009-01-21 14:45:50 -05001392 kref_init(&sdp->d_ref);
1393
1394 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
James Bottomley7c07d612007-08-05 13:36:11 -05001396 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001398 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001400 return ERR_PTR(error);
1401 }
1402 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 overflow:
Tony Battersbyc6517b72009-01-21 14:45:50 -05001405 idr_remove(&sg_index_idr, k);
1406 write_unlock_irqrestore(&sg_index_lock, iflags);
James Bottomley9ccfc752005-10-02 11:45:08 -05001407 sdev_printk(KERN_WARNING, scsidp,
1408 "Unable to attach sg device type=%d, minor "
1409 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 error = -ENODEV;
1411 goto out;
1412}
1413
1414static int
Tony Jonesee959b02008-02-22 00:13:36 +01001415sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Tony Jonesee959b02008-02-22 00:13:36 +01001417 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct gendisk *disk;
1419 Sg_device *sdp = NULL;
1420 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001421 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001422 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 disk = alloc_disk(1);
1425 if (!disk) {
1426 printk(KERN_WARNING "alloc_disk failed\n");
1427 return -ENOMEM;
1428 }
1429 disk->major = SCSI_GENERIC_MAJOR;
1430
1431 error = -ENOMEM;
1432 cdev = cdev_alloc();
1433 if (!cdev) {
1434 printk(KERN_WARNING "cdev_alloc failed\n");
1435 goto out;
1436 }
1437 cdev->owner = THIS_MODULE;
1438 cdev->ops = &sg_fops;
1439
James Bottomley7c07d612007-08-05 13:36:11 -05001440 sdp = sg_alloc(disk, scsidp);
1441 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001443 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto out;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
James Bottomley7c07d612007-08-05 13:36:11 -05001447 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001448 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001449 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sdp->cdev = cdev;
1452 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001453 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Greg Kroah-Hartmand73a1a62008-07-21 20:03:34 -07001455 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1456 MKDEV(SCSI_GENERIC_MAJOR,
1457 sdp->index),
1458 sdp, "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001459 if (IS_ERR(sg_class_member)) {
1460 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001461 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001462 error = PTR_ERR(sg_class_member);
1463 goto cdev_add_err;
1464 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001465 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 &sg_class_member->kobj, "generic");
1467 if (error)
1468 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001469 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001471 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
James Bottomley9ccfc752005-10-02 11:45:08 -05001473 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001474 "Attached scsi generic sg%d type %d\n", sdp->index,
1475 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Tony Jonesee959b02008-02-22 00:13:36 +01001477 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001481cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001482 write_lock_irqsave(&sg_index_lock, iflags);
1483 idr_remove(&sg_index_idr, sdp->index);
1484 write_unlock_irqrestore(&sg_index_lock, iflags);
1485 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487out:
1488 put_disk(disk);
1489 if (cdev)
1490 cdev_del(cdev);
1491 return error;
1492}
1493
Tony Battersbyc6517b72009-01-21 14:45:50 -05001494static void sg_device_destroy(struct kref *kref)
1495{
1496 struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1497 unsigned long flags;
1498
1499 /* CAUTION! Note that the device can still be found via idr_find()
1500 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1501 * any other cleanup.
1502 */
1503
1504 write_lock_irqsave(&sg_index_lock, flags);
1505 idr_remove(&sg_index_idr, sdp->index);
1506 write_unlock_irqrestore(&sg_index_lock, flags);
1507
1508 SCSI_LOG_TIMEOUT(3,
1509 printk("sg_device_destroy: %s\n",
1510 sdp->disk->disk_name));
1511
1512 put_disk(sdp->disk);
1513 kfree(sdp);
1514}
1515
1516static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Tony Jonesee959b02008-02-22 00:13:36 +01001518 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1519 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 unsigned long iflags;
1521 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Tony Battersbyc6517b72009-01-21 14:45:50 -05001523 if (!sdp || sdp->detached)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Tony Battersbyc6517b72009-01-21 14:45:50 -05001526 SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
1527
1528 /* Need a write lock to set sdp->detached. */
James Bottomley7c07d612007-08-05 13:36:11 -05001529 write_lock_irqsave(&sg_index_lock, iflags);
Tony Battersbyc6517b72009-01-21 14:45:50 -05001530 sdp->detached = 1;
1531 for (sfp = sdp->headfp; sfp; sfp = sfp->nextfp) {
1532 wake_up_interruptible(&sfp->read_wait);
1533 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
James Bottomley7c07d612007-08-05 13:36:11 -05001535 write_unlock_irqrestore(&sg_index_lock, iflags);
1536
1537 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001538 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001539 cdev_del(sdp->cdev);
1540 sdp->cdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Tony Battersbyc6517b72009-01-21 14:45:50 -05001542 sg_put_dev(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Douglas Gilbert6460e752006-09-20 18:20:49 -04001545module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1546module_param_named(def_reserved_size, def_reserved_size, int,
1547 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1549
1550MODULE_AUTHOR("Douglas Gilbert");
1551MODULE_DESCRIPTION("SCSI generic (sg) driver");
1552MODULE_LICENSE("GPL");
1553MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001554MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Douglas Gilbert6460e752006-09-20 18:20:49 -04001556MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1557 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1559MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1560
1561static int __init
1562init_sg(void)
1563{
1564 int rc;
1565
Douglas Gilbert6460e752006-09-20 18:20:49 -04001566 if (scatter_elem_sz < PAGE_SIZE) {
1567 scatter_elem_sz = PAGE_SIZE;
1568 scatter_elem_sz_prev = scatter_elem_sz;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (def_reserved_size >= 0)
1571 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001572 else
1573 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1576 SG_MAX_DEVS, "sg");
1577 if (rc)
1578 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001579 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if ( IS_ERR(sg_sysfs_class) ) {
1581 rc = PTR_ERR(sg_sysfs_class);
1582 goto err_out;
1583 }
1584 sg_sysfs_valid = 1;
1585 rc = scsi_register_interface(&sg_interface);
1586 if (0 == rc) {
1587#ifdef CONFIG_SCSI_PROC_FS
1588 sg_proc_init();
1589#endif /* CONFIG_SCSI_PROC_FS */
1590 return 0;
1591 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001592 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593err_out:
1594 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1595 return rc;
1596}
1597
1598static void __exit
1599exit_sg(void)
1600{
1601#ifdef CONFIG_SCSI_PROC_FS
1602 sg_proc_cleanup();
1603#endif /* CONFIG_SCSI_PROC_FS */
1604 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001605 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 sg_sysfs_valid = 0;
1607 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1608 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001609 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001612static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001613{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001614 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001615 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001616 Sg_fd *sfp = srp->parentfp;
1617 sg_io_hdr_t *hp = &srp->header;
1618 int dxfer_len = (int) hp->dxfer_len;
1619 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001620 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001621 Sg_scatter_hold *req_schp = &srp->data;
1622 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1623 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001624 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001625 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1626
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001627 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1628 dxfer_len));
1629
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001630 rq = blk_get_request(q, rw, GFP_ATOMIC);
1631 if (!rq)
1632 return -ENOMEM;
1633
1634 memcpy(rq->cmd, cmd, hp->cmd_len);
1635
1636 rq->cmd_len = hp->cmd_len;
1637 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1638
1639 srp->rq = rq;
1640 rq->end_io_data = srp;
1641 rq->sense = srp->sense_b;
1642 rq->retries = SG_DEFAULT_RETRIES;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001645 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001646
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001647 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1648 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1649 !sfp->parentdp->device->host->unchecked_isa_dma &&
FUJITA Tomonori01cfcdd2008-08-28 15:05:59 +09001650 blk_rq_aligned(q, hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001651 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001652 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001653 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001654
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001655 if (md) {
1656 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1657 sg_link_reserve(sfp, srp, dxfer_len);
1658 else {
1659 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1660 if (res)
1661 return res;
1662 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001663
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001664 md->pages = req_schp->pages;
1665 md->page_order = req_schp->page_order;
1666 md->nr_entries = req_schp->k_use_sg;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001667 md->offset = 0;
FUJITA Tomonori97ae77a2008-12-18 14:49:38 +09001668 md->null_mapped = hp->dxferp ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001670
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001671 if (iov_count)
1672 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1673 hp->dxfer_len, GFP_ATOMIC);
1674 else
1675 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1676 hp->dxfer_len, GFP_ATOMIC);
1677
1678 if (!res) {
1679 srp->bio = rq->bio;
1680
1681 if (!md) {
1682 req_schp->dio_in_use = 1;
1683 hp->info |= SG_INFO_DIRECT_IO;
1684 }
1685 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001686 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689static void
1690sg_finish_rem_req(Sg_request * srp)
1691{
1692 Sg_fd *sfp = srp->parentfp;
1693 Sg_scatter_hold *req_schp = &srp->data;
1694
1695 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1696 if (srp->res_used)
1697 sg_unlink_reserve(sfp, srp);
1698 else
1699 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001700
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001701 if (srp->rq) {
1702 if (srp->bio)
1703 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001704
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001705 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001706 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 sg_remove_request(sfp, srp);
1709}
1710
1711static int
1712sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1713{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001714 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001715 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001717 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1718 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001721 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724static int
1725sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1726{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001727 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001728 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001729 int blk_size = buff_size, order;
1730 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Eric Sesterhennfb119932007-05-23 14:41:36 -07001732 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return -EFAULT;
1734 if (0 == blk_size)
1735 ++blk_size; /* don't know why */
1736/* round request up to next highest SG_SECTOR_SZ byte boundary */
1737 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1738 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1739 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001740
1741 /* N.B. ret_sz carried into this block ... */
1742 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1743 if (mx_sc_elems < 0)
1744 return mx_sc_elems; /* most likely -ENOMEM */
1745
Douglas Gilbert6460e752006-09-20 18:20:49 -04001746 num = scatter_elem_sz;
1747 if (unlikely(num != scatter_elem_sz_prev)) {
1748 if (num < PAGE_SIZE) {
1749 scatter_elem_sz = PAGE_SIZE;
1750 scatter_elem_sz_prev = PAGE_SIZE;
1751 } else
1752 scatter_elem_sz_prev = num;
1753 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001754
1755 if (sfp->low_dma)
1756 gfp_mask |= GFP_DMA;
1757
1758 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1759 gfp_mask |= __GFP_ZERO;
1760
1761 order = get_order(num);
1762retry:
1763 ret_sz = 1 << (PAGE_SHIFT + order);
1764
1765 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1766 k++, rem_sz -= ret_sz) {
1767
Douglas Gilbert6460e752006-09-20 18:20:49 -04001768 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001769 scatter_elem_sz_prev : rem_sz;
1770
1771 schp->pages[k] = alloc_pages(gfp_mask, order);
1772 if (!schp->pages[k])
1773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Douglas Gilbert6460e752006-09-20 18:20:49 -04001775 if (num == scatter_elem_sz_prev) {
1776 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1777 scatter_elem_sz = ret_sz;
1778 scatter_elem_sz_prev = ret_sz;
1779 }
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001782 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1783 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001784 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001786 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001787 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001788 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1789 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001790
1791 schp->bufflen = blk_size;
1792 if (rem_sz > 0) /* must have failed */
1793 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001795out:
1796 for (i = 0; i < k; i++)
1797 __free_pages(schp->pages[k], order);
1798
1799 if (--order >= 0)
1800 goto retry;
1801
1802 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static void
1806sg_remove_scat(Sg_scatter_hold * schp)
1807{
1808 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001809 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001810 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int k;
1812
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001813 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001815 "sg_remove_scat: k=%d, pg=0x%p\n",
1816 k, schp->pages[k]));
1817 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001819
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001820 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
Mike Christied6b10342005-11-08 04:06:41 -06001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 memset(schp, 0, sizeof (*schp));
1824}
1825
1826static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1828{
1829 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001830 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1833 num_read_xfer));
1834 if ((!outp) || (num_read_xfer <= 0))
1835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001837 num = 1 << (PAGE_SHIFT + schp->page_order);
1838 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001839 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001840 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001841 num_read_xfer))
1842 return -EFAULT;
1843 break;
1844 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001845 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001846 num))
1847 return -EFAULT;
1848 num_read_xfer -= num;
1849 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 break;
Mike Christied6b10342005-11-08 04:06:41 -06001851 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Mike Christied6b10342005-11-08 04:06:41 -06001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return 0;
1856}
1857
1858static void
1859sg_build_reserve(Sg_fd * sfp, int req_size)
1860{
1861 Sg_scatter_hold *schp = &sfp->reserve;
1862
1863 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1864 do {
1865 if (req_size < PAGE_SIZE)
1866 req_size = PAGE_SIZE;
1867 if (0 == sg_build_indirect(schp, sfp, req_size))
1868 return;
1869 else
1870 sg_remove_scat(schp);
1871 req_size >>= 1; /* divide by 2 */
1872 } while (req_size > (PAGE_SIZE / 2));
1873}
1874
1875static void
1876sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1877{
1878 Sg_scatter_hold *req_schp = &srp->data;
1879 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001880 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 srp->res_used = 1;
1883 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001884 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001886 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1887 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001888 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001889 req_schp->k_use_sg = k + 1;
1890 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001891 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001892
1893 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001894 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001895 break;
1896 } else
1897 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Mike Christied6b10342005-11-08 04:06:41 -06001899
1900 if (k >= rsv_schp->k_use_sg)
1901 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904static void
1905sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1906{
1907 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1910 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 req_schp->k_use_sg = 0;
1912 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001913 req_schp->pages = NULL;
1914 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 req_schp->sglist_len = 0;
1916 sfp->save_scat_len = 0;
1917 srp->res_used = 0;
1918}
1919
1920static Sg_request *
1921sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1922{
1923 Sg_request *resp;
1924 unsigned long iflags;
1925
1926 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1927 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1928 /* look for requests that are ready + not SG_IO owned */
1929 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1930 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1931 resp->done = 2; /* guard against other readers */
1932 break;
1933 }
1934 }
1935 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1936 return resp;
1937}
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/* always adds to end of list */
1940static Sg_request *
1941sg_add_request(Sg_fd * sfp)
1942{
1943 int k;
1944 unsigned long iflags;
1945 Sg_request *resp;
1946 Sg_request *rp = sfp->req_arr;
1947
1948 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1949 resp = sfp->headrp;
1950 if (!resp) {
1951 memset(rp, 0, sizeof (Sg_request));
1952 rp->parentfp = sfp;
1953 resp = rp;
1954 sfp->headrp = resp;
1955 } else {
1956 if (0 == sfp->cmd_q)
1957 resp = NULL; /* command queuing disallowed */
1958 else {
1959 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1960 if (!rp->parentfp)
1961 break;
1962 }
1963 if (k < SG_MAX_QUEUE) {
1964 memset(rp, 0, sizeof (Sg_request));
1965 rp->parentfp = sfp;
1966 while (resp->nextrp)
1967 resp = resp->nextrp;
1968 resp->nextrp = rp;
1969 resp = rp;
1970 } else
1971 resp = NULL;
1972 }
1973 }
1974 if (resp) {
1975 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06001976 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1979 return resp;
1980}
1981
1982/* Return of 1 for found; 0 for not found */
1983static int
1984sg_remove_request(Sg_fd * sfp, Sg_request * srp)
1985{
1986 Sg_request *prev_rp;
1987 Sg_request *rp;
1988 unsigned long iflags;
1989 int res = 0;
1990
1991 if ((!sfp) || (!srp) || (!sfp->headrp))
1992 return res;
1993 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1994 prev_rp = sfp->headrp;
1995 if (srp == prev_rp) {
1996 sfp->headrp = prev_rp->nextrp;
1997 prev_rp->parentfp = NULL;
1998 res = 1;
1999 } else {
2000 while ((rp = prev_rp->nextrp)) {
2001 if (srp == rp) {
2002 prev_rp->nextrp = rp->nextrp;
2003 rp->parentfp = NULL;
2004 res = 1;
2005 break;
2006 }
2007 prev_rp = rp;
2008 }
2009 }
2010 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2011 return res;
2012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014static Sg_fd *
2015sg_add_sfp(Sg_device * sdp, int dev)
2016{
2017 Sg_fd *sfp;
2018 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002019 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Mike Christied6b10342005-11-08 04:06:41 -06002021 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (!sfp)
2023 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 init_waitqueue_head(&sfp->read_wait);
2026 rwlock_init(&sfp->rq_list_lock);
2027
Tony Battersbyc6517b72009-01-21 14:45:50 -05002028 kref_init(&sfp->f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 sfp->timeout = SG_DEFAULT_TIMEOUT;
2030 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2031 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2032 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2033 sdp->device->host->unchecked_isa_dma : 1;
2034 sfp->cmd_q = SG_DEF_COMMAND_Q;
2035 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2036 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002037 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (!sdp->headfp)
2039 sdp->headfp = sfp;
2040 else { /* add to tail of existing list */
2041 Sg_fd *pfp = sdp->headfp;
2042 while (pfp->nextfp)
2043 pfp = pfp->nextfp;
2044 pfp->nextfp = sfp;
2045 }
James Bottomley7c07d612007-08-05 13:36:11 -05002046 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002048 if (unlikely(sg_big_buff != def_reserved_size))
2049 sg_big_buff = def_reserved_size;
2050
Alan Stern44ec9542007-02-20 11:01:57 -05002051 bufflen = min_t(int, sg_big_buff,
2052 sdp->device->request_queue->max_sectors * 512);
2053 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2055 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
Tony Battersbyc6517b72009-01-21 14:45:50 -05002056
2057 kref_get(&sdp->d_ref);
2058 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return sfp;
2060}
2061
Tony Battersbyc6517b72009-01-21 14:45:50 -05002062static void sg_remove_sfp_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Tony Battersbyc6517b72009-01-21 14:45:50 -05002064 struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2065 struct sg_device *sdp = sfp->parentdp;
2066
2067 /* Cleanup any responses which were never read(). */
2068 while (sfp->headrp)
2069 sg_finish_rem_req(sfp->headrp);
2070
2071 if (sfp->reserve.bufflen > 0) {
2072 SCSI_LOG_TIMEOUT(6,
2073 printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2074 (int) sfp->reserve.bufflen,
2075 (int) sfp->reserve.k_use_sg));
2076 sg_remove_scat(&sfp->reserve);
2077 }
2078
2079 SCSI_LOG_TIMEOUT(6,
2080 printk("sg_remove_sfp: %s, sfp=0x%p\n",
2081 sdp->disk->disk_name,
2082 sfp));
2083 kfree(sfp);
2084
2085 scsi_device_put(sdp->device);
2086 sg_put_dev(sdp);
2087 module_put(THIS_MODULE);
2088}
2089
2090static void sg_remove_sfp(struct kref *kref)
2091{
2092 struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
2093 struct sg_device *sdp = sfp->parentdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 Sg_fd *fp;
2095 Sg_fd *prev_fp;
Tony Battersbyc6517b72009-01-21 14:45:50 -05002096 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Tony Battersbyc6517b72009-01-21 14:45:50 -05002098 /* CAUTION! Note that sfp can still be found by walking sdp->headfp
2099 * even though the refcount is now 0. Therefore, unlink sfp from
2100 * sdp->headfp BEFORE doing any other cleanup.
2101 */
2102
2103 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 prev_fp = sdp->headfp;
2105 if (sfp == prev_fp)
2106 sdp->headfp = prev_fp->nextfp;
2107 else {
2108 while ((fp = prev_fp->nextfp)) {
2109 if (sfp == fp) {
2110 prev_fp->nextfp = fp->nextfp;
2111 break;
2112 }
2113 prev_fp = fp;
2114 }
2115 }
Tony Battersbyc6517b72009-01-21 14:45:50 -05002116 write_unlock_irqrestore(&sg_index_lock, iflags);
2117 wake_up_interruptible(&sdp->o_excl_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Tony Battersbyc6517b72009-01-21 14:45:50 -05002119 execute_in_process_context(sg_remove_sfp_usercontext, &sfp->ew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122static int
2123sg_res_in_use(Sg_fd * sfp)
2124{
2125 const Sg_request *srp;
2126 unsigned long iflags;
2127
2128 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2129 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2130 if (srp->res_used)
2131 break;
2132 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2133 return srp ? 1 : 0;
2134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136#ifdef CONFIG_SCSI_PROC_FS
2137static int
James Bottomley7c07d612007-08-05 13:36:11 -05002138sg_idr_max_id(int id, void *p, void *data)
2139{
2140 int *k = data;
2141
2142 if (*k < id)
2143 *k = id;
2144
2145 return 0;
2146}
2147
2148static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149sg_last_dev(void)
2150{
Tony Battersby53474c02008-01-22 15:25:49 -05002151 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 unsigned long iflags;
2153
James Bottomley7c07d612007-08-05 13:36:11 -05002154 read_lock_irqsave(&sg_index_lock, iflags);
2155 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2156 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return k + 1; /* origin 1 */
2158}
2159#endif
2160
Tony Battersbyc6517b72009-01-21 14:45:50 -05002161/* must be called with sg_index_lock held */
2162static Sg_device *sg_lookup_dev(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Tony Battersbyc6517b72009-01-21 14:45:50 -05002164 return idr_find(&sg_index_idr, dev);
2165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Tony Battersbyc6517b72009-01-21 14:45:50 -05002167static Sg_device *sg_get_dev(int dev)
2168{
2169 struct sg_device *sdp;
2170 unsigned long flags;
2171
2172 read_lock_irqsave(&sg_index_lock, flags);
2173 sdp = sg_lookup_dev(dev);
2174 if (!sdp)
2175 sdp = ERR_PTR(-ENXIO);
2176 else if (sdp->detached) {
2177 /* If sdp->detached, then the refcount may already be 0, in
2178 * which case it would be a bug to do kref_get().
2179 */
2180 sdp = ERR_PTR(-ENODEV);
2181 } else
2182 kref_get(&sdp->d_ref);
2183 read_unlock_irqrestore(&sg_index_lock, flags);
James Bottomley7c07d612007-08-05 13:36:11 -05002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return sdp;
2186}
2187
Tony Battersbyc6517b72009-01-21 14:45:50 -05002188static void sg_put_dev(struct sg_device *sdp)
2189{
2190 kref_put(&sdp->d_ref, sg_device_destroy);
2191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#ifdef CONFIG_SCSI_PROC_FS
2194
2195static struct proc_dir_entry *sg_proc_sgp = NULL;
2196
2197static char sg_proc_sg_dirname[] = "scsi/sg";
2198
2199static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2200
2201static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2202static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2203 size_t count, loff_t *off);
2204static struct file_operations adio_fops = {
2205 /* .owner, .read and .llseek added in sg_proc_init() */
2206 .open = sg_proc_single_open_adio,
2207 .write = sg_proc_write_adio,
2208 .release = single_release,
2209};
2210
2211static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2212static ssize_t sg_proc_write_dressz(struct file *filp,
2213 const char __user *buffer, size_t count, loff_t *off);
2214static struct file_operations dressz_fops = {
2215 .open = sg_proc_single_open_dressz,
2216 .write = sg_proc_write_dressz,
2217 .release = single_release,
2218};
2219
2220static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2221static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2222static struct file_operations version_fops = {
2223 .open = sg_proc_single_open_version,
2224 .release = single_release,
2225};
2226
2227static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2228static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2229static struct file_operations devhdr_fops = {
2230 .open = sg_proc_single_open_devhdr,
2231 .release = single_release,
2232};
2233
2234static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2235static int sg_proc_open_dev(struct inode *inode, struct file *file);
2236static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2237static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2238static void dev_seq_stop(struct seq_file *s, void *v);
2239static struct file_operations dev_fops = {
2240 .open = sg_proc_open_dev,
2241 .release = seq_release,
2242};
2243static struct seq_operations dev_seq_ops = {
2244 .start = dev_seq_start,
2245 .next = dev_seq_next,
2246 .stop = dev_seq_stop,
2247 .show = sg_proc_seq_show_dev,
2248};
2249
2250static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2251static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2252static struct file_operations devstrs_fops = {
2253 .open = sg_proc_open_devstrs,
2254 .release = seq_release,
2255};
2256static struct seq_operations devstrs_seq_ops = {
2257 .start = dev_seq_start,
2258 .next = dev_seq_next,
2259 .stop = dev_seq_stop,
2260 .show = sg_proc_seq_show_devstrs,
2261};
2262
2263static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2264static int sg_proc_open_debug(struct inode *inode, struct file *file);
2265static struct file_operations debug_fops = {
2266 .open = sg_proc_open_debug,
2267 .release = seq_release,
2268};
2269static struct seq_operations debug_seq_ops = {
2270 .start = dev_seq_start,
2271 .next = dev_seq_next,
2272 .stop = dev_seq_stop,
2273 .show = sg_proc_seq_show_debug,
2274};
2275
2276
2277struct sg_proc_leaf {
2278 const char * name;
2279 struct file_operations * fops;
2280};
2281
2282static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2283 {"allow_dio", &adio_fops},
2284 {"debug", &debug_fops},
2285 {"def_reserved_size", &dressz_fops},
2286 {"device_hdr", &devhdr_fops},
2287 {"devices", &dev_fops},
2288 {"device_strs", &devstrs_fops},
2289 {"version", &version_fops}
2290};
2291
2292static int
2293sg_proc_init(void)
2294{
2295 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002296 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct sg_proc_leaf * leaf;
2298
Al Viro66600222005-09-28 22:32:57 +01002299 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (!sg_proc_sgp)
2301 return 1;
2302 for (k = 0; k < num_leaves; ++k) {
2303 leaf = &sg_proc_leaf_arr[k];
2304 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002305 leaf->fops->owner = THIS_MODULE;
2306 leaf->fops->read = seq_read;
2307 leaf->fops->llseek = seq_lseek;
2308 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 return 0;
2311}
2312
2313static void
2314sg_proc_cleanup(void)
2315{
2316 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002317 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 if (!sg_proc_sgp)
2320 return;
2321 for (k = 0; k < num_leaves; ++k)
2322 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2323 remove_proc_entry(sg_proc_sg_dirname, NULL);
2324}
2325
2326
2327static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2328{
2329 seq_printf(s, "%d\n", *((int *)s->private));
2330 return 0;
2331}
2332
2333static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2334{
2335 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2336}
2337
2338static ssize_t
2339sg_proc_write_adio(struct file *filp, const char __user *buffer,
2340 size_t count, loff_t *off)
2341{
2342 int num;
2343 char buff[11];
2344
2345 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2346 return -EACCES;
2347 num = (count < 10) ? count : 10;
2348 if (copy_from_user(buff, buffer, num))
2349 return -EFAULT;
2350 buff[num] = '\0';
2351 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2352 return count;
2353}
2354
2355static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2356{
2357 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2358}
2359
2360static ssize_t
2361sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2362 size_t count, loff_t *off)
2363{
2364 int num;
2365 unsigned long k = ULONG_MAX;
2366 char buff[11];
2367
2368 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2369 return -EACCES;
2370 num = (count < 10) ? count : 10;
2371 if (copy_from_user(buff, buffer, num))
2372 return -EFAULT;
2373 buff[num] = '\0';
2374 k = simple_strtoul(buff, NULL, 10);
2375 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2376 sg_big_buff = k;
2377 return count;
2378 }
2379 return -ERANGE;
2380}
2381
2382static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2383{
2384 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2385 sg_version_date);
2386 return 0;
2387}
2388
2389static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2390{
2391 return single_open(file, sg_proc_seq_show_version, NULL);
2392}
2393
2394static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2395{
2396 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2397 "online\n");
2398 return 0;
2399}
2400
2401static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2402{
2403 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2404}
2405
2406struct sg_proc_deviter {
2407 loff_t index;
2408 size_t max;
2409};
2410
2411static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2412{
2413 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2414
Jan Blunck729d70f2005-08-27 11:07:52 -07002415 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (! it)
2417 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 it->index = *pos;
2420 it->max = sg_last_dev();
2421 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002422 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2427{
Jan Blunck729d70f2005-08-27 11:07:52 -07002428 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 *pos = ++it->index;
2431 return (it->index < it->max) ? it : NULL;
2432}
2433
2434static void dev_seq_stop(struct seq_file *s, void *v)
2435{
Jan Blunck729d70f2005-08-27 11:07:52 -07002436 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
2439static int sg_proc_open_dev(struct inode *inode, struct file *file)
2440{
2441 return seq_open(file, &dev_seq_ops);
2442}
2443
2444static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2445{
2446 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2447 Sg_device *sdp;
2448 struct scsi_device *scsidp;
Tony Battersbyc6517b72009-01-21 14:45:50 -05002449 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Tony Battersbyc6517b72009-01-21 14:45:50 -05002451 read_lock_irqsave(&sg_index_lock, iflags);
2452 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2454 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2455 scsidp->host->host_no, scsidp->channel,
2456 scsidp->id, scsidp->lun, (int) scsidp->type,
2457 1,
2458 (int) scsidp->queue_depth,
2459 (int) scsidp->device_busy,
2460 (int) scsi_device_online(scsidp));
2461 else
2462 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
Tony Battersbyc6517b72009-01-21 14:45:50 -05002463 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return 0;
2465}
2466
2467static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2468{
2469 return seq_open(file, &devstrs_seq_ops);
2470}
2471
2472static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2473{
2474 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2475 Sg_device *sdp;
2476 struct scsi_device *scsidp;
Tony Battersbyc6517b72009-01-21 14:45:50 -05002477 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Tony Battersbyc6517b72009-01-21 14:45:50 -05002479 read_lock_irqsave(&sg_index_lock, iflags);
2480 sdp = it ? sg_lookup_dev(it->index) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2482 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2483 scsidp->vendor, scsidp->model, scsidp->rev);
2484 else
2485 seq_printf(s, "<no active device>\n");
Tony Battersbyc6517b72009-01-21 14:45:50 -05002486 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return 0;
2488}
2489
Tony Battersbyc6517b72009-01-21 14:45:50 -05002490/* must be called while holding sg_index_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2492{
2493 int k, m, new_interface, blen, usg;
2494 Sg_request *srp;
2495 Sg_fd *fp;
2496 const sg_io_hdr_t *hp;
2497 const char * cp;
cb59e842005-04-02 13:51:23 -06002498 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Tony Battersbyc6517b72009-01-21 14:45:50 -05002500 for (k = 0, fp = sdp->headfp; fp != NULL; ++k, fp = fp->nextfp) {
2501 read_lock(&fp->rq_list_lock); /* irqs already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2503 "(res)sgat=%d low_dma=%d\n", k + 1,
2504 jiffies_to_msecs(fp->timeout),
2505 fp->reserve.bufflen,
2506 (int) fp->reserve.k_use_sg,
2507 (int) fp->low_dma);
2508 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2509 (int) fp->cmd_q, (int) fp->force_packid,
2510 (int) fp->keep_orphan, (int) fp->closed);
Tony Battersbyc6517b72009-01-21 14:45:50 -05002511 for (m = 0, srp = fp->headrp;
2512 srp != NULL;
2513 ++m, srp = srp->nextrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 hp = &srp->header;
2515 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2516 if (srp->res_used) {
2517 if (new_interface &&
2518 (SG_FLAG_MMAP_IO & hp->flags))
2519 cp = " mmap>> ";
2520 else
2521 cp = " rb>> ";
2522 } else {
2523 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2524 cp = " dio>> ";
2525 else
2526 cp = " ";
2527 }
2528 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002529 blen = srp->data.bufflen;
2530 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 seq_printf(s, srp->done ?
2532 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002533 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 seq_printf(s, " id=%d blen=%d",
2535 srp->header.pack_id, blen);
2536 if (srp->done)
2537 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002538 else {
2539 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002541 (new_interface ? hp->timeout :
2542 jiffies_to_msecs(fp->timeout)),
2543 (ms > hp->duration ? ms - hp->duration : 0));
2544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2546 (int) srp->data.cmd_opcode);
2547 }
2548 if (0 == m)
2549 seq_printf(s, " No requests active\n");
Tony Battersbyc6517b72009-01-21 14:45:50 -05002550 read_unlock(&fp->rq_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552}
2553
2554static int sg_proc_open_debug(struct inode *inode, struct file *file)
2555{
2556 return seq_open(file, &debug_seq_ops);
2557}
2558
2559static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2560{
2561 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2562 Sg_device *sdp;
Tony Battersbyc6517b72009-01-21 14:45:50 -05002563 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002566 seq_printf(s, "max_active_device=%d(origin 1)\n",
2567 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2569 }
Tony Battersbyc6517b72009-01-21 14:45:50 -05002570
2571 read_lock_irqsave(&sg_index_lock, iflags);
2572 sdp = it ? sg_lookup_dev(it->index) : NULL;
2573 if (sdp && sdp->headfp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct scsi_device *scsidp = sdp->device;
2575
Tony Battersbyc6517b72009-01-21 14:45:50 -05002576 seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2577 if (sdp->detached)
2578 seq_printf(s, "detached pending close ");
2579 else
2580 seq_printf
2581 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2582 scsidp->host->host_no,
2583 scsidp->channel, scsidp->id,
2584 scsidp->lun,
2585 scsidp->host->hostt->emulated);
2586 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2587 sdp->sg_tablesize, sdp->exclude);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 sg_proc_debug_helper(s, sdp);
2589 }
Tony Battersbyc6517b72009-01-21 14:45:50 -05002590 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return 0;
2592}
2593
2594#endif /* CONFIG_SCSI_PROC_FS */
2595
2596module_init(init_sg);
2597module_exit(exit_sg);