]>
Commit | Line | Data |
---|---|---|
8c16567d | 1 | // SPDX-License-Identifier: GPL-2.0 |
3d6392cf | 2 | /* |
0c6a89ba | 3 | * bsg.c - block layer implementation of the sg v4 interface |
3d6392cf | 4 | */ |
3d6392cf JA |
5 | #include <linux/module.h> |
6 | #include <linux/init.h> | |
7 | #include <linux/file.h> | |
8 | #include <linux/blkdev.h> | |
3d6392cf | 9 | #include <linux/cdev.h> |
ad5ebd2f | 10 | #include <linux/jiffies.h> |
3d6392cf | 11 | #include <linux/percpu.h> |
598443a2 | 12 | #include <linux/idr.h> |
3d6392cf | 13 | #include <linux/bsg.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
3d6392cf JA |
15 | |
16 | #include <scsi/scsi.h> | |
17 | #include <scsi/scsi_ioctl.h> | |
3d6392cf JA |
18 | #include <scsi/sg.h> |
19 | ||
0ed081ce FT |
20 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
21 | #define BSG_VERSION "0.4" | |
3d6392cf | 22 | |
3d6392cf | 23 | struct bsg_device { |
165125e1 | 24 | struct request_queue *queue; |
ead09dd3 CH |
25 | struct device device; |
26 | struct cdev cdev; | |
3d6392cf | 27 | int max_queue; |
1e61c1a8 CH |
28 | unsigned int timeout; |
29 | unsigned int reserved_size; | |
75ca5640 | 30 | bsg_sg_io_fn *sg_io_fn; |
3d6392cf JA |
31 | }; |
32 | ||
ead09dd3 CH |
33 | static inline struct bsg_device *to_bsg_device(struct inode *inode) |
34 | { | |
35 | return container_of(inode->i_cdev, struct bsg_device, cdev); | |
36 | } | |
37 | ||
5309cb38 | 38 | #define BSG_DEFAULT_CMDS 64 |
9077fb2a | 39 | #define BSG_MAX_DEVS (1 << MINORBITS) |
3d6392cf | 40 | |
ead09dd3 | 41 | static DEFINE_IDA(bsg_minor_ida); |
72ef02b8 | 42 | static const struct class bsg_class; |
46f6ef4a | 43 | static int bsg_major; |
3d6392cf | 44 | |
75ca5640 CH |
45 | static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr) |
46 | { | |
47 | unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT; | |
48 | ||
49 | if (hdr->timeout) | |
50 | timeout = msecs_to_jiffies(hdr->timeout); | |
51 | else if (bd->timeout) | |
52 | timeout = bd->timeout; | |
53 | ||
54 | return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT); | |
55 | } | |
17cb960f | 56 | |
1991299e CH |
57 | static int bsg_sg_io(struct bsg_device *bd, bool open_for_write, |
58 | void __user *uarg) | |
3d6392cf | 59 | { |
ccf3209f | 60 | struct sg_io_v4 hdr; |
aebf526b | 61 | int ret; |
c7a841f3 | 62 | |
ccf3209f CH |
63 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
64 | return -EFAULT; | |
ccf3209f CH |
65 | if (hdr.guard != 'Q') |
66 | return -EINVAL; | |
1991299e CH |
67 | ret = bd->sg_io_fn(bd->queue, &hdr, open_for_write, |
68 | bsg_timeout(bd, &hdr)); | |
972248e9 | 69 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
ccf3209f CH |
70 | return -EFAULT; |
71 | return ret; | |
70e36ece FT |
72 | } |
73 | ||
3d6392cf JA |
74 | static int bsg_open(struct inode *inode, struct file *file) |
75 | { | |
ead09dd3 CH |
76 | if (!blk_get_queue(to_bsg_device(inode)->queue)) |
77 | return -ENXIO; | |
3d6392cf JA |
78 | return 0; |
79 | } | |
80 | ||
81 | static int bsg_release(struct inode *inode, struct file *file) | |
82 | { | |
ead09dd3 CH |
83 | blk_put_queue(to_bsg_device(inode)->queue); |
84 | return 0; | |
3d6392cf JA |
85 | } |
86 | ||
ccf3209f CH |
87 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
88 | { | |
ead09dd3 | 89 | return put_user(READ_ONCE(bd->max_queue), uarg); |
ccf3209f CH |
90 | } |
91 | ||
92 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) | |
93 | { | |
ead09dd3 | 94 | int max_queue; |
ccf3209f | 95 | |
ead09dd3 | 96 | if (get_user(max_queue, uarg)) |
ccf3209f | 97 | return -EFAULT; |
ead09dd3 | 98 | if (max_queue < 1) |
ccf3209f | 99 | return -EINVAL; |
ead09dd3 | 100 | WRITE_ONCE(bd->max_queue, max_queue); |
ccf3209f CH |
101 | return 0; |
102 | } | |
103 | ||
25fd1643 | 104 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
3d6392cf | 105 | { |
ead09dd3 | 106 | struct bsg_device *bd = to_bsg_device(file_inode(file)); |
d52fe8f4 | 107 | struct request_queue *q = bd->queue; |
ccf3209f | 108 | void __user *uarg = (void __user *) arg; |
d52fe8f4 CH |
109 | int __user *intp = uarg; |
110 | int val; | |
3d6392cf | 111 | |
3d6392cf | 112 | switch (cmd) { |
ccf3209f CH |
113 | /* |
114 | * Our own ioctls | |
115 | */ | |
3d6392cf | 116 | case SG_GET_COMMAND_Q: |
ccf3209f CH |
117 | return bsg_get_command_q(bd, uarg); |
118 | case SG_SET_COMMAND_Q: | |
119 | return bsg_set_command_q(bd, uarg); | |
3d6392cf JA |
120 | |
121 | /* | |
122 | * SCSI/sg ioctls | |
123 | */ | |
124 | case SG_GET_VERSION_NUM: | |
d52fe8f4 | 125 | return put_user(30527, intp); |
3d6392cf | 126 | case SCSI_IOCTL_GET_IDLUN: |
d52fe8f4 | 127 | return put_user(0, intp); |
3d6392cf | 128 | case SCSI_IOCTL_GET_BUS_NUMBER: |
d52fe8f4 | 129 | return put_user(0, intp); |
3d6392cf | 130 | case SG_SET_TIMEOUT: |
d52fe8f4 CH |
131 | if (get_user(val, intp)) |
132 | return -EFAULT; | |
1e61c1a8 | 133 | bd->timeout = clock_t_to_jiffies(val); |
d52fe8f4 | 134 | return 0; |
3d6392cf | 135 | case SG_GET_TIMEOUT: |
1e61c1a8 | 136 | return jiffies_to_clock_t(bd->timeout); |
3d6392cf | 137 | case SG_GET_RESERVED_SIZE: |
1e61c1a8 | 138 | return put_user(min(bd->reserved_size, queue_max_bytes(q)), |
d52fe8f4 | 139 | intp); |
3d6392cf | 140 | case SG_SET_RESERVED_SIZE: |
d52fe8f4 CH |
141 | if (get_user(val, intp)) |
142 | return -EFAULT; | |
143 | if (val < 0) | |
144 | return -EINVAL; | |
1e61c1a8 | 145 | bd->reserved_size = |
d52fe8f4 CH |
146 | min_t(unsigned int, val, queue_max_bytes(q)); |
147 | return 0; | |
3d6392cf | 148 | case SG_EMULATED_HOST: |
d52fe8f4 | 149 | return put_user(1, intp); |
ccf3209f | 150 | case SG_IO: |
1991299e | 151 | return bsg_sg_io(bd, file->f_mode & FMODE_WRITE, uarg); |
beec64d0 CH |
152 | case SCSI_IOCTL_SEND_COMMAND: |
153 | pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n", | |
154 | current->comm); | |
155 | return -EINVAL; | |
3d6392cf | 156 | default: |
3d6392cf | 157 | return -ENOTTY; |
3d6392cf JA |
158 | } |
159 | } | |
160 | ||
7344be05 | 161 | static const struct file_operations bsg_fops = { |
3d6392cf JA |
162 | .open = bsg_open, |
163 | .release = bsg_release, | |
25fd1643 | 164 | .unlocked_ioctl = bsg_ioctl, |
fe0da4e5 | 165 | .compat_ioctl = compat_ptr_ioctl, |
3d6392cf | 166 | .owner = THIS_MODULE, |
6038f373 | 167 | .llseek = default_llseek, |
3d6392cf JA |
168 | }; |
169 | ||
1a0db774 ZY |
170 | static void bsg_device_release(struct device *dev) |
171 | { | |
172 | struct bsg_device *bd = container_of(dev, struct bsg_device, device); | |
173 | ||
873cdda1 | 174 | ida_free(&bsg_minor_ida, MINOR(bd->device.devt)); |
1a0db774 ZY |
175 | kfree(bd); |
176 | } | |
177 | ||
ead09dd3 | 178 | void bsg_unregister_queue(struct bsg_device *bd) |
3d6392cf | 179 | { |
2bd85221 CH |
180 | struct gendisk *disk = bd->queue->disk; |
181 | ||
182 | if (disk && disk->queue_kobj.sd) | |
183 | sysfs_remove_link(&disk->queue_kobj, "bsg"); | |
ead09dd3 | 184 | cdev_device_del(&bd->cdev, &bd->device); |
1a0db774 | 185 | put_device(&bd->device); |
3d6392cf | 186 | } |
4cf0723a | 187 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
3d6392cf | 188 | |
ead09dd3 | 189 | struct bsg_device *bsg_register_queue(struct request_queue *q, |
75ca5640 | 190 | struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn) |
3d6392cf | 191 | { |
ead09dd3 | 192 | struct bsg_device *bd; |
bab998d6 | 193 | int ret; |
3d6392cf | 194 | |
ead09dd3 CH |
195 | bd = kzalloc(sizeof(*bd), GFP_KERNEL); |
196 | if (!bd) | |
197 | return ERR_PTR(-ENOMEM); | |
198 | bd->max_queue = BSG_DEFAULT_CMDS; | |
1e61c1a8 | 199 | bd->reserved_size = INT_MAX; |
ead09dd3 | 200 | bd->queue = q; |
75ca5640 | 201 | bd->sg_io_fn = sg_io_fn; |
292b7f27 | 202 | |
873cdda1 | 203 | ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL); |
bab998d6 | 204 | if (ret < 0) { |
ead09dd3 CH |
205 | if (ret == -ENOSPC) |
206 | dev_err(parent, "bsg: too many bsg devices\n"); | |
1a0db774 ZY |
207 | kfree(bd); |
208 | return ERR_PTR(ret); | |
4e2872d6 | 209 | } |
ead09dd3 | 210 | bd->device.devt = MKDEV(bsg_major, ret); |
72ef02b8 | 211 | bd->device.class = &bsg_class; |
ead09dd3 | 212 | bd->device.parent = parent; |
1a0db774 | 213 | bd->device.release = bsg_device_release; |
ead09dd3 CH |
214 | dev_set_name(&bd->device, "%s", name); |
215 | device_initialize(&bd->device); | |
216 | ||
217 | cdev_init(&bd->cdev, &bsg_fops); | |
218 | bd->cdev.owner = THIS_MODULE; | |
219 | ret = cdev_device_add(&bd->cdev, &bd->device); | |
220 | if (ret) | |
1a0db774 | 221 | goto out_put_device; |
4e2872d6 | 222 | |
2bd85221 CH |
223 | if (q->disk && q->disk->queue_kobj.sd) { |
224 | ret = sysfs_create_link(&q->disk->queue_kobj, &bd->device.kobj, | |
225 | "bsg"); | |
4e2872d6 | 226 | if (ret) |
ead09dd3 | 227 | goto out_device_del; |
4e2872d6 FT |
228 | } |
229 | ||
ead09dd3 | 230 | return bd; |
6826ee4f | 231 | |
ead09dd3 CH |
232 | out_device_del: |
233 | cdev_device_del(&bd->cdev, &bd->device); | |
1a0db774 ZY |
234 | out_put_device: |
235 | put_device(&bd->device); | |
ead09dd3 | 236 | return ERR_PTR(ret); |
4e2872d6 | 237 | } |
78011042 | 238 | EXPORT_SYMBOL_GPL(bsg_register_queue); |
4e2872d6 | 239 | |
ff62b8e6 | 240 | static char *bsg_devnode(const struct device *dev, umode_t *mode) |
2bdf9149 KS |
241 | { |
242 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); | |
243 | } | |
244 | ||
72ef02b8 IO |
245 | static const struct class bsg_class = { |
246 | .name = "bsg", | |
247 | .devnode = bsg_devnode, | |
248 | }; | |
249 | ||
3d6392cf JA |
250 | static int __init bsg_init(void) |
251 | { | |
46f6ef4a | 252 | dev_t devid; |
ead09dd3 | 253 | int ret; |
3d6392cf | 254 | |
72ef02b8 IO |
255 | ret = class_register(&bsg_class); |
256 | if (ret) | |
257 | return ret; | |
3d6392cf | 258 | |
46f6ef4a | 259 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
9b9f770c FT |
260 | if (ret) |
261 | goto destroy_bsg_class; | |
46f6ef4a JA |
262 | bsg_major = MAJOR(devid); |
263 | ||
5d3a8cd3 | 264 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
0ed081ce | 265 | " loaded (major %d)\n", bsg_major); |
3d6392cf | 266 | return 0; |
ead09dd3 | 267 | |
9b9f770c | 268 | destroy_bsg_class: |
72ef02b8 | 269 | class_unregister(&bsg_class); |
9b9f770c | 270 | return ret; |
3d6392cf JA |
271 | } |
272 | ||
273 | MODULE_AUTHOR("Jens Axboe"); | |
0ed081ce | 274 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
3d6392cf JA |
275 | MODULE_LICENSE("GPL"); |
276 | ||
4e2872d6 | 277 | device_initcall(bsg_init); |