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;
115 p = kzalloc(sizeof(*p), GFP_KERNEL);
119 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
121 kobject_put(&p->kobj);
124 kobject_uevent(&p->kobj, KOBJ_ADD);
128 * remove a pktcdvd kernel object.
130 static void pkt_kobj_remove(struct pktcdvd_kobj *p)
133 kobject_put(&p->kobj);
136 * default release function for pktcdvd kernel objects.
138 static void pkt_kobj_release(struct kobject *kobj)
140 kfree(to_pktcdvdkobj(kobj));
144 /**********************************************************
146 * sysfs interface for pktcdvd
147 * by (C) 2006 Thomas Maier <balagi@justmail.de>
149 **********************************************************/
151 #define DEF_ATTR(_obj,_name,_mode) \
152 static struct attribute _obj = { .name = _name, .mode = _mode }
154 /**********************************************************
155 /sys/class/pktcdvd/pktcdvd[0-7]/
158 stat/packets_finished
163 write_queue/congestion_off
164 write_queue/congestion_on
165 **********************************************************/
167 DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
168 DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
169 DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
170 DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
171 DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
172 DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
174 static struct attribute *kobj_pkt_attrs_stat[] = {
184 DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
185 DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
186 DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
188 static struct attribute *kobj_pkt_attrs_wqueue[] = {
195 static ssize_t kobj_pkt_show(struct kobject *kobj,
196 struct attribute *attr, char *data)
198 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
201 if (strcmp(attr->name, "packets_started") == 0) {
202 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
204 } else if (strcmp(attr->name, "packets_finished") == 0) {
205 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
207 } else if (strcmp(attr->name, "kb_written") == 0) {
208 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
210 } else if (strcmp(attr->name, "kb_read") == 0) {
211 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
213 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
214 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
216 } else if (strcmp(attr->name, "size") == 0) {
217 spin_lock(&pd->lock);
218 v = pd->bio_queue_size;
219 spin_unlock(&pd->lock);
220 n = sprintf(data, "%d\n", v);
222 } else if (strcmp(attr->name, "congestion_off") == 0) {
223 spin_lock(&pd->lock);
224 v = pd->write_congestion_off;
225 spin_unlock(&pd->lock);
226 n = sprintf(data, "%d\n", v);
228 } else if (strcmp(attr->name, "congestion_on") == 0) {
229 spin_lock(&pd->lock);
230 v = pd->write_congestion_on;
231 spin_unlock(&pd->lock);
232 n = sprintf(data, "%d\n", v);
237 static void init_write_congestion_marks(int* lo, int* hi)
241 *hi = min(*hi, 1000000);
245 *lo = min(*lo, *hi - 100);
254 static ssize_t kobj_pkt_store(struct kobject *kobj,
255 struct attribute *attr,
256 const char *data, size_t len)
258 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
261 if (strcmp(attr->name, "reset") == 0 && len > 0) {
262 pd->stats.pkt_started = 0;
263 pd->stats.pkt_ended = 0;
264 pd->stats.secs_w = 0;
265 pd->stats.secs_rg = 0;
266 pd->stats.secs_r = 0;
268 } else if (strcmp(attr->name, "congestion_off") == 0
269 && sscanf(data, "%d", &val) == 1) {
270 spin_lock(&pd->lock);
271 pd->write_congestion_off = val;
272 init_write_congestion_marks(&pd->write_congestion_off,
273 &pd->write_congestion_on);
274 spin_unlock(&pd->lock);
276 } else if (strcmp(attr->name, "congestion_on") == 0
277 && sscanf(data, "%d", &val) == 1) {
278 spin_lock(&pd->lock);
279 pd->write_congestion_on = val;
280 init_write_congestion_marks(&pd->write_congestion_off,
281 &pd->write_congestion_on);
282 spin_unlock(&pd->lock);
287 static struct sysfs_ops kobj_pkt_ops = {
288 .show = kobj_pkt_show,
289 .store = kobj_pkt_store
291 static struct kobj_type kobj_pkt_type_stat = {
292 .release = pkt_kobj_release,
293 .sysfs_ops = &kobj_pkt_ops,
294 .default_attrs = kobj_pkt_attrs_stat
296 static struct kobj_type kobj_pkt_type_wqueue = {
297 .release = pkt_kobj_release,
298 .sysfs_ops = &kobj_pkt_ops,
299 .default_attrs = kobj_pkt_attrs_wqueue
302 static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
305 pd->dev = device_create(class_pktcdvd, NULL, pd->pkt_dev, "%s", pd->name);
310 pd->kobj_stat = pkt_kobj_create(pd, "stat",
312 &kobj_pkt_type_stat);
313 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
315 &kobj_pkt_type_wqueue);
319 static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
321 pkt_kobj_remove(pd->kobj_stat);
322 pkt_kobj_remove(pd->kobj_wqueue);
324 device_destroy(class_pktcdvd, pd->pkt_dev);
328 /********************************************************************
331 remove unmap packet dev
332 device_map show mappings
333 *******************************************************************/
335 static void class_pktcdvd_release(struct class *cls)
339 static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
343 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
344 for (idx = 0; idx < MAX_WRITERS; idx++) {
345 struct pktcdvd_device *pd = pkt_devs[idx];
348 n += sprintf(data+n, "%s %u:%u %u:%u\n",
350 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
351 MAJOR(pd->bdev->bd_dev),
352 MINOR(pd->bdev->bd_dev));
354 mutex_unlock(&ctl_mutex);
358 static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
361 unsigned int major, minor;
363 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
364 /* pkt_setup_dev() expects caller to hold reference to self */
365 if (!try_module_get(THIS_MODULE))
368 pkt_setup_dev(MKDEV(major, minor), NULL);
370 module_put(THIS_MODULE);
378 static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
381 unsigned int major, minor;
382 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
383 pkt_remove_dev(MKDEV(major, minor));
389 static struct class_attribute class_pktcdvd_attrs[] = {
390 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
391 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
392 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
397 static int pkt_sysfs_init(void)
402 * create control files in sysfs
403 * /sys/class/pktcdvd/...
405 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
408 class_pktcdvd->name = DRIVER_NAME;
409 class_pktcdvd->owner = THIS_MODULE;
410 class_pktcdvd->class_release = class_pktcdvd_release;
411 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
412 ret = class_register(class_pktcdvd);
414 kfree(class_pktcdvd);
415 class_pktcdvd = NULL;
416 printk(DRIVER_NAME": failed to create class pktcdvd\n");
422 static void pkt_sysfs_cleanup(void)
425 class_destroy(class_pktcdvd);
426 class_pktcdvd = NULL;
429 /********************************************************************
432 /debugfs/pktcdvd[0-7]/
435 *******************************************************************/
437 static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
439 return pkt_seq_show(m, p);
442 static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
444 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
447 static const struct file_operations debug_fops = {
448 .open = pkt_debugfs_fops_open,
451 .release = single_release,
452 .owner = THIS_MODULE,
455 static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
457 if (!pkt_debugfs_root)
459 pd->dfs_f_info = NULL;
460 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
461 if (IS_ERR(pd->dfs_d_root)) {
462 pd->dfs_d_root = NULL;
465 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
466 pd->dfs_d_root, pd, &debug_fops);
467 if (IS_ERR(pd->dfs_f_info)) {
468 pd->dfs_f_info = NULL;
473 static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
475 if (!pkt_debugfs_root)
478 debugfs_remove(pd->dfs_f_info);
479 pd->dfs_f_info = NULL;
481 debugfs_remove(pd->dfs_d_root);
482 pd->dfs_d_root = NULL;
485 static void pkt_debugfs_init(void)
487 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
488 if (IS_ERR(pkt_debugfs_root)) {
489 pkt_debugfs_root = NULL;
494 static void pkt_debugfs_cleanup(void)
496 if (!pkt_debugfs_root)
498 debugfs_remove(pkt_debugfs_root);
499 pkt_debugfs_root = NULL;
502 /* ----------------------------------------------------------*/
505 static void pkt_bio_finished(struct pktcdvd_device *pd)
507 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
508 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
509 VPRINTK(DRIVER_NAME": queue empty\n");
510 atomic_set(&pd->iosched.attention, 1);
511 wake_up(&pd->wqueue);
515 static void pkt_bio_destructor(struct bio *bio)
517 kfree(bio->bi_io_vec);
521 static struct bio *pkt_bio_alloc(int nr_iovecs)
523 struct bio_vec *bvl = NULL;
526 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
531 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
535 bio->bi_max_vecs = nr_iovecs;
536 bio->bi_io_vec = bvl;
537 bio->bi_destructor = pkt_bio_destructor;
548 * Allocate a packet_data struct
550 static struct packet_data *pkt_alloc_packet_data(int frames)
553 struct packet_data *pkt;
555 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
559 pkt->frames = frames;
560 pkt->w_bio = pkt_bio_alloc(frames);
564 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
565 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
570 spin_lock_init(&pkt->lock);
572 for (i = 0; i < frames; i++) {
573 struct bio *bio = pkt_bio_alloc(1);
576 pkt->r_bios[i] = bio;
582 for (i = 0; i < frames; i++) {
583 struct bio *bio = pkt->r_bios[i];
589 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
591 __free_page(pkt->pages[i]);
600 * Free a packet_data struct
602 static void pkt_free_packet_data(struct packet_data *pkt)
606 for (i = 0; i < pkt->frames; i++) {
607 struct bio *bio = pkt->r_bios[i];
611 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
612 __free_page(pkt->pages[i]);
617 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
619 struct packet_data *pkt, *next;
621 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
623 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
624 pkt_free_packet_data(pkt);
626 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
629 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
631 struct packet_data *pkt;
633 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
635 while (nr_packets > 0) {
636 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
638 pkt_shrink_pktlist(pd);
641 pkt->id = nr_packets;
643 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
649 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
651 struct rb_node *n = rb_next(&node->rb_node);
654 return rb_entry(n, struct pkt_rb_node, rb_node);
657 static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
659 rb_erase(&node->rb_node, &pd->bio_queue);
660 mempool_free(node, pd->rb_pool);
661 pd->bio_queue_size--;
662 BUG_ON(pd->bio_queue_size < 0);
666 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
668 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
670 struct rb_node *n = pd->bio_queue.rb_node;
671 struct rb_node *next;
672 struct pkt_rb_node *tmp;
675 BUG_ON(pd->bio_queue_size > 0);
680 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
681 if (s <= tmp->bio->bi_sector)
690 if (s > tmp->bio->bi_sector) {
691 tmp = pkt_rbtree_next(tmp);
695 BUG_ON(s > tmp->bio->bi_sector);
700 * Insert a node into the pd->bio_queue rb tree.
702 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
704 struct rb_node **p = &pd->bio_queue.rb_node;
705 struct rb_node *parent = NULL;
706 sector_t s = node->bio->bi_sector;
707 struct pkt_rb_node *tmp;
711 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
712 if (s < tmp->bio->bi_sector)
717 rb_link_node(&node->rb_node, parent, p);
718 rb_insert_color(&node->rb_node, &pd->bio_queue);
719 pd->bio_queue_size++;
723 * Add a bio to a single linked list defined by its head and tail pointers.
725 static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
729 BUG_ON((*list_head) == NULL);
730 (*list_tail)->bi_next = bio;
733 BUG_ON((*list_head) != NULL);
740 * Remove and return the first bio from a single linked list defined by its
741 * head and tail pointers.
743 static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
747 if (*list_head == NULL)
751 *list_head = bio->bi_next;
752 if (*list_head == NULL)
760 * Send a packet_command to the underlying block device and
761 * wait for completion.
763 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
765 struct request_queue *q = bdev_get_queue(pd->bdev);
769 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
770 WRITE : READ, __GFP_WAIT);
773 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
777 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
778 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
779 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
780 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
783 rq->cmd_type = REQ_TYPE_BLOCK_PC;
784 rq->cmd_flags |= REQ_HARDBARRIER;
786 rq->cmd_flags |= REQ_QUIET;
788 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
797 * A generic sense dump / resolve mechanism should be implemented across
798 * all ATAPI + SCSI devices.
800 static void pkt_dump_sense(struct packet_command *cgc)
802 static char *info[9] = { "No sense", "Recovered error", "Not ready",
803 "Medium error", "Hardware error", "Illegal request",
804 "Unit attention", "Data protect", "Blank check" };
806 struct request_sense *sense = cgc->sense;
808 printk(DRIVER_NAME":");
809 for (i = 0; i < CDROM_PACKET_SIZE; i++)
810 printk(" %02x", cgc->cmd[i]);
814 printk("no sense\n");
818 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
820 if (sense->sense_key > 8) {
821 printk(" (INVALID)\n");
825 printk(" (%s)\n", info[sense->sense_key]);
829 * flush the drive cache to media
831 static int pkt_flush_cache(struct pktcdvd_device *pd)
833 struct packet_command cgc;
835 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
836 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
840 * the IMMED bit -- we default to not setting it, although that
841 * would allow a much faster close, this is safer
846 return pkt_generic_packet(pd, &cgc);
850 * speed is given as the normal factor, e.g. 4 for 4x
852 static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
853 unsigned write_speed, unsigned read_speed)
855 struct packet_command cgc;
856 struct request_sense sense;
859 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
861 cgc.cmd[0] = GPCMD_SET_SPEED;
862 cgc.cmd[2] = (read_speed >> 8) & 0xff;
863 cgc.cmd[3] = read_speed & 0xff;
864 cgc.cmd[4] = (write_speed >> 8) & 0xff;
865 cgc.cmd[5] = write_speed & 0xff;
867 if ((ret = pkt_generic_packet(pd, &cgc)))
868 pkt_dump_sense(&cgc);
874 * Queue a bio for processing by the low-level CD device. Must be called
875 * from process context.
877 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
879 spin_lock(&pd->iosched.lock);
880 if (bio_data_dir(bio) == READ) {
881 pkt_add_list_last(bio, &pd->iosched.read_queue,
882 &pd->iosched.read_queue_tail);
884 pkt_add_list_last(bio, &pd->iosched.write_queue,
885 &pd->iosched.write_queue_tail);
887 spin_unlock(&pd->iosched.lock);
889 atomic_set(&pd->iosched.attention, 1);
890 wake_up(&pd->wqueue);
894 * Process the queued read/write requests. This function handles special
895 * requirements for CDRW drives:
896 * - A cache flush command must be inserted before a read request if the
897 * previous request was a write.
898 * - Switching between reading and writing is slow, so don't do it more often
900 * - Optimize for throughput at the expense of latency. This means that streaming
901 * writes will never be interrupted by a read, but if the drive has to seek
902 * before the next write, switch to reading instead if there are any pending
904 * - Set the read speed according to current usage pattern. When only reading
905 * from the device, it's best to use the highest possible read speed, but
906 * when switching often between reading and writing, it's better to have the
907 * same read and write speeds.
909 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
912 if (atomic_read(&pd->iosched.attention) == 0)
914 atomic_set(&pd->iosched.attention, 0);
918 int reads_queued, writes_queued;
920 spin_lock(&pd->iosched.lock);
921 reads_queued = (pd->iosched.read_queue != NULL);
922 writes_queued = (pd->iosched.write_queue != NULL);
923 spin_unlock(&pd->iosched.lock);
925 if (!reads_queued && !writes_queued)
928 if (pd->iosched.writing) {
929 int need_write_seek = 1;
930 spin_lock(&pd->iosched.lock);
931 bio = pd->iosched.write_queue;
932 spin_unlock(&pd->iosched.lock);
933 if (bio && (bio->bi_sector == pd->iosched.last_write))
935 if (need_write_seek && reads_queued) {
936 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
937 VPRINTK(DRIVER_NAME": write, waiting\n");
941 pd->iosched.writing = 0;
944 if (!reads_queued && writes_queued) {
945 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
946 VPRINTK(DRIVER_NAME": read, waiting\n");
949 pd->iosched.writing = 1;
953 spin_lock(&pd->iosched.lock);
954 if (pd->iosched.writing) {
955 bio = pkt_get_list_first(&pd->iosched.write_queue,
956 &pd->iosched.write_queue_tail);
958 bio = pkt_get_list_first(&pd->iosched.read_queue,
959 &pd->iosched.read_queue_tail);
961 spin_unlock(&pd->iosched.lock);
966 if (bio_data_dir(bio) == READ)
967 pd->iosched.successive_reads += bio->bi_size >> 10;
969 pd->iosched.successive_reads = 0;
970 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
972 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
973 if (pd->read_speed == pd->write_speed) {
974 pd->read_speed = MAX_SPEED;
975 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
978 if (pd->read_speed != pd->write_speed) {
979 pd->read_speed = pd->write_speed;
980 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
984 atomic_inc(&pd->cdrw.pending_bios);
985 generic_make_request(bio);
990 * Special care is needed if the underlying block device has a small
991 * max_phys_segments value.
993 static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
995 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
997 * The cdrom device can handle one segment/frame
999 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
1001 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
1003 * We can handle this case at the expense of some extra memory
1004 * copies during write operations
1006 set_bit(PACKET_MERGE_SEGS, &pd->flags);
1009 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
1015 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1017 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
1019 unsigned int copy_size = CD_FRAMESIZE;
1021 while (copy_size > 0) {
1022 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
1023 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
1024 src_bvl->bv_offset + offs;
1025 void *vto = page_address(dst_page) + dst_offs;
1026 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
1029 memcpy(vto, vfrom, len);
1030 kunmap_atomic(vfrom, KM_USER0);
1040 * Copy all data for this packet to pkt->pages[], so that
1041 * a) The number of required segments for the write bio is minimized, which
1042 * is necessary for some scsi controllers.
1043 * b) The data can be used as cache to avoid read requests if we receive a
1044 * new write request for the same zone.
1046 static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
1050 /* Copy all data to pkt->pages[] */
1053 for (f = 0; f < pkt->frames; f++) {
1054 if (bvec[f].bv_page != pkt->pages[p]) {
1055 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
1056 void *vto = page_address(pkt->pages[p]) + offs;
1057 memcpy(vto, vfrom, CD_FRAMESIZE);
1058 kunmap_atomic(vfrom, KM_USER0);
1059 bvec[f].bv_page = pkt->pages[p];
1060 bvec[f].bv_offset = offs;
1062 BUG_ON(bvec[f].bv_offset != offs);
1064 offs += CD_FRAMESIZE;
1065 if (offs >= PAGE_SIZE) {
1072 static void pkt_end_io_read(struct bio *bio, int err)
1074 struct packet_data *pkt = bio->bi_private;
1075 struct pktcdvd_device *pd = pkt->pd;
1078 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1079 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1082 atomic_inc(&pkt->io_errors);
1083 if (atomic_dec_and_test(&pkt->io_wait)) {
1084 atomic_inc(&pkt->run_sm);
1085 wake_up(&pd->wqueue);
1087 pkt_bio_finished(pd);
1090 static void pkt_end_io_packet_write(struct bio *bio, int err)
1092 struct packet_data *pkt = bio->bi_private;
1093 struct pktcdvd_device *pd = pkt->pd;
1096 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1098 pd->stats.pkt_ended++;
1100 pkt_bio_finished(pd);
1101 atomic_dec(&pkt->io_wait);
1102 atomic_inc(&pkt->run_sm);
1103 wake_up(&pd->wqueue);
1107 * Schedule reads for the holes in a packet
1109 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1111 int frames_read = 0;
1114 char written[PACKET_MAX_SIZE];
1116 BUG_ON(!pkt->orig_bios);
1118 atomic_set(&pkt->io_wait, 0);
1119 atomic_set(&pkt->io_errors, 0);
1122 * Figure out which frames we need to read before we can write.
1124 memset(written, 0, sizeof(written));
1125 spin_lock(&pkt->lock);
1126 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1127 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1128 int num_frames = bio->bi_size / CD_FRAMESIZE;
1129 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
1130 BUG_ON(first_frame < 0);
1131 BUG_ON(first_frame + num_frames > pkt->frames);
1132 for (f = first_frame; f < first_frame + num_frames; f++)
1135 spin_unlock(&pkt->lock);
1137 if (pkt->cache_valid) {
1138 VPRINTK("pkt_gather_data: zone %llx cached\n",
1139 (unsigned long long)pkt->sector);
1144 * Schedule reads for missing parts of the packet.
1146 for (f = 0; f < pkt->frames; f++) {
1147 struct bio_vec *vec;
1152 bio = pkt->r_bios[f];
1153 vec = bio->bi_io_vec;
1155 bio->bi_max_vecs = 1;
1156 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1157 bio->bi_bdev = pd->bdev;
1158 bio->bi_end_io = pkt_end_io_read;
1159 bio->bi_private = pkt;
1160 bio->bi_io_vec = vec;
1161 bio->bi_destructor = pkt_bio_destructor;
1163 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1164 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1165 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1166 f, pkt->pages[p], offset);
1167 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1170 atomic_inc(&pkt->io_wait);
1172 pkt_queue_bio(pd, bio);
1177 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1178 frames_read, (unsigned long long)pkt->sector);
1179 pd->stats.pkt_started++;
1180 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
1184 * Find a packet matching zone, or the least recently used packet if
1185 * there is no match.
1187 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1189 struct packet_data *pkt;
1191 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1192 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1193 list_del_init(&pkt->list);
1194 if (pkt->sector != zone)
1195 pkt->cache_valid = 0;
1203 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1205 if (pkt->cache_valid) {
1206 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1208 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1213 * recover a failed write, query for relocation if possible
1215 * returns 1 if recovery is possible, or 0 if not
1218 static int pkt_start_recovery(struct packet_data *pkt)
1221 * FIXME. We need help from the file system to implement
1222 * recovery handling.
1226 struct request *rq = pkt->rq;
1227 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1228 struct block_device *pkt_bdev;
1229 struct super_block *sb = NULL;
1230 unsigned long old_block, new_block;
1231 sector_t new_sector;
1233 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1235 sb = get_super(pkt_bdev);
1242 if (!sb->s_op || !sb->s_op->relocate_blocks)
1245 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1246 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1249 new_sector = new_block * (CD_FRAMESIZE >> 9);
1250 pkt->sector = new_sector;
1252 pkt->bio->bi_sector = new_sector;
1253 pkt->bio->bi_next = NULL;
1254 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1255 pkt->bio->bi_idx = 0;
1257 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1258 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1259 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1260 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1261 BUG_ON(pkt->bio->bi_private != pkt);
1272 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1274 #if PACKET_DEBUG > 1
1275 static const char *state_name[] = {
1276 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1278 enum packet_data_state old_state = pkt->state;
1279 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1280 state_name[old_state], state_name[state]);
1286 * Scan the work queue to see if we can start a new packet.
1287 * returns non-zero if any work was done.
1289 static int pkt_handle_queue(struct pktcdvd_device *pd)
1291 struct packet_data *pkt, *p;
1292 struct bio *bio = NULL;
1293 sector_t zone = 0; /* Suppress gcc warning */
1294 struct pkt_rb_node *node, *first_node;
1298 VPRINTK("handle_queue\n");
1300 atomic_set(&pd->scan_queue, 0);
1302 if (list_empty(&pd->cdrw.pkt_free_list)) {
1303 VPRINTK("handle_queue: no pkt\n");
1308 * Try to find a zone we are not already working on.
1310 spin_lock(&pd->lock);
1311 first_node = pkt_rbtree_find(pd, pd->current_sector);
1313 n = rb_first(&pd->bio_queue);
1315 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1320 zone = ZONE(bio->bi_sector, pd);
1321 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
1322 if (p->sector == zone) {
1329 node = pkt_rbtree_next(node);
1331 n = rb_first(&pd->bio_queue);
1333 node = rb_entry(n, struct pkt_rb_node, rb_node);
1335 if (node == first_node)
1338 spin_unlock(&pd->lock);
1340 VPRINTK("handle_queue: no bio\n");
1344 pkt = pkt_get_packet_data(pd, zone);
1346 pd->current_sector = zone + pd->settings.size;
1348 BUG_ON(pkt->frames != pd->settings.size >> 2);
1349 pkt->write_size = 0;
1352 * Scan work queue for bios in the same zone and link them
1355 spin_lock(&pd->lock);
1356 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1357 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1359 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1360 (unsigned long long)ZONE(bio->bi_sector, pd));
1361 if (ZONE(bio->bi_sector, pd) != zone)
1363 pkt_rbtree_erase(pd, node);
1364 spin_lock(&pkt->lock);
1365 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1366 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1367 spin_unlock(&pkt->lock);
1369 /* check write congestion marks, and if bio_queue_size is
1370 below, wake up any waiters */
1371 wakeup = (pd->write_congestion_on > 0
1372 && pd->bio_queue_size <= pd->write_congestion_off);
1373 spin_unlock(&pd->lock);
1375 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
1377 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1378 pkt_set_state(pkt, PACKET_WAITING_STATE);
1379 atomic_set(&pkt->run_sm, 1);
1381 spin_lock(&pd->cdrw.active_list_lock);
1382 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1383 spin_unlock(&pd->cdrw.active_list_lock);
1389 * Assemble a bio to write one packet and queue the bio for processing
1390 * by the underlying block device.
1392 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1397 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
1399 for (f = 0; f < pkt->frames; f++) {
1400 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1401 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1405 * Fill-in bvec with data from orig_bios.
1408 spin_lock(&pkt->lock);
1409 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1410 int segment = bio->bi_idx;
1412 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1413 int num_frames = bio->bi_size / CD_FRAMESIZE;
1414 BUG_ON(first_frame < 0);
1415 BUG_ON(first_frame + num_frames > pkt->frames);
1416 for (f = first_frame; f < first_frame + num_frames; f++) {
1417 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1419 while (src_offs >= src_bvl->bv_len) {
1420 src_offs -= src_bvl->bv_len;
1422 BUG_ON(segment >= bio->bi_vcnt);
1423 src_bvl = bio_iovec_idx(bio, segment);
1426 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1427 bvec[f].bv_page = src_bvl->bv_page;
1428 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1430 pkt_copy_bio_data(bio, segment, src_offs,
1431 bvec[f].bv_page, bvec[f].bv_offset);
1433 src_offs += CD_FRAMESIZE;
1437 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1438 spin_unlock(&pkt->lock);
1440 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1441 frames_write, (unsigned long long)pkt->sector);
1442 BUG_ON(frames_write != pkt->write_size);
1444 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1445 pkt_make_local_copy(pkt, bvec);
1446 pkt->cache_valid = 1;
1448 pkt->cache_valid = 0;
1451 /* Start the write request */
1452 bio_init(pkt->w_bio);
1453 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1454 pkt->w_bio->bi_sector = pkt->sector;
1455 pkt->w_bio->bi_bdev = pd->bdev;
1456 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1457 pkt->w_bio->bi_private = pkt;
1458 pkt->w_bio->bi_io_vec = bvec;
1459 pkt->w_bio->bi_destructor = pkt_bio_destructor;
1460 for (f = 0; f < pkt->frames; f++)
1461 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1463 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
1465 atomic_set(&pkt->io_wait, 1);
1466 pkt->w_bio->bi_rw = WRITE;
1467 pkt_queue_bio(pd, pkt->w_bio);
1470 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1472 struct bio *bio, *next;
1475 pkt->cache_valid = 0;
1477 /* Finish all bios corresponding to this packet */
1478 bio = pkt->orig_bios;
1480 next = bio->bi_next;
1481 bio->bi_next = NULL;
1482 bio_endio(bio, uptodate ? 0 : -EIO);
1485 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1488 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1492 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1495 switch (pkt->state) {
1496 case PACKET_WAITING_STATE:
1497 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1500 pkt->sleep_time = 0;
1501 pkt_gather_data(pd, pkt);
1502 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1505 case PACKET_READ_WAIT_STATE:
1506 if (atomic_read(&pkt->io_wait) > 0)
1509 if (atomic_read(&pkt->io_errors) > 0) {
1510 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1512 pkt_start_write(pd, pkt);
1516 case PACKET_WRITE_WAIT_STATE:
1517 if (atomic_read(&pkt->io_wait) > 0)
1520 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1521 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1523 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1527 case PACKET_RECOVERY_STATE:
1528 if (pkt_start_recovery(pkt)) {
1529 pkt_start_write(pd, pkt);
1531 VPRINTK("No recovery possible\n");
1532 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1536 case PACKET_FINISHED_STATE:
1537 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1538 pkt_finish_packet(pkt, uptodate);
1548 static void pkt_handle_packets(struct pktcdvd_device *pd)
1550 struct packet_data *pkt, *next;
1552 VPRINTK("pkt_handle_packets\n");
1555 * Run state machine for active packets
1557 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1558 if (atomic_read(&pkt->run_sm) > 0) {
1559 atomic_set(&pkt->run_sm, 0);
1560 pkt_run_state_machine(pd, pkt);
1565 * Move no longer active packets to the free list
1567 spin_lock(&pd->cdrw.active_list_lock);
1568 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1569 if (pkt->state == PACKET_FINISHED_STATE) {
1570 list_del(&pkt->list);
1571 pkt_put_packet_data(pd, pkt);
1572 pkt_set_state(pkt, PACKET_IDLE_STATE);
1573 atomic_set(&pd->scan_queue, 1);
1576 spin_unlock(&pd->cdrw.active_list_lock);
1579 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1581 struct packet_data *pkt;
1584 for (i = 0; i < PACKET_NUM_STATES; i++)
1587 spin_lock(&pd->cdrw.active_list_lock);
1588 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1589 states[pkt->state]++;
1591 spin_unlock(&pd->cdrw.active_list_lock);
1595 * kcdrwd is woken up when writes have been queued for one of our
1596 * registered devices
1598 static int kcdrwd(void *foobar)
1600 struct pktcdvd_device *pd = foobar;
1601 struct packet_data *pkt;
1602 long min_sleep_time, residue;
1604 set_user_nice(current, -20);
1608 DECLARE_WAITQUEUE(wait, current);
1611 * Wait until there is something to do
1613 add_wait_queue(&pd->wqueue, &wait);
1615 set_current_state(TASK_INTERRUPTIBLE);
1617 /* Check if we need to run pkt_handle_queue */
1618 if (atomic_read(&pd->scan_queue) > 0)
1621 /* Check if we need to run the state machine for some packet */
1622 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1623 if (atomic_read(&pkt->run_sm) > 0)
1627 /* Check if we need to process the iosched queues */
1628 if (atomic_read(&pd->iosched.attention) != 0)
1631 /* Otherwise, go to sleep */
1632 if (PACKET_DEBUG > 1) {
1633 int states[PACKET_NUM_STATES];
1634 pkt_count_states(pd, states);
1635 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1636 states[0], states[1], states[2], states[3],
1637 states[4], states[5]);
1640 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1641 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1642 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1643 min_sleep_time = pkt->sleep_time;
1646 generic_unplug_device(bdev_get_queue(pd->bdev));
1648 VPRINTK("kcdrwd: sleeping\n");
1649 residue = schedule_timeout(min_sleep_time);
1650 VPRINTK("kcdrwd: wake up\n");
1652 /* make swsusp happy with our thread */
1655 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1656 if (!pkt->sleep_time)
1658 pkt->sleep_time -= min_sleep_time - residue;
1659 if (pkt->sleep_time <= 0) {
1660 pkt->sleep_time = 0;
1661 atomic_inc(&pkt->run_sm);
1665 if (kthread_should_stop())
1669 set_current_state(TASK_RUNNING);
1670 remove_wait_queue(&pd->wqueue, &wait);
1672 if (kthread_should_stop())
1676 * if pkt_handle_queue returns true, we can queue
1679 while (pkt_handle_queue(pd))
1683 * Handle packet state machine
1685 pkt_handle_packets(pd);
1688 * Handle iosched queues
1690 pkt_iosched_process_queue(pd);
1696 static void pkt_print_settings(struct pktcdvd_device *pd)
1698 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1699 printk("%u blocks, ", pd->settings.size >> 2);
1700 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1703 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1705 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1707 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1708 cgc->cmd[2] = page_code | (page_control << 6);
1709 cgc->cmd[7] = cgc->buflen >> 8;
1710 cgc->cmd[8] = cgc->buflen & 0xff;
1711 cgc->data_direction = CGC_DATA_READ;
1712 return pkt_generic_packet(pd, cgc);
1715 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1717 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1718 memset(cgc->buffer, 0, 2);
1719 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1720 cgc->cmd[1] = 0x10; /* PF */
1721 cgc->cmd[7] = cgc->buflen >> 8;
1722 cgc->cmd[8] = cgc->buflen & 0xff;
1723 cgc->data_direction = CGC_DATA_WRITE;
1724 return pkt_generic_packet(pd, cgc);
1727 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1729 struct packet_command cgc;
1732 /* set up command and get the disc info */
1733 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1734 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1735 cgc.cmd[8] = cgc.buflen = 2;
1738 if ((ret = pkt_generic_packet(pd, &cgc)))
1741 /* not all drives have the same disc_info length, so requeue
1742 * packet with the length the drive tells us it can supply
1744 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1745 sizeof(di->disc_information_length);
1747 if (cgc.buflen > sizeof(disc_information))
1748 cgc.buflen = sizeof(disc_information);
1750 cgc.cmd[8] = cgc.buflen;
1751 return pkt_generic_packet(pd, &cgc);
1754 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1756 struct packet_command cgc;
1759 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1760 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1761 cgc.cmd[1] = type & 3;
1762 cgc.cmd[4] = (track & 0xff00) >> 8;
1763 cgc.cmd[5] = track & 0xff;
1767 if ((ret = pkt_generic_packet(pd, &cgc)))
1770 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1771 sizeof(ti->track_information_length);
1773 if (cgc.buflen > sizeof(track_information))
1774 cgc.buflen = sizeof(track_information);
1776 cgc.cmd[8] = cgc.buflen;
1777 return pkt_generic_packet(pd, &cgc);
1780 static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1783 disc_information di;
1784 track_information ti;
1788 if ((ret = pkt_get_disc_info(pd, &di)))
1791 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1792 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1795 /* if this track is blank, try the previous. */
1798 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1802 /* if last recorded field is valid, return it. */
1804 *last_written = be32_to_cpu(ti.last_rec_address);
1806 /* make it up instead */
1807 *last_written = be32_to_cpu(ti.track_start) +
1808 be32_to_cpu(ti.track_size);
1810 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1816 * write mode select package based on pd->settings
1818 static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
1820 struct packet_command cgc;
1821 struct request_sense sense;
1822 write_param_page *wp;
1826 /* doesn't apply to DVD+RW or DVD-RAM */
1827 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1830 memset(buffer, 0, sizeof(buffer));
1831 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1833 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1834 pkt_dump_sense(&cgc);
1838 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1839 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1840 if (size > sizeof(buffer))
1841 size = sizeof(buffer);
1846 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1848 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1849 pkt_dump_sense(&cgc);
1854 * write page is offset header + block descriptor length
1856 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1858 wp->fp = pd->settings.fp;
1859 wp->track_mode = pd->settings.track_mode;
1860 wp->write_type = pd->settings.write_type;
1861 wp->data_block_type = pd->settings.block_mode;
1863 wp->multi_session = 0;
1865 #ifdef PACKET_USE_LS
1870 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1871 wp->session_format = 0;
1873 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1874 wp->session_format = 0x20;
1878 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1884 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
1887 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1889 cgc.buflen = cgc.cmd[8] = size;
1890 if ((ret = pkt_mode_select(pd, &cgc))) {
1891 pkt_dump_sense(&cgc);
1895 pkt_print_settings(pd);
1900 * 1 -- we can write to this track, 0 -- we can't
1902 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1904 switch (pd->mmc3_profile) {
1905 case 0x1a: /* DVD+RW */
1906 case 0x12: /* DVD-RAM */
1907 /* The track is always writable on DVD+RW/DVD-RAM */
1913 if (!ti->packet || !ti->fp)
1917 * "good" settings as per Mt Fuji.
1919 if (ti->rt == 0 && ti->blank == 0)
1922 if (ti->rt == 0 && ti->blank == 1)
1925 if (ti->rt == 1 && ti->blank == 0)
1928 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1933 * 1 -- we can write to this disc, 0 -- we can't
1935 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1937 switch (pd->mmc3_profile) {
1938 case 0x0a: /* CD-RW */
1939 case 0xffff: /* MMC3 not supported */
1941 case 0x1a: /* DVD+RW */
1942 case 0x13: /* DVD-RW */
1943 case 0x12: /* DVD-RAM */
1946 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
1951 * for disc type 0xff we should probably reserve a new track.
1952 * but i'm not sure, should we leave this to user apps? probably.
1954 if (di->disc_type == 0xff) {
1955 printk(DRIVER_NAME": Unknown disc. No track?\n");
1959 if (di->disc_type != 0x20 && di->disc_type != 0) {
1960 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
1964 if (di->erasable == 0) {
1965 printk(DRIVER_NAME": Disc not erasable\n");
1969 if (di->border_status == PACKET_SESSION_RESERVED) {
1970 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
1977 static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
1979 struct packet_command cgc;
1980 unsigned char buf[12];
1981 disc_information di;
1982 track_information ti;
1985 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1986 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1988 ret = pkt_generic_packet(pd, &cgc);
1989 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1991 memset(&di, 0, sizeof(disc_information));
1992 memset(&ti, 0, sizeof(track_information));
1994 if ((ret = pkt_get_disc_info(pd, &di))) {
1995 printk("failed get_disc\n");
1999 if (!pkt_writable_disc(pd, &di))
2002 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
2004 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
2005 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
2006 printk(DRIVER_NAME": failed get_track\n");
2010 if (!pkt_writable_track(pd, &ti)) {
2011 printk(DRIVER_NAME": can't write to this track\n");
2016 * we keep packet size in 512 byte units, makes it easier to
2017 * deal with request calculations.
2019 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2020 if (pd->settings.size == 0) {
2021 printk(DRIVER_NAME": detected zero packet size!\n");
2024 if (pd->settings.size > PACKET_MAX_SECTORS) {
2025 printk(DRIVER_NAME": packet size is too big\n");
2028 pd->settings.fp = ti.fp;
2029 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2032 pd->nwa = be32_to_cpu(ti.next_writable);
2033 set_bit(PACKET_NWA_VALID, &pd->flags);
2037 * in theory we could use lra on -RW media as well and just zero
2038 * blocks that haven't been written yet, but in practice that
2039 * is just a no-go. we'll use that for -R, naturally.
2042 pd->lra = be32_to_cpu(ti.last_rec_address);
2043 set_bit(PACKET_LRA_VALID, &pd->flags);
2045 pd->lra = 0xffffffff;
2046 set_bit(PACKET_LRA_VALID, &pd->flags);
2052 pd->settings.link_loss = 7;
2053 pd->settings.write_type = 0; /* packet */
2054 pd->settings.track_mode = ti.track_mode;
2057 * mode1 or mode2 disc
2059 switch (ti.data_mode) {
2061 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2064 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2067 printk(DRIVER_NAME": unknown data mode\n");
2074 * enable/disable write caching on drive
2076 static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2079 struct packet_command cgc;
2080 struct request_sense sense;
2081 unsigned char buf[64];
2084 memset(buf, 0, sizeof(buf));
2085 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2087 cgc.buflen = pd->mode_offset + 12;
2090 * caching mode page might not be there, so quiet this command
2094 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2097 buf[pd->mode_offset + 10] |= (!!set << 2);
2099 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2100 ret = pkt_mode_select(pd, &cgc);
2102 printk(DRIVER_NAME": write caching control failed\n");
2103 pkt_dump_sense(&cgc);
2104 } else if (!ret && set)
2105 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
2109 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2111 struct packet_command cgc;
2113 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2114 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2115 cgc.cmd[4] = lockflag ? 1 : 0;
2116 return pkt_generic_packet(pd, &cgc);
2120 * Returns drive maximum write speed
2122 static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2123 unsigned *write_speed)
2125 struct packet_command cgc;
2126 struct request_sense sense;
2127 unsigned char buf[256+18];
2128 unsigned char *cap_buf;
2131 memset(buf, 0, sizeof(buf));
2132 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2133 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2136 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2138 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2139 sizeof(struct mode_page_header);
2140 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2142 pkt_dump_sense(&cgc);
2147 offset = 20; /* Obsoleted field, used by older drives */
2148 if (cap_buf[1] >= 28)
2149 offset = 28; /* Current write speed selected */
2150 if (cap_buf[1] >= 30) {
2151 /* If the drive reports at least one "Logical Unit Write
2152 * Speed Performance Descriptor Block", use the information
2153 * in the first block. (contains the highest speed)
2155 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2160 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2164 /* These tables from cdrecord - I don't have orange book */
2165 /* standard speed CD-RW (1-4x) */
2166 static char clv_to_speed[16] = {
2167 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2168 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2170 /* high speed CD-RW (-10x) */
2171 static char hs_clv_to_speed[16] = {
2172 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2173 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2175 /* ultra high speed CD-RW */
2176 static char us_clv_to_speed[16] = {
2177 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2178 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2182 * reads the maximum media speed from ATIP
2184 static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2187 struct packet_command cgc;
2188 struct request_sense sense;
2189 unsigned char buf[64];
2190 unsigned int size, st, sp;
2193 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2195 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2197 cgc.cmd[2] = 4; /* READ ATIP */
2199 ret = pkt_generic_packet(pd, &cgc);
2201 pkt_dump_sense(&cgc);
2204 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2205 if (size > sizeof(buf))
2208 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2210 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2214 ret = pkt_generic_packet(pd, &cgc);
2216 pkt_dump_sense(&cgc);
2220 if (!(buf[6] & 0x40)) {
2221 printk(DRIVER_NAME": Disc type is not CD-RW\n");
2224 if (!(buf[6] & 0x4)) {
2225 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
2229 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2231 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2233 /* Info from cdrecord */
2235 case 0: /* standard speed */
2236 *speed = clv_to_speed[sp];
2238 case 1: /* high speed */
2239 *speed = hs_clv_to_speed[sp];
2241 case 2: /* ultra high speed */
2242 *speed = us_clv_to_speed[sp];
2245 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
2249 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
2252 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
2257 static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
2259 struct packet_command cgc;
2260 struct request_sense sense;
2263 VPRINTK(DRIVER_NAME": Performing OPC\n");
2265 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2267 cgc.timeout = 60*HZ;
2268 cgc.cmd[0] = GPCMD_SEND_OPC;
2270 if ((ret = pkt_generic_packet(pd, &cgc)))
2271 pkt_dump_sense(&cgc);
2275 static int pkt_open_write(struct pktcdvd_device *pd)
2278 unsigned int write_speed, media_write_speed, read_speed;
2280 if ((ret = pkt_probe_settings(pd))) {
2281 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
2285 if ((ret = pkt_set_write_settings(pd))) {
2286 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
2290 pkt_write_caching(pd, USE_WCACHING);
2292 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2293 write_speed = 16 * 177;
2294 switch (pd->mmc3_profile) {
2295 case 0x13: /* DVD-RW */
2296 case 0x1a: /* DVD+RW */
2297 case 0x12: /* DVD-RAM */
2298 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
2301 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2302 media_write_speed = 16;
2303 write_speed = min(write_speed, media_write_speed * 177);
2304 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
2307 read_speed = write_speed;
2309 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
2310 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
2313 pd->write_speed = write_speed;
2314 pd->read_speed = read_speed;
2316 if ((ret = pkt_perform_opc(pd))) {
2317 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
2324 * called at open time.
2326 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2330 struct request_queue *q;
2333 * We need to re-open the cdrom device without O_NONBLOCK to be able
2334 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2335 * so bdget() can't fail.
2337 bdget(pd->bdev->bd_dev);
2338 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2341 if ((ret = bd_claim(pd->bdev, pd)))
2344 if ((ret = pkt_get_last_written(pd, &lba))) {
2345 printk(DRIVER_NAME": pkt_get_last_written failed\n");
2349 set_capacity(pd->disk, lba << 2);
2350 set_capacity(pd->bdev->bd_disk, lba << 2);
2351 bd_set_size(pd->bdev, (loff_t)lba << 11);
2353 q = bdev_get_queue(pd->bdev);
2355 if ((ret = pkt_open_write(pd)))
2358 * Some CDRW drives can not handle writes larger than one packet,
2359 * even if the size is a multiple of the packet size.
2361 spin_lock_irq(q->queue_lock);
2362 blk_queue_max_sectors(q, pd->settings.size);
2363 spin_unlock_irq(q->queue_lock);
2364 set_bit(PACKET_WRITABLE, &pd->flags);
2366 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2367 clear_bit(PACKET_WRITABLE, &pd->flags);
2370 if ((ret = pkt_set_segment_merging(pd, q)))
2374 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2375 printk(DRIVER_NAME": not enough memory for buffers\n");
2379 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
2385 bd_release(pd->bdev);
2387 blkdev_put(pd->bdev);
2393 * called when the device is closed. makes sure that the device flushes
2394 * the internal cache before we close.
2396 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2398 if (flush && pkt_flush_cache(pd))
2399 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
2401 pkt_lock_door(pd, 0);
2403 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2404 bd_release(pd->bdev);
2405 blkdev_put(pd->bdev);
2407 pkt_shrink_pktlist(pd);
2410 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2412 if (dev_minor >= MAX_WRITERS)
2414 return pkt_devs[dev_minor];
2417 static int pkt_open(struct inode *inode, struct file *file)
2419 struct pktcdvd_device *pd = NULL;
2422 VPRINTK(DRIVER_NAME": entering open\n");
2424 mutex_lock(&ctl_mutex);
2425 pd = pkt_find_dev_from_minor(iminor(inode));
2430 BUG_ON(pd->refcnt < 0);
2433 if (pd->refcnt > 1) {
2434 if ((file->f_mode & FMODE_WRITE) &&
2435 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2440 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2444 * needed here as well, since ext2 (among others) may change
2445 * the blocksize at mount time
2447 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2450 mutex_unlock(&ctl_mutex);
2456 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
2457 mutex_unlock(&ctl_mutex);
2461 static int pkt_close(struct inode *inode, struct file *file)
2463 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2466 mutex_lock(&ctl_mutex);
2468 BUG_ON(pd->refcnt < 0);
2469 if (pd->refcnt == 0) {
2470 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2471 pkt_release_dev(pd, flush);
2473 mutex_unlock(&ctl_mutex);
2478 static void pkt_end_io_read_cloned(struct bio *bio, int err)
2480 struct packet_stacked_data *psd = bio->bi_private;
2481 struct pktcdvd_device *pd = psd->pd;
2484 bio_endio(psd->bio, err);
2485 mempool_free(psd, psd_pool);
2486 pkt_bio_finished(pd);
2489 static int pkt_make_request(struct request_queue *q, struct bio *bio)
2491 struct pktcdvd_device *pd;
2492 char b[BDEVNAME_SIZE];
2494 struct packet_data *pkt;
2495 int was_empty, blocked_bio;
2496 struct pkt_rb_node *node;
2500 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2505 * Clone READ bios so we can have our own bi_end_io callback.
2507 if (bio_data_dir(bio) == READ) {
2508 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2509 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2513 cloned_bio->bi_bdev = pd->bdev;
2514 cloned_bio->bi_private = psd;
2515 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2516 pd->stats.secs_r += bio->bi_size >> 9;
2517 pkt_queue_bio(pd, cloned_bio);
2521 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2522 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
2523 pd->name, (unsigned long long)bio->bi_sector);
2527 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2528 printk(DRIVER_NAME": wrong bio size\n");
2532 blk_queue_bounce(q, &bio);
2534 zone = ZONE(bio->bi_sector, pd);
2535 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2536 (unsigned long long)bio->bi_sector,
2537 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2539 /* Check if we have to split the bio */
2541 struct bio_pair *bp;
2545 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2546 if (last_zone != zone) {
2547 BUG_ON(last_zone != zone + pd->settings.size);
2548 first_sectors = last_zone - bio->bi_sector;
2549 bp = bio_split(bio, bio_split_pool, first_sectors);
2551 pkt_make_request(q, &bp->bio1);
2552 pkt_make_request(q, &bp->bio2);
2553 bio_pair_release(bp);
2559 * If we find a matching packet in state WAITING or READ_WAIT, we can
2560 * just append this bio to that packet.
2562 spin_lock(&pd->cdrw.active_list_lock);
2564 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2565 if (pkt->sector == zone) {
2566 spin_lock(&pkt->lock);
2567 if ((pkt->state == PACKET_WAITING_STATE) ||
2568 (pkt->state == PACKET_READ_WAIT_STATE)) {
2569 pkt_add_list_last(bio, &pkt->orig_bios,
2570 &pkt->orig_bios_tail);
2571 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2572 if ((pkt->write_size >= pkt->frames) &&
2573 (pkt->state == PACKET_WAITING_STATE)) {
2574 atomic_inc(&pkt->run_sm);
2575 wake_up(&pd->wqueue);
2577 spin_unlock(&pkt->lock);
2578 spin_unlock(&pd->cdrw.active_list_lock);
2583 spin_unlock(&pkt->lock);
2586 spin_unlock(&pd->cdrw.active_list_lock);
2589 * Test if there is enough room left in the bio work queue
2590 * (queue size >= congestion on mark).
2591 * If not, wait till the work queue size is below the congestion off mark.
2593 spin_lock(&pd->lock);
2594 if (pd->write_congestion_on > 0
2595 && pd->bio_queue_size >= pd->write_congestion_on) {
2596 set_bdi_congested(&q->backing_dev_info, WRITE);
2598 spin_unlock(&pd->lock);
2599 congestion_wait(WRITE, HZ);
2600 spin_lock(&pd->lock);
2601 } while(pd->bio_queue_size > pd->write_congestion_off);
2603 spin_unlock(&pd->lock);
2606 * No matching packet found. Store the bio in the work queue.
2608 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2610 spin_lock(&pd->lock);
2611 BUG_ON(pd->bio_queue_size < 0);
2612 was_empty = (pd->bio_queue_size == 0);
2613 pkt_rbtree_insert(pd, node);
2614 spin_unlock(&pd->lock);
2617 * Wake up the worker thread.
2619 atomic_set(&pd->scan_queue, 1);
2621 /* This wake_up is required for correct operation */
2622 wake_up(&pd->wqueue);
2623 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2625 * This wake up is not required for correct operation,
2626 * but improves performance in some cases.
2628 wake_up(&pd->wqueue);
2638 static int pkt_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
2640 struct pktcdvd_device *pd = q->queuedata;
2641 sector_t zone = ZONE(bio->bi_sector, pd);
2642 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2643 int remaining = (pd->settings.size << 9) - used;
2647 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2648 * boundary, pkt_make_request() will split the bio.
2650 remaining2 = PAGE_SIZE - bio->bi_size;
2651 remaining = max(remaining, remaining2);
2653 BUG_ON(remaining < 0);
2657 static void pkt_init_queue(struct pktcdvd_device *pd)
2659 struct request_queue *q = pd->disk->queue;
2661 blk_queue_make_request(q, pkt_make_request);
2662 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2663 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2664 blk_queue_merge_bvec(q, pkt_merge_bvec);
2668 static int pkt_seq_show(struct seq_file *m, void *p)
2670 struct pktcdvd_device *pd = m->private;
2672 char bdev_buf[BDEVNAME_SIZE];
2673 int states[PACKET_NUM_STATES];
2675 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2676 bdevname(pd->bdev, bdev_buf));
2678 seq_printf(m, "\nSettings:\n");
2679 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2681 if (pd->settings.write_type == 0)
2685 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2687 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2688 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2690 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2692 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2694 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2698 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2700 seq_printf(m, "\nStatistics:\n");
2701 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2702 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2703 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2704 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2705 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2707 seq_printf(m, "\nMisc:\n");
2708 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2709 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2710 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2711 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2712 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2713 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2715 seq_printf(m, "\nQueue state:\n");
2716 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2717 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2718 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2720 pkt_count_states(pd, states);
2721 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2722 states[0], states[1], states[2], states[3], states[4], states[5]);
2724 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2725 pd->write_congestion_off,
2726 pd->write_congestion_on);
2730 static int pkt_seq_open(struct inode *inode, struct file *file)
2732 return single_open(file, pkt_seq_show, PDE(inode)->data);
2735 static const struct file_operations pkt_proc_fops = {
2736 .open = pkt_seq_open,
2738 .llseek = seq_lseek,
2739 .release = single_release
2742 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2746 char b[BDEVNAME_SIZE];
2747 struct proc_dir_entry *proc;
2748 struct block_device *bdev;
2750 if (pd->pkt_dev == dev) {
2751 printk(DRIVER_NAME": Recursive setup not allowed\n");
2754 for (i = 0; i < MAX_WRITERS; i++) {
2755 struct pktcdvd_device *pd2 = pkt_devs[i];
2758 if (pd2->bdev->bd_dev == dev) {
2759 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
2762 if (pd2->pkt_dev == dev) {
2763 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
2771 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2775 /* This is safe, since we have a reference from open(). */
2776 __module_get(THIS_MODULE);
2779 set_blocksize(bdev, CD_FRAMESIZE);
2783 atomic_set(&pd->cdrw.pending_bios, 0);
2784 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2785 if (IS_ERR(pd->cdrw.thread)) {
2786 printk(DRIVER_NAME": can't start kernel thread\n");
2791 proc = create_proc_entry(pd->name, 0, pkt_proc);
2794 proc->proc_fops = &pkt_proc_fops;
2796 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2801 /* This is safe: open() is still holding a reference. */
2802 module_put(THIS_MODULE);
2806 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2808 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2810 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2814 * forward selected CDROM ioctls to CD-ROM, for UDF
2816 case CDROMMULTISESSION:
2817 case CDROMREADTOCENTRY:
2818 case CDROM_LAST_WRITTEN:
2819 case CDROM_SEND_PACKET:
2820 case SCSI_IOCTL_SEND_COMMAND:
2821 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2825 * The door gets locked when the device is opened, so we
2826 * have to unlock it or else the eject command fails.
2828 if (pd->refcnt == 1)
2829 pkt_lock_door(pd, 0);
2830 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2833 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
2840 static int pkt_media_changed(struct gendisk *disk)
2842 struct pktcdvd_device *pd = disk->private_data;
2843 struct gendisk *attached_disk;
2849 attached_disk = pd->bdev->bd_disk;
2852 return attached_disk->fops->media_changed(attached_disk);
2855 static struct block_device_operations pktcdvd_ops = {
2856 .owner = THIS_MODULE,
2858 .release = pkt_close,
2860 .media_changed = pkt_media_changed,
2864 * Set up mapping from pktcdvd device to CD-ROM device.
2866 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
2870 struct pktcdvd_device *pd;
2871 struct gendisk *disk;
2873 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2875 for (idx = 0; idx < MAX_WRITERS; idx++)
2878 if (idx == MAX_WRITERS) {
2879 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
2884 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2888 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2889 sizeof(struct pkt_rb_node));
2893 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2894 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2895 spin_lock_init(&pd->cdrw.active_list_lock);
2897 spin_lock_init(&pd->lock);
2898 spin_lock_init(&pd->iosched.lock);
2899 sprintf(pd->name, DRIVER_NAME"%d", idx);
2900 init_waitqueue_head(&pd->wqueue);
2901 pd->bio_queue = RB_ROOT;
2903 pd->write_congestion_on = write_congestion_on;
2904 pd->write_congestion_off = write_congestion_off;
2906 disk = alloc_disk(1);
2910 disk->major = pktdev_major;
2911 disk->first_minor = idx;
2912 disk->fops = &pktcdvd_ops;
2913 disk->flags = GENHD_FL_REMOVABLE;
2914 strcpy(disk->disk_name, pd->name);
2915 disk->private_data = pd;
2916 disk->queue = blk_alloc_queue(GFP_KERNEL);
2920 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2921 ret = pkt_new_dev(pd, dev);
2927 pkt_sysfs_dev_new(pd);
2928 pkt_debugfs_dev_new(pd);
2932 *pkt_dev = pd->pkt_dev;
2934 mutex_unlock(&ctl_mutex);
2938 blk_cleanup_queue(disk->queue);
2943 mempool_destroy(pd->rb_pool);
2946 mutex_unlock(&ctl_mutex);
2947 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
2952 * Tear down mapping from pktcdvd device to CD-ROM device.
2954 static int pkt_remove_dev(dev_t pkt_dev)
2956 struct pktcdvd_device *pd;
2960 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2962 for (idx = 0; idx < MAX_WRITERS; idx++) {
2964 if (pd && (pd->pkt_dev == pkt_dev))
2967 if (idx == MAX_WRITERS) {
2968 DPRINTK(DRIVER_NAME": dev not setup\n");
2973 if (pd->refcnt > 0) {
2977 if (!IS_ERR(pd->cdrw.thread))
2978 kthread_stop(pd->cdrw.thread);
2980 pkt_devs[idx] = NULL;
2982 pkt_debugfs_dev_remove(pd);
2983 pkt_sysfs_dev_remove(pd);
2985 blkdev_put(pd->bdev);
2987 remove_proc_entry(pd->name, pkt_proc);
2988 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
2990 del_gendisk(pd->disk);
2991 blk_cleanup_queue(pd->disk->queue);
2994 mempool_destroy(pd->rb_pool);
2997 /* This is safe: open() is still holding a reference. */
2998 module_put(THIS_MODULE);
3001 mutex_unlock(&ctl_mutex);
3005 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
3007 struct pktcdvd_device *pd;
3009 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3011 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
3013 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
3014 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
3017 ctrl_cmd->pkt_dev = 0;
3019 ctrl_cmd->num_devices = MAX_WRITERS;
3021 mutex_unlock(&ctl_mutex);
3024 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3026 void __user *argp = (void __user *)arg;
3027 struct pkt_ctrl_command ctrl_cmd;
3031 if (cmd != PACKET_CTRL_CMD)
3034 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3037 switch (ctrl_cmd.command) {
3038 case PKT_CTRL_CMD_SETUP:
3039 if (!capable(CAP_SYS_ADMIN))
3041 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3042 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
3044 case PKT_CTRL_CMD_TEARDOWN:
3045 if (!capable(CAP_SYS_ADMIN))
3047 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
3049 case PKT_CTRL_CMD_STATUS:
3050 pkt_get_status(&ctrl_cmd);
3056 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3062 static const struct file_operations pkt_ctl_fops = {
3063 .ioctl = pkt_ctl_ioctl,
3064 .owner = THIS_MODULE,
3067 static struct miscdevice pkt_misc = {
3068 .minor = MISC_DYNAMIC_MINOR,
3069 .name = DRIVER_NAME,
3070 .fops = &pkt_ctl_fops
3073 static int __init pkt_init(void)
3077 mutex_init(&ctl_mutex);
3079 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3080 sizeof(struct packet_stacked_data));
3084 ret = register_blkdev(pktdev_major, DRIVER_NAME);
3086 printk(DRIVER_NAME": Unable to register block device\n");
3092 ret = pkt_sysfs_init();
3098 ret = misc_register(&pkt_misc);
3100 printk(DRIVER_NAME": Unable to register misc device\n");
3104 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
3109 pkt_debugfs_cleanup();
3110 pkt_sysfs_cleanup();
3112 unregister_blkdev(pktdev_major, DRIVER_NAME);
3114 mempool_destroy(psd_pool);
3118 static void __exit pkt_exit(void)
3120 remove_proc_entry(DRIVER_NAME, proc_root_driver);
3121 misc_deregister(&pkt_misc);
3123 pkt_debugfs_cleanup();
3124 pkt_sysfs_cleanup();
3126 unregister_blkdev(pktdev_major, DRIVER_NAME);
3127 mempool_destroy(psd_pool);
3130 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3131 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3132 MODULE_LICENSE("GPL");
3134 module_init(pkt_init);
3135 module_exit(pkt_exit);