blob: d3b8ebb8377639c92e8e44503aa828554734c055 [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 */
140} Sg_request;
141
142typedef struct sg_fd { /* holds the state of a file descriptor */
143 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
144 struct sg_device *parentdp; /* owning device */
145 wait_queue_head_t read_wait; /* queue read until command done */
146 rwlock_t rq_list_lock; /* protect access to list in req_arr */
147 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
148 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
149 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
150 unsigned save_scat_len; /* original length of trunc. scat. element */
151 Sg_request *headrp; /* head of request slist, NULL->empty */
152 struct fasync_struct *async_qp; /* used by asynchronous notification */
153 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
154 char low_dma; /* as in parent but possibly overridden to 1 */
155 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
156 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
157 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
160 char mmap_called; /* 0 -> mmap() never called on this fd */
161} Sg_fd;
162
163typedef struct sg_device { /* holds the state of each scsi generic device */
164 struct scsi_device *device;
165 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
166 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500167 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 Sg_fd *headfp; /* first open fd belonging to this device */
169 volatile char detached; /* 0->attached, 1->detached pending removal */
170 volatile char exclude; /* opened for exclusive access */
171 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
172 struct gendisk *disk;
173 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
174} Sg_device;
175
176static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600177/* tasklet or soft irq callback */
178static void sg_cmd_done(void *data, char *sense, int result, int resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int sg_start_req(Sg_request * srp);
180static void sg_finish_rem_req(Sg_request * srp);
181static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
183 int tablesize);
184static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
185 Sg_request * srp);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200186static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
187 const char __user *buf, size_t count, int blocking,
188 int read_only, Sg_request **o_srp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
191static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
192 int wr_xf, int *countp, unsigned char __user **up);
193static int sg_write_xfer(Sg_request * srp);
194static int sg_read_xfer(Sg_request * srp);
195static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196static void sg_remove_scat(Sg_scatter_hold * schp);
197static void sg_build_reserve(Sg_fd * sfp, int req_size);
198static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600200static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
201static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
203static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
205static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
206static Sg_request *sg_add_request(Sg_fd * sfp);
207static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
208static int sg_res_in_use(Sg_fd * sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
210static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef CONFIG_SCSI_PROC_FS
212static int sg_last_dev(void);
213#endif
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#define SZ_SG_HEADER sizeof(struct sg_header)
216#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
217#define SZ_SG_IOVEC sizeof(sg_iovec_t)
218#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
219
220static int
221sg_open(struct inode *inode, struct file *filp)
222{
223 int dev = iminor(inode);
224 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600225 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 Sg_device *sdp;
227 Sg_fd *sfp;
228 int res;
229 int retval;
230
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600231 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 nonseekable_open(inode, filp);
233 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
234 sdp = sg_get_dev(dev);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600235 if ((!sdp) || (!sdp->device)) {
236 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -ENXIO;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600238 }
239 if (sdp->detached) {
240 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -ENODEV;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* This driver's module count bumped by fops_get in <linux/fs.h> */
245 /* Prevent the device driver from vanishing while we sleep */
246 retval = scsi_device_get(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600247 if (retval) {
248 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return retval;
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (!((flags & O_NONBLOCK) ||
253 scsi_block_when_processing_errors(sdp->device))) {
254 retval = -ENXIO;
255 /* we are in error recovery for this device */
256 goto error_out;
257 }
258
259 if (flags & O_EXCL) {
260 if (O_RDONLY == (flags & O_ACCMODE)) {
261 retval = -EPERM; /* Can't lock it with read only access */
262 goto error_out;
263 }
264 if (sdp->headfp && (flags & O_NONBLOCK)) {
265 retval = -EBUSY;
266 goto error_out;
267 }
268 res = 0;
269 __wait_event_interruptible(sdp->o_excl_wait,
270 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
271 if (res) {
272 retval = res; /* -ERESTARTSYS because signal hit process */
273 goto error_out;
274 }
275 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
276 if (flags & O_NONBLOCK) {
277 retval = -EBUSY;
278 goto error_out;
279 }
280 res = 0;
281 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
282 res);
283 if (res) {
284 retval = res; /* -ERESTARTSYS because signal hit process */
285 goto error_out;
286 }
287 }
288 if (sdp->detached) {
289 retval = -ENODEV;
290 goto error_out;
291 }
292 if (!sdp->headfp) { /* no existing opens on this device */
293 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600294 q = sdp->device->request_queue;
295 sdp->sg_tablesize = min(q->max_hw_segments,
296 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 if ((sfp = sg_add_sfp(sdp, dev)))
299 filp->private_data = sfp;
300 else {
301 if (flags & O_EXCL)
302 sdp->exclude = 0; /* undo if error */
303 retval = -ENOMEM;
304 goto error_out;
305 }
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600306 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308
309 error_out:
310 scsi_device_put(sdp->device);
Jonathan Corbeteb09d3d2008-05-15 12:22:06 -0600311 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return retval;
313}
314
315/* Following function was formerly called 'sg_close' */
316static int
317sg_release(struct inode *inode, struct file *filp)
318{
319 Sg_device *sdp;
320 Sg_fd *sfp;
321
322 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
323 return -ENXIO;
324 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
325 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
326 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
327 if (!sdp->detached) {
328 scsi_device_put(sdp->device);
329 }
330 sdp->exclude = 0;
331 wake_up_interruptible(&sdp->o_excl_wait);
332 }
333 return 0;
334}
335
336static ssize_t
337sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 Sg_device *sdp;
340 Sg_fd *sfp;
341 Sg_request *srp;
342 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600344 struct sg_header *old_hdr = NULL;
345 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
348 return -ENXIO;
349 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
350 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!access_ok(VERIFY_WRITE, buf, count))
353 return -EFAULT;
354 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600355 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
356 if (!old_hdr)
357 return -ENOMEM;
358 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
359 retval = -EFAULT;
360 goto free_old_hdr;
361 }
362 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600364 sg_io_hdr_t *new_hdr;
365 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
366 if (!new_hdr) {
367 retval = -ENOMEM;
368 goto free_old_hdr;
369 }
370 retval =__copy_from_user
371 (new_hdr, buf, SZ_SG_IO_HDR);
372 req_pack_id = new_hdr->pack_id;
373 kfree(new_hdr);
374 if (retval) {
375 retval = -EFAULT;
376 goto free_old_hdr;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 } else
cb59e842005-04-02 13:51:23 -0600380 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 srp = sg_get_rq_mark(sfp, req_pack_id);
383 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600384 if (sdp->detached) {
385 retval = -ENODEV;
386 goto free_old_hdr;
387 }
388 if (filp->f_flags & O_NONBLOCK) {
389 retval = -EAGAIN;
390 goto free_old_hdr;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 while (1) {
cb59e842005-04-02 13:51:23 -0600393 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600395 (sdp->detached ||
396 (srp = sg_get_rq_mark(sfp, req_pack_id))),
397 retval);
398 if (sdp->detached) {
399 retval = -ENODEV;
400 goto free_old_hdr;
401 }
402 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
cb59e842005-04-02 13:51:23 -0600404
405 /* -ERESTARTSYS as signal hit process */
406 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 }
cb59e842005-04-02 13:51:23 -0600409 if (srp->header.interface_id != '\0') {
410 retval = sg_new_read(sfp, buf, count, srp);
411 goto free_old_hdr;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600415 if (old_hdr == NULL) {
416 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
417 if (! old_hdr) {
418 retval = -ENOMEM;
419 goto free_old_hdr;
420 }
421 }
422 memset(old_hdr, 0, SZ_SG_HEADER);
423 old_hdr->reply_len = (int) hp->timeout;
424 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
425 old_hdr->pack_id = hp->pack_id;
426 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600428 old_hdr->target_status = hp->masked_status;
429 old_hdr->host_status = hp->host_status;
430 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ((CHECK_CONDITION & hp->masked_status) ||
432 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600433 memcpy(old_hdr->sense_buffer, srp->sense_b,
434 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 switch (hp->host_status) {
436 /* This setup of 'result' is for backward compatibility and is best
437 ignored by the user who should use target, host + driver status */
438 case DID_OK:
439 case DID_PASSTHROUGH:
440 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600441 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443 case DID_NO_CONNECT:
444 case DID_BUS_BUSY:
445 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600446 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case DID_BAD_TARGET:
449 case DID_ABORT:
450 case DID_PARITY:
451 case DID_RESET:
452 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600453 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600456 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 hp->masked_status == GOOD) ? 0 : EIO;
458 break;
459 default:
cb59e842005-04-02 13:51:23 -0600460 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 }
463
464 /* Now copy the result back to the user buffer. */
465 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600466 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
467 retval = -EFAULT;
468 goto free_old_hdr;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600471 if (count > old_hdr->reply_len)
472 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600474 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
475 retval = -EFAULT;
476 goto free_old_hdr;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 } else
cb59e842005-04-02 13:51:23 -0600480 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600482 retval = count;
483free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800484 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600485 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static ssize_t
489sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
490{
491 sg_io_hdr_t *hp = &srp->header;
492 int err = 0;
493 int len;
494
495 if (count < SZ_SG_IO_HDR) {
496 err = -EINVAL;
497 goto err_out;
498 }
499 hp->sb_len_wr = 0;
500 if ((hp->mx_sb_len > 0) && hp->sbp) {
501 if ((CHECK_CONDITION & hp->masked_status) ||
502 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600503 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
505 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
506 len = (len > sb_len) ? sb_len : len;
507 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
508 err = -EFAULT;
509 goto err_out;
510 }
511 hp->sb_len_wr = len;
512 }
513 }
514 if (hp->masked_status || hp->host_status || hp->driver_status)
515 hp->info |= SG_INFO_CHECK;
516 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
517 err = -EFAULT;
518 goto err_out;
519 }
520 err = sg_read_xfer(srp);
521 err_out:
522 sg_finish_rem_req(srp);
523 return (0 == err) ? count : err;
524}
525
526static ssize_t
527sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
528{
529 int mxsize, cmd_size, k;
530 int input_size, blocking;
531 unsigned char opcode;
532 Sg_device *sdp;
533 Sg_fd *sfp;
534 Sg_request *srp;
535 struct sg_header old_hdr;
536 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600537 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
540 return -ENXIO;
541 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
542 sdp->disk->disk_name, (int) count));
543 if (sdp->detached)
544 return -ENODEV;
545 if (!((filp->f_flags & O_NONBLOCK) ||
546 scsi_block_when_processing_errors(sdp->device)))
547 return -ENXIO;
548
549 if (!access_ok(VERIFY_READ, buf, count))
550 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
551 if (count < SZ_SG_HEADER)
552 return -EIO;
553 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
554 return -EFAULT;
555 blocking = !(filp->f_flags & O_NONBLOCK);
556 if (old_hdr.reply_len < 0)
Adel Gadllah0b07de82008-06-26 13:48:27 +0200557 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (count < (SZ_SG_HEADER + 6))
559 return -EIO; /* The minimum scsi command length is 6 bytes. */
560
561 if (!(srp = sg_add_request(sfp))) {
562 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
563 return -EDOM;
564 }
565 buf += SZ_SG_HEADER;
566 __get_user(opcode, buf);
567 if (sfp->next_cmd_len > 0) {
568 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
569 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
570 sfp->next_cmd_len = 0;
571 sg_remove_request(sfp, srp);
572 return -EIO;
573 }
574 cmd_size = sfp->next_cmd_len;
575 sfp->next_cmd_len = 0; /* reset so only this write() effected */
576 } else {
577 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
578 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
579 cmd_size = 12;
580 }
581 SCSI_LOG_TIMEOUT(4, printk(
582 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
583/* Determine buffer size. */
584 input_size = count - cmd_size;
585 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
586 mxsize -= SZ_SG_HEADER;
587 input_size -= SZ_SG_HEADER;
588 if (input_size < 0) {
589 sg_remove_request(sfp, srp);
590 return -EIO; /* User did not pass enough bytes for this command. */
591 }
592 hp = &srp->header;
593 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
594 hp->cmd_len = (unsigned char) cmd_size;
595 hp->iovec_count = 0;
596 hp->mx_sb_len = 0;
597 if (input_size > 0)
598 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
599 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
600 else
601 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
602 hp->dxfer_len = mxsize;
603 hp->dxferp = (char __user *)buf + cmd_size;
604 hp->sbp = NULL;
605 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
606 hp->flags = input_size; /* structure abuse ... */
607 hp->pack_id = old_hdr.pack_id;
608 hp->usr_ptr = NULL;
609 if (__copy_from_user(cmnd, buf, cmd_size))
610 return -EFAULT;
611 /*
612 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
613 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
614 * is a non-zero input_size, so emit a warning.
615 */
Andi Kleeneaa3e222008-01-13 17:41:43 +0100616 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
617 static char cmd[TASK_COMM_LEN];
618 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 printk(KERN_WARNING
620 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
621 "guessing data in;\n" KERN_WARNING " "
622 "program %s not setting count and/or reply_len properly\n",
623 old_hdr.reply_len - (int)SZ_SG_HEADER,
624 input_size, (unsigned int) cmnd[0],
625 current->comm);
Andi Kleeneaa3e222008-01-13 17:41:43 +0100626 strcpy(cmd, current->comm);
627 }
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
630 return (k < 0) ? k : count;
631}
632
633static ssize_t
Adel Gadllah0b07de82008-06-26 13:48:27 +0200634sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
635 size_t count, int blocking, int read_only,
636 Sg_request **o_srp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int k;
639 Sg_request *srp;
640 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600641 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int timeout;
643 unsigned long ul_timeout;
644
645 if (count < SZ_SG_IO_HDR)
646 return -EINVAL;
647 if (!access_ok(VERIFY_READ, buf, count))
648 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
649
650 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
651 if (!(srp = sg_add_request(sfp))) {
652 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
653 return -EDOM;
654 }
655 hp = &srp->header;
656 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
657 sg_remove_request(sfp, srp);
658 return -EFAULT;
659 }
660 if (hp->interface_id != 'S') {
661 sg_remove_request(sfp, srp);
662 return -ENOSYS;
663 }
664 if (hp->flags & SG_FLAG_MMAP_IO) {
665 if (hp->dxfer_len > sfp->reserve.bufflen) {
666 sg_remove_request(sfp, srp);
667 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
668 }
669 if (hp->flags & SG_FLAG_DIRECT_IO) {
670 sg_remove_request(sfp, srp);
671 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
672 }
673 if (sg_res_in_use(sfp)) {
674 sg_remove_request(sfp, srp);
675 return -EBUSY; /* reserve buffer already being used */
676 }
677 }
678 ul_timeout = msecs_to_jiffies(srp->header.timeout);
679 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
680 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
681 sg_remove_request(sfp, srp);
682 return -EMSGSIZE;
683 }
684 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
685 sg_remove_request(sfp, srp);
686 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
687 }
688 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
689 sg_remove_request(sfp, srp);
690 return -EFAULT;
691 }
Jens Axboe2b272d42008-06-26 19:45:54 +0200692 if (read_only && !blk_verify_command(file, cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 sg_remove_request(sfp, srp);
694 return -EPERM;
695 }
696 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
697 if (k < 0)
698 return k;
699 if (o_srp)
700 *o_srp = srp;
701 return count;
702}
703
704static int
705sg_common_write(Sg_fd * sfp, Sg_request * srp,
706 unsigned char *cmnd, int timeout, int blocking)
707{
Mike Christied6b10342005-11-08 04:06:41 -0600708 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 Sg_device *sdp = sfp->parentdp;
710 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
713 hp->status = 0;
714 hp->masked_status = 0;
715 hp->msg_status = 0;
716 hp->info = 0;
717 hp->host_status = 0;
718 hp->driver_status = 0;
719 hp->resid = 0;
720 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
721 (int) cmnd[0], (int) hp->cmd_len));
722
723 if ((k = sg_start_req(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400724 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 sg_finish_rem_req(srp);
726 return k; /* probably out of space --> ENOMEM */
727 }
728 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400729 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sg_finish_rem_req(srp);
731 return k;
732 }
733 if (sdp->detached) {
734 sg_finish_rem_req(srp);
735 return -ENODEV;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 switch (hp->dxfer_direction) {
739 case SG_DXFER_TO_FROM_DEV:
740 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600741 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600744 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600747 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 default:
Mike Christied6b10342005-11-08 04:06:41 -0600750 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752 }
cb59e842005-04-02 13:51:23 -0600753 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/* Now send everything of to mid-level. The next time we hear about this
755 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600756 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600757 hp->dxfer_len, srp->data.k_use_sg, timeout,
758 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
759 GFP_ATOMIC)) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400760 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
Mike Christied6b10342005-11-08 04:06:41 -0600761 /*
762 * most likely out of mem, but could also be a bad map
763 */
Mike Christie18c49b82006-03-22 16:04:38 -0600764 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600765 return -ENOMEM;
766 } else
767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static int
771sg_srp_done(Sg_request *srp, Sg_fd *sfp)
772{
773 unsigned long iflags;
774 int done;
775
776 read_lock_irqsave(&sfp->rq_list_lock, iflags);
777 done = srp->done;
778 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
779 return done;
780}
781
782static int
783sg_ioctl(struct inode *inode, struct file *filp,
784 unsigned int cmd_in, unsigned long arg)
785{
786 void __user *p = (void __user *)arg;
787 int __user *ip = p;
788 int result, val, read_only;
789 Sg_device *sdp;
790 Sg_fd *sfp;
791 Sg_request *srp;
792 unsigned long iflags;
793
794 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
795 return -ENXIO;
796 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
797 sdp->disk->disk_name, (int) cmd_in));
798 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
799
800 switch (cmd_in) {
801 case SG_IO:
802 {
803 int blocking = 1; /* ignore O_NONBLOCK flag */
804
805 if (sdp->detached)
806 return -ENODEV;
807 if (!scsi_block_when_processing_errors(sdp->device))
808 return -ENXIO;
809 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
810 return -EFAULT;
811 result =
Adel Gadllah0b07de82008-06-26 13:48:27 +0200812 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 blocking, read_only, &srp);
814 if (result < 0)
815 return result;
816 srp->sg_io_owned = 1;
817 while (1) {
818 result = 0; /* following macro to beat race condition */
819 __wait_event_interruptible(sfp->read_wait,
820 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
821 result);
822 if (sdp->detached)
823 return -ENODEV;
824 if (sfp->closed)
825 return 0; /* request packet dropped already */
826 if (0 == result)
827 break;
828 srp->orphan = 1;
829 return result; /* -ERESTARTSYS because signal hit process */
830 }
831 write_lock_irqsave(&sfp->rq_list_lock, iflags);
832 srp->done = 2;
833 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
834 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
835 return (result < 0) ? result : 0;
836 }
837 case SG_SET_TIMEOUT:
838 result = get_user(val, ip);
839 if (result)
840 return result;
841 if (val < 0)
842 return -EIO;
843 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
844 val = MULDIV (INT_MAX, USER_HZ, HZ);
845 sfp->timeout_user = val;
846 sfp->timeout = MULDIV (val, HZ, USER_HZ);
847
848 return 0;
849 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
850 /* strange ..., for backward compatibility */
851 return sfp->timeout_user;
852 case SG_SET_FORCE_LOW_DMA:
853 result = get_user(val, ip);
854 if (result)
855 return result;
856 if (val) {
857 sfp->low_dma = 1;
858 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
859 val = (int) sfp->reserve.bufflen;
860 sg_remove_scat(&sfp->reserve);
861 sg_build_reserve(sfp, val);
862 }
863 } else {
864 if (sdp->detached)
865 return -ENODEV;
866 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
867 }
868 return 0;
869 case SG_GET_LOW_DMA:
870 return put_user((int) sfp->low_dma, ip);
871 case SG_GET_SCSI_ID:
872 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
873 return -EFAULT;
874 else {
875 sg_scsi_id_t __user *sg_idp = p;
876
877 if (sdp->detached)
878 return -ENODEV;
879 __put_user((int) sdp->device->host->host_no,
880 &sg_idp->host_no);
881 __put_user((int) sdp->device->channel,
882 &sg_idp->channel);
883 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
884 __put_user((int) sdp->device->lun, &sg_idp->lun);
885 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
886 __put_user((short) sdp->device->host->cmd_per_lun,
887 &sg_idp->h_cmd_per_lun);
888 __put_user((short) sdp->device->queue_depth,
889 &sg_idp->d_queue_depth);
890 __put_user(0, &sg_idp->unused[0]);
891 __put_user(0, &sg_idp->unused[1]);
892 return 0;
893 }
894 case SG_SET_FORCE_PACK_ID:
895 result = get_user(val, ip);
896 if (result)
897 return result;
898 sfp->force_packid = val ? 1 : 0;
899 return 0;
900 case SG_GET_PACK_ID:
901 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
902 return -EFAULT;
903 read_lock_irqsave(&sfp->rq_list_lock, iflags);
904 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
905 if ((1 == srp->done) && (!srp->sg_io_owned)) {
906 read_unlock_irqrestore(&sfp->rq_list_lock,
907 iflags);
908 __put_user(srp->header.pack_id, ip);
909 return 0;
910 }
911 }
912 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
913 __put_user(-1, ip);
914 return 0;
915 case SG_GET_NUM_WAITING:
916 read_lock_irqsave(&sfp->rq_list_lock, iflags);
917 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
918 if ((1 == srp->done) && (!srp->sg_io_owned))
919 ++val;
920 }
921 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
922 return put_user(val, ip);
923 case SG_GET_SG_TABLESIZE:
924 return put_user(sdp->sg_tablesize, ip);
925 case SG_SET_RESERVED_SIZE:
926 result = get_user(val, ip);
927 if (result)
928 return result;
929 if (val < 0)
930 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500931 val = min_t(int, val,
932 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (val != sfp->reserve.bufflen) {
934 if (sg_res_in_use(sfp) || sfp->mmap_called)
935 return -EBUSY;
936 sg_remove_scat(&sfp->reserve);
937 sg_build_reserve(sfp, val);
938 }
939 return 0;
940 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500941 val = min_t(int, sfp->reserve.bufflen,
942 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return put_user(val, ip);
944 case SG_SET_COMMAND_Q:
945 result = get_user(val, ip);
946 if (result)
947 return result;
948 sfp->cmd_q = val ? 1 : 0;
949 return 0;
950 case SG_GET_COMMAND_Q:
951 return put_user((int) sfp->cmd_q, ip);
952 case SG_SET_KEEP_ORPHAN:
953 result = get_user(val, ip);
954 if (result)
955 return result;
956 sfp->keep_orphan = val;
957 return 0;
958 case SG_GET_KEEP_ORPHAN:
959 return put_user((int) sfp->keep_orphan, ip);
960 case SG_NEXT_CMD_LEN:
961 result = get_user(val, ip);
962 if (result)
963 return result;
964 sfp->next_cmd_len = (val > 0) ? val : 0;
965 return 0;
966 case SG_GET_VERSION_NUM:
967 return put_user(sg_version_num, ip);
968 case SG_GET_ACCESS_COUNT:
969 /* faked - we don't have a real access count anymore */
970 val = (sdp->device ? 1 : 0);
971 return put_user(val, ip);
972 case SG_GET_REQUEST_TABLE:
973 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
974 return -EFAULT;
975 else {
cb59e842005-04-02 13:51:23 -0600976 sg_req_info_t *rinfo;
977 unsigned int ms;
978
979 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
980 GFP_KERNEL);
981 if (!rinfo)
982 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 read_lock_irqsave(&sfp->rq_list_lock, iflags);
984 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
985 ++val, srp = srp ? srp->nextrp : srp) {
986 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
987 if (srp) {
988 rinfo[val].req_state = srp->done + 1;
989 rinfo[val].problem =
990 srp->header.masked_status &
991 srp->header.host_status &
992 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600993 if (srp->done)
994 rinfo[val].duration =
995 srp->header.duration;
996 else {
997 ms = jiffies_to_msecs(jiffies);
998 rinfo[val].duration =
999 (ms > srp->header.duration) ?
1000 (ms - srp->header.duration) : 0;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -06001003 rinfo[val].sg_io_owned =
1004 srp->sg_io_owned;
1005 rinfo[val].pack_id =
1006 srp->header.pack_id;
1007 rinfo[val].usr_ptr =
1008 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 }
1011 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -06001012 result = __copy_to_user(p, rinfo,
1013 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1014 result = result ? -EFAULT : 0;
1015 kfree(rinfo);
1016 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 case SG_EMULATED_HOST:
1019 if (sdp->detached)
1020 return -ENODEV;
1021 return put_user(sdp->device->host->hostt->emulated, ip);
1022 case SG_SCSI_RESET:
1023 if (sdp->detached)
1024 return -ENODEV;
1025 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001026 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EBUSY;
1028 } else if (!scsi_block_when_processing_errors(sdp->device))
1029 return -EBUSY;
1030 result = get_user(val, ip);
1031 if (result)
1032 return result;
1033 if (SG_SCSI_RESET_NOTHING == val)
1034 return 0;
1035 switch (val) {
1036 case SG_SCSI_RESET_DEVICE:
1037 val = SCSI_TRY_RESET_DEVICE;
1038 break;
Brian King39120e12008-07-01 13:03:19 -05001039 case SG_SCSI_RESET_TARGET:
1040 val = SCSI_TRY_RESET_TARGET;
1041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 case SG_SCSI_RESET_BUS:
1043 val = SCSI_TRY_RESET_BUS;
1044 break;
1045 case SG_SCSI_RESET_HOST:
1046 val = SCSI_TRY_RESET_HOST;
1047 break;
1048 default:
1049 return -EINVAL;
1050 }
1051 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1052 return -EACCES;
1053 return (scsi_reset_provider(sdp->device, val) ==
1054 SUCCESS) ? 0 : -EIO;
1055 case SCSI_IOCTL_SEND_COMMAND:
1056 if (sdp->detached)
1057 return -ENODEV;
1058 if (read_only) {
1059 unsigned char opcode = WRITE_6;
1060 Scsi_Ioctl_Command __user *siocp = p;
1061
1062 if (copy_from_user(&opcode, siocp->data, 1))
1063 return -EFAULT;
Adel Gadllah0b07de82008-06-26 13:48:27 +02001064 if (!blk_verify_command(filp, &opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EPERM;
1066 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001067 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 case SG_SET_DEBUG:
1069 result = get_user(val, ip);
1070 if (result)
1071 return result;
1072 sdp->sgdebug = (char) val;
1073 return 0;
1074 case SCSI_IOCTL_GET_IDLUN:
1075 case SCSI_IOCTL_GET_BUS_NUMBER:
1076 case SCSI_IOCTL_PROBE_HOST:
1077 case SG_GET_TRANSFORM:
1078 if (sdp->detached)
1079 return -ENODEV;
1080 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001081 case BLKSECTGET:
1082 return put_user(sdp->device->request_queue->max_sectors * 512,
1083 ip);
Christof Schmitt6da127a2008-01-11 10:09:43 +01001084 case BLKTRACESETUP:
1085 return blk_trace_setup(sdp->device->request_queue,
1086 sdp->disk->disk_name,
1087 sdp->device->sdev_gendev.devt,
1088 (char *)arg);
1089 case BLKTRACESTART:
1090 return blk_trace_startstop(sdp->device->request_queue, 1);
1091 case BLKTRACESTOP:
1092 return blk_trace_startstop(sdp->device->request_queue, 0);
1093 case BLKTRACETEARDOWN:
1094 return blk_trace_remove(sdp->device->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 default:
1096 if (read_only)
1097 return -EPERM; /* don't know so take safe approach */
1098 return scsi_ioctl(sdp->device, cmd_in, p);
1099 }
1100}
1101
1102#ifdef CONFIG_COMPAT
1103static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1104{
1105 Sg_device *sdp;
1106 Sg_fd *sfp;
1107 struct scsi_device *sdev;
1108
1109 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1110 return -ENXIO;
1111
1112 sdev = sdp->device;
1113 if (sdev->host->hostt->compat_ioctl) {
1114 int ret;
1115
1116 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1117
1118 return ret;
1119 }
1120
1121 return -ENOIOCTLCMD;
1122}
1123#endif
1124
1125static unsigned int
1126sg_poll(struct file *filp, poll_table * wait)
1127{
1128 unsigned int res = 0;
1129 Sg_device *sdp;
1130 Sg_fd *sfp;
1131 Sg_request *srp;
1132 int count = 0;
1133 unsigned long iflags;
1134
1135 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1136 || sfp->closed)
1137 return POLLERR;
1138 poll_wait(filp, &sfp->read_wait, wait);
1139 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1140 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1141 /* if any read waiting, flag it */
1142 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1143 res = POLLIN | POLLRDNORM;
1144 ++count;
1145 }
1146 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1147
1148 if (sdp->detached)
1149 res |= POLLHUP;
1150 else if (!sfp->cmd_q) {
1151 if (0 == count)
1152 res |= POLLOUT | POLLWRNORM;
1153 } else if (count < SG_MAX_QUEUE)
1154 res |= POLLOUT | POLLWRNORM;
1155 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1156 sdp->disk->disk_name, (int) res));
1157 return res;
1158}
1159
1160static int
1161sg_fasync(int fd, struct file *filp, int mode)
1162{
1163 int retval;
1164 Sg_device *sdp;
1165 Sg_fd *sfp;
1166
1167 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1168 return -ENXIO;
1169 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1170 sdp->disk->disk_name, mode));
1171
1172 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1173 return (retval < 0) ? retval : 0;
1174}
1175
Nick Piggina13ff0b2008-02-07 18:46:06 -08001176static int
1177sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001180 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001182 struct scatterlist *sg;
1183 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
Nick Piggina13ff0b2008-02-07 18:46:06 -08001186 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 rsv_schp = &sfp->reserve;
Nick Piggina13ff0b2008-02-07 18:46:06 -08001188 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (offset >= rsv_schp->bufflen)
Nick Piggina13ff0b2008-02-07 18:46:06 -08001190 return VM_FAULT_SIGBUS;
1191 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001193 sg = rsv_schp->buffer;
1194 sa = vma->vm_start;
1195 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001196 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001197 len = vma->vm_end - sa;
1198 len = (len < sg->length) ? len : sg->length;
1199 if (offset < len) {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001200 struct page *page;
Jens Axboe45711f12007-10-22 21:19:53 +02001201 page = virt_to_page(page_address(sg_page(sg)) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001202 get_page(page); /* increment page count */
Nick Piggina13ff0b2008-02-07 18:46:06 -08001203 vmf->page = page;
1204 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Mike Christied6b10342005-11-08 04:06:41 -06001206 sa += len;
1207 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Mike Christied6b10342005-11-08 04:06:41 -06001209
Nick Piggina13ff0b2008-02-07 18:46:06 -08001210 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213static struct vm_operations_struct sg_mmap_vm_ops = {
Nick Piggina13ff0b2008-02-07 18:46:06 -08001214 .fault = sg_vma_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215};
1216
1217static int
1218sg_mmap(struct file *filp, struct vm_area_struct *vma)
1219{
1220 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001221 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001223 int k;
1224 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1227 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001228 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1230 (void *) vma->vm_start, (int) req_sz));
1231 if (vma->vm_pgoff)
1232 return -EINVAL; /* want no offset */
1233 rsv_schp = &sfp->reserve;
1234 if (req_sz > rsv_schp->bufflen)
1235 return -ENOMEM; /* cannot map more than reserved buffer */
1236
Mike Christied6b10342005-11-08 04:06:41 -06001237 sa = vma->vm_start;
1238 sg = rsv_schp->buffer;
1239 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001240 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001241 len = vma->vm_end - sa;
1242 len = (len < sg->length) ? len : sg->length;
1243 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Mike Christied6b10342005-11-08 04:06:41 -06001245
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001246 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001247 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 vma->vm_private_data = sfp;
1249 vma->vm_ops = &sg_mmap_vm_ops;
1250 return 0;
1251}
1252
1253/* This function is a "bottom half" handler that is called by the
1254 * mid level when a command is completed (or has failed). */
1255static void
Mike Christied6b10342005-11-08 04:06:41 -06001256sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Mike Christied6b10342005-11-08 04:06:41 -06001258 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 Sg_device *sdp = NULL;
1260 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001262 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (NULL == srp) {
1265 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return;
1267 }
1268 sfp = srp->parentfp;
1269 if (sfp)
1270 sdp = sfp->parentdp;
1271 if ((NULL == sdp) || sdp->detached) {
1272 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return;
1274 }
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001278 sdp->disk->disk_name, srp->header.pack_id, result));
1279 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001280 ms = jiffies_to_msecs(jiffies);
1281 srp->header.duration = (ms > srp->header.duration) ?
1282 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001283 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct scsi_sense_hdr sshdr;
1285
Mike Christied6b10342005-11-08 04:06:41 -06001286 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1287 srp->header.status = 0xff & result;
1288 srp->header.masked_status = status_byte(result);
1289 srp->header.msg_status = msg_byte(result);
1290 srp->header.host_status = host_byte(result);
1291 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if ((sdp->sgdebug > 0) &&
1293 ((CHECK_CONDITION == srp->header.masked_status) ||
1294 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001295 __scsi_print_sense("sg_cmd_done", sense,
1296 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001299 if (driver_byte(result) != 0
1300 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 && !scsi_sense_is_deferred(&sshdr)
1302 && sshdr.sense_key == UNIT_ATTENTION
1303 && sdp->device->removable) {
1304 /* Detected possible disc change. Set the bit - this */
1305 /* may be used if there are filesystems using this device */
1306 sdp->device->changed = 1;
1307 }
1308 }
1309 /* Rely on write phase to clean out srp status values, so no "else" */
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (sfp->closed) { /* whoops this fd already released, cleanup */
1312 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1313 sg_finish_rem_req(srp);
1314 srp = NULL;
1315 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001316 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1318 scsi_device_put(sdp->device);
1319 }
1320 sfp = NULL;
1321 }
1322 } else if (srp && srp->orphan) {
1323 if (sfp->keep_orphan)
1324 srp->sg_io_owned = 0;
1325 else {
1326 sg_finish_rem_req(srp);
1327 srp = NULL;
1328 }
1329 }
1330 if (sfp && srp) {
1331 /* Now wake up any sg_read() that is waiting for this packet. */
1332 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1333 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1334 srp->done = 1;
1335 wake_up_interruptible(&sfp->read_wait);
1336 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1337 }
1338}
1339
1340static struct file_operations sg_fops = {
1341 .owner = THIS_MODULE,
1342 .read = sg_read,
1343 .write = sg_write,
1344 .poll = sg_poll,
1345 .ioctl = sg_ioctl,
1346#ifdef CONFIG_COMPAT
1347 .compat_ioctl = sg_compat_ioctl,
1348#endif
1349 .open = sg_open,
1350 .mmap = sg_mmap,
1351 .release = sg_release,
1352 .fasync = sg_fasync,
1353};
1354
gregkh@suse.ded2538782005-03-23 09:55:22 -08001355static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357static int sg_sysfs_valid = 0;
1358
James Bottomley7c07d612007-08-05 13:36:11 -05001359static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Mike Christied6b10342005-11-08 04:06:41 -06001361 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 Sg_device *sdp;
1363 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001364 int error;
1365 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jes Sorensen24669f752006-01-16 10:31:18 -05001367 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (!sdp) {
1369 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001370 return ERR_PTR(-ENOMEM);
1371 }
1372 error = -ENOMEM;
1373 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1374 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
James Bottomley7c07d612007-08-05 13:36:11 -05001378 write_lock_irqsave(&sg_index_lock, iflags);
1379 error = idr_get_new(&sg_index_idr, sdp, &k);
1380 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
James Bottomley7c07d612007-08-05 13:36:11 -05001382 if (error) {
1383 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1384 error);
1385 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (unlikely(k >= SG_MAX_DEVS))
1389 goto overflow;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1392 sprintf(disk->disk_name, "sg%d", k);
1393 disk->first_minor = k;
1394 sdp->disk = disk;
1395 sdp->device = scsidp;
1396 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001397 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001398 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
James Bottomley7c07d612007-08-05 13:36:11 -05001400 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001402 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001404 return ERR_PTR(error);
1405 }
1406 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001409 sdev_printk(KERN_WARNING, scsidp,
1410 "Unable to attach sg device type=%d, minor "
1411 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 error = -ENODEV;
1413 goto out;
1414}
1415
1416static int
Tony Jonesee959b02008-02-22 00:13:36 +01001417sg_add(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Tony Jonesee959b02008-02-22 00:13:36 +01001419 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct gendisk *disk;
1421 Sg_device *sdp = NULL;
1422 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001423 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001424 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 disk = alloc_disk(1);
1427 if (!disk) {
1428 printk(KERN_WARNING "alloc_disk failed\n");
1429 return -ENOMEM;
1430 }
1431 disk->major = SCSI_GENERIC_MAJOR;
1432
1433 error = -ENOMEM;
1434 cdev = cdev_alloc();
1435 if (!cdev) {
1436 printk(KERN_WARNING "cdev_alloc failed\n");
1437 goto out;
1438 }
1439 cdev->owner = THIS_MODULE;
1440 cdev->ops = &sg_fops;
1441
James Bottomley7c07d612007-08-05 13:36:11 -05001442 sdp = sg_alloc(disk, scsidp);
1443 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001445 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
James Bottomley7c07d612007-08-05 13:36:11 -05001449 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001450 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001451 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 sdp->cdev = cdev;
1454 if (sg_sysfs_valid) {
Tony Jonesee959b02008-02-22 00:13:36 +01001455 struct device *sg_class_member;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -07001457 sg_class_member = device_create_drvdata(sg_sysfs_class,
1458 cl_dev->parent,
1459 MKDEV(SCSI_GENERIC_MAJOR,
1460 sdp->index),
1461 sdp,
1462 "%s", disk->disk_name);
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001463 if (IS_ERR(sg_class_member)) {
1464 printk(KERN_ERR "sg_add: "
Tony Jonesee959b02008-02-22 00:13:36 +01001465 "device_create failed\n");
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001466 error = PTR_ERR(sg_class_member);
1467 goto cdev_add_err;
1468 }
FUJITA Tomonorid07e0362008-01-15 13:18:00 +09001469 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 &sg_class_member->kobj, "generic");
1471 if (error)
1472 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001473 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001475 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
James Bottomley9ccfc752005-10-02 11:45:08 -05001477 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001478 "Attached scsi generic sg%d type %d\n", sdp->index,
1479 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Tony Jonesee959b02008-02-22 00:13:36 +01001481 dev_set_drvdata(cl_dev, sdp);
FUJITA Tomonoria24484f2008-01-15 13:17:47 +09001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001485cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001486 write_lock_irqsave(&sg_index_lock, iflags);
1487 idr_remove(&sg_index_idr, sdp->index);
1488 write_unlock_irqrestore(&sg_index_lock, iflags);
1489 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out:
1492 put_disk(disk);
1493 if (cdev)
1494 cdev_del(cdev);
1495 return error;
1496}
1497
1498static void
Tony Jonesee959b02008-02-22 00:13:36 +01001499sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Tony Jonesee959b02008-02-22 00:13:36 +01001501 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1502 Sg_device *sdp = dev_get_drvdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned long iflags;
1504 Sg_fd *sfp;
1505 Sg_fd *tsfp;
1506 Sg_request *srp;
1507 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001508 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
James Bottomley7c07d612007-08-05 13:36:11 -05001510 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
James Bottomley7c07d612007-08-05 13:36:11 -05001513 delay = 0;
1514 write_lock_irqsave(&sg_index_lock, iflags);
1515 if (sdp->headfp) {
1516 sdp->detached = 1;
1517 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1518 tsfp = sfp->nextfp;
1519 for (srp = sfp->headrp; srp; srp = tsrp) {
1520 tsrp = srp->nextrp;
1521 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1522 sg_finish_rem_req(srp);
1523 }
1524 if (sfp->closed) {
1525 scsi_device_put(sdp->device);
1526 __sg_remove_sfp(sdp, sfp);
1527 } else {
1528 delay = 1;
1529 wake_up_interruptible(&sfp->read_wait);
1530 kill_fasync(&sfp->async_qp, SIGPOLL,
1531 POLL_HUP);
1532 }
1533 }
1534 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1535 if (NULL == sdp->headfp) {
1536 idr_remove(&sg_index_idr, sdp->index);
1537 }
1538 } else { /* nothing active, simple case */
1539 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1540 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
James Bottomley7c07d612007-08-05 13:36:11 -05001542 write_unlock_irqrestore(&sg_index_lock, iflags);
1543
1544 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
Tony Jonesee959b02008-02-22 00:13:36 +01001545 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
James Bottomley7c07d612007-08-05 13:36:11 -05001546 cdev_del(sdp->cdev);
1547 sdp->cdev = NULL;
1548 put_disk(sdp->disk);
1549 sdp->disk = NULL;
1550 if (NULL == sdp->headfp)
1551 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 if (delay)
1554 msleep(10); /* dirty detach so delay device destruction */
1555}
1556
Douglas Gilbert6460e752006-09-20 18:20:49 -04001557module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1558module_param_named(def_reserved_size, def_reserved_size, int,
1559 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1561
1562MODULE_AUTHOR("Douglas Gilbert");
1563MODULE_DESCRIPTION("SCSI generic (sg) driver");
1564MODULE_LICENSE("GPL");
1565MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001566MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Douglas Gilbert6460e752006-09-20 18:20:49 -04001568MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1569 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1571MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1572
1573static int __init
1574init_sg(void)
1575{
1576 int rc;
1577
Douglas Gilbert6460e752006-09-20 18:20:49 -04001578 if (scatter_elem_sz < PAGE_SIZE) {
1579 scatter_elem_sz = PAGE_SIZE;
1580 scatter_elem_sz_prev = scatter_elem_sz;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (def_reserved_size >= 0)
1583 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001584 else
1585 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1588 SG_MAX_DEVS, "sg");
1589 if (rc)
1590 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001591 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if ( IS_ERR(sg_sysfs_class) ) {
1593 rc = PTR_ERR(sg_sysfs_class);
1594 goto err_out;
1595 }
1596 sg_sysfs_valid = 1;
1597 rc = scsi_register_interface(&sg_interface);
1598 if (0 == rc) {
1599#ifdef CONFIG_SCSI_PROC_FS
1600 sg_proc_init();
1601#endif /* CONFIG_SCSI_PROC_FS */
1602 return 0;
1603 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001604 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605err_out:
1606 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1607 return rc;
1608}
1609
1610static void __exit
1611exit_sg(void)
1612{
1613#ifdef CONFIG_SCSI_PROC_FS
1614 sg_proc_cleanup();
1615#endif /* CONFIG_SCSI_PROC_FS */
1616 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001617 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 sg_sysfs_valid = 0;
1619 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1620 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001621 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
1624static int
1625sg_start_req(Sg_request * srp)
1626{
1627 int res;
1628 Sg_fd *sfp = srp->parentfp;
1629 sg_io_hdr_t *hp = &srp->header;
1630 int dxfer_len = (int) hp->dxfer_len;
1631 int dxfer_dir = hp->dxfer_direction;
1632 Sg_scatter_hold *req_schp = &srp->data;
1633 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1634
1635 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1636 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1637 return 0;
1638 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1639 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1640 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1641 res = sg_build_direct(srp, sfp, dxfer_len);
1642 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1643 return res;
1644 }
1645 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1646 sg_link_reserve(sfp, srp, dxfer_len);
1647 else {
1648 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1649 if (res) {
1650 sg_remove_scat(req_schp);
1651 return res;
1652 }
1653 }
1654 return 0;
1655}
1656
1657static void
1658sg_finish_rem_req(Sg_request * srp)
1659{
1660 Sg_fd *sfp = srp->parentfp;
1661 Sg_scatter_hold *req_schp = &srp->data;
1662
1663 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1664 if (srp->res_used)
1665 sg_unlink_reserve(sfp, srp);
1666 else
1667 sg_remove_scat(req_schp);
1668 sg_remove_request(sfp, srp);
1669}
1670
1671static int
1672sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1673{
Mike Christied6b10342005-11-08 04:06:41 -06001674 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001675 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Mike Christied6b10342005-11-08 04:06:41 -06001677 /*
1678 * TODO: test without low_dma, we should not need it since
1679 * the block layer will bounce the buffer for us
1680 *
1681 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1682 */
1683 if (sfp->low_dma)
1684 gfp_flags |= GFP_DMA;
1685 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!schp->buffer)
1687 return -ENOMEM;
Anton Blanchard30fa0d02007-10-26 14:00:14 +02001688 sg_init_table(schp->buffer, tablesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001690 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693#ifdef SG_ALLOW_DIO_CODE
1694/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001695 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1698 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 (i.e., either completely successful or fails)
1700*/
1701static int
1702st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001703 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001705 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1706 unsigned long start = uaddr >> PAGE_SHIFT;
1707 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct page **pages;
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* User attempted Overflow! */
1712 if ((uaddr + count) < uaddr)
1713 return -EINVAL;
1714
1715 /* Too big */
1716 if (nr_pages > max_pages)
1717 return -ENOMEM;
1718
1719 /* Hmm? */
1720 if (count == 0)
1721 return 0;
1722
1723 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1724 return -ENOMEM;
1725
1726 /* Try to fault in all of the necessary pages */
1727 down_read(&current->mm->mmap_sem);
1728 /* rw==READ means read from drive, write into memory area */
1729 res = get_user_pages(
1730 current,
1731 current->mm,
1732 uaddr,
1733 nr_pages,
1734 rw == READ,
1735 0, /* don't force */
1736 pages,
1737 NULL);
1738 up_read(&current->mm->mmap_sem);
1739
1740 /* Errors and no page mapped should return here */
1741 if (res < nr_pages)
1742 goto out_unmap;
1743
1744 for (i=0; i < nr_pages; i++) {
1745 /* FIXME: flush superflous for rw==READ,
1746 * probably wrong function for rw==WRITE
1747 */
1748 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /* ?? Is locking needed? I don't think so */
1750 /* if (TestSetPageLocked(pages[i]))
1751 goto out_unlock; */
1752 }
1753
Jens Axboe642f1492007-10-24 11:20:47 +02001754 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (nr_pages > 1) {
1756 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1757 count -= sgl[0].length;
Jens Axboe642f1492007-10-24 11:20:47 +02001758 for (i=1; i < nr_pages ; i++)
1759 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 else {
1762 sgl[0].length = count;
1763 }
1764
1765 kfree(pages);
1766 return nr_pages;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001769 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 for (j=0; j < res; j++)
1771 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001772 res = 0;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 kfree(pages);
1775 return res;
1776}
1777
1778
1779/* And unmap them... */
1780static int
1781st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1782 int dirtied)
1783{
1784 int i;
1785
1786 for (i=0; i < nr_pages; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +02001787 struct page *page = sg_page(&sgl[i]);
Nick Pigginb5810032005-10-29 18:16:12 -07001788
Nick Pigginb5810032005-10-29 18:16:12 -07001789 if (dirtied)
1790 SetPageDirty(page);
1791 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* FIXME: cache flush missing for rw==READ
1793 * FIXME: call the correct reference counting function
1794 */
Nick Pigginb5810032005-10-29 18:16:12 -07001795 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 return 0;
1799}
1800
1801/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1802#endif
1803
1804
1805/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1806static int
1807sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1808{
1809#ifdef SG_ALLOW_DIO_CODE
1810 sg_io_hdr_t *hp = &srp->header;
1811 Sg_scatter_hold *schp = &srp->data;
1812 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int mx_sc_elems, res;
1814 struct scsi_device *sdev = sfp->parentdp->device;
1815
1816 if (((unsigned long)hp->dxferp &
1817 queue_dma_alignment(sdev->request_queue)) != 0)
1818 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1821 if (mx_sc_elems <= 0) {
1822 return 1;
1823 }
Mike Christied6b10342005-11-08 04:06:41 -06001824 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1825 (unsigned long)hp->dxferp, dxfer_len,
1826 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001827 if (res <= 0) {
1828 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 schp->k_use_sg = res;
1832 schp->dio_in_use = 1;
1833 hp->info |= SG_INFO_DIRECT_IO;
1834 return 0;
1835#else
1836 return 1;
1837#endif
1838}
1839
1840static int
1841sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1842{
Mike Christied6b10342005-11-08 04:06:41 -06001843 struct scatterlist *sg;
1844 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1845 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001847 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Eric Sesterhennfb119932007-05-23 14:41:36 -07001849 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return -EFAULT;
1851 if (0 == blk_size)
1852 ++blk_size; /* don't know why */
1853/* round request up to next highest SG_SECTOR_SZ byte boundary */
1854 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1855 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1856 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001857
1858 /* N.B. ret_sz carried into this block ... */
1859 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1860 if (mx_sc_elems < 0)
1861 return mx_sc_elems; /* most likely -ENOMEM */
1862
Douglas Gilbert6460e752006-09-20 18:20:49 -04001863 num = scatter_elem_sz;
1864 if (unlikely(num != scatter_elem_sz_prev)) {
1865 if (num < PAGE_SIZE) {
1866 scatter_elem_sz = PAGE_SIZE;
1867 scatter_elem_sz_prev = PAGE_SIZE;
1868 } else
1869 scatter_elem_sz_prev = num;
1870 }
Mike Christied6b10342005-11-08 04:06:41 -06001871 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1872 (rem_sz > 0) && (k < mx_sc_elems);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001873 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001874
Douglas Gilbert6460e752006-09-20 18:20:49 -04001875 num = (rem_sz > scatter_elem_sz_prev) ?
1876 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001877 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!p)
1879 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Douglas Gilbert6460e752006-09-20 18:20:49 -04001881 if (num == scatter_elem_sz_prev) {
1882 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1883 scatter_elem_sz = ret_sz;
1884 scatter_elem_sz_prev = ret_sz;
1885 }
1886 }
Jens Axboe642f1492007-10-24 11:20:47 +02001887 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001889 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1890 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001891 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Mike Christied6b10342005-11-08 04:06:41 -06001893 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001894 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1895 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001896
1897 schp->bufflen = blk_size;
1898 if (rem_sz > 0) /* must have failed */
1899 return -ENOMEM;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
1904static int
1905sg_write_xfer(Sg_request * srp)
1906{
1907 sg_io_hdr_t *hp = &srp->header;
1908 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001909 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 int num_xfer = 0;
1911 int j, k, onum, usglen, ksglen, res;
1912 int iovec_count = (int) hp->iovec_count;
1913 int dxfer_dir = hp->dxfer_direction;
1914 unsigned char *p;
1915 unsigned char __user *up;
1916 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1917
1918 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1919 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1920 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1921 if (schp->bufflen < num_xfer)
1922 num_xfer = schp->bufflen;
1923 }
1924 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1925 (new_interface
1926 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1927 return 0;
1928
1929 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1930 num_xfer, iovec_count, schp->k_use_sg));
1931 if (iovec_count) {
1932 onum = iovec_count;
1933 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1934 return -EFAULT;
1935 } else
1936 onum = 1;
1937
Mike Christied6b10342005-11-08 04:06:41 -06001938 ksglen = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001939 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06001940 for (j = 0, k = 0; j < onum; ++j) {
1941 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1942 if (res)
1943 return res;
1944
Jens Axboeb0f655d2007-05-11 15:01:01 +02001945 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02001946 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06001947 if (usglen <= 0)
1948 break;
1949 if (ksglen > usglen) {
1950 if (usglen >= num_xfer) {
1951 if (__copy_from_user(p, up, num_xfer))
1952 return -EFAULT;
1953 return 0;
1954 }
1955 if (__copy_from_user(p, up, usglen))
1956 return -EFAULT;
1957 p += usglen;
1958 ksglen -= usglen;
1959 break;
1960 } else {
1961 if (ksglen >= num_xfer) {
1962 if (__copy_from_user(p, up, num_xfer))
1963 return -EFAULT;
1964 return 0;
1965 }
1966 if (__copy_from_user(p, up, ksglen))
1967 return -EFAULT;
1968 up += ksglen;
1969 usglen -= ksglen;
1970 }
1971 ++k;
1972 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Mike Christied6b10342005-11-08 04:06:41 -06001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
1978}
1979
1980static int
1981sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1982 int wr_xf, int *countp, unsigned char __user **up)
1983{
1984 int num_xfer = (int) hp->dxfer_len;
1985 unsigned char __user *p = hp->dxferp;
1986 int count;
1987
1988 if (0 == sg_num) {
1989 if (wr_xf && ('\0' == hp->interface_id))
1990 count = (int) hp->flags; /* holds "old" input_size */
1991 else
1992 count = num_xfer;
1993 } else {
1994 sg_iovec_t iovec;
1995 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1996 return -EFAULT;
1997 p = iovec.iov_base;
1998 count = (int) iovec.iov_len;
1999 }
2000 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2001 return -EFAULT;
2002 if (up)
2003 *up = p;
2004 if (countp)
2005 *countp = count;
2006 return 0;
2007}
2008
2009static void
2010sg_remove_scat(Sg_scatter_hold * schp)
2011{
2012 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2013 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002014 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (schp->dio_in_use) {
2017#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06002018 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019#endif
2020 } else {
2021 int k;
2022
Jens Axboe45711f12007-10-22 21:19:53 +02002023 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
Jens Axboeb0f655d2007-05-11 15:01:01 +02002024 ++k, sg = sg_next(sg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 SCSI_LOG_TIMEOUT(5, printk(
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04002026 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
Jens Axboe45711f12007-10-22 21:19:53 +02002027 k, sg_page(sg), sg->length));
2028 sg_page_free(sg_page(sg), sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030 }
Mike Christied6b10342005-11-08 04:06:41 -06002031 kfree(schp->buffer);
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 memset(schp, 0, sizeof (*schp));
2034}
2035
2036static int
2037sg_read_xfer(Sg_request * srp)
2038{
2039 sg_io_hdr_t *hp = &srp->header;
2040 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002041 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 int num_xfer = 0;
2043 int j, k, onum, usglen, ksglen, res;
2044 int iovec_count = (int) hp->iovec_count;
2045 int dxfer_dir = hp->dxfer_direction;
2046 unsigned char *p;
2047 unsigned char __user *up;
2048 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2049
2050 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2051 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2052 num_xfer = hp->dxfer_len;
2053 if (schp->bufflen < num_xfer)
2054 num_xfer = schp->bufflen;
2055 }
2056 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2057 (new_interface
2058 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2059 return 0;
2060
2061 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2062 num_xfer, iovec_count, schp->k_use_sg));
2063 if (iovec_count) {
2064 onum = iovec_count;
2065 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2066 return -EFAULT;
2067 } else
2068 onum = 1;
2069
Jens Axboe45711f12007-10-22 21:19:53 +02002070 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06002071 ksglen = sg->length;
2072 for (j = 0, k = 0; j < onum; ++j) {
2073 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2074 if (res)
2075 return res;
2076
Jens Axboeb0f655d2007-05-11 15:01:01 +02002077 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02002078 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06002079 if (usglen <= 0)
2080 break;
2081 if (ksglen > usglen) {
2082 if (usglen >= num_xfer) {
2083 if (__copy_to_user(up, p, num_xfer))
2084 return -EFAULT;
2085 return 0;
2086 }
2087 if (__copy_to_user(up, p, usglen))
2088 return -EFAULT;
2089 p += usglen;
2090 ksglen -= usglen;
2091 break;
2092 } else {
2093 if (ksglen >= num_xfer) {
2094 if (__copy_to_user(up, p, num_xfer))
2095 return -EFAULT;
2096 return 0;
2097 }
2098 if (__copy_to_user(up, p, ksglen))
2099 return -EFAULT;
2100 up += ksglen;
2101 usglen -= ksglen;
2102 }
2103 ++k;
2104 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Mike Christied6b10342005-11-08 04:06:41 -06002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return 0;
2110}
2111
2112static int
2113sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2114{
2115 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002116 struct scatterlist *sg = schp->buffer;
2117 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2120 num_read_xfer));
2121 if ((!outp) || (num_read_xfer <= 0))
2122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Jens Axboe45711f12007-10-22 21:19:53 +02002124 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002125 num = sg->length;
2126 if (num > num_read_xfer) {
Jens Axboe45711f12007-10-22 21:19:53 +02002127 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002128 num_read_xfer))
2129 return -EFAULT;
2130 break;
2131 } else {
Jens Axboe45711f12007-10-22 21:19:53 +02002132 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002133 num))
2134 return -EFAULT;
2135 num_read_xfer -= num;
2136 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 break;
Mike Christied6b10342005-11-08 04:06:41 -06002138 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Mike Christied6b10342005-11-08 04:06:41 -06002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143}
2144
2145static void
2146sg_build_reserve(Sg_fd * sfp, int req_size)
2147{
2148 Sg_scatter_hold *schp = &sfp->reserve;
2149
2150 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2151 do {
2152 if (req_size < PAGE_SIZE)
2153 req_size = PAGE_SIZE;
2154 if (0 == sg_build_indirect(schp, sfp, req_size))
2155 return;
2156 else
2157 sg_remove_scat(schp);
2158 req_size >>= 1; /* divide by 2 */
2159 } while (req_size > (PAGE_SIZE / 2));
2160}
2161
2162static void
2163sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2164{
2165 Sg_scatter_hold *req_schp = &srp->data;
2166 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002167 struct scatterlist *sg = rsv_schp->buffer;
2168 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 srp->res_used = 1;
2171 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002172 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Jens Axboeb0f655d2007-05-11 15:01:01 +02002174 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002175 num = sg->length;
2176 if (rem <= num) {
2177 sfp->save_scat_len = num;
2178 sg->length = rem;
2179 req_schp->k_use_sg = k + 1;
2180 req_schp->sglist_len = rsv_schp->sglist_len;
2181 req_schp->buffer = rsv_schp->buffer;
2182
2183 req_schp->bufflen = size;
2184 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2185 break;
2186 } else
2187 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Mike Christied6b10342005-11-08 04:06:41 -06002189
2190 if (k >= rsv_schp->k_use_sg)
2191 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
2194static void
2195sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2196{
2197 Sg_scatter_hold *req_schp = &srp->data;
2198 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2199
2200 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2201 (int) req_schp->k_use_sg));
2202 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002203 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002206 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 (unsigned) sfp->save_scat_len;
2208 else
2209 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2210 }
2211 req_schp->k_use_sg = 0;
2212 req_schp->bufflen = 0;
2213 req_schp->buffer = NULL;
2214 req_schp->sglist_len = 0;
2215 sfp->save_scat_len = 0;
2216 srp->res_used = 0;
2217}
2218
2219static Sg_request *
2220sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2221{
2222 Sg_request *resp;
2223 unsigned long iflags;
2224
2225 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2226 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2227 /* look for requests that are ready + not SG_IO owned */
2228 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2229 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2230 resp->done = 2; /* guard against other readers */
2231 break;
2232 }
2233 }
2234 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2235 return resp;
2236}
2237
2238#ifdef CONFIG_SCSI_PROC_FS
2239static Sg_request *
2240sg_get_nth_request(Sg_fd * sfp, int nth)
2241{
2242 Sg_request *resp;
2243 unsigned long iflags;
2244 int k;
2245
2246 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2247 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2248 ++k, resp = resp->nextrp) ;
2249 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2250 return resp;
2251}
2252#endif
2253
2254/* always adds to end of list */
2255static Sg_request *
2256sg_add_request(Sg_fd * sfp)
2257{
2258 int k;
2259 unsigned long iflags;
2260 Sg_request *resp;
2261 Sg_request *rp = sfp->req_arr;
2262
2263 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2264 resp = sfp->headrp;
2265 if (!resp) {
2266 memset(rp, 0, sizeof (Sg_request));
2267 rp->parentfp = sfp;
2268 resp = rp;
2269 sfp->headrp = resp;
2270 } else {
2271 if (0 == sfp->cmd_q)
2272 resp = NULL; /* command queuing disallowed */
2273 else {
2274 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2275 if (!rp->parentfp)
2276 break;
2277 }
2278 if (k < SG_MAX_QUEUE) {
2279 memset(rp, 0, sizeof (Sg_request));
2280 rp->parentfp = sfp;
2281 while (resp->nextrp)
2282 resp = resp->nextrp;
2283 resp->nextrp = rp;
2284 resp = rp;
2285 } else
2286 resp = NULL;
2287 }
2288 }
2289 if (resp) {
2290 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002291 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2294 return resp;
2295}
2296
2297/* Return of 1 for found; 0 for not found */
2298static int
2299sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2300{
2301 Sg_request *prev_rp;
2302 Sg_request *rp;
2303 unsigned long iflags;
2304 int res = 0;
2305
2306 if ((!sfp) || (!srp) || (!sfp->headrp))
2307 return res;
2308 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2309 prev_rp = sfp->headrp;
2310 if (srp == prev_rp) {
2311 sfp->headrp = prev_rp->nextrp;
2312 prev_rp->parentfp = NULL;
2313 res = 1;
2314 } else {
2315 while ((rp = prev_rp->nextrp)) {
2316 if (srp == rp) {
2317 prev_rp->nextrp = rp->nextrp;
2318 rp->parentfp = NULL;
2319 res = 1;
2320 break;
2321 }
2322 prev_rp = rp;
2323 }
2324 }
2325 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2326 return res;
2327}
2328
2329#ifdef CONFIG_SCSI_PROC_FS
2330static Sg_fd *
2331sg_get_nth_sfp(Sg_device * sdp, int nth)
2332{
2333 Sg_fd *resp;
2334 unsigned long iflags;
2335 int k;
2336
James Bottomley7c07d612007-08-05 13:36:11 -05002337 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2339 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002340 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return resp;
2342}
2343#endif
2344
2345static Sg_fd *
2346sg_add_sfp(Sg_device * sdp, int dev)
2347{
2348 Sg_fd *sfp;
2349 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002350 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Mike Christied6b10342005-11-08 04:06:41 -06002352 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (!sfp)
2354 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 init_waitqueue_head(&sfp->read_wait);
2357 rwlock_init(&sfp->rq_list_lock);
2358
2359 sfp->timeout = SG_DEFAULT_TIMEOUT;
2360 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2361 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2362 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2363 sdp->device->host->unchecked_isa_dma : 1;
2364 sfp->cmd_q = SG_DEF_COMMAND_Q;
2365 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2366 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002367 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (!sdp->headfp)
2369 sdp->headfp = sfp;
2370 else { /* add to tail of existing list */
2371 Sg_fd *pfp = sdp->headfp;
2372 while (pfp->nextfp)
2373 pfp = pfp->nextfp;
2374 pfp->nextfp = sfp;
2375 }
James Bottomley7c07d612007-08-05 13:36:11 -05002376 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002378 if (unlikely(sg_big_buff != def_reserved_size))
2379 sg_big_buff = def_reserved_size;
2380
Alan Stern44ec9542007-02-20 11:01:57 -05002381 bufflen = min_t(int, sg_big_buff,
2382 sdp->device->request_queue->max_sectors * 512);
2383 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2385 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2386 return sfp;
2387}
2388
2389static void
2390__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2391{
2392 Sg_fd *fp;
2393 Sg_fd *prev_fp;
2394
2395 prev_fp = sdp->headfp;
2396 if (sfp == prev_fp)
2397 sdp->headfp = prev_fp->nextfp;
2398 else {
2399 while ((fp = prev_fp->nextfp)) {
2400 if (sfp == fp) {
2401 prev_fp->nextfp = fp->nextfp;
2402 break;
2403 }
2404 prev_fp = fp;
2405 }
2406 }
2407 if (sfp->reserve.bufflen > 0) {
2408 SCSI_LOG_TIMEOUT(6,
2409 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2410 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 sg_remove_scat(&sfp->reserve);
2412 }
2413 sfp->parentdp = NULL;
2414 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002415 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418/* Returns 0 in normal case, 1 when detached and sdp object removed */
2419static int
2420sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2421{
2422 Sg_request *srp;
2423 Sg_request *tsrp;
2424 int dirty = 0;
2425 int res = 0;
2426
2427 for (srp = sfp->headrp; srp; srp = tsrp) {
2428 tsrp = srp->nextrp;
2429 if (sg_srp_done(srp, sfp))
2430 sg_finish_rem_req(srp);
2431 else
2432 ++dirty;
2433 }
2434 if (0 == dirty) {
2435 unsigned long iflags;
2436
James Bottomley7c07d612007-08-05 13:36:11 -05002437 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 __sg_remove_sfp(sdp, sfp);
2439 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002440 idr_remove(&sg_index_idr, sdp->index);
2441 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 res = 1;
2443 }
James Bottomley7c07d612007-08-05 13:36:11 -05002444 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 } else {
2446 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2447 /* only bump the access_count if we actually succeeded in
2448 * throwing another counter on the host module */
2449 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2450 sfp->closed = 1; /* flag dirty state on this fd */
2451 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2452 dirty));
2453 }
2454 return res;
2455}
2456
2457static int
2458sg_res_in_use(Sg_fd * sfp)
2459{
2460 const Sg_request *srp;
2461 unsigned long iflags;
2462
2463 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2464 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2465 if (srp->res_used)
2466 break;
2467 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2468 return srp ? 1 : 0;
2469}
2470
Douglas Gilbert6460e752006-09-20 18:20:49 -04002471/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002472static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2474{
Mike Christied6b10342005-11-08 04:06:41 -06002475 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002476 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002478 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Douglas Gilbert6460e752006-09-20 18:20:49 -04002480 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return resp;
2482
2483 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002484 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002486 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2489 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002490 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002491 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002492 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 --order;
2494 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002495 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 resSz = a_size;
2497 }
2498 if (resp) {
2499 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002500 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002501 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
2503 return resp;
2504}
2505
2506static void
Mike Christied6b10342005-11-08 04:06:41 -06002507sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
2509 int order, a_size;
2510
Mike Christied6b10342005-11-08 04:06:41 -06002511 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return;
2513 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2514 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002515 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518#ifdef CONFIG_SCSI_PROC_FS
2519static int
James Bottomley7c07d612007-08-05 13:36:11 -05002520sg_idr_max_id(int id, void *p, void *data)
2521{
2522 int *k = data;
2523
2524 if (*k < id)
2525 *k = id;
2526
2527 return 0;
2528}
2529
2530static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531sg_last_dev(void)
2532{
Tony Battersby53474c02008-01-22 15:25:49 -05002533 int k = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 unsigned long iflags;
2535
James Bottomley7c07d612007-08-05 13:36:11 -05002536 read_lock_irqsave(&sg_index_lock, iflags);
2537 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2538 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return k + 1; /* origin 1 */
2540}
2541#endif
2542
2543static Sg_device *
2544sg_get_dev(int dev)
2545{
James Bottomley7c07d612007-08-05 13:36:11 -05002546 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 unsigned long iflags;
2548
James Bottomley7c07d612007-08-05 13:36:11 -05002549 read_lock_irqsave(&sg_index_lock, iflags);
2550 sdp = idr_find(&sg_index_idr, dev);
2551 read_unlock_irqrestore(&sg_index_lock, iflags);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return sdp;
2554}
2555
2556#ifdef CONFIG_SCSI_PROC_FS
2557
2558static struct proc_dir_entry *sg_proc_sgp = NULL;
2559
2560static char sg_proc_sg_dirname[] = "scsi/sg";
2561
2562static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2563
2564static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2565static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2566 size_t count, loff_t *off);
2567static struct file_operations adio_fops = {
2568 /* .owner, .read and .llseek added in sg_proc_init() */
2569 .open = sg_proc_single_open_adio,
2570 .write = sg_proc_write_adio,
2571 .release = single_release,
2572};
2573
2574static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2575static ssize_t sg_proc_write_dressz(struct file *filp,
2576 const char __user *buffer, size_t count, loff_t *off);
2577static struct file_operations dressz_fops = {
2578 .open = sg_proc_single_open_dressz,
2579 .write = sg_proc_write_dressz,
2580 .release = single_release,
2581};
2582
2583static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2584static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2585static struct file_operations version_fops = {
2586 .open = sg_proc_single_open_version,
2587 .release = single_release,
2588};
2589
2590static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2591static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2592static struct file_operations devhdr_fops = {
2593 .open = sg_proc_single_open_devhdr,
2594 .release = single_release,
2595};
2596
2597static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2598static int sg_proc_open_dev(struct inode *inode, struct file *file);
2599static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2600static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2601static void dev_seq_stop(struct seq_file *s, void *v);
2602static struct file_operations dev_fops = {
2603 .open = sg_proc_open_dev,
2604 .release = seq_release,
2605};
2606static struct seq_operations dev_seq_ops = {
2607 .start = dev_seq_start,
2608 .next = dev_seq_next,
2609 .stop = dev_seq_stop,
2610 .show = sg_proc_seq_show_dev,
2611};
2612
2613static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2614static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2615static struct file_operations devstrs_fops = {
2616 .open = sg_proc_open_devstrs,
2617 .release = seq_release,
2618};
2619static struct seq_operations devstrs_seq_ops = {
2620 .start = dev_seq_start,
2621 .next = dev_seq_next,
2622 .stop = dev_seq_stop,
2623 .show = sg_proc_seq_show_devstrs,
2624};
2625
2626static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2627static int sg_proc_open_debug(struct inode *inode, struct file *file);
2628static struct file_operations debug_fops = {
2629 .open = sg_proc_open_debug,
2630 .release = seq_release,
2631};
2632static struct seq_operations debug_seq_ops = {
2633 .start = dev_seq_start,
2634 .next = dev_seq_next,
2635 .stop = dev_seq_stop,
2636 .show = sg_proc_seq_show_debug,
2637};
2638
2639
2640struct sg_proc_leaf {
2641 const char * name;
2642 struct file_operations * fops;
2643};
2644
2645static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2646 {"allow_dio", &adio_fops},
2647 {"debug", &debug_fops},
2648 {"def_reserved_size", &dressz_fops},
2649 {"device_hdr", &devhdr_fops},
2650 {"devices", &dev_fops},
2651 {"device_strs", &devstrs_fops},
2652 {"version", &version_fops}
2653};
2654
2655static int
2656sg_proc_init(void)
2657{
2658 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002659 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct sg_proc_leaf * leaf;
2661
Al Viro66600222005-09-28 22:32:57 +01002662 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (!sg_proc_sgp)
2664 return 1;
2665 for (k = 0; k < num_leaves; ++k) {
2666 leaf = &sg_proc_leaf_arr[k];
2667 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
Denis V. Luneva9739092008-04-29 01:02:17 -07002668 leaf->fops->owner = THIS_MODULE;
2669 leaf->fops->read = seq_read;
2670 leaf->fops->llseek = seq_lseek;
2671 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 }
2673 return 0;
2674}
2675
2676static void
2677sg_proc_cleanup(void)
2678{
2679 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002680 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 if (!sg_proc_sgp)
2683 return;
2684 for (k = 0; k < num_leaves; ++k)
2685 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2686 remove_proc_entry(sg_proc_sg_dirname, NULL);
2687}
2688
2689
2690static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2691{
2692 seq_printf(s, "%d\n", *((int *)s->private));
2693 return 0;
2694}
2695
2696static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2697{
2698 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2699}
2700
2701static ssize_t
2702sg_proc_write_adio(struct file *filp, const char __user *buffer,
2703 size_t count, loff_t *off)
2704{
2705 int num;
2706 char buff[11];
2707
2708 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2709 return -EACCES;
2710 num = (count < 10) ? count : 10;
2711 if (copy_from_user(buff, buffer, num))
2712 return -EFAULT;
2713 buff[num] = '\0';
2714 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2715 return count;
2716}
2717
2718static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2719{
2720 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2721}
2722
2723static ssize_t
2724sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2725 size_t count, loff_t *off)
2726{
2727 int num;
2728 unsigned long k = ULONG_MAX;
2729 char buff[11];
2730
2731 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2732 return -EACCES;
2733 num = (count < 10) ? count : 10;
2734 if (copy_from_user(buff, buffer, num))
2735 return -EFAULT;
2736 buff[num] = '\0';
2737 k = simple_strtoul(buff, NULL, 10);
2738 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2739 sg_big_buff = k;
2740 return count;
2741 }
2742 return -ERANGE;
2743}
2744
2745static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2746{
2747 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2748 sg_version_date);
2749 return 0;
2750}
2751
2752static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2753{
2754 return single_open(file, sg_proc_seq_show_version, NULL);
2755}
2756
2757static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2758{
2759 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2760 "online\n");
2761 return 0;
2762}
2763
2764static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2765{
2766 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2767}
2768
2769struct sg_proc_deviter {
2770 loff_t index;
2771 size_t max;
2772};
2773
2774static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2775{
2776 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2777
Jan Blunck729d70f2005-08-27 11:07:52 -07002778 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (! it)
2780 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 it->index = *pos;
2783 it->max = sg_last_dev();
2784 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002785 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2790{
Jan Blunck729d70f2005-08-27 11:07:52 -07002791 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 *pos = ++it->index;
2794 return (it->index < it->max) ? it : NULL;
2795}
2796
2797static void dev_seq_stop(struct seq_file *s, void *v)
2798{
Jan Blunck729d70f2005-08-27 11:07:52 -07002799 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
2802static int sg_proc_open_dev(struct inode *inode, struct file *file)
2803{
2804 return seq_open(file, &dev_seq_ops);
2805}
2806
2807static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2808{
2809 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2810 Sg_device *sdp;
2811 struct scsi_device *scsidp;
2812
2813 sdp = it ? sg_get_dev(it->index) : NULL;
2814 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2815 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2816 scsidp->host->host_no, scsidp->channel,
2817 scsidp->id, scsidp->lun, (int) scsidp->type,
2818 1,
2819 (int) scsidp->queue_depth,
2820 (int) scsidp->device_busy,
2821 (int) scsi_device_online(scsidp));
2822 else
2823 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2824 return 0;
2825}
2826
2827static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2828{
2829 return seq_open(file, &devstrs_seq_ops);
2830}
2831
2832static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2833{
2834 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2835 Sg_device *sdp;
2836 struct scsi_device *scsidp;
2837
2838 sdp = it ? sg_get_dev(it->index) : NULL;
2839 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2840 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2841 scsidp->vendor, scsidp->model, scsidp->rev);
2842 else
2843 seq_printf(s, "<no active device>\n");
2844 return 0;
2845}
2846
2847static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2848{
2849 int k, m, new_interface, blen, usg;
2850 Sg_request *srp;
2851 Sg_fd *fp;
2852 const sg_io_hdr_t *hp;
2853 const char * cp;
cb59e842005-04-02 13:51:23 -06002854 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2857 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2858 "(res)sgat=%d low_dma=%d\n", k + 1,
2859 jiffies_to_msecs(fp->timeout),
2860 fp->reserve.bufflen,
2861 (int) fp->reserve.k_use_sg,
2862 (int) fp->low_dma);
2863 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2864 (int) fp->cmd_q, (int) fp->force_packid,
2865 (int) fp->keep_orphan, (int) fp->closed);
2866 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2867 hp = &srp->header;
2868 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2869 if (srp->res_used) {
2870 if (new_interface &&
2871 (SG_FLAG_MMAP_IO & hp->flags))
2872 cp = " mmap>> ";
2873 else
2874 cp = " rb>> ";
2875 } else {
2876 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2877 cp = " dio>> ";
2878 else
2879 cp = " ";
2880 }
2881 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002882 blen = srp->data.bufflen;
2883 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 seq_printf(s, srp->done ?
2885 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002886 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 seq_printf(s, " id=%d blen=%d",
2888 srp->header.pack_id, blen);
2889 if (srp->done)
2890 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002891 else {
2892 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002894 (new_interface ? hp->timeout :
2895 jiffies_to_msecs(fp->timeout)),
2896 (ms > hp->duration ? ms - hp->duration : 0));
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2899 (int) srp->data.cmd_opcode);
2900 }
2901 if (0 == m)
2902 seq_printf(s, " No requests active\n");
2903 }
2904}
2905
2906static int sg_proc_open_debug(struct inode *inode, struct file *file)
2907{
2908 return seq_open(file, &debug_seq_ops);
2909}
2910
2911static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2912{
2913 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2914 Sg_device *sdp;
2915
2916 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002917 seq_printf(s, "max_active_device=%d(origin 1)\n",
2918 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2920 }
2921 sdp = it ? sg_get_dev(it->index) : NULL;
2922 if (sdp) {
2923 struct scsi_device *scsidp = sdp->device;
2924
2925 if (NULL == scsidp) {
2926 seq_printf(s, "device %d detached ??\n",
2927 (int)it->index);
2928 return 0;
2929 }
2930
2931 if (sg_get_nth_sfp(sdp, 0)) {
2932 seq_printf(s, " >>> device=%s ",
2933 sdp->disk->disk_name);
2934 if (sdp->detached)
2935 seq_printf(s, "detached pending close ");
2936 else
2937 seq_printf
2938 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2939 scsidp->host->host_no,
2940 scsidp->channel, scsidp->id,
2941 scsidp->lun,
2942 scsidp->host->hostt->emulated);
2943 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2944 sdp->sg_tablesize, sdp->exclude);
2945 }
2946 sg_proc_debug_helper(s, sdp);
2947 }
2948 return 0;
2949}
2950
2951#endif /* CONFIG_SCSI_PROC_FS */
2952
2953module_init(init_sg);
2954module_exit(exit_sg);