Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / block / pktcdvd.c
blob740a4fa5e75f2746edea54a2f25f59103c5cdd90
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/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"
70 #if PACKET_DEBUG
71 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
72 #else
73 #define DPRINTK(fmt, args...)
74 #endif
76 #if PACKET_DEBUG > 1
77 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
78 #else
79 #define VPRINTK(fmt, args...)
80 #endif
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,
108 const char* name,
109 struct kobject* parent,
110 struct kobj_type* ktype)
112 struct pktcdvd_kobj *p;
113 int error;
115 p = kzalloc(sizeof(*p), GFP_KERNEL);
116 if (!p)
117 return NULL;
118 p->pd = pd;
119 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
120 if (error) {
121 kobject_put(&p->kobj);
122 return NULL;
124 kobject_uevent(&p->kobj, KOBJ_ADD);
125 return p;
128 * remove a pktcdvd kernel object.
130 static void pkt_kobj_remove(struct pktcdvd_kobj *p)
132 if (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]/
156 stat/reset
157 stat/packets_started
158 stat/packets_finished
159 stat/kb_written
160 stat/kb_read
161 stat/kb_read_gather
162 write_queue/size
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[] = {
175 &kobj_pkt_attr_st1,
176 &kobj_pkt_attr_st2,
177 &kobj_pkt_attr_st3,
178 &kobj_pkt_attr_st4,
179 &kobj_pkt_attr_st5,
180 &kobj_pkt_attr_st6,
181 NULL
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[] = {
189 &kobj_pkt_attr_wq1,
190 &kobj_pkt_attr_wq2,
191 &kobj_pkt_attr_wq3,
192 NULL
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;
199 int n = 0;
200 int v;
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);
234 return n;
237 static void init_write_congestion_marks(int* lo, int* hi)
239 if (*hi > 0) {
240 *hi = max(*hi, 500);
241 *hi = min(*hi, 1000000);
242 if (*lo <= 0)
243 *lo = *hi - 100;
244 else {
245 *lo = min(*lo, *hi - 100);
246 *lo = max(*lo, 100);
248 } else {
249 *hi = -1;
250 *lo = -1;
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;
259 int val;
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);
284 return len;
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)
304 if (class_pktcdvd) {
305 pd->dev = device_create(class_pktcdvd, NULL, pd->pkt_dev, "%s", pd->name);
306 if (IS_ERR(pd->dev))
307 pd->dev = NULL;
309 if (pd->dev) {
310 pd->kobj_stat = pkt_kobj_create(pd, "stat",
311 &pd->dev->kobj,
312 &kobj_pkt_type_stat);
313 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
314 &pd->dev->kobj,
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);
323 if (class_pktcdvd)
324 device_destroy(class_pktcdvd, pd->pkt_dev);
328 /********************************************************************
329 /sys/class/pktcdvd/
330 add map block device
331 remove unmap packet dev
332 device_map show mappings
333 *******************************************************************/
335 static void class_pktcdvd_release(struct class *cls)
337 kfree(cls);
339 static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
341 int n = 0;
342 int idx;
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];
346 if (!pd)
347 continue;
348 n += sprintf(data+n, "%s %u:%u %u:%u\n",
349 pd->name,
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);
355 return n;
358 static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
359 size_t count)
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))
366 return -ENODEV;
368 pkt_setup_dev(MKDEV(major, minor), NULL);
370 module_put(THIS_MODULE);
372 return count;
375 return -EINVAL;
378 static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
379 size_t count)
381 unsigned int major, minor;
382 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
383 pkt_remove_dev(MKDEV(major, minor));
384 return count;
386 return -EINVAL;
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),
393 __ATTR_NULL
397 static int pkt_sysfs_init(void)
399 int ret = 0;
402 * create control files in sysfs
403 * /sys/class/pktcdvd/...
405 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
406 if (!class_pktcdvd)
407 return -ENOMEM;
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);
413 if (ret) {
414 kfree(class_pktcdvd);
415 class_pktcdvd = NULL;
416 printk(DRIVER_NAME": failed to create class pktcdvd\n");
417 return ret;
419 return 0;
422 static void pkt_sysfs_cleanup(void)
424 if (class_pktcdvd)
425 class_destroy(class_pktcdvd);
426 class_pktcdvd = NULL;
429 /********************************************************************
430 entries in debugfs
432 /debugfs/pktcdvd[0-7]/
433 info
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,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = single_release,
452 .owner = THIS_MODULE,
455 static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
457 if (!pkt_debugfs_root)
458 return;
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;
463 return;
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;
469 return;
473 static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
475 if (!pkt_debugfs_root)
476 return;
477 if (pd->dfs_f_info)
478 debugfs_remove(pd->dfs_f_info);
479 pd->dfs_f_info = NULL;
480 if (pd->dfs_d_root)
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;
490 return;
494 static void pkt_debugfs_cleanup(void)
496 if (!pkt_debugfs_root)
497 return;
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);
518 kfree(bio);
521 static struct bio *pkt_bio_alloc(int nr_iovecs)
523 struct bio_vec *bvl = NULL;
524 struct bio *bio;
526 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
527 if (!bio)
528 goto no_bio;
529 bio_init(bio);
531 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
532 if (!bvl)
533 goto no_bvl;
535 bio->bi_max_vecs = nr_iovecs;
536 bio->bi_io_vec = bvl;
537 bio->bi_destructor = pkt_bio_destructor;
539 return bio;
541 no_bvl:
542 kfree(bio);
543 no_bio:
544 return NULL;
548 * Allocate a packet_data struct
550 static struct packet_data *pkt_alloc_packet_data(int frames)
552 int i;
553 struct packet_data *pkt;
555 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
556 if (!pkt)
557 goto no_pkt;
559 pkt->frames = frames;
560 pkt->w_bio = pkt_bio_alloc(frames);
561 if (!pkt->w_bio)
562 goto no_bio;
564 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
565 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
566 if (!pkt->pages[i])
567 goto no_page;
570 spin_lock_init(&pkt->lock);
572 for (i = 0; i < frames; i++) {
573 struct bio *bio = pkt_bio_alloc(1);
574 if (!bio)
575 goto no_rd_bio;
576 pkt->r_bios[i] = bio;
579 return pkt;
581 no_rd_bio:
582 for (i = 0; i < frames; i++) {
583 struct bio *bio = pkt->r_bios[i];
584 if (bio)
585 bio_put(bio);
588 no_page:
589 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
590 if (pkt->pages[i])
591 __free_page(pkt->pages[i]);
592 bio_put(pkt->w_bio);
593 no_bio:
594 kfree(pkt);
595 no_pkt:
596 return NULL;
600 * Free a packet_data struct
602 static void pkt_free_packet_data(struct packet_data *pkt)
604 int i;
606 for (i = 0; i < pkt->frames; i++) {
607 struct bio *bio = pkt->r_bios[i];
608 if (bio)
609 bio_put(bio);
611 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
612 __free_page(pkt->pages[i]);
613 bio_put(pkt->w_bio);
614 kfree(pkt);
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);
637 if (!pkt) {
638 pkt_shrink_pktlist(pd);
639 return 0;
641 pkt->id = nr_packets;
642 pkt->pd = pd;
643 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
644 nr_packets--;
646 return 1;
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);
652 if (!n)
653 return NULL;
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;
674 if (!n) {
675 BUG_ON(pd->bio_queue_size > 0);
676 return NULL;
679 for (;;) {
680 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
681 if (s <= tmp->bio->bi_sector)
682 next = n->rb_left;
683 else
684 next = n->rb_right;
685 if (!next)
686 break;
687 n = next;
690 if (s > tmp->bio->bi_sector) {
691 tmp = pkt_rbtree_next(tmp);
692 if (!tmp)
693 return NULL;
695 BUG_ON(s > tmp->bio->bi_sector);
696 return tmp;
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;
709 while (*p) {
710 parent = *p;
711 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
712 if (s < tmp->bio->bi_sector)
713 p = &(*p)->rb_left;
714 else
715 p = &(*p)->rb_right;
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)
727 bio->bi_next = NULL;
728 if (*list_tail) {
729 BUG_ON((*list_head) == NULL);
730 (*list_tail)->bi_next = bio;
731 (*list_tail) = bio;
732 } else {
733 BUG_ON((*list_head) != NULL);
734 (*list_head) = bio;
735 (*list_tail) = bio;
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)
745 struct bio *bio;
747 if (*list_head == NULL)
748 return NULL;
750 bio = *list_head;
751 *list_head = bio->bi_next;
752 if (*list_head == NULL)
753 *list_tail = NULL;
755 bio->bi_next = NULL;
756 return bio;
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);
766 struct request *rq;
767 int ret = 0;
769 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
770 WRITE : READ, __GFP_WAIT);
772 if (cgc->buflen) {
773 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
774 goto out;
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);
782 rq->timeout = 60*HZ;
783 rq->cmd_type = REQ_TYPE_BLOCK_PC;
784 rq->cmd_flags |= REQ_HARDBARRIER;
785 if (cgc->quiet)
786 rq->cmd_flags |= REQ_QUIET;
788 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
789 if (rq->errors)
790 ret = -EIO;
791 out:
792 blk_put_request(rq);
793 return ret;
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" };
805 int i;
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]);
811 printk(" - ");
813 if (sense == NULL) {
814 printk("no sense\n");
815 return;
818 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
820 if (sense->sense_key > 8) {
821 printk(" (INVALID)\n");
822 return;
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;
837 cgc.quiet = 1;
840 * the IMMED bit -- we default to not setting it, although that
841 * would allow a much faster close, this is safer
843 #if 0
844 cgc.cmd[1] = 1 << 1;
845 #endif
846 return pkt_generic_packet(pd, &cgc);
850 * speed is given as the normal factor, e.g. 4 for 4x
852 <<<<<<< HEAD:drivers/block/pktcdvd.c
853 static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
854 =======
855 static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
856 unsigned write_speed, unsigned read_speed)
857 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
859 struct packet_command cgc;
860 struct request_sense sense;
861 int ret;
863 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
864 cgc.sense = &sense;
865 cgc.cmd[0] = GPCMD_SET_SPEED;
866 cgc.cmd[2] = (read_speed >> 8) & 0xff;
867 cgc.cmd[3] = read_speed & 0xff;
868 cgc.cmd[4] = (write_speed >> 8) & 0xff;
869 cgc.cmd[5] = write_speed & 0xff;
871 if ((ret = pkt_generic_packet(pd, &cgc)))
872 pkt_dump_sense(&cgc);
874 return ret;
878 * Queue a bio for processing by the low-level CD device. Must be called
879 * from process context.
881 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
883 spin_lock(&pd->iosched.lock);
884 if (bio_data_dir(bio) == READ) {
885 pkt_add_list_last(bio, &pd->iosched.read_queue,
886 &pd->iosched.read_queue_tail);
887 } else {
888 pkt_add_list_last(bio, &pd->iosched.write_queue,
889 &pd->iosched.write_queue_tail);
891 spin_unlock(&pd->iosched.lock);
893 atomic_set(&pd->iosched.attention, 1);
894 wake_up(&pd->wqueue);
898 * Process the queued read/write requests. This function handles special
899 * requirements for CDRW drives:
900 * - A cache flush command must be inserted before a read request if the
901 * previous request was a write.
902 * - Switching between reading and writing is slow, so don't do it more often
903 * than necessary.
904 * - Optimize for throughput at the expense of latency. This means that streaming
905 * writes will never be interrupted by a read, but if the drive has to seek
906 * before the next write, switch to reading instead if there are any pending
907 * read requests.
908 * - Set the read speed according to current usage pattern. When only reading
909 * from the device, it's best to use the highest possible read speed, but
910 * when switching often between reading and writing, it's better to have the
911 * same read and write speeds.
913 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
916 if (atomic_read(&pd->iosched.attention) == 0)
917 return;
918 atomic_set(&pd->iosched.attention, 0);
920 for (;;) {
921 struct bio *bio;
922 int reads_queued, writes_queued;
924 spin_lock(&pd->iosched.lock);
925 reads_queued = (pd->iosched.read_queue != NULL);
926 writes_queued = (pd->iosched.write_queue != NULL);
927 spin_unlock(&pd->iosched.lock);
929 if (!reads_queued && !writes_queued)
930 break;
932 if (pd->iosched.writing) {
933 int need_write_seek = 1;
934 spin_lock(&pd->iosched.lock);
935 bio = pd->iosched.write_queue;
936 spin_unlock(&pd->iosched.lock);
937 if (bio && (bio->bi_sector == pd->iosched.last_write))
938 need_write_seek = 0;
939 if (need_write_seek && reads_queued) {
940 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
941 VPRINTK(DRIVER_NAME": write, waiting\n");
942 break;
944 pkt_flush_cache(pd);
945 pd->iosched.writing = 0;
947 } else {
948 if (!reads_queued && writes_queued) {
949 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
950 VPRINTK(DRIVER_NAME": read, waiting\n");
951 break;
953 pd->iosched.writing = 1;
957 spin_lock(&pd->iosched.lock);
958 if (pd->iosched.writing) {
959 bio = pkt_get_list_first(&pd->iosched.write_queue,
960 &pd->iosched.write_queue_tail);
961 } else {
962 bio = pkt_get_list_first(&pd->iosched.read_queue,
963 &pd->iosched.read_queue_tail);
965 spin_unlock(&pd->iosched.lock);
967 if (!bio)
968 continue;
970 if (bio_data_dir(bio) == READ)
971 pd->iosched.successive_reads += bio->bi_size >> 10;
972 else {
973 pd->iosched.successive_reads = 0;
974 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
976 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
977 if (pd->read_speed == pd->write_speed) {
978 pd->read_speed = MAX_SPEED;
979 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
981 } else {
982 if (pd->read_speed != pd->write_speed) {
983 pd->read_speed = pd->write_speed;
984 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
988 atomic_inc(&pd->cdrw.pending_bios);
989 generic_make_request(bio);
994 * Special care is needed if the underlying block device has a small
995 * max_phys_segments value.
997 static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
999 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
1001 * The cdrom device can handle one segment/frame
1003 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
1004 return 0;
1005 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
1007 * We can handle this case at the expense of some extra memory
1008 * copies during write operations
1010 set_bit(PACKET_MERGE_SEGS, &pd->flags);
1011 return 0;
1012 } else {
1013 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
1014 return -EIO;
1019 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1021 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
1023 unsigned int copy_size = CD_FRAMESIZE;
1025 while (copy_size > 0) {
1026 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
1027 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
1028 src_bvl->bv_offset + offs;
1029 void *vto = page_address(dst_page) + dst_offs;
1030 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
1032 BUG_ON(len < 0);
1033 memcpy(vto, vfrom, len);
1034 kunmap_atomic(vfrom, KM_USER0);
1036 seg++;
1037 offs = 0;
1038 dst_offs += len;
1039 copy_size -= len;
1044 * Copy all data for this packet to pkt->pages[], so that
1045 * a) The number of required segments for the write bio is minimized, which
1046 * is necessary for some scsi controllers.
1047 * b) The data can be used as cache to avoid read requests if we receive a
1048 * new write request for the same zone.
1050 static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
1052 int f, p, offs;
1054 /* Copy all data to pkt->pages[] */
1055 p = 0;
1056 offs = 0;
1057 for (f = 0; f < pkt->frames; f++) {
1058 if (bvec[f].bv_page != pkt->pages[p]) {
1059 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
1060 void *vto = page_address(pkt->pages[p]) + offs;
1061 memcpy(vto, vfrom, CD_FRAMESIZE);
1062 kunmap_atomic(vfrom, KM_USER0);
1063 bvec[f].bv_page = pkt->pages[p];
1064 bvec[f].bv_offset = offs;
1065 } else {
1066 BUG_ON(bvec[f].bv_offset != offs);
1068 offs += CD_FRAMESIZE;
1069 if (offs >= PAGE_SIZE) {
1070 offs = 0;
1071 p++;
1076 static void pkt_end_io_read(struct bio *bio, int err)
1078 struct packet_data *pkt = bio->bi_private;
1079 struct pktcdvd_device *pd = pkt->pd;
1080 BUG_ON(!pd);
1082 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1083 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1085 if (err)
1086 atomic_inc(&pkt->io_errors);
1087 if (atomic_dec_and_test(&pkt->io_wait)) {
1088 atomic_inc(&pkt->run_sm);
1089 wake_up(&pd->wqueue);
1091 pkt_bio_finished(pd);
1094 static void pkt_end_io_packet_write(struct bio *bio, int err)
1096 struct packet_data *pkt = bio->bi_private;
1097 struct pktcdvd_device *pd = pkt->pd;
1098 BUG_ON(!pd);
1100 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1102 pd->stats.pkt_ended++;
1104 pkt_bio_finished(pd);
1105 atomic_dec(&pkt->io_wait);
1106 atomic_inc(&pkt->run_sm);
1107 wake_up(&pd->wqueue);
1111 * Schedule reads for the holes in a packet
1113 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1115 int frames_read = 0;
1116 struct bio *bio;
1117 int f;
1118 char written[PACKET_MAX_SIZE];
1120 BUG_ON(!pkt->orig_bios);
1122 atomic_set(&pkt->io_wait, 0);
1123 atomic_set(&pkt->io_errors, 0);
1126 * Figure out which frames we need to read before we can write.
1128 memset(written, 0, sizeof(written));
1129 spin_lock(&pkt->lock);
1130 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1131 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1132 int num_frames = bio->bi_size / CD_FRAMESIZE;
1133 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
1134 BUG_ON(first_frame < 0);
1135 BUG_ON(first_frame + num_frames > pkt->frames);
1136 for (f = first_frame; f < first_frame + num_frames; f++)
1137 written[f] = 1;
1139 spin_unlock(&pkt->lock);
1141 if (pkt->cache_valid) {
1142 VPRINTK("pkt_gather_data: zone %llx cached\n",
1143 (unsigned long long)pkt->sector);
1144 goto out_account;
1148 * Schedule reads for missing parts of the packet.
1150 for (f = 0; f < pkt->frames; f++) {
1151 struct bio_vec *vec;
1153 int p, offset;
1154 if (written[f])
1155 continue;
1156 bio = pkt->r_bios[f];
1157 vec = bio->bi_io_vec;
1158 bio_init(bio);
1159 bio->bi_max_vecs = 1;
1160 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1161 bio->bi_bdev = pd->bdev;
1162 bio->bi_end_io = pkt_end_io_read;
1163 bio->bi_private = pkt;
1164 bio->bi_io_vec = vec;
1165 bio->bi_destructor = pkt_bio_destructor;
1167 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1168 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1169 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1170 f, pkt->pages[p], offset);
1171 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1172 BUG();
1174 atomic_inc(&pkt->io_wait);
1175 bio->bi_rw = READ;
1176 pkt_queue_bio(pd, bio);
1177 frames_read++;
1180 out_account:
1181 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1182 frames_read, (unsigned long long)pkt->sector);
1183 pd->stats.pkt_started++;
1184 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
1188 * Find a packet matching zone, or the least recently used packet if
1189 * there is no match.
1191 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1193 struct packet_data *pkt;
1195 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1196 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1197 list_del_init(&pkt->list);
1198 if (pkt->sector != zone)
1199 pkt->cache_valid = 0;
1200 return pkt;
1203 BUG();
1204 return NULL;
1207 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1209 if (pkt->cache_valid) {
1210 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1211 } else {
1212 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1217 * recover a failed write, query for relocation if possible
1219 * returns 1 if recovery is possible, or 0 if not
1222 static int pkt_start_recovery(struct packet_data *pkt)
1225 * FIXME. We need help from the file system to implement
1226 * recovery handling.
1228 return 0;
1229 #if 0
1230 struct request *rq = pkt->rq;
1231 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1232 struct block_device *pkt_bdev;
1233 struct super_block *sb = NULL;
1234 unsigned long old_block, new_block;
1235 sector_t new_sector;
1237 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1238 if (pkt_bdev) {
1239 sb = get_super(pkt_bdev);
1240 bdput(pkt_bdev);
1243 if (!sb)
1244 return 0;
1246 if (!sb->s_op || !sb->s_op->relocate_blocks)
1247 goto out;
1249 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1250 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1251 goto out;
1253 new_sector = new_block * (CD_FRAMESIZE >> 9);
1254 pkt->sector = new_sector;
1256 pkt->bio->bi_sector = new_sector;
1257 pkt->bio->bi_next = NULL;
1258 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1259 pkt->bio->bi_idx = 0;
1261 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1262 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1263 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1264 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1265 BUG_ON(pkt->bio->bi_private != pkt);
1267 drop_super(sb);
1268 return 1;
1270 out:
1271 drop_super(sb);
1272 return 0;
1273 #endif
1276 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1278 #if PACKET_DEBUG > 1
1279 static const char *state_name[] = {
1280 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1282 enum packet_data_state old_state = pkt->state;
1283 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1284 state_name[old_state], state_name[state]);
1285 #endif
1286 pkt->state = state;
1290 * Scan the work queue to see if we can start a new packet.
1291 * returns non-zero if any work was done.
1293 static int pkt_handle_queue(struct pktcdvd_device *pd)
1295 struct packet_data *pkt, *p;
1296 struct bio *bio = NULL;
1297 sector_t zone = 0; /* Suppress gcc warning */
1298 struct pkt_rb_node *node, *first_node;
1299 struct rb_node *n;
1300 int wakeup;
1302 VPRINTK("handle_queue\n");
1304 atomic_set(&pd->scan_queue, 0);
1306 if (list_empty(&pd->cdrw.pkt_free_list)) {
1307 VPRINTK("handle_queue: no pkt\n");
1308 return 0;
1312 * Try to find a zone we are not already working on.
1314 spin_lock(&pd->lock);
1315 first_node = pkt_rbtree_find(pd, pd->current_sector);
1316 if (!first_node) {
1317 n = rb_first(&pd->bio_queue);
1318 if (n)
1319 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1321 node = first_node;
1322 while (node) {
1323 bio = node->bio;
1324 zone = ZONE(bio->bi_sector, pd);
1325 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
1326 if (p->sector == zone) {
1327 bio = NULL;
1328 goto try_next_bio;
1331 break;
1332 try_next_bio:
1333 node = pkt_rbtree_next(node);
1334 if (!node) {
1335 n = rb_first(&pd->bio_queue);
1336 if (n)
1337 node = rb_entry(n, struct pkt_rb_node, rb_node);
1339 if (node == first_node)
1340 node = NULL;
1342 spin_unlock(&pd->lock);
1343 if (!bio) {
1344 VPRINTK("handle_queue: no bio\n");
1345 return 0;
1348 pkt = pkt_get_packet_data(pd, zone);
1350 pd->current_sector = zone + pd->settings.size;
1351 pkt->sector = zone;
1352 BUG_ON(pkt->frames != pd->settings.size >> 2);
1353 pkt->write_size = 0;
1356 * Scan work queue for bios in the same zone and link them
1357 * to this packet.
1359 spin_lock(&pd->lock);
1360 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1361 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1362 bio = node->bio;
1363 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1364 (unsigned long long)ZONE(bio->bi_sector, pd));
1365 if (ZONE(bio->bi_sector, pd) != zone)
1366 break;
1367 pkt_rbtree_erase(pd, node);
1368 spin_lock(&pkt->lock);
1369 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1370 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1371 spin_unlock(&pkt->lock);
1373 /* check write congestion marks, and if bio_queue_size is
1374 below, wake up any waiters */
1375 wakeup = (pd->write_congestion_on > 0
1376 && pd->bio_queue_size <= pd->write_congestion_off);
1377 spin_unlock(&pd->lock);
1378 if (wakeup)
1379 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
1381 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1382 pkt_set_state(pkt, PACKET_WAITING_STATE);
1383 atomic_set(&pkt->run_sm, 1);
1385 spin_lock(&pd->cdrw.active_list_lock);
1386 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1387 spin_unlock(&pd->cdrw.active_list_lock);
1389 return 1;
1393 * Assemble a bio to write one packet and queue the bio for processing
1394 * by the underlying block device.
1396 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1398 struct bio *bio;
1399 int f;
1400 int frames_write;
1401 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
1403 for (f = 0; f < pkt->frames; f++) {
1404 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1405 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1409 * Fill-in bvec with data from orig_bios.
1411 frames_write = 0;
1412 spin_lock(&pkt->lock);
1413 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1414 int segment = bio->bi_idx;
1415 int src_offs = 0;
1416 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1417 int num_frames = bio->bi_size / CD_FRAMESIZE;
1418 BUG_ON(first_frame < 0);
1419 BUG_ON(first_frame + num_frames > pkt->frames);
1420 for (f = first_frame; f < first_frame + num_frames; f++) {
1421 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1423 while (src_offs >= src_bvl->bv_len) {
1424 src_offs -= src_bvl->bv_len;
1425 segment++;
1426 BUG_ON(segment >= bio->bi_vcnt);
1427 src_bvl = bio_iovec_idx(bio, segment);
1430 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1431 bvec[f].bv_page = src_bvl->bv_page;
1432 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1433 } else {
1434 pkt_copy_bio_data(bio, segment, src_offs,
1435 bvec[f].bv_page, bvec[f].bv_offset);
1437 src_offs += CD_FRAMESIZE;
1438 frames_write++;
1441 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1442 spin_unlock(&pkt->lock);
1444 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1445 frames_write, (unsigned long long)pkt->sector);
1446 BUG_ON(frames_write != pkt->write_size);
1448 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1449 pkt_make_local_copy(pkt, bvec);
1450 pkt->cache_valid = 1;
1451 } else {
1452 pkt->cache_valid = 0;
1455 /* Start the write request */
1456 bio_init(pkt->w_bio);
1457 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1458 pkt->w_bio->bi_sector = pkt->sector;
1459 pkt->w_bio->bi_bdev = pd->bdev;
1460 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1461 pkt->w_bio->bi_private = pkt;
1462 pkt->w_bio->bi_io_vec = bvec;
1463 pkt->w_bio->bi_destructor = pkt_bio_destructor;
1464 for (f = 0; f < pkt->frames; f++)
1465 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1466 BUG();
1467 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
1469 atomic_set(&pkt->io_wait, 1);
1470 pkt->w_bio->bi_rw = WRITE;
1471 pkt_queue_bio(pd, pkt->w_bio);
1474 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1476 struct bio *bio, *next;
1478 if (!uptodate)
1479 pkt->cache_valid = 0;
1481 /* Finish all bios corresponding to this packet */
1482 bio = pkt->orig_bios;
1483 while (bio) {
1484 next = bio->bi_next;
1485 bio->bi_next = NULL;
1486 bio_endio(bio, uptodate ? 0 : -EIO);
1487 bio = next;
1489 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1492 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1494 int uptodate;
1496 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1498 for (;;) {
1499 switch (pkt->state) {
1500 case PACKET_WAITING_STATE:
1501 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1502 return;
1504 pkt->sleep_time = 0;
1505 pkt_gather_data(pd, pkt);
1506 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1507 break;
1509 case PACKET_READ_WAIT_STATE:
1510 if (atomic_read(&pkt->io_wait) > 0)
1511 return;
1513 if (atomic_read(&pkt->io_errors) > 0) {
1514 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1515 } else {
1516 pkt_start_write(pd, pkt);
1518 break;
1520 case PACKET_WRITE_WAIT_STATE:
1521 if (atomic_read(&pkt->io_wait) > 0)
1522 return;
1524 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1525 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1526 } else {
1527 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1529 break;
1531 case PACKET_RECOVERY_STATE:
1532 if (pkt_start_recovery(pkt)) {
1533 pkt_start_write(pd, pkt);
1534 } else {
1535 VPRINTK("No recovery possible\n");
1536 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1538 break;
1540 case PACKET_FINISHED_STATE:
1541 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1542 pkt_finish_packet(pkt, uptodate);
1543 return;
1545 default:
1546 BUG();
1547 break;
1552 static void pkt_handle_packets(struct pktcdvd_device *pd)
1554 struct packet_data *pkt, *next;
1556 VPRINTK("pkt_handle_packets\n");
1559 * Run state machine for active packets
1561 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1562 if (atomic_read(&pkt->run_sm) > 0) {
1563 atomic_set(&pkt->run_sm, 0);
1564 pkt_run_state_machine(pd, pkt);
1569 * Move no longer active packets to the free list
1571 spin_lock(&pd->cdrw.active_list_lock);
1572 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1573 if (pkt->state == PACKET_FINISHED_STATE) {
1574 list_del(&pkt->list);
1575 pkt_put_packet_data(pd, pkt);
1576 pkt_set_state(pkt, PACKET_IDLE_STATE);
1577 atomic_set(&pd->scan_queue, 1);
1580 spin_unlock(&pd->cdrw.active_list_lock);
1583 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1585 struct packet_data *pkt;
1586 int i;
1588 for (i = 0; i < PACKET_NUM_STATES; i++)
1589 states[i] = 0;
1591 spin_lock(&pd->cdrw.active_list_lock);
1592 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1593 states[pkt->state]++;
1595 spin_unlock(&pd->cdrw.active_list_lock);
1599 * kcdrwd is woken up when writes have been queued for one of our
1600 * registered devices
1602 static int kcdrwd(void *foobar)
1604 struct pktcdvd_device *pd = foobar;
1605 struct packet_data *pkt;
1606 long min_sleep_time, residue;
1608 set_user_nice(current, -20);
1609 set_freezable();
1611 for (;;) {
1612 DECLARE_WAITQUEUE(wait, current);
1615 * Wait until there is something to do
1617 add_wait_queue(&pd->wqueue, &wait);
1618 for (;;) {
1619 set_current_state(TASK_INTERRUPTIBLE);
1621 /* Check if we need to run pkt_handle_queue */
1622 if (atomic_read(&pd->scan_queue) > 0)
1623 goto work_to_do;
1625 /* Check if we need to run the state machine for some packet */
1626 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1627 if (atomic_read(&pkt->run_sm) > 0)
1628 goto work_to_do;
1631 /* Check if we need to process the iosched queues */
1632 if (atomic_read(&pd->iosched.attention) != 0)
1633 goto work_to_do;
1635 /* Otherwise, go to sleep */
1636 if (PACKET_DEBUG > 1) {
1637 int states[PACKET_NUM_STATES];
1638 pkt_count_states(pd, states);
1639 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1640 states[0], states[1], states[2], states[3],
1641 states[4], states[5]);
1644 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1645 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1646 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1647 min_sleep_time = pkt->sleep_time;
1650 generic_unplug_device(bdev_get_queue(pd->bdev));
1652 VPRINTK("kcdrwd: sleeping\n");
1653 residue = schedule_timeout(min_sleep_time);
1654 VPRINTK("kcdrwd: wake up\n");
1656 /* make swsusp happy with our thread */
1657 try_to_freeze();
1659 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1660 if (!pkt->sleep_time)
1661 continue;
1662 pkt->sleep_time -= min_sleep_time - residue;
1663 if (pkt->sleep_time <= 0) {
1664 pkt->sleep_time = 0;
1665 atomic_inc(&pkt->run_sm);
1669 if (kthread_should_stop())
1670 break;
1672 work_to_do:
1673 set_current_state(TASK_RUNNING);
1674 remove_wait_queue(&pd->wqueue, &wait);
1676 if (kthread_should_stop())
1677 break;
1680 * if pkt_handle_queue returns true, we can queue
1681 * another request.
1683 while (pkt_handle_queue(pd))
1687 * Handle packet state machine
1689 pkt_handle_packets(pd);
1692 * Handle iosched queues
1694 pkt_iosched_process_queue(pd);
1697 return 0;
1700 static void pkt_print_settings(struct pktcdvd_device *pd)
1702 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1703 printk("%u blocks, ", pd->settings.size >> 2);
1704 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1707 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1709 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1711 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1712 cgc->cmd[2] = page_code | (page_control << 6);
1713 cgc->cmd[7] = cgc->buflen >> 8;
1714 cgc->cmd[8] = cgc->buflen & 0xff;
1715 cgc->data_direction = CGC_DATA_READ;
1716 return pkt_generic_packet(pd, cgc);
1719 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1721 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1722 memset(cgc->buffer, 0, 2);
1723 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1724 cgc->cmd[1] = 0x10; /* PF */
1725 cgc->cmd[7] = cgc->buflen >> 8;
1726 cgc->cmd[8] = cgc->buflen & 0xff;
1727 cgc->data_direction = CGC_DATA_WRITE;
1728 return pkt_generic_packet(pd, cgc);
1731 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1733 struct packet_command cgc;
1734 int ret;
1736 /* set up command and get the disc info */
1737 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1738 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1739 cgc.cmd[8] = cgc.buflen = 2;
1740 cgc.quiet = 1;
1742 if ((ret = pkt_generic_packet(pd, &cgc)))
1743 return ret;
1745 /* not all drives have the same disc_info length, so requeue
1746 * packet with the length the drive tells us it can supply
1748 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1749 sizeof(di->disc_information_length);
1751 if (cgc.buflen > sizeof(disc_information))
1752 cgc.buflen = sizeof(disc_information);
1754 cgc.cmd[8] = cgc.buflen;
1755 return pkt_generic_packet(pd, &cgc);
1758 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1760 struct packet_command cgc;
1761 int ret;
1763 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1764 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1765 cgc.cmd[1] = type & 3;
1766 cgc.cmd[4] = (track & 0xff00) >> 8;
1767 cgc.cmd[5] = track & 0xff;
1768 cgc.cmd[8] = 8;
1769 cgc.quiet = 1;
1771 if ((ret = pkt_generic_packet(pd, &cgc)))
1772 return ret;
1774 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1775 sizeof(ti->track_information_length);
1777 if (cgc.buflen > sizeof(track_information))
1778 cgc.buflen = sizeof(track_information);
1780 cgc.cmd[8] = cgc.buflen;
1781 return pkt_generic_packet(pd, &cgc);
1784 <<<<<<< HEAD:drivers/block/pktcdvd.c
1785 static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1786 =======
1787 static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1788 long *last_written)
1789 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
1791 disc_information di;
1792 track_information ti;
1793 __u32 last_track;
1794 int ret = -1;
1796 if ((ret = pkt_get_disc_info(pd, &di)))
1797 return ret;
1799 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1800 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1801 return ret;
1803 /* if this track is blank, try the previous. */
1804 if (ti.blank) {
1805 last_track--;
1806 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1807 return ret;
1810 /* if last recorded field is valid, return it. */
1811 if (ti.lra_v) {
1812 *last_written = be32_to_cpu(ti.last_rec_address);
1813 } else {
1814 /* make it up instead */
1815 *last_written = be32_to_cpu(ti.track_start) +
1816 be32_to_cpu(ti.track_size);
1817 if (ti.free_blocks)
1818 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1820 return 0;
1824 * write mode select package based on pd->settings
1826 <<<<<<< HEAD:drivers/block/pktcdvd.c
1827 static int pkt_set_write_settings(struct pktcdvd_device *pd)
1828 =======
1829 static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
1830 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
1832 struct packet_command cgc;
1833 struct request_sense sense;
1834 write_param_page *wp;
1835 char buffer[128];
1836 int ret, size;
1838 /* doesn't apply to DVD+RW or DVD-RAM */
1839 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1840 return 0;
1842 memset(buffer, 0, sizeof(buffer));
1843 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1844 cgc.sense = &sense;
1845 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1846 pkt_dump_sense(&cgc);
1847 return ret;
1850 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1851 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1852 if (size > sizeof(buffer))
1853 size = sizeof(buffer);
1856 * now get it all
1858 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1859 cgc.sense = &sense;
1860 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1861 pkt_dump_sense(&cgc);
1862 return ret;
1866 * write page is offset header + block descriptor length
1868 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1870 wp->fp = pd->settings.fp;
1871 wp->track_mode = pd->settings.track_mode;
1872 wp->write_type = pd->settings.write_type;
1873 wp->data_block_type = pd->settings.block_mode;
1875 wp->multi_session = 0;
1877 #ifdef PACKET_USE_LS
1878 wp->link_size = 7;
1879 wp->ls_v = 1;
1880 #endif
1882 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1883 wp->session_format = 0;
1884 wp->subhdr2 = 0x20;
1885 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1886 wp->session_format = 0x20;
1887 wp->subhdr2 = 8;
1888 #if 0
1889 wp->mcn[0] = 0x80;
1890 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1891 #endif
1892 } else {
1894 * paranoia
1896 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
1897 return 1;
1899 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1901 cgc.buflen = cgc.cmd[8] = size;
1902 if ((ret = pkt_mode_select(pd, &cgc))) {
1903 pkt_dump_sense(&cgc);
1904 return ret;
1907 pkt_print_settings(pd);
1908 return 0;
1912 * 1 -- we can write to this track, 0 -- we can't
1914 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1916 switch (pd->mmc3_profile) {
1917 case 0x1a: /* DVD+RW */
1918 case 0x12: /* DVD-RAM */
1919 /* The track is always writable on DVD+RW/DVD-RAM */
1920 return 1;
1921 default:
1922 break;
1925 if (!ti->packet || !ti->fp)
1926 return 0;
1929 * "good" settings as per Mt Fuji.
1931 if (ti->rt == 0 && ti->blank == 0)
1932 return 1;
1934 if (ti->rt == 0 && ti->blank == 1)
1935 return 1;
1937 if (ti->rt == 1 && ti->blank == 0)
1938 return 1;
1940 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1941 return 0;
1945 * 1 -- we can write to this disc, 0 -- we can't
1947 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1949 switch (pd->mmc3_profile) {
1950 case 0x0a: /* CD-RW */
1951 case 0xffff: /* MMC3 not supported */
1952 break;
1953 case 0x1a: /* DVD+RW */
1954 case 0x13: /* DVD-RW */
1955 case 0x12: /* DVD-RAM */
1956 return 1;
1957 default:
1958 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
1959 return 0;
1963 * for disc type 0xff we should probably reserve a new track.
1964 * but i'm not sure, should we leave this to user apps? probably.
1966 if (di->disc_type == 0xff) {
1967 printk(DRIVER_NAME": Unknown disc. No track?\n");
1968 return 0;
1971 if (di->disc_type != 0x20 && di->disc_type != 0) {
1972 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
1973 return 0;
1976 if (di->erasable == 0) {
1977 printk(DRIVER_NAME": Disc not erasable\n");
1978 return 0;
1981 if (di->border_status == PACKET_SESSION_RESERVED) {
1982 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
1983 return 0;
1986 return 1;
1989 <<<<<<< HEAD:drivers/block/pktcdvd.c
1990 static int pkt_probe_settings(struct pktcdvd_device *pd)
1991 =======
1992 static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
1993 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
1995 struct packet_command cgc;
1996 unsigned char buf[12];
1997 disc_information di;
1998 track_information ti;
1999 int ret, track;
2001 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2002 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
2003 cgc.cmd[8] = 8;
2004 ret = pkt_generic_packet(pd, &cgc);
2005 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
2007 memset(&di, 0, sizeof(disc_information));
2008 memset(&ti, 0, sizeof(track_information));
2010 if ((ret = pkt_get_disc_info(pd, &di))) {
2011 printk("failed get_disc\n");
2012 return ret;
2015 if (!pkt_writable_disc(pd, &di))
2016 return -EROFS;
2018 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
2020 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
2021 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
2022 printk(DRIVER_NAME": failed get_track\n");
2023 return ret;
2026 if (!pkt_writable_track(pd, &ti)) {
2027 printk(DRIVER_NAME": can't write to this track\n");
2028 return -EROFS;
2032 * we keep packet size in 512 byte units, makes it easier to
2033 * deal with request calculations.
2035 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2036 if (pd->settings.size == 0) {
2037 printk(DRIVER_NAME": detected zero packet size!\n");
2038 return -ENXIO;
2040 if (pd->settings.size > PACKET_MAX_SECTORS) {
2041 printk(DRIVER_NAME": packet size is too big\n");
2042 return -EROFS;
2044 pd->settings.fp = ti.fp;
2045 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2047 if (ti.nwa_v) {
2048 pd->nwa = be32_to_cpu(ti.next_writable);
2049 set_bit(PACKET_NWA_VALID, &pd->flags);
2053 * in theory we could use lra on -RW media as well and just zero
2054 * blocks that haven't been written yet, but in practice that
2055 * is just a no-go. we'll use that for -R, naturally.
2057 if (ti.lra_v) {
2058 pd->lra = be32_to_cpu(ti.last_rec_address);
2059 set_bit(PACKET_LRA_VALID, &pd->flags);
2060 } else {
2061 pd->lra = 0xffffffff;
2062 set_bit(PACKET_LRA_VALID, &pd->flags);
2066 * fine for now
2068 pd->settings.link_loss = 7;
2069 pd->settings.write_type = 0; /* packet */
2070 pd->settings.track_mode = ti.track_mode;
2073 * mode1 or mode2 disc
2075 switch (ti.data_mode) {
2076 case PACKET_MODE1:
2077 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2078 break;
2079 case PACKET_MODE2:
2080 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2081 break;
2082 default:
2083 printk(DRIVER_NAME": unknown data mode\n");
2084 return -EROFS;
2086 return 0;
2090 * enable/disable write caching on drive
2092 <<<<<<< HEAD:drivers/block/pktcdvd.c
2093 static int pkt_write_caching(struct pktcdvd_device *pd, int set)
2094 =======
2095 static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2096 int set)
2097 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
2099 struct packet_command cgc;
2100 struct request_sense sense;
2101 unsigned char buf[64];
2102 int ret;
2104 memset(buf, 0, sizeof(buf));
2105 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2106 cgc.sense = &sense;
2107 cgc.buflen = pd->mode_offset + 12;
2110 * caching mode page might not be there, so quiet this command
2112 cgc.quiet = 1;
2114 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2115 return ret;
2117 buf[pd->mode_offset + 10] |= (!!set << 2);
2119 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2120 ret = pkt_mode_select(pd, &cgc);
2121 if (ret) {
2122 printk(DRIVER_NAME": write caching control failed\n");
2123 pkt_dump_sense(&cgc);
2124 } else if (!ret && set)
2125 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
2126 return ret;
2129 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2131 struct packet_command cgc;
2133 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2134 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2135 cgc.cmd[4] = lockflag ? 1 : 0;
2136 return pkt_generic_packet(pd, &cgc);
2140 * Returns drive maximum write speed
2142 <<<<<<< HEAD:drivers/block/pktcdvd.c
2143 static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
2144 =======
2145 static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2146 unsigned *write_speed)
2147 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
2149 struct packet_command cgc;
2150 struct request_sense sense;
2151 unsigned char buf[256+18];
2152 unsigned char *cap_buf;
2153 int ret, offset;
2155 memset(buf, 0, sizeof(buf));
2156 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2157 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2158 cgc.sense = &sense;
2160 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2161 if (ret) {
2162 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2163 sizeof(struct mode_page_header);
2164 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2165 if (ret) {
2166 pkt_dump_sense(&cgc);
2167 return ret;
2171 offset = 20; /* Obsoleted field, used by older drives */
2172 if (cap_buf[1] >= 28)
2173 offset = 28; /* Current write speed selected */
2174 if (cap_buf[1] >= 30) {
2175 /* If the drive reports at least one "Logical Unit Write
2176 * Speed Performance Descriptor Block", use the information
2177 * in the first block. (contains the highest speed)
2179 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2180 if (num_spdb > 0)
2181 offset = 34;
2184 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2185 return 0;
2188 /* These tables from cdrecord - I don't have orange book */
2189 /* standard speed CD-RW (1-4x) */
2190 static char clv_to_speed[16] = {
2191 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2192 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2194 /* high speed CD-RW (-10x) */
2195 static char hs_clv_to_speed[16] = {
2196 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2197 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2199 /* ultra high speed CD-RW */
2200 static char us_clv_to_speed[16] = {
2201 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2202 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2206 * reads the maximum media speed from ATIP
2208 <<<<<<< HEAD:drivers/block/pktcdvd.c
2209 static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
2210 =======
2211 static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2212 unsigned *speed)
2213 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
2215 struct packet_command cgc;
2216 struct request_sense sense;
2217 unsigned char buf[64];
2218 unsigned int size, st, sp;
2219 int ret;
2221 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2222 cgc.sense = &sense;
2223 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2224 cgc.cmd[1] = 2;
2225 cgc.cmd[2] = 4; /* READ ATIP */
2226 cgc.cmd[8] = 2;
2227 ret = pkt_generic_packet(pd, &cgc);
2228 if (ret) {
2229 pkt_dump_sense(&cgc);
2230 return ret;
2232 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2233 if (size > sizeof(buf))
2234 size = sizeof(buf);
2236 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2237 cgc.sense = &sense;
2238 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2239 cgc.cmd[1] = 2;
2240 cgc.cmd[2] = 4;
2241 cgc.cmd[8] = size;
2242 ret = pkt_generic_packet(pd, &cgc);
2243 if (ret) {
2244 pkt_dump_sense(&cgc);
2245 return ret;
2248 if (!(buf[6] & 0x40)) {
2249 printk(DRIVER_NAME": Disc type is not CD-RW\n");
2250 return 1;
2252 if (!(buf[6] & 0x4)) {
2253 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
2254 return 1;
2257 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2259 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2261 /* Info from cdrecord */
2262 switch (st) {
2263 case 0: /* standard speed */
2264 *speed = clv_to_speed[sp];
2265 break;
2266 case 1: /* high speed */
2267 *speed = hs_clv_to_speed[sp];
2268 break;
2269 case 2: /* ultra high speed */
2270 *speed = us_clv_to_speed[sp];
2271 break;
2272 default:
2273 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
2274 return 1;
2276 if (*speed) {
2277 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
2278 return 0;
2279 } else {
2280 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
2281 return 1;
2285 <<<<<<< HEAD:drivers/block/pktcdvd.c
2286 static int pkt_perform_opc(struct pktcdvd_device *pd)
2287 =======
2288 static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
2289 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/pktcdvd.c
2291 struct packet_command cgc;
2292 struct request_sense sense;
2293 int ret;
2295 VPRINTK(DRIVER_NAME": Performing OPC\n");
2297 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2298 cgc.sense = &sense;
2299 cgc.timeout = 60*HZ;
2300 cgc.cmd[0] = GPCMD_SEND_OPC;
2301 cgc.cmd[1] = 1;
2302 if ((ret = pkt_generic_packet(pd, &cgc)))
2303 pkt_dump_sense(&cgc);
2304 return ret;
2307 static int pkt_open_write(struct pktcdvd_device *pd)
2309 int ret;
2310 unsigned int write_speed, media_write_speed, read_speed;
2312 if ((ret = pkt_probe_settings(pd))) {
2313 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
2314 return ret;
2317 if ((ret = pkt_set_write_settings(pd))) {
2318 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
2319 return -EIO;
2322 pkt_write_caching(pd, USE_WCACHING);
2324 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2325 write_speed = 16 * 177;
2326 switch (pd->mmc3_profile) {
2327 case 0x13: /* DVD-RW */
2328 case 0x1a: /* DVD+RW */
2329 case 0x12: /* DVD-RAM */
2330 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
2331 break;
2332 default:
2333 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2334 media_write_speed = 16;
2335 write_speed = min(write_speed, media_write_speed * 177);
2336 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
2337 break;
2339 read_speed = write_speed;
2341 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
2342 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
2343 return -EIO;
2345 pd->write_speed = write_speed;
2346 pd->read_speed = read_speed;
2348 if ((ret = pkt_perform_opc(pd))) {
2349 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
2352 return 0;
2356 * called at open time.
2358 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2360 int ret;
2361 long lba;
2362 struct request_queue *q;
2365 * We need to re-open the cdrom device without O_NONBLOCK to be able
2366 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2367 * so bdget() can't fail.
2369 bdget(pd->bdev->bd_dev);
2370 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2371 goto out;
2373 if ((ret = bd_claim(pd->bdev, pd)))
2374 goto out_putdev;
2376 if ((ret = pkt_get_last_written(pd, &lba))) {
2377 printk(DRIVER_NAME": pkt_get_last_written failed\n");
2378 goto out_unclaim;
2381 set_capacity(pd->disk, lba << 2);
2382 set_capacity(pd->bdev->bd_disk, lba << 2);
2383 bd_set_size(pd->bdev, (loff_t)lba << 11);
2385 q = bdev_get_queue(pd->bdev);
2386 if (write) {
2387 if ((ret = pkt_open_write(pd)))
2388 goto out_unclaim;
2390 * Some CDRW drives can not handle writes larger than one packet,
2391 * even if the size is a multiple of the packet size.
2393 spin_lock_irq(q->queue_lock);
2394 blk_queue_max_sectors(q, pd->settings.size);
2395 spin_unlock_irq(q->queue_lock);
2396 set_bit(PACKET_WRITABLE, &pd->flags);
2397 } else {
2398 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2399 clear_bit(PACKET_WRITABLE, &pd->flags);
2402 if ((ret = pkt_set_segment_merging(pd, q)))
2403 goto out_unclaim;
2405 if (write) {
2406 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2407 printk(DRIVER_NAME": not enough memory for buffers\n");
2408 ret = -ENOMEM;
2409 goto out_unclaim;
2411 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
2414 return 0;
2416 out_unclaim:
2417 bd_release(pd->bdev);
2418 out_putdev:
2419 blkdev_put(pd->bdev);
2420 out:
2421 return ret;
2425 * called when the device is closed. makes sure that the device flushes
2426 * the internal cache before we close.
2428 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2430 if (flush && pkt_flush_cache(pd))
2431 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
2433 pkt_lock_door(pd, 0);
2435 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2436 bd_release(pd->bdev);
2437 blkdev_put(pd->bdev);
2439 pkt_shrink_pktlist(pd);
2442 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2444 if (dev_minor >= MAX_WRITERS)
2445 return NULL;
2446 return pkt_devs[dev_minor];
2449 static int pkt_open(struct inode *inode, struct file *file)
2451 struct pktcdvd_device *pd = NULL;
2452 int ret;
2454 VPRINTK(DRIVER_NAME": entering open\n");
2456 mutex_lock(&ctl_mutex);
2457 pd = pkt_find_dev_from_minor(iminor(inode));
2458 if (!pd) {
2459 ret = -ENODEV;
2460 goto out;
2462 BUG_ON(pd->refcnt < 0);
2464 pd->refcnt++;
2465 if (pd->refcnt > 1) {
2466 if ((file->f_mode & FMODE_WRITE) &&
2467 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2468 ret = -EBUSY;
2469 goto out_dec;
2471 } else {
2472 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2473 if (ret)
2474 goto out_dec;
2476 * needed here as well, since ext2 (among others) may change
2477 * the blocksize at mount time
2479 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2482 mutex_unlock(&ctl_mutex);
2483 return 0;
2485 out_dec:
2486 pd->refcnt--;
2487 out:
2488 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
2489 mutex_unlock(&ctl_mutex);
2490 return ret;
2493 static int pkt_close(struct inode *inode, struct file *file)
2495 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2496 int ret = 0;
2498 mutex_lock(&ctl_mutex);
2499 pd->refcnt--;
2500 BUG_ON(pd->refcnt < 0);
2501 if (pd->refcnt == 0) {
2502 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2503 pkt_release_dev(pd, flush);
2505 mutex_unlock(&ctl_mutex);
2506 return ret;
2510 static void pkt_end_io_read_cloned(struct bio *bio, int err)
2512 struct packet_stacked_data *psd = bio->bi_private;
2513 struct pktcdvd_device *pd = psd->pd;
2515 bio_put(bio);
2516 bio_endio(psd->bio, err);
2517 mempool_free(psd, psd_pool);
2518 pkt_bio_finished(pd);
2521 static int pkt_make_request(struct request_queue *q, struct bio *bio)
2523 struct pktcdvd_device *pd;
2524 char b[BDEVNAME_SIZE];
2525 sector_t zone;
2526 struct packet_data *pkt;
2527 int was_empty, blocked_bio;
2528 struct pkt_rb_node *node;
2530 pd = q->queuedata;
2531 if (!pd) {
2532 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2533 goto end_io;
2537 * Clone READ bios so we can have our own bi_end_io callback.
2539 if (bio_data_dir(bio) == READ) {
2540 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2541 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2543 psd->pd = pd;
2544 psd->bio = bio;
2545 cloned_bio->bi_bdev = pd->bdev;
2546 cloned_bio->bi_private = psd;
2547 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2548 pd->stats.secs_r += bio->bi_size >> 9;
2549 pkt_queue_bio(pd, cloned_bio);
2550 return 0;
2553 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2554 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
2555 pd->name, (unsigned long long)bio->bi_sector);
2556 goto end_io;
2559 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2560 printk(DRIVER_NAME": wrong bio size\n");
2561 goto end_io;
2564 blk_queue_bounce(q, &bio);
2566 zone = ZONE(bio->bi_sector, pd);
2567 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2568 (unsigned long long)bio->bi_sector,
2569 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2571 /* Check if we have to split the bio */
2573 struct bio_pair *bp;
2574 sector_t last_zone;
2575 int first_sectors;
2577 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2578 if (last_zone != zone) {
2579 BUG_ON(last_zone != zone + pd->settings.size);
2580 first_sectors = last_zone - bio->bi_sector;
2581 bp = bio_split(bio, bio_split_pool, first_sectors);
2582 BUG_ON(!bp);
2583 pkt_make_request(q, &bp->bio1);
2584 pkt_make_request(q, &bp->bio2);
2585 bio_pair_release(bp);
2586 return 0;
2591 * If we find a matching packet in state WAITING or READ_WAIT, we can
2592 * just append this bio to that packet.
2594 spin_lock(&pd->cdrw.active_list_lock);
2595 blocked_bio = 0;
2596 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2597 if (pkt->sector == zone) {
2598 spin_lock(&pkt->lock);
2599 if ((pkt->state == PACKET_WAITING_STATE) ||
2600 (pkt->state == PACKET_READ_WAIT_STATE)) {
2601 pkt_add_list_last(bio, &pkt->orig_bios,
2602 &pkt->orig_bios_tail);
2603 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2604 if ((pkt->write_size >= pkt->frames) &&
2605 (pkt->state == PACKET_WAITING_STATE)) {
2606 atomic_inc(&pkt->run_sm);
2607 wake_up(&pd->wqueue);
2609 spin_unlock(&pkt->lock);
2610 spin_unlock(&pd->cdrw.active_list_lock);
2611 return 0;
2612 } else {
2613 blocked_bio = 1;
2615 spin_unlock(&pkt->lock);
2618 spin_unlock(&pd->cdrw.active_list_lock);
2621 * Test if there is enough room left in the bio work queue
2622 * (queue size >= congestion on mark).
2623 * If not, wait till the work queue size is below the congestion off mark.
2625 spin_lock(&pd->lock);
2626 if (pd->write_congestion_on > 0
2627 && pd->bio_queue_size >= pd->write_congestion_on) {
2628 set_bdi_congested(&q->backing_dev_info, WRITE);
2629 do {
2630 spin_unlock(&pd->lock);
2631 congestion_wait(WRITE, HZ);
2632 spin_lock(&pd->lock);
2633 } while(pd->bio_queue_size > pd->write_congestion_off);
2635 spin_unlock(&pd->lock);
2638 * No matching packet found. Store the bio in the work queue.
2640 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2641 node->bio = bio;
2642 spin_lock(&pd->lock);
2643 BUG_ON(pd->bio_queue_size < 0);
2644 was_empty = (pd->bio_queue_size == 0);
2645 pkt_rbtree_insert(pd, node);
2646 spin_unlock(&pd->lock);
2649 * Wake up the worker thread.
2651 atomic_set(&pd->scan_queue, 1);
2652 if (was_empty) {
2653 /* This wake_up is required for correct operation */
2654 wake_up(&pd->wqueue);
2655 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2657 * This wake up is not required for correct operation,
2658 * but improves performance in some cases.
2660 wake_up(&pd->wqueue);
2662 return 0;
2663 end_io:
2664 bio_io_error(bio);
2665 return 0;
2670 static int pkt_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
2672 struct pktcdvd_device *pd = q->queuedata;
2673 sector_t zone = ZONE(bio->bi_sector, pd);
2674 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2675 int remaining = (pd->settings.size << 9) - used;
2676 int remaining2;
2679 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2680 * boundary, pkt_make_request() will split the bio.
2682 remaining2 = PAGE_SIZE - bio->bi_size;
2683 remaining = max(remaining, remaining2);
2685 BUG_ON(remaining < 0);
2686 return remaining;
2689 static void pkt_init_queue(struct pktcdvd_device *pd)
2691 struct request_queue *q = pd->disk->queue;
2693 blk_queue_make_request(q, pkt_make_request);
2694 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2695 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2696 blk_queue_merge_bvec(q, pkt_merge_bvec);
2697 q->queuedata = pd;
2700 static int pkt_seq_show(struct seq_file *m, void *p)
2702 struct pktcdvd_device *pd = m->private;
2703 char *msg;
2704 char bdev_buf[BDEVNAME_SIZE];
2705 int states[PACKET_NUM_STATES];
2707 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2708 bdevname(pd->bdev, bdev_buf));
2710 seq_printf(m, "\nSettings:\n");
2711 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2713 if (pd->settings.write_type == 0)
2714 msg = "Packet";
2715 else
2716 msg = "Unknown";
2717 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2719 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2720 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2722 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2724 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2725 msg = "Mode 1";
2726 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2727 msg = "Mode 2";
2728 else
2729 msg = "Unknown";
2730 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2732 seq_printf(m, "\nStatistics:\n");
2733 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2734 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2735 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2736 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2737 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2739 seq_printf(m, "\nMisc:\n");
2740 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2741 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2742 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2743 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2744 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2745 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2747 seq_printf(m, "\nQueue state:\n");
2748 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2749 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2750 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2752 pkt_count_states(pd, states);
2753 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2754 states[0], states[1], states[2], states[3], states[4], states[5]);
2756 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2757 pd->write_congestion_off,
2758 pd->write_congestion_on);
2759 return 0;
2762 static int pkt_seq_open(struct inode *inode, struct file *file)
2764 return single_open(file, pkt_seq_show, PDE(inode)->data);
2767 static const struct file_operations pkt_proc_fops = {
2768 .open = pkt_seq_open,
2769 .read = seq_read,
2770 .llseek = seq_lseek,
2771 .release = single_release
2774 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2776 int i;
2777 int ret = 0;
2778 char b[BDEVNAME_SIZE];
2779 struct proc_dir_entry *proc;
2780 struct block_device *bdev;
2782 if (pd->pkt_dev == dev) {
2783 printk(DRIVER_NAME": Recursive setup not allowed\n");
2784 return -EBUSY;
2786 for (i = 0; i < MAX_WRITERS; i++) {
2787 struct pktcdvd_device *pd2 = pkt_devs[i];
2788 if (!pd2)
2789 continue;
2790 if (pd2->bdev->bd_dev == dev) {
2791 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
2792 return -EBUSY;
2794 if (pd2->pkt_dev == dev) {
2795 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
2796 return -EBUSY;
2800 bdev = bdget(dev);
2801 if (!bdev)
2802 return -ENOMEM;
2803 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2804 if (ret)
2805 return ret;
2807 /* This is safe, since we have a reference from open(). */
2808 __module_get(THIS_MODULE);
2810 pd->bdev = bdev;
2811 set_blocksize(bdev, CD_FRAMESIZE);
2813 pkt_init_queue(pd);
2815 atomic_set(&pd->cdrw.pending_bios, 0);
2816 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2817 if (IS_ERR(pd->cdrw.thread)) {
2818 printk(DRIVER_NAME": can't start kernel thread\n");
2819 ret = -ENOMEM;
2820 goto out_mem;
2823 proc = create_proc_entry(pd->name, 0, pkt_proc);
2824 if (proc) {
2825 proc->data = pd;
2826 proc->proc_fops = &pkt_proc_fops;
2828 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2829 return 0;
2831 out_mem:
2832 blkdev_put(bdev);
2833 /* This is safe: open() is still holding a reference. */
2834 module_put(THIS_MODULE);
2835 return ret;
2838 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2840 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2842 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2844 switch (cmd) {
2846 * forward selected CDROM ioctls to CD-ROM, for UDF
2848 case CDROMMULTISESSION:
2849 case CDROMREADTOCENTRY:
2850 case CDROM_LAST_WRITTEN:
2851 case CDROM_SEND_PACKET:
2852 case SCSI_IOCTL_SEND_COMMAND:
2853 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2855 case CDROMEJECT:
2857 * The door gets locked when the device is opened, so we
2858 * have to unlock it or else the eject command fails.
2860 if (pd->refcnt == 1)
2861 pkt_lock_door(pd, 0);
2862 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2864 default:
2865 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
2866 return -ENOTTY;
2869 return 0;
2872 static int pkt_media_changed(struct gendisk *disk)
2874 struct pktcdvd_device *pd = disk->private_data;
2875 struct gendisk *attached_disk;
2877 if (!pd)
2878 return 0;
2879 if (!pd->bdev)
2880 return 0;
2881 attached_disk = pd->bdev->bd_disk;
2882 if (!attached_disk)
2883 return 0;
2884 return attached_disk->fops->media_changed(attached_disk);
2887 static struct block_device_operations pktcdvd_ops = {
2888 .owner = THIS_MODULE,
2889 .open = pkt_open,
2890 .release = pkt_close,
2891 .ioctl = pkt_ioctl,
2892 .media_changed = pkt_media_changed,
2896 * Set up mapping from pktcdvd device to CD-ROM device.
2898 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
2900 int idx;
2901 int ret = -ENOMEM;
2902 struct pktcdvd_device *pd;
2903 struct gendisk *disk;
2905 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2907 for (idx = 0; idx < MAX_WRITERS; idx++)
2908 if (!pkt_devs[idx])
2909 break;
2910 if (idx == MAX_WRITERS) {
2911 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
2912 ret = -EBUSY;
2913 goto out_mutex;
2916 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2917 if (!pd)
2918 goto out_mutex;
2920 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2921 sizeof(struct pkt_rb_node));
2922 if (!pd->rb_pool)
2923 goto out_mem;
2925 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2926 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2927 spin_lock_init(&pd->cdrw.active_list_lock);
2929 spin_lock_init(&pd->lock);
2930 spin_lock_init(&pd->iosched.lock);
2931 sprintf(pd->name, DRIVER_NAME"%d", idx);
2932 init_waitqueue_head(&pd->wqueue);
2933 pd->bio_queue = RB_ROOT;
2935 pd->write_congestion_on = write_congestion_on;
2936 pd->write_congestion_off = write_congestion_off;
2938 disk = alloc_disk(1);
2939 if (!disk)
2940 goto out_mem;
2941 pd->disk = disk;
2942 disk->major = pktdev_major;
2943 disk->first_minor = idx;
2944 disk->fops = &pktcdvd_ops;
2945 disk->flags = GENHD_FL_REMOVABLE;
2946 strcpy(disk->disk_name, pd->name);
2947 disk->private_data = pd;
2948 disk->queue = blk_alloc_queue(GFP_KERNEL);
2949 if (!disk->queue)
2950 goto out_mem2;
2952 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2953 ret = pkt_new_dev(pd, dev);
2954 if (ret)
2955 goto out_new_dev;
2957 add_disk(disk);
2959 pkt_sysfs_dev_new(pd);
2960 pkt_debugfs_dev_new(pd);
2962 pkt_devs[idx] = pd;
2963 if (pkt_dev)
2964 *pkt_dev = pd->pkt_dev;
2966 mutex_unlock(&ctl_mutex);
2967 return 0;
2969 out_new_dev:
2970 blk_cleanup_queue(disk->queue);
2971 out_mem2:
2972 put_disk(disk);
2973 out_mem:
2974 if (pd->rb_pool)
2975 mempool_destroy(pd->rb_pool);
2976 kfree(pd);
2977 out_mutex:
2978 mutex_unlock(&ctl_mutex);
2979 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
2980 return ret;
2984 * Tear down mapping from pktcdvd device to CD-ROM device.
2986 static int pkt_remove_dev(dev_t pkt_dev)
2988 struct pktcdvd_device *pd;
2989 int idx;
2990 int ret = 0;
2992 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2994 for (idx = 0; idx < MAX_WRITERS; idx++) {
2995 pd = pkt_devs[idx];
2996 if (pd && (pd->pkt_dev == pkt_dev))
2997 break;
2999 if (idx == MAX_WRITERS) {
3000 DPRINTK(DRIVER_NAME": dev not setup\n");
3001 ret = -ENXIO;
3002 goto out;
3005 if (pd->refcnt > 0) {
3006 ret = -EBUSY;
3007 goto out;
3009 if (!IS_ERR(pd->cdrw.thread))
3010 kthread_stop(pd->cdrw.thread);
3012 pkt_devs[idx] = NULL;
3014 pkt_debugfs_dev_remove(pd);
3015 pkt_sysfs_dev_remove(pd);
3017 blkdev_put(pd->bdev);
3019 remove_proc_entry(pd->name, pkt_proc);
3020 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
3022 del_gendisk(pd->disk);
3023 blk_cleanup_queue(pd->disk->queue);
3024 put_disk(pd->disk);
3026 mempool_destroy(pd->rb_pool);
3027 kfree(pd);
3029 /* This is safe: open() is still holding a reference. */
3030 module_put(THIS_MODULE);
3032 out:
3033 mutex_unlock(&ctl_mutex);
3034 return ret;
3037 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
3039 struct pktcdvd_device *pd;
3041 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3043 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
3044 if (pd) {
3045 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
3046 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
3047 } else {
3048 ctrl_cmd->dev = 0;
3049 ctrl_cmd->pkt_dev = 0;
3051 ctrl_cmd->num_devices = MAX_WRITERS;
3053 mutex_unlock(&ctl_mutex);
3056 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3058 void __user *argp = (void __user *)arg;
3059 struct pkt_ctrl_command ctrl_cmd;
3060 int ret = 0;
3061 dev_t pkt_dev = 0;
3063 if (cmd != PACKET_CTRL_CMD)
3064 return -ENOTTY;
3066 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3067 return -EFAULT;
3069 switch (ctrl_cmd.command) {
3070 case PKT_CTRL_CMD_SETUP:
3071 if (!capable(CAP_SYS_ADMIN))
3072 return -EPERM;
3073 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3074 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
3075 break;
3076 case PKT_CTRL_CMD_TEARDOWN:
3077 if (!capable(CAP_SYS_ADMIN))
3078 return -EPERM;
3079 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
3080 break;
3081 case PKT_CTRL_CMD_STATUS:
3082 pkt_get_status(&ctrl_cmd);
3083 break;
3084 default:
3085 return -ENOTTY;
3088 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3089 return -EFAULT;
3090 return ret;
3094 static const struct file_operations pkt_ctl_fops = {
3095 .ioctl = pkt_ctl_ioctl,
3096 .owner = THIS_MODULE,
3099 static struct miscdevice pkt_misc = {
3100 .minor = MISC_DYNAMIC_MINOR,
3101 .name = DRIVER_NAME,
3102 .fops = &pkt_ctl_fops
3105 static int __init pkt_init(void)
3107 int ret;
3109 mutex_init(&ctl_mutex);
3111 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3112 sizeof(struct packet_stacked_data));
3113 if (!psd_pool)
3114 return -ENOMEM;
3116 ret = register_blkdev(pktdev_major, DRIVER_NAME);
3117 if (ret < 0) {
3118 printk(DRIVER_NAME": Unable to register block device\n");
3119 goto out2;
3121 if (!pktdev_major)
3122 pktdev_major = ret;
3124 ret = pkt_sysfs_init();
3125 if (ret)
3126 goto out;
3128 pkt_debugfs_init();
3130 ret = misc_register(&pkt_misc);
3131 if (ret) {
3132 printk(DRIVER_NAME": Unable to register misc device\n");
3133 goto out_misc;
3136 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
3138 return 0;
3140 out_misc:
3141 pkt_debugfs_cleanup();
3142 pkt_sysfs_cleanup();
3143 out:
3144 unregister_blkdev(pktdev_major, DRIVER_NAME);
3145 out2:
3146 mempool_destroy(psd_pool);
3147 return ret;
3150 static void __exit pkt_exit(void)
3152 remove_proc_entry(DRIVER_NAME, proc_root_driver);
3153 misc_deregister(&pkt_misc);
3155 pkt_debugfs_cleanup();
3156 pkt_sysfs_cleanup();
3158 unregister_blkdev(pktdev_major, DRIVER_NAME);
3159 mempool_destroy(psd_pool);
3162 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3163 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3164 MODULE_LICENSE("GPL");
3166 module_init(pkt_init);
3167 module_exit(pkt_exit);