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 - 2004 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 = 30531; /* 2 digits for each component */
22 #define SG_VERSION_STR "3.5.31"
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>
51 #include <linux/blkdev.h>
53 #include <scsi/scsi_host.h>
54 #include <scsi/scsi_driver.h>
55 #include <scsi/scsi_ioctl.h>
58 #include "scsi_logging.h"
60 #ifdef CONFIG_SCSI_PROC_FS
61 #include <linux/proc_fs.h>
62 static char *sg_version_date = "20040516";
64 static int sg_proc_init(void);
65 static void sg_proc_cleanup(void);
68 #ifndef LINUX_VERSION_CODE
69 #include <linux/version.h>
70 #endif /* LINUX_VERSION_CODE */
72 #define SG_ALLOW_DIO_DEF 0
73 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
75 #define SG_MAX_DEVS 32768
78 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
79 * Then when using 32 bit integers x * m may overflow during the calculation.
80 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
81 * calculates the same, but prevents the overflow when both m and d
82 * are "small" numbers (like HZ and USER_HZ).
83 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
86 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
88 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
90 int sg_big_buff = SG_DEF_RESERVED_SIZE;
91 /* N.B. This variable is readable and writeable via
92 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
93 of this size (or less if there is not enough memory) will be reserved
94 for use by this file descriptor. [Deprecated usage: this variable is also
95 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
96 the kernel (i.e. it is not a module).] */
97 static int def_reserved_size = -1; /* picks up init parameter */
98 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
100 #define SG_SECTOR_SZ 512
101 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
103 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
105 static int sg_add(struct class_device *);
106 static void sg_remove(struct class_device *);
108 static Scsi_Request *dummy_cmdp; /* only used for sizeof */
110 static rwlock_t sg_dev_arr_lock = RW_LOCK_UNLOCKED; /* Also used to lock
111 file descriptor list for device */
113 static struct class_interface sg_interface = {
118 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
119 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
120 unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
121 unsigned bufflen; /* Size of (aggregate) data buffer */
122 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
123 void *buffer; /* Data buffer or scatter list (k_use_sg>0) */
124 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
125 unsigned char cmd_opcode; /* first byte of command */
128 struct sg_device; /* forward declarations */
131 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
132 Scsi_Request *my_cmdp; /* != 0 when request with lower levels */
133 struct sg_request *nextrp; /* NULL -> tail request (slist) */
134 struct sg_fd *parentfp; /* NULL -> not in use */
135 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
136 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
137 unsigned char sense_b[sizeof (dummy_cmdp->sr_sense_buffer)];
138 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
139 char orphan; /* 1 -> drop on sight, 0 -> normal */
140 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
141 volatile char done; /* 0->before bh, 1->before read, 2->read */
144 typedef struct sg_fd { /* holds the state of a file descriptor */
145 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
146 struct sg_device *parentdp; /* owning device */
147 wait_queue_head_t read_wait; /* queue read until command done */
148 rwlock_t rq_list_lock; /* protect access to list in req_arr */
149 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
150 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
151 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
152 unsigned save_scat_len; /* original length of trunc. scat. element */
153 Sg_request *headrp; /* head of request slist, NULL->empty */
154 struct fasync_struct *async_qp; /* used by asynchronous notification */
155 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
156 char low_dma; /* as in parent but possibly overridden to 1 */
157 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
158 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
159 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
160 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
161 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
162 char mmap_called; /* 0 -> mmap() never called on this fd */
165 typedef struct sg_device { /* holds the state of each scsi generic device */
166 struct scsi_device *device;
167 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
168 int sg_tablesize; /* adapter's max scatter-gather table size */
169 Sg_fd *headfp; /* first open fd belonging to this device */
170 volatile char detached; /* 0->attached, 1->detached pending removal */
171 volatile char exclude; /* opened for exclusive access */
172 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
173 struct gendisk *disk;
174 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
177 static int sg_fasync(int fd, struct file *filp, int mode);
178 static void sg_cmd_done(Scsi_Cmnd * SCpnt); /* tasklet or soft irq callback */
179 static int sg_start_req(Sg_request * srp);
180 static void sg_finish_rem_req(Sg_request * srp);
181 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
184 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
186 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
187 int blocking, int read_only, Sg_request ** o_srp);
188 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
189 unsigned char *cmnd, int timeout, int blocking);
190 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
191 int wr_xf, int *countp, unsigned char __user **up);
192 static int sg_write_xfer(Sg_request * srp);
193 static int sg_read_xfer(Sg_request * srp);
194 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
195 static void sg_remove_scat(Sg_scatter_hold * schp);
196 static void sg_build_reserve(Sg_fd * sfp, int req_size);
197 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
198 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
199 static char *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
200 static void sg_page_free(char *buff, int size);
201 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
202 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
203 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
205 static Sg_request *sg_add_request(Sg_fd * sfp);
206 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
207 static int sg_res_in_use(Sg_fd * sfp);
208 static int sg_ms_to_jif(unsigned int msecs);
209 static inline unsigned sg_jif_to_ms(int jifs);
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 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
238 sdp = sg_get_dev(dev);
239 if ((!sdp) || (!sdp->device))
244 /* scsi generic is only for non-disk, non-cd, non-tape devices */
245 if ( (sdp->device->type == TYPE_DISK) || (sdp->device->type == TYPE_MOD) || (sdp->device->type == TYPE_ROM)
246 || (sdp->device->type == TYPE_WORM) || ( sdp->device->type == TYPE_TAPE))
247 printk(KERN_WARNING "%s: Using deprecated /dev/sg mechanism instead of SG_IO on the actual device\n",current->comm);
250 /* This driver's module count bumped by fops_get in <linux/fs.h> */
251 /* Prevent the device driver from vanishing while we sleep */
252 retval = scsi_device_get(sdp->device);
256 if (!((flags & O_NONBLOCK) ||
257 scsi_block_when_processing_errors(sdp->device))) {
259 /* we are in error recovery for this device */
263 if (flags & O_EXCL) {
264 if (O_RDONLY == (flags & O_ACCMODE)) {
265 retval = -EPERM; /* Can't lock it with read only access */
268 if (sdp->headfp && (flags & O_NONBLOCK)) {
273 __wait_event_interruptible(sdp->o_excl_wait,
274 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
276 retval = res; /* -ERESTARTSYS because signal hit process */
279 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
280 if (flags & O_NONBLOCK) {
285 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
288 retval = res; /* -ERESTARTSYS because signal hit process */
296 if (!sdp->headfp) { /* no existing opens on this device */
298 sdp->sg_tablesize = sdp->device->host->sg_tablesize;
300 if ((sfp = sg_add_sfp(sdp, dev)))
301 filp->private_data = sfp;
304 sdp->exclude = 0; /* undo if error */
311 scsi_device_put(sdp->device);
315 /* Following function was formerly called 'sg_close' */
317 sg_release(struct inode *inode, struct file *filp)
322 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
324 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
325 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
326 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
327 if (!sdp->detached) {
328 scsi_device_put(sdp->device);
331 wake_up_interruptible(&sdp->o_excl_wait);
337 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
343 int req_pack_id = -1;
344 struct sg_header old_hdr;
348 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
350 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
351 sdp->disk->disk_name, (int) count));
352 if (ppos != &filp->f_pos) ; /* FIXME: Hmm. Seek to the right place, or fail? */
353 if ((k = verify_area(VERIFY_WRITE, buf, count)))
355 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
356 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
358 if (old_hdr.reply_len < 0) {
359 if (count >= SZ_SG_IO_HDR) {
361 (&new_hdr, buf, SZ_SG_IO_HDR))
363 req_pack_id = new_hdr.pack_id;
366 req_pack_id = old_hdr.pack_id;
368 srp = sg_get_rq_mark(sfp, req_pack_id);
369 if (!srp) { /* now wait on packet to arrive */
372 if (filp->f_flags & O_NONBLOCK)
375 res = 0; /* following is a macro that beats race condition */
376 __wait_event_interruptible(sfp->read_wait,
377 (sdp->detached || (srp = sg_get_rq_mark(sfp, req_pack_id))),
383 return res; /* -ERESTARTSYS because signal hit process */
386 if (srp->header.interface_id != '\0')
387 return sg_new_read(sfp, buf, count, srp);
390 memset(&old_hdr, 0, SZ_SG_HEADER);
391 old_hdr.reply_len = (int) hp->timeout;
392 old_hdr.pack_len = old_hdr.reply_len; /* very old, strange behaviour */
393 old_hdr.pack_id = hp->pack_id;
394 old_hdr.twelve_byte =
395 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
396 old_hdr.target_status = hp->masked_status;
397 old_hdr.host_status = hp->host_status;
398 old_hdr.driver_status = hp->driver_status;
399 if ((CHECK_CONDITION & hp->masked_status) ||
400 (DRIVER_SENSE & hp->driver_status))
401 memcpy(old_hdr.sense_buffer, srp->sense_b,
402 sizeof (old_hdr.sense_buffer));
403 switch (hp->host_status) {
404 /* This setup of 'result' is for backward compatibility and is best
405 ignored by the user who should use target, host + driver status */
407 case DID_PASSTHROUGH:
414 old_hdr.result = EBUSY;
421 old_hdr.result = EIO;
424 old_hdr.result = (srp->sense_b[0] == 0 &&
425 hp->masked_status == GOOD) ? 0 : EIO;
428 old_hdr.result = EIO;
432 /* Now copy the result back to the user buffer. */
433 if (count >= SZ_SG_HEADER) {
434 if (__copy_to_user(buf, &old_hdr, SZ_SG_HEADER))
437 if (count > old_hdr.reply_len)
438 count = old_hdr.reply_len;
439 if (count > SZ_SG_HEADER) {
441 sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)))
445 count = (old_hdr.result == 0) ? 0 : -EIO;
446 sg_finish_rem_req(srp);
451 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
453 sg_io_hdr_t *hp = &srp->header;
457 if (count < SZ_SG_IO_HDR) {
462 if ((hp->mx_sb_len > 0) && hp->sbp) {
463 if ((CHECK_CONDITION & hp->masked_status) ||
464 (DRIVER_SENSE & hp->driver_status)) {
465 int sb_len = sizeof (dummy_cmdp->sr_sense_buffer);
466 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
467 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
468 len = (len > sb_len) ? sb_len : len;
469 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
476 if (hp->masked_status || hp->host_status || hp->driver_status)
477 hp->info |= SG_INFO_CHECK;
478 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
482 err = sg_read_xfer(srp);
484 sg_finish_rem_req(srp);
485 return (0 == err) ? count : err;
489 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
491 int mxsize, cmd_size, k;
492 int input_size, blocking;
493 unsigned char opcode;
497 struct sg_header old_hdr;
499 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
501 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
503 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
504 sdp->disk->disk_name, (int) count));
507 if (!((filp->f_flags & O_NONBLOCK) ||
508 scsi_block_when_processing_errors(sdp->device)))
510 if (ppos != &filp->f_pos) ; /* FIXME: Hmm. Seek to the right place, or fail? */
512 if ((k = verify_area(VERIFY_READ, buf, count)))
513 return k; /* protects following copy_from_user()s + get_user()s */
514 if (count < SZ_SG_HEADER)
516 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
518 blocking = !(filp->f_flags & O_NONBLOCK);
519 if (old_hdr.reply_len < 0)
520 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
521 if (count < (SZ_SG_HEADER + 6))
522 return -EIO; /* The minimum scsi command length is 6 bytes. */
524 if (!(srp = sg_add_request(sfp))) {
525 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
529 __get_user(opcode, buf);
530 if (sfp->next_cmd_len > 0) {
531 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
532 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
533 sfp->next_cmd_len = 0;
534 sg_remove_request(sfp, srp);
537 cmd_size = sfp->next_cmd_len;
538 sfp->next_cmd_len = 0; /* reset so only this write() effected */
540 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
541 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
544 SCSI_LOG_TIMEOUT(4, printk(
545 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
546 /* Determine buffer size. */
547 input_size = count - cmd_size;
548 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
549 mxsize -= SZ_SG_HEADER;
550 input_size -= SZ_SG_HEADER;
551 if (input_size < 0) {
552 sg_remove_request(sfp, srp);
553 return -EIO; /* User did not pass enough bytes for this command. */
556 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
557 hp->cmd_len = (unsigned char) cmd_size;
561 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
562 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
564 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
565 hp->dxfer_len = mxsize;
566 hp->dxferp = (char __user *)buf + cmd_size;
568 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
569 hp->flags = input_size; /* structure abuse ... */
570 hp->pack_id = old_hdr.pack_id;
572 if (__copy_from_user(cmnd, buf, cmd_size))
574 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
575 return (k < 0) ? k : count;
579 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
580 int blocking, int read_only, Sg_request ** o_srp)
585 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
588 if (count < SZ_SG_IO_HDR)
590 if ((k = verify_area(VERIFY_READ, buf, count)))
591 return k; /* protects following copy_from_user()s + get_user()s */
593 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
594 if (!(srp = sg_add_request(sfp))) {
595 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
599 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
600 sg_remove_request(sfp, srp);
603 if (hp->interface_id != 'S') {
604 sg_remove_request(sfp, srp);
607 if (hp->flags & SG_FLAG_MMAP_IO) {
608 if (hp->dxfer_len > sfp->reserve.bufflen) {
609 sg_remove_request(sfp, srp);
610 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
612 if (hp->flags & SG_FLAG_DIRECT_IO) {
613 sg_remove_request(sfp, srp);
614 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
616 if (sg_res_in_use(sfp)) {
617 sg_remove_request(sfp, srp);
618 return -EBUSY; /* reserve buffer already being used */
621 timeout = sg_ms_to_jif(srp->header.timeout);
622 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
623 sg_remove_request(sfp, srp);
626 if ((k = verify_area(VERIFY_READ, hp->cmdp, hp->cmd_len))) {
627 sg_remove_request(sfp, srp);
628 return k; /* protects following copy_from_user()s + get_user()s */
630 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
631 sg_remove_request(sfp, srp);
635 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
636 sg_remove_request(sfp, srp);
639 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
648 sg_common_write(Sg_fd * sfp, Sg_request * srp,
649 unsigned char *cmnd, int timeout, int blocking)
653 Sg_device *sdp = sfp->parentdp;
654 sg_io_hdr_t *hp = &srp->header;
657 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
659 hp->masked_status = 0;
663 hp->driver_status = 0;
665 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
666 (int) cmnd[0], (int) hp->cmd_len));
668 if ((k = sg_start_req(srp))) {
669 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
670 sg_finish_rem_req(srp);
671 return k; /* probably out of space --> ENOMEM */
673 if ((k = sg_write_xfer(srp))) {
674 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
675 sg_finish_rem_req(srp);
679 sg_finish_rem_req(srp);
682 SRpnt = scsi_allocate_request(sdp->device, GFP_ATOMIC);
684 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
685 sg_finish_rem_req(srp);
689 srp->my_cmdp = SRpnt;
690 q = SRpnt->sr_device->request_queue;
691 SRpnt->sr_request->rq_disk = sdp->disk;
692 SRpnt->sr_sense_buffer[0] = 0;
693 SRpnt->sr_cmd_len = hp->cmd_len;
694 SRpnt->sr_use_sg = srp->data.k_use_sg;
695 SRpnt->sr_sglist_len = srp->data.sglist_len;
696 SRpnt->sr_bufflen = srp->data.bufflen;
697 SRpnt->sr_underflow = 0;
698 SRpnt->sr_buffer = srp->data.buffer;
699 switch (hp->dxfer_direction) {
700 case SG_DXFER_TO_FROM_DEV:
701 case SG_DXFER_FROM_DEV:
702 SRpnt->sr_data_direction = SCSI_DATA_READ;
704 case SG_DXFER_TO_DEV:
705 SRpnt->sr_data_direction = SCSI_DATA_WRITE;
707 case SG_DXFER_UNKNOWN:
708 SRpnt->sr_data_direction = SCSI_DATA_UNKNOWN;
711 SRpnt->sr_data_direction = SCSI_DATA_NONE;
714 SRpnt->upper_private_data = srp;
715 srp->data.k_use_sg = 0;
716 srp->data.sglist_len = 0;
717 srp->data.bufflen = 0;
718 srp->data.buffer = NULL;
719 hp->duration = jiffies; /* unit jiffies now, millisecs after done */
720 /* Now send everything of to mid-level. The next time we hear about this
721 packet is when sg_cmd_done() is called (i.e. a callback). */
722 scsi_do_req(SRpnt, (void *) cmnd,
723 (void *) SRpnt->sr_buffer, hp->dxfer_len,
724 sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
725 /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
730 sg_ioctl(struct inode *inode, struct file *filp,
731 unsigned int cmd_in, unsigned long arg)
733 void __user *p = (void __user *)arg;
735 int result, val, read_only;
739 unsigned long iflags;
741 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
743 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
744 sdp->disk->disk_name, (int) cmd_in));
745 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
750 int blocking = 1; /* ignore O_NONBLOCK flag */
754 if (!scsi_block_when_processing_errors(sdp->device))
756 result = verify_area(VERIFY_WRITE, p, SZ_SG_IO_HDR);
760 sg_new_write(sfp, p, SZ_SG_IO_HDR,
761 blocking, read_only, &srp);
764 srp->sg_io_owned = 1;
766 result = 0; /* following macro to beat race condition */
767 __wait_event_interruptible(sfp->read_wait,
768 (sdp->detached || sfp->closed || srp->done),
773 return 0; /* request packet dropped already */
777 return result; /* -ERESTARTSYS because signal hit process */
780 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
781 return (result < 0) ? result : 0;
784 result = get_user(val, ip);
789 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
790 val = MULDIV (INT_MAX, USER_HZ, HZ);
791 sfp->timeout_user = val;
792 sfp->timeout = MULDIV (val, HZ, USER_HZ);
795 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
796 /* strange ..., for backward compatibility */
797 return sfp->timeout_user;
798 case SG_SET_FORCE_LOW_DMA:
799 result = get_user(val, ip);
804 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
805 val = (int) sfp->reserve.bufflen;
806 sg_remove_scat(&sfp->reserve);
807 sg_build_reserve(sfp, val);
812 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
816 return put_user((int) sfp->low_dma, ip);
819 verify_area(VERIFY_WRITE, p, sizeof (sg_scsi_id_t));
823 sg_scsi_id_t __user *sg_idp = p;
827 __put_user((int) sdp->device->host->host_no,
829 __put_user((int) sdp->device->channel,
831 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
832 __put_user((int) sdp->device->lun, &sg_idp->lun);
833 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
834 __put_user((short) sdp->device->host->cmd_per_lun,
835 &sg_idp->h_cmd_per_lun);
836 __put_user((short) sdp->device->queue_depth,
837 &sg_idp->d_queue_depth);
838 __put_user(0, &sg_idp->unused[0]);
839 __put_user(0, &sg_idp->unused[1]);
842 case SG_SET_FORCE_PACK_ID:
843 result = get_user(val, ip);
846 sfp->force_packid = val ? 1 : 0;
849 result = verify_area(VERIFY_WRITE, ip, sizeof (int));
852 read_lock_irqsave(&sfp->rq_list_lock, iflags);
853 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
854 if ((1 == srp->done) && (!srp->sg_io_owned)) {
855 read_unlock_irqrestore(&sfp->rq_list_lock,
857 __put_user(srp->header.pack_id, ip);
861 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
864 case SG_GET_NUM_WAITING:
865 read_lock_irqsave(&sfp->rq_list_lock, iflags);
866 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
867 if ((1 == srp->done) && (!srp->sg_io_owned))
870 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
871 return put_user(val, ip);
872 case SG_GET_SG_TABLESIZE:
873 return put_user(sdp->sg_tablesize, ip);
874 case SG_SET_RESERVED_SIZE:
875 result = get_user(val, ip);
880 if (val != sfp->reserve.bufflen) {
881 if (sg_res_in_use(sfp) || sfp->mmap_called)
883 sg_remove_scat(&sfp->reserve);
884 sg_build_reserve(sfp, val);
887 case SG_GET_RESERVED_SIZE:
888 val = (int) sfp->reserve.bufflen;
889 return put_user(val, ip);
890 case SG_SET_COMMAND_Q:
891 result = get_user(val, ip);
894 sfp->cmd_q = val ? 1 : 0;
896 case SG_GET_COMMAND_Q:
897 return put_user((int) sfp->cmd_q, ip);
898 case SG_SET_KEEP_ORPHAN:
899 result = get_user(val, ip);
902 sfp->keep_orphan = val;
904 case SG_GET_KEEP_ORPHAN:
905 return put_user((int) sfp->keep_orphan, ip);
906 case SG_NEXT_CMD_LEN:
907 result = get_user(val, ip);
910 sfp->next_cmd_len = (val > 0) ? val : 0;
912 case SG_GET_VERSION_NUM:
913 return put_user(sg_version_num, ip);
914 case SG_GET_ACCESS_COUNT:
915 /* faked - we don't have a real access count anymore */
916 val = (sdp->device ? 1 : 0);
917 return put_user(val, ip);
918 case SG_GET_REQUEST_TABLE:
919 result = verify_area(VERIFY_WRITE, p,
920 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
924 sg_req_info_t rinfo[SG_MAX_QUEUE];
926 read_lock_irqsave(&sfp->rq_list_lock, iflags);
927 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
928 ++val, srp = srp ? srp->nextrp : srp) {
929 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
931 rinfo[val].req_state = srp->done + 1;
933 srp->header.masked_status &
934 srp->header.host_status &
935 srp->header.driver_status;
936 rinfo[val].duration =
937 srp->done ? srp->header.duration :
939 jiffies - srp->header.duration);
940 rinfo[val].orphan = srp->orphan;
941 rinfo[val].sg_io_owned = srp->sg_io_owned;
942 rinfo[val].pack_id = srp->header.pack_id;
943 rinfo[val].usr_ptr = srp->header.usr_ptr;
946 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
947 return (__copy_to_user(p, rinfo,
948 SZ_SG_REQ_INFO * SG_MAX_QUEUE) ? -EFAULT : 0);
950 case SG_EMULATED_HOST:
953 return put_user(sdp->device->host->hostt->emulated, ip);
957 if (filp->f_flags & O_NONBLOCK) {
958 if (test_bit(SHOST_RECOVERY,
959 &sdp->device->host->shost_state))
961 } else if (!scsi_block_when_processing_errors(sdp->device))
963 result = get_user(val, ip);
966 if (SG_SCSI_RESET_NOTHING == val)
969 case SG_SCSI_RESET_DEVICE:
970 val = SCSI_TRY_RESET_DEVICE;
972 case SG_SCSI_RESET_BUS:
973 val = SCSI_TRY_RESET_BUS;
975 case SG_SCSI_RESET_HOST:
976 val = SCSI_TRY_RESET_HOST;
981 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
983 return (scsi_reset_provider(sdp->device, val) ==
985 case SCSI_IOCTL_SEND_COMMAND:
989 unsigned char opcode = WRITE_6;
990 Scsi_Ioctl_Command __user *siocp = p;
992 if (copy_from_user(&opcode, siocp->data, 1))
994 if (!sg_allow_access(opcode, sdp->device->type))
997 return scsi_ioctl_send_command(sdp->device, p);
999 result = get_user(val, ip);
1002 sdp->sgdebug = (char) val;
1004 case SCSI_IOCTL_GET_IDLUN:
1005 case SCSI_IOCTL_GET_BUS_NUMBER:
1006 case SCSI_IOCTL_PROBE_HOST:
1007 case SG_GET_TRANSFORM:
1010 return scsi_ioctl(sdp->device, cmd_in, p);
1013 return -EPERM; /* don't know so take safe approach */
1014 return scsi_ioctl(sdp->device, cmd_in, p);
1019 sg_poll(struct file *filp, poll_table * wait)
1021 unsigned int res = 0;
1026 unsigned long iflags;
1028 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1031 poll_wait(filp, &sfp->read_wait, wait);
1032 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1033 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1034 /* if any read waiting, flag it */
1035 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1036 res = POLLIN | POLLRDNORM;
1039 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1043 else if (!sfp->cmd_q) {
1045 res |= POLLOUT | POLLWRNORM;
1046 } else if (count < SG_MAX_QUEUE)
1047 res |= POLLOUT | POLLWRNORM;
1048 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1049 sdp->disk->disk_name, (int) res));
1054 sg_fasync(int fd, struct file *filp, int mode)
1060 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1062 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1063 sdp->disk->disk_name, mode));
1065 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1066 return (retval < 0) ? retval : 0;
1069 static inline unsigned char *
1070 sg_scatg2virt(const struct scatterlist *sclp)
1072 return (sclp && sclp->page) ?
1073 (unsigned char *) page_address(sclp->page) + sclp->offset : NULL;
1076 /* When startFinish==1 increments page counts for pages other than the
1077 first of scatter gather elements obtained from __get_free_pages().
1078 When startFinish==0 decrements ... */
1080 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1086 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1087 startFinish, rsv_schp->k_use_sg));
1088 /* N.B. correction _not_ applied to base page of each allocation */
1089 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1090 struct scatterlist *sclp = rsv_schp->buffer;
1092 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
1093 for (m = PAGE_SIZE; m < sclp->length; m += PAGE_SIZE) {
1094 page_ptr = sg_scatg2virt(sclp) + m;
1095 page = virt_to_page(page_ptr);
1099 if (page_count(page) > 0)
1104 } else { /* reserve buffer is just a single allocation */
1105 for (m = PAGE_SIZE; m < rsv_schp->bufflen; m += PAGE_SIZE) {
1106 page_ptr = (unsigned char *) rsv_schp->buffer + m;
1107 page = virt_to_page(page_ptr);
1111 if (page_count(page) > 0)
1118 static struct page *
1119 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1122 struct page *page = NOPAGE_SIGBUS;
1123 void *page_ptr = NULL;
1124 unsigned long offset;
1125 Sg_scatter_hold *rsv_schp;
1127 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1129 rsv_schp = &sfp->reserve;
1130 offset = addr - vma->vm_start;
1131 if (offset >= rsv_schp->bufflen)
1133 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1134 offset, rsv_schp->k_use_sg));
1135 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1137 unsigned long sa = vma->vm_start;
1139 struct scatterlist *sclp = rsv_schp->buffer;
1141 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1143 len = vma->vm_end - sa;
1144 len = (len < sclp->length) ? len : sclp->length;
1146 page_ptr = sg_scatg2virt(sclp) + offset;
1147 page = virt_to_page(page_ptr);
1148 get_page(page); /* increment page count */
1154 } else { /* reserve buffer is just a single allocation */
1155 page_ptr = (unsigned char *) rsv_schp->buffer + offset;
1156 page = virt_to_page(page_ptr);
1157 get_page(page); /* increment page count */
1160 *type = VM_FAULT_MINOR;
1164 static struct vm_operations_struct sg_mmap_vm_ops = {
1165 .nopage = sg_vma_nopage,
1169 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1172 unsigned long req_sz = vma->vm_end - vma->vm_start;
1173 Sg_scatter_hold *rsv_schp;
1175 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1177 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1178 (void *) vma->vm_start, (int) req_sz));
1180 return -EINVAL; /* want no offset */
1181 rsv_schp = &sfp->reserve;
1182 if (req_sz > rsv_schp->bufflen)
1183 return -ENOMEM; /* cannot map more than reserved buffer */
1185 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1187 unsigned long sa = vma->vm_start;
1189 struct scatterlist *sclp = rsv_schp->buffer;
1191 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1193 if (0 != sclp->offset)
1194 return -EFAULT; /* non page aligned memory ?? */
1195 len = vma->vm_end - sa;
1196 len = (len < sclp->length) ? len : sclp->length;
1199 } else { /* reserve buffer is just a single allocation */
1200 if ((unsigned long) rsv_schp->buffer & (PAGE_SIZE - 1))
1201 return -EFAULT; /* non page aligned memory ?? */
1203 if (0 == sfp->mmap_called) {
1204 sg_rb_correct4mmap(rsv_schp, 1); /* do only once per fd lifetime */
1205 sfp->mmap_called = 1;
1207 vma->vm_flags |= (VM_RESERVED | VM_IO);
1208 vma->vm_private_data = sfp;
1209 vma->vm_ops = &sg_mmap_vm_ops;
1213 /* This function is a "bottom half" handler that is called by the
1214 * mid level when a command is completed (or has failed). */
1216 sg_cmd_done(Scsi_Cmnd * SCpnt)
1218 Scsi_Request *SRpnt = NULL;
1219 Sg_device *sdp = NULL;
1221 Sg_request *srp = NULL;
1223 if (SCpnt && (SRpnt = SCpnt->sc_request))
1224 srp = (Sg_request *) SRpnt->upper_private_data;
1226 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1228 scsi_release_request(SRpnt);
1231 sfp = srp->parentfp;
1233 sdp = sfp->parentdp;
1234 if ((NULL == sdp) || sdp->detached) {
1235 printk(KERN_INFO "sg_cmd_done: device detached\n");
1236 scsi_release_request(SRpnt);
1240 /* First transfer ownership of data buffers to sg_device object. */
1241 srp->data.k_use_sg = SRpnt->sr_use_sg;
1242 srp->data.sglist_len = SRpnt->sr_sglist_len;
1243 srp->data.bufflen = SRpnt->sr_bufflen;
1244 srp->data.buffer = SRpnt->sr_buffer;
1245 /* now clear out request structure */
1246 SRpnt->sr_use_sg = 0;
1247 SRpnt->sr_sglist_len = 0;
1248 SRpnt->sr_bufflen = 0;
1249 SRpnt->sr_buffer = NULL;
1250 SRpnt->sr_underflow = 0;
1251 SRpnt->sr_request->rq_disk = NULL; /* "sg" _disowns_ request blk */
1253 srp->my_cmdp = NULL;
1255 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1256 sdp->disk->disk_name, srp->header.pack_id, (int) SRpnt->sr_result));
1257 srp->header.resid = SCpnt->resid;
1258 /* N.B. unit of duration changes here from jiffies to millisecs */
1259 srp->header.duration =
1260 sg_jif_to_ms(jiffies - (int) srp->header.duration);
1261 if (0 != SRpnt->sr_result) {
1262 memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
1263 sizeof (srp->sense_b));
1264 srp->header.status = 0xff & SRpnt->sr_result;
1265 srp->header.masked_status = status_byte(SRpnt->sr_result);
1266 srp->header.msg_status = msg_byte(SRpnt->sr_result);
1267 srp->header.host_status = host_byte(SRpnt->sr_result);
1268 srp->header.driver_status = driver_byte(SRpnt->sr_result);
1269 if ((sdp->sgdebug > 0) &&
1270 ((CHECK_CONDITION == srp->header.masked_status) ||
1271 (COMMAND_TERMINATED == srp->header.masked_status)))
1272 print_req_sense("sg_cmd_done", SRpnt);
1274 /* Following if statement is a patch supplied by Eric Youngdale */
1275 if (driver_byte(SRpnt->sr_result) != 0
1276 && (SRpnt->sr_sense_buffer[0] & 0x7f) == 0x70
1277 && (SRpnt->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION
1278 && sdp->device->removable) {
1279 /* Detected disc change. Set the bit - this may be used if */
1280 /* there are filesystems using this device. */
1281 sdp->device->changed = 1;
1284 /* Rely on write phase to clean out srp status values, so no "else" */
1286 scsi_release_request(SRpnt);
1288 if (sfp->closed) { /* whoops this fd already released, cleanup */
1289 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1290 sg_finish_rem_req(srp);
1292 if (NULL == sfp->headrp) {
1293 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1294 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1295 scsi_device_put(sdp->device);
1299 } else if (srp && srp->orphan) {
1300 if (sfp->keep_orphan)
1301 srp->sg_io_owned = 0;
1303 sg_finish_rem_req(srp);
1308 /* Now wake up any sg_read() that is waiting for this packet. */
1309 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1311 wake_up_interruptible(&sfp->read_wait);
1315 static struct file_operations sg_fops = {
1316 .owner = THIS_MODULE,
1323 .release = sg_release,
1324 .fasync = sg_fasync,
1327 static struct class_simple * sg_sysfs_class;
1329 static int sg_sysfs_valid = 0;
1331 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1334 unsigned long iflags;
1335 void *old_sg_dev_arr = NULL;
1338 sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
1340 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1344 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1345 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1347 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1348 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1350 tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1351 if (unlikely(!tmp_da))
1354 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1355 memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
1356 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1357 old_sg_dev_arr = sg_dev_arr;
1358 sg_dev_arr = tmp_da;
1359 sg_dev_max = tmp_dev_max;
1362 for (k = 0; k < sg_dev_max; k++)
1365 if (unlikely(k >= SG_MAX_DEVS))
1368 memset(sdp, 0, sizeof(*sdp));
1369 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1370 sprintf(disk->disk_name, "sg%d", k);
1371 disk->first_minor = k;
1373 sdp->device = scsidp;
1374 init_waitqueue_head(&sdp->o_excl_wait);
1375 sdp->sg_tablesize = scsidp->host ? scsidp->host->sg_tablesize : 0;
1378 sg_dev_arr[k] = sdp;
1379 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1385 kfree(old_sg_dev_arr);
1389 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1394 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1396 "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1397 "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
1398 scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
1404 sg_add(struct class_device *cl_dev)
1406 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1407 struct gendisk *disk;
1408 Sg_device *sdp = NULL;
1409 struct cdev * cdev = NULL;
1412 disk = alloc_disk(1);
1414 printk(KERN_WARNING "alloc_disk failed\n");
1417 disk->major = SCSI_GENERIC_MAJOR;
1420 cdev = cdev_alloc();
1422 printk(KERN_WARNING "cdev_alloc failed\n");
1425 cdev->owner = THIS_MODULE;
1426 cdev->ops = &sg_fops;
1428 error = sg_alloc(disk, scsidp);
1430 printk(KERN_WARNING "sg_alloc failed\n");
1434 sdp = sg_dev_arr[k];
1436 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1437 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1438 "%s/generic", scsidp->devfs_name);
1439 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1441 devfs_remove("%s/generic", scsidp->devfs_name);
1445 if (sg_sysfs_valid) {
1446 struct class_device * sg_class_member;
1448 sg_class_member = class_simple_device_add(sg_sysfs_class,
1449 MKDEV(SCSI_GENERIC_MAJOR, k),
1452 if (IS_ERR(sg_class_member))
1453 printk(KERN_WARNING "sg_add: "
1454 "class_simple_device_add failed\n");
1455 class_set_devdata(sg_class_member, sdp);
1456 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1457 &sg_class_member->kobj, "generic");
1459 printk(KERN_ERR "sg_add: unable to make symlink "
1460 "'generic' back to sg%d\n", k);
1462 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1465 "Attached scsi generic sg%d at scsi%d, channel"
1466 " %d, id %d, lun %d, type %d\n", k,
1467 scsidp->host->host_no, scsidp->channel, scsidp->id,
1468 scsidp->lun, scsidp->type);
1480 sg_remove(struct class_device *cl_dev)
1482 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1483 Sg_device *sdp = NULL;
1484 unsigned long iflags;
1491 if (NULL == sg_dev_arr)
1494 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1495 for (k = 0; k < sg_dev_max; k++) {
1496 sdp = sg_dev_arr[k];
1497 if ((NULL == sdp) || (sdp->device != scsidp))
1498 continue; /* dirty but lowers nesting */
1501 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1503 for (srp = sfp->headrp; srp; srp = tsrp) {
1505 if (sfp->closed || (0 == srp->done))
1506 sg_finish_rem_req(srp);
1509 scsi_device_put(sdp->device);
1510 __sg_remove_sfp(sdp, sfp);
1513 wake_up_interruptible(&sfp->read_wait);
1514 kill_fasync(&sfp->async_qp, SIGPOLL,
1518 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1519 if (NULL == sdp->headfp) {
1520 sg_dev_arr[k] = NULL;
1522 } else { /* nothing active, simple case */
1523 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1524 sg_dev_arr[k] = NULL;
1529 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1532 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1533 class_simple_device_remove(MKDEV(SCSI_GENERIC_MAJOR, k));
1534 cdev_del(sdp->cdev);
1536 devfs_remove("%s/generic", scsidp->devfs_name);
1537 put_disk(sdp->disk);
1539 if (NULL == sdp->headfp)
1540 kfree((char *) sdp);
1544 scsi_sleep(2); /* dirty detach so delay device destruction */
1547 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1548 * of sysfs parameters (which module_param doesn't yet support).
1549 * Sysfs parameters defined explicitly below.
1551 module_param_named(def_reserved_size, def_reserved_size, int, 0);
1552 module_param_named(allow_dio, sg_allow_dio, int, 0);
1554 MODULE_AUTHOR("Douglas Gilbert");
1555 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1556 MODULE_LICENSE("GPL");
1557 MODULE_VERSION(SG_VERSION_STR);
1559 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1560 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1567 if (def_reserved_size >= 0)
1568 sg_big_buff = def_reserved_size;
1570 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1574 sg_sysfs_class = class_simple_create(THIS_MODULE, "scsi_generic");
1575 if ( IS_ERR(sg_sysfs_class) ) {
1576 rc = PTR_ERR(sg_sysfs_class);
1580 rc = scsi_register_interface(&sg_interface);
1582 #ifdef CONFIG_SCSI_PROC_FS
1584 #endif /* CONFIG_SCSI_PROC_FS */
1587 class_simple_destroy(sg_sysfs_class);
1589 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1596 #ifdef CONFIG_SCSI_PROC_FS
1598 #endif /* CONFIG_SCSI_PROC_FS */
1599 scsi_unregister_interface(&sg_interface);
1600 class_simple_destroy(sg_sysfs_class);
1602 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1604 if (sg_dev_arr != NULL) {
1605 kfree((char *) sg_dev_arr);
1612 sg_start_req(Sg_request * srp)
1615 Sg_fd *sfp = srp->parentfp;
1616 sg_io_hdr_t *hp = &srp->header;
1617 int dxfer_len = (int) hp->dxfer_len;
1618 int dxfer_dir = hp->dxfer_direction;
1619 Sg_scatter_hold *req_schp = &srp->data;
1620 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1622 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1623 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1625 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1626 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1627 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1628 res = sg_build_direct(srp, sfp, dxfer_len);
1629 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1632 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1633 sg_link_reserve(sfp, srp, dxfer_len);
1635 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1637 sg_remove_scat(req_schp);
1645 sg_finish_rem_req(Sg_request * srp)
1647 Sg_fd *sfp = srp->parentfp;
1648 Sg_scatter_hold *req_schp = &srp->data;
1650 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1652 sg_unlink_reserve(sfp, srp);
1654 sg_remove_scat(req_schp);
1655 sg_remove_request(sfp, srp);
1659 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1662 int elem_sz = sizeof (struct scatterlist);
1663 int sg_bufflen = tablesize * elem_sz;
1664 int mx_sc_elems = tablesize;
1666 schp->buffer = sg_page_malloc(sg_bufflen, sfp->low_dma, &ret_sz);
1669 else if (ret_sz != sg_bufflen) {
1670 sg_bufflen = ret_sz;
1671 mx_sc_elems = sg_bufflen / elem_sz;
1673 schp->sglist_len = sg_bufflen;
1674 memset(schp->buffer, 0, sg_bufflen);
1675 return mx_sc_elems; /* number of scat_gath elements allocated */
1678 #ifdef SG_ALLOW_DIO_CODE
1679 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1680 /* hopefully this generic code will moved to a library */
1682 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1683 - mapping of all pages not successful
1684 - any page is above max_pfn
1685 (i.e., either completely successful or fails)
1688 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1689 unsigned long uaddr, size_t count, int rw,
1690 unsigned long max_pfn)
1693 unsigned int nr_pages;
1694 struct page **pages;
1696 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
1698 /* User attempted Overflow! */
1699 if ((uaddr + count) < uaddr)
1703 if (nr_pages > max_pages)
1710 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1713 /* Try to fault in all of the necessary pages */
1714 down_read(¤t->mm->mmap_sem);
1715 /* rw==READ means read from drive, write into memory area */
1716 res = get_user_pages(
1722 0, /* don't force */
1725 up_read(¤t->mm->mmap_sem);
1727 /* Errors and no page mapped should return here */
1731 for (i=0; i < nr_pages; i++) {
1732 /* FIXME: flush superflous for rw==READ,
1733 * probably wrong function for rw==WRITE
1735 flush_dcache_page(pages[i]);
1736 if (page_to_pfn(pages[i]) > max_pfn)
1738 /* ?? Is locking needed? I don't think so */
1739 /* if (TestSetPageLocked(pages[i]))
1743 /* Populate the scatter/gather list */
1744 sgl[0].page = pages[0];
1745 sgl[0].offset = uaddr & ~PAGE_MASK;
1747 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1748 count -= sgl[0].length;
1749 for (i=1; i < nr_pages ; i++) {
1751 sgl[i].page = pages[i];
1752 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1757 sgl[0].length = count;
1764 /* for (j=0; j < i; j++)
1765 unlock_page(pages[j]); */
1769 for (j=0; j < res; j++)
1770 page_cache_release(pages[j]);
1776 /* And unmap them... */
1778 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1783 for (i=0; i < nr_pages; i++) {
1784 if (dirtied && !PageReserved(sgl[i].page))
1785 SetPageDirty(sgl[i].page);
1786 /* unlock_page(sgl[i].page); */
1787 /* FIXME: cache flush missing for rw==READ
1788 * FIXME: call the correct reference counting function
1790 page_cache_release(sgl[i].page);
1796 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1800 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1802 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1804 #ifdef SG_ALLOW_DIO_CODE
1805 sg_io_hdr_t *hp = &srp->header;
1806 Sg_scatter_hold *schp = &srp->data;
1807 int sg_tablesize = sfp->parentdp->sg_tablesize;
1808 struct scatterlist *sgl;
1809 int mx_sc_elems, res;
1810 struct scsi_device *sdev = sfp->parentdp->device;
1812 if (((unsigned long)hp->dxferp &
1813 queue_dma_alignment(sdev->request_queue)) != 0)
1815 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1816 if (mx_sc_elems <= 0) {
1819 sgl = (struct scatterlist *)schp->buffer;
1820 res = st_map_user_pages(sgl, mx_sc_elems, (unsigned long)hp->dxferp, dxfer_len,
1821 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0, ULONG_MAX);
1824 schp->k_use_sg = res;
1825 schp->dio_in_use = 1;
1826 hp->info |= SG_INFO_DIRECT_IO;
1834 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1837 int blk_size = buff_size;
1838 unsigned char *p = NULL;
1840 if ((blk_size < 0) || (!sfp))
1843 ++blk_size; /* don't know why */
1844 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1845 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1846 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1847 buff_size, blk_size));
1848 if (blk_size <= SG_SCATTER_SZ) {
1849 p = sg_page_malloc(blk_size, sfp->low_dma, &ret_sz);
1852 if (blk_size == ret_sz) { /* got it on the first attempt */
1855 schp->bufflen = blk_size;
1856 schp->b_malloc_len = blk_size;
1860 p = sg_page_malloc(SG_SCATTER_SZ, sfp->low_dma, &ret_sz);
1864 /* Want some local declarations, so start new block ... */
1865 { /* lets try and build a scatter gather list */
1866 struct scatterlist *sclp;
1869 int sg_tablesize = sfp->parentdp->sg_tablesize;
1872 /* N.B. ret_sz carried into this block ... */
1873 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1874 if (mx_sc_elems < 0)
1875 return mx_sc_elems; /* most likely -ENOMEM */
1877 for (k = 0, sclp = schp->buffer, rem_sz = blk_size;
1878 (rem_sz > 0) && (k < mx_sc_elems);
1879 ++k, rem_sz -= ret_sz, ++sclp) {
1885 SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1886 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1890 sclp->page = virt_to_page(p);
1891 sclp->offset = offset_in_page(p);
1892 sclp->length = ret_sz;
1894 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1895 k, sg_scatg2virt(sclp), ret_sz));
1896 } /* end of for loop */
1898 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1899 schp->bufflen = blk_size;
1900 if (rem_sz > 0) /* must have failed */
1907 sg_write_xfer(Sg_request * srp)
1909 sg_io_hdr_t *hp = &srp->header;
1910 Sg_scatter_hold *schp = &srp->data;
1912 int j, k, onum, usglen, ksglen, res;
1913 int iovec_count = (int) hp->iovec_count;
1914 int dxfer_dir = hp->dxfer_direction;
1916 unsigned char __user *up;
1917 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1919 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1920 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1921 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1922 if (schp->bufflen < num_xfer)
1923 num_xfer = schp->bufflen;
1925 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1927 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1930 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1931 num_xfer, iovec_count, schp->k_use_sg));
1934 if ((k = verify_area(VERIFY_READ, hp->dxferp,
1935 SZ_SG_IOVEC * onum)))
1940 if (0 == schp->k_use_sg) { /* kernel has single buffer */
1941 for (j = 0, p = schp->buffer; j < onum; ++j) {
1942 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1945 usglen = (num_xfer > usglen) ? usglen : num_xfer;
1946 if (__copy_from_user(p, up, usglen))
1953 } else { /* kernel using scatter gather list */
1954 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
1956 ksglen = (int) sclp->length;
1957 p = sg_scatg2virt(sclp);
1958 for (j = 0, k = 0; j < onum; ++j) {
1959 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1963 for (; p; ++sclp, ksglen = (int) sclp->length,
1964 p = sg_scatg2virt(sclp)) {
1967 if (ksglen > usglen) {
1968 if (usglen >= num_xfer) {
1969 if (__copy_from_user
1974 if (__copy_from_user(p, up, usglen))
1980 if (ksglen >= num_xfer) {
1981 if (__copy_from_user
1986 if (__copy_from_user(p, up, ksglen))
1992 if (k >= schp->k_use_sg)
2001 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
2002 int wr_xf, int *countp, unsigned char __user **up)
2004 int num_xfer = (int) hp->dxfer_len;
2005 unsigned char __user *p = hp->dxferp;
2009 if (wr_xf && ('\0' == hp->interface_id))
2010 count = (int) hp->flags; /* holds "old" input_size */
2015 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2018 count = (int) iovec.iov_len;
2020 if ((k = verify_area(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count)))
2030 sg_remove_scat(Sg_scatter_hold * schp)
2032 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2033 if (schp->buffer && (schp->sglist_len > 0)) {
2034 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2036 if (schp->dio_in_use) {
2037 #ifdef SG_ALLOW_DIO_CODE
2038 st_unmap_user_pages(sclp, schp->k_use_sg, TRUE);
2043 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2045 SCSI_LOG_TIMEOUT(5, printk(
2046 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2047 k, sg_scatg2virt(sclp), sclp->length));
2048 sg_page_free(sg_scatg2virt(sclp), sclp->length);
2054 sg_page_free(schp->buffer, schp->sglist_len);
2055 } else if (schp->buffer)
2056 sg_page_free(schp->buffer, schp->b_malloc_len);
2057 memset(schp, 0, sizeof (*schp));
2061 sg_read_xfer(Sg_request * srp)
2063 sg_io_hdr_t *hp = &srp->header;
2064 Sg_scatter_hold *schp = &srp->data;
2066 int j, k, onum, usglen, ksglen, res;
2067 int iovec_count = (int) hp->iovec_count;
2068 int dxfer_dir = hp->dxfer_direction;
2070 unsigned char __user *up;
2071 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2073 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2074 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2075 num_xfer = hp->dxfer_len;
2076 if (schp->bufflen < num_xfer)
2077 num_xfer = schp->bufflen;
2079 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2081 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2084 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2085 num_xfer, iovec_count, schp->k_use_sg));
2088 if ((k = verify_area(VERIFY_READ, hp->dxferp,
2089 SZ_SG_IOVEC * onum)))
2094 if (0 == schp->k_use_sg) { /* kernel has single buffer */
2095 for (j = 0, p = schp->buffer; j < onum; ++j) {
2096 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2099 usglen = (num_xfer > usglen) ? usglen : num_xfer;
2100 if (__copy_to_user(up, p, usglen))
2107 } else { /* kernel using scatter gather list */
2108 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2110 ksglen = (int) sclp->length;
2111 p = sg_scatg2virt(sclp);
2112 for (j = 0, k = 0; j < onum; ++j) {
2113 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2117 for (; p; ++sclp, ksglen = (int) sclp->length,
2118 p = sg_scatg2virt(sclp)) {
2121 if (ksglen > usglen) {
2122 if (usglen >= num_xfer) {
2128 if (__copy_to_user(up, p, usglen))
2134 if (ksglen >= num_xfer) {
2140 if (__copy_to_user(up, p, ksglen))
2146 if (k >= schp->k_use_sg)
2155 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2157 Sg_scatter_hold *schp = &srp->data;
2159 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2161 if ((!outp) || (num_read_xfer <= 0))
2163 if (schp->k_use_sg > 0) {
2165 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2167 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2169 num = (int) sclp->length;
2170 if (num > num_read_xfer) {
2172 (outp, sg_scatg2virt(sclp), num_read_xfer))
2177 (outp, sg_scatg2virt(sclp), num))
2179 num_read_xfer -= num;
2180 if (num_read_xfer <= 0)
2186 if (__copy_to_user(outp, schp->buffer, num_read_xfer))
2193 sg_build_reserve(Sg_fd * sfp, int req_size)
2195 Sg_scatter_hold *schp = &sfp->reserve;
2197 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2199 if (req_size < PAGE_SIZE)
2200 req_size = PAGE_SIZE;
2201 if (0 == sg_build_indirect(schp, sfp, req_size))
2204 sg_remove_scat(schp);
2205 req_size >>= 1; /* divide by 2 */
2206 } while (req_size > (PAGE_SIZE / 2));
2210 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2212 Sg_scatter_hold *req_schp = &srp->data;
2213 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2216 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2217 size = (size + 1) & (~1); /* round to even for aha1542 */
2218 if (rsv_schp->k_use_sg > 0) {
2221 struct scatterlist *sclp =
2222 (struct scatterlist *) rsv_schp->buffer;
2224 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
2225 num = (int) sclp->length;
2228 req_schp->k_use_sg = 0;
2229 req_schp->buffer = sg_scatg2virt(sclp);
2231 sfp->save_scat_len = num;
2232 sclp->length = (unsigned) rem;
2233 req_schp->k_use_sg = k + 1;
2234 req_schp->sglist_len =
2235 rsv_schp->sglist_len;
2236 req_schp->buffer = rsv_schp->buffer;
2238 req_schp->bufflen = size;
2239 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2244 if (k >= rsv_schp->k_use_sg)
2245 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2247 req_schp->k_use_sg = 0;
2248 req_schp->bufflen = size;
2249 req_schp->buffer = rsv_schp->buffer;
2250 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2255 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2257 Sg_scatter_hold *req_schp = &srp->data;
2258 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2260 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2261 (int) req_schp->k_use_sg));
2262 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2263 struct scatterlist *sclp =
2264 (struct scatterlist *) rsv_schp->buffer;
2266 if (sfp->save_scat_len > 0)
2267 (sclp + (req_schp->k_use_sg - 1))->length =
2268 (unsigned) sfp->save_scat_len;
2270 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2272 req_schp->k_use_sg = 0;
2273 req_schp->bufflen = 0;
2274 req_schp->buffer = NULL;
2275 req_schp->sglist_len = 0;
2276 sfp->save_scat_len = 0;
2281 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2284 unsigned long iflags;
2286 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2287 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2288 /* look for requests that are ready + not SG_IO owned */
2289 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2290 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2291 resp->done = 2; /* guard against other readers */
2295 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2299 #ifdef CONFIG_SCSI_PROC_FS
2301 sg_get_nth_request(Sg_fd * sfp, int nth)
2304 unsigned long iflags;
2307 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2308 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2309 ++k, resp = resp->nextrp) ;
2310 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2315 /* always adds to end of list */
2317 sg_add_request(Sg_fd * sfp)
2320 unsigned long iflags;
2322 Sg_request *rp = sfp->req_arr;
2324 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2327 memset(rp, 0, sizeof (Sg_request));
2332 if (0 == sfp->cmd_q)
2333 resp = NULL; /* command queuing disallowed */
2335 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2339 if (k < SG_MAX_QUEUE) {
2340 memset(rp, 0, sizeof (Sg_request));
2342 while (resp->nextrp)
2343 resp = resp->nextrp;
2351 resp->nextrp = NULL;
2352 resp->header.duration = jiffies;
2353 resp->my_cmdp = NULL;
2355 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2359 /* Return of 1 for found; 0 for not found */
2361 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2363 Sg_request *prev_rp;
2365 unsigned long iflags;
2368 if ((!sfp) || (!srp) || (!sfp->headrp))
2370 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2371 prev_rp = sfp->headrp;
2372 if (srp == prev_rp) {
2373 sfp->headrp = prev_rp->nextrp;
2374 prev_rp->parentfp = NULL;
2377 while ((rp = prev_rp->nextrp)) {
2379 prev_rp->nextrp = rp->nextrp;
2380 rp->parentfp = NULL;
2387 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2391 #ifdef CONFIG_SCSI_PROC_FS
2393 sg_get_nth_sfp(Sg_device * sdp, int nth)
2396 unsigned long iflags;
2399 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2400 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2401 ++k, resp = resp->nextfp) ;
2402 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2408 sg_add_sfp(Sg_device * sdp, int dev)
2411 unsigned long iflags;
2413 sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, 0);
2416 memset(sfp, 0, sizeof (Sg_fd));
2417 init_waitqueue_head(&sfp->read_wait);
2418 sfp->rq_list_lock = RW_LOCK_UNLOCKED;
2420 sfp->timeout = SG_DEFAULT_TIMEOUT;
2421 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2422 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2423 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2424 sdp->device->host->unchecked_isa_dma : 1;
2425 sfp->cmd_q = SG_DEF_COMMAND_Q;
2426 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2427 sfp->parentdp = sdp;
2428 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2431 else { /* add to tail of existing list */
2432 Sg_fd *pfp = sdp->headfp;
2437 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2438 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2439 sg_build_reserve(sfp, sg_big_buff);
2440 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2441 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2446 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2451 prev_fp = sdp->headfp;
2453 sdp->headfp = prev_fp->nextfp;
2455 while ((fp = prev_fp->nextfp)) {
2457 prev_fp->nextfp = fp->nextfp;
2463 if (sfp->reserve.bufflen > 0) {
2465 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2466 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2467 if (sfp->mmap_called)
2468 sg_rb_correct4mmap(&sfp->reserve, 0); /* undo correction */
2469 sg_remove_scat(&sfp->reserve);
2471 sfp->parentdp = NULL;
2472 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2473 sg_page_free((char *) sfp, sizeof (Sg_fd));
2476 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2478 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2485 for (srp = sfp->headrp; srp; srp = tsrp) {
2488 sg_finish_rem_req(srp);
2493 unsigned long iflags;
2495 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2496 __sg_remove_sfp(sdp, sfp);
2497 if (sdp->detached && (NULL == sdp->headfp)) {
2501 for (k = 0; k < maxd; ++k) {
2502 if (sdp == sg_dev_arr[k])
2506 sg_dev_arr[k] = NULL;
2507 kfree((char *) sdp);
2510 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2512 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2513 /* only bump the access_count if we actually succeeded in
2514 * throwing another counter on the host module */
2515 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2516 sfp->closed = 1; /* flag dirty state on this fd */
2517 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2524 sg_res_in_use(Sg_fd * sfp)
2526 const Sg_request *srp;
2527 unsigned long iflags;
2529 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2530 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2533 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2537 /* If retSzp==NULL want exact size or fail */
2539 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2550 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2552 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2554 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2555 order++, a_size <<= 1) ;
2556 resp = (char *) __get_free_pages(page_mask, order);
2557 while ((!resp) && order && retSzp) {
2559 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2560 resp = (char *) __get_free_pages(page_mask, order); /* try half */
2564 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2565 memset(resp, 0, resSz);
2573 sg_page_free(char *buff, int size)
2579 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2580 order++, a_size <<= 1) ;
2581 free_pages((unsigned long) buff, order);
2585 sg_ms_to_jif(unsigned int msecs)
2587 if ((UINT_MAX / 2U) < msecs)
2588 return INT_MAX; /* special case, set largest possible */
2590 return ((int) msecs <
2591 (INT_MAX / 1000)) ? (((int) msecs * HZ) / 1000)
2592 : (((int) msecs / 1000) * HZ);
2595 static inline unsigned
2596 sg_jif_to_ms(int jifs)
2601 unsigned int j = (unsigned int) jifs;
2603 (UINT_MAX / 1000)) ? ((j * 1000) / HZ) : ((j / HZ) *
2608 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2609 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2610 MODE_SENSE, MODE_SENSE_10, LOG_SENSE
2614 sg_allow_access(unsigned char opcode, char dev_type)
2618 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2620 for (k = 0; k < sizeof (allow_ops); ++k) {
2621 if (opcode == allow_ops[k])
2627 #ifdef CONFIG_SCSI_PROC_FS
2632 unsigned long iflags;
2634 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2635 for (k = sg_dev_max - 1; k >= 0; --k)
2636 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2638 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2639 return k + 1; /* origin 1 */
2646 Sg_device *sdp = NULL;
2647 unsigned long iflags;
2649 if (sg_dev_arr && (dev >= 0)) {
2650 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2651 if (dev < sg_dev_max)
2652 sdp = sg_dev_arr[dev];
2653 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2658 #ifdef CONFIG_SCSI_PROC_FS
2660 static struct proc_dir_entry *sg_proc_sgp = NULL;
2662 static char sg_proc_sg_dirname[] = "scsi/sg";
2664 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2666 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2667 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2668 size_t count, loff_t *off);
2669 static struct file_operations adio_fops = {
2670 /* .owner, .read and .llseek added in sg_proc_init() */
2671 .open = sg_proc_single_open_adio,
2672 .write = sg_proc_write_adio,
2673 .release = single_release,
2676 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2677 static ssize_t sg_proc_write_dressz(struct file *filp,
2678 const char __user *buffer, size_t count, loff_t *off);
2679 static struct file_operations dressz_fops = {
2680 .open = sg_proc_single_open_dressz,
2681 .write = sg_proc_write_dressz,
2682 .release = single_release,
2685 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2686 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2687 static struct file_operations version_fops = {
2688 .open = sg_proc_single_open_version,
2689 .release = single_release,
2692 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2693 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2694 static struct file_operations devhdr_fops = {
2695 .open = sg_proc_single_open_devhdr,
2696 .release = single_release,
2699 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2700 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2701 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2702 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2703 static void dev_seq_stop(struct seq_file *s, void *v);
2704 static struct file_operations dev_fops = {
2705 .open = sg_proc_open_dev,
2706 .release = seq_release,
2708 static struct seq_operations dev_seq_ops = {
2709 .start = dev_seq_start,
2710 .next = dev_seq_next,
2711 .stop = dev_seq_stop,
2712 .show = sg_proc_seq_show_dev,
2715 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2716 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2717 static struct file_operations devstrs_fops = {
2718 .open = sg_proc_open_devstrs,
2719 .release = seq_release,
2721 static struct seq_operations devstrs_seq_ops = {
2722 .start = dev_seq_start,
2723 .next = dev_seq_next,
2724 .stop = dev_seq_stop,
2725 .show = sg_proc_seq_show_devstrs,
2728 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2729 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2730 static struct file_operations debug_fops = {
2731 .open = sg_proc_open_debug,
2732 .release = seq_release,
2734 static struct seq_operations debug_seq_ops = {
2735 .start = dev_seq_start,
2736 .next = dev_seq_next,
2737 .stop = dev_seq_stop,
2738 .show = sg_proc_seq_show_debug,
2742 struct sg_proc_leaf {
2744 struct file_operations * fops;
2747 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2748 {"allow_dio", &adio_fops},
2749 {"debug", &debug_fops},
2750 {"def_reserved_size", &dressz_fops},
2751 {"device_hdr", &devhdr_fops},
2752 {"devices", &dev_fops},
2753 {"device_strs", &devstrs_fops},
2754 {"version", &version_fops}
2762 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2763 struct proc_dir_entry *pdep;
2764 struct sg_proc_leaf * leaf;
2766 sg_proc_sgp = create_proc_entry(sg_proc_sg_dirname,
2767 S_IFDIR | S_IRUGO | S_IXUGO, NULL);
2770 for (k = 0; k < num_leaves; ++k) {
2771 leaf = &sg_proc_leaf_arr[k];
2772 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2773 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2775 leaf->fops->owner = THIS_MODULE,
2776 leaf->fops->read = seq_read,
2777 leaf->fops->llseek = seq_lseek,
2778 pdep->proc_fops = leaf->fops;
2785 sg_proc_cleanup(void)
2789 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2793 for (k = 0; k < num_leaves; ++k)
2794 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2795 remove_proc_entry(sg_proc_sg_dirname, NULL);
2799 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2801 seq_printf(s, "%d\n", *((int *)s->private));
2805 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2807 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2811 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2812 size_t count, loff_t *off)
2817 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2819 num = (count < 10) ? count : 10;
2820 if (copy_from_user(buff, buffer, num))
2823 sg_allow_dio = simple_strtoul(buff, 0, 10) ? 1 : 0;
2827 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2829 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2833 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2834 size_t count, loff_t *off)
2837 unsigned long k = ULONG_MAX;
2840 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2842 num = (count < 10) ? count : 10;
2843 if (copy_from_user(buff, buffer, num))
2846 k = simple_strtoul(buff, 0, 10);
2847 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2854 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2856 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2861 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2863 return single_open(file, sg_proc_seq_show_version, NULL);
2866 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2868 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2873 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2875 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2878 struct sg_proc_deviter {
2883 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2885 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2889 if (NULL == sg_dev_arr)
2892 it->max = sg_last_dev();
2893 if (it->index >= it->max)
2901 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2903 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2906 return (it->index < it->max) ? it : NULL;
2909 static void dev_seq_stop(struct seq_file *s, void *v)
2914 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2916 return seq_open(file, &dev_seq_ops);
2919 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2921 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2923 struct scsi_device *scsidp;
2925 sdp = it ? sg_get_dev(it->index) : NULL;
2926 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2927 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2928 scsidp->host->host_no, scsidp->channel,
2929 scsidp->id, scsidp->lun, (int) scsidp->type,
2931 (int) scsidp->queue_depth,
2932 (int) scsidp->device_busy,
2933 (int) scsi_device_online(scsidp));
2935 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2939 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2941 return seq_open(file, &devstrs_seq_ops);
2944 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2946 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2948 struct scsi_device *scsidp;
2950 sdp = it ? sg_get_dev(it->index) : NULL;
2951 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2952 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2953 scsidp->vendor, scsidp->model, scsidp->rev);
2955 seq_printf(s, "<no active device>\n");
2959 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2961 int k, m, new_interface, blen, usg;
2964 const sg_io_hdr_t *hp;
2967 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2968 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2969 "(res)sgat=%d low_dma=%d\n", k + 1,
2970 sg_jif_to_ms(fp->timeout),
2971 fp->reserve.bufflen,
2972 (int) fp->reserve.k_use_sg,
2974 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2975 (int) fp->cmd_q, (int) fp->force_packid,
2976 (int) fp->keep_orphan, (int) fp->closed);
2977 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2979 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2980 if (srp->res_used) {
2981 if (new_interface &&
2982 (SG_FLAG_MMAP_IO & hp->flags))
2987 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2993 blen = srp->my_cmdp ?
2994 srp->my_cmdp->sr_bufflen : srp->data.bufflen;
2995 usg = srp->my_cmdp ?
2996 srp->my_cmdp->sr_use_sg : srp->data.k_use_sg;
2997 seq_printf(s, srp->done ?
2998 ((1 == srp->done) ? "rcv:" : "fin:")
2999 : (srp->my_cmdp ? "act:" : "prior:"));
3000 seq_printf(s, " id=%d blen=%d",
3001 srp->header.pack_id, blen);
3003 seq_printf(s, " dur=%d", hp->duration);
3005 seq_printf(s, " t_o/elap=%d/%d",
3006 new_interface ? hp->timeout : sg_jif_to_ms(fp->timeout),
3007 sg_jif_to_ms(hp->duration ? (jiffies - hp->duration) : 0));
3008 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
3009 (int) srp->data.cmd_opcode);
3012 seq_printf(s, " No requests active\n");
3016 static int sg_proc_open_debug(struct inode *inode, struct file *file)
3018 return seq_open(file, &debug_seq_ops);
3021 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
3023 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3026 if (it && (0 == it->index)) {
3027 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
3028 "(origin 1)\n", sg_dev_max, (int)it->max);
3029 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
3031 sdp = it ? sg_get_dev(it->index) : NULL;
3033 struct scsi_device *scsidp = sdp->device;
3035 if (NULL == scsidp) {
3036 seq_printf(s, "device %d detached ??\n",
3041 if (sg_get_nth_sfp(sdp, 0)) {
3042 seq_printf(s, " >>> device=%s ",
3043 sdp->disk->disk_name);
3045 seq_printf(s, "detached pending close ");
3048 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
3049 scsidp->host->host_no,
3050 scsidp->channel, scsidp->id,
3052 scsidp->host->hostt->emulated);
3053 seq_printf(s, " sg_tablesize=%d excl=%d\n",
3054 sdp->sg_tablesize, sdp->exclude);
3056 sg_proc_debug_helper(s, sdp);
3061 #endif /* CONFIG_SCSI_PROC_FS */
3063 module_init(init_sg);
3064 module_exit(exit_sg);
3065 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);