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
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
12 * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
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)
21 static int sg_version_num = 30534; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.34"
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).
31 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/string.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>
44 #include <linux/moduleparam.h>
45 #include <linux/cdev.h>
46 #include <linux/idr.h>
47 #include <linux/seq_file.h>
48 #include <linux/blkdev.h>
49 #include <linux/delay.h>
50 #include <linux/blktrace_api.h>
51 #include <linux/smp_lock.h>
54 #include <scsi/scsi_dbg.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_driver.h>
57 #include <scsi/scsi_ioctl.h>
60 #include "scsi_logging.h"
62 #ifdef CONFIG_SCSI_PROC_FS
63 #include <linux/proc_fs.h>
64 static char *sg_version_date = "20061027";
66 static int sg_proc_init(void);
67 static void sg_proc_cleanup(void);
70 #define SG_ALLOW_DIO_DEF 0
72 #define SG_MAX_DEVS 32768
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
83 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
85 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
87 int 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).] */
94 static int def_reserved_size = -1; /* picks up init parameter */
95 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
97 static int scatter_elem_sz = SG_SCATTER_SZ;
98 static int scatter_elem_sz_prev = SG_SCATTER_SZ;
100 #define SG_SECTOR_SZ 512
101 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
103 static int sg_add(struct device *, struct class_interface *);
104 static void sg_remove(struct device *, struct class_interface *);
106 static DEFINE_IDR(sg_index_idr);
107 static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
108 file descriptor list for device */
110 static struct class_interface sg_interface = {
112 .remove_dev = sg_remove,
115 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
116 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
117 unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
118 unsigned bufflen; /* Size of (aggregate) data buffer */
121 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
122 unsigned char cmd_opcode; /* first byte of command */
125 struct sg_device; /* forward declarations */
128 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
129 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> */
133 unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
134 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 */
142 typedef struct sg_fd { /* holds the state of a file descriptor */
143 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
144 struct sg_device *parentdp; /* owning device */
145 wait_queue_head_t read_wait; /* queue read until command done */
146 rwlock_t rq_list_lock; /* protect access to list in req_arr */
147 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
148 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
149 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
150 unsigned save_scat_len; /* original length of trunc. scat. element */
151 Sg_request *headrp; /* head of request slist, NULL->empty */
152 struct fasync_struct *async_qp; /* used by asynchronous notification */
153 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
154 char low_dma; /* as in parent but possibly overridden to 1 */
155 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
156 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
157 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
160 char mmap_called; /* 0 -> mmap() never called on this fd */
163 typedef struct sg_device { /* holds the state of each scsi generic device */
164 struct scsi_device *device;
165 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
166 int sg_tablesize; /* adapter's max scatter-gather table size */
167 u32 index; /* device index number */
168 Sg_fd *headfp; /* first open fd belonging to this device */
169 volatile char detached; /* 0->attached, 1->detached pending removal */
170 volatile char exclude; /* opened for exclusive access */
171 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
172 struct gendisk *disk;
173 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
176 static int sg_fasync(int fd, struct file *filp, int mode);
177 /* tasklet or soft irq callback */
178 static void sg_rq_end_io(struct request *rq, int uptodate);
179 static int sg_start_req(Sg_request *srp, unsigned char *cmd);
180 static void sg_finish_rem_req(Sg_request * srp);
181 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
184 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
186 static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
187 const char __user *buf, size_t count, int blocking,
188 int read_only, Sg_request **o_srp);
189 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
191 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
192 static void sg_remove_scat(Sg_scatter_hold * schp);
193 static void sg_build_reserve(Sg_fd * sfp, int req_size);
194 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
195 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
196 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
197 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
198 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
199 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
200 static Sg_request *sg_add_request(Sg_fd * sfp);
201 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
202 static int sg_res_in_use(Sg_fd * sfp);
203 static Sg_device *sg_get_dev(int dev);
204 #ifdef CONFIG_SCSI_PROC_FS
205 static int sg_last_dev(void);
208 #define SZ_SG_HEADER sizeof(struct sg_header)
209 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
210 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
211 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
213 static int sg_allow_access(struct file *filp, unsigned char *cmd)
215 struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
216 struct request_queue *q = sfp->parentdp->device->request_queue;
218 if (sfp->parentdp->device->type == TYPE_SCANNER)
221 return blk_verify_command(&q->cmd_filter,
222 cmd, filp->f_mode & FMODE_WRITE);
226 sg_open(struct inode *inode, struct file *filp)
228 int dev = iminor(inode);
229 int flags = filp->f_flags;
230 struct request_queue *q;
237 nonseekable_open(inode, filp);
238 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
239 sdp = sg_get_dev(dev);
240 if ((!sdp) || (!sdp->device)) {
249 /* This driver's module count bumped by fops_get in <linux/fs.h> */
250 /* Prevent the device driver from vanishing while we sleep */
251 retval = scsi_device_get(sdp->device);
257 if (!((flags & O_NONBLOCK) ||
258 scsi_block_when_processing_errors(sdp->device))) {
260 /* we are in error recovery for this device */
264 if (flags & O_EXCL) {
265 if (O_RDONLY == (flags & O_ACCMODE)) {
266 retval = -EPERM; /* Can't lock it with read only access */
269 if (sdp->headfp && (flags & O_NONBLOCK)) {
274 __wait_event_interruptible(sdp->o_excl_wait,
275 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
277 retval = res; /* -ERESTARTSYS because signal hit process */
280 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
281 if (flags & O_NONBLOCK) {
286 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
289 retval = res; /* -ERESTARTSYS because signal hit process */
297 if (!sdp->headfp) { /* no existing opens on this device */
299 q = sdp->device->request_queue;
300 sdp->sg_tablesize = min(q->max_hw_segments,
301 q->max_phys_segments);
303 if ((sfp = sg_add_sfp(sdp, dev)))
304 filp->private_data = sfp;
307 sdp->exclude = 0; /* undo if error */
315 scsi_device_put(sdp->device);
320 /* Following function was formerly called 'sg_close' */
322 sg_release(struct inode *inode, struct file *filp)
327 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
329 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
330 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
331 if (!sdp->detached) {
332 scsi_device_put(sdp->device);
335 wake_up_interruptible(&sdp->o_excl_wait);
341 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
346 int req_pack_id = -1;
348 struct sg_header *old_hdr = NULL;
351 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
353 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
354 sdp->disk->disk_name, (int) count));
356 if (!access_ok(VERIFY_WRITE, buf, count))
358 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
359 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
362 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
366 if (old_hdr->reply_len < 0) {
367 if (count >= SZ_SG_IO_HDR) {
368 sg_io_hdr_t *new_hdr;
369 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
374 retval =__copy_from_user
375 (new_hdr, buf, SZ_SG_IO_HDR);
376 req_pack_id = new_hdr->pack_id;
384 req_pack_id = old_hdr->pack_id;
386 srp = sg_get_rq_mark(sfp, req_pack_id);
387 if (!srp) { /* now wait on packet to arrive */
392 if (filp->f_flags & O_NONBLOCK) {
397 retval = 0; /* following macro beats race condition */
398 __wait_event_interruptible(sfp->read_wait,
400 (srp = sg_get_rq_mark(sfp, req_pack_id))),
409 /* -ERESTARTSYS as signal hit process */
413 if (srp->header.interface_id != '\0') {
414 retval = sg_new_read(sfp, buf, count, srp);
419 if (old_hdr == NULL) {
420 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
426 memset(old_hdr, 0, SZ_SG_HEADER);
427 old_hdr->reply_len = (int) hp->timeout;
428 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
429 old_hdr->pack_id = hp->pack_id;
430 old_hdr->twelve_byte =
431 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
432 old_hdr->target_status = hp->masked_status;
433 old_hdr->host_status = hp->host_status;
434 old_hdr->driver_status = hp->driver_status;
435 if ((CHECK_CONDITION & hp->masked_status) ||
436 (DRIVER_SENSE & hp->driver_status))
437 memcpy(old_hdr->sense_buffer, srp->sense_b,
438 sizeof (old_hdr->sense_buffer));
439 switch (hp->host_status) {
440 /* This setup of 'result' is for backward compatibility and is best
441 ignored by the user who should use target, host + driver status */
443 case DID_PASSTHROUGH:
450 old_hdr->result = EBUSY;
457 old_hdr->result = EIO;
460 old_hdr->result = (srp->sense_b[0] == 0 &&
461 hp->masked_status == GOOD) ? 0 : EIO;
464 old_hdr->result = EIO;
468 /* Now copy the result back to the user buffer. */
469 if (count >= SZ_SG_HEADER) {
470 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
475 if (count > old_hdr->reply_len)
476 count = old_hdr->reply_len;
477 if (count > SZ_SG_HEADER) {
478 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
484 count = (old_hdr->result == 0) ? 0 : -EIO;
485 sg_finish_rem_req(srp);
493 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
495 sg_io_hdr_t *hp = &srp->header;
499 if (count < SZ_SG_IO_HDR) {
504 if ((hp->mx_sb_len > 0) && hp->sbp) {
505 if ((CHECK_CONDITION & hp->masked_status) ||
506 (DRIVER_SENSE & hp->driver_status)) {
507 int sb_len = SCSI_SENSE_BUFFERSIZE;
508 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
509 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
510 len = (len > sb_len) ? sb_len : len;
511 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
518 if (hp->masked_status || hp->host_status || hp->driver_status)
519 hp->info |= SG_INFO_CHECK;
520 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
525 sg_finish_rem_req(srp);
526 return (0 == err) ? count : err;
530 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
532 int mxsize, cmd_size, k;
533 int input_size, blocking;
534 unsigned char opcode;
538 struct sg_header old_hdr;
540 unsigned char cmnd[MAX_COMMAND_SIZE];
542 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
544 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
545 sdp->disk->disk_name, (int) count));
548 if (!((filp->f_flags & O_NONBLOCK) ||
549 scsi_block_when_processing_errors(sdp->device)))
552 if (!access_ok(VERIFY_READ, buf, count))
553 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
554 if (count < SZ_SG_HEADER)
556 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
558 blocking = !(filp->f_flags & O_NONBLOCK);
559 if (old_hdr.reply_len < 0)
560 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
561 if (count < (SZ_SG_HEADER + 6))
562 return -EIO; /* The minimum scsi command length is 6 bytes. */
564 if (!(srp = sg_add_request(sfp))) {
565 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
569 __get_user(opcode, buf);
570 if (sfp->next_cmd_len > 0) {
571 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
572 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
573 sfp->next_cmd_len = 0;
574 sg_remove_request(sfp, srp);
577 cmd_size = sfp->next_cmd_len;
578 sfp->next_cmd_len = 0; /* reset so only this write() effected */
580 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
581 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
584 SCSI_LOG_TIMEOUT(4, printk(
585 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
586 /* Determine buffer size. */
587 input_size = count - cmd_size;
588 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
589 mxsize -= SZ_SG_HEADER;
590 input_size -= SZ_SG_HEADER;
591 if (input_size < 0) {
592 sg_remove_request(sfp, srp);
593 return -EIO; /* User did not pass enough bytes for this command. */
596 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
597 hp->cmd_len = (unsigned char) cmd_size;
601 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
602 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
604 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
605 hp->dxfer_len = mxsize;
606 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
607 hp->dxferp = (char __user *)buf + cmd_size;
611 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
612 hp->flags = input_size; /* structure abuse ... */
613 hp->pack_id = old_hdr.pack_id;
615 if (__copy_from_user(cmnd, buf, cmd_size))
618 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
619 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
620 * is a non-zero input_size, so emit a warning.
622 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
623 static char cmd[TASK_COMM_LEN];
624 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
626 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
627 "guessing data in;\n" KERN_WARNING " "
628 "program %s not setting count and/or reply_len properly\n",
629 old_hdr.reply_len - (int)SZ_SG_HEADER,
630 input_size, (unsigned int) cmnd[0],
632 strcpy(cmd, current->comm);
635 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
636 return (k < 0) ? k : count;
640 sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
641 size_t count, int blocking, int read_only,
647 unsigned char cmnd[MAX_COMMAND_SIZE];
649 unsigned long ul_timeout;
651 if (count < SZ_SG_IO_HDR)
653 if (!access_ok(VERIFY_READ, buf, count))
654 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
656 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
657 if (!(srp = sg_add_request(sfp))) {
658 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
662 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
663 sg_remove_request(sfp, srp);
666 if (hp->interface_id != 'S') {
667 sg_remove_request(sfp, srp);
670 if (hp->flags & SG_FLAG_MMAP_IO) {
671 if (hp->dxfer_len > sfp->reserve.bufflen) {
672 sg_remove_request(sfp, srp);
673 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
675 if (hp->flags & SG_FLAG_DIRECT_IO) {
676 sg_remove_request(sfp, srp);
677 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
679 if (sg_res_in_use(sfp)) {
680 sg_remove_request(sfp, srp);
681 return -EBUSY; /* reserve buffer already being used */
684 ul_timeout = msecs_to_jiffies(srp->header.timeout);
685 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
686 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
687 sg_remove_request(sfp, srp);
690 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
691 sg_remove_request(sfp, srp);
692 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
694 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
695 sg_remove_request(sfp, srp);
698 if (read_only && sg_allow_access(file, cmnd)) {
699 sg_remove_request(sfp, srp);
702 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
711 sg_common_write(Sg_fd * sfp, Sg_request * srp,
712 unsigned char *cmnd, int timeout, int blocking)
715 Sg_device *sdp = sfp->parentdp;
716 sg_io_hdr_t *hp = &srp->header;
718 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
720 hp->masked_status = 0;
724 hp->driver_status = 0;
726 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
727 (int) cmnd[0], (int) hp->cmd_len));
729 k = sg_start_req(srp, cmnd);
731 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
732 sg_finish_rem_req(srp);
733 return k; /* probably out of space --> ENOMEM */
736 sg_finish_rem_req(srp);
740 switch (hp->dxfer_direction) {
741 case SG_DXFER_TO_FROM_DEV:
742 case SG_DXFER_FROM_DEV:
743 data_dir = DMA_FROM_DEVICE;
745 case SG_DXFER_TO_DEV:
746 data_dir = DMA_TO_DEVICE;
748 case SG_DXFER_UNKNOWN:
749 data_dir = DMA_BIDIRECTIONAL;
755 hp->duration = jiffies_to_msecs(jiffies);
757 srp->rq->timeout = timeout;
758 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
759 srp->rq, 1, sg_rq_end_io);
764 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
766 unsigned long iflags;
769 read_lock_irqsave(&sfp->rq_list_lock, iflags);
771 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
776 sg_ioctl(struct inode *inode, struct file *filp,
777 unsigned int cmd_in, unsigned long arg)
779 void __user *p = (void __user *)arg;
781 int result, val, read_only;
785 unsigned long iflags;
787 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
790 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
791 sdp->disk->disk_name, (int) cmd_in));
792 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
797 int blocking = 1; /* ignore O_NONBLOCK flag */
801 if (!scsi_block_when_processing_errors(sdp->device))
803 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
806 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
807 blocking, read_only, &srp);
810 srp->sg_io_owned = 1;
812 result = 0; /* following macro to beat race condition */
813 __wait_event_interruptible(sfp->read_wait,
814 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
819 return 0; /* request packet dropped already */
823 return result; /* -ERESTARTSYS because signal hit process */
825 write_lock_irqsave(&sfp->rq_list_lock, iflags);
827 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
828 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
829 return (result < 0) ? result : 0;
832 result = get_user(val, ip);
837 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
838 val = MULDIV (INT_MAX, USER_HZ, HZ);
839 sfp->timeout_user = val;
840 sfp->timeout = MULDIV (val, HZ, USER_HZ);
843 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
844 /* strange ..., for backward compatibility */
845 return sfp->timeout_user;
846 case SG_SET_FORCE_LOW_DMA:
847 result = get_user(val, ip);
852 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
853 val = (int) sfp->reserve.bufflen;
854 sg_remove_scat(&sfp->reserve);
855 sg_build_reserve(sfp, val);
860 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
864 return put_user((int) sfp->low_dma, ip);
866 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
869 sg_scsi_id_t __user *sg_idp = p;
873 __put_user((int) sdp->device->host->host_no,
875 __put_user((int) sdp->device->channel,
877 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
878 __put_user((int) sdp->device->lun, &sg_idp->lun);
879 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
880 __put_user((short) sdp->device->host->cmd_per_lun,
881 &sg_idp->h_cmd_per_lun);
882 __put_user((short) sdp->device->queue_depth,
883 &sg_idp->d_queue_depth);
884 __put_user(0, &sg_idp->unused[0]);
885 __put_user(0, &sg_idp->unused[1]);
888 case SG_SET_FORCE_PACK_ID:
889 result = get_user(val, ip);
892 sfp->force_packid = val ? 1 : 0;
895 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
897 read_lock_irqsave(&sfp->rq_list_lock, iflags);
898 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
899 if ((1 == srp->done) && (!srp->sg_io_owned)) {
900 read_unlock_irqrestore(&sfp->rq_list_lock,
902 __put_user(srp->header.pack_id, ip);
906 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
909 case SG_GET_NUM_WAITING:
910 read_lock_irqsave(&sfp->rq_list_lock, iflags);
911 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
912 if ((1 == srp->done) && (!srp->sg_io_owned))
915 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
916 return put_user(val, ip);
917 case SG_GET_SG_TABLESIZE:
918 return put_user(sdp->sg_tablesize, ip);
919 case SG_SET_RESERVED_SIZE:
920 result = get_user(val, ip);
925 val = min_t(int, val,
926 sdp->device->request_queue->max_sectors * 512);
927 if (val != sfp->reserve.bufflen) {
928 if (sg_res_in_use(sfp) || sfp->mmap_called)
930 sg_remove_scat(&sfp->reserve);
931 sg_build_reserve(sfp, val);
934 case SG_GET_RESERVED_SIZE:
935 val = min_t(int, sfp->reserve.bufflen,
936 sdp->device->request_queue->max_sectors * 512);
937 return put_user(val, ip);
938 case SG_SET_COMMAND_Q:
939 result = get_user(val, ip);
942 sfp->cmd_q = val ? 1 : 0;
944 case SG_GET_COMMAND_Q:
945 return put_user((int) sfp->cmd_q, ip);
946 case SG_SET_KEEP_ORPHAN:
947 result = get_user(val, ip);
950 sfp->keep_orphan = val;
952 case SG_GET_KEEP_ORPHAN:
953 return put_user((int) sfp->keep_orphan, ip);
954 case SG_NEXT_CMD_LEN:
955 result = get_user(val, ip);
958 sfp->next_cmd_len = (val > 0) ? val : 0;
960 case SG_GET_VERSION_NUM:
961 return put_user(sg_version_num, ip);
962 case SG_GET_ACCESS_COUNT:
963 /* faked - we don't have a real access count anymore */
964 val = (sdp->device ? 1 : 0);
965 return put_user(val, ip);
966 case SG_GET_REQUEST_TABLE:
967 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
970 sg_req_info_t *rinfo;
973 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
977 read_lock_irqsave(&sfp->rq_list_lock, iflags);
978 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
979 ++val, srp = srp ? srp->nextrp : srp) {
980 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
982 rinfo[val].req_state = srp->done + 1;
984 srp->header.masked_status &
985 srp->header.host_status &
986 srp->header.driver_status;
988 rinfo[val].duration =
989 srp->header.duration;
991 ms = jiffies_to_msecs(jiffies);
992 rinfo[val].duration =
993 (ms > srp->header.duration) ?
994 (ms - srp->header.duration) : 0;
996 rinfo[val].orphan = srp->orphan;
997 rinfo[val].sg_io_owned =
1000 srp->header.pack_id;
1001 rinfo[val].usr_ptr =
1002 srp->header.usr_ptr;
1005 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1006 result = __copy_to_user(p, rinfo,
1007 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1008 result = result ? -EFAULT : 0;
1012 case SG_EMULATED_HOST:
1015 return put_user(sdp->device->host->hostt->emulated, ip);
1019 if (filp->f_flags & O_NONBLOCK) {
1020 if (scsi_host_in_recovery(sdp->device->host))
1022 } else if (!scsi_block_when_processing_errors(sdp->device))
1024 result = get_user(val, ip);
1027 if (SG_SCSI_RESET_NOTHING == val)
1030 case SG_SCSI_RESET_DEVICE:
1031 val = SCSI_TRY_RESET_DEVICE;
1033 case SG_SCSI_RESET_TARGET:
1034 val = SCSI_TRY_RESET_TARGET;
1036 case SG_SCSI_RESET_BUS:
1037 val = SCSI_TRY_RESET_BUS;
1039 case SG_SCSI_RESET_HOST:
1040 val = SCSI_TRY_RESET_HOST;
1045 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1047 return (scsi_reset_provider(sdp->device, val) ==
1048 SUCCESS) ? 0 : -EIO;
1049 case SCSI_IOCTL_SEND_COMMAND:
1053 unsigned char opcode = WRITE_6;
1054 Scsi_Ioctl_Command __user *siocp = p;
1056 if (copy_from_user(&opcode, siocp->data, 1))
1058 if (sg_allow_access(filp, &opcode))
1061 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
1063 result = get_user(val, ip);
1066 sdp->sgdebug = (char) val;
1068 case SCSI_IOCTL_GET_IDLUN:
1069 case SCSI_IOCTL_GET_BUS_NUMBER:
1070 case SCSI_IOCTL_PROBE_HOST:
1071 case SG_GET_TRANSFORM:
1074 return scsi_ioctl(sdp->device, cmd_in, p);
1076 return put_user(sdp->device->request_queue->max_sectors * 512,
1079 return blk_trace_setup(sdp->device->request_queue,
1080 sdp->disk->disk_name,
1081 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
1084 return blk_trace_startstop(sdp->device->request_queue, 1);
1086 return blk_trace_startstop(sdp->device->request_queue, 0);
1087 case BLKTRACETEARDOWN:
1088 return blk_trace_remove(sdp->device->request_queue);
1091 return -EPERM; /* don't know so take safe approach */
1092 return scsi_ioctl(sdp->device, cmd_in, p);
1096 #ifdef CONFIG_COMPAT
1097 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1101 struct scsi_device *sdev;
1103 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1107 if (sdev->host->hostt->compat_ioctl) {
1110 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1115 return -ENOIOCTLCMD;
1120 sg_poll(struct file *filp, poll_table * wait)
1122 unsigned int res = 0;
1127 unsigned long iflags;
1129 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1132 poll_wait(filp, &sfp->read_wait, wait);
1133 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1134 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1135 /* if any read waiting, flag it */
1136 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1137 res = POLLIN | POLLRDNORM;
1140 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1144 else if (!sfp->cmd_q) {
1146 res |= POLLOUT | POLLWRNORM;
1147 } else if (count < SG_MAX_QUEUE)
1148 res |= POLLOUT | POLLWRNORM;
1149 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1150 sdp->disk->disk_name, (int) res));
1155 sg_fasync(int fd, struct file *filp, int mode)
1160 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1162 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1163 sdp->disk->disk_name, mode));
1165 return fasync_helper(fd, filp, mode, &sfp->async_qp);
1169 sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1172 unsigned long offset, len, sa;
1173 Sg_scatter_hold *rsv_schp;
1176 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1177 return VM_FAULT_SIGBUS;
1178 rsv_schp = &sfp->reserve;
1179 offset = vmf->pgoff << PAGE_SHIFT;
1180 if (offset >= rsv_schp->bufflen)
1181 return VM_FAULT_SIGBUS;
1182 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
1183 offset, rsv_schp->k_use_sg));
1185 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1186 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
1187 len = vma->vm_end - sa;
1188 len = (len < length) ? len : length;
1190 struct page *page = nth_page(rsv_schp->pages[k],
1191 offset >> PAGE_SHIFT);
1192 get_page(page); /* increment page count */
1194 return 0; /* success */
1200 return VM_FAULT_SIGBUS;
1203 static struct vm_operations_struct sg_mmap_vm_ops = {
1204 .fault = sg_vma_fault,
1208 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1211 unsigned long req_sz, len, sa;
1212 Sg_scatter_hold *rsv_schp;
1215 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1217 req_sz = vma->vm_end - vma->vm_start;
1218 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1219 (void *) vma->vm_start, (int) req_sz));
1221 return -EINVAL; /* want no offset */
1222 rsv_schp = &sfp->reserve;
1223 if (req_sz > rsv_schp->bufflen)
1224 return -ENOMEM; /* cannot map more than reserved buffer */
1227 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1228 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
1229 len = vma->vm_end - sa;
1230 len = (len < length) ? len : length;
1234 sfp->mmap_called = 1;
1235 vma->vm_flags |= VM_RESERVED;
1236 vma->vm_private_data = sfp;
1237 vma->vm_ops = &sg_mmap_vm_ops;
1242 * This function is a "bottom half" handler that is called by the mid
1243 * level when a command is completed (or has failed).
1245 static void sg_rq_end_io(struct request *rq, int uptodate)
1247 struct sg_request *srp = rq->end_io_data;
1248 Sg_device *sdp = NULL;
1250 unsigned long iflags;
1256 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1259 sfp = srp->parentfp;
1261 sdp = sfp->parentdp;
1262 if ((NULL == sdp) || sdp->detached) {
1263 printk(KERN_INFO "sg_cmd_done: device detached\n");
1268 result = rq->errors;
1269 resid = rq->data_len;
1271 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1272 sdp->disk->disk_name, srp->header.pack_id, result));
1273 srp->header.resid = resid;
1274 ms = jiffies_to_msecs(jiffies);
1275 srp->header.duration = (ms > srp->header.duration) ?
1276 (ms - srp->header.duration) : 0;
1278 struct scsi_sense_hdr sshdr;
1280 srp->header.status = 0xff & result;
1281 srp->header.masked_status = status_byte(result);
1282 srp->header.msg_status = msg_byte(result);
1283 srp->header.host_status = host_byte(result);
1284 srp->header.driver_status = driver_byte(result);
1285 if ((sdp->sgdebug > 0) &&
1286 ((CHECK_CONDITION == srp->header.masked_status) ||
1287 (COMMAND_TERMINATED == srp->header.masked_status)))
1288 __scsi_print_sense("sg_cmd_done", sense,
1289 SCSI_SENSE_BUFFERSIZE);
1291 /* Following if statement is a patch supplied by Eric Youngdale */
1292 if (driver_byte(result) != 0
1293 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1294 && !scsi_sense_is_deferred(&sshdr)
1295 && sshdr.sense_key == UNIT_ATTENTION
1296 && sdp->device->removable) {
1297 /* Detected possible disc change. Set the bit - this */
1298 /* may be used if there are filesystems using this device */
1299 sdp->device->changed = 1;
1302 /* Rely on write phase to clean out srp status values, so no "else" */
1304 if (sfp->closed) { /* whoops this fd already released, cleanup */
1305 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1306 sg_finish_rem_req(srp);
1308 if (NULL == sfp->headrp) {
1309 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1310 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1311 scsi_device_put(sdp->device);
1315 } else if (srp && srp->orphan) {
1316 if (sfp->keep_orphan)
1317 srp->sg_io_owned = 0;
1319 sg_finish_rem_req(srp);
1324 /* Now wake up any sg_read() that is waiting for this packet. */
1325 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1326 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1328 wake_up_interruptible(&sfp->read_wait);
1329 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1333 static struct file_operations sg_fops = {
1334 .owner = THIS_MODULE,
1339 #ifdef CONFIG_COMPAT
1340 .compat_ioctl = sg_compat_ioctl,
1344 .release = sg_release,
1345 .fasync = sg_fasync,
1348 static struct class *sg_sysfs_class;
1350 static int sg_sysfs_valid = 0;
1352 static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1354 struct request_queue *q = scsidp->request_queue;
1356 unsigned long iflags;
1360 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1362 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1363 return ERR_PTR(-ENOMEM);
1366 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1367 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1371 write_lock_irqsave(&sg_index_lock, iflags);
1372 error = idr_get_new(&sg_index_idr, sdp, &k);
1373 write_unlock_irqrestore(&sg_index_lock, iflags);
1376 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1381 if (unlikely(k >= SG_MAX_DEVS))
1384 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1385 sprintf(disk->disk_name, "sg%d", k);
1386 disk->first_minor = k;
1388 sdp->device = scsidp;
1389 init_waitqueue_head(&sdp->o_excl_wait);
1390 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1397 return ERR_PTR(error);
1402 sdev_printk(KERN_WARNING, scsidp,
1403 "Unable to attach sg device type=%d, minor "
1404 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1410 sg_add(struct device *cl_dev, struct class_interface *cl_intf)
1412 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1413 struct gendisk *disk;
1414 Sg_device *sdp = NULL;
1415 struct cdev * cdev = NULL;
1417 unsigned long iflags;
1419 disk = alloc_disk(1);
1421 printk(KERN_WARNING "alloc_disk failed\n");
1424 disk->major = SCSI_GENERIC_MAJOR;
1427 cdev = cdev_alloc();
1429 printk(KERN_WARNING "cdev_alloc failed\n");
1432 cdev->owner = THIS_MODULE;
1433 cdev->ops = &sg_fops;
1435 sdp = sg_alloc(disk, scsidp);
1437 printk(KERN_WARNING "sg_alloc failed\n");
1438 error = PTR_ERR(sdp);
1442 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
1447 if (sg_sysfs_valid) {
1448 struct device *sg_class_member;
1450 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1451 MKDEV(SCSI_GENERIC_MAJOR,
1453 sdp, "%s", disk->disk_name);
1454 if (IS_ERR(sg_class_member)) {
1455 printk(KERN_ERR "sg_add: "
1456 "device_create failed\n");
1457 error = PTR_ERR(sg_class_member);
1460 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1461 &sg_class_member->kobj, "generic");
1463 printk(KERN_ERR "sg_add: unable to make symlink "
1464 "'generic' back to sg%d\n", sdp->index);
1466 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
1468 sdev_printk(KERN_NOTICE, scsidp,
1469 "Attached scsi generic sg%d type %d\n", sdp->index,
1472 dev_set_drvdata(cl_dev, sdp);
1477 write_lock_irqsave(&sg_index_lock, iflags);
1478 idr_remove(&sg_index_idr, sdp->index);
1479 write_unlock_irqrestore(&sg_index_lock, iflags);
1490 sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
1492 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1493 Sg_device *sdp = dev_get_drvdata(cl_dev);
1494 unsigned long iflags;
1505 write_lock_irqsave(&sg_index_lock, iflags);
1508 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1510 for (srp = sfp->headrp; srp; srp = tsrp) {
1512 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1513 sg_finish_rem_req(srp);
1516 scsi_device_put(sdp->device);
1517 __sg_remove_sfp(sdp, sfp);
1520 wake_up_interruptible(&sfp->read_wait);
1521 kill_fasync(&sfp->async_qp, SIGPOLL,
1525 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1526 if (NULL == sdp->headfp) {
1527 idr_remove(&sg_index_idr, sdp->index);
1529 } else { /* nothing active, simple case */
1530 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1531 idr_remove(&sg_index_idr, sdp->index);
1533 write_unlock_irqrestore(&sg_index_lock, iflags);
1535 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1536 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1537 cdev_del(sdp->cdev);
1539 put_disk(sdp->disk);
1541 if (NULL == sdp->headfp)
1545 msleep(10); /* dirty detach so delay device destruction */
1548 module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1549 module_param_named(def_reserved_size, def_reserved_size, int,
1551 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1553 MODULE_AUTHOR("Douglas Gilbert");
1554 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1555 MODULE_LICENSE("GPL");
1556 MODULE_VERSION(SG_VERSION_STR);
1557 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
1559 MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1560 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1561 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1562 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1569 if (scatter_elem_sz < PAGE_SIZE) {
1570 scatter_elem_sz = PAGE_SIZE;
1571 scatter_elem_sz_prev = scatter_elem_sz;
1573 if (def_reserved_size >= 0)
1574 sg_big_buff = def_reserved_size;
1576 def_reserved_size = sg_big_buff;
1578 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1582 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1583 if ( IS_ERR(sg_sysfs_class) ) {
1584 rc = PTR_ERR(sg_sysfs_class);
1588 rc = scsi_register_interface(&sg_interface);
1590 #ifdef CONFIG_SCSI_PROC_FS
1592 #endif /* CONFIG_SCSI_PROC_FS */
1595 class_destroy(sg_sysfs_class);
1597 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1604 #ifdef CONFIG_SCSI_PROC_FS
1606 #endif /* CONFIG_SCSI_PROC_FS */
1607 scsi_unregister_interface(&sg_interface);
1608 class_destroy(sg_sysfs_class);
1610 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1612 idr_destroy(&sg_index_idr);
1615 static int sg_start_req(Sg_request *srp, unsigned char *cmd)
1619 Sg_fd *sfp = srp->parentfp;
1620 sg_io_hdr_t *hp = &srp->header;
1621 int dxfer_len = (int) hp->dxfer_len;
1622 int dxfer_dir = hp->dxfer_direction;
1623 unsigned int iov_count = hp->iovec_count;
1624 Sg_scatter_hold *req_schp = &srp->data;
1625 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1626 struct request_queue *q = sfp->parentdp->device->request_queue;
1627 struct rq_map_data *md, map_data;
1628 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1630 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1633 rq = blk_get_request(q, rw, GFP_ATOMIC);
1637 memcpy(rq->cmd, cmd, hp->cmd_len);
1639 rq->cmd_len = hp->cmd_len;
1640 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1643 rq->end_io_data = srp;
1644 rq->sense = srp->sense_b;
1645 rq->retries = SG_DEFAULT_RETRIES;
1647 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1650 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1651 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1652 !sfp->parentdp->device->host->unchecked_isa_dma &&
1653 blk_rq_aligned(q, hp->dxferp, dxfer_len))
1659 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1660 sg_link_reserve(sfp, srp, dxfer_len);
1662 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1667 md->pages = req_schp->pages;
1668 md->page_order = req_schp->page_order;
1669 md->nr_entries = req_schp->k_use_sg;
1671 md->null_mapped = hp->dxferp ? 0 : 1;
1675 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1676 hp->dxfer_len, GFP_ATOMIC);
1678 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1679 hp->dxfer_len, GFP_ATOMIC);
1685 req_schp->dio_in_use = 1;
1686 hp->info |= SG_INFO_DIRECT_IO;
1693 sg_finish_rem_req(Sg_request * srp)
1695 Sg_fd *sfp = srp->parentfp;
1696 Sg_scatter_hold *req_schp = &srp->data;
1698 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1700 sg_unlink_reserve(sfp, srp);
1702 sg_remove_scat(req_schp);
1706 blk_rq_unmap_user(srp->bio);
1708 blk_put_request(srp->rq);
1711 sg_remove_request(sfp, srp);
1715 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1717 int sg_bufflen = tablesize * sizeof(struct page *);
1718 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1720 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1723 schp->sglist_len = sg_bufflen;
1724 return tablesize; /* number of scat_gath elements allocated */
1728 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1730 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
1731 int sg_tablesize = sfp->parentdp->sg_tablesize;
1732 int blk_size = buff_size, order;
1733 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
1738 ++blk_size; /* don't know why */
1739 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1740 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1741 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1742 buff_size, blk_size));
1744 /* N.B. ret_sz carried into this block ... */
1745 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1746 if (mx_sc_elems < 0)
1747 return mx_sc_elems; /* most likely -ENOMEM */
1749 num = scatter_elem_sz;
1750 if (unlikely(num != scatter_elem_sz_prev)) {
1751 if (num < PAGE_SIZE) {
1752 scatter_elem_sz = PAGE_SIZE;
1753 scatter_elem_sz_prev = PAGE_SIZE;
1755 scatter_elem_sz_prev = num;
1759 gfp_mask |= GFP_DMA;
1761 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1762 gfp_mask |= __GFP_ZERO;
1764 order = get_order(num);
1766 ret_sz = 1 << (PAGE_SHIFT + order);
1768 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1769 k++, rem_sz -= ret_sz) {
1771 num = (rem_sz > scatter_elem_sz_prev) ?
1772 scatter_elem_sz_prev : rem_sz;
1774 schp->pages[k] = alloc_pages(gfp_mask, order);
1775 if (!schp->pages[k])
1778 if (num == scatter_elem_sz_prev) {
1779 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1780 scatter_elem_sz = ret_sz;
1781 scatter_elem_sz_prev = ret_sz;
1785 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1786 "ret_sz=%d\n", k, num, ret_sz));
1787 } /* end of for loop */
1789 schp->page_order = order;
1791 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1792 "rem_sz=%d\n", k, rem_sz));
1794 schp->bufflen = blk_size;
1795 if (rem_sz > 0) /* must have failed */
1799 for (i = 0; i < k; i++)
1800 __free_pages(schp->pages[k], order);
1809 sg_remove_scat(Sg_scatter_hold * schp)
1811 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1812 if (schp->pages && schp->sglist_len > 0) {
1813 if (!schp->dio_in_use) {
1816 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
1817 SCSI_LOG_TIMEOUT(5, printk(
1818 "sg_remove_scat: k=%d, pg=0x%p\n",
1819 k, schp->pages[k]));
1820 __free_pages(schp->pages[k], schp->page_order);
1826 memset(schp, 0, sizeof (*schp));
1830 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1832 Sg_scatter_hold *schp = &srp->data;
1835 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1837 if ((!outp) || (num_read_xfer <= 0))
1840 num = 1 << (PAGE_SHIFT + schp->page_order);
1841 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
1842 if (num > num_read_xfer) {
1843 if (__copy_to_user(outp, page_address(schp->pages[k]),
1848 if (__copy_to_user(outp, page_address(schp->pages[k]),
1851 num_read_xfer -= num;
1852 if (num_read_xfer <= 0)
1862 sg_build_reserve(Sg_fd * sfp, int req_size)
1864 Sg_scatter_hold *schp = &sfp->reserve;
1866 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1868 if (req_size < PAGE_SIZE)
1869 req_size = PAGE_SIZE;
1870 if (0 == sg_build_indirect(schp, sfp, req_size))
1873 sg_remove_scat(schp);
1874 req_size >>= 1; /* divide by 2 */
1875 } while (req_size > (PAGE_SIZE / 2));
1879 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1881 Sg_scatter_hold *req_schp = &srp->data;
1882 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1886 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
1889 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1890 for (k = 0; k < rsv_schp->k_use_sg; k++) {
1892 req_schp->k_use_sg = k + 1;
1893 req_schp->sglist_len = rsv_schp->sglist_len;
1894 req_schp->pages = rsv_schp->pages;
1896 req_schp->bufflen = size;
1897 req_schp->page_order = rsv_schp->page_order;
1903 if (k >= rsv_schp->k_use_sg)
1904 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
1908 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1910 Sg_scatter_hold *req_schp = &srp->data;
1912 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1913 (int) req_schp->k_use_sg));
1914 req_schp->k_use_sg = 0;
1915 req_schp->bufflen = 0;
1916 req_schp->pages = NULL;
1917 req_schp->page_order = 0;
1918 req_schp->sglist_len = 0;
1919 sfp->save_scat_len = 0;
1924 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1927 unsigned long iflags;
1929 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1930 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1931 /* look for requests that are ready + not SG_IO owned */
1932 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1933 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1934 resp->done = 2; /* guard against other readers */
1938 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1942 #ifdef CONFIG_SCSI_PROC_FS
1944 sg_get_nth_request(Sg_fd * sfp, int nth)
1947 unsigned long iflags;
1950 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1951 for (k = 0, resp = sfp->headrp; resp && (k < nth);
1952 ++k, resp = resp->nextrp) ;
1953 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1958 /* always adds to end of list */
1960 sg_add_request(Sg_fd * sfp)
1963 unsigned long iflags;
1965 Sg_request *rp = sfp->req_arr;
1967 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1970 memset(rp, 0, sizeof (Sg_request));
1975 if (0 == sfp->cmd_q)
1976 resp = NULL; /* command queuing disallowed */
1978 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1982 if (k < SG_MAX_QUEUE) {
1983 memset(rp, 0, sizeof (Sg_request));
1985 while (resp->nextrp)
1986 resp = resp->nextrp;
1994 resp->nextrp = NULL;
1995 resp->header.duration = jiffies_to_msecs(jiffies);
1997 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2001 /* Return of 1 for found; 0 for not found */
2003 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2005 Sg_request *prev_rp;
2007 unsigned long iflags;
2010 if ((!sfp) || (!srp) || (!sfp->headrp))
2012 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2013 prev_rp = sfp->headrp;
2014 if (srp == prev_rp) {
2015 sfp->headrp = prev_rp->nextrp;
2016 prev_rp->parentfp = NULL;
2019 while ((rp = prev_rp->nextrp)) {
2021 prev_rp->nextrp = rp->nextrp;
2022 rp->parentfp = NULL;
2029 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2033 #ifdef CONFIG_SCSI_PROC_FS
2035 sg_get_nth_sfp(Sg_device * sdp, int nth)
2038 unsigned long iflags;
2041 read_lock_irqsave(&sg_index_lock, iflags);
2042 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2043 ++k, resp = resp->nextfp) ;
2044 read_unlock_irqrestore(&sg_index_lock, iflags);
2050 sg_add_sfp(Sg_device * sdp, int dev)
2053 unsigned long iflags;
2056 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2060 init_waitqueue_head(&sfp->read_wait);
2061 rwlock_init(&sfp->rq_list_lock);
2063 sfp->timeout = SG_DEFAULT_TIMEOUT;
2064 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2065 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2066 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2067 sdp->device->host->unchecked_isa_dma : 1;
2068 sfp->cmd_q = SG_DEF_COMMAND_Q;
2069 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2070 sfp->parentdp = sdp;
2071 write_lock_irqsave(&sg_index_lock, iflags);
2074 else { /* add to tail of existing list */
2075 Sg_fd *pfp = sdp->headfp;
2080 write_unlock_irqrestore(&sg_index_lock, iflags);
2081 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2082 if (unlikely(sg_big_buff != def_reserved_size))
2083 sg_big_buff = def_reserved_size;
2085 bufflen = min_t(int, sg_big_buff,
2086 sdp->device->request_queue->max_sectors * 512);
2087 sg_build_reserve(sfp, bufflen);
2088 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2089 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2094 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2099 prev_fp = sdp->headfp;
2101 sdp->headfp = prev_fp->nextfp;
2103 while ((fp = prev_fp->nextfp)) {
2105 prev_fp->nextfp = fp->nextfp;
2111 if (sfp->reserve.bufflen > 0) {
2113 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2114 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2115 sg_remove_scat(&sfp->reserve);
2117 sfp->parentdp = NULL;
2118 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2122 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2124 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2131 for (srp = sfp->headrp; srp; srp = tsrp) {
2133 if (sg_srp_done(srp, sfp))
2134 sg_finish_rem_req(srp);
2139 unsigned long iflags;
2141 write_lock_irqsave(&sg_index_lock, iflags);
2142 __sg_remove_sfp(sdp, sfp);
2143 if (sdp->detached && (NULL == sdp->headfp)) {
2144 idr_remove(&sg_index_idr, sdp->index);
2148 write_unlock_irqrestore(&sg_index_lock, iflags);
2150 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2151 /* only bump the access_count if we actually succeeded in
2152 * throwing another counter on the host module */
2153 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2154 sfp->closed = 1; /* flag dirty state on this fd */
2155 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2162 sg_res_in_use(Sg_fd * sfp)
2164 const Sg_request *srp;
2165 unsigned long iflags;
2167 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2168 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2171 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2175 #ifdef CONFIG_SCSI_PROC_FS
2177 sg_idr_max_id(int id, void *p, void *data)
2191 unsigned long iflags;
2193 read_lock_irqsave(&sg_index_lock, iflags);
2194 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2195 read_unlock_irqrestore(&sg_index_lock, iflags);
2196 return k + 1; /* origin 1 */
2204 unsigned long iflags;
2206 read_lock_irqsave(&sg_index_lock, iflags);
2207 sdp = idr_find(&sg_index_idr, dev);
2208 read_unlock_irqrestore(&sg_index_lock, iflags);
2213 #ifdef CONFIG_SCSI_PROC_FS
2215 static struct proc_dir_entry *sg_proc_sgp = NULL;
2217 static char sg_proc_sg_dirname[] = "scsi/sg";
2219 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2221 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2222 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2223 size_t count, loff_t *off);
2224 static struct file_operations adio_fops = {
2225 /* .owner, .read and .llseek added in sg_proc_init() */
2226 .open = sg_proc_single_open_adio,
2227 .write = sg_proc_write_adio,
2228 .release = single_release,
2231 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2232 static ssize_t sg_proc_write_dressz(struct file *filp,
2233 const char __user *buffer, size_t count, loff_t *off);
2234 static struct file_operations dressz_fops = {
2235 .open = sg_proc_single_open_dressz,
2236 .write = sg_proc_write_dressz,
2237 .release = single_release,
2240 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2241 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2242 static struct file_operations version_fops = {
2243 .open = sg_proc_single_open_version,
2244 .release = single_release,
2247 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2248 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2249 static struct file_operations devhdr_fops = {
2250 .open = sg_proc_single_open_devhdr,
2251 .release = single_release,
2254 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2255 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2256 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2257 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2258 static void dev_seq_stop(struct seq_file *s, void *v);
2259 static struct file_operations dev_fops = {
2260 .open = sg_proc_open_dev,
2261 .release = seq_release,
2263 static struct seq_operations dev_seq_ops = {
2264 .start = dev_seq_start,
2265 .next = dev_seq_next,
2266 .stop = dev_seq_stop,
2267 .show = sg_proc_seq_show_dev,
2270 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2271 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2272 static struct file_operations devstrs_fops = {
2273 .open = sg_proc_open_devstrs,
2274 .release = seq_release,
2276 static struct seq_operations devstrs_seq_ops = {
2277 .start = dev_seq_start,
2278 .next = dev_seq_next,
2279 .stop = dev_seq_stop,
2280 .show = sg_proc_seq_show_devstrs,
2283 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2284 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2285 static struct file_operations debug_fops = {
2286 .open = sg_proc_open_debug,
2287 .release = seq_release,
2289 static struct seq_operations debug_seq_ops = {
2290 .start = dev_seq_start,
2291 .next = dev_seq_next,
2292 .stop = dev_seq_stop,
2293 .show = sg_proc_seq_show_debug,
2297 struct sg_proc_leaf {
2299 struct file_operations * fops;
2302 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2303 {"allow_dio", &adio_fops},
2304 {"debug", &debug_fops},
2305 {"def_reserved_size", &dressz_fops},
2306 {"device_hdr", &devhdr_fops},
2307 {"devices", &dev_fops},
2308 {"device_strs", &devstrs_fops},
2309 {"version", &version_fops}
2316 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2317 struct sg_proc_leaf * leaf;
2319 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2322 for (k = 0; k < num_leaves; ++k) {
2323 leaf = &sg_proc_leaf_arr[k];
2324 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2325 leaf->fops->owner = THIS_MODULE;
2326 leaf->fops->read = seq_read;
2327 leaf->fops->llseek = seq_lseek;
2328 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
2334 sg_proc_cleanup(void)
2337 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2341 for (k = 0; k < num_leaves; ++k)
2342 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2343 remove_proc_entry(sg_proc_sg_dirname, NULL);
2347 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2349 seq_printf(s, "%d\n", *((int *)s->private));
2353 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2355 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2359 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2360 size_t count, loff_t *off)
2365 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2367 num = (count < 10) ? count : 10;
2368 if (copy_from_user(buff, buffer, num))
2371 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2375 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2377 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2381 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2382 size_t count, loff_t *off)
2385 unsigned long k = ULONG_MAX;
2388 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2390 num = (count < 10) ? count : 10;
2391 if (copy_from_user(buff, buffer, num))
2394 k = simple_strtoul(buff, NULL, 10);
2395 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2402 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2404 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2409 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2411 return single_open(file, sg_proc_seq_show_version, NULL);
2414 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2416 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2421 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2423 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2426 struct sg_proc_deviter {
2431 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2433 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2440 it->max = sg_last_dev();
2441 if (it->index >= it->max)
2446 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2448 struct sg_proc_deviter * it = s->private;
2451 return (it->index < it->max) ? it : NULL;
2454 static void dev_seq_stop(struct seq_file *s, void *v)
2459 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2461 return seq_open(file, &dev_seq_ops);
2464 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2466 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2468 struct scsi_device *scsidp;
2470 sdp = it ? sg_get_dev(it->index) : NULL;
2471 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2472 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2473 scsidp->host->host_no, scsidp->channel,
2474 scsidp->id, scsidp->lun, (int) scsidp->type,
2476 (int) scsidp->queue_depth,
2477 (int) scsidp->device_busy,
2478 (int) scsi_device_online(scsidp));
2480 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2484 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2486 return seq_open(file, &devstrs_seq_ops);
2489 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2491 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2493 struct scsi_device *scsidp;
2495 sdp = it ? sg_get_dev(it->index) : NULL;
2496 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2497 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2498 scsidp->vendor, scsidp->model, scsidp->rev);
2500 seq_printf(s, "<no active device>\n");
2504 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2506 int k, m, new_interface, blen, usg;
2509 const sg_io_hdr_t *hp;
2513 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2514 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2515 "(res)sgat=%d low_dma=%d\n", k + 1,
2516 jiffies_to_msecs(fp->timeout),
2517 fp->reserve.bufflen,
2518 (int) fp->reserve.k_use_sg,
2520 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2521 (int) fp->cmd_q, (int) fp->force_packid,
2522 (int) fp->keep_orphan, (int) fp->closed);
2523 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2525 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2526 if (srp->res_used) {
2527 if (new_interface &&
2528 (SG_FLAG_MMAP_IO & hp->flags))
2533 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2539 blen = srp->data.bufflen;
2540 usg = srp->data.k_use_sg;
2541 seq_printf(s, srp->done ?
2542 ((1 == srp->done) ? "rcv:" : "fin:")
2544 seq_printf(s, " id=%d blen=%d",
2545 srp->header.pack_id, blen);
2547 seq_printf(s, " dur=%d", hp->duration);
2549 ms = jiffies_to_msecs(jiffies);
2550 seq_printf(s, " t_o/elap=%d/%d",
2551 (new_interface ? hp->timeout :
2552 jiffies_to_msecs(fp->timeout)),
2553 (ms > hp->duration ? ms - hp->duration : 0));
2555 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2556 (int) srp->data.cmd_opcode);
2559 seq_printf(s, " No requests active\n");
2563 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2565 return seq_open(file, &debug_seq_ops);
2568 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2570 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2573 if (it && (0 == it->index)) {
2574 seq_printf(s, "max_active_device=%d(origin 1)\n",
2576 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2578 sdp = it ? sg_get_dev(it->index) : NULL;
2580 struct scsi_device *scsidp = sdp->device;
2582 if (NULL == scsidp) {
2583 seq_printf(s, "device %d detached ??\n",
2588 if (sg_get_nth_sfp(sdp, 0)) {
2589 seq_printf(s, " >>> device=%s ",
2590 sdp->disk->disk_name);
2592 seq_printf(s, "detached pending close ");
2595 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2596 scsidp->host->host_no,
2597 scsidp->channel, scsidp->id,
2599 scsidp->host->hostt->emulated);
2600 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2601 sdp->sg_tablesize, sdp->exclude);
2603 sg_proc_debug_helper(s, sdp);
2608 #endif /* CONFIG_SCSI_PROC_FS */
2610 module_init(init_sg);
2611 module_exit(exit_sg);