driver-core: Add attribute argument to class_attribute show/store
[linux-2.6/cjktty.git] / drivers / block / pktcdvd.c
blob73d815d3f1b27a5fac571d1fa6e8dd7adbac5af2
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; /* /sys/kernel/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, MKDEV(0, 0), NULL,
306 "%s", pd->name);
307 if (IS_ERR(pd->dev))
308 pd->dev = NULL;
310 if (pd->dev) {
311 pd->kobj_stat = pkt_kobj_create(pd, "stat",
312 &pd->dev->kobj,
313 &kobj_pkt_type_stat);
314 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
315 &pd->dev->kobj,
316 &kobj_pkt_type_wqueue);
320 static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
322 pkt_kobj_remove(pd->kobj_stat);
323 pkt_kobj_remove(pd->kobj_wqueue);
324 if (class_pktcdvd)
325 device_unregister(pd->dev);
329 /********************************************************************
330 /sys/class/pktcdvd/
331 add map block device
332 remove unmap packet dev
333 device_map show mappings
334 *******************************************************************/
336 static void class_pktcdvd_release(struct class *cls)
338 kfree(cls);
340 static ssize_t class_pktcdvd_show_map(struct class *c,
341 struct class_attribute *attr,
342 char *data)
344 int n = 0;
345 int idx;
346 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
347 for (idx = 0; idx < MAX_WRITERS; idx++) {
348 struct pktcdvd_device *pd = pkt_devs[idx];
349 if (!pd)
350 continue;
351 n += sprintf(data+n, "%s %u:%u %u:%u\n",
352 pd->name,
353 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
354 MAJOR(pd->bdev->bd_dev),
355 MINOR(pd->bdev->bd_dev));
357 mutex_unlock(&ctl_mutex);
358 return n;
361 static ssize_t class_pktcdvd_store_add(struct class *c,
362 struct class_attribute *attr,
363 const char *buf,
364 size_t count)
366 unsigned int major, minor;
368 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
369 /* pkt_setup_dev() expects caller to hold reference to self */
370 if (!try_module_get(THIS_MODULE))
371 return -ENODEV;
373 pkt_setup_dev(MKDEV(major, minor), NULL);
375 module_put(THIS_MODULE);
377 return count;
380 return -EINVAL;
383 static ssize_t class_pktcdvd_store_remove(struct class *c,
384 struct class_attribute *attr,
385 const char *buf,
386 size_t count)
388 unsigned int major, minor;
389 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
390 pkt_remove_dev(MKDEV(major, minor));
391 return count;
393 return -EINVAL;
396 static struct class_attribute class_pktcdvd_attrs[] = {
397 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
398 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
399 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
400 __ATTR_NULL
404 static int pkt_sysfs_init(void)
406 int ret = 0;
409 * create control files in sysfs
410 * /sys/class/pktcdvd/...
412 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
413 if (!class_pktcdvd)
414 return -ENOMEM;
415 class_pktcdvd->name = DRIVER_NAME;
416 class_pktcdvd->owner = THIS_MODULE;
417 class_pktcdvd->class_release = class_pktcdvd_release;
418 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
419 ret = class_register(class_pktcdvd);
420 if (ret) {
421 kfree(class_pktcdvd);
422 class_pktcdvd = NULL;
423 printk(DRIVER_NAME": failed to create class pktcdvd\n");
424 return ret;
426 return 0;
429 static void pkt_sysfs_cleanup(void)
431 if (class_pktcdvd)
432 class_destroy(class_pktcdvd);
433 class_pktcdvd = NULL;
436 /********************************************************************
437 entries in debugfs
439 /sys/kernel/debug/pktcdvd[0-7]/
440 info
442 *******************************************************************/
444 static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
446 return pkt_seq_show(m, p);
449 static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
451 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
454 static const struct file_operations debug_fops = {
455 .open = pkt_debugfs_fops_open,
456 .read = seq_read,
457 .llseek = seq_lseek,
458 .release = single_release,
459 .owner = THIS_MODULE,
462 static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
464 if (!pkt_debugfs_root)
465 return;
466 pd->dfs_f_info = NULL;
467 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
468 if (IS_ERR(pd->dfs_d_root)) {
469 pd->dfs_d_root = NULL;
470 return;
472 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
473 pd->dfs_d_root, pd, &debug_fops);
474 if (IS_ERR(pd->dfs_f_info)) {
475 pd->dfs_f_info = NULL;
476 return;
480 static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
482 if (!pkt_debugfs_root)
483 return;
484 if (pd->dfs_f_info)
485 debugfs_remove(pd->dfs_f_info);
486 pd->dfs_f_info = NULL;
487 if (pd->dfs_d_root)
488 debugfs_remove(pd->dfs_d_root);
489 pd->dfs_d_root = NULL;
492 static void pkt_debugfs_init(void)
494 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
495 if (IS_ERR(pkt_debugfs_root)) {
496 pkt_debugfs_root = NULL;
497 return;
501 static void pkt_debugfs_cleanup(void)
503 if (!pkt_debugfs_root)
504 return;
505 debugfs_remove(pkt_debugfs_root);
506 pkt_debugfs_root = NULL;
509 /* ----------------------------------------------------------*/
512 static void pkt_bio_finished(struct pktcdvd_device *pd)
514 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
515 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
516 VPRINTK(DRIVER_NAME": queue empty\n");
517 atomic_set(&pd->iosched.attention, 1);
518 wake_up(&pd->wqueue);
522 static void pkt_bio_destructor(struct bio *bio)
524 kfree(bio->bi_io_vec);
525 kfree(bio);
528 static struct bio *pkt_bio_alloc(int nr_iovecs)
530 struct bio_vec *bvl = NULL;
531 struct bio *bio;
533 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
534 if (!bio)
535 goto no_bio;
536 bio_init(bio);
538 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
539 if (!bvl)
540 goto no_bvl;
542 bio->bi_max_vecs = nr_iovecs;
543 bio->bi_io_vec = bvl;
544 bio->bi_destructor = pkt_bio_destructor;
546 return bio;
548 no_bvl:
549 kfree(bio);
550 no_bio:
551 return NULL;
555 * Allocate a packet_data struct
557 static struct packet_data *pkt_alloc_packet_data(int frames)
559 int i;
560 struct packet_data *pkt;
562 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
563 if (!pkt)
564 goto no_pkt;
566 pkt->frames = frames;
567 pkt->w_bio = pkt_bio_alloc(frames);
568 if (!pkt->w_bio)
569 goto no_bio;
571 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
572 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
573 if (!pkt->pages[i])
574 goto no_page;
577 spin_lock_init(&pkt->lock);
578 bio_list_init(&pkt->orig_bios);
580 for (i = 0; i < frames; i++) {
581 struct bio *bio = pkt_bio_alloc(1);
582 if (!bio)
583 goto no_rd_bio;
584 pkt->r_bios[i] = bio;
587 return pkt;
589 no_rd_bio:
590 for (i = 0; i < frames; i++) {
591 struct bio *bio = pkt->r_bios[i];
592 if (bio)
593 bio_put(bio);
596 no_page:
597 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
598 if (pkt->pages[i])
599 __free_page(pkt->pages[i]);
600 bio_put(pkt->w_bio);
601 no_bio:
602 kfree(pkt);
603 no_pkt:
604 return NULL;
608 * Free a packet_data struct
610 static void pkt_free_packet_data(struct packet_data *pkt)
612 int i;
614 for (i = 0; i < pkt->frames; i++) {
615 struct bio *bio = pkt->r_bios[i];
616 if (bio)
617 bio_put(bio);
619 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
620 __free_page(pkt->pages[i]);
621 bio_put(pkt->w_bio);
622 kfree(pkt);
625 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
627 struct packet_data *pkt, *next;
629 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
631 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
632 pkt_free_packet_data(pkt);
634 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
637 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
639 struct packet_data *pkt;
641 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
643 while (nr_packets > 0) {
644 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
645 if (!pkt) {
646 pkt_shrink_pktlist(pd);
647 return 0;
649 pkt->id = nr_packets;
650 pkt->pd = pd;
651 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
652 nr_packets--;
654 return 1;
657 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
659 struct rb_node *n = rb_next(&node->rb_node);
660 if (!n)
661 return NULL;
662 return rb_entry(n, struct pkt_rb_node, rb_node);
665 static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
667 rb_erase(&node->rb_node, &pd->bio_queue);
668 mempool_free(node, pd->rb_pool);
669 pd->bio_queue_size--;
670 BUG_ON(pd->bio_queue_size < 0);
674 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
676 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
678 struct rb_node *n = pd->bio_queue.rb_node;
679 struct rb_node *next;
680 struct pkt_rb_node *tmp;
682 if (!n) {
683 BUG_ON(pd->bio_queue_size > 0);
684 return NULL;
687 for (;;) {
688 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
689 if (s <= tmp->bio->bi_sector)
690 next = n->rb_left;
691 else
692 next = n->rb_right;
693 if (!next)
694 break;
695 n = next;
698 if (s > tmp->bio->bi_sector) {
699 tmp = pkt_rbtree_next(tmp);
700 if (!tmp)
701 return NULL;
703 BUG_ON(s > tmp->bio->bi_sector);
704 return tmp;
708 * Insert a node into the pd->bio_queue rb tree.
710 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
712 struct rb_node **p = &pd->bio_queue.rb_node;
713 struct rb_node *parent = NULL;
714 sector_t s = node->bio->bi_sector;
715 struct pkt_rb_node *tmp;
717 while (*p) {
718 parent = *p;
719 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
720 if (s < tmp->bio->bi_sector)
721 p = &(*p)->rb_left;
722 else
723 p = &(*p)->rb_right;
725 rb_link_node(&node->rb_node, parent, p);
726 rb_insert_color(&node->rb_node, &pd->bio_queue);
727 pd->bio_queue_size++;
731 * Send a packet_command to the underlying block device and
732 * wait for completion.
734 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
736 struct request_queue *q = bdev_get_queue(pd->bdev);
737 struct request *rq;
738 int ret = 0;
740 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
741 WRITE : READ, __GFP_WAIT);
743 if (cgc->buflen) {
744 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
745 goto out;
748 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
749 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
751 rq->timeout = 60*HZ;
752 rq->cmd_type = REQ_TYPE_BLOCK_PC;
753 rq->cmd_flags |= REQ_HARDBARRIER;
754 if (cgc->quiet)
755 rq->cmd_flags |= REQ_QUIET;
757 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
758 if (rq->errors)
759 ret = -EIO;
760 out:
761 blk_put_request(rq);
762 return ret;
766 * A generic sense dump / resolve mechanism should be implemented across
767 * all ATAPI + SCSI devices.
769 static void pkt_dump_sense(struct packet_command *cgc)
771 static char *info[9] = { "No sense", "Recovered error", "Not ready",
772 "Medium error", "Hardware error", "Illegal request",
773 "Unit attention", "Data protect", "Blank check" };
774 int i;
775 struct request_sense *sense = cgc->sense;
777 printk(DRIVER_NAME":");
778 for (i = 0; i < CDROM_PACKET_SIZE; i++)
779 printk(" %02x", cgc->cmd[i]);
780 printk(" - ");
782 if (sense == NULL) {
783 printk("no sense\n");
784 return;
787 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
789 if (sense->sense_key > 8) {
790 printk(" (INVALID)\n");
791 return;
794 printk(" (%s)\n", info[sense->sense_key]);
798 * flush the drive cache to media
800 static int pkt_flush_cache(struct pktcdvd_device *pd)
802 struct packet_command cgc;
804 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
805 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
806 cgc.quiet = 1;
809 * the IMMED bit -- we default to not setting it, although that
810 * would allow a much faster close, this is safer
812 #if 0
813 cgc.cmd[1] = 1 << 1;
814 #endif
815 return pkt_generic_packet(pd, &cgc);
819 * speed is given as the normal factor, e.g. 4 for 4x
821 static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
822 unsigned write_speed, unsigned read_speed)
824 struct packet_command cgc;
825 struct request_sense sense;
826 int ret;
828 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
829 cgc.sense = &sense;
830 cgc.cmd[0] = GPCMD_SET_SPEED;
831 cgc.cmd[2] = (read_speed >> 8) & 0xff;
832 cgc.cmd[3] = read_speed & 0xff;
833 cgc.cmd[4] = (write_speed >> 8) & 0xff;
834 cgc.cmd[5] = write_speed & 0xff;
836 if ((ret = pkt_generic_packet(pd, &cgc)))
837 pkt_dump_sense(&cgc);
839 return ret;
843 * Queue a bio for processing by the low-level CD device. Must be called
844 * from process context.
846 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
848 spin_lock(&pd->iosched.lock);
849 if (bio_data_dir(bio) == READ)
850 bio_list_add(&pd->iosched.read_queue, bio);
851 else
852 bio_list_add(&pd->iosched.write_queue, bio);
853 spin_unlock(&pd->iosched.lock);
855 atomic_set(&pd->iosched.attention, 1);
856 wake_up(&pd->wqueue);
860 * Process the queued read/write requests. This function handles special
861 * requirements for CDRW drives:
862 * - A cache flush command must be inserted before a read request if the
863 * previous request was a write.
864 * - Switching between reading and writing is slow, so don't do it more often
865 * than necessary.
866 * - Optimize for throughput at the expense of latency. This means that streaming
867 * writes will never be interrupted by a read, but if the drive has to seek
868 * before the next write, switch to reading instead if there are any pending
869 * read requests.
870 * - Set the read speed according to current usage pattern. When only reading
871 * from the device, it's best to use the highest possible read speed, but
872 * when switching often between reading and writing, it's better to have the
873 * same read and write speeds.
875 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
878 if (atomic_read(&pd->iosched.attention) == 0)
879 return;
880 atomic_set(&pd->iosched.attention, 0);
882 for (;;) {
883 struct bio *bio;
884 int reads_queued, writes_queued;
886 spin_lock(&pd->iosched.lock);
887 reads_queued = !bio_list_empty(&pd->iosched.read_queue);
888 writes_queued = !bio_list_empty(&pd->iosched.write_queue);
889 spin_unlock(&pd->iosched.lock);
891 if (!reads_queued && !writes_queued)
892 break;
894 if (pd->iosched.writing) {
895 int need_write_seek = 1;
896 spin_lock(&pd->iosched.lock);
897 bio = bio_list_peek(&pd->iosched.write_queue);
898 spin_unlock(&pd->iosched.lock);
899 if (bio && (bio->bi_sector == pd->iosched.last_write))
900 need_write_seek = 0;
901 if (need_write_seek && reads_queued) {
902 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
903 VPRINTK(DRIVER_NAME": write, waiting\n");
904 break;
906 pkt_flush_cache(pd);
907 pd->iosched.writing = 0;
909 } else {
910 if (!reads_queued && writes_queued) {
911 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
912 VPRINTK(DRIVER_NAME": read, waiting\n");
913 break;
915 pd->iosched.writing = 1;
919 spin_lock(&pd->iosched.lock);
920 if (pd->iosched.writing)
921 bio = bio_list_pop(&pd->iosched.write_queue);
922 else
923 bio = bio_list_pop(&pd->iosched.read_queue);
924 spin_unlock(&pd->iosched.lock);
926 if (!bio)
927 continue;
929 if (bio_data_dir(bio) == READ)
930 pd->iosched.successive_reads += bio->bi_size >> 10;
931 else {
932 pd->iosched.successive_reads = 0;
933 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
935 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
936 if (pd->read_speed == pd->write_speed) {
937 pd->read_speed = MAX_SPEED;
938 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
940 } else {
941 if (pd->read_speed != pd->write_speed) {
942 pd->read_speed = pd->write_speed;
943 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
947 atomic_inc(&pd->cdrw.pending_bios);
948 generic_make_request(bio);
953 * Special care is needed if the underlying block device has a small
954 * max_phys_segments value.
956 static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
958 if ((pd->settings.size << 9) / CD_FRAMESIZE
959 <= queue_max_segments(q)) {
961 * The cdrom device can handle one segment/frame
963 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
964 return 0;
965 } else if ((pd->settings.size << 9) / PAGE_SIZE
966 <= queue_max_segments(q)) {
968 * We can handle this case at the expense of some extra memory
969 * copies during write operations
971 set_bit(PACKET_MERGE_SEGS, &pd->flags);
972 return 0;
973 } else {
974 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
975 return -EIO;
980 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
982 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
984 unsigned int copy_size = CD_FRAMESIZE;
986 while (copy_size > 0) {
987 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
988 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
989 src_bvl->bv_offset + offs;
990 void *vto = page_address(dst_page) + dst_offs;
991 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
993 BUG_ON(len < 0);
994 memcpy(vto, vfrom, len);
995 kunmap_atomic(vfrom, KM_USER0);
997 seg++;
998 offs = 0;
999 dst_offs += len;
1000 copy_size -= len;
1005 * Copy all data for this packet to pkt->pages[], so that
1006 * a) The number of required segments for the write bio is minimized, which
1007 * is necessary for some scsi controllers.
1008 * b) The data can be used as cache to avoid read requests if we receive a
1009 * new write request for the same zone.
1011 static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
1013 int f, p, offs;
1015 /* Copy all data to pkt->pages[] */
1016 p = 0;
1017 offs = 0;
1018 for (f = 0; f < pkt->frames; f++) {
1019 if (bvec[f].bv_page != pkt->pages[p]) {
1020 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
1021 void *vto = page_address(pkt->pages[p]) + offs;
1022 memcpy(vto, vfrom, CD_FRAMESIZE);
1023 kunmap_atomic(vfrom, KM_USER0);
1024 bvec[f].bv_page = pkt->pages[p];
1025 bvec[f].bv_offset = offs;
1026 } else {
1027 BUG_ON(bvec[f].bv_offset != offs);
1029 offs += CD_FRAMESIZE;
1030 if (offs >= PAGE_SIZE) {
1031 offs = 0;
1032 p++;
1037 static void pkt_end_io_read(struct bio *bio, int err)
1039 struct packet_data *pkt = bio->bi_private;
1040 struct pktcdvd_device *pd = pkt->pd;
1041 BUG_ON(!pd);
1043 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1044 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1046 if (err)
1047 atomic_inc(&pkt->io_errors);
1048 if (atomic_dec_and_test(&pkt->io_wait)) {
1049 atomic_inc(&pkt->run_sm);
1050 wake_up(&pd->wqueue);
1052 pkt_bio_finished(pd);
1055 static void pkt_end_io_packet_write(struct bio *bio, int err)
1057 struct packet_data *pkt = bio->bi_private;
1058 struct pktcdvd_device *pd = pkt->pd;
1059 BUG_ON(!pd);
1061 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1063 pd->stats.pkt_ended++;
1065 pkt_bio_finished(pd);
1066 atomic_dec(&pkt->io_wait);
1067 atomic_inc(&pkt->run_sm);
1068 wake_up(&pd->wqueue);
1072 * Schedule reads for the holes in a packet
1074 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1076 int frames_read = 0;
1077 struct bio *bio;
1078 int f;
1079 char written[PACKET_MAX_SIZE];
1081 BUG_ON(bio_list_empty(&pkt->orig_bios));
1083 atomic_set(&pkt->io_wait, 0);
1084 atomic_set(&pkt->io_errors, 0);
1087 * Figure out which frames we need to read before we can write.
1089 memset(written, 0, sizeof(written));
1090 spin_lock(&pkt->lock);
1091 bio_list_for_each(bio, &pkt->orig_bios) {
1092 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1093 int num_frames = bio->bi_size / CD_FRAMESIZE;
1094 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
1095 BUG_ON(first_frame < 0);
1096 BUG_ON(first_frame + num_frames > pkt->frames);
1097 for (f = first_frame; f < first_frame + num_frames; f++)
1098 written[f] = 1;
1100 spin_unlock(&pkt->lock);
1102 if (pkt->cache_valid) {
1103 VPRINTK("pkt_gather_data: zone %llx cached\n",
1104 (unsigned long long)pkt->sector);
1105 goto out_account;
1109 * Schedule reads for missing parts of the packet.
1111 for (f = 0; f < pkt->frames; f++) {
1112 struct bio_vec *vec;
1114 int p, offset;
1115 if (written[f])
1116 continue;
1117 bio = pkt->r_bios[f];
1118 vec = bio->bi_io_vec;
1119 bio_init(bio);
1120 bio->bi_max_vecs = 1;
1121 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1122 bio->bi_bdev = pd->bdev;
1123 bio->bi_end_io = pkt_end_io_read;
1124 bio->bi_private = pkt;
1125 bio->bi_io_vec = vec;
1126 bio->bi_destructor = pkt_bio_destructor;
1128 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1129 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1130 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1131 f, pkt->pages[p], offset);
1132 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1133 BUG();
1135 atomic_inc(&pkt->io_wait);
1136 bio->bi_rw = READ;
1137 pkt_queue_bio(pd, bio);
1138 frames_read++;
1141 out_account:
1142 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1143 frames_read, (unsigned long long)pkt->sector);
1144 pd->stats.pkt_started++;
1145 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
1149 * Find a packet matching zone, or the least recently used packet if
1150 * there is no match.
1152 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1154 struct packet_data *pkt;
1156 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1157 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1158 list_del_init(&pkt->list);
1159 if (pkt->sector != zone)
1160 pkt->cache_valid = 0;
1161 return pkt;
1164 BUG();
1165 return NULL;
1168 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1170 if (pkt->cache_valid) {
1171 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1172 } else {
1173 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1178 * recover a failed write, query for relocation if possible
1180 * returns 1 if recovery is possible, or 0 if not
1183 static int pkt_start_recovery(struct packet_data *pkt)
1186 * FIXME. We need help from the file system to implement
1187 * recovery handling.
1189 return 0;
1190 #if 0
1191 struct request *rq = pkt->rq;
1192 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1193 struct block_device *pkt_bdev;
1194 struct super_block *sb = NULL;
1195 unsigned long old_block, new_block;
1196 sector_t new_sector;
1198 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1199 if (pkt_bdev) {
1200 sb = get_super(pkt_bdev);
1201 bdput(pkt_bdev);
1204 if (!sb)
1205 return 0;
1207 if (!sb->s_op || !sb->s_op->relocate_blocks)
1208 goto out;
1210 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1211 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1212 goto out;
1214 new_sector = new_block * (CD_FRAMESIZE >> 9);
1215 pkt->sector = new_sector;
1217 pkt->bio->bi_sector = new_sector;
1218 pkt->bio->bi_next = NULL;
1219 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1220 pkt->bio->bi_idx = 0;
1222 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1223 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1224 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1225 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1226 BUG_ON(pkt->bio->bi_private != pkt);
1228 drop_super(sb);
1229 return 1;
1231 out:
1232 drop_super(sb);
1233 return 0;
1234 #endif
1237 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1239 #if PACKET_DEBUG > 1
1240 static const char *state_name[] = {
1241 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1243 enum packet_data_state old_state = pkt->state;
1244 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1245 state_name[old_state], state_name[state]);
1246 #endif
1247 pkt->state = state;
1251 * Scan the work queue to see if we can start a new packet.
1252 * returns non-zero if any work was done.
1254 static int pkt_handle_queue(struct pktcdvd_device *pd)
1256 struct packet_data *pkt, *p;
1257 struct bio *bio = NULL;
1258 sector_t zone = 0; /* Suppress gcc warning */
1259 struct pkt_rb_node *node, *first_node;
1260 struct rb_node *n;
1261 int wakeup;
1263 VPRINTK("handle_queue\n");
1265 atomic_set(&pd->scan_queue, 0);
1267 if (list_empty(&pd->cdrw.pkt_free_list)) {
1268 VPRINTK("handle_queue: no pkt\n");
1269 return 0;
1273 * Try to find a zone we are not already working on.
1275 spin_lock(&pd->lock);
1276 first_node = pkt_rbtree_find(pd, pd->current_sector);
1277 if (!first_node) {
1278 n = rb_first(&pd->bio_queue);
1279 if (n)
1280 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1282 node = first_node;
1283 while (node) {
1284 bio = node->bio;
1285 zone = ZONE(bio->bi_sector, pd);
1286 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
1287 if (p->sector == zone) {
1288 bio = NULL;
1289 goto try_next_bio;
1292 break;
1293 try_next_bio:
1294 node = pkt_rbtree_next(node);
1295 if (!node) {
1296 n = rb_first(&pd->bio_queue);
1297 if (n)
1298 node = rb_entry(n, struct pkt_rb_node, rb_node);
1300 if (node == first_node)
1301 node = NULL;
1303 spin_unlock(&pd->lock);
1304 if (!bio) {
1305 VPRINTK("handle_queue: no bio\n");
1306 return 0;
1309 pkt = pkt_get_packet_data(pd, zone);
1311 pd->current_sector = zone + pd->settings.size;
1312 pkt->sector = zone;
1313 BUG_ON(pkt->frames != pd->settings.size >> 2);
1314 pkt->write_size = 0;
1317 * Scan work queue for bios in the same zone and link them
1318 * to this packet.
1320 spin_lock(&pd->lock);
1321 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1322 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1323 bio = node->bio;
1324 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1325 (unsigned long long)ZONE(bio->bi_sector, pd));
1326 if (ZONE(bio->bi_sector, pd) != zone)
1327 break;
1328 pkt_rbtree_erase(pd, node);
1329 spin_lock(&pkt->lock);
1330 bio_list_add(&pkt->orig_bios, bio);
1331 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1332 spin_unlock(&pkt->lock);
1334 /* check write congestion marks, and if bio_queue_size is
1335 below, wake up any waiters */
1336 wakeup = (pd->write_congestion_on > 0
1337 && pd->bio_queue_size <= pd->write_congestion_off);
1338 spin_unlock(&pd->lock);
1339 if (wakeup) {
1340 clear_bdi_congested(&pd->disk->queue->backing_dev_info,
1341 BLK_RW_ASYNC);
1344 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1345 pkt_set_state(pkt, PACKET_WAITING_STATE);
1346 atomic_set(&pkt->run_sm, 1);
1348 spin_lock(&pd->cdrw.active_list_lock);
1349 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1350 spin_unlock(&pd->cdrw.active_list_lock);
1352 return 1;
1356 * Assemble a bio to write one packet and queue the bio for processing
1357 * by the underlying block device.
1359 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1361 struct bio *bio;
1362 int f;
1363 int frames_write;
1364 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
1366 for (f = 0; f < pkt->frames; f++) {
1367 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1368 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1372 * Fill-in bvec with data from orig_bios.
1374 frames_write = 0;
1375 spin_lock(&pkt->lock);
1376 bio_list_for_each(bio, &pkt->orig_bios) {
1377 int segment = bio->bi_idx;
1378 int src_offs = 0;
1379 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1380 int num_frames = bio->bi_size / CD_FRAMESIZE;
1381 BUG_ON(first_frame < 0);
1382 BUG_ON(first_frame + num_frames > pkt->frames);
1383 for (f = first_frame; f < first_frame + num_frames; f++) {
1384 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1386 while (src_offs >= src_bvl->bv_len) {
1387 src_offs -= src_bvl->bv_len;
1388 segment++;
1389 BUG_ON(segment >= bio->bi_vcnt);
1390 src_bvl = bio_iovec_idx(bio, segment);
1393 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1394 bvec[f].bv_page = src_bvl->bv_page;
1395 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1396 } else {
1397 pkt_copy_bio_data(bio, segment, src_offs,
1398 bvec[f].bv_page, bvec[f].bv_offset);
1400 src_offs += CD_FRAMESIZE;
1401 frames_write++;
1404 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1405 spin_unlock(&pkt->lock);
1407 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1408 frames_write, (unsigned long long)pkt->sector);
1409 BUG_ON(frames_write != pkt->write_size);
1411 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1412 pkt_make_local_copy(pkt, bvec);
1413 pkt->cache_valid = 1;
1414 } else {
1415 pkt->cache_valid = 0;
1418 /* Start the write request */
1419 bio_init(pkt->w_bio);
1420 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1421 pkt->w_bio->bi_sector = pkt->sector;
1422 pkt->w_bio->bi_bdev = pd->bdev;
1423 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1424 pkt->w_bio->bi_private = pkt;
1425 pkt->w_bio->bi_io_vec = bvec;
1426 pkt->w_bio->bi_destructor = pkt_bio_destructor;
1427 for (f = 0; f < pkt->frames; f++)
1428 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1429 BUG();
1430 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
1432 atomic_set(&pkt->io_wait, 1);
1433 pkt->w_bio->bi_rw = WRITE;
1434 pkt_queue_bio(pd, pkt->w_bio);
1437 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1439 struct bio *bio;
1441 if (!uptodate)
1442 pkt->cache_valid = 0;
1444 /* Finish all bios corresponding to this packet */
1445 while ((bio = bio_list_pop(&pkt->orig_bios)))
1446 bio_endio(bio, uptodate ? 0 : -EIO);
1449 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1451 int uptodate;
1453 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1455 for (;;) {
1456 switch (pkt->state) {
1457 case PACKET_WAITING_STATE:
1458 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1459 return;
1461 pkt->sleep_time = 0;
1462 pkt_gather_data(pd, pkt);
1463 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1464 break;
1466 case PACKET_READ_WAIT_STATE:
1467 if (atomic_read(&pkt->io_wait) > 0)
1468 return;
1470 if (atomic_read(&pkt->io_errors) > 0) {
1471 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1472 } else {
1473 pkt_start_write(pd, pkt);
1475 break;
1477 case PACKET_WRITE_WAIT_STATE:
1478 if (atomic_read(&pkt->io_wait) > 0)
1479 return;
1481 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1482 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1483 } else {
1484 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1486 break;
1488 case PACKET_RECOVERY_STATE:
1489 if (pkt_start_recovery(pkt)) {
1490 pkt_start_write(pd, pkt);
1491 } else {
1492 VPRINTK("No recovery possible\n");
1493 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1495 break;
1497 case PACKET_FINISHED_STATE:
1498 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1499 pkt_finish_packet(pkt, uptodate);
1500 return;
1502 default:
1503 BUG();
1504 break;
1509 static void pkt_handle_packets(struct pktcdvd_device *pd)
1511 struct packet_data *pkt, *next;
1513 VPRINTK("pkt_handle_packets\n");
1516 * Run state machine for active packets
1518 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1519 if (atomic_read(&pkt->run_sm) > 0) {
1520 atomic_set(&pkt->run_sm, 0);
1521 pkt_run_state_machine(pd, pkt);
1526 * Move no longer active packets to the free list
1528 spin_lock(&pd->cdrw.active_list_lock);
1529 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1530 if (pkt->state == PACKET_FINISHED_STATE) {
1531 list_del(&pkt->list);
1532 pkt_put_packet_data(pd, pkt);
1533 pkt_set_state(pkt, PACKET_IDLE_STATE);
1534 atomic_set(&pd->scan_queue, 1);
1537 spin_unlock(&pd->cdrw.active_list_lock);
1540 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1542 struct packet_data *pkt;
1543 int i;
1545 for (i = 0; i < PACKET_NUM_STATES; i++)
1546 states[i] = 0;
1548 spin_lock(&pd->cdrw.active_list_lock);
1549 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1550 states[pkt->state]++;
1552 spin_unlock(&pd->cdrw.active_list_lock);
1556 * kcdrwd is woken up when writes have been queued for one of our
1557 * registered devices
1559 static int kcdrwd(void *foobar)
1561 struct pktcdvd_device *pd = foobar;
1562 struct packet_data *pkt;
1563 long min_sleep_time, residue;
1565 set_user_nice(current, -20);
1566 set_freezable();
1568 for (;;) {
1569 DECLARE_WAITQUEUE(wait, current);
1572 * Wait until there is something to do
1574 add_wait_queue(&pd->wqueue, &wait);
1575 for (;;) {
1576 set_current_state(TASK_INTERRUPTIBLE);
1578 /* Check if we need to run pkt_handle_queue */
1579 if (atomic_read(&pd->scan_queue) > 0)
1580 goto work_to_do;
1582 /* Check if we need to run the state machine for some packet */
1583 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1584 if (atomic_read(&pkt->run_sm) > 0)
1585 goto work_to_do;
1588 /* Check if we need to process the iosched queues */
1589 if (atomic_read(&pd->iosched.attention) != 0)
1590 goto work_to_do;
1592 /* Otherwise, go to sleep */
1593 if (PACKET_DEBUG > 1) {
1594 int states[PACKET_NUM_STATES];
1595 pkt_count_states(pd, states);
1596 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1597 states[0], states[1], states[2], states[3],
1598 states[4], states[5]);
1601 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1602 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1603 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1604 min_sleep_time = pkt->sleep_time;
1607 generic_unplug_device(bdev_get_queue(pd->bdev));
1609 VPRINTK("kcdrwd: sleeping\n");
1610 residue = schedule_timeout(min_sleep_time);
1611 VPRINTK("kcdrwd: wake up\n");
1613 /* make swsusp happy with our thread */
1614 try_to_freeze();
1616 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1617 if (!pkt->sleep_time)
1618 continue;
1619 pkt->sleep_time -= min_sleep_time - residue;
1620 if (pkt->sleep_time <= 0) {
1621 pkt->sleep_time = 0;
1622 atomic_inc(&pkt->run_sm);
1626 if (kthread_should_stop())
1627 break;
1629 work_to_do:
1630 set_current_state(TASK_RUNNING);
1631 remove_wait_queue(&pd->wqueue, &wait);
1633 if (kthread_should_stop())
1634 break;
1637 * if pkt_handle_queue returns true, we can queue
1638 * another request.
1640 while (pkt_handle_queue(pd))
1644 * Handle packet state machine
1646 pkt_handle_packets(pd);
1649 * Handle iosched queues
1651 pkt_iosched_process_queue(pd);
1654 return 0;
1657 static void pkt_print_settings(struct pktcdvd_device *pd)
1659 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1660 printk("%u blocks, ", pd->settings.size >> 2);
1661 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1664 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1666 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1668 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1669 cgc->cmd[2] = page_code | (page_control << 6);
1670 cgc->cmd[7] = cgc->buflen >> 8;
1671 cgc->cmd[8] = cgc->buflen & 0xff;
1672 cgc->data_direction = CGC_DATA_READ;
1673 return pkt_generic_packet(pd, cgc);
1676 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1678 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1679 memset(cgc->buffer, 0, 2);
1680 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1681 cgc->cmd[1] = 0x10; /* PF */
1682 cgc->cmd[7] = cgc->buflen >> 8;
1683 cgc->cmd[8] = cgc->buflen & 0xff;
1684 cgc->data_direction = CGC_DATA_WRITE;
1685 return pkt_generic_packet(pd, cgc);
1688 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1690 struct packet_command cgc;
1691 int ret;
1693 /* set up command and get the disc info */
1694 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1695 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1696 cgc.cmd[8] = cgc.buflen = 2;
1697 cgc.quiet = 1;
1699 if ((ret = pkt_generic_packet(pd, &cgc)))
1700 return ret;
1702 /* not all drives have the same disc_info length, so requeue
1703 * packet with the length the drive tells us it can supply
1705 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1706 sizeof(di->disc_information_length);
1708 if (cgc.buflen > sizeof(disc_information))
1709 cgc.buflen = sizeof(disc_information);
1711 cgc.cmd[8] = cgc.buflen;
1712 return pkt_generic_packet(pd, &cgc);
1715 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1717 struct packet_command cgc;
1718 int ret;
1720 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1721 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1722 cgc.cmd[1] = type & 3;
1723 cgc.cmd[4] = (track & 0xff00) >> 8;
1724 cgc.cmd[5] = track & 0xff;
1725 cgc.cmd[8] = 8;
1726 cgc.quiet = 1;
1728 if ((ret = pkt_generic_packet(pd, &cgc)))
1729 return ret;
1731 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1732 sizeof(ti->track_information_length);
1734 if (cgc.buflen > sizeof(track_information))
1735 cgc.buflen = sizeof(track_information);
1737 cgc.cmd[8] = cgc.buflen;
1738 return pkt_generic_packet(pd, &cgc);
1741 static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1742 long *last_written)
1744 disc_information di;
1745 track_information ti;
1746 __u32 last_track;
1747 int ret = -1;
1749 if ((ret = pkt_get_disc_info(pd, &di)))
1750 return ret;
1752 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1753 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1754 return ret;
1756 /* if this track is blank, try the previous. */
1757 if (ti.blank) {
1758 last_track--;
1759 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1760 return ret;
1763 /* if last recorded field is valid, return it. */
1764 if (ti.lra_v) {
1765 *last_written = be32_to_cpu(ti.last_rec_address);
1766 } else {
1767 /* make it up instead */
1768 *last_written = be32_to_cpu(ti.track_start) +
1769 be32_to_cpu(ti.track_size);
1770 if (ti.free_blocks)
1771 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1773 return 0;
1777 * write mode select package based on pd->settings
1779 static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
1781 struct packet_command cgc;
1782 struct request_sense sense;
1783 write_param_page *wp;
1784 char buffer[128];
1785 int ret, size;
1787 /* doesn't apply to DVD+RW or DVD-RAM */
1788 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1789 return 0;
1791 memset(buffer, 0, sizeof(buffer));
1792 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1793 cgc.sense = &sense;
1794 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1795 pkt_dump_sense(&cgc);
1796 return ret;
1799 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1800 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1801 if (size > sizeof(buffer))
1802 size = sizeof(buffer);
1805 * now get it all
1807 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1808 cgc.sense = &sense;
1809 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1810 pkt_dump_sense(&cgc);
1811 return ret;
1815 * write page is offset header + block descriptor length
1817 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1819 wp->fp = pd->settings.fp;
1820 wp->track_mode = pd->settings.track_mode;
1821 wp->write_type = pd->settings.write_type;
1822 wp->data_block_type = pd->settings.block_mode;
1824 wp->multi_session = 0;
1826 #ifdef PACKET_USE_LS
1827 wp->link_size = 7;
1828 wp->ls_v = 1;
1829 #endif
1831 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1832 wp->session_format = 0;
1833 wp->subhdr2 = 0x20;
1834 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1835 wp->session_format = 0x20;
1836 wp->subhdr2 = 8;
1837 #if 0
1838 wp->mcn[0] = 0x80;
1839 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1840 #endif
1841 } else {
1843 * paranoia
1845 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
1846 return 1;
1848 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1850 cgc.buflen = cgc.cmd[8] = size;
1851 if ((ret = pkt_mode_select(pd, &cgc))) {
1852 pkt_dump_sense(&cgc);
1853 return ret;
1856 pkt_print_settings(pd);
1857 return 0;
1861 * 1 -- we can write to this track, 0 -- we can't
1863 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1865 switch (pd->mmc3_profile) {
1866 case 0x1a: /* DVD+RW */
1867 case 0x12: /* DVD-RAM */
1868 /* The track is always writable on DVD+RW/DVD-RAM */
1869 return 1;
1870 default:
1871 break;
1874 if (!ti->packet || !ti->fp)
1875 return 0;
1878 * "good" settings as per Mt Fuji.
1880 if (ti->rt == 0 && ti->blank == 0)
1881 return 1;
1883 if (ti->rt == 0 && ti->blank == 1)
1884 return 1;
1886 if (ti->rt == 1 && ti->blank == 0)
1887 return 1;
1889 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1890 return 0;
1894 * 1 -- we can write to this disc, 0 -- we can't
1896 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1898 switch (pd->mmc3_profile) {
1899 case 0x0a: /* CD-RW */
1900 case 0xffff: /* MMC3 not supported */
1901 break;
1902 case 0x1a: /* DVD+RW */
1903 case 0x13: /* DVD-RW */
1904 case 0x12: /* DVD-RAM */
1905 return 1;
1906 default:
1907 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
1908 return 0;
1912 * for disc type 0xff we should probably reserve a new track.
1913 * but i'm not sure, should we leave this to user apps? probably.
1915 if (di->disc_type == 0xff) {
1916 printk(DRIVER_NAME": Unknown disc. No track?\n");
1917 return 0;
1920 if (di->disc_type != 0x20 && di->disc_type != 0) {
1921 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
1922 return 0;
1925 if (di->erasable == 0) {
1926 printk(DRIVER_NAME": Disc not erasable\n");
1927 return 0;
1930 if (di->border_status == PACKET_SESSION_RESERVED) {
1931 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
1932 return 0;
1935 return 1;
1938 static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
1940 struct packet_command cgc;
1941 unsigned char buf[12];
1942 disc_information di;
1943 track_information ti;
1944 int ret, track;
1946 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1947 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1948 cgc.cmd[8] = 8;
1949 ret = pkt_generic_packet(pd, &cgc);
1950 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1952 memset(&di, 0, sizeof(disc_information));
1953 memset(&ti, 0, sizeof(track_information));
1955 if ((ret = pkt_get_disc_info(pd, &di))) {
1956 printk("failed get_disc\n");
1957 return ret;
1960 if (!pkt_writable_disc(pd, &di))
1961 return -EROFS;
1963 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1965 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1966 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1967 printk(DRIVER_NAME": failed get_track\n");
1968 return ret;
1971 if (!pkt_writable_track(pd, &ti)) {
1972 printk(DRIVER_NAME": can't write to this track\n");
1973 return -EROFS;
1977 * we keep packet size in 512 byte units, makes it easier to
1978 * deal with request calculations.
1980 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1981 if (pd->settings.size == 0) {
1982 printk(DRIVER_NAME": detected zero packet size!\n");
1983 return -ENXIO;
1985 if (pd->settings.size > PACKET_MAX_SECTORS) {
1986 printk(DRIVER_NAME": packet size is too big\n");
1987 return -EROFS;
1989 pd->settings.fp = ti.fp;
1990 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1992 if (ti.nwa_v) {
1993 pd->nwa = be32_to_cpu(ti.next_writable);
1994 set_bit(PACKET_NWA_VALID, &pd->flags);
1998 * in theory we could use lra on -RW media as well and just zero
1999 * blocks that haven't been written yet, but in practice that
2000 * is just a no-go. we'll use that for -R, naturally.
2002 if (ti.lra_v) {
2003 pd->lra = be32_to_cpu(ti.last_rec_address);
2004 set_bit(PACKET_LRA_VALID, &pd->flags);
2005 } else {
2006 pd->lra = 0xffffffff;
2007 set_bit(PACKET_LRA_VALID, &pd->flags);
2011 * fine for now
2013 pd->settings.link_loss = 7;
2014 pd->settings.write_type = 0; /* packet */
2015 pd->settings.track_mode = ti.track_mode;
2018 * mode1 or mode2 disc
2020 switch (ti.data_mode) {
2021 case PACKET_MODE1:
2022 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2023 break;
2024 case PACKET_MODE2:
2025 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2026 break;
2027 default:
2028 printk(DRIVER_NAME": unknown data mode\n");
2029 return -EROFS;
2031 return 0;
2035 * enable/disable write caching on drive
2037 static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2038 int set)
2040 struct packet_command cgc;
2041 struct request_sense sense;
2042 unsigned char buf[64];
2043 int ret;
2045 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2046 cgc.sense = &sense;
2047 cgc.buflen = pd->mode_offset + 12;
2050 * caching mode page might not be there, so quiet this command
2052 cgc.quiet = 1;
2054 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2055 return ret;
2057 buf[pd->mode_offset + 10] |= (!!set << 2);
2059 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2060 ret = pkt_mode_select(pd, &cgc);
2061 if (ret) {
2062 printk(DRIVER_NAME": write caching control failed\n");
2063 pkt_dump_sense(&cgc);
2064 } else if (!ret && set)
2065 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
2066 return ret;
2069 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2071 struct packet_command cgc;
2073 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2074 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2075 cgc.cmd[4] = lockflag ? 1 : 0;
2076 return pkt_generic_packet(pd, &cgc);
2080 * Returns drive maximum write speed
2082 static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2083 unsigned *write_speed)
2085 struct packet_command cgc;
2086 struct request_sense sense;
2087 unsigned char buf[256+18];
2088 unsigned char *cap_buf;
2089 int ret, offset;
2091 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2092 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2093 cgc.sense = &sense;
2095 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2096 if (ret) {
2097 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2098 sizeof(struct mode_page_header);
2099 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2100 if (ret) {
2101 pkt_dump_sense(&cgc);
2102 return ret;
2106 offset = 20; /* Obsoleted field, used by older drives */
2107 if (cap_buf[1] >= 28)
2108 offset = 28; /* Current write speed selected */
2109 if (cap_buf[1] >= 30) {
2110 /* If the drive reports at least one "Logical Unit Write
2111 * Speed Performance Descriptor Block", use the information
2112 * in the first block. (contains the highest speed)
2114 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2115 if (num_spdb > 0)
2116 offset = 34;
2119 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2120 return 0;
2123 /* These tables from cdrecord - I don't have orange book */
2124 /* standard speed CD-RW (1-4x) */
2125 static char clv_to_speed[16] = {
2126 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2127 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2129 /* high speed CD-RW (-10x) */
2130 static char hs_clv_to_speed[16] = {
2131 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2132 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2134 /* ultra high speed CD-RW */
2135 static char us_clv_to_speed[16] = {
2136 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2137 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2141 * reads the maximum media speed from ATIP
2143 static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2144 unsigned *speed)
2146 struct packet_command cgc;
2147 struct request_sense sense;
2148 unsigned char buf[64];
2149 unsigned int size, st, sp;
2150 int ret;
2152 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2153 cgc.sense = &sense;
2154 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2155 cgc.cmd[1] = 2;
2156 cgc.cmd[2] = 4; /* READ ATIP */
2157 cgc.cmd[8] = 2;
2158 ret = pkt_generic_packet(pd, &cgc);
2159 if (ret) {
2160 pkt_dump_sense(&cgc);
2161 return ret;
2163 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2164 if (size > sizeof(buf))
2165 size = sizeof(buf);
2167 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2168 cgc.sense = &sense;
2169 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2170 cgc.cmd[1] = 2;
2171 cgc.cmd[2] = 4;
2172 cgc.cmd[8] = size;
2173 ret = pkt_generic_packet(pd, &cgc);
2174 if (ret) {
2175 pkt_dump_sense(&cgc);
2176 return ret;
2179 if (!(buf[6] & 0x40)) {
2180 printk(DRIVER_NAME": Disc type is not CD-RW\n");
2181 return 1;
2183 if (!(buf[6] & 0x4)) {
2184 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
2185 return 1;
2188 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2190 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2192 /* Info from cdrecord */
2193 switch (st) {
2194 case 0: /* standard speed */
2195 *speed = clv_to_speed[sp];
2196 break;
2197 case 1: /* high speed */
2198 *speed = hs_clv_to_speed[sp];
2199 break;
2200 case 2: /* ultra high speed */
2201 *speed = us_clv_to_speed[sp];
2202 break;
2203 default:
2204 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
2205 return 1;
2207 if (*speed) {
2208 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
2209 return 0;
2210 } else {
2211 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
2212 return 1;
2216 static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
2218 struct packet_command cgc;
2219 struct request_sense sense;
2220 int ret;
2222 VPRINTK(DRIVER_NAME": Performing OPC\n");
2224 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2225 cgc.sense = &sense;
2226 cgc.timeout = 60*HZ;
2227 cgc.cmd[0] = GPCMD_SEND_OPC;
2228 cgc.cmd[1] = 1;
2229 if ((ret = pkt_generic_packet(pd, &cgc)))
2230 pkt_dump_sense(&cgc);
2231 return ret;
2234 static int pkt_open_write(struct pktcdvd_device *pd)
2236 int ret;
2237 unsigned int write_speed, media_write_speed, read_speed;
2239 if ((ret = pkt_probe_settings(pd))) {
2240 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
2241 return ret;
2244 if ((ret = pkt_set_write_settings(pd))) {
2245 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
2246 return -EIO;
2249 pkt_write_caching(pd, USE_WCACHING);
2251 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2252 write_speed = 16 * 177;
2253 switch (pd->mmc3_profile) {
2254 case 0x13: /* DVD-RW */
2255 case 0x1a: /* DVD+RW */
2256 case 0x12: /* DVD-RAM */
2257 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
2258 break;
2259 default:
2260 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2261 media_write_speed = 16;
2262 write_speed = min(write_speed, media_write_speed * 177);
2263 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
2264 break;
2266 read_speed = write_speed;
2268 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
2269 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
2270 return -EIO;
2272 pd->write_speed = write_speed;
2273 pd->read_speed = read_speed;
2275 if ((ret = pkt_perform_opc(pd))) {
2276 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
2279 return 0;
2283 * called at open time.
2285 static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
2287 int ret;
2288 long lba;
2289 struct request_queue *q;
2292 * We need to re-open the cdrom device without O_NONBLOCK to be able
2293 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2294 * so bdget() can't fail.
2296 bdget(pd->bdev->bd_dev);
2297 if ((ret = blkdev_get(pd->bdev, FMODE_READ)))
2298 goto out;
2300 if ((ret = bd_claim(pd->bdev, pd)))
2301 goto out_putdev;
2303 if ((ret = pkt_get_last_written(pd, &lba))) {
2304 printk(DRIVER_NAME": pkt_get_last_written failed\n");
2305 goto out_unclaim;
2308 set_capacity(pd->disk, lba << 2);
2309 set_capacity(pd->bdev->bd_disk, lba << 2);
2310 bd_set_size(pd->bdev, (loff_t)lba << 11);
2312 q = bdev_get_queue(pd->bdev);
2313 if (write) {
2314 if ((ret = pkt_open_write(pd)))
2315 goto out_unclaim;
2317 * Some CDRW drives can not handle writes larger than one packet,
2318 * even if the size is a multiple of the packet size.
2320 spin_lock_irq(q->queue_lock);
2321 blk_queue_max_hw_sectors(q, pd->settings.size);
2322 spin_unlock_irq(q->queue_lock);
2323 set_bit(PACKET_WRITABLE, &pd->flags);
2324 } else {
2325 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2326 clear_bit(PACKET_WRITABLE, &pd->flags);
2329 if ((ret = pkt_set_segment_merging(pd, q)))
2330 goto out_unclaim;
2332 if (write) {
2333 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2334 printk(DRIVER_NAME": not enough memory for buffers\n");
2335 ret = -ENOMEM;
2336 goto out_unclaim;
2338 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
2341 return 0;
2343 out_unclaim:
2344 bd_release(pd->bdev);
2345 out_putdev:
2346 blkdev_put(pd->bdev, FMODE_READ);
2347 out:
2348 return ret;
2352 * called when the device is closed. makes sure that the device flushes
2353 * the internal cache before we close.
2355 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2357 if (flush && pkt_flush_cache(pd))
2358 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
2360 pkt_lock_door(pd, 0);
2362 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2363 bd_release(pd->bdev);
2364 blkdev_put(pd->bdev, FMODE_READ);
2366 pkt_shrink_pktlist(pd);
2369 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2371 if (dev_minor >= MAX_WRITERS)
2372 return NULL;
2373 return pkt_devs[dev_minor];
2376 static int pkt_open(struct block_device *bdev, fmode_t mode)
2378 struct pktcdvd_device *pd = NULL;
2379 int ret;
2381 VPRINTK(DRIVER_NAME": entering open\n");
2383 mutex_lock(&ctl_mutex);
2384 pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev));
2385 if (!pd) {
2386 ret = -ENODEV;
2387 goto out;
2389 BUG_ON(pd->refcnt < 0);
2391 pd->refcnt++;
2392 if (pd->refcnt > 1) {
2393 if ((mode & FMODE_WRITE) &&
2394 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2395 ret = -EBUSY;
2396 goto out_dec;
2398 } else {
2399 ret = pkt_open_dev(pd, mode & FMODE_WRITE);
2400 if (ret)
2401 goto out_dec;
2403 * needed here as well, since ext2 (among others) may change
2404 * the blocksize at mount time
2406 set_blocksize(bdev, CD_FRAMESIZE);
2409 mutex_unlock(&ctl_mutex);
2410 return 0;
2412 out_dec:
2413 pd->refcnt--;
2414 out:
2415 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
2416 mutex_unlock(&ctl_mutex);
2417 return ret;
2420 static int pkt_close(struct gendisk *disk, fmode_t mode)
2422 struct pktcdvd_device *pd = disk->private_data;
2423 int ret = 0;
2425 mutex_lock(&ctl_mutex);
2426 pd->refcnt--;
2427 BUG_ON(pd->refcnt < 0);
2428 if (pd->refcnt == 0) {
2429 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2430 pkt_release_dev(pd, flush);
2432 mutex_unlock(&ctl_mutex);
2433 return ret;
2437 static void pkt_end_io_read_cloned(struct bio *bio, int err)
2439 struct packet_stacked_data *psd = bio->bi_private;
2440 struct pktcdvd_device *pd = psd->pd;
2442 bio_put(bio);
2443 bio_endio(psd->bio, err);
2444 mempool_free(psd, psd_pool);
2445 pkt_bio_finished(pd);
2448 static int pkt_make_request(struct request_queue *q, struct bio *bio)
2450 struct pktcdvd_device *pd;
2451 char b[BDEVNAME_SIZE];
2452 sector_t zone;
2453 struct packet_data *pkt;
2454 int was_empty, blocked_bio;
2455 struct pkt_rb_node *node;
2457 pd = q->queuedata;
2458 if (!pd) {
2459 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2460 goto end_io;
2464 * Clone READ bios so we can have our own bi_end_io callback.
2466 if (bio_data_dir(bio) == READ) {
2467 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2468 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2470 psd->pd = pd;
2471 psd->bio = bio;
2472 cloned_bio->bi_bdev = pd->bdev;
2473 cloned_bio->bi_private = psd;
2474 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2475 pd->stats.secs_r += bio->bi_size >> 9;
2476 pkt_queue_bio(pd, cloned_bio);
2477 return 0;
2480 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2481 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
2482 pd->name, (unsigned long long)bio->bi_sector);
2483 goto end_io;
2486 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2487 printk(DRIVER_NAME": wrong bio size\n");
2488 goto end_io;
2491 blk_queue_bounce(q, &bio);
2493 zone = ZONE(bio->bi_sector, pd);
2494 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2495 (unsigned long long)bio->bi_sector,
2496 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2498 /* Check if we have to split the bio */
2500 struct bio_pair *bp;
2501 sector_t last_zone;
2502 int first_sectors;
2504 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2505 if (last_zone != zone) {
2506 BUG_ON(last_zone != zone + pd->settings.size);
2507 first_sectors = last_zone - bio->bi_sector;
2508 bp = bio_split(bio, first_sectors);
2509 BUG_ON(!bp);
2510 pkt_make_request(q, &bp->bio1);
2511 pkt_make_request(q, &bp->bio2);
2512 bio_pair_release(bp);
2513 return 0;
2518 * If we find a matching packet in state WAITING or READ_WAIT, we can
2519 * just append this bio to that packet.
2521 spin_lock(&pd->cdrw.active_list_lock);
2522 blocked_bio = 0;
2523 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2524 if (pkt->sector == zone) {
2525 spin_lock(&pkt->lock);
2526 if ((pkt->state == PACKET_WAITING_STATE) ||
2527 (pkt->state == PACKET_READ_WAIT_STATE)) {
2528 bio_list_add(&pkt->orig_bios, bio);
2529 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2530 if ((pkt->write_size >= pkt->frames) &&
2531 (pkt->state == PACKET_WAITING_STATE)) {
2532 atomic_inc(&pkt->run_sm);
2533 wake_up(&pd->wqueue);
2535 spin_unlock(&pkt->lock);
2536 spin_unlock(&pd->cdrw.active_list_lock);
2537 return 0;
2538 } else {
2539 blocked_bio = 1;
2541 spin_unlock(&pkt->lock);
2544 spin_unlock(&pd->cdrw.active_list_lock);
2547 * Test if there is enough room left in the bio work queue
2548 * (queue size >= congestion on mark).
2549 * If not, wait till the work queue size is below the congestion off mark.
2551 spin_lock(&pd->lock);
2552 if (pd->write_congestion_on > 0
2553 && pd->bio_queue_size >= pd->write_congestion_on) {
2554 set_bdi_congested(&q->backing_dev_info, BLK_RW_ASYNC);
2555 do {
2556 spin_unlock(&pd->lock);
2557 congestion_wait(BLK_RW_ASYNC, HZ);
2558 spin_lock(&pd->lock);
2559 } while(pd->bio_queue_size > pd->write_congestion_off);
2561 spin_unlock(&pd->lock);
2564 * No matching packet found. Store the bio in the work queue.
2566 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2567 node->bio = bio;
2568 spin_lock(&pd->lock);
2569 BUG_ON(pd->bio_queue_size < 0);
2570 was_empty = (pd->bio_queue_size == 0);
2571 pkt_rbtree_insert(pd, node);
2572 spin_unlock(&pd->lock);
2575 * Wake up the worker thread.
2577 atomic_set(&pd->scan_queue, 1);
2578 if (was_empty) {
2579 /* This wake_up is required for correct operation */
2580 wake_up(&pd->wqueue);
2581 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2583 * This wake up is not required for correct operation,
2584 * but improves performance in some cases.
2586 wake_up(&pd->wqueue);
2588 return 0;
2589 end_io:
2590 bio_io_error(bio);
2591 return 0;
2596 static int pkt_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2597 struct bio_vec *bvec)
2599 struct pktcdvd_device *pd = q->queuedata;
2600 sector_t zone = ZONE(bmd->bi_sector, pd);
2601 int used = ((bmd->bi_sector - zone) << 9) + bmd->bi_size;
2602 int remaining = (pd->settings.size << 9) - used;
2603 int remaining2;
2606 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2607 * boundary, pkt_make_request() will split the bio.
2609 remaining2 = PAGE_SIZE - bmd->bi_size;
2610 remaining = max(remaining, remaining2);
2612 BUG_ON(remaining < 0);
2613 return remaining;
2616 static void pkt_init_queue(struct pktcdvd_device *pd)
2618 struct request_queue *q = pd->disk->queue;
2620 blk_queue_make_request(q, pkt_make_request);
2621 blk_queue_logical_block_size(q, CD_FRAMESIZE);
2622 blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS);
2623 blk_queue_merge_bvec(q, pkt_merge_bvec);
2624 q->queuedata = pd;
2627 static int pkt_seq_show(struct seq_file *m, void *p)
2629 struct pktcdvd_device *pd = m->private;
2630 char *msg;
2631 char bdev_buf[BDEVNAME_SIZE];
2632 int states[PACKET_NUM_STATES];
2634 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2635 bdevname(pd->bdev, bdev_buf));
2637 seq_printf(m, "\nSettings:\n");
2638 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2640 if (pd->settings.write_type == 0)
2641 msg = "Packet";
2642 else
2643 msg = "Unknown";
2644 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2646 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2647 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2649 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2651 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2652 msg = "Mode 1";
2653 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2654 msg = "Mode 2";
2655 else
2656 msg = "Unknown";
2657 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2659 seq_printf(m, "\nStatistics:\n");
2660 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2661 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2662 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2663 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2664 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2666 seq_printf(m, "\nMisc:\n");
2667 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2668 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2669 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2670 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2671 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2672 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2674 seq_printf(m, "\nQueue state:\n");
2675 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2676 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2677 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2679 pkt_count_states(pd, states);
2680 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2681 states[0], states[1], states[2], states[3], states[4], states[5]);
2683 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2684 pd->write_congestion_off,
2685 pd->write_congestion_on);
2686 return 0;
2689 static int pkt_seq_open(struct inode *inode, struct file *file)
2691 return single_open(file, pkt_seq_show, PDE(inode)->data);
2694 static const struct file_operations pkt_proc_fops = {
2695 .open = pkt_seq_open,
2696 .read = seq_read,
2697 .llseek = seq_lseek,
2698 .release = single_release
2701 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2703 int i;
2704 int ret = 0;
2705 char b[BDEVNAME_SIZE];
2706 struct block_device *bdev;
2708 if (pd->pkt_dev == dev) {
2709 printk(DRIVER_NAME": Recursive setup not allowed\n");
2710 return -EBUSY;
2712 for (i = 0; i < MAX_WRITERS; i++) {
2713 struct pktcdvd_device *pd2 = pkt_devs[i];
2714 if (!pd2)
2715 continue;
2716 if (pd2->bdev->bd_dev == dev) {
2717 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
2718 return -EBUSY;
2720 if (pd2->pkt_dev == dev) {
2721 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
2722 return -EBUSY;
2726 bdev = bdget(dev);
2727 if (!bdev)
2728 return -ENOMEM;
2729 ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY);
2730 if (ret)
2731 return ret;
2733 /* This is safe, since we have a reference from open(). */
2734 __module_get(THIS_MODULE);
2736 pd->bdev = bdev;
2737 set_blocksize(bdev, CD_FRAMESIZE);
2739 pkt_init_queue(pd);
2741 atomic_set(&pd->cdrw.pending_bios, 0);
2742 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2743 if (IS_ERR(pd->cdrw.thread)) {
2744 printk(DRIVER_NAME": can't start kernel thread\n");
2745 ret = -ENOMEM;
2746 goto out_mem;
2749 proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
2750 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2751 return 0;
2753 out_mem:
2754 blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
2755 /* This is safe: open() is still holding a reference. */
2756 module_put(THIS_MODULE);
2757 return ret;
2760 static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
2762 struct pktcdvd_device *pd = bdev->bd_disk->private_data;
2764 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd,
2765 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
2767 switch (cmd) {
2768 case CDROMEJECT:
2770 * The door gets locked when the device is opened, so we
2771 * have to unlock it or else the eject command fails.
2773 if (pd->refcnt == 1)
2774 pkt_lock_door(pd, 0);
2775 /* fallthru */
2777 * forward selected CDROM ioctls to CD-ROM, for UDF
2779 case CDROMMULTISESSION:
2780 case CDROMREADTOCENTRY:
2781 case CDROM_LAST_WRITTEN:
2782 case CDROM_SEND_PACKET:
2783 case SCSI_IOCTL_SEND_COMMAND:
2784 return __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
2786 default:
2787 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
2788 return -ENOTTY;
2791 return 0;
2794 static int pkt_media_changed(struct gendisk *disk)
2796 struct pktcdvd_device *pd = disk->private_data;
2797 struct gendisk *attached_disk;
2799 if (!pd)
2800 return 0;
2801 if (!pd->bdev)
2802 return 0;
2803 attached_disk = pd->bdev->bd_disk;
2804 if (!attached_disk)
2805 return 0;
2806 return attached_disk->fops->media_changed(attached_disk);
2809 static const struct block_device_operations pktcdvd_ops = {
2810 .owner = THIS_MODULE,
2811 .open = pkt_open,
2812 .release = pkt_close,
2813 .locked_ioctl = pkt_ioctl,
2814 .media_changed = pkt_media_changed,
2817 static char *pktcdvd_devnode(struct gendisk *gd, mode_t *mode)
2819 return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
2823 * Set up mapping from pktcdvd device to CD-ROM device.
2825 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
2827 int idx;
2828 int ret = -ENOMEM;
2829 struct pktcdvd_device *pd;
2830 struct gendisk *disk;
2832 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2834 for (idx = 0; idx < MAX_WRITERS; idx++)
2835 if (!pkt_devs[idx])
2836 break;
2837 if (idx == MAX_WRITERS) {
2838 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
2839 ret = -EBUSY;
2840 goto out_mutex;
2843 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2844 if (!pd)
2845 goto out_mutex;
2847 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2848 sizeof(struct pkt_rb_node));
2849 if (!pd->rb_pool)
2850 goto out_mem;
2852 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2853 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2854 spin_lock_init(&pd->cdrw.active_list_lock);
2856 spin_lock_init(&pd->lock);
2857 spin_lock_init(&pd->iosched.lock);
2858 bio_list_init(&pd->iosched.read_queue);
2859 bio_list_init(&pd->iosched.write_queue);
2860 sprintf(pd->name, DRIVER_NAME"%d", idx);
2861 init_waitqueue_head(&pd->wqueue);
2862 pd->bio_queue = RB_ROOT;
2864 pd->write_congestion_on = write_congestion_on;
2865 pd->write_congestion_off = write_congestion_off;
2867 disk = alloc_disk(1);
2868 if (!disk)
2869 goto out_mem;
2870 pd->disk = disk;
2871 disk->major = pktdev_major;
2872 disk->first_minor = idx;
2873 disk->fops = &pktcdvd_ops;
2874 disk->flags = GENHD_FL_REMOVABLE;
2875 strcpy(disk->disk_name, pd->name);
2876 disk->devnode = pktcdvd_devnode;
2877 disk->private_data = pd;
2878 disk->queue = blk_alloc_queue(GFP_KERNEL);
2879 if (!disk->queue)
2880 goto out_mem2;
2882 pd->pkt_dev = MKDEV(pktdev_major, idx);
2883 ret = pkt_new_dev(pd, dev);
2884 if (ret)
2885 goto out_new_dev;
2887 add_disk(disk);
2889 pkt_sysfs_dev_new(pd);
2890 pkt_debugfs_dev_new(pd);
2892 pkt_devs[idx] = pd;
2893 if (pkt_dev)
2894 *pkt_dev = pd->pkt_dev;
2896 mutex_unlock(&ctl_mutex);
2897 return 0;
2899 out_new_dev:
2900 blk_cleanup_queue(disk->queue);
2901 out_mem2:
2902 put_disk(disk);
2903 out_mem:
2904 if (pd->rb_pool)
2905 mempool_destroy(pd->rb_pool);
2906 kfree(pd);
2907 out_mutex:
2908 mutex_unlock(&ctl_mutex);
2909 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
2910 return ret;
2914 * Tear down mapping from pktcdvd device to CD-ROM device.
2916 static int pkt_remove_dev(dev_t pkt_dev)
2918 struct pktcdvd_device *pd;
2919 int idx;
2920 int ret = 0;
2922 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2924 for (idx = 0; idx < MAX_WRITERS; idx++) {
2925 pd = pkt_devs[idx];
2926 if (pd && (pd->pkt_dev == pkt_dev))
2927 break;
2929 if (idx == MAX_WRITERS) {
2930 DPRINTK(DRIVER_NAME": dev not setup\n");
2931 ret = -ENXIO;
2932 goto out;
2935 if (pd->refcnt > 0) {
2936 ret = -EBUSY;
2937 goto out;
2939 if (!IS_ERR(pd->cdrw.thread))
2940 kthread_stop(pd->cdrw.thread);
2942 pkt_devs[idx] = NULL;
2944 pkt_debugfs_dev_remove(pd);
2945 pkt_sysfs_dev_remove(pd);
2947 blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY);
2949 remove_proc_entry(pd->name, pkt_proc);
2950 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
2952 del_gendisk(pd->disk);
2953 blk_cleanup_queue(pd->disk->queue);
2954 put_disk(pd->disk);
2956 mempool_destroy(pd->rb_pool);
2957 kfree(pd);
2959 /* This is safe: open() is still holding a reference. */
2960 module_put(THIS_MODULE);
2962 out:
2963 mutex_unlock(&ctl_mutex);
2964 return ret;
2967 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2969 struct pktcdvd_device *pd;
2971 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2973 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2974 if (pd) {
2975 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2976 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2977 } else {
2978 ctrl_cmd->dev = 0;
2979 ctrl_cmd->pkt_dev = 0;
2981 ctrl_cmd->num_devices = MAX_WRITERS;
2983 mutex_unlock(&ctl_mutex);
2986 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2988 void __user *argp = (void __user *)arg;
2989 struct pkt_ctrl_command ctrl_cmd;
2990 int ret = 0;
2991 dev_t pkt_dev = 0;
2993 if (cmd != PACKET_CTRL_CMD)
2994 return -ENOTTY;
2996 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2997 return -EFAULT;
2999 switch (ctrl_cmd.command) {
3000 case PKT_CTRL_CMD_SETUP:
3001 if (!capable(CAP_SYS_ADMIN))
3002 return -EPERM;
3003 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3004 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
3005 break;
3006 case PKT_CTRL_CMD_TEARDOWN:
3007 if (!capable(CAP_SYS_ADMIN))
3008 return -EPERM;
3009 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
3010 break;
3011 case PKT_CTRL_CMD_STATUS:
3012 pkt_get_status(&ctrl_cmd);
3013 break;
3014 default:
3015 return -ENOTTY;
3018 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3019 return -EFAULT;
3020 return ret;
3024 static const struct file_operations pkt_ctl_fops = {
3025 .ioctl = pkt_ctl_ioctl,
3026 .owner = THIS_MODULE,
3029 static struct miscdevice pkt_misc = {
3030 .minor = MISC_DYNAMIC_MINOR,
3031 .name = DRIVER_NAME,
3032 .nodename = "pktcdvd/control",
3033 .fops = &pkt_ctl_fops
3036 static int __init pkt_init(void)
3038 int ret;
3040 mutex_init(&ctl_mutex);
3042 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3043 sizeof(struct packet_stacked_data));
3044 if (!psd_pool)
3045 return -ENOMEM;
3047 ret = register_blkdev(pktdev_major, DRIVER_NAME);
3048 if (ret < 0) {
3049 printk(DRIVER_NAME": Unable to register block device\n");
3050 goto out2;
3052 if (!pktdev_major)
3053 pktdev_major = ret;
3055 ret = pkt_sysfs_init();
3056 if (ret)
3057 goto out;
3059 pkt_debugfs_init();
3061 ret = misc_register(&pkt_misc);
3062 if (ret) {
3063 printk(DRIVER_NAME": Unable to register misc device\n");
3064 goto out_misc;
3067 pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
3069 return 0;
3071 out_misc:
3072 pkt_debugfs_cleanup();
3073 pkt_sysfs_cleanup();
3074 out:
3075 unregister_blkdev(pktdev_major, DRIVER_NAME);
3076 out2:
3077 mempool_destroy(psd_pool);
3078 return ret;
3081 static void __exit pkt_exit(void)
3083 remove_proc_entry("driver/"DRIVER_NAME, NULL);
3084 misc_deregister(&pkt_misc);
3086 pkt_debugfs_cleanup();
3087 pkt_sysfs_cleanup();
3089 unregister_blkdev(pktdev_major, DRIVER_NAME);
3090 mempool_destroy(psd_pool);
3093 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3094 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3095 MODULE_LICENSE("GPL");
3097 module_init(pkt_init);
3098 module_exit(pkt_exit);