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>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_driver.h>
56 #include <scsi/scsi_ioctl.h>
59 #include "scsi_logging.h"
61 #ifdef CONFIG_SCSI_PROC_FS
62 #include <linux/proc_fs.h>
63 static char *sg_version_date = "20040516";
65 static int sg_proc_init(void);
66 static void sg_proc_cleanup(void);
69 #ifndef LINUX_VERSION_CODE
70 #include <linux/version.h>
71 #endif /* LINUX_VERSION_CODE */
73 #define SG_ALLOW_DIO_DEF 0
74 #define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
76 #define SG_MAX_DEVS 32768
79 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
80 * Then when using 32 bit integers x * m may overflow during the calculation.
81 * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
82 * calculates the same, but prevents the overflow when both m and d
83 * are "small" numbers (like HZ and USER_HZ).
84 * Of course an overflow is inavoidable if the result of muldiv doesn't fit
87 #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
89 #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
91 int sg_big_buff = SG_DEF_RESERVED_SIZE;
92 /* N.B. This variable is readable and writeable via
93 /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
94 of this size (or less if there is not enough memory) will be reserved
95 for use by this file descriptor. [Deprecated usage: this variable is also
96 readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
97 the kernel (i.e. it is not a module).] */
98 static int def_reserved_size = -1; /* picks up init parameter */
99 static int sg_allow_dio = SG_ALLOW_DIO_DEF;
101 #define SG_SECTOR_SZ 512
102 #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
104 #define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
106 static int sg_add(struct class_device *);
107 static void sg_remove(struct class_device *);
109 static Scsi_Request *dummy_cmdp; /* only used for sizeof */
111 static rwlock_t sg_dev_arr_lock = RW_LOCK_UNLOCKED; /* Also used to lock
112 file descriptor list for device */
114 static struct class_interface sg_interface = {
119 typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
120 unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
121 unsigned short sglist_len; /* size of malloc'd scatter-gather list ++ */
122 unsigned bufflen; /* Size of (aggregate) data buffer */
123 unsigned b_malloc_len; /* actual len malloc'ed in buffer */
124 void *buffer; /* Data buffer or scatter list (k_use_sg>0) */
125 char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
126 unsigned char cmd_opcode; /* first byte of command */
129 struct sg_device; /* forward declarations */
132 typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
133 Scsi_Request *my_cmdp; /* != 0 when request with lower levels */
134 struct sg_request *nextrp; /* NULL -> tail request (slist) */
135 struct sg_fd *parentfp; /* NULL -> not in use */
136 Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
137 sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
138 unsigned char sense_b[sizeof (dummy_cmdp->sr_sense_buffer)];
139 char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
140 char orphan; /* 1 -> drop on sight, 0 -> normal */
141 char sg_io_owned; /* 1 -> packet belongs to SG_IO */
142 volatile char done; /* 0->before bh, 1->before read, 2->read */
145 typedef struct sg_fd { /* holds the state of a file descriptor */
146 struct sg_fd *nextfp; /* NULL when last opened fd on this device */
147 struct sg_device *parentdp; /* owning device */
148 wait_queue_head_t read_wait; /* queue read until command done */
149 rwlock_t rq_list_lock; /* protect access to list in req_arr */
150 int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
151 int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
152 Sg_scatter_hold reserve; /* buffer held for this file descriptor */
153 unsigned save_scat_len; /* original length of trunc. scat. element */
154 Sg_request *headrp; /* head of request slist, NULL->empty */
155 struct fasync_struct *async_qp; /* used by asynchronous notification */
156 Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
157 char low_dma; /* as in parent but possibly overridden to 1 */
158 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
159 volatile char closed; /* 1 -> fd closed but request(s) outstanding */
160 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
161 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
162 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
163 char mmap_called; /* 0 -> mmap() never called on this fd */
166 typedef struct sg_device { /* holds the state of each scsi generic device */
167 struct scsi_device *device;
168 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
169 int sg_tablesize; /* adapter's max scatter-gather table size */
170 Sg_fd *headfp; /* first open fd belonging to this device */
171 volatile char detached; /* 0->attached, 1->detached pending removal */
172 volatile char exclude; /* opened for exclusive access */
173 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
174 struct gendisk *disk;
175 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
178 static int sg_fasync(int fd, struct file *filp, int mode);
179 static void sg_cmd_done(Scsi_Cmnd * SCpnt); /* tasklet or soft irq callback */
180 static int sg_start_req(Sg_request * srp);
181 static void sg_finish_rem_req(Sg_request * srp);
182 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
183 static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
185 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
187 static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
188 int blocking, int read_only, Sg_request ** o_srp);
189 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking);
191 static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
192 int wr_xf, int *countp, unsigned char __user **up);
193 static int sg_write_xfer(Sg_request * srp);
194 static int sg_read_xfer(Sg_request * srp);
195 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
196 static void sg_remove_scat(Sg_scatter_hold * schp);
197 static void sg_build_reserve(Sg_fd * sfp, int req_size);
198 static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
199 static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
200 static char *sg_page_malloc(int rqSz, int lowDma, int *retSzp);
201 static void sg_page_free(char *buff, int size);
202 static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
203 static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
204 static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
205 static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
206 static Sg_request *sg_add_request(Sg_fd * sfp);
207 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
208 static int sg_res_in_use(Sg_fd * sfp);
209 static int sg_allow_access(unsigned char opcode, char dev_type);
210 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
211 static Sg_device *sg_get_dev(int dev);
212 static inline unsigned char *sg_scatg2virt(const struct scatterlist *sclp);
213 #ifdef CONFIG_SCSI_PROC_FS
214 static int sg_last_dev(void);
217 static Sg_device **sg_dev_arr = NULL;
218 static int sg_dev_max;
219 static int sg_nr_dev;
221 #define SZ_SG_HEADER sizeof(struct sg_header)
222 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
223 #define SZ_SG_IOVEC sizeof(sg_iovec_t)
224 #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
227 sg_open(struct inode *inode, struct file *filp)
229 int dev = iminor(inode);
230 int flags = filp->f_flags;
236 nonseekable_open(inode, filp);
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 /* This driver's module count bumped by fops_get in <linux/fs.h> */
245 /* Prevent the device driver from vanishing while we sleep */
246 retval = scsi_device_get(sdp->device);
250 if (!((flags & O_NONBLOCK) ||
251 scsi_block_when_processing_errors(sdp->device))) {
253 /* we are in error recovery for this device */
257 if (flags & O_EXCL) {
258 if (O_RDONLY == (flags & O_ACCMODE)) {
259 retval = -EPERM; /* Can't lock it with read only access */
262 if (sdp->headfp && (flags & O_NONBLOCK)) {
267 __wait_event_interruptible(sdp->o_excl_wait,
268 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
270 retval = res; /* -ERESTARTSYS because signal hit process */
273 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
274 if (flags & O_NONBLOCK) {
279 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
282 retval = res; /* -ERESTARTSYS because signal hit process */
290 if (!sdp->headfp) { /* no existing opens on this device */
292 sdp->sg_tablesize = sdp->device->host->sg_tablesize;
294 if ((sfp = sg_add_sfp(sdp, dev)))
295 filp->private_data = sfp;
298 sdp->exclude = 0; /* undo if error */
305 scsi_device_put(sdp->device);
309 /* Following function was formerly called 'sg_close' */
311 sg_release(struct inode *inode, struct file *filp)
316 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
318 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
319 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
320 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
321 if (!sdp->detached) {
322 scsi_device_put(sdp->device);
325 wake_up_interruptible(&sdp->o_excl_wait);
331 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
337 int req_pack_id = -1;
338 struct sg_header old_hdr;
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 ((k = verify_area(VERIFY_WRITE, buf, count)))
348 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
349 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
351 if (old_hdr.reply_len < 0) {
352 if (count >= SZ_SG_IO_HDR) {
354 (&new_hdr, buf, SZ_SG_IO_HDR))
356 req_pack_id = new_hdr.pack_id;
359 req_pack_id = old_hdr.pack_id;
361 srp = sg_get_rq_mark(sfp, req_pack_id);
362 if (!srp) { /* now wait on packet to arrive */
365 if (filp->f_flags & O_NONBLOCK)
368 res = 0; /* following is a macro that beats race condition */
369 __wait_event_interruptible(sfp->read_wait,
370 (sdp->detached || (srp = sg_get_rq_mark(sfp, req_pack_id))),
376 return res; /* -ERESTARTSYS because signal hit process */
379 if (srp->header.interface_id != '\0')
380 return sg_new_read(sfp, buf, count, srp);
383 memset(&old_hdr, 0, SZ_SG_HEADER);
384 old_hdr.reply_len = (int) hp->timeout;
385 old_hdr.pack_len = old_hdr.reply_len; /* very old, strange behaviour */
386 old_hdr.pack_id = hp->pack_id;
387 old_hdr.twelve_byte =
388 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
389 old_hdr.target_status = hp->masked_status;
390 old_hdr.host_status = hp->host_status;
391 old_hdr.driver_status = hp->driver_status;
392 if ((CHECK_CONDITION & hp->masked_status) ||
393 (DRIVER_SENSE & hp->driver_status))
394 memcpy(old_hdr.sense_buffer, srp->sense_b,
395 sizeof (old_hdr.sense_buffer));
396 switch (hp->host_status) {
397 /* This setup of 'result' is for backward compatibility and is best
398 ignored by the user who should use target, host + driver status */
400 case DID_PASSTHROUGH:
407 old_hdr.result = EBUSY;
414 old_hdr.result = EIO;
417 old_hdr.result = (srp->sense_b[0] == 0 &&
418 hp->masked_status == GOOD) ? 0 : EIO;
421 old_hdr.result = EIO;
425 /* Now copy the result back to the user buffer. */
426 if (count >= SZ_SG_HEADER) {
427 if (__copy_to_user(buf, &old_hdr, SZ_SG_HEADER))
430 if (count > old_hdr.reply_len)
431 count = old_hdr.reply_len;
432 if (count > SZ_SG_HEADER) {
434 sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)))
438 count = (old_hdr.result == 0) ? 0 : -EIO;
439 sg_finish_rem_req(srp);
444 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
446 sg_io_hdr_t *hp = &srp->header;
450 if (count < SZ_SG_IO_HDR) {
455 if ((hp->mx_sb_len > 0) && hp->sbp) {
456 if ((CHECK_CONDITION & hp->masked_status) ||
457 (DRIVER_SENSE & hp->driver_status)) {
458 int sb_len = sizeof (dummy_cmdp->sr_sense_buffer);
459 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
460 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
461 len = (len > sb_len) ? sb_len : len;
462 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
469 if (hp->masked_status || hp->host_status || hp->driver_status)
470 hp->info |= SG_INFO_CHECK;
471 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
475 err = sg_read_xfer(srp);
477 sg_finish_rem_req(srp);
478 return (0 == err) ? count : err;
482 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
484 int mxsize, cmd_size, k;
485 int input_size, blocking;
486 unsigned char opcode;
490 struct sg_header old_hdr;
492 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
494 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
496 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
497 sdp->disk->disk_name, (int) count));
500 if (!((filp->f_flags & O_NONBLOCK) ||
501 scsi_block_when_processing_errors(sdp->device)))
504 if ((k = verify_area(VERIFY_READ, buf, count)))
505 return k; /* protects following copy_from_user()s + get_user()s */
506 if (count < SZ_SG_HEADER)
508 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
510 blocking = !(filp->f_flags & O_NONBLOCK);
511 if (old_hdr.reply_len < 0)
512 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
513 if (count < (SZ_SG_HEADER + 6))
514 return -EIO; /* The minimum scsi command length is 6 bytes. */
516 if (!(srp = sg_add_request(sfp))) {
517 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
521 __get_user(opcode, buf);
522 if (sfp->next_cmd_len > 0) {
523 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
524 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
525 sfp->next_cmd_len = 0;
526 sg_remove_request(sfp, srp);
529 cmd_size = sfp->next_cmd_len;
530 sfp->next_cmd_len = 0; /* reset so only this write() effected */
532 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
533 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
536 SCSI_LOG_TIMEOUT(4, printk(
537 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
538 /* Determine buffer size. */
539 input_size = count - cmd_size;
540 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
541 mxsize -= SZ_SG_HEADER;
542 input_size -= SZ_SG_HEADER;
543 if (input_size < 0) {
544 sg_remove_request(sfp, srp);
545 return -EIO; /* User did not pass enough bytes for this command. */
548 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
549 hp->cmd_len = (unsigned char) cmd_size;
553 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
554 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
556 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
557 hp->dxfer_len = mxsize;
558 hp->dxferp = (char __user *)buf + cmd_size;
560 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
561 hp->flags = input_size; /* structure abuse ... */
562 hp->pack_id = old_hdr.pack_id;
564 if (__copy_from_user(cmnd, buf, cmd_size))
567 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
568 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
569 * is a non-zero input_size, so emit a warning.
571 if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
572 if (printk_ratelimit())
574 "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
575 "guessing data in;\n" KERN_WARNING " "
576 "program %s not setting count and/or reply_len properly\n",
577 old_hdr.reply_len - (int)SZ_SG_HEADER,
578 input_size, (unsigned int) cmnd[0],
580 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
581 return (k < 0) ? k : count;
585 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
586 int blocking, int read_only, Sg_request ** o_srp)
591 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
593 unsigned long ul_timeout;
595 if (count < SZ_SG_IO_HDR)
597 if ((k = verify_area(VERIFY_READ, buf, count)))
598 return k; /* protects following copy_from_user()s + get_user()s */
600 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
601 if (!(srp = sg_add_request(sfp))) {
602 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
606 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
607 sg_remove_request(sfp, srp);
610 if (hp->interface_id != 'S') {
611 sg_remove_request(sfp, srp);
614 if (hp->flags & SG_FLAG_MMAP_IO) {
615 if (hp->dxfer_len > sfp->reserve.bufflen) {
616 sg_remove_request(sfp, srp);
617 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
619 if (hp->flags & SG_FLAG_DIRECT_IO) {
620 sg_remove_request(sfp, srp);
621 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
623 if (sg_res_in_use(sfp)) {
624 sg_remove_request(sfp, srp);
625 return -EBUSY; /* reserve buffer already being used */
628 ul_timeout = msecs_to_jiffies(srp->header.timeout);
629 timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
630 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
631 sg_remove_request(sfp, srp);
634 if ((k = verify_area(VERIFY_READ, hp->cmdp, hp->cmd_len))) {
635 sg_remove_request(sfp, srp);
636 return k; /* protects following copy_from_user()s + get_user()s */
638 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
639 sg_remove_request(sfp, srp);
643 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
644 sg_remove_request(sfp, srp);
647 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
656 sg_common_write(Sg_fd * sfp, Sg_request * srp,
657 unsigned char *cmnd, int timeout, int blocking)
661 Sg_device *sdp = sfp->parentdp;
662 sg_io_hdr_t *hp = &srp->header;
665 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
667 hp->masked_status = 0;
671 hp->driver_status = 0;
673 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
674 (int) cmnd[0], (int) hp->cmd_len));
676 if ((k = sg_start_req(srp))) {
677 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
678 sg_finish_rem_req(srp);
679 return k; /* probably out of space --> ENOMEM */
681 if ((k = sg_write_xfer(srp))) {
682 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
683 sg_finish_rem_req(srp);
687 sg_finish_rem_req(srp);
690 SRpnt = scsi_allocate_request(sdp->device, GFP_ATOMIC);
692 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
693 sg_finish_rem_req(srp);
697 srp->my_cmdp = SRpnt;
698 q = SRpnt->sr_device->request_queue;
699 SRpnt->sr_request->rq_disk = sdp->disk;
700 SRpnt->sr_sense_buffer[0] = 0;
701 SRpnt->sr_cmd_len = hp->cmd_len;
702 SRpnt->sr_use_sg = srp->data.k_use_sg;
703 SRpnt->sr_sglist_len = srp->data.sglist_len;
704 SRpnt->sr_bufflen = srp->data.bufflen;
705 SRpnt->sr_underflow = 0;
706 SRpnt->sr_buffer = srp->data.buffer;
707 switch (hp->dxfer_direction) {
708 case SG_DXFER_TO_FROM_DEV:
709 case SG_DXFER_FROM_DEV:
710 SRpnt->sr_data_direction = SCSI_DATA_READ;
712 case SG_DXFER_TO_DEV:
713 SRpnt->sr_data_direction = SCSI_DATA_WRITE;
715 case SG_DXFER_UNKNOWN:
716 SRpnt->sr_data_direction = SCSI_DATA_UNKNOWN;
719 SRpnt->sr_data_direction = SCSI_DATA_NONE;
722 SRpnt->upper_private_data = srp;
723 srp->data.k_use_sg = 0;
724 srp->data.sglist_len = 0;
725 srp->data.bufflen = 0;
726 srp->data.buffer = NULL;
727 hp->duration = jiffies; /* unit jiffies now, millisecs after done */
728 /* Now send everything of to mid-level. The next time we hear about this
729 packet is when sg_cmd_done() is called (i.e. a callback). */
730 scsi_do_req(SRpnt, (void *) cmnd,
731 (void *) SRpnt->sr_buffer, hp->dxfer_len,
732 sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
733 /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
738 sg_srp_done(Sg_request *srp, Sg_fd *sfp)
740 unsigned long iflags;
743 read_lock_irqsave(&sfp->rq_list_lock, iflags);
745 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
750 sg_ioctl(struct inode *inode, struct file *filp,
751 unsigned int cmd_in, unsigned long arg)
753 void __user *p = (void __user *)arg;
755 int result, val, read_only;
759 unsigned long iflags;
761 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
763 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
764 sdp->disk->disk_name, (int) cmd_in));
765 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
770 int blocking = 1; /* ignore O_NONBLOCK flag */
774 if (!scsi_block_when_processing_errors(sdp->device))
776 result = verify_area(VERIFY_WRITE, p, SZ_SG_IO_HDR);
780 sg_new_write(sfp, p, SZ_SG_IO_HDR,
781 blocking, read_only, &srp);
784 srp->sg_io_owned = 1;
786 result = 0; /* following macro to beat race condition */
787 __wait_event_interruptible(sfp->read_wait,
788 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
793 return 0; /* request packet dropped already */
797 return result; /* -ERESTARTSYS because signal hit process */
799 write_lock_irqsave(&sfp->rq_list_lock, iflags);
801 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
802 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
803 return (result < 0) ? result : 0;
806 result = get_user(val, ip);
811 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
812 val = MULDIV (INT_MAX, USER_HZ, HZ);
813 sfp->timeout_user = val;
814 sfp->timeout = MULDIV (val, HZ, USER_HZ);
817 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
818 /* strange ..., for backward compatibility */
819 return sfp->timeout_user;
820 case SG_SET_FORCE_LOW_DMA:
821 result = get_user(val, ip);
826 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
827 val = (int) sfp->reserve.bufflen;
828 sg_remove_scat(&sfp->reserve);
829 sg_build_reserve(sfp, val);
834 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
838 return put_user((int) sfp->low_dma, ip);
841 verify_area(VERIFY_WRITE, p, sizeof (sg_scsi_id_t));
845 sg_scsi_id_t __user *sg_idp = p;
849 __put_user((int) sdp->device->host->host_no,
851 __put_user((int) sdp->device->channel,
853 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
854 __put_user((int) sdp->device->lun, &sg_idp->lun);
855 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
856 __put_user((short) sdp->device->host->cmd_per_lun,
857 &sg_idp->h_cmd_per_lun);
858 __put_user((short) sdp->device->queue_depth,
859 &sg_idp->d_queue_depth);
860 __put_user(0, &sg_idp->unused[0]);
861 __put_user(0, &sg_idp->unused[1]);
864 case SG_SET_FORCE_PACK_ID:
865 result = get_user(val, ip);
868 sfp->force_packid = val ? 1 : 0;
871 result = verify_area(VERIFY_WRITE, ip, sizeof (int));
874 read_lock_irqsave(&sfp->rq_list_lock, iflags);
875 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
876 if ((1 == srp->done) && (!srp->sg_io_owned)) {
877 read_unlock_irqrestore(&sfp->rq_list_lock,
879 __put_user(srp->header.pack_id, ip);
883 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
886 case SG_GET_NUM_WAITING:
887 read_lock_irqsave(&sfp->rq_list_lock, iflags);
888 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
889 if ((1 == srp->done) && (!srp->sg_io_owned))
892 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
893 return put_user(val, ip);
894 case SG_GET_SG_TABLESIZE:
895 return put_user(sdp->sg_tablesize, ip);
896 case SG_SET_RESERVED_SIZE:
897 result = get_user(val, ip);
902 if (val != sfp->reserve.bufflen) {
903 if (sg_res_in_use(sfp) || sfp->mmap_called)
905 sg_remove_scat(&sfp->reserve);
906 sg_build_reserve(sfp, val);
909 case SG_GET_RESERVED_SIZE:
910 val = (int) sfp->reserve.bufflen;
911 return put_user(val, ip);
912 case SG_SET_COMMAND_Q:
913 result = get_user(val, ip);
916 sfp->cmd_q = val ? 1 : 0;
918 case SG_GET_COMMAND_Q:
919 return put_user((int) sfp->cmd_q, ip);
920 case SG_SET_KEEP_ORPHAN:
921 result = get_user(val, ip);
924 sfp->keep_orphan = val;
926 case SG_GET_KEEP_ORPHAN:
927 return put_user((int) sfp->keep_orphan, ip);
928 case SG_NEXT_CMD_LEN:
929 result = get_user(val, ip);
932 sfp->next_cmd_len = (val > 0) ? val : 0;
934 case SG_GET_VERSION_NUM:
935 return put_user(sg_version_num, ip);
936 case SG_GET_ACCESS_COUNT:
937 /* faked - we don't have a real access count anymore */
938 val = (sdp->device ? 1 : 0);
939 return put_user(val, ip);
940 case SG_GET_REQUEST_TABLE:
941 result = verify_area(VERIFY_WRITE, p,
942 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
946 sg_req_info_t rinfo[SG_MAX_QUEUE];
948 read_lock_irqsave(&sfp->rq_list_lock, iflags);
949 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
950 ++val, srp = srp ? srp->nextrp : srp) {
951 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
953 rinfo[val].req_state = srp->done + 1;
955 srp->header.masked_status &
956 srp->header.host_status &
957 srp->header.driver_status;
958 rinfo[val].duration =
959 srp->done ? srp->header.duration :
961 jiffies - srp->header.duration);
962 rinfo[val].orphan = srp->orphan;
963 rinfo[val].sg_io_owned = srp->sg_io_owned;
964 rinfo[val].pack_id = srp->header.pack_id;
965 rinfo[val].usr_ptr = srp->header.usr_ptr;
968 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
969 return (__copy_to_user(p, rinfo,
970 SZ_SG_REQ_INFO * SG_MAX_QUEUE) ? -EFAULT : 0);
972 case SG_EMULATED_HOST:
975 return put_user(sdp->device->host->hostt->emulated, ip);
979 if (filp->f_flags & O_NONBLOCK) {
980 if (test_bit(SHOST_RECOVERY,
981 &sdp->device->host->shost_state))
983 } else if (!scsi_block_when_processing_errors(sdp->device))
985 result = get_user(val, ip);
988 if (SG_SCSI_RESET_NOTHING == val)
991 case SG_SCSI_RESET_DEVICE:
992 val = SCSI_TRY_RESET_DEVICE;
994 case SG_SCSI_RESET_BUS:
995 val = SCSI_TRY_RESET_BUS;
997 case SG_SCSI_RESET_HOST:
998 val = SCSI_TRY_RESET_HOST;
1003 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1005 return (scsi_reset_provider(sdp->device, val) ==
1006 SUCCESS) ? 0 : -EIO;
1007 case SCSI_IOCTL_SEND_COMMAND:
1011 unsigned char opcode = WRITE_6;
1012 Scsi_Ioctl_Command __user *siocp = p;
1014 if (copy_from_user(&opcode, siocp->data, 1))
1016 if (!sg_allow_access(opcode, sdp->device->type))
1019 return scsi_ioctl_send_command(sdp->device, p);
1021 result = get_user(val, ip);
1024 sdp->sgdebug = (char) val;
1026 case SCSI_IOCTL_GET_IDLUN:
1027 case SCSI_IOCTL_GET_BUS_NUMBER:
1028 case SCSI_IOCTL_PROBE_HOST:
1029 case SG_GET_TRANSFORM:
1032 return scsi_ioctl(sdp->device, cmd_in, p);
1035 return -EPERM; /* don't know so take safe approach */
1036 return scsi_ioctl(sdp->device, cmd_in, p);
1041 sg_poll(struct file *filp, poll_table * wait)
1043 unsigned int res = 0;
1048 unsigned long iflags;
1050 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1053 poll_wait(filp, &sfp->read_wait, wait);
1054 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1055 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1056 /* if any read waiting, flag it */
1057 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1058 res = POLLIN | POLLRDNORM;
1061 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1065 else if (!sfp->cmd_q) {
1067 res |= POLLOUT | POLLWRNORM;
1068 } else if (count < SG_MAX_QUEUE)
1069 res |= POLLOUT | POLLWRNORM;
1070 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1071 sdp->disk->disk_name, (int) res));
1076 sg_fasync(int fd, struct file *filp, int mode)
1082 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1084 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1085 sdp->disk->disk_name, mode));
1087 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1088 return (retval < 0) ? retval : 0;
1091 static inline unsigned char *
1092 sg_scatg2virt(const struct scatterlist *sclp)
1094 return (sclp && sclp->page) ?
1095 (unsigned char *) page_address(sclp->page) + sclp->offset : NULL;
1098 /* When startFinish==1 increments page counts for pages other than the
1099 first of scatter gather elements obtained from __get_free_pages().
1100 When startFinish==0 decrements ... */
1102 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1108 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1109 startFinish, rsv_schp->k_use_sg));
1110 /* N.B. correction _not_ applied to base page of each allocation */
1111 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1112 struct scatterlist *sclp = rsv_schp->buffer;
1114 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
1115 for (m = PAGE_SIZE; m < sclp->length; m += PAGE_SIZE) {
1116 page_ptr = sg_scatg2virt(sclp) + m;
1117 page = virt_to_page(page_ptr);
1121 if (page_count(page) > 0)
1126 } else { /* reserve buffer is just a single allocation */
1127 for (m = PAGE_SIZE; m < rsv_schp->bufflen; m += PAGE_SIZE) {
1128 page_ptr = (unsigned char *) rsv_schp->buffer + m;
1129 page = virt_to_page(page_ptr);
1133 if (page_count(page) > 0)
1140 static struct page *
1141 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1144 struct page *page = NOPAGE_SIGBUS;
1145 void *page_ptr = NULL;
1146 unsigned long offset;
1147 Sg_scatter_hold *rsv_schp;
1149 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1151 rsv_schp = &sfp->reserve;
1152 offset = addr - vma->vm_start;
1153 if (offset >= rsv_schp->bufflen)
1155 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1156 offset, rsv_schp->k_use_sg));
1157 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1159 unsigned long sa = vma->vm_start;
1161 struct scatterlist *sclp = rsv_schp->buffer;
1163 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1165 len = vma->vm_end - sa;
1166 len = (len < sclp->length) ? len : sclp->length;
1168 page_ptr = sg_scatg2virt(sclp) + offset;
1169 page = virt_to_page(page_ptr);
1170 get_page(page); /* increment page count */
1176 } else { /* reserve buffer is just a single allocation */
1177 page_ptr = (unsigned char *) rsv_schp->buffer + offset;
1178 page = virt_to_page(page_ptr);
1179 get_page(page); /* increment page count */
1182 *type = VM_FAULT_MINOR;
1186 static struct vm_operations_struct sg_mmap_vm_ops = {
1187 .nopage = sg_vma_nopage,
1191 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1194 unsigned long req_sz = vma->vm_end - vma->vm_start;
1195 Sg_scatter_hold *rsv_schp;
1197 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1199 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1200 (void *) vma->vm_start, (int) req_sz));
1202 return -EINVAL; /* want no offset */
1203 rsv_schp = &sfp->reserve;
1204 if (req_sz > rsv_schp->bufflen)
1205 return -ENOMEM; /* cannot map more than reserved buffer */
1207 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1209 unsigned long sa = vma->vm_start;
1211 struct scatterlist *sclp = rsv_schp->buffer;
1213 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1215 if (0 != sclp->offset)
1216 return -EFAULT; /* non page aligned memory ?? */
1217 len = vma->vm_end - sa;
1218 len = (len < sclp->length) ? len : sclp->length;
1221 } else { /* reserve buffer is just a single allocation */
1222 if ((unsigned long) rsv_schp->buffer & (PAGE_SIZE - 1))
1223 return -EFAULT; /* non page aligned memory ?? */
1225 if (0 == sfp->mmap_called) {
1226 sg_rb_correct4mmap(rsv_schp, 1); /* do only once per fd lifetime */
1227 sfp->mmap_called = 1;
1229 vma->vm_flags |= (VM_RESERVED | VM_IO);
1230 vma->vm_private_data = sfp;
1231 vma->vm_ops = &sg_mmap_vm_ops;
1235 /* This function is a "bottom half" handler that is called by the
1236 * mid level when a command is completed (or has failed). */
1238 sg_cmd_done(Scsi_Cmnd * SCpnt)
1240 Scsi_Request *SRpnt = NULL;
1241 Sg_device *sdp = NULL;
1243 Sg_request *srp = NULL;
1244 unsigned long iflags;
1246 if (SCpnt && (SRpnt = SCpnt->sc_request))
1247 srp = (Sg_request *) SRpnt->upper_private_data;
1249 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1251 scsi_release_request(SRpnt);
1254 sfp = srp->parentfp;
1256 sdp = sfp->parentdp;
1257 if ((NULL == sdp) || sdp->detached) {
1258 printk(KERN_INFO "sg_cmd_done: device detached\n");
1259 scsi_release_request(SRpnt);
1263 /* First transfer ownership of data buffers to sg_device object. */
1264 srp->data.k_use_sg = SRpnt->sr_use_sg;
1265 srp->data.sglist_len = SRpnt->sr_sglist_len;
1266 srp->data.bufflen = SRpnt->sr_bufflen;
1267 srp->data.buffer = SRpnt->sr_buffer;
1268 /* now clear out request structure */
1269 SRpnt->sr_use_sg = 0;
1270 SRpnt->sr_sglist_len = 0;
1271 SRpnt->sr_bufflen = 0;
1272 SRpnt->sr_buffer = NULL;
1273 SRpnt->sr_underflow = 0;
1274 SRpnt->sr_request->rq_disk = NULL; /* "sg" _disowns_ request blk */
1276 srp->my_cmdp = NULL;
1278 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1279 sdp->disk->disk_name, srp->header.pack_id, (int) SRpnt->sr_result));
1280 srp->header.resid = SCpnt->resid;
1281 /* N.B. unit of duration changes here from jiffies to millisecs */
1282 srp->header.duration =
1283 jiffies_to_msecs(jiffies - srp->header.duration);
1284 if (0 != SRpnt->sr_result) {
1285 memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
1286 sizeof (srp->sense_b));
1287 srp->header.status = 0xff & SRpnt->sr_result;
1288 srp->header.masked_status = status_byte(SRpnt->sr_result);
1289 srp->header.msg_status = msg_byte(SRpnt->sr_result);
1290 srp->header.host_status = host_byte(SRpnt->sr_result);
1291 srp->header.driver_status = driver_byte(SRpnt->sr_result);
1292 if ((sdp->sgdebug > 0) &&
1293 ((CHECK_CONDITION == srp->header.masked_status) ||
1294 (COMMAND_TERMINATED == srp->header.masked_status)))
1295 print_req_sense("sg_cmd_done", SRpnt);
1297 /* Following if statement is a patch supplied by Eric Youngdale */
1298 if (driver_byte(SRpnt->sr_result) != 0
1299 && (SRpnt->sr_sense_buffer[0] & 0x7f) == 0x70
1300 && (SRpnt->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION
1301 && sdp->device->removable) {
1302 /* Detected disc change. Set the bit - this may be used if */
1303 /* there are filesystems using this device. */
1304 sdp->device->changed = 1;
1307 /* Rely on write phase to clean out srp status values, so no "else" */
1309 scsi_release_request(SRpnt);
1311 if (sfp->closed) { /* whoops this fd already released, cleanup */
1312 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1313 sg_finish_rem_req(srp);
1315 if (NULL == sfp->headrp) {
1316 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1317 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1318 scsi_device_put(sdp->device);
1322 } else if (srp && srp->orphan) {
1323 if (sfp->keep_orphan)
1324 srp->sg_io_owned = 0;
1326 sg_finish_rem_req(srp);
1331 /* Now wake up any sg_read() that is waiting for this packet. */
1332 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1333 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1335 wake_up_interruptible(&sfp->read_wait);
1336 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1340 static struct file_operations sg_fops = {
1341 .owner = THIS_MODULE,
1348 .release = sg_release,
1349 .fasync = sg_fasync,
1352 static struct class_simple * sg_sysfs_class;
1354 static int sg_sysfs_valid = 0;
1356 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1359 unsigned long iflags;
1360 void *old_sg_dev_arr = NULL;
1363 sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
1365 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1369 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1370 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1372 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1373 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1375 tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1376 if (unlikely(!tmp_da))
1379 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1380 memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
1381 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1382 old_sg_dev_arr = sg_dev_arr;
1383 sg_dev_arr = tmp_da;
1384 sg_dev_max = tmp_dev_max;
1387 for (k = 0; k < sg_dev_max; k++)
1390 if (unlikely(k >= SG_MAX_DEVS))
1393 memset(sdp, 0, sizeof(*sdp));
1394 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1395 sprintf(disk->disk_name, "sg%d", k);
1396 disk->first_minor = k;
1398 sdp->device = scsidp;
1399 init_waitqueue_head(&sdp->o_excl_wait);
1400 sdp->sg_tablesize = scsidp->host ? scsidp->host->sg_tablesize : 0;
1403 sg_dev_arr[k] = sdp;
1404 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1410 kfree(old_sg_dev_arr);
1414 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1419 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1421 "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1422 "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
1423 scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
1429 sg_add(struct class_device *cl_dev)
1431 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1432 struct gendisk *disk;
1433 Sg_device *sdp = NULL;
1434 struct cdev * cdev = NULL;
1437 disk = alloc_disk(1);
1439 printk(KERN_WARNING "alloc_disk failed\n");
1442 disk->major = SCSI_GENERIC_MAJOR;
1445 cdev = cdev_alloc();
1447 printk(KERN_WARNING "cdev_alloc failed\n");
1450 cdev->owner = THIS_MODULE;
1451 cdev->ops = &sg_fops;
1453 error = sg_alloc(disk, scsidp);
1455 printk(KERN_WARNING "sg_alloc failed\n");
1459 sdp = sg_dev_arr[k];
1461 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1462 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1463 "%s/generic", scsidp->devfs_name);
1464 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1466 devfs_remove("%s/generic", scsidp->devfs_name);
1470 if (sg_sysfs_valid) {
1471 struct class_device * sg_class_member;
1473 sg_class_member = class_simple_device_add(sg_sysfs_class,
1474 MKDEV(SCSI_GENERIC_MAJOR, k),
1477 if (IS_ERR(sg_class_member))
1478 printk(KERN_WARNING "sg_add: "
1479 "class_simple_device_add failed\n");
1480 class_set_devdata(sg_class_member, sdp);
1481 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1482 &sg_class_member->kobj, "generic");
1484 printk(KERN_ERR "sg_add: unable to make symlink "
1485 "'generic' back to sg%d\n", k);
1487 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1490 "Attached scsi generic sg%d at scsi%d, channel"
1491 " %d, id %d, lun %d, type %d\n", k,
1492 scsidp->host->host_no, scsidp->channel, scsidp->id,
1493 scsidp->lun, scsidp->type);
1505 sg_remove(struct class_device *cl_dev)
1507 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1508 Sg_device *sdp = NULL;
1509 unsigned long iflags;
1516 if (NULL == sg_dev_arr)
1519 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1520 for (k = 0; k < sg_dev_max; k++) {
1521 sdp = sg_dev_arr[k];
1522 if ((NULL == sdp) || (sdp->device != scsidp))
1523 continue; /* dirty but lowers nesting */
1526 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1528 for (srp = sfp->headrp; srp; srp = tsrp) {
1530 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1531 sg_finish_rem_req(srp);
1534 scsi_device_put(sdp->device);
1535 __sg_remove_sfp(sdp, sfp);
1538 wake_up_interruptible(&sfp->read_wait);
1539 kill_fasync(&sfp->async_qp, SIGPOLL,
1543 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1544 if (NULL == sdp->headfp) {
1545 sg_dev_arr[k] = NULL;
1547 } else { /* nothing active, simple case */
1548 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1549 sg_dev_arr[k] = NULL;
1554 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1557 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1558 class_simple_device_remove(MKDEV(SCSI_GENERIC_MAJOR, k));
1559 cdev_del(sdp->cdev);
1561 devfs_remove("%s/generic", scsidp->devfs_name);
1562 put_disk(sdp->disk);
1564 if (NULL == sdp->headfp)
1565 kfree((char *) sdp);
1569 msleep(10); /* dirty detach so delay device destruction */
1572 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1573 * of sysfs parameters (which module_param doesn't yet support).
1574 * Sysfs parameters defined explicitly below.
1576 module_param_named(def_reserved_size, def_reserved_size, int, 0);
1577 module_param_named(allow_dio, sg_allow_dio, int, 0);
1579 MODULE_AUTHOR("Douglas Gilbert");
1580 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1581 MODULE_LICENSE("GPL");
1582 MODULE_VERSION(SG_VERSION_STR);
1584 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1585 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1592 if (def_reserved_size >= 0)
1593 sg_big_buff = def_reserved_size;
1595 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1599 sg_sysfs_class = class_simple_create(THIS_MODULE, "scsi_generic");
1600 if ( IS_ERR(sg_sysfs_class) ) {
1601 rc = PTR_ERR(sg_sysfs_class);
1605 rc = scsi_register_interface(&sg_interface);
1607 #ifdef CONFIG_SCSI_PROC_FS
1609 #endif /* CONFIG_SCSI_PROC_FS */
1612 class_simple_destroy(sg_sysfs_class);
1614 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1621 #ifdef CONFIG_SCSI_PROC_FS
1623 #endif /* CONFIG_SCSI_PROC_FS */
1624 scsi_unregister_interface(&sg_interface);
1625 class_simple_destroy(sg_sysfs_class);
1627 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1629 if (sg_dev_arr != NULL) {
1630 kfree((char *) sg_dev_arr);
1637 sg_start_req(Sg_request * srp)
1640 Sg_fd *sfp = srp->parentfp;
1641 sg_io_hdr_t *hp = &srp->header;
1642 int dxfer_len = (int) hp->dxfer_len;
1643 int dxfer_dir = hp->dxfer_direction;
1644 Sg_scatter_hold *req_schp = &srp->data;
1645 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1647 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1648 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1650 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1651 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1652 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1653 res = sg_build_direct(srp, sfp, dxfer_len);
1654 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1657 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1658 sg_link_reserve(sfp, srp, dxfer_len);
1660 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1662 sg_remove_scat(req_schp);
1670 sg_finish_rem_req(Sg_request * srp)
1672 Sg_fd *sfp = srp->parentfp;
1673 Sg_scatter_hold *req_schp = &srp->data;
1675 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1677 sg_unlink_reserve(sfp, srp);
1679 sg_remove_scat(req_schp);
1680 sg_remove_request(sfp, srp);
1684 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1687 int elem_sz = sizeof (struct scatterlist);
1688 int sg_bufflen = tablesize * elem_sz;
1689 int mx_sc_elems = tablesize;
1691 schp->buffer = sg_page_malloc(sg_bufflen, sfp->low_dma, &ret_sz);
1694 else if (ret_sz != sg_bufflen) {
1695 sg_bufflen = ret_sz;
1696 mx_sc_elems = sg_bufflen / elem_sz;
1698 schp->sglist_len = sg_bufflen;
1699 memset(schp->buffer, 0, sg_bufflen);
1700 return mx_sc_elems; /* number of scat_gath elements allocated */
1703 #ifdef SG_ALLOW_DIO_CODE
1704 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1705 /* hopefully this generic code will moved to a library */
1707 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1708 - mapping of all pages not successful
1709 - any page is above max_pfn
1710 (i.e., either completely successful or fails)
1713 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1714 unsigned long uaddr, size_t count, int rw,
1715 unsigned long max_pfn)
1718 unsigned int nr_pages;
1719 struct page **pages;
1721 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
1723 /* User attempted Overflow! */
1724 if ((uaddr + count) < uaddr)
1728 if (nr_pages > max_pages)
1735 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1738 /* Try to fault in all of the necessary pages */
1739 down_read(¤t->mm->mmap_sem);
1740 /* rw==READ means read from drive, write into memory area */
1741 res = get_user_pages(
1747 0, /* don't force */
1750 up_read(¤t->mm->mmap_sem);
1752 /* Errors and no page mapped should return here */
1756 for (i=0; i < nr_pages; i++) {
1757 /* FIXME: flush superflous for rw==READ,
1758 * probably wrong function for rw==WRITE
1760 flush_dcache_page(pages[i]);
1761 if (page_to_pfn(pages[i]) > max_pfn)
1763 /* ?? Is locking needed? I don't think so */
1764 /* if (TestSetPageLocked(pages[i]))
1768 /* Populate the scatter/gather list */
1769 sgl[0].page = pages[0];
1770 sgl[0].offset = uaddr & ~PAGE_MASK;
1772 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1773 count -= sgl[0].length;
1774 for (i=1; i < nr_pages ; i++) {
1776 sgl[i].page = pages[i];
1777 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1782 sgl[0].length = count;
1789 /* for (j=0; j < i; j++)
1790 unlock_page(pages[j]); */
1794 for (j=0; j < res; j++)
1795 page_cache_release(pages[j]);
1801 /* And unmap them... */
1803 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1808 for (i=0; i < nr_pages; i++) {
1809 if (dirtied && !PageReserved(sgl[i].page))
1810 SetPageDirty(sgl[i].page);
1811 /* unlock_page(sgl[i].page); */
1812 /* FIXME: cache flush missing for rw==READ
1813 * FIXME: call the correct reference counting function
1815 page_cache_release(sgl[i].page);
1821 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1825 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1827 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1829 #ifdef SG_ALLOW_DIO_CODE
1830 sg_io_hdr_t *hp = &srp->header;
1831 Sg_scatter_hold *schp = &srp->data;
1832 int sg_tablesize = sfp->parentdp->sg_tablesize;
1833 struct scatterlist *sgl;
1834 int mx_sc_elems, res;
1835 struct scsi_device *sdev = sfp->parentdp->device;
1837 if (((unsigned long)hp->dxferp &
1838 queue_dma_alignment(sdev->request_queue)) != 0)
1840 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1841 if (mx_sc_elems <= 0) {
1844 sgl = (struct scatterlist *)schp->buffer;
1845 res = st_map_user_pages(sgl, mx_sc_elems, (unsigned long)hp->dxferp, dxfer_len,
1846 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0, ULONG_MAX);
1849 schp->k_use_sg = res;
1850 schp->dio_in_use = 1;
1851 hp->info |= SG_INFO_DIRECT_IO;
1859 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1862 int blk_size = buff_size;
1863 unsigned char *p = NULL;
1865 if ((blk_size < 0) || (!sfp))
1868 ++blk_size; /* don't know why */
1869 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1870 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1871 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1872 buff_size, blk_size));
1873 if (blk_size <= SG_SCATTER_SZ) {
1874 p = sg_page_malloc(blk_size, sfp->low_dma, &ret_sz);
1877 if (blk_size == ret_sz) { /* got it on the first attempt */
1880 schp->bufflen = blk_size;
1881 schp->b_malloc_len = blk_size;
1885 p = sg_page_malloc(SG_SCATTER_SZ, sfp->low_dma, &ret_sz);
1889 /* Want some local declarations, so start new block ... */
1890 { /* lets try and build a scatter gather list */
1891 struct scatterlist *sclp;
1894 int sg_tablesize = sfp->parentdp->sg_tablesize;
1897 /* N.B. ret_sz carried into this block ... */
1898 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1899 if (mx_sc_elems < 0)
1900 return mx_sc_elems; /* most likely -ENOMEM */
1902 for (k = 0, sclp = schp->buffer, rem_sz = blk_size;
1903 (rem_sz > 0) && (k < mx_sc_elems);
1904 ++k, rem_sz -= ret_sz, ++sclp) {
1910 SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1911 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1915 sclp->page = virt_to_page(p);
1916 sclp->offset = offset_in_page(p);
1917 sclp->length = ret_sz;
1919 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1920 k, sg_scatg2virt(sclp), ret_sz));
1921 } /* end of for loop */
1923 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1924 schp->bufflen = blk_size;
1925 if (rem_sz > 0) /* must have failed */
1932 sg_write_xfer(Sg_request * srp)
1934 sg_io_hdr_t *hp = &srp->header;
1935 Sg_scatter_hold *schp = &srp->data;
1937 int j, k, onum, usglen, ksglen, res;
1938 int iovec_count = (int) hp->iovec_count;
1939 int dxfer_dir = hp->dxfer_direction;
1941 unsigned char __user *up;
1942 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1944 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1945 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1946 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1947 if (schp->bufflen < num_xfer)
1948 num_xfer = schp->bufflen;
1950 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1952 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1955 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1956 num_xfer, iovec_count, schp->k_use_sg));
1959 if ((k = verify_area(VERIFY_READ, hp->dxferp,
1960 SZ_SG_IOVEC * onum)))
1965 if (0 == schp->k_use_sg) { /* kernel has single buffer */
1966 for (j = 0, p = schp->buffer; j < onum; ++j) {
1967 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1970 usglen = (num_xfer > usglen) ? usglen : num_xfer;
1971 if (__copy_from_user(p, up, usglen))
1978 } else { /* kernel using scatter gather list */
1979 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
1981 ksglen = (int) sclp->length;
1982 p = sg_scatg2virt(sclp);
1983 for (j = 0, k = 0; j < onum; ++j) {
1984 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1988 for (; p; ++sclp, ksglen = (int) sclp->length,
1989 p = sg_scatg2virt(sclp)) {
1992 if (ksglen > usglen) {
1993 if (usglen >= num_xfer) {
1994 if (__copy_from_user
1999 if (__copy_from_user(p, up, usglen))
2005 if (ksglen >= num_xfer) {
2006 if (__copy_from_user
2011 if (__copy_from_user(p, up, ksglen))
2017 if (k >= schp->k_use_sg)
2026 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
2027 int wr_xf, int *countp, unsigned char __user **up)
2029 int num_xfer = (int) hp->dxfer_len;
2030 unsigned char __user *p = hp->dxferp;
2034 if (wr_xf && ('\0' == hp->interface_id))
2035 count = (int) hp->flags; /* holds "old" input_size */
2040 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2043 count = (int) iovec.iov_len;
2045 if ((k = verify_area(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count)))
2055 sg_remove_scat(Sg_scatter_hold * schp)
2057 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2058 if (schp->buffer && (schp->sglist_len > 0)) {
2059 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2061 if (schp->dio_in_use) {
2062 #ifdef SG_ALLOW_DIO_CODE
2063 st_unmap_user_pages(sclp, schp->k_use_sg, TRUE);
2068 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2070 SCSI_LOG_TIMEOUT(5, printk(
2071 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2072 k, sg_scatg2virt(sclp), sclp->length));
2073 sg_page_free(sg_scatg2virt(sclp), sclp->length);
2079 sg_page_free(schp->buffer, schp->sglist_len);
2080 } else if (schp->buffer)
2081 sg_page_free(schp->buffer, schp->b_malloc_len);
2082 memset(schp, 0, sizeof (*schp));
2086 sg_read_xfer(Sg_request * srp)
2088 sg_io_hdr_t *hp = &srp->header;
2089 Sg_scatter_hold *schp = &srp->data;
2091 int j, k, onum, usglen, ksglen, res;
2092 int iovec_count = (int) hp->iovec_count;
2093 int dxfer_dir = hp->dxfer_direction;
2095 unsigned char __user *up;
2096 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2098 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2099 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2100 num_xfer = hp->dxfer_len;
2101 if (schp->bufflen < num_xfer)
2102 num_xfer = schp->bufflen;
2104 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2106 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2109 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2110 num_xfer, iovec_count, schp->k_use_sg));
2113 if ((k = verify_area(VERIFY_READ, hp->dxferp,
2114 SZ_SG_IOVEC * onum)))
2119 if (0 == schp->k_use_sg) { /* kernel has single buffer */
2120 for (j = 0, p = schp->buffer; j < onum; ++j) {
2121 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2124 usglen = (num_xfer > usglen) ? usglen : num_xfer;
2125 if (__copy_to_user(up, p, usglen))
2132 } else { /* kernel using scatter gather list */
2133 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2135 ksglen = (int) sclp->length;
2136 p = sg_scatg2virt(sclp);
2137 for (j = 0, k = 0; j < onum; ++j) {
2138 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2142 for (; p; ++sclp, ksglen = (int) sclp->length,
2143 p = sg_scatg2virt(sclp)) {
2146 if (ksglen > usglen) {
2147 if (usglen >= num_xfer) {
2153 if (__copy_to_user(up, p, usglen))
2159 if (ksglen >= num_xfer) {
2165 if (__copy_to_user(up, p, ksglen))
2171 if (k >= schp->k_use_sg)
2180 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2182 Sg_scatter_hold *schp = &srp->data;
2184 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2186 if ((!outp) || (num_read_xfer <= 0))
2188 if (schp->k_use_sg > 0) {
2190 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2192 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2194 num = (int) sclp->length;
2195 if (num > num_read_xfer) {
2197 (outp, sg_scatg2virt(sclp), num_read_xfer))
2202 (outp, sg_scatg2virt(sclp), num))
2204 num_read_xfer -= num;
2205 if (num_read_xfer <= 0)
2211 if (__copy_to_user(outp, schp->buffer, num_read_xfer))
2218 sg_build_reserve(Sg_fd * sfp, int req_size)
2220 Sg_scatter_hold *schp = &sfp->reserve;
2222 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2224 if (req_size < PAGE_SIZE)
2225 req_size = PAGE_SIZE;
2226 if (0 == sg_build_indirect(schp, sfp, req_size))
2229 sg_remove_scat(schp);
2230 req_size >>= 1; /* divide by 2 */
2231 } while (req_size > (PAGE_SIZE / 2));
2235 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2237 Sg_scatter_hold *req_schp = &srp->data;
2238 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2241 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2242 size = (size + 1) & (~1); /* round to even for aha1542 */
2243 if (rsv_schp->k_use_sg > 0) {
2246 struct scatterlist *sclp =
2247 (struct scatterlist *) rsv_schp->buffer;
2249 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
2250 num = (int) sclp->length;
2253 req_schp->k_use_sg = 0;
2254 req_schp->buffer = sg_scatg2virt(sclp);
2256 sfp->save_scat_len = num;
2257 sclp->length = (unsigned) rem;
2258 req_schp->k_use_sg = k + 1;
2259 req_schp->sglist_len =
2260 rsv_schp->sglist_len;
2261 req_schp->buffer = rsv_schp->buffer;
2263 req_schp->bufflen = size;
2264 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2269 if (k >= rsv_schp->k_use_sg)
2270 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2272 req_schp->k_use_sg = 0;
2273 req_schp->bufflen = size;
2274 req_schp->buffer = rsv_schp->buffer;
2275 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2280 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2282 Sg_scatter_hold *req_schp = &srp->data;
2283 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2285 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2286 (int) req_schp->k_use_sg));
2287 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2288 struct scatterlist *sclp =
2289 (struct scatterlist *) rsv_schp->buffer;
2291 if (sfp->save_scat_len > 0)
2292 (sclp + (req_schp->k_use_sg - 1))->length =
2293 (unsigned) sfp->save_scat_len;
2295 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2297 req_schp->k_use_sg = 0;
2298 req_schp->bufflen = 0;
2299 req_schp->buffer = NULL;
2300 req_schp->sglist_len = 0;
2301 sfp->save_scat_len = 0;
2306 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2309 unsigned long iflags;
2311 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2312 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2313 /* look for requests that are ready + not SG_IO owned */
2314 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2315 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2316 resp->done = 2; /* guard against other readers */
2320 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2324 #ifdef CONFIG_SCSI_PROC_FS
2326 sg_get_nth_request(Sg_fd * sfp, int nth)
2329 unsigned long iflags;
2332 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2333 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2334 ++k, resp = resp->nextrp) ;
2335 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2340 /* always adds to end of list */
2342 sg_add_request(Sg_fd * sfp)
2345 unsigned long iflags;
2347 Sg_request *rp = sfp->req_arr;
2349 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2352 memset(rp, 0, sizeof (Sg_request));
2357 if (0 == sfp->cmd_q)
2358 resp = NULL; /* command queuing disallowed */
2360 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2364 if (k < SG_MAX_QUEUE) {
2365 memset(rp, 0, sizeof (Sg_request));
2367 while (resp->nextrp)
2368 resp = resp->nextrp;
2376 resp->nextrp = NULL;
2377 resp->header.duration = jiffies;
2378 resp->my_cmdp = NULL;
2380 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2384 /* Return of 1 for found; 0 for not found */
2386 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2388 Sg_request *prev_rp;
2390 unsigned long iflags;
2393 if ((!sfp) || (!srp) || (!sfp->headrp))
2395 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2396 prev_rp = sfp->headrp;
2397 if (srp == prev_rp) {
2398 sfp->headrp = prev_rp->nextrp;
2399 prev_rp->parentfp = NULL;
2402 while ((rp = prev_rp->nextrp)) {
2404 prev_rp->nextrp = rp->nextrp;
2405 rp->parentfp = NULL;
2412 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2416 #ifdef CONFIG_SCSI_PROC_FS
2418 sg_get_nth_sfp(Sg_device * sdp, int nth)
2421 unsigned long iflags;
2424 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2425 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2426 ++k, resp = resp->nextfp) ;
2427 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2433 sg_add_sfp(Sg_device * sdp, int dev)
2436 unsigned long iflags;
2438 sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
2441 memset(sfp, 0, sizeof (Sg_fd));
2442 init_waitqueue_head(&sfp->read_wait);
2443 rwlock_init(&sfp->rq_list_lock);
2445 sfp->timeout = SG_DEFAULT_TIMEOUT;
2446 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2447 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2448 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2449 sdp->device->host->unchecked_isa_dma : 1;
2450 sfp->cmd_q = SG_DEF_COMMAND_Q;
2451 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2452 sfp->parentdp = sdp;
2453 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2456 else { /* add to tail of existing list */
2457 Sg_fd *pfp = sdp->headfp;
2462 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2463 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2464 sg_build_reserve(sfp, sg_big_buff);
2465 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2466 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2471 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2476 prev_fp = sdp->headfp;
2478 sdp->headfp = prev_fp->nextfp;
2480 while ((fp = prev_fp->nextfp)) {
2482 prev_fp->nextfp = fp->nextfp;
2488 if (sfp->reserve.bufflen > 0) {
2490 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2491 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2492 if (sfp->mmap_called)
2493 sg_rb_correct4mmap(&sfp->reserve, 0); /* undo correction */
2494 sg_remove_scat(&sfp->reserve);
2496 sfp->parentdp = NULL;
2497 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2498 sg_page_free((char *) sfp, sizeof (Sg_fd));
2501 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2503 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2510 for (srp = sfp->headrp; srp; srp = tsrp) {
2512 if (sg_srp_done(srp, sfp))
2513 sg_finish_rem_req(srp);
2518 unsigned long iflags;
2520 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2521 __sg_remove_sfp(sdp, sfp);
2522 if (sdp->detached && (NULL == sdp->headfp)) {
2526 for (k = 0; k < maxd; ++k) {
2527 if (sdp == sg_dev_arr[k])
2531 sg_dev_arr[k] = NULL;
2532 kfree((char *) sdp);
2535 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2537 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2538 /* only bump the access_count if we actually succeeded in
2539 * throwing another counter on the host module */
2540 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2541 sfp->closed = 1; /* flag dirty state on this fd */
2542 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2549 sg_res_in_use(Sg_fd * sfp)
2551 const Sg_request *srp;
2552 unsigned long iflags;
2554 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2555 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2558 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2562 /* If retSzp==NULL want exact size or fail */
2564 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2575 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2577 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2579 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2580 order++, a_size <<= 1) ;
2581 resp = (char *) __get_free_pages(page_mask, order);
2582 while ((!resp) && order && retSzp) {
2584 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2585 resp = (char *) __get_free_pages(page_mask, order); /* try half */
2589 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2590 memset(resp, 0, resSz);
2598 sg_page_free(char *buff, int size)
2604 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2605 order++, a_size <<= 1) ;
2606 free_pages((unsigned long) buff, order);
2609 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2610 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2611 MODE_SENSE, MODE_SENSE_10, LOG_SENSE
2615 sg_allow_access(unsigned char opcode, char dev_type)
2619 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2621 for (k = 0; k < sizeof (allow_ops); ++k) {
2622 if (opcode == allow_ops[k])
2628 #ifdef CONFIG_SCSI_PROC_FS
2633 unsigned long iflags;
2635 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2636 for (k = sg_dev_max - 1; k >= 0; --k)
2637 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2639 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2640 return k + 1; /* origin 1 */
2647 Sg_device *sdp = NULL;
2648 unsigned long iflags;
2650 if (sg_dev_arr && (dev >= 0)) {
2651 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2652 if (dev < sg_dev_max)
2653 sdp = sg_dev_arr[dev];
2654 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2659 #ifdef CONFIG_SCSI_PROC_FS
2661 static struct proc_dir_entry *sg_proc_sgp = NULL;
2663 static char sg_proc_sg_dirname[] = "scsi/sg";
2665 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2667 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2668 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2669 size_t count, loff_t *off);
2670 static struct file_operations adio_fops = {
2671 /* .owner, .read and .llseek added in sg_proc_init() */
2672 .open = sg_proc_single_open_adio,
2673 .write = sg_proc_write_adio,
2674 .release = single_release,
2677 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2678 static ssize_t sg_proc_write_dressz(struct file *filp,
2679 const char __user *buffer, size_t count, loff_t *off);
2680 static struct file_operations dressz_fops = {
2681 .open = sg_proc_single_open_dressz,
2682 .write = sg_proc_write_dressz,
2683 .release = single_release,
2686 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2687 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2688 static struct file_operations version_fops = {
2689 .open = sg_proc_single_open_version,
2690 .release = single_release,
2693 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2694 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2695 static struct file_operations devhdr_fops = {
2696 .open = sg_proc_single_open_devhdr,
2697 .release = single_release,
2700 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2701 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2702 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2703 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2704 static void dev_seq_stop(struct seq_file *s, void *v);
2705 static struct file_operations dev_fops = {
2706 .open = sg_proc_open_dev,
2707 .release = seq_release,
2709 static struct seq_operations dev_seq_ops = {
2710 .start = dev_seq_start,
2711 .next = dev_seq_next,
2712 .stop = dev_seq_stop,
2713 .show = sg_proc_seq_show_dev,
2716 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2717 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2718 static struct file_operations devstrs_fops = {
2719 .open = sg_proc_open_devstrs,
2720 .release = seq_release,
2722 static struct seq_operations devstrs_seq_ops = {
2723 .start = dev_seq_start,
2724 .next = dev_seq_next,
2725 .stop = dev_seq_stop,
2726 .show = sg_proc_seq_show_devstrs,
2729 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2730 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2731 static struct file_operations debug_fops = {
2732 .open = sg_proc_open_debug,
2733 .release = seq_release,
2735 static struct seq_operations debug_seq_ops = {
2736 .start = dev_seq_start,
2737 .next = dev_seq_next,
2738 .stop = dev_seq_stop,
2739 .show = sg_proc_seq_show_debug,
2743 struct sg_proc_leaf {
2745 struct file_operations * fops;
2748 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2749 {"allow_dio", &adio_fops},
2750 {"debug", &debug_fops},
2751 {"def_reserved_size", &dressz_fops},
2752 {"device_hdr", &devhdr_fops},
2753 {"devices", &dev_fops},
2754 {"device_strs", &devstrs_fops},
2755 {"version", &version_fops}
2763 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2764 struct proc_dir_entry *pdep;
2765 struct sg_proc_leaf * leaf;
2767 sg_proc_sgp = create_proc_entry(sg_proc_sg_dirname,
2768 S_IFDIR | S_IRUGO | S_IXUGO, NULL);
2771 for (k = 0; k < num_leaves; ++k) {
2772 leaf = &sg_proc_leaf_arr[k];
2773 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2774 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2776 leaf->fops->owner = THIS_MODULE,
2777 leaf->fops->read = seq_read,
2778 leaf->fops->llseek = seq_lseek,
2779 pdep->proc_fops = leaf->fops;
2786 sg_proc_cleanup(void)
2790 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2794 for (k = 0; k < num_leaves; ++k)
2795 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2796 remove_proc_entry(sg_proc_sg_dirname, NULL);
2800 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2802 seq_printf(s, "%d\n", *((int *)s->private));
2806 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2808 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2812 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2813 size_t count, loff_t *off)
2818 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2820 num = (count < 10) ? count : 10;
2821 if (copy_from_user(buff, buffer, num))
2824 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2828 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2830 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2834 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2835 size_t count, loff_t *off)
2838 unsigned long k = ULONG_MAX;
2841 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2843 num = (count < 10) ? count : 10;
2844 if (copy_from_user(buff, buffer, num))
2847 k = simple_strtoul(buff, NULL, 10);
2848 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2855 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2857 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2862 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2864 return single_open(file, sg_proc_seq_show_version, NULL);
2867 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2869 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2874 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2876 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2879 struct sg_proc_deviter {
2884 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2886 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2890 if (NULL == sg_dev_arr)
2893 it->max = sg_last_dev();
2894 if (it->index >= it->max)
2902 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2904 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2907 return (it->index < it->max) ? it : NULL;
2910 static void dev_seq_stop(struct seq_file *s, void *v)
2915 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2917 return seq_open(file, &dev_seq_ops);
2920 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2922 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2924 struct scsi_device *scsidp;
2926 sdp = it ? sg_get_dev(it->index) : NULL;
2927 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2928 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2929 scsidp->host->host_no, scsidp->channel,
2930 scsidp->id, scsidp->lun, (int) scsidp->type,
2932 (int) scsidp->queue_depth,
2933 (int) scsidp->device_busy,
2934 (int) scsi_device_online(scsidp));
2936 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2940 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2942 return seq_open(file, &devstrs_seq_ops);
2945 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2947 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2949 struct scsi_device *scsidp;
2951 sdp = it ? sg_get_dev(it->index) : NULL;
2952 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2953 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2954 scsidp->vendor, scsidp->model, scsidp->rev);
2956 seq_printf(s, "<no active device>\n");
2960 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2962 int k, m, new_interface, blen, usg;
2965 const sg_io_hdr_t *hp;
2968 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2969 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2970 "(res)sgat=%d low_dma=%d\n", k + 1,
2971 jiffies_to_msecs(fp->timeout),
2972 fp->reserve.bufflen,
2973 (int) fp->reserve.k_use_sg,
2975 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2976 (int) fp->cmd_q, (int) fp->force_packid,
2977 (int) fp->keep_orphan, (int) fp->closed);
2978 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2980 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2981 if (srp->res_used) {
2982 if (new_interface &&
2983 (SG_FLAG_MMAP_IO & hp->flags))
2988 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2994 blen = srp->my_cmdp ?
2995 srp->my_cmdp->sr_bufflen : srp->data.bufflen;
2996 usg = srp->my_cmdp ?
2997 srp->my_cmdp->sr_use_sg : srp->data.k_use_sg;
2998 seq_printf(s, srp->done ?
2999 ((1 == srp->done) ? "rcv:" : "fin:")
3000 : (srp->my_cmdp ? "act:" : "prior:"));
3001 seq_printf(s, " id=%d blen=%d",
3002 srp->header.pack_id, blen);
3004 seq_printf(s, " dur=%d", hp->duration);
3006 seq_printf(s, " t_o/elap=%d/%d",
3007 new_interface ? hp->timeout : jiffies_to_msecs(fp->timeout),
3008 jiffies_to_msecs(hp->duration ? (jiffies - hp->duration) : 0));
3009 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
3010 (int) srp->data.cmd_opcode);
3013 seq_printf(s, " No requests active\n");
3017 static int sg_proc_open_debug(struct inode *inode, struct file *file)
3019 return seq_open(file, &debug_seq_ops);
3022 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
3024 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3027 if (it && (0 == it->index)) {
3028 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
3029 "(origin 1)\n", sg_dev_max, (int)it->max);
3030 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
3032 sdp = it ? sg_get_dev(it->index) : NULL;
3034 struct scsi_device *scsidp = sdp->device;
3036 if (NULL == scsidp) {
3037 seq_printf(s, "device %d detached ??\n",
3042 if (sg_get_nth_sfp(sdp, 0)) {
3043 seq_printf(s, " >>> device=%s ",
3044 sdp->disk->disk_name);
3046 seq_printf(s, "detached pending close ");
3049 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
3050 scsidp->host->host_no,
3051 scsidp->channel, scsidp->id,
3053 scsidp->host->hostt->emulated);
3054 seq_printf(s, " sg_tablesize=%d excl=%d\n",
3055 sdp->sg_tablesize, sdp->exclude);
3057 sg_proc_debug_helper(s, sdp);
3062 #endif /* CONFIG_SCSI_PROC_FS */
3064 module_init(init_sg);
3065 module_exit(exit_sg);
3066 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);