]> bbs.cooldavid.org Git - net-next-2.6.git/blame - block/bsg.c
bind bsg to all SCSI devices
[net-next-2.6.git] / block / bsg.c
CommitLineData
3d6392cf
JA
1/*
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 */
3d6392cf
JA
19#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>
4e2872d6
FT
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_driver.h>
3d6392cf
JA
34#include <scsi/sg.h>
35
36static char bsg_version[] = "block layer sg (bsg) 0.4";
37
3d6392cf 38struct bsg_device {
3d6392cf
JA
39 request_queue_t *queue;
40 spinlock_t lock;
41 struct list_head busy_list;
42 struct list_head done_list;
43 struct hlist_node dev_list;
44 atomic_t ref_count;
45 int minor;
46 int queued_cmds;
47 int done_cmds;
3d6392cf
JA
48 wait_queue_head_t wq_done;
49 wait_queue_head_t wq_free;
d351af01 50 char name[BUS_ID_SIZE];
3d6392cf
JA
51 int max_queue;
52 unsigned long flags;
53};
54
55enum {
56 BSG_F_BLOCK = 1,
57 BSG_F_WRITE_PERM = 2,
58};
59
5309cb38 60#define BSG_DEFAULT_CMDS 64
3d6392cf
JA
61
62#undef BSG_DEBUG
63
64#ifdef BSG_DEBUG
65#define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
66#else
67#define dprintk(fmt, args...)
68#endif
69
70#define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
71
72/*
73 * just for testing
74 */
75#define BSG_MAJOR (240)
76
77static DEFINE_MUTEX(bsg_mutex);
78static int bsg_device_nr;
79
80#define BSG_LIST_SIZE (8)
81#define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
82static struct hlist_head bsg_device_list[BSG_LIST_SIZE];
83
84static struct class *bsg_class;
85static LIST_HEAD(bsg_class_list);
86
5309cb38
JA
87static struct kmem_cache *bsg_cmd_cachep;
88
3d6392cf
JA
89/*
90 * our internal command type
91 */
92struct bsg_command {
93 struct bsg_device *bd;
94 struct list_head list;
95 struct request *rq;
96 struct bio *bio;
97 int err;
70e36ece
FT
98 struct sg_io_v4 hdr;
99 struct sg_io_v4 __user *uhdr;
3d6392cf
JA
100 char sense[SCSI_SENSE_BUFFERSIZE];
101};
102
103static void bsg_free_command(struct bsg_command *bc)
104{
105 struct bsg_device *bd = bc->bd;
3d6392cf
JA
106 unsigned long flags;
107
5309cb38 108 kmem_cache_free(bsg_cmd_cachep, bc);
3d6392cf
JA
109
110 spin_lock_irqsave(&bd->lock, flags);
111 bd->queued_cmds--;
3d6392cf
JA
112 spin_unlock_irqrestore(&bd->lock, flags);
113
114 wake_up(&bd->wq_free);
115}
116
117static struct bsg_command *__bsg_alloc_command(struct bsg_device *bd)
118{
119 struct bsg_command *bc = NULL;
3d6392cf
JA
120
121 spin_lock_irq(&bd->lock);
122
123 if (bd->queued_cmds >= bd->max_queue)
124 goto out;
125
3d6392cf 126 bd->queued_cmds++;
3d6392cf
JA
127 spin_unlock_irq(&bd->lock);
128
5309cb38
JA
129 bc = kmem_cache_alloc(bsg_cmd_cachep, GFP_USER);
130 if (unlikely(!bc)) {
131 spin_lock_irq(&bd->lock);
7e75d730
FT
132 bd->queued_cmds--;
133 goto out;
5309cb38
JA
134 }
135
3d6392cf
JA
136 memset(bc, 0, sizeof(*bc));
137 bc->bd = bd;
138 INIT_LIST_HEAD(&bc->list);
5309cb38 139 dprintk("%s: returning free cmd %p\n", bd->name, bc);
3d6392cf
JA
140 return bc;
141out:
3d6392cf
JA
142 spin_unlock_irq(&bd->lock);
143 return bc;
144}
145
146static inline void
147bsg_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
148{
149 bd->done_cmds--;
150 list_del(&bc->list);
151}
152
153static inline void
154bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
155{
156 bd->done_cmds++;
157 list_add_tail(&bc->list, &bd->done_list);
158 wake_up(&bd->wq_done);
159}
160
161static inline int bsg_io_schedule(struct bsg_device *bd, int state)
162{
163 DEFINE_WAIT(wait);
164 int ret = 0;
165
166 spin_lock_irq(&bd->lock);
167
168 BUG_ON(bd->done_cmds > bd->queued_cmds);
169
170 /*
171 * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
172 * work to do", even though we return -ENOSPC after this same test
173 * during bsg_write() -- there, it means our buffer can't have more
174 * bsg_commands added to it, thus has no space left.
175 */
176 if (bd->done_cmds == bd->queued_cmds) {
177 ret = -ENODATA;
178 goto unlock;
179 }
180
181 if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
182 ret = -EAGAIN;
183 goto unlock;
184 }
185
186 prepare_to_wait(&bd->wq_done, &wait, state);
187 spin_unlock_irq(&bd->lock);
188 io_schedule();
189 finish_wait(&bd->wq_done, &wait);
190
191 if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
192 ret = -ERESTARTSYS;
193
194 return ret;
195unlock:
196 spin_unlock_irq(&bd->lock);
197 return ret;
198}
199
200/*
201 * get a new free command, blocking if needed and specified
202 */
203static struct bsg_command *bsg_get_command(struct bsg_device *bd)
204{
205 struct bsg_command *bc;
206 int ret;
207
208 do {
209 bc = __bsg_alloc_command(bd);
210 if (bc)
211 break;
212
213 ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);
214 if (ret) {
215 bc = ERR_PTR(ret);
216 break;
217 }
218
219 } while (1);
220
221 return bc;
222}
223
70e36ece
FT
224static int blk_fill_sgv4_hdr_rq(request_queue_t *q, struct request *rq,
225 struct sg_io_v4 *hdr, int has_write_perm)
226{
227 memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
228
229 if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
230 hdr->request_len))
231 return -EFAULT;
232 if (blk_verify_command(rq->cmd, has_write_perm))
233 return -EPERM;
234
235 /*
236 * fill in request structure
237 */
238 rq->cmd_len = hdr->request_len;
239 rq->cmd_type = REQ_TYPE_BLOCK_PC;
240
241 rq->timeout = (hdr->timeout * HZ) / 1000;
242 if (!rq->timeout)
243 rq->timeout = q->sg_timeout;
244 if (!rq->timeout)
245 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
246
247 return 0;
248}
249
3d6392cf 250/*
70e36ece 251 * Check if sg_io_v4 from user is allowed and valid
3d6392cf
JA
252 */
253static int
70e36ece 254bsg_validate_sgv4_hdr(request_queue_t *q, struct sg_io_v4 *hdr, int *rw)
3d6392cf 255{
70e36ece 256 if (hdr->guard != 'Q')
3d6392cf 257 return -EINVAL;
70e36ece 258 if (hdr->request_len > BLK_MAX_CDB)
3d6392cf 259 return -EINVAL;
70e36ece
FT
260 if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
261 hdr->din_xfer_len > (q->max_sectors << 9))
3d6392cf
JA
262 return -EIO;
263
70e36ece
FT
264 /* not supported currently */
265 if (hdr->protocol || hdr->subprotocol)
266 return -EINVAL;
267
3d6392cf
JA
268 /*
269 * looks sane, if no data then it should be fine from our POV
270 */
70e36ece 271 if (!hdr->dout_xfer_len && !hdr->din_xfer_len)
3d6392cf
JA
272 return 0;
273
70e36ece
FT
274 /* not supported currently */
275 if (hdr->dout_xfer_len && hdr->din_xfer_len)
276 return -EINVAL;
277
278 *rw = hdr->dout_xfer_len ? WRITE : READ;
3d6392cf
JA
279
280 return 0;
281}
282
283/*
70e36ece 284 * map sg_io_v4 to a request.
3d6392cf
JA
285 */
286static struct request *
70e36ece 287bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
3d6392cf
JA
288{
289 request_queue_t *q = bd->queue;
3d6392cf 290 struct request *rq;
2ef7086a 291 int ret, rw = 0; /* shut up gcc */
70e36ece
FT
292 unsigned int dxfer_len;
293 void *dxferp = NULL;
3d6392cf 294
70e36ece
FT
295 dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
296 hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
297 hdr->din_xfer_len);
3d6392cf 298
70e36ece 299 ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
3d6392cf
JA
300 if (ret)
301 return ERR_PTR(ret);
302
303 /*
304 * map scatter-gather elements seperately and string them to request
305 */
306 rq = blk_get_request(q, rw, GFP_KERNEL);
70e36ece
FT
307 ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
308 &bd->flags));
3d6392cf
JA
309 if (ret) {
310 blk_put_request(rq);
311 return ERR_PTR(ret);
312 }
313
70e36ece
FT
314 if (hdr->dout_xfer_len) {
315 dxfer_len = hdr->dout_xfer_len;
316 dxferp = (void*)(unsigned long)hdr->dout_xferp;
317 } else if (hdr->din_xfer_len) {
318 dxfer_len = hdr->din_xfer_len;
319 dxferp = (void*)(unsigned long)hdr->din_xferp;
320 } else
321 dxfer_len = 0;
322
323 if (dxfer_len) {
324 ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
325 if (ret) {
326 dprintk("failed map at %d\n", ret);
327 blk_put_request(rq);
328 rq = ERR_PTR(ret);
3d6392cf 329 }
3d6392cf
JA
330 }
331
332 return rq;
333}
334
335/*
336 * async completion call-back from the block layer, when scsi/ide/whatever
337 * calls end_that_request_last() on a request
338 */
339static void bsg_rq_end_io(struct request *rq, int uptodate)
340{
341 struct bsg_command *bc = rq->end_io_data;
342 struct bsg_device *bd = bc->bd;
343 unsigned long flags;
344
5309cb38
JA
345 dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
346 bd->name, rq, bc, bc->bio, uptodate);
3d6392cf
JA
347
348 bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
349
350 spin_lock_irqsave(&bd->lock, flags);
351 list_del(&bc->list);
352 bsg_add_done_cmd(bd, bc);
353 spin_unlock_irqrestore(&bd->lock, flags);
354}
355
356/*
357 * do final setup of a 'bc' and submit the matching 'rq' to the block
358 * layer for io
359 */
360static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
361 struct bsg_command *bc, struct request *rq)
362{
363 rq->sense = bc->sense;
364 rq->sense_len = 0;
365
366 /*
367 * add bc command to busy queue and submit rq for io
368 */
369 bc->rq = rq;
370 bc->bio = rq->bio;
371 bc->hdr.duration = jiffies;
372 spin_lock_irq(&bd->lock);
373 list_add_tail(&bc->list, &bd->busy_list);
374 spin_unlock_irq(&bd->lock);
375
376 dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
377
378 rq->end_io_data = bc;
d351af01 379 blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
3d6392cf
JA
380}
381
382static inline struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
383{
384 struct bsg_command *bc = NULL;
385
386 spin_lock_irq(&bd->lock);
387 if (bd->done_cmds) {
388 bc = list_entry_bc(bd->done_list.next);
389 bsg_del_done_cmd(bd, bc);
390 }
391 spin_unlock_irq(&bd->lock);
392
393 return bc;
394}
395
396/*
397 * Get a finished command from the done list
398 */
399static struct bsg_command *__bsg_get_done_cmd(struct bsg_device *bd, int state)
400{
401 struct bsg_command *bc;
402 int ret;
403
404 do {
405 bc = bsg_next_done_cmd(bd);
406 if (bc)
407 break;
408
409 ret = bsg_io_schedule(bd, state);
410 if (ret) {
411 bc = ERR_PTR(ret);
412 break;
413 }
414 } while (1);
415
416 dprintk("%s: returning done %p\n", bd->name, bc);
417
418 return bc;
419}
420
421static struct bsg_command *
422bsg_get_done_cmd(struct bsg_device *bd, const struct iovec *iov)
423{
424 return __bsg_get_done_cmd(bd, TASK_INTERRUPTIBLE);
425}
426
427static struct bsg_command *
428bsg_get_done_cmd_nosignals(struct bsg_device *bd)
429{
430 return __bsg_get_done_cmd(bd, TASK_UNINTERRUPTIBLE);
431}
432
70e36ece
FT
433static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
434 struct bio *bio)
435{
436 int ret = 0;
437
438 dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
439 /*
440 * fill in all the output members
441 */
442 hdr->device_status = status_byte(rq->errors);
443 hdr->transport_status = host_byte(rq->errors);
444 hdr->driver_status = driver_byte(rq->errors);
445 hdr->info = 0;
446 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
447 hdr->info |= SG_INFO_CHECK;
448 hdr->din_resid = rq->data_len;
449 hdr->response_len = 0;
450
451 if (rq->sense_len && hdr->response) {
452 int len = min((unsigned int) hdr->max_response_len,
453 rq->sense_len);
454
455 ret = copy_to_user((void*)(unsigned long)hdr->response,
456 rq->sense, len);
457 if (!ret)
458 hdr->response_len = len;
459 else
460 ret = -EFAULT;
461 }
462
463 blk_rq_unmap_user(bio);
464 blk_put_request(rq);
465
466 return ret;
467}
468
3d6392cf
JA
469static int bsg_complete_all_commands(struct bsg_device *bd)
470{
471 struct bsg_command *bc;
472 int ret, tret;
473
474 dprintk("%s: entered\n", bd->name);
475
476 set_bit(BSG_F_BLOCK, &bd->flags);
477
478 /*
479 * wait for all commands to complete
480 */
481 ret = 0;
482 do {
483 ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
484 /*
485 * look for -ENODATA specifically -- we'll sometimes get
486 * -ERESTARTSYS when we've taken a signal, but we can't
487 * return until we're done freeing the queue, so ignore
488 * it. The signal will get handled when we're done freeing
489 * the bsg_device.
490 */
491 } while (ret != -ENODATA);
492
493 /*
494 * discard done commands
495 */
496 ret = 0;
497 do {
498 bc = bsg_get_done_cmd_nosignals(bd);
499
500 /*
501 * we _must_ complete before restarting, because
502 * bsg_release can't handle this failing.
503 */
504 if (PTR_ERR(bc) == -ERESTARTSYS)
505 continue;
506 if (IS_ERR(bc)) {
507 ret = PTR_ERR(bc);
508 break;
509 }
510
70e36ece 511 tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
3d6392cf
JA
512 if (!ret)
513 ret = tret;
514
515 bsg_free_command(bc);
516 } while (1);
517
518 return ret;
519}
520
521typedef struct bsg_command *(*bsg_command_callback)(struct bsg_device *bd, const struct iovec *iov);
522
523static ssize_t
524__bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
525 struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
526{
527 struct bsg_command *bc;
528 int nr_commands, ret;
529
70e36ece 530 if (count % sizeof(struct sg_io_v4))
3d6392cf
JA
531 return -EINVAL;
532
533 ret = 0;
70e36ece 534 nr_commands = count / sizeof(struct sg_io_v4);
3d6392cf
JA
535 while (nr_commands) {
536 bc = get_bc(bd, iov);
537 if (IS_ERR(bc)) {
538 ret = PTR_ERR(bc);
539 break;
540 }
541
542 /*
543 * this is the only case where we need to copy data back
544 * after completing the request. so do that here,
545 * bsg_complete_work() cannot do that for us
546 */
70e36ece 547 ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
3d6392cf
JA
548
549 if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
550 ret = -EFAULT;
551
552 bsg_free_command(bc);
553
554 if (ret)
555 break;
556
70e36ece
FT
557 buf += sizeof(struct sg_io_v4);
558 *bytes_read += sizeof(struct sg_io_v4);
3d6392cf
JA
559 nr_commands--;
560 }
561
562 return ret;
563}
564
565static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
566{
567 if (file->f_flags & O_NONBLOCK)
568 clear_bit(BSG_F_BLOCK, &bd->flags);
569 else
570 set_bit(BSG_F_BLOCK, &bd->flags);
571}
572
573static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
574{
575 if (file->f_mode & FMODE_WRITE)
576 set_bit(BSG_F_WRITE_PERM, &bd->flags);
577 else
578 clear_bit(BSG_F_WRITE_PERM, &bd->flags);
579}
580
581static inline int err_block_err(int ret)
582{
583 if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
584 return 1;
585
586 return 0;
587}
588
589static ssize_t
590bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
591{
592 struct bsg_device *bd = file->private_data;
593 int ret;
594 ssize_t bytes_read;
595
9e69fbb5 596 dprintk("%s: read %Zd bytes\n", bd->name, count);
3d6392cf
JA
597
598 bsg_set_block(bd, file);
599 bytes_read = 0;
600 ret = __bsg_read(buf, count, bsg_get_done_cmd,
601 bd, NULL, &bytes_read);
602 *ppos = bytes_read;
603
604 if (!bytes_read || (bytes_read && err_block_err(ret)))
605 bytes_read = ret;
606
607 return bytes_read;
608}
609
610static ssize_t __bsg_write(struct bsg_device *bd, const char __user *buf,
611 size_t count, ssize_t *bytes_read)
612{
613 struct bsg_command *bc;
614 struct request *rq;
615 int ret, nr_commands;
616
70e36ece 617 if (count % sizeof(struct sg_io_v4))
3d6392cf
JA
618 return -EINVAL;
619
70e36ece 620 nr_commands = count / sizeof(struct sg_io_v4);
3d6392cf
JA
621 rq = NULL;
622 bc = NULL;
623 ret = 0;
624 while (nr_commands) {
625 request_queue_t *q = bd->queue;
3d6392cf
JA
626
627 bc = bsg_get_command(bd);
628 if (!bc)
629 break;
630 if (IS_ERR(bc)) {
631 ret = PTR_ERR(bc);
632 bc = NULL;
633 break;
634 }
635
70e36ece 636 bc->uhdr = (struct sg_io_v4 __user *) buf;
3d6392cf
JA
637 if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
638 ret = -EFAULT;
639 break;
640 }
641
642 /*
643 * get a request, fill in the blanks, and add to request queue
644 */
70e36ece 645 rq = bsg_map_hdr(bd, &bc->hdr);
3d6392cf
JA
646 if (IS_ERR(rq)) {
647 ret = PTR_ERR(rq);
648 rq = NULL;
649 break;
650 }
651
652 bsg_add_command(bd, q, bc, rq);
653 bc = NULL;
654 rq = NULL;
655 nr_commands--;
70e36ece
FT
656 buf += sizeof(struct sg_io_v4);
657 *bytes_read += sizeof(struct sg_io_v4);
3d6392cf
JA
658 }
659
3d6392cf
JA
660 if (bc)
661 bsg_free_command(bc);
662
663 return ret;
664}
665
666static ssize_t
667bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
668{
669 struct bsg_device *bd = file->private_data;
670 ssize_t bytes_read;
671 int ret;
672
9e69fbb5 673 dprintk("%s: write %Zd bytes\n", bd->name, count);
3d6392cf
JA
674
675 bsg_set_block(bd, file);
676 bsg_set_write_perm(bd, file);
677
678 bytes_read = 0;
679 ret = __bsg_write(bd, buf, count, &bytes_read);
680 *ppos = bytes_read;
681
682 /*
683 * return bytes written on non-fatal errors
684 */
685 if (!bytes_read || (bytes_read && err_block_err(ret)))
686 bytes_read = ret;
687
9e69fbb5 688 dprintk("%s: returning %Zd\n", bd->name, bytes_read);
3d6392cf
JA
689 return bytes_read;
690}
691
3d6392cf
JA
692static struct bsg_device *bsg_alloc_device(void)
693{
3d6392cf 694 struct bsg_device *bd;
3d6392cf
JA
695
696 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
697 if (unlikely(!bd))
698 return NULL;
699
700 spin_lock_init(&bd->lock);
701
5309cb38 702 bd->max_queue = BSG_DEFAULT_CMDS;
3d6392cf
JA
703
704 INIT_LIST_HEAD(&bd->busy_list);
705 INIT_LIST_HEAD(&bd->done_list);
706 INIT_HLIST_NODE(&bd->dev_list);
707
708 init_waitqueue_head(&bd->wq_free);
709 init_waitqueue_head(&bd->wq_done);
710 return bd;
3d6392cf
JA
711}
712
713static int bsg_put_device(struct bsg_device *bd)
714{
715 int ret = 0;
716
717 mutex_lock(&bsg_mutex);
718
719 if (!atomic_dec_and_test(&bd->ref_count))
720 goto out;
721
722 dprintk("%s: tearing down\n", bd->name);
723
724 /*
725 * close can always block
726 */
727 set_bit(BSG_F_BLOCK, &bd->flags);
728
729 /*
730 * correct error detection baddies here again. it's the responsibility
731 * of the app to properly reap commands before close() if it wants
732 * fool-proof error detection
733 */
734 ret = bsg_complete_all_commands(bd);
735
736 blk_put_queue(bd->queue);
737 hlist_del(&bd->dev_list);
5309cb38 738 kfree(bd);
3d6392cf
JA
739out:
740 mutex_unlock(&bsg_mutex);
741 return ret;
742}
743
744static struct bsg_device *bsg_add_device(struct inode *inode,
d351af01 745 struct request_queue *rq,
3d6392cf
JA
746 struct file *file)
747{
748 struct bsg_device *bd = NULL;
749#ifdef BSG_DEBUG
750 unsigned char buf[32];
751#endif
752
753 bd = bsg_alloc_device();
754 if (!bd)
755 return ERR_PTR(-ENOMEM);
756
d351af01
FT
757 bd->queue = rq;
758 kobject_get(&rq->kobj);
3d6392cf
JA
759 bsg_set_block(bd, file);
760
761 atomic_set(&bd->ref_count, 1);
762 bd->minor = iminor(inode);
763 mutex_lock(&bsg_mutex);
d351af01 764 hlist_add_head(&bd->dev_list, &bsg_device_list[bsg_list_idx(bd->minor)]);
3d6392cf 765
d351af01 766 strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
3d6392cf 767 dprintk("bound to <%s>, max queue %d\n",
9e69fbb5 768 format_dev_t(buf, inode->i_rdev), bd->max_queue);
3d6392cf
JA
769
770 mutex_unlock(&bsg_mutex);
771 return bd;
772}
773
774static struct bsg_device *__bsg_get_device(int minor)
775{
776 struct hlist_head *list = &bsg_device_list[bsg_list_idx(minor)];
777 struct bsg_device *bd = NULL;
778 struct hlist_node *entry;
779
780 mutex_lock(&bsg_mutex);
781
782 hlist_for_each(entry, list) {
783 bd = hlist_entry(entry, struct bsg_device, dev_list);
784 if (bd->minor == minor) {
785 atomic_inc(&bd->ref_count);
786 break;
787 }
788
789 bd = NULL;
790 }
791
792 mutex_unlock(&bsg_mutex);
793 return bd;
794}
795
796static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
797{
798 struct bsg_device *bd = __bsg_get_device(iminor(inode));
799 struct bsg_class_device *bcd, *__bcd;
800
801 if (bd)
802 return bd;
803
804 /*
805 * find the class device
806 */
807 bcd = NULL;
808 mutex_lock(&bsg_mutex);
809 list_for_each_entry(__bcd, &bsg_class_list, list) {
810 if (__bcd->minor == iminor(inode)) {
811 bcd = __bcd;
812 break;
813 }
814 }
815 mutex_unlock(&bsg_mutex);
816
817 if (!bcd)
818 return ERR_PTR(-ENODEV);
819
d351af01 820 return bsg_add_device(inode, bcd->queue, file);
3d6392cf
JA
821}
822
823static int bsg_open(struct inode *inode, struct file *file)
824{
825 struct bsg_device *bd = bsg_get_device(inode, file);
826
827 if (IS_ERR(bd))
828 return PTR_ERR(bd);
829
830 file->private_data = bd;
831 return 0;
832}
833
834static int bsg_release(struct inode *inode, struct file *file)
835{
836 struct bsg_device *bd = file->private_data;
837
838 file->private_data = NULL;
839 return bsg_put_device(bd);
840}
841
842static unsigned int bsg_poll(struct file *file, poll_table *wait)
843{
844 struct bsg_device *bd = file->private_data;
845 unsigned int mask = 0;
846
847 poll_wait(file, &bd->wq_done, wait);
848 poll_wait(file, &bd->wq_free, wait);
849
850 spin_lock_irq(&bd->lock);
851 if (!list_empty(&bd->done_list))
852 mask |= POLLIN | POLLRDNORM;
853 if (bd->queued_cmds >= bd->max_queue)
854 mask |= POLLOUT;
855 spin_unlock_irq(&bd->lock);
856
857 return mask;
858}
859
860static int
861bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
862 unsigned long arg)
863{
864 struct bsg_device *bd = file->private_data;
865 int __user *uarg = (int __user *) arg;
866
867 if (!bd)
868 return -ENXIO;
869
870 switch (cmd) {
871 /*
872 * our own ioctls
873 */
874 case SG_GET_COMMAND_Q:
875 return put_user(bd->max_queue, uarg);
5309cb38 876 case SG_SET_COMMAND_Q: {
3d6392cf
JA
877 int queue;
878
879 if (get_user(queue, uarg))
880 return -EFAULT;
5309cb38 881 if (queue < 1)
3d6392cf
JA
882 return -EINVAL;
883
5309cb38 884 spin_lock_irq(&bd->lock);
3d6392cf 885 bd->max_queue = queue;
5309cb38 886 spin_unlock_irq(&bd->lock);
3d6392cf
JA
887 return 0;
888 }
889
890 /*
891 * SCSI/sg ioctls
892 */
893 case SG_GET_VERSION_NUM:
894 case SCSI_IOCTL_GET_IDLUN:
895 case SCSI_IOCTL_GET_BUS_NUMBER:
896 case SG_SET_TIMEOUT:
897 case SG_GET_TIMEOUT:
898 case SG_GET_RESERVED_SIZE:
899 case SG_SET_RESERVED_SIZE:
900 case SG_EMULATED_HOST:
3d6392cf
JA
901 case SCSI_IOCTL_SEND_COMMAND: {
902 void __user *uarg = (void __user *) arg;
d351af01 903 return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
3d6392cf 904 }
10e8855b
FT
905 case SG_IO: {
906 struct request *rq;
907 struct bio *bio;
908 struct sg_io_v4 hdr;
909
910 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
911 return -EFAULT;
912
913 rq = bsg_map_hdr(bd, &hdr);
914 if (IS_ERR(rq))
915 return PTR_ERR(rq);
916
917 bio = rq->bio;
d351af01 918 blk_execute_rq(bd->queue, NULL, rq, 0);
10e8855b
FT
919 blk_complete_sgv4_hdr_rq(rq, &hdr, bio);
920
921 if (copy_to_user(uarg, &hdr, sizeof(hdr)))
922 return -EFAULT;
b711afa6
JA
923
924 return 0;
10e8855b 925 }
3d6392cf
JA
926 /*
927 * block device ioctls
928 */
929 default:
930#if 0
931 return ioctl_by_bdev(bd->bdev, cmd, arg);
932#else
933 return -ENOTTY;
934#endif
935 }
936}
937
938static struct file_operations bsg_fops = {
939 .read = bsg_read,
940 .write = bsg_write,
941 .poll = bsg_poll,
942 .open = bsg_open,
943 .release = bsg_release,
944 .ioctl = bsg_ioctl,
945 .owner = THIS_MODULE,
946};
947
d351af01 948void bsg_unregister_queue(struct request_queue *q)
3d6392cf 949{
d351af01 950 struct bsg_class_device *bcd = &q->bsg_dev;
3d6392cf
JA
951
952 if (!bcd->class_dev)
953 return;
954
955 mutex_lock(&bsg_mutex);
d351af01 956 sysfs_remove_link(&q->kobj, "bsg");
3d6392cf
JA
957 class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
958 bcd->class_dev = NULL;
959 list_del_init(&bcd->list);
960 mutex_unlock(&bsg_mutex);
961}
962
d351af01 963int bsg_register_queue(struct request_queue *q, char *name)
3d6392cf 964{
3d6392cf
JA
965 struct bsg_class_device *bcd;
966 dev_t dev;
4e2872d6
FT
967 int ret;
968 struct class_device *class_dev = NULL;
3d6392cf
JA
969
970 /*
971 * we need a proper transport to send commands, not a stacked device
972 */
973 if (!q->request_fn)
974 return 0;
975
d351af01 976 bcd = &q->bsg_dev;
3d6392cf
JA
977 memset(bcd, 0, sizeof(*bcd));
978 INIT_LIST_HEAD(&bcd->list);
979
980 mutex_lock(&bsg_mutex);
981 dev = MKDEV(BSG_MAJOR, bsg_device_nr);
982 bcd->minor = bsg_device_nr;
983 bsg_device_nr++;
d351af01 984 bcd->queue = q;
4e2872d6
FT
985 class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
986 if (IS_ERR(class_dev)) {
987 ret = PTR_ERR(class_dev);
264a0472 988 goto err;
4e2872d6
FT
989 }
990 bcd->class_dev = class_dev;
991
992 if (q->kobj.dentry) {
993 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
994 if (ret)
995 goto err;
996 }
997
3d6392cf 998 list_add_tail(&bcd->list, &bsg_class_list);
4e2872d6 999
3d6392cf
JA
1000 mutex_unlock(&bsg_mutex);
1001 return 0;
264a0472
JA
1002err:
1003 bsg_device_nr--;
4e2872d6 1004 if (class_dev)
264a0472
JA
1005 class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
1006 mutex_unlock(&bsg_mutex);
4e2872d6
FT
1007 return ret;
1008}
1009
1010static int bsg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
1011{
1012 int ret;
1013 struct scsi_device *sdp = to_scsi_device(cl_dev->dev);
1014 struct request_queue *rq = sdp->request_queue;
1015
1016 if (rq->kobj.parent)
1017 ret = bsg_register_queue(rq, kobject_name(rq->kobj.parent));
1018 else
1019 ret = bsg_register_queue(rq, kobject_name(&sdp->sdev_gendev.kobj));
1020 return ret;
3d6392cf
JA
1021}
1022
4e2872d6
FT
1023static void bsg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
1024{
1025 bsg_unregister_queue(to_scsi_device(cl_dev->dev)->request_queue);
1026}
1027
1028static struct class_interface bsg_intf = {
1029 .add = bsg_add,
1030 .remove = bsg_remove,
1031};
1032
3d6392cf
JA
1033static int __init bsg_init(void)
1034{
1035 int ret, i;
1036
5309cb38
JA
1037 bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
1038 sizeof(struct bsg_command), 0, 0, NULL, NULL);
1039 if (!bsg_cmd_cachep) {
1040 printk(KERN_ERR "bsg: failed creating slab cache\n");
1041 return -ENOMEM;
1042 }
1043
3d6392cf
JA
1044 for (i = 0; i < BSG_LIST_SIZE; i++)
1045 INIT_HLIST_HEAD(&bsg_device_list[i]);
1046
1047 bsg_class = class_create(THIS_MODULE, "bsg");
5309cb38
JA
1048 if (IS_ERR(bsg_class)) {
1049 kmem_cache_destroy(bsg_cmd_cachep);
3d6392cf 1050 return PTR_ERR(bsg_class);
5309cb38 1051 }
3d6392cf
JA
1052
1053 ret = register_chrdev(BSG_MAJOR, "bsg", &bsg_fops);
1054 if (ret) {
5309cb38 1055 kmem_cache_destroy(bsg_cmd_cachep);
3d6392cf
JA
1056 class_destroy(bsg_class);
1057 return ret;
1058 }
1059
4e2872d6
FT
1060 ret = scsi_register_interface(&bsg_intf);
1061 if (ret) {
1062 printk(KERN_ERR "bsg: failed register scsi interface %d\n", ret);
1063 kmem_cache_destroy(bsg_cmd_cachep);
1064 class_destroy(bsg_class);
1065 unregister_chrdev(BSG_MAJOR, "bsg");
1066 return ret;
1067 }
1068
3d6392cf
JA
1069 printk(KERN_INFO "%s loaded\n", bsg_version);
1070 return 0;
1071}
1072
1073MODULE_AUTHOR("Jens Axboe");
1074MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
1075MODULE_LICENSE("GPL");
1076
4e2872d6 1077device_initcall(bsg_init);