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