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 #include <linux/pktcdvd.h>
47 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/kernel.h>
50 #include <linux/kthread.h>
51 #include <linux/errno.h>
52 #include <linux/spinlock.h>
53 #include <linux/file.h>
54 #include <linux/proc_fs.h>
55 #include <linux/seq_file.h>
56 #include <linux/miscdevice.h>
57 #include <linux/freezer.h>
58 #include <linux/mutex.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_ioctl.h>
61 #include <scsi/scsi.h>
63 #include <asm/uaccess.h>
65 #define DRIVER_NAME "pktcdvd"
68 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
70 #define DPRINTK(fmt, args...)
74 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
76 #define VPRINTK(fmt, args...)
79 #define MAX_SPEED 0xffff
81 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
83 static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
84 static struct proc_dir_entry *pkt_proc;
85 static int pktdev_major;
86 static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
87 static mempool_t *psd_pool;
90 static void pkt_bio_finished(struct pktcdvd_device *pd)
92 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
93 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
94 VPRINTK(DRIVER_NAME": queue empty\n");
95 atomic_set(&pd->iosched.attention, 1);
100 static void pkt_bio_destructor(struct bio *bio)
102 kfree(bio->bi_io_vec);
106 static struct bio *pkt_bio_alloc(int nr_iovecs)
108 struct bio_vec *bvl = NULL;
111 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
116 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
120 bio->bi_max_vecs = nr_iovecs;
121 bio->bi_io_vec = bvl;
122 bio->bi_destructor = pkt_bio_destructor;
133 * Allocate a packet_data struct
135 static struct packet_data *pkt_alloc_packet_data(int frames)
138 struct packet_data *pkt;
140 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
144 pkt->frames = frames;
145 pkt->w_bio = pkt_bio_alloc(frames);
149 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
150 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
155 spin_lock_init(&pkt->lock);
157 for (i = 0; i < frames; i++) {
158 struct bio *bio = pkt_bio_alloc(1);
161 pkt->r_bios[i] = bio;
167 for (i = 0; i < frames; i++) {
168 struct bio *bio = pkt->r_bios[i];
174 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
176 __free_page(pkt->pages[i]);
185 * Free a packet_data struct
187 static void pkt_free_packet_data(struct packet_data *pkt)
191 for (i = 0; i < pkt->frames; i++) {
192 struct bio *bio = pkt->r_bios[i];
196 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
197 __free_page(pkt->pages[i]);
202 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
204 struct packet_data *pkt, *next;
206 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
208 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
209 pkt_free_packet_data(pkt);
211 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
214 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
216 struct packet_data *pkt;
218 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
220 while (nr_packets > 0) {
221 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
223 pkt_shrink_pktlist(pd);
226 pkt->id = nr_packets;
228 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
234 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
236 struct rb_node *n = rb_next(&node->rb_node);
239 return rb_entry(n, struct pkt_rb_node, rb_node);
242 static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
244 rb_erase(&node->rb_node, &pd->bio_queue);
245 mempool_free(node, pd->rb_pool);
246 pd->bio_queue_size--;
247 BUG_ON(pd->bio_queue_size < 0);
251 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
253 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
255 struct rb_node *n = pd->bio_queue.rb_node;
256 struct rb_node *next;
257 struct pkt_rb_node *tmp;
260 BUG_ON(pd->bio_queue_size > 0);
265 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
266 if (s <= tmp->bio->bi_sector)
275 if (s > tmp->bio->bi_sector) {
276 tmp = pkt_rbtree_next(tmp);
280 BUG_ON(s > tmp->bio->bi_sector);
285 * Insert a node into the pd->bio_queue rb tree.
287 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
289 struct rb_node **p = &pd->bio_queue.rb_node;
290 struct rb_node *parent = NULL;
291 sector_t s = node->bio->bi_sector;
292 struct pkt_rb_node *tmp;
296 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
297 if (s < tmp->bio->bi_sector)
302 rb_link_node(&node->rb_node, parent, p);
303 rb_insert_color(&node->rb_node, &pd->bio_queue);
304 pd->bio_queue_size++;
308 * Add a bio to a single linked list defined by its head and tail pointers.
310 static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
314 BUG_ON((*list_head) == NULL);
315 (*list_tail)->bi_next = bio;
318 BUG_ON((*list_head) != NULL);
325 * Remove and return the first bio from a single linked list defined by its
326 * head and tail pointers.
328 static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
332 if (*list_head == NULL)
336 *list_head = bio->bi_next;
337 if (*list_head == NULL)
345 * Send a packet_command to the underlying block device and
346 * wait for completion.
348 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
350 char sense[SCSI_SENSE_BUFFERSIZE];
353 DECLARE_COMPLETION_ONSTACK(wait);
356 q = bdev_get_queue(pd->bdev);
358 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
361 rq->rq_disk = pd->bdev->bd_disk;
365 rq->data = cgc->buffer;
366 rq->data_len = cgc->buflen;
368 memset(sense, 0, sizeof(sense));
370 rq->cmd_type = REQ_TYPE_BLOCK_PC;
371 rq->cmd_flags |= REQ_HARDBARRIER;
373 rq->cmd_flags |= REQ_QUIET;
374 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
375 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
376 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
377 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
380 rq->end_io_data = &wait;
381 rq->end_io = blk_end_sync_rq;
382 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
383 generic_unplug_device(q);
384 wait_for_completion(&wait);
394 * A generic sense dump / resolve mechanism should be implemented across
395 * all ATAPI + SCSI devices.
397 static void pkt_dump_sense(struct packet_command *cgc)
399 static char *info[9] = { "No sense", "Recovered error", "Not ready",
400 "Medium error", "Hardware error", "Illegal request",
401 "Unit attention", "Data protect", "Blank check" };
403 struct request_sense *sense = cgc->sense;
405 printk(DRIVER_NAME":");
406 for (i = 0; i < CDROM_PACKET_SIZE; i++)
407 printk(" %02x", cgc->cmd[i]);
411 printk("no sense\n");
415 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
417 if (sense->sense_key > 8) {
418 printk(" (INVALID)\n");
422 printk(" (%s)\n", info[sense->sense_key]);
426 * flush the drive cache to media
428 static int pkt_flush_cache(struct pktcdvd_device *pd)
430 struct packet_command cgc;
432 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
433 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
437 * the IMMED bit -- we default to not setting it, although that
438 * would allow a much faster close, this is safer
443 return pkt_generic_packet(pd, &cgc);
447 * speed is given as the normal factor, e.g. 4 for 4x
449 static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
451 struct packet_command cgc;
452 struct request_sense sense;
455 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
457 cgc.cmd[0] = GPCMD_SET_SPEED;
458 cgc.cmd[2] = (read_speed >> 8) & 0xff;
459 cgc.cmd[3] = read_speed & 0xff;
460 cgc.cmd[4] = (write_speed >> 8) & 0xff;
461 cgc.cmd[5] = write_speed & 0xff;
463 if ((ret = pkt_generic_packet(pd, &cgc)))
464 pkt_dump_sense(&cgc);
470 * Queue a bio for processing by the low-level CD device. Must be called
471 * from process context.
473 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
475 spin_lock(&pd->iosched.lock);
476 if (bio_data_dir(bio) == READ) {
477 pkt_add_list_last(bio, &pd->iosched.read_queue,
478 &pd->iosched.read_queue_tail);
480 pkt_add_list_last(bio, &pd->iosched.write_queue,
481 &pd->iosched.write_queue_tail);
483 spin_unlock(&pd->iosched.lock);
485 atomic_set(&pd->iosched.attention, 1);
486 wake_up(&pd->wqueue);
490 * Process the queued read/write requests. This function handles special
491 * requirements for CDRW drives:
492 * - A cache flush command must be inserted before a read request if the
493 * previous request was a write.
494 * - Switching between reading and writing is slow, so don't do it more often
496 * - Optimize for throughput at the expense of latency. This means that streaming
497 * writes will never be interrupted by a read, but if the drive has to seek
498 * before the next write, switch to reading instead if there are any pending
500 * - Set the read speed according to current usage pattern. When only reading
501 * from the device, it's best to use the highest possible read speed, but
502 * when switching often between reading and writing, it's better to have the
503 * same read and write speeds.
505 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
508 if (atomic_read(&pd->iosched.attention) == 0)
510 atomic_set(&pd->iosched.attention, 0);
514 int reads_queued, writes_queued;
516 spin_lock(&pd->iosched.lock);
517 reads_queued = (pd->iosched.read_queue != NULL);
518 writes_queued = (pd->iosched.write_queue != NULL);
519 spin_unlock(&pd->iosched.lock);
521 if (!reads_queued && !writes_queued)
524 if (pd->iosched.writing) {
525 int need_write_seek = 1;
526 spin_lock(&pd->iosched.lock);
527 bio = pd->iosched.write_queue;
528 spin_unlock(&pd->iosched.lock);
529 if (bio && (bio->bi_sector == pd->iosched.last_write))
531 if (need_write_seek && reads_queued) {
532 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
533 VPRINTK(DRIVER_NAME": write, waiting\n");
537 pd->iosched.writing = 0;
540 if (!reads_queued && writes_queued) {
541 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
542 VPRINTK(DRIVER_NAME": read, waiting\n");
545 pd->iosched.writing = 1;
549 spin_lock(&pd->iosched.lock);
550 if (pd->iosched.writing) {
551 bio = pkt_get_list_first(&pd->iosched.write_queue,
552 &pd->iosched.write_queue_tail);
554 bio = pkt_get_list_first(&pd->iosched.read_queue,
555 &pd->iosched.read_queue_tail);
557 spin_unlock(&pd->iosched.lock);
562 if (bio_data_dir(bio) == READ)
563 pd->iosched.successive_reads += bio->bi_size >> 10;
565 pd->iosched.successive_reads = 0;
566 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
568 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
569 if (pd->read_speed == pd->write_speed) {
570 pd->read_speed = MAX_SPEED;
571 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
574 if (pd->read_speed != pd->write_speed) {
575 pd->read_speed = pd->write_speed;
576 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
580 atomic_inc(&pd->cdrw.pending_bios);
581 generic_make_request(bio);
586 * Special care is needed if the underlying block device has a small
587 * max_phys_segments value.
589 static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
591 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
593 * The cdrom device can handle one segment/frame
595 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
597 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
599 * We can handle this case at the expense of some extra memory
600 * copies during write operations
602 set_bit(PACKET_MERGE_SEGS, &pd->flags);
605 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
611 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
613 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
615 unsigned int copy_size = CD_FRAMESIZE;
617 while (copy_size > 0) {
618 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
619 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
620 src_bvl->bv_offset + offs;
621 void *vto = page_address(dst_page) + dst_offs;
622 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
625 memcpy(vto, vfrom, len);
626 kunmap_atomic(vfrom, KM_USER0);
636 * Copy all data for this packet to pkt->pages[], so that
637 * a) The number of required segments for the write bio is minimized, which
638 * is necessary for some scsi controllers.
639 * b) The data can be used as cache to avoid read requests if we receive a
640 * new write request for the same zone.
642 static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
646 /* Copy all data to pkt->pages[] */
649 for (f = 0; f < pkt->frames; f++) {
650 if (bvec[f].bv_page != pkt->pages[p]) {
651 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
652 void *vto = page_address(pkt->pages[p]) + offs;
653 memcpy(vto, vfrom, CD_FRAMESIZE);
654 kunmap_atomic(vfrom, KM_USER0);
655 bvec[f].bv_page = pkt->pages[p];
656 bvec[f].bv_offset = offs;
658 BUG_ON(bvec[f].bv_offset != offs);
660 offs += CD_FRAMESIZE;
661 if (offs >= PAGE_SIZE) {
668 static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
670 struct packet_data *pkt = bio->bi_private;
671 struct pktcdvd_device *pd = pkt->pd;
677 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
678 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
681 atomic_inc(&pkt->io_errors);
682 if (atomic_dec_and_test(&pkt->io_wait)) {
683 atomic_inc(&pkt->run_sm);
684 wake_up(&pd->wqueue);
686 pkt_bio_finished(pd);
691 static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
693 struct packet_data *pkt = bio->bi_private;
694 struct pktcdvd_device *pd = pkt->pd;
700 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
702 pd->stats.pkt_ended++;
704 pkt_bio_finished(pd);
705 atomic_dec(&pkt->io_wait);
706 atomic_inc(&pkt->run_sm);
707 wake_up(&pd->wqueue);
712 * Schedule reads for the holes in a packet
714 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
719 char written[PACKET_MAX_SIZE];
721 BUG_ON(!pkt->orig_bios);
723 atomic_set(&pkt->io_wait, 0);
724 atomic_set(&pkt->io_errors, 0);
727 * Figure out which frames we need to read before we can write.
729 memset(written, 0, sizeof(written));
730 spin_lock(&pkt->lock);
731 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
732 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
733 int num_frames = bio->bi_size / CD_FRAMESIZE;
734 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
735 BUG_ON(first_frame < 0);
736 BUG_ON(first_frame + num_frames > pkt->frames);
737 for (f = first_frame; f < first_frame + num_frames; f++)
740 spin_unlock(&pkt->lock);
742 if (pkt->cache_valid) {
743 VPRINTK("pkt_gather_data: zone %llx cached\n",
744 (unsigned long long)pkt->sector);
749 * Schedule reads for missing parts of the packet.
751 for (f = 0; f < pkt->frames; f++) {
755 bio = pkt->r_bios[f];
757 bio->bi_max_vecs = 1;
758 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
759 bio->bi_bdev = pd->bdev;
760 bio->bi_end_io = pkt_end_io_read;
761 bio->bi_private = pkt;
763 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
764 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
765 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
766 f, pkt->pages[p], offset);
767 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
770 atomic_inc(&pkt->io_wait);
772 pkt_queue_bio(pd, bio);
777 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
778 frames_read, (unsigned long long)pkt->sector);
779 pd->stats.pkt_started++;
780 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
784 * Find a packet matching zone, or the least recently used packet if
787 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
789 struct packet_data *pkt;
791 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
792 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
793 list_del_init(&pkt->list);
794 if (pkt->sector != zone)
795 pkt->cache_valid = 0;
803 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
805 if (pkt->cache_valid) {
806 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
808 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
813 * recover a failed write, query for relocation if possible
815 * returns 1 if recovery is possible, or 0 if not
818 static int pkt_start_recovery(struct packet_data *pkt)
821 * FIXME. We need help from the file system to implement
826 struct request *rq = pkt->rq;
827 struct pktcdvd_device *pd = rq->rq_disk->private_data;
828 struct block_device *pkt_bdev;
829 struct super_block *sb = NULL;
830 unsigned long old_block, new_block;
833 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
835 sb = get_super(pkt_bdev);
842 if (!sb->s_op || !sb->s_op->relocate_blocks)
845 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
846 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
849 new_sector = new_block * (CD_FRAMESIZE >> 9);
850 pkt->sector = new_sector;
852 pkt->bio->bi_sector = new_sector;
853 pkt->bio->bi_next = NULL;
854 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
855 pkt->bio->bi_idx = 0;
857 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
858 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
859 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
860 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
861 BUG_ON(pkt->bio->bi_private != pkt);
872 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
875 static const char *state_name[] = {
876 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
878 enum packet_data_state old_state = pkt->state;
879 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
880 state_name[old_state], state_name[state]);
886 * Scan the work queue to see if we can start a new packet.
887 * returns non-zero if any work was done.
889 static int pkt_handle_queue(struct pktcdvd_device *pd)
891 struct packet_data *pkt, *p;
892 struct bio *bio = NULL;
893 sector_t zone = 0; /* Suppress gcc warning */
894 struct pkt_rb_node *node, *first_node;
897 VPRINTK("handle_queue\n");
899 atomic_set(&pd->scan_queue, 0);
901 if (list_empty(&pd->cdrw.pkt_free_list)) {
902 VPRINTK("handle_queue: no pkt\n");
907 * Try to find a zone we are not already working on.
909 spin_lock(&pd->lock);
910 first_node = pkt_rbtree_find(pd, pd->current_sector);
912 n = rb_first(&pd->bio_queue);
914 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
919 zone = ZONE(bio->bi_sector, pd);
920 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
921 if (p->sector == zone) {
928 node = pkt_rbtree_next(node);
930 n = rb_first(&pd->bio_queue);
932 node = rb_entry(n, struct pkt_rb_node, rb_node);
934 if (node == first_node)
937 spin_unlock(&pd->lock);
939 VPRINTK("handle_queue: no bio\n");
943 pkt = pkt_get_packet_data(pd, zone);
945 pd->current_sector = zone + pd->settings.size;
947 BUG_ON(pkt->frames != pd->settings.size >> 2);
951 * Scan work queue for bios in the same zone and link them
954 spin_lock(&pd->lock);
955 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
956 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
958 VPRINTK("pkt_handle_queue: found zone=%llx\n",
959 (unsigned long long)ZONE(bio->bi_sector, pd));
960 if (ZONE(bio->bi_sector, pd) != zone)
962 pkt_rbtree_erase(pd, node);
963 spin_lock(&pkt->lock);
964 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
965 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
966 spin_unlock(&pkt->lock);
968 spin_unlock(&pd->lock);
970 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
971 pkt_set_state(pkt, PACKET_WAITING_STATE);
972 atomic_set(&pkt->run_sm, 1);
974 spin_lock(&pd->cdrw.active_list_lock);
975 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
976 spin_unlock(&pd->cdrw.active_list_lock);
982 * Assemble a bio to write one packet and queue the bio for processing
983 * by the underlying block device.
985 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
990 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
992 for (f = 0; f < pkt->frames; f++) {
993 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
994 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
998 * Fill-in bvec with data from orig_bios.
1001 spin_lock(&pkt->lock);
1002 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1003 int segment = bio->bi_idx;
1005 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1006 int num_frames = bio->bi_size / CD_FRAMESIZE;
1007 BUG_ON(first_frame < 0);
1008 BUG_ON(first_frame + num_frames > pkt->frames);
1009 for (f = first_frame; f < first_frame + num_frames; f++) {
1010 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1012 while (src_offs >= src_bvl->bv_len) {
1013 src_offs -= src_bvl->bv_len;
1015 BUG_ON(segment >= bio->bi_vcnt);
1016 src_bvl = bio_iovec_idx(bio, segment);
1019 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1020 bvec[f].bv_page = src_bvl->bv_page;
1021 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1023 pkt_copy_bio_data(bio, segment, src_offs,
1024 bvec[f].bv_page, bvec[f].bv_offset);
1026 src_offs += CD_FRAMESIZE;
1030 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1031 spin_unlock(&pkt->lock);
1033 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1034 frames_write, (unsigned long long)pkt->sector);
1035 BUG_ON(frames_write != pkt->write_size);
1037 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1038 pkt_make_local_copy(pkt, bvec);
1039 pkt->cache_valid = 1;
1041 pkt->cache_valid = 0;
1044 /* Start the write request */
1045 bio_init(pkt->w_bio);
1046 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1047 pkt->w_bio->bi_sector = pkt->sector;
1048 pkt->w_bio->bi_bdev = pd->bdev;
1049 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1050 pkt->w_bio->bi_private = pkt;
1051 for (f = 0; f < pkt->frames; f++)
1052 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1054 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
1056 atomic_set(&pkt->io_wait, 1);
1057 pkt->w_bio->bi_rw = WRITE;
1058 pkt_queue_bio(pd, pkt->w_bio);
1061 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1063 struct bio *bio, *next;
1066 pkt->cache_valid = 0;
1068 /* Finish all bios corresponding to this packet */
1069 bio = pkt->orig_bios;
1071 next = bio->bi_next;
1072 bio->bi_next = NULL;
1073 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1076 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1079 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1083 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1086 switch (pkt->state) {
1087 case PACKET_WAITING_STATE:
1088 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1091 pkt->sleep_time = 0;
1092 pkt_gather_data(pd, pkt);
1093 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1096 case PACKET_READ_WAIT_STATE:
1097 if (atomic_read(&pkt->io_wait) > 0)
1100 if (atomic_read(&pkt->io_errors) > 0) {
1101 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1103 pkt_start_write(pd, pkt);
1107 case PACKET_WRITE_WAIT_STATE:
1108 if (atomic_read(&pkt->io_wait) > 0)
1111 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1112 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1114 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1118 case PACKET_RECOVERY_STATE:
1119 if (pkt_start_recovery(pkt)) {
1120 pkt_start_write(pd, pkt);
1122 VPRINTK("No recovery possible\n");
1123 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1127 case PACKET_FINISHED_STATE:
1128 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1129 pkt_finish_packet(pkt, uptodate);
1139 static void pkt_handle_packets(struct pktcdvd_device *pd)
1141 struct packet_data *pkt, *next;
1143 VPRINTK("pkt_handle_packets\n");
1146 * Run state machine for active packets
1148 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1149 if (atomic_read(&pkt->run_sm) > 0) {
1150 atomic_set(&pkt->run_sm, 0);
1151 pkt_run_state_machine(pd, pkt);
1156 * Move no longer active packets to the free list
1158 spin_lock(&pd->cdrw.active_list_lock);
1159 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1160 if (pkt->state == PACKET_FINISHED_STATE) {
1161 list_del(&pkt->list);
1162 pkt_put_packet_data(pd, pkt);
1163 pkt_set_state(pkt, PACKET_IDLE_STATE);
1164 atomic_set(&pd->scan_queue, 1);
1167 spin_unlock(&pd->cdrw.active_list_lock);
1170 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1172 struct packet_data *pkt;
1175 for (i = 0; i < PACKET_NUM_STATES; i++)
1178 spin_lock(&pd->cdrw.active_list_lock);
1179 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1180 states[pkt->state]++;
1182 spin_unlock(&pd->cdrw.active_list_lock);
1186 * kcdrwd is woken up when writes have been queued for one of our
1187 * registered devices
1189 static int kcdrwd(void *foobar)
1191 struct pktcdvd_device *pd = foobar;
1192 struct packet_data *pkt;
1193 long min_sleep_time, residue;
1195 set_user_nice(current, -20);
1198 DECLARE_WAITQUEUE(wait, current);
1201 * Wait until there is something to do
1203 add_wait_queue(&pd->wqueue, &wait);
1205 set_current_state(TASK_INTERRUPTIBLE);
1207 /* Check if we need to run pkt_handle_queue */
1208 if (atomic_read(&pd->scan_queue) > 0)
1211 /* Check if we need to run the state machine for some packet */
1212 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1213 if (atomic_read(&pkt->run_sm) > 0)
1217 /* Check if we need to process the iosched queues */
1218 if (atomic_read(&pd->iosched.attention) != 0)
1221 /* Otherwise, go to sleep */
1222 if (PACKET_DEBUG > 1) {
1223 int states[PACKET_NUM_STATES];
1224 pkt_count_states(pd, states);
1225 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1226 states[0], states[1], states[2], states[3],
1227 states[4], states[5]);
1230 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1231 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1232 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1233 min_sleep_time = pkt->sleep_time;
1236 generic_unplug_device(bdev_get_queue(pd->bdev));
1238 VPRINTK("kcdrwd: sleeping\n");
1239 residue = schedule_timeout(min_sleep_time);
1240 VPRINTK("kcdrwd: wake up\n");
1242 /* make swsusp happy with our thread */
1245 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1246 if (!pkt->sleep_time)
1248 pkt->sleep_time -= min_sleep_time - residue;
1249 if (pkt->sleep_time <= 0) {
1250 pkt->sleep_time = 0;
1251 atomic_inc(&pkt->run_sm);
1255 if (signal_pending(current)) {
1256 flush_signals(current);
1258 if (kthread_should_stop())
1262 set_current_state(TASK_RUNNING);
1263 remove_wait_queue(&pd->wqueue, &wait);
1265 if (kthread_should_stop())
1269 * if pkt_handle_queue returns true, we can queue
1272 while (pkt_handle_queue(pd))
1276 * Handle packet state machine
1278 pkt_handle_packets(pd);
1281 * Handle iosched queues
1283 pkt_iosched_process_queue(pd);
1289 static void pkt_print_settings(struct pktcdvd_device *pd)
1291 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1292 printk("%u blocks, ", pd->settings.size >> 2);
1293 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1296 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1298 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1300 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1301 cgc->cmd[2] = page_code | (page_control << 6);
1302 cgc->cmd[7] = cgc->buflen >> 8;
1303 cgc->cmd[8] = cgc->buflen & 0xff;
1304 cgc->data_direction = CGC_DATA_READ;
1305 return pkt_generic_packet(pd, cgc);
1308 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1310 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1311 memset(cgc->buffer, 0, 2);
1312 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1313 cgc->cmd[1] = 0x10; /* PF */
1314 cgc->cmd[7] = cgc->buflen >> 8;
1315 cgc->cmd[8] = cgc->buflen & 0xff;
1316 cgc->data_direction = CGC_DATA_WRITE;
1317 return pkt_generic_packet(pd, cgc);
1320 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1322 struct packet_command cgc;
1325 /* set up command and get the disc info */
1326 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1327 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1328 cgc.cmd[8] = cgc.buflen = 2;
1331 if ((ret = pkt_generic_packet(pd, &cgc)))
1334 /* not all drives have the same disc_info length, so requeue
1335 * packet with the length the drive tells us it can supply
1337 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1338 sizeof(di->disc_information_length);
1340 if (cgc.buflen > sizeof(disc_information))
1341 cgc.buflen = sizeof(disc_information);
1343 cgc.cmd[8] = cgc.buflen;
1344 return pkt_generic_packet(pd, &cgc);
1347 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1349 struct packet_command cgc;
1352 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1353 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1354 cgc.cmd[1] = type & 3;
1355 cgc.cmd[4] = (track & 0xff00) >> 8;
1356 cgc.cmd[5] = track & 0xff;
1360 if ((ret = pkt_generic_packet(pd, &cgc)))
1363 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1364 sizeof(ti->track_information_length);
1366 if (cgc.buflen > sizeof(track_information))
1367 cgc.buflen = sizeof(track_information);
1369 cgc.cmd[8] = cgc.buflen;
1370 return pkt_generic_packet(pd, &cgc);
1373 static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1375 disc_information di;
1376 track_information ti;
1380 if ((ret = pkt_get_disc_info(pd, &di)))
1383 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1384 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1387 /* if this track is blank, try the previous. */
1390 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1394 /* if last recorded field is valid, return it. */
1396 *last_written = be32_to_cpu(ti.last_rec_address);
1398 /* make it up instead */
1399 *last_written = be32_to_cpu(ti.track_start) +
1400 be32_to_cpu(ti.track_size);
1402 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1408 * write mode select package based on pd->settings
1410 static int pkt_set_write_settings(struct pktcdvd_device *pd)
1412 struct packet_command cgc;
1413 struct request_sense sense;
1414 write_param_page *wp;
1418 /* doesn't apply to DVD+RW or DVD-RAM */
1419 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1422 memset(buffer, 0, sizeof(buffer));
1423 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1425 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1426 pkt_dump_sense(&cgc);
1430 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1431 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1432 if (size > sizeof(buffer))
1433 size = sizeof(buffer);
1438 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1440 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1441 pkt_dump_sense(&cgc);
1446 * write page is offset header + block descriptor length
1448 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1450 wp->fp = pd->settings.fp;
1451 wp->track_mode = pd->settings.track_mode;
1452 wp->write_type = pd->settings.write_type;
1453 wp->data_block_type = pd->settings.block_mode;
1455 wp->multi_session = 0;
1457 #ifdef PACKET_USE_LS
1462 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1463 wp->session_format = 0;
1465 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1466 wp->session_format = 0x20;
1470 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1476 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
1479 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1481 cgc.buflen = cgc.cmd[8] = size;
1482 if ((ret = pkt_mode_select(pd, &cgc))) {
1483 pkt_dump_sense(&cgc);
1487 pkt_print_settings(pd);
1492 * 1 -- we can write to this track, 0 -- we can't
1494 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1496 switch (pd->mmc3_profile) {
1497 case 0x1a: /* DVD+RW */
1498 case 0x12: /* DVD-RAM */
1499 /* The track is always writable on DVD+RW/DVD-RAM */
1505 if (!ti->packet || !ti->fp)
1509 * "good" settings as per Mt Fuji.
1511 if (ti->rt == 0 && ti->blank == 0)
1514 if (ti->rt == 0 && ti->blank == 1)
1517 if (ti->rt == 1 && ti->blank == 0)
1520 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1525 * 1 -- we can write to this disc, 0 -- we can't
1527 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1529 switch (pd->mmc3_profile) {
1530 case 0x0a: /* CD-RW */
1531 case 0xffff: /* MMC3 not supported */
1533 case 0x1a: /* DVD+RW */
1534 case 0x13: /* DVD-RW */
1535 case 0x12: /* DVD-RAM */
1538 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
1543 * for disc type 0xff we should probably reserve a new track.
1544 * but i'm not sure, should we leave this to user apps? probably.
1546 if (di->disc_type == 0xff) {
1547 printk(DRIVER_NAME": Unknown disc. No track?\n");
1551 if (di->disc_type != 0x20 && di->disc_type != 0) {
1552 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
1556 if (di->erasable == 0) {
1557 printk(DRIVER_NAME": Disc not erasable\n");
1561 if (di->border_status == PACKET_SESSION_RESERVED) {
1562 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
1569 static int pkt_probe_settings(struct pktcdvd_device *pd)
1571 struct packet_command cgc;
1572 unsigned char buf[12];
1573 disc_information di;
1574 track_information ti;
1577 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1578 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1580 ret = pkt_generic_packet(pd, &cgc);
1581 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1583 memset(&di, 0, sizeof(disc_information));
1584 memset(&ti, 0, sizeof(track_information));
1586 if ((ret = pkt_get_disc_info(pd, &di))) {
1587 printk("failed get_disc\n");
1591 if (!pkt_writable_disc(pd, &di))
1594 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1596 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1597 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1598 printk(DRIVER_NAME": failed get_track\n");
1602 if (!pkt_writable_track(pd, &ti)) {
1603 printk(DRIVER_NAME": can't write to this track\n");
1608 * we keep packet size in 512 byte units, makes it easier to
1609 * deal with request calculations.
1611 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1612 if (pd->settings.size == 0) {
1613 printk(DRIVER_NAME": detected zero packet size!\n");
1616 if (pd->settings.size > PACKET_MAX_SECTORS) {
1617 printk(DRIVER_NAME": packet size is too big\n");
1620 pd->settings.fp = ti.fp;
1621 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1624 pd->nwa = be32_to_cpu(ti.next_writable);
1625 set_bit(PACKET_NWA_VALID, &pd->flags);
1629 * in theory we could use lra on -RW media as well and just zero
1630 * blocks that haven't been written yet, but in practice that
1631 * is just a no-go. we'll use that for -R, naturally.
1634 pd->lra = be32_to_cpu(ti.last_rec_address);
1635 set_bit(PACKET_LRA_VALID, &pd->flags);
1637 pd->lra = 0xffffffff;
1638 set_bit(PACKET_LRA_VALID, &pd->flags);
1644 pd->settings.link_loss = 7;
1645 pd->settings.write_type = 0; /* packet */
1646 pd->settings.track_mode = ti.track_mode;
1649 * mode1 or mode2 disc
1651 switch (ti.data_mode) {
1653 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1656 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1659 printk(DRIVER_NAME": unknown data mode\n");
1666 * enable/disable write caching on drive
1668 static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1670 struct packet_command cgc;
1671 struct request_sense sense;
1672 unsigned char buf[64];
1675 memset(buf, 0, sizeof(buf));
1676 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1678 cgc.buflen = pd->mode_offset + 12;
1681 * caching mode page might not be there, so quiet this command
1685 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1688 buf[pd->mode_offset + 10] |= (!!set << 2);
1690 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1691 ret = pkt_mode_select(pd, &cgc);
1693 printk(DRIVER_NAME": write caching control failed\n");
1694 pkt_dump_sense(&cgc);
1695 } else if (!ret && set)
1696 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
1700 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1702 struct packet_command cgc;
1704 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1705 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1706 cgc.cmd[4] = lockflag ? 1 : 0;
1707 return pkt_generic_packet(pd, &cgc);
1711 * Returns drive maximum write speed
1713 static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1715 struct packet_command cgc;
1716 struct request_sense sense;
1717 unsigned char buf[256+18];
1718 unsigned char *cap_buf;
1721 memset(buf, 0, sizeof(buf));
1722 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1723 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1726 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1728 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1729 sizeof(struct mode_page_header);
1730 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1732 pkt_dump_sense(&cgc);
1737 offset = 20; /* Obsoleted field, used by older drives */
1738 if (cap_buf[1] >= 28)
1739 offset = 28; /* Current write speed selected */
1740 if (cap_buf[1] >= 30) {
1741 /* If the drive reports at least one "Logical Unit Write
1742 * Speed Performance Descriptor Block", use the information
1743 * in the first block. (contains the highest speed)
1745 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1750 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1754 /* These tables from cdrecord - I don't have orange book */
1755 /* standard speed CD-RW (1-4x) */
1756 static char clv_to_speed[16] = {
1757 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1758 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1760 /* high speed CD-RW (-10x) */
1761 static char hs_clv_to_speed[16] = {
1762 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1763 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1765 /* ultra high speed CD-RW */
1766 static char us_clv_to_speed[16] = {
1767 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1768 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1772 * reads the maximum media speed from ATIP
1774 static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1776 struct packet_command cgc;
1777 struct request_sense sense;
1778 unsigned char buf[64];
1779 unsigned int size, st, sp;
1782 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1784 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1786 cgc.cmd[2] = 4; /* READ ATIP */
1788 ret = pkt_generic_packet(pd, &cgc);
1790 pkt_dump_sense(&cgc);
1793 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1794 if (size > sizeof(buf))
1797 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1799 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1803 ret = pkt_generic_packet(pd, &cgc);
1805 pkt_dump_sense(&cgc);
1809 if (!buf[6] & 0x40) {
1810 printk(DRIVER_NAME": Disc type is not CD-RW\n");
1813 if (!buf[6] & 0x4) {
1814 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
1818 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1820 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1822 /* Info from cdrecord */
1824 case 0: /* standard speed */
1825 *speed = clv_to_speed[sp];
1827 case 1: /* high speed */
1828 *speed = hs_clv_to_speed[sp];
1830 case 2: /* ultra high speed */
1831 *speed = us_clv_to_speed[sp];
1834 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
1838 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
1841 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
1846 static int pkt_perform_opc(struct pktcdvd_device *pd)
1848 struct packet_command cgc;
1849 struct request_sense sense;
1852 VPRINTK(DRIVER_NAME": Performing OPC\n");
1854 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1856 cgc.timeout = 60*HZ;
1857 cgc.cmd[0] = GPCMD_SEND_OPC;
1859 if ((ret = pkt_generic_packet(pd, &cgc)))
1860 pkt_dump_sense(&cgc);
1864 static int pkt_open_write(struct pktcdvd_device *pd)
1867 unsigned int write_speed, media_write_speed, read_speed;
1869 if ((ret = pkt_probe_settings(pd))) {
1870 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
1874 if ((ret = pkt_set_write_settings(pd))) {
1875 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
1879 pkt_write_caching(pd, USE_WCACHING);
1881 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1882 write_speed = 16 * 177;
1883 switch (pd->mmc3_profile) {
1884 case 0x13: /* DVD-RW */
1885 case 0x1a: /* DVD+RW */
1886 case 0x12: /* DVD-RAM */
1887 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
1890 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1891 media_write_speed = 16;
1892 write_speed = min(write_speed, media_write_speed * 177);
1893 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
1896 read_speed = write_speed;
1898 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1899 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
1902 pd->write_speed = write_speed;
1903 pd->read_speed = read_speed;
1905 if ((ret = pkt_perform_opc(pd))) {
1906 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
1913 * called at open time.
1915 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1922 * We need to re-open the cdrom device without O_NONBLOCK to be able
1923 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1924 * so bdget() can't fail.
1926 bdget(pd->bdev->bd_dev);
1927 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1930 if ((ret = bd_claim(pd->bdev, pd)))
1933 if ((ret = pkt_get_last_written(pd, &lba))) {
1934 printk(DRIVER_NAME": pkt_get_last_written failed\n");
1938 set_capacity(pd->disk, lba << 2);
1939 set_capacity(pd->bdev->bd_disk, lba << 2);
1940 bd_set_size(pd->bdev, (loff_t)lba << 11);
1942 q = bdev_get_queue(pd->bdev);
1944 if ((ret = pkt_open_write(pd)))
1947 * Some CDRW drives can not handle writes larger than one packet,
1948 * even if the size is a multiple of the packet size.
1950 spin_lock_irq(q->queue_lock);
1951 blk_queue_max_sectors(q, pd->settings.size);
1952 spin_unlock_irq(q->queue_lock);
1953 set_bit(PACKET_WRITABLE, &pd->flags);
1955 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1956 clear_bit(PACKET_WRITABLE, &pd->flags);
1959 if ((ret = pkt_set_segment_merging(pd, q)))
1963 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
1964 printk(DRIVER_NAME": not enough memory for buffers\n");
1968 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
1974 bd_release(pd->bdev);
1976 blkdev_put(pd->bdev);
1982 * called when the device is closed. makes sure that the device flushes
1983 * the internal cache before we close.
1985 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
1987 if (flush && pkt_flush_cache(pd))
1988 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
1990 pkt_lock_door(pd, 0);
1992 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1993 bd_release(pd->bdev);
1994 blkdev_put(pd->bdev);
1996 pkt_shrink_pktlist(pd);
1999 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2001 if (dev_minor >= MAX_WRITERS)
2003 return pkt_devs[dev_minor];
2006 static int pkt_open(struct inode *inode, struct file *file)
2008 struct pktcdvd_device *pd = NULL;
2011 VPRINTK(DRIVER_NAME": entering open\n");
2013 mutex_lock(&ctl_mutex);
2014 pd = pkt_find_dev_from_minor(iminor(inode));
2019 BUG_ON(pd->refcnt < 0);
2022 if (pd->refcnt > 1) {
2023 if ((file->f_mode & FMODE_WRITE) &&
2024 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2029 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2033 * needed here as well, since ext2 (among others) may change
2034 * the blocksize at mount time
2036 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2039 mutex_unlock(&ctl_mutex);
2045 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
2046 mutex_unlock(&ctl_mutex);
2050 static int pkt_close(struct inode *inode, struct file *file)
2052 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2055 mutex_lock(&ctl_mutex);
2057 BUG_ON(pd->refcnt < 0);
2058 if (pd->refcnt == 0) {
2059 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2060 pkt_release_dev(pd, flush);
2062 mutex_unlock(&ctl_mutex);
2067 static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2069 struct packet_stacked_data *psd = bio->bi_private;
2070 struct pktcdvd_device *pd = psd->pd;
2076 bio_endio(psd->bio, psd->bio->bi_size, err);
2077 mempool_free(psd, psd_pool);
2078 pkt_bio_finished(pd);
2082 static int pkt_make_request(request_queue_t *q, struct bio *bio)
2084 struct pktcdvd_device *pd;
2085 char b[BDEVNAME_SIZE];
2087 struct packet_data *pkt;
2088 int was_empty, blocked_bio;
2089 struct pkt_rb_node *node;
2093 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2098 * Clone READ bios so we can have our own bi_end_io callback.
2100 if (bio_data_dir(bio) == READ) {
2101 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2102 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2106 cloned_bio->bi_bdev = pd->bdev;
2107 cloned_bio->bi_private = psd;
2108 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2109 pd->stats.secs_r += bio->bi_size >> 9;
2110 pkt_queue_bio(pd, cloned_bio);
2114 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2115 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
2116 pd->name, (unsigned long long)bio->bi_sector);
2120 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2121 printk(DRIVER_NAME": wrong bio size\n");
2125 blk_queue_bounce(q, &bio);
2127 zone = ZONE(bio->bi_sector, pd);
2128 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2129 (unsigned long long)bio->bi_sector,
2130 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2132 /* Check if we have to split the bio */
2134 struct bio_pair *bp;
2138 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2139 if (last_zone != zone) {
2140 BUG_ON(last_zone != zone + pd->settings.size);
2141 first_sectors = last_zone - bio->bi_sector;
2142 bp = bio_split(bio, bio_split_pool, first_sectors);
2144 pkt_make_request(q, &bp->bio1);
2145 pkt_make_request(q, &bp->bio2);
2146 bio_pair_release(bp);
2152 * If we find a matching packet in state WAITING or READ_WAIT, we can
2153 * just append this bio to that packet.
2155 spin_lock(&pd->cdrw.active_list_lock);
2157 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2158 if (pkt->sector == zone) {
2159 spin_lock(&pkt->lock);
2160 if ((pkt->state == PACKET_WAITING_STATE) ||
2161 (pkt->state == PACKET_READ_WAIT_STATE)) {
2162 pkt_add_list_last(bio, &pkt->orig_bios,
2163 &pkt->orig_bios_tail);
2164 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2165 if ((pkt->write_size >= pkt->frames) &&
2166 (pkt->state == PACKET_WAITING_STATE)) {
2167 atomic_inc(&pkt->run_sm);
2168 wake_up(&pd->wqueue);
2170 spin_unlock(&pkt->lock);
2171 spin_unlock(&pd->cdrw.active_list_lock);
2176 spin_unlock(&pkt->lock);
2179 spin_unlock(&pd->cdrw.active_list_lock);
2182 * No matching packet found. Store the bio in the work queue.
2184 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2186 spin_lock(&pd->lock);
2187 BUG_ON(pd->bio_queue_size < 0);
2188 was_empty = (pd->bio_queue_size == 0);
2189 pkt_rbtree_insert(pd, node);
2190 spin_unlock(&pd->lock);
2193 * Wake up the worker thread.
2195 atomic_set(&pd->scan_queue, 1);
2197 /* This wake_up is required for correct operation */
2198 wake_up(&pd->wqueue);
2199 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2201 * This wake up is not required for correct operation,
2202 * but improves performance in some cases.
2204 wake_up(&pd->wqueue);
2208 bio_io_error(bio, bio->bi_size);
2214 static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2216 struct pktcdvd_device *pd = q->queuedata;
2217 sector_t zone = ZONE(bio->bi_sector, pd);
2218 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2219 int remaining = (pd->settings.size << 9) - used;
2223 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2224 * boundary, pkt_make_request() will split the bio.
2226 remaining2 = PAGE_SIZE - bio->bi_size;
2227 remaining = max(remaining, remaining2);
2229 BUG_ON(remaining < 0);
2233 static void pkt_init_queue(struct pktcdvd_device *pd)
2235 request_queue_t *q = pd->disk->queue;
2237 blk_queue_make_request(q, pkt_make_request);
2238 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2239 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2240 blk_queue_merge_bvec(q, pkt_merge_bvec);
2244 static int pkt_seq_show(struct seq_file *m, void *p)
2246 struct pktcdvd_device *pd = m->private;
2248 char bdev_buf[BDEVNAME_SIZE];
2249 int states[PACKET_NUM_STATES];
2251 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2252 bdevname(pd->bdev, bdev_buf));
2254 seq_printf(m, "\nSettings:\n");
2255 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2257 if (pd->settings.write_type == 0)
2261 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2263 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2264 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2266 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2268 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2270 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2274 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2276 seq_printf(m, "\nStatistics:\n");
2277 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2278 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2279 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2280 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2281 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2283 seq_printf(m, "\nMisc:\n");
2284 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2285 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2286 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2287 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2288 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2289 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2291 seq_printf(m, "\nQueue state:\n");
2292 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2293 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2294 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2296 pkt_count_states(pd, states);
2297 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2298 states[0], states[1], states[2], states[3], states[4], states[5]);
2303 static int pkt_seq_open(struct inode *inode, struct file *file)
2305 return single_open(file, pkt_seq_show, PDE(inode)->data);
2308 static struct file_operations pkt_proc_fops = {
2309 .open = pkt_seq_open,
2311 .llseek = seq_lseek,
2312 .release = single_release
2315 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2319 char b[BDEVNAME_SIZE];
2320 struct proc_dir_entry *proc;
2321 struct block_device *bdev;
2323 if (pd->pkt_dev == dev) {
2324 printk(DRIVER_NAME": Recursive setup not allowed\n");
2327 for (i = 0; i < MAX_WRITERS; i++) {
2328 struct pktcdvd_device *pd2 = pkt_devs[i];
2331 if (pd2->bdev->bd_dev == dev) {
2332 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
2335 if (pd2->pkt_dev == dev) {
2336 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
2344 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2348 /* This is safe, since we have a reference from open(). */
2349 __module_get(THIS_MODULE);
2352 set_blocksize(bdev, CD_FRAMESIZE);
2356 atomic_set(&pd->cdrw.pending_bios, 0);
2357 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2358 if (IS_ERR(pd->cdrw.thread)) {
2359 printk(DRIVER_NAME": can't start kernel thread\n");
2364 proc = create_proc_entry(pd->name, 0, pkt_proc);
2367 proc->proc_fops = &pkt_proc_fops;
2369 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2374 /* This is safe: open() is still holding a reference. */
2375 module_put(THIS_MODULE);
2379 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2381 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2383 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2387 * forward selected CDROM ioctls to CD-ROM, for UDF
2389 case CDROMMULTISESSION:
2390 case CDROMREADTOCENTRY:
2391 case CDROM_LAST_WRITTEN:
2392 case CDROM_SEND_PACKET:
2393 case SCSI_IOCTL_SEND_COMMAND:
2394 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2398 * The door gets locked when the device is opened, so we
2399 * have to unlock it or else the eject command fails.
2401 if (pd->refcnt == 1)
2402 pkt_lock_door(pd, 0);
2403 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2406 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
2413 static int pkt_media_changed(struct gendisk *disk)
2415 struct pktcdvd_device *pd = disk->private_data;
2416 struct gendisk *attached_disk;
2422 attached_disk = pd->bdev->bd_disk;
2425 return attached_disk->fops->media_changed(attached_disk);
2428 static struct block_device_operations pktcdvd_ops = {
2429 .owner = THIS_MODULE,
2431 .release = pkt_close,
2433 .media_changed = pkt_media_changed,
2437 * Set up mapping from pktcdvd device to CD-ROM device.
2439 static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2443 struct pktcdvd_device *pd;
2444 struct gendisk *disk;
2445 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2447 for (idx = 0; idx < MAX_WRITERS; idx++)
2450 if (idx == MAX_WRITERS) {
2451 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
2455 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2459 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2460 sizeof(struct pkt_rb_node));
2464 disk = alloc_disk(1);
2469 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2470 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2471 spin_lock_init(&pd->cdrw.active_list_lock);
2473 spin_lock_init(&pd->lock);
2474 spin_lock_init(&pd->iosched.lock);
2475 sprintf(pd->name, DRIVER_NAME"%d", idx);
2476 init_waitqueue_head(&pd->wqueue);
2477 pd->bio_queue = RB_ROOT;
2479 disk->major = pktdev_major;
2480 disk->first_minor = idx;
2481 disk->fops = &pktcdvd_ops;
2482 disk->flags = GENHD_FL_REMOVABLE;
2483 sprintf(disk->disk_name, DRIVER_NAME"%d", idx);
2484 disk->private_data = pd;
2485 disk->queue = blk_alloc_queue(GFP_KERNEL);
2489 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2490 ret = pkt_new_dev(pd, dev);
2496 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2500 blk_cleanup_queue(disk->queue);
2505 mempool_destroy(pd->rb_pool);
2511 * Tear down mapping from pktcdvd device to CD-ROM device.
2513 static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2515 struct pktcdvd_device *pd;
2517 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2519 for (idx = 0; idx < MAX_WRITERS; idx++) {
2521 if (pd && (pd->pkt_dev == pkt_dev))
2524 if (idx == MAX_WRITERS) {
2525 DPRINTK(DRIVER_NAME": dev not setup\n");
2532 if (!IS_ERR(pd->cdrw.thread))
2533 kthread_stop(pd->cdrw.thread);
2535 blkdev_put(pd->bdev);
2537 remove_proc_entry(pd->name, pkt_proc);
2538 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
2540 del_gendisk(pd->disk);
2541 blk_cleanup_queue(pd->disk->queue);
2544 pkt_devs[idx] = NULL;
2545 mempool_destroy(pd->rb_pool);
2548 /* This is safe: open() is still holding a reference. */
2549 module_put(THIS_MODULE);
2553 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2555 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2557 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2558 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2561 ctrl_cmd->pkt_dev = 0;
2563 ctrl_cmd->num_devices = MAX_WRITERS;
2566 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2568 void __user *argp = (void __user *)arg;
2569 struct pkt_ctrl_command ctrl_cmd;
2572 if (cmd != PACKET_CTRL_CMD)
2575 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2578 switch (ctrl_cmd.command) {
2579 case PKT_CTRL_CMD_SETUP:
2580 if (!capable(CAP_SYS_ADMIN))
2582 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2583 ret = pkt_setup_dev(&ctrl_cmd);
2584 mutex_unlock(&ctl_mutex);
2586 case PKT_CTRL_CMD_TEARDOWN:
2587 if (!capable(CAP_SYS_ADMIN))
2589 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2590 ret = pkt_remove_dev(&ctrl_cmd);
2591 mutex_unlock(&ctl_mutex);
2593 case PKT_CTRL_CMD_STATUS:
2594 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2595 pkt_get_status(&ctrl_cmd);
2596 mutex_unlock(&ctl_mutex);
2602 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2608 static struct file_operations pkt_ctl_fops = {
2609 .ioctl = pkt_ctl_ioctl,
2610 .owner = THIS_MODULE,
2613 static struct miscdevice pkt_misc = {
2614 .minor = MISC_DYNAMIC_MINOR,
2615 .name = DRIVER_NAME,
2616 .fops = &pkt_ctl_fops
2619 static int __init pkt_init(void)
2623 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
2624 sizeof(struct packet_stacked_data));
2628 ret = register_blkdev(pktdev_major, DRIVER_NAME);
2630 printk(DRIVER_NAME": Unable to register block device\n");
2636 ret = misc_register(&pkt_misc);
2638 printk(DRIVER_NAME": Unable to register misc device\n");
2642 mutex_init(&ctl_mutex);
2644 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
2649 unregister_blkdev(pktdev_major, DRIVER_NAME);
2651 mempool_destroy(psd_pool);
2655 static void __exit pkt_exit(void)
2657 remove_proc_entry(DRIVER_NAME, proc_root_driver);
2658 misc_deregister(&pkt_misc);
2659 unregister_blkdev(pktdev_major, DRIVER_NAME);
2660 mempool_destroy(psd_pool);
2663 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2664 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2665 MODULE_LICENSE("GPL");
2667 module_init(pkt_init);
2668 module_exit(pkt_exit);