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>
54 #include <scsi/scsi_dbg.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_driver.h>
57 #include <scsi/scsi_ioctl.h>
60 #include "scsi_logging.h"
62 #ifdef CONFIG_SCSI_PROC_FS
63 #include <linux/proc_fs.h>
64 static char *sg_version_date = "20050328";
66 static int sg_proc_init(void);
67 static void sg_proc_cleanup(void);
70 #ifndef LINUX_VERSION_CODE
71 #include <linux/version.h>
72 #endif /* LINUX_VERSION_CODE */
74 #define SG_ALLOW_DIO_DEF 0
75 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
77 #define SG_MAX_DEVS 32768
80 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
81 * Then when using 32 bit integers x * m may overflow during the calculation.
82 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
83 * calculates the same, but prevents the overflow when both m and d
84 * are "small" numbers (like HZ and USER_HZ).
85 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
88 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
90 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
92 int sg_big_buff = SG_DEF_RESERVED_SIZE;
93 /* N.B. This variable is readable and writeable via
94 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
95 of this size (or less if there is not enough memory) will be reserved
96 for use by this file descriptor. [Deprecated usage: this variable is also
97 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
98 the kernel (i.e. it is not a module).] */
99 static int def_reserved_size = -1; /* picks up init parameter */
100 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
102 #define SG_SECTOR_SZ 512
103 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
105 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
107 static int sg_add(struct class_device *);
108 static void sg_remove(struct class_device *);
110 static Scsi_Request *dummy_cmdp; /* only used for sizeof */
112 static DEFINE_RWLOCK(sg_dev_arr_lock); /* Also used to lock
113 file descriptor list for device */
115 static struct class_interface sg_interface = {
120 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
121 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
122 unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
123 unsigned bufflen; /* Size of (aggregate) data buffer */
124 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
125 void *buffer; /* Data buffer or scatter list (k_use_sg>0) */
126 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
127 unsigned char cmd_opcode; /* first byte of command */
130 struct sg_device; /* forward declarations */
133 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
134 Scsi_Request *my_cmdp; /* != 0 when request with lower levels */
135 struct sg_request *nextrp; /* NULL -> tail request (slist) */
136 struct sg_fd *parentfp; /* NULL -> not in use */
137 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
138 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
139 unsigned char sense_b[sizeof (dummy_cmdp->sr_sense_buffer)];
140 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
141 char orphan; /* 1 -> drop on sight, 0 -> normal */
142 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
143 volatile char done; /* 0->before bh, 1->before read, 2->read */
146 typedef struct sg_fd { /* holds the state of a file descriptor */
147 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
148 struct sg_device *parentdp; /* owning device */
149 wait_queue_head_t read_wait; /* queue read until command done */
150 rwlock_t rq_list_lock; /* protect access to list in req_arr */
151 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
152 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
153 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
154 unsigned save_scat_len; /* original length of trunc. scat. element */
155 Sg_request *headrp; /* head of request slist, NULL->empty */
156 struct fasync_struct *async_qp; /* used by asynchronous notification */
157 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
158 char low_dma; /* as in parent but possibly overridden to 1 */
159 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
160 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
161 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
162 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
163 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
164 char mmap_called; /* 0 -> mmap() never called on this fd */
167 typedef struct sg_device { /* holds the state of each scsi generic device */
168 struct scsi_device *device;
169 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
170 int sg_tablesize; /* adapter's max scatter-gather table size */
171 Sg_fd *headfp; /* first open fd belonging to this device */
172 volatile char detached; /* 0->attached, 1->detached pending removal */
173 volatile char exclude; /* opened for exclusive access */
174 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
175 struct gendisk *disk;
176 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
179 static int sg_fasync(int fd, struct file *filp, int mode);
180 static void sg_cmd_done(Scsi_Cmnd * SCpnt); /* tasklet or soft irq callback */
181 static int sg_start_req(Sg_request * srp);
182 static void sg_finish_rem_req(Sg_request * srp);
183 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
184 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
186 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
188 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
189 int blocking, int read_only, Sg_request ** o_srp);
190 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
191 unsigned char *cmnd, int timeout, int blocking);
192 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
193 int wr_xf, int *countp, unsigned char __user **up);
194 static int sg_write_xfer(Sg_request * srp);
195 static int sg_read_xfer(Sg_request * srp);
196 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
197 static void sg_remove_scat(Sg_scatter_hold * schp);
198 static void sg_build_reserve(Sg_fd * sfp, int req_size);
199 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
200 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
201 static char *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
202 static void sg_page_free(char *buff, int size);
203 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
204 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
205 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
206 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
207 static Sg_request *sg_add_request(Sg_fd * sfp);
208 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
209 static int sg_res_in_use(Sg_fd * sfp);
210 static int sg_allow_access(unsigned char opcode, char dev_type);
211 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
212 static Sg_device *sg_get_dev(int dev);
213 static inline unsigned char *sg_scatg2virt(const struct scatterlist *sclp);
214 #ifdef CONFIG_SCSI_PROC_FS
215 static int sg_last_dev(void);
218 static Sg_device **sg_dev_arr = NULL;
219 static int sg_dev_max;
220 static int sg_nr_dev;
222 #define SZ_SG_HEADER sizeof(struct sg_header)
223 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
224 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
225 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
228 sg_open(struct inode *inode, struct file *filp)
230 int dev = iminor(inode);
231 int flags = filp->f_flags;
237 nonseekable_open(inode, filp);
238 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
239 sdp = sg_get_dev(dev);
240 if ((!sdp) || (!sdp->device))
245 /* This driver's module count bumped by fops_get in <linux/fs.h> */
246 /* Prevent the device driver from vanishing while we sleep */
247 retval = scsi_device_get(sdp->device);
251 if (!((flags & O_NONBLOCK) ||
252 scsi_block_when_processing_errors(sdp->device))) {
254 /* we are in error recovery for this device */
258 if (flags & O_EXCL) {
259 if (O_RDONLY == (flags & O_ACCMODE)) {
260 retval = -EPERM; /* Can't lock it with read only access */
263 if (sdp->headfp && (flags & O_NONBLOCK)) {
268 __wait_event_interruptible(sdp->o_excl_wait,
269 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
271 retval = res; /* -ERESTARTSYS because signal hit process */
274 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
275 if (flags & O_NONBLOCK) {
280 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
283 retval = res; /* -ERESTARTSYS because signal hit process */
291 if (!sdp->headfp) { /* no existing opens on this device */
293 sdp->sg_tablesize = sdp->device->host->sg_tablesize;
295 if ((sfp = sg_add_sfp(sdp, dev)))
296 filp->private_data = sfp;
299 sdp->exclude = 0; /* undo if error */
306 scsi_device_put(sdp->device);
310 /* Following function was formerly called 'sg_close' */
312 sg_release(struct inode *inode, struct file *filp)
317 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
319 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
320 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
321 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
322 if (!sdp->detached) {
323 scsi_device_put(sdp->device);
326 wake_up_interruptible(&sdp->o_excl_wait);
332 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
337 int req_pack_id = -1;
339 struct sg_header *old_hdr = NULL;
342 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
344 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
345 sdp->disk->disk_name, (int) count));
346 if (!access_ok(VERIFY_WRITE, buf, count))
348 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
349 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
352 if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
356 if (old_hdr->reply_len < 0) {
357 if (count >= SZ_SG_IO_HDR) {
358 sg_io_hdr_t *new_hdr;
359 new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
364 retval =__copy_from_user
365 (new_hdr, buf, SZ_SG_IO_HDR);
366 req_pack_id = new_hdr->pack_id;
374 req_pack_id = old_hdr->pack_id;
376 srp = sg_get_rq_mark(sfp, req_pack_id);
377 if (!srp) { /* now wait on packet to arrive */
382 if (filp->f_flags & O_NONBLOCK) {
387 retval = 0; /* following macro beats race condition */
388 __wait_event_interruptible(sfp->read_wait,
390 (srp = sg_get_rq_mark(sfp, req_pack_id))),
399 /* -ERESTARTSYS as signal hit process */
403 if (srp->header.interface_id != '\0') {
404 retval = sg_new_read(sfp, buf, count, srp);
409 if (old_hdr == NULL) {
410 old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
416 memset(old_hdr, 0, SZ_SG_HEADER);
417 old_hdr->reply_len = (int) hp->timeout;
418 old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
419 old_hdr->pack_id = hp->pack_id;
420 old_hdr->twelve_byte =
421 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
422 old_hdr->target_status = hp->masked_status;
423 old_hdr->host_status = hp->host_status;
424 old_hdr->driver_status = hp->driver_status;
425 if ((CHECK_CONDITION & hp->masked_status) ||
426 (DRIVER_SENSE & hp->driver_status))
427 memcpy(old_hdr->sense_buffer, srp->sense_b,
428 sizeof (old_hdr->sense_buffer));
429 switch (hp->host_status) {
430 /* This setup of 'result' is for backward compatibility and is best
431 ignored by the user who should use target, host + driver status */
433 case DID_PASSTHROUGH:
440 old_hdr->result = EBUSY;
447 old_hdr->result = EIO;
450 old_hdr->result = (srp->sense_b[0] == 0 &&
451 hp->masked_status == GOOD) ? 0 : EIO;
454 old_hdr->result = EIO;
458 /* Now copy the result back to the user buffer. */
459 if (count >= SZ_SG_HEADER) {
460 if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
465 if (count > old_hdr->reply_len)
466 count = old_hdr->reply_len;
467 if (count > SZ_SG_HEADER) {
468 if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
474 count = (old_hdr->result == 0) ? 0 : -EIO;
475 sg_finish_rem_req(srp);
484 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
486 sg_io_hdr_t *hp = &srp->header;
490 if (count < SZ_SG_IO_HDR) {
495 if ((hp->mx_sb_len > 0) && hp->sbp) {
496 if ((CHECK_CONDITION & hp->masked_status) ||
497 (DRIVER_SENSE & hp->driver_status)) {
498 int sb_len = sizeof (dummy_cmdp->sr_sense_buffer);
499 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
500 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
501 len = (len > sb_len) ? sb_len : len;
502 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
509 if (hp->masked_status || hp->host_status || hp->driver_status)
510 hp->info |= SG_INFO_CHECK;
511 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
515 err = sg_read_xfer(srp);
517 sg_finish_rem_req(srp);
518 return (0 == err) ? count : err;
522 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
524 int mxsize, cmd_size, k;
525 int input_size, blocking;
526 unsigned char opcode;
530 struct sg_header old_hdr;
532 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
534 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
536 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
537 sdp->disk->disk_name, (int) count));
540 if (!((filp->f_flags & O_NONBLOCK) ||
541 scsi_block_when_processing_errors(sdp->device)))
544 if (!access_ok(VERIFY_READ, buf, count))
545 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
546 if (count < SZ_SG_HEADER)
548 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
550 blocking = !(filp->f_flags & O_NONBLOCK);
551 if (old_hdr.reply_len < 0)
552 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
553 if (count < (SZ_SG_HEADER + 6))
554 return -EIO; /* The minimum scsi command length is 6 bytes. */
556 if (!(srp = sg_add_request(sfp))) {
557 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
561 __get_user(opcode, buf);
562 if (sfp->next_cmd_len > 0) {
563 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
564 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
565 sfp->next_cmd_len = 0;
566 sg_remove_request(sfp, srp);
569 cmd_size = sfp->next_cmd_len;
570 sfp->next_cmd_len = 0; /* reset so only this write() effected */
572 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
573 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
576 SCSI_LOG_TIMEOUT(4, printk(
577 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
578 /* Determine buffer size. */
579 input_size = count - cmd_size;
580 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
581 mxsize -= SZ_SG_HEADER;
582 input_size -= SZ_SG_HEADER;
583 if (input_size < 0) {
584 sg_remove_request(sfp, srp);
585 return -EIO; /* User did not pass enough bytes for this command. */
588 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
589 hp->cmd_len = (unsigned char) cmd_size;
593 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
594 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
596 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
597 hp->dxfer_len = mxsize;
598 hp->dxferp = (char __user *)buf + cmd_size;
600 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
601 hp->flags = input_size; /* structure abuse ... */
602 hp->pack_id = old_hdr.pack_id;
604 if (__copy_from_user(cmnd, buf, cmd_size))
607 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
608 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
609 * is a non-zero input_size, so emit a warning.
611 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
612 if (printk_ratelimit())
614 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
615 "guessing data in;\n" KERN_WARNING " "
616 "program %s not setting count and/or reply_len properly\n",
617 old_hdr.reply_len - (int)SZ_SG_HEADER,
618 input_size, (unsigned int) cmnd[0],
620 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
621 return (k < 0) ? k : count;
625 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
626 int blocking, int read_only, Sg_request ** o_srp)
631 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
633 unsigned long ul_timeout;
635 if (count < SZ_SG_IO_HDR)
637 if (!access_ok(VERIFY_READ, buf, count))
638 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
640 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
641 if (!(srp = sg_add_request(sfp))) {
642 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
646 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
647 sg_remove_request(sfp, srp);
650 if (hp->interface_id != 'S') {
651 sg_remove_request(sfp, srp);
654 if (hp->flags & SG_FLAG_MMAP_IO) {
655 if (hp->dxfer_len > sfp->reserve.bufflen) {
656 sg_remove_request(sfp, srp);
657 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
659 if (hp->flags & SG_FLAG_DIRECT_IO) {
660 sg_remove_request(sfp, srp);
661 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
663 if (sg_res_in_use(sfp)) {
664 sg_remove_request(sfp, srp);
665 return -EBUSY; /* reserve buffer already being used */
668 ul_timeout = msecs_to_jiffies(srp->header.timeout);
669 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
670 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
671 sg_remove_request(sfp, srp);
674 if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
675 sg_remove_request(sfp, srp);
676 return -EFAULT; /* protects following copy_from_user()s + get_user()s */
678 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
679 sg_remove_request(sfp, srp);
683 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
684 sg_remove_request(sfp, srp);
687 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
696 sg_common_write(Sg_fd * sfp, Sg_request * srp,
697 unsigned char *cmnd, int timeout, int blocking)
701 Sg_device *sdp = sfp->parentdp;
702 sg_io_hdr_t *hp = &srp->header;
705 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
707 hp->masked_status = 0;
711 hp->driver_status = 0;
713 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
714 (int) cmnd[0], (int) hp->cmd_len));
716 if ((k = sg_start_req(srp))) {
717 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
718 sg_finish_rem_req(srp);
719 return k; /* probably out of space --> ENOMEM */
721 if ((k = sg_write_xfer(srp))) {
722 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
723 sg_finish_rem_req(srp);
727 sg_finish_rem_req(srp);
730 SRpnt = scsi_allocate_request(sdp->device, GFP_ATOMIC);
732 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
733 sg_finish_rem_req(srp);
737 srp->my_cmdp = SRpnt;
738 q = SRpnt->sr_device->request_queue;
739 SRpnt->sr_request->rq_disk = sdp->disk;
740 SRpnt->sr_sense_buffer[0] = 0;
741 SRpnt->sr_cmd_len = hp->cmd_len;
742 SRpnt->sr_use_sg = srp->data.k_use_sg;
743 SRpnt->sr_sglist_len = srp->data.sglist_len;
744 SRpnt->sr_bufflen = srp->data.bufflen;
745 SRpnt->sr_underflow = 0;
746 SRpnt->sr_buffer = srp->data.buffer;
747 switch (hp->dxfer_direction) {
748 case SG_DXFER_TO_FROM_DEV:
749 case SG_DXFER_FROM_DEV:
750 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
752 case SG_DXFER_TO_DEV:
753 SRpnt->sr_data_direction = DMA_TO_DEVICE;
755 case SG_DXFER_UNKNOWN:
756 SRpnt->sr_data_direction = DMA_BIDIRECTIONAL;
759 SRpnt->sr_data_direction = DMA_NONE;
762 SRpnt->upper_private_data = srp;
763 srp->data.k_use_sg = 0;
764 srp->data.sglist_len = 0;
765 srp->data.bufflen = 0;
766 srp->data.buffer = NULL;
767 hp->duration = jiffies_to_msecs(jiffies);
768 /* Now send everything of to mid-level. The next time we hear about this
769 packet is when sg_cmd_done() is called (i.e. a callback). */
770 scsi_do_req(SRpnt, (void *) cmnd,
771 (void *) SRpnt->sr_buffer, hp->dxfer_len,
772 sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
773 /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
778 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
780 unsigned long iflags;
783 read_lock_irqsave(&sfp->rq_list_lock, iflags);
785 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
790 sg_ioctl(struct inode *inode, struct file *filp,
791 unsigned int cmd_in, unsigned long arg)
793 void __user *p = (void __user *)arg;
795 int result, val, read_only;
799 unsigned long iflags;
801 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
803 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
804 sdp->disk->disk_name, (int) cmd_in));
805 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
810 int blocking = 1; /* ignore O_NONBLOCK flag */
814 if (!scsi_block_when_processing_errors(sdp->device))
816 if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
819 sg_new_write(sfp, p, SZ_SG_IO_HDR,
820 blocking, read_only, &srp);
823 srp->sg_io_owned = 1;
825 result = 0; /* following macro to beat race condition */
826 __wait_event_interruptible(sfp->read_wait,
827 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
832 return 0; /* request packet dropped already */
836 return result; /* -ERESTARTSYS because signal hit process */
838 write_lock_irqsave(&sfp->rq_list_lock, iflags);
840 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
841 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
842 return (result < 0) ? result : 0;
845 result = get_user(val, ip);
850 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
851 val = MULDIV (INT_MAX, USER_HZ, HZ);
852 sfp->timeout_user = val;
853 sfp->timeout = MULDIV (val, HZ, USER_HZ);
856 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
857 /* strange ..., for backward compatibility */
858 return sfp->timeout_user;
859 case SG_SET_FORCE_LOW_DMA:
860 result = get_user(val, ip);
865 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
866 val = (int) sfp->reserve.bufflen;
867 sg_remove_scat(&sfp->reserve);
868 sg_build_reserve(sfp, val);
873 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
877 return put_user((int) sfp->low_dma, ip);
879 if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
882 sg_scsi_id_t __user *sg_idp = p;
886 __put_user((int) sdp->device->host->host_no,
888 __put_user((int) sdp->device->channel,
890 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
891 __put_user((int) sdp->device->lun, &sg_idp->lun);
892 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
893 __put_user((short) sdp->device->host->cmd_per_lun,
894 &sg_idp->h_cmd_per_lun);
895 __put_user((short) sdp->device->queue_depth,
896 &sg_idp->d_queue_depth);
897 __put_user(0, &sg_idp->unused[0]);
898 __put_user(0, &sg_idp->unused[1]);
901 case SG_SET_FORCE_PACK_ID:
902 result = get_user(val, ip);
905 sfp->force_packid = val ? 1 : 0;
908 if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
910 read_lock_irqsave(&sfp->rq_list_lock, iflags);
911 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
912 if ((1 == srp->done) && (!srp->sg_io_owned)) {
913 read_unlock_irqrestore(&sfp->rq_list_lock,
915 __put_user(srp->header.pack_id, ip);
919 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
922 case SG_GET_NUM_WAITING:
923 read_lock_irqsave(&sfp->rq_list_lock, iflags);
924 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
925 if ((1 == srp->done) && (!srp->sg_io_owned))
928 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
929 return put_user(val, ip);
930 case SG_GET_SG_TABLESIZE:
931 return put_user(sdp->sg_tablesize, ip);
932 case SG_SET_RESERVED_SIZE:
933 result = get_user(val, ip);
938 if (val != sfp->reserve.bufflen) {
939 if (sg_res_in_use(sfp) || sfp->mmap_called)
941 sg_remove_scat(&sfp->reserve);
942 sg_build_reserve(sfp, val);
945 case SG_GET_RESERVED_SIZE:
946 val = (int) sfp->reserve.bufflen;
947 return put_user(val, ip);
948 case SG_SET_COMMAND_Q:
949 result = get_user(val, ip);
952 sfp->cmd_q = val ? 1 : 0;
954 case SG_GET_COMMAND_Q:
955 return put_user((int) sfp->cmd_q, ip);
956 case SG_SET_KEEP_ORPHAN:
957 result = get_user(val, ip);
960 sfp->keep_orphan = val;
962 case SG_GET_KEEP_ORPHAN:
963 return put_user((int) sfp->keep_orphan, ip);
964 case SG_NEXT_CMD_LEN:
965 result = get_user(val, ip);
968 sfp->next_cmd_len = (val > 0) ? val : 0;
970 case SG_GET_VERSION_NUM:
971 return put_user(sg_version_num, ip);
972 case SG_GET_ACCESS_COUNT:
973 /* faked - we don't have a real access count anymore */
974 val = (sdp->device ? 1 : 0);
975 return put_user(val, ip);
976 case SG_GET_REQUEST_TABLE:
977 if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
980 sg_req_info_t *rinfo;
983 rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
987 read_lock_irqsave(&sfp->rq_list_lock, iflags);
988 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
989 ++val, srp = srp ? srp->nextrp : srp) {
990 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
992 rinfo[val].req_state = srp->done + 1;
994 srp->header.masked_status &
995 srp->header.host_status &
996 srp->header.driver_status;
998 rinfo[val].duration =
999 srp->header.duration;
1001 ms = jiffies_to_msecs(jiffies);
1002 rinfo[val].duration =
1003 (ms > srp->header.duration) ?
1004 (ms - srp->header.duration) : 0;
1006 rinfo[val].orphan = srp->orphan;
1007 rinfo[val].sg_io_owned =
1009 rinfo[val].pack_id =
1010 srp->header.pack_id;
1011 rinfo[val].usr_ptr =
1012 srp->header.usr_ptr;
1015 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1016 result = __copy_to_user(p, rinfo,
1017 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1018 result = result ? -EFAULT : 0;
1022 case SG_EMULATED_HOST:
1025 return put_user(sdp->device->host->hostt->emulated, ip);
1029 if (filp->f_flags & O_NONBLOCK) {
1030 if (test_bit(SHOST_RECOVERY,
1031 &sdp->device->host->shost_state))
1033 } else if (!scsi_block_when_processing_errors(sdp->device))
1035 result = get_user(val, ip);
1038 if (SG_SCSI_RESET_NOTHING == val)
1041 case SG_SCSI_RESET_DEVICE:
1042 val = SCSI_TRY_RESET_DEVICE;
1044 case SG_SCSI_RESET_BUS:
1045 val = SCSI_TRY_RESET_BUS;
1047 case SG_SCSI_RESET_HOST:
1048 val = SCSI_TRY_RESET_HOST;
1053 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1055 return (scsi_reset_provider(sdp->device, val) ==
1056 SUCCESS) ? 0 : -EIO;
1057 case SCSI_IOCTL_SEND_COMMAND:
1061 unsigned char opcode = WRITE_6;
1062 Scsi_Ioctl_Command __user *siocp = p;
1064 if (copy_from_user(&opcode, siocp->data, 1))
1066 if (!sg_allow_access(opcode, sdp->device->type))
1069 return scsi_ioctl_send_command(sdp->device, p);
1071 result = get_user(val, ip);
1074 sdp->sgdebug = (char) val;
1076 case SCSI_IOCTL_GET_IDLUN:
1077 case SCSI_IOCTL_GET_BUS_NUMBER:
1078 case SCSI_IOCTL_PROBE_HOST:
1079 case SG_GET_TRANSFORM:
1082 return scsi_ioctl(sdp->device, cmd_in, p);
1085 return -EPERM; /* don't know so take safe approach */
1086 return scsi_ioctl(sdp->device, cmd_in, p);
1090 #ifdef CONFIG_COMPAT
1091 static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1095 struct scsi_device *sdev;
1097 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1101 if (sdev->host->hostt->compat_ioctl) {
1104 ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
1109 return -ENOIOCTLCMD;
1114 sg_poll(struct file *filp, poll_table * wait)
1116 unsigned int res = 0;
1121 unsigned long iflags;
1123 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1126 poll_wait(filp, &sfp->read_wait, wait);
1127 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1128 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1129 /* if any read waiting, flag it */
1130 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1131 res = POLLIN | POLLRDNORM;
1134 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1138 else if (!sfp->cmd_q) {
1140 res |= POLLOUT | POLLWRNORM;
1141 } else if (count < SG_MAX_QUEUE)
1142 res |= POLLOUT | POLLWRNORM;
1143 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1144 sdp->disk->disk_name, (int) res));
1149 sg_fasync(int fd, struct file *filp, int mode)
1155 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1157 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1158 sdp->disk->disk_name, mode));
1160 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1161 return (retval < 0) ? retval : 0;
1164 static inline unsigned char *
1165 sg_scatg2virt(const struct scatterlist *sclp)
1167 return (sclp && sclp->page) ?
1168 (unsigned char *) page_address(sclp->page) + sclp->offset : NULL;
1171 /* When startFinish==1 increments page counts for pages other than the
1172 first of scatter gather elements obtained from __get_free_pages().
1173 When startFinish==0 decrements ... */
1175 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1181 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1182 startFinish, rsv_schp->k_use_sg));
1183 /* N.B. correction _not_ applied to base page of each allocation */
1184 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1185 struct scatterlist *sclp = rsv_schp->buffer;
1187 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
1188 for (m = PAGE_SIZE; m < sclp->length; m += PAGE_SIZE) {
1189 page_ptr = sg_scatg2virt(sclp) + m;
1190 page = virt_to_page(page_ptr);
1194 if (page_count(page) > 0)
1199 } else { /* reserve buffer is just a single allocation */
1200 for (m = PAGE_SIZE; m < rsv_schp->bufflen; m += PAGE_SIZE) {
1201 page_ptr = (unsigned char *) rsv_schp->buffer + m;
1202 page = virt_to_page(page_ptr);
1206 if (page_count(page) > 0)
1213 static struct page *
1214 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1217 struct page *page = NOPAGE_SIGBUS;
1218 void *page_ptr = NULL;
1219 unsigned long offset;
1220 Sg_scatter_hold *rsv_schp;
1222 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1224 rsv_schp = &sfp->reserve;
1225 offset = addr - vma->vm_start;
1226 if (offset >= rsv_schp->bufflen)
1228 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1229 offset, rsv_schp->k_use_sg));
1230 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1232 unsigned long sa = vma->vm_start;
1234 struct scatterlist *sclp = rsv_schp->buffer;
1236 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1238 len = vma->vm_end - sa;
1239 len = (len < sclp->length) ? len : sclp->length;
1241 page_ptr = sg_scatg2virt(sclp) + offset;
1242 page = virt_to_page(page_ptr);
1243 get_page(page); /* increment page count */
1249 } else { /* reserve buffer is just a single allocation */
1250 page_ptr = (unsigned char *) rsv_schp->buffer + offset;
1251 page = virt_to_page(page_ptr);
1252 get_page(page); /* increment page count */
1255 *type = VM_FAULT_MINOR;
1259 static struct vm_operations_struct sg_mmap_vm_ops = {
1260 .nopage = sg_vma_nopage,
1264 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1267 unsigned long req_sz;
1268 Sg_scatter_hold *rsv_schp;
1270 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1272 req_sz = vma->vm_end - vma->vm_start;
1273 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1274 (void *) vma->vm_start, (int) req_sz));
1276 return -EINVAL; /* want no offset */
1277 rsv_schp = &sfp->reserve;
1278 if (req_sz > rsv_schp->bufflen)
1279 return -ENOMEM; /* cannot map more than reserved buffer */
1281 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1283 unsigned long sa = vma->vm_start;
1285 struct scatterlist *sclp = rsv_schp->buffer;
1287 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1289 if (0 != sclp->offset)
1290 return -EFAULT; /* non page aligned memory ?? */
1291 len = vma->vm_end - sa;
1292 len = (len < sclp->length) ? len : sclp->length;
1295 } else { /* reserve buffer is just a single allocation */
1296 if ((unsigned long) rsv_schp->buffer & (PAGE_SIZE - 1))
1297 return -EFAULT; /* non page aligned memory ?? */
1299 if (0 == sfp->mmap_called) {
1300 sg_rb_correct4mmap(rsv_schp, 1); /* do only once per fd lifetime */
1301 sfp->mmap_called = 1;
1303 vma->vm_flags |= (VM_RESERVED | VM_IO);
1304 vma->vm_private_data = sfp;
1305 vma->vm_ops = &sg_mmap_vm_ops;
1309 /* This function is a "bottom half" handler that is called by the
1310 * mid level when a command is completed (or has failed). */
1312 sg_cmd_done(Scsi_Cmnd * SCpnt)
1314 Scsi_Request *SRpnt = NULL;
1315 Sg_device *sdp = NULL;
1317 Sg_request *srp = NULL;
1318 unsigned long iflags;
1321 if (SCpnt && (SRpnt = SCpnt->sc_request))
1322 srp = (Sg_request *) SRpnt->upper_private_data;
1324 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1326 scsi_release_request(SRpnt);
1329 sfp = srp->parentfp;
1331 sdp = sfp->parentdp;
1332 if ((NULL == sdp) || sdp->detached) {
1333 printk(KERN_INFO "sg_cmd_done: device detached\n");
1334 scsi_release_request(SRpnt);
1338 /* First transfer ownership of data buffers to sg_device object. */
1339 srp->data.k_use_sg = SRpnt->sr_use_sg;
1340 srp->data.sglist_len = SRpnt->sr_sglist_len;
1341 srp->data.bufflen = SRpnt->sr_bufflen;
1342 srp->data.buffer = SRpnt->sr_buffer;
1343 /* now clear out request structure */
1344 SRpnt->sr_use_sg = 0;
1345 SRpnt->sr_sglist_len = 0;
1346 SRpnt->sr_bufflen = 0;
1347 SRpnt->sr_buffer = NULL;
1348 SRpnt->sr_underflow = 0;
1349 SRpnt->sr_request->rq_disk = NULL; /* "sg" _disowns_ request blk */
1351 srp->my_cmdp = NULL;
1353 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1354 sdp->disk->disk_name, srp->header.pack_id, (int) SRpnt->sr_result));
1355 srp->header.resid = SCpnt->resid;
1356 ms = jiffies_to_msecs(jiffies);
1357 srp->header.duration = (ms > srp->header.duration) ?
1358 (ms - srp->header.duration) : 0;
1359 if (0 != SRpnt->sr_result) {
1360 struct scsi_sense_hdr sshdr;
1362 memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
1363 sizeof (srp->sense_b));
1364 srp->header.status = 0xff & SRpnt->sr_result;
1365 srp->header.masked_status = status_byte(SRpnt->sr_result);
1366 srp->header.msg_status = msg_byte(SRpnt->sr_result);
1367 srp->header.host_status = host_byte(SRpnt->sr_result);
1368 srp->header.driver_status = driver_byte(SRpnt->sr_result);
1369 if ((sdp->sgdebug > 0) &&
1370 ((CHECK_CONDITION == srp->header.masked_status) ||
1371 (COMMAND_TERMINATED == srp->header.masked_status)))
1372 scsi_print_req_sense("sg_cmd_done", SRpnt);
1374 /* Following if statement is a patch supplied by Eric Youngdale */
1375 if (driver_byte(SRpnt->sr_result) != 0
1376 && scsi_command_normalize_sense(SCpnt, &sshdr)
1377 && !scsi_sense_is_deferred(&sshdr)
1378 && sshdr.sense_key == UNIT_ATTENTION
1379 && sdp->device->removable) {
1380 /* Detected possible disc change. Set the bit - this */
1381 /* may be used if there are filesystems using this device */
1382 sdp->device->changed = 1;
1385 /* Rely on write phase to clean out srp status values, so no "else" */
1387 scsi_release_request(SRpnt);
1389 if (sfp->closed) { /* whoops this fd already released, cleanup */
1390 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1391 sg_finish_rem_req(srp);
1393 if (NULL == sfp->headrp) {
1394 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1395 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1396 scsi_device_put(sdp->device);
1400 } else if (srp && srp->orphan) {
1401 if (sfp->keep_orphan)
1402 srp->sg_io_owned = 0;
1404 sg_finish_rem_req(srp);
1409 /* Now wake up any sg_read() that is waiting for this packet. */
1410 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1411 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1413 wake_up_interruptible(&sfp->read_wait);
1414 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1418 static struct file_operations sg_fops = {
1419 .owner = THIS_MODULE,
1424 #ifdef CONFIG_COMPAT
1425 .compat_ioctl = sg_compat_ioctl,
1429 .release = sg_release,
1430 .fasync = sg_fasync,
1433 static struct class *sg_sysfs_class;
1435 static int sg_sysfs_valid = 0;
1437 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1440 unsigned long iflags;
1441 void *old_sg_dev_arr = NULL;
1444 sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
1446 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1450 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1451 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1453 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1454 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1456 tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1457 if (unlikely(!tmp_da))
1460 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1461 memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
1462 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1463 old_sg_dev_arr = sg_dev_arr;
1464 sg_dev_arr = tmp_da;
1465 sg_dev_max = tmp_dev_max;
1468 for (k = 0; k < sg_dev_max; k++)
1471 if (unlikely(k >= SG_MAX_DEVS))
1474 memset(sdp, 0, sizeof(*sdp));
1475 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1476 sprintf(disk->disk_name, "sg%d", k);
1477 disk->first_minor = k;
1479 sdp->device = scsidp;
1480 init_waitqueue_head(&sdp->o_excl_wait);
1481 sdp->sg_tablesize = scsidp->host ? scsidp->host->sg_tablesize : 0;
1484 sg_dev_arr[k] = sdp;
1485 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1491 kfree(old_sg_dev_arr);
1495 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1500 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1502 "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1503 "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
1504 scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
1510 sg_add(struct class_device *cl_dev)
1512 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1513 struct gendisk *disk;
1514 Sg_device *sdp = NULL;
1515 struct cdev * cdev = NULL;
1518 disk = alloc_disk(1);
1520 printk(KERN_WARNING "alloc_disk failed\n");
1523 disk->major = SCSI_GENERIC_MAJOR;
1526 cdev = cdev_alloc();
1528 printk(KERN_WARNING "cdev_alloc failed\n");
1531 cdev->owner = THIS_MODULE;
1532 cdev->ops = &sg_fops;
1534 error = sg_alloc(disk, scsidp);
1536 printk(KERN_WARNING "sg_alloc failed\n");
1540 sdp = sg_dev_arr[k];
1542 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1543 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1544 "%s/generic", scsidp->devfs_name);
1545 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1547 devfs_remove("%s/generic", scsidp->devfs_name);
1551 if (sg_sysfs_valid) {
1552 struct class_device * sg_class_member;
1554 sg_class_member = class_device_create(sg_sysfs_class,
1555 MKDEV(SCSI_GENERIC_MAJOR, k),
1558 if (IS_ERR(sg_class_member))
1559 printk(KERN_WARNING "sg_add: "
1560 "class_device_create failed\n");
1561 class_set_devdata(sg_class_member, sdp);
1562 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1563 &sg_class_member->kobj, "generic");
1565 printk(KERN_ERR "sg_add: unable to make symlink "
1566 "'generic' back to sg%d\n", k);
1568 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1571 "Attached scsi generic sg%d at scsi%d, channel"
1572 " %d, id %d, lun %d, type %d\n", k,
1573 scsidp->host->host_no, scsidp->channel, scsidp->id,
1574 scsidp->lun, scsidp->type);
1586 sg_remove(struct class_device *cl_dev)
1588 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1589 Sg_device *sdp = NULL;
1590 unsigned long iflags;
1597 if (NULL == sg_dev_arr)
1600 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1601 for (k = 0; k < sg_dev_max; k++) {
1602 sdp = sg_dev_arr[k];
1603 if ((NULL == sdp) || (sdp->device != scsidp))
1604 continue; /* dirty but lowers nesting */
1607 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1609 for (srp = sfp->headrp; srp; srp = tsrp) {
1611 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1612 sg_finish_rem_req(srp);
1615 scsi_device_put(sdp->device);
1616 __sg_remove_sfp(sdp, sfp);
1619 wake_up_interruptible(&sfp->read_wait);
1620 kill_fasync(&sfp->async_qp, SIGPOLL,
1624 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1625 if (NULL == sdp->headfp) {
1626 sg_dev_arr[k] = NULL;
1628 } else { /* nothing active, simple case */
1629 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1630 sg_dev_arr[k] = NULL;
1635 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1638 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1639 class_device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, k));
1640 cdev_del(sdp->cdev);
1642 devfs_remove("%s/generic", scsidp->devfs_name);
1643 put_disk(sdp->disk);
1645 if (NULL == sdp->headfp)
1646 kfree((char *) sdp);
1650 msleep(10); /* dirty detach so delay device destruction */
1653 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1654 * of sysfs parameters (which module_param doesn't yet support).
1655 * Sysfs parameters defined explicitly below.
1657 module_param_named(def_reserved_size, def_reserved_size, int, S_IRUGO);
1658 module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
1660 MODULE_AUTHOR("Douglas Gilbert");
1661 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1662 MODULE_LICENSE("GPL");
1663 MODULE_VERSION(SG_VERSION_STR);
1665 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1666 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1673 if (def_reserved_size >= 0)
1674 sg_big_buff = def_reserved_size;
1676 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1680 sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
1681 if ( IS_ERR(sg_sysfs_class) ) {
1682 rc = PTR_ERR(sg_sysfs_class);
1686 rc = scsi_register_interface(&sg_interface);
1688 #ifdef CONFIG_SCSI_PROC_FS
1690 #endif /* CONFIG_SCSI_PROC_FS */
1693 class_destroy(sg_sysfs_class);
1695 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1702 #ifdef CONFIG_SCSI_PROC_FS
1704 #endif /* CONFIG_SCSI_PROC_FS */
1705 scsi_unregister_interface(&sg_interface);
1706 class_destroy(sg_sysfs_class);
1708 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1710 if (sg_dev_arr != NULL) {
1711 kfree((char *) sg_dev_arr);
1718 sg_start_req(Sg_request * srp)
1721 Sg_fd *sfp = srp->parentfp;
1722 sg_io_hdr_t *hp = &srp->header;
1723 int dxfer_len = (int) hp->dxfer_len;
1724 int dxfer_dir = hp->dxfer_direction;
1725 Sg_scatter_hold *req_schp = &srp->data;
1726 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1728 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1729 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1731 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1732 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1733 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1734 res = sg_build_direct(srp, sfp, dxfer_len);
1735 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1738 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1739 sg_link_reserve(sfp, srp, dxfer_len);
1741 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1743 sg_remove_scat(req_schp);
1751 sg_finish_rem_req(Sg_request * srp)
1753 Sg_fd *sfp = srp->parentfp;
1754 Sg_scatter_hold *req_schp = &srp->data;
1756 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1758 sg_unlink_reserve(sfp, srp);
1760 sg_remove_scat(req_schp);
1761 sg_remove_request(sfp, srp);
1765 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1768 int elem_sz = sizeof (struct scatterlist);
1769 int sg_bufflen = tablesize * elem_sz;
1770 int mx_sc_elems = tablesize;
1772 schp->buffer = sg_page_malloc(sg_bufflen, sfp->low_dma, &ret_sz);
1775 else if (ret_sz != sg_bufflen) {
1776 sg_bufflen = ret_sz;
1777 mx_sc_elems = sg_bufflen / elem_sz;
1779 schp->sglist_len = sg_bufflen;
1780 memset(schp->buffer, 0, sg_bufflen);
1781 return mx_sc_elems; /* number of scat_gath elements allocated */
1784 #ifdef SG_ALLOW_DIO_CODE
1785 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1786 /* hopefully this generic code will moved to a library */
1788 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1789 - mapping of all pages not successful
1790 - any page is above max_pfn
1791 (i.e., either completely successful or fails)
1794 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1795 unsigned long uaddr, size_t count, int rw,
1796 unsigned long max_pfn)
1799 unsigned int nr_pages;
1800 struct page **pages;
1802 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
1804 /* User attempted Overflow! */
1805 if ((uaddr + count) < uaddr)
1809 if (nr_pages > max_pages)
1816 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1819 /* Try to fault in all of the necessary pages */
1820 down_read(¤t->mm->mmap_sem);
1821 /* rw==READ means read from drive, write into memory area */
1822 res = get_user_pages(
1828 0, /* don't force */
1831 up_read(¤t->mm->mmap_sem);
1833 /* Errors and no page mapped should return here */
1837 for (i=0; i < nr_pages; i++) {
1838 /* FIXME: flush superflous for rw==READ,
1839 * probably wrong function for rw==WRITE
1841 flush_dcache_page(pages[i]);
1842 if (page_to_pfn(pages[i]) > max_pfn)
1844 /* ?? Is locking needed? I don't think so */
1845 /* if (TestSetPageLocked(pages[i]))
1849 /* Populate the scatter/gather list */
1850 sgl[0].page = pages[0];
1851 sgl[0].offset = uaddr & ~PAGE_MASK;
1853 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1854 count -= sgl[0].length;
1855 for (i=1; i < nr_pages ; i++) {
1857 sgl[i].page = pages[i];
1858 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1863 sgl[0].length = count;
1870 /* for (j=0; j < i; j++)
1871 unlock_page(pages[j]); */
1875 for (j=0; j < res; j++)
1876 page_cache_release(pages[j]);
1882 /* And unmap them... */
1884 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1889 for (i=0; i < nr_pages; i++) {
1890 if (dirtied && !PageReserved(sgl[i].page))
1891 SetPageDirty(sgl[i].page);
1892 /* unlock_page(sgl[i].page); */
1893 /* FIXME: cache flush missing for rw==READ
1894 * FIXME: call the correct reference counting function
1896 page_cache_release(sgl[i].page);
1902 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1906 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1908 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1910 #ifdef SG_ALLOW_DIO_CODE
1911 sg_io_hdr_t *hp = &srp->header;
1912 Sg_scatter_hold *schp = &srp->data;
1913 int sg_tablesize = sfp->parentdp->sg_tablesize;
1914 struct scatterlist *sgl;
1915 int mx_sc_elems, res;
1916 struct scsi_device *sdev = sfp->parentdp->device;
1918 if (((unsigned long)hp->dxferp &
1919 queue_dma_alignment(sdev->request_queue)) != 0)
1921 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1922 if (mx_sc_elems <= 0) {
1925 sgl = (struct scatterlist *)schp->buffer;
1926 res = st_map_user_pages(sgl, mx_sc_elems, (unsigned long)hp->dxferp, dxfer_len,
1927 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0, ULONG_MAX);
1930 schp->k_use_sg = res;
1931 schp->dio_in_use = 1;
1932 hp->info |= SG_INFO_DIRECT_IO;
1940 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1943 int blk_size = buff_size;
1944 unsigned char *p = NULL;
1946 if ((blk_size < 0) || (!sfp))
1949 ++blk_size; /* don't know why */
1950 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1951 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1952 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1953 buff_size, blk_size));
1954 if (blk_size <= SG_SCATTER_SZ) {
1955 p = sg_page_malloc(blk_size, sfp->low_dma, &ret_sz);
1958 if (blk_size == ret_sz) { /* got it on the first attempt */
1961 schp->bufflen = blk_size;
1962 schp->b_malloc_len = blk_size;
1966 p = sg_page_malloc(SG_SCATTER_SZ, sfp->low_dma, &ret_sz);
1970 /* Want some local declarations, so start new block ... */
1971 { /* lets try and build a scatter gather list */
1972 struct scatterlist *sclp;
1975 int sg_tablesize = sfp->parentdp->sg_tablesize;
1978 /* N.B. ret_sz carried into this block ... */
1979 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1980 if (mx_sc_elems < 0)
1981 return mx_sc_elems; /* most likely -ENOMEM */
1983 for (k = 0, sclp = schp->buffer, rem_sz = blk_size;
1984 (rem_sz > 0) && (k < mx_sc_elems);
1985 ++k, rem_sz -= ret_sz, ++sclp) {
1991 SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1992 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1996 sclp->page = virt_to_page(p);
1997 sclp->offset = offset_in_page(p);
1998 sclp->length = ret_sz;
2000 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
2001 k, sg_scatg2virt(sclp), ret_sz));
2002 } /* end of for loop */
2004 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
2005 schp->bufflen = blk_size;
2006 if (rem_sz > 0) /* must have failed */
2013 sg_write_xfer(Sg_request * srp)
2015 sg_io_hdr_t *hp = &srp->header;
2016 Sg_scatter_hold *schp = &srp->data;
2018 int j, k, onum, usglen, ksglen, res;
2019 int iovec_count = (int) hp->iovec_count;
2020 int dxfer_dir = hp->dxfer_direction;
2022 unsigned char __user *up;
2023 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2025 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
2026 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2027 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
2028 if (schp->bufflen < num_xfer)
2029 num_xfer = schp->bufflen;
2031 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2033 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2036 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2037 num_xfer, iovec_count, schp->k_use_sg));
2040 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2045 if (0 == schp->k_use_sg) { /* kernel has single buffer */
2046 for (j = 0, p = schp->buffer; j < onum; ++j) {
2047 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
2050 usglen = (num_xfer > usglen) ? usglen : num_xfer;
2051 if (__copy_from_user(p, up, usglen))
2058 } else { /* kernel using scatter gather list */
2059 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2061 ksglen = (int) sclp->length;
2062 p = sg_scatg2virt(sclp);
2063 for (j = 0, k = 0; j < onum; ++j) {
2064 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
2068 for (; p; ++sclp, ksglen = (int) sclp->length,
2069 p = sg_scatg2virt(sclp)) {
2072 if (ksglen > usglen) {
2073 if (usglen >= num_xfer) {
2074 if (__copy_from_user
2079 if (__copy_from_user(p, up, usglen))
2085 if (ksglen >= num_xfer) {
2086 if (__copy_from_user
2091 if (__copy_from_user(p, up, ksglen))
2097 if (k >= schp->k_use_sg)
2106 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
2107 int wr_xf, int *countp, unsigned char __user **up)
2109 int num_xfer = (int) hp->dxfer_len;
2110 unsigned char __user *p = hp->dxferp;
2114 if (wr_xf && ('\0' == hp->interface_id))
2115 count = (int) hp->flags; /* holds "old" input_size */
2120 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2123 count = (int) iovec.iov_len;
2125 if (!access_ok(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count))
2135 sg_remove_scat(Sg_scatter_hold * schp)
2137 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2138 if (schp->buffer && (schp->sglist_len > 0)) {
2139 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2141 if (schp->dio_in_use) {
2142 #ifdef SG_ALLOW_DIO_CODE
2143 st_unmap_user_pages(sclp, schp->k_use_sg, TRUE);
2148 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2150 SCSI_LOG_TIMEOUT(5, printk(
2151 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2152 k, sg_scatg2virt(sclp), sclp->length));
2153 sg_page_free(sg_scatg2virt(sclp), sclp->length);
2159 sg_page_free(schp->buffer, schp->sglist_len);
2160 } else if (schp->buffer)
2161 sg_page_free(schp->buffer, schp->b_malloc_len);
2162 memset(schp, 0, sizeof (*schp));
2166 sg_read_xfer(Sg_request * srp)
2168 sg_io_hdr_t *hp = &srp->header;
2169 Sg_scatter_hold *schp = &srp->data;
2171 int j, k, onum, usglen, ksglen, res;
2172 int iovec_count = (int) hp->iovec_count;
2173 int dxfer_dir = hp->dxfer_direction;
2175 unsigned char __user *up;
2176 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2178 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2179 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2180 num_xfer = hp->dxfer_len;
2181 if (schp->bufflen < num_xfer)
2182 num_xfer = schp->bufflen;
2184 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2186 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2189 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2190 num_xfer, iovec_count, schp->k_use_sg));
2193 if (!access_ok(VERIFY_READ, hp->dxferp, SZ_SG_IOVEC * onum))
2198 if (0 == schp->k_use_sg) { /* kernel has single buffer */
2199 for (j = 0, p = schp->buffer; j < onum; ++j) {
2200 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2203 usglen = (num_xfer > usglen) ? usglen : num_xfer;
2204 if (__copy_to_user(up, p, usglen))
2211 } else { /* kernel using scatter gather list */
2212 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2214 ksglen = (int) sclp->length;
2215 p = sg_scatg2virt(sclp);
2216 for (j = 0, k = 0; j < onum; ++j) {
2217 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2221 for (; p; ++sclp, ksglen = (int) sclp->length,
2222 p = sg_scatg2virt(sclp)) {
2225 if (ksglen > usglen) {
2226 if (usglen >= num_xfer) {
2232 if (__copy_to_user(up, p, usglen))
2238 if (ksglen >= num_xfer) {
2244 if (__copy_to_user(up, p, ksglen))
2250 if (k >= schp->k_use_sg)
2259 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2261 Sg_scatter_hold *schp = &srp->data;
2263 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2265 if ((!outp) || (num_read_xfer <= 0))
2267 if (schp->k_use_sg > 0) {
2269 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2271 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2273 num = (int) sclp->length;
2274 if (num > num_read_xfer) {
2276 (outp, sg_scatg2virt(sclp), num_read_xfer))
2281 (outp, sg_scatg2virt(sclp), num))
2283 num_read_xfer -= num;
2284 if (num_read_xfer <= 0)
2290 if (__copy_to_user(outp, schp->buffer, num_read_xfer))
2297 sg_build_reserve(Sg_fd * sfp, int req_size)
2299 Sg_scatter_hold *schp = &sfp->reserve;
2301 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2303 if (req_size < PAGE_SIZE)
2304 req_size = PAGE_SIZE;
2305 if (0 == sg_build_indirect(schp, sfp, req_size))
2308 sg_remove_scat(schp);
2309 req_size >>= 1; /* divide by 2 */
2310 } while (req_size > (PAGE_SIZE / 2));
2314 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2316 Sg_scatter_hold *req_schp = &srp->data;
2317 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2320 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2321 size = (size + 1) & (~1); /* round to even for aha1542 */
2322 if (rsv_schp->k_use_sg > 0) {
2325 struct scatterlist *sclp =
2326 (struct scatterlist *) rsv_schp->buffer;
2328 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
2329 num = (int) sclp->length;
2332 req_schp->k_use_sg = 0;
2333 req_schp->buffer = sg_scatg2virt(sclp);
2335 sfp->save_scat_len = num;
2336 sclp->length = (unsigned) rem;
2337 req_schp->k_use_sg = k + 1;
2338 req_schp->sglist_len =
2339 rsv_schp->sglist_len;
2340 req_schp->buffer = rsv_schp->buffer;
2342 req_schp->bufflen = size;
2343 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2348 if (k >= rsv_schp->k_use_sg)
2349 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2351 req_schp->k_use_sg = 0;
2352 req_schp->bufflen = size;
2353 req_schp->buffer = rsv_schp->buffer;
2354 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2359 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2361 Sg_scatter_hold *req_schp = &srp->data;
2362 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2364 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2365 (int) req_schp->k_use_sg));
2366 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2367 struct scatterlist *sclp =
2368 (struct scatterlist *) rsv_schp->buffer;
2370 if (sfp->save_scat_len > 0)
2371 (sclp + (req_schp->k_use_sg - 1))->length =
2372 (unsigned) sfp->save_scat_len;
2374 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2376 req_schp->k_use_sg = 0;
2377 req_schp->bufflen = 0;
2378 req_schp->buffer = NULL;
2379 req_schp->sglist_len = 0;
2380 sfp->save_scat_len = 0;
2385 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2388 unsigned long iflags;
2390 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2391 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2392 /* look for requests that are ready + not SG_IO owned */
2393 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2394 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2395 resp->done = 2; /* guard against other readers */
2399 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2403 #ifdef CONFIG_SCSI_PROC_FS
2405 sg_get_nth_request(Sg_fd * sfp, int nth)
2408 unsigned long iflags;
2411 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2412 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2413 ++k, resp = resp->nextrp) ;
2414 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2419 /* always adds to end of list */
2421 sg_add_request(Sg_fd * sfp)
2424 unsigned long iflags;
2426 Sg_request *rp = sfp->req_arr;
2428 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2431 memset(rp, 0, sizeof (Sg_request));
2436 if (0 == sfp->cmd_q)
2437 resp = NULL; /* command queuing disallowed */
2439 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2443 if (k < SG_MAX_QUEUE) {
2444 memset(rp, 0, sizeof (Sg_request));
2446 while (resp->nextrp)
2447 resp = resp->nextrp;
2455 resp->nextrp = NULL;
2456 resp->header.duration = jiffies_to_msecs(jiffies);
2457 resp->my_cmdp = NULL;
2459 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2463 /* Return of 1 for found; 0 for not found */
2465 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2467 Sg_request *prev_rp;
2469 unsigned long iflags;
2472 if ((!sfp) || (!srp) || (!sfp->headrp))
2474 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2476 srp->my_cmdp->upper_private_data = NULL;
2477 prev_rp = sfp->headrp;
2478 if (srp == prev_rp) {
2479 sfp->headrp = prev_rp->nextrp;
2480 prev_rp->parentfp = NULL;
2483 while ((rp = prev_rp->nextrp)) {
2485 prev_rp->nextrp = rp->nextrp;
2486 rp->parentfp = NULL;
2493 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2497 #ifdef CONFIG_SCSI_PROC_FS
2499 sg_get_nth_sfp(Sg_device * sdp, int nth)
2502 unsigned long iflags;
2505 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2506 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2507 ++k, resp = resp->nextfp) ;
2508 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2514 sg_add_sfp(Sg_device * sdp, int dev)
2517 unsigned long iflags;
2519 sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
2522 memset(sfp, 0, sizeof (Sg_fd));
2523 init_waitqueue_head(&sfp->read_wait);
2524 rwlock_init(&sfp->rq_list_lock);
2526 sfp->timeout = SG_DEFAULT_TIMEOUT;
2527 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2528 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2529 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2530 sdp->device->host->unchecked_isa_dma : 1;
2531 sfp->cmd_q = SG_DEF_COMMAND_Q;
2532 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2533 sfp->parentdp = sdp;
2534 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2537 else { /* add to tail of existing list */
2538 Sg_fd *pfp = sdp->headfp;
2543 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2544 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2545 sg_build_reserve(sfp, sg_big_buff);
2546 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2547 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2552 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2557 prev_fp = sdp->headfp;
2559 sdp->headfp = prev_fp->nextfp;
2561 while ((fp = prev_fp->nextfp)) {
2563 prev_fp->nextfp = fp->nextfp;
2569 if (sfp->reserve.bufflen > 0) {
2571 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2572 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2573 if (sfp->mmap_called)
2574 sg_rb_correct4mmap(&sfp->reserve, 0); /* undo correction */
2575 sg_remove_scat(&sfp->reserve);
2577 sfp->parentdp = NULL;
2578 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2579 sg_page_free((char *) sfp, sizeof (Sg_fd));
2582 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2584 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2591 for (srp = sfp->headrp; srp; srp = tsrp) {
2593 if (sg_srp_done(srp, sfp))
2594 sg_finish_rem_req(srp);
2599 unsigned long iflags;
2601 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2602 __sg_remove_sfp(sdp, sfp);
2603 if (sdp->detached && (NULL == sdp->headfp)) {
2607 for (k = 0; k < maxd; ++k) {
2608 if (sdp == sg_dev_arr[k])
2612 sg_dev_arr[k] = NULL;
2613 kfree((char *) sdp);
2616 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2618 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2619 /* only bump the access_count if we actually succeeded in
2620 * throwing another counter on the host module */
2621 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2622 sfp->closed = 1; /* flag dirty state on this fd */
2623 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2630 sg_res_in_use(Sg_fd * sfp)
2632 const Sg_request *srp;
2633 unsigned long iflags;
2635 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2636 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2639 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2643 /* If retSzp==NULL want exact size or fail */
2645 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2656 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2658 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2660 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2661 order++, a_size <<= 1) ;
2662 resp = (char *) __get_free_pages(page_mask, order);
2663 while ((!resp) && order && retSzp) {
2665 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2666 resp = (char *) __get_free_pages(page_mask, order); /* try half */
2670 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2671 memset(resp, 0, resSz);
2679 sg_page_free(char *buff, int size)
2685 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2686 order++, a_size <<= 1) ;
2687 free_pages((unsigned long) buff, order);
2690 #ifndef MAINTENANCE_IN_CMD
2691 #define MAINTENANCE_IN_CMD 0xa3
2694 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2695 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2696 READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
2697 SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
2701 sg_allow_access(unsigned char opcode, char dev_type)
2705 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2707 for (k = 0; k < sizeof (allow_ops); ++k) {
2708 if (opcode == allow_ops[k])
2714 #ifdef CONFIG_SCSI_PROC_FS
2719 unsigned long iflags;
2721 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2722 for (k = sg_dev_max - 1; k >= 0; --k)
2723 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2725 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2726 return k + 1; /* origin 1 */
2733 Sg_device *sdp = NULL;
2734 unsigned long iflags;
2736 if (sg_dev_arr && (dev >= 0)) {
2737 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2738 if (dev < sg_dev_max)
2739 sdp = sg_dev_arr[dev];
2740 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2745 #ifdef CONFIG_SCSI_PROC_FS
2747 static struct proc_dir_entry *sg_proc_sgp = NULL;
2749 static char sg_proc_sg_dirname[] = "scsi/sg";
2751 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2753 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2754 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2755 size_t count, loff_t *off);
2756 static struct file_operations adio_fops = {
2757 /* .owner, .read and .llseek added in sg_proc_init() */
2758 .open = sg_proc_single_open_adio,
2759 .write = sg_proc_write_adio,
2760 .release = single_release,
2763 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2764 static ssize_t sg_proc_write_dressz(struct file *filp,
2765 const char __user *buffer, size_t count, loff_t *off);
2766 static struct file_operations dressz_fops = {
2767 .open = sg_proc_single_open_dressz,
2768 .write = sg_proc_write_dressz,
2769 .release = single_release,
2772 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2773 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2774 static struct file_operations version_fops = {
2775 .open = sg_proc_single_open_version,
2776 .release = single_release,
2779 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2780 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2781 static struct file_operations devhdr_fops = {
2782 .open = sg_proc_single_open_devhdr,
2783 .release = single_release,
2786 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2787 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2788 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2789 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2790 static void dev_seq_stop(struct seq_file *s, void *v);
2791 static struct file_operations dev_fops = {
2792 .open = sg_proc_open_dev,
2793 .release = seq_release,
2795 static struct seq_operations dev_seq_ops = {
2796 .start = dev_seq_start,
2797 .next = dev_seq_next,
2798 .stop = dev_seq_stop,
2799 .show = sg_proc_seq_show_dev,
2802 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2803 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2804 static struct file_operations devstrs_fops = {
2805 .open = sg_proc_open_devstrs,
2806 .release = seq_release,
2808 static struct seq_operations devstrs_seq_ops = {
2809 .start = dev_seq_start,
2810 .next = dev_seq_next,
2811 .stop = dev_seq_stop,
2812 .show = sg_proc_seq_show_devstrs,
2815 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2816 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2817 static struct file_operations debug_fops = {
2818 .open = sg_proc_open_debug,
2819 .release = seq_release,
2821 static struct seq_operations debug_seq_ops = {
2822 .start = dev_seq_start,
2823 .next = dev_seq_next,
2824 .stop = dev_seq_stop,
2825 .show = sg_proc_seq_show_debug,
2829 struct sg_proc_leaf {
2831 struct file_operations * fops;
2834 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2835 {"allow_dio", &adio_fops},
2836 {"debug", &debug_fops},
2837 {"def_reserved_size", &dressz_fops},
2838 {"device_hdr", &devhdr_fops},
2839 {"devices", &dev_fops},
2840 {"device_strs", &devstrs_fops},
2841 {"version", &version_fops}
2849 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2850 struct proc_dir_entry *pdep;
2851 struct sg_proc_leaf * leaf;
2853 sg_proc_sgp = create_proc_entry(sg_proc_sg_dirname,
2854 S_IFDIR | S_IRUGO | S_IXUGO, NULL);
2857 for (k = 0; k < num_leaves; ++k) {
2858 leaf = &sg_proc_leaf_arr[k];
2859 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2860 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2862 leaf->fops->owner = THIS_MODULE,
2863 leaf->fops->read = seq_read,
2864 leaf->fops->llseek = seq_lseek,
2865 pdep->proc_fops = leaf->fops;
2872 sg_proc_cleanup(void)
2876 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2880 for (k = 0; k < num_leaves; ++k)
2881 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2882 remove_proc_entry(sg_proc_sg_dirname, NULL);
2886 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2888 seq_printf(s, "%d\n", *((int *)s->private));
2892 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2894 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2898 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2899 size_t count, loff_t *off)
2904 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2906 num = (count < 10) ? count : 10;
2907 if (copy_from_user(buff, buffer, num))
2910 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2914 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2916 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2920 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2921 size_t count, loff_t *off)
2924 unsigned long k = ULONG_MAX;
2927 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2929 num = (count < 10) ? count : 10;
2930 if (copy_from_user(buff, buffer, num))
2933 k = simple_strtoul(buff, NULL, 10);
2934 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2941 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2943 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2948 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2950 return single_open(file, sg_proc_seq_show_version, NULL);
2953 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2955 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2960 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2962 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2965 struct sg_proc_deviter {
2970 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2972 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2976 if (NULL == sg_dev_arr)
2979 it->max = sg_last_dev();
2980 if (it->index >= it->max)
2988 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2990 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2993 return (it->index < it->max) ? it : NULL;
2996 static void dev_seq_stop(struct seq_file *s, void *v)
3001 static int sg_proc_open_dev(struct inode *inode, struct file *file)
3003 return seq_open(file, &dev_seq_ops);
3006 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
3008 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3010 struct scsi_device *scsidp;
3012 sdp = it ? sg_get_dev(it->index) : NULL;
3013 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
3014 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
3015 scsidp->host->host_no, scsidp->channel,
3016 scsidp->id, scsidp->lun, (int) scsidp->type,
3018 (int) scsidp->queue_depth,
3019 (int) scsidp->device_busy,
3020 (int) scsi_device_online(scsidp));
3022 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
3026 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
3028 return seq_open(file, &devstrs_seq_ops);
3031 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
3033 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3035 struct scsi_device *scsidp;
3037 sdp = it ? sg_get_dev(it->index) : NULL;
3038 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
3039 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
3040 scsidp->vendor, scsidp->model, scsidp->rev);
3042 seq_printf(s, "<no active device>\n");
3046 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
3048 int k, m, new_interface, blen, usg;
3051 const sg_io_hdr_t *hp;
3055 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
3056 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
3057 "(res)sgat=%d low_dma=%d\n", k + 1,
3058 jiffies_to_msecs(fp->timeout),
3059 fp->reserve.bufflen,
3060 (int) fp->reserve.k_use_sg,
3062 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
3063 (int) fp->cmd_q, (int) fp->force_packid,
3064 (int) fp->keep_orphan, (int) fp->closed);
3065 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
3067 new_interface = (hp->interface_id == '\0') ? 0 : 1;
3068 if (srp->res_used) {
3069 if (new_interface &&
3070 (SG_FLAG_MMAP_IO & hp->flags))
3075 if (SG_INFO_DIRECT_IO_MASK & hp->info)
3081 blen = srp->my_cmdp ?
3082 srp->my_cmdp->sr_bufflen : srp->data.bufflen;
3083 usg = srp->my_cmdp ?
3084 srp->my_cmdp->sr_use_sg : srp->data.k_use_sg;
3085 seq_printf(s, srp->done ?
3086 ((1 == srp->done) ? "rcv:" : "fin:")
3087 : (srp->my_cmdp ? "act:" : "prior:"));
3088 seq_printf(s, " id=%d blen=%d",
3089 srp->header.pack_id, blen);
3091 seq_printf(s, " dur=%d", hp->duration);
3093 ms = jiffies_to_msecs(jiffies);
3094 seq_printf(s, " t_o/elap=%d/%d",
3095 (new_interface ? hp->timeout :
3096 jiffies_to_msecs(fp->timeout)),
3097 (ms > hp->duration ? ms - hp->duration : 0));
3099 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
3100 (int) srp->data.cmd_opcode);
3103 seq_printf(s, " No requests active\n");
3107 static int sg_proc_open_debug(struct inode *inode, struct file *file)
3109 return seq_open(file, &debug_seq_ops);
3112 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
3114 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3117 if (it && (0 == it->index)) {
3118 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
3119 "(origin 1)\n", sg_dev_max, (int)it->max);
3120 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
3122 sdp = it ? sg_get_dev(it->index) : NULL;
3124 struct scsi_device *scsidp = sdp->device;
3126 if (NULL == scsidp) {
3127 seq_printf(s, "device %d detached ??\n",
3132 if (sg_get_nth_sfp(sdp, 0)) {
3133 seq_printf(s, " >>> device=%s ",
3134 sdp->disk->disk_name);
3136 seq_printf(s, "detached pending close ");
3139 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
3140 scsidp->host->host_no,
3141 scsidp->channel, scsidp->id,
3143 scsidp->host->hostt->emulated);
3144 seq_printf(s, " sg_tablesize=%d excl=%d\n",
3145 sdp->sg_tablesize, sdp->exclude);
3147 sg_proc_debug_helper(s, sdp);
3152 #endif /* CONFIG_SCSI_PROC_FS */
3154 module_init(init_sg);
3155 module_exit(exit_sg);
3156 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);