bsg.c 11 KB

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