blob: b5fa4f091387eb83cdc9009e2050d0062fa9201f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "scsi.h"
db9dff32005-04-03 14:53:59 -050053#include <scsi/scsi_dbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_host.h>
55#include <scsi/scsi_driver.h>
56#include <scsi/scsi_ioctl.h>
57#include <scsi/sg.h>
58
59#include "scsi_logging.h"
60
61#ifdef CONFIG_SCSI_PROC_FS
62#include <linux/proc_fs.h>
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -040063static char *sg_version_date = "20061027";
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int sg_proc_init(void);
66static void sg_proc_cleanup(void);
67#endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define SG_ALLOW_DIO_DEF 0
70#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
71
72#define SG_MAX_DEVS 32768
73
74/*
75 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
76 * Then when using 32 bit integers x * m may overflow during the calculation.
77 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
78 * calculates the same, but prevents the overflow when both m and d
79 * are "small" numbers (like HZ and USER_HZ).
80 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
81 * in 32 bits.
82 */
83#define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
84
85#define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
86
87int sg_big_buff = SG_DEF_RESERVED_SIZE;
88/* N.B. This variable is readable and writeable via
89 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
90 of this size (or less if there is not enough memory) will be reserved
91 for use by this file descriptor. [Deprecated usage: this variable is also
92 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
93 the kernel (i.e. it is not a module).] */
94static int def_reserved_size = -1; /* picks up init parameter */
95static int sg_allow_dio = SG_ALLOW_DIO_DEF;
96
Douglas Gilbert6460e752006-09-20 18:20:49 -040097static int scatter_elem_sz = SG_SCATTER_SZ;
98static int scatter_elem_sz_prev = SG_SCATTER_SZ;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define SG_SECTOR_SZ 512
101#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
102
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500103static int sg_add(struct class_device *, struct class_interface *);
104static void sg_remove(struct class_device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
James Bottomley7c07d612007-08-05 13:36:11 -0500106static DEFINE_IDR(sg_index_idr);
107static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 file descriptor list for device */
109
110static struct class_interface sg_interface = {
111 .add = sg_add,
112 .remove = sg_remove,
113};
114
115typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
116 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
FUJITA Tomonoriea312552007-08-06 00:31:24 +0900117 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned bufflen; /* Size of (aggregate) data buffer */
119 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
Mike Christied6b10342005-11-08 04:06:41 -0600120 struct scatterlist *buffer;/* scatter list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
122 unsigned char cmd_opcode; /* first byte of command */
123} Sg_scatter_hold;
124
125struct sg_device; /* forward declarations */
126struct sg_fd;
127
128typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct sg_request *nextrp; /* NULL -> tail request (slist) */
130 struct sg_fd *parentfp; /* NULL -> not in use */
131 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
132 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
Mike Christied6b10342005-11-08 04:06:41 -0600133 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
135 char orphan; /* 1 -> drop on sight, 0 -> normal */
136 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
137 volatile char done; /* 0->before bh, 1->before read, 2->read */
138} Sg_request;
139
140typedef struct sg_fd { /* holds the state of a file descriptor */
141 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
142 struct sg_device *parentdp; /* owning device */
143 wait_queue_head_t read_wait; /* queue read until command done */
144 rwlock_t rq_list_lock; /* protect access to list in req_arr */
145 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
146 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
147 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
148 unsigned save_scat_len; /* original length of trunc. scat. element */
149 Sg_request *headrp; /* head of request slist, NULL->empty */
150 struct fasync_struct *async_qp; /* used by asynchronous notification */
151 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
152 char low_dma; /* as in parent but possibly overridden to 1 */
153 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
154 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
155 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
156 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
157 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
158 char mmap_called; /* 0 -> mmap() never called on this fd */
159} Sg_fd;
160
161typedef struct sg_device { /* holds the state of each scsi generic device */
162 struct scsi_device *device;
163 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
164 int sg_tablesize; /* adapter's max scatter-gather table size */
James Bottomley7c07d612007-08-05 13:36:11 -0500165 u32 index; /* device index number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 Sg_fd *headfp; /* first open fd belonging to this device */
167 volatile char detached; /* 0->attached, 1->detached pending removal */
168 volatile char exclude; /* opened for exclusive access */
169 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
170 struct gendisk *disk;
171 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
172} Sg_device;
173
174static int sg_fasync(int fd, struct file *filp, int mode);
Mike Christied6b10342005-11-08 04:06:41 -0600175/* tasklet or soft irq callback */
176static void sg_cmd_done(void *data, char *sense, int result, int resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int sg_start_req(Sg_request * srp);
178static void sg_finish_rem_req(Sg_request * srp);
179static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
180static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
181 int tablesize);
182static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
183 Sg_request * srp);
184static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
185 int blocking, int read_only, Sg_request ** o_srp);
186static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
187 unsigned char *cmnd, int timeout, int blocking);
188static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
189 int wr_xf, int *countp, unsigned char __user **up);
190static int sg_write_xfer(Sg_request * srp);
191static int sg_read_xfer(Sg_request * srp);
192static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
193static void sg_remove_scat(Sg_scatter_hold * schp);
194static void sg_build_reserve(Sg_fd * sfp, int req_size);
195static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
196static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
Mike Christied6b10342005-11-08 04:06:41 -0600197static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
198static void sg_page_free(struct page *page, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
200static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
201static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
203static Sg_request *sg_add_request(Sg_fd * sfp);
204static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
205static int sg_res_in_use(Sg_fd * sfp);
206static int sg_allow_access(unsigned char opcode, char dev_type);
207static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
208static Sg_device *sg_get_dev(int dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#ifdef CONFIG_SCSI_PROC_FS
210static int sg_last_dev(void);
211#endif
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define SZ_SG_HEADER sizeof(struct sg_header)
214#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
215#define SZ_SG_IOVEC sizeof(sg_iovec_t)
216#define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
217
218static int
219sg_open(struct inode *inode, struct file *filp)
220{
221 int dev = iminor(inode);
222 int flags = filp->f_flags;
Mike Christied6b10342005-11-08 04:06:41 -0600223 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 Sg_device *sdp;
225 Sg_fd *sfp;
226 int res;
227 int retval;
228
229 nonseekable_open(inode, filp);
230 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
231 sdp = sg_get_dev(dev);
232 if ((!sdp) || (!sdp->device))
233 return -ENXIO;
234 if (sdp->detached)
235 return -ENODEV;
236
237 /* This driver's module count bumped by fops_get in <linux/fs.h> */
238 /* Prevent the device driver from vanishing while we sleep */
239 retval = scsi_device_get(sdp->device);
240 if (retval)
241 return retval;
242
243 if (!((flags & O_NONBLOCK) ||
244 scsi_block_when_processing_errors(sdp->device))) {
245 retval = -ENXIO;
246 /* we are in error recovery for this device */
247 goto error_out;
248 }
249
250 if (flags & O_EXCL) {
251 if (O_RDONLY == (flags & O_ACCMODE)) {
252 retval = -EPERM; /* Can't lock it with read only access */
253 goto error_out;
254 }
255 if (sdp->headfp && (flags & O_NONBLOCK)) {
256 retval = -EBUSY;
257 goto error_out;
258 }
259 res = 0;
260 __wait_event_interruptible(sdp->o_excl_wait,
261 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
262 if (res) {
263 retval = res; /* -ERESTARTSYS because signal hit process */
264 goto error_out;
265 }
266 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
267 if (flags & O_NONBLOCK) {
268 retval = -EBUSY;
269 goto error_out;
270 }
271 res = 0;
272 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
273 res);
274 if (res) {
275 retval = res; /* -ERESTARTSYS because signal hit process */
276 goto error_out;
277 }
278 }
279 if (sdp->detached) {
280 retval = -ENODEV;
281 goto error_out;
282 }
283 if (!sdp->headfp) { /* no existing opens on this device */
284 sdp->sgdebug = 0;
Mike Christied6b10342005-11-08 04:06:41 -0600285 q = sdp->device->request_queue;
286 sdp->sg_tablesize = min(q->max_hw_segments,
287 q->max_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 if ((sfp = sg_add_sfp(sdp, dev)))
290 filp->private_data = sfp;
291 else {
292 if (flags & O_EXCL)
293 sdp->exclude = 0; /* undo if error */
294 retval = -ENOMEM;
295 goto error_out;
296 }
297 return 0;
298
299 error_out:
300 scsi_device_put(sdp->device);
301 return retval;
302}
303
304/* Following function was formerly called 'sg_close' */
305static int
306sg_release(struct inode *inode, struct file *filp)
307{
308 Sg_device *sdp;
309 Sg_fd *sfp;
310
311 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
312 return -ENXIO;
313 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
314 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
315 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
316 if (!sdp->detached) {
317 scsi_device_put(sdp->device);
318 }
319 sdp->exclude = 0;
320 wake_up_interruptible(&sdp->o_excl_wait);
321 }
322 return 0;
323}
324
325static ssize_t
326sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 Sg_device *sdp;
329 Sg_fd *sfp;
330 Sg_request *srp;
331 int req_pack_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sg_io_hdr_t *hp;
cb59e842005-04-02 13:51:23 -0600333 struct sg_header *old_hdr = NULL;
334 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
337 return -ENXIO;
338 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
339 sdp->disk->disk_name, (int) count));
Mike Christied6b10342005-11-08 04:06:41 -0600340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!access_ok(VERIFY_WRITE, buf, count))
342 return -EFAULT;
343 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
cb59e842005-04-02 13:51:23 -0600344 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
345 if (!old_hdr)
346 return -ENOMEM;
347 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
348 retval = -EFAULT;
349 goto free_old_hdr;
350 }
351 if (old_hdr->reply_len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (count >= SZ_SG_IO_HDR) {
cb59e842005-04-02 13:51:23 -0600353 sg_io_hdr_t *new_hdr;
354 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
355 if (!new_hdr) {
356 retval = -ENOMEM;
357 goto free_old_hdr;
358 }
359 retval =__copy_from_user
360 (new_hdr, buf, SZ_SG_IO_HDR);
361 req_pack_id = new_hdr->pack_id;
362 kfree(new_hdr);
363 if (retval) {
364 retval = -EFAULT;
365 goto free_old_hdr;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 } else
cb59e842005-04-02 13:51:23 -0600369 req_pack_id = old_hdr->pack_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 srp = sg_get_rq_mark(sfp, req_pack_id);
372 if (!srp) { /* now wait on packet to arrive */
cb59e842005-04-02 13:51:23 -0600373 if (sdp->detached) {
374 retval = -ENODEV;
375 goto free_old_hdr;
376 }
377 if (filp->f_flags & O_NONBLOCK) {
378 retval = -EAGAIN;
379 goto free_old_hdr;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 while (1) {
cb59e842005-04-02 13:51:23 -0600382 retval = 0; /* following macro beats race condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 __wait_event_interruptible(sfp->read_wait,
cb59e842005-04-02 13:51:23 -0600384 (sdp->detached ||
385 (srp = sg_get_rq_mark(sfp, req_pack_id))),
386 retval);
387 if (sdp->detached) {
388 retval = -ENODEV;
389 goto free_old_hdr;
390 }
391 if (0 == retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
cb59e842005-04-02 13:51:23 -0600393
394 /* -ERESTARTSYS as signal hit process */
395 goto free_old_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 }
cb59e842005-04-02 13:51:23 -0600398 if (srp->header.interface_id != '\0') {
399 retval = sg_new_read(sfp, buf, count, srp);
400 goto free_old_hdr;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 hp = &srp->header;
cb59e842005-04-02 13:51:23 -0600404 if (old_hdr == NULL) {
405 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
406 if (! old_hdr) {
407 retval = -ENOMEM;
408 goto free_old_hdr;
409 }
410 }
411 memset(old_hdr, 0, SZ_SG_HEADER);
412 old_hdr->reply_len = (int) hp->timeout;
413 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
414 old_hdr->pack_id = hp->pack_id;
415 old_hdr->twelve_byte =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
cb59e842005-04-02 13:51:23 -0600417 old_hdr->target_status = hp->masked_status;
418 old_hdr->host_status = hp->host_status;
419 old_hdr->driver_status = hp->driver_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if ((CHECK_CONDITION & hp->masked_status) ||
421 (DRIVER_SENSE & hp->driver_status))
cb59e842005-04-02 13:51:23 -0600422 memcpy(old_hdr->sense_buffer, srp->sense_b,
423 sizeof (old_hdr->sense_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 switch (hp->host_status) {
425 /* This setup of 'result' is for backward compatibility and is best
426 ignored by the user who should use target, host + driver status */
427 case DID_OK:
428 case DID_PASSTHROUGH:
429 case DID_SOFT_ERROR:
cb59e842005-04-02 13:51:23 -0600430 old_hdr->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 case DID_NO_CONNECT:
433 case DID_BUS_BUSY:
434 case DID_TIME_OUT:
cb59e842005-04-02 13:51:23 -0600435 old_hdr->result = EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 case DID_BAD_TARGET:
438 case DID_ABORT:
439 case DID_PARITY:
440 case DID_RESET:
441 case DID_BAD_INTR:
cb59e842005-04-02 13:51:23 -0600442 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 case DID_ERROR:
cb59e842005-04-02 13:51:23 -0600445 old_hdr->result = (srp->sense_b[0] == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 hp->masked_status == GOOD) ? 0 : EIO;
447 break;
448 default:
cb59e842005-04-02 13:51:23 -0600449 old_hdr->result = EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 }
452
453 /* Now copy the result back to the user buffer. */
454 if (count >= SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600455 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
456 retval = -EFAULT;
457 goto free_old_hdr;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 buf += SZ_SG_HEADER;
cb59e842005-04-02 13:51:23 -0600460 if (count > old_hdr->reply_len)
461 count = old_hdr->reply_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (count > SZ_SG_HEADER) {
cb59e842005-04-02 13:51:23 -0600463 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
464 retval = -EFAULT;
465 goto free_old_hdr;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 } else
cb59e842005-04-02 13:51:23 -0600469 count = (old_hdr->result == 0) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sg_finish_rem_req(srp);
cb59e842005-04-02 13:51:23 -0600471 retval = count;
472free_old_hdr:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800473 kfree(old_hdr);
cb59e842005-04-02 13:51:23 -0600474 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static ssize_t
478sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
479{
480 sg_io_hdr_t *hp = &srp->header;
481 int err = 0;
482 int len;
483
484 if (count < SZ_SG_IO_HDR) {
485 err = -EINVAL;
486 goto err_out;
487 }
488 hp->sb_len_wr = 0;
489 if ((hp->mx_sb_len > 0) && hp->sbp) {
490 if ((CHECK_CONDITION & hp->masked_status) ||
491 (DRIVER_SENSE & hp->driver_status)) {
Mike Christied6b10342005-11-08 04:06:41 -0600492 int sb_len = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
494 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
495 len = (len > sb_len) ? sb_len : len;
496 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
497 err = -EFAULT;
498 goto err_out;
499 }
500 hp->sb_len_wr = len;
501 }
502 }
503 if (hp->masked_status || hp->host_status || hp->driver_status)
504 hp->info |= SG_INFO_CHECK;
505 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
506 err = -EFAULT;
507 goto err_out;
508 }
509 err = sg_read_xfer(srp);
510 err_out:
511 sg_finish_rem_req(srp);
512 return (0 == err) ? count : err;
513}
514
515static ssize_t
516sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
517{
518 int mxsize, cmd_size, k;
519 int input_size, blocking;
520 unsigned char opcode;
521 Sg_device *sdp;
522 Sg_fd *sfp;
523 Sg_request *srp;
524 struct sg_header old_hdr;
525 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600526 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
529 return -ENXIO;
530 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
531 sdp->disk->disk_name, (int) count));
532 if (sdp->detached)
533 return -ENODEV;
534 if (!((filp->f_flags & O_NONBLOCK) ||
535 scsi_block_when_processing_errors(sdp->device)))
536 return -ENXIO;
537
538 if (!access_ok(VERIFY_READ, buf, count))
539 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
540 if (count < SZ_SG_HEADER)
541 return -EIO;
542 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
543 return -EFAULT;
544 blocking = !(filp->f_flags & O_NONBLOCK);
545 if (old_hdr.reply_len < 0)
546 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
547 if (count < (SZ_SG_HEADER + 6))
548 return -EIO; /* The minimum scsi command length is 6 bytes. */
549
550 if (!(srp = sg_add_request(sfp))) {
551 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
552 return -EDOM;
553 }
554 buf += SZ_SG_HEADER;
555 __get_user(opcode, buf);
556 if (sfp->next_cmd_len > 0) {
557 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
558 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
559 sfp->next_cmd_len = 0;
560 sg_remove_request(sfp, srp);
561 return -EIO;
562 }
563 cmd_size = sfp->next_cmd_len;
564 sfp->next_cmd_len = 0; /* reset so only this write() effected */
565 } else {
566 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
567 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
568 cmd_size = 12;
569 }
570 SCSI_LOG_TIMEOUT(4, printk(
571 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
572/* Determine buffer size. */
573 input_size = count - cmd_size;
574 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
575 mxsize -= SZ_SG_HEADER;
576 input_size -= SZ_SG_HEADER;
577 if (input_size < 0) {
578 sg_remove_request(sfp, srp);
579 return -EIO; /* User did not pass enough bytes for this command. */
580 }
581 hp = &srp->header;
582 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
583 hp->cmd_len = (unsigned char) cmd_size;
584 hp->iovec_count = 0;
585 hp->mx_sb_len = 0;
586 if (input_size > 0)
587 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
588 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
589 else
590 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
591 hp->dxfer_len = mxsize;
592 hp->dxferp = (char __user *)buf + cmd_size;
593 hp->sbp = NULL;
594 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
595 hp->flags = input_size; /* structure abuse ... */
596 hp->pack_id = old_hdr.pack_id;
597 hp->usr_ptr = NULL;
598 if (__copy_from_user(cmnd, buf, cmd_size))
599 return -EFAULT;
600 /*
601 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
602 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
603 * is a non-zero input_size, so emit a warning.
604 */
605 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
606 if (printk_ratelimit())
607 printk(KERN_WARNING
608 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
609 "guessing data in;\n" KERN_WARNING " "
610 "program %s not setting count and/or reply_len properly\n",
611 old_hdr.reply_len - (int)SZ_SG_HEADER,
612 input_size, (unsigned int) cmnd[0],
613 current->comm);
614 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
615 return (k < 0) ? k : count;
616}
617
618static ssize_t
619sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
620 int blocking, int read_only, Sg_request ** o_srp)
621{
622 int k;
623 Sg_request *srp;
624 sg_io_hdr_t *hp;
Mike Christied6b10342005-11-08 04:06:41 -0600625 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int timeout;
627 unsigned long ul_timeout;
628
629 if (count < SZ_SG_IO_HDR)
630 return -EINVAL;
631 if (!access_ok(VERIFY_READ, buf, count))
632 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
633
634 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
635 if (!(srp = sg_add_request(sfp))) {
636 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
637 return -EDOM;
638 }
639 hp = &srp->header;
640 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
641 sg_remove_request(sfp, srp);
642 return -EFAULT;
643 }
644 if (hp->interface_id != 'S') {
645 sg_remove_request(sfp, srp);
646 return -ENOSYS;
647 }
648 if (hp->flags & SG_FLAG_MMAP_IO) {
649 if (hp->dxfer_len > sfp->reserve.bufflen) {
650 sg_remove_request(sfp, srp);
651 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
652 }
653 if (hp->flags & SG_FLAG_DIRECT_IO) {
654 sg_remove_request(sfp, srp);
655 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
656 }
657 if (sg_res_in_use(sfp)) {
658 sg_remove_request(sfp, srp);
659 return -EBUSY; /* reserve buffer already being used */
660 }
661 }
662 ul_timeout = msecs_to_jiffies(srp->header.timeout);
663 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
664 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
665 sg_remove_request(sfp, srp);
666 return -EMSGSIZE;
667 }
668 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
669 sg_remove_request(sfp, srp);
670 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
671 }
672 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
673 sg_remove_request(sfp, srp);
674 return -EFAULT;
675 }
676 if (read_only &&
677 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
678 sg_remove_request(sfp, srp);
679 return -EPERM;
680 }
681 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
682 if (k < 0)
683 return k;
684 if (o_srp)
685 *o_srp = srp;
686 return count;
687}
688
689static int
690sg_common_write(Sg_fd * sfp, Sg_request * srp,
691 unsigned char *cmnd, int timeout, int blocking)
692{
Mike Christied6b10342005-11-08 04:06:41 -0600693 int k, data_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 Sg_device *sdp = sfp->parentdp;
695 sg_io_hdr_t *hp = &srp->header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
698 hp->status = 0;
699 hp->masked_status = 0;
700 hp->msg_status = 0;
701 hp->info = 0;
702 hp->host_status = 0;
703 hp->driver_status = 0;
704 hp->resid = 0;
705 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
706 (int) cmnd[0], (int) hp->cmd_len));
707
708 if ((k = sg_start_req(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400709 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 sg_finish_rem_req(srp);
711 return k; /* probably out of space --> ENOMEM */
712 }
713 if ((k = sg_write_xfer(srp))) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400714 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 sg_finish_rem_req(srp);
716 return k;
717 }
718 if (sdp->detached) {
719 sg_finish_rem_req(srp);
720 return -ENODEV;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 switch (hp->dxfer_direction) {
724 case SG_DXFER_TO_FROM_DEV:
725 case SG_DXFER_FROM_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600726 data_dir = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
728 case SG_DXFER_TO_DEV:
Mike Christied6b10342005-11-08 04:06:41 -0600729 data_dir = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
731 case SG_DXFER_UNKNOWN:
Mike Christied6b10342005-11-08 04:06:41 -0600732 data_dir = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734 default:
Mike Christied6b10342005-11-08 04:06:41 -0600735 data_dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 }
cb59e842005-04-02 13:51:23 -0600738 hp->duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/* Now send everything of to mid-level. The next time we hear about this
740 packet is when sg_cmd_done() is called (i.e. a callback). */
brking@us.ibm.combb1d1072006-01-23 15:03:22 -0600741 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
Mike Christied6b10342005-11-08 04:06:41 -0600742 hp->dxfer_len, srp->data.k_use_sg, timeout,
743 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
744 GFP_ATOMIC)) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -0400745 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
Mike Christied6b10342005-11-08 04:06:41 -0600746 /*
747 * most likely out of mem, but could also be a bad map
748 */
Mike Christie18c49b82006-03-22 16:04:38 -0600749 sg_finish_rem_req(srp);
Mike Christied6b10342005-11-08 04:06:41 -0600750 return -ENOMEM;
751 } else
752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static int
756sg_srp_done(Sg_request *srp, Sg_fd *sfp)
757{
758 unsigned long iflags;
759 int done;
760
761 read_lock_irqsave(&sfp->rq_list_lock, iflags);
762 done = srp->done;
763 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
764 return done;
765}
766
767static int
768sg_ioctl(struct inode *inode, struct file *filp,
769 unsigned int cmd_in, unsigned long arg)
770{
771 void __user *p = (void __user *)arg;
772 int __user *ip = p;
773 int result, val, read_only;
774 Sg_device *sdp;
775 Sg_fd *sfp;
776 Sg_request *srp;
777 unsigned long iflags;
778
779 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
780 return -ENXIO;
781 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
782 sdp->disk->disk_name, (int) cmd_in));
783 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
784
785 switch (cmd_in) {
786 case SG_IO:
787 {
788 int blocking = 1; /* ignore O_NONBLOCK flag */
789
790 if (sdp->detached)
791 return -ENODEV;
792 if (!scsi_block_when_processing_errors(sdp->device))
793 return -ENXIO;
794 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
795 return -EFAULT;
796 result =
797 sg_new_write(sfp, p, SZ_SG_IO_HDR,
798 blocking, read_only, &srp);
799 if (result < 0)
800 return result;
801 srp->sg_io_owned = 1;
802 while (1) {
803 result = 0; /* following macro to beat race condition */
804 __wait_event_interruptible(sfp->read_wait,
805 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
806 result);
807 if (sdp->detached)
808 return -ENODEV;
809 if (sfp->closed)
810 return 0; /* request packet dropped already */
811 if (0 == result)
812 break;
813 srp->orphan = 1;
814 return result; /* -ERESTARTSYS because signal hit process */
815 }
816 write_lock_irqsave(&sfp->rq_list_lock, iflags);
817 srp->done = 2;
818 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
819 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
820 return (result < 0) ? result : 0;
821 }
822 case SG_SET_TIMEOUT:
823 result = get_user(val, ip);
824 if (result)
825 return result;
826 if (val < 0)
827 return -EIO;
828 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
829 val = MULDIV (INT_MAX, USER_HZ, HZ);
830 sfp->timeout_user = val;
831 sfp->timeout = MULDIV (val, HZ, USER_HZ);
832
833 return 0;
834 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
835 /* strange ..., for backward compatibility */
836 return sfp->timeout_user;
837 case SG_SET_FORCE_LOW_DMA:
838 result = get_user(val, ip);
839 if (result)
840 return result;
841 if (val) {
842 sfp->low_dma = 1;
843 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
844 val = (int) sfp->reserve.bufflen;
845 sg_remove_scat(&sfp->reserve);
846 sg_build_reserve(sfp, val);
847 }
848 } else {
849 if (sdp->detached)
850 return -ENODEV;
851 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
852 }
853 return 0;
854 case SG_GET_LOW_DMA:
855 return put_user((int) sfp->low_dma, ip);
856 case SG_GET_SCSI_ID:
857 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
858 return -EFAULT;
859 else {
860 sg_scsi_id_t __user *sg_idp = p;
861
862 if (sdp->detached)
863 return -ENODEV;
864 __put_user((int) sdp->device->host->host_no,
865 &sg_idp->host_no);
866 __put_user((int) sdp->device->channel,
867 &sg_idp->channel);
868 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
869 __put_user((int) sdp->device->lun, &sg_idp->lun);
870 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
871 __put_user((short) sdp->device->host->cmd_per_lun,
872 &sg_idp->h_cmd_per_lun);
873 __put_user((short) sdp->device->queue_depth,
874 &sg_idp->d_queue_depth);
875 __put_user(0, &sg_idp->unused[0]);
876 __put_user(0, &sg_idp->unused[1]);
877 return 0;
878 }
879 case SG_SET_FORCE_PACK_ID:
880 result = get_user(val, ip);
881 if (result)
882 return result;
883 sfp->force_packid = val ? 1 : 0;
884 return 0;
885 case SG_GET_PACK_ID:
886 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
887 return -EFAULT;
888 read_lock_irqsave(&sfp->rq_list_lock, iflags);
889 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
890 if ((1 == srp->done) && (!srp->sg_io_owned)) {
891 read_unlock_irqrestore(&sfp->rq_list_lock,
892 iflags);
893 __put_user(srp->header.pack_id, ip);
894 return 0;
895 }
896 }
897 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
898 __put_user(-1, ip);
899 return 0;
900 case SG_GET_NUM_WAITING:
901 read_lock_irqsave(&sfp->rq_list_lock, iflags);
902 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
903 if ((1 == srp->done) && (!srp->sg_io_owned))
904 ++val;
905 }
906 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
907 return put_user(val, ip);
908 case SG_GET_SG_TABLESIZE:
909 return put_user(sdp->sg_tablesize, ip);
910 case SG_SET_RESERVED_SIZE:
911 result = get_user(val, ip);
912 if (result)
913 return result;
914 if (val < 0)
915 return -EINVAL;
Alan Stern44ec9542007-02-20 11:01:57 -0500916 val = min_t(int, val,
917 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (val != sfp->reserve.bufflen) {
919 if (sg_res_in_use(sfp) || sfp->mmap_called)
920 return -EBUSY;
921 sg_remove_scat(&sfp->reserve);
922 sg_build_reserve(sfp, val);
923 }
924 return 0;
925 case SG_GET_RESERVED_SIZE:
Alan Stern44ec9542007-02-20 11:01:57 -0500926 val = min_t(int, sfp->reserve.bufflen,
927 sdp->device->request_queue->max_sectors * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return put_user(val, ip);
929 case SG_SET_COMMAND_Q:
930 result = get_user(val, ip);
931 if (result)
932 return result;
933 sfp->cmd_q = val ? 1 : 0;
934 return 0;
935 case SG_GET_COMMAND_Q:
936 return put_user((int) sfp->cmd_q, ip);
937 case SG_SET_KEEP_ORPHAN:
938 result = get_user(val, ip);
939 if (result)
940 return result;
941 sfp->keep_orphan = val;
942 return 0;
943 case SG_GET_KEEP_ORPHAN:
944 return put_user((int) sfp->keep_orphan, ip);
945 case SG_NEXT_CMD_LEN:
946 result = get_user(val, ip);
947 if (result)
948 return result;
949 sfp->next_cmd_len = (val > 0) ? val : 0;
950 return 0;
951 case SG_GET_VERSION_NUM:
952 return put_user(sg_version_num, ip);
953 case SG_GET_ACCESS_COUNT:
954 /* faked - we don't have a real access count anymore */
955 val = (sdp->device ? 1 : 0);
956 return put_user(val, ip);
957 case SG_GET_REQUEST_TABLE:
958 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
959 return -EFAULT;
960 else {
cb59e842005-04-02 13:51:23 -0600961 sg_req_info_t *rinfo;
962 unsigned int ms;
963
964 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
965 GFP_KERNEL);
966 if (!rinfo)
967 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 read_lock_irqsave(&sfp->rq_list_lock, iflags);
969 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
970 ++val, srp = srp ? srp->nextrp : srp) {
971 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
972 if (srp) {
973 rinfo[val].req_state = srp->done + 1;
974 rinfo[val].problem =
975 srp->header.masked_status &
976 srp->header.host_status &
977 srp->header.driver_status;
cb59e842005-04-02 13:51:23 -0600978 if (srp->done)
979 rinfo[val].duration =
980 srp->header.duration;
981 else {
982 ms = jiffies_to_msecs(jiffies);
983 rinfo[val].duration =
984 (ms > srp->header.duration) ?
985 (ms - srp->header.duration) : 0;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 rinfo[val].orphan = srp->orphan;
cb59e842005-04-02 13:51:23 -0600988 rinfo[val].sg_io_owned =
989 srp->sg_io_owned;
990 rinfo[val].pack_id =
991 srp->header.pack_id;
992 rinfo[val].usr_ptr =
993 srp->header.usr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 }
996 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
cb59e842005-04-02 13:51:23 -0600997 result = __copy_to_user(p, rinfo,
998 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
999 result = result ? -EFAULT : 0;
1000 kfree(rinfo);
1001 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 case SG_EMULATED_HOST:
1004 if (sdp->detached)
1005 return -ENODEV;
1006 return put_user(sdp->device->host->hostt->emulated, ip);
1007 case SG_SCSI_RESET:
1008 if (sdp->detached)
1009 return -ENODEV;
1010 if (filp->f_flags & O_NONBLOCK) {
James Bottomley939647e2005-09-18 15:05:20 -05001011 if (scsi_host_in_recovery(sdp->device->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -EBUSY;
1013 } else if (!scsi_block_when_processing_errors(sdp->device))
1014 return -EBUSY;
1015 result = get_user(val, ip);
1016 if (result)
1017 return result;
1018 if (SG_SCSI_RESET_NOTHING == val)
1019 return 0;
1020 switch (val) {
1021 case SG_SCSI_RESET_DEVICE:
1022 val = SCSI_TRY_RESET_DEVICE;
1023 break;
1024 case SG_SCSI_RESET_BUS:
1025 val = SCSI_TRY_RESET_BUS;
1026 break;
1027 case SG_SCSI_RESET_HOST:
1028 val = SCSI_TRY_RESET_HOST;
1029 break;
1030 default:
1031 return -EINVAL;
1032 }
1033 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1034 return -EACCES;
1035 return (scsi_reset_provider(sdp->device, val) ==
1036 SUCCESS) ? 0 : -EIO;
1037 case SCSI_IOCTL_SEND_COMMAND:
1038 if (sdp->detached)
1039 return -ENODEV;
1040 if (read_only) {
1041 unsigned char opcode = WRITE_6;
1042 Scsi_Ioctl_Command __user *siocp = p;
1043
1044 if (copy_from_user(&opcode, siocp->data, 1))
1045 return -EFAULT;
1046 if (!sg_allow_access(opcode, sdp->device->type))
1047 return -EPERM;
1048 }
Christoph Hellwig21b2f0c2006-03-22 17:52:04 +01001049 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 case SG_SET_DEBUG:
1051 result = get_user(val, ip);
1052 if (result)
1053 return result;
1054 sdp->sgdebug = (char) val;
1055 return 0;
1056 case SCSI_IOCTL_GET_IDLUN:
1057 case SCSI_IOCTL_GET_BUS_NUMBER:
1058 case SCSI_IOCTL_PROBE_HOST:
1059 case SG_GET_TRANSFORM:
1060 if (sdp->detached)
1061 return -ENODEV;
1062 return scsi_ioctl(sdp->device, cmd_in, p);
Alan Stern44ec9542007-02-20 11:01:57 -05001063 case BLKSECTGET:
1064 return put_user(sdp->device->request_queue->max_sectors * 512,
1065 ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 default:
1067 if (read_only)
1068 return -EPERM; /* don't know so take safe approach */
1069 return scsi_ioctl(sdp->device, cmd_in, p);
1070 }
1071}
1072
1073#ifdef CONFIG_COMPAT
1074static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1075{
1076 Sg_device *sdp;
1077 Sg_fd *sfp;
1078 struct scsi_device *sdev;
1079
1080 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1081 return -ENXIO;
1082
1083 sdev = sdp->device;
1084 if (sdev->host->hostt->compat_ioctl) {
1085 int ret;
1086
1087 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1088
1089 return ret;
1090 }
1091
1092 return -ENOIOCTLCMD;
1093}
1094#endif
1095
1096static unsigned int
1097sg_poll(struct file *filp, poll_table * wait)
1098{
1099 unsigned int res = 0;
1100 Sg_device *sdp;
1101 Sg_fd *sfp;
1102 Sg_request *srp;
1103 int count = 0;
1104 unsigned long iflags;
1105
1106 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1107 || sfp->closed)
1108 return POLLERR;
1109 poll_wait(filp, &sfp->read_wait, wait);
1110 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1111 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1112 /* if any read waiting, flag it */
1113 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1114 res = POLLIN | POLLRDNORM;
1115 ++count;
1116 }
1117 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1118
1119 if (sdp->detached)
1120 res |= POLLHUP;
1121 else if (!sfp->cmd_q) {
1122 if (0 == count)
1123 res |= POLLOUT | POLLWRNORM;
1124 } else if (count < SG_MAX_QUEUE)
1125 res |= POLLOUT | POLLWRNORM;
1126 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1127 sdp->disk->disk_name, (int) res));
1128 return res;
1129}
1130
1131static int
1132sg_fasync(int fd, struct file *filp, int mode)
1133{
1134 int retval;
1135 Sg_device *sdp;
1136 Sg_fd *sfp;
1137
1138 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1139 return -ENXIO;
1140 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1141 sdp->disk->disk_name, mode));
1142
1143 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1144 return (retval < 0) ? retval : 0;
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static struct page *
1148sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1149{
1150 Sg_fd *sfp;
1151 struct page *page = NOPAGE_SIGBUS;
Mike Christied6b10342005-11-08 04:06:41 -06001152 unsigned long offset, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001154 struct scatterlist *sg;
1155 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1158 return page;
1159 rsv_schp = &sfp->reserve;
1160 offset = addr - vma->vm_start;
1161 if (offset >= rsv_schp->bufflen)
1162 return page;
1163 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1164 offset, rsv_schp->k_use_sg));
Mike Christied6b10342005-11-08 04:06:41 -06001165 sg = rsv_schp->buffer;
1166 sa = vma->vm_start;
1167 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001168 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001169 len = vma->vm_end - sa;
1170 len = (len < sg->length) ? len : sg->length;
1171 if (offset < len) {
Jens Axboe45711f12007-10-22 21:19:53 +02001172 page = virt_to_page(page_address(sg_page(sg)) + offset);
Mike Christied6b10342005-11-08 04:06:41 -06001173 get_page(page); /* increment page count */
1174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Mike Christied6b10342005-11-08 04:06:41 -06001176 sa += len;
1177 offset -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Mike Christied6b10342005-11-08 04:06:41 -06001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (type)
1181 *type = VM_FAULT_MINOR;
1182 return page;
1183}
1184
1185static struct vm_operations_struct sg_mmap_vm_ops = {
1186 .nopage = sg_vma_nopage,
1187};
1188
1189static int
1190sg_mmap(struct file *filp, struct vm_area_struct *vma)
1191{
1192 Sg_fd *sfp;
Mike Christied6b10342005-11-08 04:06:41 -06001193 unsigned long req_sz, len, sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 Sg_scatter_hold *rsv_schp;
Mike Christied6b10342005-11-08 04:06:41 -06001195 int k;
1196 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1199 return -ENXIO;
cb59e842005-04-02 13:51:23 -06001200 req_sz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1202 (void *) vma->vm_start, (int) req_sz));
1203 if (vma->vm_pgoff)
1204 return -EINVAL; /* want no offset */
1205 rsv_schp = &sfp->reserve;
1206 if (req_sz > rsv_schp->bufflen)
1207 return -ENOMEM; /* cannot map more than reserved buffer */
1208
Mike Christied6b10342005-11-08 04:06:41 -06001209 sa = vma->vm_start;
1210 sg = rsv_schp->buffer;
1211 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001212 ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001213 len = vma->vm_end - sa;
1214 len = (len < sg->length) ? len : sg->length;
1215 sa += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Mike Christied6b10342005-11-08 04:06:41 -06001217
Nick Pigginf9aed0e2006-03-22 00:08:30 -08001218 sfp->mmap_called = 1;
Douglas Gilbert1c8e71d2005-09-09 17:18:57 +10001219 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 vma->vm_private_data = sfp;
1221 vma->vm_ops = &sg_mmap_vm_ops;
1222 return 0;
1223}
1224
1225/* This function is a "bottom half" handler that is called by the
1226 * mid level when a command is completed (or has failed). */
1227static void
Mike Christied6b10342005-11-08 04:06:41 -06001228sg_cmd_done(void *data, char *sense, int result, int resid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Mike Christied6b10342005-11-08 04:06:41 -06001230 Sg_request *srp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 Sg_device *sdp = NULL;
1232 Sg_fd *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned long iflags;
cb59e842005-04-02 13:51:23 -06001234 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (NULL == srp) {
1237 printk(KERN_ERR "sg_cmd_done: NULL request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return;
1239 }
1240 sfp = srp->parentfp;
1241 if (sfp)
1242 sdp = sfp->parentdp;
1243 if ((NULL == sdp) || sdp->detached) {
1244 printk(KERN_INFO "sg_cmd_done: device detached\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
1246 }
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
Mike Christied6b10342005-11-08 04:06:41 -06001250 sdp->disk->disk_name, srp->header.pack_id, result));
1251 srp->header.resid = resid;
cb59e842005-04-02 13:51:23 -06001252 ms = jiffies_to_msecs(jiffies);
1253 srp->header.duration = (ms > srp->header.duration) ?
1254 (ms - srp->header.duration) : 0;
Mike Christied6b10342005-11-08 04:06:41 -06001255 if (0 != result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct scsi_sense_hdr sshdr;
1257
Mike Christied6b10342005-11-08 04:06:41 -06001258 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1259 srp->header.status = 0xff & result;
1260 srp->header.masked_status = status_byte(result);
1261 srp->header.msg_status = msg_byte(result);
1262 srp->header.host_status = host_byte(result);
1263 srp->header.driver_status = driver_byte(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if ((sdp->sgdebug > 0) &&
1265 ((CHECK_CONDITION == srp->header.masked_status) ||
1266 (COMMAND_TERMINATED == srp->header.masked_status)))
Mike Christied6b10342005-11-08 04:06:41 -06001267 __scsi_print_sense("sg_cmd_done", sense,
1268 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /* Following if statement is a patch supplied by Eric Youngdale */
Mike Christied6b10342005-11-08 04:06:41 -06001271 if (driver_byte(result) != 0
1272 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 && !scsi_sense_is_deferred(&sshdr)
1274 && sshdr.sense_key == UNIT_ATTENTION
1275 && sdp->device->removable) {
1276 /* Detected possible disc change. Set the bit - this */
1277 /* may be used if there are filesystems using this device */
1278 sdp->device->changed = 1;
1279 }
1280 }
1281 /* Rely on write phase to clean out srp status values, so no "else" */
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (sfp->closed) { /* whoops this fd already released, cleanup */
1284 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1285 sg_finish_rem_req(srp);
1286 srp = NULL;
1287 if (NULL == sfp->headrp) {
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001288 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1290 scsi_device_put(sdp->device);
1291 }
1292 sfp = NULL;
1293 }
1294 } else if (srp && srp->orphan) {
1295 if (sfp->keep_orphan)
1296 srp->sg_io_owned = 0;
1297 else {
1298 sg_finish_rem_req(srp);
1299 srp = NULL;
1300 }
1301 }
1302 if (sfp && srp) {
1303 /* Now wake up any sg_read() that is waiting for this packet. */
1304 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1305 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1306 srp->done = 1;
1307 wake_up_interruptible(&sfp->read_wait);
1308 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1309 }
1310}
1311
1312static struct file_operations sg_fops = {
1313 .owner = THIS_MODULE,
1314 .read = sg_read,
1315 .write = sg_write,
1316 .poll = sg_poll,
1317 .ioctl = sg_ioctl,
1318#ifdef CONFIG_COMPAT
1319 .compat_ioctl = sg_compat_ioctl,
1320#endif
1321 .open = sg_open,
1322 .mmap = sg_mmap,
1323 .release = sg_release,
1324 .fasync = sg_fasync,
1325};
1326
gregkh@suse.ded2538782005-03-23 09:55:22 -08001327static struct class *sg_sysfs_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329static int sg_sysfs_valid = 0;
1330
James Bottomley7c07d612007-08-05 13:36:11 -05001331static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Mike Christied6b10342005-11-08 04:06:41 -06001333 struct request_queue *q = scsidp->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 Sg_device *sdp;
1335 unsigned long iflags;
James Bottomley7c07d612007-08-05 13:36:11 -05001336 int error;
1337 u32 k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Jes Sorensen24669f752006-01-16 10:31:18 -05001339 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!sdp) {
1341 printk(KERN_WARNING "kmalloc Sg_device failure\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001342 return ERR_PTR(-ENOMEM);
1343 }
1344 error = -ENOMEM;
1345 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1346 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
James Bottomley7c07d612007-08-05 13:36:11 -05001350 write_lock_irqsave(&sg_index_lock, iflags);
1351 error = idr_get_new(&sg_index_idr, sdp, &k);
1352 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
James Bottomley7c07d612007-08-05 13:36:11 -05001354 if (error) {
1355 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1356 error);
1357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (unlikely(k >= SG_MAX_DEVS))
1361 goto overflow;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1364 sprintf(disk->disk_name, "sg%d", k);
1365 disk->first_minor = k;
1366 sdp->disk = disk;
1367 sdp->device = scsidp;
1368 init_waitqueue_head(&sdp->o_excl_wait);
Mike Christied6b10342005-11-08 04:06:41 -06001369 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
James Bottomley7c07d612007-08-05 13:36:11 -05001370 sdp->index = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
James Bottomley7c07d612007-08-05 13:36:11 -05001372 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 out:
James Bottomley7c07d612007-08-05 13:36:11 -05001374 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 kfree(sdp);
James Bottomley7c07d612007-08-05 13:36:11 -05001376 return ERR_PTR(error);
1377 }
1378 return sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 overflow:
James Bottomley9ccfc752005-10-02 11:45:08 -05001381 sdev_printk(KERN_WARNING, scsidp,
1382 "Unable to attach sg device type=%d, minor "
1383 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 error = -ENODEV;
1385 goto out;
1386}
1387
1388static int
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001389sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1392 struct gendisk *disk;
1393 Sg_device *sdp = NULL;
1394 struct cdev * cdev = NULL;
James Bottomley7c07d612007-08-05 13:36:11 -05001395 int error;
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001396 unsigned long iflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 disk = alloc_disk(1);
1399 if (!disk) {
1400 printk(KERN_WARNING "alloc_disk failed\n");
1401 return -ENOMEM;
1402 }
1403 disk->major = SCSI_GENERIC_MAJOR;
1404
1405 error = -ENOMEM;
1406 cdev = cdev_alloc();
1407 if (!cdev) {
1408 printk(KERN_WARNING "cdev_alloc failed\n");
1409 goto out;
1410 }
1411 cdev->owner = THIS_MODULE;
1412 cdev->ops = &sg_fops;
1413
James Bottomley7c07d612007-08-05 13:36:11 -05001414 sdp = sg_alloc(disk, scsidp);
1415 if (IS_ERR(sdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 printk(KERN_WARNING "sg_alloc failed\n");
James Bottomley7c07d612007-08-05 13:36:11 -05001417 error = PTR_ERR(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto out;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
James Bottomley7c07d612007-08-05 13:36:11 -05001421 class_set_devdata(cl_dev, sdp);
1422 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
Greg KH5e3c34c2006-01-18 16:17:46 -08001423 if (error)
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001424 goto cdev_add_err;
Greg KH5e3c34c2006-01-18 16:17:46 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 sdp->cdev = cdev;
1427 if (sg_sysfs_valid) {
1428 struct class_device * sg_class_member;
1429
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07001430 sg_class_member = class_device_create(sg_sysfs_class, NULL,
James Bottomley7c07d612007-08-05 13:36:11 -05001431 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
1432 cl_dev->dev, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 disk->disk_name);
1434 if (IS_ERR(sg_class_member))
1435 printk(KERN_WARNING "sg_add: "
gregkh@suse.ded2538782005-03-23 09:55:22 -08001436 "class_device_create failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 class_set_devdata(sg_class_member, sdp);
1438 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1439 &sg_class_member->kobj, "generic");
1440 if (error)
1441 printk(KERN_ERR "sg_add: unable to make symlink "
James Bottomley7c07d612007-08-05 13:36:11 -05001442 "'generic' back to sg%d\n", sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 } else
James Bottomley7c07d612007-08-05 13:36:11 -05001444 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
James Bottomley9ccfc752005-10-02 11:45:08 -05001446 sdev_printk(KERN_NOTICE, scsidp,
James Bottomley7c07d612007-08-05 13:36:11 -05001447 "Attached scsi generic sg%d type %d\n", sdp->index,
1448 scsidp->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 return 0;
1451
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001452cdev_add_err:
James Bottomley7c07d612007-08-05 13:36:11 -05001453 write_lock_irqsave(&sg_index_lock, iflags);
1454 idr_remove(&sg_index_idr, sdp->index);
1455 write_unlock_irqrestore(&sg_index_lock, iflags);
1456 kfree(sdp);
Ishai Rabinovitz454e8952006-06-29 16:39:54 +03001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458out:
1459 put_disk(disk);
1460 if (cdev)
1461 cdev_del(cdev);
1462 return error;
1463}
1464
1465static void
Dmitry Torokhovd8539d82005-09-15 02:01:36 -05001466sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
James Bottomley7c07d612007-08-05 13:36:11 -05001469 Sg_device *sdp = class_get_devdata(cl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 unsigned long iflags;
1471 Sg_fd *sfp;
1472 Sg_fd *tsfp;
1473 Sg_request *srp;
1474 Sg_request *tsrp;
James Bottomley7c07d612007-08-05 13:36:11 -05001475 int delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
James Bottomley7c07d612007-08-05 13:36:11 -05001477 if (!sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
James Bottomley7c07d612007-08-05 13:36:11 -05001480 delay = 0;
1481 write_lock_irqsave(&sg_index_lock, iflags);
1482 if (sdp->headfp) {
1483 sdp->detached = 1;
1484 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1485 tsfp = sfp->nextfp;
1486 for (srp = sfp->headrp; srp; srp = tsrp) {
1487 tsrp = srp->nextrp;
1488 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1489 sg_finish_rem_req(srp);
1490 }
1491 if (sfp->closed) {
1492 scsi_device_put(sdp->device);
1493 __sg_remove_sfp(sdp, sfp);
1494 } else {
1495 delay = 1;
1496 wake_up_interruptible(&sfp->read_wait);
1497 kill_fasync(&sfp->async_qp, SIGPOLL,
1498 POLL_HUP);
1499 }
1500 }
1501 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1502 if (NULL == sdp->headfp) {
1503 idr_remove(&sg_index_idr, sdp->index);
1504 }
1505 } else { /* nothing active, simple case */
1506 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1507 idr_remove(&sg_index_idr, sdp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
James Bottomley7c07d612007-08-05 13:36:11 -05001509 write_unlock_irqrestore(&sg_index_lock, iflags);
1510
1511 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1512 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1513 cdev_del(sdp->cdev);
1514 sdp->cdev = NULL;
1515 put_disk(sdp->disk);
1516 sdp->disk = NULL;
1517 if (NULL == sdp->headfp)
1518 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (delay)
1521 msleep(10); /* dirty detach so delay device destruction */
1522}
1523
Douglas Gilbert6460e752006-09-20 18:20:49 -04001524module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1525module_param_named(def_reserved_size, def_reserved_size, int,
1526 S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1528
1529MODULE_AUTHOR("Douglas Gilbert");
1530MODULE_DESCRIPTION("SCSI generic (sg) driver");
1531MODULE_LICENSE("GPL");
1532MODULE_VERSION(SG_VERSION_STR);
Rene Hermanf018fa52006-03-08 00:14:20 -08001533MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Douglas Gilbert6460e752006-09-20 18:20:49 -04001535MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1536 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1538MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1539
1540static int __init
1541init_sg(void)
1542{
1543 int rc;
1544
Douglas Gilbert6460e752006-09-20 18:20:49 -04001545 if (scatter_elem_sz < PAGE_SIZE) {
1546 scatter_elem_sz = PAGE_SIZE;
1547 scatter_elem_sz_prev = scatter_elem_sz;
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (def_reserved_size >= 0)
1550 sg_big_buff = def_reserved_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04001551 else
1552 def_reserved_size = sg_big_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1555 SG_MAX_DEVS, "sg");
1556 if (rc)
1557 return rc;
gregkh@suse.ded2538782005-03-23 09:55:22 -08001558 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if ( IS_ERR(sg_sysfs_class) ) {
1560 rc = PTR_ERR(sg_sysfs_class);
1561 goto err_out;
1562 }
1563 sg_sysfs_valid = 1;
1564 rc = scsi_register_interface(&sg_interface);
1565 if (0 == rc) {
1566#ifdef CONFIG_SCSI_PROC_FS
1567 sg_proc_init();
1568#endif /* CONFIG_SCSI_PROC_FS */
1569 return 0;
1570 }
gregkh@suse.ded2538782005-03-23 09:55:22 -08001571 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572err_out:
1573 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1574 return rc;
1575}
1576
1577static void __exit
1578exit_sg(void)
1579{
1580#ifdef CONFIG_SCSI_PROC_FS
1581 sg_proc_cleanup();
1582#endif /* CONFIG_SCSI_PROC_FS */
1583 scsi_unregister_interface(&sg_interface);
gregkh@suse.ded2538782005-03-23 09:55:22 -08001584 class_destroy(sg_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 sg_sysfs_valid = 0;
1586 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1587 SG_MAX_DEVS);
James Bottomley7c07d612007-08-05 13:36:11 -05001588 idr_destroy(&sg_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591static int
1592sg_start_req(Sg_request * srp)
1593{
1594 int res;
1595 Sg_fd *sfp = srp->parentfp;
1596 sg_io_hdr_t *hp = &srp->header;
1597 int dxfer_len = (int) hp->dxfer_len;
1598 int dxfer_dir = hp->dxfer_direction;
1599 Sg_scatter_hold *req_schp = &srp->data;
1600 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1601
1602 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1603 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1604 return 0;
1605 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1606 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1607 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1608 res = sg_build_direct(srp, sfp, dxfer_len);
1609 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1610 return res;
1611 }
1612 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1613 sg_link_reserve(sfp, srp, dxfer_len);
1614 else {
1615 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1616 if (res) {
1617 sg_remove_scat(req_schp);
1618 return res;
1619 }
1620 }
1621 return 0;
1622}
1623
1624static void
1625sg_finish_rem_req(Sg_request * srp)
1626{
1627 Sg_fd *sfp = srp->parentfp;
1628 Sg_scatter_hold *req_schp = &srp->data;
1629
1630 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1631 if (srp->res_used)
1632 sg_unlink_reserve(sfp, srp);
1633 else
1634 sg_remove_scat(req_schp);
1635 sg_remove_request(sfp, srp);
1636}
1637
1638static int
1639sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1640{
Mike Christied6b10342005-11-08 04:06:41 -06001641 int sg_bufflen = tablesize * sizeof(struct scatterlist);
Al Viro2d20eaf2006-02-01 06:31:40 -05001642 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Mike Christied6b10342005-11-08 04:06:41 -06001644 /*
1645 * TODO: test without low_dma, we should not need it since
1646 * the block layer will bounce the buffer for us
1647 *
1648 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1649 */
1650 if (sfp->low_dma)
1651 gfp_flags |= GFP_DMA;
1652 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (!schp->buffer)
1654 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 schp->sglist_len = sg_bufflen;
Mike Christied6b10342005-11-08 04:06:41 -06001656 return tablesize; /* number of scat_gath elements allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659#ifdef SG_ALLOW_DIO_CODE
1660/* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
Mike Christied6b10342005-11-08 04:06:41 -06001661 /* TODO: hopefully we can use the generic block layer code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663/* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1664 - mapping of all pages not successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 (i.e., either completely successful or fails)
1666*/
1667static int
1668st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
Mike Christied6b10342005-11-08 04:06:41 -06001669 unsigned long uaddr, size_t count, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Douglas Gilbertdeb92b72005-09-01 21:50:02 +10001671 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1672 unsigned long start = uaddr >> PAGE_SHIFT;
1673 const int nr_pages = end - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int res, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct page **pages;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* User attempted Overflow! */
1678 if ((uaddr + count) < uaddr)
1679 return -EINVAL;
1680
1681 /* Too big */
1682 if (nr_pages > max_pages)
1683 return -ENOMEM;
1684
1685 /* Hmm? */
1686 if (count == 0)
1687 return 0;
1688
1689 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1690 return -ENOMEM;
1691
1692 /* Try to fault in all of the necessary pages */
1693 down_read(&current->mm->mmap_sem);
1694 /* rw==READ means read from drive, write into memory area */
1695 res = get_user_pages(
1696 current,
1697 current->mm,
1698 uaddr,
1699 nr_pages,
1700 rw == READ,
1701 0, /* don't force */
1702 pages,
1703 NULL);
1704 up_read(&current->mm->mmap_sem);
1705
1706 /* Errors and no page mapped should return here */
1707 if (res < nr_pages)
1708 goto out_unmap;
1709
1710 for (i=0; i < nr_pages; i++) {
1711 /* FIXME: flush superflous for rw==READ,
1712 * probably wrong function for rw==WRITE
1713 */
1714 flush_dcache_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* ?? Is locking needed? I don't think so */
1716 /* if (TestSetPageLocked(pages[i]))
1717 goto out_unlock; */
1718 }
1719
Jens Axboe642f149032007-10-24 11:20:47 +02001720 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (nr_pages > 1) {
1722 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1723 count -= sgl[0].length;
Jens Axboe642f149032007-10-24 11:20:47 +02001724 for (i=1; i < nr_pages ; i++)
1725 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727 else {
1728 sgl[0].length = count;
1729 }
1730
1731 kfree(pages);
1732 return nr_pages;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 out_unmap:
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001735 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 for (j=0; j < res; j++)
1737 page_cache_release(pages[j]);
Hugh Dickins4d5cda02005-12-02 15:58:09 +00001738 res = 0;
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 kfree(pages);
1741 return res;
1742}
1743
1744
1745/* And unmap them... */
1746static int
1747st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1748 int dirtied)
1749{
1750 int i;
1751
1752 for (i=0; i < nr_pages; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +02001753 struct page *page = sg_page(&sgl[i]);
Nick Pigginb5810032005-10-29 18:16:12 -07001754
Nick Pigginb5810032005-10-29 18:16:12 -07001755 if (dirtied)
1756 SetPageDirty(page);
1757 /* unlock_page(page); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* FIXME: cache flush missing for rw==READ
1759 * FIXME: call the correct reference counting function
1760 */
Nick Pigginb5810032005-10-29 18:16:12 -07001761 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763
1764 return 0;
1765}
1766
1767/* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1768#endif
1769
1770
1771/* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1772static int
1773sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1774{
1775#ifdef SG_ALLOW_DIO_CODE
1776 sg_io_hdr_t *hp = &srp->header;
1777 Sg_scatter_hold *schp = &srp->data;
1778 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int mx_sc_elems, res;
1780 struct scsi_device *sdev = sfp->parentdp->device;
1781
1782 if (((unsigned long)hp->dxferp &
1783 queue_dma_alignment(sdev->request_queue)) != 0)
1784 return 1;
Mike Christied6b10342005-11-08 04:06:41 -06001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1787 if (mx_sc_elems <= 0) {
1788 return 1;
1789 }
Mike Christied6b10342005-11-08 04:06:41 -06001790 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1791 (unsigned long)hp->dxferp, dxfer_len,
1792 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001793 if (res <= 0) {
1794 sg_remove_scat(schp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return 1;
Douglas Gilbertc06bb7f2006-03-28 14:48:22 -05001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 schp->k_use_sg = res;
1798 schp->dio_in_use = 1;
1799 hp->info |= SG_INFO_DIRECT_IO;
1800 return 0;
1801#else
1802 return 1;
1803#endif
1804}
1805
1806static int
1807sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1808{
Mike Christied6b10342005-11-08 04:06:41 -06001809 struct scatterlist *sg;
1810 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1811 int sg_tablesize = sfp->parentdp->sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int blk_size = buff_size;
Mike Christied6b10342005-11-08 04:06:41 -06001813 struct page *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Eric Sesterhennfb119932007-05-23 14:41:36 -07001815 if (blk_size < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return -EFAULT;
1817 if (0 == blk_size)
1818 ++blk_size; /* don't know why */
1819/* round request up to next highest SG_SECTOR_SZ byte boundary */
1820 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1821 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1822 buff_size, blk_size));
Mike Christied6b10342005-11-08 04:06:41 -06001823
1824 /* N.B. ret_sz carried into this block ... */
1825 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1826 if (mx_sc_elems < 0)
1827 return mx_sc_elems; /* most likely -ENOMEM */
1828
Douglas Gilbert6460e752006-09-20 18:20:49 -04001829 num = scatter_elem_sz;
1830 if (unlikely(num != scatter_elem_sz_prev)) {
1831 if (num < PAGE_SIZE) {
1832 scatter_elem_sz = PAGE_SIZE;
1833 scatter_elem_sz_prev = PAGE_SIZE;
1834 } else
1835 scatter_elem_sz_prev = num;
1836 }
Mike Christied6b10342005-11-08 04:06:41 -06001837 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1838 (rem_sz > 0) && (k < mx_sc_elems);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001839 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06001840
Douglas Gilbert6460e752006-09-20 18:20:49 -04001841 num = (rem_sz > scatter_elem_sz_prev) ?
1842 scatter_elem_sz_prev : rem_sz;
Mike Christied6b10342005-11-08 04:06:41 -06001843 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!p)
1845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Douglas Gilbert6460e752006-09-20 18:20:49 -04001847 if (num == scatter_elem_sz_prev) {
1848 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1849 scatter_elem_sz = ret_sz;
1850 scatter_elem_sz_prev = ret_sz;
1851 }
1852 }
Jens Axboe642f149032007-10-24 11:20:47 +02001853 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001855 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1856 "ret_sz=%d\n", k, num, ret_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001857 } /* end of for loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Mike Christied6b10342005-11-08 04:06:41 -06001859 schp->k_use_sg = k;
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001860 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1861 "rem_sz=%d\n", k, rem_sz));
Mike Christied6b10342005-11-08 04:06:41 -06001862
1863 schp->bufflen = blk_size;
1864 if (rem_sz > 0) /* must have failed */
1865 return -ENOMEM;
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return 0;
1868}
1869
1870static int
1871sg_write_xfer(Sg_request * srp)
1872{
1873 sg_io_hdr_t *hp = &srp->header;
1874 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06001875 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int num_xfer = 0;
1877 int j, k, onum, usglen, ksglen, res;
1878 int iovec_count = (int) hp->iovec_count;
1879 int dxfer_dir = hp->dxfer_direction;
1880 unsigned char *p;
1881 unsigned char __user *up;
1882 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1883
1884 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1885 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1886 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1887 if (schp->bufflen < num_xfer)
1888 num_xfer = schp->bufflen;
1889 }
1890 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1891 (new_interface
1892 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1893 return 0;
1894
1895 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1896 num_xfer, iovec_count, schp->k_use_sg));
1897 if (iovec_count) {
1898 onum = iovec_count;
1899 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1900 return -EFAULT;
1901 } else
1902 onum = 1;
1903
Mike Christied6b10342005-11-08 04:06:41 -06001904 ksglen = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001905 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06001906 for (j = 0, k = 0; j < onum; ++j) {
1907 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1908 if (res)
1909 return res;
1910
Jens Axboeb0f655d2007-05-11 15:01:01 +02001911 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02001912 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06001913 if (usglen <= 0)
1914 break;
1915 if (ksglen > usglen) {
1916 if (usglen >= num_xfer) {
1917 if (__copy_from_user(p, up, num_xfer))
1918 return -EFAULT;
1919 return 0;
1920 }
1921 if (__copy_from_user(p, up, usglen))
1922 return -EFAULT;
1923 p += usglen;
1924 ksglen -= usglen;
1925 break;
1926 } else {
1927 if (ksglen >= num_xfer) {
1928 if (__copy_from_user(p, up, num_xfer))
1929 return -EFAULT;
1930 return 0;
1931 }
1932 if (__copy_from_user(p, up, ksglen))
1933 return -EFAULT;
1934 up += ksglen;
1935 usglen -= ksglen;
1936 }
1937 ++k;
1938 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return 0;
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Mike Christied6b10342005-11-08 04:06:41 -06001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 0;
1944}
1945
1946static int
1947sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1948 int wr_xf, int *countp, unsigned char __user **up)
1949{
1950 int num_xfer = (int) hp->dxfer_len;
1951 unsigned char __user *p = hp->dxferp;
1952 int count;
1953
1954 if (0 == sg_num) {
1955 if (wr_xf && ('\0' == hp->interface_id))
1956 count = (int) hp->flags; /* holds "old" input_size */
1957 else
1958 count = num_xfer;
1959 } else {
1960 sg_iovec_t iovec;
1961 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1962 return -EFAULT;
1963 p = iovec.iov_base;
1964 count = (int) iovec.iov_len;
1965 }
1966 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1967 return -EFAULT;
1968 if (up)
1969 *up = p;
1970 if (countp)
1971 *countp = count;
1972 return 0;
1973}
1974
1975static void
1976sg_remove_scat(Sg_scatter_hold * schp)
1977{
1978 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1979 if (schp->buffer && (schp->sglist_len > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06001980 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 if (schp->dio_in_use) {
1983#ifdef SG_ALLOW_DIO_CODE
Mike Christied6b10342005-11-08 04:06:41 -06001984 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985#endif
1986 } else {
1987 int k;
1988
Jens Axboe45711f12007-10-22 21:19:53 +02001989 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
Jens Axboeb0f655d2007-05-11 15:01:01 +02001990 ++k, sg = sg_next(sg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 SCSI_LOG_TIMEOUT(5, printk(
Douglas Gilbert7ca63cb2006-10-27 17:47:49 -04001992 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
Jens Axboe45711f12007-10-22 21:19:53 +02001993 k, sg_page(sg), sg->length));
1994 sg_page_free(sg_page(sg), sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 }
Mike Christied6b10342005-11-08 04:06:41 -06001997 kfree(schp->buffer);
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 memset(schp, 0, sizeof (*schp));
2000}
2001
2002static int
2003sg_read_xfer(Sg_request * srp)
2004{
2005 sg_io_hdr_t *hp = &srp->header;
2006 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002007 struct scatterlist *sg = schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int num_xfer = 0;
2009 int j, k, onum, usglen, ksglen, res;
2010 int iovec_count = (int) hp->iovec_count;
2011 int dxfer_dir = hp->dxfer_direction;
2012 unsigned char *p;
2013 unsigned char __user *up;
2014 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2015
2016 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2017 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2018 num_xfer = hp->dxfer_len;
2019 if (schp->bufflen < num_xfer)
2020 num_xfer = schp->bufflen;
2021 }
2022 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2023 (new_interface
2024 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2025 return 0;
2026
2027 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2028 num_xfer, iovec_count, schp->k_use_sg));
2029 if (iovec_count) {
2030 onum = iovec_count;
2031 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2032 return -EFAULT;
2033 } else
2034 onum = 1;
2035
Jens Axboe45711f12007-10-22 21:19:53 +02002036 p = page_address(sg_page(sg));
Mike Christied6b10342005-11-08 04:06:41 -06002037 ksglen = sg->length;
2038 for (j = 0, k = 0; j < onum; ++j) {
2039 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2040 if (res)
2041 return res;
2042
Jens Axboeb0f655d2007-05-11 15:01:01 +02002043 for (; p; sg = sg_next(sg), ksglen = sg->length,
Jens Axboe45711f12007-10-22 21:19:53 +02002044 p = page_address(sg_page(sg))) {
Mike Christied6b10342005-11-08 04:06:41 -06002045 if (usglen <= 0)
2046 break;
2047 if (ksglen > usglen) {
2048 if (usglen >= num_xfer) {
2049 if (__copy_to_user(up, p, num_xfer))
2050 return -EFAULT;
2051 return 0;
2052 }
2053 if (__copy_to_user(up, p, usglen))
2054 return -EFAULT;
2055 p += usglen;
2056 ksglen -= usglen;
2057 break;
2058 } else {
2059 if (ksglen >= num_xfer) {
2060 if (__copy_to_user(up, p, num_xfer))
2061 return -EFAULT;
2062 return 0;
2063 }
2064 if (__copy_to_user(up, p, ksglen))
2065 return -EFAULT;
2066 up += ksglen;
2067 usglen -= ksglen;
2068 }
2069 ++k;
2070 if (k >= schp->k_use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Mike Christied6b10342005-11-08 04:06:41 -06002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
2076}
2077
2078static int
2079sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2080{
2081 Sg_scatter_hold *schp = &srp->data;
Mike Christied6b10342005-11-08 04:06:41 -06002082 struct scatterlist *sg = schp->buffer;
2083 int k, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2086 num_read_xfer));
2087 if ((!outp) || (num_read_xfer <= 0))
2088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Jens Axboe45711f12007-10-22 21:19:53 +02002090 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002091 num = sg->length;
2092 if (num > num_read_xfer) {
Jens Axboe45711f12007-10-22 21:19:53 +02002093 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002094 num_read_xfer))
2095 return -EFAULT;
2096 break;
2097 } else {
Jens Axboe45711f12007-10-22 21:19:53 +02002098 if (__copy_to_user(outp, page_address(sg_page(sg)),
Mike Christied6b10342005-11-08 04:06:41 -06002099 num))
2100 return -EFAULT;
2101 num_read_xfer -= num;
2102 if (num_read_xfer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 break;
Mike Christied6b10342005-11-08 04:06:41 -06002104 outp += num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Mike Christied6b10342005-11-08 04:06:41 -06002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return 0;
2109}
2110
2111static void
2112sg_build_reserve(Sg_fd * sfp, int req_size)
2113{
2114 Sg_scatter_hold *schp = &sfp->reserve;
2115
2116 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2117 do {
2118 if (req_size < PAGE_SIZE)
2119 req_size = PAGE_SIZE;
2120 if (0 == sg_build_indirect(schp, sfp, req_size))
2121 return;
2122 else
2123 sg_remove_scat(schp);
2124 req_size >>= 1; /* divide by 2 */
2125 } while (req_size > (PAGE_SIZE / 2));
2126}
2127
2128static void
2129sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2130{
2131 Sg_scatter_hold *req_schp = &srp->data;
2132 Sg_scatter_hold *rsv_schp = &sfp->reserve;
Mike Christied6b10342005-11-08 04:06:41 -06002133 struct scatterlist *sg = rsv_schp->buffer;
2134 int k, num, rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 srp->res_used = 1;
2137 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
Brian Kingeca7be52006-02-14 12:42:24 -06002138 rem = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Jens Axboeb0f655d2007-05-11 15:01:01 +02002140 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
Mike Christied6b10342005-11-08 04:06:41 -06002141 num = sg->length;
2142 if (rem <= num) {
2143 sfp->save_scat_len = num;
2144 sg->length = rem;
2145 req_schp->k_use_sg = k + 1;
2146 req_schp->sglist_len = rsv_schp->sglist_len;
2147 req_schp->buffer = rsv_schp->buffer;
2148
2149 req_schp->bufflen = size;
2150 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2151 break;
2152 } else
2153 rem -= num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
Mike Christied6b10342005-11-08 04:06:41 -06002155
2156 if (k >= rsv_schp->k_use_sg)
2157 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
2160static void
2161sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2162{
2163 Sg_scatter_hold *req_schp = &srp->data;
2164 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2165
2166 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2167 (int) req_schp->k_use_sg));
2168 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
Mike Christied6b10342005-11-08 04:06:41 -06002169 struct scatterlist *sg = rsv_schp->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 if (sfp->save_scat_len > 0)
Mike Christied6b10342005-11-08 04:06:41 -06002172 (sg + (req_schp->k_use_sg - 1))->length =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 (unsigned) sfp->save_scat_len;
2174 else
2175 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2176 }
2177 req_schp->k_use_sg = 0;
2178 req_schp->bufflen = 0;
2179 req_schp->buffer = NULL;
2180 req_schp->sglist_len = 0;
2181 sfp->save_scat_len = 0;
2182 srp->res_used = 0;
2183}
2184
2185static Sg_request *
2186sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2187{
2188 Sg_request *resp;
2189 unsigned long iflags;
2190
2191 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2192 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2193 /* look for requests that are ready + not SG_IO owned */
2194 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2195 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2196 resp->done = 2; /* guard against other readers */
2197 break;
2198 }
2199 }
2200 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2201 return resp;
2202}
2203
2204#ifdef CONFIG_SCSI_PROC_FS
2205static Sg_request *
2206sg_get_nth_request(Sg_fd * sfp, int nth)
2207{
2208 Sg_request *resp;
2209 unsigned long iflags;
2210 int k;
2211
2212 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2213 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2214 ++k, resp = resp->nextrp) ;
2215 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2216 return resp;
2217}
2218#endif
2219
2220/* always adds to end of list */
2221static Sg_request *
2222sg_add_request(Sg_fd * sfp)
2223{
2224 int k;
2225 unsigned long iflags;
2226 Sg_request *resp;
2227 Sg_request *rp = sfp->req_arr;
2228
2229 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2230 resp = sfp->headrp;
2231 if (!resp) {
2232 memset(rp, 0, sizeof (Sg_request));
2233 rp->parentfp = sfp;
2234 resp = rp;
2235 sfp->headrp = resp;
2236 } else {
2237 if (0 == sfp->cmd_q)
2238 resp = NULL; /* command queuing disallowed */
2239 else {
2240 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2241 if (!rp->parentfp)
2242 break;
2243 }
2244 if (k < SG_MAX_QUEUE) {
2245 memset(rp, 0, sizeof (Sg_request));
2246 rp->parentfp = sfp;
2247 while (resp->nextrp)
2248 resp = resp->nextrp;
2249 resp->nextrp = rp;
2250 resp = rp;
2251 } else
2252 resp = NULL;
2253 }
2254 }
2255 if (resp) {
2256 resp->nextrp = NULL;
cb59e842005-04-02 13:51:23 -06002257 resp->header.duration = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2260 return resp;
2261}
2262
2263/* Return of 1 for found; 0 for not found */
2264static int
2265sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2266{
2267 Sg_request *prev_rp;
2268 Sg_request *rp;
2269 unsigned long iflags;
2270 int res = 0;
2271
2272 if ((!sfp) || (!srp) || (!sfp->headrp))
2273 return res;
2274 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2275 prev_rp = sfp->headrp;
2276 if (srp == prev_rp) {
2277 sfp->headrp = prev_rp->nextrp;
2278 prev_rp->parentfp = NULL;
2279 res = 1;
2280 } else {
2281 while ((rp = prev_rp->nextrp)) {
2282 if (srp == rp) {
2283 prev_rp->nextrp = rp->nextrp;
2284 rp->parentfp = NULL;
2285 res = 1;
2286 break;
2287 }
2288 prev_rp = rp;
2289 }
2290 }
2291 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2292 return res;
2293}
2294
2295#ifdef CONFIG_SCSI_PROC_FS
2296static Sg_fd *
2297sg_get_nth_sfp(Sg_device * sdp, int nth)
2298{
2299 Sg_fd *resp;
2300 unsigned long iflags;
2301 int k;
2302
James Bottomley7c07d612007-08-05 13:36:11 -05002303 read_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2305 ++k, resp = resp->nextfp) ;
James Bottomley7c07d612007-08-05 13:36:11 -05002306 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return resp;
2308}
2309#endif
2310
2311static Sg_fd *
2312sg_add_sfp(Sg_device * sdp, int dev)
2313{
2314 Sg_fd *sfp;
2315 unsigned long iflags;
Alan Stern44ec9542007-02-20 11:01:57 -05002316 int bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Mike Christied6b10342005-11-08 04:06:41 -06002318 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!sfp)
2320 return NULL;
Mike Christied6b10342005-11-08 04:06:41 -06002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 init_waitqueue_head(&sfp->read_wait);
2323 rwlock_init(&sfp->rq_list_lock);
2324
2325 sfp->timeout = SG_DEFAULT_TIMEOUT;
2326 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2327 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2328 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2329 sdp->device->host->unchecked_isa_dma : 1;
2330 sfp->cmd_q = SG_DEF_COMMAND_Q;
2331 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2332 sfp->parentdp = sdp;
James Bottomley7c07d612007-08-05 13:36:11 -05002333 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (!sdp->headfp)
2335 sdp->headfp = sfp;
2336 else { /* add to tail of existing list */
2337 Sg_fd *pfp = sdp->headfp;
2338 while (pfp->nextfp)
2339 pfp = pfp->nextfp;
2340 pfp->nextfp = sfp;
2341 }
James Bottomley7c07d612007-08-05 13:36:11 -05002342 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
Douglas Gilbert6460e752006-09-20 18:20:49 -04002344 if (unlikely(sg_big_buff != def_reserved_size))
2345 sg_big_buff = def_reserved_size;
2346
Alan Stern44ec9542007-02-20 11:01:57 -05002347 bufflen = min_t(int, sg_big_buff,
2348 sdp->device->request_queue->max_sectors * 512);
2349 sg_build_reserve(sfp, bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2351 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2352 return sfp;
2353}
2354
2355static void
2356__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2357{
2358 Sg_fd *fp;
2359 Sg_fd *prev_fp;
2360
2361 prev_fp = sdp->headfp;
2362 if (sfp == prev_fp)
2363 sdp->headfp = prev_fp->nextfp;
2364 else {
2365 while ((fp = prev_fp->nextfp)) {
2366 if (sfp == fp) {
2367 prev_fp->nextfp = fp->nextfp;
2368 break;
2369 }
2370 prev_fp = fp;
2371 }
2372 }
2373 if (sfp->reserve.bufflen > 0) {
2374 SCSI_LOG_TIMEOUT(6,
2375 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2376 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 sg_remove_scat(&sfp->reserve);
2378 }
2379 sfp->parentdp = NULL;
2380 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
Mike Christied6b10342005-11-08 04:06:41 -06002381 kfree(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
2384/* Returns 0 in normal case, 1 when detached and sdp object removed */
2385static int
2386sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2387{
2388 Sg_request *srp;
2389 Sg_request *tsrp;
2390 int dirty = 0;
2391 int res = 0;
2392
2393 for (srp = sfp->headrp; srp; srp = tsrp) {
2394 tsrp = srp->nextrp;
2395 if (sg_srp_done(srp, sfp))
2396 sg_finish_rem_req(srp);
2397 else
2398 ++dirty;
2399 }
2400 if (0 == dirty) {
2401 unsigned long iflags;
2402
James Bottomley7c07d612007-08-05 13:36:11 -05002403 write_lock_irqsave(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 __sg_remove_sfp(sdp, sfp);
2405 if (sdp->detached && (NULL == sdp->headfp)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002406 idr_remove(&sg_index_idr, sdp->index);
2407 kfree(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 res = 1;
2409 }
James Bottomley7c07d612007-08-05 13:36:11 -05002410 write_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 } else {
2412 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2413 /* only bump the access_count if we actually succeeded in
2414 * throwing another counter on the host module */
2415 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2416 sfp->closed = 1; /* flag dirty state on this fd */
2417 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2418 dirty));
2419 }
2420 return res;
2421}
2422
2423static int
2424sg_res_in_use(Sg_fd * sfp)
2425{
2426 const Sg_request *srp;
2427 unsigned long iflags;
2428
2429 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2430 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2431 if (srp->res_used)
2432 break;
2433 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2434 return srp ? 1 : 0;
2435}
2436
Douglas Gilbert6460e752006-09-20 18:20:49 -04002437/* The size fetched (value output via retSzp) set when non-NULL return */
Mike Christied6b10342005-11-08 04:06:41 -06002438static struct page *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2440{
Mike Christied6b10342005-11-08 04:06:41 -06002441 struct page *resp = NULL;
Al Viroc53033f2005-10-21 03:22:08 -04002442 gfp_t page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int order, a_size;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002444 int resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Douglas Gilbert6460e752006-09-20 18:20:49 -04002446 if ((rqSz <= 0) || (NULL == retSzp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return resp;
2448
2449 if (lowDma)
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002450 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 else
Nick Pigginf9aed0e2006-03-22 00:08:30 -08002452 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2455 order++, a_size <<= 1) ;
Douglas Gilbert6460e752006-09-20 18:20:49 -04002456 resSz = a_size; /* rounded up if necessary */
Mike Christied6b10342005-11-08 04:06:41 -06002457 resp = alloc_pages(page_mask, order);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002458 while ((!resp) && order) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 --order;
2460 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
Mike Christied6b10342005-11-08 04:06:41 -06002461 resp = alloc_pages(page_mask, order); /* try half */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 resSz = a_size;
2463 }
2464 if (resp) {
2465 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
Hugh Dickins41ed16f2006-01-09 20:46:49 +00002466 memset(page_address(resp), 0, resSz);
Douglas Gilbert6460e752006-09-20 18:20:49 -04002467 *retSzp = resSz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469 return resp;
2470}
2471
2472static void
Mike Christied6b10342005-11-08 04:06:41 -06002473sg_page_free(struct page *page, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 int order, a_size;
2476
Mike Christied6b10342005-11-08 04:06:41 -06002477 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return;
2479 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2480 order++, a_size <<= 1) ;
Mike Christied6b10342005-11-08 04:06:41 -06002481 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484#ifndef MAINTENANCE_IN_CMD
2485#define MAINTENANCE_IN_CMD 0xa3
2486#endif
2487
2488static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2489 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2490 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2491 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2492};
2493
2494static int
2495sg_allow_access(unsigned char opcode, char dev_type)
2496{
2497 int k;
2498
2499 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2500 return 1;
2501 for (k = 0; k < sizeof (allow_ops); ++k) {
2502 if (opcode == allow_ops[k])
2503 return 1;
2504 }
2505 return 0;
2506}
2507
2508#ifdef CONFIG_SCSI_PROC_FS
2509static int
James Bottomley7c07d612007-08-05 13:36:11 -05002510sg_idr_max_id(int id, void *p, void *data)
2511{
2512 int *k = data;
2513
2514 if (*k < id)
2515 *k = id;
2516
2517 return 0;
2518}
2519
2520static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521sg_last_dev(void)
2522{
James Bottomley7c07d612007-08-05 13:36:11 -05002523 int k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 unsigned long iflags;
2525
James Bottomley7c07d612007-08-05 13:36:11 -05002526 read_lock_irqsave(&sg_index_lock, iflags);
2527 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2528 read_unlock_irqrestore(&sg_index_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return k + 1; /* origin 1 */
2530}
2531#endif
2532
2533static Sg_device *
2534sg_get_dev(int dev)
2535{
James Bottomley7c07d612007-08-05 13:36:11 -05002536 Sg_device *sdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 unsigned long iflags;
2538
James Bottomley7c07d612007-08-05 13:36:11 -05002539 read_lock_irqsave(&sg_index_lock, iflags);
2540 sdp = idr_find(&sg_index_idr, dev);
2541 read_unlock_irqrestore(&sg_index_lock, iflags);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return sdp;
2544}
2545
2546#ifdef CONFIG_SCSI_PROC_FS
2547
2548static struct proc_dir_entry *sg_proc_sgp = NULL;
2549
2550static char sg_proc_sg_dirname[] = "scsi/sg";
2551
2552static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2553
2554static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2555static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2556 size_t count, loff_t *off);
2557static struct file_operations adio_fops = {
2558 /* .owner, .read and .llseek added in sg_proc_init() */
2559 .open = sg_proc_single_open_adio,
2560 .write = sg_proc_write_adio,
2561 .release = single_release,
2562};
2563
2564static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2565static ssize_t sg_proc_write_dressz(struct file *filp,
2566 const char __user *buffer, size_t count, loff_t *off);
2567static struct file_operations dressz_fops = {
2568 .open = sg_proc_single_open_dressz,
2569 .write = sg_proc_write_dressz,
2570 .release = single_release,
2571};
2572
2573static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2574static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2575static struct file_operations version_fops = {
2576 .open = sg_proc_single_open_version,
2577 .release = single_release,
2578};
2579
2580static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2581static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2582static struct file_operations devhdr_fops = {
2583 .open = sg_proc_single_open_devhdr,
2584 .release = single_release,
2585};
2586
2587static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2588static int sg_proc_open_dev(struct inode *inode, struct file *file);
2589static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2590static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2591static void dev_seq_stop(struct seq_file *s, void *v);
2592static struct file_operations dev_fops = {
2593 .open = sg_proc_open_dev,
2594 .release = seq_release,
2595};
2596static struct seq_operations dev_seq_ops = {
2597 .start = dev_seq_start,
2598 .next = dev_seq_next,
2599 .stop = dev_seq_stop,
2600 .show = sg_proc_seq_show_dev,
2601};
2602
2603static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2604static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2605static struct file_operations devstrs_fops = {
2606 .open = sg_proc_open_devstrs,
2607 .release = seq_release,
2608};
2609static struct seq_operations devstrs_seq_ops = {
2610 .start = dev_seq_start,
2611 .next = dev_seq_next,
2612 .stop = dev_seq_stop,
2613 .show = sg_proc_seq_show_devstrs,
2614};
2615
2616static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2617static int sg_proc_open_debug(struct inode *inode, struct file *file);
2618static struct file_operations debug_fops = {
2619 .open = sg_proc_open_debug,
2620 .release = seq_release,
2621};
2622static struct seq_operations debug_seq_ops = {
2623 .start = dev_seq_start,
2624 .next = dev_seq_next,
2625 .stop = dev_seq_stop,
2626 .show = sg_proc_seq_show_debug,
2627};
2628
2629
2630struct sg_proc_leaf {
2631 const char * name;
2632 struct file_operations * fops;
2633};
2634
2635static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2636 {"allow_dio", &adio_fops},
2637 {"debug", &debug_fops},
2638 {"def_reserved_size", &dressz_fops},
2639 {"device_hdr", &devhdr_fops},
2640 {"devices", &dev_fops},
2641 {"device_strs", &devstrs_fops},
2642 {"version", &version_fops}
2643};
2644
2645static int
2646sg_proc_init(void)
2647{
2648 int k, mask;
Tobias Klauser6391a112006-06-08 22:23:48 -07002649 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct proc_dir_entry *pdep;
2651 struct sg_proc_leaf * leaf;
2652
Al Viro66600222005-09-28 22:32:57 +01002653 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (!sg_proc_sgp)
2655 return 1;
2656 for (k = 0; k < num_leaves; ++k) {
2657 leaf = &sg_proc_leaf_arr[k];
2658 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2659 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2660 if (pdep) {
2661 leaf->fops->owner = THIS_MODULE,
2662 leaf->fops->read = seq_read,
2663 leaf->fops->llseek = seq_lseek,
2664 pdep->proc_fops = leaf->fops;
2665 }
2666 }
2667 return 0;
2668}
2669
2670static void
2671sg_proc_cleanup(void)
2672{
2673 int k;
Tobias Klauser6391a112006-06-08 22:23:48 -07002674 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 if (!sg_proc_sgp)
2677 return;
2678 for (k = 0; k < num_leaves; ++k)
2679 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2680 remove_proc_entry(sg_proc_sg_dirname, NULL);
2681}
2682
2683
2684static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2685{
2686 seq_printf(s, "%d\n", *((int *)s->private));
2687 return 0;
2688}
2689
2690static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2691{
2692 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2693}
2694
2695static ssize_t
2696sg_proc_write_adio(struct file *filp, const char __user *buffer,
2697 size_t count, loff_t *off)
2698{
2699 int num;
2700 char buff[11];
2701
2702 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2703 return -EACCES;
2704 num = (count < 10) ? count : 10;
2705 if (copy_from_user(buff, buffer, num))
2706 return -EFAULT;
2707 buff[num] = '\0';
2708 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2709 return count;
2710}
2711
2712static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2713{
2714 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2715}
2716
2717static ssize_t
2718sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2719 size_t count, loff_t *off)
2720{
2721 int num;
2722 unsigned long k = ULONG_MAX;
2723 char buff[11];
2724
2725 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2726 return -EACCES;
2727 num = (count < 10) ? count : 10;
2728 if (copy_from_user(buff, buffer, num))
2729 return -EFAULT;
2730 buff[num] = '\0';
2731 k = simple_strtoul(buff, NULL, 10);
2732 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2733 sg_big_buff = k;
2734 return count;
2735 }
2736 return -ERANGE;
2737}
2738
2739static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2740{
2741 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2742 sg_version_date);
2743 return 0;
2744}
2745
2746static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2747{
2748 return single_open(file, sg_proc_seq_show_version, NULL);
2749}
2750
2751static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2752{
2753 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2754 "online\n");
2755 return 0;
2756}
2757
2758static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2759{
2760 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2761}
2762
2763struct sg_proc_deviter {
2764 loff_t index;
2765 size_t max;
2766};
2767
2768static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2769{
2770 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2771
Jan Blunck729d70f2005-08-27 11:07:52 -07002772 s->private = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (! it)
2774 return NULL;
Jan Blunck729d70f2005-08-27 11:07:52 -07002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 it->index = *pos;
2777 it->max = sg_last_dev();
2778 if (it->index >= it->max)
Jan Blunck729d70f2005-08-27 11:07:52 -07002779 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2784{
Jan Blunck729d70f2005-08-27 11:07:52 -07002785 struct sg_proc_deviter * it = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 *pos = ++it->index;
2788 return (it->index < it->max) ? it : NULL;
2789}
2790
2791static void dev_seq_stop(struct seq_file *s, void *v)
2792{
Jan Blunck729d70f2005-08-27 11:07:52 -07002793 kfree(s->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796static int sg_proc_open_dev(struct inode *inode, struct file *file)
2797{
2798 return seq_open(file, &dev_seq_ops);
2799}
2800
2801static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2802{
2803 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2804 Sg_device *sdp;
2805 struct scsi_device *scsidp;
2806
2807 sdp = it ? sg_get_dev(it->index) : NULL;
2808 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2809 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2810 scsidp->host->host_no, scsidp->channel,
2811 scsidp->id, scsidp->lun, (int) scsidp->type,
2812 1,
2813 (int) scsidp->queue_depth,
2814 (int) scsidp->device_busy,
2815 (int) scsi_device_online(scsidp));
2816 else
2817 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2818 return 0;
2819}
2820
2821static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2822{
2823 return seq_open(file, &devstrs_seq_ops);
2824}
2825
2826static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2827{
2828 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2829 Sg_device *sdp;
2830 struct scsi_device *scsidp;
2831
2832 sdp = it ? sg_get_dev(it->index) : NULL;
2833 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2834 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2835 scsidp->vendor, scsidp->model, scsidp->rev);
2836 else
2837 seq_printf(s, "<no active device>\n");
2838 return 0;
2839}
2840
2841static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2842{
2843 int k, m, new_interface, blen, usg;
2844 Sg_request *srp;
2845 Sg_fd *fp;
2846 const sg_io_hdr_t *hp;
2847 const char * cp;
cb59e842005-04-02 13:51:23 -06002848 unsigned int ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2851 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2852 "(res)sgat=%d low_dma=%d\n", k + 1,
2853 jiffies_to_msecs(fp->timeout),
2854 fp->reserve.bufflen,
2855 (int) fp->reserve.k_use_sg,
2856 (int) fp->low_dma);
2857 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2858 (int) fp->cmd_q, (int) fp->force_packid,
2859 (int) fp->keep_orphan, (int) fp->closed);
2860 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2861 hp = &srp->header;
2862 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2863 if (srp->res_used) {
2864 if (new_interface &&
2865 (SG_FLAG_MMAP_IO & hp->flags))
2866 cp = " mmap>> ";
2867 else
2868 cp = " rb>> ";
2869 } else {
2870 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2871 cp = " dio>> ";
2872 else
2873 cp = " ";
2874 }
2875 seq_printf(s, cp);
Mike Christied6b10342005-11-08 04:06:41 -06002876 blen = srp->data.bufflen;
2877 usg = srp->data.k_use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 seq_printf(s, srp->done ?
2879 ((1 == srp->done) ? "rcv:" : "fin:")
Mike Christied6b10342005-11-08 04:06:41 -06002880 : "act:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 seq_printf(s, " id=%d blen=%d",
2882 srp->header.pack_id, blen);
2883 if (srp->done)
2884 seq_printf(s, " dur=%d", hp->duration);
cb59e842005-04-02 13:51:23 -06002885 else {
2886 ms = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 seq_printf(s, " t_o/elap=%d/%d",
cb59e842005-04-02 13:51:23 -06002888 (new_interface ? hp->timeout :
2889 jiffies_to_msecs(fp->timeout)),
2890 (ms > hp->duration ? ms - hp->duration : 0));
2891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2893 (int) srp->data.cmd_opcode);
2894 }
2895 if (0 == m)
2896 seq_printf(s, " No requests active\n");
2897 }
2898}
2899
2900static int sg_proc_open_debug(struct inode *inode, struct file *file)
2901{
2902 return seq_open(file, &debug_seq_ops);
2903}
2904
2905static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2906{
2907 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2908 Sg_device *sdp;
2909
2910 if (it && (0 == it->index)) {
James Bottomley7c07d612007-08-05 13:36:11 -05002911 seq_printf(s, "max_active_device=%d(origin 1)\n",
2912 (int)it->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2914 }
2915 sdp = it ? sg_get_dev(it->index) : NULL;
2916 if (sdp) {
2917 struct scsi_device *scsidp = sdp->device;
2918
2919 if (NULL == scsidp) {
2920 seq_printf(s, "device %d detached ??\n",
2921 (int)it->index);
2922 return 0;
2923 }
2924
2925 if (sg_get_nth_sfp(sdp, 0)) {
2926 seq_printf(s, " >>> device=%s ",
2927 sdp->disk->disk_name);
2928 if (sdp->detached)
2929 seq_printf(s, "detached pending close ");
2930 else
2931 seq_printf
2932 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2933 scsidp->host->host_no,
2934 scsidp->channel, scsidp->id,
2935 scsidp->lun,
2936 scsidp->host->hostt->emulated);
2937 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2938 sdp->sg_tablesize, sdp->exclude);
2939 }
2940 sg_proc_debug_helper(s, sdp);
2941 }
2942 return 0;
2943}
2944
2945#endif /* CONFIG_SCSI_PROC_FS */
2946
2947module_init(init_sg);
2948module_exit(exit_sg);