blob: 4ea4bedb413fd8bf19ffe0e280f3deda7d25b0dd [file] [log] [blame]
Jens Axboe3d6392c2007-07-09 12:38:05 +02001/*
2 * bsg.c - block layer implementation of the sg v3 interface
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 */
12/*
13 * TODO
14 * - Should this get merged, block/scsi_ioctl.c will be migrated into
15 * this file. To keep maintenance down, it's easier to have them
16 * seperated right now.
17 *
18 */
Jens Axboe3d6392c2007-07-09 12:38:05 +020019#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/file.h>
22#include <linux/blkdev.h>
23#include <linux/poll.h>
24#include <linux/cdev.h>
25#include <linux/percpu.h>
26#include <linux/uio.h>
27#include <linux/bsg.h>
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_ioctl.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/sg.h>
33
34static char bsg_version[] = "block layer sg (bsg) 0.4";
35
Jens Axboe3d6392c2007-07-09 12:38:05 +020036struct bsg_device {
Jens Axboe3d6392c2007-07-09 12:38:05 +020037 request_queue_t *queue;
38 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;
43 int minor;
44 int queued_cmds;
45 int done_cmds;
Jens Axboe3d6392c2007-07-09 12:38:05 +020046 wait_queue_head_t wq_done;
47 wait_queue_head_t wq_free;
FUJITA Tomonorid351af02007-07-09 12:40:35 +020048 char name[BUS_ID_SIZE];
Jens Axboe3d6392c2007-07-09 12:38:05 +020049 int max_queue;
50 unsigned long flags;
51};
52
53enum {
54 BSG_F_BLOCK = 1,
55 BSG_F_WRITE_PERM = 2,
56};
57
Jens Axboe5309cb32007-01-23 16:24:41 +010058#define BSG_DEFAULT_CMDS 64
Jens Axboe3d6392c2007-07-09 12:38:05 +020059
60#undef BSG_DEBUG
61
62#ifdef BSG_DEBUG
63#define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
64#else
65#define dprintk(fmt, args...)
66#endif
67
68#define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
69
70/*
71 * just for testing
72 */
73#define BSG_MAJOR (240)
74
75static DEFINE_MUTEX(bsg_mutex);
76static int bsg_device_nr;
77
78#define BSG_LIST_SIZE (8)
79#define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
80static struct hlist_head bsg_device_list[BSG_LIST_SIZE];
81
82static struct class *bsg_class;
83static LIST_HEAD(bsg_class_list);
84
Jens Axboe5309cb32007-01-23 16:24:41 +010085static struct kmem_cache *bsg_cmd_cachep;
86
Jens Axboe3d6392c2007-07-09 12:38:05 +020087/*
88 * our internal command type
89 */
90struct bsg_command {
91 struct bsg_device *bd;
92 struct list_head list;
93 struct request *rq;
94 struct bio *bio;
95 int err;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010096 struct sg_io_v4 hdr;
97 struct sg_io_v4 __user *uhdr;
Jens Axboe3d6392c2007-07-09 12:38:05 +020098 char sense[SCSI_SENSE_BUFFERSIZE];
99};
100
101static void bsg_free_command(struct bsg_command *bc)
102{
103 struct bsg_device *bd = bc->bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200104 unsigned long flags;
105
Jens Axboe5309cb32007-01-23 16:24:41 +0100106 kmem_cache_free(bsg_cmd_cachep, bc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200107
108 spin_lock_irqsave(&bd->lock, flags);
109 bd->queued_cmds--;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200110 spin_unlock_irqrestore(&bd->lock, flags);
111
112 wake_up(&bd->wq_free);
113}
114
115static struct bsg_command *__bsg_alloc_command(struct bsg_device *bd)
116{
117 struct bsg_command *bc = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200118
119 spin_lock_irq(&bd->lock);
120
121 if (bd->queued_cmds >= bd->max_queue)
122 goto out;
123
Jens Axboe3d6392c2007-07-09 12:38:05 +0200124 bd->queued_cmds++;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200125 spin_unlock_irq(&bd->lock);
126
Jens Axboe5309cb32007-01-23 16:24:41 +0100127 bc = kmem_cache_alloc(bsg_cmd_cachep, GFP_USER);
128 if (unlikely(!bc)) {
129 spin_lock_irq(&bd->lock);
FUJITA Tomonori7e75d732007-01-24 09:05:54 +0100130 bd->queued_cmds--;
131 goto out;
Jens Axboe5309cb32007-01-23 16:24:41 +0100132 }
133
Jens Axboe3d6392c2007-07-09 12:38:05 +0200134 memset(bc, 0, sizeof(*bc));
135 bc->bd = bd;
136 INIT_LIST_HEAD(&bc->list);
Jens Axboe5309cb32007-01-23 16:24:41 +0100137 dprintk("%s: returning free cmd %p\n", bd->name, bc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200138 return bc;
139out:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200140 spin_unlock_irq(&bd->lock);
141 return bc;
142}
143
144static inline void
145bsg_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
146{
147 bd->done_cmds--;
148 list_del(&bc->list);
149}
150
151static inline void
152bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
153{
154 bd->done_cmds++;
155 list_add_tail(&bc->list, &bd->done_list);
156 wake_up(&bd->wq_done);
157}
158
159static inline int bsg_io_schedule(struct bsg_device *bd, int state)
160{
161 DEFINE_WAIT(wait);
162 int ret = 0;
163
164 spin_lock_irq(&bd->lock);
165
166 BUG_ON(bd->done_cmds > bd->queued_cmds);
167
168 /*
169 * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
170 * work to do", even though we return -ENOSPC after this same test
171 * during bsg_write() -- there, it means our buffer can't have more
172 * bsg_commands added to it, thus has no space left.
173 */
174 if (bd->done_cmds == bd->queued_cmds) {
175 ret = -ENODATA;
176 goto unlock;
177 }
178
179 if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
180 ret = -EAGAIN;
181 goto unlock;
182 }
183
184 prepare_to_wait(&bd->wq_done, &wait, state);
185 spin_unlock_irq(&bd->lock);
186 io_schedule();
187 finish_wait(&bd->wq_done, &wait);
188
189 if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
190 ret = -ERESTARTSYS;
191
192 return ret;
193unlock:
194 spin_unlock_irq(&bd->lock);
195 return ret;
196}
197
198/*
199 * get a new free command, blocking if needed and specified
200 */
201static struct bsg_command *bsg_get_command(struct bsg_device *bd)
202{
203 struct bsg_command *bc;
204 int ret;
205
206 do {
207 bc = __bsg_alloc_command(bd);
208 if (bc)
209 break;
210
211 ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);
212 if (ret) {
213 bc = ERR_PTR(ret);
214 break;
215 }
216
217 } while (1);
218
219 return bc;
220}
221
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100222static int blk_fill_sgv4_hdr_rq(request_queue_t *q, struct request *rq,
223 struct sg_io_v4 *hdr, int has_write_perm)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200224{
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100225 memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
226
227 if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
228 hdr->request_len))
229 return -EFAULT;
230 if (blk_verify_command(rq->cmd, has_write_perm))
231 return -EPERM;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200232
233 /*
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100234 * fill in request structure
Jens Axboe3d6392c2007-07-09 12:38:05 +0200235 */
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100236 rq->cmd_len = hdr->request_len;
237 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200238
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100239 rq->timeout = (hdr->timeout * HZ) / 1000;
240 if (!rq->timeout)
241 rq->timeout = q->sg_timeout;
242 if (!rq->timeout)
243 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200244
245 return 0;
246}
247
248/*
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100249 * Check if sg_io_v4 from user is allowed and valid
250 */
251static int
252bsg_validate_sgv4_hdr(request_queue_t *q, struct sg_io_v4 *hdr, int *rw)
253{
254 if (hdr->guard != 'Q')
255 return -EINVAL;
256 if (hdr->request_len > BLK_MAX_CDB)
257 return -EINVAL;
258 if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
259 hdr->din_xfer_len > (q->max_sectors << 9))
260 return -EIO;
261
262 /* not supported currently */
263 if (hdr->protocol || hdr->subprotocol)
264 return -EINVAL;
265
266 /*
267 * looks sane, if no data then it should be fine from our POV
268 */
269 if (!hdr->dout_xfer_len && !hdr->din_xfer_len)
270 return 0;
271
272 /* not supported currently */
273 if (hdr->dout_xfer_len && hdr->din_xfer_len)
274 return -EINVAL;
275
276 *rw = hdr->dout_xfer_len ? WRITE : READ;
277
278 return 0;
279}
280
281/*
282 * map sg_io_v4 to a request.
Jens Axboe3d6392c2007-07-09 12:38:05 +0200283 */
284static struct request *
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100285bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200286{
287 request_queue_t *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200288 struct request *rq;
Jens Axboe2ef70862006-12-20 11:26:11 +0100289 int ret, rw = 0; /* shut up gcc */
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100290 unsigned int dxfer_len;
291 void *dxferp = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200292
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100293 dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
294 hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
295 hdr->din_xfer_len);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200296
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100297 ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200298 if (ret)
299 return ERR_PTR(ret);
300
301 /*
302 * map scatter-gather elements seperately and string them to request
303 */
304 rq = blk_get_request(q, rw, GFP_KERNEL);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100305 ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
306 &bd->flags));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200307 if (ret) {
308 blk_put_request(rq);
309 return ERR_PTR(ret);
310 }
311
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100312 if (hdr->dout_xfer_len) {
313 dxfer_len = hdr->dout_xfer_len;
314 dxferp = (void*)(unsigned long)hdr->dout_xferp;
315 } else if (hdr->din_xfer_len) {
316 dxfer_len = hdr->din_xfer_len;
317 dxferp = (void*)(unsigned long)hdr->din_xferp;
318 } else
319 dxfer_len = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200320
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100321 if (dxfer_len) {
322 ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
323 if (ret) {
324 dprintk("failed map at %d\n", ret);
325 blk_put_request(rq);
326 rq = ERR_PTR(ret);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200327 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200328 }
329
330 return rq;
331}
332
333/*
334 * async completion call-back from the block layer, when scsi/ide/whatever
335 * calls end_that_request_last() on a request
336 */
337static void bsg_rq_end_io(struct request *rq, int uptodate)
338{
339 struct bsg_command *bc = rq->end_io_data;
340 struct bsg_device *bd = bc->bd;
341 unsigned long flags;
342
Jens Axboe5309cb32007-01-23 16:24:41 +0100343 dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
344 bd->name, rq, bc, bc->bio, uptodate);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200345
346 bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
347
348 spin_lock_irqsave(&bd->lock, flags);
349 list_del(&bc->list);
350 bsg_add_done_cmd(bd, bc);
351 spin_unlock_irqrestore(&bd->lock, flags);
352}
353
354/*
355 * do final setup of a 'bc' and submit the matching 'rq' to the block
356 * layer for io
357 */
358static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
359 struct bsg_command *bc, struct request *rq)
360{
361 rq->sense = bc->sense;
362 rq->sense_len = 0;
363
364 /*
365 * add bc command to busy queue and submit rq for io
366 */
367 bc->rq = rq;
368 bc->bio = rq->bio;
369 bc->hdr.duration = jiffies;
370 spin_lock_irq(&bd->lock);
371 list_add_tail(&bc->list, &bd->busy_list);
372 spin_unlock_irq(&bd->lock);
373
374 dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
375
376 rq->end_io_data = bc;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200377 blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200378}
379
380static inline struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
381{
382 struct bsg_command *bc = NULL;
383
384 spin_lock_irq(&bd->lock);
385 if (bd->done_cmds) {
386 bc = list_entry_bc(bd->done_list.next);
387 bsg_del_done_cmd(bd, bc);
388 }
389 spin_unlock_irq(&bd->lock);
390
391 return bc;
392}
393
394/*
395 * Get a finished command from the done list
396 */
397static struct bsg_command *__bsg_get_done_cmd(struct bsg_device *bd, int state)
398{
399 struct bsg_command *bc;
400 int ret;
401
402 do {
403 bc = bsg_next_done_cmd(bd);
404 if (bc)
405 break;
406
407 ret = bsg_io_schedule(bd, state);
408 if (ret) {
409 bc = ERR_PTR(ret);
410 break;
411 }
412 } while (1);
413
414 dprintk("%s: returning done %p\n", bd->name, bc);
415
416 return bc;
417}
418
419static struct bsg_command *
420bsg_get_done_cmd(struct bsg_device *bd, const struct iovec *iov)
421{
422 return __bsg_get_done_cmd(bd, TASK_INTERRUPTIBLE);
423}
424
425static struct bsg_command *
426bsg_get_done_cmd_nosignals(struct bsg_device *bd)
427{
428 return __bsg_get_done_cmd(bd, TASK_UNINTERRUPTIBLE);
429}
430
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100431static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
432 struct bio *bio)
433{
434 int ret = 0;
435
436 dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
437 /*
438 * fill in all the output members
439 */
440 hdr->device_status = status_byte(rq->errors);
441 hdr->transport_status = host_byte(rq->errors);
442 hdr->driver_status = driver_byte(rq->errors);
443 hdr->info = 0;
444 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
445 hdr->info |= SG_INFO_CHECK;
446 hdr->din_resid = rq->data_len;
447 hdr->response_len = 0;
448
449 if (rq->sense_len && hdr->response) {
450 int len = min((unsigned int) hdr->max_response_len,
451 rq->sense_len);
452
453 ret = copy_to_user((void*)(unsigned long)hdr->response,
454 rq->sense, len);
455 if (!ret)
456 hdr->response_len = len;
457 else
458 ret = -EFAULT;
459 }
460
461 blk_rq_unmap_user(bio);
462 blk_put_request(rq);
463
464 return ret;
465}
466
Jens Axboe3d6392c2007-07-09 12:38:05 +0200467static int bsg_complete_all_commands(struct bsg_device *bd)
468{
469 struct bsg_command *bc;
470 int ret, tret;
471
472 dprintk("%s: entered\n", bd->name);
473
474 set_bit(BSG_F_BLOCK, &bd->flags);
475
476 /*
477 * wait for all commands to complete
478 */
479 ret = 0;
480 do {
481 ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
482 /*
483 * look for -ENODATA specifically -- we'll sometimes get
484 * -ERESTARTSYS when we've taken a signal, but we can't
485 * return until we're done freeing the queue, so ignore
486 * it. The signal will get handled when we're done freeing
487 * the bsg_device.
488 */
489 } while (ret != -ENODATA);
490
491 /*
492 * discard done commands
493 */
494 ret = 0;
495 do {
496 bc = bsg_get_done_cmd_nosignals(bd);
497
498 /*
499 * we _must_ complete before restarting, because
500 * bsg_release can't handle this failing.
501 */
502 if (PTR_ERR(bc) == -ERESTARTSYS)
503 continue;
504 if (IS_ERR(bc)) {
505 ret = PTR_ERR(bc);
506 break;
507 }
508
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100509 tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200510 if (!ret)
511 ret = tret;
512
513 bsg_free_command(bc);
514 } while (1);
515
516 return ret;
517}
518
519typedef struct bsg_command *(*bsg_command_callback)(struct bsg_device *bd, const struct iovec *iov);
520
521static ssize_t
522__bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
523 struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
524{
525 struct bsg_command *bc;
526 int nr_commands, ret;
527
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100528 if (count % sizeof(struct sg_io_v4))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200529 return -EINVAL;
530
531 ret = 0;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100532 nr_commands = count / sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200533 while (nr_commands) {
534 bc = get_bc(bd, iov);
535 if (IS_ERR(bc)) {
536 ret = PTR_ERR(bc);
537 break;
538 }
539
540 /*
541 * this is the only case where we need to copy data back
542 * after completing the request. so do that here,
543 * bsg_complete_work() cannot do that for us
544 */
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100545 ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200546
547 if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
548 ret = -EFAULT;
549
550 bsg_free_command(bc);
551
552 if (ret)
553 break;
554
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100555 buf += sizeof(struct sg_io_v4);
556 *bytes_read += sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200557 nr_commands--;
558 }
559
560 return ret;
561}
562
563static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
564{
565 if (file->f_flags & O_NONBLOCK)
566 clear_bit(BSG_F_BLOCK, &bd->flags);
567 else
568 set_bit(BSG_F_BLOCK, &bd->flags);
569}
570
571static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
572{
573 if (file->f_mode & FMODE_WRITE)
574 set_bit(BSG_F_WRITE_PERM, &bd->flags);
575 else
576 clear_bit(BSG_F_WRITE_PERM, &bd->flags);
577}
578
579static 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);
597 bytes_read = 0;
598 ret = __bsg_read(buf, count, bsg_get_done_cmd,
599 bd, NULL, &bytes_read);
600 *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
608static ssize_t __bsg_write(struct bsg_device *bd, const char __user *buf,
609 size_t count, ssize_t *bytes_read)
610{
611 struct bsg_command *bc;
612 struct request *rq;
613 int ret, nr_commands;
614
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100615 if (count % sizeof(struct sg_io_v4))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200616 return -EINVAL;
617
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100618 nr_commands = count / sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200619 rq = NULL;
620 bc = NULL;
621 ret = 0;
622 while (nr_commands) {
623 request_queue_t *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200624
625 bc = bsg_get_command(bd);
626 if (!bc)
627 break;
628 if (IS_ERR(bc)) {
629 ret = PTR_ERR(bc);
630 bc = NULL;
631 break;
632 }
633
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100634 bc->uhdr = (struct sg_io_v4 __user *) buf;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200635 if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
636 ret = -EFAULT;
637 break;
638 }
639
640 /*
641 * get a request, fill in the blanks, and add to request queue
642 */
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100643 rq = bsg_map_hdr(bd, &bc->hdr);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200644 if (IS_ERR(rq)) {
645 ret = PTR_ERR(rq);
646 rq = NULL;
647 break;
648 }
649
650 bsg_add_command(bd, q, bc, rq);
651 bc = NULL;
652 rq = NULL;
653 nr_commands--;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100654 buf += sizeof(struct sg_io_v4);
655 *bytes_read += sizeof(struct sg_io_v4);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200656 }
657
Jens Axboe3d6392c2007-07-09 12:38:05 +0200658 if (bc)
659 bsg_free_command(bc);
660
661 return ret;
662}
663
664static ssize_t
665bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
666{
667 struct bsg_device *bd = file->private_data;
668 ssize_t bytes_read;
669 int ret;
670
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100671 dprintk("%s: write %Zd bytes\n", bd->name, count);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200672
673 bsg_set_block(bd, file);
674 bsg_set_write_perm(bd, file);
675
676 bytes_read = 0;
677 ret = __bsg_write(bd, buf, count, &bytes_read);
678 *ppos = bytes_read;
679
680 /*
681 * return bytes written on non-fatal errors
682 */
683 if (!bytes_read || (bytes_read && err_block_err(ret)))
684 bytes_read = ret;
685
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100686 dprintk("%s: returning %Zd\n", bd->name, bytes_read);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200687 return bytes_read;
688}
689
Jens Axboe3d6392c2007-07-09 12:38:05 +0200690static struct bsg_device *bsg_alloc_device(void)
691{
Jens Axboe3d6392c2007-07-09 12:38:05 +0200692 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200693
694 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
695 if (unlikely(!bd))
696 return NULL;
697
698 spin_lock_init(&bd->lock);
699
Jens Axboe5309cb32007-01-23 16:24:41 +0100700 bd->max_queue = BSG_DEFAULT_CMDS;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200701
702 INIT_LIST_HEAD(&bd->busy_list);
703 INIT_LIST_HEAD(&bd->done_list);
704 INIT_HLIST_NODE(&bd->dev_list);
705
706 init_waitqueue_head(&bd->wq_free);
707 init_waitqueue_head(&bd->wq_done);
708 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200709}
710
711static int bsg_put_device(struct bsg_device *bd)
712{
713 int ret = 0;
714
715 mutex_lock(&bsg_mutex);
716
717 if (!atomic_dec_and_test(&bd->ref_count))
718 goto out;
719
720 dprintk("%s: tearing down\n", bd->name);
721
722 /*
723 * close can always block
724 */
725 set_bit(BSG_F_BLOCK, &bd->flags);
726
727 /*
728 * correct error detection baddies here again. it's the responsibility
729 * of the app to properly reap commands before close() if it wants
730 * fool-proof error detection
731 */
732 ret = bsg_complete_all_commands(bd);
733
734 blk_put_queue(bd->queue);
735 hlist_del(&bd->dev_list);
Jens Axboe5309cb32007-01-23 16:24:41 +0100736 kfree(bd);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200737out:
738 mutex_unlock(&bsg_mutex);
739 return ret;
740}
741
742static struct bsg_device *bsg_add_device(struct inode *inode,
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200743 struct request_queue *rq,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200744 struct file *file)
745{
746 struct bsg_device *bd = NULL;
747#ifdef BSG_DEBUG
748 unsigned char buf[32];
749#endif
750
751 bd = bsg_alloc_device();
752 if (!bd)
753 return ERR_PTR(-ENOMEM);
754
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200755 bd->queue = rq;
756 kobject_get(&rq->kobj);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200757 bsg_set_block(bd, file);
758
759 atomic_set(&bd->ref_count, 1);
760 bd->minor = iminor(inode);
761 mutex_lock(&bsg_mutex);
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200762 hlist_add_head(&bd->dev_list, &bsg_device_list[bsg_list_idx(bd->minor)]);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200763
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200764 strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200765 dprintk("bound to <%s>, max queue %d\n",
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100766 format_dev_t(buf, inode->i_rdev), bd->max_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200767
768 mutex_unlock(&bsg_mutex);
769 return bd;
770}
771
772static struct bsg_device *__bsg_get_device(int minor)
773{
774 struct hlist_head *list = &bsg_device_list[bsg_list_idx(minor)];
775 struct bsg_device *bd = NULL;
776 struct hlist_node *entry;
777
778 mutex_lock(&bsg_mutex);
779
780 hlist_for_each(entry, list) {
781 bd = hlist_entry(entry, struct bsg_device, dev_list);
782 if (bd->minor == minor) {
783 atomic_inc(&bd->ref_count);
784 break;
785 }
786
787 bd = NULL;
788 }
789
790 mutex_unlock(&bsg_mutex);
791 return bd;
792}
793
794static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
795{
796 struct bsg_device *bd = __bsg_get_device(iminor(inode));
797 struct bsg_class_device *bcd, *__bcd;
798
799 if (bd)
800 return bd;
801
802 /*
803 * find the class device
804 */
805 bcd = NULL;
806 mutex_lock(&bsg_mutex);
807 list_for_each_entry(__bcd, &bsg_class_list, list) {
808 if (__bcd->minor == iminor(inode)) {
809 bcd = __bcd;
810 break;
811 }
812 }
813 mutex_unlock(&bsg_mutex);
814
815 if (!bcd)
816 return ERR_PTR(-ENODEV);
817
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200818 return bsg_add_device(inode, bcd->queue, file);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200819}
820
821static int bsg_open(struct inode *inode, struct file *file)
822{
823 struct bsg_device *bd = bsg_get_device(inode, file);
824
825 if (IS_ERR(bd))
826 return PTR_ERR(bd);
827
828 file->private_data = bd;
829 return 0;
830}
831
832static int bsg_release(struct inode *inode, struct file *file)
833{
834 struct bsg_device *bd = file->private_data;
835
836 file->private_data = NULL;
837 return bsg_put_device(bd);
838}
839
840static unsigned int bsg_poll(struct file *file, poll_table *wait)
841{
842 struct bsg_device *bd = file->private_data;
843 unsigned int mask = 0;
844
845 poll_wait(file, &bd->wq_done, wait);
846 poll_wait(file, &bd->wq_free, wait);
847
848 spin_lock_irq(&bd->lock);
849 if (!list_empty(&bd->done_list))
850 mask |= POLLIN | POLLRDNORM;
851 if (bd->queued_cmds >= bd->max_queue)
852 mask |= POLLOUT;
853 spin_unlock_irq(&bd->lock);
854
855 return mask;
856}
857
858static int
859bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
860 unsigned long arg)
861{
862 struct bsg_device *bd = file->private_data;
863 int __user *uarg = (int __user *) arg;
864
865 if (!bd)
866 return -ENXIO;
867
868 switch (cmd) {
869 /*
870 * our own ioctls
871 */
872 case SG_GET_COMMAND_Q:
873 return put_user(bd->max_queue, uarg);
Jens Axboe5309cb32007-01-23 16:24:41 +0100874 case SG_SET_COMMAND_Q: {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200875 int queue;
876
877 if (get_user(queue, uarg))
878 return -EFAULT;
Jens Axboe5309cb32007-01-23 16:24:41 +0100879 if (queue < 1)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200880 return -EINVAL;
881
Jens Axboe5309cb32007-01-23 16:24:41 +0100882 spin_lock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200883 bd->max_queue = queue;
Jens Axboe5309cb32007-01-23 16:24:41 +0100884 spin_unlock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200885 return 0;
886 }
887
888 /*
889 * SCSI/sg ioctls
890 */
891 case SG_GET_VERSION_NUM:
892 case SCSI_IOCTL_GET_IDLUN:
893 case SCSI_IOCTL_GET_BUS_NUMBER:
894 case SG_SET_TIMEOUT:
895 case SG_GET_TIMEOUT:
896 case SG_GET_RESERVED_SIZE:
897 case SG_SET_RESERVED_SIZE:
898 case SG_EMULATED_HOST:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200899 case SCSI_IOCTL_SEND_COMMAND: {
900 void __user *uarg = (void __user *) arg;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200901 return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200902 }
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100903 case SG_IO: {
904 struct request *rq;
905 struct bio *bio;
906 struct sg_io_v4 hdr;
907
908 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
909 return -EFAULT;
910
911 rq = bsg_map_hdr(bd, &hdr);
912 if (IS_ERR(rq))
913 return PTR_ERR(rq);
914
915 bio = rq->bio;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200916 blk_execute_rq(bd->queue, NULL, rq, 0);
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100917 blk_complete_sgv4_hdr_rq(rq, &hdr, bio);
918
919 if (copy_to_user(uarg, &hdr, sizeof(hdr)))
920 return -EFAULT;
Jens Axboeb711afa2006-12-20 11:25:23 +0100921
922 return 0;
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100923 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200924 /*
925 * block device ioctls
926 */
927 default:
928#if 0
929 return ioctl_by_bdev(bd->bdev, cmd, arg);
930#else
931 return -ENOTTY;
932#endif
933 }
934}
935
936static struct file_operations bsg_fops = {
937 .read = bsg_read,
938 .write = bsg_write,
939 .poll = bsg_poll,
940 .open = bsg_open,
941 .release = bsg_release,
942 .ioctl = bsg_ioctl,
943 .owner = THIS_MODULE,
944};
945
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200946void bsg_unregister_queue(struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200947{
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200948 struct bsg_class_device *bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200949
950 if (!bcd->class_dev)
951 return;
952
953 mutex_lock(&bsg_mutex);
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200954 sysfs_remove_link(&q->kobj, "bsg");
Jens Axboe3d6392c2007-07-09 12:38:05 +0200955 class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
956 bcd->class_dev = NULL;
957 list_del_init(&bcd->list);
958 mutex_unlock(&bsg_mutex);
959}
960
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200961int bsg_register_queue(struct request_queue *q, char *name)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200962{
Jens Axboe3d6392c2007-07-09 12:38:05 +0200963 struct bsg_class_device *bcd;
964 dev_t dev;
965
966 /*
967 * we need a proper transport to send commands, not a stacked device
968 */
969 if (!q->request_fn)
970 return 0;
971
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200972 bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200973 memset(bcd, 0, sizeof(*bcd));
974 INIT_LIST_HEAD(&bcd->list);
975
976 mutex_lock(&bsg_mutex);
977 dev = MKDEV(BSG_MAJOR, bsg_device_nr);
978 bcd->minor = bsg_device_nr;
979 bsg_device_nr++;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200980 bcd->queue = q;
981 bcd->class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
Jens Axboe264a0472007-01-23 16:30:17 +0100982 if (!bcd->class_dev)
983 goto err;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200984 list_add_tail(&bcd->list, &bsg_class_list);
Jens Axboe264a0472007-01-23 16:30:17 +0100985 if (sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"))
986 goto err;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200987 mutex_unlock(&bsg_mutex);
988 return 0;
Jens Axboe264a0472007-01-23 16:30:17 +0100989err:
990 bsg_device_nr--;
991 if (bcd->class_dev)
992 class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
993 mutex_unlock(&bsg_mutex);
994 return -ENOMEM;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200995}
996
997static int __init bsg_init(void)
998{
999 int ret, i;
1000
Jens Axboe5309cb32007-01-23 16:24:41 +01001001 bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
1002 sizeof(struct bsg_command), 0, 0, NULL, NULL);
1003 if (!bsg_cmd_cachep) {
1004 printk(KERN_ERR "bsg: failed creating slab cache\n");
1005 return -ENOMEM;
1006 }
1007
Jens Axboe3d6392c2007-07-09 12:38:05 +02001008 for (i = 0; i < BSG_LIST_SIZE; i++)
1009 INIT_HLIST_HEAD(&bsg_device_list[i]);
1010
1011 bsg_class = class_create(THIS_MODULE, "bsg");
Jens Axboe5309cb32007-01-23 16:24:41 +01001012 if (IS_ERR(bsg_class)) {
1013 kmem_cache_destroy(bsg_cmd_cachep);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001014 return PTR_ERR(bsg_class);
Jens Axboe5309cb32007-01-23 16:24:41 +01001015 }
Jens Axboe3d6392c2007-07-09 12:38:05 +02001016
1017 ret = register_chrdev(BSG_MAJOR, "bsg", &bsg_fops);
1018 if (ret) {
Jens Axboe5309cb32007-01-23 16:24:41 +01001019 kmem_cache_destroy(bsg_cmd_cachep);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001020 class_destroy(bsg_class);
1021 return ret;
1022 }
1023
1024 printk(KERN_INFO "%s loaded\n", bsg_version);
1025 return 0;
1026}
1027
1028MODULE_AUTHOR("Jens Axboe");
1029MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
1030MODULE_LICENSE("GPL");
1031
1032subsys_initcall(bsg_init);