2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
4 * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
6 * May be copied or modified under the terms of the GNU General Public
7 * License. See linux/COPYING for more information.
9 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
12 * Theory of operation:
14 * At the lowest level, there is the standard driver for the CD/DVD device,
15 * typically ide-cd.c or sr.c. This driver can handle read and write requests,
16 * but it doesn't know anything about the special restrictions that apply to
17 * packet writing. One restriction is that write requests must be aligned to
18 * packet boundaries on the physical media, and the size of a write request
19 * must be equal to the packet size. Another restriction is that a
20 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
21 * command, if the previous command was a write.
23 * The purpose of the packet writing driver is to hide these restrictions from
24 * higher layers, such as file systems, and present a block device that can be
25 * randomly read and written using 2kB-sized blocks.
27 * The lowest layer in the packet writing driver is the packet I/O scheduler.
28 * Its data is defined by the struct packet_iosched and includes two bio
29 * queues with pending read and write requests. These queues are processed
30 * by the pkt_iosched_process_queue() function. The write requests in this
31 * queue are already properly aligned and sized. This layer is responsible for
32 * issuing the flush cache commands and scheduling the I/O in a good order.
34 * The next layer transforms unaligned write requests to aligned writes. This
35 * transformation requires reading missing pieces of data from the underlying
36 * block device, assembling the pieces to full packets and queuing them to the
37 * packet I/O scheduler.
39 * At the top layer there is a custom make_request_fn function that forwards
40 * read requests directly to the iosched queue and puts write requests in the
41 * unaligned write queue. A kernel thread performs the necessary read
42 * gathering to convert the unaligned writes to aligned writes and then feeds
43 * them to the packet I/O scheduler.
45 *************************************************************************/
47 #include <linux/pktcdvd.h>
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/kernel.h>
51 #include <linux/kthread.h>
52 #include <linux/errno.h>
53 #include <linux/spinlock.h>
54 #include <linux/file.h>
55 #include <linux/proc_fs.h>
56 #include <linux/seq_file.h>
57 #include <linux/miscdevice.h>
58 #include <linux/freezer.h>
59 #include <linux/mutex.h>
60 #include <scsi/scsi_cmnd.h>
61 #include <scsi/scsi_ioctl.h>
62 #include <scsi/scsi.h>
63 #include <linux/debugfs.h>
64 #include <linux/device.h>
66 #include <asm/uaccess.h>
68 #define DRIVER_NAME "pktcdvd"
71 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
73 #define DPRINTK(fmt, args...)
77 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
79 #define VPRINTK(fmt, args...)
82 #define MAX_SPEED 0xffff
84 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
86 static struct pktcdvd_device
*pkt_devs
[MAX_WRITERS
];
87 static struct proc_dir_entry
*pkt_proc
;
88 static int pktdev_major
;
89 static int write_congestion_on
= PKT_WRITE_CONGESTION_ON
;
90 static int write_congestion_off
= PKT_WRITE_CONGESTION_OFF
;
91 static struct mutex ctl_mutex
; /* Serialize open/close/setup/teardown */
92 static mempool_t
*psd_pool
;
94 static struct class *class_pktcdvd
= NULL
; /* /sys/class/pktcdvd */
95 static struct dentry
*pkt_debugfs_root
= NULL
; /* /debug/pktcdvd */
97 /* forward declaration */
98 static int pkt_setup_dev(dev_t dev
, dev_t
* pkt_dev
);
99 static int pkt_remove_dev(dev_t pkt_dev
);
100 static int pkt_seq_show(struct seq_file
*m
, void *p
);
105 * create and register a pktcdvd kernel object.
107 static struct pktcdvd_kobj
* pkt_kobj_create(struct pktcdvd_device
*pd
,
109 struct kobject
* parent
,
110 struct kobj_type
* ktype
)
112 struct pktcdvd_kobj
*p
;
113 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
116 kobject_set_name(&p
->kobj
, "%s", name
);
117 p
->kobj
.parent
= parent
;
118 p
->kobj
.ktype
= ktype
;
120 if (kobject_register(&p
->kobj
) != 0)
125 * remove a pktcdvd kernel object.
127 static void pkt_kobj_remove(struct pktcdvd_kobj
*p
)
130 kobject_unregister(&p
->kobj
);
133 * default release function for pktcdvd kernel objects.
135 static void pkt_kobj_release(struct kobject
*kobj
)
137 kfree(to_pktcdvdkobj(kobj
));
141 /**********************************************************
143 * sysfs interface for pktcdvd
144 * by (C) 2006 Thomas Maier <balagi@justmail.de>
146 **********************************************************/
148 #define DEF_ATTR(_obj,_name,_mode) \
149 static struct attribute _obj = { \
150 .name = _name, .owner = THIS_MODULE, .mode = _mode }
152 /**********************************************************
153 /sys/class/pktcdvd/pktcdvd[0-7]/
156 stat/packets_finished
161 write_queue/congestion_off
162 write_queue/congestion_on
163 **********************************************************/
165 DEF_ATTR(kobj_pkt_attr_st1
, "reset", 0200);
166 DEF_ATTR(kobj_pkt_attr_st2
, "packets_started", 0444);
167 DEF_ATTR(kobj_pkt_attr_st3
, "packets_finished", 0444);
168 DEF_ATTR(kobj_pkt_attr_st4
, "kb_written", 0444);
169 DEF_ATTR(kobj_pkt_attr_st5
, "kb_read", 0444);
170 DEF_ATTR(kobj_pkt_attr_st6
, "kb_read_gather", 0444);
172 static struct attribute
*kobj_pkt_attrs_stat
[] = {
182 DEF_ATTR(kobj_pkt_attr_wq1
, "size", 0444);
183 DEF_ATTR(kobj_pkt_attr_wq2
, "congestion_off", 0644);
184 DEF_ATTR(kobj_pkt_attr_wq3
, "congestion_on", 0644);
186 static struct attribute
*kobj_pkt_attrs_wqueue
[] = {
193 /* declares a char buffer[64] _dbuf, copies data from
194 * _b with length _l into it and ensures that _dbuf ends
195 * with a \0 character.
197 #define DECLARE_BUF_AS_STRING(_dbuf, _b, _l) \
198 char _dbuf[64]; int dlen = (_l) < 0 ? 0 : (_l); \
199 if (dlen >= sizeof(_dbuf)) dlen = sizeof(_dbuf)-1; \
200 memcpy(_dbuf, _b, dlen); _dbuf[dlen] = 0
202 static ssize_t
kobj_pkt_show(struct kobject
*kobj
,
203 struct attribute
*attr
, char *data
)
205 struct pktcdvd_device
*pd
= to_pktcdvdkobj(kobj
)->pd
;
208 if (strcmp(attr
->name
, "packets_started") == 0) {
209 n
= sprintf(data
, "%lu\n", pd
->stats
.pkt_started
);
211 } else if (strcmp(attr
->name
, "packets_finished") == 0) {
212 n
= sprintf(data
, "%lu\n", pd
->stats
.pkt_ended
);
214 } else if (strcmp(attr
->name
, "kb_written") == 0) {
215 n
= sprintf(data
, "%lu\n", pd
->stats
.secs_w
>> 1);
217 } else if (strcmp(attr
->name
, "kb_read") == 0) {
218 n
= sprintf(data
, "%lu\n", pd
->stats
.secs_r
>> 1);
220 } else if (strcmp(attr
->name
, "kb_read_gather") == 0) {
221 n
= sprintf(data
, "%lu\n", pd
->stats
.secs_rg
>> 1);
223 } else if (strcmp(attr
->name
, "size") == 0) {
224 spin_lock(&pd
->lock
);
225 v
= pd
->bio_queue_size
;
226 spin_unlock(&pd
->lock
);
227 n
= sprintf(data
, "%d\n", v
);
229 } else if (strcmp(attr
->name
, "congestion_off") == 0) {
230 spin_lock(&pd
->lock
);
231 v
= pd
->write_congestion_off
;
232 spin_unlock(&pd
->lock
);
233 n
= sprintf(data
, "%d\n", v
);
235 } else if (strcmp(attr
->name
, "congestion_on") == 0) {
236 spin_lock(&pd
->lock
);
237 v
= pd
->write_congestion_on
;
238 spin_unlock(&pd
->lock
);
239 n
= sprintf(data
, "%d\n", v
);
244 static void init_write_congestion_marks(int* lo
, int* hi
)
248 *hi
= min(*hi
, 1000000);
252 *lo
= min(*lo
, *hi
- 100);
261 static ssize_t
kobj_pkt_store(struct kobject
*kobj
,
262 struct attribute
*attr
,
263 const char *data
, size_t len
)
265 struct pktcdvd_device
*pd
= to_pktcdvdkobj(kobj
)->pd
;
267 DECLARE_BUF_AS_STRING(dbuf
, data
, len
); /* ensure sscanf scans a string */
269 if (strcmp(attr
->name
, "reset") == 0 && dlen
> 0) {
270 pd
->stats
.pkt_started
= 0;
271 pd
->stats
.pkt_ended
= 0;
272 pd
->stats
.secs_w
= 0;
273 pd
->stats
.secs_rg
= 0;
274 pd
->stats
.secs_r
= 0;
276 } else if (strcmp(attr
->name
, "congestion_off") == 0
277 && sscanf(dbuf
, "%d", &val
) == 1) {
278 spin_lock(&pd
->lock
);
279 pd
->write_congestion_off
= val
;
280 init_write_congestion_marks(&pd
->write_congestion_off
,
281 &pd
->write_congestion_on
);
282 spin_unlock(&pd
->lock
);
284 } else if (strcmp(attr
->name
, "congestion_on") == 0
285 && sscanf(dbuf
, "%d", &val
) == 1) {
286 spin_lock(&pd
->lock
);
287 pd
->write_congestion_on
= val
;
288 init_write_congestion_marks(&pd
->write_congestion_off
,
289 &pd
->write_congestion_on
);
290 spin_unlock(&pd
->lock
);
295 static struct sysfs_ops kobj_pkt_ops
= {
296 .show
= kobj_pkt_show
,
297 .store
= kobj_pkt_store
299 static struct kobj_type kobj_pkt_type_stat
= {
300 .release
= pkt_kobj_release
,
301 .sysfs_ops
= &kobj_pkt_ops
,
302 .default_attrs
= kobj_pkt_attrs_stat
304 static struct kobj_type kobj_pkt_type_wqueue
= {
305 .release
= pkt_kobj_release
,
306 .sysfs_ops
= &kobj_pkt_ops
,
307 .default_attrs
= kobj_pkt_attrs_wqueue
310 static void pkt_sysfs_dev_new(struct pktcdvd_device
*pd
)
313 pd
->clsdev
= class_device_create(class_pktcdvd
,
315 NULL
, "%s", pd
->name
);
316 if (IS_ERR(pd
->clsdev
))
320 pd
->kobj_stat
= pkt_kobj_create(pd
, "stat",
322 &kobj_pkt_type_stat
);
323 pd
->kobj_wqueue
= pkt_kobj_create(pd
, "write_queue",
325 &kobj_pkt_type_wqueue
);
329 static void pkt_sysfs_dev_remove(struct pktcdvd_device
*pd
)
331 pkt_kobj_remove(pd
->kobj_stat
);
332 pkt_kobj_remove(pd
->kobj_wqueue
);
334 class_device_destroy(class_pktcdvd
, pd
->pkt_dev
);
338 /********************************************************************
341 remove unmap packet dev
342 device_map show mappings
343 *******************************************************************/
345 static void class_pktcdvd_release(struct class *cls
)
349 static ssize_t
class_pktcdvd_show_map(struct class *c
, char *data
)
353 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
354 for (idx
= 0; idx
< MAX_WRITERS
; idx
++) {
355 struct pktcdvd_device
*pd
= pkt_devs
[idx
];
358 n
+= sprintf(data
+n
, "%s %u:%u %u:%u\n",
360 MAJOR(pd
->pkt_dev
), MINOR(pd
->pkt_dev
),
361 MAJOR(pd
->bdev
->bd_dev
),
362 MINOR(pd
->bdev
->bd_dev
));
364 mutex_unlock(&ctl_mutex
);
368 static ssize_t
class_pktcdvd_store_add(struct class *c
, const char *buf
,
371 unsigned int major
, minor
;
372 DECLARE_BUF_AS_STRING(dbuf
, buf
, count
);
373 if (sscanf(dbuf
, "%u:%u", &major
, &minor
) == 2) {
374 pkt_setup_dev(MKDEV(major
, minor
), NULL
);
380 static ssize_t
class_pktcdvd_store_remove(struct class *c
, const char *buf
,
383 unsigned int major
, minor
;
384 DECLARE_BUF_AS_STRING(dbuf
, buf
, count
);
385 if (sscanf(dbuf
, "%u:%u", &major
, &minor
) == 2) {
386 pkt_remove_dev(MKDEV(major
, minor
));
392 static struct class_attribute class_pktcdvd_attrs
[] = {
393 __ATTR(add
, 0200, NULL
, class_pktcdvd_store_add
),
394 __ATTR(remove
, 0200, NULL
, class_pktcdvd_store_remove
),
395 __ATTR(device_map
, 0444, class_pktcdvd_show_map
, NULL
),
400 static int pkt_sysfs_init(void)
405 * create control files in sysfs
406 * /sys/class/pktcdvd/...
408 class_pktcdvd
= kzalloc(sizeof(*class_pktcdvd
), GFP_KERNEL
);
411 class_pktcdvd
->name
= DRIVER_NAME
;
412 class_pktcdvd
->owner
= THIS_MODULE
;
413 class_pktcdvd
->class_release
= class_pktcdvd_release
;
414 class_pktcdvd
->class_attrs
= class_pktcdvd_attrs
;
415 ret
= class_register(class_pktcdvd
);
417 kfree(class_pktcdvd
);
418 class_pktcdvd
= NULL
;
419 printk(DRIVER_NAME
": failed to create class pktcdvd\n");
425 static void pkt_sysfs_cleanup(void)
428 class_destroy(class_pktcdvd
);
429 class_pktcdvd
= NULL
;
432 /********************************************************************
435 /debugfs/pktcdvd[0-7]/
438 *******************************************************************/
440 static int pkt_debugfs_seq_show(struct seq_file
*m
, void *p
)
442 return pkt_seq_show(m
, p
);
445 static int pkt_debugfs_fops_open(struct inode
*inode
, struct file
*file
)
447 return single_open(file
, pkt_debugfs_seq_show
, inode
->i_private
);
450 static struct file_operations debug_fops
= {
451 .open
= pkt_debugfs_fops_open
,
454 .release
= single_release
,
455 .owner
= THIS_MODULE
,
458 static void pkt_debugfs_dev_new(struct pktcdvd_device
*pd
)
460 if (!pkt_debugfs_root
)
462 pd
->dfs_f_info
= NULL
;
463 pd
->dfs_d_root
= debugfs_create_dir(pd
->name
, pkt_debugfs_root
);
464 if (IS_ERR(pd
->dfs_d_root
)) {
465 pd
->dfs_d_root
= NULL
;
468 pd
->dfs_f_info
= debugfs_create_file("info", S_IRUGO
,
469 pd
->dfs_d_root
, pd
, &debug_fops
);
470 if (IS_ERR(pd
->dfs_f_info
)) {
471 pd
->dfs_f_info
= NULL
;
476 static void pkt_debugfs_dev_remove(struct pktcdvd_device
*pd
)
478 if (!pkt_debugfs_root
)
481 debugfs_remove(pd
->dfs_f_info
);
482 pd
->dfs_f_info
= NULL
;
484 debugfs_remove(pd
->dfs_d_root
);
485 pd
->dfs_d_root
= NULL
;
488 static void pkt_debugfs_init(void)
490 pkt_debugfs_root
= debugfs_create_dir(DRIVER_NAME
, NULL
);
491 if (IS_ERR(pkt_debugfs_root
)) {
492 pkt_debugfs_root
= NULL
;
497 static void pkt_debugfs_cleanup(void)
499 if (!pkt_debugfs_root
)
501 debugfs_remove(pkt_debugfs_root
);
502 pkt_debugfs_root
= NULL
;
505 /* ----------------------------------------------------------*/
508 static void pkt_bio_finished(struct pktcdvd_device
*pd
)
510 BUG_ON(atomic_read(&pd
->cdrw
.pending_bios
) <= 0);
511 if (atomic_dec_and_test(&pd
->cdrw
.pending_bios
)) {
512 VPRINTK(DRIVER_NAME
": queue empty\n");
513 atomic_set(&pd
->iosched
.attention
, 1);
514 wake_up(&pd
->wqueue
);
518 static void pkt_bio_destructor(struct bio
*bio
)
520 kfree(bio
->bi_io_vec
);
524 static struct bio
*pkt_bio_alloc(int nr_iovecs
)
526 struct bio_vec
*bvl
= NULL
;
529 bio
= kmalloc(sizeof(struct bio
), GFP_KERNEL
);
534 bvl
= kcalloc(nr_iovecs
, sizeof(struct bio_vec
), GFP_KERNEL
);
538 bio
->bi_max_vecs
= nr_iovecs
;
539 bio
->bi_io_vec
= bvl
;
540 bio
->bi_destructor
= pkt_bio_destructor
;
551 * Allocate a packet_data struct
553 static struct packet_data
*pkt_alloc_packet_data(int frames
)
556 struct packet_data
*pkt
;
558 pkt
= kzalloc(sizeof(struct packet_data
), GFP_KERNEL
);
562 pkt
->frames
= frames
;
563 pkt
->w_bio
= pkt_bio_alloc(frames
);
567 for (i
= 0; i
< frames
/ FRAMES_PER_PAGE
; i
++) {
568 pkt
->pages
[i
] = alloc_page(GFP_KERNEL
|__GFP_ZERO
);
573 spin_lock_init(&pkt
->lock
);
575 for (i
= 0; i
< frames
; i
++) {
576 struct bio
*bio
= pkt_bio_alloc(1);
579 pkt
->r_bios
[i
] = bio
;
585 for (i
= 0; i
< frames
; i
++) {
586 struct bio
*bio
= pkt
->r_bios
[i
];
592 for (i
= 0; i
< frames
/ FRAMES_PER_PAGE
; i
++)
594 __free_page(pkt
->pages
[i
]);
603 * Free a packet_data struct
605 static void pkt_free_packet_data(struct packet_data
*pkt
)
609 for (i
= 0; i
< pkt
->frames
; i
++) {
610 struct bio
*bio
= pkt
->r_bios
[i
];
614 for (i
= 0; i
< pkt
->frames
/ FRAMES_PER_PAGE
; i
++)
615 __free_page(pkt
->pages
[i
]);
620 static void pkt_shrink_pktlist(struct pktcdvd_device
*pd
)
622 struct packet_data
*pkt
, *next
;
624 BUG_ON(!list_empty(&pd
->cdrw
.pkt_active_list
));
626 list_for_each_entry_safe(pkt
, next
, &pd
->cdrw
.pkt_free_list
, list
) {
627 pkt_free_packet_data(pkt
);
629 INIT_LIST_HEAD(&pd
->cdrw
.pkt_free_list
);
632 static int pkt_grow_pktlist(struct pktcdvd_device
*pd
, int nr_packets
)
634 struct packet_data
*pkt
;
636 BUG_ON(!list_empty(&pd
->cdrw
.pkt_free_list
));
638 while (nr_packets
> 0) {
639 pkt
= pkt_alloc_packet_data(pd
->settings
.size
>> 2);
641 pkt_shrink_pktlist(pd
);
644 pkt
->id
= nr_packets
;
646 list_add(&pkt
->list
, &pd
->cdrw
.pkt_free_list
);
652 static inline struct pkt_rb_node
*pkt_rbtree_next(struct pkt_rb_node
*node
)
654 struct rb_node
*n
= rb_next(&node
->rb_node
);
657 return rb_entry(n
, struct pkt_rb_node
, rb_node
);
660 static void pkt_rbtree_erase(struct pktcdvd_device
*pd
, struct pkt_rb_node
*node
)
662 rb_erase(&node
->rb_node
, &pd
->bio_queue
);
663 mempool_free(node
, pd
->rb_pool
);
664 pd
->bio_queue_size
--;
665 BUG_ON(pd
->bio_queue_size
< 0);
669 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
671 static struct pkt_rb_node
*pkt_rbtree_find(struct pktcdvd_device
*pd
, sector_t s
)
673 struct rb_node
*n
= pd
->bio_queue
.rb_node
;
674 struct rb_node
*next
;
675 struct pkt_rb_node
*tmp
;
678 BUG_ON(pd
->bio_queue_size
> 0);
683 tmp
= rb_entry(n
, struct pkt_rb_node
, rb_node
);
684 if (s
<= tmp
->bio
->bi_sector
)
693 if (s
> tmp
->bio
->bi_sector
) {
694 tmp
= pkt_rbtree_next(tmp
);
698 BUG_ON(s
> tmp
->bio
->bi_sector
);
703 * Insert a node into the pd->bio_queue rb tree.
705 static void pkt_rbtree_insert(struct pktcdvd_device
*pd
, struct pkt_rb_node
*node
)
707 struct rb_node
**p
= &pd
->bio_queue
.rb_node
;
708 struct rb_node
*parent
= NULL
;
709 sector_t s
= node
->bio
->bi_sector
;
710 struct pkt_rb_node
*tmp
;
714 tmp
= rb_entry(parent
, struct pkt_rb_node
, rb_node
);
715 if (s
< tmp
->bio
->bi_sector
)
720 rb_link_node(&node
->rb_node
, parent
, p
);
721 rb_insert_color(&node
->rb_node
, &pd
->bio_queue
);
722 pd
->bio_queue_size
++;
726 * Add a bio to a single linked list defined by its head and tail pointers.
728 static void pkt_add_list_last(struct bio
*bio
, struct bio
**list_head
, struct bio
**list_tail
)
732 BUG_ON((*list_head
) == NULL
);
733 (*list_tail
)->bi_next
= bio
;
736 BUG_ON((*list_head
) != NULL
);
743 * Remove and return the first bio from a single linked list defined by its
744 * head and tail pointers.
746 static inline struct bio
*pkt_get_list_first(struct bio
**list_head
, struct bio
**list_tail
)
750 if (*list_head
== NULL
)
754 *list_head
= bio
->bi_next
;
755 if (*list_head
== NULL
)
763 * Send a packet_command to the underlying block device and
764 * wait for completion.
766 static int pkt_generic_packet(struct pktcdvd_device
*pd
, struct packet_command
*cgc
)
768 char sense
[SCSI_SENSE_BUFFERSIZE
];
771 DECLARE_COMPLETION_ONSTACK(wait
);
774 q
= bdev_get_queue(pd
->bdev
);
776 rq
= blk_get_request(q
, (cgc
->data_direction
== CGC_DATA_WRITE
) ? WRITE
: READ
,
779 rq
->rq_disk
= pd
->bdev
->bd_disk
;
783 rq
->data
= cgc
->buffer
;
784 rq
->data_len
= cgc
->buflen
;
786 memset(sense
, 0, sizeof(sense
));
788 rq
->cmd_type
= REQ_TYPE_BLOCK_PC
;
789 rq
->cmd_flags
|= REQ_HARDBARRIER
;
791 rq
->cmd_flags
|= REQ_QUIET
;
792 memcpy(rq
->cmd
, cgc
->cmd
, CDROM_PACKET_SIZE
);
793 if (sizeof(rq
->cmd
) > CDROM_PACKET_SIZE
)
794 memset(rq
->cmd
+ CDROM_PACKET_SIZE
, 0, sizeof(rq
->cmd
) - CDROM_PACKET_SIZE
);
795 rq
->cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
798 rq
->end_io_data
= &wait
;
799 rq
->end_io
= blk_end_sync_rq
;
800 elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 1);
801 generic_unplug_device(q
);
802 wait_for_completion(&wait
);
812 * A generic sense dump / resolve mechanism should be implemented across
813 * all ATAPI + SCSI devices.
815 static void pkt_dump_sense(struct packet_command
*cgc
)
817 static char *info
[9] = { "No sense", "Recovered error", "Not ready",
818 "Medium error", "Hardware error", "Illegal request",
819 "Unit attention", "Data protect", "Blank check" };
821 struct request_sense
*sense
= cgc
->sense
;
823 printk(DRIVER_NAME
":");
824 for (i
= 0; i
< CDROM_PACKET_SIZE
; i
++)
825 printk(" %02x", cgc
->cmd
[i
]);
829 printk("no sense\n");
833 printk("sense %02x.%02x.%02x", sense
->sense_key
, sense
->asc
, sense
->ascq
);
835 if (sense
->sense_key
> 8) {
836 printk(" (INVALID)\n");
840 printk(" (%s)\n", info
[sense
->sense_key
]);
844 * flush the drive cache to media
846 static int pkt_flush_cache(struct pktcdvd_device
*pd
)
848 struct packet_command cgc
;
850 init_cdrom_command(&cgc
, NULL
, 0, CGC_DATA_NONE
);
851 cgc
.cmd
[0] = GPCMD_FLUSH_CACHE
;
855 * the IMMED bit -- we default to not setting it, although that
856 * would allow a much faster close, this is safer
861 return pkt_generic_packet(pd
, &cgc
);
865 * speed is given as the normal factor, e.g. 4 for 4x
867 static int pkt_set_speed(struct pktcdvd_device
*pd
, unsigned write_speed
, unsigned read_speed
)
869 struct packet_command cgc
;
870 struct request_sense sense
;
873 init_cdrom_command(&cgc
, NULL
, 0, CGC_DATA_NONE
);
875 cgc
.cmd
[0] = GPCMD_SET_SPEED
;
876 cgc
.cmd
[2] = (read_speed
>> 8) & 0xff;
877 cgc
.cmd
[3] = read_speed
& 0xff;
878 cgc
.cmd
[4] = (write_speed
>> 8) & 0xff;
879 cgc
.cmd
[5] = write_speed
& 0xff;
881 if ((ret
= pkt_generic_packet(pd
, &cgc
)))
882 pkt_dump_sense(&cgc
);
888 * Queue a bio for processing by the low-level CD device. Must be called
889 * from process context.
891 static void pkt_queue_bio(struct pktcdvd_device
*pd
, struct bio
*bio
)
893 spin_lock(&pd
->iosched
.lock
);
894 if (bio_data_dir(bio
) == READ
) {
895 pkt_add_list_last(bio
, &pd
->iosched
.read_queue
,
896 &pd
->iosched
.read_queue_tail
);
898 pkt_add_list_last(bio
, &pd
->iosched
.write_queue
,
899 &pd
->iosched
.write_queue_tail
);
901 spin_unlock(&pd
->iosched
.lock
);
903 atomic_set(&pd
->iosched
.attention
, 1);
904 wake_up(&pd
->wqueue
);
908 * Process the queued read/write requests. This function handles special
909 * requirements for CDRW drives:
910 * - A cache flush command must be inserted before a read request if the
911 * previous request was a write.
912 * - Switching between reading and writing is slow, so don't do it more often
914 * - Optimize for throughput at the expense of latency. This means that streaming
915 * writes will never be interrupted by a read, but if the drive has to seek
916 * before the next write, switch to reading instead if there are any pending
918 * - Set the read speed according to current usage pattern. When only reading
919 * from the device, it's best to use the highest possible read speed, but
920 * when switching often between reading and writing, it's better to have the
921 * same read and write speeds.
923 static void pkt_iosched_process_queue(struct pktcdvd_device
*pd
)
926 if (atomic_read(&pd
->iosched
.attention
) == 0)
928 atomic_set(&pd
->iosched
.attention
, 0);
932 int reads_queued
, writes_queued
;
934 spin_lock(&pd
->iosched
.lock
);
935 reads_queued
= (pd
->iosched
.read_queue
!= NULL
);
936 writes_queued
= (pd
->iosched
.write_queue
!= NULL
);
937 spin_unlock(&pd
->iosched
.lock
);
939 if (!reads_queued
&& !writes_queued
)
942 if (pd
->iosched
.writing
) {
943 int need_write_seek
= 1;
944 spin_lock(&pd
->iosched
.lock
);
945 bio
= pd
->iosched
.write_queue
;
946 spin_unlock(&pd
->iosched
.lock
);
947 if (bio
&& (bio
->bi_sector
== pd
->iosched
.last_write
))
949 if (need_write_seek
&& reads_queued
) {
950 if (atomic_read(&pd
->cdrw
.pending_bios
) > 0) {
951 VPRINTK(DRIVER_NAME
": write, waiting\n");
955 pd
->iosched
.writing
= 0;
958 if (!reads_queued
&& writes_queued
) {
959 if (atomic_read(&pd
->cdrw
.pending_bios
) > 0) {
960 VPRINTK(DRIVER_NAME
": read, waiting\n");
963 pd
->iosched
.writing
= 1;
967 spin_lock(&pd
->iosched
.lock
);
968 if (pd
->iosched
.writing
) {
969 bio
= pkt_get_list_first(&pd
->iosched
.write_queue
,
970 &pd
->iosched
.write_queue_tail
);
972 bio
= pkt_get_list_first(&pd
->iosched
.read_queue
,
973 &pd
->iosched
.read_queue_tail
);
975 spin_unlock(&pd
->iosched
.lock
);
980 if (bio_data_dir(bio
) == READ
)
981 pd
->iosched
.successive_reads
+= bio
->bi_size
>> 10;
983 pd
->iosched
.successive_reads
= 0;
984 pd
->iosched
.last_write
= bio
->bi_sector
+ bio_sectors(bio
);
986 if (pd
->iosched
.successive_reads
>= HI_SPEED_SWITCH
) {
987 if (pd
->read_speed
== pd
->write_speed
) {
988 pd
->read_speed
= MAX_SPEED
;
989 pkt_set_speed(pd
, pd
->write_speed
, pd
->read_speed
);
992 if (pd
->read_speed
!= pd
->write_speed
) {
993 pd
->read_speed
= pd
->write_speed
;
994 pkt_set_speed(pd
, pd
->write_speed
, pd
->read_speed
);
998 atomic_inc(&pd
->cdrw
.pending_bios
);
999 generic_make_request(bio
);
1004 * Special care is needed if the underlying block device has a small
1005 * max_phys_segments value.
1007 static int pkt_set_segment_merging(struct pktcdvd_device
*pd
, request_queue_t
*q
)
1009 if ((pd
->settings
.size
<< 9) / CD_FRAMESIZE
<= q
->max_phys_segments
) {
1011 * The cdrom device can handle one segment/frame
1013 clear_bit(PACKET_MERGE_SEGS
, &pd
->flags
);
1015 } else if ((pd
->settings
.size
<< 9) / PAGE_SIZE
<= q
->max_phys_segments
) {
1017 * We can handle this case at the expense of some extra memory
1018 * copies during write operations
1020 set_bit(PACKET_MERGE_SEGS
, &pd
->flags
);
1023 printk(DRIVER_NAME
": cdrom max_phys_segments too small\n");
1029 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1031 static void pkt_copy_bio_data(struct bio
*src_bio
, int seg
, int offs
, struct page
*dst_page
, int dst_offs
)
1033 unsigned int copy_size
= CD_FRAMESIZE
;
1035 while (copy_size
> 0) {
1036 struct bio_vec
*src_bvl
= bio_iovec_idx(src_bio
, seg
);
1037 void *vfrom
= kmap_atomic(src_bvl
->bv_page
, KM_USER0
) +
1038 src_bvl
->bv_offset
+ offs
;
1039 void *vto
= page_address(dst_page
) + dst_offs
;
1040 int len
= min_t(int, copy_size
, src_bvl
->bv_len
- offs
);
1043 memcpy(vto
, vfrom
, len
);
1044 kunmap_atomic(vfrom
, KM_USER0
);
1054 * Copy all data for this packet to pkt->pages[], so that
1055 * a) The number of required segments for the write bio is minimized, which
1056 * is necessary for some scsi controllers.
1057 * b) The data can be used as cache to avoid read requests if we receive a
1058 * new write request for the same zone.
1060 static void pkt_make_local_copy(struct packet_data
*pkt
, struct bio_vec
*bvec
)
1064 /* Copy all data to pkt->pages[] */
1067 for (f
= 0; f
< pkt
->frames
; f
++) {
1068 if (bvec
[f
].bv_page
!= pkt
->pages
[p
]) {
1069 void *vfrom
= kmap_atomic(bvec
[f
].bv_page
, KM_USER0
) + bvec
[f
].bv_offset
;
1070 void *vto
= page_address(pkt
->pages
[p
]) + offs
;
1071 memcpy(vto
, vfrom
, CD_FRAMESIZE
);
1072 kunmap_atomic(vfrom
, KM_USER0
);
1073 bvec
[f
].bv_page
= pkt
->pages
[p
];
1074 bvec
[f
].bv_offset
= offs
;
1076 BUG_ON(bvec
[f
].bv_offset
!= offs
);
1078 offs
+= CD_FRAMESIZE
;
1079 if (offs
>= PAGE_SIZE
) {
1086 static int pkt_end_io_read(struct bio
*bio
, unsigned int bytes_done
, int err
)
1088 struct packet_data
*pkt
= bio
->bi_private
;
1089 struct pktcdvd_device
*pd
= pkt
->pd
;
1095 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio
,
1096 (unsigned long long)pkt
->sector
, (unsigned long long)bio
->bi_sector
, err
);
1099 atomic_inc(&pkt
->io_errors
);
1100 if (atomic_dec_and_test(&pkt
->io_wait
)) {
1101 atomic_inc(&pkt
->run_sm
);
1102 wake_up(&pd
->wqueue
);
1104 pkt_bio_finished(pd
);
1109 static int pkt_end_io_packet_write(struct bio
*bio
, unsigned int bytes_done
, int err
)
1111 struct packet_data
*pkt
= bio
->bi_private
;
1112 struct pktcdvd_device
*pd
= pkt
->pd
;
1118 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt
->id
, err
);
1120 pd
->stats
.pkt_ended
++;
1122 pkt_bio_finished(pd
);
1123 atomic_dec(&pkt
->io_wait
);
1124 atomic_inc(&pkt
->run_sm
);
1125 wake_up(&pd
->wqueue
);
1130 * Schedule reads for the holes in a packet
1132 static void pkt_gather_data(struct pktcdvd_device
*pd
, struct packet_data
*pkt
)
1134 int frames_read
= 0;
1137 char written
[PACKET_MAX_SIZE
];
1139 BUG_ON(!pkt
->orig_bios
);
1141 atomic_set(&pkt
->io_wait
, 0);
1142 atomic_set(&pkt
->io_errors
, 0);
1145 * Figure out which frames we need to read before we can write.
1147 memset(written
, 0, sizeof(written
));
1148 spin_lock(&pkt
->lock
);
1149 for (bio
= pkt
->orig_bios
; bio
; bio
= bio
->bi_next
) {
1150 int first_frame
= (bio
->bi_sector
- pkt
->sector
) / (CD_FRAMESIZE
>> 9);
1151 int num_frames
= bio
->bi_size
/ CD_FRAMESIZE
;
1152 pd
->stats
.secs_w
+= num_frames
* (CD_FRAMESIZE
>> 9);
1153 BUG_ON(first_frame
< 0);
1154 BUG_ON(first_frame
+ num_frames
> pkt
->frames
);
1155 for (f
= first_frame
; f
< first_frame
+ num_frames
; f
++)
1158 spin_unlock(&pkt
->lock
);
1160 if (pkt
->cache_valid
) {
1161 VPRINTK("pkt_gather_data: zone %llx cached\n",
1162 (unsigned long long)pkt
->sector
);
1167 * Schedule reads for missing parts of the packet.
1169 for (f
= 0; f
< pkt
->frames
; f
++) {
1173 bio
= pkt
->r_bios
[f
];
1175 bio
->bi_max_vecs
= 1;
1176 bio
->bi_sector
= pkt
->sector
+ f
* (CD_FRAMESIZE
>> 9);
1177 bio
->bi_bdev
= pd
->bdev
;
1178 bio
->bi_end_io
= pkt_end_io_read
;
1179 bio
->bi_private
= pkt
;
1181 p
= (f
* CD_FRAMESIZE
) / PAGE_SIZE
;
1182 offset
= (f
* CD_FRAMESIZE
) % PAGE_SIZE
;
1183 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1184 f
, pkt
->pages
[p
], offset
);
1185 if (!bio_add_page(bio
, pkt
->pages
[p
], CD_FRAMESIZE
, offset
))
1188 atomic_inc(&pkt
->io_wait
);
1190 pkt_queue_bio(pd
, bio
);
1195 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1196 frames_read
, (unsigned long long)pkt
->sector
);
1197 pd
->stats
.pkt_started
++;
1198 pd
->stats
.secs_rg
+= frames_read
* (CD_FRAMESIZE
>> 9);
1202 * Find a packet matching zone, or the least recently used packet if
1203 * there is no match.
1205 static struct packet_data
*pkt_get_packet_data(struct pktcdvd_device
*pd
, int zone
)
1207 struct packet_data
*pkt
;
1209 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_free_list
, list
) {
1210 if (pkt
->sector
== zone
|| pkt
->list
.next
== &pd
->cdrw
.pkt_free_list
) {
1211 list_del_init(&pkt
->list
);
1212 if (pkt
->sector
!= zone
)
1213 pkt
->cache_valid
= 0;
1221 static void pkt_put_packet_data(struct pktcdvd_device
*pd
, struct packet_data
*pkt
)
1223 if (pkt
->cache_valid
) {
1224 list_add(&pkt
->list
, &pd
->cdrw
.pkt_free_list
);
1226 list_add_tail(&pkt
->list
, &pd
->cdrw
.pkt_free_list
);
1231 * recover a failed write, query for relocation if possible
1233 * returns 1 if recovery is possible, or 0 if not
1236 static int pkt_start_recovery(struct packet_data
*pkt
)
1239 * FIXME. We need help from the file system to implement
1240 * recovery handling.
1244 struct request
*rq
= pkt
->rq
;
1245 struct pktcdvd_device
*pd
= rq
->rq_disk
->private_data
;
1246 struct block_device
*pkt_bdev
;
1247 struct super_block
*sb
= NULL
;
1248 unsigned long old_block
, new_block
;
1249 sector_t new_sector
;
1251 pkt_bdev
= bdget(kdev_t_to_nr(pd
->pkt_dev
));
1253 sb
= get_super(pkt_bdev
);
1260 if (!sb
->s_op
|| !sb
->s_op
->relocate_blocks
)
1263 old_block
= pkt
->sector
/ (CD_FRAMESIZE
>> 9);
1264 if (sb
->s_op
->relocate_blocks(sb
, old_block
, &new_block
))
1267 new_sector
= new_block
* (CD_FRAMESIZE
>> 9);
1268 pkt
->sector
= new_sector
;
1270 pkt
->bio
->bi_sector
= new_sector
;
1271 pkt
->bio
->bi_next
= NULL
;
1272 pkt
->bio
->bi_flags
= 1 << BIO_UPTODATE
;
1273 pkt
->bio
->bi_idx
= 0;
1275 BUG_ON(pkt
->bio
->bi_rw
!= (1 << BIO_RW
));
1276 BUG_ON(pkt
->bio
->bi_vcnt
!= pkt
->frames
);
1277 BUG_ON(pkt
->bio
->bi_size
!= pkt
->frames
* CD_FRAMESIZE
);
1278 BUG_ON(pkt
->bio
->bi_end_io
!= pkt_end_io_packet_write
);
1279 BUG_ON(pkt
->bio
->bi_private
!= pkt
);
1290 static inline void pkt_set_state(struct packet_data
*pkt
, enum packet_data_state state
)
1292 #if PACKET_DEBUG > 1
1293 static const char *state_name
[] = {
1294 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1296 enum packet_data_state old_state
= pkt
->state
;
1297 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt
->id
, (unsigned long long)pkt
->sector
,
1298 state_name
[old_state
], state_name
[state
]);
1304 * Scan the work queue to see if we can start a new packet.
1305 * returns non-zero if any work was done.
1307 static int pkt_handle_queue(struct pktcdvd_device
*pd
)
1309 struct packet_data
*pkt
, *p
;
1310 struct bio
*bio
= NULL
;
1311 sector_t zone
= 0; /* Suppress gcc warning */
1312 struct pkt_rb_node
*node
, *first_node
;
1316 VPRINTK("handle_queue\n");
1318 atomic_set(&pd
->scan_queue
, 0);
1320 if (list_empty(&pd
->cdrw
.pkt_free_list
)) {
1321 VPRINTK("handle_queue: no pkt\n");
1326 * Try to find a zone we are not already working on.
1328 spin_lock(&pd
->lock
);
1329 first_node
= pkt_rbtree_find(pd
, pd
->current_sector
);
1331 n
= rb_first(&pd
->bio_queue
);
1333 first_node
= rb_entry(n
, struct pkt_rb_node
, rb_node
);
1338 zone
= ZONE(bio
->bi_sector
, pd
);
1339 list_for_each_entry(p
, &pd
->cdrw
.pkt_active_list
, list
) {
1340 if (p
->sector
== zone
) {
1347 node
= pkt_rbtree_next(node
);
1349 n
= rb_first(&pd
->bio_queue
);
1351 node
= rb_entry(n
, struct pkt_rb_node
, rb_node
);
1353 if (node
== first_node
)
1356 spin_unlock(&pd
->lock
);
1358 VPRINTK("handle_queue: no bio\n");
1362 pkt
= pkt_get_packet_data(pd
, zone
);
1364 pd
->current_sector
= zone
+ pd
->settings
.size
;
1366 BUG_ON(pkt
->frames
!= pd
->settings
.size
>> 2);
1367 pkt
->write_size
= 0;
1370 * Scan work queue for bios in the same zone and link them
1373 spin_lock(&pd
->lock
);
1374 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone
);
1375 while ((node
= pkt_rbtree_find(pd
, zone
)) != NULL
) {
1377 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1378 (unsigned long long)ZONE(bio
->bi_sector
, pd
));
1379 if (ZONE(bio
->bi_sector
, pd
) != zone
)
1381 pkt_rbtree_erase(pd
, node
);
1382 spin_lock(&pkt
->lock
);
1383 pkt_add_list_last(bio
, &pkt
->orig_bios
, &pkt
->orig_bios_tail
);
1384 pkt
->write_size
+= bio
->bi_size
/ CD_FRAMESIZE
;
1385 spin_unlock(&pkt
->lock
);
1387 /* check write congestion marks, and if bio_queue_size is
1388 below, wake up any waiters */
1389 wakeup
= (pd
->write_congestion_on
> 0
1390 && pd
->bio_queue_size
<= pd
->write_congestion_off
);
1391 spin_unlock(&pd
->lock
);
1393 blk_clear_queue_congested(pd
->disk
->queue
, WRITE
);
1395 pkt
->sleep_time
= max(PACKET_WAIT_TIME
, 1);
1396 pkt_set_state(pkt
, PACKET_WAITING_STATE
);
1397 atomic_set(&pkt
->run_sm
, 1);
1399 spin_lock(&pd
->cdrw
.active_list_lock
);
1400 list_add(&pkt
->list
, &pd
->cdrw
.pkt_active_list
);
1401 spin_unlock(&pd
->cdrw
.active_list_lock
);
1407 * Assemble a bio to write one packet and queue the bio for processing
1408 * by the underlying block device.
1410 static void pkt_start_write(struct pktcdvd_device
*pd
, struct packet_data
*pkt
)
1415 struct bio_vec
*bvec
= pkt
->w_bio
->bi_io_vec
;
1417 for (f
= 0; f
< pkt
->frames
; f
++) {
1418 bvec
[f
].bv_page
= pkt
->pages
[(f
* CD_FRAMESIZE
) / PAGE_SIZE
];
1419 bvec
[f
].bv_offset
= (f
* CD_FRAMESIZE
) % PAGE_SIZE
;
1423 * Fill-in bvec with data from orig_bios.
1426 spin_lock(&pkt
->lock
);
1427 for (bio
= pkt
->orig_bios
; bio
; bio
= bio
->bi_next
) {
1428 int segment
= bio
->bi_idx
;
1430 int first_frame
= (bio
->bi_sector
- pkt
->sector
) / (CD_FRAMESIZE
>> 9);
1431 int num_frames
= bio
->bi_size
/ CD_FRAMESIZE
;
1432 BUG_ON(first_frame
< 0);
1433 BUG_ON(first_frame
+ num_frames
> pkt
->frames
);
1434 for (f
= first_frame
; f
< first_frame
+ num_frames
; f
++) {
1435 struct bio_vec
*src_bvl
= bio_iovec_idx(bio
, segment
);
1437 while (src_offs
>= src_bvl
->bv_len
) {
1438 src_offs
-= src_bvl
->bv_len
;
1440 BUG_ON(segment
>= bio
->bi_vcnt
);
1441 src_bvl
= bio_iovec_idx(bio
, segment
);
1444 if (src_bvl
->bv_len
- src_offs
>= CD_FRAMESIZE
) {
1445 bvec
[f
].bv_page
= src_bvl
->bv_page
;
1446 bvec
[f
].bv_offset
= src_bvl
->bv_offset
+ src_offs
;
1448 pkt_copy_bio_data(bio
, segment
, src_offs
,
1449 bvec
[f
].bv_page
, bvec
[f
].bv_offset
);
1451 src_offs
+= CD_FRAMESIZE
;
1455 pkt_set_state(pkt
, PACKET_WRITE_WAIT_STATE
);
1456 spin_unlock(&pkt
->lock
);
1458 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1459 frames_write
, (unsigned long long)pkt
->sector
);
1460 BUG_ON(frames_write
!= pkt
->write_size
);
1462 if (test_bit(PACKET_MERGE_SEGS
, &pd
->flags
) || (pkt
->write_size
< pkt
->frames
)) {
1463 pkt_make_local_copy(pkt
, bvec
);
1464 pkt
->cache_valid
= 1;
1466 pkt
->cache_valid
= 0;
1469 /* Start the write request */
1470 bio_init(pkt
->w_bio
);
1471 pkt
->w_bio
->bi_max_vecs
= PACKET_MAX_SIZE
;
1472 pkt
->w_bio
->bi_sector
= pkt
->sector
;
1473 pkt
->w_bio
->bi_bdev
= pd
->bdev
;
1474 pkt
->w_bio
->bi_end_io
= pkt_end_io_packet_write
;
1475 pkt
->w_bio
->bi_private
= pkt
;
1476 for (f
= 0; f
< pkt
->frames
; f
++)
1477 if (!bio_add_page(pkt
->w_bio
, bvec
[f
].bv_page
, CD_FRAMESIZE
, bvec
[f
].bv_offset
))
1479 VPRINTK(DRIVER_NAME
": vcnt=%d\n", pkt
->w_bio
->bi_vcnt
);
1481 atomic_set(&pkt
->io_wait
, 1);
1482 pkt
->w_bio
->bi_rw
= WRITE
;
1483 pkt_queue_bio(pd
, pkt
->w_bio
);
1486 static void pkt_finish_packet(struct packet_data
*pkt
, int uptodate
)
1488 struct bio
*bio
, *next
;
1491 pkt
->cache_valid
= 0;
1493 /* Finish all bios corresponding to this packet */
1494 bio
= pkt
->orig_bios
;
1496 next
= bio
->bi_next
;
1497 bio
->bi_next
= NULL
;
1498 bio_endio(bio
, bio
->bi_size
, uptodate
? 0 : -EIO
);
1501 pkt
->orig_bios
= pkt
->orig_bios_tail
= NULL
;
1504 static void pkt_run_state_machine(struct pktcdvd_device
*pd
, struct packet_data
*pkt
)
1508 VPRINTK("run_state_machine: pkt %d\n", pkt
->id
);
1511 switch (pkt
->state
) {
1512 case PACKET_WAITING_STATE
:
1513 if ((pkt
->write_size
< pkt
->frames
) && (pkt
->sleep_time
> 0))
1516 pkt
->sleep_time
= 0;
1517 pkt_gather_data(pd
, pkt
);
1518 pkt_set_state(pkt
, PACKET_READ_WAIT_STATE
);
1521 case PACKET_READ_WAIT_STATE
:
1522 if (atomic_read(&pkt
->io_wait
) > 0)
1525 if (atomic_read(&pkt
->io_errors
) > 0) {
1526 pkt_set_state(pkt
, PACKET_RECOVERY_STATE
);
1528 pkt_start_write(pd
, pkt
);
1532 case PACKET_WRITE_WAIT_STATE
:
1533 if (atomic_read(&pkt
->io_wait
) > 0)
1536 if (test_bit(BIO_UPTODATE
, &pkt
->w_bio
->bi_flags
)) {
1537 pkt_set_state(pkt
, PACKET_FINISHED_STATE
);
1539 pkt_set_state(pkt
, PACKET_RECOVERY_STATE
);
1543 case PACKET_RECOVERY_STATE
:
1544 if (pkt_start_recovery(pkt
)) {
1545 pkt_start_write(pd
, pkt
);
1547 VPRINTK("No recovery possible\n");
1548 pkt_set_state(pkt
, PACKET_FINISHED_STATE
);
1552 case PACKET_FINISHED_STATE
:
1553 uptodate
= test_bit(BIO_UPTODATE
, &pkt
->w_bio
->bi_flags
);
1554 pkt_finish_packet(pkt
, uptodate
);
1564 static void pkt_handle_packets(struct pktcdvd_device
*pd
)
1566 struct packet_data
*pkt
, *next
;
1568 VPRINTK("pkt_handle_packets\n");
1571 * Run state machine for active packets
1573 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_active_list
, list
) {
1574 if (atomic_read(&pkt
->run_sm
) > 0) {
1575 atomic_set(&pkt
->run_sm
, 0);
1576 pkt_run_state_machine(pd
, pkt
);
1581 * Move no longer active packets to the free list
1583 spin_lock(&pd
->cdrw
.active_list_lock
);
1584 list_for_each_entry_safe(pkt
, next
, &pd
->cdrw
.pkt_active_list
, list
) {
1585 if (pkt
->state
== PACKET_FINISHED_STATE
) {
1586 list_del(&pkt
->list
);
1587 pkt_put_packet_data(pd
, pkt
);
1588 pkt_set_state(pkt
, PACKET_IDLE_STATE
);
1589 atomic_set(&pd
->scan_queue
, 1);
1592 spin_unlock(&pd
->cdrw
.active_list_lock
);
1595 static void pkt_count_states(struct pktcdvd_device
*pd
, int *states
)
1597 struct packet_data
*pkt
;
1600 for (i
= 0; i
< PACKET_NUM_STATES
; i
++)
1603 spin_lock(&pd
->cdrw
.active_list_lock
);
1604 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_active_list
, list
) {
1605 states
[pkt
->state
]++;
1607 spin_unlock(&pd
->cdrw
.active_list_lock
);
1611 * kcdrwd is woken up when writes have been queued for one of our
1612 * registered devices
1614 static int kcdrwd(void *foobar
)
1616 struct pktcdvd_device
*pd
= foobar
;
1617 struct packet_data
*pkt
;
1618 long min_sleep_time
, residue
;
1620 set_user_nice(current
, -20);
1623 DECLARE_WAITQUEUE(wait
, current
);
1626 * Wait until there is something to do
1628 add_wait_queue(&pd
->wqueue
, &wait
);
1630 set_current_state(TASK_INTERRUPTIBLE
);
1632 /* Check if we need to run pkt_handle_queue */
1633 if (atomic_read(&pd
->scan_queue
) > 0)
1636 /* Check if we need to run the state machine for some packet */
1637 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_active_list
, list
) {
1638 if (atomic_read(&pkt
->run_sm
) > 0)
1642 /* Check if we need to process the iosched queues */
1643 if (atomic_read(&pd
->iosched
.attention
) != 0)
1646 /* Otherwise, go to sleep */
1647 if (PACKET_DEBUG
> 1) {
1648 int states
[PACKET_NUM_STATES
];
1649 pkt_count_states(pd
, states
);
1650 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1651 states
[0], states
[1], states
[2], states
[3],
1652 states
[4], states
[5]);
1655 min_sleep_time
= MAX_SCHEDULE_TIMEOUT
;
1656 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_active_list
, list
) {
1657 if (pkt
->sleep_time
&& pkt
->sleep_time
< min_sleep_time
)
1658 min_sleep_time
= pkt
->sleep_time
;
1661 generic_unplug_device(bdev_get_queue(pd
->bdev
));
1663 VPRINTK("kcdrwd: sleeping\n");
1664 residue
= schedule_timeout(min_sleep_time
);
1665 VPRINTK("kcdrwd: wake up\n");
1667 /* make swsusp happy with our thread */
1670 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_active_list
, list
) {
1671 if (!pkt
->sleep_time
)
1673 pkt
->sleep_time
-= min_sleep_time
- residue
;
1674 if (pkt
->sleep_time
<= 0) {
1675 pkt
->sleep_time
= 0;
1676 atomic_inc(&pkt
->run_sm
);
1680 if (signal_pending(current
)) {
1681 flush_signals(current
);
1683 if (kthread_should_stop())
1687 set_current_state(TASK_RUNNING
);
1688 remove_wait_queue(&pd
->wqueue
, &wait
);
1690 if (kthread_should_stop())
1694 * if pkt_handle_queue returns true, we can queue
1697 while (pkt_handle_queue(pd
))
1701 * Handle packet state machine
1703 pkt_handle_packets(pd
);
1706 * Handle iosched queues
1708 pkt_iosched_process_queue(pd
);
1714 static void pkt_print_settings(struct pktcdvd_device
*pd
)
1716 printk(DRIVER_NAME
": %s packets, ", pd
->settings
.fp
? "Fixed" : "Variable");
1717 printk("%u blocks, ", pd
->settings
.size
>> 2);
1718 printk("Mode-%c disc\n", pd
->settings
.block_mode
== 8 ? '1' : '2');
1721 static int pkt_mode_sense(struct pktcdvd_device
*pd
, struct packet_command
*cgc
, int page_code
, int page_control
)
1723 memset(cgc
->cmd
, 0, sizeof(cgc
->cmd
));
1725 cgc
->cmd
[0] = GPCMD_MODE_SENSE_10
;
1726 cgc
->cmd
[2] = page_code
| (page_control
<< 6);
1727 cgc
->cmd
[7] = cgc
->buflen
>> 8;
1728 cgc
->cmd
[8] = cgc
->buflen
& 0xff;
1729 cgc
->data_direction
= CGC_DATA_READ
;
1730 return pkt_generic_packet(pd
, cgc
);
1733 static int pkt_mode_select(struct pktcdvd_device
*pd
, struct packet_command
*cgc
)
1735 memset(cgc
->cmd
, 0, sizeof(cgc
->cmd
));
1736 memset(cgc
->buffer
, 0, 2);
1737 cgc
->cmd
[0] = GPCMD_MODE_SELECT_10
;
1738 cgc
->cmd
[1] = 0x10; /* PF */
1739 cgc
->cmd
[7] = cgc
->buflen
>> 8;
1740 cgc
->cmd
[8] = cgc
->buflen
& 0xff;
1741 cgc
->data_direction
= CGC_DATA_WRITE
;
1742 return pkt_generic_packet(pd
, cgc
);
1745 static int pkt_get_disc_info(struct pktcdvd_device
*pd
, disc_information
*di
)
1747 struct packet_command cgc
;
1750 /* set up command and get the disc info */
1751 init_cdrom_command(&cgc
, di
, sizeof(*di
), CGC_DATA_READ
);
1752 cgc
.cmd
[0] = GPCMD_READ_DISC_INFO
;
1753 cgc
.cmd
[8] = cgc
.buflen
= 2;
1756 if ((ret
= pkt_generic_packet(pd
, &cgc
)))
1759 /* not all drives have the same disc_info length, so requeue
1760 * packet with the length the drive tells us it can supply
1762 cgc
.buflen
= be16_to_cpu(di
->disc_information_length
) +
1763 sizeof(di
->disc_information_length
);
1765 if (cgc
.buflen
> sizeof(disc_information
))
1766 cgc
.buflen
= sizeof(disc_information
);
1768 cgc
.cmd
[8] = cgc
.buflen
;
1769 return pkt_generic_packet(pd
, &cgc
);
1772 static int pkt_get_track_info(struct pktcdvd_device
*pd
, __u16 track
, __u8 type
, track_information
*ti
)
1774 struct packet_command cgc
;
1777 init_cdrom_command(&cgc
, ti
, 8, CGC_DATA_READ
);
1778 cgc
.cmd
[0] = GPCMD_READ_TRACK_RZONE_INFO
;
1779 cgc
.cmd
[1] = type
& 3;
1780 cgc
.cmd
[4] = (track
& 0xff00) >> 8;
1781 cgc
.cmd
[5] = track
& 0xff;
1785 if ((ret
= pkt_generic_packet(pd
, &cgc
)))
1788 cgc
.buflen
= be16_to_cpu(ti
->track_information_length
) +
1789 sizeof(ti
->track_information_length
);
1791 if (cgc
.buflen
> sizeof(track_information
))
1792 cgc
.buflen
= sizeof(track_information
);
1794 cgc
.cmd
[8] = cgc
.buflen
;
1795 return pkt_generic_packet(pd
, &cgc
);
1798 static int pkt_get_last_written(struct pktcdvd_device
*pd
, long *last_written
)
1800 disc_information di
;
1801 track_information ti
;
1805 if ((ret
= pkt_get_disc_info(pd
, &di
)))
1808 last_track
= (di
.last_track_msb
<< 8) | di
.last_track_lsb
;
1809 if ((ret
= pkt_get_track_info(pd
, last_track
, 1, &ti
)))
1812 /* if this track is blank, try the previous. */
1815 if ((ret
= pkt_get_track_info(pd
, last_track
, 1, &ti
)))
1819 /* if last recorded field is valid, return it. */
1821 *last_written
= be32_to_cpu(ti
.last_rec_address
);
1823 /* make it up instead */
1824 *last_written
= be32_to_cpu(ti
.track_start
) +
1825 be32_to_cpu(ti
.track_size
);
1827 *last_written
-= (be32_to_cpu(ti
.free_blocks
) + 7);
1833 * write mode select package based on pd->settings
1835 static int pkt_set_write_settings(struct pktcdvd_device
*pd
)
1837 struct packet_command cgc
;
1838 struct request_sense sense
;
1839 write_param_page
*wp
;
1843 /* doesn't apply to DVD+RW or DVD-RAM */
1844 if ((pd
->mmc3_profile
== 0x1a) || (pd
->mmc3_profile
== 0x12))
1847 memset(buffer
, 0, sizeof(buffer
));
1848 init_cdrom_command(&cgc
, buffer
, sizeof(*wp
), CGC_DATA_READ
);
1850 if ((ret
= pkt_mode_sense(pd
, &cgc
, GPMODE_WRITE_PARMS_PAGE
, 0))) {
1851 pkt_dump_sense(&cgc
);
1855 size
= 2 + ((buffer
[0] << 8) | (buffer
[1] & 0xff));
1856 pd
->mode_offset
= (buffer
[6] << 8) | (buffer
[7] & 0xff);
1857 if (size
> sizeof(buffer
))
1858 size
= sizeof(buffer
);
1863 init_cdrom_command(&cgc
, buffer
, size
, CGC_DATA_READ
);
1865 if ((ret
= pkt_mode_sense(pd
, &cgc
, GPMODE_WRITE_PARMS_PAGE
, 0))) {
1866 pkt_dump_sense(&cgc
);
1871 * write page is offset header + block descriptor length
1873 wp
= (write_param_page
*) &buffer
[sizeof(struct mode_page_header
) + pd
->mode_offset
];
1875 wp
->fp
= pd
->settings
.fp
;
1876 wp
->track_mode
= pd
->settings
.track_mode
;
1877 wp
->write_type
= pd
->settings
.write_type
;
1878 wp
->data_block_type
= pd
->settings
.block_mode
;
1880 wp
->multi_session
= 0;
1882 #ifdef PACKET_USE_LS
1887 if (wp
->data_block_type
== PACKET_BLOCK_MODE1
) {
1888 wp
->session_format
= 0;
1890 } else if (wp
->data_block_type
== PACKET_BLOCK_MODE2
) {
1891 wp
->session_format
= 0x20;
1895 memcpy(&wp
->mcn
[1], PACKET_MCN
, sizeof(wp
->mcn
) - 1);
1901 printk(DRIVER_NAME
": write mode wrong %d\n", wp
->data_block_type
);
1904 wp
->packet_size
= cpu_to_be32(pd
->settings
.size
>> 2);
1906 cgc
.buflen
= cgc
.cmd
[8] = size
;
1907 if ((ret
= pkt_mode_select(pd
, &cgc
))) {
1908 pkt_dump_sense(&cgc
);
1912 pkt_print_settings(pd
);
1917 * 1 -- we can write to this track, 0 -- we can't
1919 static int pkt_writable_track(struct pktcdvd_device
*pd
, track_information
*ti
)
1921 switch (pd
->mmc3_profile
) {
1922 case 0x1a: /* DVD+RW */
1923 case 0x12: /* DVD-RAM */
1924 /* The track is always writable on DVD+RW/DVD-RAM */
1930 if (!ti
->packet
|| !ti
->fp
)
1934 * "good" settings as per Mt Fuji.
1936 if (ti
->rt
== 0 && ti
->blank
== 0)
1939 if (ti
->rt
== 0 && ti
->blank
== 1)
1942 if (ti
->rt
== 1 && ti
->blank
== 0)
1945 printk(DRIVER_NAME
": bad state %d-%d-%d\n", ti
->rt
, ti
->blank
, ti
->packet
);
1950 * 1 -- we can write to this disc, 0 -- we can't
1952 static int pkt_writable_disc(struct pktcdvd_device
*pd
, disc_information
*di
)
1954 switch (pd
->mmc3_profile
) {
1955 case 0x0a: /* CD-RW */
1956 case 0xffff: /* MMC3 not supported */
1958 case 0x1a: /* DVD+RW */
1959 case 0x13: /* DVD-RW */
1960 case 0x12: /* DVD-RAM */
1963 VPRINTK(DRIVER_NAME
": Wrong disc profile (%x)\n", pd
->mmc3_profile
);
1968 * for disc type 0xff we should probably reserve a new track.
1969 * but i'm not sure, should we leave this to user apps? probably.
1971 if (di
->disc_type
== 0xff) {
1972 printk(DRIVER_NAME
": Unknown disc. No track?\n");
1976 if (di
->disc_type
!= 0x20 && di
->disc_type
!= 0) {
1977 printk(DRIVER_NAME
": Wrong disc type (%x)\n", di
->disc_type
);
1981 if (di
->erasable
== 0) {
1982 printk(DRIVER_NAME
": Disc not erasable\n");
1986 if (di
->border_status
== PACKET_SESSION_RESERVED
) {
1987 printk(DRIVER_NAME
": Can't write to last track (reserved)\n");
1994 static int pkt_probe_settings(struct pktcdvd_device
*pd
)
1996 struct packet_command cgc
;
1997 unsigned char buf
[12];
1998 disc_information di
;
1999 track_information ti
;
2002 init_cdrom_command(&cgc
, buf
, sizeof(buf
), CGC_DATA_READ
);
2003 cgc
.cmd
[0] = GPCMD_GET_CONFIGURATION
;
2005 ret
= pkt_generic_packet(pd
, &cgc
);
2006 pd
->mmc3_profile
= ret
? 0xffff : buf
[6] << 8 | buf
[7];
2008 memset(&di
, 0, sizeof(disc_information
));
2009 memset(&ti
, 0, sizeof(track_information
));
2011 if ((ret
= pkt_get_disc_info(pd
, &di
))) {
2012 printk("failed get_disc\n");
2016 if (!pkt_writable_disc(pd
, &di
))
2019 pd
->type
= di
.erasable
? PACKET_CDRW
: PACKET_CDR
;
2021 track
= 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
2022 if ((ret
= pkt_get_track_info(pd
, track
, 1, &ti
))) {
2023 printk(DRIVER_NAME
": failed get_track\n");
2027 if (!pkt_writable_track(pd
, &ti
)) {
2028 printk(DRIVER_NAME
": can't write to this track\n");
2033 * we keep packet size in 512 byte units, makes it easier to
2034 * deal with request calculations.
2036 pd
->settings
.size
= be32_to_cpu(ti
.fixed_packet_size
) << 2;
2037 if (pd
->settings
.size
== 0) {
2038 printk(DRIVER_NAME
": detected zero packet size!\n");
2041 if (pd
->settings
.size
> PACKET_MAX_SECTORS
) {
2042 printk(DRIVER_NAME
": packet size is too big\n");
2045 pd
->settings
.fp
= ti
.fp
;
2046 pd
->offset
= (be32_to_cpu(ti
.track_start
) << 2) & (pd
->settings
.size
- 1);
2049 pd
->nwa
= be32_to_cpu(ti
.next_writable
);
2050 set_bit(PACKET_NWA_VALID
, &pd
->flags
);
2054 * in theory we could use lra on -RW media as well and just zero
2055 * blocks that haven't been written yet, but in practice that
2056 * is just a no-go. we'll use that for -R, naturally.
2059 pd
->lra
= be32_to_cpu(ti
.last_rec_address
);
2060 set_bit(PACKET_LRA_VALID
, &pd
->flags
);
2062 pd
->lra
= 0xffffffff;
2063 set_bit(PACKET_LRA_VALID
, &pd
->flags
);
2069 pd
->settings
.link_loss
= 7;
2070 pd
->settings
.write_type
= 0; /* packet */
2071 pd
->settings
.track_mode
= ti
.track_mode
;
2074 * mode1 or mode2 disc
2076 switch (ti
.data_mode
) {
2078 pd
->settings
.block_mode
= PACKET_BLOCK_MODE1
;
2081 pd
->settings
.block_mode
= PACKET_BLOCK_MODE2
;
2084 printk(DRIVER_NAME
": unknown data mode\n");
2091 * enable/disable write caching on drive
2093 static int pkt_write_caching(struct pktcdvd_device
*pd
, int set
)
2095 struct packet_command cgc
;
2096 struct request_sense sense
;
2097 unsigned char buf
[64];
2100 memset(buf
, 0, sizeof(buf
));
2101 init_cdrom_command(&cgc
, buf
, sizeof(buf
), CGC_DATA_READ
);
2103 cgc
.buflen
= pd
->mode_offset
+ 12;
2106 * caching mode page might not be there, so quiet this command
2110 if ((ret
= pkt_mode_sense(pd
, &cgc
, GPMODE_WCACHING_PAGE
, 0)))
2113 buf
[pd
->mode_offset
+ 10] |= (!!set
<< 2);
2115 cgc
.buflen
= cgc
.cmd
[8] = 2 + ((buf
[0] << 8) | (buf
[1] & 0xff));
2116 ret
= pkt_mode_select(pd
, &cgc
);
2118 printk(DRIVER_NAME
": write caching control failed\n");
2119 pkt_dump_sense(&cgc
);
2120 } else if (!ret
&& set
)
2121 printk(DRIVER_NAME
": enabled write caching on %s\n", pd
->name
);
2125 static int pkt_lock_door(struct pktcdvd_device
*pd
, int lockflag
)
2127 struct packet_command cgc
;
2129 init_cdrom_command(&cgc
, NULL
, 0, CGC_DATA_NONE
);
2130 cgc
.cmd
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
2131 cgc
.cmd
[4] = lockflag
? 1 : 0;
2132 return pkt_generic_packet(pd
, &cgc
);
2136 * Returns drive maximum write speed
2138 static int pkt_get_max_speed(struct pktcdvd_device
*pd
, unsigned *write_speed
)
2140 struct packet_command cgc
;
2141 struct request_sense sense
;
2142 unsigned char buf
[256+18];
2143 unsigned char *cap_buf
;
2146 memset(buf
, 0, sizeof(buf
));
2147 cap_buf
= &buf
[sizeof(struct mode_page_header
) + pd
->mode_offset
];
2148 init_cdrom_command(&cgc
, buf
, sizeof(buf
), CGC_DATA_UNKNOWN
);
2151 ret
= pkt_mode_sense(pd
, &cgc
, GPMODE_CAPABILITIES_PAGE
, 0);
2153 cgc
.buflen
= pd
->mode_offset
+ cap_buf
[1] + 2 +
2154 sizeof(struct mode_page_header
);
2155 ret
= pkt_mode_sense(pd
, &cgc
, GPMODE_CAPABILITIES_PAGE
, 0);
2157 pkt_dump_sense(&cgc
);
2162 offset
= 20; /* Obsoleted field, used by older drives */
2163 if (cap_buf
[1] >= 28)
2164 offset
= 28; /* Current write speed selected */
2165 if (cap_buf
[1] >= 30) {
2166 /* If the drive reports at least one "Logical Unit Write
2167 * Speed Performance Descriptor Block", use the information
2168 * in the first block. (contains the highest speed)
2170 int num_spdb
= (cap_buf
[30] << 8) + cap_buf
[31];
2175 *write_speed
= (cap_buf
[offset
] << 8) | cap_buf
[offset
+ 1];
2179 /* These tables from cdrecord - I don't have orange book */
2180 /* standard speed CD-RW (1-4x) */
2181 static char clv_to_speed
[16] = {
2182 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2183 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2185 /* high speed CD-RW (-10x) */
2186 static char hs_clv_to_speed
[16] = {
2187 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2188 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2190 /* ultra high speed CD-RW */
2191 static char us_clv_to_speed
[16] = {
2192 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2193 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2197 * reads the maximum media speed from ATIP
2199 static int pkt_media_speed(struct pktcdvd_device
*pd
, unsigned *speed
)
2201 struct packet_command cgc
;
2202 struct request_sense sense
;
2203 unsigned char buf
[64];
2204 unsigned int size
, st
, sp
;
2207 init_cdrom_command(&cgc
, buf
, 2, CGC_DATA_READ
);
2209 cgc
.cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
2211 cgc
.cmd
[2] = 4; /* READ ATIP */
2213 ret
= pkt_generic_packet(pd
, &cgc
);
2215 pkt_dump_sense(&cgc
);
2218 size
= ((unsigned int) buf
[0]<<8) + buf
[1] + 2;
2219 if (size
> sizeof(buf
))
2222 init_cdrom_command(&cgc
, buf
, size
, CGC_DATA_READ
);
2224 cgc
.cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
2228 ret
= pkt_generic_packet(pd
, &cgc
);
2230 pkt_dump_sense(&cgc
);
2234 if (!buf
[6] & 0x40) {
2235 printk(DRIVER_NAME
": Disc type is not CD-RW\n");
2238 if (!buf
[6] & 0x4) {
2239 printk(DRIVER_NAME
": A1 values on media are not valid, maybe not CDRW?\n");
2243 st
= (buf
[6] >> 3) & 0x7; /* disc sub-type */
2245 sp
= buf
[16] & 0xf; /* max speed from ATIP A1 field */
2247 /* Info from cdrecord */
2249 case 0: /* standard speed */
2250 *speed
= clv_to_speed
[sp
];
2252 case 1: /* high speed */
2253 *speed
= hs_clv_to_speed
[sp
];
2255 case 2: /* ultra high speed */
2256 *speed
= us_clv_to_speed
[sp
];
2259 printk(DRIVER_NAME
": Unknown disc sub-type %d\n",st
);
2263 printk(DRIVER_NAME
": Max. media speed: %d\n",*speed
);
2266 printk(DRIVER_NAME
": Unknown speed %d for sub-type %d\n",sp
,st
);
2271 static int pkt_perform_opc(struct pktcdvd_device
*pd
)
2273 struct packet_command cgc
;
2274 struct request_sense sense
;
2277 VPRINTK(DRIVER_NAME
": Performing OPC\n");
2279 init_cdrom_command(&cgc
, NULL
, 0, CGC_DATA_NONE
);
2281 cgc
.timeout
= 60*HZ
;
2282 cgc
.cmd
[0] = GPCMD_SEND_OPC
;
2284 if ((ret
= pkt_generic_packet(pd
, &cgc
)))
2285 pkt_dump_sense(&cgc
);
2289 static int pkt_open_write(struct pktcdvd_device
*pd
)
2292 unsigned int write_speed
, media_write_speed
, read_speed
;
2294 if ((ret
= pkt_probe_settings(pd
))) {
2295 VPRINTK(DRIVER_NAME
": %s failed probe\n", pd
->name
);
2299 if ((ret
= pkt_set_write_settings(pd
))) {
2300 DPRINTK(DRIVER_NAME
": %s failed saving write settings\n", pd
->name
);
2304 pkt_write_caching(pd
, USE_WCACHING
);
2306 if ((ret
= pkt_get_max_speed(pd
, &write_speed
)))
2307 write_speed
= 16 * 177;
2308 switch (pd
->mmc3_profile
) {
2309 case 0x13: /* DVD-RW */
2310 case 0x1a: /* DVD+RW */
2311 case 0x12: /* DVD-RAM */
2312 DPRINTK(DRIVER_NAME
": write speed %ukB/s\n", write_speed
);
2315 if ((ret
= pkt_media_speed(pd
, &media_write_speed
)))
2316 media_write_speed
= 16;
2317 write_speed
= min(write_speed
, media_write_speed
* 177);
2318 DPRINTK(DRIVER_NAME
": write speed %ux\n", write_speed
/ 176);
2321 read_speed
= write_speed
;
2323 if ((ret
= pkt_set_speed(pd
, write_speed
, read_speed
))) {
2324 DPRINTK(DRIVER_NAME
": %s couldn't set write speed\n", pd
->name
);
2327 pd
->write_speed
= write_speed
;
2328 pd
->read_speed
= read_speed
;
2330 if ((ret
= pkt_perform_opc(pd
))) {
2331 DPRINTK(DRIVER_NAME
": %s Optimum Power Calibration failed\n", pd
->name
);
2338 * called at open time.
2340 static int pkt_open_dev(struct pktcdvd_device
*pd
, int write
)
2347 * We need to re-open the cdrom device without O_NONBLOCK to be able
2348 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2349 * so bdget() can't fail.
2351 bdget(pd
->bdev
->bd_dev
);
2352 if ((ret
= blkdev_get(pd
->bdev
, FMODE_READ
, O_RDONLY
)))
2355 if ((ret
= bd_claim(pd
->bdev
, pd
)))
2358 if ((ret
= pkt_get_last_written(pd
, &lba
))) {
2359 printk(DRIVER_NAME
": pkt_get_last_written failed\n");
2363 set_capacity(pd
->disk
, lba
<< 2);
2364 set_capacity(pd
->bdev
->bd_disk
, lba
<< 2);
2365 bd_set_size(pd
->bdev
, (loff_t
)lba
<< 11);
2367 q
= bdev_get_queue(pd
->bdev
);
2369 if ((ret
= pkt_open_write(pd
)))
2372 * Some CDRW drives can not handle writes larger than one packet,
2373 * even if the size is a multiple of the packet size.
2375 spin_lock_irq(q
->queue_lock
);
2376 blk_queue_max_sectors(q
, pd
->settings
.size
);
2377 spin_unlock_irq(q
->queue_lock
);
2378 set_bit(PACKET_WRITABLE
, &pd
->flags
);
2380 pkt_set_speed(pd
, MAX_SPEED
, MAX_SPEED
);
2381 clear_bit(PACKET_WRITABLE
, &pd
->flags
);
2384 if ((ret
= pkt_set_segment_merging(pd
, q
)))
2388 if (!pkt_grow_pktlist(pd
, CONFIG_CDROM_PKTCDVD_BUFFERS
)) {
2389 printk(DRIVER_NAME
": not enough memory for buffers\n");
2393 printk(DRIVER_NAME
": %lukB available on disc\n", lba
<< 1);
2399 bd_release(pd
->bdev
);
2401 blkdev_put(pd
->bdev
);
2407 * called when the device is closed. makes sure that the device flushes
2408 * the internal cache before we close.
2410 static void pkt_release_dev(struct pktcdvd_device
*pd
, int flush
)
2412 if (flush
&& pkt_flush_cache(pd
))
2413 DPRINTK(DRIVER_NAME
": %s not flushing cache\n", pd
->name
);
2415 pkt_lock_door(pd
, 0);
2417 pkt_set_speed(pd
, MAX_SPEED
, MAX_SPEED
);
2418 bd_release(pd
->bdev
);
2419 blkdev_put(pd
->bdev
);
2421 pkt_shrink_pktlist(pd
);
2424 static struct pktcdvd_device
*pkt_find_dev_from_minor(int dev_minor
)
2426 if (dev_minor
>= MAX_WRITERS
)
2428 return pkt_devs
[dev_minor
];
2431 static int pkt_open(struct inode
*inode
, struct file
*file
)
2433 struct pktcdvd_device
*pd
= NULL
;
2436 VPRINTK(DRIVER_NAME
": entering open\n");
2438 mutex_lock(&ctl_mutex
);
2439 pd
= pkt_find_dev_from_minor(iminor(inode
));
2444 BUG_ON(pd
->refcnt
< 0);
2447 if (pd
->refcnt
> 1) {
2448 if ((file
->f_mode
& FMODE_WRITE
) &&
2449 !test_bit(PACKET_WRITABLE
, &pd
->flags
)) {
2454 ret
= pkt_open_dev(pd
, file
->f_mode
& FMODE_WRITE
);
2458 * needed here as well, since ext2 (among others) may change
2459 * the blocksize at mount time
2461 set_blocksize(inode
->i_bdev
, CD_FRAMESIZE
);
2464 mutex_unlock(&ctl_mutex
);
2470 VPRINTK(DRIVER_NAME
": failed open (%d)\n", ret
);
2471 mutex_unlock(&ctl_mutex
);
2475 static int pkt_close(struct inode
*inode
, struct file
*file
)
2477 struct pktcdvd_device
*pd
= inode
->i_bdev
->bd_disk
->private_data
;
2480 mutex_lock(&ctl_mutex
);
2482 BUG_ON(pd
->refcnt
< 0);
2483 if (pd
->refcnt
== 0) {
2484 int flush
= test_bit(PACKET_WRITABLE
, &pd
->flags
);
2485 pkt_release_dev(pd
, flush
);
2487 mutex_unlock(&ctl_mutex
);
2492 static int pkt_end_io_read_cloned(struct bio
*bio
, unsigned int bytes_done
, int err
)
2494 struct packet_stacked_data
*psd
= bio
->bi_private
;
2495 struct pktcdvd_device
*pd
= psd
->pd
;
2501 bio_endio(psd
->bio
, psd
->bio
->bi_size
, err
);
2502 mempool_free(psd
, psd_pool
);
2503 pkt_bio_finished(pd
);
2507 static int pkt_make_request(request_queue_t
*q
, struct bio
*bio
)
2509 struct pktcdvd_device
*pd
;
2510 char b
[BDEVNAME_SIZE
];
2512 struct packet_data
*pkt
;
2513 int was_empty
, blocked_bio
;
2514 struct pkt_rb_node
*node
;
2518 printk(DRIVER_NAME
": %s incorrect request queue\n", bdevname(bio
->bi_bdev
, b
));
2523 * Clone READ bios so we can have our own bi_end_io callback.
2525 if (bio_data_dir(bio
) == READ
) {
2526 struct bio
*cloned_bio
= bio_clone(bio
, GFP_NOIO
);
2527 struct packet_stacked_data
*psd
= mempool_alloc(psd_pool
, GFP_NOIO
);
2531 cloned_bio
->bi_bdev
= pd
->bdev
;
2532 cloned_bio
->bi_private
= psd
;
2533 cloned_bio
->bi_end_io
= pkt_end_io_read_cloned
;
2534 pd
->stats
.secs_r
+= bio
->bi_size
>> 9;
2535 pkt_queue_bio(pd
, cloned_bio
);
2539 if (!test_bit(PACKET_WRITABLE
, &pd
->flags
)) {
2540 printk(DRIVER_NAME
": WRITE for ro device %s (%llu)\n",
2541 pd
->name
, (unsigned long long)bio
->bi_sector
);
2545 if (!bio
->bi_size
|| (bio
->bi_size
% CD_FRAMESIZE
)) {
2546 printk(DRIVER_NAME
": wrong bio size\n");
2550 blk_queue_bounce(q
, &bio
);
2552 zone
= ZONE(bio
->bi_sector
, pd
);
2553 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2554 (unsigned long long)bio
->bi_sector
,
2555 (unsigned long long)(bio
->bi_sector
+ bio_sectors(bio
)));
2557 /* Check if we have to split the bio */
2559 struct bio_pair
*bp
;
2563 last_zone
= ZONE(bio
->bi_sector
+ bio_sectors(bio
) - 1, pd
);
2564 if (last_zone
!= zone
) {
2565 BUG_ON(last_zone
!= zone
+ pd
->settings
.size
);
2566 first_sectors
= last_zone
- bio
->bi_sector
;
2567 bp
= bio_split(bio
, bio_split_pool
, first_sectors
);
2569 pkt_make_request(q
, &bp
->bio1
);
2570 pkt_make_request(q
, &bp
->bio2
);
2571 bio_pair_release(bp
);
2577 * If we find a matching packet in state WAITING or READ_WAIT, we can
2578 * just append this bio to that packet.
2580 spin_lock(&pd
->cdrw
.active_list_lock
);
2582 list_for_each_entry(pkt
, &pd
->cdrw
.pkt_active_list
, list
) {
2583 if (pkt
->sector
== zone
) {
2584 spin_lock(&pkt
->lock
);
2585 if ((pkt
->state
== PACKET_WAITING_STATE
) ||
2586 (pkt
->state
== PACKET_READ_WAIT_STATE
)) {
2587 pkt_add_list_last(bio
, &pkt
->orig_bios
,
2588 &pkt
->orig_bios_tail
);
2589 pkt
->write_size
+= bio
->bi_size
/ CD_FRAMESIZE
;
2590 if ((pkt
->write_size
>= pkt
->frames
) &&
2591 (pkt
->state
== PACKET_WAITING_STATE
)) {
2592 atomic_inc(&pkt
->run_sm
);
2593 wake_up(&pd
->wqueue
);
2595 spin_unlock(&pkt
->lock
);
2596 spin_unlock(&pd
->cdrw
.active_list_lock
);
2601 spin_unlock(&pkt
->lock
);
2604 spin_unlock(&pd
->cdrw
.active_list_lock
);
2607 * Test if there is enough room left in the bio work queue
2608 * (queue size >= congestion on mark).
2609 * If not, wait till the work queue size is below the congestion off mark.
2611 spin_lock(&pd
->lock
);
2612 if (pd
->write_congestion_on
> 0
2613 && pd
->bio_queue_size
>= pd
->write_congestion_on
) {
2614 blk_set_queue_congested(q
, WRITE
);
2616 spin_unlock(&pd
->lock
);
2617 congestion_wait(WRITE
, HZ
);
2618 spin_lock(&pd
->lock
);
2619 } while(pd
->bio_queue_size
> pd
->write_congestion_off
);
2621 spin_unlock(&pd
->lock
);
2624 * No matching packet found. Store the bio in the work queue.
2626 node
= mempool_alloc(pd
->rb_pool
, GFP_NOIO
);
2628 spin_lock(&pd
->lock
);
2629 BUG_ON(pd
->bio_queue_size
< 0);
2630 was_empty
= (pd
->bio_queue_size
== 0);
2631 pkt_rbtree_insert(pd
, node
);
2632 spin_unlock(&pd
->lock
);
2635 * Wake up the worker thread.
2637 atomic_set(&pd
->scan_queue
, 1);
2639 /* This wake_up is required for correct operation */
2640 wake_up(&pd
->wqueue
);
2641 } else if (!list_empty(&pd
->cdrw
.pkt_free_list
) && !blocked_bio
) {
2643 * This wake up is not required for correct operation,
2644 * but improves performance in some cases.
2646 wake_up(&pd
->wqueue
);
2650 bio_io_error(bio
, bio
->bi_size
);
2656 static int pkt_merge_bvec(request_queue_t
*q
, struct bio
*bio
, struct bio_vec
*bvec
)
2658 struct pktcdvd_device
*pd
= q
->queuedata
;
2659 sector_t zone
= ZONE(bio
->bi_sector
, pd
);
2660 int used
= ((bio
->bi_sector
- zone
) << 9) + bio
->bi_size
;
2661 int remaining
= (pd
->settings
.size
<< 9) - used
;
2665 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2666 * boundary, pkt_make_request() will split the bio.
2668 remaining2
= PAGE_SIZE
- bio
->bi_size
;
2669 remaining
= max(remaining
, remaining2
);
2671 BUG_ON(remaining
< 0);
2675 static void pkt_init_queue(struct pktcdvd_device
*pd
)
2677 request_queue_t
*q
= pd
->disk
->queue
;
2679 blk_queue_make_request(q
, pkt_make_request
);
2680 blk_queue_hardsect_size(q
, CD_FRAMESIZE
);
2681 blk_queue_max_sectors(q
, PACKET_MAX_SECTORS
);
2682 blk_queue_merge_bvec(q
, pkt_merge_bvec
);
2686 static int pkt_seq_show(struct seq_file
*m
, void *p
)
2688 struct pktcdvd_device
*pd
= m
->private;
2690 char bdev_buf
[BDEVNAME_SIZE
];
2691 int states
[PACKET_NUM_STATES
];
2693 seq_printf(m
, "Writer %s mapped to %s:\n", pd
->name
,
2694 bdevname(pd
->bdev
, bdev_buf
));
2696 seq_printf(m
, "\nSettings:\n");
2697 seq_printf(m
, "\tpacket size:\t\t%dkB\n", pd
->settings
.size
/ 2);
2699 if (pd
->settings
.write_type
== 0)
2703 seq_printf(m
, "\twrite type:\t\t%s\n", msg
);
2705 seq_printf(m
, "\tpacket type:\t\t%s\n", pd
->settings
.fp
? "Fixed" : "Variable");
2706 seq_printf(m
, "\tlink loss:\t\t%d\n", pd
->settings
.link_loss
);
2708 seq_printf(m
, "\ttrack mode:\t\t%d\n", pd
->settings
.track_mode
);
2710 if (pd
->settings
.block_mode
== PACKET_BLOCK_MODE1
)
2712 else if (pd
->settings
.block_mode
== PACKET_BLOCK_MODE2
)
2716 seq_printf(m
, "\tblock mode:\t\t%s\n", msg
);
2718 seq_printf(m
, "\nStatistics:\n");
2719 seq_printf(m
, "\tpackets started:\t%lu\n", pd
->stats
.pkt_started
);
2720 seq_printf(m
, "\tpackets ended:\t\t%lu\n", pd
->stats
.pkt_ended
);
2721 seq_printf(m
, "\twritten:\t\t%lukB\n", pd
->stats
.secs_w
>> 1);
2722 seq_printf(m
, "\tread gather:\t\t%lukB\n", pd
->stats
.secs_rg
>> 1);
2723 seq_printf(m
, "\tread:\t\t\t%lukB\n", pd
->stats
.secs_r
>> 1);
2725 seq_printf(m
, "\nMisc:\n");
2726 seq_printf(m
, "\treference count:\t%d\n", pd
->refcnt
);
2727 seq_printf(m
, "\tflags:\t\t\t0x%lx\n", pd
->flags
);
2728 seq_printf(m
, "\tread speed:\t\t%ukB/s\n", pd
->read_speed
);
2729 seq_printf(m
, "\twrite speed:\t\t%ukB/s\n", pd
->write_speed
);
2730 seq_printf(m
, "\tstart offset:\t\t%lu\n", pd
->offset
);
2731 seq_printf(m
, "\tmode page offset:\t%u\n", pd
->mode_offset
);
2733 seq_printf(m
, "\nQueue state:\n");
2734 seq_printf(m
, "\tbios queued:\t\t%d\n", pd
->bio_queue_size
);
2735 seq_printf(m
, "\tbios pending:\t\t%d\n", atomic_read(&pd
->cdrw
.pending_bios
));
2736 seq_printf(m
, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd
->current_sector
);
2738 pkt_count_states(pd
, states
);
2739 seq_printf(m
, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2740 states
[0], states
[1], states
[2], states
[3], states
[4], states
[5]);
2742 seq_printf(m
, "\twrite congestion marks:\toff=%d on=%d\n",
2743 pd
->write_congestion_off
,
2744 pd
->write_congestion_on
);
2748 static int pkt_seq_open(struct inode
*inode
, struct file
*file
)
2750 return single_open(file
, pkt_seq_show
, PDE(inode
)->data
);
2753 static struct file_operations pkt_proc_fops
= {
2754 .open
= pkt_seq_open
,
2756 .llseek
= seq_lseek
,
2757 .release
= single_release
2760 static int pkt_new_dev(struct pktcdvd_device
*pd
, dev_t dev
)
2764 char b
[BDEVNAME_SIZE
];
2765 struct proc_dir_entry
*proc
;
2766 struct block_device
*bdev
;
2768 if (pd
->pkt_dev
== dev
) {
2769 printk(DRIVER_NAME
": Recursive setup not allowed\n");
2772 for (i
= 0; i
< MAX_WRITERS
; i
++) {
2773 struct pktcdvd_device
*pd2
= pkt_devs
[i
];
2776 if (pd2
->bdev
->bd_dev
== dev
) {
2777 printk(DRIVER_NAME
": %s already setup\n", bdevname(pd2
->bdev
, b
));
2780 if (pd2
->pkt_dev
== dev
) {
2781 printk(DRIVER_NAME
": Can't chain pktcdvd devices\n");
2789 ret
= blkdev_get(bdev
, FMODE_READ
, O_RDONLY
| O_NONBLOCK
);
2793 /* This is safe, since we have a reference from open(). */
2794 __module_get(THIS_MODULE
);
2797 set_blocksize(bdev
, CD_FRAMESIZE
);
2801 atomic_set(&pd
->cdrw
.pending_bios
, 0);
2802 pd
->cdrw
.thread
= kthread_run(kcdrwd
, pd
, "%s", pd
->name
);
2803 if (IS_ERR(pd
->cdrw
.thread
)) {
2804 printk(DRIVER_NAME
": can't start kernel thread\n");
2809 proc
= create_proc_entry(pd
->name
, 0, pkt_proc
);
2812 proc
->proc_fops
= &pkt_proc_fops
;
2814 DPRINTK(DRIVER_NAME
": writer %s mapped to %s\n", pd
->name
, bdevname(bdev
, b
));
2819 /* This is safe: open() is still holding a reference. */
2820 module_put(THIS_MODULE
);
2824 static int pkt_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2826 struct pktcdvd_device
*pd
= inode
->i_bdev
->bd_disk
->private_data
;
2828 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd
, imajor(inode
), iminor(inode
));
2832 * forward selected CDROM ioctls to CD-ROM, for UDF
2834 case CDROMMULTISESSION
:
2835 case CDROMREADTOCENTRY
:
2836 case CDROM_LAST_WRITTEN
:
2837 case CDROM_SEND_PACKET
:
2838 case SCSI_IOCTL_SEND_COMMAND
:
2839 return blkdev_ioctl(pd
->bdev
->bd_inode
, file
, cmd
, arg
);
2843 * The door gets locked when the device is opened, so we
2844 * have to unlock it or else the eject command fails.
2846 if (pd
->refcnt
== 1)
2847 pkt_lock_door(pd
, 0);
2848 return blkdev_ioctl(pd
->bdev
->bd_inode
, file
, cmd
, arg
);
2851 VPRINTK(DRIVER_NAME
": Unknown ioctl for %s (%x)\n", pd
->name
, cmd
);
2858 static int pkt_media_changed(struct gendisk
*disk
)
2860 struct pktcdvd_device
*pd
= disk
->private_data
;
2861 struct gendisk
*attached_disk
;
2867 attached_disk
= pd
->bdev
->bd_disk
;
2870 return attached_disk
->fops
->media_changed(attached_disk
);
2873 static struct block_device_operations pktcdvd_ops
= {
2874 .owner
= THIS_MODULE
,
2876 .release
= pkt_close
,
2878 .media_changed
= pkt_media_changed
,
2882 * Set up mapping from pktcdvd device to CD-ROM device.
2884 static int pkt_setup_dev(dev_t dev
, dev_t
* pkt_dev
)
2888 struct pktcdvd_device
*pd
;
2889 struct gendisk
*disk
;
2891 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
2893 for (idx
= 0; idx
< MAX_WRITERS
; idx
++)
2896 if (idx
== MAX_WRITERS
) {
2897 printk(DRIVER_NAME
": max %d writers supported\n", MAX_WRITERS
);
2902 pd
= kzalloc(sizeof(struct pktcdvd_device
), GFP_KERNEL
);
2906 pd
->rb_pool
= mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE
,
2907 sizeof(struct pkt_rb_node
));
2911 INIT_LIST_HEAD(&pd
->cdrw
.pkt_free_list
);
2912 INIT_LIST_HEAD(&pd
->cdrw
.pkt_active_list
);
2913 spin_lock_init(&pd
->cdrw
.active_list_lock
);
2915 spin_lock_init(&pd
->lock
);
2916 spin_lock_init(&pd
->iosched
.lock
);
2917 sprintf(pd
->name
, DRIVER_NAME
"%d", idx
);
2918 init_waitqueue_head(&pd
->wqueue
);
2919 pd
->bio_queue
= RB_ROOT
;
2921 pd
->write_congestion_on
= write_congestion_on
;
2922 pd
->write_congestion_off
= write_congestion_off
;
2924 disk
= alloc_disk(1);
2928 disk
->major
= pktdev_major
;
2929 disk
->first_minor
= idx
;
2930 disk
->fops
= &pktcdvd_ops
;
2931 disk
->flags
= GENHD_FL_REMOVABLE
;
2932 strcpy(disk
->disk_name
, pd
->name
);
2933 disk
->private_data
= pd
;
2934 disk
->queue
= blk_alloc_queue(GFP_KERNEL
);
2938 pd
->pkt_dev
= MKDEV(disk
->major
, disk
->first_minor
);
2939 ret
= pkt_new_dev(pd
, dev
);
2945 pkt_sysfs_dev_new(pd
);
2946 pkt_debugfs_dev_new(pd
);
2950 *pkt_dev
= pd
->pkt_dev
;
2952 mutex_unlock(&ctl_mutex
);
2956 blk_cleanup_queue(disk
->queue
);
2961 mempool_destroy(pd
->rb_pool
);
2964 mutex_unlock(&ctl_mutex
);
2965 printk(DRIVER_NAME
": setup of pktcdvd device failed\n");
2970 * Tear down mapping from pktcdvd device to CD-ROM device.
2972 static int pkt_remove_dev(dev_t pkt_dev
)
2974 struct pktcdvd_device
*pd
;
2978 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
2980 for (idx
= 0; idx
< MAX_WRITERS
; idx
++) {
2982 if (pd
&& (pd
->pkt_dev
== pkt_dev
))
2985 if (idx
== MAX_WRITERS
) {
2986 DPRINTK(DRIVER_NAME
": dev not setup\n");
2991 if (pd
->refcnt
> 0) {
2995 if (!IS_ERR(pd
->cdrw
.thread
))
2996 kthread_stop(pd
->cdrw
.thread
);
2998 pkt_devs
[idx
] = NULL
;
3000 pkt_debugfs_dev_remove(pd
);
3001 pkt_sysfs_dev_remove(pd
);
3003 blkdev_put(pd
->bdev
);
3005 remove_proc_entry(pd
->name
, pkt_proc
);
3006 DPRINTK(DRIVER_NAME
": writer %s unmapped\n", pd
->name
);
3008 del_gendisk(pd
->disk
);
3009 blk_cleanup_queue(pd
->disk
->queue
);
3012 mempool_destroy(pd
->rb_pool
);
3015 /* This is safe: open() is still holding a reference. */
3016 module_put(THIS_MODULE
);
3019 mutex_unlock(&ctl_mutex
);
3023 static void pkt_get_status(struct pkt_ctrl_command
*ctrl_cmd
)
3025 struct pktcdvd_device
*pd
;
3027 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
3029 pd
= pkt_find_dev_from_minor(ctrl_cmd
->dev_index
);
3031 ctrl_cmd
->dev
= new_encode_dev(pd
->bdev
->bd_dev
);
3032 ctrl_cmd
->pkt_dev
= new_encode_dev(pd
->pkt_dev
);
3035 ctrl_cmd
->pkt_dev
= 0;
3037 ctrl_cmd
->num_devices
= MAX_WRITERS
;
3039 mutex_unlock(&ctl_mutex
);
3042 static int pkt_ctl_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
3044 void __user
*argp
= (void __user
*)arg
;
3045 struct pkt_ctrl_command ctrl_cmd
;
3049 if (cmd
!= PACKET_CTRL_CMD
)
3052 if (copy_from_user(&ctrl_cmd
, argp
, sizeof(struct pkt_ctrl_command
)))
3055 switch (ctrl_cmd
.command
) {
3056 case PKT_CTRL_CMD_SETUP
:
3057 if (!capable(CAP_SYS_ADMIN
))
3059 ret
= pkt_setup_dev(new_decode_dev(ctrl_cmd
.dev
), &pkt_dev
);
3060 ctrl_cmd
.pkt_dev
= new_encode_dev(pkt_dev
);
3062 case PKT_CTRL_CMD_TEARDOWN
:
3063 if (!capable(CAP_SYS_ADMIN
))
3065 ret
= pkt_remove_dev(new_decode_dev(ctrl_cmd
.pkt_dev
));
3067 case PKT_CTRL_CMD_STATUS
:
3068 pkt_get_status(&ctrl_cmd
);
3074 if (copy_to_user(argp
, &ctrl_cmd
, sizeof(struct pkt_ctrl_command
)))
3080 static struct file_operations pkt_ctl_fops
= {
3081 .ioctl
= pkt_ctl_ioctl
,
3082 .owner
= THIS_MODULE
,
3085 static struct miscdevice pkt_misc
= {
3086 .minor
= MISC_DYNAMIC_MINOR
,
3087 .name
= DRIVER_NAME
,
3088 .fops
= &pkt_ctl_fops
3091 static int __init
pkt_init(void)
3095 mutex_init(&ctl_mutex
);
3097 psd_pool
= mempool_create_kmalloc_pool(PSD_POOL_SIZE
,
3098 sizeof(struct packet_stacked_data
));
3102 ret
= register_blkdev(pktdev_major
, DRIVER_NAME
);
3104 printk(DRIVER_NAME
": Unable to register block device\n");
3110 ret
= pkt_sysfs_init();
3116 ret
= misc_register(&pkt_misc
);
3118 printk(DRIVER_NAME
": Unable to register misc device\n");
3122 pkt_proc
= proc_mkdir(DRIVER_NAME
, proc_root_driver
);
3127 pkt_debugfs_cleanup();
3128 pkt_sysfs_cleanup();
3130 unregister_blkdev(pktdev_major
, DRIVER_NAME
);
3132 mempool_destroy(psd_pool
);
3136 static void __exit
pkt_exit(void)
3138 remove_proc_entry(DRIVER_NAME
, proc_root_driver
);
3139 misc_deregister(&pkt_misc
);
3141 pkt_debugfs_cleanup();
3142 pkt_sysfs_cleanup();
3144 unregister_blkdev(pktdev_major
, DRIVER_NAME
);
3145 mempool_destroy(psd_pool
);
3148 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3149 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3150 MODULE_LICENSE("GPL");
3152 module_init(pkt_init
);
3153 module_exit(pkt_exit
);