Commit 65c26a0f39695ba01d9693754f27ca76cc8a3ab5

Authored by Douglas Gilbert
Committed by Christoph Hellwig
1 parent bcdb247c6b

sg: relax 16 byte cdb restriction

- remove the 16 byte CDB (SCSI command) length limit from the sg driver
   by handling longer CDBs the same way as the bsg driver. Remove comment
   from sg.h public interface about the cmd_len field being limited to 16
   bytes.
 - remove some dead code caused by this change
 - cleanup comment block at the top of sg.h, fix urls

Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
Reviewed-by: Mike Christie <michaelc@cs.wisc.edu>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>

Showing 2 changed files with 53 additions and 84 deletions Side-by-side Diff

... ... @@ -7,10 +7,8 @@
7 7 * Original driver (sg.c):
8 8 * Copyright (C) 1992 Lawrence Foard
9 9 * Version 2 and 3 extensions to driver:
10   - * Copyright (C) 1998 - 2005 Douglas Gilbert
  10 + * Copyright (C) 1998 - 2014 Douglas Gilbert
11 11 *
12   - * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
13   - *
14 12 * This program is free software; you can redistribute it and/or modify
15 13 * it under the terms of the GNU General Public License as published by
16 14 * the Free Software Foundation; either version 2, or (at your option)
17 15  
... ... @@ -18,11 +16,11 @@
18 16 *
19 17 */
20 18  
21   -static int sg_version_num = 30534; /* 2 digits for each component */
22   -#define SG_VERSION_STR "3.5.34"
  19 +static int sg_version_num = 30536; /* 2 digits for each component */
  20 +#define SG_VERSION_STR "3.5.36"
23 21  
24 22 /*
25   - * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
  23 + * D. P. Gilbert (dgilbert@interlog.com), notes:
26 24 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
27 25 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
28 26 * (otherwise the macros compile to empty statements).
... ... @@ -64,7 +62,7 @@
64 62  
65 63 #ifdef CONFIG_SCSI_PROC_FS
66 64 #include <linux/proc_fs.h>
67   -static char *sg_version_date = "20061027";
  65 +static char *sg_version_date = "20140603";
68 66  
69 67 static int sg_proc_init(void);
70 68 static void sg_proc_cleanup(void);
... ... @@ -74,6 +72,12 @@
74 72  
75 73 #define SG_MAX_DEVS 32768
76 74  
  75 +/* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
  76 + * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
  77 + * than 16 bytes are "variable length" whose length is a multiple of 4
  78 + */
  79 +#define SG_MAX_CDB_SIZE 252
  80 +
77 81 /*
78 82 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
79 83 * Then when using 32 bit integers x * m may overflow during the calculation.
... ... @@ -161,7 +165,7 @@
161 165 char low_dma; /* as in parent but possibly overridden to 1 */
162 166 char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
163 167 char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
164   - char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
  168 + unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
165 169 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
166 170 char mmap_called; /* 0 -> mmap() never called on this fd */
167 171 struct kref f_ref;
... ... @@ -566,7 +570,7 @@
566 570 Sg_request *srp;
567 571 struct sg_header old_hdr;
568 572 sg_io_hdr_t *hp;
569   - unsigned char cmnd[MAX_COMMAND_SIZE];
  573 + unsigned char cmnd[SG_MAX_CDB_SIZE];
570 574  
571 575 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
572 576 return -ENXIO;
... ... @@ -598,12 +602,6 @@
598 602 buf += SZ_SG_HEADER;
599 603 __get_user(opcode, buf);
600 604 if (sfp->next_cmd_len > 0) {
601   - if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
602   - SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
603   - sfp->next_cmd_len = 0;
604   - sg_remove_request(sfp, srp);
605   - return -EIO;
606   - }
607 605 cmd_size = sfp->next_cmd_len;
608 606 sfp->next_cmd_len = 0; /* reset so only this write() effected */
609 607 } else {
... ... @@ -675,7 +673,7 @@
675 673 int k;
676 674 Sg_request *srp;
677 675 sg_io_hdr_t *hp;
678   - unsigned char cmnd[MAX_COMMAND_SIZE];
  676 + unsigned char cmnd[SG_MAX_CDB_SIZE];
679 677 int timeout;
680 678 unsigned long ul_timeout;
681 679  
682 680  
683 681  
684 682  
685 683  
... ... @@ -1654,15 +1652,27 @@
1654 1652 struct request_queue *q = sfp->parentdp->device->request_queue;
1655 1653 struct rq_map_data *md, map_data;
1656 1654 int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
  1655 + unsigned char *long_cmdp = NULL;
1657 1656  
1658 1657 SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
1659 1658 dxfer_len));
1660 1659  
  1660 + if (hp->cmd_len > BLK_MAX_CDB) {
  1661 + long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
  1662 + if (!long_cmdp)
  1663 + return -ENOMEM;
  1664 + }
  1665 +
1661 1666 rq = blk_get_request(q, rw, GFP_ATOMIC);
1662   - if (!rq)
  1667 + if (!rq) {
  1668 + kfree(long_cmdp);
1663 1669 return -ENOMEM;
  1670 + }
1664 1671  
1665 1672 blk_rq_set_block_pc(rq);
  1673 +
  1674 + if (hp->cmd_len > BLK_MAX_CDB)
  1675 + rq->cmd = long_cmdp;
1666 1676 memcpy(rq->cmd, cmd, hp->cmd_len);
1667 1677 rq->cmd_len = hp->cmd_len;
1668 1678  
... ... @@ -1747,6 +1757,8 @@
1747 1757 if (srp->bio)
1748 1758 ret = blk_rq_unmap_user(srp->bio);
1749 1759  
  1760 + if (srp->rq->cmd != srp->rq->__cmd)
  1761 + kfree(srp->rq->cmd);
1750 1762 blk_put_request(srp->rq);
1751 1763 }
1752 1764  
... ... @@ -4,77 +4,34 @@
4 4 #include <linux/compiler.h>
5 5  
6 6 /*
7   - History:
8   - Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
9   - process control of SCSI devices.
10   - Development Sponsored by Killy Corp. NY NY
11   -Original driver (sg.h):
12   -* Copyright (C) 1992 Lawrence Foard
13   -Version 2 and 3 extensions to driver:
14   -* Copyright (C) 1998 - 2006 Douglas Gilbert
  7 + * History:
  8 + * Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
  9 + * process control of SCSI devices.
  10 + * Development Sponsored by Killy Corp. NY NY
  11 + *
  12 + * Original driver (sg.h):
  13 + * Copyright (C) 1992 Lawrence Foard
  14 + * Version 2 and 3 extensions to driver:
  15 + * Copyright (C) 1998 - 2014 Douglas Gilbert
  16 + *
  17 + * Version: 3.5.36 (20140603)
  18 + * This version is for 2.6 and 3 series kernels.
  19 + *
  20 + * Documentation
  21 + * =============
  22 + * A web site for the SG device driver can be found at:
  23 + * http://sg.danny.cz/sg [alternatively check the MAINTAINERS file]
  24 + * The documentation for the sg version 3 driver can be found at:
  25 + * http://sg.danny.cz/sg/p/sg_v3_ho.html
  26 + * Also see: <kernel_source>/Documentation/scsi/scsi-generic.txt
  27 + *
  28 + * For utility and test programs see: http://sg.danny.cz/sg/sg3_utils.html
  29 + */
15 30  
16   - Version: 3.5.34 (20060920)
17   - This version is for 2.6 series kernels.
18   -
19   - For a full changelog see http://www.torque.net/sg
20   -
21   -Map of SG verions to the Linux kernels in which they appear:
22   - ---------- ----------------------------------
23   - original all kernels < 2.2.6
24   - 2.1.40 2.2.20
25   - 3.0.x optional version 3 sg driver for 2.2 series
26   - 3.1.17++ 2.4.0++
27   - 3.5.30++ 2.6.0++
28   -
29   -Major new features in SG 3.x driver (cf SG 2.x drivers)
30   - - SG_IO ioctl() combines function if write() and read()
31   - - new interface (sg_io_hdr_t) but still supports old interface
32   - - scatter/gather in user space, direct IO, and mmap supported
33   -
34   - The normal action of this driver is to use the adapter (HBA) driver to DMA
35   - data into kernel buffers and then use the CPU to copy the data into the
36   - user space (vice versa for writes). That is called "indirect" IO due to
37   - the double handling of data. There are two methods offered to remove the
38   - redundant copy: 1) direct IO and 2) using the mmap() system call to map
39   - the reserve buffer (this driver has one reserve buffer per fd) into the
40   - user space. Both have their advantages.
41   - In terms of absolute speed mmap() is faster. If speed is not a concern,
42   - indirect IO should be fine. Read the documentation for more information.
43   -
44   - ** N.B. To use direct IO 'echo 1 > /proc/scsi/sg/allow_dio' or
45   - 'echo 1 > /sys/module/sg/parameters/allow_dio' is needed.
46   - That attribute is 0 by default. **
47   -
48   - Historical note: this SCSI pass-through driver has been known as "sg" for
49   - a decade. In broader kernel discussions "sg" is used to refer to scatter
50   - gather techniques. The context should clarify which "sg" is referred to.
51   -
52   - Documentation
53   - =============
54   - A web site for the SG device driver can be found at:
55   - http://www.torque.net/sg [alternatively check the MAINTAINERS file]
56   - The documentation for the sg version 3 driver can be found at:
57   - http://www.torque.net/sg/p/sg_v3_ho.html
58   - This is a rendering from DocBook source [change the extension to "sgml"
59   - or "xml"]. There are renderings in "ps", "pdf", "rtf" and "txt" (soon).
60   - The SG_IO ioctl is now found in other parts kernel (e.g. the block layer).
61   - For more information see http://www.torque.net/sg/sg_io.html
62   -
63   - The older, version 2 documents discuss the original sg interface in detail:
64   - http://www.torque.net/sg/p/scsi-generic.txt
65   - http://www.torque.net/sg/p/scsi-generic_long.txt
66   - Also available: <kernel_source>/Documentation/scsi/scsi-generic.txt
67   -
68   - Utility and test programs are available at the sg web site. They are
69   - packaged as sg3_utils (for the lk 2.4 and 2.6 series) and sg_utils
70   - (for the lk 2.2 series).
71   -*/
72   -
73 31 #ifdef __KERNEL__
74 32 extern int sg_big_buff; /* for sysctl */
75 33 #endif
76 34  
77   -/* New interface introduced in the 3.x SG drivers follows */
78 35  
79 36 typedef struct sg_iovec /* same structure as used by readv() Linux system */
80 37 { /* call. It defines one scatter-gather element. */
... ... @@ -87,7 +44,7 @@
87 44 {
88 45 int interface_id; /* [i] 'S' for SCSI generic (required) */
89 46 int dxfer_direction; /* [i] data transfer direction */
90   - unsigned char cmd_len; /* [i] SCSI command length ( <= 16 bytes) */
  47 + unsigned char cmd_len; /* [i] SCSI command length */
91 48 unsigned char mx_sb_len; /* [i] max length to write to sbp */
92 49 unsigned short iovec_count; /* [i] 0 implies no scatter gather */
93 50 unsigned int dxfer_len; /* [i] byte count of data transfer */