blob: ba9b9bbd4e7385e8d83f3ed4f4a1151b96ab3a1f [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 *);
104static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
James Bottomley7c07d612007-08-05 13:36:11 -0500106static DEFINE_IDR(sg_index_idr);
107static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 file descriptor list for device */
109
110static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100111 .add_dev = sg_add,
112 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
116 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900117 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned bufflen; /* Size of (aggregate) data buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200119 struct page **pages;
120 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
122 unsigned char cmd_opcode; /* first byte of command */
123} Sg_scatter_hold;
124
125struct sg_device; /* forward declarations */
126struct sg_fd;
127
128typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct sg_request *nextrp; /* NULL -> tail request (slist) */
130 struct sg_fd *parentfp; /* NULL -> not in use */
131 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
132 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600133 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
135 char orphan; /* 1 -> drop on sight, 0 -> normal */
136 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
137 volatile char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900138 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900139 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140} Sg_request;
141
142typedef struct sg_fd { /* holds the state of a file descriptor */
143 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
144 struct sg_device *parentdp; /* owning device */
145 wait_queue_head_t read_wait; /* queue read until command done */
146 rwlock_t rq_list_lock; /* protect access to list in req_arr */
147 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
148 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
149 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
150 unsigned save_scat_len; /* original length of trunc. scat. element */
151 Sg_request *headrp; /* head of request slist, NULL->empty */
152 struct fasync_struct *async_qp; /* used by asynchronous notification */
153 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
154 char low_dma; /* as in parent but possibly overridden to 1 */
155 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
156 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
157 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
160 char mmap_called; /* 0 -> mmap() never called on this fd */
161} Sg_fd;
162
163typedef struct sg_device { /* holds the state of each scsi generic device */
164 struct scsi_device *device;
165 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
166 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500167 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 Sg_fd *headfp; /* first open fd belonging to this device */
169 volatile char detached; /* 0->attached, 1->detached pending removal */
170 volatile char exclude; /* opened for exclusive access */
171 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
172 struct gendisk *disk;
173 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
174} Sg_device;
175
176static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600177/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900178static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900179static int sg_start_req(Sg_request *srp, unsigned char *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void sg_finish_rem_req(Sg_request * srp);
181static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
183 int tablesize);
184static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
185 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200186static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
187 const char __user *buf, size_t count, int blocking,
188 int read_only, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
192static void sg_remove_scat(Sg_scatter_hold * schp);
193static void sg_build_reserve(Sg_fd * sfp, int req_size);
194static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
195static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
197static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
198static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
199static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
200static Sg_request *sg_add_request(Sg_fd * sfp);
201static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
202static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#ifdef CONFIG_SCSI_PROC_FS
205static int sg_last_dev(void);
206#endif
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define SZ_SG_HEADER sizeof(struct sg_header)
209#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
210#define SZ_SG_IOVEC sizeof(sg_iovec_t)
211#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
212
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900213static int sg_allow_access(struct file *filp, unsigned char *cmd)
214{
215 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
216 struct request_queue *q = sfp->parentdp->device->request_queue;
217
218 if (sfp->parentdp->device->type == TYPE_SCANNER)
219 return 0;
220
221 return blk_verify_command(&q->cmd_filter,
222 cmd, filp->f_mode & FMODE_WRITE);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int
226sg_open(struct inode *inode, struct file *filp)
227{
228 int dev = iminor(inode);
229 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600230 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 Sg_device *sdp;
232 Sg_fd *sfp;
233 int res;
234 int retval;
235
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600236 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 nonseekable_open(inode, filp);
238 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
239 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600240 if ((!sdp) || (!sdp->device)) {
241 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600243 }
244 if (sdp->detached) {
245 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* This driver's module count bumped by fops_get in <linux/fs.h> */
250 /* Prevent the device driver from vanishing while we sleep */
251 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600252 if (retval) {
253 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600255 }
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 {
306 if (flags & O_EXCL)
307 sdp->exclude = 0; /* undo if error */
308 retval = -ENOMEM;
309 goto error_out;
310 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600311 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313
314 error_out:
315 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600316 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return retval;
318}
319
320/* Following function was formerly called 'sg_close' */
321static int
322sg_release(struct inode *inode, struct file *filp)
323{
324 Sg_device *sdp;
325 Sg_fd *sfp;
326
327 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
328 return -ENXIO;
329 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
330 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
331 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
332 if (!sdp->detached) {
333 scsi_device_put(sdp->device);
334 }
335 sdp->exclude = 0;
336 wake_up_interruptible(&sdp->o_excl_wait);
337 }
338 return 0;
339}
340
341static ssize_t
342sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 Sg_device *sdp;
345 Sg_fd *sfp;
346 Sg_request *srp;
347 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600349 struct sg_header *old_hdr = NULL;
350 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
353 return -ENXIO;
354 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
355 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!access_ok(VERIFY_WRITE, buf, count))
358 return -EFAULT;
359 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600360 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
361 if (!old_hdr)
362 return -ENOMEM;
363 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
364 retval = -EFAULT;
365 goto free_old_hdr;
366 }
367 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600369 sg_io_hdr_t *new_hdr;
370 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
371 if (!new_hdr) {
372 retval = -ENOMEM;
373 goto free_old_hdr;
374 }
375 retval =__copy_from_user
376 (new_hdr, buf, SZ_SG_IO_HDR);
377 req_pack_id = new_hdr->pack_id;
378 kfree(new_hdr);
379 if (retval) {
380 retval = -EFAULT;
381 goto free_old_hdr;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 } else
cb59e842005-04-02 13:51:23 -0600385 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 srp = sg_get_rq_mark(sfp, req_pack_id);
388 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600389 if (sdp->detached) {
390 retval = -ENODEV;
391 goto free_old_hdr;
392 }
393 if (filp->f_flags & O_NONBLOCK) {
394 retval = -EAGAIN;
395 goto free_old_hdr;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 while (1) {
cb59e842005-04-02 13:51:23 -0600398 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600400 (sdp->detached ||
401 (srp = sg_get_rq_mark(sfp, req_pack_id))),
402 retval);
403 if (sdp->detached) {
404 retval = -ENODEV;
405 goto free_old_hdr;
406 }
407 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
cb59e842005-04-02 13:51:23 -0600409
410 /* -ERESTARTSYS as signal hit process */
411 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
cb59e842005-04-02 13:51:23 -0600414 if (srp->header.interface_id != '\0') {
415 retval = sg_new_read(sfp, buf, count, srp);
416 goto free_old_hdr;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600420 if (old_hdr == NULL) {
421 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
422 if (! old_hdr) {
423 retval = -ENOMEM;
424 goto free_old_hdr;
425 }
426 }
427 memset(old_hdr, 0, SZ_SG_HEADER);
428 old_hdr->reply_len = (int) hp->timeout;
429 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
430 old_hdr->pack_id = hp->pack_id;
431 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600433 old_hdr->target_status = hp->masked_status;
434 old_hdr->host_status = hp->host_status;
435 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if ((CHECK_CONDITION & hp->masked_status) ||
437 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600438 memcpy(old_hdr->sense_buffer, srp->sense_b,
439 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 switch (hp->host_status) {
441 /* This setup of 'result' is for backward compatibility and is best
442 ignored by the user who should use target, host + driver status */
443 case DID_OK:
444 case DID_PASSTHROUGH:
445 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case DID_NO_CONNECT:
449 case DID_BUS_BUSY:
450 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600451 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case DID_BAD_TARGET:
454 case DID_ABORT:
455 case DID_PARITY:
456 case DID_RESET:
457 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600458 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600461 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 hp->masked_status == GOOD) ? 0 : EIO;
463 break;
464 default:
cb59e842005-04-02 13:51:23 -0600465 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 }
468
469 /* Now copy the result back to the user buffer. */
470 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600471 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
472 retval = -EFAULT;
473 goto free_old_hdr;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600476 if (count > old_hdr->reply_len)
477 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600479 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
480 retval = -EFAULT;
481 goto free_old_hdr;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 } else
cb59e842005-04-02 13:51:23 -0600485 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600487 retval = count;
488free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800489 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600490 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static ssize_t
494sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
495{
496 sg_io_hdr_t *hp = &srp->header;
497 int err = 0;
498 int len;
499
500 if (count < SZ_SG_IO_HDR) {
501 err = -EINVAL;
502 goto err_out;
503 }
504 hp->sb_len_wr = 0;
505 if ((hp->mx_sb_len > 0) && hp->sbp) {
506 if ((CHECK_CONDITION & hp->masked_status) ||
507 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600508 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
510 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
511 len = (len > sb_len) ? sb_len : len;
512 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
513 err = -EFAULT;
514 goto err_out;
515 }
516 hp->sb_len_wr = len;
517 }
518 }
519 if (hp->masked_status || hp->host_status || hp->driver_status)
520 hp->info |= SG_INFO_CHECK;
521 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
522 err = -EFAULT;
523 goto err_out;
524 }
FUJITA Tomonori0b6cb262008-09-02 22:50:07 +0900525err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sg_finish_rem_req(srp);
527 return (0 == err) ? count : err;
528}
529
530static ssize_t
531sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
532{
533 int mxsize, cmd_size, k;
534 int input_size, blocking;
535 unsigned char opcode;
536 Sg_device *sdp;
537 Sg_fd *sfp;
538 Sg_request *srp;
539 struct sg_header old_hdr;
540 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600541 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
544 return -ENXIO;
545 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
546 sdp->disk->disk_name, (int) count));
547 if (sdp->detached)
548 return -ENODEV;
549 if (!((filp->f_flags & O_NONBLOCK) ||
550 scsi_block_when_processing_errors(sdp->device)))
551 return -ENXIO;
552
553 if (!access_ok(VERIFY_READ, buf, count))
554 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
555 if (count < SZ_SG_HEADER)
556 return -EIO;
557 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
558 return -EFAULT;
559 blocking = !(filp->f_flags & O_NONBLOCK);
560 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200561 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (count < (SZ_SG_HEADER + 6))
563 return -EIO; /* The minimum scsi command length is 6 bytes. */
564
565 if (!(srp = sg_add_request(sfp))) {
566 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
567 return -EDOM;
568 }
569 buf += SZ_SG_HEADER;
570 __get_user(opcode, buf);
571 if (sfp->next_cmd_len > 0) {
572 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
573 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
574 sfp->next_cmd_len = 0;
575 sg_remove_request(sfp, srp);
576 return -EIO;
577 }
578 cmd_size = sfp->next_cmd_len;
579 sfp->next_cmd_len = 0; /* reset so only this write() effected */
580 } else {
581 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
582 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
583 cmd_size = 12;
584 }
585 SCSI_LOG_TIMEOUT(4, printk(
586 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
587/* Determine buffer size. */
588 input_size = count - cmd_size;
589 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
590 mxsize -= SZ_SG_HEADER;
591 input_size -= SZ_SG_HEADER;
592 if (input_size < 0) {
593 sg_remove_request(sfp, srp);
594 return -EIO; /* User did not pass enough bytes for this command. */
595 }
596 hp = &srp->header;
597 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
598 hp->cmd_len = (unsigned char) cmd_size;
599 hp->iovec_count = 0;
600 hp->mx_sb_len = 0;
601 if (input_size > 0)
602 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
603 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
604 else
605 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
606 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900607 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
608 hp->dxferp = (char __user *)buf + cmd_size;
609 else
610 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 hp->sbp = NULL;
612 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
613 hp->flags = input_size; /* structure abuse ... */
614 hp->pack_id = old_hdr.pack_id;
615 hp->usr_ptr = NULL;
616 if (__copy_from_user(cmnd, buf, cmd_size))
617 return -EFAULT;
618 /*
619 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
620 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
621 * is a non-zero input_size, so emit a warning.
622 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100623 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
624 static char cmd[TASK_COMM_LEN];
625 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 printk(KERN_WARNING
627 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
628 "guessing data in;\n" KERN_WARNING " "
629 "program %s not setting count and/or reply_len properly\n",
630 old_hdr.reply_len - (int)SZ_SG_HEADER,
631 input_size, (unsigned int) cmnd[0],
632 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100633 strcpy(cmd, current->comm);
634 }
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
637 return (k < 0) ? k : count;
638}
639
640static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200641sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
642 size_t count, int blocking, int read_only,
643 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int k;
646 Sg_request *srp;
647 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600648 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int timeout;
650 unsigned long ul_timeout;
651
652 if (count < SZ_SG_IO_HDR)
653 return -EINVAL;
654 if (!access_ok(VERIFY_READ, buf, count))
655 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
656
657 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
658 if (!(srp = sg_add_request(sfp))) {
659 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
660 return -EDOM;
661 }
662 hp = &srp->header;
663 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
664 sg_remove_request(sfp, srp);
665 return -EFAULT;
666 }
667 if (hp->interface_id != 'S') {
668 sg_remove_request(sfp, srp);
669 return -ENOSYS;
670 }
671 if (hp->flags & SG_FLAG_MMAP_IO) {
672 if (hp->dxfer_len > sfp->reserve.bufflen) {
673 sg_remove_request(sfp, srp);
674 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
675 }
676 if (hp->flags & SG_FLAG_DIRECT_IO) {
677 sg_remove_request(sfp, srp);
678 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
679 }
680 if (sg_res_in_use(sfp)) {
681 sg_remove_request(sfp, srp);
682 return -EBUSY; /* reserve buffer already being used */
683 }
684 }
685 ul_timeout = msecs_to_jiffies(srp->header.timeout);
686 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
687 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
688 sg_remove_request(sfp, srp);
689 return -EMSGSIZE;
690 }
691 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
692 sg_remove_request(sfp, srp);
693 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
694 }
695 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
696 sg_remove_request(sfp, srp);
697 return -EFAULT;
698 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900699 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sg_remove_request(sfp, srp);
701 return -EPERM;
702 }
703 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
704 if (k < 0)
705 return k;
706 if (o_srp)
707 *o_srp = srp;
708 return count;
709}
710
711static int
712sg_common_write(Sg_fd * sfp, Sg_request * srp,
713 unsigned char *cmnd, int timeout, int blocking)
714{
Mike Christied6b10342005-11-08 04:06:41 -0600715 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 Sg_device *sdp = sfp->parentdp;
717 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
720 hp->status = 0;
721 hp->masked_status = 0;
722 hp->msg_status = 0;
723 hp->info = 0;
724 hp->host_status = 0;
725 hp->driver_status = 0;
726 hp->resid = 0;
727 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
728 (int) cmnd[0], (int) hp->cmd_len));
729
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900730 k = sg_start_req(srp, cmnd);
731 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400732 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sg_finish_rem_req(srp);
734 return k; /* probably out of space --> ENOMEM */
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (sdp->detached) {
737 sg_finish_rem_req(srp);
738 return -ENODEV;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 switch (hp->dxfer_direction) {
742 case SG_DXFER_TO_FROM_DEV:
743 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600744 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600747 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600750 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752 default:
Mike Christied6b10342005-11-08 04:06:41 -0600753 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 }
cb59e842005-04-02 13:51:23 -0600756 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200757
758 srp->rq->timeout = timeout;
759 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
760 srp->rq, 1, sg_rq_end_io);
761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static int
765sg_srp_done(Sg_request *srp, Sg_fd *sfp)
766{
767 unsigned long iflags;
768 int done;
769
770 read_lock_irqsave(&sfp->rq_list_lock, iflags);
771 done = srp->done;
772 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
773 return done;
774}
775
776static int
777sg_ioctl(struct inode *inode, struct file *filp,
778 unsigned int cmd_in, unsigned long arg)
779{
780 void __user *p = (void __user *)arg;
781 int __user *ip = p;
782 int result, val, read_only;
783 Sg_device *sdp;
784 Sg_fd *sfp;
785 Sg_request *srp;
786 unsigned long iflags;
787
788 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
789 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
792 sdp->disk->disk_name, (int) cmd_in));
793 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
794
795 switch (cmd_in) {
796 case SG_IO:
797 {
798 int blocking = 1; /* ignore O_NONBLOCK flag */
799
800 if (sdp->detached)
801 return -ENODEV;
802 if (!scsi_block_when_processing_errors(sdp->device))
803 return -ENXIO;
804 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
805 return -EFAULT;
806 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200807 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 blocking, read_only, &srp);
809 if (result < 0)
810 return result;
811 srp->sg_io_owned = 1;
812 while (1) {
813 result = 0; /* following macro to beat race condition */
814 __wait_event_interruptible(sfp->read_wait,
815 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
816 result);
817 if (sdp->detached)
818 return -ENODEV;
819 if (sfp->closed)
820 return 0; /* request packet dropped already */
821 if (0 == result)
822 break;
823 srp->orphan = 1;
824 return result; /* -ERESTARTSYS because signal hit process */
825 }
826 write_lock_irqsave(&sfp->rq_list_lock, iflags);
827 srp->done = 2;
828 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
829 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
830 return (result < 0) ? result : 0;
831 }
832 case SG_SET_TIMEOUT:
833 result = get_user(val, ip);
834 if (result)
835 return result;
836 if (val < 0)
837 return -EIO;
838 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
839 val = MULDIV (INT_MAX, USER_HZ, HZ);
840 sfp->timeout_user = val;
841 sfp->timeout = MULDIV (val, HZ, USER_HZ);
842
843 return 0;
844 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
845 /* strange ..., for backward compatibility */
846 return sfp->timeout_user;
847 case SG_SET_FORCE_LOW_DMA:
848 result = get_user(val, ip);
849 if (result)
850 return result;
851 if (val) {
852 sfp->low_dma = 1;
853 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
854 val = (int) sfp->reserve.bufflen;
855 sg_remove_scat(&sfp->reserve);
856 sg_build_reserve(sfp, val);
857 }
858 } else {
859 if (sdp->detached)
860 return -ENODEV;
861 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
862 }
863 return 0;
864 case SG_GET_LOW_DMA:
865 return put_user((int) sfp->low_dma, ip);
866 case SG_GET_SCSI_ID:
867 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
868 return -EFAULT;
869 else {
870 sg_scsi_id_t __user *sg_idp = p;
871
872 if (sdp->detached)
873 return -ENODEV;
874 __put_user((int) sdp->device->host->host_no,
875 &sg_idp->host_no);
876 __put_user((int) sdp->device->channel,
877 &sg_idp->channel);
878 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
879 __put_user((int) sdp->device->lun, &sg_idp->lun);
880 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
881 __put_user((short) sdp->device->host->cmd_per_lun,
882 &sg_idp->h_cmd_per_lun);
883 __put_user((short) sdp->device->queue_depth,
884 &sg_idp->d_queue_depth);
885 __put_user(0, &sg_idp->unused[0]);
886 __put_user(0, &sg_idp->unused[1]);
887 return 0;
888 }
889 case SG_SET_FORCE_PACK_ID:
890 result = get_user(val, ip);
891 if (result)
892 return result;
893 sfp->force_packid = val ? 1 : 0;
894 return 0;
895 case SG_GET_PACK_ID:
896 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
897 return -EFAULT;
898 read_lock_irqsave(&sfp->rq_list_lock, iflags);
899 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
900 if ((1 == srp->done) && (!srp->sg_io_owned)) {
901 read_unlock_irqrestore(&sfp->rq_list_lock,
902 iflags);
903 __put_user(srp->header.pack_id, ip);
904 return 0;
905 }
906 }
907 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
908 __put_user(-1, ip);
909 return 0;
910 case SG_GET_NUM_WAITING:
911 read_lock_irqsave(&sfp->rq_list_lock, iflags);
912 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
913 if ((1 == srp->done) && (!srp->sg_io_owned))
914 ++val;
915 }
916 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
917 return put_user(val, ip);
918 case SG_GET_SG_TABLESIZE:
919 return put_user(sdp->sg_tablesize, ip);
920 case SG_SET_RESERVED_SIZE:
921 result = get_user(val, ip);
922 if (result)
923 return result;
924 if (val < 0)
925 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500926 val = min_t(int, val,
927 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (val != sfp->reserve.bufflen) {
929 if (sg_res_in_use(sfp) || sfp->mmap_called)
930 return -EBUSY;
931 sg_remove_scat(&sfp->reserve);
932 sg_build_reserve(sfp, val);
933 }
934 return 0;
935 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500936 val = min_t(int, sfp->reserve.bufflen,
937 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return put_user(val, ip);
939 case SG_SET_COMMAND_Q:
940 result = get_user(val, ip);
941 if (result)
942 return result;
943 sfp->cmd_q = val ? 1 : 0;
944 return 0;
945 case SG_GET_COMMAND_Q:
946 return put_user((int) sfp->cmd_q, ip);
947 case SG_SET_KEEP_ORPHAN:
948 result = get_user(val, ip);
949 if (result)
950 return result;
951 sfp->keep_orphan = val;
952 return 0;
953 case SG_GET_KEEP_ORPHAN:
954 return put_user((int) sfp->keep_orphan, ip);
955 case SG_NEXT_CMD_LEN:
956 result = get_user(val, ip);
957 if (result)
958 return result;
959 sfp->next_cmd_len = (val > 0) ? val : 0;
960 return 0;
961 case SG_GET_VERSION_NUM:
962 return put_user(sg_version_num, ip);
963 case SG_GET_ACCESS_COUNT:
964 /* faked - we don't have a real access count anymore */
965 val = (sdp->device ? 1 : 0);
966 return put_user(val, ip);
967 case SG_GET_REQUEST_TABLE:
968 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
969 return -EFAULT;
970 else {
cb59e842005-04-02 13:51:23 -0600971 sg_req_info_t *rinfo;
972 unsigned int ms;
973
974 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
975 GFP_KERNEL);
976 if (!rinfo)
977 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 read_lock_irqsave(&sfp->rq_list_lock, iflags);
979 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
980 ++val, srp = srp ? srp->nextrp : srp) {
981 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
982 if (srp) {
983 rinfo[val].req_state = srp->done + 1;
984 rinfo[val].problem =
985 srp->header.masked_status &
986 srp->header.host_status &
987 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600988 if (srp->done)
989 rinfo[val].duration =
990 srp->header.duration;
991 else {
992 ms = jiffies_to_msecs(jiffies);
993 rinfo[val].duration =
994 (ms > srp->header.duration) ?
995 (ms - srp->header.duration) : 0;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600998 rinfo[val].sg_io_owned =
999 srp->sg_io_owned;
1000 rinfo[val].pack_id =
1001 srp->header.pack_id;
1002 rinfo[val].usr_ptr =
1003 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 }
1006 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001007 result = __copy_to_user(p, rinfo,
1008 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1009 result = result ? -EFAULT : 0;
1010 kfree(rinfo);
1011 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 case SG_EMULATED_HOST:
1014 if (sdp->detached)
1015 return -ENODEV;
1016 return put_user(sdp->device->host->hostt->emulated, ip);
1017 case SG_SCSI_RESET:
1018 if (sdp->detached)
1019 return -ENODEV;
1020 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001021 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -EBUSY;
1023 } else if (!scsi_block_when_processing_errors(sdp->device))
1024 return -EBUSY;
1025 result = get_user(val, ip);
1026 if (result)
1027 return result;
1028 if (SG_SCSI_RESET_NOTHING == val)
1029 return 0;
1030 switch (val) {
1031 case SG_SCSI_RESET_DEVICE:
1032 val = SCSI_TRY_RESET_DEVICE;
1033 break;
Brian King39120e12008-07-01 13:03:19 -05001034 case SG_SCSI_RESET_TARGET:
1035 val = SCSI_TRY_RESET_TARGET;
1036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 case SG_SCSI_RESET_BUS:
1038 val = SCSI_TRY_RESET_BUS;
1039 break;
1040 case SG_SCSI_RESET_HOST:
1041 val = SCSI_TRY_RESET_HOST;
1042 break;
1043 default:
1044 return -EINVAL;
1045 }
1046 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1047 return -EACCES;
1048 return (scsi_reset_provider(sdp->device, val) ==
1049 SUCCESS) ? 0 : -EIO;
1050 case SCSI_IOCTL_SEND_COMMAND:
1051 if (sdp->detached)
1052 return -ENODEV;
1053 if (read_only) {
1054 unsigned char opcode = WRITE_6;
1055 Scsi_Ioctl_Command __user *siocp = p;
1056
1057 if (copy_from_user(&opcode, siocp->data, 1))
1058 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001059 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -EPERM;
1061 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001062 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 case SG_SET_DEBUG:
1064 result = get_user(val, ip);
1065 if (result)
1066 return result;
1067 sdp->sgdebug = (char) val;
1068 return 0;
1069 case SCSI_IOCTL_GET_IDLUN:
1070 case SCSI_IOCTL_GET_BUS_NUMBER:
1071 case SCSI_IOCTL_PROBE_HOST:
1072 case SG_GET_TRANSFORM:
1073 if (sdp->detached)
1074 return -ENODEV;
1075 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001076 case BLKSECTGET:
1077 return put_user(sdp->device->request_queue->max_sectors * 512,
1078 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001079 case BLKTRACESETUP:
1080 return blk_trace_setup(sdp->device->request_queue,
1081 sdp->disk->disk_name,
1082 sdp->device->sdev_gendev.devt,
1083 (char *)arg);
1084 case BLKTRACESTART:
1085 return blk_trace_startstop(sdp->device->request_queue, 1);
1086 case BLKTRACESTOP:
1087 return blk_trace_startstop(sdp->device->request_queue, 0);
1088 case BLKTRACETEARDOWN:
1089 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 default:
1091 if (read_only)
1092 return -EPERM; /* don't know so take safe approach */
1093 return scsi_ioctl(sdp->device, cmd_in, p);
1094 }
1095}
1096
1097#ifdef CONFIG_COMPAT
1098static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1099{
1100 Sg_device *sdp;
1101 Sg_fd *sfp;
1102 struct scsi_device *sdev;
1103
1104 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1105 return -ENXIO;
1106
1107 sdev = sdp->device;
1108 if (sdev->host->hostt->compat_ioctl) {
1109 int ret;
1110
1111 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1112
1113 return ret;
1114 }
1115
1116 return -ENOIOCTLCMD;
1117}
1118#endif
1119
1120static unsigned int
1121sg_poll(struct file *filp, poll_table * wait)
1122{
1123 unsigned int res = 0;
1124 Sg_device *sdp;
1125 Sg_fd *sfp;
1126 Sg_request *srp;
1127 int count = 0;
1128 unsigned long iflags;
1129
1130 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1131 || sfp->closed)
1132 return POLLERR;
1133 poll_wait(filp, &sfp->read_wait, wait);
1134 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1135 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1136 /* if any read waiting, flag it */
1137 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1138 res = POLLIN | POLLRDNORM;
1139 ++count;
1140 }
1141 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1142
1143 if (sdp->detached)
1144 res |= POLLHUP;
1145 else if (!sfp->cmd_q) {
1146 if (0 == count)
1147 res |= POLLOUT | POLLWRNORM;
1148 } else if (count < SG_MAX_QUEUE)
1149 res |= POLLOUT | POLLWRNORM;
1150 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1151 sdp->disk->disk_name, (int) res));
1152 return res;
1153}
1154
1155static int
1156sg_fasync(int fd, struct file *filp, int mode)
1157{
1158 int retval;
1159 Sg_device *sdp;
1160 Sg_fd *sfp;
1161
1162 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1163 return -ENXIO;
1164 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1165 sdp->disk->disk_name, mode));
1166
1167 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1168 return (retval < 0) ? retval : 0;
1169}
1170
Nick Piggina13ff0b2008-02-07 18:46:06 -08001171static int
1172sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001175 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001177 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001180 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001182 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001184 return VM_FAULT_SIGBUS;
1185 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001187 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001188 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1189 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001190 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001191 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001192 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001193 struct page *page = nth_page(rsv_schp->pages[k],
1194 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001195 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001196 vmf->page = page;
1197 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Mike Christied6b10342005-11-08 04:06:41 -06001199 sa += len;
1200 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Mike Christied6b10342005-11-08 04:06:41 -06001202
Nick Piggina13ff0b2008-02-07 18:46:06 -08001203 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001207 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208};
1209
1210static int
1211sg_mmap(struct file *filp, struct vm_area_struct *vma)
1212{
1213 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001214 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001216 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1219 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001220 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1222 (void *) vma->vm_start, (int) req_sz));
1223 if (vma->vm_pgoff)
1224 return -EINVAL; /* want no offset */
1225 rsv_schp = &sfp->reserve;
1226 if (req_sz > rsv_schp->bufflen)
1227 return -ENOMEM; /* cannot map more than reserved buffer */
1228
Mike Christied6b10342005-11-08 04:06:41 -06001229 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001230 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1231 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001232 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001233 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001234 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Mike Christied6b10342005-11-08 04:06:41 -06001236
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001237 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001238 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 vma->vm_private_data = sfp;
1240 vma->vm_ops = &sg_mmap_vm_ops;
1241 return 0;
1242}
1243
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001244/*
1245 * This function is a "bottom half" handler that is called by the mid
1246 * level when a command is completed (or has failed).
1247 */
1248static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001250 struct sg_request *srp = rq->end_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 Sg_device *sdp = NULL;
1252 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001254 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001255 char *sense;
1256 int result, resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (NULL == srp) {
1259 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return;
1261 }
1262 sfp = srp->parentfp;
1263 if (sfp)
1264 sdp = sfp->parentdp;
1265 if ((NULL == sdp) || sdp->detached) {
1266 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return;
1268 }
1269
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001270 sense = rq->sense;
1271 result = rq->errors;
1272 resid = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001275 sdp->disk->disk_name, srp->header.pack_id, result));
1276 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001277 ms = jiffies_to_msecs(jiffies);
1278 srp->header.duration = (ms > srp->header.duration) ?
1279 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001280 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct scsi_sense_hdr sshdr;
1282
Mike Christied6b10342005-11-08 04:06:41 -06001283 srp->header.status = 0xff & result;
1284 srp->header.masked_status = status_byte(result);
1285 srp->header.msg_status = msg_byte(result);
1286 srp->header.host_status = host_byte(result);
1287 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if ((sdp->sgdebug > 0) &&
1289 ((CHECK_CONDITION == srp->header.masked_status) ||
1290 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001291 __scsi_print_sense("sg_cmd_done", sense,
1292 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001295 if (driver_byte(result) != 0
1296 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 && !scsi_sense_is_deferred(&sshdr)
1298 && sshdr.sense_key == UNIT_ATTENTION
1299 && sdp->device->removable) {
1300 /* Detected possible disc change. Set the bit - this */
1301 /* may be used if there are filesystems using this device */
1302 sdp->device->changed = 1;
1303 }
1304 }
1305 /* Rely on write phase to clean out srp status values, so no "else" */
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (sfp->closed) { /* whoops this fd already released, cleanup */
1308 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1309 sg_finish_rem_req(srp);
1310 srp = NULL;
1311 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001312 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1314 scsi_device_put(sdp->device);
1315 }
1316 sfp = NULL;
1317 }
1318 } else if (srp && srp->orphan) {
1319 if (sfp->keep_orphan)
1320 srp->sg_io_owned = 0;
1321 else {
1322 sg_finish_rem_req(srp);
1323 srp = NULL;
1324 }
1325 }
1326 if (sfp && srp) {
1327 /* Now wake up any sg_read() that is waiting for this packet. */
1328 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1329 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1330 srp->done = 1;
1331 wake_up_interruptible(&sfp->read_wait);
1332 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1333 }
1334}
1335
1336static struct file_operations sg_fops = {
1337 .owner = THIS_MODULE,
1338 .read = sg_read,
1339 .write = sg_write,
1340 .poll = sg_poll,
1341 .ioctl = sg_ioctl,
1342#ifdef CONFIG_COMPAT
1343 .compat_ioctl = sg_compat_ioctl,
1344#endif
1345 .open = sg_open,
1346 .mmap = sg_mmap,
1347 .release = sg_release,
1348 .fasync = sg_fasync,
1349};
1350
gregkh@suse.ded2538782005-03-23 09:55:22 -08001351static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353static int sg_sysfs_valid = 0;
1354
James Bottomley7c07d612007-08-05 13:36:11 -05001355static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Mike Christied6b10342005-11-08 04:06:41 -06001357 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 Sg_device *sdp;
1359 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001360 int error;
1361 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jes Sorensen24669f752006-01-16 10:31:18 -05001363 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (!sdp) {
1365 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001366 return ERR_PTR(-ENOMEM);
1367 }
1368 error = -ENOMEM;
1369 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1370 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
James Bottomley7c07d612007-08-05 13:36:11 -05001374 write_lock_irqsave(&sg_index_lock, iflags);
1375 error = idr_get_new(&sg_index_idr, sdp, &k);
1376 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
James Bottomley7c07d612007-08-05 13:36:11 -05001378 if (error) {
1379 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1380 error);
1381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (unlikely(k >= SG_MAX_DEVS))
1385 goto overflow;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1388 sprintf(disk->disk_name, "sg%d", k);
1389 disk->first_minor = k;
1390 sdp->disk = disk;
1391 sdp->device = scsidp;
1392 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001393 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001394 sdp->index = k;
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:
James Bottomley9ccfc752005-10-02 11:45:08 -05001405 sdev_printk(KERN_WARNING, scsidp,
1406 "Unable to attach sg device type=%d, minor "
1407 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 error = -ENODEV;
1409 goto out;
1410}
1411
1412static int
Tony Jonesee959b02008-02-22 00:13:36 +01001413sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Tony Jonesee959b02008-02-22 00:13:36 +01001415 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 struct gendisk *disk;
1417 Sg_device *sdp = NULL;
1418 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001419 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001420 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 disk = alloc_disk(1);
1423 if (!disk) {
1424 printk(KERN_WARNING "alloc_disk failed\n");
1425 return -ENOMEM;
1426 }
1427 disk->major = SCSI_GENERIC_MAJOR;
1428
1429 error = -ENOMEM;
1430 cdev = cdev_alloc();
1431 if (!cdev) {
1432 printk(KERN_WARNING "cdev_alloc failed\n");
1433 goto out;
1434 }
1435 cdev->owner = THIS_MODULE;
1436 cdev->ops = &sg_fops;
1437
James Bottomley7c07d612007-08-05 13:36:11 -05001438 sdp = sg_alloc(disk, scsidp);
1439 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001441 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto out;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
James Bottomley7c07d612007-08-05 13:36:11 -05001445 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001446 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001447 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 sdp->cdev = cdev;
1450 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001451 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001453 sg_class_member = device_create_drvdata(sg_sysfs_class,
1454 cl_dev->parent,
1455 MKDEV(SCSI_GENERIC_MAJOR,
1456 sdp->index),
1457 sdp,
1458 "%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
1494static void
Tony Jonesee959b02008-02-22 00:13:36 +01001495sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Tony Jonesee959b02008-02-22 00:13:36 +01001497 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1498 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 unsigned long iflags;
1500 Sg_fd *sfp;
1501 Sg_fd *tsfp;
1502 Sg_request *srp;
1503 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001504 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
James Bottomley7c07d612007-08-05 13:36:11 -05001506 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
James Bottomley7c07d612007-08-05 13:36:11 -05001509 delay = 0;
1510 write_lock_irqsave(&sg_index_lock, iflags);
1511 if (sdp->headfp) {
1512 sdp->detached = 1;
1513 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1514 tsfp = sfp->nextfp;
1515 for (srp = sfp->headrp; srp; srp = tsrp) {
1516 tsrp = srp->nextrp;
1517 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1518 sg_finish_rem_req(srp);
1519 }
1520 if (sfp->closed) {
1521 scsi_device_put(sdp->device);
1522 __sg_remove_sfp(sdp, sfp);
1523 } else {
1524 delay = 1;
1525 wake_up_interruptible(&sfp->read_wait);
1526 kill_fasync(&sfp->async_qp, SIGPOLL,
1527 POLL_HUP);
1528 }
1529 }
1530 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1531 if (NULL == sdp->headfp) {
1532 idr_remove(&sg_index_idr, sdp->index);
1533 }
1534 } else { /* nothing active, simple case */
1535 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1536 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
James Bottomley7c07d612007-08-05 13:36:11 -05001538 write_unlock_irqrestore(&sg_index_lock, iflags);
1539
1540 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001541 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001542 cdev_del(sdp->cdev);
1543 sdp->cdev = NULL;
1544 put_disk(sdp->disk);
1545 sdp->disk = NULL;
1546 if (NULL == sdp->headfp)
1547 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (delay)
1550 msleep(10); /* dirty detach so delay device destruction */
1551}
1552
Douglas Gilbert6460e752006-09-20 18:20:49 -04001553module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1554module_param_named(def_reserved_size, def_reserved_size, int,
1555 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1557
1558MODULE_AUTHOR("Douglas Gilbert");
1559MODULE_DESCRIPTION("SCSI generic (sg) driver");
1560MODULE_LICENSE("GPL");
1561MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001562MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Douglas Gilbert6460e752006-09-20 18:20:49 -04001564MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1565 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1567MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1568
1569static int __init
1570init_sg(void)
1571{
1572 int rc;
1573
Douglas Gilbert6460e752006-09-20 18:20:49 -04001574 if (scatter_elem_sz < PAGE_SIZE) {
1575 scatter_elem_sz = PAGE_SIZE;
1576 scatter_elem_sz_prev = scatter_elem_sz;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (def_reserved_size >= 0)
1579 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001580 else
1581 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1584 SG_MAX_DEVS, "sg");
1585 if (rc)
1586 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001587 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if ( IS_ERR(sg_sysfs_class) ) {
1589 rc = PTR_ERR(sg_sysfs_class);
1590 goto err_out;
1591 }
1592 sg_sysfs_valid = 1;
1593 rc = scsi_register_interface(&sg_interface);
1594 if (0 == rc) {
1595#ifdef CONFIG_SCSI_PROC_FS
1596 sg_proc_init();
1597#endif /* CONFIG_SCSI_PROC_FS */
1598 return 0;
1599 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001600 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601err_out:
1602 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1603 return rc;
1604}
1605
1606static void __exit
1607exit_sg(void)
1608{
1609#ifdef CONFIG_SCSI_PROC_FS
1610 sg_proc_cleanup();
1611#endif /* CONFIG_SCSI_PROC_FS */
1612 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001613 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 sg_sysfs_valid = 0;
1615 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1616 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001617 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001620static int sg_start_req(Sg_request *srp, unsigned char *cmd)
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001621{
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001622 int res;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001623 struct request *rq;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001624 Sg_fd *sfp = srp->parentfp;
1625 sg_io_hdr_t *hp = &srp->header;
1626 int dxfer_len = (int) hp->dxfer_len;
1627 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001628 unsigned int iov_count = hp->iovec_count;
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001629 Sg_scatter_hold *req_schp = &srp->data;
1630 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1631 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001632 struct rq_map_data *md, map_data;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001633 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1634
FUJITA Tomonori44c7b0e2008-09-02 22:50:04 +09001635 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1636 dxfer_len));
1637
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001638 rq = blk_get_request(q, rw, GFP_ATOMIC);
1639 if (!rq)
1640 return -ENOMEM;
1641
1642 memcpy(rq->cmd, cmd, hp->cmd_len);
1643
1644 rq->cmd_len = hp->cmd_len;
1645 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1646
1647 srp->rq = rq;
1648 rq->end_io_data = srp;
1649 rq->sense = srp->sense_b;
1650 rq->retries = SG_DEFAULT_RETRIES;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001653 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001654
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001655 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1656 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1657 !sfp->parentdp->device->host->unchecked_isa_dma &&
FUJITA Tomonori01cfcdd2008-08-28 15:05:59 +09001658 blk_rq_aligned(q, hp->dxferp, dxfer_len))
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001659 md = NULL;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001660 else
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001661 md = &map_data;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001662
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001663 if (md) {
1664 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1665 sg_link_reserve(sfp, srp, dxfer_len);
1666 else {
1667 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1668 if (res)
1669 return res;
1670 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001671
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001672 md->pages = req_schp->pages;
1673 md->page_order = req_schp->page_order;
1674 md->nr_entries = req_schp->k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001676
FUJITA Tomonori626710c2008-09-02 22:50:05 +09001677 if (iov_count)
1678 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1679 hp->dxfer_len, GFP_ATOMIC);
1680 else
1681 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1682 hp->dxfer_len, GFP_ATOMIC);
1683
1684 if (!res) {
1685 srp->bio = rq->bio;
1686
1687 if (!md) {
1688 req_schp->dio_in_use = 1;
1689 hp->info |= SG_INFO_DIRECT_IO;
1690 }
1691 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001692 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695static void
1696sg_finish_rem_req(Sg_request * srp)
1697{
1698 Sg_fd *sfp = srp->parentfp;
1699 Sg_scatter_hold *req_schp = &srp->data;
1700
1701 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1702 if (srp->res_used)
1703 sg_unlink_reserve(sfp, srp);
1704 else
1705 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001706
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001707 if (srp->rq) {
1708 if (srp->bio)
1709 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001710
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001711 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001712 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 sg_remove_request(sfp, srp);
1715}
1716
1717static int
1718sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1719{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001720 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001721 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001723 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1724 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001727 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730static int
1731sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1732{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001733 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001734 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001735 int blk_size = buff_size, order;
1736 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Eric Sesterhennfb119932007-05-23 14:41:36 -07001738 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EFAULT;
1740 if (0 == blk_size)
1741 ++blk_size; /* don't know why */
1742/* round request up to next highest SG_SECTOR_SZ byte boundary */
1743 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1744 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1745 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001746
1747 /* N.B. ret_sz carried into this block ... */
1748 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1749 if (mx_sc_elems < 0)
1750 return mx_sc_elems; /* most likely -ENOMEM */
1751
Douglas Gilbert6460e752006-09-20 18:20:49 -04001752 num = scatter_elem_sz;
1753 if (unlikely(num != scatter_elem_sz_prev)) {
1754 if (num < PAGE_SIZE) {
1755 scatter_elem_sz = PAGE_SIZE;
1756 scatter_elem_sz_prev = PAGE_SIZE;
1757 } else
1758 scatter_elem_sz_prev = num;
1759 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001760
1761 if (sfp->low_dma)
1762 gfp_mask |= GFP_DMA;
1763
1764 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1765 gfp_mask |= __GFP_ZERO;
1766
1767 order = get_order(num);
1768retry:
1769 ret_sz = 1 << (PAGE_SHIFT + order);
1770
1771 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1772 k++, rem_sz -= ret_sz) {
1773
Douglas Gilbert6460e752006-09-20 18:20:49 -04001774 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001775 scatter_elem_sz_prev : rem_sz;
1776
1777 schp->pages[k] = alloc_pages(gfp_mask, order);
1778 if (!schp->pages[k])
1779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Douglas Gilbert6460e752006-09-20 18:20:49 -04001781 if (num == scatter_elem_sz_prev) {
1782 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1783 scatter_elem_sz = ret_sz;
1784 scatter_elem_sz_prev = ret_sz;
1785 }
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001788 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1789 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001790 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001792 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001793 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001794 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1795 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001796
1797 schp->bufflen = blk_size;
1798 if (rem_sz > 0) /* must have failed */
1799 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001801out:
1802 for (i = 0; i < k; i++)
1803 __free_pages(schp->pages[k], order);
1804
1805 if (--order >= 0)
1806 goto retry;
1807
1808 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811static void
1812sg_remove_scat(Sg_scatter_hold * schp)
1813{
1814 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001815 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001816 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int k;
1818
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001819 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001821 "sg_remove_scat: k=%d, pg=0x%p\n",
1822 k, schp->pages[k]));
1823 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001825
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001826 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Mike Christied6b10342005-11-08 04:06:41 -06001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 memset(schp, 0, sizeof (*schp));
1830}
1831
1832static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1834{
1835 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001836 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1839 num_read_xfer));
1840 if ((!outp) || (num_read_xfer <= 0))
1841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001843 num = 1 << (PAGE_SHIFT + schp->page_order);
1844 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001845 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001846 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001847 num_read_xfer))
1848 return -EFAULT;
1849 break;
1850 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001851 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001852 num))
1853 return -EFAULT;
1854 num_read_xfer -= num;
1855 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
Mike Christied6b10342005-11-08 04:06:41 -06001857 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Mike Christied6b10342005-11-08 04:06:41 -06001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
1863
1864static void
1865sg_build_reserve(Sg_fd * sfp, int req_size)
1866{
1867 Sg_scatter_hold *schp = &sfp->reserve;
1868
1869 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1870 do {
1871 if (req_size < PAGE_SIZE)
1872 req_size = PAGE_SIZE;
1873 if (0 == sg_build_indirect(schp, sfp, req_size))
1874 return;
1875 else
1876 sg_remove_scat(schp);
1877 req_size >>= 1; /* divide by 2 */
1878 } while (req_size > (PAGE_SIZE / 2));
1879}
1880
1881static void
1882sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1883{
1884 Sg_scatter_hold *req_schp = &srp->data;
1885 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001886 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 srp->res_used = 1;
1889 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001890 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001892 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1893 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001894 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001895 req_schp->k_use_sg = k + 1;
1896 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001897 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001898
1899 req_schp->bufflen = size;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001900 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001901 break;
1902 } else
1903 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Mike Christied6b10342005-11-08 04:06:41 -06001905
1906 if (k >= rsv_schp->k_use_sg)
1907 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910static void
1911sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1912{
1913 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1916 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 req_schp->k_use_sg = 0;
1918 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001919 req_schp->pages = NULL;
1920 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 req_schp->sglist_len = 0;
1922 sfp->save_scat_len = 0;
1923 srp->res_used = 0;
1924}
1925
1926static Sg_request *
1927sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1928{
1929 Sg_request *resp;
1930 unsigned long iflags;
1931
1932 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1933 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1934 /* look for requests that are ready + not SG_IO owned */
1935 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1936 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1937 resp->done = 2; /* guard against other readers */
1938 break;
1939 }
1940 }
1941 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1942 return resp;
1943}
1944
1945#ifdef CONFIG_SCSI_PROC_FS
1946static Sg_request *
1947sg_get_nth_request(Sg_fd * sfp, int nth)
1948{
1949 Sg_request *resp;
1950 unsigned long iflags;
1951 int k;
1952
1953 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1954 for (k = 0, resp = sfp->headrp; resp && (k < nth);
1955 ++k, resp = resp->nextrp) ;
1956 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1957 return resp;
1958}
1959#endif
1960
1961/* always adds to end of list */
1962static Sg_request *
1963sg_add_request(Sg_fd * sfp)
1964{
1965 int k;
1966 unsigned long iflags;
1967 Sg_request *resp;
1968 Sg_request *rp = sfp->req_arr;
1969
1970 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1971 resp = sfp->headrp;
1972 if (!resp) {
1973 memset(rp, 0, sizeof (Sg_request));
1974 rp->parentfp = sfp;
1975 resp = rp;
1976 sfp->headrp = resp;
1977 } else {
1978 if (0 == sfp->cmd_q)
1979 resp = NULL; /* command queuing disallowed */
1980 else {
1981 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1982 if (!rp->parentfp)
1983 break;
1984 }
1985 if (k < SG_MAX_QUEUE) {
1986 memset(rp, 0, sizeof (Sg_request));
1987 rp->parentfp = sfp;
1988 while (resp->nextrp)
1989 resp = resp->nextrp;
1990 resp->nextrp = rp;
1991 resp = rp;
1992 } else
1993 resp = NULL;
1994 }
1995 }
1996 if (resp) {
1997 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06001998 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2001 return resp;
2002}
2003
2004/* Return of 1 for found; 0 for not found */
2005static int
2006sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2007{
2008 Sg_request *prev_rp;
2009 Sg_request *rp;
2010 unsigned long iflags;
2011 int res = 0;
2012
2013 if ((!sfp) || (!srp) || (!sfp->headrp))
2014 return res;
2015 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2016 prev_rp = sfp->headrp;
2017 if (srp == prev_rp) {
2018 sfp->headrp = prev_rp->nextrp;
2019 prev_rp->parentfp = NULL;
2020 res = 1;
2021 } else {
2022 while ((rp = prev_rp->nextrp)) {
2023 if (srp == rp) {
2024 prev_rp->nextrp = rp->nextrp;
2025 rp->parentfp = NULL;
2026 res = 1;
2027 break;
2028 }
2029 prev_rp = rp;
2030 }
2031 }
2032 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2033 return res;
2034}
2035
2036#ifdef CONFIG_SCSI_PROC_FS
2037static Sg_fd *
2038sg_get_nth_sfp(Sg_device * sdp, int nth)
2039{
2040 Sg_fd *resp;
2041 unsigned long iflags;
2042 int k;
2043
James Bottomley7c07d612007-08-05 13:36:11 -05002044 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2046 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002047 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return resp;
2049}
2050#endif
2051
2052static Sg_fd *
2053sg_add_sfp(Sg_device * sdp, int dev)
2054{
2055 Sg_fd *sfp;
2056 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002057 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Mike Christied6b10342005-11-08 04:06:41 -06002059 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (!sfp)
2061 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 init_waitqueue_head(&sfp->read_wait);
2064 rwlock_init(&sfp->rq_list_lock);
2065
2066 sfp->timeout = SG_DEFAULT_TIMEOUT;
2067 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2068 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2069 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2070 sdp->device->host->unchecked_isa_dma : 1;
2071 sfp->cmd_q = SG_DEF_COMMAND_Q;
2072 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2073 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002074 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!sdp->headfp)
2076 sdp->headfp = sfp;
2077 else { /* add to tail of existing list */
2078 Sg_fd *pfp = sdp->headfp;
2079 while (pfp->nextfp)
2080 pfp = pfp->nextfp;
2081 pfp->nextfp = sfp;
2082 }
James Bottomley7c07d612007-08-05 13:36:11 -05002083 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002085 if (unlikely(sg_big_buff != def_reserved_size))
2086 sg_big_buff = def_reserved_size;
2087
Alan Stern44ec9542007-02-20 11:01:57 -05002088 bufflen = min_t(int, sg_big_buff,
2089 sdp->device->request_queue->max_sectors * 512);
2090 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2092 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2093 return sfp;
2094}
2095
2096static void
2097__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2098{
2099 Sg_fd *fp;
2100 Sg_fd *prev_fp;
2101
2102 prev_fp = sdp->headfp;
2103 if (sfp == prev_fp)
2104 sdp->headfp = prev_fp->nextfp;
2105 else {
2106 while ((fp = prev_fp->nextfp)) {
2107 if (sfp == fp) {
2108 prev_fp->nextfp = fp->nextfp;
2109 break;
2110 }
2111 prev_fp = fp;
2112 }
2113 }
2114 if (sfp->reserve.bufflen > 0) {
2115 SCSI_LOG_TIMEOUT(6,
2116 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2117 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 sg_remove_scat(&sfp->reserve);
2119 }
2120 sfp->parentdp = NULL;
2121 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002122 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125/* Returns 0 in normal case, 1 when detached and sdp object removed */
2126static int
2127sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2128{
2129 Sg_request *srp;
2130 Sg_request *tsrp;
2131 int dirty = 0;
2132 int res = 0;
2133
2134 for (srp = sfp->headrp; srp; srp = tsrp) {
2135 tsrp = srp->nextrp;
2136 if (sg_srp_done(srp, sfp))
2137 sg_finish_rem_req(srp);
2138 else
2139 ++dirty;
2140 }
2141 if (0 == dirty) {
2142 unsigned long iflags;
2143
James Bottomley7c07d612007-08-05 13:36:11 -05002144 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 __sg_remove_sfp(sdp, sfp);
2146 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002147 idr_remove(&sg_index_idr, sdp->index);
2148 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 res = 1;
2150 }
James Bottomley7c07d612007-08-05 13:36:11 -05002151 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 } else {
2153 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2154 /* only bump the access_count if we actually succeeded in
2155 * throwing another counter on the host module */
2156 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2157 sfp->closed = 1; /* flag dirty state on this fd */
2158 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2159 dirty));
2160 }
2161 return res;
2162}
2163
2164static int
2165sg_res_in_use(Sg_fd * sfp)
2166{
2167 const Sg_request *srp;
2168 unsigned long iflags;
2169
2170 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2171 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2172 if (srp->res_used)
2173 break;
2174 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2175 return srp ? 1 : 0;
2176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178#ifdef CONFIG_SCSI_PROC_FS
2179static int
James Bottomley7c07d612007-08-05 13:36:11 -05002180sg_idr_max_id(int id, void *p, void *data)
2181{
2182 int *k = data;
2183
2184 if (*k < id)
2185 *k = id;
2186
2187 return 0;
2188}
2189
2190static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191sg_last_dev(void)
2192{
Tony Battersby53474c02008-01-22 15:25:49 -05002193 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 unsigned long iflags;
2195
James Bottomley7c07d612007-08-05 13:36:11 -05002196 read_lock_irqsave(&sg_index_lock, iflags);
2197 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2198 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return k + 1; /* origin 1 */
2200}
2201#endif
2202
2203static Sg_device *
2204sg_get_dev(int dev)
2205{
James Bottomley7c07d612007-08-05 13:36:11 -05002206 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 unsigned long iflags;
2208
James Bottomley7c07d612007-08-05 13:36:11 -05002209 read_lock_irqsave(&sg_index_lock, iflags);
2210 sdp = idr_find(&sg_index_idr, dev);
2211 read_unlock_irqrestore(&sg_index_lock, iflags);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return sdp;
2214}
2215
2216#ifdef CONFIG_SCSI_PROC_FS
2217
2218static struct proc_dir_entry *sg_proc_sgp = NULL;
2219
2220static char sg_proc_sg_dirname[] = "scsi/sg";
2221
2222static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2223
2224static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2225static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2226 size_t count, loff_t *off);
2227static struct file_operations adio_fops = {
2228 /* .owner, .read and .llseek added in sg_proc_init() */
2229 .open = sg_proc_single_open_adio,
2230 .write = sg_proc_write_adio,
2231 .release = single_release,
2232};
2233
2234static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2235static ssize_t sg_proc_write_dressz(struct file *filp,
2236 const char __user *buffer, size_t count, loff_t *off);
2237static struct file_operations dressz_fops = {
2238 .open = sg_proc_single_open_dressz,
2239 .write = sg_proc_write_dressz,
2240 .release = single_release,
2241};
2242
2243static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2244static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2245static struct file_operations version_fops = {
2246 .open = sg_proc_single_open_version,
2247 .release = single_release,
2248};
2249
2250static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2251static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2252static struct file_operations devhdr_fops = {
2253 .open = sg_proc_single_open_devhdr,
2254 .release = single_release,
2255};
2256
2257static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2258static int sg_proc_open_dev(struct inode *inode, struct file *file);
2259static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2260static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2261static void dev_seq_stop(struct seq_file *s, void *v);
2262static struct file_operations dev_fops = {
2263 .open = sg_proc_open_dev,
2264 .release = seq_release,
2265};
2266static struct seq_operations dev_seq_ops = {
2267 .start = dev_seq_start,
2268 .next = dev_seq_next,
2269 .stop = dev_seq_stop,
2270 .show = sg_proc_seq_show_dev,
2271};
2272
2273static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2274static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2275static struct file_operations devstrs_fops = {
2276 .open = sg_proc_open_devstrs,
2277 .release = seq_release,
2278};
2279static struct seq_operations devstrs_seq_ops = {
2280 .start = dev_seq_start,
2281 .next = dev_seq_next,
2282 .stop = dev_seq_stop,
2283 .show = sg_proc_seq_show_devstrs,
2284};
2285
2286static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2287static int sg_proc_open_debug(struct inode *inode, struct file *file);
2288static struct file_operations debug_fops = {
2289 .open = sg_proc_open_debug,
2290 .release = seq_release,
2291};
2292static struct seq_operations debug_seq_ops = {
2293 .start = dev_seq_start,
2294 .next = dev_seq_next,
2295 .stop = dev_seq_stop,
2296 .show = sg_proc_seq_show_debug,
2297};
2298
2299
2300struct sg_proc_leaf {
2301 const char * name;
2302 struct file_operations * fops;
2303};
2304
2305static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2306 {"allow_dio", &adio_fops},
2307 {"debug", &debug_fops},
2308 {"def_reserved_size", &dressz_fops},
2309 {"device_hdr", &devhdr_fops},
2310 {"devices", &dev_fops},
2311 {"device_strs", &devstrs_fops},
2312 {"version", &version_fops}
2313};
2314
2315static int
2316sg_proc_init(void)
2317{
2318 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002319 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 struct sg_proc_leaf * leaf;
2321
Al Viro66600222005-09-28 22:32:57 +01002322 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!sg_proc_sgp)
2324 return 1;
2325 for (k = 0; k < num_leaves; ++k) {
2326 leaf = &sg_proc_leaf_arr[k];
2327 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002328 leaf->fops->owner = THIS_MODULE;
2329 leaf->fops->read = seq_read;
2330 leaf->fops->llseek = seq_lseek;
2331 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333 return 0;
2334}
2335
2336static void
2337sg_proc_cleanup(void)
2338{
2339 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002340 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 if (!sg_proc_sgp)
2343 return;
2344 for (k = 0; k < num_leaves; ++k)
2345 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2346 remove_proc_entry(sg_proc_sg_dirname, NULL);
2347}
2348
2349
2350static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2351{
2352 seq_printf(s, "%d\n", *((int *)s->private));
2353 return 0;
2354}
2355
2356static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2357{
2358 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2359}
2360
2361static ssize_t
2362sg_proc_write_adio(struct file *filp, const char __user *buffer,
2363 size_t count, loff_t *off)
2364{
2365 int num;
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 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2375 return count;
2376}
2377
2378static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2379{
2380 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2381}
2382
2383static ssize_t
2384sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2385 size_t count, loff_t *off)
2386{
2387 int num;
2388 unsigned long k = ULONG_MAX;
2389 char buff[11];
2390
2391 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2392 return -EACCES;
2393 num = (count < 10) ? count : 10;
2394 if (copy_from_user(buff, buffer, num))
2395 return -EFAULT;
2396 buff[num] = '\0';
2397 k = simple_strtoul(buff, NULL, 10);
2398 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2399 sg_big_buff = k;
2400 return count;
2401 }
2402 return -ERANGE;
2403}
2404
2405static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2406{
2407 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2408 sg_version_date);
2409 return 0;
2410}
2411
2412static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2413{
2414 return single_open(file, sg_proc_seq_show_version, NULL);
2415}
2416
2417static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2418{
2419 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2420 "online\n");
2421 return 0;
2422}
2423
2424static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2425{
2426 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2427}
2428
2429struct sg_proc_deviter {
2430 loff_t index;
2431 size_t max;
2432};
2433
2434static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2435{
2436 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2437
Jan Blunck729d70f2005-08-27 11:07:52 -07002438 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (! it)
2440 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 it->index = *pos;
2443 it->max = sg_last_dev();
2444 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
2449static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2450{
Jan Blunck729d70f2005-08-27 11:07:52 -07002451 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 *pos = ++it->index;
2454 return (it->index < it->max) ? it : NULL;
2455}
2456
2457static void dev_seq_stop(struct seq_file *s, void *v)
2458{
Jan Blunck729d70f2005-08-27 11:07:52 -07002459 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
2462static int sg_proc_open_dev(struct inode *inode, struct file *file)
2463{
2464 return seq_open(file, &dev_seq_ops);
2465}
2466
2467static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2468{
2469 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2470 Sg_device *sdp;
2471 struct scsi_device *scsidp;
2472
2473 sdp = it ? sg_get_dev(it->index) : NULL;
2474 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2475 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2476 scsidp->host->host_no, scsidp->channel,
2477 scsidp->id, scsidp->lun, (int) scsidp->type,
2478 1,
2479 (int) scsidp->queue_depth,
2480 (int) scsidp->device_busy,
2481 (int) scsi_device_online(scsidp));
2482 else
2483 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2484 return 0;
2485}
2486
2487static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2488{
2489 return seq_open(file, &devstrs_seq_ops);
2490}
2491
2492static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2493{
2494 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2495 Sg_device *sdp;
2496 struct scsi_device *scsidp;
2497
2498 sdp = it ? sg_get_dev(it->index) : NULL;
2499 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2500 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2501 scsidp->vendor, scsidp->model, scsidp->rev);
2502 else
2503 seq_printf(s, "<no active device>\n");
2504 return 0;
2505}
2506
2507static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2508{
2509 int k, m, new_interface, blen, usg;
2510 Sg_request *srp;
2511 Sg_fd *fp;
2512 const sg_io_hdr_t *hp;
2513 const char * cp;
cb59e842005-04-02 13:51:23 -06002514 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2517 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2518 "(res)sgat=%d low_dma=%d\n", k + 1,
2519 jiffies_to_msecs(fp->timeout),
2520 fp->reserve.bufflen,
2521 (int) fp->reserve.k_use_sg,
2522 (int) fp->low_dma);
2523 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2524 (int) fp->cmd_q, (int) fp->force_packid,
2525 (int) fp->keep_orphan, (int) fp->closed);
2526 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2527 hp = &srp->header;
2528 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2529 if (srp->res_used) {
2530 if (new_interface &&
2531 (SG_FLAG_MMAP_IO & hp->flags))
2532 cp = " mmap>> ";
2533 else
2534 cp = " rb>> ";
2535 } else {
2536 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2537 cp = " dio>> ";
2538 else
2539 cp = " ";
2540 }
2541 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002542 blen = srp->data.bufflen;
2543 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 seq_printf(s, srp->done ?
2545 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002546 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 seq_printf(s, " id=%d blen=%d",
2548 srp->header.pack_id, blen);
2549 if (srp->done)
2550 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002551 else {
2552 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002554 (new_interface ? hp->timeout :
2555 jiffies_to_msecs(fp->timeout)),
2556 (ms > hp->duration ? ms - hp->duration : 0));
2557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2559 (int) srp->data.cmd_opcode);
2560 }
2561 if (0 == m)
2562 seq_printf(s, " No requests active\n");
2563 }
2564}
2565
2566static int sg_proc_open_debug(struct inode *inode, struct file *file)
2567{
2568 return seq_open(file, &debug_seq_ops);
2569}
2570
2571static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2572{
2573 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2574 Sg_device *sdp;
2575
2576 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002577 seq_printf(s, "max_active_device=%d(origin 1)\n",
2578 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2580 }
2581 sdp = it ? sg_get_dev(it->index) : NULL;
2582 if (sdp) {
2583 struct scsi_device *scsidp = sdp->device;
2584
2585 if (NULL == scsidp) {
2586 seq_printf(s, "device %d detached ??\n",
2587 (int)it->index);
2588 return 0;
2589 }
2590
2591 if (sg_get_nth_sfp(sdp, 0)) {
2592 seq_printf(s, " >>> device=%s ",
2593 sdp->disk->disk_name);
2594 if (sdp->detached)
2595 seq_printf(s, "detached pending close ");
2596 else
2597 seq_printf
2598 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2599 scsidp->host->host_no,
2600 scsidp->channel, scsidp->id,
2601 scsidp->lun,
2602 scsidp->host->hostt->emulated);
2603 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2604 sdp->sg_tablesize, sdp->exclude);
2605 }
2606 sg_proc_debug_helper(s, sdp);
2607 }
2608 return 0;
2609}
2610
2611#endif /* CONFIG_SCSI_PROC_FS */
2612
2613module_init(init_sg);
2614module_exit(exit_sg);