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/scatterlist.h>
53 #include <scsi/scsi_dbg.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_driver.h>
56 #include <scsi/scsi_ioctl.h>
59 #include "scsi_logging.h"
61 #ifdef CONFIG_SCSI_PROC_FS
62 #include <linux/proc_fs.h>
63 static char *sg_version_date = "20061027";
65 static int sg_proc_init(void);
66 static void sg_proc_cleanup(void);
69 #define SG_ALLOW_DIO_DEF 0
70 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
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 class_device *, struct class_interface *);
104 static void sg_remove(struct class_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 = {
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 */
119 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
120 struct scatterlist *buffer;/* scatter list */
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 */
140 typedef struct sg_fd { /* holds the state of a file descriptor */
141 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
142 struct sg_device *parentdp; /* owning device */
143 wait_queue_head_t read_wait; /* queue read until command done */
144 rwlock_t rq_list_lock; /* protect access to list in req_arr */
145 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
146 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
147 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
148 unsigned save_scat_len; /* original length of trunc. scat. element */
149 Sg_request *headrp; /* head of request slist, NULL->empty */
150 struct fasync_struct *async_qp; /* used by asynchronous notification */
151 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
152 char low_dma; /* as in parent but possibly overridden to 1 */
153 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
154 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
155 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
156 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
157 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
158 char mmap_called; /* 0 -> mmap() never called on this fd */
161 typedef struct sg_device { /* holds the state of each scsi generic device */
162 struct scsi_device *device;
163 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
164 int sg_tablesize; /* adapter's max scatter-gather table size */
165 u32 index; /* device index number */
166 Sg_fd *headfp; /* first open fd belonging to this device */
167 volatile char detached; /* 0->attached, 1->detached pending removal */
168 volatile char exclude; /* opened for exclusive access */
169 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
170 struct gendisk *disk;
171 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
174 static int sg_fasync(int fd, struct file *filp, int mode);
175 /* tasklet or soft irq callback */
176 static void sg_cmd_done(void *data, char *sense, int result, int resid);
177 static int sg_start_req(Sg_request * srp);
178 static void sg_finish_rem_req(Sg_request * srp);
179 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
180 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
182 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
184 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
185 int blocking, int read_only, Sg_request ** o_srp);
186 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
187 unsigned char *cmnd, int timeout, int blocking);
188 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
189 int wr_xf, int *countp, unsigned char __user **up);
190 static int sg_write_xfer(Sg_request * srp);
191 static int sg_read_xfer(Sg_request * srp);
192 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
193 static void sg_remove_scat(Sg_scatter_hold * schp);
194 static void sg_build_reserve(Sg_fd * sfp, int req_size);
195 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
196 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
197 static struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
198 static void sg_page_free(struct page *page, int size);
199 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
200 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
201 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
202 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
203 static Sg_request *sg_add_request(Sg_fd * sfp);
204 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
205 static int sg_res_in_use(Sg_fd * sfp);
206 static int sg_allow_access(unsigned char opcode, char dev_type);
207 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
208 static Sg_device *sg_get_dev(int dev);
209 #ifdef CONFIG_SCSI_PROC_FS
210 static int sg_last_dev(void);
213 #define SZ_SG_HEADER sizeof(struct sg_header)
214 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
215 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
216 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
219 sg_open(struct inode *inode, struct file *filp)
221 int dev = iminor(inode);
222 int flags = filp->f_flags;
223 struct request_queue *q;
229 nonseekable_open(inode, filp);
230 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
231 sdp = sg_get_dev(dev);
232 if ((!sdp) || (!sdp->device))
237 /* This driver's module count bumped by fops_get in <linux/fs.h> */
238 /* Prevent the device driver from vanishing while we sleep */
239 retval = scsi_device_get(sdp->device);
243 if (!((flags & O_NONBLOCK) ||
244 scsi_block_when_processing_errors(sdp->device))) {
246 /* we are in error recovery for this device */
250 if (flags & O_EXCL) {
251 if (O_RDONLY == (flags & O_ACCMODE)) {
252 retval = -EPERM; /* Can't lock it with read only access */
255 if (sdp->headfp && (flags & O_NONBLOCK)) {
260 __wait_event_interruptible(sdp->o_excl_wait,
261 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
263 retval = res; /* -ERESTARTSYS because signal hit process */
266 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
267 if (flags & O_NONBLOCK) {
272 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
275 retval = res; /* -ERESTARTSYS because signal hit process */
283 if (!sdp->headfp) { /* no existing opens on this device */
285 q = sdp->device->request_queue;
286 sdp->sg_tablesize = min(q->max_hw_segments,
287 q->max_phys_segments);
289 if ((sfp = sg_add_sfp(sdp, dev)))
290 filp->private_data = sfp;
293 sdp->exclude = 0; /* undo if error */
300 scsi_device_put(sdp->device);
304 /* Following function was formerly called 'sg_close' */
306 sg_release(struct inode *inode, struct file *filp)
311 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
313 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
314 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
315 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
316 if (!sdp->detached) {
317 scsi_device_put(sdp->device);
320 wake_up_interruptible(&sdp->o_excl_wait);
326 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
331 int req_pack_id = -1;
333 struct sg_header *old_hdr = NULL;
336 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
338 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
339 sdp->disk->disk_name, (int) count));
341 if (!access_ok(VERIFY_WRITE, buf, count))
343 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
344 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
347 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
351 if (old_hdr->reply_len < 0) {
352 if (count >= SZ_SG_IO_HDR) {
353 sg_io_hdr_t *new_hdr;
354 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
359 retval =__copy_from_user
360 (new_hdr, buf, SZ_SG_IO_HDR);
361 req_pack_id = new_hdr->pack_id;
369 req_pack_id = old_hdr->pack_id;
371 srp = sg_get_rq_mark(sfp, req_pack_id);
372 if (!srp) { /* now wait on packet to arrive */
377 if (filp->f_flags & O_NONBLOCK) {
382 retval = 0; /* following macro beats race condition */
383 __wait_event_interruptible(sfp->read_wait,
385 (srp = sg_get_rq_mark(sfp, req_pack_id))),
394 /* -ERESTARTSYS as signal hit process */
398 if (srp->header.interface_id != '\0') {
399 retval = sg_new_read(sfp, buf, count, srp);
404 if (old_hdr == NULL) {
405 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
411 memset(old_hdr, 0, SZ_SG_HEADER);
412 old_hdr->reply_len = (int) hp->timeout;
413 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
414 old_hdr->pack_id = hp->pack_id;
415 old_hdr->twelve_byte =
416 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
417 old_hdr->target_status = hp->masked_status;
418 old_hdr->host_status = hp->host_status;
419 old_hdr->driver_status = hp->driver_status;
420 if ((CHECK_CONDITION & hp->masked_status) ||
421 (DRIVER_SENSE & hp->driver_status))
422 memcpy(old_hdr->sense_buffer, srp->sense_b,
423 sizeof (old_hdr->sense_buffer));
424 switch (hp->host_status) {
425 /* This setup of 'result' is for backward compatibility and is best
426 ignored by the user who should use target, host + driver status */
428 case DID_PASSTHROUGH:
435 old_hdr->result = EBUSY;
442 old_hdr->result = EIO;
445 old_hdr->result = (srp->sense_b[0] == 0 &&
446 hp->masked_status == GOOD) ? 0 : EIO;
449 old_hdr->result = EIO;
453 /* Now copy the result back to the user buffer. */
454 if (count >= SZ_SG_HEADER) {
455 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
460 if (count > old_hdr->reply_len)
461 count = old_hdr->reply_len;
462 if (count > SZ_SG_HEADER) {
463 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
469 count = (old_hdr->result == 0) ? 0 : -EIO;
470 sg_finish_rem_req(srp);
478 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
480 sg_io_hdr_t *hp = &srp->header;
484 if (count < SZ_SG_IO_HDR) {
489 if ((hp->mx_sb_len > 0) && hp->sbp) {
490 if ((CHECK_CONDITION & hp->masked_status) ||
491 (DRIVER_SENSE & hp->driver_status)) {
492 int sb_len = SCSI_SENSE_BUFFERSIZE;
493 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
494 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
495 len = (len > sb_len) ? sb_len : len;
496 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
503 if (hp->masked_status || hp->host_status || hp->driver_status)
504 hp->info |= SG_INFO_CHECK;
505 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
509 err = sg_read_xfer(srp);
511 sg_finish_rem_req(srp);
512 return (0 == err) ? count : err;
516 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
518 int mxsize, cmd_size, k;
519 int input_size, blocking;
520 unsigned char opcode;
524 struct sg_header old_hdr;
526 unsigned char cmnd[MAX_COMMAND_SIZE];
528 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
530 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
531 sdp->disk->disk_name, (int) count));
534 if (!((filp->f_flags & O_NONBLOCK) ||
535 scsi_block_when_processing_errors(sdp->device)))
538 if (!access_ok(VERIFY_READ, buf, count))
539 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
540 if (count < SZ_SG_HEADER)
542 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
544 blocking = !(filp->f_flags & O_NONBLOCK);
545 if (old_hdr.reply_len < 0)
546 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
547 if (count < (SZ_SG_HEADER + 6))
548 return -EIO; /* The minimum scsi command length is 6 bytes. */
550 if (!(srp = sg_add_request(sfp))) {
551 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
555 __get_user(opcode, buf);
556 if (sfp->next_cmd_len > 0) {
557 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
558 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
559 sfp->next_cmd_len = 0;
560 sg_remove_request(sfp, srp);
563 cmd_size = sfp->next_cmd_len;
564 sfp->next_cmd_len = 0; /* reset so only this write() effected */
566 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
567 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
570 SCSI_LOG_TIMEOUT(4, printk(
571 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
572 /* Determine buffer size. */
573 input_size = count - cmd_size;
574 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
575 mxsize -= SZ_SG_HEADER;
576 input_size -= SZ_SG_HEADER;
577 if (input_size < 0) {
578 sg_remove_request(sfp, srp);
579 return -EIO; /* User did not pass enough bytes for this command. */
582 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
583 hp->cmd_len = (unsigned char) cmd_size;
587 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
588 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
590 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
591 hp->dxfer_len = mxsize;
592 hp->dxferp = (char __user *)buf + cmd_size;
594 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
595 hp->flags = input_size; /* structure abuse ... */
596 hp->pack_id = old_hdr.pack_id;
598 if (__copy_from_user(cmnd, buf, cmd_size))
601 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
602 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
603 * is a non-zero input_size, so emit a warning.
605 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
606 if (printk_ratelimit())
608 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
609 "guessing data in;\n" KERN_WARNING " "
610 "program %s not setting count and/or reply_len properly\n",
611 old_hdr.reply_len - (int)SZ_SG_HEADER,
612 input_size, (unsigned int) cmnd[0],
614 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
615 return (k < 0) ? k : count;
619 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
620 int blocking, int read_only, Sg_request ** o_srp)
625 unsigned char cmnd[MAX_COMMAND_SIZE];
627 unsigned long ul_timeout;
629 if (count < SZ_SG_IO_HDR)
631 if (!access_ok(VERIFY_READ, buf, count))
632 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
634 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
635 if (!(srp = sg_add_request(sfp))) {
636 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
640 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
641 sg_remove_request(sfp, srp);
644 if (hp->interface_id != 'S') {
645 sg_remove_request(sfp, srp);
648 if (hp->flags & SG_FLAG_MMAP_IO) {
649 if (hp->dxfer_len > sfp->reserve.bufflen) {
650 sg_remove_request(sfp, srp);
651 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
653 if (hp->flags & SG_FLAG_DIRECT_IO) {
654 sg_remove_request(sfp, srp);
655 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
657 if (sg_res_in_use(sfp)) {
658 sg_remove_request(sfp, srp);
659 return -EBUSY; /* reserve buffer already being used */
662 ul_timeout = msecs_to_jiffies(srp->header.timeout);
663 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
664 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
665 sg_remove_request(sfp, srp);
668 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
669 sg_remove_request(sfp, srp);
670 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
672 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
673 sg_remove_request(sfp, srp);
677 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
678 sg_remove_request(sfp, srp);
681 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
690 sg_common_write(Sg_fd * sfp, Sg_request * srp,
691 unsigned char *cmnd, int timeout, int blocking)
694 Sg_device *sdp = sfp->parentdp;
695 sg_io_hdr_t *hp = &srp->header;
697 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
699 hp->masked_status = 0;
703 hp->driver_status = 0;
705 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
706 (int) cmnd[0], (int) hp->cmd_len));
708 if ((k = sg_start_req(srp))) {
709 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
710 sg_finish_rem_req(srp);
711 return k; /* probably out of space --> ENOMEM */
713 if ((k = sg_write_xfer(srp))) {
714 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: write_xfer, bad address\n"));
715 sg_finish_rem_req(srp);
719 sg_finish_rem_req(srp);
723 switch (hp->dxfer_direction) {
724 case SG_DXFER_TO_FROM_DEV:
725 case SG_DXFER_FROM_DEV:
726 data_dir = DMA_FROM_DEVICE;
728 case SG_DXFER_TO_DEV:
729 data_dir = DMA_TO_DEVICE;
731 case SG_DXFER_UNKNOWN:
732 data_dir = DMA_BIDIRECTIONAL;
738 hp->duration = jiffies_to_msecs(jiffies);
739 /* Now send everything of to mid-level. The next time we hear about this
740 packet is when sg_cmd_done() is called (i.e. a callback). */
741 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
742 hp->dxfer_len, srp->data.k_use_sg, timeout,
743 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
745 SCSI_LOG_TIMEOUT(1, printk("sg_common_write: scsi_execute_async failed\n"));
747 * most likely out of mem, but could also be a bad map
749 sg_finish_rem_req(srp);
756 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
758 unsigned long iflags;
761 read_lock_irqsave(&sfp->rq_list_lock, iflags);
763 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
768 sg_ioctl(struct inode *inode, struct file *filp,
769 unsigned int cmd_in, unsigned long arg)
771 void __user *p = (void __user *)arg;
773 int result, val, read_only;
777 unsigned long iflags;
779 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
781 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
782 sdp->disk->disk_name, (int) cmd_in));
783 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
788 int blocking = 1; /* ignore O_NONBLOCK flag */
792 if (!scsi_block_when_processing_errors(sdp->device))
794 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
797 sg_new_write(sfp, p, SZ_SG_IO_HDR,
798 blocking, read_only, &srp);
801 srp->sg_io_owned = 1;
803 result = 0; /* following macro to beat race condition */
804 __wait_event_interruptible(sfp->read_wait,
805 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
810 return 0; /* request packet dropped already */
814 return result; /* -ERESTARTSYS because signal hit process */
816 write_lock_irqsave(&sfp->rq_list_lock, iflags);
818 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
819 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
820 return (result < 0) ? result : 0;
823 result = get_user(val, ip);
828 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
829 val = MULDIV (INT_MAX, USER_HZ, HZ);
830 sfp->timeout_user = val;
831 sfp->timeout = MULDIV (val, HZ, USER_HZ);
834 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
835 /* strange ..., for backward compatibility */
836 return sfp->timeout_user;
837 case SG_SET_FORCE_LOW_DMA:
838 result = get_user(val, ip);
843 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
844 val = (int) sfp->reserve.bufflen;
845 sg_remove_scat(&sfp->reserve);
846 sg_build_reserve(sfp, val);
851 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
855 return put_user((int) sfp->low_dma, ip);
857 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
860 sg_scsi_id_t __user *sg_idp = p;
864 __put_user((int) sdp->device->host->host_no,
866 __put_user((int) sdp->device->channel,
868 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
869 __put_user((int) sdp->device->lun, &sg_idp->lun);
870 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
871 __put_user((short) sdp->device->host->cmd_per_lun,
872 &sg_idp->h_cmd_per_lun);
873 __put_user((short) sdp->device->queue_depth,
874 &sg_idp->d_queue_depth);
875 __put_user(0, &sg_idp->unused[0]);
876 __put_user(0, &sg_idp->unused[1]);
879 case SG_SET_FORCE_PACK_ID:
880 result = get_user(val, ip);
883 sfp->force_packid = val ? 1 : 0;
886 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
888 read_lock_irqsave(&sfp->rq_list_lock, iflags);
889 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
890 if ((1 == srp->done) && (!srp->sg_io_owned)) {
891 read_unlock_irqrestore(&sfp->rq_list_lock,
893 __put_user(srp->header.pack_id, ip);
897 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
900 case SG_GET_NUM_WAITING:
901 read_lock_irqsave(&sfp->rq_list_lock, iflags);
902 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
903 if ((1 == srp->done) && (!srp->sg_io_owned))
906 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
907 return put_user(val, ip);
908 case SG_GET_SG_TABLESIZE:
909 return put_user(sdp->sg_tablesize, ip);
910 case SG_SET_RESERVED_SIZE:
911 result = get_user(val, ip);
916 val = min_t(int, val,
917 sdp->device->request_queue->max_sectors * 512);
918 if (val != sfp->reserve.bufflen) {
919 if (sg_res_in_use(sfp) || sfp->mmap_called)
921 sg_remove_scat(&sfp->reserve);
922 sg_build_reserve(sfp, val);
925 case SG_GET_RESERVED_SIZE:
926 val = min_t(int, sfp->reserve.bufflen,
927 sdp->device->request_queue->max_sectors * 512);
928 return put_user(val, ip);
929 case SG_SET_COMMAND_Q:
930 result = get_user(val, ip);
933 sfp->cmd_q = val ? 1 : 0;
935 case SG_GET_COMMAND_Q:
936 return put_user((int) sfp->cmd_q, ip);
937 case SG_SET_KEEP_ORPHAN:
938 result = get_user(val, ip);
941 sfp->keep_orphan = val;
943 case SG_GET_KEEP_ORPHAN:
944 return put_user((int) sfp->keep_orphan, ip);
945 case SG_NEXT_CMD_LEN:
946 result = get_user(val, ip);
949 sfp->next_cmd_len = (val > 0) ? val : 0;
951 case SG_GET_VERSION_NUM:
952 return put_user(sg_version_num, ip);
953 case SG_GET_ACCESS_COUNT:
954 /* faked - we don't have a real access count anymore */
955 val = (sdp->device ? 1 : 0);
956 return put_user(val, ip);
957 case SG_GET_REQUEST_TABLE:
958 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
961 sg_req_info_t *rinfo;
964 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
968 read_lock_irqsave(&sfp->rq_list_lock, iflags);
969 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
970 ++val, srp = srp ? srp->nextrp : srp) {
971 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
973 rinfo[val].req_state = srp->done + 1;
975 srp->header.masked_status &
976 srp->header.host_status &
977 srp->header.driver_status;
979 rinfo[val].duration =
980 srp->header.duration;
982 ms = jiffies_to_msecs(jiffies);
983 rinfo[val].duration =
984 (ms > srp->header.duration) ?
985 (ms - srp->header.duration) : 0;
987 rinfo[val].orphan = srp->orphan;
988 rinfo[val].sg_io_owned =
996 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
997 result = __copy_to_user(p, rinfo,
998 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
999 result = result ? -EFAULT : 0;
1003 case SG_EMULATED_HOST:
1006 return put_user(sdp->device->host->hostt->emulated, ip);
1010 if (filp->f_flags & O_NONBLOCK) {
1011 if (scsi_host_in_recovery(sdp->device->host))
1013 } else if (!scsi_block_when_processing_errors(sdp->device))
1015 result = get_user(val, ip);
1018 if (SG_SCSI_RESET_NOTHING == val)
1021 case SG_SCSI_RESET_DEVICE:
1022 val = SCSI_TRY_RESET_DEVICE;
1024 case SG_SCSI_RESET_BUS:
1025 val = SCSI_TRY_RESET_BUS;
1027 case SG_SCSI_RESET_HOST:
1028 val = SCSI_TRY_RESET_HOST;
1033 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1035 return (scsi_reset_provider(sdp->device, val) ==
1036 SUCCESS) ? 0 : -EIO;
1037 case SCSI_IOCTL_SEND_COMMAND:
1041 unsigned char opcode = WRITE_6;
1042 Scsi_Ioctl_Command __user *siocp = p;
1044 if (copy_from_user(&opcode, siocp->data, 1))
1046 if (!sg_allow_access(opcode, sdp->device->type))
1049 return sg_scsi_ioctl(filp, sdp->device->request_queue, NULL, p);
1051 result = get_user(val, ip);
1054 sdp->sgdebug = (char) val;
1056 case SCSI_IOCTL_GET_IDLUN:
1057 case SCSI_IOCTL_GET_BUS_NUMBER:
1058 case SCSI_IOCTL_PROBE_HOST:
1059 case SG_GET_TRANSFORM:
1062 return scsi_ioctl(sdp->device, cmd_in, p);
1064 return put_user(sdp->device->request_queue->max_sectors * 512,
1068 return -EPERM; /* don't know so take safe approach */
1069 return scsi_ioctl(sdp->device, cmd_in, p);
1073 #ifdef CONFIG_COMPAT
1074 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1078 struct scsi_device *sdev;
1080 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1084 if (sdev->host->hostt->compat_ioctl) {
1087 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1092 return -ENOIOCTLCMD;
1097 sg_poll(struct file *filp, poll_table * wait)
1099 unsigned int res = 0;
1104 unsigned long iflags;
1106 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1109 poll_wait(filp, &sfp->read_wait, wait);
1110 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1111 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1112 /* if any read waiting, flag it */
1113 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1114 res = POLLIN | POLLRDNORM;
1117 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1121 else if (!sfp->cmd_q) {
1123 res |= POLLOUT | POLLWRNORM;
1124 } else if (count < SG_MAX_QUEUE)
1125 res |= POLLOUT | POLLWRNORM;
1126 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1127 sdp->disk->disk_name, (int) res));
1132 sg_fasync(int fd, struct file *filp, int mode)
1138 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1140 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1141 sdp->disk->disk_name, mode));
1143 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1144 return (retval < 0) ? retval : 0;
1147 static struct page *
1148 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1151 struct page *page = NOPAGE_SIGBUS;
1152 unsigned long offset, len, sa;
1153 Sg_scatter_hold *rsv_schp;
1154 struct scatterlist *sg;
1157 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1159 rsv_schp = &sfp->reserve;
1160 offset = addr - vma->vm_start;
1161 if (offset >= rsv_schp->bufflen)
1163 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1164 offset, rsv_schp->k_use_sg));
1165 sg = rsv_schp->buffer;
1167 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1168 ++k, sg = sg_next(sg)) {
1169 len = vma->vm_end - sa;
1170 len = (len < sg->length) ? len : sg->length;
1172 page = virt_to_page(page_address(sg_page(sg)) + offset);
1173 get_page(page); /* increment page count */
1181 *type = VM_FAULT_MINOR;
1185 static struct vm_operations_struct sg_mmap_vm_ops = {
1186 .nopage = sg_vma_nopage,
1190 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1193 unsigned long req_sz, len, sa;
1194 Sg_scatter_hold *rsv_schp;
1196 struct scatterlist *sg;
1198 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1200 req_sz = vma->vm_end - vma->vm_start;
1201 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1202 (void *) vma->vm_start, (int) req_sz));
1204 return -EINVAL; /* want no offset */
1205 rsv_schp = &sfp->reserve;
1206 if (req_sz > rsv_schp->bufflen)
1207 return -ENOMEM; /* cannot map more than reserved buffer */
1210 sg = rsv_schp->buffer;
1211 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1212 ++k, sg = sg_next(sg)) {
1213 len = vma->vm_end - sa;
1214 len = (len < sg->length) ? len : sg->length;
1218 sfp->mmap_called = 1;
1219 vma->vm_flags |= VM_RESERVED;
1220 vma->vm_private_data = sfp;
1221 vma->vm_ops = &sg_mmap_vm_ops;
1225 /* This function is a "bottom half" handler that is called by the
1226 * mid level when a command is completed (or has failed). */
1228 sg_cmd_done(void *data, char *sense, int result, int resid)
1230 Sg_request *srp = data;
1231 Sg_device *sdp = NULL;
1233 unsigned long iflags;
1237 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1240 sfp = srp->parentfp;
1242 sdp = sfp->parentdp;
1243 if ((NULL == sdp) || sdp->detached) {
1244 printk(KERN_INFO "sg_cmd_done: device detached\n");
1249 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1250 sdp->disk->disk_name, srp->header.pack_id, result));
1251 srp->header.resid = resid;
1252 ms = jiffies_to_msecs(jiffies);
1253 srp->header.duration = (ms > srp->header.duration) ?
1254 (ms - srp->header.duration) : 0;
1256 struct scsi_sense_hdr sshdr;
1258 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1259 srp->header.status = 0xff & result;
1260 srp->header.masked_status = status_byte(result);
1261 srp->header.msg_status = msg_byte(result);
1262 srp->header.host_status = host_byte(result);
1263 srp->header.driver_status = driver_byte(result);
1264 if ((sdp->sgdebug > 0) &&
1265 ((CHECK_CONDITION == srp->header.masked_status) ||
1266 (COMMAND_TERMINATED == srp->header.masked_status)))
1267 __scsi_print_sense("sg_cmd_done", sense,
1268 SCSI_SENSE_BUFFERSIZE);
1270 /* Following if statement is a patch supplied by Eric Youngdale */
1271 if (driver_byte(result) != 0
1272 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1273 && !scsi_sense_is_deferred(&sshdr)
1274 && sshdr.sense_key == UNIT_ATTENTION
1275 && sdp->device->removable) {
1276 /* Detected possible disc change. Set the bit - this */
1277 /* may be used if there are filesystems using this device */
1278 sdp->device->changed = 1;
1281 /* Rely on write phase to clean out srp status values, so no "else" */
1283 if (sfp->closed) { /* whoops this fd already released, cleanup */
1284 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1285 sg_finish_rem_req(srp);
1287 if (NULL == sfp->headrp) {
1288 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1289 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1290 scsi_device_put(sdp->device);
1294 } else if (srp && srp->orphan) {
1295 if (sfp->keep_orphan)
1296 srp->sg_io_owned = 0;
1298 sg_finish_rem_req(srp);
1303 /* Now wake up any sg_read() that is waiting for this packet. */
1304 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1305 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1307 wake_up_interruptible(&sfp->read_wait);
1308 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1312 static struct file_operations sg_fops = {
1313 .owner = THIS_MODULE,
1318 #ifdef CONFIG_COMPAT
1319 .compat_ioctl = sg_compat_ioctl,
1323 .release = sg_release,
1324 .fasync = sg_fasync,
1327 static struct class *sg_sysfs_class;
1329 static int sg_sysfs_valid = 0;
1331 static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1333 struct request_queue *q = scsidp->request_queue;
1335 unsigned long iflags;
1339 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1341 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1342 return ERR_PTR(-ENOMEM);
1345 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1346 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1350 write_lock_irqsave(&sg_index_lock, iflags);
1351 error = idr_get_new(&sg_index_idr, sdp, &k);
1352 write_unlock_irqrestore(&sg_index_lock, iflags);
1355 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1360 if (unlikely(k >= SG_MAX_DEVS))
1363 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1364 sprintf(disk->disk_name, "sg%d", k);
1365 disk->first_minor = k;
1367 sdp->device = scsidp;
1368 init_waitqueue_head(&sdp->o_excl_wait);
1369 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1376 return ERR_PTR(error);
1381 sdev_printk(KERN_WARNING, scsidp,
1382 "Unable to attach sg device type=%d, minor "
1383 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1389 sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
1391 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1392 struct gendisk *disk;
1393 Sg_device *sdp = NULL;
1394 struct cdev * cdev = NULL;
1396 unsigned long iflags;
1398 disk = alloc_disk(1);
1400 printk(KERN_WARNING "alloc_disk failed\n");
1403 disk->major = SCSI_GENERIC_MAJOR;
1406 cdev = cdev_alloc();
1408 printk(KERN_WARNING "cdev_alloc failed\n");
1411 cdev->owner = THIS_MODULE;
1412 cdev->ops = &sg_fops;
1414 sdp = sg_alloc(disk, scsidp);
1416 printk(KERN_WARNING "sg_alloc failed\n");
1417 error = PTR_ERR(sdp);
1421 class_set_devdata(cl_dev, sdp);
1422 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
1427 if (sg_sysfs_valid) {
1428 struct class_device * sg_class_member;
1430 sg_class_member = class_device_create(sg_sysfs_class, NULL,
1431 MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
1434 if (IS_ERR(sg_class_member))
1435 printk(KERN_WARNING "sg_add: "
1436 "class_device_create failed\n");
1437 class_set_devdata(sg_class_member, sdp);
1438 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1439 &sg_class_member->kobj, "generic");
1441 printk(KERN_ERR "sg_add: unable to make symlink "
1442 "'generic' back to sg%d\n", sdp->index);
1444 printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
1446 sdev_printk(KERN_NOTICE, scsidp,
1447 "Attached scsi generic sg%d type %d\n", sdp->index,
1453 write_lock_irqsave(&sg_index_lock, iflags);
1454 idr_remove(&sg_index_idr, sdp->index);
1455 write_unlock_irqrestore(&sg_index_lock, iflags);
1466 sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
1468 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1469 Sg_device *sdp = class_get_devdata(cl_dev);
1470 unsigned long iflags;
1481 write_lock_irqsave(&sg_index_lock, iflags);
1484 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1486 for (srp = sfp->headrp; srp; srp = tsrp) {
1488 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1489 sg_finish_rem_req(srp);
1492 scsi_device_put(sdp->device);
1493 __sg_remove_sfp(sdp, sfp);
1496 wake_up_interruptible(&sfp->read_wait);
1497 kill_fasync(&sfp->async_qp, SIGPOLL,
1501 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1502 if (NULL == sdp->headfp) {
1503 idr_remove(&sg_index_idr, sdp->index);
1505 } else { /* nothing active, simple case */
1506 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1507 idr_remove(&sg_index_idr, sdp->index);
1509 write_unlock_irqrestore(&sg_index_lock, iflags);
1511 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1512 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1513 cdev_del(sdp->cdev);
1515 put_disk(sdp->disk);
1517 if (NULL == sdp->headfp)
1521 msleep(10); /* dirty detach so delay device destruction */
1524 module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
1525 module_param_named(def_reserved_size, def_reserved_size, int,
1527 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1529 MODULE_AUTHOR("Douglas Gilbert");
1530 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1531 MODULE_LICENSE("GPL");
1532 MODULE_VERSION(SG_VERSION_STR);
1533 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
1535 MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
1536 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
1537 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1538 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1545 if (scatter_elem_sz < PAGE_SIZE) {
1546 scatter_elem_sz = PAGE_SIZE;
1547 scatter_elem_sz_prev = scatter_elem_sz;
1549 if (def_reserved_size >= 0)
1550 sg_big_buff = def_reserved_size;
1552 def_reserved_size = sg_big_buff;
1554 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1558 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1559 if ( IS_ERR(sg_sysfs_class) ) {
1560 rc = PTR_ERR(sg_sysfs_class);
1564 rc = scsi_register_interface(&sg_interface);
1566 #ifdef CONFIG_SCSI_PROC_FS
1568 #endif /* CONFIG_SCSI_PROC_FS */
1571 class_destroy(sg_sysfs_class);
1573 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1580 #ifdef CONFIG_SCSI_PROC_FS
1582 #endif /* CONFIG_SCSI_PROC_FS */
1583 scsi_unregister_interface(&sg_interface);
1584 class_destroy(sg_sysfs_class);
1586 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1588 idr_destroy(&sg_index_idr);
1592 sg_start_req(Sg_request * srp)
1595 Sg_fd *sfp = srp->parentfp;
1596 sg_io_hdr_t *hp = &srp->header;
1597 int dxfer_len = (int) hp->dxfer_len;
1598 int dxfer_dir = hp->dxfer_direction;
1599 Sg_scatter_hold *req_schp = &srp->data;
1600 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1602 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1603 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1605 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1606 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1607 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1608 res = sg_build_direct(srp, sfp, dxfer_len);
1609 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1612 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1613 sg_link_reserve(sfp, srp, dxfer_len);
1615 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1617 sg_remove_scat(req_schp);
1625 sg_finish_rem_req(Sg_request * srp)
1627 Sg_fd *sfp = srp->parentfp;
1628 Sg_scatter_hold *req_schp = &srp->data;
1630 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1632 sg_unlink_reserve(sfp, srp);
1634 sg_remove_scat(req_schp);
1635 sg_remove_request(sfp, srp);
1639 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1641 int sg_bufflen = tablesize * sizeof(struct scatterlist);
1642 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1645 * TODO: test without low_dma, we should not need it since
1646 * the block layer will bounce the buffer for us
1648 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1651 gfp_flags |= GFP_DMA;
1652 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
1655 sg_init_table(schp->buffer, tablesize);
1656 schp->sglist_len = sg_bufflen;
1657 return tablesize; /* number of scat_gath elements allocated */
1660 #ifdef SG_ALLOW_DIO_CODE
1661 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1662 /* TODO: hopefully we can use the generic block layer code */
1664 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1665 - mapping of all pages not successful
1666 (i.e., either completely successful or fails)
1669 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1670 unsigned long uaddr, size_t count, int rw)
1672 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1673 unsigned long start = uaddr >> PAGE_SHIFT;
1674 const int nr_pages = end - start;
1676 struct page **pages;
1678 /* User attempted Overflow! */
1679 if ((uaddr + count) < uaddr)
1683 if (nr_pages > max_pages)
1690 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1693 /* Try to fault in all of the necessary pages */
1694 down_read(¤t->mm->mmap_sem);
1695 /* rw==READ means read from drive, write into memory area */
1696 res = get_user_pages(
1702 0, /* don't force */
1705 up_read(¤t->mm->mmap_sem);
1707 /* Errors and no page mapped should return here */
1711 for (i=0; i < nr_pages; i++) {
1712 /* FIXME: flush superflous for rw==READ,
1713 * probably wrong function for rw==WRITE
1715 flush_dcache_page(pages[i]);
1716 /* ?? Is locking needed? I don't think so */
1717 /* if (TestSetPageLocked(pages[i]))
1721 sg_set_page(sgl, pages[0], 0, uaddr & ~PAGE_MASK);
1723 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1724 count -= sgl[0].length;
1725 for (i=1; i < nr_pages ; i++)
1726 sg_set_page(&sgl[i], pages[i], count < PAGE_SIZE ? count : PAGE_SIZE, 0);
1729 sgl[0].length = count;
1737 for (j=0; j < res; j++)
1738 page_cache_release(pages[j]);
1746 /* And unmap them... */
1748 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1753 for (i=0; i < nr_pages; i++) {
1754 struct page *page = sg_page(&sgl[i]);
1758 /* unlock_page(page); */
1759 /* FIXME: cache flush missing for rw==READ
1760 * FIXME: call the correct reference counting function
1762 page_cache_release(page);
1768 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1772 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1774 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1776 #ifdef SG_ALLOW_DIO_CODE
1777 sg_io_hdr_t *hp = &srp->header;
1778 Sg_scatter_hold *schp = &srp->data;
1779 int sg_tablesize = sfp->parentdp->sg_tablesize;
1780 int mx_sc_elems, res;
1781 struct scsi_device *sdev = sfp->parentdp->device;
1783 if (((unsigned long)hp->dxferp &
1784 queue_dma_alignment(sdev->request_queue)) != 0)
1787 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1788 if (mx_sc_elems <= 0) {
1791 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1792 (unsigned long)hp->dxferp, dxfer_len,
1793 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
1795 sg_remove_scat(schp);
1798 schp->k_use_sg = res;
1799 schp->dio_in_use = 1;
1800 hp->info |= SG_INFO_DIRECT_IO;
1808 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1810 struct scatterlist *sg;
1811 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1812 int sg_tablesize = sfp->parentdp->sg_tablesize;
1813 int blk_size = buff_size;
1814 struct page *p = NULL;
1819 ++blk_size; /* don't know why */
1820 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1821 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1822 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1823 buff_size, blk_size));
1825 /* N.B. ret_sz carried into this block ... */
1826 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1827 if (mx_sc_elems < 0)
1828 return mx_sc_elems; /* most likely -ENOMEM */
1830 num = scatter_elem_sz;
1831 if (unlikely(num != scatter_elem_sz_prev)) {
1832 if (num < PAGE_SIZE) {
1833 scatter_elem_sz = PAGE_SIZE;
1834 scatter_elem_sz_prev = PAGE_SIZE;
1836 scatter_elem_sz_prev = num;
1838 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1839 (rem_sz > 0) && (k < mx_sc_elems);
1840 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
1842 num = (rem_sz > scatter_elem_sz_prev) ?
1843 scatter_elem_sz_prev : rem_sz;
1844 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1848 if (num == scatter_elem_sz_prev) {
1849 if (unlikely(ret_sz > scatter_elem_sz_prev)) {
1850 scatter_elem_sz = ret_sz;
1851 scatter_elem_sz_prev = ret_sz;
1854 sg_set_page(sg, p, (ret_sz > num) ? num : ret_sz, 0);
1856 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
1857 "ret_sz=%d\n", k, num, ret_sz));
1858 } /* end of for loop */
1861 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
1862 "rem_sz=%d\n", k, rem_sz));
1864 schp->bufflen = blk_size;
1865 if (rem_sz > 0) /* must have failed */
1872 sg_write_xfer(Sg_request * srp)
1874 sg_io_hdr_t *hp = &srp->header;
1875 Sg_scatter_hold *schp = &srp->data;
1876 struct scatterlist *sg = schp->buffer;
1878 int j, k, onum, usglen, ksglen, res;
1879 int iovec_count = (int) hp->iovec_count;
1880 int dxfer_dir = hp->dxfer_direction;
1882 unsigned char __user *up;
1883 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1885 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1886 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1887 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1888 if (schp->bufflen < num_xfer)
1889 num_xfer = schp->bufflen;
1891 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1893 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1896 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1897 num_xfer, iovec_count, schp->k_use_sg));
1900 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1905 ksglen = sg->length;
1906 p = page_address(sg_page(sg));
1907 for (j = 0, k = 0; j < onum; ++j) {
1908 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1912 for (; p; sg = sg_next(sg), ksglen = sg->length,
1913 p = page_address(sg_page(sg))) {
1916 if (ksglen > usglen) {
1917 if (usglen >= num_xfer) {
1918 if (__copy_from_user(p, up, num_xfer))
1922 if (__copy_from_user(p, up, usglen))
1928 if (ksglen >= num_xfer) {
1929 if (__copy_from_user(p, up, num_xfer))
1933 if (__copy_from_user(p, up, ksglen))
1939 if (k >= schp->k_use_sg)
1948 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1949 int wr_xf, int *countp, unsigned char __user **up)
1951 int num_xfer = (int) hp->dxfer_len;
1952 unsigned char __user *p = hp->dxferp;
1956 if (wr_xf && ('\0' == hp->interface_id))
1957 count = (int) hp->flags; /* holds "old" input_size */
1962 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1965 count = (int) iovec.iov_len;
1967 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
1977 sg_remove_scat(Sg_scatter_hold * schp)
1979 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
1980 if (schp->buffer && (schp->sglist_len > 0)) {
1981 struct scatterlist *sg = schp->buffer;
1983 if (schp->dio_in_use) {
1984 #ifdef SG_ALLOW_DIO_CODE
1985 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
1990 for (k = 0; (k < schp->k_use_sg) && sg_page(sg);
1991 ++k, sg = sg_next(sg)) {
1992 SCSI_LOG_TIMEOUT(5, printk(
1993 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
1994 k, sg_page(sg), sg->length));
1995 sg_page_free(sg_page(sg), sg->length);
1998 kfree(schp->buffer);
2000 memset(schp, 0, sizeof (*schp));
2004 sg_read_xfer(Sg_request * srp)
2006 sg_io_hdr_t *hp = &srp->header;
2007 Sg_scatter_hold *schp = &srp->data;
2008 struct scatterlist *sg = schp->buffer;
2010 int j, k, onum, usglen, ksglen, res;
2011 int iovec_count = (int) hp->iovec_count;
2012 int dxfer_dir = hp->dxfer_direction;
2014 unsigned char __user *up;
2015 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2017 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2018 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2019 num_xfer = hp->dxfer_len;
2020 if (schp->bufflen < num_xfer)
2021 num_xfer = schp->bufflen;
2023 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2025 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2028 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2029 num_xfer, iovec_count, schp->k_use_sg));
2032 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2037 p = page_address(sg_page(sg));
2038 ksglen = sg->length;
2039 for (j = 0, k = 0; j < onum; ++j) {
2040 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2044 for (; p; sg = sg_next(sg), ksglen = sg->length,
2045 p = page_address(sg_page(sg))) {
2048 if (ksglen > usglen) {
2049 if (usglen >= num_xfer) {
2050 if (__copy_to_user(up, p, num_xfer))
2054 if (__copy_to_user(up, p, usglen))
2060 if (ksglen >= num_xfer) {
2061 if (__copy_to_user(up, p, num_xfer))
2065 if (__copy_to_user(up, p, ksglen))
2071 if (k >= schp->k_use_sg)
2080 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2082 Sg_scatter_hold *schp = &srp->data;
2083 struct scatterlist *sg = schp->buffer;
2086 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2088 if ((!outp) || (num_read_xfer <= 0))
2091 for (k = 0; (k < schp->k_use_sg) && sg_page(sg); ++k, sg = sg_next(sg)) {
2093 if (num > num_read_xfer) {
2094 if (__copy_to_user(outp, page_address(sg_page(sg)),
2099 if (__copy_to_user(outp, page_address(sg_page(sg)),
2102 num_read_xfer -= num;
2103 if (num_read_xfer <= 0)
2113 sg_build_reserve(Sg_fd * sfp, int req_size)
2115 Sg_scatter_hold *schp = &sfp->reserve;
2117 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2119 if (req_size < PAGE_SIZE)
2120 req_size = PAGE_SIZE;
2121 if (0 == sg_build_indirect(schp, sfp, req_size))
2124 sg_remove_scat(schp);
2125 req_size >>= 1; /* divide by 2 */
2126 } while (req_size > (PAGE_SIZE / 2));
2130 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2132 Sg_scatter_hold *req_schp = &srp->data;
2133 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2134 struct scatterlist *sg = rsv_schp->buffer;
2138 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2141 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
2144 sfp->save_scat_len = num;
2146 req_schp->k_use_sg = k + 1;
2147 req_schp->sglist_len = rsv_schp->sglist_len;
2148 req_schp->buffer = rsv_schp->buffer;
2150 req_schp->bufflen = size;
2151 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2157 if (k >= rsv_schp->k_use_sg)
2158 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2162 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2164 Sg_scatter_hold *req_schp = &srp->data;
2165 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2167 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2168 (int) req_schp->k_use_sg));
2169 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2170 struct scatterlist *sg = rsv_schp->buffer;
2172 if (sfp->save_scat_len > 0)
2173 (sg + (req_schp->k_use_sg - 1))->length =
2174 (unsigned) sfp->save_scat_len;
2176 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2178 req_schp->k_use_sg = 0;
2179 req_schp->bufflen = 0;
2180 req_schp->buffer = NULL;
2181 req_schp->sglist_len = 0;
2182 sfp->save_scat_len = 0;
2187 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2190 unsigned long iflags;
2192 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2193 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2194 /* look for requests that are ready + not SG_IO owned */
2195 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2196 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2197 resp->done = 2; /* guard against other readers */
2201 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2205 #ifdef CONFIG_SCSI_PROC_FS
2207 sg_get_nth_request(Sg_fd * sfp, int nth)
2210 unsigned long iflags;
2213 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2214 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2215 ++k, resp = resp->nextrp) ;
2216 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2221 /* always adds to end of list */
2223 sg_add_request(Sg_fd * sfp)
2226 unsigned long iflags;
2228 Sg_request *rp = sfp->req_arr;
2230 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2233 memset(rp, 0, sizeof (Sg_request));
2238 if (0 == sfp->cmd_q)
2239 resp = NULL; /* command queuing disallowed */
2241 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2245 if (k < SG_MAX_QUEUE) {
2246 memset(rp, 0, sizeof (Sg_request));
2248 while (resp->nextrp)
2249 resp = resp->nextrp;
2257 resp->nextrp = NULL;
2258 resp->header.duration = jiffies_to_msecs(jiffies);
2260 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2264 /* Return of 1 for found; 0 for not found */
2266 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2268 Sg_request *prev_rp;
2270 unsigned long iflags;
2273 if ((!sfp) || (!srp) || (!sfp->headrp))
2275 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2276 prev_rp = sfp->headrp;
2277 if (srp == prev_rp) {
2278 sfp->headrp = prev_rp->nextrp;
2279 prev_rp->parentfp = NULL;
2282 while ((rp = prev_rp->nextrp)) {
2284 prev_rp->nextrp = rp->nextrp;
2285 rp->parentfp = NULL;
2292 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2296 #ifdef CONFIG_SCSI_PROC_FS
2298 sg_get_nth_sfp(Sg_device * sdp, int nth)
2301 unsigned long iflags;
2304 read_lock_irqsave(&sg_index_lock, iflags);
2305 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2306 ++k, resp = resp->nextfp) ;
2307 read_unlock_irqrestore(&sg_index_lock, iflags);
2313 sg_add_sfp(Sg_device * sdp, int dev)
2316 unsigned long iflags;
2319 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2323 init_waitqueue_head(&sfp->read_wait);
2324 rwlock_init(&sfp->rq_list_lock);
2326 sfp->timeout = SG_DEFAULT_TIMEOUT;
2327 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2328 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2329 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2330 sdp->device->host->unchecked_isa_dma : 1;
2331 sfp->cmd_q = SG_DEF_COMMAND_Q;
2332 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2333 sfp->parentdp = sdp;
2334 write_lock_irqsave(&sg_index_lock, iflags);
2337 else { /* add to tail of existing list */
2338 Sg_fd *pfp = sdp->headfp;
2343 write_unlock_irqrestore(&sg_index_lock, iflags);
2344 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2345 if (unlikely(sg_big_buff != def_reserved_size))
2346 sg_big_buff = def_reserved_size;
2348 bufflen = min_t(int, sg_big_buff,
2349 sdp->device->request_queue->max_sectors * 512);
2350 sg_build_reserve(sfp, bufflen);
2351 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2352 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2357 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2362 prev_fp = sdp->headfp;
2364 sdp->headfp = prev_fp->nextfp;
2366 while ((fp = prev_fp->nextfp)) {
2368 prev_fp->nextfp = fp->nextfp;
2374 if (sfp->reserve.bufflen > 0) {
2376 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2377 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2378 sg_remove_scat(&sfp->reserve);
2380 sfp->parentdp = NULL;
2381 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2385 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2387 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2394 for (srp = sfp->headrp; srp; srp = tsrp) {
2396 if (sg_srp_done(srp, sfp))
2397 sg_finish_rem_req(srp);
2402 unsigned long iflags;
2404 write_lock_irqsave(&sg_index_lock, iflags);
2405 __sg_remove_sfp(sdp, sfp);
2406 if (sdp->detached && (NULL == sdp->headfp)) {
2407 idr_remove(&sg_index_idr, sdp->index);
2411 write_unlock_irqrestore(&sg_index_lock, iflags);
2413 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2414 /* only bump the access_count if we actually succeeded in
2415 * throwing another counter on the host module */
2416 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2417 sfp->closed = 1; /* flag dirty state on this fd */
2418 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2425 sg_res_in_use(Sg_fd * sfp)
2427 const Sg_request *srp;
2428 unsigned long iflags;
2430 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2431 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2434 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2438 /* The size fetched (value output via retSzp) set when non-NULL return */
2439 static struct page *
2440 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2442 struct page *resp = NULL;
2447 if ((rqSz <= 0) || (NULL == retSzp))
2451 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_COMP | __GFP_NOWARN;
2453 page_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
2455 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2456 order++, a_size <<= 1) ;
2457 resSz = a_size; /* rounded up if necessary */
2458 resp = alloc_pages(page_mask, order);
2459 while ((!resp) && order) {
2461 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2462 resp = alloc_pages(page_mask, order); /* try half */
2466 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2467 memset(page_address(resp), 0, resSz);
2474 sg_page_free(struct page *page, int size)
2480 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2481 order++, a_size <<= 1) ;
2482 __free_pages(page, order);
2485 #ifndef MAINTENANCE_IN_CMD
2486 #define MAINTENANCE_IN_CMD 0xa3
2489 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2490 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2491 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2492 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2496 sg_allow_access(unsigned char opcode, char dev_type)
2500 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2502 for (k = 0; k < sizeof (allow_ops); ++k) {
2503 if (opcode == allow_ops[k])
2509 #ifdef CONFIG_SCSI_PROC_FS
2511 sg_idr_max_id(int id, void *p, void *data)
2525 unsigned long iflags;
2527 read_lock_irqsave(&sg_index_lock, iflags);
2528 idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
2529 read_unlock_irqrestore(&sg_index_lock, iflags);
2530 return k + 1; /* origin 1 */
2538 unsigned long iflags;
2540 read_lock_irqsave(&sg_index_lock, iflags);
2541 sdp = idr_find(&sg_index_idr, dev);
2542 read_unlock_irqrestore(&sg_index_lock, iflags);
2547 #ifdef CONFIG_SCSI_PROC_FS
2549 static struct proc_dir_entry *sg_proc_sgp = NULL;
2551 static char sg_proc_sg_dirname[] = "scsi/sg";
2553 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2555 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2556 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2557 size_t count, loff_t *off);
2558 static struct file_operations adio_fops = {
2559 /* .owner, .read and .llseek added in sg_proc_init() */
2560 .open = sg_proc_single_open_adio,
2561 .write = sg_proc_write_adio,
2562 .release = single_release,
2565 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2566 static ssize_t sg_proc_write_dressz(struct file *filp,
2567 const char __user *buffer, size_t count, loff_t *off);
2568 static struct file_operations dressz_fops = {
2569 .open = sg_proc_single_open_dressz,
2570 .write = sg_proc_write_dressz,
2571 .release = single_release,
2574 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2575 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2576 static struct file_operations version_fops = {
2577 .open = sg_proc_single_open_version,
2578 .release = single_release,
2581 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2582 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2583 static struct file_operations devhdr_fops = {
2584 .open = sg_proc_single_open_devhdr,
2585 .release = single_release,
2588 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2589 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2590 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2591 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2592 static void dev_seq_stop(struct seq_file *s, void *v);
2593 static struct file_operations dev_fops = {
2594 .open = sg_proc_open_dev,
2595 .release = seq_release,
2597 static struct seq_operations dev_seq_ops = {
2598 .start = dev_seq_start,
2599 .next = dev_seq_next,
2600 .stop = dev_seq_stop,
2601 .show = sg_proc_seq_show_dev,
2604 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2605 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2606 static struct file_operations devstrs_fops = {
2607 .open = sg_proc_open_devstrs,
2608 .release = seq_release,
2610 static struct seq_operations devstrs_seq_ops = {
2611 .start = dev_seq_start,
2612 .next = dev_seq_next,
2613 .stop = dev_seq_stop,
2614 .show = sg_proc_seq_show_devstrs,
2617 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2618 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2619 static struct file_operations debug_fops = {
2620 .open = sg_proc_open_debug,
2621 .release = seq_release,
2623 static struct seq_operations debug_seq_ops = {
2624 .start = dev_seq_start,
2625 .next = dev_seq_next,
2626 .stop = dev_seq_stop,
2627 .show = sg_proc_seq_show_debug,
2631 struct sg_proc_leaf {
2633 struct file_operations * fops;
2636 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2637 {"allow_dio", &adio_fops},
2638 {"debug", &debug_fops},
2639 {"def_reserved_size", &dressz_fops},
2640 {"device_hdr", &devhdr_fops},
2641 {"devices", &dev_fops},
2642 {"device_strs", &devstrs_fops},
2643 {"version", &version_fops}
2650 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2651 struct proc_dir_entry *pdep;
2652 struct sg_proc_leaf * leaf;
2654 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2657 for (k = 0; k < num_leaves; ++k) {
2658 leaf = &sg_proc_leaf_arr[k];
2659 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2660 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2662 leaf->fops->owner = THIS_MODULE,
2663 leaf->fops->read = seq_read,
2664 leaf->fops->llseek = seq_lseek,
2665 pdep->proc_fops = leaf->fops;
2672 sg_proc_cleanup(void)
2675 int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
2679 for (k = 0; k < num_leaves; ++k)
2680 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2681 remove_proc_entry(sg_proc_sg_dirname, NULL);
2685 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2687 seq_printf(s, "%d\n", *((int *)s->private));
2691 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2693 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2697 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2698 size_t count, loff_t *off)
2703 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2705 num = (count < 10) ? count : 10;
2706 if (copy_from_user(buff, buffer, num))
2709 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2713 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2715 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2719 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2720 size_t count, loff_t *off)
2723 unsigned long k = ULONG_MAX;
2726 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2728 num = (count < 10) ? count : 10;
2729 if (copy_from_user(buff, buffer, num))
2732 k = simple_strtoul(buff, NULL, 10);
2733 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2740 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2742 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2747 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2749 return single_open(file, sg_proc_seq_show_version, NULL);
2752 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2754 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2759 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2761 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2764 struct sg_proc_deviter {
2769 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2771 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2778 it->max = sg_last_dev();
2779 if (it->index >= it->max)
2784 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2786 struct sg_proc_deviter * it = s->private;
2789 return (it->index < it->max) ? it : NULL;
2792 static void dev_seq_stop(struct seq_file *s, void *v)
2797 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2799 return seq_open(file, &dev_seq_ops);
2802 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2804 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2806 struct scsi_device *scsidp;
2808 sdp = it ? sg_get_dev(it->index) : NULL;
2809 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2810 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2811 scsidp->host->host_no, scsidp->channel,
2812 scsidp->id, scsidp->lun, (int) scsidp->type,
2814 (int) scsidp->queue_depth,
2815 (int) scsidp->device_busy,
2816 (int) scsi_device_online(scsidp));
2818 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2822 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2824 return seq_open(file, &devstrs_seq_ops);
2827 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2829 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2831 struct scsi_device *scsidp;
2833 sdp = it ? sg_get_dev(it->index) : NULL;
2834 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2835 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2836 scsidp->vendor, scsidp->model, scsidp->rev);
2838 seq_printf(s, "<no active device>\n");
2842 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2844 int k, m, new_interface, blen, usg;
2847 const sg_io_hdr_t *hp;
2851 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2852 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2853 "(res)sgat=%d low_dma=%d\n", k + 1,
2854 jiffies_to_msecs(fp->timeout),
2855 fp->reserve.bufflen,
2856 (int) fp->reserve.k_use_sg,
2858 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2859 (int) fp->cmd_q, (int) fp->force_packid,
2860 (int) fp->keep_orphan, (int) fp->closed);
2861 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2863 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2864 if (srp->res_used) {
2865 if (new_interface &&
2866 (SG_FLAG_MMAP_IO & hp->flags))
2871 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2877 blen = srp->data.bufflen;
2878 usg = srp->data.k_use_sg;
2879 seq_printf(s, srp->done ?
2880 ((1 == srp->done) ? "rcv:" : "fin:")
2882 seq_printf(s, " id=%d blen=%d",
2883 srp->header.pack_id, blen);
2885 seq_printf(s, " dur=%d", hp->duration);
2887 ms = jiffies_to_msecs(jiffies);
2888 seq_printf(s, " t_o/elap=%d/%d",
2889 (new_interface ? hp->timeout :
2890 jiffies_to_msecs(fp->timeout)),
2891 (ms > hp->duration ? ms - hp->duration : 0));
2893 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2894 (int) srp->data.cmd_opcode);
2897 seq_printf(s, " No requests active\n");
2901 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2903 return seq_open(file, &debug_seq_ops);
2906 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2908 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2911 if (it && (0 == it->index)) {
2912 seq_printf(s, "max_active_device=%d(origin 1)\n",
2914 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2916 sdp = it ? sg_get_dev(it->index) : NULL;
2918 struct scsi_device *scsidp = sdp->device;
2920 if (NULL == scsidp) {
2921 seq_printf(s, "device %d detached ??\n",
2926 if (sg_get_nth_sfp(sdp, 0)) {
2927 seq_printf(s, " >>> device=%s ",
2928 sdp->disk->disk_name);
2930 seq_printf(s, "detached pending close ");
2933 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2934 scsidp->host->host_no,
2935 scsidp->channel, scsidp->id,
2937 scsidp->host->hostt->emulated);
2938 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2939 sdp->sg_tablesize, sdp->exclude);
2941 sg_proc_debug_helper(s, sdp);
2946 #endif /* CONFIG_SCSI_PROC_FS */
2948 module_init(init_sg);
2949 module_exit(exit_sg);