bsg.c 12 KB

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