Christoph Hellwig | 8c16567 | 2019-04-30 14:42:39 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 2 | /* |
FUJITA Tomonori | 0c6a89b | 2007-07-29 23:00:46 +0900 | [diff] [blame] | 3 | * bsg.c - block layer implementation of the sg v4 interface |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 4 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 5 | #include <linux/module.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/file.h> |
| 8 | #include <linux/blkdev.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 9 | #include <linux/cdev.h> |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 10 | #include <linux/jiffies.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 11 | #include <linux/percpu.h> |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 12 | #include <linux/idr.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 13 | #include <linux/bsg.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 15 | |
| 16 | #include <scsi/scsi.h> |
| 17 | #include <scsi/scsi_ioctl.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 18 | #include <scsi/sg.h> |
| 19 | |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 20 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
| 21 | #define BSG_VERSION "0.4" |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 22 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 23 | struct bsg_device { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 24 | struct request_queue *queue; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 25 | const struct bsg_ops *ops; |
| 26 | struct device device; |
| 27 | struct cdev cdev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 28 | int max_queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 31 | static inline struct bsg_device *to_bsg_device(struct inode *inode) |
| 32 | { |
| 33 | return container_of(inode->i_cdev, struct bsg_device, cdev); |
| 34 | } |
| 35 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 36 | #define BSG_DEFAULT_CMDS 64 |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 37 | #define BSG_MAX_DEVS 32768 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 38 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 39 | static DEFINE_IDA(bsg_minor_ida); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 40 | static struct class *bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 41 | static int bsg_major; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 42 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 43 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 44 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 45 | static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg) |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 46 | { |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 47 | struct request *rq; |
| 48 | struct bio *bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 49 | struct sg_io_v4 hdr; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 50 | int ret; |
| 51 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 52 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
| 53 | return -EFAULT; |
| 54 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 55 | if (hdr.guard != 'Q') |
| 56 | return -EINVAL; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 57 | ret = bd->ops->check_proto(&hdr); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 58 | if (ret) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 59 | return ret; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 60 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 61 | rq = blk_get_request(bd->queue, hdr.dout_xfer_len ? |
Christoph Hellwig | da6269d | 2021-06-24 14:39:34 +0200 | [diff] [blame] | 62 | REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 63 | if (IS_ERR(rq)) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 64 | return PTR_ERR(rq); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 65 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 66 | ret = bd->ops->fill_hdr(rq, &hdr, mode); |
Pan Bian | 0f7b4bc | 2021-01-19 04:33:11 -0800 | [diff] [blame] | 67 | if (ret) { |
| 68 | blk_put_request(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 69 | return ret; |
Pan Bian | 0f7b4bc | 2021-01-19 04:33:11 -0800 | [diff] [blame] | 70 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 71 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 72 | rq->timeout = msecs_to_jiffies(hdr.timeout); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 73 | if (!rq->timeout) |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 74 | rq->timeout = rq->q->sg_timeout; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 75 | if (!rq->timeout) |
| 76 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; |
Linus Torvalds | f2f1fa7 | 2008-12-05 14:49:18 -0800 | [diff] [blame] | 77 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
| 78 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 79 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 80 | if (hdr.dout_xfer_len) { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 81 | ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr.dout_xferp), |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 82 | hdr.dout_xfer_len, GFP_KERNEL); |
| 83 | } else if (hdr.din_xfer_len) { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 84 | ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr.din_xferp), |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 85 | hdr.din_xfer_len, GFP_KERNEL); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 86 | } |
Boaz Harrosh | c1c2012 | 2009-02-03 07:47:29 +0100 | [diff] [blame] | 87 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 88 | if (ret) |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 89 | goto out_free_rq; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 90 | |
| 91 | bio = rq->bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 92 | |
Guoqing Jiang | 684da76 | 2021-01-25 05:49:58 +0100 | [diff] [blame] | 93 | blk_execute_rq(NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 94 | ret = bd->ops->complete_rq(rq, &hdr); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 95 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 96 | |
| 97 | out_free_rq: |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 98 | bd->ops->free_rq(rq); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 99 | blk_put_request(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 100 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 101 | return -EFAULT; |
| 102 | return ret; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 105 | static int bsg_open(struct inode *inode, struct file *file) |
| 106 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 107 | if (!blk_get_queue(to_bsg_device(inode)->queue)) |
| 108 | return -ENXIO; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int bsg_release(struct inode *inode, struct file *file) |
| 113 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 114 | blk_put_queue(to_bsg_device(inode)->queue); |
| 115 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 118 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
| 119 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 120 | return put_user(READ_ONCE(bd->max_queue), uarg); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) |
| 124 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 125 | int max_queue; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 126 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 127 | if (get_user(max_queue, uarg)) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 128 | return -EFAULT; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 129 | if (max_queue < 1) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 130 | return -EINVAL; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 131 | WRITE_ONCE(bd->max_queue, max_queue); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 135 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 136 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 137 | struct bsg_device *bd = to_bsg_device(file_inode(file)); |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 138 | struct request_queue *q = bd->queue; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 139 | void __user *uarg = (void __user *) arg; |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 140 | int __user *intp = uarg; |
| 141 | int val; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 142 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 143 | switch (cmd) { |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 144 | /* |
| 145 | * Our own ioctls |
| 146 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 147 | case SG_GET_COMMAND_Q: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 148 | return bsg_get_command_q(bd, uarg); |
| 149 | case SG_SET_COMMAND_Q: |
| 150 | return bsg_set_command_q(bd, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * SCSI/sg ioctls |
| 154 | */ |
| 155 | case SG_GET_VERSION_NUM: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 156 | return put_user(30527, intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 157 | case SCSI_IOCTL_GET_IDLUN: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 158 | return put_user(0, intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 159 | case SCSI_IOCTL_GET_BUS_NUMBER: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 160 | return put_user(0, intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 161 | case SG_SET_TIMEOUT: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 162 | if (get_user(val, intp)) |
| 163 | return -EFAULT; |
| 164 | q->sg_timeout = clock_t_to_jiffies(val); |
| 165 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 166 | case SG_GET_TIMEOUT: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 167 | return jiffies_to_clock_t(q->sg_timeout); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 168 | case SG_GET_RESERVED_SIZE: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 169 | return put_user(min(q->sg_reserved_size, queue_max_bytes(q)), |
| 170 | intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 171 | case SG_SET_RESERVED_SIZE: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 172 | if (get_user(val, intp)) |
| 173 | return -EFAULT; |
| 174 | if (val < 0) |
| 175 | return -EINVAL; |
| 176 | q->sg_reserved_size = |
| 177 | min_t(unsigned int, val, queue_max_bytes(q)); |
| 178 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 179 | case SG_EMULATED_HOST: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 180 | return put_user(1, intp); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 181 | case SG_IO: |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 182 | return bsg_sg_io(bd, file->f_mode, uarg); |
Christoph Hellwig | beec64d | 2021-07-24 09:20:10 +0200 | [diff] [blame] | 183 | case SCSI_IOCTL_SEND_COMMAND: |
| 184 | pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n", |
| 185 | current->comm); |
| 186 | return -EINVAL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 187 | default: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 188 | return -ENOTTY; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Arjan van de Ven | 7344be0 | 2007-10-15 11:01:53 +0200 | [diff] [blame] | 192 | static const struct file_operations bsg_fops = { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 193 | .open = bsg_open, |
| 194 | .release = bsg_release, |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 195 | .unlocked_ioctl = bsg_ioctl, |
Arnd Bergmann | fe0da4e | 2019-03-15 17:13:06 +0100 | [diff] [blame] | 196 | .compat_ioctl = compat_ptr_ioctl, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 197 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 198 | .llseek = default_llseek, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 199 | }; |
| 200 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 201 | void bsg_unregister_queue(struct bsg_device *bd) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 202 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 203 | if (bd->queue->kobj.sd) |
| 204 | sysfs_remove_link(&bd->queue->kobj, "bsg"); |
| 205 | cdev_device_del(&bd->cdev, &bd->device); |
| 206 | ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); |
| 207 | kfree(bd); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 208 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 210 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 211 | struct bsg_device *bsg_register_queue(struct request_queue *q, |
| 212 | struct device *parent, const char *name, |
| 213 | const struct bsg_ops *ops) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 214 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 215 | struct bsg_device *bd; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 216 | int ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 217 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 218 | bd = kzalloc(sizeof(*bd), GFP_KERNEL); |
| 219 | if (!bd) |
| 220 | return ERR_PTR(-ENOMEM); |
| 221 | bd->max_queue = BSG_DEFAULT_CMDS; |
| 222 | bd->queue = q; |
| 223 | bd->ops = ops; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 224 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 225 | ret = ida_simple_get(&bsg_minor_ida, 0, BSG_MAX_DEVS, GFP_KERNEL); |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 226 | if (ret < 0) { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 227 | if (ret == -ENOSPC) |
| 228 | dev_err(parent, "bsg: too many bsg devices\n"); |
| 229 | goto out_kfree; |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 230 | } |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 231 | bd->device.devt = MKDEV(bsg_major, ret); |
| 232 | bd->device.class = bsg_class; |
| 233 | bd->device.parent = parent; |
| 234 | dev_set_name(&bd->device, "%s", name); |
| 235 | device_initialize(&bd->device); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 236 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 237 | cdev_init(&bd->cdev, &bsg_fops); |
| 238 | bd->cdev.owner = THIS_MODULE; |
| 239 | ret = cdev_device_add(&bd->cdev, &bd->device); |
| 240 | if (ret) |
| 241 | goto out_ida_remove; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 242 | |
Linus Torvalds | abce891 | 2007-07-16 11:18:23 -0700 | [diff] [blame] | 243 | if (q->kobj.sd) { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 244 | ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg"); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 245 | if (ret) |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 246 | goto out_device_del; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 249 | return bd; |
James Bottomley | 6826ee4 | 2007-07-20 16:50:10 -0500 | [diff] [blame] | 250 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 251 | out_device_del: |
| 252 | cdev_device_del(&bd->cdev, &bd->device); |
| 253 | out_ida_remove: |
| 254 | ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); |
| 255 | out_kfree: |
| 256 | kfree(bd); |
| 257 | return ERR_PTR(ret); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 258 | } |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 259 | EXPORT_SYMBOL_GPL(bsg_register_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 260 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 261 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 2bdf914 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 262 | { |
| 263 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| 264 | } |
| 265 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 266 | static int __init bsg_init(void) |
| 267 | { |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 268 | dev_t devid; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 269 | int ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 270 | |
| 271 | bsg_class = class_create(THIS_MODULE, "bsg"); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 272 | if (IS_ERR(bsg_class)) |
| 273 | return PTR_ERR(bsg_class); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 274 | bsg_class->devnode = bsg_devnode; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 275 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 276 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 277 | if (ret) |
| 278 | goto destroy_bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 279 | bsg_major = MAJOR(devid); |
| 280 | |
Jens Axboe | 5d3a8cd | 2007-07-17 15:10:09 +0200 | [diff] [blame] | 281 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 282 | " loaded (major %d)\n", bsg_major); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 283 | return 0; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame^] | 284 | |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 285 | destroy_bsg_class: |
| 286 | class_destroy(bsg_class); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 287 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | MODULE_AUTHOR("Jens Axboe"); |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 291 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 292 | MODULE_LICENSE("GPL"); |
| 293 | |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 294 | device_initcall(bsg_init); |