pcmcia: synclink_cs: fix information leak to userland
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / pktcdvd.c
blob37a2bb5950761fcfa6cd5ea16f30b5c74dc51a1e
1 /*
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
10 * DVD-RAM devices.
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/compat.h>
52 #include <linux/kthread.h>
53 #include <linux/errno.h>
54 #include <linux/spinlock.h>
55 #include <linux/file.h>
56 #include <linux/proc_fs.h>
57 #include <linux/seq_file.h>
58 #include <linux/miscdevice.h>
59 #include <linux/freezer.h>
60 #include <linux/smp_lock.h>
61 #include <linux/mutex.h>
62 #include <linux/slab.h>
63 #include <scsi/scsi_cmnd.h>
64 #include <scsi/scsi_ioctl.h>
65 #include <scsi/scsi.h>
66 #include <linux/debugfs.h>
67 #include <linux/device.h>
69 #include <asm/uaccess.h>
71 #define DRIVER_NAME "pktcdvd"
73 #if PACKET_DEBUG
74 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
75 #else
76 #define DPRINTK(fmt, args...)
77 #endif
79 #if PACKET_DEBUG > 1
80 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
81 #else
82 #define VPRINTK(fmt, args...)
83 #endif
85 #define MAX_SPEED 0xffff
87 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
89 static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
90 static struct proc_dir_entry *pkt_proc;
91 static int pktdev_major;
92 static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
93 static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
94 static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
95 static mempool_t *psd_pool;
97 static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
98 static struct dentry *pkt_debugfs_root = NULL; /* /sys/kernel/debug/pktcdvd */
100 /* forward declaration */
101 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
102 static int pkt_remove_dev(dev_t pkt_dev);
103 static int pkt_seq_show(struct seq_file *m, void *p);
108 * create and register a pktcdvd kernel object.
110 static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
111 const char* name,
112 struct kobject* parent,
113 struct kobj_type* ktype)
115 struct pktcdvd_kobj *p;
116 int error;
118 p = kzalloc(sizeof(*p), GFP_KERNEL);
119 if (!p)
120 return NULL;
121 p->pd = pd;
122 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
123 if (error) {
124 kobject_put(&p->kobj);
125 return NULL;
127 kobject_uevent(&p->kobj, KOBJ_ADD);
128 return p;
131 * remove a pktcdvd kernel object.
133 static void pkt_kobj_remove(struct pktcdvd_kobj *p)
135 if (p)
136 kobject_put(&p->kobj);
139 * default release function for pktcdvd kernel objects.
141 static void pkt_kobj_release(struct kobject *kobj)
143 kfree(to_pktcdvdkobj(kobj));
147 /**********************************************************
149 * sysfs interface for pktcdvd
150 * by (C) 2006 Thomas Maier <balagi@justmail.de>
152 **********************************************************/
154 #define DEF_ATTR(_obj,_name,_mode) \
155 static struct attribute _obj = { .name = _name, .mode = _mode }
157 /**********************************************************
158 /sys/class/pktcdvd/pktcdvd[0-7]/
159 stat/reset
160 stat/packets_started
161 stat/packets_finished
162 stat/kb_written
163 stat/kb_read
164 stat/kb_read_gather
165 write_queue/size
166 write_queue/congestion_off
167 write_queue/congestion_on
168 **********************************************************/
170 DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
171 DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
172 DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
173 DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
174 DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
175 DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
177 static struct attribute *kobj_pkt_attrs_stat[] = {
178 &kobj_pkt_attr_st1,
179 &kobj_pkt_attr_st2,
180 &kobj_pkt_attr_st3,
181 &kobj_pkt_attr_st4,
182 &kobj_pkt_attr_st5,
183 &kobj_pkt_attr_st6,
184 NULL
187 DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
188 DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
189 DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
191 static struct attribute *kobj_pkt_attrs_wqueue[] = {
192 &kobj_pkt_attr_wq1,
193 &kobj_pkt_attr_wq2,
194 &kobj_pkt_attr_wq3,
195 NULL
198 static ssize_t kobj_pkt_show(struct kobject *kobj,
199 struct attribute *attr, char *data)
201 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
202 int n = 0;
203 int v;
204 if (strcmp(attr->name, "packets_started") == 0) {
205 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
207 } else if (strcmp(attr->name, "packets_finished") == 0) {
208 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
210 } else if (strcmp(attr->name, "kb_written") == 0) {
211 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
213 } else if (strcmp(attr->name, "kb_read") == 0) {
214 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
216 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
217 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
219 } else if (strcmp(attr->name, "size") == 0) {
220 spin_lock(&pd->lock);
221 v = pd->bio_queue_size;
222 spin_unlock(&pd->lock);
223 n = sprintf(data, "%d\n", v);
225 } else if (strcmp(attr->name, "congestion_off") == 0) {
226 spin_lock(&pd->lock);
227 v = pd->write_congestion_off;
228 spin_unlock(&pd->lock);
229 n = sprintf(data, "%d\n", v);
231 } else if (strcmp(attr->name, "congestion_on") == 0) {
232 spin_lock(&pd->lock);
233 v = pd->write_congestion_on;
234 spin_unlock(&pd->lock);
235 n = sprintf(data, "%d\n", v);
237 return n;
240 static void init_write_congestion_marks(int* lo, int* hi)
242 if (*hi > 0) {
243 *hi = max(*hi, 500);
244 *hi = min(*hi, 1000000);
245 if (*lo <= 0)
246 *lo = *hi - 100;
247 else {
248 *lo = min(*lo, *hi - 100);
249 *lo = max(*lo, 100);
251 } else {
252 *hi = -1;
253 *lo = -1;
257 static ssize_t kobj_pkt_store(struct kobject *kobj,
258 struct attribute *attr,
259 const char *data, size_t len)
261 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
262 int val;
264 if (strcmp(attr->name, "reset") == 0 && len > 0) {
265 pd->stats.pkt_started = 0;
266 pd->stats.pkt_ended = 0;
267 pd->stats.secs_w = 0;
268 pd->stats.secs_rg = 0;
269 pd->stats.secs_r = 0;
271 } else if (strcmp(attr->name, "congestion_off") == 0
272 && sscanf(data, "%d", &val) == 1) {
273 spin_lock(&pd->lock);
274 pd->write_congestion_off = val;
275 init_write_congestion_marks(&pd->write_congestion_off,
276 &pd->write_congestion_on);
277 spin_unlock(&pd->lock);
279 } else if (strcmp(attr->name, "congestion_on") == 0
280 && sscanf(data, "%d", &val) == 1) {
281 spin_lock(&pd->lock);
282 pd->write_congestion_on = val;
283 init_write_congestion_marks(&pd->write_congestion_off,
284 &pd->write_congestion_on);
285 spin_unlock(&pd->lock);
287 return len;
290 static const struct sysfs_ops kobj_pkt_ops = {
291 .show = kobj_pkt_show,
292 .store = kobj_pkt_store
294 static struct kobj_type kobj_pkt_type_stat = {
295 .release = pkt_kobj_release,
296 .sysfs_ops = &kobj_pkt_ops,
297 .default_attrs = kobj_pkt_attrs_stat
299 static struct kobj_type kobj_pkt_type_wqueue = {
300 .release = pkt_kobj_release,
301 .sysfs_ops = &kobj_pkt_ops,
302 .default_attrs = kobj_pkt_attrs_wqueue
305 static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
307 if (class_pktcdvd) {
308 pd->dev = device_create(class_pktcdvd, NULL, MKDEV(0, 0), NULL,
309 "%s", pd->name);
310 if (IS_ERR(pd->dev))
311 pd->dev = NULL;
313 if (pd->dev) {
314 pd->kobj_stat = pkt_kobj_create(pd, "stat",
315 &pd->dev->kobj,
316 &kobj_pkt_type_stat);
317 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
318 &pd->dev->kobj,
319 &kobj_pkt_type_wqueue);
323 static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
325 pkt_kobj_remove(pd->kobj_stat);
326 pkt_kobj_remove(pd->kobj_wqueue);
327 if (class_pktcdvd)
328 device_unregister(pd->dev);
332 /********************************************************************
333 /sys/class/pktcdvd/
334 add map block device
335 remove unmap packet dev
336 device_map show mappings
337 *******************************************************************/
339 static void class_pktcdvd_release(struct class *cls)
341 kfree(cls);
343 static ssize_t class_pktcdvd_show_map(struct class *c,
344 struct class_attribute *attr,
345 char *data)
347 int n = 0;
348 int idx;
349 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
350 for (idx = 0; idx < MAX_WRITERS; idx++) {
351 struct pktcdvd_device *pd = pkt_devs[idx];
352 if (!pd)
353 continue;
354 n += sprintf(data+n, "%s %u:%u %u:%u\n",
355 pd->name,
356 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
357 MAJOR(pd->bdev->bd_dev),
358 MINOR(pd->bdev->bd_dev));
360 mutex_unlock(&ctl_mutex);
361 return n;
364 static ssize_t class_pktcdvd_store_add(struct class *c,
365 struct class_attribute *attr,
366 const char *buf,
367 size_t count)
369 unsigned int major, minor;
371 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
372 /* pkt_setup_dev() expects caller to hold reference to self */
373 if (!try_module_get(THIS_MODULE))
374 return -ENODEV;
376 pkt_setup_dev(MKDEV(major, minor), NULL);
378 module_put(THIS_MODULE);
380 return count;
383 return -EINVAL;
386 static ssize_t class_pktcdvd_store_remove(struct class *c,
387 struct class_attribute *attr,
388 const char *buf,
389 size_t count)
391 unsigned int major, minor;
392 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
393 pkt_remove_dev(MKDEV(major, minor));
394 return count;
396 return -EINVAL;
399 static struct class_attribute class_pktcdvd_attrs[] = {
400 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
401 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
402 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
403 __ATTR_NULL
407 static int pkt_sysfs_init(void)
409 int ret = 0;
412 * create control files in sysfs
413 * /sys/class/pktcdvd/...
415 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
416 if (!class_pktcdvd)
417 return -ENOMEM;
418 class_pktcdvd->name = DRIVER_NAME;
419 class_pktcdvd->owner = THIS_MODULE;
420 class_pktcdvd->class_release = class_pktcdvd_release;
421 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
422 ret = class_register(class_pktcdvd);
423 if (ret) {
424 kfree(class_pktcdvd);
425 class_pktcdvd = NULL;
426 printk(DRIVER_NAME": failed to create class pktcdvd\n");
427 return ret;
429 return 0;
432 static void pkt_sysfs_cleanup(void)
434 if (class_pktcdvd)
435 class_destroy(class_pktcdvd);
436 class_pktcdvd = NULL;
439 /********************************************************************
440 entries in debugfs
442 /sys/kernel/debug/pktcdvd[0-7]/
443 info
445 *******************************************************************/
447 static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
449 return pkt_seq_show(m, p);
452 static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
454 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
457 static const struct file_operations debug_fops = {
458 .open = pkt_debugfs_fops_open,
459 .read = seq_read,
460 .llseek = seq_lseek,
461 .release = single_release,
462 .owner = THIS_MODULE,
465 static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
467 if (!pkt_debugfs_root)
468 return;
469 pd->dfs_f_info = NULL;
470 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
471 if (IS_ERR(pd->dfs_d_root)) {
472 pd->dfs_d_root = NULL;
473 return;
475 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
476 pd->dfs_d_root, pd, &debug_fops);
477 if (IS_ERR(pd->dfs_f_info)) {
478 pd->dfs_f_info = NULL;
479 return;
483 static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
485 if (!pkt_debugfs_root)
486 return;
487 if (pd->dfs_f_info)
488 debugfs_remove(pd->dfs_f_info);
489 pd->dfs_f_info = NULL;
490 if (pd->dfs_d_root)
491 debugfs_remove(pd->dfs_d_root);
492 pd->dfs_d_root = NULL;
495 static void pkt_debugfs_init(void)
497 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
498 if (IS_ERR(pkt_debugfs_root)) {
499 pkt_debugfs_root = NULL;
500 return;
504 static void pkt_debugfs_cleanup(void)
506 if (!pkt_debugfs_root)
507 return;
508 debugfs_remove(pkt_debugfs_root);
509 pkt_debugfs_root = NULL;
512 /* ----------------------------------------------------------*/
515 static void pkt_bio_finished(struct pktcdvd_device *pd)
517 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
518 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
519 VPRINTK(DRIVER_NAME": queue empty\n");
520 atomic_set(&pd->iosched.attention, 1);
521 wake_up(&pd->wqueue);
525 static void pkt_bio_destructor(struct bio *bio)
527 kfree(bio->bi_io_vec);
528 kfree(bio);
531 static struct bio *pkt_bio_alloc(int nr_iovecs)
533 struct bio_vec *bvl = NULL;
534 struct bio *bio;
536 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
537 if (!bio)
538 goto no_bio;
539 bio_init(bio);
541 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
542 if (!bvl)
543 goto no_bvl;
545 bio->bi_max_vecs = nr_iovecs;
546 bio->bi_io_vec = bvl;
547 bio->bi_destructor = pkt_bio_destructor;
549 return bio;
551 no_bvl:
552 kfree(bio);
553 no_bio:
554 return NULL;
558 * Allocate a packet_data struct
560 static struct packet_data *pkt_alloc_packet_data(int frames)
562 int i;
563 struct packet_data *pkt;
565 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
566 if (!pkt)
567 goto no_pkt;
569 pkt->frames = frames;
570 pkt->w_bio = pkt_bio_alloc(frames);
571 if (!pkt->w_bio)
572 goto no_bio;
574 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
575 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
576 if (!pkt->pages[i])
577 goto no_page;
580 spin_lock_init(&pkt->lock);
581 bio_list_init(&pkt->orig_bios);
583 for (i = 0; i < frames; i++) {
584 struct bio *bio = pkt_bio_alloc(1);
585 if (!bio)
586 goto no_rd_bio;
587 pkt->r_bios[i] = bio;
590 return pkt;
592 no_rd_bio:
593 for (i = 0; i < frames; i++) {
594 struct bio *bio = pkt->r_bios[i];
595 if (bio)
596 bio_put(bio);
599 no_page:
600 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
601 if (pkt->pages[i])
602 __free_page(pkt->pages[i]);
603 bio_put(pkt->w_bio);
604 no_bio:
605 kfree(pkt);
606 no_pkt:
607 return NULL;
611 * Free a packet_data struct
613 static void pkt_free_packet_data(struct packet_data *pkt)
615 int i;
617 for (i = 0; i < pkt->frames; i++) {
618 struct bio *bio = pkt->r_bios[i];
619 if (bio)
620 bio_put(bio);
622 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
623 __free_page(pkt->pages[i]);
624 bio_put(pkt->w_bio);
625 kfree(pkt);
628 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
630 struct packet_data *pkt, *next;
632 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
634 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
635 pkt_free_packet_data(pkt);
637 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
640 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
642 struct packet_data *pkt;
644 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
646 while (nr_packets > 0) {
647 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
648 if (!pkt) {
649 pkt_shrink_pktlist(pd);
650 return 0;
652 pkt->id = nr_packets;
653 pkt->pd = pd;
654 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
655 nr_packets--;
657 return 1;
660 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
662 struct rb_node *n = rb_next(&node->rb_node);
663 if (!n)
664 return NULL;
665 return rb_entry(n, struct pkt_rb_node, rb_node);
668 static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
670 rb_erase(&node->rb_node, &pd->bio_queue);
671 mempool_free(node, pd->rb_pool);
672 pd->bio_queue_size--;
673 BUG_ON(pd->bio_queue_size < 0);
677 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
679 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
681 struct rb_node *n = pd->bio_queue.rb_node;
682 struct rb_node *next;
683 struct pkt_rb_node *tmp;
685 if (!n) {
686 BUG_ON(pd->bio_queue_size > 0);
687 return NULL;
690 for (;;) {
691 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
692 if (s <= tmp->bio->bi_sector)
693 next = n->rb_left;
694 else
695 next = n->rb_right;
696 if (!next)
697 break;
698 n = next;
701 if (s > tmp->bio->bi_sector) {
702 tmp = pkt_rbtree_next(tmp);
703 if (!tmp)
704 return NULL;
706 BUG_ON(s > tmp->bio->bi_sector);
707 return tmp;
711 * Insert a node into the pd->bio_queue rb tree.
713 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
715 struct rb_node **p = &pd->bio_queue.rb_node;
716 struct rb_node *parent = NULL;
717 sector_t s = node->bio->bi_sector;
718 struct pkt_rb_node *tmp;
720 while (*p) {
721 parent = *p;
722 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
723 if (s < tmp->bio->bi_sector)
724 p = &(*p)->rb_left;
725 else
726 p = &(*p)->rb_right;
728 rb_link_node(&node->rb_node, parent, p);
729 rb_insert_color(&node->rb_node, &pd->bio_queue);
730 pd->bio_queue_size++;
734 * Send a packet_command to the underlying block device and
735 * wait for completion.
737 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
739 struct request_queue *q = bdev_get_queue(pd->bdev);
740 struct request *rq;
741 int ret = 0;
743 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
744 WRITE : READ, __GFP_WAIT);
746 if (cgc->buflen) {
747 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
748 goto out;
751 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
752 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
754 rq->timeout = 60*HZ;
755 rq->cmd_type = REQ_TYPE_BLOCK_PC;
756 rq->cmd_flags |= REQ_HARDBARRIER;
757 if (cgc->quiet)
758 rq->cmd_flags |= REQ_QUIET;
760 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
761 if (rq->errors)
762 ret = -EIO;
763 out:
764 blk_put_request(rq);
765 return ret;
769 * A generic sense dump / resolve mechanism should be implemented across
770 * all ATAPI + SCSI devices.
772 static void pkt_dump_sense(struct packet_command *cgc)
774 static char *info[9] = { "No sense", "Recovered error", "Not ready",
775 "Medium error", "Hardware error", "Illegal request",
776 "Unit attention", "Data protect", "Blank check" };
777 int i;
778 struct request_sense *sense = cgc->sense;
780 printk(DRIVER_NAME":");
781 for (i = 0; i < CDROM_PACKET_SIZE; i++)
782 printk(" %02x", cgc->cmd[i]);
783 printk(" - ");
785 if (sense == NULL) {
786 printk("no sense\n");
787 return;
790 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
792 if (sense->sense_key > 8) {
793 printk(" (INVALID)\n");
794 return;
797 printk(" (%s)\n", info[sense->sense_key]);
801 * flush the drive cache to media
803 static int pkt_flush_cache(struct pktcdvd_device *pd)
805 struct packet_command cgc;
807 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
808 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
809 cgc.quiet = 1;
812 * the IMMED bit -- we default to not setting it, although that
813 * would allow a much faster close, this is safer
815 #if 0
816 cgc.cmd[1] = 1 << 1;
817 #endif
818 return pkt_generic_packet(pd, &cgc);
822 * speed is given as the normal factor, e.g. 4 for 4x
824 static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
825 unsigned write_speed, unsigned read_speed)
827 struct packet_command cgc;
828 struct request_sense sense;
829 int ret;
831 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
832 cgc.sense = &sense;
833 cgc.cmd[0] = GPCMD_SET_SPEED;
834 cgc.cmd[2] = (read_speed >> 8) & 0xff;
835 cgc.cmd[3] = read_speed & 0xff;
836 cgc.cmd[4] = (write_speed >> 8) & 0xff;
837 cgc.cmd[5] = write_speed & 0xff;
839 if ((ret = pkt_generic_packet(pd, &cgc)))
840 pkt_dump_sense(&cgc);
842 return ret;
846 * Queue a bio for processing by the low-level CD device. Must be called
847 * from process context.
849 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
851 spin_lock(&pd->iosched.lock);
852 if (bio_data_dir(bio) == READ)
853 bio_list_add(&pd->iosched.read_queue, bio);
854 else
855 bio_list_add(&pd->iosched.write_queue, bio);
856 spin_unlock(&pd->iosched.lock);
858 atomic_set(&pd->iosched.attention, 1);
859 wake_up(&pd->wqueue);
863 * Process the queued read/write requests. This function handles special
864 * requirements for CDRW drives:
865 * - A cache flush command must be inserted before a read request if the
866 * previous request was a write.
867 * - Switching between reading and writing is slow, so don't do it more often
868 * than necessary.
869 * - Optimize for throughput at the expense of latency. This means that streaming
870 * writes will never be interrupted by a read, but if the drive has to seek
871 * before the next write, switch to reading instead if there are any pending
872 * read requests.
873 * - Set the read speed according to current usage pattern. When only reading
874 * from the device, it's best to use the highest possible read speed, but
875 * when switching often between reading and writing, it's better to have the
876 * same read and write speeds.
878 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
881 if (atomic_read(&pd->iosched.attention) == 0)
882 return;
883 atomic_set(&pd->iosched.attention, 0);
885 for (;;) {
886 struct bio *bio;
887 int reads_queued, writes_queued;
889 spin_lock(&pd->iosched.lock);
890 reads_queued = !bio_list_empty(&pd->iosched.read_queue);
891 writes_queued = !bio_list_empty(&pd->iosched.write_queue);
892 spin_unlock(&pd->iosched.lock);
894 if (!reads_queued && !writes_queued)
895 break;
897 if (pd->iosched.writing) {
898 int need_write_seek = 1;
899 spin_lock(&pd->iosched.lock);
900 bio = bio_list_peek(&pd->iosched.write_queue);
901 spin_unlock(&pd->iosched.lock);
902 if (bio && (bio->bi_sector == pd->iosched.last_write))
903 need_write_seek = 0;
904 if (need_write_seek && reads_queued) {
905 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
906 VPRINTK(DRIVER_NAME": write, waiting\n");
907 break;
909 pkt_flush_cache(pd);
910 pd->iosched.writing = 0;
912 } else {
913 if (!reads_queued && writes_queued) {
914 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
915 VPRINTK(DRIVER_NAME": read, waiting\n");
916 break;
918 pd->iosched.writing = 1;
922 spin_lock(&pd->iosched.lock);
923 if (pd->iosched.writing)
924 bio = bio_list_pop(&pd->iosched.write_queue);
925 else
926 bio = bio_list_pop(&pd->iosched.read_queue);
927 spin_unlock(&pd->iosched.lock);
929 if (!bio)
930 continue;
932 if (bio_data_dir(bio) == READ)
933 pd->iosched.successive_reads += bio->bi_size >> 10;
934 else {
935 pd->iosched.successive_reads = 0;
936 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
938 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
939 if (pd->read_speed == pd->write_speed) {
940 pd->read_speed = MAX_SPEED;
941 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
943 } else {
944 if (pd->read_speed != pd->write_speed) {
945 pd->read_speed = pd->write_speed;
946 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
950 atomic_inc(&pd->cdrw.pending_bios);
951 generic_make_request(bio);
956 * Special care is needed if the underlying block device has a small
957 * max_phys_segments value.
959 static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
961 if ((pd->settings.size << 9) / CD_FRAMESIZE
962 <= queue_max_segments(q)) {
964 * The cdrom device can handle one segment/frame
966 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
967 return 0;
968 } else if ((pd->settings.size << 9) / PAGE_SIZE
969 <= queue_max_segments(q)) {
971 * We can handle this case at the expense of some extra memory
972 * copies during write operations
974 set_bit(PACKET_MERGE_SEGS, &pd->flags);
975 return 0;
976 } else {
977 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
978 return -EIO;
983 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
985 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
987 unsigned int copy_size = CD_FRAMESIZE;
989 while (copy_size > 0) {
990 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
991 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
992 src_bvl->bv_offset + offs;
993 void *vto = page_address(dst_page) + dst_offs;
994 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
996 BUG_ON(len < 0);
997 memcpy(vto, vfrom, len);
998 kunmap_atomic(vfrom, KM_USER0);
1000 seg++;
1001 offs = 0;
1002 dst_offs += len;
1003 copy_size -= len;
1008 * Copy all data for this packet to pkt->pages[], so that
1009 * a) The number of required segments for the write bio is minimized, which
1010 * is necessary for some scsi controllers.
1011 * b) The data can be used as cache to avoid read requests if we receive a
1012 * new write request for the same zone.
1014 static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
1016 int f, p, offs;
1018 /* Copy all data to pkt->pages[] */
1019 p = 0;
1020 offs = 0;
1021 for (f = 0; f < pkt->frames; f++) {
1022 if (bvec[f].bv_page != pkt->pages[p]) {
1023 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
1024 void *vto = page_address(pkt->pages[p]) + offs;
1025 memcpy(vto, vfrom, CD_FRAMESIZE);
1026 kunmap_atomic(vfrom, KM_USER0);
1027 bvec[f].bv_page = pkt->pages[p];
1028 bvec[f].bv_offset = offs;
1029 } else {
1030 BUG_ON(bvec[f].bv_offset != offs);
1032 offs += CD_FRAMESIZE;
1033 if (offs >= PAGE_SIZE) {
1034 offs = 0;
1035 p++;
1040 static void pkt_end_io_read(struct bio *bio, int err)
1042 struct packet_data *pkt = bio->bi_private;
1043 struct pktcdvd_device *pd = pkt->pd;
1044 BUG_ON(!pd);
1046 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1047 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1049 if (err)
1050 atomic_inc(&pkt->io_errors);
1051 if (atomic_dec_and_test(&pkt->io_wait)) {
1052 atomic_inc(&pkt->run_sm);
1053 wake_up(&pd->wqueue);
1055 pkt_bio_finished(pd);
1058 static void pkt_end_io_packet_write(struct bio *bio, int err)
1060 struct packet_data *pkt = bio->bi_private;
1061 struct pktcdvd_device *pd = pkt->pd;
1062 BUG_ON(!pd);
1064 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1066 pd->stats.pkt_ended++;
1068 pkt_bio_finished(pd);
1069 atomic_dec(&pkt->io_wait);
1070 atomic_inc(&pkt->run_sm);
1071 wake_up(&pd->wqueue);
1075 * Schedule reads for the holes in a packet
1077 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1079 int frames_read = 0;
1080 struct bio *bio;
1081 int f;
1082 char written[PACKET_MAX_SIZE];
1084 BUG_ON(bio_list_empty(&pkt->orig_bios));
1086 atomic_set(&pkt->io_wait, 0);
1087 atomic_set(&pkt->io_errors, 0);
1090 * Figure out which frames we need to read before we can write.
1092 memset(written, 0, sizeof(written));
1093 spin_lock(&pkt->lock);
1094 bio_list_for_each(bio, &pkt->orig_bios) {
1095 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1096 int num_frames = bio->bi_size / CD_FRAMESIZE;
1097 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
1098 BUG_ON(first_frame < 0);
1099 BUG_ON(first_frame + num_frames > pkt->frames);
1100 for (f = first_frame; f < first_frame + num_frames; f++)
1101 written[f] = 1;
1103 spin_unlock(&pkt->lock);
1105 if (pkt->cache_valid) {
1106 VPRINTK("pkt_gather_data: zone %llx cached\n",
1107 (unsigned long long)pkt->sector);
1108 goto out_account;
1112 * Schedule reads for missing parts of the packet.
1114 for (f = 0; f < pkt->frames; f++) {
1115 struct bio_vec *vec;
1117 int p, offset;
1118 if (written[f])
1119 continue;
1120 bio = pkt->r_bios[f];
1121 vec = bio->bi_io_vec;
1122 bio_init(bio);
1123 bio->bi_max_vecs = 1;
1124 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1125 bio->bi_bdev = pd->bdev;
1126 bio->bi_end_io = pkt_end_io_read;
1127 bio->bi_private = pkt;
1128 bio->bi_io_vec = vec;
1129 bio->bi_destructor = pkt_bio_destructor;
1131 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1132 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1133 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1134 f, pkt->pages[p], offset);
1135 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1136 BUG();
1138 atomic_inc(&pkt->io_wait);
1139 bio->bi_rw = READ;
1140 pkt_queue_bio(pd, bio);
1141 frames_read++;
1144 out_account:
1145 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1146 frames_read, (unsigned long long)pkt->sector);
1147 pd->stats.pkt_started++;
1148 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
1152 * Find a packet matching zone, or the least recently used packet if
1153 * there is no match.
1155 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1157 struct packet_data *pkt;
1159 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1160 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1161 list_del_init(&pkt->list);
1162 if (pkt->sector != zone)
1163 pkt->cache_valid = 0;
1164 return pkt;
1167 BUG();
1168 return NULL;
1171 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1173 if (pkt->cache_valid) {
1174 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1175 } else {
1176 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1181 * recover a failed write, query for relocation if possible
1183 * returns 1 if recovery is possible, or 0 if not
1186 static int pkt_start_recovery(struct packet_data *pkt)
1189 * FIXME. We need help from the file system to implement
1190 * recovery handling.
1192 return 0;
1193 #if 0
1194 struct request *rq = pkt->rq;
1195 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1196 struct block_device *pkt_bdev;
1197 struct super_block *sb = NULL;
1198 unsigned long old_block, new_block;
1199 sector_t new_sector;
1201 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1202 if (pkt_bdev) {
1203 sb = get_super(pkt_bdev);
1204 bdput(pkt_bdev);
1207 if (!sb)
1208 return 0;
1210 if (!sb->s_op || !sb->s_op->relocate_blocks)
1211 goto out;
1213 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1214 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1215 goto out;
1217 new_sector = new_block * (CD_FRAMESIZE >> 9);
1218 pkt->sector = new_sector;
1220 pkt->bio->bi_sector = new_sector;
1221 pkt->bio->bi_next = NULL;
1222 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1223 pkt->bio->bi_idx = 0;
1225 BUG_ON(pkt->bio->bi_rw != REQ_WRITE);
1226 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1227 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1228 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1229 BUG_ON(pkt->bio->bi_private != pkt);
1231 drop_super(sb);
1232 return 1;
1234 out:
1235 drop_super(sb);
1236 return 0;
1237 #endif
1240 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1242 #if PACKET_DEBUG > 1
1243 static const char *state_name[] = {
1244 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1246 enum packet_data_state old_state = pkt->state;
1247 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1248 state_name[old_state], state_name[state]);
1249 #endif
1250 pkt->state = state;
1254 * Scan the work queue to see if we can start a new packet.
1255 * returns non-zero if any work was done.
1257 static int pkt_handle_queue(struct pktcdvd_device *pd)
1259 struct packet_data *pkt, *p;
1260 struct bio *bio = NULL;
1261 sector_t zone = 0; /* Suppress gcc warning */
1262 struct pkt_rb_node *node, *first_node;
1263 struct rb_node *n;
1264 int wakeup;
1266 VPRINTK("handle_queue\n");
1268 atomic_set(&pd->scan_queue, 0);
1270 if (list_empty(&pd->cdrw.pkt_free_list)) {
1271 VPRINTK("handle_queue: no pkt\n");
1272 return 0;
1276 * Try to find a zone we are not already working on.
1278 spin_lock(&pd->lock);
1279 first_node = pkt_rbtree_find(pd, pd->current_sector);
1280 if (!first_node) {
1281 n = rb_first(&pd->bio_queue);
1282 if (n)
1283 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1285 node = first_node;
1286 while (node) {
1287 bio = node->bio;
1288 zone = ZONE(bio->bi_sector, pd);
1289 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
1290 if (p->sector == zone) {
1291 bio = NULL;
1292 goto try_next_bio;
1295 break;
1296 try_next_bio:
1297 node = pkt_rbtree_next(node);
1298 if (!node) {
1299 n = rb_first(&pd->bio_queue);
1300 if (n)
1301 node = rb_entry(n, struct pkt_rb_node, rb_node);
1303 if (node == first_node)
1304 node = NULL;
1306 spin_unlock(&pd->lock);
1307 if (!bio) {
1308 VPRINTK("handle_queue: no bio\n");
1309 return 0;
1312 pkt = pkt_get_packet_data(pd, zone);
1314 pd->current_sector = zone + pd->settings.size;
1315 pkt->sector = zone;
1316 BUG_ON(pkt->frames != pd->settings.size >> 2);
1317 pkt->write_size = 0;
1320 * Scan work queue for bios in the same zone and link them
1321 * to this packet.
1323 spin_lock(&pd->lock);
1324 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1325 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1326 bio = node->bio;
1327 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1328 (unsigned long long)ZONE(bio->bi_sector, pd));
1329 if (ZONE(bio->bi_sector, pd) != zone)
1330 break;
1331 pkt_rbtree_erase(pd, node);
1332 spin_lock(&pkt->lock);
1333 bio_list_add(&pkt->orig_bios, bio);
1334 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1335 spin_unlock(&pkt->lock);
1337 /* check write congestion marks, and if bio_queue_size is
1338 below, wake up any waiters */
1339 wakeup = (pd->write_congestion_on > 0
1340 && pd->bio_queue_size <= pd->write_congestion_off);
1341 spin_unlock(&pd->lock);
1342 if (wakeup) {
1343 clear_bdi_congested(&pd->disk->queue->backing_dev_info,
1344 BLK_RW_ASYNC);
1347 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1348 pkt_set_state(pkt, PACKET_WAITING_STATE);
1349 atomic_set(&pkt->run_sm, 1);
1351 spin_lock(&pd->cdrw.active_list_lock);
1352 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1353 spin_unlock(&pd->cdrw.active_list_lock);
1355 return 1;
1359 * Assemble a bio to write one packet and queue the bio for processing
1360 * by the underlying block device.
1362 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1364 struct bio *bio;
1365 int f;
1366 int frames_write;
1367 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
1369 for (f = 0; f < pkt->frames; f++) {
1370 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1371 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1375 * Fill-in bvec with data from orig_bios.
1377 frames_write = 0;
1378 spin_lock(&pkt->lock);
1379 bio_list_for_each(bio, &pkt->orig_bios) {
1380 int segment = bio->bi_idx;
1381 int src_offs = 0;
1382 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1383 int num_frames = bio->bi_size / CD_FRAMESIZE;
1384 BUG_ON(first_frame < 0);
1385 BUG_ON(first_frame + num_frames > pkt->frames);
1386 for (f = first_frame; f < first_frame + num_frames; f++) {
1387 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1389 while (src_offs >= src_bvl->bv_len) {
1390 src_offs -= src_bvl->bv_len;
1391 segment++;
1392 BUG_ON(segment >= bio->bi_vcnt);
1393 src_bvl = bio_iovec_idx(bio, segment);
1396 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1397 bvec[f].bv_page = src_bvl->bv_page;
1398 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1399 } else {
1400 pkt_copy_bio_data(bio, segment, src_offs,
1401 bvec[f].bv_page, bvec[f].bv_offset);
1403 src_offs += CD_FRAMESIZE;
1404 frames_write++;
1407 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1408 spin_unlock(&pkt->lock);
1410 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1411 frames_write, (unsigned long long)pkt->sector);
1412 BUG_ON(frames_write != pkt->write_size);
1414 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1415 pkt_make_local_copy(pkt, bvec);
1416 pkt->cache_valid = 1;
1417 } else {
1418 pkt->cache_valid = 0;
1421 /* Start the write request */
1422 bio_init(pkt->w_bio);
1423 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1424 pkt->w_bio->bi_sector = pkt->sector;
1425 pkt->w_bio->bi_bdev = pd->bdev;
1426 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1427 pkt->w_bio->bi_private = pkt;
1428 pkt->w_bio->bi_io_vec = bvec;
1429 pkt->w_bio->bi_destructor = pkt_bio_destructor;
1430 for (f = 0; f < pkt->frames; f++)
1431 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1432 BUG();
1433 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
1435 atomic_set(&pkt->io_wait, 1);
1436 pkt->w_bio->bi_rw = WRITE;
1437 pkt_queue_bio(pd, pkt->w_bio);
1440 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1442 struct bio *bio;
1444 if (!uptodate)
1445 pkt->cache_valid = 0;
1447 /* Finish all bios corresponding to this packet */
1448 while ((bio = bio_list_pop(&pkt->orig_bios)))
1449 bio_endio(bio, uptodate ? 0 : -EIO);
1452 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1454 int uptodate;
1456 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1458 for (;;) {
1459 switch (pkt->state) {
1460 case PACKET_WAITING_STATE:
1461 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1462 return;
1464 pkt->sleep_time = 0;
1465 pkt_gather_data(pd, pkt);
1466 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1467 break;
1469 case PACKET_READ_WAIT_STATE:
1470 if (atomic_read(&pkt->io_wait) > 0)
1471 return;
1473 if (atomic_read(&pkt->io_errors) > 0) {
1474 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1475 } else {
1476 pkt_start_write(pd, pkt);
1478 break;
1480 case PACKET_WRITE_WAIT_STATE:
1481 if (atomic_read(&pkt->io_wait) > 0)
1482 return;
1484 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1485 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1486 } else {
1487 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1489 break;
1491 case PACKET_RECOVERY_STATE:
1492 if (pkt_start_recovery(pkt)) {
1493 pkt_start_write(pd, pkt);
1494 } else {
1495 VPRINTK("No recovery possible\n");
1496 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1498 break;
1500 case PACKET_FINISHED_STATE:
1501 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1502 pkt_finish_packet(pkt, uptodate);
1503 return;
1505 default:
1506 BUG();
1507 break;
1512 static void pkt_handle_packets(struct pktcdvd_device *pd)
1514 struct packet_data *pkt, *next;
1516 VPRINTK("pkt_handle_packets\n");
1519 * Run state machine for active packets
1521 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1522 if (atomic_read(&pkt->run_sm) > 0) {
1523 atomic_set(&pkt->run_sm, 0);
1524 pkt_run_state_machine(pd, pkt);
1529 * Move no longer active packets to the free list
1531 spin_lock(&pd->cdrw.active_list_lock);
1532 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1533 if (pkt->state == PACKET_FINISHED_STATE) {
1534 list_del(&pkt->list);
1535 pkt_put_packet_data(pd, pkt);
1536 pkt_set_state(pkt, PACKET_IDLE_STATE);
1537 atomic_set(&pd->scan_queue, 1);
1540 spin_unlock(&pd->cdrw.active_list_lock);
1543 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1545 struct packet_data *pkt;
1546 int i;
1548 for (i = 0; i < PACKET_NUM_STATES; i++)
1549 states[i] = 0;
1551 spin_lock(&pd->cdrw.active_list_lock);
1552 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1553 states[pkt->state]++;
1555 spin_unlock(&pd->cdrw.active_list_lock);
1559 * kcdrwd is woken up when writes have been queued for one of our
1560 * registered devices
1562 static int kcdrwd(void *foobar)
1564 struct pktcdvd_device *pd = foobar;
1565 struct packet_data *pkt;
1566 long min_sleep_time, residue;
1568 set_user_nice(current, -20);
1569 set_freezable();
1571 for (;;) {
1572 DECLARE_WAITQUEUE(wait, current);
1575 * Wait until there is something to do
1577 add_wait_queue(&pd->wqueue, &wait);
1578 for (;;) {
1579 set_current_state(TASK_INTERRUPTIBLE);
1581 /* Check if we need to run pkt_handle_queue */
1582 if (atomic_read(&pd->scan_queue) > 0)
1583 goto work_to_do;
1585 /* Check if we need to run the state machine for some packet */
1586 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1587 if (atomic_read(&pkt->run_sm) > 0)
1588 goto work_to_do;
1591 /* Check if we need to process the iosched queues */
1592 if (atomic_read(&pd->iosched.attention) != 0)
1593 goto work_to_do;
1595 /* Otherwise, go to sleep */
1596 if (PACKET_DEBUG > 1) {
1597 int states[PACKET_NUM_STATES];
1598 pkt_count_states(pd, states);
1599 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1600 states[0], states[1], states[2], states[3],
1601 states[4], states[5]);
1604 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1605 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1606 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1607 min_sleep_time = pkt->sleep_time;
1610 generic_unplug_device(bdev_get_queue(pd->bdev));
1612 VPRINTK("kcdrwd: sleeping\n");
1613 residue = schedule_timeout(min_sleep_time);
1614 VPRINTK("kcdrwd: wake up\n");
1616 /* make swsusp happy with our thread */
1617 try_to_freeze();
1619 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1620 if (!pkt->sleep_time)
1621 continue;
1622 pkt->sleep_time -= min_sleep_time - residue;
1623 if (pkt->sleep_time <= 0) {
1624 pkt->sleep_time = 0;
1625 atomic_inc(&pkt->run_sm);
1629 if (kthread_should_stop())
1630 break;
1632 work_to_do:
1633 set_current_state(TASK_RUNNING);
1634 remove_wait_queue(&pd->wqueue, &wait);
1636 if (kthread_should_stop())
1637 break;
1640 * if pkt_handle_queue returns true, we can queue
1641 * another request.
1643 while (pkt_handle_queue(pd))
1647 * Handle packet state machine
1649 pkt_handle_packets(pd);
1652 * Handle iosched queues
1654 pkt_iosched_process_queue(pd);
1657 return 0;
1660 static void pkt_print_settings(struct pktcdvd_device *pd)
1662 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1663 printk("%u blocks, ", pd->settings.size >> 2);
1664 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1667 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1669 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1671 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1672 cgc->cmd[2] = page_code | (page_control << 6);
1673 cgc->cmd[7] = cgc->buflen >> 8;
1674 cgc->cmd[8] = cgc->buflen & 0xff;
1675 cgc->data_direction = CGC_DATA_READ;
1676 return pkt_generic_packet(pd, cgc);
1679 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1681 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1682 memset(cgc->buffer, 0, 2);
1683 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1684 cgc->cmd[1] = 0x10; /* PF */
1685 cgc->cmd[7] = cgc->buflen >> 8;
1686 cgc->cmd[8] = cgc->buflen & 0xff;
1687 cgc->data_direction = CGC_DATA_WRITE;
1688 return pkt_generic_packet(pd, cgc);
1691 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1693 struct packet_command cgc;
1694 int ret;
1696 /* set up command and get the disc info */
1697 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1698 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1699 cgc.cmd[8] = cgc.buflen = 2;
1700 cgc.quiet = 1;
1702 if ((ret = pkt_generic_packet(pd, &cgc)))
1703 return ret;
1705 /* not all drives have the same disc_info length, so requeue
1706 * packet with the length the drive tells us it can supply
1708 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1709 sizeof(di->disc_information_length);
1711 if (cgc.buflen > sizeof(disc_information))
1712 cgc.buflen = sizeof(disc_information);
1714 cgc.cmd[8] = cgc.buflen;
1715 return pkt_generic_packet(pd, &cgc);
1718 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1720 struct packet_command cgc;
1721 int ret;
1723 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1724 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1725 cgc.cmd[1] = type & 3;
1726 cgc.cmd[4] = (track & 0xff00) >> 8;
1727 cgc.cmd[5] = track & 0xff;
1728 cgc.cmd[8] = 8;
1729 cgc.quiet = 1;
1731 if ((ret = pkt_generic_packet(pd, &cgc)))
1732 return ret;
1734 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1735 sizeof(ti->track_information_length);
1737 if (cgc.buflen > sizeof(track_information))
1738 cgc.buflen = sizeof(track_information);
1740 cgc.cmd[8] = cgc.buflen;
1741 return pkt_generic_packet(pd, &cgc);
1744 static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1745 long *last_written)
1747 disc_information di;
1748 track_information ti;
1749 __u32 last_track;
1750 int ret = -1;
1752 if ((ret = pkt_get_disc_info(pd, &di)))
1753 return ret;
1755 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1756 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1757 return ret;
1759 /* if this track is blank, try the previous. */
1760 if (ti.blank) {
1761 last_track--;
1762 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1763 return ret;
1766 /* if last recorded field is valid, return it. */
1767 if (ti.lra_v) {
1768 *last_written = be32_to_cpu(ti.last_rec_address);
1769 } else {
1770 /* make it up instead */
1771 *last_written = be32_to_cpu(ti.track_start) +
1772 be32_to_cpu(ti.track_size);
1773 if (ti.free_blocks)
1774 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1776 return 0;
1780 * write mode select package based on pd->settings
1782 static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
1784 struct packet_command cgc;
1785 struct request_sense sense;
1786 write_param_page *wp;
1787 char buffer[128];
1788 int ret, size;
1790 /* doesn't apply to DVD+RW or DVD-RAM */
1791 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1792 return 0;
1794 memset(buffer, 0, sizeof(buffer));
1795 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1796 cgc.sense = &sense;
1797 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1798 pkt_dump_sense(&cgc);
1799 return ret;
1802 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1803 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1804 if (size > sizeof(buffer))
1805 size = sizeof(buffer);
1808 * now get it all
1810 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1811 cgc.sense = &sense;
1812 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1813 pkt_dump_sense(&cgc);
1814 return ret;
1818 * write page is offset header + block descriptor length
1820 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1822 wp->fp = pd->settings.fp;
1823 wp->track_mode = pd->settings.track_mode;
1824 wp->write_type = pd->settings.write_type;
1825 wp->data_block_type = pd->settings.block_mode;
1827 wp->multi_session = 0;
1829 #ifdef PACKET_USE_LS
1830 wp->link_size = 7;
1831 wp->ls_v = 1;
1832 #endif
1834 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1835 wp->session_format = 0;
1836 wp->subhdr2 = 0x20;
1837 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1838 wp->session_format = 0x20;
1839 wp->subhdr2 = 8;
1840 #if 0
1841 wp->mcn[0] = 0x80;
1842 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1843 #endif
1844 } else {
1846 * paranoia
1848 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
1849 return 1;
1851 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1853 cgc.buflen = cgc.cmd[8] = size;
1854 if ((ret = pkt_mode_select(pd, &cgc))) {
1855 pkt_dump_sense(&cgc);
1856 return ret;
1859 pkt_print_settings(pd);
1860 return 0;
1864 * 1 -- we can write to this track, 0 -- we can't
1866 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1868 switch (pd->mmc3_profile) {
1869 case 0x1a: /* DVD+RW */
1870 case 0x12: /* DVD-RAM */
1871 /* The track is always writable on DVD+RW/DVD-RAM */
1872 return 1;
1873 default:
1874 break;
1877 if (!ti->packet || !ti->fp)
1878 return 0;
1881 * "good" settings as per Mt Fuji.
1883 if (ti->rt == 0 && ti->blank == 0)
1884 return 1;
1886 if (ti->rt == 0 && ti->blank == 1)
1887 return 1;
1889 if (ti->rt == 1 && ti->blank == 0)
1890 return 1;
1892 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1893 return 0;
1897 * 1 -- we can write to this disc, 0 -- we can't
1899 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1901 switch (pd->mmc3_profile) {
1902 case 0x0a: /* CD-RW */
1903 case 0xffff: /* MMC3 not supported */
1904 break;
1905 case 0x1a: /* DVD+RW */
1906 case 0x13: /* DVD-RW */
1907 case 0x12: /* DVD-RAM */
1908 return 1;
1909 default:
1910 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
1911 return 0;
1915 * for disc type 0xff we should probably reserve a new track.
1916 * but i'm not sure, should we leave this to user apps? probably.
1918 if (di->disc_type == 0xff) {
1919 printk(DRIVER_NAME": Unknown disc. No track?\n");
1920 return 0;
1923 if (di->disc_type != 0x20 && di->disc_type != 0) {
1924 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
1925 return 0;
1928 if (di->erasable == 0) {
1929 printk(DRIVER_NAME": Disc not erasable\n");
1930 return 0;
1933 if (di->border_status == PACKET_SESSION_RESERVED) {
1934 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
1935 return 0;
1938 return 1;
1941 static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
1943 struct packet_command cgc;
1944 unsigned char buf[12];
1945 disc_information di;
1946 track_information ti;
1947 int ret, track;
1949 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1950 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1951 cgc.cmd[8] = 8;
1952 ret = pkt_generic_packet(pd, &cgc);
1953 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1955 memset(&di, 0, sizeof(disc_information));
1956 memset(&ti, 0, sizeof(track_information));
1958 if ((ret = pkt_get_disc_info(pd, &di))) {
1959 printk("failed get_disc\n");
1960 return ret;
1963 if (!pkt_writable_disc(pd, &di))
1964 return -EROFS;
1966 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1968 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1969 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1970 printk(DRIVER_NAME": failed get_track\n");
1971 return ret;
1974 if (!pkt_writable_track(pd, &ti)) {
1975 printk(DRIVER_NAME": can't write to this track\n");
1976 return -EROFS;
1980 * we keep packet size in 512 byte units, makes it easier to
1981 * deal with request calculations.
1983 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1984 if (pd->settings.size == 0) {
1985 printk(DRIVER_NAME": detected zero packet size!\n");
1986 return -ENXIO;
1988 if (pd->settings.size > PACKET_MAX_SECTORS) {
1989 printk(DRIVER_NAME": packet size is too big\n");
1990 return -EROFS;
1992 pd->settings.fp = ti.fp;
1993 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1995 if (ti.nwa_v) {
1996 pd->nwa = be32_to_cpu(ti.next_writable);
1997 set_bit(PACKET_NWA_VALID, &pd->flags);
2001 * in theory we could use lra on -RW media as well and just zero
2002 * blocks that haven't been written yet, but in practice that
2003 * is just a no-go. we'll use that for -R, naturally.
2005 if (ti.lra_v) {
2006 pd->lra = be32_to_cpu(ti.last_rec_address);
2007 set_bit(PACKET_LRA_VALID, &pd->flags);
2008 } else {
2009 pd->lra = 0xffffffff;
2010 set_bit(PACKET_LRA_VALID, &pd->flags);
2014 * fine for now
2016 pd->settings.link_loss = 7;
2017 pd->settings.write_type = 0; /* packet */
2018 pd->settings.track_mode = ti.track_mode;
2021 * mode1 or mode2 disc
2023 switch (ti.data_mode) {
2024 case PACKET_MODE1:
2025 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2026 break;
2027 case PACKET_MODE2:
2028 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2029 break;
2030 default:
2031 printk(DRIVER_NAME": unknown data mode\n");
2032 return -EROFS;
2034 return 0;
2038 * enable/disable write caching on drive
2040 static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2041 int set)
2043 struct packet_command cgc;
2044 struct request_sense sense;
2045 unsigned char buf[64];
2046 int ret;
2048 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2049 cgc.sense = &sense;
2050 cgc.buflen = pd->mode_offset + 12;
2053 * caching mode page might not be there, so quiet this command
2055 cgc.quiet = 1;
2057 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2058 return ret;
2060 buf[pd->mode_offset + 10] |= (!!set << 2);
2062 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2063 ret = pkt_mode_select(pd, &cgc);
2064 if (ret) {
2065 printk(DRIVER_NAME": write caching control failed\n");
2066 pkt_dump_sense(&cgc);
2067 } else if (!ret && set)
2068 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
2069 return ret;
2072 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2074 struct packet_command cgc;
2076 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2077 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2078 cgc.cmd[4] = lockflag ? 1 : 0;
2079 return pkt_generic_packet(pd, &cgc);
2083 * Returns drive maximum write speed
2085 static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2086 unsigned *write_speed)
2088 struct packet_command cgc;
2089 struct request_sense sense;
2090 unsigned char buf[256+18];
2091 unsigned char *cap_buf;
2092 int ret, offset;
2094 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2095 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2096 cgc.sense = &sense;
2098 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2099 if (ret) {
2100 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2101 sizeof(struct mode_page_header);
2102 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2103 if (ret) {
2104 pkt_dump_sense(&cgc);
2105 return ret;
2109 offset = 20; /* Obsoleted field, used by older drives */
2110 if (cap_buf[1] >= 28)
2111 offset = 28; /* Current write speed selected */
2112 if (cap_buf[1] >= 30) {
2113 /* If the drive reports at least one "Logical Unit Write
2114 * Speed Performance Descriptor Block", use the information
2115 * in the first block. (contains the highest speed)
2117 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2118 if (num_spdb > 0)
2119 offset = 34;
2122 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2123 return 0;
2126 /* These tables from cdrecord - I don't have orange book */
2127 /* standard speed CD-RW (1-4x) */
2128 static char clv_to_speed[16] = {
2129 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2130 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2132 /* high speed CD-RW (-10x) */
2133 static char hs_clv_to_speed[16] = {
2134 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2135 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2137 /* ultra high speed CD-RW */
2138 static char us_clv_to_speed[16] = {
2139 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2140 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2144 * reads the maximum media speed from ATIP
2146 static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2147 unsigned *speed)
2149 struct packet_command cgc;
2150 struct request_sense sense;
2151 unsigned char buf[64];
2152 unsigned int size, st, sp;
2153 int ret;
2155 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2156 cgc.sense = &sense;
2157 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2158 cgc.cmd[1] = 2;
2159 cgc.cmd[2] = 4; /* READ ATIP */
2160 cgc.cmd[8] = 2;
2161 ret = pkt_generic_packet(pd, &cgc);
2162 if (ret) {
2163 pkt_dump_sense(&cgc);
2164 return ret;
2166 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2167 if (size > sizeof(buf))
2168 size = sizeof(buf);
2170 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2171 cgc.sense = &sense;
2172 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2173 cgc.cmd[1] = 2;
2174 cgc.cmd[2] = 4;
2175 cgc.cmd[8] = size;
2176 ret = pkt_generic_packet(pd, &cgc);
2177 if (ret) {
2178 pkt_dump_sense(&cgc);
2179 return ret;
2182 if (!(buf[6] & 0x40)) {
2183 printk(DRIVER_NAME": Disc type is not CD-RW\n");
2184 return 1;
2186 if (!(buf[6] & 0x4)) {
2187 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
2188 return 1;
2191 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2193 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2195 /* Info from cdrecord */
2196 switch (st) {
2197 case 0: /* standard speed */
2198 *speed = clv_to_speed[sp];
2199 break;
2200 case 1: /* high speed */
2201 *speed = hs_clv_to_speed[sp];
2202 break;
2203 case 2: /* ultra high speed */
2204 *speed = us_clv_to_speed[sp];
2205 break;
2206 default:
2207 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
2208 return 1;
2210 if (*speed) {
2211 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
2212 return 0;
2213 } else {
2214 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
2215 return 1;
2219 static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
2221 struct packet_command cgc;
2222 struct request_sense sense;
2223 int ret;
2225 VPRINTK(DRIVER_NAME": Performing OPC\n");
2227 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2228 cgc.sense = &sense;
2229 cgc.timeout = 60*HZ;
2230 cgc.cmd[0] = GPCMD_SEND_OPC;
2231 cgc.cmd[1] = 1;
2232 if ((ret = pkt_generic_packet(pd, &cgc)))
2233 pkt_dump_sense(&cgc);
2234 return ret;
2237 static int pkt_open_write(struct pktcdvd_device *pd)
2239 int ret;
2240 unsigned int write_speed, media_write_speed, read_speed;
2242 if ((ret = pkt_probe_settings(pd))) {
2243 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
2244 return ret;
2247 if ((ret = pkt_set_write_settings(pd))) {
2248 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
2249 return -EIO;
2252 pkt_write_caching(pd, USE_WCACHING);
2254 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2255 write_speed = 16 * 177;
2256 switch (pd->mmc3_profile) {
2257 case 0x13: /* DVD-RW */
2258 case 0x1a: /* DVD+RW */
2259 case 0x12: /* DVD-RAM */
2260 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
2261 break;
2262 default:
2263 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2264 media_write_speed = 16;
2265 write_speed = min(write_speed, media_write_speed * 177);
2266 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
2267 break;
2269 read_speed = write_speed;
2271 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
2272 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
2273 return -EIO;
2275 pd->write_speed = write_speed;
2276 pd->read_speed = read_speed;
2278 if ((ret = pkt_perform_opc(pd))) {
2279 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
2282 return 0;
2286 * called at open time.
2288 static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
2290 int ret;
2291 long lba;
2292 struct request_queue *q;
2295 * We need to re-open the cdrom device without O_NONBLOCK to be able
2296 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2297 * so bdget() can't fail.
2299 bdget(pd->bdev->bd_dev);
2300 if ((ret = blkdev_get(pd->bdev, FMODE_READ)))
2301 goto out;
2303 if ((ret = bd_claim(pd->bdev, pd)))
2304 goto out_putdev;
2306 if ((ret = pkt_get_last_written(pd, &lba))) {
2307 printk(DRIVER_NAME": pkt_get_last_written failed\n");
2308 goto out_unclaim;
2311 set_capacity(pd->disk, lba << 2);
2312 set_capacity(pd->bdev->bd_disk, lba << 2);
2313 bd_set_size(pd->bdev, (loff_t)lba << 11);
2315 q = bdev_get_queue(pd->bdev);
2316 if (write) {
2317 if ((ret = pkt_open_write(pd)))
2318 goto out_unclaim;
2320 * Some CDRW drives can not handle writes larger than one packet,
2321 * even if the size is a multiple of the packet size.
2323 spin_lock_irq(q->queue_lock);
2324 blk_queue_max_hw_sectors(q, pd->settings.size);
2325 spin_unlock_irq(q->queue_lock);
2326 set_bit(PACKET_WRITABLE, &pd->flags);
2327 } else {
2328 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2329 clear_bit(PACKET_WRITABLE, &pd->flags);
2332 if ((ret = pkt_set_segment_merging(pd, q)))
2333 goto out_unclaim;
2335 if (write) {
2336 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2337 printk(DRIVER_NAME": not enough memory for buffers\n");
2338 ret = -ENOMEM;
2339 goto out_unclaim;
2341 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
2344 return 0;
2346 out_unclaim:
2347 bd_release(pd->bdev);
2348 out_putdev:
2349 blkdev_put(pd->bdev, FMODE_READ);
2350 out:
2351 return ret;
2355 * called when the device is closed. makes sure that the device flushes
2356 * the internal cache before we close.
2358 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2360 if (flush && pkt_flush_cache(pd))
2361 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
2363 pkt_lock_door(pd, 0);
2365 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2366 bd_release(pd->bdev);
2367 blkdev_put(pd->bdev, FMODE_READ);
2369 pkt_shrink_pktlist(pd);
2372 static struct pktcdvd_device *pkt_find_dev_from_minor(unsigned int dev_minor)
2374 if (dev_minor >= MAX_WRITERS)
2375 return NULL;
2376 return pkt_devs[dev_minor];
2379 static int pkt_open(struct block_device *bdev, fmode_t mode)
2381 struct pktcdvd_device *pd = NULL;
2382 int ret;
2384 VPRINTK(DRIVER_NAME": entering open\n");
2386 lock_kernel();
2387 mutex_lock(&ctl_mutex);
2388 pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev));
2389 if (!pd) {
2390 ret = -ENODEV;
2391 goto out;
2393 BUG_ON(pd->refcnt < 0);
2395 pd->refcnt++;
2396 if (pd->refcnt > 1) {
2397 if ((mode & FMODE_WRITE) &&
2398 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2399 ret = -EBUSY;
2400 goto out_dec;
2402 } else {
2403 ret = pkt_open_dev(pd, mode & FMODE_WRITE);
2404 if (ret)
2405 goto out_dec;
2407 * needed here as well, since ext2 (among others) may change
2408 * the blocksize at mount time
2410 set_blocksize(bdev, CD_FRAMESIZE);
2413 mutex_unlock(&ctl_mutex);
2414 unlock_kernel();
2415 return 0;
2417 out_dec:
2418 pd->refcnt--;
2419 out:
2420 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
2421 mutex_unlock(&ctl_mutex);
2422 unlock_kernel();
2423 return ret;
2426 static int pkt_close(struct gendisk *disk, fmode_t mode)
2428 struct pktcdvd_device *pd = disk->private_data;
2429 int ret = 0;
2431 lock_kernel();
2432 mutex_lock(&ctl_mutex);
2433 pd->refcnt--;
2434 BUG_ON(pd->refcnt < 0);
2435 if (pd->refcnt == 0) {
2436 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2437 pkt_release_dev(pd, flush);
2439 mutex_unlock(&ctl_mutex);
2440 unlock_kernel();
2441 return ret;
2445 static void pkt_end_io_read_cloned(struct bio *bio, int err)
2447 struct packet_stacked_data *psd = bio->bi_private;
2448 struct pktcdvd_device *pd = psd->pd;
2450 bio_put(bio);
2451 bio_endio(psd->bio, err);
2452 mempool_free(psd, psd_pool);
2453 pkt_bio_finished(pd);
2456 static int pkt_make_request(struct request_queue *q, struct bio *bio)
2458 struct pktcdvd_device *pd;
2459 char b[BDEVNAME_SIZE];
2460 sector_t zone;
2461 struct packet_data *pkt;
2462 int was_empty, blocked_bio;
2463 struct pkt_rb_node *node;
2465 pd = q->queuedata;
2466 if (!pd) {
2467 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2468 goto end_io;
2472 * Clone READ bios so we can have our own bi_end_io callback.
2474 if (bio_data_dir(bio) == READ) {
2475 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2476 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2478 psd->pd = pd;
2479 psd->bio = bio;
2480 cloned_bio->bi_bdev = pd->bdev;
2481 cloned_bio->bi_private = psd;
2482 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2483 pd->stats.secs_r += bio->bi_size >> 9;
2484 pkt_queue_bio(pd, cloned_bio);
2485 return 0;
2488 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2489 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
2490 pd->name, (unsigned long long)bio->bi_sector);
2491 goto end_io;
2494 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2495 printk(DRIVER_NAME": wrong bio size\n");
2496 goto end_io;
2499 blk_queue_bounce(q, &bio);
2501 zone = ZONE(bio->bi_sector, pd);
2502 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2503 (unsigned long long)bio->bi_sector,
2504 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2506 /* Check if we have to split the bio */
2508 struct bio_pair *bp;
2509 sector_t last_zone;
2510 int first_sectors;
2512 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2513 if (last_zone != zone) {
2514 BUG_ON(last_zone != zone + pd->settings.size);
2515 first_sectors = last_zone - bio->bi_sector;
2516 bp = bio_split(bio, first_sectors);
2517 BUG_ON(!bp);
2518 pkt_make_request(q, &bp->bio1);
2519 pkt_make_request(q, &bp->bio2);
2520 bio_pair_release(bp);
2521 return 0;
2526 * If we find a matching packet in state WAITING or READ_WAIT, we can
2527 * just append this bio to that packet.
2529 spin_lock(&pd->cdrw.active_list_lock);
2530 blocked_bio = 0;
2531 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2532 if (pkt->sector == zone) {
2533 spin_lock(&pkt->lock);
2534 if ((pkt->state == PACKET_WAITING_STATE) ||
2535 (pkt->state == PACKET_READ_WAIT_STATE)) {
2536 bio_list_add(&pkt->orig_bios, bio);
2537 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2538 if ((pkt->write_size >= pkt->frames) &&
2539 (pkt->state == PACKET_WAITING_STATE)) {
2540 atomic_inc(&pkt->run_sm);
2541 wake_up(&pd->wqueue);
2543 spin_unlock(&pkt->lock);
2544 spin_unlock(&pd->cdrw.active_list_lock);
2545 return 0;
2546 } else {
2547 blocked_bio = 1;
2549 spin_unlock(&pkt->lock);
2552 spin_unlock(&pd->cdrw.active_list_lock);
2555 * Test if there is enough room left in the bio work queue
2556 * (queue size >= congestion on mark).
2557 * If not, wait till the work queue size is below the congestion off mark.
2559 spin_lock(&pd->lock);
2560 if (pd->write_congestion_on > 0
2561 && pd->bio_queue_size >= pd->write_congestion_on) {
2562 set_bdi_congested(&q->backing_dev_info, BLK_RW_ASYNC);
2563 do {
2564 spin_unlock(&pd->lock);
2565 congestion_wait(BLK_RW_ASYNC, HZ);
2566 spin_lock(&pd->lock);
2567 } while(pd->bio_queue_size > pd->write_congestion_off);
2569 spin_unlock(&pd->lock);
2572 * No matching packet found. Store the bio in the work queue.
2574 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2575 node->bio = bio;
2576 spin_lock(&pd->lock);
2577 BUG_ON(pd->bio_queue_size < 0);
2578 was_empty = (pd->bio_queue_size == 0);
2579 pkt_rbtree_insert(pd, node);
2580 spin_unlock(&pd->lock);
2583 * Wake up the worker thread.
2585 atomic_set(&pd->scan_queue, 1);
2586 if (was_empty) {
2587 /* This wake_up is required for correct operation */
2588 wake_up(&pd->wqueue);
2589 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2591 * This wake up is not required for correct operation,
2592 * but improves performance in some cases.
2594 wake_up(&pd->wqueue);
2596 return 0;
2597 end_io:
2598 bio_io_error(bio);
2599 return 0;
2604 static int pkt_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2605 struct bio_vec *bvec)
2607 struct pktcdvd_device *pd = q->queuedata;
2608 sector_t zone = ZONE(bmd->bi_sector, pd);
2609 int used = ((bmd->bi_sector - zone) << 9) + bmd->bi_size;
2610 int remaining = (pd->settings.size << 9) - used;
2611 int remaining2;
2614 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2615 * boundary, pkt_make_request() will split the bio.
2617 remaining2 = PAGE_SIZE - bmd->bi_size;
2618 remaining = max(remaining, remaining2);
2620 BUG_ON(remaining < 0);
2621 return remaining;
2624 static void pkt_init_queue(struct pktcdvd_device *pd)
2626 struct request_queue *q = pd->disk->queue;
2628 blk_queue_make_request(q, pkt_make_request);
2629 blk_queue_logical_block_size(q, CD_FRAMESIZE);
2630 blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS);
2631 blk_queue_merge_bvec(q, pkt_merge_bvec);
2632 q->queuedata = pd;
2635 static int pkt_seq_show(struct seq_file *m, void *p)
2637 struct pktcdvd_device *pd = m->private;
2638 char *msg;
2639 char bdev_buf[BDEVNAME_SIZE];
2640 int states[PACKET_NUM_STATES];
2642 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2643 bdevname(pd->bdev, bdev_buf));
2645 seq_printf(m, "\nSettings:\n");
2646 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2648 if (pd->settings.write_type == 0)
2649 msg = "Packet";
2650 else
2651 msg = "Unknown";
2652 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2654 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2655 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2657 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2659 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2660 msg = "Mode 1";
2661 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2662 msg = "Mode 2";
2663 else
2664 msg = "Unknown";
2665 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2667 seq_printf(m, "\nStatistics:\n");
2668 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2669 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2670 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2671 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2672 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2674 seq_printf(m, "\nMisc:\n");
2675 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2676 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2677 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2678 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2679 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2680 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2682 seq_printf(m, "\nQueue state:\n");
2683 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2684 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2685 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2687 pkt_count_states(pd, states);
2688 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2689 states[0], states[1], states[2], states[3], states[4], states[5]);
2691 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2692 pd->write_congestion_off,
2693 pd->write_congestion_on);
2694 return 0;
2697 static int pkt_seq_open(struct inode *inode, struct file *file)
2699 return single_open(file, pkt_seq_show, PDE(inode)->data);
2702 static const struct file_operations pkt_proc_fops = {
2703 .open = pkt_seq_open,
2704 .read = seq_read,
2705 .llseek = seq_lseek,
2706 .release = single_release
2709 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2711 int i;
2712 int ret = 0;
2713 char b[BDEVNAME_SIZE];
2714 struct block_device *bdev;
2716 if (pd->pkt_dev == dev) {
2717 printk(DRIVER_NAME": Recursive setup not allowed\n");
2718 return -EBUSY;
2720 for (i = 0; i < MAX_WRITERS; i++) {
2721 struct pktcdvd_device *pd2 = pkt_devs[i];
2722 if (!pd2)
2723 continue;
2724 if (pd2->bdev->bd_dev == dev) {
2725 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
2726 return -EBUSY;
2728 if (pd2->pkt_dev == dev) {
2729 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
2730 return -EBUSY;
2734 bdev = bdget(dev);
2735 if (!bdev)
2736 return -ENOMEM;
2737 ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY);
2738 if (ret)
2739 return ret;
2741 /* This is safe, since we have a reference from open(). */
2742 __module_get(THIS_MODULE);
2744 pd->bdev = bdev;
2745 set_blocksize(bdev, CD_FRAMESIZE);
2747 pkt_init_queue(pd);
2749 atomic_set(&pd->cdrw.pending_bios, 0);
2750 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2751 if (IS_ERR(pd->cdrw.thread)) {
2752 printk(DRIVER_NAME": can't start kernel thread\n");
2753 ret = -ENOMEM;
2754 goto out_mem;
2757 proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
2758 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2759 return 0;
2761 out_mem:
2762 blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
2763 /* This is safe: open() is still holding a reference. */
2764 module_put(THIS_MODULE);
2765 return ret;
2768 static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
2770 struct pktcdvd_device *pd = bdev->bd_disk->private_data;
2771 int ret;
2773 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd,
2774 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
2776 lock_kernel();
2777 switch (cmd) {
2778 case CDROMEJECT:
2780 * The door gets locked when the device is opened, so we
2781 * have to unlock it or else the eject command fails.
2783 if (pd->refcnt == 1)
2784 pkt_lock_door(pd, 0);
2785 /* fallthru */
2787 * forward selected CDROM ioctls to CD-ROM, for UDF
2789 case CDROMMULTISESSION:
2790 case CDROMREADTOCENTRY:
2791 case CDROM_LAST_WRITTEN:
2792 case CDROM_SEND_PACKET:
2793 case SCSI_IOCTL_SEND_COMMAND:
2794 ret = __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
2795 break;
2797 default:
2798 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
2799 ret = -ENOTTY;
2801 unlock_kernel();
2803 return ret;
2806 static int pkt_media_changed(struct gendisk *disk)
2808 struct pktcdvd_device *pd = disk->private_data;
2809 struct gendisk *attached_disk;
2811 if (!pd)
2812 return 0;
2813 if (!pd->bdev)
2814 return 0;
2815 attached_disk = pd->bdev->bd_disk;
2816 if (!attached_disk)
2817 return 0;
2818 return attached_disk->fops->media_changed(attached_disk);
2821 static const struct block_device_operations pktcdvd_ops = {
2822 .owner = THIS_MODULE,
2823 .open = pkt_open,
2824 .release = pkt_close,
2825 .ioctl = pkt_ioctl,
2826 .media_changed = pkt_media_changed,
2829 static char *pktcdvd_devnode(struct gendisk *gd, mode_t *mode)
2831 return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
2835 * Set up mapping from pktcdvd device to CD-ROM device.
2837 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
2839 int idx;
2840 int ret = -ENOMEM;
2841 struct pktcdvd_device *pd;
2842 struct gendisk *disk;
2844 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2846 for (idx = 0; idx < MAX_WRITERS; idx++)
2847 if (!pkt_devs[idx])
2848 break;
2849 if (idx == MAX_WRITERS) {
2850 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
2851 ret = -EBUSY;
2852 goto out_mutex;
2855 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2856 if (!pd)
2857 goto out_mutex;
2859 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2860 sizeof(struct pkt_rb_node));
2861 if (!pd->rb_pool)
2862 goto out_mem;
2864 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2865 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2866 spin_lock_init(&pd->cdrw.active_list_lock);
2868 spin_lock_init(&pd->lock);
2869 spin_lock_init(&pd->iosched.lock);
2870 bio_list_init(&pd->iosched.read_queue);
2871 bio_list_init(&pd->iosched.write_queue);
2872 sprintf(pd->name, DRIVER_NAME"%d", idx);
2873 init_waitqueue_head(&pd->wqueue);
2874 pd->bio_queue = RB_ROOT;
2876 pd->write_congestion_on = write_congestion_on;
2877 pd->write_congestion_off = write_congestion_off;
2879 disk = alloc_disk(1);
2880 if (!disk)
2881 goto out_mem;
2882 pd->disk = disk;
2883 disk->major = pktdev_major;
2884 disk->first_minor = idx;
2885 disk->fops = &pktcdvd_ops;
2886 disk->flags = GENHD_FL_REMOVABLE;
2887 strcpy(disk->disk_name, pd->name);
2888 disk->devnode = pktcdvd_devnode;
2889 disk->private_data = pd;
2890 disk->queue = blk_alloc_queue(GFP_KERNEL);
2891 if (!disk->queue)
2892 goto out_mem2;
2894 pd->pkt_dev = MKDEV(pktdev_major, idx);
2895 ret = pkt_new_dev(pd, dev);
2896 if (ret)
2897 goto out_new_dev;
2899 add_disk(disk);
2901 pkt_sysfs_dev_new(pd);
2902 pkt_debugfs_dev_new(pd);
2904 pkt_devs[idx] = pd;
2905 if (pkt_dev)
2906 *pkt_dev = pd->pkt_dev;
2908 mutex_unlock(&ctl_mutex);
2909 return 0;
2911 out_new_dev:
2912 blk_cleanup_queue(disk->queue);
2913 out_mem2:
2914 put_disk(disk);
2915 out_mem:
2916 if (pd->rb_pool)
2917 mempool_destroy(pd->rb_pool);
2918 kfree(pd);
2919 out_mutex:
2920 mutex_unlock(&ctl_mutex);
2921 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
2922 return ret;
2926 * Tear down mapping from pktcdvd device to CD-ROM device.
2928 static int pkt_remove_dev(dev_t pkt_dev)
2930 struct pktcdvd_device *pd;
2931 int idx;
2932 int ret = 0;
2934 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2936 for (idx = 0; idx < MAX_WRITERS; idx++) {
2937 pd = pkt_devs[idx];
2938 if (pd && (pd->pkt_dev == pkt_dev))
2939 break;
2941 if (idx == MAX_WRITERS) {
2942 DPRINTK(DRIVER_NAME": dev not setup\n");
2943 ret = -ENXIO;
2944 goto out;
2947 if (pd->refcnt > 0) {
2948 ret = -EBUSY;
2949 goto out;
2951 if (!IS_ERR(pd->cdrw.thread))
2952 kthread_stop(pd->cdrw.thread);
2954 pkt_devs[idx] = NULL;
2956 pkt_debugfs_dev_remove(pd);
2957 pkt_sysfs_dev_remove(pd);
2959 blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY);
2961 remove_proc_entry(pd->name, pkt_proc);
2962 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
2964 del_gendisk(pd->disk);
2965 blk_cleanup_queue(pd->disk->queue);
2966 put_disk(pd->disk);
2968 mempool_destroy(pd->rb_pool);
2969 kfree(pd);
2971 /* This is safe: open() is still holding a reference. */
2972 module_put(THIS_MODULE);
2974 out:
2975 mutex_unlock(&ctl_mutex);
2976 return ret;
2979 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2981 struct pktcdvd_device *pd;
2983 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2985 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2986 if (pd) {
2987 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2988 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2989 } else {
2990 ctrl_cmd->dev = 0;
2991 ctrl_cmd->pkt_dev = 0;
2993 ctrl_cmd->num_devices = MAX_WRITERS;
2995 mutex_unlock(&ctl_mutex);
2998 static long pkt_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3000 void __user *argp = (void __user *)arg;
3001 struct pkt_ctrl_command ctrl_cmd;
3002 int ret = 0;
3003 dev_t pkt_dev = 0;
3005 if (cmd != PACKET_CTRL_CMD)
3006 return -ENOTTY;
3008 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3009 return -EFAULT;
3011 switch (ctrl_cmd.command) {
3012 case PKT_CTRL_CMD_SETUP:
3013 if (!capable(CAP_SYS_ADMIN))
3014 return -EPERM;
3015 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3016 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
3017 break;
3018 case PKT_CTRL_CMD_TEARDOWN:
3019 if (!capable(CAP_SYS_ADMIN))
3020 return -EPERM;
3021 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
3022 break;
3023 case PKT_CTRL_CMD_STATUS:
3024 pkt_get_status(&ctrl_cmd);
3025 break;
3026 default:
3027 return -ENOTTY;
3030 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3031 return -EFAULT;
3032 return ret;
3035 #ifdef CONFIG_COMPAT
3036 static long pkt_ctl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3038 return pkt_ctl_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
3040 #endif
3042 static const struct file_operations pkt_ctl_fops = {
3043 .open = nonseekable_open,
3044 .unlocked_ioctl = pkt_ctl_ioctl,
3045 #ifdef CONFIG_COMPAT
3046 .compat_ioctl = pkt_ctl_compat_ioctl,
3047 #endif
3048 .owner = THIS_MODULE,
3051 static struct miscdevice pkt_misc = {
3052 .minor = MISC_DYNAMIC_MINOR,
3053 .name = DRIVER_NAME,
3054 .nodename = "pktcdvd/control",
3055 .fops = &pkt_ctl_fops
3058 static int __init pkt_init(void)
3060 int ret;
3062 mutex_init(&ctl_mutex);
3064 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3065 sizeof(struct packet_stacked_data));
3066 if (!psd_pool)
3067 return -ENOMEM;
3069 ret = register_blkdev(pktdev_major, DRIVER_NAME);
3070 if (ret < 0) {
3071 printk(DRIVER_NAME": Unable to register block device\n");
3072 goto out2;
3074 if (!pktdev_major)
3075 pktdev_major = ret;
3077 ret = pkt_sysfs_init();
3078 if (ret)
3079 goto out;
3081 pkt_debugfs_init();
3083 ret = misc_register(&pkt_misc);
3084 if (ret) {
3085 printk(DRIVER_NAME": Unable to register misc device\n");
3086 goto out_misc;
3089 pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
3091 return 0;
3093 out_misc:
3094 pkt_debugfs_cleanup();
3095 pkt_sysfs_cleanup();
3096 out:
3097 unregister_blkdev(pktdev_major, DRIVER_NAME);
3098 out2:
3099 mempool_destroy(psd_pool);
3100 return ret;
3103 static void __exit pkt_exit(void)
3105 remove_proc_entry("driver/"DRIVER_NAME, NULL);
3106 misc_deregister(&pkt_misc);
3108 pkt_debugfs_cleanup();
3109 pkt_sysfs_cleanup();
3111 unregister_blkdev(pktdev_major, DRIVER_NAME);
3112 mempool_destroy(psd_pool);
3115 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3116 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3117 MODULE_LICENSE("GPL");
3119 module_init(pkt_init);
3120 module_exit(pkt_exit);