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 nonseekable_open(inode, filp);
238 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
239 sdp = sg_get_dev(dev);
240 if ((!sdp) || (!sdp->device))
245 /* This driver's module count bumped by fops_get in <linux/fs.h> */
246 /* Prevent the device driver from vanishing while we sleep */
247 retval = scsi_device_get(sdp->device);
251 if (!((flags & O_NONBLOCK) ||
252 scsi_block_when_processing_errors(sdp->device))) {
254 /* we are in error recovery for this device */
258 if (flags & O_EXCL) {
259 if (O_RDONLY == (flags & O_ACCMODE)) {
260 retval = -EPERM; /* Can't lock it with read only access */
263 if (sdp->headfp && (flags & O_NONBLOCK)) {
268 __wait_event_interruptible(sdp->o_excl_wait,
269 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
271 retval = res; /* -ERESTARTSYS because signal hit process */
274 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
275 if (flags & O_NONBLOCK) {
280 __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
283 retval = res; /* -ERESTARTSYS because signal hit process */
291 if (!sdp->headfp) { /* no existing opens on this device */
293 sdp->sg_tablesize = sdp->device->host->sg_tablesize;
295 if ((sfp = sg_add_sfp(sdp, dev)))
296 filp->private_data = sfp;
299 sdp->exclude = 0; /* undo if error */
306 scsi_device_put(sdp->device);
310 /* Following function was formerly called 'sg_close' */
312 sg_release(struct inode *inode, struct file *filp)
317 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
319 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
320 sg_fasync(-1, filp, 0); /* remove filp from async notification list */
321 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */
322 if (!sdp->detached) {
323 scsi_device_put(sdp->device);
326 wake_up_interruptible(&sdp->o_excl_wait);
332 sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
338 int req_pack_id = -1;
339 struct sg_header old_hdr;
343 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
345 SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
346 sdp->disk->disk_name, (int) count));
347 if ((k = verify_area(VERIFY_WRITE, buf, count)))
349 if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
350 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
352 if (old_hdr.reply_len < 0) {
353 if (count >= SZ_SG_IO_HDR) {
355 (&new_hdr, buf, SZ_SG_IO_HDR))
357 req_pack_id = new_hdr.pack_id;
360 req_pack_id = old_hdr.pack_id;
362 srp = sg_get_rq_mark(sfp, req_pack_id);
363 if (!srp) { /* now wait on packet to arrive */
366 if (filp->f_flags & O_NONBLOCK)
369 res = 0; /* following is a macro that beats race condition */
370 __wait_event_interruptible(sfp->read_wait,
371 (sdp->detached || (srp = sg_get_rq_mark(sfp, req_pack_id))),
377 return res; /* -ERESTARTSYS because signal hit process */
380 if (srp->header.interface_id != '\0')
381 return sg_new_read(sfp, buf, count, srp);
384 memset(&old_hdr, 0, SZ_SG_HEADER);
385 old_hdr.reply_len = (int) hp->timeout;
386 old_hdr.pack_len = old_hdr.reply_len; /* very old, strange behaviour */
387 old_hdr.pack_id = hp->pack_id;
388 old_hdr.twelve_byte =
389 ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
390 old_hdr.target_status = hp->masked_status;
391 old_hdr.host_status = hp->host_status;
392 old_hdr.driver_status = hp->driver_status;
393 if ((CHECK_CONDITION & hp->masked_status) ||
394 (DRIVER_SENSE & hp->driver_status))
395 memcpy(old_hdr.sense_buffer, srp->sense_b,
396 sizeof (old_hdr.sense_buffer));
397 switch (hp->host_status) {
398 /* This setup of 'result' is for backward compatibility and is best
399 ignored by the user who should use target, host + driver status */
401 case DID_PASSTHROUGH:
408 old_hdr.result = EBUSY;
415 old_hdr.result = EIO;
418 old_hdr.result = (srp->sense_b[0] == 0 &&
419 hp->masked_status == GOOD) ? 0 : EIO;
422 old_hdr.result = EIO;
426 /* Now copy the result back to the user buffer. */
427 if (count >= SZ_SG_HEADER) {
428 if (__copy_to_user(buf, &old_hdr, SZ_SG_HEADER))
431 if (count > old_hdr.reply_len)
432 count = old_hdr.reply_len;
433 if (count > SZ_SG_HEADER) {
435 sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)))
439 count = (old_hdr.result == 0) ? 0 : -EIO;
440 sg_finish_rem_req(srp);
445 sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
447 sg_io_hdr_t *hp = &srp->header;
451 if (count < SZ_SG_IO_HDR) {
456 if ((hp->mx_sb_len > 0) && hp->sbp) {
457 if ((CHECK_CONDITION & hp->masked_status) ||
458 (DRIVER_SENSE & hp->driver_status)) {
459 int sb_len = sizeof (dummy_cmdp->sr_sense_buffer);
460 sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
461 len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
462 len = (len > sb_len) ? sb_len : len;
463 if (copy_to_user(hp->sbp, srp->sense_b, len)) {
470 if (hp->masked_status || hp->host_status || hp->driver_status)
471 hp->info |= SG_INFO_CHECK;
472 if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
476 err = sg_read_xfer(srp);
478 sg_finish_rem_req(srp);
479 return (0 == err) ? count : err;
483 sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
485 int mxsize, cmd_size, k;
486 int input_size, blocking;
487 unsigned char opcode;
491 struct sg_header old_hdr;
493 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
495 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
497 SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
498 sdp->disk->disk_name, (int) count));
501 if (!((filp->f_flags & O_NONBLOCK) ||
502 scsi_block_when_processing_errors(sdp->device)))
505 if ((k = verify_area(VERIFY_READ, buf, count)))
506 return k; /* protects following copy_from_user()s + get_user()s */
507 if (count < SZ_SG_HEADER)
509 if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
511 blocking = !(filp->f_flags & O_NONBLOCK);
512 if (old_hdr.reply_len < 0)
513 return sg_new_write(sfp, buf, count, blocking, 0, NULL);
514 if (count < (SZ_SG_HEADER + 6))
515 return -EIO; /* The minimum scsi command length is 6 bytes. */
517 if (!(srp = sg_add_request(sfp))) {
518 SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
522 __get_user(opcode, buf);
523 if (sfp->next_cmd_len > 0) {
524 if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
525 SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
526 sfp->next_cmd_len = 0;
527 sg_remove_request(sfp, srp);
530 cmd_size = sfp->next_cmd_len;
531 sfp->next_cmd_len = 0; /* reset so only this write() effected */
533 cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
534 if ((opcode >= 0xc0) && old_hdr.twelve_byte)
537 SCSI_LOG_TIMEOUT(4, printk(
538 "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
539 /* Determine buffer size. */
540 input_size = count - cmd_size;
541 mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
542 mxsize -= SZ_SG_HEADER;
543 input_size -= SZ_SG_HEADER;
544 if (input_size < 0) {
545 sg_remove_request(sfp, srp);
546 return -EIO; /* User did not pass enough bytes for this command. */
549 hp->interface_id = '\0'; /* indicator of old interface tunnelled */
550 hp->cmd_len = (unsigned char) cmd_size;
554 hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
555 SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
557 hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
558 hp->dxfer_len = mxsize;
559 hp->dxferp = (char __user *)buf + cmd_size;
561 hp->timeout = old_hdr.reply_len; /* structure abuse ... */
562 hp->flags = input_size; /* structure abuse ... */
563 hp->pack_id = old_hdr.pack_id;
565 if (__copy_from_user(cmnd, buf, cmd_size))
567 k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
568 return (k < 0) ? k : count;
572 sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
573 int blocking, int read_only, Sg_request ** o_srp)
578 unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
581 if (count < SZ_SG_IO_HDR)
583 if ((k = verify_area(VERIFY_READ, buf, count)))
584 return k; /* protects following copy_from_user()s + get_user()s */
586 sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
587 if (!(srp = sg_add_request(sfp))) {
588 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
592 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
593 sg_remove_request(sfp, srp);
596 if (hp->interface_id != 'S') {
597 sg_remove_request(sfp, srp);
600 if (hp->flags & SG_FLAG_MMAP_IO) {
601 if (hp->dxfer_len > sfp->reserve.bufflen) {
602 sg_remove_request(sfp, srp);
603 return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
605 if (hp->flags & SG_FLAG_DIRECT_IO) {
606 sg_remove_request(sfp, srp);
607 return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
609 if (sg_res_in_use(sfp)) {
610 sg_remove_request(sfp, srp);
611 return -EBUSY; /* reserve buffer already being used */
614 timeout = sg_ms_to_jif(srp->header.timeout);
615 if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
616 sg_remove_request(sfp, srp);
619 if ((k = verify_area(VERIFY_READ, hp->cmdp, hp->cmd_len))) {
620 sg_remove_request(sfp, srp);
621 return k; /* protects following copy_from_user()s + get_user()s */
623 if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
624 sg_remove_request(sfp, srp);
628 (!sg_allow_access(cmnd[0], sfp->parentdp->device->type))) {
629 sg_remove_request(sfp, srp);
632 k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
641 sg_common_write(Sg_fd * sfp, Sg_request * srp,
642 unsigned char *cmnd, int timeout, int blocking)
646 Sg_device *sdp = sfp->parentdp;
647 sg_io_hdr_t *hp = &srp->header;
650 srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
652 hp->masked_status = 0;
656 hp->driver_status = 0;
658 SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
659 (int) cmnd[0], (int) hp->cmd_len));
661 if ((k = sg_start_req(srp))) {
662 SCSI_LOG_TIMEOUT(1, printk("sg_write: start_req err=%d\n", k));
663 sg_finish_rem_req(srp);
664 return k; /* probably out of space --> ENOMEM */
666 if ((k = sg_write_xfer(srp))) {
667 SCSI_LOG_TIMEOUT(1, printk("sg_write: write_xfer, bad address\n"));
668 sg_finish_rem_req(srp);
672 sg_finish_rem_req(srp);
675 SRpnt = scsi_allocate_request(sdp->device, GFP_ATOMIC);
677 SCSI_LOG_TIMEOUT(1, printk("sg_write: no mem\n"));
678 sg_finish_rem_req(srp);
682 srp->my_cmdp = SRpnt;
683 q = SRpnt->sr_device->request_queue;
684 SRpnt->sr_request->rq_disk = sdp->disk;
685 SRpnt->sr_sense_buffer[0] = 0;
686 SRpnt->sr_cmd_len = hp->cmd_len;
687 SRpnt->sr_use_sg = srp->data.k_use_sg;
688 SRpnt->sr_sglist_len = srp->data.sglist_len;
689 SRpnt->sr_bufflen = srp->data.bufflen;
690 SRpnt->sr_underflow = 0;
691 SRpnt->sr_buffer = srp->data.buffer;
692 switch (hp->dxfer_direction) {
693 case SG_DXFER_TO_FROM_DEV:
694 case SG_DXFER_FROM_DEV:
695 SRpnt->sr_data_direction = SCSI_DATA_READ;
697 case SG_DXFER_TO_DEV:
698 SRpnt->sr_data_direction = SCSI_DATA_WRITE;
700 case SG_DXFER_UNKNOWN:
701 SRpnt->sr_data_direction = SCSI_DATA_UNKNOWN;
704 SRpnt->sr_data_direction = SCSI_DATA_NONE;
707 SRpnt->upper_private_data = srp;
708 srp->data.k_use_sg = 0;
709 srp->data.sglist_len = 0;
710 srp->data.bufflen = 0;
711 srp->data.buffer = NULL;
712 hp->duration = jiffies; /* unit jiffies now, millisecs after done */
713 /* Now send everything of to mid-level. The next time we hear about this
714 packet is when sg_cmd_done() is called (i.e. a callback). */
715 scsi_do_req(SRpnt, (void *) cmnd,
716 (void *) SRpnt->sr_buffer, hp->dxfer_len,
717 sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
718 /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
722 static inline unsigned
723 sg_jif_to_ms(int jifs)
728 unsigned int j = (unsigned int) jifs;
730 (UINT_MAX / 1000)) ? ((j * 1000) / HZ) : ((j / HZ) *
736 sg_ioctl(struct inode *inode, struct file *filp,
737 unsigned int cmd_in, unsigned long arg)
739 void __user *p = (void __user *)arg;
741 int result, val, read_only;
745 unsigned long iflags;
747 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
749 SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
750 sdp->disk->disk_name, (int) cmd_in));
751 read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
756 int blocking = 1; /* ignore O_NONBLOCK flag */
760 if (!scsi_block_when_processing_errors(sdp->device))
762 result = verify_area(VERIFY_WRITE, p, SZ_SG_IO_HDR);
766 sg_new_write(sfp, p, SZ_SG_IO_HDR,
767 blocking, read_only, &srp);
770 srp->sg_io_owned = 1;
772 result = 0; /* following macro to beat race condition */
773 __wait_event_interruptible(sfp->read_wait,
774 (sdp->detached || sfp->closed || srp->done),
779 return 0; /* request packet dropped already */
783 return result; /* -ERESTARTSYS because signal hit process */
786 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
787 return (result < 0) ? result : 0;
790 result = get_user(val, ip);
795 if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
796 val = MULDIV (INT_MAX, USER_HZ, HZ);
797 sfp->timeout_user = val;
798 sfp->timeout = MULDIV (val, HZ, USER_HZ);
801 case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
802 /* strange ..., for backward compatibility */
803 return sfp->timeout_user;
804 case SG_SET_FORCE_LOW_DMA:
805 result = get_user(val, ip);
810 if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
811 val = (int) sfp->reserve.bufflen;
812 sg_remove_scat(&sfp->reserve);
813 sg_build_reserve(sfp, val);
818 sfp->low_dma = sdp->device->host->unchecked_isa_dma;
822 return put_user((int) sfp->low_dma, ip);
825 verify_area(VERIFY_WRITE, p, sizeof (sg_scsi_id_t));
829 sg_scsi_id_t __user *sg_idp = p;
833 __put_user((int) sdp->device->host->host_no,
835 __put_user((int) sdp->device->channel,
837 __put_user((int) sdp->device->id, &sg_idp->scsi_id);
838 __put_user((int) sdp->device->lun, &sg_idp->lun);
839 __put_user((int) sdp->device->type, &sg_idp->scsi_type);
840 __put_user((short) sdp->device->host->cmd_per_lun,
841 &sg_idp->h_cmd_per_lun);
842 __put_user((short) sdp->device->queue_depth,
843 &sg_idp->d_queue_depth);
844 __put_user(0, &sg_idp->unused[0]);
845 __put_user(0, &sg_idp->unused[1]);
848 case SG_SET_FORCE_PACK_ID:
849 result = get_user(val, ip);
852 sfp->force_packid = val ? 1 : 0;
855 result = verify_area(VERIFY_WRITE, ip, sizeof (int));
858 read_lock_irqsave(&sfp->rq_list_lock, iflags);
859 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
860 if ((1 == srp->done) && (!srp->sg_io_owned)) {
861 read_unlock_irqrestore(&sfp->rq_list_lock,
863 __put_user(srp->header.pack_id, ip);
867 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
870 case SG_GET_NUM_WAITING:
871 read_lock_irqsave(&sfp->rq_list_lock, iflags);
872 for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
873 if ((1 == srp->done) && (!srp->sg_io_owned))
876 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
877 return put_user(val, ip);
878 case SG_GET_SG_TABLESIZE:
879 return put_user(sdp->sg_tablesize, ip);
880 case SG_SET_RESERVED_SIZE:
881 result = get_user(val, ip);
886 if (val != sfp->reserve.bufflen) {
887 if (sg_res_in_use(sfp) || sfp->mmap_called)
889 sg_remove_scat(&sfp->reserve);
890 sg_build_reserve(sfp, val);
893 case SG_GET_RESERVED_SIZE:
894 val = (int) sfp->reserve.bufflen;
895 return put_user(val, ip);
896 case SG_SET_COMMAND_Q:
897 result = get_user(val, ip);
900 sfp->cmd_q = val ? 1 : 0;
902 case SG_GET_COMMAND_Q:
903 return put_user((int) sfp->cmd_q, ip);
904 case SG_SET_KEEP_ORPHAN:
905 result = get_user(val, ip);
908 sfp->keep_orphan = val;
910 case SG_GET_KEEP_ORPHAN:
911 return put_user((int) sfp->keep_orphan, ip);
912 case SG_NEXT_CMD_LEN:
913 result = get_user(val, ip);
916 sfp->next_cmd_len = (val > 0) ? val : 0;
918 case SG_GET_VERSION_NUM:
919 return put_user(sg_version_num, ip);
920 case SG_GET_ACCESS_COUNT:
921 /* faked - we don't have a real access count anymore */
922 val = (sdp->device ? 1 : 0);
923 return put_user(val, ip);
924 case SG_GET_REQUEST_TABLE:
925 result = verify_area(VERIFY_WRITE, p,
926 SZ_SG_REQ_INFO * SG_MAX_QUEUE);
930 sg_req_info_t rinfo[SG_MAX_QUEUE];
932 read_lock_irqsave(&sfp->rq_list_lock, iflags);
933 for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
934 ++val, srp = srp ? srp->nextrp : srp) {
935 memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
937 rinfo[val].req_state = srp->done + 1;
939 srp->header.masked_status &
940 srp->header.host_status &
941 srp->header.driver_status;
942 rinfo[val].duration =
943 srp->done ? srp->header.duration :
945 jiffies - srp->header.duration);
946 rinfo[val].orphan = srp->orphan;
947 rinfo[val].sg_io_owned = srp->sg_io_owned;
948 rinfo[val].pack_id = srp->header.pack_id;
949 rinfo[val].usr_ptr = srp->header.usr_ptr;
952 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
953 return (__copy_to_user(p, rinfo,
954 SZ_SG_REQ_INFO * SG_MAX_QUEUE) ? -EFAULT : 0);
956 case SG_EMULATED_HOST:
959 return put_user(sdp->device->host->hostt->emulated, ip);
963 if (filp->f_flags & O_NONBLOCK) {
964 if (test_bit(SHOST_RECOVERY,
965 &sdp->device->host->shost_state))
967 } else if (!scsi_block_when_processing_errors(sdp->device))
969 result = get_user(val, ip);
972 if (SG_SCSI_RESET_NOTHING == val)
975 case SG_SCSI_RESET_DEVICE:
976 val = SCSI_TRY_RESET_DEVICE;
978 case SG_SCSI_RESET_BUS:
979 val = SCSI_TRY_RESET_BUS;
981 case SG_SCSI_RESET_HOST:
982 val = SCSI_TRY_RESET_HOST;
987 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
989 return (scsi_reset_provider(sdp->device, val) ==
991 case SCSI_IOCTL_SEND_COMMAND:
995 unsigned char opcode = WRITE_6;
996 Scsi_Ioctl_Command __user *siocp = p;
998 if (copy_from_user(&opcode, siocp->data, 1))
1000 if (!sg_allow_access(opcode, sdp->device->type))
1003 return scsi_ioctl_send_command(sdp->device, p);
1005 result = get_user(val, ip);
1008 sdp->sgdebug = (char) val;
1010 case SCSI_IOCTL_GET_IDLUN:
1011 case SCSI_IOCTL_GET_BUS_NUMBER:
1012 case SCSI_IOCTL_PROBE_HOST:
1013 case SG_GET_TRANSFORM:
1016 return scsi_ioctl(sdp->device, cmd_in, p);
1019 return -EPERM; /* don't know so take safe approach */
1020 return scsi_ioctl(sdp->device, cmd_in, p);
1025 sg_poll(struct file *filp, poll_table * wait)
1027 unsigned int res = 0;
1032 unsigned long iflags;
1034 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
1037 poll_wait(filp, &sfp->read_wait, wait);
1038 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1039 for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1040 /* if any read waiting, flag it */
1041 if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
1042 res = POLLIN | POLLRDNORM;
1045 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1049 else if (!sfp->cmd_q) {
1051 res |= POLLOUT | POLLWRNORM;
1052 } else if (count < SG_MAX_QUEUE)
1053 res |= POLLOUT | POLLWRNORM;
1054 SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
1055 sdp->disk->disk_name, (int) res));
1060 sg_fasync(int fd, struct file *filp, int mode)
1066 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
1068 SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
1069 sdp->disk->disk_name, mode));
1071 retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
1072 return (retval < 0) ? retval : 0;
1075 static inline unsigned char *
1076 sg_scatg2virt(const struct scatterlist *sclp)
1078 return (sclp && sclp->page) ?
1079 (unsigned char *) page_address(sclp->page) + sclp->offset : NULL;
1082 /* When startFinish==1 increments page counts for pages other than the
1083 first of scatter gather elements obtained from __get_free_pages().
1084 When startFinish==0 decrements ... */
1086 sg_rb_correct4mmap(Sg_scatter_hold * rsv_schp, int startFinish)
1092 SCSI_LOG_TIMEOUT(3, printk("sg_rb_correct4mmap: startFinish=%d, scatg=%d\n",
1093 startFinish, rsv_schp->k_use_sg));
1094 /* N.B. correction _not_ applied to base page of each allocation */
1095 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1096 struct scatterlist *sclp = rsv_schp->buffer;
1098 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
1099 for (m = PAGE_SIZE; m < sclp->length; m += PAGE_SIZE) {
1100 page_ptr = sg_scatg2virt(sclp) + m;
1101 page = virt_to_page(page_ptr);
1105 if (page_count(page) > 0)
1110 } else { /* reserve buffer is just a single allocation */
1111 for (m = PAGE_SIZE; m < rsv_schp->bufflen; m += PAGE_SIZE) {
1112 page_ptr = (unsigned char *) rsv_schp->buffer + m;
1113 page = virt_to_page(page_ptr);
1117 if (page_count(page) > 0)
1124 static struct page *
1125 sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1128 struct page *page = NOPAGE_SIGBUS;
1129 void *page_ptr = NULL;
1130 unsigned long offset;
1131 Sg_scatter_hold *rsv_schp;
1133 if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1135 rsv_schp = &sfp->reserve;
1136 offset = addr - vma->vm_start;
1137 if (offset >= rsv_schp->bufflen)
1139 SCSI_LOG_TIMEOUT(3, printk("sg_vma_nopage: offset=%lu, scatg=%d\n",
1140 offset, rsv_schp->k_use_sg));
1141 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1143 unsigned long sa = vma->vm_start;
1145 struct scatterlist *sclp = rsv_schp->buffer;
1147 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1149 len = vma->vm_end - sa;
1150 len = (len < sclp->length) ? len : sclp->length;
1152 page_ptr = sg_scatg2virt(sclp) + offset;
1153 page = virt_to_page(page_ptr);
1154 get_page(page); /* increment page count */
1160 } else { /* reserve buffer is just a single allocation */
1161 page_ptr = (unsigned char *) rsv_schp->buffer + offset;
1162 page = virt_to_page(page_ptr);
1163 get_page(page); /* increment page count */
1166 *type = VM_FAULT_MINOR;
1170 static struct vm_operations_struct sg_mmap_vm_ops = {
1171 .nopage = sg_vma_nopage,
1175 sg_mmap(struct file *filp, struct vm_area_struct *vma)
1178 unsigned long req_sz = vma->vm_end - vma->vm_start;
1179 Sg_scatter_hold *rsv_schp;
1181 if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
1183 SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
1184 (void *) vma->vm_start, (int) req_sz));
1186 return -EINVAL; /* want no offset */
1187 rsv_schp = &sfp->reserve;
1188 if (req_sz > rsv_schp->bufflen)
1189 return -ENOMEM; /* cannot map more than reserved buffer */
1191 if (rsv_schp->k_use_sg) { /* reserve buffer is a scatter gather list */
1193 unsigned long sa = vma->vm_start;
1195 struct scatterlist *sclp = rsv_schp->buffer;
1197 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1199 if (0 != sclp->offset)
1200 return -EFAULT; /* non page aligned memory ?? */
1201 len = vma->vm_end - sa;
1202 len = (len < sclp->length) ? len : sclp->length;
1205 } else { /* reserve buffer is just a single allocation */
1206 if ((unsigned long) rsv_schp->buffer & (PAGE_SIZE - 1))
1207 return -EFAULT; /* non page aligned memory ?? */
1209 if (0 == sfp->mmap_called) {
1210 sg_rb_correct4mmap(rsv_schp, 1); /* do only once per fd lifetime */
1211 sfp->mmap_called = 1;
1213 vma->vm_flags |= (VM_RESERVED | VM_IO);
1214 vma->vm_private_data = sfp;
1215 vma->vm_ops = &sg_mmap_vm_ops;
1219 /* This function is a "bottom half" handler that is called by the
1220 * mid level when a command is completed (or has failed). */
1222 sg_cmd_done(Scsi_Cmnd * SCpnt)
1224 Scsi_Request *SRpnt = NULL;
1225 Sg_device *sdp = NULL;
1227 Sg_request *srp = NULL;
1229 if (SCpnt && (SRpnt = SCpnt->sc_request))
1230 srp = (Sg_request *) SRpnt->upper_private_data;
1232 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1234 scsi_release_request(SRpnt);
1237 sfp = srp->parentfp;
1239 sdp = sfp->parentdp;
1240 if ((NULL == sdp) || sdp->detached) {
1241 printk(KERN_INFO "sg_cmd_done: device detached\n");
1242 scsi_release_request(SRpnt);
1246 /* First transfer ownership of data buffers to sg_device object. */
1247 srp->data.k_use_sg = SRpnt->sr_use_sg;
1248 srp->data.sglist_len = SRpnt->sr_sglist_len;
1249 srp->data.bufflen = SRpnt->sr_bufflen;
1250 srp->data.buffer = SRpnt->sr_buffer;
1251 /* now clear out request structure */
1252 SRpnt->sr_use_sg = 0;
1253 SRpnt->sr_sglist_len = 0;
1254 SRpnt->sr_bufflen = 0;
1255 SRpnt->sr_buffer = NULL;
1256 SRpnt->sr_underflow = 0;
1257 SRpnt->sr_request->rq_disk = NULL; /* "sg" _disowns_ request blk */
1259 srp->my_cmdp = NULL;
1261 SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1262 sdp->disk->disk_name, srp->header.pack_id, (int) SRpnt->sr_result));
1263 srp->header.resid = SCpnt->resid;
1264 /* N.B. unit of duration changes here from jiffies to millisecs */
1265 srp->header.duration =
1266 sg_jif_to_ms(jiffies - (int) srp->header.duration);
1267 if (0 != SRpnt->sr_result) {
1268 memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
1269 sizeof (srp->sense_b));
1270 srp->header.status = 0xff & SRpnt->sr_result;
1271 srp->header.masked_status = status_byte(SRpnt->sr_result);
1272 srp->header.msg_status = msg_byte(SRpnt->sr_result);
1273 srp->header.host_status = host_byte(SRpnt->sr_result);
1274 srp->header.driver_status = driver_byte(SRpnt->sr_result);
1275 if ((sdp->sgdebug > 0) &&
1276 ((CHECK_CONDITION == srp->header.masked_status) ||
1277 (COMMAND_TERMINATED == srp->header.masked_status)))
1278 print_req_sense("sg_cmd_done", SRpnt);
1280 /* Following if statement is a patch supplied by Eric Youngdale */
1281 if (driver_byte(SRpnt->sr_result) != 0
1282 && (SRpnt->sr_sense_buffer[0] & 0x7f) == 0x70
1283 && (SRpnt->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION
1284 && sdp->device->removable) {
1285 /* Detected disc change. Set the bit - this may be used if */
1286 /* there are filesystems using this device. */
1287 sdp->device->changed = 1;
1290 /* Rely on write phase to clean out srp status values, so no "else" */
1292 scsi_release_request(SRpnt);
1294 if (sfp->closed) { /* whoops this fd already released, cleanup */
1295 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
1296 sg_finish_rem_req(srp);
1298 if (NULL == sfp->headrp) {
1299 SCSI_LOG_TIMEOUT(1, printk("sg...bh: already closed, final cleanup\n"));
1300 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1301 scsi_device_put(sdp->device);
1305 } else if (srp && srp->orphan) {
1306 if (sfp->keep_orphan)
1307 srp->sg_io_owned = 0;
1309 sg_finish_rem_req(srp);
1314 /* Now wake up any sg_read() that is waiting for this packet. */
1315 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1317 wake_up_interruptible(&sfp->read_wait);
1321 static struct file_operations sg_fops = {
1322 .owner = THIS_MODULE,
1329 .release = sg_release,
1330 .fasync = sg_fasync,
1333 static struct class_simple * sg_sysfs_class;
1335 static int sg_sysfs_valid = 0;
1337 static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1340 unsigned long iflags;
1341 void *old_sg_dev_arr = NULL;
1344 sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
1346 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1350 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1351 if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
1353 int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
1354 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1356 tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
1357 if (unlikely(!tmp_da))
1360 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1361 memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
1362 memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
1363 old_sg_dev_arr = sg_dev_arr;
1364 sg_dev_arr = tmp_da;
1365 sg_dev_max = tmp_dev_max;
1368 for (k = 0; k < sg_dev_max; k++)
1371 if (unlikely(k >= SG_MAX_DEVS))
1374 memset(sdp, 0, sizeof(*sdp));
1375 SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
1376 sprintf(disk->disk_name, "sg%d", k);
1377 disk->first_minor = k;
1379 sdp->device = scsidp;
1380 init_waitqueue_head(&sdp->o_excl_wait);
1381 sdp->sg_tablesize = scsidp->host ? scsidp->host->sg_tablesize : 0;
1384 sg_dev_arr[k] = sdp;
1385 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1391 kfree(old_sg_dev_arr);
1395 printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
1400 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1402 "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
1403 "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
1404 scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
1410 sg_add(struct class_device *cl_dev)
1412 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1413 struct gendisk *disk;
1414 Sg_device *sdp = NULL;
1415 struct cdev * cdev = NULL;
1418 disk = alloc_disk(1);
1420 printk(KERN_WARNING "alloc_disk failed\n");
1423 disk->major = SCSI_GENERIC_MAJOR;
1426 cdev = cdev_alloc();
1428 printk(KERN_WARNING "cdev_alloc failed\n");
1431 cdev->owner = THIS_MODULE;
1432 cdev->ops = &sg_fops;
1434 error = sg_alloc(disk, scsidp);
1436 printk(KERN_WARNING "sg_alloc failed\n");
1440 sdp = sg_dev_arr[k];
1442 devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
1443 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
1444 "%s/generic", scsidp->devfs_name);
1445 error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, k), 1);
1447 devfs_remove("%s/generic", scsidp->devfs_name);
1451 if (sg_sysfs_valid) {
1452 struct class_device * sg_class_member;
1454 sg_class_member = class_simple_device_add(sg_sysfs_class,
1455 MKDEV(SCSI_GENERIC_MAJOR, k),
1458 if (IS_ERR(sg_class_member))
1459 printk(KERN_WARNING "sg_add: "
1460 "class_simple_device_add failed\n");
1461 class_set_devdata(sg_class_member, sdp);
1462 error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
1463 &sg_class_member->kobj, "generic");
1465 printk(KERN_ERR "sg_add: unable to make symlink "
1466 "'generic' back to sg%d\n", k);
1468 printk(KERN_WARNING "sg_add: sg_sys INvalid\n");
1471 "Attached scsi generic sg%d at scsi%d, channel"
1472 " %d, id %d, lun %d, type %d\n", k,
1473 scsidp->host->host_no, scsidp->channel, scsidp->id,
1474 scsidp->lun, scsidp->type);
1486 sg_remove(struct class_device *cl_dev)
1488 struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
1489 Sg_device *sdp = NULL;
1490 unsigned long iflags;
1497 if (NULL == sg_dev_arr)
1500 write_lock_irqsave(&sg_dev_arr_lock, iflags);
1501 for (k = 0; k < sg_dev_max; k++) {
1502 sdp = sg_dev_arr[k];
1503 if ((NULL == sdp) || (sdp->device != scsidp))
1504 continue; /* dirty but lowers nesting */
1507 for (sfp = sdp->headfp; sfp; sfp = tsfp) {
1509 for (srp = sfp->headrp; srp; srp = tsrp) {
1511 if (sfp->closed || (0 == srp->done))
1512 sg_finish_rem_req(srp);
1515 scsi_device_put(sdp->device);
1516 __sg_remove_sfp(sdp, sfp);
1519 wake_up_interruptible(&sfp->read_wait);
1520 kill_fasync(&sfp->async_qp, SIGPOLL,
1524 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d, dirty\n", k));
1525 if (NULL == sdp->headfp) {
1526 sg_dev_arr[k] = NULL;
1528 } else { /* nothing active, simple case */
1529 SCSI_LOG_TIMEOUT(3, printk("sg_detach: dev=%d\n", k));
1530 sg_dev_arr[k] = NULL;
1535 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
1538 sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1539 class_simple_device_remove(MKDEV(SCSI_GENERIC_MAJOR, k));
1540 cdev_del(sdp->cdev);
1542 devfs_remove("%s/generic", scsidp->devfs_name);
1543 put_disk(sdp->disk);
1545 if (NULL == sdp->headfp)
1546 kfree((char *) sdp);
1550 scsi_sleep(2); /* dirty detach so delay device destruction */
1553 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1554 * of sysfs parameters (which module_param doesn't yet support).
1555 * Sysfs parameters defined explicitly below.
1557 module_param_named(def_reserved_size, def_reserved_size, int, 0);
1558 module_param_named(allow_dio, sg_allow_dio, int, 0);
1560 MODULE_AUTHOR("Douglas Gilbert");
1561 MODULE_DESCRIPTION("SCSI generic (sg) driver");
1562 MODULE_LICENSE("GPL");
1563 MODULE_VERSION(SG_VERSION_STR);
1565 MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
1566 MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
1573 if (def_reserved_size >= 0)
1574 sg_big_buff = def_reserved_size;
1576 rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1580 sg_sysfs_class = class_simple_create(THIS_MODULE, "scsi_generic");
1581 if ( IS_ERR(sg_sysfs_class) ) {
1582 rc = PTR_ERR(sg_sysfs_class);
1586 rc = scsi_register_interface(&sg_interface);
1588 #ifdef CONFIG_SCSI_PROC_FS
1590 #endif /* CONFIG_SCSI_PROC_FS */
1593 class_simple_destroy(sg_sysfs_class);
1595 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
1602 #ifdef CONFIG_SCSI_PROC_FS
1604 #endif /* CONFIG_SCSI_PROC_FS */
1605 scsi_unregister_interface(&sg_interface);
1606 class_simple_destroy(sg_sysfs_class);
1608 unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
1610 if (sg_dev_arr != NULL) {
1611 kfree((char *) sg_dev_arr);
1618 sg_start_req(Sg_request * srp)
1621 Sg_fd *sfp = srp->parentfp;
1622 sg_io_hdr_t *hp = &srp->header;
1623 int dxfer_len = (int) hp->dxfer_len;
1624 int dxfer_dir = hp->dxfer_direction;
1625 Sg_scatter_hold *req_schp = &srp->data;
1626 Sg_scatter_hold *rsv_schp = &sfp->reserve;
1628 SCSI_LOG_TIMEOUT(4, printk("sg_start_req: dxfer_len=%d\n", dxfer_len));
1629 if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
1631 if (sg_allow_dio && (hp->flags & SG_FLAG_DIRECT_IO) &&
1632 (dxfer_dir != SG_DXFER_UNKNOWN) && (0 == hp->iovec_count) &&
1633 (!sfp->parentdp->device->host->unchecked_isa_dma)) {
1634 res = sg_build_direct(srp, sfp, dxfer_len);
1635 if (res <= 0) /* -ve -> error, 0 -> done, 1 -> try indirect */
1638 if ((!sg_res_in_use(sfp)) && (dxfer_len <= rsv_schp->bufflen))
1639 sg_link_reserve(sfp, srp, dxfer_len);
1641 res = sg_build_indirect(req_schp, sfp, dxfer_len);
1643 sg_remove_scat(req_schp);
1651 sg_finish_rem_req(Sg_request * srp)
1653 Sg_fd *sfp = srp->parentfp;
1654 Sg_scatter_hold *req_schp = &srp->data;
1656 SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
1658 sg_unlink_reserve(sfp, srp);
1660 sg_remove_scat(req_schp);
1661 sg_remove_request(sfp, srp);
1665 sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
1668 int elem_sz = sizeof (struct scatterlist);
1669 int sg_bufflen = tablesize * elem_sz;
1670 int mx_sc_elems = tablesize;
1672 schp->buffer = sg_page_malloc(sg_bufflen, sfp->low_dma, &ret_sz);
1675 else if (ret_sz != sg_bufflen) {
1676 sg_bufflen = ret_sz;
1677 mx_sc_elems = sg_bufflen / elem_sz;
1679 schp->sglist_len = sg_bufflen;
1680 memset(schp->buffer, 0, sg_bufflen);
1681 return mx_sc_elems; /* number of scat_gath elements allocated */
1684 #ifdef SG_ALLOW_DIO_CODE
1685 /* vvvvvvvv following code borrowed from st driver's direct IO vvvvvvvvv */
1686 /* hopefully this generic code will moved to a library */
1688 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
1689 - mapping of all pages not successful
1690 - any page is above max_pfn
1691 (i.e., either completely successful or fails)
1694 st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
1695 unsigned long uaddr, size_t count, int rw,
1696 unsigned long max_pfn)
1699 unsigned int nr_pages;
1700 struct page **pages;
1702 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
1704 /* User attempted Overflow! */
1705 if ((uaddr + count) < uaddr)
1709 if (nr_pages > max_pages)
1716 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_ATOMIC)) == NULL)
1719 /* Try to fault in all of the necessary pages */
1720 down_read(¤t->mm->mmap_sem);
1721 /* rw==READ means read from drive, write into memory area */
1722 res = get_user_pages(
1728 0, /* don't force */
1731 up_read(¤t->mm->mmap_sem);
1733 /* Errors and no page mapped should return here */
1737 for (i=0; i < nr_pages; i++) {
1738 /* FIXME: flush superflous for rw==READ,
1739 * probably wrong function for rw==WRITE
1741 flush_dcache_page(pages[i]);
1742 if (page_to_pfn(pages[i]) > max_pfn)
1744 /* ?? Is locking needed? I don't think so */
1745 /* if (TestSetPageLocked(pages[i]))
1749 /* Populate the scatter/gather list */
1750 sgl[0].page = pages[0];
1751 sgl[0].offset = uaddr & ~PAGE_MASK;
1753 sgl[0].length = PAGE_SIZE - sgl[0].offset;
1754 count -= sgl[0].length;
1755 for (i=1; i < nr_pages ; i++) {
1757 sgl[i].page = pages[i];
1758 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
1763 sgl[0].length = count;
1770 /* for (j=0; j < i; j++)
1771 unlock_page(pages[j]); */
1775 for (j=0; j < res; j++)
1776 page_cache_release(pages[j]);
1782 /* And unmap them... */
1784 st_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
1789 for (i=0; i < nr_pages; i++) {
1790 if (dirtied && !PageReserved(sgl[i].page))
1791 SetPageDirty(sgl[i].page);
1792 /* unlock_page(sgl[i].page); */
1793 /* FIXME: cache flush missing for rw==READ
1794 * FIXME: call the correct reference counting function
1796 page_cache_release(sgl[i].page);
1802 /* ^^^^^^^^ above code borrowed from st driver's direct IO ^^^^^^^^^ */
1806 /* Returns: -ve -> error, 0 -> done, 1 -> try indirect */
1808 sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len)
1810 #ifdef SG_ALLOW_DIO_CODE
1811 sg_io_hdr_t *hp = &srp->header;
1812 Sg_scatter_hold *schp = &srp->data;
1813 int sg_tablesize = sfp->parentdp->sg_tablesize;
1814 struct scatterlist *sgl;
1815 int mx_sc_elems, res;
1816 struct scsi_device *sdev = sfp->parentdp->device;
1818 if (((unsigned long)hp->dxferp &
1819 queue_dma_alignment(sdev->request_queue)) != 0)
1821 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1822 if (mx_sc_elems <= 0) {
1825 sgl = (struct scatterlist *)schp->buffer;
1826 res = st_map_user_pages(sgl, mx_sc_elems, (unsigned long)hp->dxferp, dxfer_len,
1827 (SG_DXFER_TO_DEV == hp->dxfer_direction) ? 1 : 0, ULONG_MAX);
1830 schp->k_use_sg = res;
1831 schp->dio_in_use = 1;
1832 hp->info |= SG_INFO_DIRECT_IO;
1840 sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1843 int blk_size = buff_size;
1844 unsigned char *p = NULL;
1846 if ((blk_size < 0) || (!sfp))
1849 ++blk_size; /* don't know why */
1850 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1851 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
1852 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1853 buff_size, blk_size));
1854 if (blk_size <= SG_SCATTER_SZ) {
1855 p = sg_page_malloc(blk_size, sfp->low_dma, &ret_sz);
1858 if (blk_size == ret_sz) { /* got it on the first attempt */
1861 schp->bufflen = blk_size;
1862 schp->b_malloc_len = blk_size;
1866 p = sg_page_malloc(SG_SCATTER_SZ, sfp->low_dma, &ret_sz);
1870 /* Want some local declarations, so start new block ... */
1871 { /* lets try and build a scatter gather list */
1872 struct scatterlist *sclp;
1875 int sg_tablesize = sfp->parentdp->sg_tablesize;
1878 /* N.B. ret_sz carried into this block ... */
1879 mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
1880 if (mx_sc_elems < 0)
1881 return mx_sc_elems; /* most likely -ENOMEM */
1883 for (k = 0, sclp = schp->buffer, rem_sz = blk_size;
1884 (rem_sz > 0) && (k < mx_sc_elems);
1885 ++k, rem_sz -= ret_sz, ++sclp) {
1891 SG_SCATTER_SZ) ? SG_SCATTER_SZ : rem_sz;
1892 p = sg_page_malloc(num, sfp->low_dma, &ret_sz);
1896 sclp->page = virt_to_page(p);
1897 sclp->offset = offset_in_page(p);
1898 sclp->length = ret_sz;
1900 SCSI_LOG_TIMEOUT(5, printk("sg_build_build: k=%d, a=0x%p, len=%d\n",
1901 k, sg_scatg2virt(sclp), ret_sz));
1902 } /* end of for loop */
1904 SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", k, rem_sz));
1905 schp->bufflen = blk_size;
1906 if (rem_sz > 0) /* must have failed */
1913 sg_write_xfer(Sg_request * srp)
1915 sg_io_hdr_t *hp = &srp->header;
1916 Sg_scatter_hold *schp = &srp->data;
1918 int j, k, onum, usglen, ksglen, res;
1919 int iovec_count = (int) hp->iovec_count;
1920 int dxfer_dir = hp->dxfer_direction;
1922 unsigned char __user *up;
1923 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
1925 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
1926 (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
1927 num_xfer = (int) (new_interface ? hp->dxfer_len : hp->flags);
1928 if (schp->bufflen < num_xfer)
1929 num_xfer = schp->bufflen;
1931 if ((num_xfer <= 0) || (schp->dio_in_use) ||
1933 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
1936 SCSI_LOG_TIMEOUT(4, printk("sg_write_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
1937 num_xfer, iovec_count, schp->k_use_sg));
1940 if ((k = verify_area(VERIFY_READ, hp->dxferp,
1941 SZ_SG_IOVEC * onum)))
1946 if (0 == schp->k_use_sg) { /* kernel has single buffer */
1947 for (j = 0, p = schp->buffer; j < onum; ++j) {
1948 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1951 usglen = (num_xfer > usglen) ? usglen : num_xfer;
1952 if (__copy_from_user(p, up, usglen))
1959 } else { /* kernel using scatter gather list */
1960 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
1962 ksglen = (int) sclp->length;
1963 p = sg_scatg2virt(sclp);
1964 for (j = 0, k = 0; j < onum; ++j) {
1965 res = sg_u_iovec(hp, iovec_count, j, 1, &usglen, &up);
1969 for (; p; ++sclp, ksglen = (int) sclp->length,
1970 p = sg_scatg2virt(sclp)) {
1973 if (ksglen > usglen) {
1974 if (usglen >= num_xfer) {
1975 if (__copy_from_user
1980 if (__copy_from_user(p, up, usglen))
1986 if (ksglen >= num_xfer) {
1987 if (__copy_from_user
1992 if (__copy_from_user(p, up, ksglen))
1998 if (k >= schp->k_use_sg)
2007 sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
2008 int wr_xf, int *countp, unsigned char __user **up)
2010 int num_xfer = (int) hp->dxfer_len;
2011 unsigned char __user *p = hp->dxferp;
2015 if (wr_xf && ('\0' == hp->interface_id))
2016 count = (int) hp->flags; /* holds "old" input_size */
2021 if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
2024 count = (int) iovec.iov_len;
2026 if ((k = verify_area(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count)))
2036 sg_remove_scat(Sg_scatter_hold * schp)
2038 SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
2039 if (schp->buffer && (schp->sglist_len > 0)) {
2040 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2042 if (schp->dio_in_use) {
2043 #ifdef SG_ALLOW_DIO_CODE
2044 st_unmap_user_pages(sclp, schp->k_use_sg, TRUE);
2049 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2051 SCSI_LOG_TIMEOUT(5, printk(
2052 "sg_remove_scat: k=%d, a=0x%p, len=%d\n",
2053 k, sg_scatg2virt(sclp), sclp->length));
2054 sg_page_free(sg_scatg2virt(sclp), sclp->length);
2060 sg_page_free(schp->buffer, schp->sglist_len);
2061 } else if (schp->buffer)
2062 sg_page_free(schp->buffer, schp->b_malloc_len);
2063 memset(schp, 0, sizeof (*schp));
2067 sg_read_xfer(Sg_request * srp)
2069 sg_io_hdr_t *hp = &srp->header;
2070 Sg_scatter_hold *schp = &srp->data;
2072 int j, k, onum, usglen, ksglen, res;
2073 int iovec_count = (int) hp->iovec_count;
2074 int dxfer_dir = hp->dxfer_direction;
2076 unsigned char __user *up;
2077 int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
2079 if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
2080 || (SG_DXFER_TO_FROM_DEV == dxfer_dir)) {
2081 num_xfer = hp->dxfer_len;
2082 if (schp->bufflen < num_xfer)
2083 num_xfer = schp->bufflen;
2085 if ((num_xfer <= 0) || (schp->dio_in_use) ||
2087 && ((SG_FLAG_NO_DXFER | SG_FLAG_MMAP_IO) & hp->flags)))
2090 SCSI_LOG_TIMEOUT(4, printk("sg_read_xfer: num_xfer=%d, iovec_count=%d, k_use_sg=%d\n",
2091 num_xfer, iovec_count, schp->k_use_sg));
2094 if ((k = verify_area(VERIFY_READ, hp->dxferp,
2095 SZ_SG_IOVEC * onum)))
2100 if (0 == schp->k_use_sg) { /* kernel has single buffer */
2101 for (j = 0, p = schp->buffer; j < onum; ++j) {
2102 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2105 usglen = (num_xfer > usglen) ? usglen : num_xfer;
2106 if (__copy_to_user(up, p, usglen))
2113 } else { /* kernel using scatter gather list */
2114 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2116 ksglen = (int) sclp->length;
2117 p = sg_scatg2virt(sclp);
2118 for (j = 0, k = 0; j < onum; ++j) {
2119 res = sg_u_iovec(hp, iovec_count, j, 0, &usglen, &up);
2123 for (; p; ++sclp, ksglen = (int) sclp->length,
2124 p = sg_scatg2virt(sclp)) {
2127 if (ksglen > usglen) {
2128 if (usglen >= num_xfer) {
2134 if (__copy_to_user(up, p, usglen))
2140 if (ksglen >= num_xfer) {
2146 if (__copy_to_user(up, p, ksglen))
2152 if (k >= schp->k_use_sg)
2161 sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2163 Sg_scatter_hold *schp = &srp->data;
2165 SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
2167 if ((!outp) || (num_read_xfer <= 0))
2169 if (schp->k_use_sg > 0) {
2171 struct scatterlist *sclp = (struct scatterlist *) schp->buffer;
2173 for (k = 0; (k < schp->k_use_sg) && sg_scatg2virt(sclp);
2175 num = (int) sclp->length;
2176 if (num > num_read_xfer) {
2178 (outp, sg_scatg2virt(sclp), num_read_xfer))
2183 (outp, sg_scatg2virt(sclp), num))
2185 num_read_xfer -= num;
2186 if (num_read_xfer <= 0)
2192 if (__copy_to_user(outp, schp->buffer, num_read_xfer))
2199 sg_build_reserve(Sg_fd * sfp, int req_size)
2201 Sg_scatter_hold *schp = &sfp->reserve;
2203 SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
2205 if (req_size < PAGE_SIZE)
2206 req_size = PAGE_SIZE;
2207 if (0 == sg_build_indirect(schp, sfp, req_size))
2210 sg_remove_scat(schp);
2211 req_size >>= 1; /* divide by 2 */
2212 } while (req_size > (PAGE_SIZE / 2));
2216 sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2218 Sg_scatter_hold *req_schp = &srp->data;
2219 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2222 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2223 size = (size + 1) & (~1); /* round to even for aha1542 */
2224 if (rsv_schp->k_use_sg > 0) {
2227 struct scatterlist *sclp =
2228 (struct scatterlist *) rsv_schp->buffer;
2230 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sclp) {
2231 num = (int) sclp->length;
2234 req_schp->k_use_sg = 0;
2235 req_schp->buffer = sg_scatg2virt(sclp);
2237 sfp->save_scat_len = num;
2238 sclp->length = (unsigned) rem;
2239 req_schp->k_use_sg = k + 1;
2240 req_schp->sglist_len =
2241 rsv_schp->sglist_len;
2242 req_schp->buffer = rsv_schp->buffer;
2244 req_schp->bufflen = size;
2245 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2250 if (k >= rsv_schp->k_use_sg)
2251 SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
2253 req_schp->k_use_sg = 0;
2254 req_schp->bufflen = size;
2255 req_schp->buffer = rsv_schp->buffer;
2256 req_schp->b_malloc_len = rsv_schp->b_malloc_len;
2261 sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
2263 Sg_scatter_hold *req_schp = &srp->data;
2264 Sg_scatter_hold *rsv_schp = &sfp->reserve;
2266 SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
2267 (int) req_schp->k_use_sg));
2268 if ((rsv_schp->k_use_sg > 0) && (req_schp->k_use_sg > 0)) {
2269 struct scatterlist *sclp =
2270 (struct scatterlist *) rsv_schp->buffer;
2272 if (sfp->save_scat_len > 0)
2273 (sclp + (req_schp->k_use_sg - 1))->length =
2274 (unsigned) sfp->save_scat_len;
2276 SCSI_LOG_TIMEOUT(1, printk ("sg_unlink_reserve: BAD save_scat_len\n"));
2278 req_schp->k_use_sg = 0;
2279 req_schp->bufflen = 0;
2280 req_schp->buffer = NULL;
2281 req_schp->sglist_len = 0;
2282 sfp->save_scat_len = 0;
2287 sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2290 unsigned long iflags;
2292 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2293 for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2294 /* look for requests that are ready + not SG_IO owned */
2295 if ((1 == resp->done) && (!resp->sg_io_owned) &&
2296 ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
2297 resp->done = 2; /* guard against other readers */
2301 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2305 #ifdef CONFIG_SCSI_PROC_FS
2307 sg_get_nth_request(Sg_fd * sfp, int nth)
2310 unsigned long iflags;
2313 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2314 for (k = 0, resp = sfp->headrp; resp && (k < nth);
2315 ++k, resp = resp->nextrp) ;
2316 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2321 /* always adds to end of list */
2323 sg_add_request(Sg_fd * sfp)
2326 unsigned long iflags;
2328 Sg_request *rp = sfp->req_arr;
2330 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2333 memset(rp, 0, sizeof (Sg_request));
2338 if (0 == sfp->cmd_q)
2339 resp = NULL; /* command queuing disallowed */
2341 for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2345 if (k < SG_MAX_QUEUE) {
2346 memset(rp, 0, sizeof (Sg_request));
2348 while (resp->nextrp)
2349 resp = resp->nextrp;
2357 resp->nextrp = NULL;
2358 resp->header.duration = jiffies;
2359 resp->my_cmdp = NULL;
2361 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2365 /* Return of 1 for found; 0 for not found */
2367 sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2369 Sg_request *prev_rp;
2371 unsigned long iflags;
2374 if ((!sfp) || (!srp) || (!sfp->headrp))
2376 write_lock_irqsave(&sfp->rq_list_lock, iflags);
2377 prev_rp = sfp->headrp;
2378 if (srp == prev_rp) {
2379 sfp->headrp = prev_rp->nextrp;
2380 prev_rp->parentfp = NULL;
2383 while ((rp = prev_rp->nextrp)) {
2385 prev_rp->nextrp = rp->nextrp;
2386 rp->parentfp = NULL;
2393 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2397 #ifdef CONFIG_SCSI_PROC_FS
2399 sg_get_nth_sfp(Sg_device * sdp, int nth)
2402 unsigned long iflags;
2405 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2406 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2407 ++k, resp = resp->nextfp) ;
2408 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2414 sg_add_sfp(Sg_device * sdp, int dev)
2417 unsigned long iflags;
2419 sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
2422 memset(sfp, 0, sizeof (Sg_fd));
2423 init_waitqueue_head(&sfp->read_wait);
2424 sfp->rq_list_lock = RW_LOCK_UNLOCKED;
2426 sfp->timeout = SG_DEFAULT_TIMEOUT;
2427 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2428 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
2429 sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
2430 sdp->device->host->unchecked_isa_dma : 1;
2431 sfp->cmd_q = SG_DEF_COMMAND_Q;
2432 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2433 sfp->parentdp = sdp;
2434 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2437 else { /* add to tail of existing list */
2438 Sg_fd *pfp = sdp->headfp;
2443 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2444 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2445 sg_build_reserve(sfp, sg_big_buff);
2446 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2447 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2452 __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2457 prev_fp = sdp->headfp;
2459 sdp->headfp = prev_fp->nextfp;
2461 while ((fp = prev_fp->nextfp)) {
2463 prev_fp->nextfp = fp->nextfp;
2469 if (sfp->reserve.bufflen > 0) {
2471 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2472 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
2473 if (sfp->mmap_called)
2474 sg_rb_correct4mmap(&sfp->reserve, 0); /* undo correction */
2475 sg_remove_scat(&sfp->reserve);
2477 sfp->parentdp = NULL;
2478 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp));
2479 sg_page_free((char *) sfp, sizeof (Sg_fd));
2482 /* Returns 0 in normal case, 1 when detached and sdp object removed */
2484 sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2491 for (srp = sfp->headrp; srp; srp = tsrp) {
2494 sg_finish_rem_req(srp);
2499 unsigned long iflags;
2501 write_lock_irqsave(&sg_dev_arr_lock, iflags);
2502 __sg_remove_sfp(sdp, sfp);
2503 if (sdp->detached && (NULL == sdp->headfp)) {
2507 for (k = 0; k < maxd; ++k) {
2508 if (sdp == sg_dev_arr[k])
2512 sg_dev_arr[k] = NULL;
2513 kfree((char *) sdp);
2516 write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2518 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2519 /* only bump the access_count if we actually succeeded in
2520 * throwing another counter on the host module */
2521 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2522 sfp->closed = 1; /* flag dirty state on this fd */
2523 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2530 sg_res_in_use(Sg_fd * sfp)
2532 const Sg_request *srp;
2533 unsigned long iflags;
2535 read_lock_irqsave(&sfp->rq_list_lock, iflags);
2536 for (srp = sfp->headrp; srp; srp = srp->nextrp)
2539 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2543 /* If retSzp==NULL want exact size or fail */
2545 sg_page_malloc(int rqSz, int lowDma, int *retSzp)
2556 page_mask = GFP_ATOMIC | GFP_DMA | __GFP_NOWARN;
2558 page_mask = GFP_ATOMIC | __GFP_NOWARN;
2560 for (order = 0, a_size = PAGE_SIZE; a_size < rqSz;
2561 order++, a_size <<= 1) ;
2562 resp = (char *) __get_free_pages(page_mask, order);
2563 while ((!resp) && order && retSzp) {
2565 a_size >>= 1; /* divide by 2, until PAGE_SIZE */
2566 resp = (char *) __get_free_pages(page_mask, order); /* try half */
2570 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2571 memset(resp, 0, resSz);
2579 sg_page_free(char *buff, int size)
2585 for (order = 0, a_size = PAGE_SIZE; a_size < size;
2586 order++, a_size <<= 1) ;
2587 free_pages((unsigned long) buff, order);
2591 sg_ms_to_jif(unsigned int msecs)
2593 if ((UINT_MAX / 2U) < msecs)
2594 return INT_MAX; /* special case, set largest possible */
2596 return ((int) msecs <
2597 (INT_MAX / 1000)) ? (((int) msecs * HZ) / 1000)
2598 : (((int) msecs / 1000) * HZ);
2601 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
2602 INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
2603 MODE_SENSE, MODE_SENSE_10, LOG_SENSE
2607 sg_allow_access(unsigned char opcode, char dev_type)
2611 if (TYPE_SCANNER == dev_type) /* TYPE_ROM maybe burner */
2613 for (k = 0; k < sizeof (allow_ops); ++k) {
2614 if (opcode == allow_ops[k])
2620 #ifdef CONFIG_SCSI_PROC_FS
2625 unsigned long iflags;
2627 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2628 for (k = sg_dev_max - 1; k >= 0; --k)
2629 if (sg_dev_arr[k] && sg_dev_arr[k]->device)
2631 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2632 return k + 1; /* origin 1 */
2639 Sg_device *sdp = NULL;
2640 unsigned long iflags;
2642 if (sg_dev_arr && (dev >= 0)) {
2643 read_lock_irqsave(&sg_dev_arr_lock, iflags);
2644 if (dev < sg_dev_max)
2645 sdp = sg_dev_arr[dev];
2646 read_unlock_irqrestore(&sg_dev_arr_lock, iflags);
2651 #ifdef CONFIG_SCSI_PROC_FS
2653 static struct proc_dir_entry *sg_proc_sgp = NULL;
2655 static char sg_proc_sg_dirname[] = "scsi/sg";
2657 static int sg_proc_seq_show_int(struct seq_file *s, void *v);
2659 static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
2660 static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
2661 size_t count, loff_t *off);
2662 static struct file_operations adio_fops = {
2663 /* .owner, .read and .llseek added in sg_proc_init() */
2664 .open = sg_proc_single_open_adio,
2665 .write = sg_proc_write_adio,
2666 .release = single_release,
2669 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
2670 static ssize_t sg_proc_write_dressz(struct file *filp,
2671 const char __user *buffer, size_t count, loff_t *off);
2672 static struct file_operations dressz_fops = {
2673 .open = sg_proc_single_open_dressz,
2674 .write = sg_proc_write_dressz,
2675 .release = single_release,
2678 static int sg_proc_seq_show_version(struct seq_file *s, void *v);
2679 static int sg_proc_single_open_version(struct inode *inode, struct file *file);
2680 static struct file_operations version_fops = {
2681 .open = sg_proc_single_open_version,
2682 .release = single_release,
2685 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
2686 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
2687 static struct file_operations devhdr_fops = {
2688 .open = sg_proc_single_open_devhdr,
2689 .release = single_release,
2692 static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
2693 static int sg_proc_open_dev(struct inode *inode, struct file *file);
2694 static void * dev_seq_start(struct seq_file *s, loff_t *pos);
2695 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
2696 static void dev_seq_stop(struct seq_file *s, void *v);
2697 static struct file_operations dev_fops = {
2698 .open = sg_proc_open_dev,
2699 .release = seq_release,
2701 static struct seq_operations dev_seq_ops = {
2702 .start = dev_seq_start,
2703 .next = dev_seq_next,
2704 .stop = dev_seq_stop,
2705 .show = sg_proc_seq_show_dev,
2708 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
2709 static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
2710 static struct file_operations devstrs_fops = {
2711 .open = sg_proc_open_devstrs,
2712 .release = seq_release,
2714 static struct seq_operations devstrs_seq_ops = {
2715 .start = dev_seq_start,
2716 .next = dev_seq_next,
2717 .stop = dev_seq_stop,
2718 .show = sg_proc_seq_show_devstrs,
2721 static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
2722 static int sg_proc_open_debug(struct inode *inode, struct file *file);
2723 static struct file_operations debug_fops = {
2724 .open = sg_proc_open_debug,
2725 .release = seq_release,
2727 static struct seq_operations debug_seq_ops = {
2728 .start = dev_seq_start,
2729 .next = dev_seq_next,
2730 .stop = dev_seq_stop,
2731 .show = sg_proc_seq_show_debug,
2735 struct sg_proc_leaf {
2737 struct file_operations * fops;
2740 static struct sg_proc_leaf sg_proc_leaf_arr[] = {
2741 {"allow_dio", &adio_fops},
2742 {"debug", &debug_fops},
2743 {"def_reserved_size", &dressz_fops},
2744 {"device_hdr", &devhdr_fops},
2745 {"devices", &dev_fops},
2746 {"device_strs", &devstrs_fops},
2747 {"version", &version_fops}
2755 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2756 struct proc_dir_entry *pdep;
2757 struct sg_proc_leaf * leaf;
2759 sg_proc_sgp = create_proc_entry(sg_proc_sg_dirname,
2760 S_IFDIR | S_IRUGO | S_IXUGO, NULL);
2763 for (k = 0; k < num_leaves; ++k) {
2764 leaf = &sg_proc_leaf_arr[k];
2765 mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2766 pdep = create_proc_entry(leaf->name, mask, sg_proc_sgp);
2768 leaf->fops->owner = THIS_MODULE,
2769 leaf->fops->read = seq_read,
2770 leaf->fops->llseek = seq_lseek,
2771 pdep->proc_fops = leaf->fops;
2778 sg_proc_cleanup(void)
2782 sizeof (sg_proc_leaf_arr) / sizeof (sg_proc_leaf_arr[0]);
2786 for (k = 0; k < num_leaves; ++k)
2787 remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
2788 remove_proc_entry(sg_proc_sg_dirname, NULL);
2792 static int sg_proc_seq_show_int(struct seq_file *s, void *v)
2794 seq_printf(s, "%d\n", *((int *)s->private));
2798 static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
2800 return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
2804 sg_proc_write_adio(struct file *filp, const char __user *buffer,
2805 size_t count, loff_t *off)
2810 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2812 num = (count < 10) ? count : 10;
2813 if (copy_from_user(buff, buffer, num))
2816 sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
2820 static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
2822 return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
2826 sg_proc_write_dressz(struct file *filp, const char __user *buffer,
2827 size_t count, loff_t *off)
2830 unsigned long k = ULONG_MAX;
2833 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2835 num = (count < 10) ? count : 10;
2836 if (copy_from_user(buff, buffer, num))
2839 k = simple_strtoul(buff, NULL, 10);
2840 if (k <= 1048576) { /* limit "big buff" to 1 MB */
2847 static int sg_proc_seq_show_version(struct seq_file *s, void *v)
2849 seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
2854 static int sg_proc_single_open_version(struct inode *inode, struct file *file)
2856 return single_open(file, sg_proc_seq_show_version, NULL);
2859 static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
2861 seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
2866 static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
2868 return single_open(file, sg_proc_seq_show_devhdr, NULL);
2871 struct sg_proc_deviter {
2876 static void * dev_seq_start(struct seq_file *s, loff_t *pos)
2878 struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
2882 if (NULL == sg_dev_arr)
2885 it->max = sg_last_dev();
2886 if (it->index >= it->max)
2894 static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
2896 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2899 return (it->index < it->max) ? it : NULL;
2902 static void dev_seq_stop(struct seq_file *s, void *v)
2907 static int sg_proc_open_dev(struct inode *inode, struct file *file)
2909 return seq_open(file, &dev_seq_ops);
2912 static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2914 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2916 struct scsi_device *scsidp;
2918 sdp = it ? sg_get_dev(it->index) : NULL;
2919 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2920 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2921 scsidp->host->host_no, scsidp->channel,
2922 scsidp->id, scsidp->lun, (int) scsidp->type,
2924 (int) scsidp->queue_depth,
2925 (int) scsidp->device_busy,
2926 (int) scsi_device_online(scsidp));
2928 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2932 static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
2934 return seq_open(file, &devstrs_seq_ops);
2937 static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2939 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2941 struct scsi_device *scsidp;
2943 sdp = it ? sg_get_dev(it->index) : NULL;
2944 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2945 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2946 scsidp->vendor, scsidp->model, scsidp->rev);
2948 seq_printf(s, "<no active device>\n");
2952 static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2954 int k, m, new_interface, blen, usg;
2957 const sg_io_hdr_t *hp;
2960 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
2961 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2962 "(res)sgat=%d low_dma=%d\n", k + 1,
2963 sg_jif_to_ms(fp->timeout),
2964 fp->reserve.bufflen,
2965 (int) fp->reserve.k_use_sg,
2967 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2968 (int) fp->cmd_q, (int) fp->force_packid,
2969 (int) fp->keep_orphan, (int) fp->closed);
2970 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
2972 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2973 if (srp->res_used) {
2974 if (new_interface &&
2975 (SG_FLAG_MMAP_IO & hp->flags))
2980 if (SG_INFO_DIRECT_IO_MASK & hp->info)
2986 blen = srp->my_cmdp ?
2987 srp->my_cmdp->sr_bufflen : srp->data.bufflen;
2988 usg = srp->my_cmdp ?
2989 srp->my_cmdp->sr_use_sg : srp->data.k_use_sg;
2990 seq_printf(s, srp->done ?
2991 ((1 == srp->done) ? "rcv:" : "fin:")
2992 : (srp->my_cmdp ? "act:" : "prior:"));
2993 seq_printf(s, " id=%d blen=%d",
2994 srp->header.pack_id, blen);
2996 seq_printf(s, " dur=%d", hp->duration);
2998 seq_printf(s, " t_o/elap=%d/%d",
2999 new_interface ? hp->timeout : sg_jif_to_ms(fp->timeout),
3000 sg_jif_to_ms(hp->duration ? (jiffies - hp->duration) : 0));
3001 seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
3002 (int) srp->data.cmd_opcode);
3005 seq_printf(s, " No requests active\n");
3009 static int sg_proc_open_debug(struct inode *inode, struct file *file)
3011 return seq_open(file, &debug_seq_ops);
3014 static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
3016 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
3019 if (it && (0 == it->index)) {
3020 seq_printf(s, "dev_max(currently)=%d max_active_device=%d "
3021 "(origin 1)\n", sg_dev_max, (int)it->max);
3022 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
3024 sdp = it ? sg_get_dev(it->index) : NULL;
3026 struct scsi_device *scsidp = sdp->device;
3028 if (NULL == scsidp) {
3029 seq_printf(s, "device %d detached ??\n",
3034 if (sg_get_nth_sfp(sdp, 0)) {
3035 seq_printf(s, " >>> device=%s ",
3036 sdp->disk->disk_name);
3038 seq_printf(s, "detached pending close ");
3041 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
3042 scsidp->host->host_no,
3043 scsidp->channel, scsidp->id,
3045 scsidp->host->hostt->emulated);
3046 seq_printf(s, " sg_tablesize=%d excl=%d\n",
3047 sdp->sg_tablesize, sdp->exclude);
3049 sg_proc_debug_helper(s, sdp);
3054 #endif /* CONFIG_SCSI_PROC_FS */
3056 module_init(init_sg);
3057 module_exit(exit_sg);
3058 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);