Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1 | /* |
FUJITA Tomonori | 0c6a89b | 2007-07-29 23:00:46 +0900 | [diff] [blame] | 2 | * bsg.c - block layer implementation of the sg v4 interface |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs |
| 5 | * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com> |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License version 2. See the file "COPYING" in the main directory of this |
| 9 | * archive for more details. |
| 10 | * |
| 11 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/file.h> |
| 15 | #include <linux/blkdev.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 16 | #include <linux/cdev.h> |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 17 | #include <linux/jiffies.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 18 | #include <linux/percpu.h> |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 19 | #include <linux/idr.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 20 | #include <linux/bsg.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 22 | |
| 23 | #include <scsi/scsi.h> |
| 24 | #include <scsi/scsi_ioctl.h> |
| 25 | #include <scsi/scsi_cmnd.h> |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 26 | #include <scsi/scsi_device.h> |
| 27 | #include <scsi/scsi_driver.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 28 | #include <scsi/sg.h> |
| 29 | |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 30 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
| 31 | #define BSG_VERSION "0.4" |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 32 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 33 | #define bsg_dbg(bd, fmt, ...) \ |
| 34 | pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__) |
| 35 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 36 | struct bsg_device { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 37 | struct request_queue *queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 38 | spinlock_t lock; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 39 | struct hlist_node dev_list; |
John Pittman | db19395 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 40 | refcount_t ref_count; |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 41 | char name[20]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 42 | int max_queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 45 | #define BSG_DEFAULT_CMDS 64 |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 46 | #define BSG_MAX_DEVS 32768 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 47 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 48 | static DEFINE_MUTEX(bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 49 | static DEFINE_IDR(bsg_minor_idr); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 50 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 51 | #define BSG_LIST_ARRAY_SIZE 8 |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 52 | static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 53 | |
| 54 | static struct class *bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 55 | static int bsg_major; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 56 | |
FUJITA Tomonori | 1c1133e | 2007-07-17 12:21:15 +0200 | [diff] [blame] | 57 | static inline struct hlist_head *bsg_dev_idx_hash(int index) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 58 | { |
FUJITA Tomonori | 1c1133e | 2007-07-17 12:21:15 +0200 | [diff] [blame] | 59 | return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 62 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 63 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 64 | static int bsg_scsi_check_proto(struct sg_io_v4 *hdr) |
| 65 | { |
| 66 | if (hdr->protocol != BSG_PROTOCOL_SCSI || |
| 67 | hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD) |
| 68 | return -EINVAL; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr, |
| 73 | fmode_t mode) |
| 74 | { |
| 75 | struct scsi_request *sreq = scsi_req(rq); |
| 76 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 77 | if (hdr->dout_xfer_len && hdr->din_xfer_len) { |
| 78 | pr_warn_once("BIDI support in bsg has been removed.\n"); |
| 79 | return -EOPNOTSUPP; |
| 80 | } |
| 81 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 82 | sreq->cmd_len = hdr->request_len; |
| 83 | if (sreq->cmd_len > BLK_MAX_CDB) { |
| 84 | sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL); |
| 85 | if (!sreq->cmd) |
FUJITA Tomonori | 9f5de6b | 2008-04-30 13:16:21 +0900 | [diff] [blame] | 86 | return -ENOMEM; |
| 87 | } |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 88 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 89 | if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len)) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 90 | return -EFAULT; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 91 | if (blk_verify_command(sreq->cmd, mode)) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 92 | return -EPERM; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr) |
| 97 | { |
| 98 | struct scsi_request *sreq = scsi_req(rq); |
| 99 | int ret = 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 100 | |
| 101 | /* |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 102 | * fill in all the output members |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 103 | */ |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 104 | hdr->device_status = sreq->result & 0xff; |
| 105 | hdr->transport_status = host_byte(sreq->result); |
| 106 | hdr->driver_status = driver_byte(sreq->result); |
| 107 | hdr->info = 0; |
| 108 | if (hdr->device_status || hdr->transport_status || hdr->driver_status) |
| 109 | hdr->info |= SG_INFO_CHECK; |
| 110 | hdr->response_len = 0; |
| 111 | |
| 112 | if (sreq->sense_len && hdr->response) { |
| 113 | int len = min_t(unsigned int, hdr->max_response_len, |
| 114 | sreq->sense_len); |
| 115 | |
| 116 | if (copy_to_user(uptr64(hdr->response), sreq->sense, len)) |
| 117 | ret = -EFAULT; |
| 118 | else |
| 119 | hdr->response_len = len; |
| 120 | } |
| 121 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 122 | if (rq_data_dir(rq) == READ) |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 123 | hdr->din_resid = sreq->resid_len; |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 124 | else |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 125 | hdr->dout_resid = sreq->resid_len; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static void bsg_scsi_free_rq(struct request *rq) |
| 131 | { |
| 132 | scsi_req_free_cmd(scsi_req(rq)); |
| 133 | } |
| 134 | |
| 135 | static const struct bsg_ops bsg_scsi_ops = { |
| 136 | .check_proto = bsg_scsi_check_proto, |
| 137 | .fill_hdr = bsg_scsi_fill_hdr, |
| 138 | .complete_rq = bsg_scsi_complete_rq, |
| 139 | .free_rq = bsg_scsi_free_rq, |
| 140 | }; |
| 141 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 142 | 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] | 143 | { |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 144 | struct request *rq; |
| 145 | struct bio *bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 146 | struct sg_io_v4 hdr; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 147 | int ret; |
| 148 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 149 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
| 150 | return -EFAULT; |
| 151 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 152 | if (!q->bsg_dev.class_dev) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 153 | return -ENXIO; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 154 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 155 | if (hdr.guard != 'Q') |
| 156 | return -EINVAL; |
| 157 | ret = q->bsg_dev.ops->check_proto(&hdr); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 158 | if (ret) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 159 | return ret; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 160 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 161 | rq = blk_get_request(q, hdr.dout_xfer_len ? |
Christoph Hellwig | ff005a0 | 2018-05-09 09:54:05 +0200 | [diff] [blame] | 162 | REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 163 | if (IS_ERR(rq)) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 164 | return PTR_ERR(rq); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 165 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 166 | ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 167 | if (ret) |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 168 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 169 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 170 | rq->timeout = msecs_to_jiffies(hdr.timeout); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 171 | if (!rq->timeout) |
| 172 | rq->timeout = q->sg_timeout; |
| 173 | if (!rq->timeout) |
| 174 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; |
Linus Torvalds | f2f1fa7 | 2008-12-05 14:49:18 -0800 | [diff] [blame] | 175 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
| 176 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 177 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 178 | if (hdr.dout_xfer_len) { |
| 179 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp), |
| 180 | hdr.dout_xfer_len, GFP_KERNEL); |
| 181 | } else if (hdr.din_xfer_len) { |
| 182 | ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp), |
| 183 | hdr.din_xfer_len, GFP_KERNEL); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 184 | } |
Boaz Harrosh | c1c2012 | 2009-02-03 07:47:29 +0100 | [diff] [blame] | 185 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 186 | if (ret) |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 187 | goto out_free_rq; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 188 | |
| 189 | bio = rq->bio; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 190 | |
| 191 | blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
| 192 | ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 193 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 194 | |
| 195 | out_free_rq: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 196 | rq->q->bsg_dev.ops->free_rq(rq); |
| 197 | blk_put_request(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame^] | 198 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 199 | return -EFAULT; |
| 200 | return ret; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 203 | static struct bsg_device *bsg_alloc_device(void) |
| 204 | { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 205 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 206 | |
| 207 | bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); |
| 208 | if (unlikely(!bd)) |
| 209 | return NULL; |
| 210 | |
| 211 | spin_lock_init(&bd->lock); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 212 | bd->max_queue = BSG_DEFAULT_CMDS; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 213 | INIT_HLIST_NODE(&bd->dev_list); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 214 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static int bsg_put_device(struct bsg_device *bd) |
| 218 | { |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 219 | struct request_queue *q = bd->queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 220 | |
| 221 | mutex_lock(&bsg_mutex); |
| 222 | |
John Pittman | db19395 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 223 | if (!refcount_dec_and_test(&bd->ref_count)) { |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 224 | mutex_unlock(&bsg_mutex); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 225 | return 0; |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | hlist_del(&bd->dev_list); |
| 229 | mutex_unlock(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 230 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 231 | bsg_dbg(bd, "tearing down\n"); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | * close can always block |
| 235 | */ |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 236 | kfree(bd); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 237 | blk_put_queue(q); |
| 238 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static struct bsg_device *bsg_add_device(struct inode *inode, |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 242 | struct request_queue *rq, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 243 | struct file *file) |
| 244 | { |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 245 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 246 | unsigned char buf[32]; |
Bart Van Assche | d9f9726 | 2017-05-31 14:43:47 -0700 | [diff] [blame] | 247 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 248 | lockdep_assert_held(&bsg_mutex); |
| 249 | |
Tejun Heo | 09ac46c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 250 | if (!blk_get_queue(rq)) |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 251 | return ERR_PTR(-ENXIO); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 252 | |
| 253 | bd = bsg_alloc_device(); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 254 | if (!bd) { |
| 255 | blk_put_queue(rq); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 256 | return ERR_PTR(-ENOMEM); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 257 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 258 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 259 | bd->queue = rq; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 260 | |
John Pittman | db19395 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 261 | refcount_set(&bd->ref_count, 1); |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 262 | hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 263 | |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 264 | 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] | 265 | bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
FUJITA Tomonori | 9e69fbb | 2006-12-20 11:18:22 +0100 | [diff] [blame] | 266 | format_dev_t(buf, inode->i_rdev), bd->max_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 267 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 268 | return bd; |
| 269 | } |
| 270 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 271 | 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] | 272 | { |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 273 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 274 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 275 | lockdep_assert_held(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 276 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 277 | hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 278 | if (bd->queue == q) { |
John Pittman | db19395 | 2018-08-27 14:33:05 -0400 | [diff] [blame] | 279 | refcount_inc(&bd->ref_count); |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 280 | goto found; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 281 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 282 | } |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 283 | bd = NULL; |
| 284 | found: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 285 | return bd; |
| 286 | } |
| 287 | |
| 288 | static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) |
| 289 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 290 | struct bsg_device *bd; |
| 291 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 292 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 293 | /* |
| 294 | * find the class device |
| 295 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 296 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 297 | bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 298 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 299 | if (!bcd) { |
| 300 | bd = ERR_PTR(-ENODEV); |
| 301 | goto out_unlock; |
| 302 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 303 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 304 | bd = __bsg_get_device(iminor(inode), bcd->queue); |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 305 | if (!bd) |
| 306 | bd = bsg_add_device(inode, bcd->queue, file); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 307 | |
Anatoliy Glagolev | d6c7396 | 2018-06-13 15:38:51 -0600 | [diff] [blame] | 308 | out_unlock: |
| 309 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 310 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int bsg_open(struct inode *inode, struct file *file) |
| 314 | { |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 315 | struct bsg_device *bd; |
| 316 | |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 317 | bd = bsg_get_device(inode, file); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 318 | |
| 319 | if (IS_ERR(bd)) |
| 320 | return PTR_ERR(bd); |
| 321 | |
| 322 | file->private_data = bd; |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int bsg_release(struct inode *inode, struct file *file) |
| 327 | { |
| 328 | struct bsg_device *bd = file->private_data; |
| 329 | |
| 330 | file->private_data = NULL; |
| 331 | return bsg_put_device(bd); |
| 332 | } |
| 333 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 334 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
| 335 | { |
| 336 | return put_user(bd->max_queue, uarg); |
| 337 | } |
| 338 | |
| 339 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) |
| 340 | { |
| 341 | int queue; |
| 342 | |
| 343 | if (get_user(queue, uarg)) |
| 344 | return -EFAULT; |
| 345 | if (queue < 1) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | spin_lock_irq(&bd->lock); |
| 349 | bd->max_queue = queue; |
| 350 | spin_unlock_irq(&bd->lock); |
| 351 | return 0; |
| 352 | } |
| 353 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 354 | 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] | 355 | { |
| 356 | struct bsg_device *bd = file->private_data; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 357 | void __user *uarg = (void __user *) arg; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 358 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 359 | switch (cmd) { |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 360 | /* |
| 361 | * Our own ioctls |
| 362 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 363 | case SG_GET_COMMAND_Q: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 364 | return bsg_get_command_q(bd, uarg); |
| 365 | case SG_SET_COMMAND_Q: |
| 366 | return bsg_set_command_q(bd, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * SCSI/sg ioctls |
| 370 | */ |
| 371 | case SG_GET_VERSION_NUM: |
| 372 | case SCSI_IOCTL_GET_IDLUN: |
| 373 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 374 | case SG_SET_TIMEOUT: |
| 375 | case SG_GET_TIMEOUT: |
| 376 | case SG_GET_RESERVED_SIZE: |
| 377 | case SG_SET_RESERVED_SIZE: |
| 378 | case SG_EMULATED_HOST: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 379 | case SCSI_IOCTL_SEND_COMMAND: |
Al Viro | 74f3c8a | 2007-08-27 15:38:10 -0400 | [diff] [blame] | 380 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 381 | case SG_IO: |
| 382 | return bsg_sg_io(bd->queue, file->f_mode, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 383 | default: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 384 | return -ENOTTY; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Arjan van de Ven | 7344be0 | 2007-10-15 11:01:53 +0200 | [diff] [blame] | 388 | static const struct file_operations bsg_fops = { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 389 | .open = bsg_open, |
| 390 | .release = bsg_release, |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 391 | .unlocked_ioctl = bsg_ioctl, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 392 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 393 | .llseek = default_llseek, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 394 | }; |
| 395 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 396 | void bsg_unregister_queue(struct request_queue *q) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 397 | { |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 398 | struct bsg_class_device *bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 399 | |
FUJITA Tomonori | df46882 | 2007-07-21 13:23:25 +0900 | [diff] [blame] | 400 | if (!bcd->class_dev) |
| 401 | return; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 402 | |
| 403 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 404 | idr_remove(&bsg_minor_idr, bcd->minor); |
Stanislaw Gruszka | 37b40ad | 2012-02-08 20:02:03 +0100 | [diff] [blame] | 405 | if (q->kobj.sd) |
| 406 | sysfs_remove_link(&q->kobj, "bsg"); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 407 | device_unregister(bcd->class_dev); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 408 | bcd->class_dev = NULL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 409 | mutex_unlock(&bsg_mutex); |
| 410 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 411 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 412 | |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 413 | int bsg_register_queue(struct request_queue *q, struct device *parent, |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 414 | const char *name, const struct bsg_ops *ops) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 415 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 416 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 417 | dev_t dev; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 418 | int ret; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 419 | struct device *class_dev = NULL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * we need a proper transport to send commands, not a stacked device |
| 423 | */ |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 424 | if (!queue_is_mq(q)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 425 | return 0; |
| 426 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 427 | bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 428 | memset(bcd, 0, sizeof(*bcd)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 429 | |
| 430 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 431 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 432 | ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); |
| 433 | if (ret < 0) { |
| 434 | if (ret == -ENOSPC) { |
| 435 | printk(KERN_ERR "bsg: too many bsg devices\n"); |
| 436 | ret = -EINVAL; |
| 437 | } |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 438 | goto unlock; |
| 439 | } |
| 440 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 441 | bcd->minor = ret; |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 442 | bcd->queue = q; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 443 | bcd->ops = ops; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 444 | dev = MKDEV(bsg_major, bcd->minor); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 445 | class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 446 | if (IS_ERR(class_dev)) { |
| 447 | ret = PTR_ERR(class_dev); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 448 | goto idr_remove; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 449 | } |
| 450 | bcd->class_dev = class_dev; |
| 451 | |
Linus Torvalds | abce891 | 2007-07-16 11:18:23 -0700 | [diff] [blame] | 452 | if (q->kobj.sd) { |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 453 | ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
| 454 | if (ret) |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 455 | goto unregister_class_dev; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 458 | mutex_unlock(&bsg_mutex); |
| 459 | return 0; |
James Bottomley | 6826ee4 | 2007-07-20 16:50:10 -0500 | [diff] [blame] | 460 | |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 461 | unregister_class_dev: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 462 | device_unregister(class_dev); |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 463 | idr_remove: |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 464 | idr_remove(&bsg_minor_idr, bcd->minor); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 465 | unlock: |
Jens Axboe | 264a047 | 2007-01-23 16:30:17 +0100 | [diff] [blame] | 466 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 467 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 468 | } |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 469 | |
| 470 | int bsg_scsi_register_queue(struct request_queue *q, struct device *parent) |
| 471 | { |
| 472 | if (!blk_queue_scsi_passthrough(q)) { |
| 473 | WARN_ONCE(true, "Attempt to register a non-SCSI queue\n"); |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 477 | return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 478 | } |
| 479 | EXPORT_SYMBOL_GPL(bsg_scsi_register_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 480 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 481 | static struct cdev bsg_cdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 482 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 483 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 2bdf914 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 484 | { |
| 485 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| 486 | } |
| 487 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 488 | static int __init bsg_init(void) |
| 489 | { |
| 490 | int ret, i; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 491 | dev_t devid; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 492 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 493 | for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 494 | INIT_HLIST_HEAD(&bsg_device_list[i]); |
| 495 | |
| 496 | bsg_class = class_create(THIS_MODULE, "bsg"); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 497 | if (IS_ERR(bsg_class)) |
| 498 | return PTR_ERR(bsg_class); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 499 | bsg_class->devnode = bsg_devnode; |
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 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 502 | if (ret) |
| 503 | goto destroy_bsg_class; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 504 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 505 | bsg_major = MAJOR(devid); |
| 506 | |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 507 | cdev_init(&bsg_cdev, &bsg_fops); |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 508 | ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 509 | if (ret) |
| 510 | goto unregister_chrdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 511 | |
Jens Axboe | 5d3a8cd | 2007-07-17 15:10:09 +0200 | [diff] [blame] | 512 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 513 | " loaded (major %d)\n", bsg_major); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 514 | return 0; |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 515 | unregister_chrdev: |
| 516 | unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
| 517 | destroy_bsg_class: |
| 518 | class_destroy(bsg_class); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 519 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | MODULE_AUTHOR("Jens Axboe"); |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 523 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 524 | MODULE_LICENSE("GPL"); |
| 525 | |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 526 | device_initcall(bsg_init); |