blob: cb6de0752ee1318ba9e4b708f2e39256aa9e1008 [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>
David Hardeman378f0582005-09-17 17:55:31 +100050#include <linux/scatterlist.h>
Christof Schmitt6da127a2008-01-11 10:09:43 +010051#include <linux/blktrace_api.h>
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -060052#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "scsi.h"
db9dff32005-04-03 14:53:59 -050055#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <scsi/scsi_driver.h>
58#include <scsi/scsi_ioctl.h>
59#include <scsi/sg.h>
60
61#include "scsi_logging.h"
62
63#ifdef CONFIG_SCSI_PROC_FS
64#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040065static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int sg_proc_init(void);
68static void sg_proc_cleanup(void);
69#endif
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SG_ALLOW_DIO_DEF 0
72#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
73
74#define SG_MAX_DEVS 32768
75
76/*
77 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
78 * Then when using 32 bit integers x * m may overflow during the calculation.
79 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
80 * calculates the same, but prevents the overflow when both m and d
81 * are "small" numbers (like HZ and USER_HZ).
82 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
83 * in 32 bits.
84 */
85#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
86
87#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
88
89int sg_big_buff = SG_DEF_RESERVED_SIZE;
90/* N.B. This variable is readable and writeable via
91 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
92 of this size (or less if there is not enough memory) will be reserved
93 for use by this file descriptor. [Deprecated usage: this variable is also
94 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
95 the kernel (i.e. it is not a module).] */
96static int def_reserved_size = -1; /* picks up init parameter */
97static int sg_allow_dio = SG_ALLOW_DIO_DEF;
98
Douglas Gilbert6460e752006-09-20 18:20:49 -040099static int scatter_elem_sz = SG_SCATTER_SZ;
100static int scatter_elem_sz_prev = SG_SCATTER_SZ;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define SG_SECTOR_SZ 512
103#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
104
Tony Jonesee959b02008-02-22 00:13:36 +0100105static int sg_add(struct device *, struct class_interface *);
106static void sg_remove(struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
James Bottomley7c07d612007-08-05 13:36:11 -0500108static DEFINE_IDR(sg_index_idr);
109static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 file descriptor list for device */
111
112static struct class_interface sg_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100113 .add_dev = sg_add,
114 .remove_dev = sg_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
118 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900119 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned bufflen; /* Size of (aggregate) data buffer */
121 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
Mike Christied6b10342005-11-08 04:06:41 -0600122 struct scatterlist *buffer;/* scatter list */
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 */
180static void sg_cmd_done(void *data, char *sense, int result, int resid);
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);
193static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
194 int wr_xf, int *countp, unsigned char __user **up);
195static int sg_write_xfer(Sg_request * srp);
196static int sg_read_xfer(Sg_request * srp);
197static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
198static void sg_remove_scat(Sg_scatter_hold * schp);
199static void sg_build_reserve(Sg_fd * sfp, int req_size);
200static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
201static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600202static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
203static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
205static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
206static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
207static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
208static Sg_request *sg_add_request(Sg_fd * sfp);
209static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
210static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
212static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#ifdef CONFIG_SCSI_PROC_FS
214static int sg_last_dev(void);
215#endif
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define SZ_SG_HEADER sizeof(struct sg_header)
218#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
219#define SZ_SG_IOVEC sizeof(sg_iovec_t)
220#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
221
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900222static int sg_allow_access(struct file *filp, unsigned char *cmd)
223{
224 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
225 struct request_queue *q = sfp->parentdp->device->request_queue;
226
227 if (sfp->parentdp->device->type == TYPE_SCANNER)
228 return 0;
229
230 return blk_verify_command(&q->cmd_filter,
231 cmd, filp->f_mode & FMODE_WRITE);
232}
233
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900234static void sg_rq_end_io(struct request *rq, int uptodate)
235{
236 sg_cmd_done(rq->end_io_data, rq->sense, rq->errors, rq->data_len);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int
240sg_open(struct inode *inode, struct file *filp)
241{
242 int dev = iminor(inode);
243 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600244 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 Sg_device *sdp;
246 Sg_fd *sfp;
247 int res;
248 int retval;
249
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600250 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 nonseekable_open(inode, filp);
252 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
253 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600254 if ((!sdp) || (!sdp->device)) {
255 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600257 }
258 if (sdp->detached) {
259 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* This driver's module count bumped by fops_get in <linux/fs.h> */
264 /* Prevent the device driver from vanishing while we sleep */
265 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600266 if (retval) {
267 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (!((flags & O_NONBLOCK) ||
272 scsi_block_when_processing_errors(sdp->device))) {
273 retval = -ENXIO;
274 /* we are in error recovery for this device */
275 goto error_out;
276 }
277
278 if (flags & O_EXCL) {
279 if (O_RDONLY == (flags & O_ACCMODE)) {
280 retval = -EPERM; /* Can't lock it with read only access */
281 goto error_out;
282 }
283 if (sdp->headfp && (flags & O_NONBLOCK)) {
284 retval = -EBUSY;
285 goto error_out;
286 }
287 res = 0;
288 __wait_event_interruptible(sdp->o_excl_wait,
289 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
290 if (res) {
291 retval = res; /* -ERESTARTSYS because signal hit process */
292 goto error_out;
293 }
294 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
295 if (flags & O_NONBLOCK) {
296 retval = -EBUSY;
297 goto error_out;
298 }
299 res = 0;
300 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
301 res);
302 if (res) {
303 retval = res; /* -ERESTARTSYS because signal hit process */
304 goto error_out;
305 }
306 }
307 if (sdp->detached) {
308 retval = -ENODEV;
309 goto error_out;
310 }
311 if (!sdp->headfp) { /* no existing opens on this device */
312 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600313 q = sdp->device->request_queue;
314 sdp->sg_tablesize = min(q->max_hw_segments,
315 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 if ((sfp = sg_add_sfp(sdp, dev)))
318 filp->private_data = sfp;
319 else {
320 if (flags & O_EXCL)
321 sdp->exclude = 0; /* undo if error */
322 retval = -ENOMEM;
323 goto error_out;
324 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600325 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327
328 error_out:
329 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600330 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return retval;
332}
333
334/* Following function was formerly called 'sg_close' */
335static int
336sg_release(struct inode *inode, struct file *filp)
337{
338 Sg_device *sdp;
339 Sg_fd *sfp;
340
341 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
342 return -ENXIO;
343 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
344 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
345 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
346 if (!sdp->detached) {
347 scsi_device_put(sdp->device);
348 }
349 sdp->exclude = 0;
350 wake_up_interruptible(&sdp->o_excl_wait);
351 }
352 return 0;
353}
354
355static ssize_t
356sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 Sg_device *sdp;
359 Sg_fd *sfp;
360 Sg_request *srp;
361 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600363 struct sg_header *old_hdr = NULL;
364 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
367 return -ENXIO;
368 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
369 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!access_ok(VERIFY_WRITE, buf, count))
372 return -EFAULT;
373 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600374 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
375 if (!old_hdr)
376 return -ENOMEM;
377 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
378 retval = -EFAULT;
379 goto free_old_hdr;
380 }
381 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600383 sg_io_hdr_t *new_hdr;
384 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
385 if (!new_hdr) {
386 retval = -ENOMEM;
387 goto free_old_hdr;
388 }
389 retval =__copy_from_user
390 (new_hdr, buf, SZ_SG_IO_HDR);
391 req_pack_id = new_hdr->pack_id;
392 kfree(new_hdr);
393 if (retval) {
394 retval = -EFAULT;
395 goto free_old_hdr;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 } else
cb59e842005-04-02 13:51:23 -0600399 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 srp = sg_get_rq_mark(sfp, req_pack_id);
402 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600403 if (sdp->detached) {
404 retval = -ENODEV;
405 goto free_old_hdr;
406 }
407 if (filp->f_flags & O_NONBLOCK) {
408 retval = -EAGAIN;
409 goto free_old_hdr;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 while (1) {
cb59e842005-04-02 13:51:23 -0600412 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600414 (sdp->detached ||
415 (srp = sg_get_rq_mark(sfp, req_pack_id))),
416 retval);
417 if (sdp->detached) {
418 retval = -ENODEV;
419 goto free_old_hdr;
420 }
421 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
cb59e842005-04-02 13:51:23 -0600423
424 /* -ERESTARTSYS as signal hit process */
425 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
cb59e842005-04-02 13:51:23 -0600428 if (srp->header.interface_id != '\0') {
429 retval = sg_new_read(sfp, buf, count, srp);
430 goto free_old_hdr;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600434 if (old_hdr == NULL) {
435 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
436 if (! old_hdr) {
437 retval = -ENOMEM;
438 goto free_old_hdr;
439 }
440 }
441 memset(old_hdr, 0, SZ_SG_HEADER);
442 old_hdr->reply_len = (int) hp->timeout;
443 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
444 old_hdr->pack_id = hp->pack_id;
445 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600447 old_hdr->target_status = hp->masked_status;
448 old_hdr->host_status = hp->host_status;
449 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if ((CHECK_CONDITION & hp->masked_status) ||
451 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600452 memcpy(old_hdr->sense_buffer, srp->sense_b,
453 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 switch (hp->host_status) {
455 /* This setup of 'result' is for backward compatibility and is best
456 ignored by the user who should use target, host + driver status */
457 case DID_OK:
458 case DID_PASSTHROUGH:
459 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600460 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 case DID_NO_CONNECT:
463 case DID_BUS_BUSY:
464 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600465 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 case DID_BAD_TARGET:
468 case DID_ABORT:
469 case DID_PARITY:
470 case DID_RESET:
471 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600472 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600475 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 hp->masked_status == GOOD) ? 0 : EIO;
477 break;
478 default:
cb59e842005-04-02 13:51:23 -0600479 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 }
482
483 /* Now copy the result back to the user buffer. */
484 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600485 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
486 retval = -EFAULT;
487 goto free_old_hdr;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600490 if (count > old_hdr->reply_len)
491 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600493 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
494 retval = -EFAULT;
495 goto free_old_hdr;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 } else
cb59e842005-04-02 13:51:23 -0600499 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600501 retval = count;
502free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800503 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600504 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static ssize_t
508sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
509{
510 sg_io_hdr_t *hp = &srp->header;
511 int err = 0;
512 int len;
513
514 if (count < SZ_SG_IO_HDR) {
515 err = -EINVAL;
516 goto err_out;
517 }
518 hp->sb_len_wr = 0;
519 if ((hp->mx_sb_len > 0) && hp->sbp) {
520 if ((CHECK_CONDITION & hp->masked_status) ||
521 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600522 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
524 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
525 len = (len > sb_len) ? sb_len : len;
526 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
527 err = -EFAULT;
528 goto err_out;
529 }
530 hp->sb_len_wr = len;
531 }
532 }
533 if (hp->masked_status || hp->host_status || hp->driver_status)
534 hp->info |= SG_INFO_CHECK;
535 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
536 err = -EFAULT;
537 goto err_out;
538 }
539 err = sg_read_xfer(srp);
540 err_out:
541 sg_finish_rem_req(srp);
542 return (0 == err) ? count : err;
543}
544
545static ssize_t
546sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
547{
548 int mxsize, cmd_size, k;
549 int input_size, blocking;
550 unsigned char opcode;
551 Sg_device *sdp;
552 Sg_fd *sfp;
553 Sg_request *srp;
554 struct sg_header old_hdr;
555 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600556 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
559 return -ENXIO;
560 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
561 sdp->disk->disk_name, (int) count));
562 if (sdp->detached)
563 return -ENODEV;
564 if (!((filp->f_flags & O_NONBLOCK) ||
565 scsi_block_when_processing_errors(sdp->device)))
566 return -ENXIO;
567
568 if (!access_ok(VERIFY_READ, buf, count))
569 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
570 if (count < SZ_SG_HEADER)
571 return -EIO;
572 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
573 return -EFAULT;
574 blocking = !(filp->f_flags & O_NONBLOCK);
575 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200576 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (count < (SZ_SG_HEADER + 6))
578 return -EIO; /* The minimum scsi command length is 6 bytes. */
579
580 if (!(srp = sg_add_request(sfp))) {
581 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
582 return -EDOM;
583 }
584 buf += SZ_SG_HEADER;
585 __get_user(opcode, buf);
586 if (sfp->next_cmd_len > 0) {
587 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
588 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
589 sfp->next_cmd_len = 0;
590 sg_remove_request(sfp, srp);
591 return -EIO;
592 }
593 cmd_size = sfp->next_cmd_len;
594 sfp->next_cmd_len = 0; /* reset so only this write() effected */
595 } else {
596 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
597 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
598 cmd_size = 12;
599 }
600 SCSI_LOG_TIMEOUT(4, printk(
601 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
602/* Determine buffer size. */
603 input_size = count - cmd_size;
604 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
605 mxsize -= SZ_SG_HEADER;
606 input_size -= SZ_SG_HEADER;
607 if (input_size < 0) {
608 sg_remove_request(sfp, srp);
609 return -EIO; /* User did not pass enough bytes for this command. */
610 }
611 hp = &srp->header;
612 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
613 hp->cmd_len = (unsigned char) cmd_size;
614 hp->iovec_count = 0;
615 hp->mx_sb_len = 0;
616 if (input_size > 0)
617 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
618 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
619 else
620 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
621 hp->dxfer_len = mxsize;
622 hp->dxferp = (char __user *)buf + cmd_size;
623 hp->sbp = NULL;
624 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
625 hp->flags = input_size; /* structure abuse ... */
626 hp->pack_id = old_hdr.pack_id;
627 hp->usr_ptr = NULL;
628 if (__copy_from_user(cmnd, buf, cmd_size))
629 return -EFAULT;
630 /*
631 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
632 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
633 * is a non-zero input_size, so emit a warning.
634 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100635 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
636 static char cmd[TASK_COMM_LEN];
637 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 printk(KERN_WARNING
639 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
640 "guessing data in;\n" KERN_WARNING " "
641 "program %s not setting count and/or reply_len properly\n",
642 old_hdr.reply_len - (int)SZ_SG_HEADER,
643 input_size, (unsigned int) cmnd[0],
644 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100645 strcpy(cmd, current->comm);
646 }
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
649 return (k < 0) ? k : count;
650}
651
652static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200653sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
654 size_t count, int blocking, int read_only,
655 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int k;
658 Sg_request *srp;
659 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600660 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int timeout;
662 unsigned long ul_timeout;
663
664 if (count < SZ_SG_IO_HDR)
665 return -EINVAL;
666 if (!access_ok(VERIFY_READ, buf, count))
667 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
668
669 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
670 if (!(srp = sg_add_request(sfp))) {
671 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
672 return -EDOM;
673 }
674 hp = &srp->header;
675 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
676 sg_remove_request(sfp, srp);
677 return -EFAULT;
678 }
679 if (hp->interface_id != 'S') {
680 sg_remove_request(sfp, srp);
681 return -ENOSYS;
682 }
683 if (hp->flags & SG_FLAG_MMAP_IO) {
684 if (hp->dxfer_len > sfp->reserve.bufflen) {
685 sg_remove_request(sfp, srp);
686 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
687 }
688 if (hp->flags & SG_FLAG_DIRECT_IO) {
689 sg_remove_request(sfp, srp);
690 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
691 }
692 if (sg_res_in_use(sfp)) {
693 sg_remove_request(sfp, srp);
694 return -EBUSY; /* reserve buffer already being used */
695 }
696 }
697 ul_timeout = msecs_to_jiffies(srp->header.timeout);
698 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
699 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
700 sg_remove_request(sfp, srp);
701 return -EMSGSIZE;
702 }
703 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
704 sg_remove_request(sfp, srp);
705 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
706 }
707 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
708 sg_remove_request(sfp, srp);
709 return -EFAULT;
710 }
FUJITA Tomonori14e507b2008-07-26 18:03:24 +0900711 if (read_only && sg_allow_access(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 sg_remove_request(sfp, srp);
713 return -EPERM;
714 }
715 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
716 if (k < 0)
717 return k;
718 if (o_srp)
719 *o_srp = srp;
720 return count;
721}
722
723static int
724sg_common_write(Sg_fd * sfp, Sg_request * srp,
725 unsigned char *cmnd, int timeout, int blocking)
726{
Mike Christied6b10342005-11-08 04:06:41 -0600727 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 Sg_device *sdp = sfp->parentdp;
729 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
732 hp->status = 0;
733 hp->masked_status = 0;
734 hp->msg_status = 0;
735 hp->info = 0;
736 hp->host_status = 0;
737 hp->driver_status = 0;
738 hp->resid = 0;
739 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
740 (int) cmnd[0], (int) hp->cmd_len));
741
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900742 k = sg_start_req(srp, cmnd);
743 if (k) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400744 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 sg_finish_rem_req(srp);
746 return k; /* probably out of space --> ENOMEM */
747 }
748 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400749 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 sg_finish_rem_req(srp);
751 return k;
752 }
753 if (sdp->detached) {
754 sg_finish_rem_req(srp);
755 return -ENODEV;
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 switch (hp->dxfer_direction) {
759 case SG_DXFER_TO_FROM_DEV:
760 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600761 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600764 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600767 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
769 default:
Mike Christied6b10342005-11-08 04:06:41 -0600770 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 }
cb59e842005-04-02 13:51:23 -0600773 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/* Now send everything of to mid-level. The next time we hear about this
775 packet is when sg_cmd_done() is called (i.e. a callback). */
FUJITA Tomonori10865df2008-08-28 16:17:07 +0900776 if (srp->rq) {
777 srp->rq->timeout = timeout;
778 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
779 srp->rq, 1, sg_rq_end_io);
780 return 0;
781 }
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600782 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600783 hp->dxfer_len, srp->data.k_use_sg, timeout,
784 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
785 GFP_ATOMIC)) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400786 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
Mike Christied6b10342005-11-08 04:06:41 -0600787 /*
788 * most likely out of mem, but could also be a bad map
789 */
Mike Christie18c49b82006-03-22 16:04:38 -0600790 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600791 return -ENOMEM;
792 } else
793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796static int
797sg_srp_done(Sg_request *srp, Sg_fd *sfp)
798{
799 unsigned long iflags;
800 int done;
801
802 read_lock_irqsave(&sfp->rq_list_lock, iflags);
803 done = srp->done;
804 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
805 return done;
806}
807
808static int
809sg_ioctl(struct inode *inode, struct file *filp,
810 unsigned int cmd_in, unsigned long arg)
811{
812 void __user *p = (void __user *)arg;
813 int __user *ip = p;
814 int result, val, read_only;
815 Sg_device *sdp;
816 Sg_fd *sfp;
817 Sg_request *srp;
818 unsigned long iflags;
819
820 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
821 return -ENXIO;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
824 sdp->disk->disk_name, (int) cmd_in));
825 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
826
827 switch (cmd_in) {
828 case SG_IO:
829 {
830 int blocking = 1; /* ignore O_NONBLOCK flag */
831
832 if (sdp->detached)
833 return -ENODEV;
834 if (!scsi_block_when_processing_errors(sdp->device))
835 return -ENXIO;
836 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
837 return -EFAULT;
838 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200839 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 blocking, read_only, &srp);
841 if (result < 0)
842 return result;
843 srp->sg_io_owned = 1;
844 while (1) {
845 result = 0; /* following macro to beat race condition */
846 __wait_event_interruptible(sfp->read_wait,
847 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
848 result);
849 if (sdp->detached)
850 return -ENODEV;
851 if (sfp->closed)
852 return 0; /* request packet dropped already */
853 if (0 == result)
854 break;
855 srp->orphan = 1;
856 return result; /* -ERESTARTSYS because signal hit process */
857 }
858 write_lock_irqsave(&sfp->rq_list_lock, iflags);
859 srp->done = 2;
860 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
861 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
862 return (result < 0) ? result : 0;
863 }
864 case SG_SET_TIMEOUT:
865 result = get_user(val, ip);
866 if (result)
867 return result;
868 if (val < 0)
869 return -EIO;
870 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
871 val = MULDIV (INT_MAX, USER_HZ, HZ);
872 sfp->timeout_user = val;
873 sfp->timeout = MULDIV (val, HZ, USER_HZ);
874
875 return 0;
876 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
877 /* strange ..., for backward compatibility */
878 return sfp->timeout_user;
879 case SG_SET_FORCE_LOW_DMA:
880 result = get_user(val, ip);
881 if (result)
882 return result;
883 if (val) {
884 sfp->low_dma = 1;
885 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
886 val = (int) sfp->reserve.bufflen;
887 sg_remove_scat(&sfp->reserve);
888 sg_build_reserve(sfp, val);
889 }
890 } else {
891 if (sdp->detached)
892 return -ENODEV;
893 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
894 }
895 return 0;
896 case SG_GET_LOW_DMA:
897 return put_user((int) sfp->low_dma, ip);
898 case SG_GET_SCSI_ID:
899 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
900 return -EFAULT;
901 else {
902 sg_scsi_id_t __user *sg_idp = p;
903
904 if (sdp->detached)
905 return -ENODEV;
906 __put_user((int) sdp->device->host->host_no,
907 &sg_idp->host_no);
908 __put_user((int) sdp->device->channel,
909 &sg_idp->channel);
910 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
911 __put_user((int) sdp->device->lun, &sg_idp->lun);
912 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
913 __put_user((short) sdp->device->host->cmd_per_lun,
914 &sg_idp->h_cmd_per_lun);
915 __put_user((short) sdp->device->queue_depth,
916 &sg_idp->d_queue_depth);
917 __put_user(0, &sg_idp->unused[0]);
918 __put_user(0, &sg_idp->unused[1]);
919 return 0;
920 }
921 case SG_SET_FORCE_PACK_ID:
922 result = get_user(val, ip);
923 if (result)
924 return result;
925 sfp->force_packid = val ? 1 : 0;
926 return 0;
927 case SG_GET_PACK_ID:
928 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
929 return -EFAULT;
930 read_lock_irqsave(&sfp->rq_list_lock, iflags);
931 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
932 if ((1 == srp->done) && (!srp->sg_io_owned)) {
933 read_unlock_irqrestore(&sfp->rq_list_lock,
934 iflags);
935 __put_user(srp->header.pack_id, ip);
936 return 0;
937 }
938 }
939 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
940 __put_user(-1, ip);
941 return 0;
942 case SG_GET_NUM_WAITING:
943 read_lock_irqsave(&sfp->rq_list_lock, iflags);
944 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
945 if ((1 == srp->done) && (!srp->sg_io_owned))
946 ++val;
947 }
948 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
949 return put_user(val, ip);
950 case SG_GET_SG_TABLESIZE:
951 return put_user(sdp->sg_tablesize, ip);
952 case SG_SET_RESERVED_SIZE:
953 result = get_user(val, ip);
954 if (result)
955 return result;
956 if (val < 0)
957 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500958 val = min_t(int, val,
959 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (val != sfp->reserve.bufflen) {
961 if (sg_res_in_use(sfp) || sfp->mmap_called)
962 return -EBUSY;
963 sg_remove_scat(&sfp->reserve);
964 sg_build_reserve(sfp, val);
965 }
966 return 0;
967 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500968 val = min_t(int, sfp->reserve.bufflen,
969 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return put_user(val, ip);
971 case SG_SET_COMMAND_Q:
972 result = get_user(val, ip);
973 if (result)
974 return result;
975 sfp->cmd_q = val ? 1 : 0;
976 return 0;
977 case SG_GET_COMMAND_Q:
978 return put_user((int) sfp->cmd_q, ip);
979 case SG_SET_KEEP_ORPHAN:
980 result = get_user(val, ip);
981 if (result)
982 return result;
983 sfp->keep_orphan = val;
984 return 0;
985 case SG_GET_KEEP_ORPHAN:
986 return put_user((int) sfp->keep_orphan, ip);
987 case SG_NEXT_CMD_LEN:
988 result = get_user(val, ip);
989 if (result)
990 return result;
991 sfp->next_cmd_len = (val > 0) ? val : 0;
992 return 0;
993 case SG_GET_VERSION_NUM:
994 return put_user(sg_version_num, ip);
995 case SG_GET_ACCESS_COUNT:
996 /* faked - we don't have a real access count anymore */
997 val = (sdp->device ? 1 : 0);
998 return put_user(val, ip);
999 case SG_GET_REQUEST_TABLE:
1000 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
1001 return -EFAULT;
1002 else {
cb59e842005-04-02 13:51:23 -06001003 sg_req_info_t *rinfo;
1004 unsigned int ms;
1005
1006 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
1007 GFP_KERNEL);
1008 if (!rinfo)
1009 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1011 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
1012 ++val, srp = srp ? srp->nextrp : srp) {
1013 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
1014 if (srp) {
1015 rinfo[val].req_state = srp->done + 1;
1016 rinfo[val].problem =
1017 srp->header.masked_status &
1018 srp->header.host_status &
1019 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -06001020 if (srp->done)
1021 rinfo[val].duration =
1022 srp->header.duration;
1023 else {
1024 ms = jiffies_to_msecs(jiffies);
1025 rinfo[val].duration =
1026 (ms > srp->header.duration) ?
1027 (ms - srp->header.duration) : 0;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001030 rinfo[val].sg_io_owned =
1031 srp->sg_io_owned;
1032 rinfo[val].pack_id =
1033 srp->header.pack_id;
1034 rinfo[val].usr_ptr =
1035 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 }
1038 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001039 result = __copy_to_user(p, rinfo,
1040 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1041 result = result ? -EFAULT : 0;
1042 kfree(rinfo);
1043 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 case SG_EMULATED_HOST:
1046 if (sdp->detached)
1047 return -ENODEV;
1048 return put_user(sdp->device->host->hostt->emulated, ip);
1049 case SG_SCSI_RESET:
1050 if (sdp->detached)
1051 return -ENODEV;
1052 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001053 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EBUSY;
1055 } else if (!scsi_block_when_processing_errors(sdp->device))
1056 return -EBUSY;
1057 result = get_user(val, ip);
1058 if (result)
1059 return result;
1060 if (SG_SCSI_RESET_NOTHING == val)
1061 return 0;
1062 switch (val) {
1063 case SG_SCSI_RESET_DEVICE:
1064 val = SCSI_TRY_RESET_DEVICE;
1065 break;
Brian King39120e12008-07-01 13:03:19 -05001066 case SG_SCSI_RESET_TARGET:
1067 val = SCSI_TRY_RESET_TARGET;
1068 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 case SG_SCSI_RESET_BUS:
1070 val = SCSI_TRY_RESET_BUS;
1071 break;
1072 case SG_SCSI_RESET_HOST:
1073 val = SCSI_TRY_RESET_HOST;
1074 break;
1075 default:
1076 return -EINVAL;
1077 }
1078 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1079 return -EACCES;
1080 return (scsi_reset_provider(sdp->device, val) ==
1081 SUCCESS) ? 0 : -EIO;
1082 case SCSI_IOCTL_SEND_COMMAND:
1083 if (sdp->detached)
1084 return -ENODEV;
1085 if (read_only) {
1086 unsigned char opcode = WRITE_6;
1087 Scsi_Ioctl_Command __user *siocp = p;
1088
1089 if (copy_from_user(&opcode, siocp->data, 1))
1090 return -EFAULT;
FUJITA Tomonori14e507b2008-07-26 18:03:24 +09001091 if (sg_allow_access(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EPERM;
1093 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001094 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 case SG_SET_DEBUG:
1096 result = get_user(val, ip);
1097 if (result)
1098 return result;
1099 sdp->sgdebug = (char) val;
1100 return 0;
1101 case SCSI_IOCTL_GET_IDLUN:
1102 case SCSI_IOCTL_GET_BUS_NUMBER:
1103 case SCSI_IOCTL_PROBE_HOST:
1104 case SG_GET_TRANSFORM:
1105 if (sdp->detached)
1106 return -ENODEV;
1107 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001108 case BLKSECTGET:
1109 return put_user(sdp->device->request_queue->max_sectors * 512,
1110 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001111 case BLKTRACESETUP:
1112 return blk_trace_setup(sdp->device->request_queue,
1113 sdp->disk->disk_name,
1114 sdp->device->sdev_gendev.devt,
1115 (char *)arg);
1116 case BLKTRACESTART:
1117 return blk_trace_startstop(sdp->device->request_queue, 1);
1118 case BLKTRACESTOP:
1119 return blk_trace_startstop(sdp->device->request_queue, 0);
1120 case BLKTRACETEARDOWN:
1121 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 default:
1123 if (read_only)
1124 return -EPERM; /* don't know so take safe approach */
1125 return scsi_ioctl(sdp->device, cmd_in, p);
1126 }
1127}
1128
1129#ifdef CONFIG_COMPAT
1130static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1131{
1132 Sg_device *sdp;
1133 Sg_fd *sfp;
1134 struct scsi_device *sdev;
1135
1136 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1137 return -ENXIO;
1138
1139 sdev = sdp->device;
1140 if (sdev->host->hostt->compat_ioctl) {
1141 int ret;
1142
1143 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1144
1145 return ret;
1146 }
1147
1148 return -ENOIOCTLCMD;
1149}
1150#endif
1151
1152static unsigned int
1153sg_poll(struct file *filp, poll_table * wait)
1154{
1155 unsigned int res = 0;
1156 Sg_device *sdp;
1157 Sg_fd *sfp;
1158 Sg_request *srp;
1159 int count = 0;
1160 unsigned long iflags;
1161
1162 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1163 || sfp->closed)
1164 return POLLERR;
1165 poll_wait(filp, &sfp->read_wait, wait);
1166 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1167 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1168 /* if any read waiting, flag it */
1169 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1170 res = POLLIN | POLLRDNORM;
1171 ++count;
1172 }
1173 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1174
1175 if (sdp->detached)
1176 res |= POLLHUP;
1177 else if (!sfp->cmd_q) {
1178 if (0 == count)
1179 res |= POLLOUT | POLLWRNORM;
1180 } else if (count < SG_MAX_QUEUE)
1181 res |= POLLOUT | POLLWRNORM;
1182 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1183 sdp->disk->disk_name, (int) res));
1184 return res;
1185}
1186
1187static int
1188sg_fasync(int fd, struct file *filp, int mode)
1189{
1190 int retval;
1191 Sg_device *sdp;
1192 Sg_fd *sfp;
1193
1194 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1195 return -ENXIO;
1196 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1197 sdp->disk->disk_name, mode));
1198
1199 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1200 return (retval < 0) ? retval : 0;
1201}
1202
Nick Piggina13ff0b2008-02-07 18:46:06 -08001203static int
1204sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001207 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001209 struct scatterlist *sg;
1210 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001213 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001215 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001217 return VM_FAULT_SIGBUS;
1218 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001220 sg = rsv_schp->buffer;
1221 sa = vma->vm_start;
1222 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001223 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001224 len = vma->vm_end - sa;
1225 len = (len < sg->length) ? len : sg->length;
1226 if (offset < len) {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001227 struct page *page;
Jens Axboe45711f12007-10-22 21:19:53 +02001228 page = virt_to_page(page_address(sg_page(sg)) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001229 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001230 vmf->page = page;
1231 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Mike Christied6b10342005-11-08 04:06:41 -06001233 sa += len;
1234 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Mike Christied6b10342005-11-08 04:06:41 -06001236
Nick Piggina13ff0b2008-02-07 18:46:06 -08001237 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001241 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242};
1243
1244static int
1245sg_mmap(struct file *filp, struct vm_area_struct *vma)
1246{
1247 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001248 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001250 int k;
1251 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1254 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001255 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1257 (void *) vma->vm_start, (int) req_sz));
1258 if (vma->vm_pgoff)
1259 return -EINVAL; /* want no offset */
1260 rsv_schp = &sfp->reserve;
1261 if (req_sz > rsv_schp->bufflen)
1262 return -ENOMEM; /* cannot map more than reserved buffer */
1263
Mike Christied6b10342005-11-08 04:06:41 -06001264 sa = vma->vm_start;
1265 sg = rsv_schp->buffer;
1266 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001267 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001268 len = vma->vm_end - sa;
1269 len = (len < sg->length) ? len : sg->length;
1270 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Mike Christied6b10342005-11-08 04:06:41 -06001272
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001273 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001274 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 vma->vm_private_data = sfp;
1276 vma->vm_ops = &sg_mmap_vm_ops;
1277 return 0;
1278}
1279
1280/* This function is a "bottom half" handler that is called by the
1281 * mid level when a command is completed (or has failed). */
1282static void
Mike Christied6b10342005-11-08 04:06:41 -06001283sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Mike Christied6b10342005-11-08 04:06:41 -06001285 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 Sg_device *sdp = NULL;
1287 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001289 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (NULL == srp) {
1292 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return;
1294 }
1295 sfp = srp->parentfp;
1296 if (sfp)
1297 sdp = sfp->parentdp;
1298 if ((NULL == sdp) || sdp->detached) {
1299 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return;
1301 }
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001305 sdp->disk->disk_name, srp->header.pack_id, result));
1306 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001307 ms = jiffies_to_msecs(jiffies);
1308 srp->header.duration = (ms > srp->header.duration) ?
1309 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001310 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 struct scsi_sense_hdr sshdr;
1312
Mike Christied6b10342005-11-08 04:06:41 -06001313 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1314 srp->header.status = 0xff & result;
1315 srp->header.masked_status = status_byte(result);
1316 srp->header.msg_status = msg_byte(result);
1317 srp->header.host_status = host_byte(result);
1318 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if ((sdp->sgdebug > 0) &&
1320 ((CHECK_CONDITION == srp->header.masked_status) ||
1321 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001322 __scsi_print_sense("sg_cmd_done", sense,
1323 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001326 if (driver_byte(result) != 0
1327 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 && !scsi_sense_is_deferred(&sshdr)
1329 && sshdr.sense_key == UNIT_ATTENTION
1330 && sdp->device->removable) {
1331 /* Detected possible disc change. Set the bit - this */
1332 /* may be used if there are filesystems using this device */
1333 sdp->device->changed = 1;
1334 }
1335 }
1336 /* Rely on write phase to clean out srp status values, so no "else" */
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (sfp->closed) { /* whoops this fd already released, cleanup */
1339 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1340 sg_finish_rem_req(srp);
1341 srp = NULL;
1342 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001343 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1345 scsi_device_put(sdp->device);
1346 }
1347 sfp = NULL;
1348 }
1349 } else if (srp && srp->orphan) {
1350 if (sfp->keep_orphan)
1351 srp->sg_io_owned = 0;
1352 else {
1353 sg_finish_rem_req(srp);
1354 srp = NULL;
1355 }
1356 }
1357 if (sfp && srp) {
1358 /* Now wake up any sg_read() that is waiting for this packet. */
1359 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1360 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1361 srp->done = 1;
1362 wake_up_interruptible(&sfp->read_wait);
1363 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1364 }
1365}
1366
1367static struct file_operations sg_fops = {
1368 .owner = THIS_MODULE,
1369 .read = sg_read,
1370 .write = sg_write,
1371 .poll = sg_poll,
1372 .ioctl = sg_ioctl,
1373#ifdef CONFIG_COMPAT
1374 .compat_ioctl = sg_compat_ioctl,
1375#endif
1376 .open = sg_open,
1377 .mmap = sg_mmap,
1378 .release = sg_release,
1379 .fasync = sg_fasync,
1380};
1381
gregkh@suse.ded2538782005-03-23 09:55:22 -08001382static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384static int sg_sysfs_valid = 0;
1385
James Bottomley7c07d612007-08-05 13:36:11 -05001386static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Mike Christied6b10342005-11-08 04:06:41 -06001388 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 Sg_device *sdp;
1390 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001391 int error;
1392 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jes Sorensen24669f752006-01-16 10:31:18 -05001394 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (!sdp) {
1396 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001397 return ERR_PTR(-ENOMEM);
1398 }
1399 error = -ENOMEM;
1400 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1401 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1402 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
James Bottomley7c07d612007-08-05 13:36:11 -05001405 write_lock_irqsave(&sg_index_lock, iflags);
1406 error = idr_get_new(&sg_index_idr, sdp, &k);
1407 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
James Bottomley7c07d612007-08-05 13:36:11 -05001409 if (error) {
1410 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1411 error);
1412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (unlikely(k >= SG_MAX_DEVS))
1416 goto overflow;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1419 sprintf(disk->disk_name, "sg%d", k);
1420 disk->first_minor = k;
1421 sdp->disk = disk;
1422 sdp->device = scsidp;
1423 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001424 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001425 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
James Bottomley7c07d612007-08-05 13:36:11 -05001427 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001429 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001431 return ERR_PTR(error);
1432 }
1433 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001436 sdev_printk(KERN_WARNING, scsidp,
1437 "Unable to attach sg device type=%d, minor "
1438 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 error = -ENODEV;
1440 goto out;
1441}
1442
1443static int
Tony Jonesee959b02008-02-22 00:13:36 +01001444sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Tony Jonesee959b02008-02-22 00:13:36 +01001446 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 struct gendisk *disk;
1448 Sg_device *sdp = NULL;
1449 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001450 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001451 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 disk = alloc_disk(1);
1454 if (!disk) {
1455 printk(KERN_WARNING "alloc_disk failed\n");
1456 return -ENOMEM;
1457 }
1458 disk->major = SCSI_GENERIC_MAJOR;
1459
1460 error = -ENOMEM;
1461 cdev = cdev_alloc();
1462 if (!cdev) {
1463 printk(KERN_WARNING "cdev_alloc failed\n");
1464 goto out;
1465 }
1466 cdev->owner = THIS_MODULE;
1467 cdev->ops = &sg_fops;
1468
James Bottomley7c07d612007-08-05 13:36:11 -05001469 sdp = sg_alloc(disk, scsidp);
1470 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001472 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto out;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
James Bottomley7c07d612007-08-05 13:36:11 -05001476 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001477 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001478 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 sdp->cdev = cdev;
1481 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001482 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001484 sg_class_member = device_create_drvdata(sg_sysfs_class,
1485 cl_dev->parent,
1486 MKDEV(SCSI_GENERIC_MAJOR,
1487 sdp->index),
1488 sdp,
1489 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001490 if (IS_ERR(sg_class_member)) {
1491 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001492 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001493 error = PTR_ERR(sg_class_member);
1494 goto cdev_add_err;
1495 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001496 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 &sg_class_member->kobj, "generic");
1498 if (error)
1499 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001500 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001502 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
James Bottomley9ccfc752005-10-02 11:45:08 -05001504 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001505 "Attached scsi generic sg%d type %d\n", sdp->index,
1506 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Tony Jonesee959b02008-02-22 00:13:36 +01001508 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001512cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001513 write_lock_irqsave(&sg_index_lock, iflags);
1514 idr_remove(&sg_index_idr, sdp->index);
1515 write_unlock_irqrestore(&sg_index_lock, iflags);
1516 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518out:
1519 put_disk(disk);
1520 if (cdev)
1521 cdev_del(cdev);
1522 return error;
1523}
1524
1525static void
Tony Jonesee959b02008-02-22 00:13:36 +01001526sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Tony Jonesee959b02008-02-22 00:13:36 +01001528 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1529 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 unsigned long iflags;
1531 Sg_fd *sfp;
1532 Sg_fd *tsfp;
1533 Sg_request *srp;
1534 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001535 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
James Bottomley7c07d612007-08-05 13:36:11 -05001537 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
James Bottomley7c07d612007-08-05 13:36:11 -05001540 delay = 0;
1541 write_lock_irqsave(&sg_index_lock, iflags);
1542 if (sdp->headfp) {
1543 sdp->detached = 1;
1544 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1545 tsfp = sfp->nextfp;
1546 for (srp = sfp->headrp; srp; srp = tsrp) {
1547 tsrp = srp->nextrp;
1548 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1549 sg_finish_rem_req(srp);
1550 }
1551 if (sfp->closed) {
1552 scsi_device_put(sdp->device);
1553 __sg_remove_sfp(sdp, sfp);
1554 } else {
1555 delay = 1;
1556 wake_up_interruptible(&sfp->read_wait);
1557 kill_fasync(&sfp->async_qp, SIGPOLL,
1558 POLL_HUP);
1559 }
1560 }
1561 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1562 if (NULL == sdp->headfp) {
1563 idr_remove(&sg_index_idr, sdp->index);
1564 }
1565 } else { /* nothing active, simple case */
1566 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1567 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
James Bottomley7c07d612007-08-05 13:36:11 -05001569 write_unlock_irqrestore(&sg_index_lock, iflags);
1570
1571 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001572 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001573 cdev_del(sdp->cdev);
1574 sdp->cdev = NULL;
1575 put_disk(sdp->disk);
1576 sdp->disk = NULL;
1577 if (NULL == sdp->headfp)
1578 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (delay)
1581 msleep(10); /* dirty detach so delay device destruction */
1582}
1583
Douglas Gilbert6460e752006-09-20 18:20:49 -04001584module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1585module_param_named(def_reserved_size, def_reserved_size, int,
1586 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1588
1589MODULE_AUTHOR("Douglas Gilbert");
1590MODULE_DESCRIPTION("SCSI generic (sg) driver");
1591MODULE_LICENSE("GPL");
1592MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001593MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Douglas Gilbert6460e752006-09-20 18:20:49 -04001595MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1596 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1598MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1599
1600static int __init
1601init_sg(void)
1602{
1603 int rc;
1604
Douglas Gilbert6460e752006-09-20 18:20:49 -04001605 if (scatter_elem_sz < PAGE_SIZE) {
1606 scatter_elem_sz = PAGE_SIZE;
1607 scatter_elem_sz_prev = scatter_elem_sz;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (def_reserved_size >= 0)
1610 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001611 else
1612 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1615 SG_MAX_DEVS, "sg");
1616 if (rc)
1617 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001618 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if ( IS_ERR(sg_sysfs_class) ) {
1620 rc = PTR_ERR(sg_sysfs_class);
1621 goto err_out;
1622 }
1623 sg_sysfs_valid = 1;
1624 rc = scsi_register_interface(&sg_interface);
1625 if (0 == rc) {
1626#ifdef CONFIG_SCSI_PROC_FS
1627 sg_proc_init();
1628#endif /* CONFIG_SCSI_PROC_FS */
1629 return 0;
1630 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001631 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632err_out:
1633 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1634 return rc;
1635}
1636
1637static void __exit
1638exit_sg(void)
1639{
1640#ifdef CONFIG_SCSI_PROC_FS
1641 sg_proc_cleanup();
1642#endif /* CONFIG_SCSI_PROC_FS */
1643 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001644 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 sg_sysfs_valid = 0;
1646 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1647 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001648 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001651static int __sg_start_req(struct sg_request *srp, struct sg_io_hdr *hp,
1652 unsigned char *cmd)
1653{
1654 struct sg_fd *sfp = srp->parentfp;
1655 struct request_queue *q = sfp->parentdp->device->request_queue;
1656 struct request *rq;
1657 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1658
1659 rq = blk_get_request(q, rw, GFP_ATOMIC);
1660 if (!rq)
1661 return -ENOMEM;
1662
1663 memcpy(rq->cmd, cmd, hp->cmd_len);
1664
1665 rq->cmd_len = hp->cmd_len;
1666 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1667
1668 srp->rq = rq;
1669 rq->end_io_data = srp;
1670 rq->sense = srp->sense_b;
1671 rq->retries = SG_DEFAULT_RETRIES;
1672
1673 return 0;
1674}
1675
1676static int sg_start_req(Sg_request *srp, unsigned char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 int res;
1679 Sg_fd *sfp = srp->parentfp;
1680 sg_io_hdr_t *hp = &srp->header;
1681 int dxfer_len = (int) hp->dxfer_len;
1682 int dxfer_dir = hp->dxfer_direction;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001683 unsigned long uaddr = (unsigned long)hp->dxferp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 Sg_scatter_hold *req_schp = &srp->data;
1685 Sg_scatter_hold *rsv_schp = &sfp->reserve;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001686 struct request_queue *q = sfp->parentdp->device->request_queue;
1687 unsigned long alignment = queue_dma_alignment(q) | q->dma_pad_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001692 return __sg_start_req(srp, hp, cmd);
1693
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001694#ifdef SG_ALLOW_DIO_CODE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1696 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001697 (!sfp->parentdp->device->host->unchecked_isa_dma) &&
1698 !(uaddr & alignment) && !(dxfer_len & alignment)) {
1699 res = __sg_start_req(srp, hp, cmd);
1700 if (!res)
1701 res = sg_build_direct(srp, sfp, dxfer_len);
1702
1703 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1707 sg_link_reserve(sfp, srp, dxfer_len);
1708 else {
1709 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1710 if (res) {
1711 sg_remove_scat(req_schp);
1712 return res;
1713 }
1714 }
1715 return 0;
1716}
1717
1718static void
1719sg_finish_rem_req(Sg_request * srp)
1720{
1721 Sg_fd *sfp = srp->parentfp;
1722 Sg_scatter_hold *req_schp = &srp->data;
1723
1724 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1725 if (srp->res_used)
1726 sg_unlink_reserve(sfp, srp);
1727 else
1728 sg_remove_scat(req_schp);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001729
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001730 if (srp->rq) {
1731 if (srp->bio)
1732 blk_rq_unmap_user(srp->bio);
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001733 blk_put_request(srp->rq);
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001734 }
FUJITA Tomonori10865df2008-08-28 16:17:07 +09001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 sg_remove_request(sfp, srp);
1737}
1738
1739static int
1740sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1741{
Mike Christied6b10342005-11-08 04:06:41 -06001742 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001743 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Mike Christied6b10342005-11-08 04:06:41 -06001745 /*
1746 * TODO: test without low_dma, we should not need it since
1747 * the block layer will bounce the buffer for us
1748 *
1749 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1750 */
1751 if (sfp->low_dma)
1752 gfp_flags |= GFP_DMA;
1753 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (!schp->buffer)
1755 return -ENOMEM;
Anton Blanchard30fa0d02007-10-26 14:00:14 +02001756 sg_init_table(schp->buffer, tablesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001758 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1762static int
1763sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 sg_io_hdr_t *hp = &srp->header;
1766 Sg_scatter_hold *schp = &srp->data;
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001767 int res;
1768 struct request *rq = srp->rq;
1769 struct request_queue *q = sfp->parentdp->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001771 res = blk_rq_map_user(q, rq, NULL, hp->dxferp, dxfer_len, GFP_ATOMIC);
1772 if (res)
1773 return res;
1774 srp->bio = rq->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 schp->dio_in_use = 1;
1776 hp->info |= SG_INFO_DIRECT_IO;
1777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780static int
1781sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1782{
Mike Christied6b10342005-11-08 04:06:41 -06001783 struct scatterlist *sg;
1784 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1785 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001787 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Eric Sesterhennfb119932007-05-23 14:41:36 -07001789 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return -EFAULT;
1791 if (0 == blk_size)
1792 ++blk_size; /* don't know why */
1793/* round request up to next highest SG_SECTOR_SZ byte boundary */
1794 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1795 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1796 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001797
1798 /* N.B. ret_sz carried into this block ... */
1799 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1800 if (mx_sc_elems < 0)
1801 return mx_sc_elems; /* most likely -ENOMEM */
1802
Douglas Gilbert6460e752006-09-20 18:20:49 -04001803 num = scatter_elem_sz;
1804 if (unlikely(num != scatter_elem_sz_prev)) {
1805 if (num < PAGE_SIZE) {
1806 scatter_elem_sz = PAGE_SIZE;
1807 scatter_elem_sz_prev = PAGE_SIZE;
1808 } else
1809 scatter_elem_sz_prev = num;
1810 }
Mike Christied6b10342005-11-08 04:06:41 -06001811 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1812 (rem_sz > 0) && (k < mx_sc_elems);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001813 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001814
Douglas Gilbert6460e752006-09-20 18:20:49 -04001815 num = (rem_sz > scatter_elem_sz_prev) ?
1816 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001817 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (!p)
1819 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Douglas Gilbert6460e752006-09-20 18:20:49 -04001821 if (num == scatter_elem_sz_prev) {
1822 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1823 scatter_elem_sz = ret_sz;
1824 scatter_elem_sz_prev = ret_sz;
1825 }
1826 }
Jens Axboe642f1492007-10-24 11:20:47 +02001827 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001829 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1830 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001831 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Mike Christied6b10342005-11-08 04:06:41 -06001833 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001834 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1835 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001836
1837 schp->bufflen = blk_size;
1838 if (rem_sz > 0) /* must have failed */
1839 return -ENOMEM;
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return 0;
1842}
1843
1844static int
1845sg_write_xfer(Sg_request * srp)
1846{
1847 sg_io_hdr_t *hp = &srp->header;
1848 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001849 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 int num_xfer = 0;
1851 int j, k, onum, usglen, ksglen, res;
1852 int iovec_count = (int) hp->iovec_count;
1853 int dxfer_dir = hp->dxfer_direction;
1854 unsigned char *p;
1855 unsigned char __user *up;
1856 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1857
1858 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1859 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1860 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1861 if (schp->bufflen < num_xfer)
1862 num_xfer = schp->bufflen;
1863 }
1864 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1865 (new_interface
1866 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1867 return 0;
1868
1869 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1870 num_xfer, iovec_count, schp->k_use_sg));
1871 if (iovec_count) {
1872 onum = iovec_count;
1873 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1874 return -EFAULT;
1875 } else
1876 onum = 1;
1877
Mike Christied6b10342005-11-08 04:06:41 -06001878 ksglen = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001879 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06001880 for (j = 0, k = 0; j < onum; ++j) {
1881 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1882 if (res)
1883 return res;
1884
Jens Axboeb0f655d2007-05-11 15:01:01 +02001885 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02001886 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06001887 if (usglen <= 0)
1888 break;
1889 if (ksglen > usglen) {
1890 if (usglen >= num_xfer) {
1891 if (__copy_from_user(p, up, num_xfer))
1892 return -EFAULT;
1893 return 0;
1894 }
1895 if (__copy_from_user(p, up, usglen))
1896 return -EFAULT;
1897 p += usglen;
1898 ksglen -= usglen;
1899 break;
1900 } else {
1901 if (ksglen >= num_xfer) {
1902 if (__copy_from_user(p, up, num_xfer))
1903 return -EFAULT;
1904 return 0;
1905 }
1906 if (__copy_from_user(p, up, ksglen))
1907 return -EFAULT;
1908 up += ksglen;
1909 usglen -= ksglen;
1910 }
1911 ++k;
1912 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return 0;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Mike Christied6b10342005-11-08 04:06:41 -06001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return 0;
1918}
1919
1920static int
1921sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1922 int wr_xf, int *countp, unsigned char __user **up)
1923{
1924 int num_xfer = (int) hp->dxfer_len;
1925 unsigned char __user *p = hp->dxferp;
1926 int count;
1927
1928 if (0 == sg_num) {
1929 if (wr_xf && ('\0' == hp->interface_id))
1930 count = (int) hp->flags; /* holds "old" input_size */
1931 else
1932 count = num_xfer;
1933 } else {
1934 sg_iovec_t iovec;
1935 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1936 return -EFAULT;
1937 p = iovec.iov_base;
1938 count = (int) iovec.iov_len;
1939 }
1940 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1941 return -EFAULT;
1942 if (up)
1943 *up = p;
1944 if (countp)
1945 *countp = count;
1946 return 0;
1947}
1948
1949static void
1950sg_remove_scat(Sg_scatter_hold * schp)
1951{
1952 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1953 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06001954 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001956 if (!schp->dio_in_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int k;
1958
Jens Axboe45711f12007-10-22 21:19:53 +02001959 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001960 ++k, sg = sg_next(sg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 SCSI_LOG_TIMEOUT(5, printk(
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001962 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
Jens Axboe45711f12007-10-22 21:19:53 +02001963 k, sg_page(sg), sg->length));
1964 sg_page_free(sg_page(sg), sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
FUJITA Tomonori6e5a30c2008-08-28 16:17:08 +09001966
1967 kfree(schp->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Mike Christied6b10342005-11-08 04:06:41 -06001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 memset(schp, 0, sizeof (*schp));
1971}
1972
1973static int
1974sg_read_xfer(Sg_request * srp)
1975{
1976 sg_io_hdr_t *hp = &srp->header;
1977 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001978 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 int num_xfer = 0;
1980 int j, k, onum, usglen, ksglen, res;
1981 int iovec_count = (int) hp->iovec_count;
1982 int dxfer_dir = hp->dxfer_direction;
1983 unsigned char *p;
1984 unsigned char __user *up;
1985 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1986
1987 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
1988 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1989 num_xfer = hp->dxfer_len;
1990 if (schp->bufflen < num_xfer)
1991 num_xfer = schp->bufflen;
1992 }
1993 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1994 (new_interface
1995 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1996 return 0;
1997
1998 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1999 num_xfer, iovec_count, schp->k_use_sg));
2000 if (iovec_count) {
2001 onum = iovec_count;
2002 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2003 return -EFAULT;
2004 } else
2005 onum = 1;
2006
Jens Axboe45711f12007-10-22 21:19:53 +02002007 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06002008 ksglen = sg->length;
2009 for (j = 0, k = 0; j < onum; ++j) {
2010 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2011 if (res)
2012 return res;
2013
Jens Axboeb0f655d2007-05-11 15:01:01 +02002014 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02002015 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06002016 if (usglen <= 0)
2017 break;
2018 if (ksglen > usglen) {
2019 if (usglen >= num_xfer) {
2020 if (__copy_to_user(up, p, num_xfer))
2021 return -EFAULT;
2022 return 0;
2023 }
2024 if (__copy_to_user(up, p, usglen))
2025 return -EFAULT;
2026 p += usglen;
2027 ksglen -= usglen;
2028 break;
2029 } else {
2030 if (ksglen >= num_xfer) {
2031 if (__copy_to_user(up, p, num_xfer))
2032 return -EFAULT;
2033 return 0;
2034 }
2035 if (__copy_to_user(up, p, ksglen))
2036 return -EFAULT;
2037 up += ksglen;
2038 usglen -= ksglen;
2039 }
2040 ++k;
2041 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Mike Christied6b10342005-11-08 04:06:41 -06002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return 0;
2047}
2048
2049static int
2050sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2051{
2052 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002053 struct scatterlist *sg = schp->buffer;
2054 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2057 num_read_xfer));
2058 if ((!outp) || (num_read_xfer <= 0))
2059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Jens Axboe45711f12007-10-22 21:19:53 +02002061 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002062 num = sg->length;
2063 if (num > num_read_xfer) {
Jens Axboe45711f12007-10-22 21:19:53 +02002064 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002065 num_read_xfer))
2066 return -EFAULT;
2067 break;
2068 } else {
Jens Axboe45711f12007-10-22 21:19:53 +02002069 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002070 num))
2071 return -EFAULT;
2072 num_read_xfer -= num;
2073 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 break;
Mike Christied6b10342005-11-08 04:06:41 -06002075 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
Mike Christied6b10342005-11-08 04:06:41 -06002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080}
2081
2082static void
2083sg_build_reserve(Sg_fd * sfp, int req_size)
2084{
2085 Sg_scatter_hold *schp = &sfp->reserve;
2086
2087 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2088 do {
2089 if (req_size < PAGE_SIZE)
2090 req_size = PAGE_SIZE;
2091 if (0 == sg_build_indirect(schp, sfp, req_size))
2092 return;
2093 else
2094 sg_remove_scat(schp);
2095 req_size >>= 1; /* divide by 2 */
2096 } while (req_size > (PAGE_SIZE / 2));
2097}
2098
2099static void
2100sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2101{
2102 Sg_scatter_hold *req_schp = &srp->data;
2103 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002104 struct scatterlist *sg = rsv_schp->buffer;
2105 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 srp->res_used = 1;
2108 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002109 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Jens Axboeb0f655d2007-05-11 15:01:01 +02002111 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002112 num = sg->length;
2113 if (rem <= num) {
2114 sfp->save_scat_len = num;
2115 sg->length = rem;
2116 req_schp->k_use_sg = k + 1;
2117 req_schp->sglist_len = rsv_schp->sglist_len;
2118 req_schp->buffer = rsv_schp->buffer;
2119
2120 req_schp->bufflen = size;
2121 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2122 break;
2123 } else
2124 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Mike Christied6b10342005-11-08 04:06:41 -06002126
2127 if (k >= rsv_schp->k_use_sg)
2128 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
2131static void
2132sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2133{
2134 Sg_scatter_hold *req_schp = &srp->data;
2135 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2136
2137 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2138 (int) req_schp->k_use_sg));
2139 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002140 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002143 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 (unsigned) sfp->save_scat_len;
2145 else
2146 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2147 }
2148 req_schp->k_use_sg = 0;
2149 req_schp->bufflen = 0;
2150 req_schp->buffer = NULL;
2151 req_schp->sglist_len = 0;
2152 sfp->save_scat_len = 0;
2153 srp->res_used = 0;
2154}
2155
2156static Sg_request *
2157sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2158{
2159 Sg_request *resp;
2160 unsigned long iflags;
2161
2162 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2163 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2164 /* look for requests that are ready + not SG_IO owned */
2165 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2166 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2167 resp->done = 2; /* guard against other readers */
2168 break;
2169 }
2170 }
2171 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2172 return resp;
2173}
2174
2175#ifdef CONFIG_SCSI_PROC_FS
2176static Sg_request *
2177sg_get_nth_request(Sg_fd * sfp, int nth)
2178{
2179 Sg_request *resp;
2180 unsigned long iflags;
2181 int k;
2182
2183 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2184 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2185 ++k, resp = resp->nextrp) ;
2186 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2187 return resp;
2188}
2189#endif
2190
2191/* always adds to end of list */
2192static Sg_request *
2193sg_add_request(Sg_fd * sfp)
2194{
2195 int k;
2196 unsigned long iflags;
2197 Sg_request *resp;
2198 Sg_request *rp = sfp->req_arr;
2199
2200 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2201 resp = sfp->headrp;
2202 if (!resp) {
2203 memset(rp, 0, sizeof (Sg_request));
2204 rp->parentfp = sfp;
2205 resp = rp;
2206 sfp->headrp = resp;
2207 } else {
2208 if (0 == sfp->cmd_q)
2209 resp = NULL; /* command queuing disallowed */
2210 else {
2211 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2212 if (!rp->parentfp)
2213 break;
2214 }
2215 if (k < SG_MAX_QUEUE) {
2216 memset(rp, 0, sizeof (Sg_request));
2217 rp->parentfp = sfp;
2218 while (resp->nextrp)
2219 resp = resp->nextrp;
2220 resp->nextrp = rp;
2221 resp = rp;
2222 } else
2223 resp = NULL;
2224 }
2225 }
2226 if (resp) {
2227 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002228 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2231 return resp;
2232}
2233
2234/* Return of 1 for found; 0 for not found */
2235static int
2236sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2237{
2238 Sg_request *prev_rp;
2239 Sg_request *rp;
2240 unsigned long iflags;
2241 int res = 0;
2242
2243 if ((!sfp) || (!srp) || (!sfp->headrp))
2244 return res;
2245 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2246 prev_rp = sfp->headrp;
2247 if (srp == prev_rp) {
2248 sfp->headrp = prev_rp->nextrp;
2249 prev_rp->parentfp = NULL;
2250 res = 1;
2251 } else {
2252 while ((rp = prev_rp->nextrp)) {
2253 if (srp == rp) {
2254 prev_rp->nextrp = rp->nextrp;
2255 rp->parentfp = NULL;
2256 res = 1;
2257 break;
2258 }
2259 prev_rp = rp;
2260 }
2261 }
2262 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2263 return res;
2264}
2265
2266#ifdef CONFIG_SCSI_PROC_FS
2267static Sg_fd *
2268sg_get_nth_sfp(Sg_device * sdp, int nth)
2269{
2270 Sg_fd *resp;
2271 unsigned long iflags;
2272 int k;
2273
James Bottomley7c07d612007-08-05 13:36:11 -05002274 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2276 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002277 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return resp;
2279}
2280#endif
2281
2282static Sg_fd *
2283sg_add_sfp(Sg_device * sdp, int dev)
2284{
2285 Sg_fd *sfp;
2286 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002287 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Mike Christied6b10342005-11-08 04:06:41 -06002289 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (!sfp)
2291 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 init_waitqueue_head(&sfp->read_wait);
2294 rwlock_init(&sfp->rq_list_lock);
2295
2296 sfp->timeout = SG_DEFAULT_TIMEOUT;
2297 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2298 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2299 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2300 sdp->device->host->unchecked_isa_dma : 1;
2301 sfp->cmd_q = SG_DEF_COMMAND_Q;
2302 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2303 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002304 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (!sdp->headfp)
2306 sdp->headfp = sfp;
2307 else { /* add to tail of existing list */
2308 Sg_fd *pfp = sdp->headfp;
2309 while (pfp->nextfp)
2310 pfp = pfp->nextfp;
2311 pfp->nextfp = sfp;
2312 }
James Bottomley7c07d612007-08-05 13:36:11 -05002313 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002315 if (unlikely(sg_big_buff != def_reserved_size))
2316 sg_big_buff = def_reserved_size;
2317
Alan Stern44ec9542007-02-20 11:01:57 -05002318 bufflen = min_t(int, sg_big_buff,
2319 sdp->device->request_queue->max_sectors * 512);
2320 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2322 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2323 return sfp;
2324}
2325
2326static void
2327__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2328{
2329 Sg_fd *fp;
2330 Sg_fd *prev_fp;
2331
2332 prev_fp = sdp->headfp;
2333 if (sfp == prev_fp)
2334 sdp->headfp = prev_fp->nextfp;
2335 else {
2336 while ((fp = prev_fp->nextfp)) {
2337 if (sfp == fp) {
2338 prev_fp->nextfp = fp->nextfp;
2339 break;
2340 }
2341 prev_fp = fp;
2342 }
2343 }
2344 if (sfp->reserve.bufflen > 0) {
2345 SCSI_LOG_TIMEOUT(6,
2346 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2347 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 sg_remove_scat(&sfp->reserve);
2349 }
2350 sfp->parentdp = NULL;
2351 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002352 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
2355/* Returns 0 in normal case, 1 when detached and sdp object removed */
2356static int
2357sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2358{
2359 Sg_request *srp;
2360 Sg_request *tsrp;
2361 int dirty = 0;
2362 int res = 0;
2363
2364 for (srp = sfp->headrp; srp; srp = tsrp) {
2365 tsrp = srp->nextrp;
2366 if (sg_srp_done(srp, sfp))
2367 sg_finish_rem_req(srp);
2368 else
2369 ++dirty;
2370 }
2371 if (0 == dirty) {
2372 unsigned long iflags;
2373
James Bottomley7c07d612007-08-05 13:36:11 -05002374 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 __sg_remove_sfp(sdp, sfp);
2376 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002377 idr_remove(&sg_index_idr, sdp->index);
2378 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 res = 1;
2380 }
James Bottomley7c07d612007-08-05 13:36:11 -05002381 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 } else {
2383 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2384 /* only bump the access_count if we actually succeeded in
2385 * throwing another counter on the host module */
2386 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2387 sfp->closed = 1; /* flag dirty state on this fd */
2388 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2389 dirty));
2390 }
2391 return res;
2392}
2393
2394static int
2395sg_res_in_use(Sg_fd * sfp)
2396{
2397 const Sg_request *srp;
2398 unsigned long iflags;
2399
2400 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2401 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2402 if (srp->res_used)
2403 break;
2404 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2405 return srp ? 1 : 0;
2406}
2407
Douglas Gilbert6460e752006-09-20 18:20:49 -04002408/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002409static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2411{
Mike Christied6b10342005-11-08 04:06:41 -06002412 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002413 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002415 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Douglas Gilbert6460e752006-09-20 18:20:49 -04002417 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return resp;
2419
2420 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002421 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002423 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2426 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002427 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002428 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002429 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 --order;
2431 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002432 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 resSz = a_size;
2434 }
2435 if (resp) {
2436 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002437 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002438 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 return resp;
2441}
2442
2443static void
Mike Christied6b10342005-11-08 04:06:41 -06002444sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 int order, a_size;
2447
Mike Christied6b10342005-11-08 04:06:41 -06002448 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return;
2450 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2451 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002452 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455#ifdef CONFIG_SCSI_PROC_FS
2456static int
James Bottomley7c07d612007-08-05 13:36:11 -05002457sg_idr_max_id(int id, void *p, void *data)
2458{
2459 int *k = data;
2460
2461 if (*k < id)
2462 *k = id;
2463
2464 return 0;
2465}
2466
2467static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468sg_last_dev(void)
2469{
Tony Battersby53474c02008-01-22 15:25:49 -05002470 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 unsigned long iflags;
2472
James Bottomley7c07d612007-08-05 13:36:11 -05002473 read_lock_irqsave(&sg_index_lock, iflags);
2474 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2475 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return k + 1; /* origin 1 */
2477}
2478#endif
2479
2480static Sg_device *
2481sg_get_dev(int dev)
2482{
James Bottomley7c07d612007-08-05 13:36:11 -05002483 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 unsigned long iflags;
2485
James Bottomley7c07d612007-08-05 13:36:11 -05002486 read_lock_irqsave(&sg_index_lock, iflags);
2487 sdp = idr_find(&sg_index_idr, dev);
2488 read_unlock_irqrestore(&sg_index_lock, iflags);
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return sdp;
2491}
2492
2493#ifdef CONFIG_SCSI_PROC_FS
2494
2495static struct proc_dir_entry *sg_proc_sgp = NULL;
2496
2497static char sg_proc_sg_dirname[] = "scsi/sg";
2498
2499static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2500
2501static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2502static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2503 size_t count, loff_t *off);
2504static struct file_operations adio_fops = {
2505 /* .owner, .read and .llseek added in sg_proc_init() */
2506 .open = sg_proc_single_open_adio,
2507 .write = sg_proc_write_adio,
2508 .release = single_release,
2509};
2510
2511static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2512static ssize_t sg_proc_write_dressz(struct file *filp,
2513 const char __user *buffer, size_t count, loff_t *off);
2514static struct file_operations dressz_fops = {
2515 .open = sg_proc_single_open_dressz,
2516 .write = sg_proc_write_dressz,
2517 .release = single_release,
2518};
2519
2520static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2521static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2522static struct file_operations version_fops = {
2523 .open = sg_proc_single_open_version,
2524 .release = single_release,
2525};
2526
2527static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2528static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2529static struct file_operations devhdr_fops = {
2530 .open = sg_proc_single_open_devhdr,
2531 .release = single_release,
2532};
2533
2534static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2535static int sg_proc_open_dev(struct inode *inode, struct file *file);
2536static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2537static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2538static void dev_seq_stop(struct seq_file *s, void *v);
2539static struct file_operations dev_fops = {
2540 .open = sg_proc_open_dev,
2541 .release = seq_release,
2542};
2543static struct seq_operations dev_seq_ops = {
2544 .start = dev_seq_start,
2545 .next = dev_seq_next,
2546 .stop = dev_seq_stop,
2547 .show = sg_proc_seq_show_dev,
2548};
2549
2550static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2551static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2552static struct file_operations devstrs_fops = {
2553 .open = sg_proc_open_devstrs,
2554 .release = seq_release,
2555};
2556static struct seq_operations devstrs_seq_ops = {
2557 .start = dev_seq_start,
2558 .next = dev_seq_next,
2559 .stop = dev_seq_stop,
2560 .show = sg_proc_seq_show_devstrs,
2561};
2562
2563static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2564static int sg_proc_open_debug(struct inode *inode, struct file *file);
2565static struct file_operations debug_fops = {
2566 .open = sg_proc_open_debug,
2567 .release = seq_release,
2568};
2569static struct seq_operations debug_seq_ops = {
2570 .start = dev_seq_start,
2571 .next = dev_seq_next,
2572 .stop = dev_seq_stop,
2573 .show = sg_proc_seq_show_debug,
2574};
2575
2576
2577struct sg_proc_leaf {
2578 const char * name;
2579 struct file_operations * fops;
2580};
2581
2582static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2583 {"allow_dio", &adio_fops},
2584 {"debug", &debug_fops},
2585 {"def_reserved_size", &dressz_fops},
2586 {"device_hdr", &devhdr_fops},
2587 {"devices", &dev_fops},
2588 {"device_strs", &devstrs_fops},
2589 {"version", &version_fops}
2590};
2591
2592static int
2593sg_proc_init(void)
2594{
2595 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002596 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct sg_proc_leaf * leaf;
2598
Al Viro66600222005-09-28 22:32:57 +01002599 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (!sg_proc_sgp)
2601 return 1;
2602 for (k = 0; k < num_leaves; ++k) {
2603 leaf = &sg_proc_leaf_arr[k];
2604 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002605 leaf->fops->owner = THIS_MODULE;
2606 leaf->fops->read = seq_read;
2607 leaf->fops->llseek = seq_lseek;
2608 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610 return 0;
2611}
2612
2613static void
2614sg_proc_cleanup(void)
2615{
2616 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002617 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 if (!sg_proc_sgp)
2620 return;
2621 for (k = 0; k < num_leaves; ++k)
2622 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2623 remove_proc_entry(sg_proc_sg_dirname, NULL);
2624}
2625
2626
2627static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2628{
2629 seq_printf(s, "%d\n", *((int *)s->private));
2630 return 0;
2631}
2632
2633static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2634{
2635 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2636}
2637
2638static ssize_t
2639sg_proc_write_adio(struct file *filp, const char __user *buffer,
2640 size_t count, loff_t *off)
2641{
2642 int num;
2643 char buff[11];
2644
2645 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2646 return -EACCES;
2647 num = (count < 10) ? count : 10;
2648 if (copy_from_user(buff, buffer, num))
2649 return -EFAULT;
2650 buff[num] = '\0';
2651 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2652 return count;
2653}
2654
2655static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2656{
2657 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2658}
2659
2660static ssize_t
2661sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2662 size_t count, loff_t *off)
2663{
2664 int num;
2665 unsigned long k = ULONG_MAX;
2666 char buff[11];
2667
2668 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2669 return -EACCES;
2670 num = (count < 10) ? count : 10;
2671 if (copy_from_user(buff, buffer, num))
2672 return -EFAULT;
2673 buff[num] = '\0';
2674 k = simple_strtoul(buff, NULL, 10);
2675 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2676 sg_big_buff = k;
2677 return count;
2678 }
2679 return -ERANGE;
2680}
2681
2682static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2683{
2684 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2685 sg_version_date);
2686 return 0;
2687}
2688
2689static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2690{
2691 return single_open(file, sg_proc_seq_show_version, NULL);
2692}
2693
2694static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2695{
2696 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2697 "online\n");
2698 return 0;
2699}
2700
2701static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2702{
2703 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2704}
2705
2706struct sg_proc_deviter {
2707 loff_t index;
2708 size_t max;
2709};
2710
2711static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2712{
2713 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2714
Jan Blunck729d70f2005-08-27 11:07:52 -07002715 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (! it)
2717 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 it->index = *pos;
2720 it->max = sg_last_dev();
2721 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002722 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
2726static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2727{
Jan Blunck729d70f2005-08-27 11:07:52 -07002728 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 *pos = ++it->index;
2731 return (it->index < it->max) ? it : NULL;
2732}
2733
2734static void dev_seq_stop(struct seq_file *s, void *v)
2735{
Jan Blunck729d70f2005-08-27 11:07:52 -07002736 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
2739static int sg_proc_open_dev(struct inode *inode, struct file *file)
2740{
2741 return seq_open(file, &dev_seq_ops);
2742}
2743
2744static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2745{
2746 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2747 Sg_device *sdp;
2748 struct scsi_device *scsidp;
2749
2750 sdp = it ? sg_get_dev(it->index) : NULL;
2751 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2752 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2753 scsidp->host->host_no, scsidp->channel,
2754 scsidp->id, scsidp->lun, (int) scsidp->type,
2755 1,
2756 (int) scsidp->queue_depth,
2757 (int) scsidp->device_busy,
2758 (int) scsi_device_online(scsidp));
2759 else
2760 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2761 return 0;
2762}
2763
2764static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2765{
2766 return seq_open(file, &devstrs_seq_ops);
2767}
2768
2769static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2770{
2771 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2772 Sg_device *sdp;
2773 struct scsi_device *scsidp;
2774
2775 sdp = it ? sg_get_dev(it->index) : NULL;
2776 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2777 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2778 scsidp->vendor, scsidp->model, scsidp->rev);
2779 else
2780 seq_printf(s, "<no active device>\n");
2781 return 0;
2782}
2783
2784static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2785{
2786 int k, m, new_interface, blen, usg;
2787 Sg_request *srp;
2788 Sg_fd *fp;
2789 const sg_io_hdr_t *hp;
2790 const char * cp;
cb59e842005-04-02 13:51:23 -06002791 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2794 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2795 "(res)sgat=%d low_dma=%d\n", k + 1,
2796 jiffies_to_msecs(fp->timeout),
2797 fp->reserve.bufflen,
2798 (int) fp->reserve.k_use_sg,
2799 (int) fp->low_dma);
2800 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2801 (int) fp->cmd_q, (int) fp->force_packid,
2802 (int) fp->keep_orphan, (int) fp->closed);
2803 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2804 hp = &srp->header;
2805 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2806 if (srp->res_used) {
2807 if (new_interface &&
2808 (SG_FLAG_MMAP_IO & hp->flags))
2809 cp = " mmap>> ";
2810 else
2811 cp = " rb>> ";
2812 } else {
2813 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2814 cp = " dio>> ";
2815 else
2816 cp = " ";
2817 }
2818 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002819 blen = srp->data.bufflen;
2820 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 seq_printf(s, srp->done ?
2822 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002823 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 seq_printf(s, " id=%d blen=%d",
2825 srp->header.pack_id, blen);
2826 if (srp->done)
2827 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002828 else {
2829 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002831 (new_interface ? hp->timeout :
2832 jiffies_to_msecs(fp->timeout)),
2833 (ms > hp->duration ? ms - hp->duration : 0));
2834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2836 (int) srp->data.cmd_opcode);
2837 }
2838 if (0 == m)
2839 seq_printf(s, " No requests active\n");
2840 }
2841}
2842
2843static int sg_proc_open_debug(struct inode *inode, struct file *file)
2844{
2845 return seq_open(file, &debug_seq_ops);
2846}
2847
2848static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2849{
2850 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2851 Sg_device *sdp;
2852
2853 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002854 seq_printf(s, "max_active_device=%d(origin 1)\n",
2855 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2857 }
2858 sdp = it ? sg_get_dev(it->index) : NULL;
2859 if (sdp) {
2860 struct scsi_device *scsidp = sdp->device;
2861
2862 if (NULL == scsidp) {
2863 seq_printf(s, "device %d detached ??\n",
2864 (int)it->index);
2865 return 0;
2866 }
2867
2868 if (sg_get_nth_sfp(sdp, 0)) {
2869 seq_printf(s, " >>> device=%s ",
2870 sdp->disk->disk_name);
2871 if (sdp->detached)
2872 seq_printf(s, "detached pending close ");
2873 else
2874 seq_printf
2875 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2876 scsidp->host->host_no,
2877 scsidp->channel, scsidp->id,
2878 scsidp->lun,
2879 scsidp->host->hostt->emulated);
2880 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2881 sdp->sg_tablesize, sdp->exclude);
2882 }
2883 sg_proc_debug_helper(s, sdp);
2884 }
2885 return 0;
2886}
2887
2888#endif /* CONFIG_SCSI_PROC_FS */
2889
2890module_init(init_sg);
2891module_exit(exit_sg);