[PATCH] irq-flags: ARM26: Use the new IRQF_ constants
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / pktcdvd.c
blobbde2c64b634660b0a49057b127ce5e05a97c8cc1
1 /*
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
9 * DVD-RAM devices.
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/suspend.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 #if PACKET_DEBUG
66 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
67 #else
68 #define DPRINTK(fmt, args...)
69 #endif
71 #if PACKET_DEBUG > 1
72 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
73 #else
74 #define VPRINTK(fmt, args...)
75 #endif
77 #define MAX_SPEED 0xffff
79 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
81 static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
82 static struct proc_dir_entry *pkt_proc;
83 static int pkt_major;
84 static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
85 static mempool_t *psd_pool;
88 static void pkt_bio_finished(struct pktcdvd_device *pd)
90 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
91 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
92 VPRINTK("pktcdvd: queue empty\n");
93 atomic_set(&pd->iosched.attention, 1);
94 wake_up(&pd->wqueue);
98 static void pkt_bio_destructor(struct bio *bio)
100 kfree(bio->bi_io_vec);
101 kfree(bio);
104 static struct bio *pkt_bio_alloc(int nr_iovecs)
106 struct bio_vec *bvl = NULL;
107 struct bio *bio;
109 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
110 if (!bio)
111 goto no_bio;
112 bio_init(bio);
114 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
115 if (!bvl)
116 goto no_bvl;
118 bio->bi_max_vecs = nr_iovecs;
119 bio->bi_io_vec = bvl;
120 bio->bi_destructor = pkt_bio_destructor;
122 return bio;
124 no_bvl:
125 kfree(bio);
126 no_bio:
127 return NULL;
131 * Allocate a packet_data struct
133 static struct packet_data *pkt_alloc_packet_data(int frames)
135 int i;
136 struct packet_data *pkt;
138 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
139 if (!pkt)
140 goto no_pkt;
142 pkt->frames = frames;
143 pkt->w_bio = pkt_bio_alloc(frames);
144 if (!pkt->w_bio)
145 goto no_bio;
147 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
148 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
149 if (!pkt->pages[i])
150 goto no_page;
153 spin_lock_init(&pkt->lock);
155 for (i = 0; i < frames; i++) {
156 struct bio *bio = pkt_bio_alloc(1);
157 if (!bio)
158 goto no_rd_bio;
159 pkt->r_bios[i] = bio;
162 return pkt;
164 no_rd_bio:
165 for (i = 0; i < frames; i++) {
166 struct bio *bio = pkt->r_bios[i];
167 if (bio)
168 bio_put(bio);
171 no_page:
172 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
173 if (pkt->pages[i])
174 __free_page(pkt->pages[i]);
175 bio_put(pkt->w_bio);
176 no_bio:
177 kfree(pkt);
178 no_pkt:
179 return NULL;
183 * Free a packet_data struct
185 static void pkt_free_packet_data(struct packet_data *pkt)
187 int i;
189 for (i = 0; i < pkt->frames; i++) {
190 struct bio *bio = pkt->r_bios[i];
191 if (bio)
192 bio_put(bio);
194 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
195 __free_page(pkt->pages[i]);
196 bio_put(pkt->w_bio);
197 kfree(pkt);
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);
209 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
212 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
214 struct packet_data *pkt;
216 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
218 while (nr_packets > 0) {
219 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
220 if (!pkt) {
221 pkt_shrink_pktlist(pd);
222 return 0;
224 pkt->id = nr_packets;
225 pkt->pd = pd;
226 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
227 nr_packets--;
229 return 1;
232 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
234 struct rb_node *n = rb_next(&node->rb_node);
235 if (!n)
236 return NULL;
237 return rb_entry(n, struct pkt_rb_node, rb_node);
240 static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
242 rb_erase(&node->rb_node, &pd->bio_queue);
243 mempool_free(node, pd->rb_pool);
244 pd->bio_queue_size--;
245 BUG_ON(pd->bio_queue_size < 0);
249 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
251 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
253 struct rb_node *n = pd->bio_queue.rb_node;
254 struct rb_node *next;
255 struct pkt_rb_node *tmp;
257 if (!n) {
258 BUG_ON(pd->bio_queue_size > 0);
259 return NULL;
262 for (;;) {
263 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
264 if (s <= tmp->bio->bi_sector)
265 next = n->rb_left;
266 else
267 next = n->rb_right;
268 if (!next)
269 break;
270 n = next;
273 if (s > tmp->bio->bi_sector) {
274 tmp = pkt_rbtree_next(tmp);
275 if (!tmp)
276 return NULL;
278 BUG_ON(s > tmp->bio->bi_sector);
279 return tmp;
283 * Insert a node into the pd->bio_queue rb tree.
285 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
287 struct rb_node **p = &pd->bio_queue.rb_node;
288 struct rb_node *parent = NULL;
289 sector_t s = node->bio->bi_sector;
290 struct pkt_rb_node *tmp;
292 while (*p) {
293 parent = *p;
294 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
295 if (s < tmp->bio->bi_sector)
296 p = &(*p)->rb_left;
297 else
298 p = &(*p)->rb_right;
300 rb_link_node(&node->rb_node, parent, p);
301 rb_insert_color(&node->rb_node, &pd->bio_queue);
302 pd->bio_queue_size++;
306 * Add a bio to a single linked list defined by its head and tail pointers.
308 static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
310 bio->bi_next = NULL;
311 if (*list_tail) {
312 BUG_ON((*list_head) == NULL);
313 (*list_tail)->bi_next = bio;
314 (*list_tail) = bio;
315 } else {
316 BUG_ON((*list_head) != NULL);
317 (*list_head) = bio;
318 (*list_tail) = bio;
323 * Remove and return the first bio from a single linked list defined by its
324 * head and tail pointers.
326 static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
328 struct bio *bio;
330 if (*list_head == NULL)
331 return NULL;
333 bio = *list_head;
334 *list_head = bio->bi_next;
335 if (*list_head == NULL)
336 *list_tail = NULL;
338 bio->bi_next = NULL;
339 return bio;
343 * Send a packet_command to the underlying block device and
344 * wait for completion.
346 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
348 char sense[SCSI_SENSE_BUFFERSIZE];
349 request_queue_t *q;
350 struct request *rq;
351 DECLARE_COMPLETION(wait);
352 int err = 0;
354 q = bdev_get_queue(pd->bdev);
356 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
357 __GFP_WAIT);
358 rq->errors = 0;
359 rq->rq_disk = pd->bdev->bd_disk;
360 rq->bio = NULL;
361 rq->buffer = NULL;
362 rq->timeout = 60*HZ;
363 rq->data = cgc->buffer;
364 rq->data_len = cgc->buflen;
365 rq->sense = sense;
366 memset(sense, 0, sizeof(sense));
367 rq->sense_len = 0;
368 rq->flags |= REQ_BLOCK_PC | REQ_HARDBARRIER;
369 if (cgc->quiet)
370 rq->flags |= REQ_QUIET;
371 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
372 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
373 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
374 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
376 rq->ref_count++;
377 rq->flags |= REQ_NOMERGE;
378 rq->waiting = &wait;
379 rq->end_io = blk_end_sync_rq;
380 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
381 generic_unplug_device(q);
382 wait_for_completion(&wait);
384 if (rq->errors)
385 err = -EIO;
387 blk_put_request(rq);
388 return err;
392 * A generic sense dump / resolve mechanism should be implemented across
393 * all ATAPI + SCSI devices.
395 static void pkt_dump_sense(struct packet_command *cgc)
397 static char *info[9] = { "No sense", "Recovered error", "Not ready",
398 "Medium error", "Hardware error", "Illegal request",
399 "Unit attention", "Data protect", "Blank check" };
400 int i;
401 struct request_sense *sense = cgc->sense;
403 printk("pktcdvd:");
404 for (i = 0; i < CDROM_PACKET_SIZE; i++)
405 printk(" %02x", cgc->cmd[i]);
406 printk(" - ");
408 if (sense == NULL) {
409 printk("no sense\n");
410 return;
413 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
415 if (sense->sense_key > 8) {
416 printk(" (INVALID)\n");
417 return;
420 printk(" (%s)\n", info[sense->sense_key]);
424 * flush the drive cache to media
426 static int pkt_flush_cache(struct pktcdvd_device *pd)
428 struct packet_command cgc;
430 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
431 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
432 cgc.quiet = 1;
435 * the IMMED bit -- we default to not setting it, although that
436 * would allow a much faster close, this is safer
438 #if 0
439 cgc.cmd[1] = 1 << 1;
440 #endif
441 return pkt_generic_packet(pd, &cgc);
445 * speed is given as the normal factor, e.g. 4 for 4x
447 static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
449 struct packet_command cgc;
450 struct request_sense sense;
451 int ret;
453 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
454 cgc.sense = &sense;
455 cgc.cmd[0] = GPCMD_SET_SPEED;
456 cgc.cmd[2] = (read_speed >> 8) & 0xff;
457 cgc.cmd[3] = read_speed & 0xff;
458 cgc.cmd[4] = (write_speed >> 8) & 0xff;
459 cgc.cmd[5] = write_speed & 0xff;
461 if ((ret = pkt_generic_packet(pd, &cgc)))
462 pkt_dump_sense(&cgc);
464 return ret;
468 * Queue a bio for processing by the low-level CD device. Must be called
469 * from process context.
471 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
473 spin_lock(&pd->iosched.lock);
474 if (bio_data_dir(bio) == READ) {
475 pkt_add_list_last(bio, &pd->iosched.read_queue,
476 &pd->iosched.read_queue_tail);
477 } else {
478 pkt_add_list_last(bio, &pd->iosched.write_queue,
479 &pd->iosched.write_queue_tail);
481 spin_unlock(&pd->iosched.lock);
483 atomic_set(&pd->iosched.attention, 1);
484 wake_up(&pd->wqueue);
488 * Process the queued read/write requests. This function handles special
489 * requirements for CDRW drives:
490 * - A cache flush command must be inserted before a read request if the
491 * previous request was a write.
492 * - Switching between reading and writing is slow, so don't do it more often
493 * than necessary.
494 * - Optimize for throughput at the expense of latency. This means that streaming
495 * writes will never be interrupted by a read, but if the drive has to seek
496 * before the next write, switch to reading instead if there are any pending
497 * read requests.
498 * - Set the read speed according to current usage pattern. When only reading
499 * from the device, it's best to use the highest possible read speed, but
500 * when switching often between reading and writing, it's better to have the
501 * same read and write speeds.
503 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
506 if (atomic_read(&pd->iosched.attention) == 0)
507 return;
508 atomic_set(&pd->iosched.attention, 0);
510 for (;;) {
511 struct bio *bio;
512 int reads_queued, writes_queued;
514 spin_lock(&pd->iosched.lock);
515 reads_queued = (pd->iosched.read_queue != NULL);
516 writes_queued = (pd->iosched.write_queue != NULL);
517 spin_unlock(&pd->iosched.lock);
519 if (!reads_queued && !writes_queued)
520 break;
522 if (pd->iosched.writing) {
523 int need_write_seek = 1;
524 spin_lock(&pd->iosched.lock);
525 bio = pd->iosched.write_queue;
526 spin_unlock(&pd->iosched.lock);
527 if (bio && (bio->bi_sector == pd->iosched.last_write))
528 need_write_seek = 0;
529 if (need_write_seek && reads_queued) {
530 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
531 VPRINTK("pktcdvd: write, waiting\n");
532 break;
534 pkt_flush_cache(pd);
535 pd->iosched.writing = 0;
537 } else {
538 if (!reads_queued && writes_queued) {
539 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
540 VPRINTK("pktcdvd: read, waiting\n");
541 break;
543 pd->iosched.writing = 1;
547 spin_lock(&pd->iosched.lock);
548 if (pd->iosched.writing) {
549 bio = pkt_get_list_first(&pd->iosched.write_queue,
550 &pd->iosched.write_queue_tail);
551 } else {
552 bio = pkt_get_list_first(&pd->iosched.read_queue,
553 &pd->iosched.read_queue_tail);
555 spin_unlock(&pd->iosched.lock);
557 if (!bio)
558 continue;
560 if (bio_data_dir(bio) == READ)
561 pd->iosched.successive_reads += bio->bi_size >> 10;
562 else {
563 pd->iosched.successive_reads = 0;
564 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
566 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
567 if (pd->read_speed == pd->write_speed) {
568 pd->read_speed = MAX_SPEED;
569 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
571 } else {
572 if (pd->read_speed != pd->write_speed) {
573 pd->read_speed = pd->write_speed;
574 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
578 atomic_inc(&pd->cdrw.pending_bios);
579 generic_make_request(bio);
584 * Special care is needed if the underlying block device has a small
585 * max_phys_segments value.
587 static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
589 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
591 * The cdrom device can handle one segment/frame
593 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
594 return 0;
595 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
597 * We can handle this case at the expense of some extra memory
598 * copies during write operations
600 set_bit(PACKET_MERGE_SEGS, &pd->flags);
601 return 0;
602 } else {
603 printk("pktcdvd: cdrom max_phys_segments too small\n");
604 return -EIO;
609 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
611 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
613 unsigned int copy_size = CD_FRAMESIZE;
615 while (copy_size > 0) {
616 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
617 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
618 src_bvl->bv_offset + offs;
619 void *vto = page_address(dst_page) + dst_offs;
620 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
622 BUG_ON(len < 0);
623 memcpy(vto, vfrom, len);
624 kunmap_atomic(vfrom, KM_USER0);
626 seg++;
627 offs = 0;
628 dst_offs += len;
629 copy_size -= len;
634 * Copy all data for this packet to pkt->pages[], so that
635 * a) The number of required segments for the write bio is minimized, which
636 * is necessary for some scsi controllers.
637 * b) The data can be used as cache to avoid read requests if we receive a
638 * new write request for the same zone.
640 static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
642 int f, p, offs;
644 /* Copy all data to pkt->pages[] */
645 p = 0;
646 offs = 0;
647 for (f = 0; f < pkt->frames; f++) {
648 if (bvec[f].bv_page != pkt->pages[p]) {
649 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
650 void *vto = page_address(pkt->pages[p]) + offs;
651 memcpy(vto, vfrom, CD_FRAMESIZE);
652 kunmap_atomic(vfrom, KM_USER0);
653 bvec[f].bv_page = pkt->pages[p];
654 bvec[f].bv_offset = offs;
655 } else {
656 BUG_ON(bvec[f].bv_offset != offs);
658 offs += CD_FRAMESIZE;
659 if (offs >= PAGE_SIZE) {
660 offs = 0;
661 p++;
666 static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
668 struct packet_data *pkt = bio->bi_private;
669 struct pktcdvd_device *pd = pkt->pd;
670 BUG_ON(!pd);
672 if (bio->bi_size)
673 return 1;
675 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
676 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
678 if (err)
679 atomic_inc(&pkt->io_errors);
680 if (atomic_dec_and_test(&pkt->io_wait)) {
681 atomic_inc(&pkt->run_sm);
682 wake_up(&pd->wqueue);
684 pkt_bio_finished(pd);
686 return 0;
689 static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
691 struct packet_data *pkt = bio->bi_private;
692 struct pktcdvd_device *pd = pkt->pd;
693 BUG_ON(!pd);
695 if (bio->bi_size)
696 return 1;
698 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
700 pd->stats.pkt_ended++;
702 pkt_bio_finished(pd);
703 atomic_dec(&pkt->io_wait);
704 atomic_inc(&pkt->run_sm);
705 wake_up(&pd->wqueue);
706 return 0;
710 * Schedule reads for the holes in a packet
712 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
714 int frames_read = 0;
715 struct bio *bio;
716 int f;
717 char written[PACKET_MAX_SIZE];
719 BUG_ON(!pkt->orig_bios);
721 atomic_set(&pkt->io_wait, 0);
722 atomic_set(&pkt->io_errors, 0);
725 * Figure out which frames we need to read before we can write.
727 memset(written, 0, sizeof(written));
728 spin_lock(&pkt->lock);
729 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
730 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
731 int num_frames = bio->bi_size / CD_FRAMESIZE;
732 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
733 BUG_ON(first_frame < 0);
734 BUG_ON(first_frame + num_frames > pkt->frames);
735 for (f = first_frame; f < first_frame + num_frames; f++)
736 written[f] = 1;
738 spin_unlock(&pkt->lock);
740 if (pkt->cache_valid) {
741 VPRINTK("pkt_gather_data: zone %llx cached\n",
742 (unsigned long long)pkt->sector);
743 goto out_account;
747 * Schedule reads for missing parts of the packet.
749 for (f = 0; f < pkt->frames; f++) {
750 int p, offset;
751 if (written[f])
752 continue;
753 bio = pkt->r_bios[f];
754 bio_init(bio);
755 bio->bi_max_vecs = 1;
756 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
757 bio->bi_bdev = pd->bdev;
758 bio->bi_end_io = pkt_end_io_read;
759 bio->bi_private = pkt;
761 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
762 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
763 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
764 f, pkt->pages[p], offset);
765 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
766 BUG();
768 atomic_inc(&pkt->io_wait);
769 bio->bi_rw = READ;
770 pkt_queue_bio(pd, bio);
771 frames_read++;
774 out_account:
775 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
776 frames_read, (unsigned long long)pkt->sector);
777 pd->stats.pkt_started++;
778 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
782 * Find a packet matching zone, or the least recently used packet if
783 * there is no match.
785 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
787 struct packet_data *pkt;
789 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
790 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
791 list_del_init(&pkt->list);
792 if (pkt->sector != zone)
793 pkt->cache_valid = 0;
794 return pkt;
797 BUG();
798 return NULL;
801 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
803 if (pkt->cache_valid) {
804 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
805 } else {
806 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
811 * recover a failed write, query for relocation if possible
813 * returns 1 if recovery is possible, or 0 if not
816 static int pkt_start_recovery(struct packet_data *pkt)
819 * FIXME. We need help from the file system to implement
820 * recovery handling.
822 return 0;
823 #if 0
824 struct request *rq = pkt->rq;
825 struct pktcdvd_device *pd = rq->rq_disk->private_data;
826 struct block_device *pkt_bdev;
827 struct super_block *sb = NULL;
828 unsigned long old_block, new_block;
829 sector_t new_sector;
831 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
832 if (pkt_bdev) {
833 sb = get_super(pkt_bdev);
834 bdput(pkt_bdev);
837 if (!sb)
838 return 0;
840 if (!sb->s_op || !sb->s_op->relocate_blocks)
841 goto out;
843 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
844 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
845 goto out;
847 new_sector = new_block * (CD_FRAMESIZE >> 9);
848 pkt->sector = new_sector;
850 pkt->bio->bi_sector = new_sector;
851 pkt->bio->bi_next = NULL;
852 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
853 pkt->bio->bi_idx = 0;
855 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
856 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
857 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
858 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
859 BUG_ON(pkt->bio->bi_private != pkt);
861 drop_super(sb);
862 return 1;
864 out:
865 drop_super(sb);
866 return 0;
867 #endif
870 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
872 #if PACKET_DEBUG > 1
873 static const char *state_name[] = {
874 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
876 enum packet_data_state old_state = pkt->state;
877 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
878 state_name[old_state], state_name[state]);
879 #endif
880 pkt->state = state;
884 * Scan the work queue to see if we can start a new packet.
885 * returns non-zero if any work was done.
887 static int pkt_handle_queue(struct pktcdvd_device *pd)
889 struct packet_data *pkt, *p;
890 struct bio *bio = NULL;
891 sector_t zone = 0; /* Suppress gcc warning */
892 struct pkt_rb_node *node, *first_node;
893 struct rb_node *n;
895 VPRINTK("handle_queue\n");
897 atomic_set(&pd->scan_queue, 0);
899 if (list_empty(&pd->cdrw.pkt_free_list)) {
900 VPRINTK("handle_queue: no pkt\n");
901 return 0;
905 * Try to find a zone we are not already working on.
907 spin_lock(&pd->lock);
908 first_node = pkt_rbtree_find(pd, pd->current_sector);
909 if (!first_node) {
910 n = rb_first(&pd->bio_queue);
911 if (n)
912 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
914 node = first_node;
915 while (node) {
916 bio = node->bio;
917 zone = ZONE(bio->bi_sector, pd);
918 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
919 if (p->sector == zone) {
920 bio = NULL;
921 goto try_next_bio;
924 break;
925 try_next_bio:
926 node = pkt_rbtree_next(node);
927 if (!node) {
928 n = rb_first(&pd->bio_queue);
929 if (n)
930 node = rb_entry(n, struct pkt_rb_node, rb_node);
932 if (node == first_node)
933 node = NULL;
935 spin_unlock(&pd->lock);
936 if (!bio) {
937 VPRINTK("handle_queue: no bio\n");
938 return 0;
941 pkt = pkt_get_packet_data(pd, zone);
943 pd->current_sector = zone + pd->settings.size;
944 pkt->sector = zone;
945 BUG_ON(pkt->frames != pd->settings.size >> 2);
946 pkt->write_size = 0;
949 * Scan work queue for bios in the same zone and link them
950 * to this packet.
952 spin_lock(&pd->lock);
953 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
954 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
955 bio = node->bio;
956 VPRINTK("pkt_handle_queue: found zone=%llx\n",
957 (unsigned long long)ZONE(bio->bi_sector, pd));
958 if (ZONE(bio->bi_sector, pd) != zone)
959 break;
960 pkt_rbtree_erase(pd, node);
961 spin_lock(&pkt->lock);
962 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
963 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
964 spin_unlock(&pkt->lock);
966 spin_unlock(&pd->lock);
968 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
969 pkt_set_state(pkt, PACKET_WAITING_STATE);
970 atomic_set(&pkt->run_sm, 1);
972 spin_lock(&pd->cdrw.active_list_lock);
973 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
974 spin_unlock(&pd->cdrw.active_list_lock);
976 return 1;
980 * Assemble a bio to write one packet and queue the bio for processing
981 * by the underlying block device.
983 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
985 struct bio *bio;
986 int f;
987 int frames_write;
988 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
990 for (f = 0; f < pkt->frames; f++) {
991 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
992 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
996 * Fill-in bvec with data from orig_bios.
998 frames_write = 0;
999 spin_lock(&pkt->lock);
1000 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1001 int segment = bio->bi_idx;
1002 int src_offs = 0;
1003 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1004 int num_frames = bio->bi_size / CD_FRAMESIZE;
1005 BUG_ON(first_frame < 0);
1006 BUG_ON(first_frame + num_frames > pkt->frames);
1007 for (f = first_frame; f < first_frame + num_frames; f++) {
1008 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1010 while (src_offs >= src_bvl->bv_len) {
1011 src_offs -= src_bvl->bv_len;
1012 segment++;
1013 BUG_ON(segment >= bio->bi_vcnt);
1014 src_bvl = bio_iovec_idx(bio, segment);
1017 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1018 bvec[f].bv_page = src_bvl->bv_page;
1019 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1020 } else {
1021 pkt_copy_bio_data(bio, segment, src_offs,
1022 bvec[f].bv_page, bvec[f].bv_offset);
1024 src_offs += CD_FRAMESIZE;
1025 frames_write++;
1028 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1029 spin_unlock(&pkt->lock);
1031 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1032 frames_write, (unsigned long long)pkt->sector);
1033 BUG_ON(frames_write != pkt->write_size);
1035 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1036 pkt_make_local_copy(pkt, bvec);
1037 pkt->cache_valid = 1;
1038 } else {
1039 pkt->cache_valid = 0;
1042 /* Start the write request */
1043 bio_init(pkt->w_bio);
1044 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1045 pkt->w_bio->bi_sector = pkt->sector;
1046 pkt->w_bio->bi_bdev = pd->bdev;
1047 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1048 pkt->w_bio->bi_private = pkt;
1049 for (f = 0; f < pkt->frames; f++)
1050 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1051 BUG();
1052 VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
1054 atomic_set(&pkt->io_wait, 1);
1055 pkt->w_bio->bi_rw = WRITE;
1056 pkt_queue_bio(pd, pkt->w_bio);
1059 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1061 struct bio *bio, *next;
1063 if (!uptodate)
1064 pkt->cache_valid = 0;
1066 /* Finish all bios corresponding to this packet */
1067 bio = pkt->orig_bios;
1068 while (bio) {
1069 next = bio->bi_next;
1070 bio->bi_next = NULL;
1071 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1072 bio = next;
1074 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1077 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1079 int uptodate;
1081 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1083 for (;;) {
1084 switch (pkt->state) {
1085 case PACKET_WAITING_STATE:
1086 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1087 return;
1089 pkt->sleep_time = 0;
1090 pkt_gather_data(pd, pkt);
1091 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1092 break;
1094 case PACKET_READ_WAIT_STATE:
1095 if (atomic_read(&pkt->io_wait) > 0)
1096 return;
1098 if (atomic_read(&pkt->io_errors) > 0) {
1099 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1100 } else {
1101 pkt_start_write(pd, pkt);
1103 break;
1105 case PACKET_WRITE_WAIT_STATE:
1106 if (atomic_read(&pkt->io_wait) > 0)
1107 return;
1109 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1110 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1111 } else {
1112 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1114 break;
1116 case PACKET_RECOVERY_STATE:
1117 if (pkt_start_recovery(pkt)) {
1118 pkt_start_write(pd, pkt);
1119 } else {
1120 VPRINTK("No recovery possible\n");
1121 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1123 break;
1125 case PACKET_FINISHED_STATE:
1126 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1127 pkt_finish_packet(pkt, uptodate);
1128 return;
1130 default:
1131 BUG();
1132 break;
1137 static void pkt_handle_packets(struct pktcdvd_device *pd)
1139 struct packet_data *pkt, *next;
1141 VPRINTK("pkt_handle_packets\n");
1144 * Run state machine for active packets
1146 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1147 if (atomic_read(&pkt->run_sm) > 0) {
1148 atomic_set(&pkt->run_sm, 0);
1149 pkt_run_state_machine(pd, pkt);
1154 * Move no longer active packets to the free list
1156 spin_lock(&pd->cdrw.active_list_lock);
1157 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1158 if (pkt->state == PACKET_FINISHED_STATE) {
1159 list_del(&pkt->list);
1160 pkt_put_packet_data(pd, pkt);
1161 pkt_set_state(pkt, PACKET_IDLE_STATE);
1162 atomic_set(&pd->scan_queue, 1);
1165 spin_unlock(&pd->cdrw.active_list_lock);
1168 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1170 struct packet_data *pkt;
1171 int i;
1173 for (i = 0; i < PACKET_NUM_STATES; i++)
1174 states[i] = 0;
1176 spin_lock(&pd->cdrw.active_list_lock);
1177 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1178 states[pkt->state]++;
1180 spin_unlock(&pd->cdrw.active_list_lock);
1184 * kcdrwd is woken up when writes have been queued for one of our
1185 * registered devices
1187 static int kcdrwd(void *foobar)
1189 struct pktcdvd_device *pd = foobar;
1190 struct packet_data *pkt;
1191 long min_sleep_time, residue;
1193 set_user_nice(current, -20);
1195 for (;;) {
1196 DECLARE_WAITQUEUE(wait, current);
1199 * Wait until there is something to do
1201 add_wait_queue(&pd->wqueue, &wait);
1202 for (;;) {
1203 set_current_state(TASK_INTERRUPTIBLE);
1205 /* Check if we need to run pkt_handle_queue */
1206 if (atomic_read(&pd->scan_queue) > 0)
1207 goto work_to_do;
1209 /* Check if we need to run the state machine for some packet */
1210 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1211 if (atomic_read(&pkt->run_sm) > 0)
1212 goto work_to_do;
1215 /* Check if we need to process the iosched queues */
1216 if (atomic_read(&pd->iosched.attention) != 0)
1217 goto work_to_do;
1219 /* Otherwise, go to sleep */
1220 if (PACKET_DEBUG > 1) {
1221 int states[PACKET_NUM_STATES];
1222 pkt_count_states(pd, states);
1223 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1224 states[0], states[1], states[2], states[3],
1225 states[4], states[5]);
1228 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1229 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1230 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1231 min_sleep_time = pkt->sleep_time;
1234 generic_unplug_device(bdev_get_queue(pd->bdev));
1236 VPRINTK("kcdrwd: sleeping\n");
1237 residue = schedule_timeout(min_sleep_time);
1238 VPRINTK("kcdrwd: wake up\n");
1240 /* make swsusp happy with our thread */
1241 try_to_freeze();
1243 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1244 if (!pkt->sleep_time)
1245 continue;
1246 pkt->sleep_time -= min_sleep_time - residue;
1247 if (pkt->sleep_time <= 0) {
1248 pkt->sleep_time = 0;
1249 atomic_inc(&pkt->run_sm);
1253 if (signal_pending(current)) {
1254 flush_signals(current);
1256 if (kthread_should_stop())
1257 break;
1259 work_to_do:
1260 set_current_state(TASK_RUNNING);
1261 remove_wait_queue(&pd->wqueue, &wait);
1263 if (kthread_should_stop())
1264 break;
1267 * if pkt_handle_queue returns true, we can queue
1268 * another request.
1270 while (pkt_handle_queue(pd))
1274 * Handle packet state machine
1276 pkt_handle_packets(pd);
1279 * Handle iosched queues
1281 pkt_iosched_process_queue(pd);
1284 return 0;
1287 static void pkt_print_settings(struct pktcdvd_device *pd)
1289 printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1290 printk("%u blocks, ", pd->settings.size >> 2);
1291 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1294 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1296 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1298 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1299 cgc->cmd[2] = page_code | (page_control << 6);
1300 cgc->cmd[7] = cgc->buflen >> 8;
1301 cgc->cmd[8] = cgc->buflen & 0xff;
1302 cgc->data_direction = CGC_DATA_READ;
1303 return pkt_generic_packet(pd, cgc);
1306 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1308 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1309 memset(cgc->buffer, 0, 2);
1310 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1311 cgc->cmd[1] = 0x10; /* PF */
1312 cgc->cmd[7] = cgc->buflen >> 8;
1313 cgc->cmd[8] = cgc->buflen & 0xff;
1314 cgc->data_direction = CGC_DATA_WRITE;
1315 return pkt_generic_packet(pd, cgc);
1318 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1320 struct packet_command cgc;
1321 int ret;
1323 /* set up command and get the disc info */
1324 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1325 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1326 cgc.cmd[8] = cgc.buflen = 2;
1327 cgc.quiet = 1;
1329 if ((ret = pkt_generic_packet(pd, &cgc)))
1330 return ret;
1332 /* not all drives have the same disc_info length, so requeue
1333 * packet with the length the drive tells us it can supply
1335 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1336 sizeof(di->disc_information_length);
1338 if (cgc.buflen > sizeof(disc_information))
1339 cgc.buflen = sizeof(disc_information);
1341 cgc.cmd[8] = cgc.buflen;
1342 return pkt_generic_packet(pd, &cgc);
1345 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1347 struct packet_command cgc;
1348 int ret;
1350 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1351 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1352 cgc.cmd[1] = type & 3;
1353 cgc.cmd[4] = (track & 0xff00) >> 8;
1354 cgc.cmd[5] = track & 0xff;
1355 cgc.cmd[8] = 8;
1356 cgc.quiet = 1;
1358 if ((ret = pkt_generic_packet(pd, &cgc)))
1359 return ret;
1361 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1362 sizeof(ti->track_information_length);
1364 if (cgc.buflen > sizeof(track_information))
1365 cgc.buflen = sizeof(track_information);
1367 cgc.cmd[8] = cgc.buflen;
1368 return pkt_generic_packet(pd, &cgc);
1371 static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1373 disc_information di;
1374 track_information ti;
1375 __u32 last_track;
1376 int ret = -1;
1378 if ((ret = pkt_get_disc_info(pd, &di)))
1379 return ret;
1381 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1382 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1383 return ret;
1385 /* if this track is blank, try the previous. */
1386 if (ti.blank) {
1387 last_track--;
1388 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1389 return ret;
1392 /* if last recorded field is valid, return it. */
1393 if (ti.lra_v) {
1394 *last_written = be32_to_cpu(ti.last_rec_address);
1395 } else {
1396 /* make it up instead */
1397 *last_written = be32_to_cpu(ti.track_start) +
1398 be32_to_cpu(ti.track_size);
1399 if (ti.free_blocks)
1400 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1402 return 0;
1406 * write mode select package based on pd->settings
1408 static int pkt_set_write_settings(struct pktcdvd_device *pd)
1410 struct packet_command cgc;
1411 struct request_sense sense;
1412 write_param_page *wp;
1413 char buffer[128];
1414 int ret, size;
1416 /* doesn't apply to DVD+RW or DVD-RAM */
1417 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1418 return 0;
1420 memset(buffer, 0, sizeof(buffer));
1421 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1422 cgc.sense = &sense;
1423 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1424 pkt_dump_sense(&cgc);
1425 return ret;
1428 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1429 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1430 if (size > sizeof(buffer))
1431 size = sizeof(buffer);
1434 * now get it all
1436 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1437 cgc.sense = &sense;
1438 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1439 pkt_dump_sense(&cgc);
1440 return ret;
1444 * write page is offset header + block descriptor length
1446 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1448 wp->fp = pd->settings.fp;
1449 wp->track_mode = pd->settings.track_mode;
1450 wp->write_type = pd->settings.write_type;
1451 wp->data_block_type = pd->settings.block_mode;
1453 wp->multi_session = 0;
1455 #ifdef PACKET_USE_LS
1456 wp->link_size = 7;
1457 wp->ls_v = 1;
1458 #endif
1460 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1461 wp->session_format = 0;
1462 wp->subhdr2 = 0x20;
1463 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1464 wp->session_format = 0x20;
1465 wp->subhdr2 = 8;
1466 #if 0
1467 wp->mcn[0] = 0x80;
1468 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1469 #endif
1470 } else {
1472 * paranoia
1474 printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
1475 return 1;
1477 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1479 cgc.buflen = cgc.cmd[8] = size;
1480 if ((ret = pkt_mode_select(pd, &cgc))) {
1481 pkt_dump_sense(&cgc);
1482 return ret;
1485 pkt_print_settings(pd);
1486 return 0;
1490 * 1 -- we can write to this track, 0 -- we can't
1492 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1494 switch (pd->mmc3_profile) {
1495 case 0x1a: /* DVD+RW */
1496 case 0x12: /* DVD-RAM */
1497 /* The track is always writable on DVD+RW/DVD-RAM */
1498 return 1;
1499 default:
1500 break;
1503 if (!ti->packet || !ti->fp)
1504 return 0;
1507 * "good" settings as per Mt Fuji.
1509 if (ti->rt == 0 && ti->blank == 0)
1510 return 1;
1512 if (ti->rt == 0 && ti->blank == 1)
1513 return 1;
1515 if (ti->rt == 1 && ti->blank == 0)
1516 return 1;
1518 printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1519 return 0;
1523 * 1 -- we can write to this disc, 0 -- we can't
1525 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1527 switch (pd->mmc3_profile) {
1528 case 0x0a: /* CD-RW */
1529 case 0xffff: /* MMC3 not supported */
1530 break;
1531 case 0x1a: /* DVD+RW */
1532 case 0x13: /* DVD-RW */
1533 case 0x12: /* DVD-RAM */
1534 return 1;
1535 default:
1536 VPRINTK("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
1537 return 0;
1541 * for disc type 0xff we should probably reserve a new track.
1542 * but i'm not sure, should we leave this to user apps? probably.
1544 if (di->disc_type == 0xff) {
1545 printk("pktcdvd: Unknown disc. No track?\n");
1546 return 0;
1549 if (di->disc_type != 0x20 && di->disc_type != 0) {
1550 printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
1551 return 0;
1554 if (di->erasable == 0) {
1555 printk("pktcdvd: Disc not erasable\n");
1556 return 0;
1559 if (di->border_status == PACKET_SESSION_RESERVED) {
1560 printk("pktcdvd: Can't write to last track (reserved)\n");
1561 return 0;
1564 return 1;
1567 static int pkt_probe_settings(struct pktcdvd_device *pd)
1569 struct packet_command cgc;
1570 unsigned char buf[12];
1571 disc_information di;
1572 track_information ti;
1573 int ret, track;
1575 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1576 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1577 cgc.cmd[8] = 8;
1578 ret = pkt_generic_packet(pd, &cgc);
1579 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1581 memset(&di, 0, sizeof(disc_information));
1582 memset(&ti, 0, sizeof(track_information));
1584 if ((ret = pkt_get_disc_info(pd, &di))) {
1585 printk("failed get_disc\n");
1586 return ret;
1589 if (!pkt_writable_disc(pd, &di))
1590 return -EROFS;
1592 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1594 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1595 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1596 printk("pktcdvd: failed get_track\n");
1597 return ret;
1600 if (!pkt_writable_track(pd, &ti)) {
1601 printk("pktcdvd: can't write to this track\n");
1602 return -EROFS;
1606 * we keep packet size in 512 byte units, makes it easier to
1607 * deal with request calculations.
1609 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1610 if (pd->settings.size == 0) {
1611 printk("pktcdvd: detected zero packet size!\n");
1612 return -ENXIO;
1614 if (pd->settings.size > PACKET_MAX_SECTORS) {
1615 printk("pktcdvd: packet size is too big\n");
1616 return -EROFS;
1618 pd->settings.fp = ti.fp;
1619 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1621 if (ti.nwa_v) {
1622 pd->nwa = be32_to_cpu(ti.next_writable);
1623 set_bit(PACKET_NWA_VALID, &pd->flags);
1627 * in theory we could use lra on -RW media as well and just zero
1628 * blocks that haven't been written yet, but in practice that
1629 * is just a no-go. we'll use that for -R, naturally.
1631 if (ti.lra_v) {
1632 pd->lra = be32_to_cpu(ti.last_rec_address);
1633 set_bit(PACKET_LRA_VALID, &pd->flags);
1634 } else {
1635 pd->lra = 0xffffffff;
1636 set_bit(PACKET_LRA_VALID, &pd->flags);
1640 * fine for now
1642 pd->settings.link_loss = 7;
1643 pd->settings.write_type = 0; /* packet */
1644 pd->settings.track_mode = ti.track_mode;
1647 * mode1 or mode2 disc
1649 switch (ti.data_mode) {
1650 case PACKET_MODE1:
1651 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1652 break;
1653 case PACKET_MODE2:
1654 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1655 break;
1656 default:
1657 printk("pktcdvd: unknown data mode\n");
1658 return -EROFS;
1660 return 0;
1664 * enable/disable write caching on drive
1666 static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1668 struct packet_command cgc;
1669 struct request_sense sense;
1670 unsigned char buf[64];
1671 int ret;
1673 memset(buf, 0, sizeof(buf));
1674 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1675 cgc.sense = &sense;
1676 cgc.buflen = pd->mode_offset + 12;
1679 * caching mode page might not be there, so quiet this command
1681 cgc.quiet = 1;
1683 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1684 return ret;
1686 buf[pd->mode_offset + 10] |= (!!set << 2);
1688 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1689 ret = pkt_mode_select(pd, &cgc);
1690 if (ret) {
1691 printk("pktcdvd: write caching control failed\n");
1692 pkt_dump_sense(&cgc);
1693 } else if (!ret && set)
1694 printk("pktcdvd: enabled write caching on %s\n", pd->name);
1695 return ret;
1698 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1700 struct packet_command cgc;
1702 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1703 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1704 cgc.cmd[4] = lockflag ? 1 : 0;
1705 return pkt_generic_packet(pd, &cgc);
1709 * Returns drive maximum write speed
1711 static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1713 struct packet_command cgc;
1714 struct request_sense sense;
1715 unsigned char buf[256+18];
1716 unsigned char *cap_buf;
1717 int ret, offset;
1719 memset(buf, 0, sizeof(buf));
1720 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1721 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1722 cgc.sense = &sense;
1724 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1725 if (ret) {
1726 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1727 sizeof(struct mode_page_header);
1728 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1729 if (ret) {
1730 pkt_dump_sense(&cgc);
1731 return ret;
1735 offset = 20; /* Obsoleted field, used by older drives */
1736 if (cap_buf[1] >= 28)
1737 offset = 28; /* Current write speed selected */
1738 if (cap_buf[1] >= 30) {
1739 /* If the drive reports at least one "Logical Unit Write
1740 * Speed Performance Descriptor Block", use the information
1741 * in the first block. (contains the highest speed)
1743 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1744 if (num_spdb > 0)
1745 offset = 34;
1748 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1749 return 0;
1752 /* These tables from cdrecord - I don't have orange book */
1753 /* standard speed CD-RW (1-4x) */
1754 static char clv_to_speed[16] = {
1755 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1756 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1758 /* high speed CD-RW (-10x) */
1759 static char hs_clv_to_speed[16] = {
1760 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1761 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1763 /* ultra high speed CD-RW */
1764 static char us_clv_to_speed[16] = {
1765 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1766 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1770 * reads the maximum media speed from ATIP
1772 static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1774 struct packet_command cgc;
1775 struct request_sense sense;
1776 unsigned char buf[64];
1777 unsigned int size, st, sp;
1778 int ret;
1780 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1781 cgc.sense = &sense;
1782 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1783 cgc.cmd[1] = 2;
1784 cgc.cmd[2] = 4; /* READ ATIP */
1785 cgc.cmd[8] = 2;
1786 ret = pkt_generic_packet(pd, &cgc);
1787 if (ret) {
1788 pkt_dump_sense(&cgc);
1789 return ret;
1791 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1792 if (size > sizeof(buf))
1793 size = sizeof(buf);
1795 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1796 cgc.sense = &sense;
1797 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1798 cgc.cmd[1] = 2;
1799 cgc.cmd[2] = 4;
1800 cgc.cmd[8] = size;
1801 ret = pkt_generic_packet(pd, &cgc);
1802 if (ret) {
1803 pkt_dump_sense(&cgc);
1804 return ret;
1807 if (!buf[6] & 0x40) {
1808 printk("pktcdvd: Disc type is not CD-RW\n");
1809 return 1;
1811 if (!buf[6] & 0x4) {
1812 printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
1813 return 1;
1816 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1818 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1820 /* Info from cdrecord */
1821 switch (st) {
1822 case 0: /* standard speed */
1823 *speed = clv_to_speed[sp];
1824 break;
1825 case 1: /* high speed */
1826 *speed = hs_clv_to_speed[sp];
1827 break;
1828 case 2: /* ultra high speed */
1829 *speed = us_clv_to_speed[sp];
1830 break;
1831 default:
1832 printk("pktcdvd: Unknown disc sub-type %d\n",st);
1833 return 1;
1835 if (*speed) {
1836 printk("pktcdvd: Max. media speed: %d\n",*speed);
1837 return 0;
1838 } else {
1839 printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
1840 return 1;
1844 static int pkt_perform_opc(struct pktcdvd_device *pd)
1846 struct packet_command cgc;
1847 struct request_sense sense;
1848 int ret;
1850 VPRINTK("pktcdvd: Performing OPC\n");
1852 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1853 cgc.sense = &sense;
1854 cgc.timeout = 60*HZ;
1855 cgc.cmd[0] = GPCMD_SEND_OPC;
1856 cgc.cmd[1] = 1;
1857 if ((ret = pkt_generic_packet(pd, &cgc)))
1858 pkt_dump_sense(&cgc);
1859 return ret;
1862 static int pkt_open_write(struct pktcdvd_device *pd)
1864 int ret;
1865 unsigned int write_speed, media_write_speed, read_speed;
1867 if ((ret = pkt_probe_settings(pd))) {
1868 VPRINTK("pktcdvd: %s failed probe\n", pd->name);
1869 return ret;
1872 if ((ret = pkt_set_write_settings(pd))) {
1873 DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
1874 return -EIO;
1877 pkt_write_caching(pd, USE_WCACHING);
1879 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1880 write_speed = 16 * 177;
1881 switch (pd->mmc3_profile) {
1882 case 0x13: /* DVD-RW */
1883 case 0x1a: /* DVD+RW */
1884 case 0x12: /* DVD-RAM */
1885 DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
1886 break;
1887 default:
1888 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1889 media_write_speed = 16;
1890 write_speed = min(write_speed, media_write_speed * 177);
1891 DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
1892 break;
1894 read_speed = write_speed;
1896 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1897 DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
1898 return -EIO;
1900 pd->write_speed = write_speed;
1901 pd->read_speed = read_speed;
1903 if ((ret = pkt_perform_opc(pd))) {
1904 DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
1907 return 0;
1911 * called at open time.
1913 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1915 int ret;
1916 long lba;
1917 request_queue_t *q;
1920 * We need to re-open the cdrom device without O_NONBLOCK to be able
1921 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1922 * so bdget() can't fail.
1924 bdget(pd->bdev->bd_dev);
1925 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1926 goto out;
1928 if ((ret = bd_claim(pd->bdev, pd)))
1929 goto out_putdev;
1931 if ((ret = pkt_get_last_written(pd, &lba))) {
1932 printk("pktcdvd: pkt_get_last_written failed\n");
1933 goto out_unclaim;
1936 set_capacity(pd->disk, lba << 2);
1937 set_capacity(pd->bdev->bd_disk, lba << 2);
1938 bd_set_size(pd->bdev, (loff_t)lba << 11);
1940 q = bdev_get_queue(pd->bdev);
1941 if (write) {
1942 if ((ret = pkt_open_write(pd)))
1943 goto out_unclaim;
1945 * Some CDRW drives can not handle writes larger than one packet,
1946 * even if the size is a multiple of the packet size.
1948 spin_lock_irq(q->queue_lock);
1949 blk_queue_max_sectors(q, pd->settings.size);
1950 spin_unlock_irq(q->queue_lock);
1951 set_bit(PACKET_WRITABLE, &pd->flags);
1952 } else {
1953 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1954 clear_bit(PACKET_WRITABLE, &pd->flags);
1957 if ((ret = pkt_set_segment_merging(pd, q)))
1958 goto out_unclaim;
1960 if (write) {
1961 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
1962 printk("pktcdvd: not enough memory for buffers\n");
1963 ret = -ENOMEM;
1964 goto out_unclaim;
1966 printk("pktcdvd: %lukB available on disc\n", lba << 1);
1969 return 0;
1971 out_unclaim:
1972 bd_release(pd->bdev);
1973 out_putdev:
1974 blkdev_put(pd->bdev);
1975 out:
1976 return ret;
1980 * called when the device is closed. makes sure that the device flushes
1981 * the internal cache before we close.
1983 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
1985 if (flush && pkt_flush_cache(pd))
1986 DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
1988 pkt_lock_door(pd, 0);
1990 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1991 bd_release(pd->bdev);
1992 blkdev_put(pd->bdev);
1994 pkt_shrink_pktlist(pd);
1997 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
1999 if (dev_minor >= MAX_WRITERS)
2000 return NULL;
2001 return pkt_devs[dev_minor];
2004 static int pkt_open(struct inode *inode, struct file *file)
2006 struct pktcdvd_device *pd = NULL;
2007 int ret;
2009 VPRINTK("pktcdvd: entering open\n");
2011 mutex_lock(&ctl_mutex);
2012 pd = pkt_find_dev_from_minor(iminor(inode));
2013 if (!pd) {
2014 ret = -ENODEV;
2015 goto out;
2017 BUG_ON(pd->refcnt < 0);
2019 pd->refcnt++;
2020 if (pd->refcnt > 1) {
2021 if ((file->f_mode & FMODE_WRITE) &&
2022 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2023 ret = -EBUSY;
2024 goto out_dec;
2026 } else {
2027 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2028 if (ret)
2029 goto out_dec;
2031 * needed here as well, since ext2 (among others) may change
2032 * the blocksize at mount time
2034 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2037 mutex_unlock(&ctl_mutex);
2038 return 0;
2040 out_dec:
2041 pd->refcnt--;
2042 out:
2043 VPRINTK("pktcdvd: failed open (%d)\n", ret);
2044 mutex_unlock(&ctl_mutex);
2045 return ret;
2048 static int pkt_close(struct inode *inode, struct file *file)
2050 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2051 int ret = 0;
2053 mutex_lock(&ctl_mutex);
2054 pd->refcnt--;
2055 BUG_ON(pd->refcnt < 0);
2056 if (pd->refcnt == 0) {
2057 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2058 pkt_release_dev(pd, flush);
2060 mutex_unlock(&ctl_mutex);
2061 return ret;
2065 static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2067 struct packet_stacked_data *psd = bio->bi_private;
2068 struct pktcdvd_device *pd = psd->pd;
2070 if (bio->bi_size)
2071 return 1;
2073 bio_put(bio);
2074 bio_endio(psd->bio, psd->bio->bi_size, err);
2075 mempool_free(psd, psd_pool);
2076 pkt_bio_finished(pd);
2077 return 0;
2080 static int pkt_make_request(request_queue_t *q, struct bio *bio)
2082 struct pktcdvd_device *pd;
2083 char b[BDEVNAME_SIZE];
2084 sector_t zone;
2085 struct packet_data *pkt;
2086 int was_empty, blocked_bio;
2087 struct pkt_rb_node *node;
2089 pd = q->queuedata;
2090 if (!pd) {
2091 printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2092 goto end_io;
2096 * Clone READ bios so we can have our own bi_end_io callback.
2098 if (bio_data_dir(bio) == READ) {
2099 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2100 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2102 psd->pd = pd;
2103 psd->bio = bio;
2104 cloned_bio->bi_bdev = pd->bdev;
2105 cloned_bio->bi_private = psd;
2106 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2107 pd->stats.secs_r += bio->bi_size >> 9;
2108 pkt_queue_bio(pd, cloned_bio);
2109 return 0;
2112 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2113 printk("pktcdvd: WRITE for ro device %s (%llu)\n",
2114 pd->name, (unsigned long long)bio->bi_sector);
2115 goto end_io;
2118 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2119 printk("pktcdvd: wrong bio size\n");
2120 goto end_io;
2123 blk_queue_bounce(q, &bio);
2125 zone = ZONE(bio->bi_sector, pd);
2126 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2127 (unsigned long long)bio->bi_sector,
2128 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2130 /* Check if we have to split the bio */
2132 struct bio_pair *bp;
2133 sector_t last_zone;
2134 int first_sectors;
2136 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2137 if (last_zone != zone) {
2138 BUG_ON(last_zone != zone + pd->settings.size);
2139 first_sectors = last_zone - bio->bi_sector;
2140 bp = bio_split(bio, bio_split_pool, first_sectors);
2141 BUG_ON(!bp);
2142 pkt_make_request(q, &bp->bio1);
2143 pkt_make_request(q, &bp->bio2);
2144 bio_pair_release(bp);
2145 return 0;
2150 * If we find a matching packet in state WAITING or READ_WAIT, we can
2151 * just append this bio to that packet.
2153 spin_lock(&pd->cdrw.active_list_lock);
2154 blocked_bio = 0;
2155 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2156 if (pkt->sector == zone) {
2157 spin_lock(&pkt->lock);
2158 if ((pkt->state == PACKET_WAITING_STATE) ||
2159 (pkt->state == PACKET_READ_WAIT_STATE)) {
2160 pkt_add_list_last(bio, &pkt->orig_bios,
2161 &pkt->orig_bios_tail);
2162 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2163 if ((pkt->write_size >= pkt->frames) &&
2164 (pkt->state == PACKET_WAITING_STATE)) {
2165 atomic_inc(&pkt->run_sm);
2166 wake_up(&pd->wqueue);
2168 spin_unlock(&pkt->lock);
2169 spin_unlock(&pd->cdrw.active_list_lock);
2170 return 0;
2171 } else {
2172 blocked_bio = 1;
2174 spin_unlock(&pkt->lock);
2177 spin_unlock(&pd->cdrw.active_list_lock);
2180 * No matching packet found. Store the bio in the work queue.
2182 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2183 node->bio = bio;
2184 spin_lock(&pd->lock);
2185 BUG_ON(pd->bio_queue_size < 0);
2186 was_empty = (pd->bio_queue_size == 0);
2187 pkt_rbtree_insert(pd, node);
2188 spin_unlock(&pd->lock);
2191 * Wake up the worker thread.
2193 atomic_set(&pd->scan_queue, 1);
2194 if (was_empty) {
2195 /* This wake_up is required for correct operation */
2196 wake_up(&pd->wqueue);
2197 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2199 * This wake up is not required for correct operation,
2200 * but improves performance in some cases.
2202 wake_up(&pd->wqueue);
2204 return 0;
2205 end_io:
2206 bio_io_error(bio, bio->bi_size);
2207 return 0;
2212 static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2214 struct pktcdvd_device *pd = q->queuedata;
2215 sector_t zone = ZONE(bio->bi_sector, pd);
2216 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2217 int remaining = (pd->settings.size << 9) - used;
2218 int remaining2;
2221 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2222 * boundary, pkt_make_request() will split the bio.
2224 remaining2 = PAGE_SIZE - bio->bi_size;
2225 remaining = max(remaining, remaining2);
2227 BUG_ON(remaining < 0);
2228 return remaining;
2231 static void pkt_init_queue(struct pktcdvd_device *pd)
2233 request_queue_t *q = pd->disk->queue;
2235 blk_queue_make_request(q, pkt_make_request);
2236 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2237 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2238 blk_queue_merge_bvec(q, pkt_merge_bvec);
2239 q->queuedata = pd;
2242 static int pkt_seq_show(struct seq_file *m, void *p)
2244 struct pktcdvd_device *pd = m->private;
2245 char *msg;
2246 char bdev_buf[BDEVNAME_SIZE];
2247 int states[PACKET_NUM_STATES];
2249 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2250 bdevname(pd->bdev, bdev_buf));
2252 seq_printf(m, "\nSettings:\n");
2253 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2255 if (pd->settings.write_type == 0)
2256 msg = "Packet";
2257 else
2258 msg = "Unknown";
2259 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2261 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2262 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2264 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2266 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2267 msg = "Mode 1";
2268 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2269 msg = "Mode 2";
2270 else
2271 msg = "Unknown";
2272 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2274 seq_printf(m, "\nStatistics:\n");
2275 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2276 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2277 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2278 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2279 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2281 seq_printf(m, "\nMisc:\n");
2282 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2283 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2284 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2285 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2286 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2287 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2289 seq_printf(m, "\nQueue state:\n");
2290 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2291 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2292 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2294 pkt_count_states(pd, states);
2295 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2296 states[0], states[1], states[2], states[3], states[4], states[5]);
2298 return 0;
2301 static int pkt_seq_open(struct inode *inode, struct file *file)
2303 return single_open(file, pkt_seq_show, PDE(inode)->data);
2306 static struct file_operations pkt_proc_fops = {
2307 .open = pkt_seq_open,
2308 .read = seq_read,
2309 .llseek = seq_lseek,
2310 .release = single_release
2313 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2315 int i;
2316 int ret = 0;
2317 char b[BDEVNAME_SIZE];
2318 struct proc_dir_entry *proc;
2319 struct block_device *bdev;
2321 if (pd->pkt_dev == dev) {
2322 printk("pktcdvd: Recursive setup not allowed\n");
2323 return -EBUSY;
2325 for (i = 0; i < MAX_WRITERS; i++) {
2326 struct pktcdvd_device *pd2 = pkt_devs[i];
2327 if (!pd2)
2328 continue;
2329 if (pd2->bdev->bd_dev == dev) {
2330 printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
2331 return -EBUSY;
2333 if (pd2->pkt_dev == dev) {
2334 printk("pktcdvd: Can't chain pktcdvd devices\n");
2335 return -EBUSY;
2339 bdev = bdget(dev);
2340 if (!bdev)
2341 return -ENOMEM;
2342 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2343 if (ret)
2344 return ret;
2346 /* This is safe, since we have a reference from open(). */
2347 __module_get(THIS_MODULE);
2349 pd->bdev = bdev;
2350 set_blocksize(bdev, CD_FRAMESIZE);
2352 pkt_init_queue(pd);
2354 atomic_set(&pd->cdrw.pending_bios, 0);
2355 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2356 if (IS_ERR(pd->cdrw.thread)) {
2357 printk("pktcdvd: can't start kernel thread\n");
2358 ret = -ENOMEM;
2359 goto out_mem;
2362 proc = create_proc_entry(pd->name, 0, pkt_proc);
2363 if (proc) {
2364 proc->data = pd;
2365 proc->proc_fops = &pkt_proc_fops;
2367 DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2368 return 0;
2370 out_mem:
2371 blkdev_put(bdev);
2372 /* This is safe: open() is still holding a reference. */
2373 module_put(THIS_MODULE);
2374 return ret;
2377 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2379 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2381 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2383 switch (cmd) {
2385 * forward selected CDROM ioctls to CD-ROM, for UDF
2387 case CDROMMULTISESSION:
2388 case CDROMREADTOCENTRY:
2389 case CDROM_LAST_WRITTEN:
2390 case CDROM_SEND_PACKET:
2391 case SCSI_IOCTL_SEND_COMMAND:
2392 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2394 case CDROMEJECT:
2396 * The door gets locked when the device is opened, so we
2397 * have to unlock it or else the eject command fails.
2399 if (pd->refcnt == 1)
2400 pkt_lock_door(pd, 0);
2401 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2403 default:
2404 VPRINTK("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
2405 return -ENOTTY;
2408 return 0;
2411 static int pkt_media_changed(struct gendisk *disk)
2413 struct pktcdvd_device *pd = disk->private_data;
2414 struct gendisk *attached_disk;
2416 if (!pd)
2417 return 0;
2418 if (!pd->bdev)
2419 return 0;
2420 attached_disk = pd->bdev->bd_disk;
2421 if (!attached_disk)
2422 return 0;
2423 return attached_disk->fops->media_changed(attached_disk);
2426 static struct block_device_operations pktcdvd_ops = {
2427 .owner = THIS_MODULE,
2428 .open = pkt_open,
2429 .release = pkt_close,
2430 .ioctl = pkt_ioctl,
2431 .media_changed = pkt_media_changed,
2435 * Set up mapping from pktcdvd device to CD-ROM device.
2437 static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2439 int idx;
2440 int ret = -ENOMEM;
2441 struct pktcdvd_device *pd;
2442 struct gendisk *disk;
2443 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2445 for (idx = 0; idx < MAX_WRITERS; idx++)
2446 if (!pkt_devs[idx])
2447 break;
2448 if (idx == MAX_WRITERS) {
2449 printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
2450 return -EBUSY;
2453 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2454 if (!pd)
2455 return ret;
2457 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2458 sizeof(struct pkt_rb_node));
2459 if (!pd->rb_pool)
2460 goto out_mem;
2462 disk = alloc_disk(1);
2463 if (!disk)
2464 goto out_mem;
2465 pd->disk = disk;
2467 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2468 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2469 spin_lock_init(&pd->cdrw.active_list_lock);
2471 spin_lock_init(&pd->lock);
2472 spin_lock_init(&pd->iosched.lock);
2473 sprintf(pd->name, "pktcdvd%d", idx);
2474 init_waitqueue_head(&pd->wqueue);
2475 pd->bio_queue = RB_ROOT;
2477 disk->major = pkt_major;
2478 disk->first_minor = idx;
2479 disk->fops = &pktcdvd_ops;
2480 disk->flags = GENHD_FL_REMOVABLE;
2481 sprintf(disk->disk_name, "pktcdvd%d", idx);
2482 disk->private_data = pd;
2483 disk->queue = blk_alloc_queue(GFP_KERNEL);
2484 if (!disk->queue)
2485 goto out_mem2;
2487 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2488 ret = pkt_new_dev(pd, dev);
2489 if (ret)
2490 goto out_new_dev;
2492 add_disk(disk);
2493 pkt_devs[idx] = pd;
2494 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2495 return 0;
2497 out_new_dev:
2498 blk_cleanup_queue(disk->queue);
2499 out_mem2:
2500 put_disk(disk);
2501 out_mem:
2502 if (pd->rb_pool)
2503 mempool_destroy(pd->rb_pool);
2504 kfree(pd);
2505 return ret;
2509 * Tear down mapping from pktcdvd device to CD-ROM device.
2511 static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2513 struct pktcdvd_device *pd;
2514 int idx;
2515 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2517 for (idx = 0; idx < MAX_WRITERS; idx++) {
2518 pd = pkt_devs[idx];
2519 if (pd && (pd->pkt_dev == pkt_dev))
2520 break;
2522 if (idx == MAX_WRITERS) {
2523 DPRINTK("pktcdvd: dev not setup\n");
2524 return -ENXIO;
2527 if (pd->refcnt > 0)
2528 return -EBUSY;
2530 if (!IS_ERR(pd->cdrw.thread))
2531 kthread_stop(pd->cdrw.thread);
2533 blkdev_put(pd->bdev);
2535 remove_proc_entry(pd->name, pkt_proc);
2536 DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
2538 del_gendisk(pd->disk);
2539 blk_cleanup_queue(pd->disk->queue);
2540 put_disk(pd->disk);
2542 pkt_devs[idx] = NULL;
2543 mempool_destroy(pd->rb_pool);
2544 kfree(pd);
2546 /* This is safe: open() is still holding a reference. */
2547 module_put(THIS_MODULE);
2548 return 0;
2551 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2553 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2554 if (pd) {
2555 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2556 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2557 } else {
2558 ctrl_cmd->dev = 0;
2559 ctrl_cmd->pkt_dev = 0;
2561 ctrl_cmd->num_devices = MAX_WRITERS;
2564 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2566 void __user *argp = (void __user *)arg;
2567 struct pkt_ctrl_command ctrl_cmd;
2568 int ret = 0;
2570 if (cmd != PACKET_CTRL_CMD)
2571 return -ENOTTY;
2573 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2574 return -EFAULT;
2576 switch (ctrl_cmd.command) {
2577 case PKT_CTRL_CMD_SETUP:
2578 if (!capable(CAP_SYS_ADMIN))
2579 return -EPERM;
2580 mutex_lock(&ctl_mutex);
2581 ret = pkt_setup_dev(&ctrl_cmd);
2582 mutex_unlock(&ctl_mutex);
2583 break;
2584 case PKT_CTRL_CMD_TEARDOWN:
2585 if (!capable(CAP_SYS_ADMIN))
2586 return -EPERM;
2587 mutex_lock(&ctl_mutex);
2588 ret = pkt_remove_dev(&ctrl_cmd);
2589 mutex_unlock(&ctl_mutex);
2590 break;
2591 case PKT_CTRL_CMD_STATUS:
2592 mutex_lock(&ctl_mutex);
2593 pkt_get_status(&ctrl_cmd);
2594 mutex_unlock(&ctl_mutex);
2595 break;
2596 default:
2597 return -ENOTTY;
2600 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2601 return -EFAULT;
2602 return ret;
2606 static struct file_operations pkt_ctl_fops = {
2607 .ioctl = pkt_ctl_ioctl,
2608 .owner = THIS_MODULE,
2611 static struct miscdevice pkt_misc = {
2612 .minor = MISC_DYNAMIC_MINOR,
2613 .name = "pktcdvd",
2614 .fops = &pkt_ctl_fops
2617 static int __init pkt_init(void)
2619 int ret;
2621 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
2622 sizeof(struct packet_stacked_data));
2623 if (!psd_pool)
2624 return -ENOMEM;
2626 ret = register_blkdev(pkt_major, "pktcdvd");
2627 if (ret < 0) {
2628 printk("pktcdvd: Unable to register block device\n");
2629 goto out2;
2631 if (!pkt_major)
2632 pkt_major = ret;
2634 ret = misc_register(&pkt_misc);
2635 if (ret) {
2636 printk("pktcdvd: Unable to register misc device\n");
2637 goto out;
2640 mutex_init(&ctl_mutex);
2642 pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
2644 return 0;
2646 out:
2647 unregister_blkdev(pkt_major, "pktcdvd");
2648 out2:
2649 mempool_destroy(psd_pool);
2650 return ret;
2653 static void __exit pkt_exit(void)
2655 remove_proc_entry("pktcdvd", proc_root_driver);
2656 misc_deregister(&pkt_misc);
2657 unregister_blkdev(pkt_major, "pktcdvd");
2658 mempool_destroy(psd_pool);
2661 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2662 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2663 MODULE_LICENSE("GPL");
2665 module_init(pkt_init);
2666 module_exit(pkt_exit);