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 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
331 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
332 if (!sdp->detached) {
333 scsi_device_put(sdp->device);
336 wake_up_interruptible(&sdp->o_excl_wait);
342 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
347 int req_pack_id = -1;
349 struct sg_header *old_hdr = NULL;
352 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
354 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
355 sdp->disk->disk_name, (int) count));
357 if (!access_ok(VERIFY_WRITE, buf, count))
359 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
360 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
363 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
367 if (old_hdr->reply_len < 0) {
368 if (count >= SZ_SG_IO_HDR) {
369 sg_io_hdr_t *new_hdr;
370 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
375 retval =__copy_from_user
376 (new_hdr, buf, SZ_SG_IO_HDR);
377 req_pack_id = new_hdr->pack_id;
385 req_pack_id = old_hdr->pack_id;
387 srp = sg_get_rq_mark(sfp, req_pack_id);
388 if (!srp) { /* now wait on packet to arrive */
393 if (filp->f_flags & O_NONBLOCK) {
398 retval = 0; /* following macro beats race condition */
399 __wait_event_interruptible(sfp->read_wait,
401 (srp = sg_get_rq_mark(sfp, req_pack_id))),
410 /* -ERESTARTSYS as signal hit process */
414 if (srp->header.interface_id != '\0') {
415 retval = sg_new_read(sfp, buf, count, srp);
420 if (old_hdr == NULL) {
421 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
427 memset(old_hdr, 0, SZ_SG_HEADER);
428 old_hdr->reply_len = (int) hp->timeout;
429 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
430 old_hdr->pack_id = hp->pack_id;
431 old_hdr->twelve_byte =
432 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
433 old_hdr->target_status = hp->masked_status;
434 old_hdr->host_status = hp->host_status;
435 old_hdr->driver_status = hp->driver_status;
436 if ((CHECK_CONDITION & hp->masked_status) ||
437 (DRIVER_SENSE & hp->driver_status))
438 memcpy(old_hdr->sense_buffer, srp->sense_b,
439 sizeof (old_hdr->sense_buffer));
440 switch (hp->host_status) {
441 /* This setup of 'result' is for backward compatibility and is best
442 ignored by the user who should use target, host + driver status */
444 case DID_PASSTHROUGH:
451 old_hdr->result = EBUSY;
458 old_hdr->result = EIO;
461 old_hdr->result = (srp->sense_b[0] == 0 &&
462 hp->masked_status == GOOD) ? 0 : EIO;
465 old_hdr->result = EIO;
469 /* Now copy the result back to the user buffer. */
470 if (count >= SZ_SG_HEADER) {
471 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
476 if (count > old_hdr->reply_len)
477 count = old_hdr->reply_len;
478 if (count > SZ_SG_HEADER) {
479 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
485 count = (old_hdr->result == 0) ? 0 : -EIO;
486 sg_finish_rem_req(srp);
494 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
496 sg_io_hdr_t *hp = &srp->header;
500 if (count < SZ_SG_IO_HDR) {
505 if ((hp->mx_sb_len > 0) && hp->sbp) {
506 if ((CHECK_CONDITION & hp->masked_status) ||
507 (DRIVER_SENSE & hp->driver_status)) {
508 int sb_len = SCSI_SENSE_BUFFERSIZE;
509 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
510 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
511 len = (len > sb_len) ? sb_len : len;
512 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
519 if (hp->masked_status || hp->host_status || hp->driver_status)
520 hp->info |= SG_INFO_CHECK;
521 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
526 sg_finish_rem_req(srp);
527 return (0 == err) ? count : err;
531 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
533 int mxsize, cmd_size, k;
534 int input_size, blocking;
535 unsigned char opcode;
539 struct sg_header old_hdr;
541 unsigned char cmnd[MAX_COMMAND_SIZE];
543 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
545 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
546 sdp->disk->disk_name, (int) count));
549 if (!((filp->f_flags & O_NONBLOCK) ||
550 scsi_block_when_processing_errors(sdp->device)))
553 if (!access_ok(VERIFY_READ, buf, count))
554 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
555 if (count < SZ_SG_HEADER)
557 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
559 blocking = !(filp->f_flags & O_NONBLOCK);
560 if (old_hdr.reply_len < 0)
561 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
562 if (count < (SZ_SG_HEADER + 6))
563 return -EIO; /* The minimum scsi command length is 6 bytes. */
565 if (!(srp = sg_add_request(sfp))) {
566 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
570 __get_user(opcode, buf);
571 if (sfp->next_cmd_len > 0) {
572 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
573 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
574 sfp->next_cmd_len = 0;
575 sg_remove_request(sfp, srp);
578 cmd_size = sfp->next_cmd_len;
579 sfp->next_cmd_len = 0; /* reset so only this write() effected */
581 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
582 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
585 SCSI_LOG_TIMEOUT(4, printk(
586 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
587 /* Determine buffer size. */
588 input_size = count - cmd_size;
589 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
590 mxsize -= SZ_SG_HEADER;
591 input_size -= SZ_SG_HEADER;
592 if (input_size < 0) {
593 sg_remove_request(sfp, srp);
594 return -EIO; /* User did not pass enough bytes for this command. */
597 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
598 hp->cmd_len = (unsigned char) cmd_size;
602 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
603 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
605 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
606 hp->dxfer_len = mxsize;
607 if (hp->dxfer_direction == SG_DXFER_TO_DEV)
608 hp->dxferp = (char __user *)buf + cmd_size;
612 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
613 hp->flags = input_size; /* structure abuse ... */
614 hp->pack_id = old_hdr.pack_id;
616 if (__copy_from_user(cmnd, buf, cmd_size))
619 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
620 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
621 * is a non-zero input_size, so emit a warning.
623 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
624 static char cmd[TASK_COMM_LEN];
625 if (strcmp(current->comm, cmd) && printk_ratelimit()) {
627 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
628 "guessing data in;\n" KERN_WARNING " "
629 "program %s not setting count and/or reply_len properly\n",
630 old_hdr.reply_len - (int)SZ_SG_HEADER,
631 input_size, (unsigned int) cmnd[0],
633 strcpy(cmd, current->comm);
636 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
637 return (k < 0) ? k : count;
641 sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
642 size_t count, int blocking, int read_only,
648 unsigned char cmnd[MAX_COMMAND_SIZE];
650 unsigned long ul_timeout;
652 if (count < SZ_SG_IO_HDR)
654 if (!access_ok(VERIFY_READ, buf, count))
655 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
657 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
658 if (!(srp = sg_add_request(sfp))) {
659 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
663 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
664 sg_remove_request(sfp, srp);
667 if (hp->interface_id != 'S') {
668 sg_remove_request(sfp, srp);
671 if (hp->flags & SG_FLAG_MMAP_IO) {
672 if (hp->dxfer_len > sfp->reserve.bufflen) {
673 sg_remove_request(sfp, srp);
674 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
676 if (hp->flags & SG_FLAG_DIRECT_IO) {
677 sg_remove_request(sfp, srp);
678 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
680 if (sg_res_in_use(sfp)) {
681 sg_remove_request(sfp, srp);
682 return -EBUSY; /* reserve buffer already being used */
685 ul_timeout = msecs_to_jiffies(srp->header.timeout);
686 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
687 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
688 sg_remove_request(sfp, srp);
691 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
692 sg_remove_request(sfp, srp);
693 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
695 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
696 sg_remove_request(sfp, srp);
699 if (read_only && sg_allow_access(file, cmnd)) {
700 sg_remove_request(sfp, srp);
703 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
712 sg_common_write(Sg_fd * sfp, Sg_request * srp,
713 unsigned char *cmnd, int timeout, int blocking)
716 Sg_device *sdp = sfp->parentdp;
717 sg_io_hdr_t *hp = &srp->header;
719 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
721 hp->masked_status = 0;
725 hp->driver_status = 0;
727 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
728 (int) cmnd[0], (int) hp->cmd_len));
730 k = sg_start_req(srp, cmnd);
732 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
733 sg_finish_rem_req(srp);
734 return k; /* probably out of space --> ENOMEM */
737 sg_finish_rem_req(srp);
741 switch (hp->dxfer_direction) {
742 case SG_DXFER_TO_FROM_DEV:
743 case SG_DXFER_FROM_DEV:
744 data_dir = DMA_FROM_DEVICE;
746 case SG_DXFER_TO_DEV:
747 data_dir = DMA_TO_DEVICE;
749 case SG_DXFER_UNKNOWN:
750 data_dir = DMA_BIDIRECTIONAL;
756 hp->duration = jiffies_to_msecs(jiffies);
758 srp->rq->timeout = timeout;
759 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
760 srp->rq, 1, sg_rq_end_io);
765 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
767 unsigned long iflags;
770 read_lock_irqsave(&sfp->rq_list_lock, iflags);
772 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
777 sg_ioctl(struct inode *inode, struct file *filp,
778 unsigned int cmd_in, unsigned long arg)
780 void __user *p = (void __user *)arg;
782 int result, val, read_only;
786 unsigned long iflags;
788 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
791 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
792 sdp->disk->disk_name, (int) cmd_in));
793 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
798 int blocking = 1; /* ignore O_NONBLOCK flag */
802 if (!scsi_block_when_processing_errors(sdp->device))
804 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
807 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
808 blocking, read_only, &srp);
811 srp->sg_io_owned = 1;
813 result = 0; /* following macro to beat race condition */
814 __wait_event_interruptible(sfp->read_wait,
815 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
820 return 0; /* request packet dropped already */
824 return result; /* -ERESTARTSYS because signal hit process */
826 write_lock_irqsave(&sfp->rq_list_lock, iflags);
828 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
829 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
830 return (result < 0) ? result : 0;
833 result = get_user(val, ip);
838 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
839 val = MULDIV (INT_MAX, USER_HZ, HZ);
840 sfp->timeout_user = val;
841 sfp->timeout = MULDIV (val, HZ, USER_HZ);
844 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
845 /* strange ..., for backward compatibility */
846 return sfp->timeout_user;
847 case SG_SET_FORCE_LOW_DMA:
848 result = get_user(val, ip);
853 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
854 val = (int) sfp->reserve.bufflen;
855 sg_remove_scat(&sfp->reserve);
856 sg_build_reserve(sfp, val);
861 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
865 return put_user((int) sfp->low_dma, ip);
867 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
870 sg_scsi_id_t __user *sg_idp = p;
874 __put_user((int) sdp->device->host->host_no,
876 __put_user((int) sdp->device->channel,
878 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
879 __put_user((int) sdp->device->lun, &sg_idp->lun);
880 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
881 __put_user((short) sdp->device->host->cmd_per_lun,
882 &sg_idp->h_cmd_per_lun);
883 __put_user((short) sdp->device->queue_depth,
884 &sg_idp->d_queue_depth);
885 __put_user(0, &sg_idp->unused[0]);
886 __put_user(0, &sg_idp->unused[1]);
889 case SG_SET_FORCE_PACK_ID:
890 result = get_user(val, ip);
893 sfp->force_packid = val ? 1 : 0;
896 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
898 read_lock_irqsave(&sfp->rq_list_lock, iflags);
899 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
900 if ((1 == srp->done) && (!srp->sg_io_owned)) {
901 read_unlock_irqrestore(&sfp->rq_list_lock,
903 __put_user(srp->header.pack_id, ip);
907 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
910 case SG_GET_NUM_WAITING:
911 read_lock_irqsave(&sfp->rq_list_lock, iflags);
912 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
913 if ((1 == srp->done) && (!srp->sg_io_owned))
916 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
917 return put_user(val, ip);
918 case SG_GET_SG_TABLESIZE:
919 return put_user(sdp->sg_tablesize, ip);
920 case SG_SET_RESERVED_SIZE:
921 result = get_user(val, ip);
926 val = min_t(int, val,
927 sdp->device->request_queue->max_sectors * 512);
928 if (val != sfp->reserve.bufflen) {
929 if (sg_res_in_use(sfp) || sfp->mmap_called)
931 sg_remove_scat(&sfp->reserve);
932 sg_build_reserve(sfp, val);
935 case SG_GET_RESERVED_SIZE:
936 val = min_t(int, sfp->reserve.bufflen,
937 sdp->device->request_queue->max_sectors * 512);
938 return put_user(val, ip);
939 case SG_SET_COMMAND_Q:
940 result = get_user(val, ip);
943 sfp->cmd_q = val ? 1 : 0;
945 case SG_GET_COMMAND_Q:
946 return put_user((int) sfp->cmd_q, ip);
947 case SG_SET_KEEP_ORPHAN:
948 result = get_user(val, ip);
951 sfp->keep_orphan = val;
953 case SG_GET_KEEP_ORPHAN:
954 return put_user((int) sfp->keep_orphan, ip);
955 case SG_NEXT_CMD_LEN:
956 result = get_user(val, ip);
959 sfp->next_cmd_len = (val > 0) ? val : 0;
961 case SG_GET_VERSION_NUM:
962 return put_user(sg_version_num, ip);
963 case SG_GET_ACCESS_COUNT:
964 /* faked - we don't have a real access count anymore */
965 val = (sdp->device ? 1 : 0);
966 return put_user(val, ip);
967 case SG_GET_REQUEST_TABLE:
968 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
971 sg_req_info_t *rinfo;
974 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
978 read_lock_irqsave(&sfp->rq_list_lock, iflags);
979 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
980 ++val, srp = srp ? srp->nextrp : srp) {
981 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
983 rinfo[val].req_state = srp->done + 1;
985 srp->header.masked_status &
986 srp->header.host_status &
987 srp->header.driver_status;
989 rinfo[val].duration =
990 srp->header.duration;
992 ms = jiffies_to_msecs(jiffies);
993 rinfo[val].duration =
994 (ms > srp->header.duration) ?
995 (ms - srp->header.duration) : 0;
997 rinfo[val].orphan = srp->orphan;
998 rinfo[val].sg_io_owned =
1000 rinfo[val].pack_id =
1001 srp->header.pack_id;
1002 rinfo[val].usr_ptr =
1003 srp->header.usr_ptr;
1006 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1007 result = __copy_to_user(p, rinfo,
1008 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1009 result = result ? -EFAULT : 0;
1013 case SG_EMULATED_HOST:
1016 return put_user(sdp->device->host->hostt->emulated, ip);
1020 if (filp->f_flags & O_NONBLOCK) {
1021 if (scsi_host_in_recovery(sdp->device->host))
1023 } else if (!scsi_block_when_processing_errors(sdp->device))
1025 result = get_user(val, ip);
1028 if (SG_SCSI_RESET_NOTHING == val)
1031 case SG_SCSI_RESET_DEVICE:
1032 val = SCSI_TRY_RESET_DEVICE;
1034 case SG_SCSI_RESET_TARGET:
1035 val = SCSI_TRY_RESET_TARGET;
1037 case SG_SCSI_RESET_BUS:
1038 val = SCSI_TRY_RESET_BUS;
1040 case SG_SCSI_RESET_HOST:
1041 val = SCSI_TRY_RESET_HOST;
1046 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1048 return (scsi_reset_provider(sdp->device, val) ==
1049 SUCCESS) ? 0 : -EIO;
1050 case SCSI_IOCTL_SEND_COMMAND:
1054 unsigned char opcode = WRITE_6;
1055 Scsi_Ioctl_Command __user *siocp = p;
1057 if (copy_from_user(&opcode, siocp->data, 1))
1059 if (sg_allow_access(filp, &opcode))
1062 return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
1064 result = get_user(val, ip);
1067 sdp->sgdebug = (char) val;
1069 case SCSI_IOCTL_GET_IDLUN:
1070 case SCSI_IOCTL_GET_BUS_NUMBER:
1071 case SCSI_IOCTL_PROBE_HOST:
1072 case SG_GET_TRANSFORM:
1075 return scsi_ioctl(sdp->device, cmd_in, p);
1077 return put_user(sdp->device->request_queue->max_sectors * 512,
1080 return blk_trace_setup(sdp->device->request_queue,
1081 sdp->disk->disk_name,
1082 sdp->device->sdev_gendev.devt,
1085 return blk_trace_startstop(sdp->device->request_queue, 1);
1087 return blk_trace_startstop(sdp->device->request_queue, 0);
1088 case BLKTRACETEARDOWN:
1089 return blk_trace_remove(sdp->device->request_queue);
1092 return -EPERM; /* don't know so take safe approach */
1093 return scsi_ioctl(sdp->device, cmd_in, p);
1097 #ifdef CONFIG_COMPAT
1098 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1102 struct scsi_device *sdev;
1104 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1108 if (sdev->host->hostt->compat_ioctl) {
1111 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1116 return -ENOIOCTLCMD;
1121 sg_poll(struct file *filp, poll_table * wait)
1123 unsigned int res = 0;
1128 unsigned long iflags;
1130 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1133 poll_wait(filp, &sfp->read_wait, wait);
1134 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1135 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1136 /* if any read waiting, flag it */
1137 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1138 res = POLLIN | POLLRDNORM;
1141 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1145 else if (!sfp->cmd_q) {
1147 res |= POLLOUT | POLLWRNORM;
1148 } else if (count < SG_MAX_QUEUE)
1149 res |= POLLOUT | POLLWRNORM;
1150 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1151 sdp->disk->disk_name, (int) res));
1156 sg_fasync(int fd, struct file *filp, int mode)
1162 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1164 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1165 sdp->disk->disk_name, mode));
1167 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1168 return (retval < 0) ? retval : 0;
1172 sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1175 unsigned long offset, len, sa;
1176 Sg_scatter_hold *rsv_schp;
1179 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1180 return VM_FAULT_SIGBUS;
1181 rsv_schp = &sfp->reserve;
1182 offset = vmf->pgoff << PAGE_SHIFT;
1183 if (offset >= rsv_schp->bufflen)
1184 return VM_FAULT_SIGBUS;
1185 SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
1186 offset, rsv_schp->k_use_sg));
1188 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1189 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
1190 len = vma->vm_end - sa;
1191 len = (len < length) ? len : length;
1193 struct page *page = nth_page(rsv_schp->pages[k],
1194 offset >> PAGE_SHIFT);
1195 get_page(page); /* increment page count */
1197 return 0; /* success */
1203 return VM_FAULT_SIGBUS;
1206 static struct vm_operations_struct sg_mmap_vm_ops = {
1207 .fault = sg_vma_fault,
1211 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1214 unsigned long req_sz, len, sa;
1215 Sg_scatter_hold *rsv_schp;
1218 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1220 req_sz = vma->vm_end - vma->vm_start;
1221 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1222 (void *) vma->vm_start, (int) req_sz));
1224 return -EINVAL; /* want no offset */
1225 rsv_schp = &sfp->reserve;
1226 if (req_sz > rsv_schp->bufflen)
1227 return -ENOMEM; /* cannot map more than reserved buffer */
1230 length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1231 for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
1232 len = vma->vm_end - sa;
1233 len = (len < length) ? len : length;
1237 sfp->mmap_called = 1;
1238 vma->vm_flags |= VM_RESERVED;
1239 vma->vm_private_data = sfp;
1240 vma->vm_ops = &sg_mmap_vm_ops;
1245 * This function is a "bottom half" handler that is called by the mid
1246 * level when a command is completed (or has failed).
1248 static void sg_rq_end_io(struct request *rq, int uptodate)
1250 struct sg_request *srp = rq->end_io_data;
1251 Sg_device *sdp = NULL;
1253 unsigned long iflags;
1259 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1262 sfp = srp->parentfp;
1264 sdp = sfp->parentdp;
1265 if ((NULL == sdp) || sdp->detached) {
1266 printk(KERN_INFO "sg_cmd_done: device detached\n");
1271 result = rq->errors;
1272 resid = rq->data_len;
1274 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1275 sdp->disk->disk_name, srp->header.pack_id, result));
1276 srp->header.resid = resid;
1277 ms = jiffies_to_msecs(jiffies);
1278 srp->header.duration = (ms > srp->header.duration) ?
1279 (ms - srp->header.duration) : 0;
1281 struct scsi_sense_hdr sshdr;
1283 srp->header.status = 0xff & result;
1284 srp->header.masked_status = status_byte(result);
1285 srp->header.msg_status = msg_byte(result);
1286 srp->header.host_status = host_byte(result);
1287 srp->header.driver_status = driver_byte(result);
1288 if ((sdp->sgdebug > 0) &&
1289 ((CHECK_CONDITION == srp->header.masked_status) ||
1290 (COMMAND_TERMINATED == srp->header.masked_status)))
1291 __scsi_print_sense("sg_cmd_done", sense,
1292 SCSI_SENSE_BUFFERSIZE);
1294 /* Following if statement is a patch supplied by Eric Youngdale */
1295 if (driver_byte(result) != 0
1296 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1297 && !scsi_sense_is_deferred(&sshdr)
1298 && sshdr.sense_key == UNIT_ATTENTION
1299 && sdp->device->removable) {
1300 /* Detected possible disc change. Set the bit - this */
1301 /* may be used if there are filesystems using this device */
1302 sdp->device->changed = 1;
1305 /* Rely on write phase to clean out srp status values, so no "else" */
1307 if (sfp->closed) { /* whoops this fd already released, cleanup */
1308 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1309 sg_finish_rem_req(srp);
1311 if (NULL == sfp->headrp) {
1312 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1313 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1314 scsi_device_put(sdp->device);
1318 } else if (srp && srp->orphan) {
1319 if (sfp->keep_orphan)
1320 srp->sg_io_owned = 0;
1322 sg_finish_rem_req(srp);
1327 /* Now wake up any sg_read() that is waiting for this packet. */
1328 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1329 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1331 wake_up_interruptible(&sfp->read_wait);
1332 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1336 static struct file_operations sg_fops = {
1337 .owner = THIS_MODULE,
1342 #ifdef CONFIG_COMPAT
1343 .compat_ioctl = sg_compat_ioctl,
1347 .release = sg_release,
1348 .fasync = sg_fasync,
1351 static struct class *sg_sysfs_class;
1353 static int sg_sysfs_valid = 0;
1355 static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1357 struct request_queue *q = scsidp->request_queue;
1359 unsigned long iflags;
1363 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1365 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1366 return ERR_PTR(-ENOMEM);
1369 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1370 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1374 write_lock_irqsave(&sg_index_lock, iflags);
1375 error = idr_get_new(&sg_index_idr, sdp, &k);
1376 write_unlock_irqrestore(&sg_index_lock, iflags);
1379 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1384 if (unlikely(k >= SG_MAX_DEVS))
1387 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1388 sprintf(disk->disk_name, "sg%d", k);
1389 disk->first_minor = k;
1391 sdp->device = scsidp;
1392 init_waitqueue_head(&sdp->o_excl_wait);
1393 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1400 return ERR_PTR(error);
1405 sdev_printk(KERN_WARNING, scsidp,
1406 "Unable to attach sg device type=%d, minor "
1407 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1413 sg_add(struct device *cl_dev, struct class_interface *cl_intf)
1415 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1416 struct gendisk *disk;
1417 Sg_device *sdp = NULL;
1418 struct cdev * cdev = NULL;
1420 unsigned long iflags;
1422 disk = alloc_disk(1);
1424 printk(KERN_WARNING "alloc_disk failed\n");
1427 disk->major = SCSI_GENERIC_MAJOR;
1430 cdev = cdev_alloc();
1432 printk(KERN_WARNING "cdev_alloc failed\n");
1435 cdev->owner = THIS_MODULE;
1436 cdev->ops = &sg_fops;
1438 sdp = sg_alloc(disk, scsidp);
1440 printk(KERN_WARNING "sg_alloc failed\n");
1441 error = PTR_ERR(sdp);
1445 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
1450 if (sg_sysfs_valid) {
1451 struct device *sg_class_member;
1453 sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1454 MKDEV(SCSI_GENERIC_MAJOR,
1456 sdp, "%s", disk->disk_name);
1457 if (IS_ERR(sg_class_member)) {
1458 printk(KERN_ERR "sg_add: "
1459 "device_create failed\n");
1460 error = PTR_ERR(sg_class_member);
1463 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1464 &sg_class_member->kobj, "generic");
1466 printk(KERN_ERR "sg_add: unable to make symlink "
1467 "'generic' back to sg%d\n", sdp->index);
1469 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
1471 sdev_printk(KERN_NOTICE, scsidp,
1472 "Attached scsi generic sg%d type %d\n", sdp->index,
1475 dev_set_drvdata(cl_dev, sdp);
1480 write_lock_irqsave(&sg_index_lock, iflags);
1481 idr_remove(&sg_index_idr, sdp->index);
1482 write_unlock_irqrestore(&sg_index_lock, iflags);
1493 sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
1495 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1496 Sg_device *sdp = dev_get_drvdata(cl_dev);
1497 unsigned long iflags;
1508 write_lock_irqsave(&sg_index_lock, iflags);
1511 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1513 for (srp = sfp->headrp; srp; srp = tsrp) {
1515 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1516 sg_finish_rem_req(srp);
1519 scsi_device_put(sdp->device);
1520 __sg_remove_sfp(sdp, sfp);
1523 wake_up_interruptible(&sfp->read_wait);
1524 kill_fasync(&sfp->async_qp, SIGPOLL,
1528 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1529 if (NULL == sdp->headfp) {
1530 idr_remove(&sg_index_idr, sdp->index);
1532 } else { /* nothing active, simple case */
1533 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1534 idr_remove(&sg_index_idr, sdp->index);
1536 write_unlock_irqrestore(&sg_index_lock, iflags);
1538 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1539 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1540 cdev_del(sdp->cdev);
1542 put_disk(sdp->disk);
1544 if (NULL == sdp->headfp)
1548 msleep(10); /* dirty detach so delay device destruction */
1551 module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1552 module_param_named(def_reserved_size, def_reserved_size, int,
1554 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1556 MODULE_AUTHOR("Douglas Gilbert");
1557 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1558 MODULE_LICENSE("GPL");
1559 MODULE_VERSION(SG_VERSION_STR);
1560 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
1562 MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1563 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1564 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1565 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1572 if (scatter_elem_sz < PAGE_SIZE) {
1573 scatter_elem_sz = PAGE_SIZE;
1574 scatter_elem_sz_prev = scatter_elem_sz;
1576 if (def_reserved_size >= 0)
1577 sg_big_buff = def_reserved_size;
1579 def_reserved_size = sg_big_buff;
1581 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1585 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1586 if ( IS_ERR(sg_sysfs_class) ) {
1587 rc = PTR_ERR(sg_sysfs_class);
1591 rc = scsi_register_interface(&sg_interface);
1593 #ifdef CONFIG_SCSI_PROC_FS
1595 #endif /* CONFIG_SCSI_PROC_FS */
1598 class_destroy(sg_sysfs_class);
1600 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1607 #ifdef CONFIG_SCSI_PROC_FS
1609 #endif /* CONFIG_SCSI_PROC_FS */
1610 scsi_unregister_interface(&sg_interface);
1611 class_destroy(sg_sysfs_class);
1613 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1615 idr_destroy(&sg_index_idr);
1618 static int sg_start_req(Sg_request *srp, unsigned char *cmd)
1622 Sg_fd *sfp = srp->parentfp;
1623 sg_io_hdr_t *hp = &srp->header;
1624 int dxfer_len = (int) hp->dxfer_len;
1625 int dxfer_dir = hp->dxfer_direction;
1626 unsigned int iov_count = hp->iovec_count;
1627 Sg_scatter_hold *req_schp = &srp->data;
1628 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1629 struct request_queue *q = sfp->parentdp->device->request_queue;
1630 struct rq_map_data *md, map_data;
1631 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
1633 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1636 rq = blk_get_request(q, rw, GFP_ATOMIC);
1640 memcpy(rq->cmd, cmd, hp->cmd_len);
1642 rq->cmd_len = hp->cmd_len;
1643 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1646 rq->end_io_data = srp;
1647 rq->sense = srp->sense_b;
1648 rq->retries = SG_DEFAULT_RETRIES;
1650 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1653 if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1654 dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1655 !sfp->parentdp->device->host->unchecked_isa_dma &&
1656 blk_rq_aligned(q, hp->dxferp, dxfer_len))
1662 if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
1663 sg_link_reserve(sfp, srp, dxfer_len);
1665 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1670 md->pages = req_schp->pages;
1671 md->page_order = req_schp->page_order;
1672 md->nr_entries = req_schp->k_use_sg;
1676 res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1677 hp->dxfer_len, GFP_ATOMIC);
1679 res = blk_rq_map_user(q, rq, md, hp->dxferp,
1680 hp->dxfer_len, GFP_ATOMIC);
1686 req_schp->dio_in_use = 1;
1687 hp->info |= SG_INFO_DIRECT_IO;
1694 sg_finish_rem_req(Sg_request * srp)
1696 Sg_fd *sfp = srp->parentfp;
1697 Sg_scatter_hold *req_schp = &srp->data;
1699 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1701 sg_unlink_reserve(sfp, srp);
1703 sg_remove_scat(req_schp);
1707 blk_rq_unmap_user(srp->bio);
1709 blk_put_request(srp->rq);
1712 sg_remove_request(sfp, srp);
1716 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1718 int sg_bufflen = tablesize * sizeof(struct page *);
1719 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1721 schp->pages = kzalloc(sg_bufflen, gfp_flags);
1724 schp->sglist_len = sg_bufflen;
1725 return tablesize; /* number of scat_gath elements allocated */
1729 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1731 int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
1732 int sg_tablesize = sfp->parentdp->sg_tablesize;
1733 int blk_size = buff_size, order;
1734 gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
1739 ++blk_size; /* don't know why */
1740 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1741 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1742 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1743 buff_size, blk_size));
1745 /* N.B. ret_sz carried into this block ... */
1746 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1747 if (mx_sc_elems < 0)
1748 return mx_sc_elems; /* most likely -ENOMEM */
1750 num = scatter_elem_sz;
1751 if (unlikely(num != scatter_elem_sz_prev)) {
1752 if (num < PAGE_SIZE) {
1753 scatter_elem_sz = PAGE_SIZE;
1754 scatter_elem_sz_prev = PAGE_SIZE;
1756 scatter_elem_sz_prev = num;
1760 gfp_mask |= GFP_DMA;
1762 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1763 gfp_mask |= __GFP_ZERO;
1765 order = get_order(num);
1767 ret_sz = 1 << (PAGE_SHIFT + order);
1769 for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
1770 k++, rem_sz -= ret_sz) {
1772 num = (rem_sz > scatter_elem_sz_prev) ?
1773 scatter_elem_sz_prev : rem_sz;
1775 schp->pages[k] = alloc_pages(gfp_mask, order);
1776 if (!schp->pages[k])
1779 if (num == scatter_elem_sz_prev) {
1780 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1781 scatter_elem_sz = ret_sz;
1782 scatter_elem_sz_prev = ret_sz;
1786 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1787 "ret_sz=%d\n", k, num, ret_sz));
1788 } /* end of for loop */
1790 schp->page_order = order;
1792 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1793 "rem_sz=%d\n", k, rem_sz));
1795 schp->bufflen = blk_size;
1796 if (rem_sz > 0) /* must have failed */
1800 for (i = 0; i < k; i++)
1801 __free_pages(schp->pages[k], order);
1810 sg_remove_scat(Sg_scatter_hold * schp)
1812 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1813 if (schp->pages && schp->sglist_len > 0) {
1814 if (!schp->dio_in_use) {
1817 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
1818 SCSI_LOG_TIMEOUT(5, printk(
1819 "sg_remove_scat: k=%d, pg=0x%p\n",
1820 k, schp->pages[k]));
1821 __free_pages(schp->pages[k], schp->page_order);
1827 memset(schp, 0, sizeof (*schp));
1831 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
1833 Sg_scatter_hold *schp = &srp->data;
1836 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
1838 if ((!outp) || (num_read_xfer <= 0))
1841 num = 1 << (PAGE_SHIFT + schp->page_order);
1842 for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
1843 if (num > num_read_xfer) {
1844 if (__copy_to_user(outp, page_address(schp->pages[k]),
1849 if (__copy_to_user(outp, page_address(schp->pages[k]),
1852 num_read_xfer -= num;
1853 if (num_read_xfer <= 0)
1863 sg_build_reserve(Sg_fd * sfp, int req_size)
1865 Sg_scatter_hold *schp = &sfp->reserve;
1867 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
1869 if (req_size < PAGE_SIZE)
1870 req_size = PAGE_SIZE;
1871 if (0 == sg_build_indirect(schp, sfp, req_size))
1874 sg_remove_scat(schp);
1875 req_size >>= 1; /* divide by 2 */
1876 } while (req_size > (PAGE_SIZE / 2));
1880 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
1882 Sg_scatter_hold *req_schp = &srp->data;
1883 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1887 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
1890 num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
1891 for (k = 0; k < rsv_schp->k_use_sg; k++) {
1893 req_schp->k_use_sg = k + 1;
1894 req_schp->sglist_len = rsv_schp->sglist_len;
1895 req_schp->pages = rsv_schp->pages;
1897 req_schp->bufflen = size;
1898 req_schp->page_order = rsv_schp->page_order;
1904 if (k >= rsv_schp->k_use_sg)
1905 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
1909 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
1911 Sg_scatter_hold *req_schp = &srp->data;
1913 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
1914 (int) req_schp->k_use_sg));
1915 req_schp->k_use_sg = 0;
1916 req_schp->bufflen = 0;
1917 req_schp->pages = NULL;
1918 req_schp->page_order = 0;
1919 req_schp->sglist_len = 0;
1920 sfp->save_scat_len = 0;
1925 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1928 unsigned long iflags;
1930 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1931 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
1932 /* look for requests that are ready + not SG_IO owned */
1933 if ((1 == resp->done) && (!resp->sg_io_owned) &&
1934 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
1935 resp->done = 2; /* guard against other readers */
1939 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1943 #ifdef CONFIG_SCSI_PROC_FS
1945 sg_get_nth_request(Sg_fd * sfp, int nth)
1948 unsigned long iflags;
1951 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1952 for (k = 0, resp = sfp->headrp; resp && (k < nth);
1953 ++k, resp = resp->nextrp) ;
1954 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1959 /* always adds to end of list */
1961 sg_add_request(Sg_fd * sfp)
1964 unsigned long iflags;
1966 Sg_request *rp = sfp->req_arr;
1968 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1971 memset(rp, 0, sizeof (Sg_request));
1976 if (0 == sfp->cmd_q)
1977 resp = NULL; /* command queuing disallowed */
1979 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
1983 if (k < SG_MAX_QUEUE) {
1984 memset(rp, 0, sizeof (Sg_request));
1986 while (resp->nextrp)
1987 resp = resp->nextrp;
1995 resp->nextrp = NULL;
1996 resp->header.duration = jiffies_to_msecs(jiffies);
1998 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2002 /* Return of 1 for found; 0 for not found */
2004 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2006 Sg_request *prev_rp;
2008 unsigned long iflags;
2011 if ((!sfp) || (!srp) || (!sfp->headrp))
2013 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2014 prev_rp = sfp->headrp;
2015 if (srp == prev_rp) {
2016 sfp->headrp = prev_rp->nextrp;
2017 prev_rp->parentfp = NULL;
2020 while ((rp = prev_rp->nextrp)) {
2022 prev_rp->nextrp = rp->nextrp;
2023 rp->parentfp = NULL;
2030 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2034 #ifdef CONFIG_SCSI_PROC_FS
2036 sg_get_nth_sfp(Sg_device * sdp, int nth)
2039 unsigned long iflags;
2042 read_lock_irqsave(&sg_index_lock, iflags);
2043 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2044 ++k, resp = resp->nextfp) ;
2045 read_unlock_irqrestore(&sg_index_lock, iflags);
2051 sg_add_sfp(Sg_device * sdp, int dev)
2054 unsigned long iflags;
2057 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2061 init_waitqueue_head(&sfp->read_wait);
2062 rwlock_init(&sfp->rq_list_lock);
2064 sfp->timeout = SG_DEFAULT_TIMEOUT;
2065 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2066 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2067 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2068 sdp->device->host->unchecked_isa_dma : 1;
2069 sfp->cmd_q = SG_DEF_COMMAND_Q;
2070 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2071 sfp->parentdp = sdp;
2072 write_lock_irqsave(&sg_index_lock, iflags);
2075 else { /* add to tail of existing list */
2076 Sg_fd *pfp = sdp->headfp;
2081 write_unlock_irqrestore(&sg_index_lock, iflags);
2082 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2083 if (unlikely(sg_big_buff != def_reserved_size))
2084 sg_big_buff = def_reserved_size;
2086 bufflen = min_t(int, sg_big_buff,
2087 sdp->device->request_queue->max_sectors * 512);
2088 sg_build_reserve(sfp, bufflen);
2089 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2090 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2095 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2100 prev_fp = sdp->headfp;
2102 sdp->headfp = prev_fp->nextfp;
2104 while ((fp = prev_fp->nextfp)) {
2106 prev_fp->nextfp = fp->nextfp;
2112 if (sfp->reserve.bufflen > 0) {
2114 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2115 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2116 sg_remove_scat(&sfp->reserve);
2118 sfp->parentdp = NULL;
2119 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2123 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2125 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2132 for (srp = sfp->headrp; srp; srp = tsrp) {
2134 if (sg_srp_done(srp, sfp))
2135 sg_finish_rem_req(srp);
2140 unsigned long iflags;
2142 write_lock_irqsave(&sg_index_lock, iflags);
2143 __sg_remove_sfp(sdp, sfp);
2144 if (sdp->detached && (NULL == sdp->headfp)) {
2145 idr_remove(&sg_index_idr, sdp->index);
2149 write_unlock_irqrestore(&sg_index_lock, iflags);
2151 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2152 /* only bump the access_count if we actually succeeded in
2153 * throwing another counter on the host module */
2154 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2155 sfp->closed = 1; /* flag dirty state on this fd */
2156 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2163 sg_res_in_use(Sg_fd * sfp)
2165 const Sg_request *srp;
2166 unsigned long iflags;
2168 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2169 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2172 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2176 #ifdef CONFIG_SCSI_PROC_FS
2178 sg_idr_max_id(int id, void *p, void *data)
2192 unsigned long iflags;
2194 read_lock_irqsave(&sg_index_lock, iflags);
2195 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2196 read_unlock_irqrestore(&sg_index_lock, iflags);
2197 return k + 1; /* origin 1 */
2205 unsigned long iflags;
2207 read_lock_irqsave(&sg_index_lock, iflags);
2208 sdp = idr_find(&sg_index_idr, dev);
2209 read_unlock_irqrestore(&sg_index_lock, iflags);
2214 #ifdef CONFIG_SCSI_PROC_FS
2216 static struct proc_dir_entry *sg_proc_sgp = NULL;
2218 static char sg_proc_sg_dirname[] = "scsi/sg";
2220 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2222 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2223 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2224 size_t count, loff_t *off);
2225 static struct file_operations adio_fops = {
2226 /* .owner, .read and .llseek added in sg_proc_init() */
2227 .open = sg_proc_single_open_adio,
2228 .write = sg_proc_write_adio,
2229 .release = single_release,
2232 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2233 static ssize_t sg_proc_write_dressz(struct file *filp,
2234 const char __user *buffer, size_t count, loff_t *off);
2235 static struct file_operations dressz_fops = {
2236 .open = sg_proc_single_open_dressz,
2237 .write = sg_proc_write_dressz,
2238 .release = single_release,
2241 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2242 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2243 static struct file_operations version_fops = {
2244 .open = sg_proc_single_open_version,
2245 .release = single_release,
2248 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2249 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2250 static struct file_operations devhdr_fops = {
2251 .open = sg_proc_single_open_devhdr,
2252 .release = single_release,
2255 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2256 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2257 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2258 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2259 static void dev_seq_stop(struct seq_file *s, void *v);
2260 static struct file_operations dev_fops = {
2261 .open = sg_proc_open_dev,
2262 .release = seq_release,
2264 static struct seq_operations dev_seq_ops = {
2265 .start = dev_seq_start,
2266 .next = dev_seq_next,
2267 .stop = dev_seq_stop,
2268 .show = sg_proc_seq_show_dev,
2271 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2272 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2273 static struct file_operations devstrs_fops = {
2274 .open = sg_proc_open_devstrs,
2275 .release = seq_release,
2277 static struct seq_operations devstrs_seq_ops = {
2278 .start = dev_seq_start,
2279 .next = dev_seq_next,
2280 .stop = dev_seq_stop,
2281 .show = sg_proc_seq_show_devstrs,
2284 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2285 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2286 static struct file_operations debug_fops = {
2287 .open = sg_proc_open_debug,
2288 .release = seq_release,
2290 static struct seq_operations debug_seq_ops = {
2291 .start = dev_seq_start,
2292 .next = dev_seq_next,
2293 .stop = dev_seq_stop,
2294 .show = sg_proc_seq_show_debug,
2298 struct sg_proc_leaf {
2300 struct file_operations * fops;
2303 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2304 {"allow_dio", &adio_fops},
2305 {"debug", &debug_fops},
2306 {"def_reserved_size", &dressz_fops},
2307 {"device_hdr", &devhdr_fops},
2308 {"devices", &dev_fops},
2309 {"device_strs", &devstrs_fops},
2310 {"version", &version_fops}
2317 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2318 struct sg_proc_leaf * leaf;
2320 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2323 for (k = 0; k < num_leaves; ++k) {
2324 leaf = &sg_proc_leaf_arr[k];
2325 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2326 leaf->fops->owner = THIS_MODULE;
2327 leaf->fops->read = seq_read;
2328 leaf->fops->llseek = seq_lseek;
2329 proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
2335 sg_proc_cleanup(void)
2338 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2342 for (k = 0; k < num_leaves; ++k)
2343 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2344 remove_proc_entry(sg_proc_sg_dirname, NULL);
2348 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2350 seq_printf(s, "%d\n", *((int *)s->private));
2354 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2356 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2360 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2361 size_t count, loff_t *off)
2366 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2368 num = (count < 10) ? count : 10;
2369 if (copy_from_user(buff, buffer, num))
2372 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2376 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2378 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2382 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2383 size_t count, loff_t *off)
2386 unsigned long k = ULONG_MAX;
2389 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2391 num = (count < 10) ? count : 10;
2392 if (copy_from_user(buff, buffer, num))
2395 k = simple_strtoul(buff, NULL, 10);
2396 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2403 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2405 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2410 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2412 return single_open(file, sg_proc_seq_show_version, NULL);
2415 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2417 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2422 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2424 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2427 struct sg_proc_deviter {
2432 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2434 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2441 it->max = sg_last_dev();
2442 if (it->index >= it->max)
2447 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2449 struct sg_proc_deviter * it = s->private;
2452 return (it->index < it->max) ? it : NULL;
2455 static void dev_seq_stop(struct seq_file *s, void *v)
2460 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2462 return seq_open(file, &dev_seq_ops);
2465 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2467 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2469 struct scsi_device *scsidp;
2471 sdp = it ? sg_get_dev(it->index) : NULL;
2472 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2473 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2474 scsidp->host->host_no, scsidp->channel,
2475 scsidp->id, scsidp->lun, (int) scsidp->type,
2477 (int) scsidp->queue_depth,
2478 (int) scsidp->device_busy,
2479 (int) scsi_device_online(scsidp));
2481 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2485 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2487 return seq_open(file, &devstrs_seq_ops);
2490 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2492 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2494 struct scsi_device *scsidp;
2496 sdp = it ? sg_get_dev(it->index) : NULL;
2497 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2498 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2499 scsidp->vendor, scsidp->model, scsidp->rev);
2501 seq_printf(s, "<no active device>\n");
2505 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2507 int k, m, new_interface, blen, usg;
2510 const sg_io_hdr_t *hp;
2514 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2515 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2516 "(res)sgat=%d low_dma=%d\n", k + 1,
2517 jiffies_to_msecs(fp->timeout),
2518 fp->reserve.bufflen,
2519 (int) fp->reserve.k_use_sg,
2521 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2522 (int) fp->cmd_q, (int) fp->force_packid,
2523 (int) fp->keep_orphan, (int) fp->closed);
2524 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2526 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2527 if (srp->res_used) {
2528 if (new_interface &&
2529 (SG_FLAG_MMAP_IO & hp->flags))
2534 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2540 blen = srp->data.bufflen;
2541 usg = srp->data.k_use_sg;
2542 seq_printf(s, srp->done ?
2543 ((1 == srp->done) ? "rcv:" : "fin:")
2545 seq_printf(s, " id=%d blen=%d",
2546 srp->header.pack_id, blen);
2548 seq_printf(s, " dur=%d", hp->duration);
2550 ms = jiffies_to_msecs(jiffies);
2551 seq_printf(s, " t_o/elap=%d/%d",
2552 (new_interface ? hp->timeout :
2553 jiffies_to_msecs(fp->timeout)),
2554 (ms > hp->duration ? ms - hp->duration : 0));
2556 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2557 (int) srp->data.cmd_opcode);
2560 seq_printf(s, " No requests active\n");
2564 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2566 return seq_open(file, &debug_seq_ops);
2569 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2571 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2574 if (it && (0 == it->index)) {
2575 seq_printf(s, "max_active_device=%d(origin 1)\n",
2577 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2579 sdp = it ? sg_get_dev(it->index) : NULL;
2581 struct scsi_device *scsidp = sdp->device;
2583 if (NULL == scsidp) {
2584 seq_printf(s, "device %d detached ??\n",
2589 if (sg_get_nth_sfp(sdp, 0)) {
2590 seq_printf(s, " >>> device=%s ",
2591 sdp->disk->disk_name);
2593 seq_printf(s, "detached pending close ");
2596 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2597 scsidp->host->host_no,
2598 scsidp->channel, scsidp->id,
2600 scsidp->host->hostt->emulated);
2601 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2602 sdp->sg_tablesize, sdp->exclude);
2604 sg_proc_debug_helper(s, sdp);
2609 #endif /* CONFIG_SCSI_PROC_FS */
2611 module_init(init_sg);
2612 module_exit(exit_sg);