blob: d18f90d1d9adb0d36cfb1f086714b507b13b1709 [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
71#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
72
73#define SG_MAX_DEVS 32768
74
75/*
76 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
77 * Then when using 32 bit integers x * m may overflow during the calculation.
78 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
79 * calculates the same, but prevents the overflow when both m and d
80 * are "small" numbers (like HZ and USER_HZ).
81 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
82 * in 32 bits.
83 */
84#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
85
86#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
87
88int sg_big_buff = SG_DEF_RESERVED_SIZE;
89/* N.B. This variable is readable and writeable via
90 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
91 of this size (or less if there is not enough memory) will be reserved
92 for use by this file descriptor. [Deprecated usage: this variable is also
93 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
94 the kernel (i.e. it is not a module).] */
95static int def_reserved_size = -1; /* picks up init parameter */
96static int sg_allow_dio = SG_ALLOW_DIO_DEF;
97
Douglas Gilbert6460e752006-09-20 18:20:49 -040098static int scatter_elem_sz = SG_SCATTER_SZ;
99static int scatter_elem_sz_prev = SG_SCATTER_SZ;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define SG_SECTOR_SZ 512
102#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
103
Tony Jonesee959b02008-02-22 00:13:36 +0100104static int sg_add(struct device *, struct class_interface *);
105static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
James Bottomley7c07d612007-08-05 13:36:11 -0500107static DEFINE_IDR(sg_index_idr);
108static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 file descriptor list for device */
110
111static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100112 .add_dev = sg_add,
113 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
117 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900118 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned bufflen; /* Size of (aggregate) data buffer */
120 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200121 struct page **pages;
122 int page_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
124 unsigned char cmd_opcode; /* first byte of command */
125} Sg_scatter_hold;
126
127struct sg_device; /* forward declarations */
128struct sg_fd;
129
130typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct sg_request *nextrp; /* NULL -> tail request (slist) */
132 struct sg_fd *parentfp; /* NULL -> not in use */
133 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
134 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600135 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
137 char orphan; /* 1 -> drop on sight, 0 -> normal */
138 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
139 volatile char done; /* 0->before bh, 1->before read, 2->read */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900140 struct request *rq;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +0900141 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142} Sg_request;
143
144typedef struct sg_fd { /* holds the state of a file descriptor */
145 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
146 struct sg_device *parentdp; /* owning device */
147 wait_queue_head_t read_wait; /* queue read until command done */
148 rwlock_t rq_list_lock; /* protect access to list in req_arr */
149 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
150 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
151 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
152 unsigned save_scat_len; /* original length of trunc. scat. element */
153 Sg_request *headrp; /* head of request slist, NULL->empty */
154 struct fasync_struct *async_qp; /* used by asynchronous notification */
155 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
156 char low_dma; /* as in parent but possibly overridden to 1 */
157 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
158 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
159 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
160 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
161 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
162 char mmap_called; /* 0 -> mmap() never called on this fd */
163} Sg_fd;
164
165typedef struct sg_device { /* holds the state of each scsi generic device */
166 struct scsi_device *device;
167 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
168 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500169 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 Sg_fd *headfp; /* first open fd belonging to this device */
171 volatile char detached; /* 0->attached, 1->detached pending removal */
172 volatile char exclude; /* opened for exclusive access */
173 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
174 struct gendisk *disk;
175 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
176} Sg_device;
177
178static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600179/* tasklet or soft irq callback */
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +0900180static void sg_rq_end_io(struct request *rq, int uptodate);
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900181static int sg_start_req(Sg_request *srp, unsigned char *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void sg_finish_rem_req(Sg_request * srp);
183static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
184static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
185 int tablesize);
186static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
187 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200188static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
189 const char __user *buf, size_t count, int blocking,
190 int read_only, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
192 unsigned char *cmnd, int timeout, int blocking);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int sg_write_xfer(Sg_request * srp);
194static int sg_read_xfer(Sg_request * srp);
195static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196static void sg_remove_scat(Sg_scatter_hold * schp);
197static void sg_build_reserve(Sg_fd * sfp, int req_size);
198static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
201static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
203static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
204static Sg_request *sg_add_request(Sg_fd * sfp);
205static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
206static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
208static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#ifdef CONFIG_SCSI_PROC_FS
210static int sg_last_dev(void);
211#endif
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define SZ_SG_HEADER sizeof(struct sg_header)
214#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
215#define SZ_SG_IOVEC sizeof(sg_iovec_t)
216#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
217
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900218static int sg_allow_access(struct file *filp, unsigned char *cmd)
219{
220 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
221 struct request_queue *q = sfp->parentdp->device->request_queue;
222
223 if (sfp->parentdp->device->type == TYPE_SCANNER)
224 return 0;
225
226 return blk_verify_command(&q->cmd_filter,
227 cmd, filp->f_mode & FMODE_WRITE);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int
231sg_open(struct inode *inode, struct file *filp)
232{
233 int dev = iminor(inode);
234 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600235 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 Sg_device *sdp;
237 Sg_fd *sfp;
238 int res;
239 int retval;
240
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600241 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 nonseekable_open(inode, filp);
243 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
244 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600245 if ((!sdp) || (!sdp->device)) {
246 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600248 }
249 if (sdp->detached) {
250 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* This driver's module count bumped by fops_get in <linux/fs.h> */
255 /* Prevent the device driver from vanishing while we sleep */
256 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600257 if (retval) {
258 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!((flags & O_NONBLOCK) ||
263 scsi_block_when_processing_errors(sdp->device))) {
264 retval = -ENXIO;
265 /* we are in error recovery for this device */
266 goto error_out;
267 }
268
269 if (flags & O_EXCL) {
270 if (O_RDONLY == (flags & O_ACCMODE)) {
271 retval = -EPERM; /* Can't lock it with read only access */
272 goto error_out;
273 }
274 if (sdp->headfp && (flags & O_NONBLOCK)) {
275 retval = -EBUSY;
276 goto error_out;
277 }
278 res = 0;
279 __wait_event_interruptible(sdp->o_excl_wait,
280 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
281 if (res) {
282 retval = res; /* -ERESTARTSYS because signal hit process */
283 goto error_out;
284 }
285 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
286 if (flags & O_NONBLOCK) {
287 retval = -EBUSY;
288 goto error_out;
289 }
290 res = 0;
291 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
292 res);
293 if (res) {
294 retval = res; /* -ERESTARTSYS because signal hit process */
295 goto error_out;
296 }
297 }
298 if (sdp->detached) {
299 retval = -ENODEV;
300 goto error_out;
301 }
302 if (!sdp->headfp) { /* no existing opens on this device */
303 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600304 q = sdp->device->request_queue;
305 sdp->sg_tablesize = min(q->max_hw_segments,
306 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 if ((sfp = sg_add_sfp(sdp, dev)))
309 filp->private_data = sfp;
310 else {
311 if (flags & O_EXCL)
312 sdp->exclude = 0; /* undo if error */
313 retval = -ENOMEM;
314 goto error_out;
315 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600316 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318
319 error_out:
320 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600321 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return retval;
323}
324
325/* Following function was formerly called 'sg_close' */
326static int
327sg_release(struct inode *inode, struct file *filp)
328{
329 Sg_device *sdp;
330 Sg_fd *sfp;
331
332 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
333 return -ENXIO;
334 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
335 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
336 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
337 if (!sdp->detached) {
338 scsi_device_put(sdp->device);
339 }
340 sdp->exclude = 0;
341 wake_up_interruptible(&sdp->o_excl_wait);
342 }
343 return 0;
344}
345
346static ssize_t
347sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
348{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 Sg_device *sdp;
350 Sg_fd *sfp;
351 Sg_request *srp;
352 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600354 struct sg_header *old_hdr = NULL;
355 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
358 return -ENXIO;
359 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
360 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!access_ok(VERIFY_WRITE, buf, count))
363 return -EFAULT;
364 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600365 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
366 if (!old_hdr)
367 return -ENOMEM;
368 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
369 retval = -EFAULT;
370 goto free_old_hdr;
371 }
372 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600374 sg_io_hdr_t *new_hdr;
375 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
376 if (!new_hdr) {
377 retval = -ENOMEM;
378 goto free_old_hdr;
379 }
380 retval =__copy_from_user
381 (new_hdr, buf, SZ_SG_IO_HDR);
382 req_pack_id = new_hdr->pack_id;
383 kfree(new_hdr);
384 if (retval) {
385 retval = -EFAULT;
386 goto free_old_hdr;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 } else
cb59e842005-04-02 13:51:23 -0600390 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 srp = sg_get_rq_mark(sfp, req_pack_id);
393 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600394 if (sdp->detached) {
395 retval = -ENODEV;
396 goto free_old_hdr;
397 }
398 if (filp->f_flags & O_NONBLOCK) {
399 retval = -EAGAIN;
400 goto free_old_hdr;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 while (1) {
cb59e842005-04-02 13:51:23 -0600403 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600405 (sdp->detached ||
406 (srp = sg_get_rq_mark(sfp, req_pack_id))),
407 retval);
408 if (sdp->detached) {
409 retval = -ENODEV;
410 goto free_old_hdr;
411 }
412 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
cb59e842005-04-02 13:51:23 -0600414
415 /* -ERESTARTSYS as signal hit process */
416 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 }
cb59e842005-04-02 13:51:23 -0600419 if (srp->header.interface_id != '\0') {
420 retval = sg_new_read(sfp, buf, count, srp);
421 goto free_old_hdr;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600425 if (old_hdr == NULL) {
426 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
427 if (! old_hdr) {
428 retval = -ENOMEM;
429 goto free_old_hdr;
430 }
431 }
432 memset(old_hdr, 0, SZ_SG_HEADER);
433 old_hdr->reply_len = (int) hp->timeout;
434 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
435 old_hdr->pack_id = hp->pack_id;
436 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600438 old_hdr->target_status = hp->masked_status;
439 old_hdr->host_status = hp->host_status;
440 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if ((CHECK_CONDITION & hp->masked_status) ||
442 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600443 memcpy(old_hdr->sense_buffer, srp->sense_b,
444 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 switch (hp->host_status) {
446 /* This setup of 'result' is for backward compatibility and is best
447 ignored by the user who should use target, host + driver status */
448 case DID_OK:
449 case DID_PASSTHROUGH:
450 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600451 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case DID_NO_CONNECT:
454 case DID_BUS_BUSY:
455 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600456 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 case DID_BAD_TARGET:
459 case DID_ABORT:
460 case DID_PARITY:
461 case DID_RESET:
462 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600463 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600466 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 hp->masked_status == GOOD) ? 0 : EIO;
468 break;
469 default:
cb59e842005-04-02 13:51:23 -0600470 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 }
473
474 /* Now copy the result back to the user buffer. */
475 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600476 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
477 retval = -EFAULT;
478 goto free_old_hdr;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600481 if (count > old_hdr->reply_len)
482 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600484 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
485 retval = -EFAULT;
486 goto free_old_hdr;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 } else
cb59e842005-04-02 13:51:23 -0600490 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600492 retval = count;
493free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800494 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600495 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498static ssize_t
499sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
500{
501 sg_io_hdr_t *hp = &srp->header;
502 int err = 0;
503 int len;
504
505 if (count < SZ_SG_IO_HDR) {
506 err = -EINVAL;
507 goto err_out;
508 }
509 hp->sb_len_wr = 0;
510 if ((hp->mx_sb_len > 0) && hp->sbp) {
511 if ((CHECK_CONDITION & hp->masked_status) ||
512 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600513 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
515 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
516 len = (len > sb_len) ? sb_len : len;
517 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
518 err = -EFAULT;
519 goto err_out;
520 }
521 hp->sb_len_wr = len;
522 }
523 }
524 if (hp->masked_status || hp->host_status || hp->driver_status)
525 hp->info |= SG_INFO_CHECK;
526 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
527 err = -EFAULT;
528 goto err_out;
529 }
530 err = sg_read_xfer(srp);
531 err_out:
532 sg_finish_rem_req(srp);
533 return (0 == err) ? count : err;
534}
535
536static ssize_t
537sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
538{
539 int mxsize, cmd_size, k;
540 int input_size, blocking;
541 unsigned char opcode;
542 Sg_device *sdp;
543 Sg_fd *sfp;
544 Sg_request *srp;
545 struct sg_header old_hdr;
546 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600547 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
550 return -ENXIO;
551 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
552 sdp->disk->disk_name, (int) count));
553 if (sdp->detached)
554 return -ENODEV;
555 if (!((filp->f_flags & O_NONBLOCK) ||
556 scsi_block_when_processing_errors(sdp->device)))
557 return -ENXIO;
558
559 if (!access_ok(VERIFY_READ, buf, count))
560 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
561 if (count < SZ_SG_HEADER)
562 return -EIO;
563 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
564 return -EFAULT;
565 blocking = !(filp->f_flags & O_NONBLOCK);
566 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200567 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (count < (SZ_SG_HEADER + 6))
569 return -EIO; /* The minimum scsi command length is 6 bytes. */
570
571 if (!(srp = sg_add_request(sfp))) {
572 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
573 return -EDOM;
574 }
575 buf += SZ_SG_HEADER;
576 __get_user(opcode, buf);
577 if (sfp->next_cmd_len > 0) {
578 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
579 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
580 sfp->next_cmd_len = 0;
581 sg_remove_request(sfp, srp);
582 return -EIO;
583 }
584 cmd_size = sfp->next_cmd_len;
585 sfp->next_cmd_len = 0; /* reset so only this write() effected */
586 } else {
587 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
588 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
589 cmd_size = 12;
590 }
591 SCSI_LOG_TIMEOUT(4, printk(
592 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
593/* Determine buffer size. */
594 input_size = count - cmd_size;
595 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
596 mxsize -= SZ_SG_HEADER;
597 input_size -= SZ_SG_HEADER;
598 if (input_size < 0) {
599 sg_remove_request(sfp, srp);
600 return -EIO; /* User did not pass enough bytes for this command. */
601 }
602 hp = &srp->header;
603 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
604 hp->cmd_len = (unsigned char) cmd_size;
605 hp->iovec_count = 0;
606 hp->mx_sb_len = 0;
607 if (input_size > 0)
608 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
609 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
610 else
611 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
612 hp->dxfer_len = mxsize;
FUJITA Tomonorifad7f012008-09-02 16:20:20 +0900613 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
614 hp->dxferp = (char __user *)buf + cmd_size;
615 else
616 hp->dxferp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 hp->sbp = NULL;
618 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
619 hp->flags = input_size; /* structure abuse ... */
620 hp->pack_id = old_hdr.pack_id;
621 hp->usr_ptr = NULL;
622 if (__copy_from_user(cmnd, buf, cmd_size))
623 return -EFAULT;
624 /*
625 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
626 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
627 * is a non-zero input_size, so emit a warning.
628 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100629 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
630 static char cmd[TASK_COMM_LEN];
631 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 printk(KERN_WARNING
633 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
634 "guessing data in;\n" KERN_WARNING " "
635 "program %s not setting count and/or reply_len properly\n",
636 old_hdr.reply_len - (int)SZ_SG_HEADER,
637 input_size, (unsigned int) cmnd[0],
638 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100639 strcpy(cmd, current->comm);
640 }
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
643 return (k < 0) ? k : count;
644}
645
646static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200647sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
648 size_t count, int blocking, int read_only,
649 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 int k;
652 Sg_request *srp;
653 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600654 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int timeout;
656 unsigned long ul_timeout;
657
658 if (count < SZ_SG_IO_HDR)
659 return -EINVAL;
660 if (!access_ok(VERIFY_READ, buf, count))
661 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
662
663 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
664 if (!(srp = sg_add_request(sfp))) {
665 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
666 return -EDOM;
667 }
668 hp = &srp->header;
669 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
670 sg_remove_request(sfp, srp);
671 return -EFAULT;
672 }
673 if (hp->interface_id != 'S') {
674 sg_remove_request(sfp, srp);
675 return -ENOSYS;
676 }
677 if (hp->flags & SG_FLAG_MMAP_IO) {
678 if (hp->dxfer_len > sfp->reserve.bufflen) {
679 sg_remove_request(sfp, srp);
680 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
681 }
682 if (hp->flags & SG_FLAG_DIRECT_IO) {
683 sg_remove_request(sfp, srp);
684 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
685 }
686 if (sg_res_in_use(sfp)) {
687 sg_remove_request(sfp, srp);
688 return -EBUSY; /* reserve buffer already being used */
689 }
690 }
691 ul_timeout = msecs_to_jiffies(srp->header.timeout);
692 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
693 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
694 sg_remove_request(sfp, srp);
695 return -EMSGSIZE;
696 }
697 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
698 sg_remove_request(sfp, srp);
699 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
700 }
701 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
702 sg_remove_request(sfp, srp);
703 return -EFAULT;
704 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900705 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sg_remove_request(sfp, srp);
707 return -EPERM;
708 }
709 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
710 if (k < 0)
711 return k;
712 if (o_srp)
713 *o_srp = srp;
714 return count;
715}
716
717static int
718sg_common_write(Sg_fd * sfp, Sg_request * srp,
719 unsigned char *cmnd, int timeout, int blocking)
720{
Mike Christied6b10342005-11-08 04:06:41 -0600721 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 Sg_device *sdp = sfp->parentdp;
723 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
726 hp->status = 0;
727 hp->masked_status = 0;
728 hp->msg_status = 0;
729 hp->info = 0;
730 hp->host_status = 0;
731 hp->driver_status = 0;
732 hp->resid = 0;
733 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
734 (int) cmnd[0], (int) hp->cmd_len));
735
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900736 k = sg_start_req(srp, cmnd);
737 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400738 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sg_finish_rem_req(srp);
740 return k; /* probably out of space --> ENOMEM */
741 }
742 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400743 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 sg_finish_rem_req(srp);
745 return k;
746 }
747 if (sdp->detached) {
748 sg_finish_rem_req(srp);
749 return -ENODEV;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 switch (hp->dxfer_direction) {
753 case SG_DXFER_TO_FROM_DEV:
754 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600755 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
757 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600758 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600761 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 default:
Mike Christied6b10342005-11-08 04:06:41 -0600764 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766 }
cb59e842005-04-02 13:51:23 -0600767 hp->duration = jiffies_to_msecs(jiffies);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +0200768
769 srp->rq->timeout = timeout;
770 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
771 srp->rq, 1, sg_rq_end_io);
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775static int
776sg_srp_done(Sg_request *srp, Sg_fd *sfp)
777{
778 unsigned long iflags;
779 int done;
780
781 read_lock_irqsave(&sfp->rq_list_lock, iflags);
782 done = srp->done;
783 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
784 return done;
785}
786
787static int
788sg_ioctl(struct inode *inode, struct file *filp,
789 unsigned int cmd_in, unsigned long arg)
790{
791 void __user *p = (void __user *)arg;
792 int __user *ip = p;
793 int result, val, read_only;
794 Sg_device *sdp;
795 Sg_fd *sfp;
796 Sg_request *srp;
797 unsigned long iflags;
798
799 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
800 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
803 sdp->disk->disk_name, (int) cmd_in));
804 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
805
806 switch (cmd_in) {
807 case SG_IO:
808 {
809 int blocking = 1; /* ignore O_NONBLOCK flag */
810
811 if (sdp->detached)
812 return -ENODEV;
813 if (!scsi_block_when_processing_errors(sdp->device))
814 return -ENXIO;
815 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
816 return -EFAULT;
817 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200818 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 blocking, read_only, &srp);
820 if (result < 0)
821 return result;
822 srp->sg_io_owned = 1;
823 while (1) {
824 result = 0; /* following macro to beat race condition */
825 __wait_event_interruptible(sfp->read_wait,
826 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
827 result);
828 if (sdp->detached)
829 return -ENODEV;
830 if (sfp->closed)
831 return 0; /* request packet dropped already */
832 if (0 == result)
833 break;
834 srp->orphan = 1;
835 return result; /* -ERESTARTSYS because signal hit process */
836 }
837 write_lock_irqsave(&sfp->rq_list_lock, iflags);
838 srp->done = 2;
839 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
840 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
841 return (result < 0) ? result : 0;
842 }
843 case SG_SET_TIMEOUT:
844 result = get_user(val, ip);
845 if (result)
846 return result;
847 if (val < 0)
848 return -EIO;
849 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
850 val = MULDIV (INT_MAX, USER_HZ, HZ);
851 sfp->timeout_user = val;
852 sfp->timeout = MULDIV (val, HZ, USER_HZ);
853
854 return 0;
855 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
856 /* strange ..., for backward compatibility */
857 return sfp->timeout_user;
858 case SG_SET_FORCE_LOW_DMA:
859 result = get_user(val, ip);
860 if (result)
861 return result;
862 if (val) {
863 sfp->low_dma = 1;
864 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
865 val = (int) sfp->reserve.bufflen;
866 sg_remove_scat(&sfp->reserve);
867 sg_build_reserve(sfp, val);
868 }
869 } else {
870 if (sdp->detached)
871 return -ENODEV;
872 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
873 }
874 return 0;
875 case SG_GET_LOW_DMA:
876 return put_user((int) sfp->low_dma, ip);
877 case SG_GET_SCSI_ID:
878 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
879 return -EFAULT;
880 else {
881 sg_scsi_id_t __user *sg_idp = p;
882
883 if (sdp->detached)
884 return -ENODEV;
885 __put_user((int) sdp->device->host->host_no,
886 &sg_idp->host_no);
887 __put_user((int) sdp->device->channel,
888 &sg_idp->channel);
889 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
890 __put_user((int) sdp->device->lun, &sg_idp->lun);
891 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
892 __put_user((short) sdp->device->host->cmd_per_lun,
893 &sg_idp->h_cmd_per_lun);
894 __put_user((short) sdp->device->queue_depth,
895 &sg_idp->d_queue_depth);
896 __put_user(0, &sg_idp->unused[0]);
897 __put_user(0, &sg_idp->unused[1]);
898 return 0;
899 }
900 case SG_SET_FORCE_PACK_ID:
901 result = get_user(val, ip);
902 if (result)
903 return result;
904 sfp->force_packid = val ? 1 : 0;
905 return 0;
906 case SG_GET_PACK_ID:
907 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
908 return -EFAULT;
909 read_lock_irqsave(&sfp->rq_list_lock, iflags);
910 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
911 if ((1 == srp->done) && (!srp->sg_io_owned)) {
912 read_unlock_irqrestore(&sfp->rq_list_lock,
913 iflags);
914 __put_user(srp->header.pack_id, ip);
915 return 0;
916 }
917 }
918 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
919 __put_user(-1, ip);
920 return 0;
921 case SG_GET_NUM_WAITING:
922 read_lock_irqsave(&sfp->rq_list_lock, iflags);
923 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
924 if ((1 == srp->done) && (!srp->sg_io_owned))
925 ++val;
926 }
927 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
928 return put_user(val, ip);
929 case SG_GET_SG_TABLESIZE:
930 return put_user(sdp->sg_tablesize, ip);
931 case SG_SET_RESERVED_SIZE:
932 result = get_user(val, ip);
933 if (result)
934 return result;
935 if (val < 0)
936 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500937 val = min_t(int, val,
938 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (val != sfp->reserve.bufflen) {
940 if (sg_res_in_use(sfp) || sfp->mmap_called)
941 return -EBUSY;
942 sg_remove_scat(&sfp->reserve);
943 sg_build_reserve(sfp, val);
944 }
945 return 0;
946 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500947 val = min_t(int, sfp->reserve.bufflen,
948 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return put_user(val, ip);
950 case SG_SET_COMMAND_Q:
951 result = get_user(val, ip);
952 if (result)
953 return result;
954 sfp->cmd_q = val ? 1 : 0;
955 return 0;
956 case SG_GET_COMMAND_Q:
957 return put_user((int) sfp->cmd_q, ip);
958 case SG_SET_KEEP_ORPHAN:
959 result = get_user(val, ip);
960 if (result)
961 return result;
962 sfp->keep_orphan = val;
963 return 0;
964 case SG_GET_KEEP_ORPHAN:
965 return put_user((int) sfp->keep_orphan, ip);
966 case SG_NEXT_CMD_LEN:
967 result = get_user(val, ip);
968 if (result)
969 return result;
970 sfp->next_cmd_len = (val > 0) ? val : 0;
971 return 0;
972 case SG_GET_VERSION_NUM:
973 return put_user(sg_version_num, ip);
974 case SG_GET_ACCESS_COUNT:
975 /* faked - we don't have a real access count anymore */
976 val = (sdp->device ? 1 : 0);
977 return put_user(val, ip);
978 case SG_GET_REQUEST_TABLE:
979 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
980 return -EFAULT;
981 else {
cb59e842005-04-02 13:51:23 -0600982 sg_req_info_t *rinfo;
983 unsigned int ms;
984
985 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
986 GFP_KERNEL);
987 if (!rinfo)
988 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 read_lock_irqsave(&sfp->rq_list_lock, iflags);
990 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
991 ++val, srp = srp ? srp->nextrp : srp) {
992 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
993 if (srp) {
994 rinfo[val].req_state = srp->done + 1;
995 rinfo[val].problem =
996 srp->header.masked_status &
997 srp->header.host_status &
998 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600999 if (srp->done)
1000 rinfo[val].duration =
1001 srp->header.duration;
1002 else {
1003 ms = jiffies_to_msecs(jiffies);
1004 rinfo[val].duration =
1005 (ms > srp->header.duration) ?
1006 (ms - srp->header.duration) : 0;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001009 rinfo[val].sg_io_owned =
1010 srp->sg_io_owned;
1011 rinfo[val].pack_id =
1012 srp->header.pack_id;
1013 rinfo[val].usr_ptr =
1014 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 }
1017 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001018 result = __copy_to_user(p, rinfo,
1019 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1020 result = result ? -EFAULT : 0;
1021 kfree(rinfo);
1022 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 case SG_EMULATED_HOST:
1025 if (sdp->detached)
1026 return -ENODEV;
1027 return put_user(sdp->device->host->hostt->emulated, ip);
1028 case SG_SCSI_RESET:
1029 if (sdp->detached)
1030 return -ENODEV;
1031 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001032 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return -EBUSY;
1034 } else if (!scsi_block_when_processing_errors(sdp->device))
1035 return -EBUSY;
1036 result = get_user(val, ip);
1037 if (result)
1038 return result;
1039 if (SG_SCSI_RESET_NOTHING == val)
1040 return 0;
1041 switch (val) {
1042 case SG_SCSI_RESET_DEVICE:
1043 val = SCSI_TRY_RESET_DEVICE;
1044 break;
Brian King39120e12008-07-01 13:03:19 -05001045 case SG_SCSI_RESET_TARGET:
1046 val = SCSI_TRY_RESET_TARGET;
1047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 case SG_SCSI_RESET_BUS:
1049 val = SCSI_TRY_RESET_BUS;
1050 break;
1051 case SG_SCSI_RESET_HOST:
1052 val = SCSI_TRY_RESET_HOST;
1053 break;
1054 default:
1055 return -EINVAL;
1056 }
1057 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1058 return -EACCES;
1059 return (scsi_reset_provider(sdp->device, val) ==
1060 SUCCESS) ? 0 : -EIO;
1061 case SCSI_IOCTL_SEND_COMMAND:
1062 if (sdp->detached)
1063 return -ENODEV;
1064 if (read_only) {
1065 unsigned char opcode = WRITE_6;
1066 Scsi_Ioctl_Command __user *siocp = p;
1067
1068 if (copy_from_user(&opcode, siocp->data, 1))
1069 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001070 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -EPERM;
1072 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001073 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 case SG_SET_DEBUG:
1075 result = get_user(val, ip);
1076 if (result)
1077 return result;
1078 sdp->sgdebug = (char) val;
1079 return 0;
1080 case SCSI_IOCTL_GET_IDLUN:
1081 case SCSI_IOCTL_GET_BUS_NUMBER:
1082 case SCSI_IOCTL_PROBE_HOST:
1083 case SG_GET_TRANSFORM:
1084 if (sdp->detached)
1085 return -ENODEV;
1086 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001087 case BLKSECTGET:
1088 return put_user(sdp->device->request_queue->max_sectors * 512,
1089 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001090 case BLKTRACESETUP:
1091 return blk_trace_setup(sdp->device->request_queue,
1092 sdp->disk->disk_name,
1093 sdp->device->sdev_gendev.devt,
1094 (char *)arg);
1095 case BLKTRACESTART:
1096 return blk_trace_startstop(sdp->device->request_queue, 1);
1097 case BLKTRACESTOP:
1098 return blk_trace_startstop(sdp->device->request_queue, 0);
1099 case BLKTRACETEARDOWN:
1100 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 default:
1102 if (read_only)
1103 return -EPERM; /* don't know so take safe approach */
1104 return scsi_ioctl(sdp->device, cmd_in, p);
1105 }
1106}
1107
1108#ifdef CONFIG_COMPAT
1109static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1110{
1111 Sg_device *sdp;
1112 Sg_fd *sfp;
1113 struct scsi_device *sdev;
1114
1115 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1116 return -ENXIO;
1117
1118 sdev = sdp->device;
1119 if (sdev->host->hostt->compat_ioctl) {
1120 int ret;
1121
1122 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1123
1124 return ret;
1125 }
1126
1127 return -ENOIOCTLCMD;
1128}
1129#endif
1130
1131static unsigned int
1132sg_poll(struct file *filp, poll_table * wait)
1133{
1134 unsigned int res = 0;
1135 Sg_device *sdp;
1136 Sg_fd *sfp;
1137 Sg_request *srp;
1138 int count = 0;
1139 unsigned long iflags;
1140
1141 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1142 || sfp->closed)
1143 return POLLERR;
1144 poll_wait(filp, &sfp->read_wait, wait);
1145 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1146 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1147 /* if any read waiting, flag it */
1148 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1149 res = POLLIN | POLLRDNORM;
1150 ++count;
1151 }
1152 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1153
1154 if (sdp->detached)
1155 res |= POLLHUP;
1156 else if (!sfp->cmd_q) {
1157 if (0 == count)
1158 res |= POLLOUT | POLLWRNORM;
1159 } else if (count < SG_MAX_QUEUE)
1160 res |= POLLOUT | POLLWRNORM;
1161 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1162 sdp->disk->disk_name, (int) res));
1163 return res;
1164}
1165
1166static int
1167sg_fasync(int fd, struct file *filp, int mode)
1168{
1169 int retval;
1170 Sg_device *sdp;
1171 Sg_fd *sfp;
1172
1173 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1174 return -ENXIO;
1175 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1176 sdp->disk->disk_name, mode));
1177
1178 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1179 return (retval < 0) ? retval : 0;
1180}
1181
Nick Piggina13ff0b2008-02-07 18:46:06 -08001182static int
1183sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001186 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001188 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001191 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001193 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001195 return VM_FAULT_SIGBUS;
1196 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001198 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001199 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1200 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001201 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001202 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001203 if (offset < len) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001204 struct page *page = nth_page(rsv_schp->pages[k],
1205 offset >> PAGE_SHIFT);
Mike Christied6b10342005-11-08 04:06:41 -06001206 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001207 vmf->page = page;
1208 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Mike Christied6b10342005-11-08 04:06:41 -06001210 sa += len;
1211 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Mike Christied6b10342005-11-08 04:06:41 -06001213
Nick Piggina13ff0b2008-02-07 18:46:06 -08001214 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001218 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219};
1220
1221static int
1222sg_mmap(struct file *filp, struct vm_area_struct *vma)
1223{
1224 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001225 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 Sg_scatter_hold *rsv_schp;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001227 int k, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1230 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001231 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1233 (void *) vma->vm_start, (int) req_sz));
1234 if (vma->vm_pgoff)
1235 return -EINVAL; /* want no offset */
1236 rsv_schp = &sfp->reserve;
1237 if (req_sz > rsv_schp->bufflen)
1238 return -ENOMEM; /* cannot map more than reserved buffer */
1239
Mike Christied6b10342005-11-08 04:06:41 -06001240 sa = vma->vm_start;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001241 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1242 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001243 len = vma->vm_end - sa;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001244 len = (len < length) ? len : length;
Mike Christied6b10342005-11-08 04:06:41 -06001245 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Mike Christied6b10342005-11-08 04:06:41 -06001247
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001248 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001249 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 vma->vm_private_data = sfp;
1251 vma->vm_ops = &sg_mmap_vm_ops;
1252 return 0;
1253}
1254
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001255/*
1256 * This function is a "bottom half" handler that is called by the mid
1257 * level when a command is completed (or has failed).
1258 */
1259static void sg_rq_end_io(struct request *rq, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001261 struct sg_request *srp = rq->end_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 Sg_device *sdp = NULL;
1263 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001265 unsigned int ms;
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001266 char *sense;
1267 int result, resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (NULL == srp) {
1270 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return;
1272 }
1273 sfp = srp->parentfp;
1274 if (sfp)
1275 sdp = sfp->parentdp;
1276 if ((NULL == sdp) || sdp->detached) {
1277 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return;
1279 }
1280
FUJITA Tomonoria91a3a22008-09-02 22:50:01 +09001281 sense = rq->sense;
1282 result = rq->errors;
1283 resid = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001286 sdp->disk->disk_name, srp->header.pack_id, result));
1287 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001288 ms = jiffies_to_msecs(jiffies);
1289 srp->header.duration = (ms > srp->header.duration) ?
1290 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001291 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct scsi_sense_hdr sshdr;
1293
Mike Christied6b10342005-11-08 04:06:41 -06001294 srp->header.status = 0xff & result;
1295 srp->header.masked_status = status_byte(result);
1296 srp->header.msg_status = msg_byte(result);
1297 srp->header.host_status = host_byte(result);
1298 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if ((sdp->sgdebug > 0) &&
1300 ((CHECK_CONDITION == srp->header.masked_status) ||
1301 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001302 __scsi_print_sense("sg_cmd_done", sense,
1303 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001306 if (driver_byte(result) != 0
1307 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 && !scsi_sense_is_deferred(&sshdr)
1309 && sshdr.sense_key == UNIT_ATTENTION
1310 && sdp->device->removable) {
1311 /* Detected possible disc change. Set the bit - this */
1312 /* may be used if there are filesystems using this device */
1313 sdp->device->changed = 1;
1314 }
1315 }
1316 /* Rely on write phase to clean out srp status values, so no "else" */
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (sfp->closed) { /* whoops this fd already released, cleanup */
1319 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1320 sg_finish_rem_req(srp);
1321 srp = NULL;
1322 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001323 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1325 scsi_device_put(sdp->device);
1326 }
1327 sfp = NULL;
1328 }
1329 } else if (srp && srp->orphan) {
1330 if (sfp->keep_orphan)
1331 srp->sg_io_owned = 0;
1332 else {
1333 sg_finish_rem_req(srp);
1334 srp = NULL;
1335 }
1336 }
1337 if (sfp && srp) {
1338 /* Now wake up any sg_read() that is waiting for this packet. */
1339 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1340 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1341 srp->done = 1;
1342 wake_up_interruptible(&sfp->read_wait);
1343 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1344 }
1345}
1346
1347static struct file_operations sg_fops = {
1348 .owner = THIS_MODULE,
1349 .read = sg_read,
1350 .write = sg_write,
1351 .poll = sg_poll,
1352 .ioctl = sg_ioctl,
1353#ifdef CONFIG_COMPAT
1354 .compat_ioctl = sg_compat_ioctl,
1355#endif
1356 .open = sg_open,
1357 .mmap = sg_mmap,
1358 .release = sg_release,
1359 .fasync = sg_fasync,
1360};
1361
gregkh@suse.ded2538782005-03-23 09:55:22 -08001362static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364static int sg_sysfs_valid = 0;
1365
James Bottomley7c07d612007-08-05 13:36:11 -05001366static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Mike Christied6b10342005-11-08 04:06:41 -06001368 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 Sg_device *sdp;
1370 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001371 int error;
1372 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Jes Sorensen24669f752006-01-16 10:31:18 -05001374 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!sdp) {
1376 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001377 return ERR_PTR(-ENOMEM);
1378 }
1379 error = -ENOMEM;
1380 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1381 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
James Bottomley7c07d612007-08-05 13:36:11 -05001385 write_lock_irqsave(&sg_index_lock, iflags);
1386 error = idr_get_new(&sg_index_idr, sdp, &k);
1387 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
James Bottomley7c07d612007-08-05 13:36:11 -05001389 if (error) {
1390 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1391 error);
1392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (unlikely(k >= SG_MAX_DEVS))
1396 goto overflow;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1399 sprintf(disk->disk_name, "sg%d", k);
1400 disk->first_minor = k;
1401 sdp->disk = disk;
1402 sdp->device = scsidp;
1403 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001404 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001405 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
James Bottomley7c07d612007-08-05 13:36:11 -05001407 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001409 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001411 return ERR_PTR(error);
1412 }
1413 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001416 sdev_printk(KERN_WARNING, scsidp,
1417 "Unable to attach sg device type=%d, minor "
1418 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 error = -ENODEV;
1420 goto out;
1421}
1422
1423static int
Tony Jonesee959b02008-02-22 00:13:36 +01001424sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Tony Jonesee959b02008-02-22 00:13:36 +01001426 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct gendisk *disk;
1428 Sg_device *sdp = NULL;
1429 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001430 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001431 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 disk = alloc_disk(1);
1434 if (!disk) {
1435 printk(KERN_WARNING "alloc_disk failed\n");
1436 return -ENOMEM;
1437 }
1438 disk->major = SCSI_GENERIC_MAJOR;
1439
1440 error = -ENOMEM;
1441 cdev = cdev_alloc();
1442 if (!cdev) {
1443 printk(KERN_WARNING "cdev_alloc failed\n");
1444 goto out;
1445 }
1446 cdev->owner = THIS_MODULE;
1447 cdev->ops = &sg_fops;
1448
James Bottomley7c07d612007-08-05 13:36:11 -05001449 sdp = sg_alloc(disk, scsidp);
1450 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001452 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto out;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
James Bottomley7c07d612007-08-05 13:36:11 -05001456 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001457 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001458 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 sdp->cdev = cdev;
1461 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001462 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001464 sg_class_member = device_create_drvdata(sg_sysfs_class,
1465 cl_dev->parent,
1466 MKDEV(SCSI_GENERIC_MAJOR,
1467 sdp->index),
1468 sdp,
1469 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001470 if (IS_ERR(sg_class_member)) {
1471 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001472 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001473 error = PTR_ERR(sg_class_member);
1474 goto cdev_add_err;
1475 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001476 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 &sg_class_member->kobj, "generic");
1478 if (error)
1479 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001480 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001482 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
James Bottomley9ccfc752005-10-02 11:45:08 -05001484 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001485 "Attached scsi generic sg%d type %d\n", sdp->index,
1486 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Tony Jonesee959b02008-02-22 00:13:36 +01001488 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001492cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001493 write_lock_irqsave(&sg_index_lock, iflags);
1494 idr_remove(&sg_index_idr, sdp->index);
1495 write_unlock_irqrestore(&sg_index_lock, iflags);
1496 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498out:
1499 put_disk(disk);
1500 if (cdev)
1501 cdev_del(cdev);
1502 return error;
1503}
1504
1505static void
Tony Jonesee959b02008-02-22 00:13:36 +01001506sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Tony Jonesee959b02008-02-22 00:13:36 +01001508 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1509 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unsigned long iflags;
1511 Sg_fd *sfp;
1512 Sg_fd *tsfp;
1513 Sg_request *srp;
1514 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001515 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
James Bottomley7c07d612007-08-05 13:36:11 -05001517 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
James Bottomley7c07d612007-08-05 13:36:11 -05001520 delay = 0;
1521 write_lock_irqsave(&sg_index_lock, iflags);
1522 if (sdp->headfp) {
1523 sdp->detached = 1;
1524 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1525 tsfp = sfp->nextfp;
1526 for (srp = sfp->headrp; srp; srp = tsrp) {
1527 tsrp = srp->nextrp;
1528 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1529 sg_finish_rem_req(srp);
1530 }
1531 if (sfp->closed) {
1532 scsi_device_put(sdp->device);
1533 __sg_remove_sfp(sdp, sfp);
1534 } else {
1535 delay = 1;
1536 wake_up_interruptible(&sfp->read_wait);
1537 kill_fasync(&sfp->async_qp, SIGPOLL,
1538 POLL_HUP);
1539 }
1540 }
1541 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1542 if (NULL == sdp->headfp) {
1543 idr_remove(&sg_index_idr, sdp->index);
1544 }
1545 } else { /* nothing active, simple case */
1546 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1547 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
James Bottomley7c07d612007-08-05 13:36:11 -05001549 write_unlock_irqrestore(&sg_index_lock, iflags);
1550
1551 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001552 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001553 cdev_del(sdp->cdev);
1554 sdp->cdev = NULL;
1555 put_disk(sdp->disk);
1556 sdp->disk = NULL;
1557 if (NULL == sdp->headfp)
1558 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (delay)
1561 msleep(10); /* dirty detach so delay device destruction */
1562}
1563
Douglas Gilbert6460e752006-09-20 18:20:49 -04001564module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1565module_param_named(def_reserved_size, def_reserved_size, int,
1566 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1568
1569MODULE_AUTHOR("Douglas Gilbert");
1570MODULE_DESCRIPTION("SCSI generic (sg) driver");
1571MODULE_LICENSE("GPL");
1572MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001573MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Douglas Gilbert6460e752006-09-20 18:20:49 -04001575MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1576 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1578MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1579
1580static int __init
1581init_sg(void)
1582{
1583 int rc;
1584
Douglas Gilbert6460e752006-09-20 18:20:49 -04001585 if (scatter_elem_sz < PAGE_SIZE) {
1586 scatter_elem_sz = PAGE_SIZE;
1587 scatter_elem_sz_prev = scatter_elem_sz;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (def_reserved_size >= 0)
1590 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001591 else
1592 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1595 SG_MAX_DEVS, "sg");
1596 if (rc)
1597 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001598 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if ( IS_ERR(sg_sysfs_class) ) {
1600 rc = PTR_ERR(sg_sysfs_class);
1601 goto err_out;
1602 }
1603 sg_sysfs_valid = 1;
1604 rc = scsi_register_interface(&sg_interface);
1605 if (0 == rc) {
1606#ifdef CONFIG_SCSI_PROC_FS
1607 sg_proc_init();
1608#endif /* CONFIG_SCSI_PROC_FS */
1609 return 0;
1610 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001611 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612err_out:
1613 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1614 return rc;
1615}
1616
1617static void __exit
1618exit_sg(void)
1619{
1620#ifdef CONFIG_SCSI_PROC_FS
1621 sg_proc_cleanup();
1622#endif /* CONFIG_SCSI_PROC_FS */
1623 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001624 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 sg_sysfs_valid = 0;
1626 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1627 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001628 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001631static int __sg_start_req(struct sg_request *srp, struct sg_io_hdr *hp,
1632 unsigned char *cmd)
1633{
1634 struct sg_fd *sfp = srp->parentfp;
1635 struct request_queue *q = sfp->parentdp->device->request_queue;
1636 struct request *rq;
1637 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1638
1639 rq = blk_get_request(q, rw, GFP_ATOMIC);
1640 if (!rq)
1641 return -ENOMEM;
1642
1643 memcpy(rq->cmd, cmd, hp->cmd_len);
1644
1645 rq->cmd_len = hp->cmd_len;
1646 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1647
1648 srp->rq = rq;
1649 rq->end_io_data = srp;
1650 rq->sense = srp->sense_b;
1651 rq->retries = SG_DEFAULT_RETRIES;
1652
1653 return 0;
1654}
1655
1656static int sg_start_req(Sg_request *srp, unsigned char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 int res;
1659 Sg_fd *sfp = srp->parentfp;
1660 sg_io_hdr_t *hp = &srp->header;
1661 int dxfer_len = (int) hp->dxfer_len;
1662 int dxfer_dir = hp->dxfer_direction;
1663 Sg_scatter_hold *req_schp = &srp->data;
1664 Sg_scatter_hold *rsv_schp = &sfp->reserve;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001665 struct request_queue *q = sfp->parentdp->device->request_queue;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001666 struct rq_map_data map_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001669
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001670 res = __sg_start_req(srp, hp, cmd);
1671 if (res)
1672 return res;
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001675 return 0;
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001676
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001677#ifdef SG_ALLOW_DIO_CODE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1679 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001680 (!sfp->parentdp->device->host->unchecked_isa_dma) &&
FUJITA Tomonori01cfcdd2008-08-28 15:05:59 +09001681 blk_rq_aligned(q, hp->dxferp, dxfer_len))
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001682 return sg_build_direct(srp, sfp, dxfer_len);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001683#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1685 sg_link_reserve(sfp, srp, dxfer_len);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001686 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 res = sg_build_indirect(req_schp, sfp, dxfer_len);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001688
1689 if (!res) {
1690 struct request *rq = srp->rq;
1691 Sg_scatter_hold *schp = &srp->data;
1692 int iovec_count = (int) hp->iovec_count;
1693
1694 map_data.pages = schp->pages;
1695 map_data.page_order = schp->page_order;
1696 map_data.nr_entries = schp->k_use_sg;
1697
1698 if (iovec_count)
1699 res = blk_rq_map_user_iov(q, rq, &map_data, hp->dxferp,
1700 iovec_count,
1701 hp->dxfer_len, GFP_ATOMIC);
1702 else
1703 res = blk_rq_map_user(q, rq, &map_data, hp->dxferp,
1704 hp->dxfer_len, GFP_ATOMIC);
1705
1706 if (!res)
1707 srp->bio = rq->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001709
1710 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
1713static void
1714sg_finish_rem_req(Sg_request * srp)
1715{
1716 Sg_fd *sfp = srp->parentfp;
1717 Sg_scatter_hold *req_schp = &srp->data;
1718
1719 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1720 if (srp->res_used)
1721 sg_unlink_reserve(sfp, srp);
1722 else
1723 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001724
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001725 if (srp->rq) {
1726 if (srp->bio)
1727 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001728
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001729 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001730 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 sg_remove_request(sfp, srp);
1733}
1734
1735static int
1736sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1737{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001738 int sg_bufflen = tablesize * sizeof(struct page *);
Al Viro2d20eaf2006-02-01 06:31:40 -05001739 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001741 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1742 if (!schp->pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001745 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1749static int
1750sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 sg_io_hdr_t *hp = &srp->header;
1753 Sg_scatter_hold *schp = &srp->data;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001754 int res;
1755 struct request *rq = srp->rq;
1756 struct request_queue *q = sfp->parentdp->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001758 res = blk_rq_map_user(q, rq, NULL, hp->dxferp, dxfer_len, GFP_ATOMIC);
1759 if (res)
1760 return res;
1761 srp->bio = rq->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 schp->dio_in_use = 1;
1763 hp->info |= SG_INFO_DIRECT_IO;
1764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
1767static int
1768sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1769{
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001770 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
Mike Christied6b10342005-11-08 04:06:41 -06001771 int sg_tablesize = sfp->parentdp->sg_tablesize;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001772 int blk_size = buff_size, order;
1773 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Eric Sesterhennfb119932007-05-23 14:41:36 -07001775 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return -EFAULT;
1777 if (0 == blk_size)
1778 ++blk_size; /* don't know why */
1779/* round request up to next highest SG_SECTOR_SZ byte boundary */
1780 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1781 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1782 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001783
1784 /* N.B. ret_sz carried into this block ... */
1785 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1786 if (mx_sc_elems < 0)
1787 return mx_sc_elems; /* most likely -ENOMEM */
1788
Douglas Gilbert6460e752006-09-20 18:20:49 -04001789 num = scatter_elem_sz;
1790 if (unlikely(num != scatter_elem_sz_prev)) {
1791 if (num < PAGE_SIZE) {
1792 scatter_elem_sz = PAGE_SIZE;
1793 scatter_elem_sz_prev = PAGE_SIZE;
1794 } else
1795 scatter_elem_sz_prev = num;
1796 }
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001797
1798 if (sfp->low_dma)
1799 gfp_mask |= GFP_DMA;
1800
1801 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1802 gfp_mask |= __GFP_ZERO;
1803
1804 order = get_order(num);
1805retry:
1806 ret_sz = 1 << (PAGE_SHIFT + order);
1807
1808 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1809 k++, rem_sz -= ret_sz) {
1810
Douglas Gilbert6460e752006-09-20 18:20:49 -04001811 num = (rem_sz > scatter_elem_sz_prev) ?
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001812 scatter_elem_sz_prev : rem_sz;
1813
1814 schp->pages[k] = alloc_pages(gfp_mask, order);
1815 if (!schp->pages[k])
1816 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Douglas Gilbert6460e752006-09-20 18:20:49 -04001818 if (num == scatter_elem_sz_prev) {
1819 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1820 scatter_elem_sz = ret_sz;
1821 scatter_elem_sz_prev = ret_sz;
1822 }
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001825 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1826 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001827 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001829 schp->page_order = order;
Mike Christied6b10342005-11-08 04:06:41 -06001830 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001831 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1832 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001833
1834 schp->bufflen = blk_size;
1835 if (rem_sz > 0) /* must have failed */
1836 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001838out:
1839 for (i = 0; i < k; i++)
1840 __free_pages(schp->pages[k], order);
1841
1842 if (--order >= 0)
1843 goto retry;
1844
1845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848static int
1849sg_write_xfer(Sg_request * srp)
1850{
1851 sg_io_hdr_t *hp = &srp->header;
1852 Sg_scatter_hold *schp = &srp->data;
1853 int num_xfer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 int dxfer_dir = hp->dxfer_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1856
1857 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1858 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1859 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1860 if (schp->bufflen < num_xfer)
1861 num_xfer = schp->bufflen;
1862 }
1863 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1864 (new_interface
1865 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1866 return 0;
1867
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001868 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, k_use_sg=%d\n",
1869 num_xfer, schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return 0;
1872}
1873
1874static void
1875sg_remove_scat(Sg_scatter_hold * schp)
1876{
1877 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001878 if (schp->pages && schp->sglist_len > 0) {
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001879 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int k;
1881
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001882 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 SCSI_LOG_TIMEOUT(5, printk(
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001884 "sg_remove_scat: k=%d, pg=0x%p\n",
1885 k, schp->pages[k]));
1886 __free_pages(schp->pages[k], schp->page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001888
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001889 kfree(schp->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
Mike Christied6b10342005-11-08 04:06:41 -06001891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 memset(schp, 0, sizeof (*schp));
1893}
1894
1895static int
1896sg_read_xfer(Sg_request * srp)
1897{
1898 sg_io_hdr_t *hp = &srp->header;
1899 Sg_scatter_hold *schp = &srp->data;
1900 int num_xfer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int dxfer_dir = hp->dxfer_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1903
1904 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
1905 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1906 num_xfer = hp->dxfer_len;
1907 if (schp->bufflen < num_xfer)
1908 num_xfer = schp->bufflen;
1909 }
1910 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1911 (new_interface
1912 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1913 return 0;
1914
1915 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001916 num_xfer, (int)hp->iovec_count, schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return 0;
1918}
1919
1920static int
1921sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1922{
1923 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001924 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1927 num_read_xfer));
1928 if ((!outp) || (num_read_xfer <= 0))
1929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001931 blk_rq_unmap_user(srp->bio);
1932 srp->bio = NULL;
1933
1934 num = 1 << (PAGE_SHIFT + schp->page_order);
1935 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001936 if (num > num_read_xfer) {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001937 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001938 num_read_xfer))
1939 return -EFAULT;
1940 break;
1941 } else {
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001942 if (__copy_to_user(outp, page_address(schp->pages[k]),
Mike Christied6b10342005-11-08 04:06:41 -06001943 num))
1944 return -EFAULT;
1945 num_read_xfer -= num;
1946 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
Mike Christied6b10342005-11-08 04:06:41 -06001948 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Mike Christied6b10342005-11-08 04:06:41 -06001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953}
1954
1955static void
1956sg_build_reserve(Sg_fd * sfp, int req_size)
1957{
1958 Sg_scatter_hold *schp = &sfp->reserve;
1959
1960 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1961 do {
1962 if (req_size < PAGE_SIZE)
1963 req_size = PAGE_SIZE;
1964 if (0 == sg_build_indirect(schp, sfp, req_size))
1965 return;
1966 else
1967 sg_remove_scat(schp);
1968 req_size >>= 1; /* divide by 2 */
1969 } while (req_size > (PAGE_SIZE / 2));
1970}
1971
1972static void
1973sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1974{
1975 Sg_scatter_hold *req_schp = &srp->data;
1976 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06001977 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 srp->res_used = 1;
1980 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06001981 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001983 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1984 for (k = 0; k < rsv_schp->k_use_sg; k++) {
Mike Christied6b10342005-11-08 04:06:41 -06001985 if (rem <= num) {
Mike Christied6b10342005-11-08 04:06:41 -06001986 req_schp->k_use_sg = k + 1;
1987 req_schp->sglist_len = rsv_schp->sglist_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001988 req_schp->pages = rsv_schp->pages;
Mike Christied6b10342005-11-08 04:06:41 -06001989
1990 req_schp->bufflen = size;
1991 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02001992 req_schp->page_order = rsv_schp->page_order;
Mike Christied6b10342005-11-08 04:06:41 -06001993 break;
1994 } else
1995 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Mike Christied6b10342005-11-08 04:06:41 -06001997
1998 if (k >= rsv_schp->k_use_sg)
1999 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002static void
2003sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2004{
2005 Sg_scatter_hold *req_schp = &srp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2008 (int) req_schp->k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 req_schp->k_use_sg = 0;
2010 req_schp->bufflen = 0;
FUJITA Tomonori10db10d2008-08-29 12:32:18 +02002011 req_schp->pages = NULL;
2012 req_schp->page_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 req_schp->sglist_len = 0;
2014 sfp->save_scat_len = 0;
2015 srp->res_used = 0;
2016}
2017
2018static Sg_request *
2019sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2020{
2021 Sg_request *resp;
2022 unsigned long iflags;
2023
2024 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2025 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2026 /* look for requests that are ready + not SG_IO owned */
2027 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2028 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2029 resp->done = 2; /* guard against other readers */
2030 break;
2031 }
2032 }
2033 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2034 return resp;
2035}
2036
2037#ifdef CONFIG_SCSI_PROC_FS
2038static Sg_request *
2039sg_get_nth_request(Sg_fd * sfp, int nth)
2040{
2041 Sg_request *resp;
2042 unsigned long iflags;
2043 int k;
2044
2045 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2046 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2047 ++k, resp = resp->nextrp) ;
2048 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2049 return resp;
2050}
2051#endif
2052
2053/* always adds to end of list */
2054static Sg_request *
2055sg_add_request(Sg_fd * sfp)
2056{
2057 int k;
2058 unsigned long iflags;
2059 Sg_request *resp;
2060 Sg_request *rp = sfp->req_arr;
2061
2062 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2063 resp = sfp->headrp;
2064 if (!resp) {
2065 memset(rp, 0, sizeof (Sg_request));
2066 rp->parentfp = sfp;
2067 resp = rp;
2068 sfp->headrp = resp;
2069 } else {
2070 if (0 == sfp->cmd_q)
2071 resp = NULL; /* command queuing disallowed */
2072 else {
2073 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2074 if (!rp->parentfp)
2075 break;
2076 }
2077 if (k < SG_MAX_QUEUE) {
2078 memset(rp, 0, sizeof (Sg_request));
2079 rp->parentfp = sfp;
2080 while (resp->nextrp)
2081 resp = resp->nextrp;
2082 resp->nextrp = rp;
2083 resp = rp;
2084 } else
2085 resp = NULL;
2086 }
2087 }
2088 if (resp) {
2089 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002090 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2093 return resp;
2094}
2095
2096/* Return of 1 for found; 0 for not found */
2097static int
2098sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2099{
2100 Sg_request *prev_rp;
2101 Sg_request *rp;
2102 unsigned long iflags;
2103 int res = 0;
2104
2105 if ((!sfp) || (!srp) || (!sfp->headrp))
2106 return res;
2107 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2108 prev_rp = sfp->headrp;
2109 if (srp == prev_rp) {
2110 sfp->headrp = prev_rp->nextrp;
2111 prev_rp->parentfp = NULL;
2112 res = 1;
2113 } else {
2114 while ((rp = prev_rp->nextrp)) {
2115 if (srp == rp) {
2116 prev_rp->nextrp = rp->nextrp;
2117 rp->parentfp = NULL;
2118 res = 1;
2119 break;
2120 }
2121 prev_rp = rp;
2122 }
2123 }
2124 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2125 return res;
2126}
2127
2128#ifdef CONFIG_SCSI_PROC_FS
2129static Sg_fd *
2130sg_get_nth_sfp(Sg_device * sdp, int nth)
2131{
2132 Sg_fd *resp;
2133 unsigned long iflags;
2134 int k;
2135
James Bottomley7c07d612007-08-05 13:36:11 -05002136 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2138 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002139 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return resp;
2141}
2142#endif
2143
2144static Sg_fd *
2145sg_add_sfp(Sg_device * sdp, int dev)
2146{
2147 Sg_fd *sfp;
2148 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002149 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Mike Christied6b10342005-11-08 04:06:41 -06002151 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (!sfp)
2153 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 init_waitqueue_head(&sfp->read_wait);
2156 rwlock_init(&sfp->rq_list_lock);
2157
2158 sfp->timeout = SG_DEFAULT_TIMEOUT;
2159 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2160 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2161 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2162 sdp->device->host->unchecked_isa_dma : 1;
2163 sfp->cmd_q = SG_DEF_COMMAND_Q;
2164 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2165 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002166 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (!sdp->headfp)
2168 sdp->headfp = sfp;
2169 else { /* add to tail of existing list */
2170 Sg_fd *pfp = sdp->headfp;
2171 while (pfp->nextfp)
2172 pfp = pfp->nextfp;
2173 pfp->nextfp = sfp;
2174 }
James Bottomley7c07d612007-08-05 13:36:11 -05002175 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002177 if (unlikely(sg_big_buff != def_reserved_size))
2178 sg_big_buff = def_reserved_size;
2179
Alan Stern44ec9542007-02-20 11:01:57 -05002180 bufflen = min_t(int, sg_big_buff,
2181 sdp->device->request_queue->max_sectors * 512);
2182 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2184 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2185 return sfp;
2186}
2187
2188static void
2189__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2190{
2191 Sg_fd *fp;
2192 Sg_fd *prev_fp;
2193
2194 prev_fp = sdp->headfp;
2195 if (sfp == prev_fp)
2196 sdp->headfp = prev_fp->nextfp;
2197 else {
2198 while ((fp = prev_fp->nextfp)) {
2199 if (sfp == fp) {
2200 prev_fp->nextfp = fp->nextfp;
2201 break;
2202 }
2203 prev_fp = fp;
2204 }
2205 }
2206 if (sfp->reserve.bufflen > 0) {
2207 SCSI_LOG_TIMEOUT(6,
2208 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2209 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 sg_remove_scat(&sfp->reserve);
2211 }
2212 sfp->parentdp = NULL;
2213 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002214 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217/* Returns 0 in normal case, 1 when detached and sdp object removed */
2218static int
2219sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2220{
2221 Sg_request *srp;
2222 Sg_request *tsrp;
2223 int dirty = 0;
2224 int res = 0;
2225
2226 for (srp = sfp->headrp; srp; srp = tsrp) {
2227 tsrp = srp->nextrp;
2228 if (sg_srp_done(srp, sfp))
2229 sg_finish_rem_req(srp);
2230 else
2231 ++dirty;
2232 }
2233 if (0 == dirty) {
2234 unsigned long iflags;
2235
James Bottomley7c07d612007-08-05 13:36:11 -05002236 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 __sg_remove_sfp(sdp, sfp);
2238 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002239 idr_remove(&sg_index_idr, sdp->index);
2240 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 res = 1;
2242 }
James Bottomley7c07d612007-08-05 13:36:11 -05002243 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 } else {
2245 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2246 /* only bump the access_count if we actually succeeded in
2247 * throwing another counter on the host module */
2248 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2249 sfp->closed = 1; /* flag dirty state on this fd */
2250 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2251 dirty));
2252 }
2253 return res;
2254}
2255
2256static int
2257sg_res_in_use(Sg_fd * sfp)
2258{
2259 const Sg_request *srp;
2260 unsigned long iflags;
2261
2262 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2263 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2264 if (srp->res_used)
2265 break;
2266 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2267 return srp ? 1 : 0;
2268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270#ifdef CONFIG_SCSI_PROC_FS
2271static int
James Bottomley7c07d612007-08-05 13:36:11 -05002272sg_idr_max_id(int id, void *p, void *data)
2273{
2274 int *k = data;
2275
2276 if (*k < id)
2277 *k = id;
2278
2279 return 0;
2280}
2281
2282static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283sg_last_dev(void)
2284{
Tony Battersby53474c02008-01-22 15:25:49 -05002285 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 unsigned long iflags;
2287
James Bottomley7c07d612007-08-05 13:36:11 -05002288 read_lock_irqsave(&sg_index_lock, iflags);
2289 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2290 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return k + 1; /* origin 1 */
2292}
2293#endif
2294
2295static Sg_device *
2296sg_get_dev(int dev)
2297{
James Bottomley7c07d612007-08-05 13:36:11 -05002298 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 unsigned long iflags;
2300
James Bottomley7c07d612007-08-05 13:36:11 -05002301 read_lock_irqsave(&sg_index_lock, iflags);
2302 sdp = idr_find(&sg_index_idr, dev);
2303 read_unlock_irqrestore(&sg_index_lock, iflags);
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return sdp;
2306}
2307
2308#ifdef CONFIG_SCSI_PROC_FS
2309
2310static struct proc_dir_entry *sg_proc_sgp = NULL;
2311
2312static char sg_proc_sg_dirname[] = "scsi/sg";
2313
2314static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2315
2316static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2317static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2318 size_t count, loff_t *off);
2319static struct file_operations adio_fops = {
2320 /* .owner, .read and .llseek added in sg_proc_init() */
2321 .open = sg_proc_single_open_adio,
2322 .write = sg_proc_write_adio,
2323 .release = single_release,
2324};
2325
2326static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2327static ssize_t sg_proc_write_dressz(struct file *filp,
2328 const char __user *buffer, size_t count, loff_t *off);
2329static struct file_operations dressz_fops = {
2330 .open = sg_proc_single_open_dressz,
2331 .write = sg_proc_write_dressz,
2332 .release = single_release,
2333};
2334
2335static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2336static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2337static struct file_operations version_fops = {
2338 .open = sg_proc_single_open_version,
2339 .release = single_release,
2340};
2341
2342static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2343static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2344static struct file_operations devhdr_fops = {
2345 .open = sg_proc_single_open_devhdr,
2346 .release = single_release,
2347};
2348
2349static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2350static int sg_proc_open_dev(struct inode *inode, struct file *file);
2351static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2352static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2353static void dev_seq_stop(struct seq_file *s, void *v);
2354static struct file_operations dev_fops = {
2355 .open = sg_proc_open_dev,
2356 .release = seq_release,
2357};
2358static struct seq_operations dev_seq_ops = {
2359 .start = dev_seq_start,
2360 .next = dev_seq_next,
2361 .stop = dev_seq_stop,
2362 .show = sg_proc_seq_show_dev,
2363};
2364
2365static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2366static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2367static struct file_operations devstrs_fops = {
2368 .open = sg_proc_open_devstrs,
2369 .release = seq_release,
2370};
2371static struct seq_operations devstrs_seq_ops = {
2372 .start = dev_seq_start,
2373 .next = dev_seq_next,
2374 .stop = dev_seq_stop,
2375 .show = sg_proc_seq_show_devstrs,
2376};
2377
2378static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2379static int sg_proc_open_debug(struct inode *inode, struct file *file);
2380static struct file_operations debug_fops = {
2381 .open = sg_proc_open_debug,
2382 .release = seq_release,
2383};
2384static struct seq_operations debug_seq_ops = {
2385 .start = dev_seq_start,
2386 .next = dev_seq_next,
2387 .stop = dev_seq_stop,
2388 .show = sg_proc_seq_show_debug,
2389};
2390
2391
2392struct sg_proc_leaf {
2393 const char * name;
2394 struct file_operations * fops;
2395};
2396
2397static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2398 {"allow_dio", &adio_fops},
2399 {"debug", &debug_fops},
2400 {"def_reserved_size", &dressz_fops},
2401 {"device_hdr", &devhdr_fops},
2402 {"devices", &dev_fops},
2403 {"device_strs", &devstrs_fops},
2404 {"version", &version_fops}
2405};
2406
2407static int
2408sg_proc_init(void)
2409{
2410 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002411 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct sg_proc_leaf * leaf;
2413
Al Viro66600222005-09-28 22:32:57 +01002414 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (!sg_proc_sgp)
2416 return 1;
2417 for (k = 0; k < num_leaves; ++k) {
2418 leaf = &sg_proc_leaf_arr[k];
2419 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002420 leaf->fops->owner = THIS_MODULE;
2421 leaf->fops->read = seq_read;
2422 leaf->fops->llseek = seq_lseek;
2423 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425 return 0;
2426}
2427
2428static void
2429sg_proc_cleanup(void)
2430{
2431 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002432 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 if (!sg_proc_sgp)
2435 return;
2436 for (k = 0; k < num_leaves; ++k)
2437 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2438 remove_proc_entry(sg_proc_sg_dirname, NULL);
2439}
2440
2441
2442static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2443{
2444 seq_printf(s, "%d\n", *((int *)s->private));
2445 return 0;
2446}
2447
2448static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2449{
2450 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2451}
2452
2453static ssize_t
2454sg_proc_write_adio(struct file *filp, const char __user *buffer,
2455 size_t count, loff_t *off)
2456{
2457 int num;
2458 char buff[11];
2459
2460 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2461 return -EACCES;
2462 num = (count < 10) ? count : 10;
2463 if (copy_from_user(buff, buffer, num))
2464 return -EFAULT;
2465 buff[num] = '\0';
2466 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2467 return count;
2468}
2469
2470static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2471{
2472 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2473}
2474
2475static ssize_t
2476sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2477 size_t count, loff_t *off)
2478{
2479 int num;
2480 unsigned long k = ULONG_MAX;
2481 char buff[11];
2482
2483 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2484 return -EACCES;
2485 num = (count < 10) ? count : 10;
2486 if (copy_from_user(buff, buffer, num))
2487 return -EFAULT;
2488 buff[num] = '\0';
2489 k = simple_strtoul(buff, NULL, 10);
2490 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2491 sg_big_buff = k;
2492 return count;
2493 }
2494 return -ERANGE;
2495}
2496
2497static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2498{
2499 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2500 sg_version_date);
2501 return 0;
2502}
2503
2504static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2505{
2506 return single_open(file, sg_proc_seq_show_version, NULL);
2507}
2508
2509static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2510{
2511 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2512 "online\n");
2513 return 0;
2514}
2515
2516static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2517{
2518 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2519}
2520
2521struct sg_proc_deviter {
2522 loff_t index;
2523 size_t max;
2524};
2525
2526static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2527{
2528 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2529
Jan Blunck729d70f2005-08-27 11:07:52 -07002530 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (! it)
2532 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 it->index = *pos;
2535 it->max = sg_last_dev();
2536 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002537 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
2541static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2542{
Jan Blunck729d70f2005-08-27 11:07:52 -07002543 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 *pos = ++it->index;
2546 return (it->index < it->max) ? it : NULL;
2547}
2548
2549static void dev_seq_stop(struct seq_file *s, void *v)
2550{
Jan Blunck729d70f2005-08-27 11:07:52 -07002551 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554static int sg_proc_open_dev(struct inode *inode, struct file *file)
2555{
2556 return seq_open(file, &dev_seq_ops);
2557}
2558
2559static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2560{
2561 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2562 Sg_device *sdp;
2563 struct scsi_device *scsidp;
2564
2565 sdp = it ? sg_get_dev(it->index) : NULL;
2566 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2567 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2568 scsidp->host->host_no, scsidp->channel,
2569 scsidp->id, scsidp->lun, (int) scsidp->type,
2570 1,
2571 (int) scsidp->queue_depth,
2572 (int) scsidp->device_busy,
2573 (int) scsi_device_online(scsidp));
2574 else
2575 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2576 return 0;
2577}
2578
2579static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2580{
2581 return seq_open(file, &devstrs_seq_ops);
2582}
2583
2584static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2585{
2586 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2587 Sg_device *sdp;
2588 struct scsi_device *scsidp;
2589
2590 sdp = it ? sg_get_dev(it->index) : NULL;
2591 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2592 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2593 scsidp->vendor, scsidp->model, scsidp->rev);
2594 else
2595 seq_printf(s, "<no active device>\n");
2596 return 0;
2597}
2598
2599static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2600{
2601 int k, m, new_interface, blen, usg;
2602 Sg_request *srp;
2603 Sg_fd *fp;
2604 const sg_io_hdr_t *hp;
2605 const char * cp;
cb59e842005-04-02 13:51:23 -06002606 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2609 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2610 "(res)sgat=%d low_dma=%d\n", k + 1,
2611 jiffies_to_msecs(fp->timeout),
2612 fp->reserve.bufflen,
2613 (int) fp->reserve.k_use_sg,
2614 (int) fp->low_dma);
2615 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2616 (int) fp->cmd_q, (int) fp->force_packid,
2617 (int) fp->keep_orphan, (int) fp->closed);
2618 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2619 hp = &srp->header;
2620 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2621 if (srp->res_used) {
2622 if (new_interface &&
2623 (SG_FLAG_MMAP_IO & hp->flags))
2624 cp = " mmap>> ";
2625 else
2626 cp = " rb>> ";
2627 } else {
2628 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2629 cp = " dio>> ";
2630 else
2631 cp = " ";
2632 }
2633 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002634 blen = srp->data.bufflen;
2635 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 seq_printf(s, srp->done ?
2637 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002638 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 seq_printf(s, " id=%d blen=%d",
2640 srp->header.pack_id, blen);
2641 if (srp->done)
2642 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002643 else {
2644 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002646 (new_interface ? hp->timeout :
2647 jiffies_to_msecs(fp->timeout)),
2648 (ms > hp->duration ? ms - hp->duration : 0));
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2651 (int) srp->data.cmd_opcode);
2652 }
2653 if (0 == m)
2654 seq_printf(s, " No requests active\n");
2655 }
2656}
2657
2658static int sg_proc_open_debug(struct inode *inode, struct file *file)
2659{
2660 return seq_open(file, &debug_seq_ops);
2661}
2662
2663static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2664{
2665 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2666 Sg_device *sdp;
2667
2668 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002669 seq_printf(s, "max_active_device=%d(origin 1)\n",
2670 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2672 }
2673 sdp = it ? sg_get_dev(it->index) : NULL;
2674 if (sdp) {
2675 struct scsi_device *scsidp = sdp->device;
2676
2677 if (NULL == scsidp) {
2678 seq_printf(s, "device %d detached ??\n",
2679 (int)it->index);
2680 return 0;
2681 }
2682
2683 if (sg_get_nth_sfp(sdp, 0)) {
2684 seq_printf(s, " >>> device=%s ",
2685 sdp->disk->disk_name);
2686 if (sdp->detached)
2687 seq_printf(s, "detached pending close ");
2688 else
2689 seq_printf
2690 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2691 scsidp->host->host_no,
2692 scsidp->channel, scsidp->id,
2693 scsidp->lun,
2694 scsidp->host->hostt->emulated);
2695 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2696 sdp->sg_tablesize, sdp->exclude);
2697 }
2698 sg_proc_debug_helper(s, sdp);
2699 }
2700 return 0;
2701}
2702
2703#endif /* CONFIG_SCSI_PROC_FS */
2704
2705module_init(init_sg);
2706module_exit(exit_sg);