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> |
| 18 | #include <scsi/scsi_cmnd.h> |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 19 | #include <scsi/scsi_device.h> |
| 20 | #include <scsi/scsi_driver.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 21 | #include <scsi/sg.h> |
| 22 | |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 23 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
| 24 | #define BSG_VERSION "0.4" |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 25 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 26 | #define bsg_dbg(bd, fmt, ...) \ |
| 27 | pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__) |
| 28 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 29 | struct bsg_device { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 30 | struct request_queue *queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 31 | spinlock_t lock; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 32 | struct hlist_node dev_list; |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 33 | refcount_t ref_count; |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 34 | char name[20]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 35 | int max_queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 38 | #define BSG_DEFAULT_CMDS 64 |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 39 | #define BSG_MAX_DEVS 32768 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 40 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 41 | static DEFINE_MUTEX(bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 42 | static DEFINE_IDR(bsg_minor_idr); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 43 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 44 | #define BSG_LIST_ARRAY_SIZE 8 |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 45 | static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 46 | |
| 47 | static struct class *bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 48 | static int bsg_major; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 49 | |
FUJITA Tomonori | 1c1133e | 2007-07-17 12:21:15 +0200 | [diff] [blame] | 50 | static inline struct hlist_head *bsg_dev_idx_hash(int index) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 51 | { |
FUJITA Tomonori | 1c1133e | 2007-07-17 12:21:15 +0200 | [diff] [blame] | 52 | return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 55 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 56 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 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) |
FUJITA Tomonori | 9f5de6b | 2008-04-30 13:16:21 +0900 | [diff] [blame] | 79 | return -ENOMEM; |
| 80 | } |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 81 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 82 | if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len)) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 83 | return -EFAULT; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 84 | if (blk_verify_command(sreq->cmd, mode)) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 85 | return -EPERM; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 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; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 93 | |
| 94 | /* |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 95 | * fill in all the output members |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 96 | */ |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 115 | if (rq_data_dir(rq) == READ) |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 116 | hdr->din_resid = sreq->resid_len; |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 117 | else |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 118 | hdr->dout_resid = sreq->resid_len; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 135 | static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 136 | { |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 137 | struct request *rq; |
| 138 | struct bio *bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 139 | struct sg_io_v4 hdr; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 140 | int ret; |
| 141 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 142 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
| 143 | return -EFAULT; |
| 144 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 145 | if (!q->bsg_dev.class_dev) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 146 | return -ENXIO; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 147 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 148 | if (hdr.guard != 'Q') |
| 149 | return -EINVAL; |
| 150 | ret = q->bsg_dev.ops->check_proto(&hdr); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 151 | if (ret) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 152 | return ret; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 153 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 154 | rq = blk_get_request(q, hdr.dout_xfer_len ? |
Christoph Hellwig | ff005a0 | 2018-05-09 09:54:05 +0200 | [diff] [blame] | 155 | REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 156 | if (IS_ERR(rq)) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 157 | return PTR_ERR(rq); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 158 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 159 | ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode); |
Pan Bian | 1c7b7d4 | 2021-01-19 04:33:11 -0800 | [diff] [blame] | 160 | if (ret) { |
| 161 | blk_put_request(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 162 | return ret; |
Pan Bian | 1c7b7d4 | 2021-01-19 04:33:11 -0800 | [diff] [blame] | 163 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 164 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 165 | rq->timeout = msecs_to_jiffies(hdr.timeout); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 166 | if (!rq->timeout) |
| 167 | rq->timeout = q->sg_timeout; |
| 168 | if (!rq->timeout) |
| 169 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; |
Linus Torvalds | f2f1fa7 | 2008-12-05 14:49:18 -0800 | [diff] [blame] | 170 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
| 171 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 172 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 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); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 179 | } |
Boaz Harrosh | c1c2012 | 2009-02-03 07:47:29 +0100 | [diff] [blame] | 180 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 181 | if (ret) |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 182 | goto out_free_rq; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 183 | |
| 184 | bio = rq->bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 185 | |
| 186 | blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
| 187 | ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 188 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 189 | |
| 190 | out_free_rq: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 191 | rq->q->bsg_dev.ops->free_rq(rq); |
| 192 | blk_put_request(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 193 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 194 | return -EFAULT; |
| 195 | return ret; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 198 | static struct bsg_device *bsg_alloc_device(void) |
| 199 | { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 200 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 201 | |
| 202 | bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); |
| 203 | if (unlikely(!bd)) |
| 204 | return NULL; |
| 205 | |
| 206 | spin_lock_init(&bd->lock); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 207 | bd->max_queue = BSG_DEFAULT_CMDS; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 208 | INIT_HLIST_NODE(&bd->dev_list); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 209 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int bsg_put_device(struct bsg_device *bd) |
| 213 | { |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 214 | struct request_queue *q = bd->queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 215 | |
| 216 | mutex_lock(&bsg_mutex); |
| 217 | |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 218 | if (!refcount_dec_and_test(&bd->ref_count)) { |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 219 | mutex_unlock(&bsg_mutex); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 220 | return 0; |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | hlist_del(&bd->dev_list); |
| 224 | mutex_unlock(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 225 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 226 | bsg_dbg(bd, "tearing down\n"); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * close can always block |
| 230 | */ |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 231 | kfree(bd); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 232 | blk_put_queue(q); |
| 233 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static struct bsg_device *bsg_add_device(struct inode *inode, |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 237 | struct request_queue *rq, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 238 | struct file *file) |
| 239 | { |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 240 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 241 | unsigned char buf[32]; |
Bart Van Assche | d9f9726 | 2017-05-31 14:43:47 -0700 | [diff] [blame] | 242 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 243 | lockdep_assert_held(&bsg_mutex); |
| 244 | |
Tejun Heo | 09ac46c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 245 | if (!blk_get_queue(rq)) |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 246 | return ERR_PTR(-ENXIO); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 247 | |
| 248 | bd = bsg_alloc_device(); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 249 | if (!bd) { |
| 250 | blk_put_queue(rq); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 251 | return ERR_PTR(-ENOMEM); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 252 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 253 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 254 | bd->queue = rq; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 255 | |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 256 | refcount_set(&bd->ref_count, 1); |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 257 | hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 258 | |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 259 | strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 260 | bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
FUJITA Tomonori | 9e69fbb | 2006-12-20 11:18:22 +0100 | [diff] [blame] | 261 | format_dev_t(buf, inode->i_rdev), bd->max_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 262 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 263 | return bd; |
| 264 | } |
| 265 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 266 | static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 267 | { |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 268 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 269 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 270 | lockdep_assert_held(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 271 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 272 | hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 273 | if (bd->queue == q) { |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 274 | refcount_inc(&bd->ref_count); |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 275 | goto found; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 276 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 277 | } |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 278 | bd = NULL; |
| 279 | found: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 280 | return bd; |
| 281 | } |
| 282 | |
| 283 | static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) |
| 284 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 285 | struct bsg_device *bd; |
| 286 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 287 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 288 | /* |
| 289 | * find the class device |
| 290 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 291 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 292 | bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 293 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 294 | if (!bcd) { |
| 295 | bd = ERR_PTR(-ENODEV); |
| 296 | goto out_unlock; |
| 297 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 298 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 299 | bd = __bsg_get_device(iminor(inode), bcd->queue); |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 300 | if (!bd) |
| 301 | bd = bsg_add_device(inode, bcd->queue, file); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 302 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 303 | out_unlock: |
| 304 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 305 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static int bsg_open(struct inode *inode, struct file *file) |
| 309 | { |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 310 | struct bsg_device *bd; |
| 311 | |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 312 | bd = bsg_get_device(inode, file); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 349 | 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] | 350 | { |
| 351 | struct bsg_device *bd = file->private_data; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 352 | void __user *uarg = (void __user *) arg; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 353 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 354 | switch (cmd) { |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 355 | /* |
| 356 | * Our own ioctls |
| 357 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 358 | case SG_GET_COMMAND_Q: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 359 | return bsg_get_command_q(bd, uarg); |
| 360 | case SG_SET_COMMAND_Q: |
| 361 | return bsg_set_command_q(bd, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 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: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 374 | case SCSI_IOCTL_SEND_COMMAND: |
Al Viro | 74f3c8a | 2007-08-27 15:38:10 -0400 | [diff] [blame] | 375 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 376 | case SG_IO: |
| 377 | return bsg_sg_io(bd->queue, file->f_mode, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 378 | default: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 379 | return -ENOTTY; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Arjan van de Ven | 7344be0 | 2007-10-15 11:01:53 +0200 | [diff] [blame] | 383 | static const struct file_operations bsg_fops = { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 384 | .open = bsg_open, |
| 385 | .release = bsg_release, |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 386 | .unlocked_ioctl = bsg_ioctl, |
Arnd Bergmann | fe0da4e | 2019-03-15 17:13:06 +0100 | [diff] [blame] | 387 | .compat_ioctl = compat_ptr_ioctl, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 388 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 389 | .llseek = default_llseek, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 390 | }; |
| 391 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 392 | void bsg_unregister_queue(struct request_queue *q) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 393 | { |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 394 | struct bsg_class_device *bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 395 | |
FUJITA Tomonori | df46882 | 2007-07-21 13:23:25 +0900 | [diff] [blame] | 396 | if (!bcd->class_dev) |
| 397 | return; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 398 | |
| 399 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 400 | idr_remove(&bsg_minor_idr, bcd->minor); |
Stanislaw Gruszka | 37b40ad | 2012-02-08 20:02:03 +0100 | [diff] [blame] | 401 | if (q->kobj.sd) |
| 402 | sysfs_remove_link(&q->kobj, "bsg"); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 403 | device_unregister(bcd->class_dev); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 404 | bcd->class_dev = NULL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 405 | mutex_unlock(&bsg_mutex); |
| 406 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 407 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 408 | |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 409 | int bsg_register_queue(struct request_queue *q, struct device *parent, |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 410 | const char *name, const struct bsg_ops *ops) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 411 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 412 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 413 | dev_t dev; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 414 | int ret; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 415 | struct device *class_dev = NULL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * we need a proper transport to send commands, not a stacked device |
| 419 | */ |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 420 | if (!queue_is_mq(q)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 421 | return 0; |
| 422 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 423 | bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 424 | memset(bcd, 0, sizeof(*bcd)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 425 | |
| 426 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 427 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 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 | } |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 434 | goto unlock; |
| 435 | } |
| 436 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 437 | bcd->minor = ret; |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 438 | bcd->queue = q; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 439 | bcd->ops = ops; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 440 | dev = MKDEV(bsg_major, bcd->minor); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 441 | class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 442 | if (IS_ERR(class_dev)) { |
| 443 | ret = PTR_ERR(class_dev); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 444 | goto idr_remove; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 445 | } |
| 446 | bcd->class_dev = class_dev; |
| 447 | |
Linus Torvalds | abce891 | 2007-07-16 11:18:23 -0700 | [diff] [blame] | 448 | if (q->kobj.sd) { |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 449 | ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
| 450 | if (ret) |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 451 | goto unregister_class_dev; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 454 | mutex_unlock(&bsg_mutex); |
| 455 | return 0; |
James Bottomley | 6826ee4 | 2007-07-20 16:50:10 -0500 | [diff] [blame] | 456 | |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 457 | unregister_class_dev: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 458 | device_unregister(class_dev); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 459 | idr_remove: |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 460 | idr_remove(&bsg_minor_idr, bcd->minor); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 461 | unlock: |
Jens Axboe | 264a047 | 2007-01-23 16:30:17 +0100 | [diff] [blame] | 462 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 463 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 464 | } |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 473 | return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 474 | } |
| 475 | EXPORT_SYMBOL_GPL(bsg_scsi_register_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 476 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 477 | static struct cdev bsg_cdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 478 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 479 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 2bdf914 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 480 | { |
| 481 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| 482 | } |
| 483 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 484 | static int __init bsg_init(void) |
| 485 | { |
| 486 | int ret, i; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 487 | dev_t devid; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 488 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 489 | for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 490 | INIT_HLIST_HEAD(&bsg_device_list[i]); |
| 491 | |
| 492 | bsg_class = class_create(THIS_MODULE, "bsg"); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 493 | if (IS_ERR(bsg_class)) |
| 494 | return PTR_ERR(bsg_class); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 495 | bsg_class->devnode = bsg_devnode; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 496 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 497 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 498 | if (ret) |
| 499 | goto destroy_bsg_class; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 500 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 501 | bsg_major = MAJOR(devid); |
| 502 | |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 503 | cdev_init(&bsg_cdev, &bsg_fops); |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 504 | ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 505 | if (ret) |
| 506 | goto unregister_chrdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 507 | |
Jens Axboe | 5d3a8cd | 2007-07-17 15:10:09 +0200 | [diff] [blame] | 508 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 509 | " loaded (major %d)\n", bsg_major); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 510 | return 0; |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 511 | unregister_chrdev: |
| 512 | unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
| 513 | destroy_bsg_class: |
| 514 | class_destroy(bsg_class); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 515 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | MODULE_AUTHOR("Jens Axboe"); |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 519 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 520 | MODULE_LICENSE("GPL"); |
| 521 | |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 522 | device_initcall(bsg_init); |