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); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 160 | if (ret) |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 161 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 162 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 163 | rq->timeout = msecs_to_jiffies(hdr.timeout); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 164 | if (!rq->timeout) |
| 165 | rq->timeout = q->sg_timeout; |
| 166 | if (!rq->timeout) |
| 167 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; |
Linus Torvalds | f2f1fa7 | 2008-12-05 14:49:18 -0800 | [diff] [blame] | 168 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
| 169 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 170 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 171 | if (hdr.dout_xfer_len) { |
| 172 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp), |
| 173 | hdr.dout_xfer_len, GFP_KERNEL); |
| 174 | } else if (hdr.din_xfer_len) { |
| 175 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp), |
| 176 | hdr.din_xfer_len, GFP_KERNEL); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 177 | } |
Boaz Harrosh | c1c2012 | 2009-02-03 07:47:29 +0100 | [diff] [blame] | 178 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 179 | if (ret) |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 180 | goto out_free_rq; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 181 | |
| 182 | bio = rq->bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 183 | |
| 184 | blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
| 185 | ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 186 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 187 | |
| 188 | out_free_rq: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 189 | rq->q->bsg_dev.ops->free_rq(rq); |
| 190 | blk_put_request(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 191 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 192 | return -EFAULT; |
| 193 | return ret; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 196 | static struct bsg_device *bsg_alloc_device(void) |
| 197 | { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 198 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 199 | |
| 200 | bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); |
| 201 | if (unlikely(!bd)) |
| 202 | return NULL; |
| 203 | |
| 204 | spin_lock_init(&bd->lock); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 205 | bd->max_queue = BSG_DEFAULT_CMDS; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 206 | INIT_HLIST_NODE(&bd->dev_list); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 207 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int bsg_put_device(struct bsg_device *bd) |
| 211 | { |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 212 | struct request_queue *q = bd->queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 213 | |
| 214 | mutex_lock(&bsg_mutex); |
| 215 | |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 216 | if (!refcount_dec_and_test(&bd->ref_count)) { |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 217 | mutex_unlock(&bsg_mutex); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 218 | return 0; |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | hlist_del(&bd->dev_list); |
| 222 | mutex_unlock(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 223 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 224 | bsg_dbg(bd, "tearing down\n"); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * close can always block |
| 228 | */ |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 229 | kfree(bd); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 230 | blk_put_queue(q); |
| 231 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static struct bsg_device *bsg_add_device(struct inode *inode, |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 235 | struct request_queue *rq, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 236 | struct file *file) |
| 237 | { |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 238 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 239 | unsigned char buf[32]; |
Bart Van Assche | d9f9726 | 2017-05-31 14:43:47 -0700 | [diff] [blame] | 240 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 241 | lockdep_assert_held(&bsg_mutex); |
| 242 | |
Tejun Heo | 09ac46c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 243 | if (!blk_get_queue(rq)) |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 244 | return ERR_PTR(-ENXIO); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 245 | |
| 246 | bd = bsg_alloc_device(); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 247 | if (!bd) { |
| 248 | blk_put_queue(rq); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 249 | return ERR_PTR(-ENOMEM); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 250 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 251 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 252 | bd->queue = rq; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 253 | |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 254 | refcount_set(&bd->ref_count, 1); |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 255 | hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 256 | |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 257 | 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] | 258 | bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
FUJITA Tomonori | 9e69fbb | 2006-12-20 11:18:22 +0100 | [diff] [blame] | 259 | format_dev_t(buf, inode->i_rdev), bd->max_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 260 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 261 | return bd; |
| 262 | } |
| 263 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 264 | 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] | 265 | { |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 266 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 267 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 268 | lockdep_assert_held(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 269 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 270 | hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 271 | if (bd->queue == q) { |
John Pittman | db193954 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 272 | refcount_inc(&bd->ref_count); |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 273 | goto found; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 274 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 275 | } |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 276 | bd = NULL; |
| 277 | found: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 278 | return bd; |
| 279 | } |
| 280 | |
| 281 | static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) |
| 282 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 283 | struct bsg_device *bd; |
| 284 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 285 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 286 | /* |
| 287 | * find the class device |
| 288 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 289 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 290 | bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 291 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 292 | if (!bcd) { |
| 293 | bd = ERR_PTR(-ENODEV); |
| 294 | goto out_unlock; |
| 295 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 296 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 297 | bd = __bsg_get_device(iminor(inode), bcd->queue); |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 298 | if (!bd) |
| 299 | bd = bsg_add_device(inode, bcd->queue, file); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 300 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 301 | out_unlock: |
| 302 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 303 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int bsg_open(struct inode *inode, struct file *file) |
| 307 | { |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 308 | struct bsg_device *bd; |
| 309 | |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 310 | bd = bsg_get_device(inode, file); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 311 | |
| 312 | if (IS_ERR(bd)) |
| 313 | return PTR_ERR(bd); |
| 314 | |
| 315 | file->private_data = bd; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int bsg_release(struct inode *inode, struct file *file) |
| 320 | { |
| 321 | struct bsg_device *bd = file->private_data; |
| 322 | |
| 323 | file->private_data = NULL; |
| 324 | return bsg_put_device(bd); |
| 325 | } |
| 326 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 327 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
| 328 | { |
| 329 | return put_user(bd->max_queue, uarg); |
| 330 | } |
| 331 | |
| 332 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) |
| 333 | { |
| 334 | int queue; |
| 335 | |
| 336 | if (get_user(queue, uarg)) |
| 337 | return -EFAULT; |
| 338 | if (queue < 1) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | spin_lock_irq(&bd->lock); |
| 342 | bd->max_queue = queue; |
| 343 | spin_unlock_irq(&bd->lock); |
| 344 | return 0; |
| 345 | } |
| 346 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 347 | 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] | 348 | { |
| 349 | struct bsg_device *bd = file->private_data; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 350 | void __user *uarg = (void __user *) arg; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 351 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 352 | switch (cmd) { |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 353 | /* |
| 354 | * Our own ioctls |
| 355 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 356 | case SG_GET_COMMAND_Q: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 357 | return bsg_get_command_q(bd, uarg); |
| 358 | case SG_SET_COMMAND_Q: |
| 359 | return bsg_set_command_q(bd, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * SCSI/sg ioctls |
| 363 | */ |
| 364 | case SG_GET_VERSION_NUM: |
| 365 | case SCSI_IOCTL_GET_IDLUN: |
| 366 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 367 | case SG_SET_TIMEOUT: |
| 368 | case SG_GET_TIMEOUT: |
| 369 | case SG_GET_RESERVED_SIZE: |
| 370 | case SG_SET_RESERVED_SIZE: |
| 371 | case SG_EMULATED_HOST: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 372 | case SCSI_IOCTL_SEND_COMMAND: |
Al Viro | 74f3c8a | 2007-08-27 15:38:10 -0400 | [diff] [blame] | 373 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 374 | case SG_IO: |
| 375 | return bsg_sg_io(bd->queue, file->f_mode, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 376 | default: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 377 | return -ENOTTY; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Arjan van de Ven | 7344be0 | 2007-10-15 11:01:53 +0200 | [diff] [blame] | 381 | static const struct file_operations bsg_fops = { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 382 | .open = bsg_open, |
| 383 | .release = bsg_release, |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 384 | .unlocked_ioctl = bsg_ioctl, |
Arnd Bergmann | fe0da4e | 2019-03-15 17:13:06 +0100 | [diff] [blame] | 385 | .compat_ioctl = compat_ptr_ioctl, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 386 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 387 | .llseek = default_llseek, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 388 | }; |
| 389 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 390 | void bsg_unregister_queue(struct request_queue *q) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 391 | { |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 392 | struct bsg_class_device *bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 393 | |
FUJITA Tomonori | df46882 | 2007-07-21 13:23:25 +0900 | [diff] [blame] | 394 | if (!bcd->class_dev) |
| 395 | return; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 396 | |
| 397 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 398 | idr_remove(&bsg_minor_idr, bcd->minor); |
Stanislaw Gruszka | 37b40ad | 2012-02-08 20:02:03 +0100 | [diff] [blame] | 399 | if (q->kobj.sd) |
| 400 | sysfs_remove_link(&q->kobj, "bsg"); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 401 | device_unregister(bcd->class_dev); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 402 | bcd->class_dev = NULL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 403 | mutex_unlock(&bsg_mutex); |
| 404 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 405 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 406 | |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 407 | int bsg_register_queue(struct request_queue *q, struct device *parent, |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 408 | const char *name, const struct bsg_ops *ops) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 409 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 410 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 411 | dev_t dev; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 412 | int ret; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 413 | struct device *class_dev = NULL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * we need a proper transport to send commands, not a stacked device |
| 417 | */ |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 418 | if (!queue_is_mq(q)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 419 | return 0; |
| 420 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 421 | bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 422 | memset(bcd, 0, sizeof(*bcd)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 423 | |
| 424 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 425 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 426 | ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); |
| 427 | if (ret < 0) { |
| 428 | if (ret == -ENOSPC) { |
| 429 | printk(KERN_ERR "bsg: too many bsg devices\n"); |
| 430 | ret = -EINVAL; |
| 431 | } |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 432 | goto unlock; |
| 433 | } |
| 434 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 435 | bcd->minor = ret; |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 436 | bcd->queue = q; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 437 | bcd->ops = ops; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 438 | dev = MKDEV(bsg_major, bcd->minor); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 439 | class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 440 | if (IS_ERR(class_dev)) { |
| 441 | ret = PTR_ERR(class_dev); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 442 | goto idr_remove; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 443 | } |
| 444 | bcd->class_dev = class_dev; |
| 445 | |
Linus Torvalds | abce891 | 2007-07-16 11:18:23 -0700 | [diff] [blame] | 446 | if (q->kobj.sd) { |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 447 | ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
| 448 | if (ret) |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 449 | goto unregister_class_dev; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 452 | mutex_unlock(&bsg_mutex); |
| 453 | return 0; |
James Bottomley | 6826ee4 | 2007-07-20 16:50:10 -0500 | [diff] [blame] | 454 | |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 455 | unregister_class_dev: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 456 | device_unregister(class_dev); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 457 | idr_remove: |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 458 | idr_remove(&bsg_minor_idr, bcd->minor); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 459 | unlock: |
Jens Axboe | 264a047 | 2007-01-23 16:30:17 +0100 | [diff] [blame] | 460 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 461 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 462 | } |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 463 | |
| 464 | int bsg_scsi_register_queue(struct request_queue *q, struct device *parent) |
| 465 | { |
| 466 | if (!blk_queue_scsi_passthrough(q)) { |
| 467 | WARN_ONCE(true, "Attempt to register a non-SCSI queue\n"); |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 471 | return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 472 | } |
| 473 | EXPORT_SYMBOL_GPL(bsg_scsi_register_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 474 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 475 | static struct cdev bsg_cdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 476 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 477 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 2bdf914 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 478 | { |
| 479 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| 480 | } |
| 481 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 482 | static int __init bsg_init(void) |
| 483 | { |
| 484 | int ret, i; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 485 | dev_t devid; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 486 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 487 | for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 488 | INIT_HLIST_HEAD(&bsg_device_list[i]); |
| 489 | |
| 490 | bsg_class = class_create(THIS_MODULE, "bsg"); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 491 | if (IS_ERR(bsg_class)) |
| 492 | return PTR_ERR(bsg_class); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 493 | bsg_class->devnode = bsg_devnode; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 494 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 495 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 496 | if (ret) |
| 497 | goto destroy_bsg_class; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 498 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 499 | bsg_major = MAJOR(devid); |
| 500 | |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 501 | cdev_init(&bsg_cdev, &bsg_fops); |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 502 | ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 503 | if (ret) |
| 504 | goto unregister_chrdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 505 | |
Jens Axboe | 5d3a8cd | 2007-07-17 15:10:09 +0200 | [diff] [blame] | 506 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 507 | " loaded (major %d)\n", bsg_major); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 508 | return 0; |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 509 | unregister_chrdev: |
| 510 | unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
| 511 | destroy_bsg_class: |
| 512 | class_destroy(bsg_class); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 513 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | MODULE_AUTHOR("Jens Axboe"); |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 517 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 518 | MODULE_LICENSE("GPL"); |
| 519 | |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 520 | device_initcall(bsg_init); |