2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
5 * May be copied or modified under the terms of the GNU General Public
6 * License. See linux/COPYING for more information.
8 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
11 * Theory of operation:
13 * At the lowest level, there is the standard driver for the CD/DVD device,
14 * typically ide-cd.c or sr.c. This driver can handle read and write requests,
15 * but it doesn't know anything about the special restrictions that apply to
16 * packet writing. One restriction is that write requests must be aligned to
17 * packet boundaries on the physical media, and the size of a write request
18 * must be equal to the packet size. Another restriction is that a
19 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
20 * command, if the previous command was a write.
22 * The purpose of the packet writing driver is to hide these restrictions from
23 * higher layers, such as file systems, and present a block device that can be
24 * randomly read and written using 2kB-sized blocks.
26 * The lowest layer in the packet writing driver is the packet I/O scheduler.
27 * Its data is defined by the struct packet_iosched and includes two bio
28 * queues with pending read and write requests. These queues are processed
29 * by the pkt_iosched_process_queue() function. The write requests in this
30 * queue are already properly aligned and sized. This layer is responsible for
31 * issuing the flush cache commands and scheduling the I/O in a good order.
33 * The next layer transforms unaligned write requests to aligned writes. This
34 * transformation requires reading missing pieces of data from the underlying
35 * block device, assembling the pieces to full packets and queuing them to the
36 * packet I/O scheduler.
38 * At the top layer there is a custom make_request_fn function that forwards
39 * read requests directly to the iosched queue and puts write requests in the
40 * unaligned write queue. A kernel thread performs the necessary read
41 * gathering to convert the unaligned writes to aligned writes and then feeds
42 * them to the packet I/O scheduler.
44 *************************************************************************/
46 #define VERSION_CODE "v0.2.0a 2004-07-14 Jens Axboe (axboe@suse.de) and petero2@telia.com"
48 #include <linux/pktcdvd.h>
49 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/types.h>
52 #include <linux/kernel.h>
53 #include <linux/kthread.h>
54 #include <linux/errno.h>
55 #include <linux/spinlock.h>
56 #include <linux/file.h>
57 #include <linux/proc_fs.h>
58 #include <linux/seq_file.h>
59 #include <linux/miscdevice.h>
60 #include <linux/suspend.h>
61 #include <scsi/scsi_cmnd.h>
62 #include <scsi/scsi_ioctl.h>
64 #include <asm/uaccess.h>
67 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
69 #define DPRINTK(fmt, args...)
73 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
75 #define VPRINTK(fmt, args...)
78 #define MAX_SPEED 0xffff
80 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
82 static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
83 static struct proc_dir_entry *pkt_proc;
85 static struct semaphore ctl_mutex; /* Serialize open/close/setup/teardown */
86 static mempool_t *psd_pool;
89 static void pkt_bio_finished(struct pktcdvd_device *pd)
91 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
92 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
93 VPRINTK("pktcdvd: queue empty\n");
94 atomic_set(&pd->iosched.attention, 1);
99 static void pkt_bio_destructor(struct bio *bio)
101 kfree(bio->bi_io_vec);
105 static struct bio *pkt_bio_alloc(int nr_iovecs)
107 struct bio_vec *bvl = NULL;
110 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
115 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
119 bio->bi_max_vecs = nr_iovecs;
120 bio->bi_io_vec = bvl;
121 bio->bi_destructor = pkt_bio_destructor;
132 * Allocate a packet_data struct
134 static struct packet_data *pkt_alloc_packet_data(void)
137 struct packet_data *pkt;
139 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
143 pkt->w_bio = pkt_bio_alloc(PACKET_MAX_SIZE);
147 for (i = 0; i < PAGES_PER_PACKET; i++) {
148 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
153 spin_lock_init(&pkt->lock);
155 for (i = 0; i < PACKET_MAX_SIZE; i++) {
156 struct bio *bio = pkt_bio_alloc(1);
159 pkt->r_bios[i] = bio;
165 for (i = 0; i < PACKET_MAX_SIZE; i++) {
166 struct bio *bio = pkt->r_bios[i];
172 for (i = 0; i < PAGES_PER_PACKET; i++)
174 __free_page(pkt->pages[i]);
183 * Free a packet_data struct
185 static void pkt_free_packet_data(struct packet_data *pkt)
189 for (i = 0; i < PACKET_MAX_SIZE; i++) {
190 struct bio *bio = pkt->r_bios[i];
194 for (i = 0; i < PAGES_PER_PACKET; i++)
195 __free_page(pkt->pages[i]);
200 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
202 struct packet_data *pkt, *next;
204 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
206 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
207 pkt_free_packet_data(pkt);
211 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
213 struct packet_data *pkt;
215 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
216 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
217 spin_lock_init(&pd->cdrw.active_list_lock);
218 while (nr_packets > 0) {
219 pkt = pkt_alloc_packet_data();
221 pkt_shrink_pktlist(pd);
224 pkt->id = nr_packets;
226 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
232 static void *pkt_rb_alloc(unsigned int __nocast gfp_mask, void *data)
234 return kmalloc(sizeof(struct pkt_rb_node), gfp_mask);
237 static void pkt_rb_free(void *ptr, void *data)
242 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
244 struct rb_node *n = rb_next(&node->rb_node);
247 return rb_entry(n, struct pkt_rb_node, rb_node);
250 static inline void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
252 rb_erase(&node->rb_node, &pd->bio_queue);
253 mempool_free(node, pd->rb_pool);
254 pd->bio_queue_size--;
255 BUG_ON(pd->bio_queue_size < 0);
259 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
261 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
263 struct rb_node *n = pd->bio_queue.rb_node;
264 struct rb_node *next;
265 struct pkt_rb_node *tmp;
268 BUG_ON(pd->bio_queue_size > 0);
273 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
274 if (s <= tmp->bio->bi_sector)
283 if (s > tmp->bio->bi_sector) {
284 tmp = pkt_rbtree_next(tmp);
288 BUG_ON(s > tmp->bio->bi_sector);
293 * Insert a node into the pd->bio_queue rb tree.
295 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
297 struct rb_node **p = &pd->bio_queue.rb_node;
298 struct rb_node *parent = NULL;
299 sector_t s = node->bio->bi_sector;
300 struct pkt_rb_node *tmp;
304 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
305 if (s < tmp->bio->bi_sector)
310 rb_link_node(&node->rb_node, parent, p);
311 rb_insert_color(&node->rb_node, &pd->bio_queue);
312 pd->bio_queue_size++;
316 * Add a bio to a single linked list defined by its head and tail pointers.
318 static inline void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
322 BUG_ON((*list_head) == NULL);
323 (*list_tail)->bi_next = bio;
326 BUG_ON((*list_head) != NULL);
333 * Remove and return the first bio from a single linked list defined by its
334 * head and tail pointers.
336 static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
340 if (*list_head == NULL)
344 *list_head = bio->bi_next;
345 if (*list_head == NULL)
353 * Send a packet_command to the underlying block device and
354 * wait for completion.
356 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
358 char sense[SCSI_SENSE_BUFFERSIZE];
361 DECLARE_COMPLETION(wait);
364 q = bdev_get_queue(pd->bdev);
366 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
369 rq->rq_disk = pd->bdev->bd_disk;
373 rq->data = cgc->buffer;
374 rq->data_len = cgc->buflen;
376 memset(sense, 0, sizeof(sense));
378 rq->flags |= REQ_BLOCK_PC | REQ_HARDBARRIER;
380 rq->flags |= REQ_QUIET;
381 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
382 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
383 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
386 rq->flags |= REQ_NOMERGE;
388 rq->end_io = blk_end_sync_rq;
389 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
390 generic_unplug_device(q);
391 wait_for_completion(&wait);
401 * A generic sense dump / resolve mechanism should be implemented across
402 * all ATAPI + SCSI devices.
404 static void pkt_dump_sense(struct packet_command *cgc)
406 static char *info[9] = { "No sense", "Recovered error", "Not ready",
407 "Medium error", "Hardware error", "Illegal request",
408 "Unit attention", "Data protect", "Blank check" };
410 struct request_sense *sense = cgc->sense;
413 for (i = 0; i < CDROM_PACKET_SIZE; i++)
414 printk(" %02x", cgc->cmd[i]);
418 printk("no sense\n");
422 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
424 if (sense->sense_key > 8) {
425 printk(" (INVALID)\n");
429 printk(" (%s)\n", info[sense->sense_key]);
433 * flush the drive cache to media
435 static int pkt_flush_cache(struct pktcdvd_device *pd)
437 struct packet_command cgc;
439 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
440 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
444 * the IMMED bit -- we default to not setting it, although that
445 * would allow a much faster close, this is safer
450 return pkt_generic_packet(pd, &cgc);
454 * speed is given as the normal factor, e.g. 4 for 4x
456 static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
458 struct packet_command cgc;
459 struct request_sense sense;
462 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
464 cgc.cmd[0] = GPCMD_SET_SPEED;
465 cgc.cmd[2] = (read_speed >> 8) & 0xff;
466 cgc.cmd[3] = read_speed & 0xff;
467 cgc.cmd[4] = (write_speed >> 8) & 0xff;
468 cgc.cmd[5] = write_speed & 0xff;
470 if ((ret = pkt_generic_packet(pd, &cgc)))
471 pkt_dump_sense(&cgc);
477 * Queue a bio for processing by the low-level CD device. Must be called
478 * from process context.
480 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
482 spin_lock(&pd->iosched.lock);
483 if (bio_data_dir(bio) == READ) {
484 pkt_add_list_last(bio, &pd->iosched.read_queue,
485 &pd->iosched.read_queue_tail);
487 pkt_add_list_last(bio, &pd->iosched.write_queue,
488 &pd->iosched.write_queue_tail);
490 spin_unlock(&pd->iosched.lock);
492 atomic_set(&pd->iosched.attention, 1);
493 wake_up(&pd->wqueue);
497 * Process the queued read/write requests. This function handles special
498 * requirements for CDRW drives:
499 * - A cache flush command must be inserted before a read request if the
500 * previous request was a write.
501 * - Switching between reading and writing is slow, so don't do it more often
503 * - Optimize for throughput at the expense of latency. This means that streaming
504 * writes will never be interrupted by a read, but if the drive has to seek
505 * before the next write, switch to reading instead if there are any pending
507 * - Set the read speed according to current usage pattern. When only reading
508 * from the device, it's best to use the highest possible read speed, but
509 * when switching often between reading and writing, it's better to have the
510 * same read and write speeds.
512 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
516 if (atomic_read(&pd->iosched.attention) == 0)
518 atomic_set(&pd->iosched.attention, 0);
520 q = bdev_get_queue(pd->bdev);
524 int reads_queued, writes_queued;
526 spin_lock(&pd->iosched.lock);
527 reads_queued = (pd->iosched.read_queue != NULL);
528 writes_queued = (pd->iosched.write_queue != NULL);
529 spin_unlock(&pd->iosched.lock);
531 if (!reads_queued && !writes_queued)
534 if (pd->iosched.writing) {
535 int need_write_seek = 1;
536 spin_lock(&pd->iosched.lock);
537 bio = pd->iosched.write_queue;
538 spin_unlock(&pd->iosched.lock);
539 if (bio && (bio->bi_sector == pd->iosched.last_write))
541 if (need_write_seek && reads_queued) {
542 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
543 VPRINTK("pktcdvd: write, waiting\n");
547 pd->iosched.writing = 0;
550 if (!reads_queued && writes_queued) {
551 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
552 VPRINTK("pktcdvd: read, waiting\n");
555 pd->iosched.writing = 1;
559 spin_lock(&pd->iosched.lock);
560 if (pd->iosched.writing) {
561 bio = pkt_get_list_first(&pd->iosched.write_queue,
562 &pd->iosched.write_queue_tail);
564 bio = pkt_get_list_first(&pd->iosched.read_queue,
565 &pd->iosched.read_queue_tail);
567 spin_unlock(&pd->iosched.lock);
572 if (bio_data_dir(bio) == READ)
573 pd->iosched.successive_reads += bio->bi_size >> 10;
575 pd->iosched.successive_reads = 0;
576 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
578 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
579 if (pd->read_speed == pd->write_speed) {
580 pd->read_speed = MAX_SPEED;
581 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
584 if (pd->read_speed != pd->write_speed) {
585 pd->read_speed = pd->write_speed;
586 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
590 atomic_inc(&pd->cdrw.pending_bios);
591 generic_make_request(bio);
596 * Special care is needed if the underlying block device has a small
597 * max_phys_segments value.
599 static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
601 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
603 * The cdrom device can handle one segment/frame
605 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
607 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
609 * We can handle this case at the expense of some extra memory
610 * copies during write operations
612 set_bit(PACKET_MERGE_SEGS, &pd->flags);
615 printk("pktcdvd: cdrom max_phys_segments too small\n");
621 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
623 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
625 unsigned int copy_size = CD_FRAMESIZE;
627 while (copy_size > 0) {
628 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
629 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
630 src_bvl->bv_offset + offs;
631 void *vto = page_address(dst_page) + dst_offs;
632 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
635 memcpy(vto, vfrom, len);
636 kunmap_atomic(vfrom, KM_USER0);
646 * Copy all data for this packet to pkt->pages[], so that
647 * a) The number of required segments for the write bio is minimized, which
648 * is necessary for some scsi controllers.
649 * b) The data can be used as cache to avoid read requests if we receive a
650 * new write request for the same zone.
652 static void pkt_make_local_copy(struct packet_data *pkt, struct page **pages, int *offsets)
656 /* Copy all data to pkt->pages[] */
659 for (f = 0; f < pkt->frames; f++) {
660 if (pages[f] != pkt->pages[p]) {
661 void *vfrom = kmap_atomic(pages[f], KM_USER0) + offsets[f];
662 void *vto = page_address(pkt->pages[p]) + offs;
663 memcpy(vto, vfrom, CD_FRAMESIZE);
664 kunmap_atomic(vfrom, KM_USER0);
665 pages[f] = pkt->pages[p];
668 BUG_ON(offsets[f] != offs);
670 offs += CD_FRAMESIZE;
671 if (offs >= PAGE_SIZE) {
672 BUG_ON(offs > PAGE_SIZE);
679 static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
681 struct packet_data *pkt = bio->bi_private;
682 struct pktcdvd_device *pd = pkt->pd;
688 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
689 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
692 atomic_inc(&pkt->io_errors);
693 if (atomic_dec_and_test(&pkt->io_wait)) {
694 atomic_inc(&pkt->run_sm);
695 wake_up(&pd->wqueue);
697 pkt_bio_finished(pd);
702 static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
704 struct packet_data *pkt = bio->bi_private;
705 struct pktcdvd_device *pd = pkt->pd;
711 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
713 pd->stats.pkt_ended++;
715 pkt_bio_finished(pd);
716 atomic_dec(&pkt->io_wait);
717 atomic_inc(&pkt->run_sm);
718 wake_up(&pd->wqueue);
723 * Schedule reads for the holes in a packet
725 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
730 char written[PACKET_MAX_SIZE];
732 BUG_ON(!pkt->orig_bios);
734 atomic_set(&pkt->io_wait, 0);
735 atomic_set(&pkt->io_errors, 0);
738 * Figure out which frames we need to read before we can write.
740 memset(written, 0, sizeof(written));
741 spin_lock(&pkt->lock);
742 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
743 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
744 int num_frames = bio->bi_size / CD_FRAMESIZE;
745 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
746 BUG_ON(first_frame < 0);
747 BUG_ON(first_frame + num_frames > pkt->frames);
748 for (f = first_frame; f < first_frame + num_frames; f++)
751 spin_unlock(&pkt->lock);
753 if (pkt->cache_valid) {
754 VPRINTK("pkt_gather_data: zone %llx cached\n",
755 (unsigned long long)pkt->sector);
760 * Schedule reads for missing parts of the packet.
762 for (f = 0; f < pkt->frames; f++) {
766 bio = pkt->r_bios[f];
768 bio->bi_max_vecs = 1;
769 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
770 bio->bi_bdev = pd->bdev;
771 bio->bi_end_io = pkt_end_io_read;
772 bio->bi_private = pkt;
774 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
775 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
776 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
777 f, pkt->pages[p], offset);
778 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
781 atomic_inc(&pkt->io_wait);
783 pkt_queue_bio(pd, bio);
788 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
789 frames_read, (unsigned long long)pkt->sector);
790 pd->stats.pkt_started++;
791 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
795 * Find a packet matching zone, or the least recently used packet if
798 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
800 struct packet_data *pkt;
802 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
803 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
804 list_del_init(&pkt->list);
805 if (pkt->sector != zone)
806 pkt->cache_valid = 0;
813 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
815 if (pkt->cache_valid) {
816 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
818 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
823 * recover a failed write, query for relocation if possible
825 * returns 1 if recovery is possible, or 0 if not
828 static int pkt_start_recovery(struct packet_data *pkt)
831 * FIXME. We need help from the file system to implement
836 struct request *rq = pkt->rq;
837 struct pktcdvd_device *pd = rq->rq_disk->private_data;
838 struct block_device *pkt_bdev;
839 struct super_block *sb = NULL;
840 unsigned long old_block, new_block;
843 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
845 sb = get_super(pkt_bdev);
852 if (!sb->s_op || !sb->s_op->relocate_blocks)
855 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
856 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
859 new_sector = new_block * (CD_FRAMESIZE >> 9);
860 pkt->sector = new_sector;
862 pkt->bio->bi_sector = new_sector;
863 pkt->bio->bi_next = NULL;
864 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
865 pkt->bio->bi_idx = 0;
867 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
868 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
869 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
870 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
871 BUG_ON(pkt->bio->bi_private != pkt);
882 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
885 static const char *state_name[] = {
886 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
888 enum packet_data_state old_state = pkt->state;
889 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
890 state_name[old_state], state_name[state]);
896 * Scan the work queue to see if we can start a new packet.
897 * returns non-zero if any work was done.
899 static int pkt_handle_queue(struct pktcdvd_device *pd)
901 struct packet_data *pkt, *p;
902 struct bio *bio = NULL;
903 sector_t zone = 0; /* Suppress gcc warning */
904 struct pkt_rb_node *node, *first_node;
907 VPRINTK("handle_queue\n");
909 atomic_set(&pd->scan_queue, 0);
911 if (list_empty(&pd->cdrw.pkt_free_list)) {
912 VPRINTK("handle_queue: no pkt\n");
917 * Try to find a zone we are not already working on.
919 spin_lock(&pd->lock);
920 first_node = pkt_rbtree_find(pd, pd->current_sector);
922 n = rb_first(&pd->bio_queue);
924 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
929 zone = ZONE(bio->bi_sector, pd);
930 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
931 if (p->sector == zone) {
938 node = pkt_rbtree_next(node);
940 n = rb_first(&pd->bio_queue);
942 node = rb_entry(n, struct pkt_rb_node, rb_node);
944 if (node == first_node)
947 spin_unlock(&pd->lock);
949 VPRINTK("handle_queue: no bio\n");
953 pkt = pkt_get_packet_data(pd, zone);
956 pd->current_sector = zone + pd->settings.size;
958 pkt->frames = pd->settings.size >> 2;
962 * Scan work queue for bios in the same zone and link them
965 spin_lock(&pd->lock);
966 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
967 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
969 VPRINTK("pkt_handle_queue: found zone=%llx\n",
970 (unsigned long long)ZONE(bio->bi_sector, pd));
971 if (ZONE(bio->bi_sector, pd) != zone)
973 pkt_rbtree_erase(pd, node);
974 spin_lock(&pkt->lock);
975 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
976 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
977 spin_unlock(&pkt->lock);
979 spin_unlock(&pd->lock);
981 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
982 pkt_set_state(pkt, PACKET_WAITING_STATE);
983 atomic_set(&pkt->run_sm, 1);
985 spin_lock(&pd->cdrw.active_list_lock);
986 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
987 spin_unlock(&pd->cdrw.active_list_lock);
993 * Assemble a bio to write one packet and queue the bio for processing
994 * by the underlying block device.
996 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
999 struct page *pages[PACKET_MAX_SIZE];
1000 int offsets[PACKET_MAX_SIZE];
1004 for (f = 0; f < pkt->frames; f++) {
1005 pages[f] = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1006 offsets[f] = (f * CD_FRAMESIZE) % PAGE_SIZE;
1010 * Fill-in pages[] and offsets[] with data from orig_bios.
1013 spin_lock(&pkt->lock);
1014 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1015 int segment = bio->bi_idx;
1017 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1018 int num_frames = bio->bi_size / CD_FRAMESIZE;
1019 BUG_ON(first_frame < 0);
1020 BUG_ON(first_frame + num_frames > pkt->frames);
1021 for (f = first_frame; f < first_frame + num_frames; f++) {
1022 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1024 while (src_offs >= src_bvl->bv_len) {
1025 src_offs -= src_bvl->bv_len;
1027 BUG_ON(segment >= bio->bi_vcnt);
1028 src_bvl = bio_iovec_idx(bio, segment);
1031 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1032 pages[f] = src_bvl->bv_page;
1033 offsets[f] = src_bvl->bv_offset + src_offs;
1035 pkt_copy_bio_data(bio, segment, src_offs,
1036 pages[f], offsets[f]);
1038 src_offs += CD_FRAMESIZE;
1042 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1043 spin_unlock(&pkt->lock);
1045 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1046 frames_write, (unsigned long long)pkt->sector);
1047 BUG_ON(frames_write != pkt->write_size);
1049 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1050 pkt_make_local_copy(pkt, pages, offsets);
1051 pkt->cache_valid = 1;
1053 pkt->cache_valid = 0;
1056 /* Start the write request */
1057 bio_init(pkt->w_bio);
1058 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1059 pkt->w_bio->bi_sector = pkt->sector;
1060 pkt->w_bio->bi_bdev = pd->bdev;
1061 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1062 pkt->w_bio->bi_private = pkt;
1063 for (f = 0; f < pkt->frames; f++) {
1064 if ((f + 1 < pkt->frames) && (pages[f + 1] == pages[f]) &&
1065 (offsets[f + 1] = offsets[f] + CD_FRAMESIZE)) {
1066 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE * 2, offsets[f]))
1070 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE, offsets[f]))
1074 VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
1076 atomic_set(&pkt->io_wait, 1);
1077 pkt->w_bio->bi_rw = WRITE;
1078 pkt_queue_bio(pd, pkt->w_bio);
1081 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1083 struct bio *bio, *next;
1086 pkt->cache_valid = 0;
1088 /* Finish all bios corresponding to this packet */
1089 bio = pkt->orig_bios;
1091 next = bio->bi_next;
1092 bio->bi_next = NULL;
1093 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1096 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1099 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1103 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1106 switch (pkt->state) {
1107 case PACKET_WAITING_STATE:
1108 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1111 pkt->sleep_time = 0;
1112 pkt_gather_data(pd, pkt);
1113 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1116 case PACKET_READ_WAIT_STATE:
1117 if (atomic_read(&pkt->io_wait) > 0)
1120 if (atomic_read(&pkt->io_errors) > 0) {
1121 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1123 pkt_start_write(pd, pkt);
1127 case PACKET_WRITE_WAIT_STATE:
1128 if (atomic_read(&pkt->io_wait) > 0)
1131 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1132 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1134 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1138 case PACKET_RECOVERY_STATE:
1139 if (pkt_start_recovery(pkt)) {
1140 pkt_start_write(pd, pkt);
1142 VPRINTK("No recovery possible\n");
1143 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1147 case PACKET_FINISHED_STATE:
1148 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1149 pkt_finish_packet(pkt, uptodate);
1159 static void pkt_handle_packets(struct pktcdvd_device *pd)
1161 struct packet_data *pkt, *next;
1163 VPRINTK("pkt_handle_packets\n");
1166 * Run state machine for active packets
1168 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1169 if (atomic_read(&pkt->run_sm) > 0) {
1170 atomic_set(&pkt->run_sm, 0);
1171 pkt_run_state_machine(pd, pkt);
1176 * Move no longer active packets to the free list
1178 spin_lock(&pd->cdrw.active_list_lock);
1179 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1180 if (pkt->state == PACKET_FINISHED_STATE) {
1181 list_del(&pkt->list);
1182 pkt_put_packet_data(pd, pkt);
1183 pkt_set_state(pkt, PACKET_IDLE_STATE);
1184 atomic_set(&pd->scan_queue, 1);
1187 spin_unlock(&pd->cdrw.active_list_lock);
1190 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1192 struct packet_data *pkt;
1195 for (i = 0; i <= PACKET_NUM_STATES; i++)
1198 spin_lock(&pd->cdrw.active_list_lock);
1199 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1200 states[pkt->state]++;
1202 spin_unlock(&pd->cdrw.active_list_lock);
1206 * kcdrwd is woken up when writes have been queued for one of our
1207 * registered devices
1209 static int kcdrwd(void *foobar)
1211 struct pktcdvd_device *pd = foobar;
1212 struct packet_data *pkt;
1213 long min_sleep_time, residue;
1215 set_user_nice(current, -20);
1218 DECLARE_WAITQUEUE(wait, current);
1221 * Wait until there is something to do
1223 add_wait_queue(&pd->wqueue, &wait);
1225 set_current_state(TASK_INTERRUPTIBLE);
1227 /* Check if we need to run pkt_handle_queue */
1228 if (atomic_read(&pd->scan_queue) > 0)
1231 /* Check if we need to run the state machine for some packet */
1232 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1233 if (atomic_read(&pkt->run_sm) > 0)
1237 /* Check if we need to process the iosched queues */
1238 if (atomic_read(&pd->iosched.attention) != 0)
1241 /* Otherwise, go to sleep */
1242 if (PACKET_DEBUG > 1) {
1243 int states[PACKET_NUM_STATES];
1244 pkt_count_states(pd, states);
1245 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1246 states[0], states[1], states[2], states[3],
1247 states[4], states[5]);
1250 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1251 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1252 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1253 min_sleep_time = pkt->sleep_time;
1256 generic_unplug_device(bdev_get_queue(pd->bdev));
1258 VPRINTK("kcdrwd: sleeping\n");
1259 residue = schedule_timeout(min_sleep_time);
1260 VPRINTK("kcdrwd: wake up\n");
1262 /* make swsusp happy with our thread */
1265 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1266 if (!pkt->sleep_time)
1268 pkt->sleep_time -= min_sleep_time - residue;
1269 if (pkt->sleep_time <= 0) {
1270 pkt->sleep_time = 0;
1271 atomic_inc(&pkt->run_sm);
1275 if (signal_pending(current)) {
1276 flush_signals(current);
1278 if (kthread_should_stop())
1282 set_current_state(TASK_RUNNING);
1283 remove_wait_queue(&pd->wqueue, &wait);
1285 if (kthread_should_stop())
1289 * if pkt_handle_queue returns true, we can queue
1292 while (pkt_handle_queue(pd))
1296 * Handle packet state machine
1298 pkt_handle_packets(pd);
1301 * Handle iosched queues
1303 pkt_iosched_process_queue(pd);
1309 static void pkt_print_settings(struct pktcdvd_device *pd)
1311 printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1312 printk("%u blocks, ", pd->settings.size >> 2);
1313 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1316 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1318 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1320 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1321 cgc->cmd[2] = page_code | (page_control << 6);
1322 cgc->cmd[7] = cgc->buflen >> 8;
1323 cgc->cmd[8] = cgc->buflen & 0xff;
1324 cgc->data_direction = CGC_DATA_READ;
1325 return pkt_generic_packet(pd, cgc);
1328 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1330 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1331 memset(cgc->buffer, 0, 2);
1332 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1333 cgc->cmd[1] = 0x10; /* PF */
1334 cgc->cmd[7] = cgc->buflen >> 8;
1335 cgc->cmd[8] = cgc->buflen & 0xff;
1336 cgc->data_direction = CGC_DATA_WRITE;
1337 return pkt_generic_packet(pd, cgc);
1340 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1342 struct packet_command cgc;
1345 /* set up command and get the disc info */
1346 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1347 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1348 cgc.cmd[8] = cgc.buflen = 2;
1351 if ((ret = pkt_generic_packet(pd, &cgc)))
1354 /* not all drives have the same disc_info length, so requeue
1355 * packet with the length the drive tells us it can supply
1357 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1358 sizeof(di->disc_information_length);
1360 if (cgc.buflen > sizeof(disc_information))
1361 cgc.buflen = sizeof(disc_information);
1363 cgc.cmd[8] = cgc.buflen;
1364 return pkt_generic_packet(pd, &cgc);
1367 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1369 struct packet_command cgc;
1372 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1373 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1374 cgc.cmd[1] = type & 3;
1375 cgc.cmd[4] = (track & 0xff00) >> 8;
1376 cgc.cmd[5] = track & 0xff;
1380 if ((ret = pkt_generic_packet(pd, &cgc)))
1383 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1384 sizeof(ti->track_information_length);
1386 if (cgc.buflen > sizeof(track_information))
1387 cgc.buflen = sizeof(track_information);
1389 cgc.cmd[8] = cgc.buflen;
1390 return pkt_generic_packet(pd, &cgc);
1393 static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1395 disc_information di;
1396 track_information ti;
1400 if ((ret = pkt_get_disc_info(pd, &di)))
1403 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1404 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1407 /* if this track is blank, try the previous. */
1410 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1414 /* if last recorded field is valid, return it. */
1416 *last_written = be32_to_cpu(ti.last_rec_address);
1418 /* make it up instead */
1419 *last_written = be32_to_cpu(ti.track_start) +
1420 be32_to_cpu(ti.track_size);
1422 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1428 * write mode select package based on pd->settings
1430 static int pkt_set_write_settings(struct pktcdvd_device *pd)
1432 struct packet_command cgc;
1433 struct request_sense sense;
1434 write_param_page *wp;
1438 /* doesn't apply to DVD+RW or DVD-RAM */
1439 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1442 memset(buffer, 0, sizeof(buffer));
1443 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1445 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1446 pkt_dump_sense(&cgc);
1450 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1451 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1452 if (size > sizeof(buffer))
1453 size = sizeof(buffer);
1458 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1460 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1461 pkt_dump_sense(&cgc);
1466 * write page is offset header + block descriptor length
1468 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1470 wp->fp = pd->settings.fp;
1471 wp->track_mode = pd->settings.track_mode;
1472 wp->write_type = pd->settings.write_type;
1473 wp->data_block_type = pd->settings.block_mode;
1475 wp->multi_session = 0;
1477 #ifdef PACKET_USE_LS
1482 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1483 wp->session_format = 0;
1485 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1486 wp->session_format = 0x20;
1490 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1496 printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
1499 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1501 cgc.buflen = cgc.cmd[8] = size;
1502 if ((ret = pkt_mode_select(pd, &cgc))) {
1503 pkt_dump_sense(&cgc);
1507 pkt_print_settings(pd);
1512 * 0 -- we can write to this track, 1 -- we can't
1514 static int pkt_good_track(track_information *ti)
1517 * only good for CD-RW at the moment, not DVD-RW
1521 * FIXME: only for FP
1527 * "good" settings as per Mt Fuji.
1529 if (ti->rt == 0 && ti->blank == 0 && ti->packet == 1)
1532 if (ti->rt == 0 && ti->blank == 1 && ti->packet == 1)
1535 if (ti->rt == 1 && ti->blank == 0 && ti->packet == 1)
1538 printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1543 * 0 -- we can write to this disc, 1 -- we can't
1545 static int pkt_good_disc(struct pktcdvd_device *pd, disc_information *di)
1547 switch (pd->mmc3_profile) {
1548 case 0x0a: /* CD-RW */
1549 case 0xffff: /* MMC3 not supported */
1551 case 0x1a: /* DVD+RW */
1552 case 0x13: /* DVD-RW */
1553 case 0x12: /* DVD-RAM */
1556 printk("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
1561 * for disc type 0xff we should probably reserve a new track.
1562 * but i'm not sure, should we leave this to user apps? probably.
1564 if (di->disc_type == 0xff) {
1565 printk("pktcdvd: Unknown disc. No track?\n");
1569 if (di->disc_type != 0x20 && di->disc_type != 0) {
1570 printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
1574 if (di->erasable == 0) {
1575 printk("pktcdvd: Disc not erasable\n");
1579 if (di->border_status == PACKET_SESSION_RESERVED) {
1580 printk("pktcdvd: Can't write to last track (reserved)\n");
1587 static int pkt_probe_settings(struct pktcdvd_device *pd)
1589 struct packet_command cgc;
1590 unsigned char buf[12];
1591 disc_information di;
1592 track_information ti;
1595 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1596 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1598 ret = pkt_generic_packet(pd, &cgc);
1599 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1601 memset(&di, 0, sizeof(disc_information));
1602 memset(&ti, 0, sizeof(track_information));
1604 if ((ret = pkt_get_disc_info(pd, &di))) {
1605 printk("failed get_disc\n");
1609 if (pkt_good_disc(pd, &di))
1612 switch (pd->mmc3_profile) {
1613 case 0x1a: /* DVD+RW */
1614 printk("pktcdvd: inserted media is DVD+RW\n");
1616 case 0x13: /* DVD-RW */
1617 printk("pktcdvd: inserted media is DVD-RW\n");
1619 case 0x12: /* DVD-RAM */
1620 printk("pktcdvd: inserted media is DVD-RAM\n");
1623 printk("pktcdvd: inserted media is CD-R%s\n", di.erasable ? "W" : "");
1626 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1628 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1629 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1630 printk("pktcdvd: failed get_track\n");
1634 if (pkt_good_track(&ti)) {
1635 printk("pktcdvd: can't write to this track\n");
1640 * we keep packet size in 512 byte units, makes it easier to
1641 * deal with request calculations.
1643 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1644 if (pd->settings.size == 0) {
1645 printk("pktcdvd: detected zero packet size!\n");
1646 pd->settings.size = 128;
1648 if (pd->settings.size > PACKET_MAX_SECTORS) {
1649 printk("pktcdvd: packet size is too big\n");
1652 pd->settings.fp = ti.fp;
1653 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1656 pd->nwa = be32_to_cpu(ti.next_writable);
1657 set_bit(PACKET_NWA_VALID, &pd->flags);
1661 * in theory we could use lra on -RW media as well and just zero
1662 * blocks that haven't been written yet, but in practice that
1663 * is just a no-go. we'll use that for -R, naturally.
1666 pd->lra = be32_to_cpu(ti.last_rec_address);
1667 set_bit(PACKET_LRA_VALID, &pd->flags);
1669 pd->lra = 0xffffffff;
1670 set_bit(PACKET_LRA_VALID, &pd->flags);
1676 pd->settings.link_loss = 7;
1677 pd->settings.write_type = 0; /* packet */
1678 pd->settings.track_mode = ti.track_mode;
1681 * mode1 or mode2 disc
1683 switch (ti.data_mode) {
1685 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1688 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1691 printk("pktcdvd: unknown data mode\n");
1698 * enable/disable write caching on drive
1700 static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1702 struct packet_command cgc;
1703 struct request_sense sense;
1704 unsigned char buf[64];
1707 memset(buf, 0, sizeof(buf));
1708 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1710 cgc.buflen = pd->mode_offset + 12;
1713 * caching mode page might not be there, so quiet this command
1717 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1720 buf[pd->mode_offset + 10] |= (!!set << 2);
1722 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1723 ret = pkt_mode_select(pd, &cgc);
1725 printk("pktcdvd: write caching control failed\n");
1726 pkt_dump_sense(&cgc);
1727 } else if (!ret && set)
1728 printk("pktcdvd: enabled write caching on %s\n", pd->name);
1732 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1734 struct packet_command cgc;
1736 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1737 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1738 cgc.cmd[4] = lockflag ? 1 : 0;
1739 return pkt_generic_packet(pd, &cgc);
1743 * Returns drive maximum write speed
1745 static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1747 struct packet_command cgc;
1748 struct request_sense sense;
1749 unsigned char buf[256+18];
1750 unsigned char *cap_buf;
1753 memset(buf, 0, sizeof(buf));
1754 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1755 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1758 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1760 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1761 sizeof(struct mode_page_header);
1762 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1764 pkt_dump_sense(&cgc);
1769 offset = 20; /* Obsoleted field, used by older drives */
1770 if (cap_buf[1] >= 28)
1771 offset = 28; /* Current write speed selected */
1772 if (cap_buf[1] >= 30) {
1773 /* If the drive reports at least one "Logical Unit Write
1774 * Speed Performance Descriptor Block", use the information
1775 * in the first block. (contains the highest speed)
1777 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1782 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1786 /* These tables from cdrecord - I don't have orange book */
1787 /* standard speed CD-RW (1-4x) */
1788 static char clv_to_speed[16] = {
1789 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1790 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1792 /* high speed CD-RW (-10x) */
1793 static char hs_clv_to_speed[16] = {
1794 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1795 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1797 /* ultra high speed CD-RW */
1798 static char us_clv_to_speed[16] = {
1799 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1800 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1804 * reads the maximum media speed from ATIP
1806 static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1808 struct packet_command cgc;
1809 struct request_sense sense;
1810 unsigned char buf[64];
1811 unsigned int size, st, sp;
1814 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1816 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1818 cgc.cmd[2] = 4; /* READ ATIP */
1820 ret = pkt_generic_packet(pd, &cgc);
1822 pkt_dump_sense(&cgc);
1825 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1826 if (size > sizeof(buf))
1829 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1831 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1835 ret = pkt_generic_packet(pd, &cgc);
1837 pkt_dump_sense(&cgc);
1841 if (!buf[6] & 0x40) {
1842 printk("pktcdvd: Disc type is not CD-RW\n");
1845 if (!buf[6] & 0x4) {
1846 printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
1850 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1852 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1854 /* Info from cdrecord */
1856 case 0: /* standard speed */
1857 *speed = clv_to_speed[sp];
1859 case 1: /* high speed */
1860 *speed = hs_clv_to_speed[sp];
1862 case 2: /* ultra high speed */
1863 *speed = us_clv_to_speed[sp];
1866 printk("pktcdvd: Unknown disc sub-type %d\n",st);
1870 printk("pktcdvd: Max. media speed: %d\n",*speed);
1873 printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
1878 static int pkt_perform_opc(struct pktcdvd_device *pd)
1880 struct packet_command cgc;
1881 struct request_sense sense;
1884 VPRINTK("pktcdvd: Performing OPC\n");
1886 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1888 cgc.timeout = 60*HZ;
1889 cgc.cmd[0] = GPCMD_SEND_OPC;
1891 if ((ret = pkt_generic_packet(pd, &cgc)))
1892 pkt_dump_sense(&cgc);
1896 static int pkt_open_write(struct pktcdvd_device *pd)
1899 unsigned int write_speed, media_write_speed, read_speed;
1901 if ((ret = pkt_probe_settings(pd))) {
1902 DPRINTK("pktcdvd: %s failed probe\n", pd->name);
1906 if ((ret = pkt_set_write_settings(pd))) {
1907 DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
1911 pkt_write_caching(pd, USE_WCACHING);
1913 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1914 write_speed = 16 * 177;
1915 switch (pd->mmc3_profile) {
1916 case 0x13: /* DVD-RW */
1917 case 0x1a: /* DVD+RW */
1918 case 0x12: /* DVD-RAM */
1919 DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
1922 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1923 media_write_speed = 16;
1924 write_speed = min(write_speed, media_write_speed * 177);
1925 DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
1928 read_speed = write_speed;
1930 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1931 DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
1934 pd->write_speed = write_speed;
1935 pd->read_speed = read_speed;
1937 if ((ret = pkt_perform_opc(pd))) {
1938 DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
1945 * called at open time.
1947 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1954 * We need to re-open the cdrom device without O_NONBLOCK to be able
1955 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1956 * so bdget() can't fail.
1958 bdget(pd->bdev->bd_dev);
1959 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1962 if ((ret = pkt_get_last_written(pd, &lba))) {
1963 printk("pktcdvd: pkt_get_last_written failed\n");
1967 set_capacity(pd->disk, lba << 2);
1968 set_capacity(pd->bdev->bd_disk, lba << 2);
1969 bd_set_size(pd->bdev, (loff_t)lba << 11);
1971 q = bdev_get_queue(pd->bdev);
1973 if ((ret = pkt_open_write(pd)))
1976 * Some CDRW drives can not handle writes larger than one packet,
1977 * even if the size is a multiple of the packet size.
1979 spin_lock_irq(q->queue_lock);
1980 blk_queue_max_sectors(q, pd->settings.size);
1981 spin_unlock_irq(q->queue_lock);
1982 set_bit(PACKET_WRITABLE, &pd->flags);
1984 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1985 clear_bit(PACKET_WRITABLE, &pd->flags);
1988 if ((ret = pkt_set_segment_merging(pd, q)))
1992 printk("pktcdvd: %lukB available on disc\n", lba << 1);
1997 blkdev_put(pd->bdev);
2003 * called when the device is closed. makes sure that the device flushes
2004 * the internal cache before we close.
2006 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2008 if (flush && pkt_flush_cache(pd))
2009 DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
2011 pkt_lock_door(pd, 0);
2013 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2014 blkdev_put(pd->bdev);
2017 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2019 if (dev_minor >= MAX_WRITERS)
2021 return pkt_devs[dev_minor];
2024 static int pkt_open(struct inode *inode, struct file *file)
2026 struct pktcdvd_device *pd = NULL;
2029 VPRINTK("pktcdvd: entering open\n");
2032 pd = pkt_find_dev_from_minor(iminor(inode));
2037 BUG_ON(pd->refcnt < 0);
2040 if (pd->refcnt > 1) {
2041 if ((file->f_mode & FMODE_WRITE) &&
2042 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2047 if (pkt_open_dev(pd, file->f_mode & FMODE_WRITE)) {
2052 * needed here as well, since ext2 (among others) may change
2053 * the blocksize at mount time
2055 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2064 VPRINTK("pktcdvd: failed open (%d)\n", ret);
2069 static int pkt_close(struct inode *inode, struct file *file)
2071 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2076 BUG_ON(pd->refcnt < 0);
2077 if (pd->refcnt == 0) {
2078 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2079 pkt_release_dev(pd, flush);
2086 static void *psd_pool_alloc(unsigned int __nocast gfp_mask, void *data)
2088 return kmalloc(sizeof(struct packet_stacked_data), gfp_mask);
2091 static void psd_pool_free(void *ptr, void *data)
2096 static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2098 struct packet_stacked_data *psd = bio->bi_private;
2099 struct pktcdvd_device *pd = psd->pd;
2105 bio_endio(psd->bio, psd->bio->bi_size, err);
2106 mempool_free(psd, psd_pool);
2107 pkt_bio_finished(pd);
2111 static int pkt_make_request(request_queue_t *q, struct bio *bio)
2113 struct pktcdvd_device *pd;
2114 char b[BDEVNAME_SIZE];
2116 struct packet_data *pkt;
2117 int was_empty, blocked_bio;
2118 struct pkt_rb_node *node;
2122 printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2127 * Clone READ bios so we can have our own bi_end_io callback.
2129 if (bio_data_dir(bio) == READ) {
2130 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2131 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2135 cloned_bio->bi_bdev = pd->bdev;
2136 cloned_bio->bi_private = psd;
2137 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2138 pd->stats.secs_r += bio->bi_size >> 9;
2139 pkt_queue_bio(pd, cloned_bio);
2143 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2144 printk("pktcdvd: WRITE for ro device %s (%llu)\n",
2145 pd->name, (unsigned long long)bio->bi_sector);
2149 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2150 printk("pktcdvd: wrong bio size\n");
2154 blk_queue_bounce(q, &bio);
2156 zone = ZONE(bio->bi_sector, pd);
2157 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2158 (unsigned long long)bio->bi_sector,
2159 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2161 /* Check if we have to split the bio */
2163 struct bio_pair *bp;
2167 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2168 if (last_zone != zone) {
2169 BUG_ON(last_zone != zone + pd->settings.size);
2170 first_sectors = last_zone - bio->bi_sector;
2171 bp = bio_split(bio, bio_split_pool, first_sectors);
2173 pkt_make_request(q, &bp->bio1);
2174 pkt_make_request(q, &bp->bio2);
2175 bio_pair_release(bp);
2181 * If we find a matching packet in state WAITING or READ_WAIT, we can
2182 * just append this bio to that packet.
2184 spin_lock(&pd->cdrw.active_list_lock);
2186 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2187 if (pkt->sector == zone) {
2188 spin_lock(&pkt->lock);
2189 if ((pkt->state == PACKET_WAITING_STATE) ||
2190 (pkt->state == PACKET_READ_WAIT_STATE)) {
2191 pkt_add_list_last(bio, &pkt->orig_bios,
2192 &pkt->orig_bios_tail);
2193 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2194 if ((pkt->write_size >= pkt->frames) &&
2195 (pkt->state == PACKET_WAITING_STATE)) {
2196 atomic_inc(&pkt->run_sm);
2197 wake_up(&pd->wqueue);
2199 spin_unlock(&pkt->lock);
2200 spin_unlock(&pd->cdrw.active_list_lock);
2205 spin_unlock(&pkt->lock);
2208 spin_unlock(&pd->cdrw.active_list_lock);
2211 * No matching packet found. Store the bio in the work queue.
2213 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2216 spin_lock(&pd->lock);
2217 BUG_ON(pd->bio_queue_size < 0);
2218 was_empty = (pd->bio_queue_size == 0);
2219 pkt_rbtree_insert(pd, node);
2220 spin_unlock(&pd->lock);
2223 * Wake up the worker thread.
2225 atomic_set(&pd->scan_queue, 1);
2227 /* This wake_up is required for correct operation */
2228 wake_up(&pd->wqueue);
2229 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2231 * This wake up is not required for correct operation,
2232 * but improves performance in some cases.
2234 wake_up(&pd->wqueue);
2238 bio_io_error(bio, bio->bi_size);
2244 static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2246 struct pktcdvd_device *pd = q->queuedata;
2247 sector_t zone = ZONE(bio->bi_sector, pd);
2248 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2249 int remaining = (pd->settings.size << 9) - used;
2253 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2254 * boundary, pkt_make_request() will split the bio.
2256 remaining2 = PAGE_SIZE - bio->bi_size;
2257 remaining = max(remaining, remaining2);
2259 BUG_ON(remaining < 0);
2263 static void pkt_init_queue(struct pktcdvd_device *pd)
2265 request_queue_t *q = pd->disk->queue;
2267 blk_queue_make_request(q, pkt_make_request);
2268 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2269 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2270 blk_queue_merge_bvec(q, pkt_merge_bvec);
2274 static int pkt_seq_show(struct seq_file *m, void *p)
2276 struct pktcdvd_device *pd = m->private;
2278 char bdev_buf[BDEVNAME_SIZE];
2279 int states[PACKET_NUM_STATES];
2281 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2282 bdevname(pd->bdev, bdev_buf));
2284 seq_printf(m, "\nSettings:\n");
2285 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2287 if (pd->settings.write_type == 0)
2291 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2293 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2294 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2296 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2298 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2300 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2304 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2306 seq_printf(m, "\nStatistics:\n");
2307 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2308 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2309 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2310 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2311 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2313 seq_printf(m, "\nMisc:\n");
2314 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2315 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2316 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2317 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2318 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2319 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2321 seq_printf(m, "\nQueue state:\n");
2322 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2323 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2324 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2326 pkt_count_states(pd, states);
2327 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2328 states[0], states[1], states[2], states[3], states[4], states[5]);
2333 static int pkt_seq_open(struct inode *inode, struct file *file)
2335 return single_open(file, pkt_seq_show, PDE(inode)->data);
2338 static struct file_operations pkt_proc_fops = {
2339 .open = pkt_seq_open,
2341 .llseek = seq_lseek,
2342 .release = single_release
2345 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2349 char b[BDEVNAME_SIZE];
2350 struct proc_dir_entry *proc;
2351 struct block_device *bdev;
2353 if (pd->pkt_dev == dev) {
2354 printk("pktcdvd: Recursive setup not allowed\n");
2357 for (i = 0; i < MAX_WRITERS; i++) {
2358 struct pktcdvd_device *pd2 = pkt_devs[i];
2361 if (pd2->bdev->bd_dev == dev) {
2362 printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
2365 if (pd2->pkt_dev == dev) {
2366 printk("pktcdvd: Can't chain pktcdvd devices\n");
2374 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2378 /* This is safe, since we have a reference from open(). */
2379 __module_get(THIS_MODULE);
2381 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2382 printk("pktcdvd: not enough memory for buffers\n");
2388 set_blocksize(bdev, CD_FRAMESIZE);
2392 atomic_set(&pd->cdrw.pending_bios, 0);
2393 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2394 if (IS_ERR(pd->cdrw.thread)) {
2395 printk("pktcdvd: can't start kernel thread\n");
2400 proc = create_proc_entry(pd->name, 0, pkt_proc);
2403 proc->proc_fops = &pkt_proc_fops;
2405 DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2409 pkt_shrink_pktlist(pd);
2412 /* This is safe: open() is still holding a reference. */
2413 module_put(THIS_MODULE);
2417 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2419 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2421 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2426 * forward selected CDROM ioctls to CD-ROM, for UDF
2428 case CDROMMULTISESSION:
2429 case CDROMREADTOCENTRY:
2430 case CDROM_LAST_WRITTEN:
2431 case CDROM_SEND_PACKET:
2432 case SCSI_IOCTL_SEND_COMMAND:
2433 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2437 * The door gets locked when the device is opened, so we
2438 * have to unlock it or else the eject command fails.
2440 pkt_lock_door(pd, 0);
2441 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2444 printk("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
2451 static int pkt_media_changed(struct gendisk *disk)
2453 struct pktcdvd_device *pd = disk->private_data;
2454 struct gendisk *attached_disk;
2460 attached_disk = pd->bdev->bd_disk;
2463 return attached_disk->fops->media_changed(attached_disk);
2466 static struct block_device_operations pktcdvd_ops = {
2467 .owner = THIS_MODULE,
2469 .release = pkt_close,
2471 .media_changed = pkt_media_changed,
2475 * Set up mapping from pktcdvd device to CD-ROM device.
2477 static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2481 struct pktcdvd_device *pd;
2482 struct gendisk *disk;
2483 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2485 for (idx = 0; idx < MAX_WRITERS; idx++)
2488 if (idx == MAX_WRITERS) {
2489 printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
2493 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2497 pd->rb_pool = mempool_create(PKT_RB_POOL_SIZE, pkt_rb_alloc, pkt_rb_free, NULL);
2501 disk = alloc_disk(1);
2506 spin_lock_init(&pd->lock);
2507 spin_lock_init(&pd->iosched.lock);
2508 sprintf(pd->name, "pktcdvd%d", idx);
2509 init_waitqueue_head(&pd->wqueue);
2510 pd->bio_queue = RB_ROOT;
2512 disk->major = pkt_major;
2513 disk->first_minor = idx;
2514 disk->fops = &pktcdvd_ops;
2515 disk->flags = GENHD_FL_REMOVABLE;
2516 sprintf(disk->disk_name, "pktcdvd%d", idx);
2517 disk->private_data = pd;
2518 disk->queue = blk_alloc_queue(GFP_KERNEL);
2522 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2523 ret = pkt_new_dev(pd, dev);
2529 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2533 blk_put_queue(disk->queue);
2538 mempool_destroy(pd->rb_pool);
2544 * Tear down mapping from pktcdvd device to CD-ROM device.
2546 static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2548 struct pktcdvd_device *pd;
2550 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2552 for (idx = 0; idx < MAX_WRITERS; idx++) {
2554 if (pd && (pd->pkt_dev == pkt_dev))
2557 if (idx == MAX_WRITERS) {
2558 DPRINTK("pktcdvd: dev not setup\n");
2565 if (!IS_ERR(pd->cdrw.thread))
2566 kthread_stop(pd->cdrw.thread);
2568 blkdev_put(pd->bdev);
2570 pkt_shrink_pktlist(pd);
2572 remove_proc_entry(pd->name, pkt_proc);
2573 DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
2575 del_gendisk(pd->disk);
2576 blk_put_queue(pd->disk->queue);
2579 pkt_devs[idx] = NULL;
2580 mempool_destroy(pd->rb_pool);
2583 /* This is safe: open() is still holding a reference. */
2584 module_put(THIS_MODULE);
2588 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2590 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2592 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2593 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2596 ctrl_cmd->pkt_dev = 0;
2598 ctrl_cmd->num_devices = MAX_WRITERS;
2601 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2603 void __user *argp = (void __user *)arg;
2604 struct pkt_ctrl_command ctrl_cmd;
2607 if (cmd != PACKET_CTRL_CMD)
2610 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2613 switch (ctrl_cmd.command) {
2614 case PKT_CTRL_CMD_SETUP:
2615 if (!capable(CAP_SYS_ADMIN))
2618 ret = pkt_setup_dev(&ctrl_cmd);
2621 case PKT_CTRL_CMD_TEARDOWN:
2622 if (!capable(CAP_SYS_ADMIN))
2625 ret = pkt_remove_dev(&ctrl_cmd);
2628 case PKT_CTRL_CMD_STATUS:
2630 pkt_get_status(&ctrl_cmd);
2637 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2643 static struct file_operations pkt_ctl_fops = {
2644 .ioctl = pkt_ctl_ioctl,
2645 .owner = THIS_MODULE,
2648 static struct miscdevice pkt_misc = {
2649 .minor = MISC_DYNAMIC_MINOR,
2651 .devfs_name = "pktcdvd/control",
2652 .fops = &pkt_ctl_fops
2655 static int __init pkt_init(void)
2659 psd_pool = mempool_create(PSD_POOL_SIZE, psd_pool_alloc, psd_pool_free, NULL);
2663 ret = register_blkdev(pkt_major, "pktcdvd");
2665 printk("pktcdvd: Unable to register block device\n");
2671 ret = misc_register(&pkt_misc);
2673 printk("pktcdvd: Unable to register misc device\n");
2677 init_MUTEX(&ctl_mutex);
2679 pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
2681 DPRINTK("pktcdvd: %s\n", VERSION_CODE);
2685 unregister_blkdev(pkt_major, "pktcdvd");
2687 mempool_destroy(psd_pool);
2691 static void __exit pkt_exit(void)
2693 remove_proc_entry("pktcdvd", proc_root_driver);
2694 misc_deregister(&pkt_misc);
2695 unregister_blkdev(pkt_major, "pktcdvd");
2696 mempool_destroy(psd_pool);
2699 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2700 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2701 MODULE_LICENSE("GPL");
2703 module_init(pkt_init);
2704 module_exit(pkt_exit);