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 = 30533; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.33"
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/config.h>
32 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/mtio.h>
41 #include <linux/ioctl.h>
42 #include <linux/fcntl.h>
43 #include <linux/init.h>
44 #include <linux/poll.h>
45 #include <linux/smp_lock.h>
46 #include <linux/moduleparam.h>
47 #include <linux/devfs_fs_kernel.h>
48 #include <linux/cdev.h>
49 #include <linux/seq_file.h>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
52 #include <linux/scatterlist.h>
55 #include <scsi/scsi_dbg.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_driver.h>
58 #include <scsi/scsi_ioctl.h>
61 #include "scsi_logging.h"
63 #ifdef CONFIG_SCSI_PROC_FS
64 #include <linux/proc_fs.h>
65 static char *sg_version_date = "20050908";
67 static int sg_proc_init(void);
68 static void sg_proc_cleanup(void);
71 #define SG_ALLOW_DIO_DEF 0
72 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
74 #define SG_MAX_DEVS 32768
77 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
78 * Then when using 32 bit integers x * m may overflow during the calculation.
79 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
80 * calculates the same, but prevents the overflow when both m and d
81 * are "small" numbers (like HZ and USER_HZ).
82 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
85 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
87 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
89 int sg_big_buff = SG_DEF_RESERVED_SIZE;
90 /* N.B. This variable is readable and writeable via
91 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
92 of this size (or less if there is not enough memory) will be reserved
93 for use by this file descriptor. [Deprecated usage: this variable is also
94 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
95 the kernel (i.e. it is not a module).] */
96 static int def_reserved_size = -1; /* picks up init parameter */
97 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
99 #define SG_SECTOR_SZ 512
100 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
102 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
104 static int sg_add(struct class_device *, struct class_interface *);
105 static void sg_remove(struct class_device *, struct class_interface *);
107 static DEFINE_RWLOCK(sg_dev_arr_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 short 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 Sg_fd *headfp; /* first open fd belonging to this device */
166 volatile char detached; /* 0->attached, 1->detached pending removal */
167 volatile char exclude; /* opened for exclusive access */
168 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
169 struct gendisk *disk;
170 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
173 static int sg_fasync(int fd, struct file *filp, int mode);
174 /* tasklet or soft irq callback */
175 static void sg_cmd_done(void *data, char *sense, int result, int resid);
176 static int sg_start_req(Sg_request * srp);
177 static void sg_finish_rem_req(Sg_request * srp);
178 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
179 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
181 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
183 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
184 int blocking, int read_only, Sg_request ** o_srp);
185 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
186 unsigned char *cmnd, int timeout, int blocking);
187 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
188 int wr_xf, int *countp, unsigned char __user **up);
189 static int sg_write_xfer(Sg_request * srp);
190 static int sg_read_xfer(Sg_request * srp);
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 struct page *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
197 static void sg_page_free(struct page *page, int size);
198 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
199 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
200 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
201 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
202 static Sg_request *sg_add_request(Sg_fd * sfp);
203 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
204 static int sg_res_in_use(Sg_fd * sfp);
205 static int sg_allow_access(unsigned char opcode, char dev_type);
206 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
207 static Sg_device *sg_get_dev(int dev);
208 #ifdef CONFIG_SCSI_PROC_FS
209 static int sg_last_dev(void);
212 static Sg_device **sg_dev_arr = NULL;
213 static int sg_dev_max;
214 static int sg_nr_dev;
216 #define SZ_SG_HEADER sizeof(struct sg_header)
217 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
218 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
219 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
222 sg_open(struct inode *inode, struct file *filp)
224 int dev = iminor(inode);
225 int flags = filp->f_flags;
226 struct request_queue *q;
232 nonseekable_open(inode, filp);
233 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
234 sdp = sg_get_dev(dev);
235 if ((!sdp) || (!sdp->device))
240 /* This driver's module count bumped by fops_get in <linux/fs.h> */
241 /* Prevent the device driver from vanishing while we sleep */
242 retval = scsi_device_get(sdp->device);
246 if (!((flags & O_NONBLOCK) ||
247 scsi_block_when_processing_errors(sdp->device))) {
249 /* we are in error recovery for this device */
253 if (flags & O_EXCL) {
254 if (O_RDONLY == (flags & O_ACCMODE)) {
255 retval = -EPERM; /* Can't lock it with read only access */
258 if (sdp->headfp && (flags & O_NONBLOCK)) {
263 __wait_event_interruptible(sdp->o_excl_wait,
264 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
266 retval = res; /* -ERESTARTSYS because signal hit process */
269 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
270 if (flags & O_NONBLOCK) {
275 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
278 retval = res; /* -ERESTARTSYS because signal hit process */
286 if (!sdp->headfp) { /* no existing opens on this device */
288 q = sdp->device->request_queue;
289 sdp->sg_tablesize = min(q->max_hw_segments,
290 q->max_phys_segments);
292 if ((sfp = sg_add_sfp(sdp, dev)))
293 filp->private_data = sfp;
296 sdp->exclude = 0; /* undo if error */
303 scsi_device_put(sdp->device);
307 /* Following function was formerly called 'sg_close' */
309 sg_release(struct inode *inode, struct file *filp)
314 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
316 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
317 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
318 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
319 if (!sdp->detached) {
320 scsi_device_put(sdp->device);
323 wake_up_interruptible(&sdp->o_excl_wait);
329 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
334 int req_pack_id = -1;
336 struct sg_header *old_hdr = NULL;
339 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
341 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
342 sdp->disk->disk_name, (int) count));
344 if (!access_ok(VERIFY_WRITE, buf, count))
346 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
347 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
350 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
354 if (old_hdr->reply_len < 0) {
355 if (count >= SZ_SG_IO_HDR) {
356 sg_io_hdr_t *new_hdr;
357 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
362 retval =__copy_from_user
363 (new_hdr, buf, SZ_SG_IO_HDR);
364 req_pack_id = new_hdr->pack_id;
372 req_pack_id = old_hdr->pack_id;
374 srp = sg_get_rq_mark(sfp, req_pack_id);
375 if (!srp) { /* now wait on packet to arrive */
380 if (filp->f_flags & O_NONBLOCK) {
385 retval = 0; /* following macro beats race condition */
386 __wait_event_interruptible(sfp->read_wait,
388 (srp = sg_get_rq_mark(sfp, req_pack_id))),
397 /* -ERESTARTSYS as signal hit process */
401 if (srp->header.interface_id != '\0') {
402 retval = sg_new_read(sfp, buf, count, srp);
407 if (old_hdr == NULL) {
408 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
414 memset(old_hdr, 0, SZ_SG_HEADER);
415 old_hdr->reply_len = (int) hp->timeout;
416 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
417 old_hdr->pack_id = hp->pack_id;
418 old_hdr->twelve_byte =
419 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
420 old_hdr->target_status = hp->masked_status;
421 old_hdr->host_status = hp->host_status;
422 old_hdr->driver_status = hp->driver_status;
423 if ((CHECK_CONDITION & hp->masked_status) ||
424 (DRIVER_SENSE & hp->driver_status))
425 memcpy(old_hdr->sense_buffer, srp->sense_b,
426 sizeof (old_hdr->sense_buffer));
427 switch (hp->host_status) {
428 /* This setup of 'result' is for backward compatibility and is best
429 ignored by the user who should use target, host + driver status */
431 case DID_PASSTHROUGH:
438 old_hdr->result = EBUSY;
445 old_hdr->result = EIO;
448 old_hdr->result = (srp->sense_b[0] == 0 &&
449 hp->masked_status == GOOD) ? 0 : EIO;
452 old_hdr->result = EIO;
456 /* Now copy the result back to the user buffer. */
457 if (count >= SZ_SG_HEADER) {
458 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
463 if (count > old_hdr->reply_len)
464 count = old_hdr->reply_len;
465 if (count > SZ_SG_HEADER) {
466 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
472 count = (old_hdr->result == 0) ? 0 : -EIO;
473 sg_finish_rem_req(srp);
481 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
483 sg_io_hdr_t *hp = &srp->header;
487 if (count < SZ_SG_IO_HDR) {
492 if ((hp->mx_sb_len > 0) && hp->sbp) {
493 if ((CHECK_CONDITION & hp->masked_status) ||
494 (DRIVER_SENSE & hp->driver_status)) {
495 int sb_len = SCSI_SENSE_BUFFERSIZE;
496 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
497 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
498 len = (len > sb_len) ? sb_len : len;
499 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
506 if (hp->masked_status || hp->host_status || hp->driver_status)
507 hp->info |= SG_INFO_CHECK;
508 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
512 err = sg_read_xfer(srp);
514 sg_finish_rem_req(srp);
515 return (0 == err) ? count : err;
519 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
521 int mxsize, cmd_size, k;
522 int input_size, blocking;
523 unsigned char opcode;
527 struct sg_header old_hdr;
529 unsigned char cmnd[MAX_COMMAND_SIZE];
531 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
533 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
534 sdp->disk->disk_name, (int) count));
537 if (!((filp->f_flags & O_NONBLOCK) ||
538 scsi_block_when_processing_errors(sdp->device)))
541 if (!access_ok(VERIFY_READ, buf, count))
542 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
543 if (count < SZ_SG_HEADER)
545 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
547 blocking = !(filp->f_flags & O_NONBLOCK);
548 if (old_hdr.reply_len < 0)
549 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
550 if (count < (SZ_SG_HEADER + 6))
551 return -EIO; /* The minimum scsi command length is 6 bytes. */
553 if (!(srp = sg_add_request(sfp))) {
554 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
558 __get_user(opcode, buf);
559 if (sfp->next_cmd_len > 0) {
560 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
561 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
562 sfp->next_cmd_len = 0;
563 sg_remove_request(sfp, srp);
566 cmd_size = sfp->next_cmd_len;
567 sfp->next_cmd_len = 0; /* reset so only this write() effected */
569 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
570 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
573 SCSI_LOG_TIMEOUT(4, printk(
574 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
575 /* Determine buffer size. */
576 input_size = count - cmd_size;
577 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
578 mxsize -= SZ_SG_HEADER;
579 input_size -= SZ_SG_HEADER;
580 if (input_size < 0) {
581 sg_remove_request(sfp, srp);
582 return -EIO; /* User did not pass enough bytes for this command. */
585 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
586 hp->cmd_len = (unsigned char) cmd_size;
590 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
591 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
593 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
594 hp->dxfer_len = mxsize;
595 hp->dxferp = (char __user *)buf + cmd_size;
597 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
598 hp->flags = input_size; /* structure abuse ... */
599 hp->pack_id = old_hdr.pack_id;
601 if (__copy_from_user(cmnd, buf, cmd_size))
604 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
605 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
606 * is a non-zero input_size, so emit a warning.
608 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
609 if (printk_ratelimit())
611 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
612 "guessing data in;\n" KERN_WARNING " "
613 "program %s not setting count and/or reply_len properly\n",
614 old_hdr.reply_len - (int)SZ_SG_HEADER,
615 input_size, (unsigned int) cmnd[0],
617 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
618 return (k < 0) ? k : count;
622 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
623 int blocking, int read_only, Sg_request ** o_srp)
628 unsigned char cmnd[MAX_COMMAND_SIZE];
630 unsigned long ul_timeout;
632 if (count < SZ_SG_IO_HDR)
634 if (!access_ok(VERIFY_READ, buf, count))
635 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
637 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
638 if (!(srp = sg_add_request(sfp))) {
639 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
643 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
644 sg_remove_request(sfp, srp);
647 if (hp->interface_id != 'S') {
648 sg_remove_request(sfp, srp);
651 if (hp->flags & SG_FLAG_MMAP_IO) {
652 if (hp->dxfer_len > sfp->reserve.bufflen) {
653 sg_remove_request(sfp, srp);
654 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
656 if (hp->flags & SG_FLAG_DIRECT_IO) {
657 sg_remove_request(sfp, srp);
658 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
660 if (sg_res_in_use(sfp)) {
661 sg_remove_request(sfp, srp);
662 return -EBUSY; /* reserve buffer already being used */
665 ul_timeout = msecs_to_jiffies(srp->header.timeout);
666 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
667 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
668 sg_remove_request(sfp, srp);
671 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
672 sg_remove_request(sfp, srp);
673 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
675 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
676 sg_remove_request(sfp, srp);
680 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
681 sg_remove_request(sfp, srp);
684 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
693 sg_common_write(Sg_fd * sfp, Sg_request * srp,
694 unsigned char *cmnd, int timeout, int blocking)
697 Sg_device *sdp = sfp->parentdp;
698 sg_io_hdr_t *hp = &srp->header;
700 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
702 hp->masked_status = 0;
706 hp->driver_status = 0;
708 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
709 (int) cmnd[0], (int) hp->cmd_len));
711 if ((k = sg_start_req(srp))) {
712 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
713 sg_finish_rem_req(srp);
714 return k; /* probably out of space --> ENOMEM */
716 if ((k = sg_write_xfer(srp))) {
717 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
718 sg_finish_rem_req(srp);
722 sg_finish_rem_req(srp);
726 switch (hp->dxfer_direction) {
727 case SG_DXFER_TO_FROM_DEV:
728 case SG_DXFER_FROM_DEV:
729 data_dir = DMA_FROM_DEVICE;
731 case SG_DXFER_TO_DEV:
732 data_dir = DMA_TO_DEVICE;
734 case SG_DXFER_UNKNOWN:
735 data_dir = DMA_BIDIRECTIONAL;
741 hp->duration = jiffies_to_msecs(jiffies);
742 /* Now send everything of to mid-level. The next time we hear about this
743 packet is when sg_cmd_done() is called (i.e. a callback). */
744 if (scsi_execute_async(sdp->device, cmnd, hp->cmd_len, data_dir, srp->data.buffer,
745 hp->dxfer_len, srp->data.k_use_sg, timeout,
746 SG_DEFAULT_RETRIES, srp, sg_cmd_done,
748 SCSI_LOG_TIMEOUT(1, printk("sg_write: scsi_execute_async failed\n"));
750 * most likely out of mem, but could also be a bad map
758 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
760 unsigned long iflags;
763 read_lock_irqsave(&sfp->rq_list_lock, iflags);
765 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
770 sg_ioctl(struct inode *inode, struct file *filp,
771 unsigned int cmd_in, unsigned long arg)
773 void __user *p = (void __user *)arg;
775 int result, val, read_only;
779 unsigned long iflags;
781 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
783 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
784 sdp->disk->disk_name, (int) cmd_in));
785 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
790 int blocking = 1; /* ignore O_NONBLOCK flag */
794 if (!scsi_block_when_processing_errors(sdp->device))
796 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
799 sg_new_write(sfp, p, SZ_SG_IO_HDR,
800 blocking, read_only, &srp);
803 srp->sg_io_owned = 1;
805 result = 0; /* following macro to beat race condition */
806 __wait_event_interruptible(sfp->read_wait,
807 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
812 return 0; /* request packet dropped already */
816 return result; /* -ERESTARTSYS because signal hit process */
818 write_lock_irqsave(&sfp->rq_list_lock, iflags);
820 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
821 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
822 return (result < 0) ? result : 0;
825 result = get_user(val, ip);
830 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
831 val = MULDIV (INT_MAX, USER_HZ, HZ);
832 sfp->timeout_user = val;
833 sfp->timeout = MULDIV (val, HZ, USER_HZ);
836 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
837 /* strange ..., for backward compatibility */
838 return sfp->timeout_user;
839 case SG_SET_FORCE_LOW_DMA:
840 result = get_user(val, ip);
845 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
846 val = (int) sfp->reserve.bufflen;
847 sg_remove_scat(&sfp->reserve);
848 sg_build_reserve(sfp, val);
853 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
857 return put_user((int) sfp->low_dma, ip);
859 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
862 sg_scsi_id_t __user *sg_idp = p;
866 __put_user((int) sdp->device->host->host_no,
868 __put_user((int) sdp->device->channel,
870 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
871 __put_user((int) sdp->device->lun, &sg_idp->lun);
872 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
873 __put_user((short) sdp->device->host->cmd_per_lun,
874 &sg_idp->h_cmd_per_lun);
875 __put_user((short) sdp->device->queue_depth,
876 &sg_idp->d_queue_depth);
877 __put_user(0, &sg_idp->unused[0]);
878 __put_user(0, &sg_idp->unused[1]);
881 case SG_SET_FORCE_PACK_ID:
882 result = get_user(val, ip);
885 sfp->force_packid = val ? 1 : 0;
888 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
890 read_lock_irqsave(&sfp->rq_list_lock, iflags);
891 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
892 if ((1 == srp->done) && (!srp->sg_io_owned)) {
893 read_unlock_irqrestore(&sfp->rq_list_lock,
895 __put_user(srp->header.pack_id, ip);
899 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
902 case SG_GET_NUM_WAITING:
903 read_lock_irqsave(&sfp->rq_list_lock, iflags);
904 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
905 if ((1 == srp->done) && (!srp->sg_io_owned))
908 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
909 return put_user(val, ip);
910 case SG_GET_SG_TABLESIZE:
911 return put_user(sdp->sg_tablesize, ip);
912 case SG_SET_RESERVED_SIZE:
913 result = get_user(val, ip);
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 = (int) sfp->reserve.bufflen;
927 return put_user(val, ip);
928 case SG_SET_COMMAND_Q:
929 result = get_user(val, ip);
932 sfp->cmd_q = val ? 1 : 0;
934 case SG_GET_COMMAND_Q:
935 return put_user((int) sfp->cmd_q, ip);
936 case SG_SET_KEEP_ORPHAN:
937 result = get_user(val, ip);
940 sfp->keep_orphan = val;
942 case SG_GET_KEEP_ORPHAN:
943 return put_user((int) sfp->keep_orphan, ip);
944 case SG_NEXT_CMD_LEN:
945 result = get_user(val, ip);
948 sfp->next_cmd_len = (val > 0) ? val : 0;
950 case SG_GET_VERSION_NUM:
951 return put_user(sg_version_num, ip);
952 case SG_GET_ACCESS_COUNT:
953 /* faked - we don't have a real access count anymore */
954 val = (sdp->device ? 1 : 0);
955 return put_user(val, ip);
956 case SG_GET_REQUEST_TABLE:
957 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
960 sg_req_info_t *rinfo;
963 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
967 read_lock_irqsave(&sfp->rq_list_lock, iflags);
968 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
969 ++val, srp = srp ? srp->nextrp : srp) {
970 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
972 rinfo[val].req_state = srp->done + 1;
974 srp->header.masked_status &
975 srp->header.host_status &
976 srp->header.driver_status;
978 rinfo[val].duration =
979 srp->header.duration;
981 ms = jiffies_to_msecs(jiffies);
982 rinfo[val].duration =
983 (ms > srp->header.duration) ?
984 (ms - srp->header.duration) : 0;
986 rinfo[val].orphan = srp->orphan;
987 rinfo[val].sg_io_owned =
995 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
996 result = __copy_to_user(p, rinfo,
997 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
998 result = result ? -EFAULT : 0;
1002 case SG_EMULATED_HOST:
1005 return put_user(sdp->device->host->hostt->emulated, ip);
1009 if (filp->f_flags & O_NONBLOCK) {
1010 if (scsi_host_in_recovery(sdp->device->host))
1012 } else if (!scsi_block_when_processing_errors(sdp->device))
1014 result = get_user(val, ip);
1017 if (SG_SCSI_RESET_NOTHING == val)
1020 case SG_SCSI_RESET_DEVICE:
1021 val = SCSI_TRY_RESET_DEVICE;
1023 case SG_SCSI_RESET_BUS:
1024 val = SCSI_TRY_RESET_BUS;
1026 case SG_SCSI_RESET_HOST:
1027 val = SCSI_TRY_RESET_HOST;
1032 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1034 return (scsi_reset_provider(sdp->device, val) ==
1035 SUCCESS) ? 0 : -EIO;
1036 case SCSI_IOCTL_SEND_COMMAND:
1040 unsigned char opcode = WRITE_6;
1041 Scsi_Ioctl_Command __user *siocp = p;
1043 if (copy_from_user(&opcode, siocp->data, 1))
1045 if (!sg_allow_access(opcode, sdp->device->type))
1048 return scsi_ioctl_send_command(sdp->device, p);
1050 result = get_user(val, ip);
1053 sdp->sgdebug = (char) val;
1055 case SCSI_IOCTL_GET_IDLUN:
1056 case SCSI_IOCTL_GET_BUS_NUMBER:
1057 case SCSI_IOCTL_PROBE_HOST:
1058 case SG_GET_TRANSFORM:
1061 return scsi_ioctl(sdp->device, cmd_in, p);
1064 return -EPERM; /* don't know so take safe approach */
1065 return scsi_ioctl(sdp->device, cmd_in, p);
1069 #ifdef CONFIG_COMPAT
1070 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1074 struct scsi_device *sdev;
1076 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1080 if (sdev->host->hostt->compat_ioctl) {
1083 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1088 return -ENOIOCTLCMD;
1093 sg_poll(struct file *filp, poll_table * wait)
1095 unsigned int res = 0;
1100 unsigned long iflags;
1102 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1105 poll_wait(filp, &sfp->read_wait, wait);
1106 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1107 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1108 /* if any read waiting, flag it */
1109 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1110 res = POLLIN | POLLRDNORM;
1113 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1117 else if (!sfp->cmd_q) {
1119 res |= POLLOUT | POLLWRNORM;
1120 } else if (count < SG_MAX_QUEUE)
1121 res |= POLLOUT | POLLWRNORM;
1122 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1123 sdp->disk->disk_name, (int) res));
1128 sg_fasync(int fd, struct file *filp, int mode)
1134 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1136 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1137 sdp->disk->disk_name, mode));
1139 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1140 return (retval < 0) ? retval : 0;
1143 /* When startFinish==1 increments page counts for pages other than the
1144 first of scatter gather elements obtained from alloc_pages().
1145 When startFinish==0 decrements ... */
1147 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1149 struct scatterlist *sg = rsv_schp->buffer;
1153 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1154 startFinish, rsv_schp->k_use_sg));
1155 /* N.B. correction _not_ applied to base page of each allocation */
1156 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sg) {
1157 for (m = PAGE_SIZE; m < sg->length; m += PAGE_SIZE) {
1162 if (page_count(page) > 0)
1169 static struct page *
1170 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1173 struct page *page = NOPAGE_SIGBUS;
1174 unsigned long offset, len, sa;
1175 Sg_scatter_hold *rsv_schp;
1176 struct scatterlist *sg;
1179 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1181 rsv_schp = &sfp->reserve;
1182 offset = addr - vma->vm_start;
1183 if (offset >= rsv_schp->bufflen)
1185 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1186 offset, rsv_schp->k_use_sg));
1187 sg = rsv_schp->buffer;
1189 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1191 len = vma->vm_end - sa;
1192 len = (len < sg->length) ? len : sg->length;
1195 get_page(page); /* increment page count */
1203 *type = VM_FAULT_MINOR;
1207 static struct vm_operations_struct sg_mmap_vm_ops = {
1208 .nopage = sg_vma_nopage,
1212 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1215 unsigned long req_sz, len, sa;
1216 Sg_scatter_hold *rsv_schp;
1218 struct scatterlist *sg;
1220 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1222 req_sz = vma->vm_end - vma->vm_start;
1223 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1224 (void *) vma->vm_start, (int) req_sz));
1226 return -EINVAL; /* want no offset */
1227 rsv_schp = &sfp->reserve;
1228 if (req_sz > rsv_schp->bufflen)
1229 return -ENOMEM; /* cannot map more than reserved buffer */
1232 sg = rsv_schp->buffer;
1233 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1235 len = vma->vm_end - sa;
1236 len = (len < sg->length) ? len : sg->length;
1240 if (0 == sfp->mmap_called) {
1241 sg_rb_correct4mmap(rsv_schp, 1); /* do only once per fd lifetime */
1242 sfp->mmap_called = 1;
1244 vma->vm_flags |= VM_RESERVED;
1245 vma->vm_private_data = sfp;
1246 vma->vm_ops = &sg_mmap_vm_ops;
1250 /* This function is a "bottom half" handler that is called by the
1251 * mid level when a command is completed (or has failed). */
1253 sg_cmd_done(void *data, char *sense, int result, int resid)
1255 Sg_request *srp = data;
1256 Sg_device *sdp = NULL;
1258 unsigned long iflags;
1262 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1265 sfp = srp->parentfp;
1267 sdp = sfp->parentdp;
1268 if ((NULL == sdp) || sdp->detached) {
1269 printk(KERN_INFO "sg_cmd_done: device detached\n");
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 memcpy(srp->sense_b, sense, sizeof (srp->sense_b));
1284 srp->header.status = 0xff & result;
1285 srp->header.masked_status = status_byte(result);
1286 srp->header.msg_status = msg_byte(result);
1287 srp->header.host_status = host_byte(result);
1288 srp->header.driver_status = driver_byte(result);
1289 if ((sdp->sgdebug > 0) &&
1290 ((CHECK_CONDITION == srp->header.masked_status) ||
1291 (COMMAND_TERMINATED == srp->header.masked_status)))
1292 __scsi_print_sense("sg_cmd_done", sense,
1293 SCSI_SENSE_BUFFERSIZE);
1295 /* Following if statement is a patch supplied by Eric Youngdale */
1296 if (driver_byte(result) != 0
1297 && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
1298 && !scsi_sense_is_deferred(&sshdr)
1299 && sshdr.sense_key == UNIT_ATTENTION
1300 && sdp->device->removable) {
1301 /* Detected possible disc change. Set the bit - this */
1302 /* may be used if there are filesystems using this device */
1303 sdp->device->changed = 1;
1306 /* Rely on write phase to clean out srp status values, so no "else" */
1308 if (sfp->closed) { /* whoops this fd already released, cleanup */
1309 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1310 sg_finish_rem_req(srp);
1312 if (NULL == sfp->headrp) {
1313 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1314 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1315 scsi_device_put(sdp->device);
1319 } else if (srp && srp->orphan) {
1320 if (sfp->keep_orphan)
1321 srp->sg_io_owned = 0;
1323 sg_finish_rem_req(srp);
1328 /* Now wake up any sg_read() that is waiting for this packet. */
1329 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1330 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1332 wake_up_interruptible(&sfp->read_wait);
1333 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1337 static struct file_operations sg_fops = {
1338 .owner = THIS_MODULE,
1343 #ifdef CONFIG_COMPAT
1344 .compat_ioctl = sg_compat_ioctl,
1348 .release = sg_release,
1349 .fasync = sg_fasync,
1352 static struct class *sg_sysfs_class;
1354 static int sg_sysfs_valid = 0;
1356 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1358 struct request_queue *q = scsidp->request_queue;
1360 unsigned long iflags;
1361 void *old_sg_dev_arr = NULL;
1364 sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
1366 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1370 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1371 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1373 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1374 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1376 tmp_da = kzalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1377 if (unlikely(!tmp_da))
1380 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1381 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1382 old_sg_dev_arr = sg_dev_arr;
1383 sg_dev_arr = tmp_da;
1384 sg_dev_max = tmp_dev_max;
1387 for (k = 0; k < sg_dev_max; k++)
1390 if (unlikely(k >= SG_MAX_DEVS))
1393 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1394 sprintf(disk->disk_name, "sg%d", k);
1395 disk->first_minor = k;
1397 sdp->device = scsidp;
1398 init_waitqueue_head(&sdp->o_excl_wait);
1399 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1402 sg_dev_arr[k] = sdp;
1403 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1409 kfree(old_sg_dev_arr);
1413 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1418 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1419 sdev_printk(KERN_WARNING, scsidp,
1420 "Unable to attach sg device type=%d, minor "
1421 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
1427 sg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
1429 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1430 struct gendisk *disk;
1431 Sg_device *sdp = NULL;
1432 struct cdev * cdev = NULL;
1435 disk = alloc_disk(1);
1437 printk(KERN_WARNING "alloc_disk failed\n");
1440 disk->major = SCSI_GENERIC_MAJOR;
1443 cdev = cdev_alloc();
1445 printk(KERN_WARNING "cdev_alloc failed\n");
1448 cdev->owner = THIS_MODULE;
1449 cdev->ops = &sg_fops;
1451 error = sg_alloc(disk, scsidp);
1453 printk(KERN_WARNING "sg_alloc failed\n");
1457 sdp = sg_dev_arr[k];
1459 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1460 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1461 "%s/generic", scsidp->devfs_name);
1462 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1464 devfs_remove("%s/generic", scsidp->devfs_name);
1468 if (sg_sysfs_valid) {
1469 struct class_device * sg_class_member;
1471 sg_class_member = class_device_create(sg_sysfs_class, NULL,
1472 MKDEV(SCSI_GENERIC_MAJOR, k),
1475 if (IS_ERR(sg_class_member))
1476 printk(KERN_WARNING "sg_add: "
1477 "class_device_create failed\n");
1478 class_set_devdata(sg_class_member, sdp);
1479 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1480 &sg_class_member->kobj, "generic");
1482 printk(KERN_ERR "sg_add: unable to make symlink "
1483 "'generic' back to sg%d\n", k);
1485 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1487 sdev_printk(KERN_NOTICE, scsidp,
1488 "Attached scsi generic sg%d type %d\n", k,scsidp->type);
1500 sg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
1502 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1503 Sg_device *sdp = NULL;
1504 unsigned long iflags;
1511 if (NULL == sg_dev_arr)
1514 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1515 for (k = 0; k < sg_dev_max; k++) {
1516 sdp = sg_dev_arr[k];
1517 if ((NULL == sdp) || (sdp->device != scsidp))
1518 continue; /* dirty but lowers nesting */
1521 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1523 for (srp = sfp->headrp; srp; srp = tsrp) {
1525 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1526 sg_finish_rem_req(srp);
1529 scsi_device_put(sdp->device);
1530 __sg_remove_sfp(sdp, sfp);
1533 wake_up_interruptible(&sfp->read_wait);
1534 kill_fasync(&sfp->async_qp, SIGPOLL,
1538 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1539 if (NULL == sdp->headfp) {
1540 sg_dev_arr[k] = NULL;
1542 } else { /* nothing active, simple case */
1543 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1544 sg_dev_arr[k] = NULL;
1549 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1552 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1553 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, k));
1554 cdev_del(sdp->cdev);
1556 devfs_remove("%s/generic", scsidp->devfs_name);
1557 put_disk(sdp->disk);
1559 if (NULL == sdp->headfp)
1560 kfree((char *) sdp);
1564 msleep(10); /* dirty detach so delay device destruction */
1567 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1568 * of sysfs parameters (which module_param doesn't yet support).
1569 * Sysfs parameters defined explicitly below.
1571 module_param_named(def_reserved_size, def_reserved_size, int, S_IRUGO);
1572 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1574 MODULE_AUTHOR("Douglas Gilbert");
1575 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1576 MODULE_LICENSE("GPL");
1577 MODULE_VERSION(SG_VERSION_STR);
1579 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1580 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1587 if (def_reserved_size >= 0)
1588 sg_big_buff = def_reserved_size;
1590 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1594 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1595 if ( IS_ERR(sg_sysfs_class) ) {
1596 rc = PTR_ERR(sg_sysfs_class);
1600 rc = scsi_register_interface(&sg_interface);
1602 #ifdef CONFIG_SCSI_PROC_FS
1604 #endif /* CONFIG_SCSI_PROC_FS */
1607 class_destroy(sg_sysfs_class);
1609 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1616 #ifdef CONFIG_SCSI_PROC_FS
1618 #endif /* CONFIG_SCSI_PROC_FS */
1619 scsi_unregister_interface(&sg_interface);
1620 class_destroy(sg_sysfs_class);
1622 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1624 kfree((char *)sg_dev_arr);
1630 sg_start_req(Sg_request * srp)
1633 Sg_fd *sfp = srp->parentfp;
1634 sg_io_hdr_t *hp = &srp->header;
1635 int dxfer_len = (int) hp->dxfer_len;
1636 int dxfer_dir = hp->dxfer_direction;
1637 Sg_scatter_hold *req_schp = &srp->data;
1638 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1640 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1641 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1643 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1644 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1645 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1646 res = sg_build_direct(srp, sfp, dxfer_len);
1647 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1650 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1651 sg_link_reserve(sfp, srp, dxfer_len);
1653 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1655 sg_remove_scat(req_schp);
1663 sg_finish_rem_req(Sg_request * srp)
1665 Sg_fd *sfp = srp->parentfp;
1666 Sg_scatter_hold *req_schp = &srp->data;
1668 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1670 sg_unlink_reserve(sfp, srp);
1672 sg_remove_scat(req_schp);
1673 sg_remove_request(sfp, srp);
1677 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1679 int sg_bufflen = tablesize * sizeof(struct scatterlist);
1680 gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
1683 * TODO: test without low_dma, we should not need it since
1684 * the block layer will bounce the buffer for us
1686 * XXX(hch): we shouldn't need GFP_DMA for the actual S/G list.
1689 gfp_flags |= GFP_DMA;
1690 schp->buffer = kzalloc(sg_bufflen, gfp_flags);
1693 schp->sglist_len = sg_bufflen;
1694 return tablesize; /* number of scat_gath elements allocated */
1697 #ifdef SG_ALLOW_DIO_CODE
1698 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1699 /* TODO: hopefully we can use the generic block layer code */
1701 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1702 - mapping of all pages not successful
1703 (i.e., either completely successful or fails)
1706 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1707 unsigned long uaddr, size_t count, int rw)
1709 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1710 unsigned long start = uaddr >> PAGE_SHIFT;
1711 const int nr_pages = end - start;
1713 struct page **pages;
1715 /* User attempted Overflow! */
1716 if ((uaddr + count) < uaddr)
1720 if (nr_pages > max_pages)
1727 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1730 /* Try to fault in all of the necessary pages */
1731 down_read(¤t->mm->mmap_sem);
1732 /* rw==READ means read from drive, write into memory area */
1733 res = get_user_pages(
1739 0, /* don't force */
1742 up_read(¤t->mm->mmap_sem);
1744 /* Errors and no page mapped should return here */
1748 for (i=0; i < nr_pages; i++) {
1749 /* FIXME: flush superflous for rw==READ,
1750 * probably wrong function for rw==WRITE
1752 flush_dcache_page(pages[i]);
1753 /* ?? Is locking needed? I don't think so */
1754 /* if (TestSetPageLocked(pages[i]))
1758 sgl[0].page = pages[0];
1759 sgl[0].offset = uaddr & ~PAGE_MASK;
1761 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1762 count -= sgl[0].length;
1763 for (i=1; i < nr_pages ; i++) {
1764 sgl[i].page = pages[i];
1765 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1770 sgl[0].length = count;
1778 for (j=0; j < res; j++)
1779 page_cache_release(pages[j]);
1787 /* And unmap them... */
1789 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1794 for (i=0; i < nr_pages; i++) {
1795 struct page *page = sgl[i].page;
1799 /* unlock_page(page); */
1800 /* FIXME: cache flush missing for rw==READ
1801 * FIXME: call the correct reference counting function
1803 page_cache_release(page);
1809 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1813 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1815 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1817 #ifdef SG_ALLOW_DIO_CODE
1818 sg_io_hdr_t *hp = &srp->header;
1819 Sg_scatter_hold *schp = &srp->data;
1820 int sg_tablesize = sfp->parentdp->sg_tablesize;
1821 int mx_sc_elems, res;
1822 struct scsi_device *sdev = sfp->parentdp->device;
1824 if (((unsigned long)hp->dxferp &
1825 queue_dma_alignment(sdev->request_queue)) != 0)
1828 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1829 if (mx_sc_elems <= 0) {
1832 res = st_map_user_pages(schp->buffer, mx_sc_elems,
1833 (unsigned long)hp->dxferp, dxfer_len,
1834 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0);
1837 schp->k_use_sg = res;
1838 schp->dio_in_use = 1;
1839 hp->info |= SG_INFO_DIRECT_IO;
1847 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1849 struct scatterlist *sg;
1850 int ret_sz = 0, k, rem_sz, num, mx_sc_elems;
1851 int sg_tablesize = sfp->parentdp->sg_tablesize;
1852 int blk_size = buff_size;
1853 struct page *p = NULL;
1855 if ((blk_size < 0) || (!sfp))
1858 ++blk_size; /* don't know why */
1859 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1860 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1861 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1862 buff_size, blk_size));
1864 /* N.B. ret_sz carried into this block ... */
1865 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1866 if (mx_sc_elems < 0)
1867 return mx_sc_elems; /* most likely -ENOMEM */
1869 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1870 (rem_sz > 0) && (k < mx_sc_elems);
1871 ++k, rem_sz -= ret_sz, ++sg) {
1873 num = (rem_sz > SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1874 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1879 sg->length = ret_sz;
1881 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1883 } /* end of for loop */
1886 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1888 schp->bufflen = blk_size;
1889 if (rem_sz > 0) /* must have failed */
1896 sg_write_xfer(Sg_request * srp)
1898 sg_io_hdr_t *hp = &srp->header;
1899 Sg_scatter_hold *schp = &srp->data;
1900 struct scatterlist *sg = schp->buffer;
1902 int j, k, onum, usglen, ksglen, res;
1903 int iovec_count = (int) hp->iovec_count;
1904 int dxfer_dir = hp->dxfer_direction;
1906 unsigned char __user *up;
1907 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1909 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1910 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1911 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1912 if (schp->bufflen < num_xfer)
1913 num_xfer = schp->bufflen;
1915 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1917 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1920 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1921 num_xfer, iovec_count, schp->k_use_sg));
1924 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
1929 ksglen = sg->length;
1930 p = page_address(sg->page);
1931 for (j = 0, k = 0; j < onum; ++j) {
1932 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1936 for (; p; ++sg, ksglen = sg->length,
1937 p = page_address(sg->page)) {
1940 if (ksglen > usglen) {
1941 if (usglen >= num_xfer) {
1942 if (__copy_from_user(p, up, num_xfer))
1946 if (__copy_from_user(p, up, usglen))
1952 if (ksglen >= num_xfer) {
1953 if (__copy_from_user(p, up, num_xfer))
1957 if (__copy_from_user(p, up, ksglen))
1963 if (k >= schp->k_use_sg)
1972 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
1973 int wr_xf, int *countp, unsigned char __user **up)
1975 int num_xfer = (int) hp->dxfer_len;
1976 unsigned char __user *p = hp->dxferp;
1980 if (wr_xf && ('\0' == hp->interface_id))
1981 count = (int) hp->flags; /* holds "old" input_size */
1986 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
1989 count = (int) iovec.iov_len;
1991 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2001 sg_remove_scat(Sg_scatter_hold * schp)
2003 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2004 if (schp->buffer && (schp->sglist_len > 0)) {
2005 struct scatterlist *sg = schp->buffer;
2007 if (schp->dio_in_use) {
2008 #ifdef SG_ALLOW_DIO_CODE
2009 st_unmap_user_pages(sg, schp->k_use_sg, TRUE);
2014 for (k = 0; (k < schp->k_use_sg) && sg->page;
2016 SCSI_LOG_TIMEOUT(5, printk(
2017 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2018 k, sg->page, sg->length));
2019 sg_page_free(sg->page, sg->length);
2022 kfree(schp->buffer);
2024 memset(schp, 0, sizeof (*schp));
2028 sg_read_xfer(Sg_request * srp)
2030 sg_io_hdr_t *hp = &srp->header;
2031 Sg_scatter_hold *schp = &srp->data;
2032 struct scatterlist *sg = schp->buffer;
2034 int j, k, onum, usglen, ksglen, res;
2035 int iovec_count = (int) hp->iovec_count;
2036 int dxfer_dir = hp->dxfer_direction;
2038 unsigned char __user *up;
2039 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2041 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2042 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2043 num_xfer = hp->dxfer_len;
2044 if (schp->bufflen < num_xfer)
2045 num_xfer = schp->bufflen;
2047 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2049 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2052 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2053 num_xfer, iovec_count, schp->k_use_sg));
2056 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2061 p = page_address(sg->page);
2062 ksglen = sg->length;
2063 for (j = 0, k = 0; j < onum; ++j) {
2064 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2068 for (; p; ++sg, ksglen = sg->length,
2069 p = page_address(sg->page)) {
2072 if (ksglen > usglen) {
2073 if (usglen >= num_xfer) {
2074 if (__copy_to_user(up, p, num_xfer))
2078 if (__copy_to_user(up, p, usglen))
2084 if (ksglen >= num_xfer) {
2085 if (__copy_to_user(up, p, num_xfer))
2089 if (__copy_to_user(up, p, ksglen))
2095 if (k >= schp->k_use_sg)
2104 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2106 Sg_scatter_hold *schp = &srp->data;
2107 struct scatterlist *sg = schp->buffer;
2110 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2112 if ((!outp) || (num_read_xfer <= 0))
2115 for (k = 0; (k < schp->k_use_sg) && sg->page; ++k, ++sg) {
2117 if (num > num_read_xfer) {
2118 if (__copy_to_user(outp, page_address(sg->page),
2123 if (__copy_to_user(outp, page_address(sg->page),
2126 num_read_xfer -= num;
2127 if (num_read_xfer <= 0)
2137 sg_build_reserve(Sg_fd * sfp, int req_size)
2139 Sg_scatter_hold *schp = &sfp->reserve;
2141 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2143 if (req_size < PAGE_SIZE)
2144 req_size = PAGE_SIZE;
2145 if (0 == sg_build_indirect(schp, sfp, req_size))
2148 sg_remove_scat(schp);
2149 req_size >>= 1; /* divide by 2 */
2150 } while (req_size > (PAGE_SIZE / 2));
2154 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2156 Sg_scatter_hold *req_schp = &srp->data;
2157 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2158 struct scatterlist *sg = rsv_schp->buffer;
2162 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2163 rem = size = (size + 1) & (~1); /* round to even for aha1542 */
2165 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sg) {
2168 sfp->save_scat_len = num;
2170 req_schp->k_use_sg = k + 1;
2171 req_schp->sglist_len = rsv_schp->sglist_len;
2172 req_schp->buffer = rsv_schp->buffer;
2174 req_schp->bufflen = size;
2175 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2181 if (k >= rsv_schp->k_use_sg)
2182 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2186 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2188 Sg_scatter_hold *req_schp = &srp->data;
2189 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2191 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2192 (int) req_schp->k_use_sg));
2193 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2194 struct scatterlist *sg = rsv_schp->buffer;
2196 if (sfp->save_scat_len > 0)
2197 (sg + (req_schp->k_use_sg - 1))->length =
2198 (unsigned) sfp->save_scat_len;
2200 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2202 req_schp->k_use_sg = 0;
2203 req_schp->bufflen = 0;
2204 req_schp->buffer = NULL;
2205 req_schp->sglist_len = 0;
2206 sfp->save_scat_len = 0;
2211 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2214 unsigned long iflags;
2216 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2217 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2218 /* look for requests that are ready + not SG_IO owned */
2219 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2220 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2221 resp->done = 2; /* guard against other readers */
2225 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2229 #ifdef CONFIG_SCSI_PROC_FS
2231 sg_get_nth_request(Sg_fd * sfp, int nth)
2234 unsigned long iflags;
2237 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2238 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2239 ++k, resp = resp->nextrp) ;
2240 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2245 /* always adds to end of list */
2247 sg_add_request(Sg_fd * sfp)
2250 unsigned long iflags;
2252 Sg_request *rp = sfp->req_arr;
2254 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2257 memset(rp, 0, sizeof (Sg_request));
2262 if (0 == sfp->cmd_q)
2263 resp = NULL; /* command queuing disallowed */
2265 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2269 if (k < SG_MAX_QUEUE) {
2270 memset(rp, 0, sizeof (Sg_request));
2272 while (resp->nextrp)
2273 resp = resp->nextrp;
2281 resp->nextrp = NULL;
2282 resp->header.duration = jiffies_to_msecs(jiffies);
2284 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2288 /* Return of 1 for found; 0 for not found */
2290 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2292 Sg_request *prev_rp;
2294 unsigned long iflags;
2297 if ((!sfp) || (!srp) || (!sfp->headrp))
2299 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2300 prev_rp = sfp->headrp;
2301 if (srp == prev_rp) {
2302 sfp->headrp = prev_rp->nextrp;
2303 prev_rp->parentfp = NULL;
2306 while ((rp = prev_rp->nextrp)) {
2308 prev_rp->nextrp = rp->nextrp;
2309 rp->parentfp = NULL;
2316 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2320 #ifdef CONFIG_SCSI_PROC_FS
2322 sg_get_nth_sfp(Sg_device * sdp, int nth)
2325 unsigned long iflags;
2328 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2329 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2330 ++k, resp = resp->nextfp) ;
2331 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2337 sg_add_sfp(Sg_device * sdp, int dev)
2340 unsigned long iflags;
2342 sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
2346 init_waitqueue_head(&sfp->read_wait);
2347 rwlock_init(&sfp->rq_list_lock);
2349 sfp->timeout = SG_DEFAULT_TIMEOUT;
2350 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2351 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2352 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2353 sdp->device->host->unchecked_isa_dma : 1;
2354 sfp->cmd_q = SG_DEF_COMMAND_Q;
2355 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2356 sfp->parentdp = sdp;
2357 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2360 else { /* add to tail of existing list */
2361 Sg_fd *pfp = sdp->headfp;
2366 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2367 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2368 sg_build_reserve(sfp, sg_big_buff);
2369 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2370 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2375 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2380 prev_fp = sdp->headfp;
2382 sdp->headfp = prev_fp->nextfp;
2384 while ((fp = prev_fp->nextfp)) {
2386 prev_fp->nextfp = fp->nextfp;
2392 if (sfp->reserve.bufflen > 0) {
2394 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2395 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2396 if (sfp->mmap_called)
2397 sg_rb_correct4mmap(&sfp->reserve, 0); /* undo correction */
2398 sg_remove_scat(&sfp->reserve);
2400 sfp->parentdp = NULL;
2401 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2405 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2407 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2414 for (srp = sfp->headrp; srp; srp = tsrp) {
2416 if (sg_srp_done(srp, sfp))
2417 sg_finish_rem_req(srp);
2422 unsigned long iflags;
2424 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2425 __sg_remove_sfp(sdp, sfp);
2426 if (sdp->detached && (NULL == sdp->headfp)) {
2430 for (k = 0; k < maxd; ++k) {
2431 if (sdp == sg_dev_arr[k])
2435 sg_dev_arr[k] = NULL;
2436 kfree((char *) sdp);
2439 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2441 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2442 /* only bump the access_count if we actually succeeded in
2443 * throwing another counter on the host module */
2444 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2445 sfp->closed = 1; /* flag dirty state on this fd */
2446 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2453 sg_res_in_use(Sg_fd * sfp)
2455 const Sg_request *srp;
2456 unsigned long iflags;
2458 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2459 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2462 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2466 /* If retSzp==NULL want exact size or fail */
2467 static struct page *
2468 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2470 struct page *resp = NULL;
2479 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2481 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2483 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2484 order++, a_size <<= 1) ;
2485 resp = alloc_pages(page_mask, order);
2486 while ((!resp) && order && retSzp) {
2488 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2489 resp = alloc_pages(page_mask, order); /* try half */
2493 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2494 memset(page_address(resp), 0, resSz);
2502 sg_page_free(struct page *page, int size)
2508 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2509 order++, a_size <<= 1) ;
2510 __free_pages(page, order);
2513 #ifndef MAINTENANCE_IN_CMD
2514 #define MAINTENANCE_IN_CMD 0xa3
2517 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2518 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2519 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2520 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2524 sg_allow_access(unsigned char opcode, char dev_type)
2528 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2530 for (k = 0; k < sizeof (allow_ops); ++k) {
2531 if (opcode == allow_ops[k])
2537 #ifdef CONFIG_SCSI_PROC_FS
2542 unsigned long iflags;
2544 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2545 for (k = sg_dev_max - 1; k >= 0; --k)
2546 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2548 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2549 return k + 1; /* origin 1 */
2556 Sg_device *sdp = NULL;
2557 unsigned long iflags;
2559 if (sg_dev_arr && (dev >= 0)) {
2560 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2561 if (dev < sg_dev_max)
2562 sdp = sg_dev_arr[dev];
2563 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2568 #ifdef CONFIG_SCSI_PROC_FS
2570 static struct proc_dir_entry *sg_proc_sgp = NULL;
2572 static char sg_proc_sg_dirname[] = "scsi/sg";
2574 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2576 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2577 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2578 size_t count, loff_t *off);
2579 static struct file_operations adio_fops = {
2580 /* .owner, .read and .llseek added in sg_proc_init() */
2581 .open = sg_proc_single_open_adio,
2582 .write = sg_proc_write_adio,
2583 .release = single_release,
2586 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2587 static ssize_t sg_proc_write_dressz(struct file *filp,
2588 const char __user *buffer, size_t count, loff_t *off);
2589 static struct file_operations dressz_fops = {
2590 .open = sg_proc_single_open_dressz,
2591 .write = sg_proc_write_dressz,
2592 .release = single_release,
2595 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2596 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2597 static struct file_operations version_fops = {
2598 .open = sg_proc_single_open_version,
2599 .release = single_release,
2602 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2603 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2604 static struct file_operations devhdr_fops = {
2605 .open = sg_proc_single_open_devhdr,
2606 .release = single_release,
2609 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2610 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2611 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2612 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2613 static void dev_seq_stop(struct seq_file *s, void *v);
2614 static struct file_operations dev_fops = {
2615 .open = sg_proc_open_dev,
2616 .release = seq_release,
2618 static struct seq_operations dev_seq_ops = {
2619 .start = dev_seq_start,
2620 .next = dev_seq_next,
2621 .stop = dev_seq_stop,
2622 .show = sg_proc_seq_show_dev,
2625 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2626 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2627 static struct file_operations devstrs_fops = {
2628 .open = sg_proc_open_devstrs,
2629 .release = seq_release,
2631 static struct seq_operations devstrs_seq_ops = {
2632 .start = dev_seq_start,
2633 .next = dev_seq_next,
2634 .stop = dev_seq_stop,
2635 .show = sg_proc_seq_show_devstrs,
2638 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2639 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2640 static struct file_operations debug_fops = {
2641 .open = sg_proc_open_debug,
2642 .release = seq_release,
2644 static struct seq_operations debug_seq_ops = {
2645 .start = dev_seq_start,
2646 .next = dev_seq_next,
2647 .stop = dev_seq_stop,
2648 .show = sg_proc_seq_show_debug,
2652 struct sg_proc_leaf {
2654 struct file_operations * fops;
2657 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2658 {"allow_dio", &adio_fops},
2659 {"debug", &debug_fops},
2660 {"def_reserved_size", &dressz_fops},
2661 {"device_hdr", &devhdr_fops},
2662 {"devices", &dev_fops},
2663 {"device_strs", &devstrs_fops},
2664 {"version", &version_fops}
2672 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2673 struct proc_dir_entry *pdep;
2674 struct sg_proc_leaf * leaf;
2676 sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
2679 for (k = 0; k < num_leaves; ++k) {
2680 leaf = &sg_proc_leaf_arr[k];
2681 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2682 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2684 leaf->fops->owner = THIS_MODULE,
2685 leaf->fops->read = seq_read,
2686 leaf->fops->llseek = seq_lseek,
2687 pdep->proc_fops = leaf->fops;
2694 sg_proc_cleanup(void)
2698 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2702 for (k = 0; k < num_leaves; ++k)
2703 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2704 remove_proc_entry(sg_proc_sg_dirname, NULL);
2708 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2710 seq_printf(s, "%d\n", *((int *)s->private));
2714 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2716 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2720 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2721 size_t count, loff_t *off)
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 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2736 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2738 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2742 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2743 size_t count, loff_t *off)
2746 unsigned long k = ULONG_MAX;
2749 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2751 num = (count < 10) ? count : 10;
2752 if (copy_from_user(buff, buffer, num))
2755 k = simple_strtoul(buff, NULL, 10);
2756 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2763 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2765 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2770 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2772 return single_open(file, sg_proc_seq_show_version, NULL);
2775 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2777 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2782 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2784 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2787 struct sg_proc_deviter {
2792 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2794 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2800 if (NULL == sg_dev_arr)
2803 it->max = sg_last_dev();
2804 if (it->index >= it->max)
2809 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2811 struct sg_proc_deviter * it = s->private;
2814 return (it->index < it->max) ? it : NULL;
2817 static void dev_seq_stop(struct seq_file *s, void *v)
2822 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2824 return seq_open(file, &dev_seq_ops);
2827 static int sg_proc_seq_show_dev(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, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2836 scsidp->host->host_no, scsidp->channel,
2837 scsidp->id, scsidp->lun, (int) scsidp->type,
2839 (int) scsidp->queue_depth,
2840 (int) scsidp->device_busy,
2841 (int) scsi_device_online(scsidp));
2843 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2847 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2849 return seq_open(file, &devstrs_seq_ops);
2852 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2854 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2856 struct scsi_device *scsidp;
2858 sdp = it ? sg_get_dev(it->index) : NULL;
2859 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2860 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2861 scsidp->vendor, scsidp->model, scsidp->rev);
2863 seq_printf(s, "<no active device>\n");
2867 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2869 int k, m, new_interface, blen, usg;
2872 const sg_io_hdr_t *hp;
2876 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2877 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2878 "(res)sgat=%d low_dma=%d\n", k + 1,
2879 jiffies_to_msecs(fp->timeout),
2880 fp->reserve.bufflen,
2881 (int) fp->reserve.k_use_sg,
2883 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2884 (int) fp->cmd_q, (int) fp->force_packid,
2885 (int) fp->keep_orphan, (int) fp->closed);
2886 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2888 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2889 if (srp->res_used) {
2890 if (new_interface &&
2891 (SG_FLAG_MMAP_IO & hp->flags))
2896 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2902 blen = srp->data.bufflen;
2903 usg = srp->data.k_use_sg;
2904 seq_printf(s, srp->done ?
2905 ((1 == srp->done) ? "rcv:" : "fin:")
2907 seq_printf(s, " id=%d blen=%d",
2908 srp->header.pack_id, blen);
2910 seq_printf(s, " dur=%d", hp->duration);
2912 ms = jiffies_to_msecs(jiffies);
2913 seq_printf(s, " t_o/elap=%d/%d",
2914 (new_interface ? hp->timeout :
2915 jiffies_to_msecs(fp->timeout)),
2916 (ms > hp->duration ? ms - hp->duration : 0));
2918 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
2919 (int) srp->data.cmd_opcode);
2922 seq_printf(s, " No requests active\n");
2926 static int sg_proc_open_debug(struct inode *inode, struct file *file)
2928 return seq_open(file, &debug_seq_ops);
2931 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2933 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2936 if (it && (0 == it->index)) {
2937 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
2938 "(origin 1)\n", sg_dev_max, (int)it->max);
2939 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2941 sdp = it ? sg_get_dev(it->index) : NULL;
2943 struct scsi_device *scsidp = sdp->device;
2945 if (NULL == scsidp) {
2946 seq_printf(s, "device %d detached ??\n",
2951 if (sg_get_nth_sfp(sdp, 0)) {
2952 seq_printf(s, " >>> device=%s ",
2953 sdp->disk->disk_name);
2955 seq_printf(s, "detached pending close ");
2958 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2959 scsidp->host->host_no,
2960 scsidp->channel, scsidp->id,
2962 scsidp->host->hostt->emulated);
2963 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2964 sdp->sg_tablesize, sdp->exclude);
2966 sg_proc_debug_helper(s, sdp);
2971 #endif /* CONFIG_SCSI_PROC_FS */
2973 module_init(init_sg);
2974 module_exit(exit_sg);
2975 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);