xref: /openbmc/linux/block/bsg.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bsg.c - block layer implementation of the sg v4 interface
4  */
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/file.h>
8 #include <linux/blkdev.h>
9 #include <linux/cdev.h>
10 #include <linux/jiffies.h>
11 #include <linux/percpu.h>
12 #include <linux/idr.h>
13 #include <linux/bsg.h>
14 #include <linux/slab.h>
15 
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_ioctl.h>
18 #include <scsi/scsi_cmnd.h>
19 #include <scsi/scsi_device.h>
20 #include <scsi/scsi_driver.h>
21 #include <scsi/sg.h>
22 
23 #define BSG_DESCRIPTION	"Block layer SCSI generic (bsg) driver"
24 #define BSG_VERSION	"0.4"
25 
26 #define bsg_dbg(bd, fmt, ...) \
27 	pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
28 
29 struct bsg_device {
30 	struct request_queue *queue;
31 	spinlock_t lock;
32 	struct hlist_node dev_list;
33 	refcount_t ref_count;
34 	char name[20];
35 	int max_queue;
36 };
37 
38 #define BSG_DEFAULT_CMDS	64
39 #define BSG_MAX_DEVS		32768
40 
41 static DEFINE_MUTEX(bsg_mutex);
42 static DEFINE_IDR(bsg_minor_idr);
43 
44 #define BSG_LIST_ARRAY_SIZE	8
45 static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
46 
47 static struct class *bsg_class;
48 static int bsg_major;
49 
50 static inline struct hlist_head *bsg_dev_idx_hash(int index)
51 {
52 	return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
53 }
54 
55 #define uptr64(val) ((void __user *)(uintptr_t)(val))
56 
57 static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
58 {
59 	if (hdr->protocol != BSG_PROTOCOL_SCSI  ||
60 	    hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
61 		return -EINVAL;
62 	return 0;
63 }
64 
65 static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
66 		fmode_t mode)
67 {
68 	struct scsi_request *sreq = scsi_req(rq);
69 
70 	if (hdr->dout_xfer_len && hdr->din_xfer_len) {
71 		pr_warn_once("BIDI support in bsg has been removed.\n");
72 		return -EOPNOTSUPP;
73 	}
74 
75 	sreq->cmd_len = hdr->request_len;
76 	if (sreq->cmd_len > BLK_MAX_CDB) {
77 		sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
78 		if (!sreq->cmd)
79 			return -ENOMEM;
80 	}
81 
82 	if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
83 		return -EFAULT;
84 	if (blk_verify_command(sreq->cmd, mode))
85 		return -EPERM;
86 	return 0;
87 }
88 
89 static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
90 {
91 	struct scsi_request *sreq = scsi_req(rq);
92 	int ret = 0;
93 
94 	/*
95 	 * fill in all the output members
96 	 */
97 	hdr->device_status = sreq->result & 0xff;
98 	hdr->transport_status = host_byte(sreq->result);
99 	hdr->driver_status = driver_byte(sreq->result);
100 	hdr->info = 0;
101 	if (hdr->device_status || hdr->transport_status || hdr->driver_status)
102 		hdr->info |= SG_INFO_CHECK;
103 	hdr->response_len = 0;
104 
105 	if (sreq->sense_len && hdr->response) {
106 		int len = min_t(unsigned int, hdr->max_response_len,
107 					sreq->sense_len);
108 
109 		if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
110 			ret = -EFAULT;
111 		else
112 			hdr->response_len = len;
113 	}
114 
115 	if (rq_data_dir(rq) == READ)
116 		hdr->din_resid = sreq->resid_len;
117 	else
118 		hdr->dout_resid = sreq->resid_len;
119 
120 	return ret;
121 }
122 
123 static void bsg_scsi_free_rq(struct request *rq)
124 {
125 	scsi_req_free_cmd(scsi_req(rq));
126 }
127 
128 static const struct bsg_ops bsg_scsi_ops = {
129 	.check_proto		= bsg_scsi_check_proto,
130 	.fill_hdr		= bsg_scsi_fill_hdr,
131 	.complete_rq		= bsg_scsi_complete_rq,
132 	.free_rq		= bsg_scsi_free_rq,
133 };
134 
135 static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
136 {
137 	struct request *rq;
138 	struct bio *bio;
139 	struct sg_io_v4 hdr;
140 	int ret;
141 
142 	if (copy_from_user(&hdr, uarg, sizeof(hdr)))
143 		return -EFAULT;
144 
145 	if (!q->bsg_dev.class_dev)
146 		return -ENXIO;
147 
148 	if (hdr.guard != 'Q')
149 		return -EINVAL;
150 	ret = q->bsg_dev.ops->check_proto(&hdr);
151 	if (ret)
152 		return ret;
153 
154 	rq = blk_get_request(q, hdr.dout_xfer_len ?
155 			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
156 	if (IS_ERR(rq))
157 		return PTR_ERR(rq);
158 
159 	ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
160 	if (ret) {
161 		blk_put_request(rq);
162 		return ret;
163 	}
164 
165 	rq->timeout = msecs_to_jiffies(hdr.timeout);
166 	if (!rq->timeout)
167 		rq->timeout = q->sg_timeout;
168 	if (!rq->timeout)
169 		rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
170 	if (rq->timeout < BLK_MIN_SG_TIMEOUT)
171 		rq->timeout = BLK_MIN_SG_TIMEOUT;
172 
173 	if (hdr.dout_xfer_len) {
174 		ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
175 				hdr.dout_xfer_len, GFP_KERNEL);
176 	} else if (hdr.din_xfer_len) {
177 		ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
178 				hdr.din_xfer_len, GFP_KERNEL);
179 	}
180 
181 	if (ret)
182 		goto out_free_rq;
183 
184 	bio = rq->bio;
185 
186 	blk_execute_rq(NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
187 	ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
188 	blk_rq_unmap_user(bio);
189 
190 out_free_rq:
191 	rq->q->bsg_dev.ops->free_rq(rq);
192 	blk_put_request(rq);
193 	if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
194 		return -EFAULT;
195 	return ret;
196 }
197 
198 static struct bsg_device *bsg_alloc_device(void)
199 {
200 	struct bsg_device *bd;
201 
202 	bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
203 	if (unlikely(!bd))
204 		return NULL;
205 
206 	spin_lock_init(&bd->lock);
207 	bd->max_queue = BSG_DEFAULT_CMDS;
208 	INIT_HLIST_NODE(&bd->dev_list);
209 	return bd;
210 }
211 
212 static int bsg_put_device(struct bsg_device *bd)
213 {
214 	struct request_queue *q = bd->queue;
215 
216 	mutex_lock(&bsg_mutex);
217 
218 	if (!refcount_dec_and_test(&bd->ref_count)) {
219 		mutex_unlock(&bsg_mutex);
220 		return 0;
221 	}
222 
223 	hlist_del(&bd->dev_list);
224 	mutex_unlock(&bsg_mutex);
225 
226 	bsg_dbg(bd, "tearing down\n");
227 
228 	/*
229 	 * close can always block
230 	 */
231 	kfree(bd);
232 	blk_put_queue(q);
233 	return 0;
234 }
235 
236 static struct bsg_device *bsg_add_device(struct inode *inode,
237 					 struct request_queue *rq,
238 					 struct file *file)
239 {
240 	struct bsg_device *bd;
241 	unsigned char buf[32];
242 
243 	lockdep_assert_held(&bsg_mutex);
244 
245 	if (!blk_get_queue(rq))
246 		return ERR_PTR(-ENXIO);
247 
248 	bd = bsg_alloc_device();
249 	if (!bd) {
250 		blk_put_queue(rq);
251 		return ERR_PTR(-ENOMEM);
252 	}
253 
254 	bd->queue = rq;
255 
256 	refcount_set(&bd->ref_count, 1);
257 	hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
258 
259 	strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
260 	bsg_dbg(bd, "bound to <%s>, max queue %d\n",
261 		format_dev_t(buf, inode->i_rdev), bd->max_queue);
262 
263 	return bd;
264 }
265 
266 static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
267 {
268 	struct bsg_device *bd;
269 
270 	lockdep_assert_held(&bsg_mutex);
271 
272 	hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
273 		if (bd->queue == q) {
274 			refcount_inc(&bd->ref_count);
275 			goto found;
276 		}
277 	}
278 	bd = NULL;
279 found:
280 	return bd;
281 }
282 
283 static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
284 {
285 	struct bsg_device *bd;
286 	struct bsg_class_device *bcd;
287 
288 	/*
289 	 * find the class device
290 	 */
291 	mutex_lock(&bsg_mutex);
292 	bcd = idr_find(&bsg_minor_idr, iminor(inode));
293 
294 	if (!bcd) {
295 		bd = ERR_PTR(-ENODEV);
296 		goto out_unlock;
297 	}
298 
299 	bd = __bsg_get_device(iminor(inode), bcd->queue);
300 	if (!bd)
301 		bd = bsg_add_device(inode, bcd->queue, file);
302 
303 out_unlock:
304 	mutex_unlock(&bsg_mutex);
305 	return bd;
306 }
307 
308 static int bsg_open(struct inode *inode, struct file *file)
309 {
310 	struct bsg_device *bd;
311 
312 	bd = bsg_get_device(inode, file);
313 
314 	if (IS_ERR(bd))
315 		return PTR_ERR(bd);
316 
317 	file->private_data = bd;
318 	return 0;
319 }
320 
321 static int bsg_release(struct inode *inode, struct file *file)
322 {
323 	struct bsg_device *bd = file->private_data;
324 
325 	file->private_data = NULL;
326 	return bsg_put_device(bd);
327 }
328 
329 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
330 {
331 	return put_user(bd->max_queue, uarg);
332 }
333 
334 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
335 {
336 	int queue;
337 
338 	if (get_user(queue, uarg))
339 		return -EFAULT;
340 	if (queue < 1)
341 		return -EINVAL;
342 
343 	spin_lock_irq(&bd->lock);
344 	bd->max_queue = queue;
345 	spin_unlock_irq(&bd->lock);
346 	return 0;
347 }
348 
349 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
350 {
351 	struct bsg_device *bd = file->private_data;
352 	void __user *uarg = (void __user *) arg;
353 
354 	switch (cmd) {
355 	/*
356 	 * Our own ioctls
357 	 */
358 	case SG_GET_COMMAND_Q:
359 		return bsg_get_command_q(bd, uarg);
360 	case SG_SET_COMMAND_Q:
361 		return bsg_set_command_q(bd, uarg);
362 
363 	/*
364 	 * SCSI/sg ioctls
365 	 */
366 	case SG_GET_VERSION_NUM:
367 	case SCSI_IOCTL_GET_IDLUN:
368 	case SCSI_IOCTL_GET_BUS_NUMBER:
369 	case SG_SET_TIMEOUT:
370 	case SG_GET_TIMEOUT:
371 	case SG_GET_RESERVED_SIZE:
372 	case SG_SET_RESERVED_SIZE:
373 	case SG_EMULATED_HOST:
374 	case SCSI_IOCTL_SEND_COMMAND:
375 		return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
376 	case SG_IO:
377 		return bsg_sg_io(bd->queue, file->f_mode, uarg);
378 	default:
379 		return -ENOTTY;
380 	}
381 }
382 
383 static const struct file_operations bsg_fops = {
384 	.open		=	bsg_open,
385 	.release	=	bsg_release,
386 	.unlocked_ioctl	=	bsg_ioctl,
387 	.compat_ioctl	=	compat_ptr_ioctl,
388 	.owner		=	THIS_MODULE,
389 	.llseek		=	default_llseek,
390 };
391 
392 void bsg_unregister_queue(struct request_queue *q)
393 {
394 	struct bsg_class_device *bcd = &q->bsg_dev;
395 
396 	if (!bcd->class_dev)
397 		return;
398 
399 	mutex_lock(&bsg_mutex);
400 	idr_remove(&bsg_minor_idr, bcd->minor);
401 	if (q->kobj.sd)
402 		sysfs_remove_link(&q->kobj, "bsg");
403 	device_unregister(bcd->class_dev);
404 	bcd->class_dev = NULL;
405 	mutex_unlock(&bsg_mutex);
406 }
407 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
408 
409 int bsg_register_queue(struct request_queue *q, struct device *parent,
410 		const char *name, const struct bsg_ops *ops)
411 {
412 	struct bsg_class_device *bcd;
413 	dev_t dev;
414 	int ret;
415 	struct device *class_dev = NULL;
416 
417 	/*
418 	 * we need a proper transport to send commands, not a stacked device
419 	 */
420 	if (!queue_is_mq(q))
421 		return 0;
422 
423 	bcd = &q->bsg_dev;
424 	memset(bcd, 0, sizeof(*bcd));
425 
426 	mutex_lock(&bsg_mutex);
427 
428 	ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
429 	if (ret < 0) {
430 		if (ret == -ENOSPC) {
431 			printk(KERN_ERR "bsg: too many bsg devices\n");
432 			ret = -EINVAL;
433 		}
434 		goto unlock;
435 	}
436 
437 	bcd->minor = ret;
438 	bcd->queue = q;
439 	bcd->ops = ops;
440 	dev = MKDEV(bsg_major, bcd->minor);
441 	class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
442 	if (IS_ERR(class_dev)) {
443 		ret = PTR_ERR(class_dev);
444 		goto idr_remove;
445 	}
446 	bcd->class_dev = class_dev;
447 
448 	if (q->kobj.sd) {
449 		ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
450 		if (ret)
451 			goto unregister_class_dev;
452 	}
453 
454 	mutex_unlock(&bsg_mutex);
455 	return 0;
456 
457 unregister_class_dev:
458 	device_unregister(class_dev);
459 idr_remove:
460 	idr_remove(&bsg_minor_idr, bcd->minor);
461 unlock:
462 	mutex_unlock(&bsg_mutex);
463 	return ret;
464 }
465 
466 int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
467 {
468 	if (!blk_queue_scsi_passthrough(q)) {
469 		WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
470 		return -EINVAL;
471 	}
472 
473 	return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
474 }
475 EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
476 
477 static struct cdev bsg_cdev;
478 
479 static char *bsg_devnode(struct device *dev, umode_t *mode)
480 {
481 	return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
482 }
483 
484 static int __init bsg_init(void)
485 {
486 	int ret, i;
487 	dev_t devid;
488 
489 	for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
490 		INIT_HLIST_HEAD(&bsg_device_list[i]);
491 
492 	bsg_class = class_create(THIS_MODULE, "bsg");
493 	if (IS_ERR(bsg_class))
494 		return PTR_ERR(bsg_class);
495 	bsg_class->devnode = bsg_devnode;
496 
497 	ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
498 	if (ret)
499 		goto destroy_bsg_class;
500 
501 	bsg_major = MAJOR(devid);
502 
503 	cdev_init(&bsg_cdev, &bsg_fops);
504 	ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
505 	if (ret)
506 		goto unregister_chrdev;
507 
508 	printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
509 	       " loaded (major %d)\n", bsg_major);
510 	return 0;
511 unregister_chrdev:
512 	unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
513 destroy_bsg_class:
514 	class_destroy(bsg_class);
515 	return ret;
516 }
517 
518 MODULE_AUTHOR("Jens Axboe");
519 MODULE_DESCRIPTION(BSG_DESCRIPTION);
520 MODULE_LICENSE("GPL");
521 
522 device_initcall(bsg_init);
523