blob: 82d58829ba591eeef13a3ed4eca96b8a019c4d85 [file] [log] [blame]
Jens Axboe3d6392c2007-07-09 12:38:05 +02001/*
FUJITA Tomonori0c6a89b2007-07-29 23:00:46 +09002 * bsg.c - block layer implementation of the sg v4 interface
Jens Axboe3d6392c2007-07-09 12:38:05 +02003 *
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 Axboe3d6392c2007-07-09 12:38:05 +020012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/file.h>
15#include <linux/blkdev.h>
16#include <linux/poll.h>
17#include <linux/cdev.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010018#include <linux/jiffies.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020019#include <linux/percpu.h>
20#include <linux/uio.h>
FUJITA Tomonori598443a2007-07-23 09:33:26 +090021#include <linux/idr.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020022#include <linux/bsg.h>
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -060023#include <linux/smp_lock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020025
26#include <scsi/scsi.h>
27#include <scsi/scsi_ioctl.h>
28#include <scsi/scsi_cmnd.h>
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +020029#include <scsi/scsi_device.h>
30#include <scsi/scsi_driver.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020031#include <scsi/sg.h>
32
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +020033#define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
34#define BSG_VERSION "0.4"
Jens Axboe3d6392c2007-07-09 12:38:05 +020035
Jens Axboe3d6392c2007-07-09 12:38:05 +020036struct bsg_device {
Jens Axboe165125e2007-07-24 09:28:11 +020037 struct request_queue *queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020038 spinlock_t lock;
39 struct list_head busy_list;
40 struct list_head done_list;
41 struct hlist_node dev_list;
42 atomic_t ref_count;
Jens Axboe3d6392c2007-07-09 12:38:05 +020043 int queued_cmds;
44 int done_cmds;
Jens Axboe3d6392c2007-07-09 12:38:05 +020045 wait_queue_head_t wq_done;
46 wait_queue_head_t wq_free;
Kay Sievers3ada8b72009-01-06 10:44:43 -080047 char name[20];
Jens Axboe3d6392c2007-07-09 12:38:05 +020048 int max_queue;
49 unsigned long flags;
50};
51
52enum {
53 BSG_F_BLOCK = 1,
Jens Axboe3d6392c2007-07-09 12:38:05 +020054};
55
Jens Axboe5309cb32007-01-23 16:24:41 +010056#define BSG_DEFAULT_CMDS 64
FUJITA Tomonori292b7f22007-03-28 13:29:58 +020057#define BSG_MAX_DEVS 32768
Jens Axboe3d6392c2007-07-09 12:38:05 +020058
59#undef BSG_DEBUG
60
61#ifdef BSG_DEBUG
Harvey Harrison24c03d42008-05-01 04:35:17 -070062#define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ##args)
Jens Axboe3d6392c2007-07-09 12:38:05 +020063#else
64#define dprintk(fmt, args...)
65#endif
66
Jens Axboe3d6392c2007-07-09 12:38:05 +020067static DEFINE_MUTEX(bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +090068static DEFINE_IDR(bsg_minor_idr);
Jens Axboe3d6392c2007-07-09 12:38:05 +020069
Jens Axboe25fd1642007-07-17 08:52:29 +020070#define BSG_LIST_ARRAY_SIZE 8
Jens Axboe25fd1642007-07-17 08:52:29 +020071static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
Jens Axboe3d6392c2007-07-09 12:38:05 +020072
73static struct class *bsg_class;
Jens Axboe46f6ef42007-07-17 08:56:10 +020074static int bsg_major;
Jens Axboe3d6392c2007-07-09 12:38:05 +020075
Jens Axboe5309cb32007-01-23 16:24:41 +010076static struct kmem_cache *bsg_cmd_cachep;
77
Jens Axboe3d6392c2007-07-09 12:38:05 +020078/*
79 * our internal command type
80 */
81struct bsg_command {
82 struct bsg_device *bd;
83 struct list_head list;
84 struct request *rq;
85 struct bio *bio;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +020086 struct bio *bidi_bio;
Jens Axboe3d6392c2007-07-09 12:38:05 +020087 int err;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010088 struct sg_io_v4 hdr;
Jens Axboe3d6392c2007-07-09 12:38:05 +020089 char sense[SCSI_SENSE_BUFFERSIZE];
90};
91
92static void bsg_free_command(struct bsg_command *bc)
93{
94 struct bsg_device *bd = bc->bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +020095 unsigned long flags;
96
Jens Axboe5309cb32007-01-23 16:24:41 +010097 kmem_cache_free(bsg_cmd_cachep, bc);
Jens Axboe3d6392c2007-07-09 12:38:05 +020098
99 spin_lock_irqsave(&bd->lock, flags);
100 bd->queued_cmds--;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200101 spin_unlock_irqrestore(&bd->lock, flags);
102
103 wake_up(&bd->wq_free);
104}
105
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200106static struct bsg_command *bsg_alloc_command(struct bsg_device *bd)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200107{
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200108 struct bsg_command *bc = ERR_PTR(-EINVAL);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200109
110 spin_lock_irq(&bd->lock);
111
112 if (bd->queued_cmds >= bd->max_queue)
113 goto out;
114
Jens Axboe3d6392c2007-07-09 12:38:05 +0200115 bd->queued_cmds++;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200116 spin_unlock_irq(&bd->lock);
117
Jens Axboe25fd1642007-07-17 08:52:29 +0200118 bc = kmem_cache_zalloc(bsg_cmd_cachep, GFP_KERNEL);
Jens Axboe5309cb32007-01-23 16:24:41 +0100119 if (unlikely(!bc)) {
120 spin_lock_irq(&bd->lock);
FUJITA Tomonori7e75d732007-01-24 09:05:54 +0100121 bd->queued_cmds--;
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200122 bc = ERR_PTR(-ENOMEM);
FUJITA Tomonori7e75d732007-01-24 09:05:54 +0100123 goto out;
Jens Axboe5309cb32007-01-23 16:24:41 +0100124 }
125
Jens Axboe3d6392c2007-07-09 12:38:05 +0200126 bc->bd = bd;
127 INIT_LIST_HEAD(&bc->list);
Jens Axboe5309cb32007-01-23 16:24:41 +0100128 dprintk("%s: returning free cmd %p\n", bd->name, bc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200129 return bc;
130out:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200131 spin_unlock_irq(&bd->lock);
132 return bc;
133}
134
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +0200135static inline struct hlist_head *bsg_dev_idx_hash(int index)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200136{
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +0200137 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
Jens Axboe3d6392c2007-07-09 12:38:05 +0200138}
139
Jens Axboe25fd1642007-07-17 08:52:29 +0200140static int bsg_io_schedule(struct bsg_device *bd)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200141{
142 DEFINE_WAIT(wait);
143 int ret = 0;
144
145 spin_lock_irq(&bd->lock);
146
147 BUG_ON(bd->done_cmds > bd->queued_cmds);
148
149 /*
150 * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
151 * work to do", even though we return -ENOSPC after this same test
152 * during bsg_write() -- there, it means our buffer can't have more
153 * bsg_commands added to it, thus has no space left.
154 */
155 if (bd->done_cmds == bd->queued_cmds) {
156 ret = -ENODATA;
157 goto unlock;
158 }
159
160 if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
161 ret = -EAGAIN;
162 goto unlock;
163 }
164
Jens Axboe25fd1642007-07-17 08:52:29 +0200165 prepare_to_wait(&bd->wq_done, &wait, TASK_UNINTERRUPTIBLE);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200166 spin_unlock_irq(&bd->lock);
167 io_schedule();
168 finish_wait(&bd->wq_done, &wait);
169
Jens Axboe3d6392c2007-07-09 12:38:05 +0200170 return ret;
171unlock:
172 spin_unlock_irq(&bd->lock);
173 return ret;
174}
175
Jens Axboe165125e2007-07-24 09:28:11 +0200176static int blk_fill_sgv4_hdr_rq(struct request_queue *q, struct request *rq,
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900177 struct sg_io_v4 *hdr, struct bsg_device *bd,
Al Viroaeb5d722008-09-02 15:28:45 -0400178 fmode_t has_write_perm)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200179{
FUJITA Tomonori9f5de6b2008-04-30 13:16:21 +0900180 if (hdr->request_len > BLK_MAX_CDB) {
181 rq->cmd = kzalloc(hdr->request_len, GFP_KERNEL);
182 if (!rq->cmd)
183 return -ENOMEM;
184 }
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100185
186 if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
187 hdr->request_len))
188 return -EFAULT;
FUJITA Tomonori15d10b62007-07-16 08:52:16 +0200189
190 if (hdr->subprotocol == BSG_SUB_PROTOCOL_SCSI_CMD) {
Jens Axboe018e0442009-06-26 16:27:10 +0200191 if (blk_verify_command(rq->cmd, has_write_perm))
FUJITA Tomonori15d10b62007-07-16 08:52:16 +0200192 return -EPERM;
193 } else if (!capable(CAP_SYS_RAWIO))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100194 return -EPERM;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200195
196 /*
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100197 * fill in request structure
Jens Axboe3d6392c2007-07-09 12:38:05 +0200198 */
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100199 rq->cmd_len = hdr->request_len;
200 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200201
Randy Dunlapad5ebd22009-11-11 13:47:45 +0100202 rq->timeout = msecs_to_jiffies(hdr->timeout);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100203 if (!rq->timeout)
204 rq->timeout = q->sg_timeout;
205 if (!rq->timeout)
206 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
Linus Torvaldsf2f1fa72008-12-05 14:49:18 -0800207 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
208 rq->timeout = BLK_MIN_SG_TIMEOUT;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200209
210 return 0;
211}
212
213/*
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100214 * Check if sg_io_v4 from user is allowed and valid
215 */
216static int
Jens Axboe165125e2007-07-24 09:28:11 +0200217bsg_validate_sgv4_hdr(struct request_queue *q, struct sg_io_v4 *hdr, int *rw)
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100218{
FUJITA Tomonori15d10b62007-07-16 08:52:16 +0200219 int ret = 0;
220
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100221 if (hdr->guard != 'Q')
222 return -EINVAL;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100223
FUJITA Tomonori15d10b62007-07-16 08:52:16 +0200224 switch (hdr->protocol) {
225 case BSG_PROTOCOL_SCSI:
226 switch (hdr->subprotocol) {
227 case BSG_SUB_PROTOCOL_SCSI_CMD:
228 case BSG_SUB_PROTOCOL_SCSI_TRANSPORT:
229 break;
230 default:
231 ret = -EINVAL;
232 }
233 break;
234 default:
235 ret = -EINVAL;
236 }
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100237
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100238 *rw = hdr->dout_xfer_len ? WRITE : READ;
FUJITA Tomonori15d10b62007-07-16 08:52:16 +0200239 return ret;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100240}
241
242/*
243 * map sg_io_v4 to a request.
Jens Axboe3d6392c2007-07-09 12:38:05 +0200244 */
245static struct request *
Boaz Harroshc1c20122009-02-03 07:47:29 +0100246bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr, fmode_t has_write_perm,
247 u8 *sense)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200248{
Jens Axboe165125e2007-07-24 09:28:11 +0200249 struct request_queue *q = bd->queue;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200250 struct request *rq, *next_rq = NULL;
Jens Axboe25fd1642007-07-17 08:52:29 +0200251 int ret, rw;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100252 unsigned int dxfer_len;
253 void *dxferp = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200254
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100255 dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
256 hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
257 hdr->din_xfer_len);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200258
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100259 ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200260 if (ret)
261 return ERR_PTR(ret);
262
263 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800264 * map scatter-gather elements separately and string them to request
Jens Axboe3d6392c2007-07-09 12:38:05 +0200265 */
266 rq = blk_get_request(q, rw, GFP_KERNEL);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200267 if (!rq)
268 return ERR_PTR(-ENOMEM);
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900269 ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, bd, has_write_perm);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200270 if (ret)
271 goto out;
272
273 if (rw == WRITE && hdr->din_xfer_len) {
274 if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
275 ret = -EOPNOTSUPP;
276 goto out;
277 }
278
279 next_rq = blk_get_request(q, READ, GFP_KERNEL);
280 if (!next_rq) {
281 ret = -ENOMEM;
282 goto out;
283 }
284 rq->next_rq = next_rq;
James Bottomley40f62022008-01-25 20:05:55 -0600285 next_rq->cmd_type = rq->cmd_type;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200286
287 dxferp = (void*)(unsigned long)hdr->din_xferp;
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900288 ret = blk_rq_map_user(q, next_rq, NULL, dxferp,
289 hdr->din_xfer_len, GFP_KERNEL);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200290 if (ret)
291 goto out;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200292 }
293
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100294 if (hdr->dout_xfer_len) {
295 dxfer_len = hdr->dout_xfer_len;
296 dxferp = (void*)(unsigned long)hdr->dout_xferp;
297 } else if (hdr->din_xfer_len) {
298 dxfer_len = hdr->din_xfer_len;
299 dxferp = (void*)(unsigned long)hdr->din_xferp;
300 } else
301 dxfer_len = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200302
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100303 if (dxfer_len) {
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900304 ret = blk_rq_map_user(q, rq, NULL, dxferp, dxfer_len,
305 GFP_KERNEL);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200306 if (ret)
307 goto out;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200308 }
Boaz Harroshc1c20122009-02-03 07:47:29 +0100309
310 rq->sense = sense;
311 rq->sense_len = 0;
312
Jens Axboe3d6392c2007-07-09 12:38:05 +0200313 return rq;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200314out:
FUJITA Tomonori9f5de6b2008-04-30 13:16:21 +0900315 if (rq->cmd != rq->__cmd)
316 kfree(rq->cmd);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200317 blk_put_request(rq);
318 if (next_rq) {
319 blk_rq_unmap_user(next_rq->bio);
320 blk_put_request(next_rq);
321 }
322 return ERR_PTR(ret);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200323}
324
325/*
326 * async completion call-back from the block layer, when scsi/ide/whatever
327 * calls end_that_request_last() on a request
328 */
329static void bsg_rq_end_io(struct request *rq, int uptodate)
330{
331 struct bsg_command *bc = rq->end_io_data;
332 struct bsg_device *bd = bc->bd;
333 unsigned long flags;
334
Jens Axboe5309cb32007-01-23 16:24:41 +0100335 dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
336 bd->name, rq, bc, bc->bio, uptodate);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200337
338 bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
339
340 spin_lock_irqsave(&bd->lock, flags);
Jens Axboe25fd1642007-07-17 08:52:29 +0200341 list_move_tail(&bc->list, &bd->done_list);
342 bd->done_cmds++;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200343 spin_unlock_irqrestore(&bd->lock, flags);
Jens Axboe25fd1642007-07-17 08:52:29 +0200344
345 wake_up(&bd->wq_done);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200346}
347
348/*
349 * do final setup of a 'bc' and submit the matching 'rq' to the block
350 * layer for io
351 */
Jens Axboe165125e2007-07-24 09:28:11 +0200352static void bsg_add_command(struct bsg_device *bd, struct request_queue *q,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200353 struct bsg_command *bc, struct request *rq)
354{
Boaz Harrosh05378942009-03-24 12:23:40 +0100355 int at_head = (0 == (bc->hdr.flags & BSG_FLAG_Q_AT_TAIL));
356
Jens Axboe3d6392c2007-07-09 12:38:05 +0200357 /*
358 * add bc command to busy queue and submit rq for io
359 */
360 bc->rq = rq;
361 bc->bio = rq->bio;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200362 if (rq->next_rq)
363 bc->bidi_bio = rq->next_rq->bio;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200364 bc->hdr.duration = jiffies;
365 spin_lock_irq(&bd->lock);
366 list_add_tail(&bc->list, &bd->busy_list);
367 spin_unlock_irq(&bd->lock);
368
369 dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
370
371 rq->end_io_data = bc;
Boaz Harrosh05378942009-03-24 12:23:40 +0100372 blk_execute_rq_nowait(q, NULL, rq, at_head, bsg_rq_end_io);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200373}
374
Jens Axboe25fd1642007-07-17 08:52:29 +0200375static struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200376{
377 struct bsg_command *bc = NULL;
378
379 spin_lock_irq(&bd->lock);
380 if (bd->done_cmds) {
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900381 bc = list_first_entry(&bd->done_list, struct bsg_command, list);
Jens Axboe25fd1642007-07-17 08:52:29 +0200382 list_del(&bc->list);
383 bd->done_cmds--;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200384 }
385 spin_unlock_irq(&bd->lock);
386
387 return bc;
388}
389
390/*
391 * Get a finished command from the done list
392 */
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200393static struct bsg_command *bsg_get_done_cmd(struct bsg_device *bd)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200394{
395 struct bsg_command *bc;
396 int ret;
397
398 do {
399 bc = bsg_next_done_cmd(bd);
400 if (bc)
401 break;
402
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200403 if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
404 bc = ERR_PTR(-EAGAIN);
405 break;
406 }
407
408 ret = wait_event_interruptible(bd->wq_done, bd->done_cmds);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200409 if (ret) {
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200410 bc = ERR_PTR(-ERESTARTSYS);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200411 break;
412 }
413 } while (1);
414
415 dprintk("%s: returning done %p\n", bd->name, bc);
416
417 return bc;
418}
419
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100420static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200421 struct bio *bio, struct bio *bidi_bio)
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100422{
423 int ret = 0;
424
Boaz Harroshc1c20122009-02-03 07:47:29 +0100425 dprintk("rq %p bio %p 0x%x\n", rq, bio, rq->errors);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100426 /*
427 * fill in all the output members
428 */
429 hdr->device_status = status_byte(rq->errors);
430 hdr->transport_status = host_byte(rq->errors);
431 hdr->driver_status = driver_byte(rq->errors);
432 hdr->info = 0;
433 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
434 hdr->info |= SG_INFO_CHECK;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100435 hdr->response_len = 0;
436
437 if (rq->sense_len && hdr->response) {
Jens Axboe25fd1642007-07-17 08:52:29 +0200438 int len = min_t(unsigned int, hdr->max_response_len,
439 rq->sense_len);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100440
441 ret = copy_to_user((void*)(unsigned long)hdr->response,
442 rq->sense, len);
443 if (!ret)
444 hdr->response_len = len;
445 else
446 ret = -EFAULT;
447 }
448
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200449 if (rq->next_rq) {
Tejun Heoc3a4d782009-05-07 22:24:37 +0900450 hdr->dout_resid = rq->resid_len;
451 hdr->din_resid = rq->next_rq->resid_len;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200452 blk_rq_unmap_user(bidi_bio);
453 blk_put_request(rq->next_rq);
FUJITA Tomonori0c6a89b2007-07-29 23:00:46 +0900454 } else if (rq_data_dir(rq) == READ)
Tejun Heoc3a4d782009-05-07 22:24:37 +0900455 hdr->din_resid = rq->resid_len;
FUJITA Tomonori0c6a89b2007-07-29 23:00:46 +0900456 else
Tejun Heoc3a4d782009-05-07 22:24:37 +0900457 hdr->dout_resid = rq->resid_len;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200458
James Bottomley2d507a02007-12-29 10:59:53 -0600459 /*
460 * If the request generated a negative error number, return it
461 * (providing we aren't already returning an error); if it's
462 * just a protocol response (i.e. non negative), that gets
463 * processed above.
464 */
465 if (!ret && rq->errors < 0)
466 ret = rq->errors;
467
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100468 blk_rq_unmap_user(bio);
FUJITA Tomonori9f5de6b2008-04-30 13:16:21 +0900469 if (rq->cmd != rq->__cmd)
470 kfree(rq->cmd);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100471 blk_put_request(rq);
472
473 return ret;
474}
475
Jens Axboe3d6392c2007-07-09 12:38:05 +0200476static int bsg_complete_all_commands(struct bsg_device *bd)
477{
478 struct bsg_command *bc;
479 int ret, tret;
480
481 dprintk("%s: entered\n", bd->name);
482
Jens Axboe3d6392c2007-07-09 12:38:05 +0200483 /*
484 * wait for all commands to complete
485 */
486 ret = 0;
487 do {
Jens Axboe25fd1642007-07-17 08:52:29 +0200488 ret = bsg_io_schedule(bd);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200489 /*
490 * look for -ENODATA specifically -- we'll sometimes get
491 * -ERESTARTSYS when we've taken a signal, but we can't
492 * return until we're done freeing the queue, so ignore
493 * it. The signal will get handled when we're done freeing
494 * the bsg_device.
495 */
496 } while (ret != -ENODATA);
497
498 /*
499 * discard done commands
500 */
501 ret = 0;
502 do {
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200503 spin_lock_irq(&bd->lock);
504 if (!bd->queued_cmds) {
505 spin_unlock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200506 break;
507 }
FUJITA Tomonoriefba1a32007-06-07 13:24:06 +0200508 spin_unlock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200509
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200510 bc = bsg_get_done_cmd(bd);
511 if (IS_ERR(bc))
512 break;
513
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200514 tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio,
515 bc->bidi_bio);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200516 if (!ret)
517 ret = tret;
518
519 bsg_free_command(bc);
520 } while (1);
521
522 return ret;
523}
524
Jens Axboe25fd1642007-07-17 08:52:29 +0200525static int
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200526__bsg_read(char __user *buf, size_t count, struct bsg_device *bd,
527 const struct iovec *iov, ssize_t *bytes_read)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200528{
529 struct bsg_command *bc;
530 int nr_commands, ret;
531
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100532 if (count % sizeof(struct sg_io_v4))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200533 return -EINVAL;
534
535 ret = 0;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100536 nr_commands = count / sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200537 while (nr_commands) {
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200538 bc = bsg_get_done_cmd(bd);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200539 if (IS_ERR(bc)) {
540 ret = PTR_ERR(bc);
541 break;
542 }
543
544 /*
545 * this is the only case where we need to copy data back
546 * after completing the request. so do that here,
547 * bsg_complete_work() cannot do that for us
548 */
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200549 ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio,
550 bc->bidi_bio);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200551
Jens Axboe25fd1642007-07-17 08:52:29 +0200552 if (copy_to_user(buf, &bc->hdr, sizeof(bc->hdr)))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200553 ret = -EFAULT;
554
555 bsg_free_command(bc);
556
557 if (ret)
558 break;
559
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100560 buf += sizeof(struct sg_io_v4);
561 *bytes_read += sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200562 nr_commands--;
563 }
564
565 return ret;
566}
567
568static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
569{
570 if (file->f_flags & O_NONBLOCK)
571 clear_bit(BSG_F_BLOCK, &bd->flags);
572 else
573 set_bit(BSG_F_BLOCK, &bd->flags);
574}
575
Jens Axboe25fd1642007-07-17 08:52:29 +0200576/*
577 * Check if the error is a "real" error that we should return.
578 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200579static inline int err_block_err(int ret)
580{
581 if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
582 return 1;
583
584 return 0;
585}
586
587static ssize_t
588bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
589{
590 struct bsg_device *bd = file->private_data;
591 int ret;
592 ssize_t bytes_read;
593
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100594 dprintk("%s: read %Zd bytes\n", bd->name, count);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200595
596 bsg_set_block(bd, file);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200597
Jens Axboe3d6392c2007-07-09 12:38:05 +0200598 bytes_read = 0;
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200599 ret = __bsg_read(buf, count, bd, NULL, &bytes_read);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200600 *ppos = bytes_read;
601
602 if (!bytes_read || (bytes_read && err_block_err(ret)))
603 bytes_read = ret;
604
605 return bytes_read;
606}
607
Jens Axboe25fd1642007-07-17 08:52:29 +0200608static int __bsg_write(struct bsg_device *bd, const char __user *buf,
Al Viroaeb5d722008-09-02 15:28:45 -0400609 size_t count, ssize_t *bytes_written,
610 fmode_t has_write_perm)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200611{
612 struct bsg_command *bc;
613 struct request *rq;
614 int ret, nr_commands;
615
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100616 if (count % sizeof(struct sg_io_v4))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200617 return -EINVAL;
618
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100619 nr_commands = count / sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200620 rq = NULL;
621 bc = NULL;
622 ret = 0;
623 while (nr_commands) {
Jens Axboe165125e2007-07-24 09:28:11 +0200624 struct request_queue *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200625
FUJITA Tomonorie7d72172007-05-08 15:32:03 +0200626 bc = bsg_alloc_command(bd);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200627 if (IS_ERR(bc)) {
628 ret = PTR_ERR(bc);
629 bc = NULL;
630 break;
631 }
632
Jens Axboe3d6392c2007-07-09 12:38:05 +0200633 if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
634 ret = -EFAULT;
635 break;
636 }
637
638 /*
639 * get a request, fill in the blanks, and add to request queue
640 */
Boaz Harroshc1c20122009-02-03 07:47:29 +0100641 rq = bsg_map_hdr(bd, &bc->hdr, has_write_perm, bc->sense);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200642 if (IS_ERR(rq)) {
643 ret = PTR_ERR(rq);
644 rq = NULL;
645 break;
646 }
647
648 bsg_add_command(bd, q, bc, rq);
649 bc = NULL;
650 rq = NULL;
651 nr_commands--;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100652 buf += sizeof(struct sg_io_v4);
Jens Axboe25fd1642007-07-17 08:52:29 +0200653 *bytes_written += sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200654 }
655
Jens Axboe3d6392c2007-07-09 12:38:05 +0200656 if (bc)
657 bsg_free_command(bc);
658
659 return ret;
660}
661
662static ssize_t
663bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
664{
665 struct bsg_device *bd = file->private_data;
Jens Axboe25fd1642007-07-17 08:52:29 +0200666 ssize_t bytes_written;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200667 int ret;
668
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100669 dprintk("%s: write %Zd bytes\n", bd->name, count);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200670
671 bsg_set_block(bd, file);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200672
Jens Axboe25fd1642007-07-17 08:52:29 +0200673 bytes_written = 0;
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900674 ret = __bsg_write(bd, buf, count, &bytes_written,
675 file->f_mode & FMODE_WRITE);
676
Jens Axboe25fd1642007-07-17 08:52:29 +0200677 *ppos = bytes_written;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200678
679 /*
680 * return bytes written on non-fatal errors
681 */
Jens Axboe25fd1642007-07-17 08:52:29 +0200682 if (!bytes_written || (bytes_written && err_block_err(ret)))
683 bytes_written = ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200684
Jens Axboe25fd1642007-07-17 08:52:29 +0200685 dprintk("%s: returning %Zd\n", bd->name, bytes_written);
686 return bytes_written;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200687}
688
Jens Axboe3d6392c2007-07-09 12:38:05 +0200689static struct bsg_device *bsg_alloc_device(void)
690{
Jens Axboe3d6392c2007-07-09 12:38:05 +0200691 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200692
693 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
694 if (unlikely(!bd))
695 return NULL;
696
697 spin_lock_init(&bd->lock);
698
Jens Axboe5309cb32007-01-23 16:24:41 +0100699 bd->max_queue = BSG_DEFAULT_CMDS;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200700
701 INIT_LIST_HEAD(&bd->busy_list);
702 INIT_LIST_HEAD(&bd->done_list);
703 INIT_HLIST_NODE(&bd->dev_list);
704
705 init_waitqueue_head(&bd->wq_free);
706 init_waitqueue_head(&bd->wq_done);
707 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200708}
709
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900710static void bsg_kref_release_function(struct kref *kref)
711{
712 struct bsg_class_device *bcd =
713 container_of(kref, struct bsg_class_device, ref);
James Bottomley8df5fc02008-07-07 15:50:01 -0500714 struct device *parent = bcd->parent;
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900715
716 if (bcd->release)
717 bcd->release(bcd->parent);
718
James Bottomley8df5fc02008-07-07 15:50:01 -0500719 put_device(parent);
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900720}
721
Jens Axboe3d6392c2007-07-09 12:38:05 +0200722static int bsg_put_device(struct bsg_device *bd)
723{
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900724 int ret = 0, do_free;
725 struct request_queue *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200726
727 mutex_lock(&bsg_mutex);
728
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900729 do_free = atomic_dec_and_test(&bd->ref_count);
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900730 if (!do_free) {
731 mutex_unlock(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200732 goto out;
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900733 }
734
735 hlist_del(&bd->dev_list);
736 mutex_unlock(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200737
738 dprintk("%s: tearing down\n", bd->name);
739
740 /*
741 * close can always block
742 */
743 set_bit(BSG_F_BLOCK, &bd->flags);
744
745 /*
746 * correct error detection baddies here again. it's the responsibility
747 * of the app to properly reap commands before close() if it wants
748 * fool-proof error detection
749 */
750 ret = bsg_complete_all_commands(bd);
751
Jens Axboe5309cb32007-01-23 16:24:41 +0100752 kfree(bd);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200753out:
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900754 kref_put(&q->bsg_dev.ref, bsg_kref_release_function);
755 if (do_free)
756 blk_put_queue(q);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200757 return ret;
758}
759
760static struct bsg_device *bsg_add_device(struct inode *inode,
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200761 struct request_queue *rq,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200762 struct file *file)
763{
Jens Axboe25fd1642007-07-17 08:52:29 +0200764 struct bsg_device *bd;
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900765 int ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200766#ifdef BSG_DEBUG
767 unsigned char buf[32];
768#endif
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900769 ret = blk_get_queue(rq);
770 if (ret)
771 return ERR_PTR(-ENXIO);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200772
773 bd = bsg_alloc_device();
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900774 if (!bd) {
775 blk_put_queue(rq);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200776 return ERR_PTR(-ENOMEM);
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900777 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200778
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200779 bd->queue = rq;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200780
Jens Axboe3d6392c2007-07-09 12:38:05 +0200781 bsg_set_block(bd, file);
782
783 atomic_set(&bd->ref_count, 1);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200784 mutex_lock(&bsg_mutex);
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900785 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200786
Kay Sievers3ada8b72009-01-06 10:44:43 -0800787 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200788 dprintk("bound to <%s>, max queue %d\n",
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100789 format_dev_t(buf, inode->i_rdev), bd->max_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200790
791 mutex_unlock(&bsg_mutex);
792 return bd;
793}
794
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900795static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200796{
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900797 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200798 struct hlist_node *entry;
799
800 mutex_lock(&bsg_mutex);
801
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900802 hlist_for_each_entry(bd, entry, bsg_dev_idx_hash(minor), dev_list) {
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900803 if (bd->queue == q) {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200804 atomic_inc(&bd->ref_count);
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900805 goto found;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200806 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200807 }
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900808 bd = NULL;
809found:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200810 mutex_unlock(&bsg_mutex);
811 return bd;
812}
813
814static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
815{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900816 struct bsg_device *bd;
817 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200818
Jens Axboe3d6392c2007-07-09 12:38:05 +0200819 /*
820 * find the class device
821 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200822 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900823 bcd = idr_find(&bsg_minor_idr, iminor(inode));
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900824 if (bcd)
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900825 kref_get(&bcd->ref);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200826 mutex_unlock(&bsg_mutex);
827
828 if (!bcd)
829 return ERR_PTR(-ENODEV);
830
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900831 bd = __bsg_get_device(iminor(inode), bcd->queue);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900832 if (bd)
833 return bd;
834
835 bd = bsg_add_device(inode, bcd->queue, file);
836 if (IS_ERR(bd))
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900837 kref_put(&bcd->ref, bsg_kref_release_function);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900838
839 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200840}
841
842static int bsg_open(struct inode *inode, struct file *file)
843{
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600844 struct bsg_device *bd;
845
846 lock_kernel();
847 bd = bsg_get_device(inode, file);
848 unlock_kernel();
Jens Axboe3d6392c2007-07-09 12:38:05 +0200849
850 if (IS_ERR(bd))
851 return PTR_ERR(bd);
852
853 file->private_data = bd;
854 return 0;
855}
856
857static int bsg_release(struct inode *inode, struct file *file)
858{
859 struct bsg_device *bd = file->private_data;
860
861 file->private_data = NULL;
862 return bsg_put_device(bd);
863}
864
865static unsigned int bsg_poll(struct file *file, poll_table *wait)
866{
867 struct bsg_device *bd = file->private_data;
868 unsigned int mask = 0;
869
870 poll_wait(file, &bd->wq_done, wait);
871 poll_wait(file, &bd->wq_free, wait);
872
873 spin_lock_irq(&bd->lock);
874 if (!list_empty(&bd->done_list))
875 mask |= POLLIN | POLLRDNORM;
876 if (bd->queued_cmds >= bd->max_queue)
877 mask |= POLLOUT;
878 spin_unlock_irq(&bd->lock);
879
880 return mask;
881}
882
Jens Axboe25fd1642007-07-17 08:52:29 +0200883static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200884{
885 struct bsg_device *bd = file->private_data;
886 int __user *uarg = (int __user *) arg;
James Bottomley2d507a02007-12-29 10:59:53 -0600887 int ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200888
Jens Axboe3d6392c2007-07-09 12:38:05 +0200889 switch (cmd) {
890 /*
891 * our own ioctls
892 */
893 case SG_GET_COMMAND_Q:
894 return put_user(bd->max_queue, uarg);
Jens Axboe5309cb32007-01-23 16:24:41 +0100895 case SG_SET_COMMAND_Q: {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200896 int queue;
897
898 if (get_user(queue, uarg))
899 return -EFAULT;
Jens Axboe5309cb32007-01-23 16:24:41 +0100900 if (queue < 1)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200901 return -EINVAL;
902
Jens Axboe5309cb32007-01-23 16:24:41 +0100903 spin_lock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200904 bd->max_queue = queue;
Jens Axboe5309cb32007-01-23 16:24:41 +0100905 spin_unlock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200906 return 0;
907 }
908
909 /*
910 * SCSI/sg ioctls
911 */
912 case SG_GET_VERSION_NUM:
913 case SCSI_IOCTL_GET_IDLUN:
914 case SCSI_IOCTL_GET_BUS_NUMBER:
915 case SG_SET_TIMEOUT:
916 case SG_GET_TIMEOUT:
917 case SG_GET_RESERVED_SIZE:
918 case SG_SET_RESERVED_SIZE:
919 case SG_EMULATED_HOST:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200920 case SCSI_IOCTL_SEND_COMMAND: {
921 void __user *uarg = (void __user *) arg;
Al Viro74f3c8a2007-08-27 15:38:10 -0400922 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200923 }
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100924 case SG_IO: {
925 struct request *rq;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200926 struct bio *bio, *bidi_bio = NULL;
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100927 struct sg_io_v4 hdr;
Boaz Harrosh05378942009-03-24 12:23:40 +0100928 int at_head;
Boaz Harroshc1c20122009-02-03 07:47:29 +0100929 u8 sense[SCSI_SENSE_BUFFERSIZE];
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100930
931 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
932 return -EFAULT;
933
Boaz Harroshc1c20122009-02-03 07:47:29 +0100934 rq = bsg_map_hdr(bd, &hdr, file->f_mode & FMODE_WRITE, sense);
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100935 if (IS_ERR(rq))
936 return PTR_ERR(rq);
937
938 bio = rq->bio;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200939 if (rq->next_rq)
940 bidi_bio = rq->next_rq->bio;
Boaz Harrosh05378942009-03-24 12:23:40 +0100941
942 at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL));
943 blk_execute_rq(bd->queue, NULL, rq, at_head);
James Bottomley2d507a02007-12-29 10:59:53 -0600944 ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100945
946 if (copy_to_user(uarg, &hdr, sizeof(hdr)))
947 return -EFAULT;
Jens Axboeb711afa2006-12-20 11:25:23 +0100948
James Bottomley2d507a02007-12-29 10:59:53 -0600949 return ret;
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100950 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200951 /*
952 * block device ioctls
953 */
954 default:
955#if 0
956 return ioctl_by_bdev(bd->bdev, cmd, arg);
957#else
958 return -ENOTTY;
959#endif
960 }
961}
962
Arjan van de Ven7344be02007-10-15 11:01:53 +0200963static const struct file_operations bsg_fops = {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200964 .read = bsg_read,
965 .write = bsg_write,
966 .poll = bsg_poll,
967 .open = bsg_open,
968 .release = bsg_release,
Jens Axboe25fd1642007-07-17 08:52:29 +0200969 .unlocked_ioctl = bsg_ioctl,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200970 .owner = THIS_MODULE,
971};
972
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200973void bsg_unregister_queue(struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200974{
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200975 struct bsg_class_device *bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200976
FUJITA Tomonoridf468822007-07-21 13:23:25 +0900977 if (!bcd->class_dev)
978 return;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200979
980 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900981 idr_remove(&bsg_minor_idr, bcd->minor);
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200982 sysfs_remove_link(&q->kobj, "bsg");
Tony Jonesee959b02008-02-22 00:13:36 +0100983 device_unregister(bcd->class_dev);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200984 bcd->class_dev = NULL;
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900985 kref_put(&bcd->ref, bsg_kref_release_function);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200986 mutex_unlock(&bsg_mutex);
987}
FUJITA Tomonori4cf07232007-03-30 11:19:39 +0200988EXPORT_SYMBOL_GPL(bsg_unregister_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200989
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900990int bsg_register_queue(struct request_queue *q, struct device *parent,
991 const char *name, void (*release)(struct device *))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200992{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900993 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200994 dev_t dev;
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900995 int ret, minor;
Tony Jonesee959b02008-02-22 00:13:36 +0100996 struct device *class_dev = NULL;
James Bottomley39dca552007-07-20 18:22:17 -0500997 const char *devname;
998
999 if (name)
1000 devname = name;
1001 else
Kay Sievers3ada8b72009-01-06 10:44:43 -08001002 devname = dev_name(parent);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001003
1004 /*
1005 * we need a proper transport to send commands, not a stacked device
1006 */
1007 if (!q->request_fn)
1008 return 0;
1009
FUJITA Tomonorid351af02007-07-09 12:40:35 +02001010 bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +02001011 memset(bcd, 0, sizeof(*bcd));
Jens Axboe3d6392c2007-07-09 12:38:05 +02001012
1013 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001014
1015 ret = idr_pre_get(&bsg_minor_idr, GFP_KERNEL);
1016 if (!ret) {
1017 ret = -ENOMEM;
1018 goto unlock;
1019 }
1020
1021 ret = idr_get_new(&bsg_minor_idr, bcd, &minor);
1022 if (ret < 0)
1023 goto unlock;
1024
1025 if (minor >= BSG_MAX_DEVS) {
FUJITA Tomonori292b7f22007-03-28 13:29:58 +02001026 printk(KERN_ERR "bsg: too many bsg devices\n");
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001027 ret = -EINVAL;
1028 goto remove_idr;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +02001029 }
1030
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001031 bcd->minor = minor;
FUJITA Tomonorid351af02007-07-09 12:40:35 +02001032 bcd->queue = q;
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +09001033 bcd->parent = get_device(parent);
1034 bcd->release = release;
1035 kref_init(&bcd->ref);
Jens Axboe46f6ef42007-07-17 08:56:10 +02001036 dev = MKDEV(bsg_major, bcd->minor);
Greg Kroah-Hartman1ff9f542008-07-21 20:03:34 -07001037 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", devname);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +02001038 if (IS_ERR(class_dev)) {
1039 ret = PTR_ERR(class_dev);
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001040 goto put_dev;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +02001041 }
1042 bcd->class_dev = class_dev;
1043
Linus Torvaldsabce8912007-07-16 11:18:23 -07001044 if (q->kobj.sd) {
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +02001045 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
1046 if (ret)
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001047 goto unregister_class_dev;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +02001048 }
1049
Jens Axboe3d6392c2007-07-09 12:38:05 +02001050 mutex_unlock(&bsg_mutex);
1051 return 0;
James Bottomley6826ee42007-07-20 16:50:10 -05001052
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001053unregister_class_dev:
Tony Jonesee959b02008-02-22 00:13:36 +01001054 device_unregister(class_dev);
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001055put_dev:
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +09001056 put_device(parent);
FUJITA Tomonori598443a2007-07-23 09:33:26 +09001057remove_idr:
1058 idr_remove(&bsg_minor_idr, minor);
1059unlock:
Jens Axboe264a0472007-01-23 16:30:17 +01001060 mutex_unlock(&bsg_mutex);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +02001061 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +02001062}
FUJITA Tomonori4cf07232007-03-30 11:19:39 +02001063EXPORT_SYMBOL_GPL(bsg_register_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001064
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001065static struct cdev bsg_cdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +02001066
Kay Sieverse454cea2009-09-18 23:01:12 +02001067static char *bsg_devnode(struct device *dev, mode_t *mode)
Kay Sievers2bdf9142009-04-30 15:23:42 +02001068{
1069 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
1070}
1071
Jens Axboe3d6392c2007-07-09 12:38:05 +02001072static int __init bsg_init(void)
1073{
1074 int ret, i;
Jens Axboe46f6ef42007-07-17 08:56:10 +02001075 dev_t devid;
Jens Axboe3d6392c2007-07-09 12:38:05 +02001076
Jens Axboe5309cb32007-01-23 16:24:41 +01001077 bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
Paul Mundt20c2df82007-07-20 10:11:58 +09001078 sizeof(struct bsg_command), 0, 0, NULL);
Jens Axboe5309cb32007-01-23 16:24:41 +01001079 if (!bsg_cmd_cachep) {
1080 printk(KERN_ERR "bsg: failed creating slab cache\n");
1081 return -ENOMEM;
1082 }
1083
Jens Axboe25fd1642007-07-17 08:52:29 +02001084 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
Jens Axboe3d6392c2007-07-09 12:38:05 +02001085 INIT_HLIST_HEAD(&bsg_device_list[i]);
1086
1087 bsg_class = class_create(THIS_MODULE, "bsg");
Jens Axboe5309cb32007-01-23 16:24:41 +01001088 if (IS_ERR(bsg_class)) {
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +02001089 ret = PTR_ERR(bsg_class);
1090 goto destroy_kmemcache;
Jens Axboe5309cb32007-01-23 16:24:41 +01001091 }
Kay Sieverse454cea2009-09-18 23:01:12 +02001092 bsg_class->devnode = bsg_devnode;
Jens Axboe3d6392c2007-07-09 12:38:05 +02001093
Jens Axboe46f6ef42007-07-17 08:56:10 +02001094 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +02001095 if (ret)
1096 goto destroy_bsg_class;
Jens Axboe3d6392c2007-07-09 12:38:05 +02001097
Jens Axboe46f6ef42007-07-17 08:56:10 +02001098 bsg_major = MAJOR(devid);
1099
FUJITA Tomonori292b7f22007-03-28 13:29:58 +02001100 cdev_init(&bsg_cdev, &bsg_fops);
Jens Axboe46f6ef42007-07-17 08:56:10 +02001101 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +02001102 if (ret)
1103 goto unregister_chrdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +02001104
Jens Axboe5d3a8cd2007-07-17 15:10:09 +02001105 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +02001106 " loaded (major %d)\n", bsg_major);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001107 return 0;
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +02001108unregister_chrdev:
1109 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
1110destroy_bsg_class:
1111 class_destroy(bsg_class);
1112destroy_kmemcache:
1113 kmem_cache_destroy(bsg_cmd_cachep);
1114 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +02001115}
1116
1117MODULE_AUTHOR("Jens Axboe");
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +02001118MODULE_DESCRIPTION(BSG_DESCRIPTION);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001119MODULE_LICENSE("GPL");
1120
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +02001121device_initcall(bsg_init);