[NETPOLL]: deadlock bugfix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / pktcdvd.c
blob7b838342f0a353939324c228212074b15ea48c76
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-R, CD-RW, DVD-R, and
9 * DVD-RW devices (aka an exercise in block layer masturbation)
12 * TODO: (circa order of when I will fix it)
13 * - Only able to write on CD-RW media right now.
14 * - check host application code on media and set it in write page
15 * - interface for UDF <-> packet to negotiate a new location when a write
16 * fails.
17 * - handle OPC, especially for -RW media
19 * Theory of operation:
21 * We use a custom make_request_fn function that forwards reads directly to
22 * the underlying CD device. Write requests are either attached directly to
23 * a live packet_data object, or simply stored sequentially in a list for
24 * later processing by the kcdrwd kernel thread. This driver doesn't use
25 * any elevator functionally as defined by the elevator_s struct, but the
26 * underlying CD device uses a standard elevator.
28 * This strategy makes it possible to do very late merging of IO requests.
29 * A new bio sent to pkt_make_request can be merged with a live packet_data
30 * object even if the object is in the data gathering state.
32 *************************************************************************/
34 #define VERSION_CODE "v0.2.0a 2004-07-14 Jens Axboe (axboe@suse.de) and petero2@telia.com"
36 #include <linux/pktcdvd.h>
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/kernel.h>
41 #include <linux/kthread.h>
42 #include <linux/errno.h>
43 #include <linux/spinlock.h>
44 #include <linux/file.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/miscdevice.h>
48 #include <linux/suspend.h>
49 #include <scsi/scsi_cmnd.h>
50 #include <scsi/scsi_ioctl.h>
52 #include <asm/uaccess.h>
54 #if PACKET_DEBUG
55 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
56 #else
57 #define DPRINTK(fmt, args...)
58 #endif
60 #if PACKET_DEBUG > 1
61 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
62 #else
63 #define VPRINTK(fmt, args...)
64 #endif
66 #define MAX_SPEED 0xffff
68 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
70 static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
71 static struct proc_dir_entry *pkt_proc;
72 static int pkt_major;
73 static struct semaphore ctl_mutex; /* Serialize open/close/setup/teardown */
74 static mempool_t *psd_pool;
77 static void pkt_bio_finished(struct pktcdvd_device *pd)
79 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
80 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
81 VPRINTK("pktcdvd: queue empty\n");
82 atomic_set(&pd->iosched.attention, 1);
83 wake_up(&pd->wqueue);
87 static void pkt_bio_destructor(struct bio *bio)
89 kfree(bio->bi_io_vec);
90 kfree(bio);
93 static struct bio *pkt_bio_alloc(int nr_iovecs)
95 struct bio_vec *bvl = NULL;
96 struct bio *bio;
98 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
99 if (!bio)
100 goto no_bio;
101 bio_init(bio);
103 bvl = kmalloc(nr_iovecs * sizeof(struct bio_vec), GFP_KERNEL);
104 if (!bvl)
105 goto no_bvl;
106 memset(bvl, 0, nr_iovecs * sizeof(struct bio_vec));
108 bio->bi_max_vecs = nr_iovecs;
109 bio->bi_io_vec = bvl;
110 bio->bi_destructor = pkt_bio_destructor;
112 return bio;
114 no_bvl:
115 kfree(bio);
116 no_bio:
117 return NULL;
121 * Allocate a packet_data struct
123 static struct packet_data *pkt_alloc_packet_data(void)
125 int i;
126 struct packet_data *pkt;
128 pkt = kmalloc(sizeof(struct packet_data), GFP_KERNEL);
129 if (!pkt)
130 goto no_pkt;
131 memset(pkt, 0, sizeof(struct packet_data));
133 pkt->w_bio = pkt_bio_alloc(PACKET_MAX_SIZE);
134 if (!pkt->w_bio)
135 goto no_bio;
137 for (i = 0; i < PAGES_PER_PACKET; i++) {
138 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
139 if (!pkt->pages[i])
140 goto no_page;
143 spin_lock_init(&pkt->lock);
145 for (i = 0; i < PACKET_MAX_SIZE; i++) {
146 struct bio *bio = pkt_bio_alloc(1);
147 if (!bio)
148 goto no_rd_bio;
149 pkt->r_bios[i] = bio;
152 return pkt;
154 no_rd_bio:
155 for (i = 0; i < PACKET_MAX_SIZE; i++) {
156 struct bio *bio = pkt->r_bios[i];
157 if (bio)
158 bio_put(bio);
161 no_page:
162 for (i = 0; i < PAGES_PER_PACKET; i++)
163 if (pkt->pages[i])
164 __free_page(pkt->pages[i]);
165 bio_put(pkt->w_bio);
166 no_bio:
167 kfree(pkt);
168 no_pkt:
169 return NULL;
173 * Free a packet_data struct
175 static void pkt_free_packet_data(struct packet_data *pkt)
177 int i;
179 for (i = 0; i < PACKET_MAX_SIZE; i++) {
180 struct bio *bio = pkt->r_bios[i];
181 if (bio)
182 bio_put(bio);
184 for (i = 0; i < PAGES_PER_PACKET; i++)
185 __free_page(pkt->pages[i]);
186 bio_put(pkt->w_bio);
187 kfree(pkt);
190 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
192 struct packet_data *pkt, *next;
194 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
196 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
197 pkt_free_packet_data(pkt);
201 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
203 struct packet_data *pkt;
205 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
206 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
207 spin_lock_init(&pd->cdrw.active_list_lock);
208 while (nr_packets > 0) {
209 pkt = pkt_alloc_packet_data();
210 if (!pkt) {
211 pkt_shrink_pktlist(pd);
212 return 0;
214 pkt->id = nr_packets;
215 pkt->pd = pd;
216 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
217 nr_packets--;
219 return 1;
222 static void *pkt_rb_alloc(unsigned int __nocast gfp_mask, void *data)
224 return kmalloc(sizeof(struct pkt_rb_node), gfp_mask);
227 static void pkt_rb_free(void *ptr, void *data)
229 kfree(ptr);
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 inline 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 inline 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);
375 rq->ref_count++;
376 rq->flags |= REQ_NOMERGE;
377 rq->waiting = &wait;
378 rq->end_io = blk_end_sync_rq;
379 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
380 generic_unplug_device(q);
381 wait_for_completion(&wait);
383 if (rq->errors)
384 err = -EIO;
386 blk_put_request(rq);
387 return err;
391 * A generic sense dump / resolve mechanism should be implemented across
392 * all ATAPI + SCSI devices.
394 static void pkt_dump_sense(struct packet_command *cgc)
396 static char *info[9] = { "No sense", "Recovered error", "Not ready",
397 "Medium error", "Hardware error", "Illegal request",
398 "Unit attention", "Data protect", "Blank check" };
399 int i;
400 struct request_sense *sense = cgc->sense;
402 printk("pktcdvd:");
403 for (i = 0; i < CDROM_PACKET_SIZE; i++)
404 printk(" %02x", cgc->cmd[i]);
405 printk(" - ");
407 if (sense == NULL) {
408 printk("no sense\n");
409 return;
412 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
414 if (sense->sense_key > 8) {
415 printk(" (INVALID)\n");
416 return;
419 printk(" (%s)\n", info[sense->sense_key]);
423 * flush the drive cache to media
425 static int pkt_flush_cache(struct pktcdvd_device *pd)
427 struct packet_command cgc;
429 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
430 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
431 cgc.quiet = 1;
434 * the IMMED bit -- we default to not setting it, although that
435 * would allow a much faster close, this is safer
437 #if 0
438 cgc.cmd[1] = 1 << 1;
439 #endif
440 return pkt_generic_packet(pd, &cgc);
444 * speed is given as the normal factor, e.g. 4 for 4x
446 static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
448 struct packet_command cgc;
449 struct request_sense sense;
450 int ret;
452 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
453 cgc.sense = &sense;
454 cgc.cmd[0] = GPCMD_SET_SPEED;
455 cgc.cmd[2] = (read_speed >> 8) & 0xff;
456 cgc.cmd[3] = read_speed & 0xff;
457 cgc.cmd[4] = (write_speed >> 8) & 0xff;
458 cgc.cmd[5] = write_speed & 0xff;
460 if ((ret = pkt_generic_packet(pd, &cgc)))
461 pkt_dump_sense(&cgc);
463 return ret;
467 * Queue a bio for processing by the low-level CD device. Must be called
468 * from process context.
470 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
472 spin_lock(&pd->iosched.lock);
473 if (bio_data_dir(bio) == READ) {
474 pkt_add_list_last(bio, &pd->iosched.read_queue,
475 &pd->iosched.read_queue_tail);
476 } else {
477 pkt_add_list_last(bio, &pd->iosched.write_queue,
478 &pd->iosched.write_queue_tail);
480 spin_unlock(&pd->iosched.lock);
482 atomic_set(&pd->iosched.attention, 1);
483 wake_up(&pd->wqueue);
487 * Process the queued read/write requests. This function handles special
488 * requirements for CDRW drives:
489 * - A cache flush command must be inserted before a read request if the
490 * previous request was a write.
491 * - Switching between reading and writing is slow, so don't do it more often
492 * than necessary.
493 * - Optimize for throughput at the expense of latency. This means that streaming
494 * writes will never be interrupted by a read, but if the drive has to seek
495 * before the next write, switch to reading instead if there are any pending
496 * read requests.
497 * - Set the read speed according to current usage pattern. When only reading
498 * from the device, it's best to use the highest possible read speed, but
499 * when switching often between reading and writing, it's better to have the
500 * same read and write speeds.
502 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
504 request_queue_t *q;
506 if (atomic_read(&pd->iosched.attention) == 0)
507 return;
508 atomic_set(&pd->iosched.attention, 0);
510 q = bdev_get_queue(pd->bdev);
512 for (;;) {
513 struct bio *bio;
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)
522 break;
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))
530 need_write_seek = 0;
531 if (need_write_seek && reads_queued) {
532 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
533 VPRINTK("pktcdvd: write, waiting\n");
534 break;
536 pkt_flush_cache(pd);
537 pd->iosched.writing = 0;
539 } else {
540 if (!reads_queued && writes_queued) {
541 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
542 VPRINTK("pktcdvd: read, waiting\n");
543 break;
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);
553 } else {
554 bio = pkt_get_list_first(&pd->iosched.read_queue,
555 &pd->iosched.read_queue_tail);
557 spin_unlock(&pd->iosched.lock);
559 if (!bio)
560 continue;
562 if (bio_data_dir(bio) == READ)
563 pd->iosched.successive_reads += bio->bi_size >> 10;
564 else {
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);
573 } else {
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);
596 return 0;
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);
603 return 0;
604 } else {
605 printk("pktcdvd: cdrom max_phys_segments too small\n");
606 return -EIO;
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);
624 BUG_ON(len < 0);
625 memcpy(vto, vfrom, len);
626 kunmap_atomic(vfrom, KM_USER0);
628 seg++;
629 offs = 0;
630 dst_offs += len;
631 copy_size -= len;
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 page **pages, int *offsets)
644 int f, p, offs;
646 /* Copy all data to pkt->pages[] */
647 p = 0;
648 offs = 0;
649 for (f = 0; f < pkt->frames; f++) {
650 if (pages[f] != pkt->pages[p]) {
651 void *vfrom = kmap_atomic(pages[f], KM_USER0) + offsets[f];
652 void *vto = page_address(pkt->pages[p]) + offs;
653 memcpy(vto, vfrom, CD_FRAMESIZE);
654 kunmap_atomic(vfrom, KM_USER0);
655 pages[f] = pkt->pages[p];
656 offsets[f] = offs;
657 } else {
658 BUG_ON(offsets[f] != offs);
660 offs += CD_FRAMESIZE;
661 if (offs >= PAGE_SIZE) {
662 BUG_ON(offs > PAGE_SIZE);
663 offs = 0;
664 p++;
669 static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
671 struct packet_data *pkt = bio->bi_private;
672 struct pktcdvd_device *pd = pkt->pd;
673 BUG_ON(!pd);
675 if (bio->bi_size)
676 return 1;
678 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
679 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
681 if (err)
682 atomic_inc(&pkt->io_errors);
683 if (atomic_dec_and_test(&pkt->io_wait)) {
684 atomic_inc(&pkt->run_sm);
685 wake_up(&pd->wqueue);
687 pkt_bio_finished(pd);
689 return 0;
692 static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
694 struct packet_data *pkt = bio->bi_private;
695 struct pktcdvd_device *pd = pkt->pd;
696 BUG_ON(!pd);
698 if (bio->bi_size)
699 return 1;
701 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
703 pd->stats.pkt_ended++;
705 pkt_bio_finished(pd);
706 atomic_dec(&pkt->io_wait);
707 atomic_inc(&pkt->run_sm);
708 wake_up(&pd->wqueue);
709 return 0;
713 * Schedule reads for the holes in a packet
715 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
717 int frames_read = 0;
718 struct bio *bio;
719 int f;
720 char written[PACKET_MAX_SIZE];
722 BUG_ON(!pkt->orig_bios);
724 atomic_set(&pkt->io_wait, 0);
725 atomic_set(&pkt->io_errors, 0);
727 if (pkt->cache_valid) {
728 VPRINTK("pkt_gather_data: zone %llx cached\n",
729 (unsigned long long)pkt->sector);
730 goto out_account;
734 * Figure out which frames we need to read before we can write.
736 memset(written, 0, sizeof(written));
737 spin_lock(&pkt->lock);
738 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
739 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
740 int num_frames = bio->bi_size / CD_FRAMESIZE;
741 BUG_ON(first_frame < 0);
742 BUG_ON(first_frame + num_frames > pkt->frames);
743 for (f = first_frame; f < first_frame + num_frames; f++)
744 written[f] = 1;
746 spin_unlock(&pkt->lock);
749 * Schedule reads for missing parts of the packet.
751 for (f = 0; f < pkt->frames; f++) {
752 int p, offset;
753 if (written[f])
754 continue;
755 bio = pkt->r_bios[f];
756 bio_init(bio);
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))
768 BUG();
770 atomic_inc(&pkt->io_wait);
771 bio->bi_rw = READ;
772 pkt_queue_bio(pd, bio);
773 frames_read++;
776 out_account:
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);
781 pd->stats.secs_w += pd->settings.size;
785 * Find a packet matching zone, or the least recently used packet if
786 * there is no match.
788 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
790 struct packet_data *pkt;
792 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
793 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
794 list_del_init(&pkt->list);
795 if (pkt->sector != zone)
796 pkt->cache_valid = 0;
797 break;
800 return pkt;
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);
807 } else {
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
822 * recovery handling.
824 return 0;
825 #if 0
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;
831 sector_t new_sector;
833 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
834 if (pkt_bdev) {
835 sb = get_super(pkt_bdev);
836 bdput(pkt_bdev);
839 if (!sb)
840 return 0;
842 if (!sb->s_op || !sb->s_op->relocate_blocks)
843 goto out;
845 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
846 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
847 goto out;
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);
863 drop_super(sb);
864 return 1;
866 out:
867 drop_super(sb);
868 return 0;
869 #endif
872 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
874 #if PACKET_DEBUG > 1
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]);
881 #endif
882 pkt->state = 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;
895 struct rb_node *n;
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");
903 return 0;
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);
911 if (!first_node) {
912 n = rb_first(&pd->bio_queue);
913 if (n)
914 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
916 node = first_node;
917 while (node) {
918 bio = node->bio;
919 zone = ZONE(bio->bi_sector, pd);
920 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
921 if (p->sector == zone) {
922 bio = NULL;
923 goto try_next_bio;
926 break;
927 try_next_bio:
928 node = pkt_rbtree_next(node);
929 if (!node) {
930 n = rb_first(&pd->bio_queue);
931 if (n)
932 node = rb_entry(n, struct pkt_rb_node, rb_node);
934 if (node == first_node)
935 node = NULL;
937 spin_unlock(&pd->lock);
938 if (!bio) {
939 VPRINTK("handle_queue: no bio\n");
940 return 0;
943 pkt = pkt_get_packet_data(pd, zone);
944 BUG_ON(!pkt);
946 pd->current_sector = zone + pd->settings.size;
947 pkt->sector = zone;
948 pkt->frames = pd->settings.size >> 2;
949 BUG_ON(pkt->frames > PACKET_MAX_SIZE);
950 pkt->write_size = 0;
953 * Scan work queue for bios in the same zone and link them
954 * to this packet.
956 spin_lock(&pd->lock);
957 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
958 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
959 bio = node->bio;
960 VPRINTK("pkt_handle_queue: found zone=%llx\n",
961 (unsigned long long)ZONE(bio->bi_sector, pd));
962 if (ZONE(bio->bi_sector, pd) != zone)
963 break;
964 pkt_rbtree_erase(pd, node);
965 spin_lock(&pkt->lock);
966 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
967 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
968 spin_unlock(&pkt->lock);
970 spin_unlock(&pd->lock);
972 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
973 pkt_set_state(pkt, PACKET_WAITING_STATE);
974 atomic_set(&pkt->run_sm, 1);
976 spin_lock(&pd->cdrw.active_list_lock);
977 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
978 spin_unlock(&pd->cdrw.active_list_lock);
980 return 1;
984 * Assemble a bio to write one packet and queue the bio for processing
985 * by the underlying block device.
987 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
989 struct bio *bio;
990 struct page *pages[PACKET_MAX_SIZE];
991 int offsets[PACKET_MAX_SIZE];
992 int f;
993 int frames_write;
995 for (f = 0; f < pkt->frames; f++) {
996 pages[f] = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
997 offsets[f] = (f * CD_FRAMESIZE) % PAGE_SIZE;
1001 * Fill-in pages[] and offsets[] with data from orig_bios.
1003 frames_write = 0;
1004 spin_lock(&pkt->lock);
1005 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1006 int segment = bio->bi_idx;
1007 int src_offs = 0;
1008 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1009 int num_frames = bio->bi_size / CD_FRAMESIZE;
1010 BUG_ON(first_frame < 0);
1011 BUG_ON(first_frame + num_frames > pkt->frames);
1012 for (f = first_frame; f < first_frame + num_frames; f++) {
1013 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1015 while (src_offs >= src_bvl->bv_len) {
1016 src_offs -= src_bvl->bv_len;
1017 segment++;
1018 BUG_ON(segment >= bio->bi_vcnt);
1019 src_bvl = bio_iovec_idx(bio, segment);
1022 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1023 pages[f] = src_bvl->bv_page;
1024 offsets[f] = src_bvl->bv_offset + src_offs;
1025 } else {
1026 pkt_copy_bio_data(bio, segment, src_offs,
1027 pages[f], offsets[f]);
1029 src_offs += CD_FRAMESIZE;
1030 frames_write++;
1033 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1034 spin_unlock(&pkt->lock);
1036 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1037 frames_write, (unsigned long long)pkt->sector);
1038 BUG_ON(frames_write != pkt->write_size);
1040 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1041 pkt_make_local_copy(pkt, pages, offsets);
1042 pkt->cache_valid = 1;
1043 } else {
1044 pkt->cache_valid = 0;
1047 /* Start the write request */
1048 bio_init(pkt->w_bio);
1049 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1050 pkt->w_bio->bi_sector = pkt->sector;
1051 pkt->w_bio->bi_bdev = pd->bdev;
1052 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1053 pkt->w_bio->bi_private = pkt;
1054 for (f = 0; f < pkt->frames; f++) {
1055 if ((f + 1 < pkt->frames) && (pages[f + 1] == pages[f]) &&
1056 (offsets[f + 1] = offsets[f] + CD_FRAMESIZE)) {
1057 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE * 2, offsets[f]))
1058 BUG();
1059 f++;
1060 } else {
1061 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE, offsets[f]))
1062 BUG();
1065 VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
1067 atomic_set(&pkt->io_wait, 1);
1068 pkt->w_bio->bi_rw = WRITE;
1069 pkt_queue_bio(pd, pkt->w_bio);
1072 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1074 struct bio *bio, *next;
1076 if (!uptodate)
1077 pkt->cache_valid = 0;
1079 /* Finish all bios corresponding to this packet */
1080 bio = pkt->orig_bios;
1081 while (bio) {
1082 next = bio->bi_next;
1083 bio->bi_next = NULL;
1084 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1085 bio = next;
1087 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1090 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1092 int uptodate;
1094 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1096 for (;;) {
1097 switch (pkt->state) {
1098 case PACKET_WAITING_STATE:
1099 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1100 return;
1102 pkt->sleep_time = 0;
1103 pkt_gather_data(pd, pkt);
1104 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1105 break;
1107 case PACKET_READ_WAIT_STATE:
1108 if (atomic_read(&pkt->io_wait) > 0)
1109 return;
1111 if (atomic_read(&pkt->io_errors) > 0) {
1112 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1113 } else {
1114 pkt_start_write(pd, pkt);
1116 break;
1118 case PACKET_WRITE_WAIT_STATE:
1119 if (atomic_read(&pkt->io_wait) > 0)
1120 return;
1122 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1123 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1124 } else {
1125 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1127 break;
1129 case PACKET_RECOVERY_STATE:
1130 if (pkt_start_recovery(pkt)) {
1131 pkt_start_write(pd, pkt);
1132 } else {
1133 VPRINTK("No recovery possible\n");
1134 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1136 break;
1138 case PACKET_FINISHED_STATE:
1139 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1140 pkt_finish_packet(pkt, uptodate);
1141 return;
1143 default:
1144 BUG();
1145 break;
1150 static void pkt_handle_packets(struct pktcdvd_device *pd)
1152 struct packet_data *pkt, *next;
1154 VPRINTK("pkt_handle_packets\n");
1157 * Run state machine for active packets
1159 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1160 if (atomic_read(&pkt->run_sm) > 0) {
1161 atomic_set(&pkt->run_sm, 0);
1162 pkt_run_state_machine(pd, pkt);
1167 * Move no longer active packets to the free list
1169 spin_lock(&pd->cdrw.active_list_lock);
1170 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1171 if (pkt->state == PACKET_FINISHED_STATE) {
1172 list_del(&pkt->list);
1173 pkt_put_packet_data(pd, pkt);
1174 pkt_set_state(pkt, PACKET_IDLE_STATE);
1175 atomic_set(&pd->scan_queue, 1);
1178 spin_unlock(&pd->cdrw.active_list_lock);
1181 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1183 struct packet_data *pkt;
1184 int i;
1186 for (i = 0; i <= PACKET_NUM_STATES; i++)
1187 states[i] = 0;
1189 spin_lock(&pd->cdrw.active_list_lock);
1190 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1191 states[pkt->state]++;
1193 spin_unlock(&pd->cdrw.active_list_lock);
1197 * kcdrwd is woken up when writes have been queued for one of our
1198 * registered devices
1200 static int kcdrwd(void *foobar)
1202 struct pktcdvd_device *pd = foobar;
1203 struct packet_data *pkt;
1204 long min_sleep_time, residue;
1206 set_user_nice(current, -20);
1208 for (;;) {
1209 DECLARE_WAITQUEUE(wait, current);
1212 * Wait until there is something to do
1214 add_wait_queue(&pd->wqueue, &wait);
1215 for (;;) {
1216 set_current_state(TASK_INTERRUPTIBLE);
1218 /* Check if we need to run pkt_handle_queue */
1219 if (atomic_read(&pd->scan_queue) > 0)
1220 goto work_to_do;
1222 /* Check if we need to run the state machine for some packet */
1223 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1224 if (atomic_read(&pkt->run_sm) > 0)
1225 goto work_to_do;
1228 /* Check if we need to process the iosched queues */
1229 if (atomic_read(&pd->iosched.attention) != 0)
1230 goto work_to_do;
1232 /* Otherwise, go to sleep */
1233 if (PACKET_DEBUG > 1) {
1234 int states[PACKET_NUM_STATES];
1235 pkt_count_states(pd, states);
1236 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1237 states[0], states[1], states[2], states[3],
1238 states[4], states[5]);
1241 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1242 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1243 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1244 min_sleep_time = pkt->sleep_time;
1247 generic_unplug_device(bdev_get_queue(pd->bdev));
1249 VPRINTK("kcdrwd: sleeping\n");
1250 residue = schedule_timeout(min_sleep_time);
1251 VPRINTK("kcdrwd: wake up\n");
1253 /* make swsusp happy with our thread */
1254 try_to_freeze();
1256 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1257 if (!pkt->sleep_time)
1258 continue;
1259 pkt->sleep_time -= min_sleep_time - residue;
1260 if (pkt->sleep_time <= 0) {
1261 pkt->sleep_time = 0;
1262 atomic_inc(&pkt->run_sm);
1266 if (signal_pending(current)) {
1267 flush_signals(current);
1269 if (kthread_should_stop())
1270 break;
1272 work_to_do:
1273 set_current_state(TASK_RUNNING);
1274 remove_wait_queue(&pd->wqueue, &wait);
1276 if (kthread_should_stop())
1277 break;
1280 * if pkt_handle_queue returns true, we can queue
1281 * another request.
1283 while (pkt_handle_queue(pd))
1287 * Handle packet state machine
1289 pkt_handle_packets(pd);
1292 * Handle iosched queues
1294 pkt_iosched_process_queue(pd);
1297 return 0;
1300 static void pkt_print_settings(struct pktcdvd_device *pd)
1302 printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1303 printk("%u blocks, ", pd->settings.size >> 2);
1304 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1307 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1309 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1311 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1312 cgc->cmd[2] = page_code | (page_control << 6);
1313 cgc->cmd[7] = cgc->buflen >> 8;
1314 cgc->cmd[8] = cgc->buflen & 0xff;
1315 cgc->data_direction = CGC_DATA_READ;
1316 return pkt_generic_packet(pd, cgc);
1319 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1321 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1322 memset(cgc->buffer, 0, 2);
1323 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1324 cgc->cmd[1] = 0x10; /* PF */
1325 cgc->cmd[7] = cgc->buflen >> 8;
1326 cgc->cmd[8] = cgc->buflen & 0xff;
1327 cgc->data_direction = CGC_DATA_WRITE;
1328 return pkt_generic_packet(pd, cgc);
1331 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1333 struct packet_command cgc;
1334 int ret;
1336 /* set up command and get the disc info */
1337 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1338 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1339 cgc.cmd[8] = cgc.buflen = 2;
1340 cgc.quiet = 1;
1342 if ((ret = pkt_generic_packet(pd, &cgc)))
1343 return ret;
1345 /* not all drives have the same disc_info length, so requeue
1346 * packet with the length the drive tells us it can supply
1348 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1349 sizeof(di->disc_information_length);
1351 if (cgc.buflen > sizeof(disc_information))
1352 cgc.buflen = sizeof(disc_information);
1354 cgc.cmd[8] = cgc.buflen;
1355 return pkt_generic_packet(pd, &cgc);
1358 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1360 struct packet_command cgc;
1361 int ret;
1363 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1364 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1365 cgc.cmd[1] = type & 3;
1366 cgc.cmd[4] = (track & 0xff00) >> 8;
1367 cgc.cmd[5] = track & 0xff;
1368 cgc.cmd[8] = 8;
1369 cgc.quiet = 1;
1371 if ((ret = pkt_generic_packet(pd, &cgc)))
1372 return ret;
1374 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1375 sizeof(ti->track_information_length);
1377 if (cgc.buflen > sizeof(track_information))
1378 cgc.buflen = sizeof(track_information);
1380 cgc.cmd[8] = cgc.buflen;
1381 return pkt_generic_packet(pd, &cgc);
1384 static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1386 disc_information di;
1387 track_information ti;
1388 __u32 last_track;
1389 int ret = -1;
1391 if ((ret = pkt_get_disc_info(pd, &di)))
1392 return ret;
1394 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1395 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1396 return ret;
1398 /* if this track is blank, try the previous. */
1399 if (ti.blank) {
1400 last_track--;
1401 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1402 return ret;
1405 /* if last recorded field is valid, return it. */
1406 if (ti.lra_v) {
1407 *last_written = be32_to_cpu(ti.last_rec_address);
1408 } else {
1409 /* make it up instead */
1410 *last_written = be32_to_cpu(ti.track_start) +
1411 be32_to_cpu(ti.track_size);
1412 if (ti.free_blocks)
1413 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1415 return 0;
1419 * write mode select package based on pd->settings
1421 static int pkt_set_write_settings(struct pktcdvd_device *pd)
1423 struct packet_command cgc;
1424 struct request_sense sense;
1425 write_param_page *wp;
1426 char buffer[128];
1427 int ret, size;
1429 /* doesn't apply to DVD+RW or DVD-RAM */
1430 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1431 return 0;
1433 memset(buffer, 0, sizeof(buffer));
1434 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1435 cgc.sense = &sense;
1436 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1437 pkt_dump_sense(&cgc);
1438 return ret;
1441 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1442 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1443 if (size > sizeof(buffer))
1444 size = sizeof(buffer);
1447 * now get it all
1449 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1450 cgc.sense = &sense;
1451 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1452 pkt_dump_sense(&cgc);
1453 return ret;
1457 * write page is offset header + block descriptor length
1459 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1461 wp->fp = pd->settings.fp;
1462 wp->track_mode = pd->settings.track_mode;
1463 wp->write_type = pd->settings.write_type;
1464 wp->data_block_type = pd->settings.block_mode;
1466 wp->multi_session = 0;
1468 #ifdef PACKET_USE_LS
1469 wp->link_size = 7;
1470 wp->ls_v = 1;
1471 #endif
1473 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1474 wp->session_format = 0;
1475 wp->subhdr2 = 0x20;
1476 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1477 wp->session_format = 0x20;
1478 wp->subhdr2 = 8;
1479 #if 0
1480 wp->mcn[0] = 0x80;
1481 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1482 #endif
1483 } else {
1485 * paranoia
1487 printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
1488 return 1;
1490 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1492 cgc.buflen = cgc.cmd[8] = size;
1493 if ((ret = pkt_mode_select(pd, &cgc))) {
1494 pkt_dump_sense(&cgc);
1495 return ret;
1498 pkt_print_settings(pd);
1499 return 0;
1503 * 0 -- we can write to this track, 1 -- we can't
1505 static int pkt_good_track(track_information *ti)
1508 * only good for CD-RW at the moment, not DVD-RW
1512 * FIXME: only for FP
1514 if (ti->fp == 0)
1515 return 0;
1518 * "good" settings as per Mt Fuji.
1520 if (ti->rt == 0 && ti->blank == 0 && ti->packet == 1)
1521 return 0;
1523 if (ti->rt == 0 && ti->blank == 1 && ti->packet == 1)
1524 return 0;
1526 if (ti->rt == 1 && ti->blank == 0 && ti->packet == 1)
1527 return 0;
1529 printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1530 return 1;
1534 * 0 -- we can write to this disc, 1 -- we can't
1536 static int pkt_good_disc(struct pktcdvd_device *pd, disc_information *di)
1538 switch (pd->mmc3_profile) {
1539 case 0x0a: /* CD-RW */
1540 case 0xffff: /* MMC3 not supported */
1541 break;
1542 case 0x1a: /* DVD+RW */
1543 case 0x13: /* DVD-RW */
1544 case 0x12: /* DVD-RAM */
1545 return 0;
1546 default:
1547 printk("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
1548 return 1;
1552 * for disc type 0xff we should probably reserve a new track.
1553 * but i'm not sure, should we leave this to user apps? probably.
1555 if (di->disc_type == 0xff) {
1556 printk("pktcdvd: Unknown disc. No track?\n");
1557 return 1;
1560 if (di->disc_type != 0x20 && di->disc_type != 0) {
1561 printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
1562 return 1;
1565 if (di->erasable == 0) {
1566 printk("pktcdvd: Disc not erasable\n");
1567 return 1;
1570 if (di->border_status == PACKET_SESSION_RESERVED) {
1571 printk("pktcdvd: Can't write to last track (reserved)\n");
1572 return 1;
1575 return 0;
1578 static int pkt_probe_settings(struct pktcdvd_device *pd)
1580 struct packet_command cgc;
1581 unsigned char buf[12];
1582 disc_information di;
1583 track_information ti;
1584 int ret, track;
1586 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1587 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1588 cgc.cmd[8] = 8;
1589 ret = pkt_generic_packet(pd, &cgc);
1590 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1592 memset(&di, 0, sizeof(disc_information));
1593 memset(&ti, 0, sizeof(track_information));
1595 if ((ret = pkt_get_disc_info(pd, &di))) {
1596 printk("failed get_disc\n");
1597 return ret;
1600 if (pkt_good_disc(pd, &di))
1601 return -ENXIO;
1603 switch (pd->mmc3_profile) {
1604 case 0x1a: /* DVD+RW */
1605 printk("pktcdvd: inserted media is DVD+RW\n");
1606 break;
1607 case 0x13: /* DVD-RW */
1608 printk("pktcdvd: inserted media is DVD-RW\n");
1609 break;
1610 case 0x12: /* DVD-RAM */
1611 printk("pktcdvd: inserted media is DVD-RAM\n");
1612 break;
1613 default:
1614 printk("pktcdvd: inserted media is CD-R%s\n", di.erasable ? "W" : "");
1615 break;
1617 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1619 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1620 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1621 printk("pktcdvd: failed get_track\n");
1622 return ret;
1625 if (pkt_good_track(&ti)) {
1626 printk("pktcdvd: can't write to this track\n");
1627 return -ENXIO;
1631 * we keep packet size in 512 byte units, makes it easier to
1632 * deal with request calculations.
1634 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1635 if (pd->settings.size == 0) {
1636 printk("pktcdvd: detected zero packet size!\n");
1637 pd->settings.size = 128;
1639 pd->settings.fp = ti.fp;
1640 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1642 if (ti.nwa_v) {
1643 pd->nwa = be32_to_cpu(ti.next_writable);
1644 set_bit(PACKET_NWA_VALID, &pd->flags);
1648 * in theory we could use lra on -RW media as well and just zero
1649 * blocks that haven't been written yet, but in practice that
1650 * is just a no-go. we'll use that for -R, naturally.
1652 if (ti.lra_v) {
1653 pd->lra = be32_to_cpu(ti.last_rec_address);
1654 set_bit(PACKET_LRA_VALID, &pd->flags);
1655 } else {
1656 pd->lra = 0xffffffff;
1657 set_bit(PACKET_LRA_VALID, &pd->flags);
1661 * fine for now
1663 pd->settings.link_loss = 7;
1664 pd->settings.write_type = 0; /* packet */
1665 pd->settings.track_mode = ti.track_mode;
1668 * mode1 or mode2 disc
1670 switch (ti.data_mode) {
1671 case PACKET_MODE1:
1672 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1673 break;
1674 case PACKET_MODE2:
1675 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1676 break;
1677 default:
1678 printk("pktcdvd: unknown data mode\n");
1679 return 1;
1681 return 0;
1685 * enable/disable write caching on drive
1687 static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1689 struct packet_command cgc;
1690 struct request_sense sense;
1691 unsigned char buf[64];
1692 int ret;
1694 memset(buf, 0, sizeof(buf));
1695 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1696 cgc.sense = &sense;
1697 cgc.buflen = pd->mode_offset + 12;
1700 * caching mode page might not be there, so quiet this command
1702 cgc.quiet = 1;
1704 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1705 return ret;
1707 buf[pd->mode_offset + 10] |= (!!set << 2);
1709 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1710 ret = pkt_mode_select(pd, &cgc);
1711 if (ret) {
1712 printk("pktcdvd: write caching control failed\n");
1713 pkt_dump_sense(&cgc);
1714 } else if (!ret && set)
1715 printk("pktcdvd: enabled write caching on %s\n", pd->name);
1716 return ret;
1719 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1721 struct packet_command cgc;
1723 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1724 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1725 cgc.cmd[4] = lockflag ? 1 : 0;
1726 return pkt_generic_packet(pd, &cgc);
1730 * Returns drive maximum write speed
1732 static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1734 struct packet_command cgc;
1735 struct request_sense sense;
1736 unsigned char buf[256+18];
1737 unsigned char *cap_buf;
1738 int ret, offset;
1740 memset(buf, 0, sizeof(buf));
1741 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1742 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1743 cgc.sense = &sense;
1745 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1746 if (ret) {
1747 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1748 sizeof(struct mode_page_header);
1749 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1750 if (ret) {
1751 pkt_dump_sense(&cgc);
1752 return ret;
1756 offset = 20; /* Obsoleted field, used by older drives */
1757 if (cap_buf[1] >= 28)
1758 offset = 28; /* Current write speed selected */
1759 if (cap_buf[1] >= 30) {
1760 /* If the drive reports at least one "Logical Unit Write
1761 * Speed Performance Descriptor Block", use the information
1762 * in the first block. (contains the highest speed)
1764 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1765 if (num_spdb > 0)
1766 offset = 34;
1769 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1770 return 0;
1773 /* These tables from cdrecord - I don't have orange book */
1774 /* standard speed CD-RW (1-4x) */
1775 static char clv_to_speed[16] = {
1776 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1777 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1779 /* high speed CD-RW (-10x) */
1780 static char hs_clv_to_speed[16] = {
1781 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1782 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1784 /* ultra high speed CD-RW */
1785 static char us_clv_to_speed[16] = {
1786 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1787 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1791 * reads the maximum media speed from ATIP
1793 static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1795 struct packet_command cgc;
1796 struct request_sense sense;
1797 unsigned char buf[64];
1798 unsigned int size, st, sp;
1799 int ret;
1801 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1802 cgc.sense = &sense;
1803 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1804 cgc.cmd[1] = 2;
1805 cgc.cmd[2] = 4; /* READ ATIP */
1806 cgc.cmd[8] = 2;
1807 ret = pkt_generic_packet(pd, &cgc);
1808 if (ret) {
1809 pkt_dump_sense(&cgc);
1810 return ret;
1812 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1813 if (size > sizeof(buf))
1814 size = sizeof(buf);
1816 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1817 cgc.sense = &sense;
1818 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1819 cgc.cmd[1] = 2;
1820 cgc.cmd[2] = 4;
1821 cgc.cmd[8] = size;
1822 ret = pkt_generic_packet(pd, &cgc);
1823 if (ret) {
1824 pkt_dump_sense(&cgc);
1825 return ret;
1828 if (!buf[6] & 0x40) {
1829 printk("pktcdvd: Disc type is not CD-RW\n");
1830 return 1;
1832 if (!buf[6] & 0x4) {
1833 printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
1834 return 1;
1837 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1839 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1841 /* Info from cdrecord */
1842 switch (st) {
1843 case 0: /* standard speed */
1844 *speed = clv_to_speed[sp];
1845 break;
1846 case 1: /* high speed */
1847 *speed = hs_clv_to_speed[sp];
1848 break;
1849 case 2: /* ultra high speed */
1850 *speed = us_clv_to_speed[sp];
1851 break;
1852 default:
1853 printk("pktcdvd: Unknown disc sub-type %d\n",st);
1854 return 1;
1856 if (*speed) {
1857 printk("pktcdvd: Max. media speed: %d\n",*speed);
1858 return 0;
1859 } else {
1860 printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
1861 return 1;
1865 static int pkt_perform_opc(struct pktcdvd_device *pd)
1867 struct packet_command cgc;
1868 struct request_sense sense;
1869 int ret;
1871 VPRINTK("pktcdvd: Performing OPC\n");
1873 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1874 cgc.sense = &sense;
1875 cgc.timeout = 60*HZ;
1876 cgc.cmd[0] = GPCMD_SEND_OPC;
1877 cgc.cmd[1] = 1;
1878 if ((ret = pkt_generic_packet(pd, &cgc)))
1879 pkt_dump_sense(&cgc);
1880 return ret;
1883 static int pkt_open_write(struct pktcdvd_device *pd)
1885 int ret;
1886 unsigned int write_speed, media_write_speed, read_speed;
1888 if ((ret = pkt_probe_settings(pd))) {
1889 DPRINTK("pktcdvd: %s failed probe\n", pd->name);
1890 return -EIO;
1893 if ((ret = pkt_set_write_settings(pd))) {
1894 DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
1895 return -EIO;
1898 pkt_write_caching(pd, USE_WCACHING);
1900 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1901 write_speed = 16 * 177;
1902 switch (pd->mmc3_profile) {
1903 case 0x13: /* DVD-RW */
1904 case 0x1a: /* DVD+RW */
1905 case 0x12: /* DVD-RAM */
1906 DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
1907 break;
1908 default:
1909 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1910 media_write_speed = 16;
1911 write_speed = min(write_speed, media_write_speed * 177);
1912 DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
1913 break;
1915 read_speed = write_speed;
1917 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1918 DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
1919 return -EIO;
1921 pd->write_speed = write_speed;
1922 pd->read_speed = read_speed;
1924 if ((ret = pkt_perform_opc(pd))) {
1925 DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
1928 return 0;
1932 * called at open time.
1934 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1936 int ret;
1937 long lba;
1938 request_queue_t *q;
1941 * We need to re-open the cdrom device without O_NONBLOCK to be able
1942 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1943 * so bdget() can't fail.
1945 bdget(pd->bdev->bd_dev);
1946 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1947 goto out;
1949 if ((ret = pkt_get_last_written(pd, &lba))) {
1950 printk("pktcdvd: pkt_get_last_written failed\n");
1951 goto out_putdev;
1954 set_capacity(pd->disk, lba << 2);
1955 set_capacity(pd->bdev->bd_disk, lba << 2);
1956 bd_set_size(pd->bdev, (loff_t)lba << 11);
1958 q = bdev_get_queue(pd->bdev);
1959 if (write) {
1960 if ((ret = pkt_open_write(pd)))
1961 goto out_putdev;
1963 * Some CDRW drives can not handle writes larger than one packet,
1964 * even if the size is a multiple of the packet size.
1966 spin_lock_irq(q->queue_lock);
1967 blk_queue_max_sectors(q, pd->settings.size);
1968 spin_unlock_irq(q->queue_lock);
1969 set_bit(PACKET_WRITABLE, &pd->flags);
1970 } else {
1971 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1972 clear_bit(PACKET_WRITABLE, &pd->flags);
1975 if ((ret = pkt_set_segment_merging(pd, q)))
1976 goto out_putdev;
1978 if (write)
1979 printk("pktcdvd: %lukB available on disc\n", lba << 1);
1981 return 0;
1983 out_putdev:
1984 blkdev_put(pd->bdev);
1985 out:
1986 return ret;
1990 * called when the device is closed. makes sure that the device flushes
1991 * the internal cache before we close.
1993 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
1995 if (flush && pkt_flush_cache(pd))
1996 DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
1998 pkt_lock_door(pd, 0);
2000 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2001 blkdev_put(pd->bdev);
2004 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2006 if (dev_minor >= MAX_WRITERS)
2007 return NULL;
2008 return pkt_devs[dev_minor];
2011 static int pkt_open(struct inode *inode, struct file *file)
2013 struct pktcdvd_device *pd = NULL;
2014 int ret;
2016 VPRINTK("pktcdvd: entering open\n");
2018 down(&ctl_mutex);
2019 pd = pkt_find_dev_from_minor(iminor(inode));
2020 if (!pd) {
2021 ret = -ENODEV;
2022 goto out;
2024 BUG_ON(pd->refcnt < 0);
2026 pd->refcnt++;
2027 if (pd->refcnt > 1) {
2028 if ((file->f_mode & FMODE_WRITE) &&
2029 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2030 ret = -EBUSY;
2031 goto out_dec;
2033 } else {
2034 if (pkt_open_dev(pd, file->f_mode & FMODE_WRITE)) {
2035 ret = -EIO;
2036 goto out_dec;
2039 * needed here as well, since ext2 (among others) may change
2040 * the blocksize at mount time
2042 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2045 up(&ctl_mutex);
2046 return 0;
2048 out_dec:
2049 pd->refcnt--;
2050 out:
2051 VPRINTK("pktcdvd: failed open (%d)\n", ret);
2052 up(&ctl_mutex);
2053 return ret;
2056 static int pkt_close(struct inode *inode, struct file *file)
2058 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2059 int ret = 0;
2061 down(&ctl_mutex);
2062 pd->refcnt--;
2063 BUG_ON(pd->refcnt < 0);
2064 if (pd->refcnt == 0) {
2065 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2066 pkt_release_dev(pd, flush);
2068 up(&ctl_mutex);
2069 return ret;
2073 static void *psd_pool_alloc(unsigned int __nocast gfp_mask, void *data)
2075 return kmalloc(sizeof(struct packet_stacked_data), gfp_mask);
2078 static void psd_pool_free(void *ptr, void *data)
2080 kfree(ptr);
2083 static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2085 struct packet_stacked_data *psd = bio->bi_private;
2086 struct pktcdvd_device *pd = psd->pd;
2088 if (bio->bi_size)
2089 return 1;
2091 bio_put(bio);
2092 bio_endio(psd->bio, psd->bio->bi_size, err);
2093 mempool_free(psd, psd_pool);
2094 pkt_bio_finished(pd);
2095 return 0;
2098 static int pkt_make_request(request_queue_t *q, struct bio *bio)
2100 struct pktcdvd_device *pd;
2101 char b[BDEVNAME_SIZE];
2102 sector_t zone;
2103 struct packet_data *pkt;
2104 int was_empty, blocked_bio;
2105 struct pkt_rb_node *node;
2107 pd = q->queuedata;
2108 if (!pd) {
2109 printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2110 goto end_io;
2114 * Clone READ bios so we can have our own bi_end_io callback.
2116 if (bio_data_dir(bio) == READ) {
2117 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2118 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2120 psd->pd = pd;
2121 psd->bio = bio;
2122 cloned_bio->bi_bdev = pd->bdev;
2123 cloned_bio->bi_private = psd;
2124 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2125 pd->stats.secs_r += bio->bi_size >> 9;
2126 pkt_queue_bio(pd, cloned_bio);
2127 return 0;
2130 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2131 printk("pktcdvd: WRITE for ro device %s (%llu)\n",
2132 pd->name, (unsigned long long)bio->bi_sector);
2133 goto end_io;
2136 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2137 printk("pktcdvd: wrong bio size\n");
2138 goto end_io;
2141 blk_queue_bounce(q, &bio);
2143 zone = ZONE(bio->bi_sector, pd);
2144 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2145 (unsigned long long)bio->bi_sector,
2146 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2148 /* Check if we have to split the bio */
2150 struct bio_pair *bp;
2151 sector_t last_zone;
2152 int first_sectors;
2154 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2155 if (last_zone != zone) {
2156 BUG_ON(last_zone != zone + pd->settings.size);
2157 first_sectors = last_zone - bio->bi_sector;
2158 bp = bio_split(bio, bio_split_pool, first_sectors);
2159 BUG_ON(!bp);
2160 pkt_make_request(q, &bp->bio1);
2161 pkt_make_request(q, &bp->bio2);
2162 bio_pair_release(bp);
2163 return 0;
2168 * If we find a matching packet in state WAITING or READ_WAIT, we can
2169 * just append this bio to that packet.
2171 spin_lock(&pd->cdrw.active_list_lock);
2172 blocked_bio = 0;
2173 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2174 if (pkt->sector == zone) {
2175 spin_lock(&pkt->lock);
2176 if ((pkt->state == PACKET_WAITING_STATE) ||
2177 (pkt->state == PACKET_READ_WAIT_STATE)) {
2178 pkt_add_list_last(bio, &pkt->orig_bios,
2179 &pkt->orig_bios_tail);
2180 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2181 if ((pkt->write_size >= pkt->frames) &&
2182 (pkt->state == PACKET_WAITING_STATE)) {
2183 atomic_inc(&pkt->run_sm);
2184 wake_up(&pd->wqueue);
2186 spin_unlock(&pkt->lock);
2187 spin_unlock(&pd->cdrw.active_list_lock);
2188 return 0;
2189 } else {
2190 blocked_bio = 1;
2192 spin_unlock(&pkt->lock);
2195 spin_unlock(&pd->cdrw.active_list_lock);
2198 * No matching packet found. Store the bio in the work queue.
2200 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2201 BUG_ON(!node);
2202 node->bio = bio;
2203 spin_lock(&pd->lock);
2204 BUG_ON(pd->bio_queue_size < 0);
2205 was_empty = (pd->bio_queue_size == 0);
2206 pkt_rbtree_insert(pd, node);
2207 spin_unlock(&pd->lock);
2210 * Wake up the worker thread.
2212 atomic_set(&pd->scan_queue, 1);
2213 if (was_empty) {
2214 /* This wake_up is required for correct operation */
2215 wake_up(&pd->wqueue);
2216 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2218 * This wake up is not required for correct operation,
2219 * but improves performance in some cases.
2221 wake_up(&pd->wqueue);
2223 return 0;
2224 end_io:
2225 bio_io_error(bio, bio->bi_size);
2226 return 0;
2231 static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2233 struct pktcdvd_device *pd = q->queuedata;
2234 sector_t zone = ZONE(bio->bi_sector, pd);
2235 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2236 int remaining = (pd->settings.size << 9) - used;
2237 int remaining2;
2240 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2241 * boundary, pkt_make_request() will split the bio.
2243 remaining2 = PAGE_SIZE - bio->bi_size;
2244 remaining = max(remaining, remaining2);
2246 BUG_ON(remaining < 0);
2247 return remaining;
2250 static void pkt_init_queue(struct pktcdvd_device *pd)
2252 request_queue_t *q = pd->disk->queue;
2254 blk_queue_make_request(q, pkt_make_request);
2255 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2256 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2257 blk_queue_merge_bvec(q, pkt_merge_bvec);
2258 q->queuedata = pd;
2261 static int pkt_seq_show(struct seq_file *m, void *p)
2263 struct pktcdvd_device *pd = m->private;
2264 char *msg;
2265 char bdev_buf[BDEVNAME_SIZE];
2266 int states[PACKET_NUM_STATES];
2268 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2269 bdevname(pd->bdev, bdev_buf));
2271 seq_printf(m, "\nSettings:\n");
2272 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2274 if (pd->settings.write_type == 0)
2275 msg = "Packet";
2276 else
2277 msg = "Unknown";
2278 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2280 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2281 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2283 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2285 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2286 msg = "Mode 1";
2287 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2288 msg = "Mode 2";
2289 else
2290 msg = "Unknown";
2291 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2293 seq_printf(m, "\nStatistics:\n");
2294 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2295 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2296 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2297 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2298 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2300 seq_printf(m, "\nMisc:\n");
2301 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2302 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2303 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2304 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2305 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2306 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2308 seq_printf(m, "\nQueue state:\n");
2309 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2310 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2311 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2313 pkt_count_states(pd, states);
2314 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2315 states[0], states[1], states[2], states[3], states[4], states[5]);
2317 return 0;
2320 static int pkt_seq_open(struct inode *inode, struct file *file)
2322 return single_open(file, pkt_seq_show, PDE(inode)->data);
2325 static struct file_operations pkt_proc_fops = {
2326 .open = pkt_seq_open,
2327 .read = seq_read,
2328 .llseek = seq_lseek,
2329 .release = single_release
2332 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2334 int i;
2335 int ret = 0;
2336 char b[BDEVNAME_SIZE];
2337 struct proc_dir_entry *proc;
2338 struct block_device *bdev;
2340 if (pd->pkt_dev == dev) {
2341 printk("pktcdvd: Recursive setup not allowed\n");
2342 return -EBUSY;
2344 for (i = 0; i < MAX_WRITERS; i++) {
2345 struct pktcdvd_device *pd2 = pkt_devs[i];
2346 if (!pd2)
2347 continue;
2348 if (pd2->bdev->bd_dev == dev) {
2349 printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
2350 return -EBUSY;
2352 if (pd2->pkt_dev == dev) {
2353 printk("pktcdvd: Can't chain pktcdvd devices\n");
2354 return -EBUSY;
2358 bdev = bdget(dev);
2359 if (!bdev)
2360 return -ENOMEM;
2361 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2362 if (ret)
2363 return ret;
2365 /* This is safe, since we have a reference from open(). */
2366 __module_get(THIS_MODULE);
2368 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2369 printk("pktcdvd: not enough memory for buffers\n");
2370 ret = -ENOMEM;
2371 goto out_mem;
2374 pd->bdev = bdev;
2375 set_blocksize(bdev, CD_FRAMESIZE);
2377 pkt_init_queue(pd);
2379 atomic_set(&pd->cdrw.pending_bios, 0);
2380 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2381 if (IS_ERR(pd->cdrw.thread)) {
2382 printk("pktcdvd: can't start kernel thread\n");
2383 ret = -ENOMEM;
2384 goto out_thread;
2387 proc = create_proc_entry(pd->name, 0, pkt_proc);
2388 if (proc) {
2389 proc->data = pd;
2390 proc->proc_fops = &pkt_proc_fops;
2392 DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2393 return 0;
2395 out_thread:
2396 pkt_shrink_pktlist(pd);
2397 out_mem:
2398 blkdev_put(bdev);
2399 /* This is safe: open() is still holding a reference. */
2400 module_put(THIS_MODULE);
2401 return ret;
2404 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2406 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2408 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2409 BUG_ON(!pd);
2411 switch (cmd) {
2413 * forward selected CDROM ioctls to CD-ROM, for UDF
2415 case CDROMMULTISESSION:
2416 case CDROMREADTOCENTRY:
2417 case CDROM_LAST_WRITTEN:
2418 case CDROM_SEND_PACKET:
2419 case SCSI_IOCTL_SEND_COMMAND:
2420 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2422 case CDROMEJECT:
2424 * The door gets locked when the device is opened, so we
2425 * have to unlock it or else the eject command fails.
2427 pkt_lock_door(pd, 0);
2428 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2430 default:
2431 printk("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
2432 return -ENOTTY;
2435 return 0;
2438 static int pkt_media_changed(struct gendisk *disk)
2440 struct pktcdvd_device *pd = disk->private_data;
2441 struct gendisk *attached_disk;
2443 if (!pd)
2444 return 0;
2445 if (!pd->bdev)
2446 return 0;
2447 attached_disk = pd->bdev->bd_disk;
2448 if (!attached_disk)
2449 return 0;
2450 return attached_disk->fops->media_changed(attached_disk);
2453 static struct block_device_operations pktcdvd_ops = {
2454 .owner = THIS_MODULE,
2455 .open = pkt_open,
2456 .release = pkt_close,
2457 .ioctl = pkt_ioctl,
2458 .media_changed = pkt_media_changed,
2462 * Set up mapping from pktcdvd device to CD-ROM device.
2464 static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2466 int idx;
2467 int ret = -ENOMEM;
2468 struct pktcdvd_device *pd;
2469 struct gendisk *disk;
2470 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2472 for (idx = 0; idx < MAX_WRITERS; idx++)
2473 if (!pkt_devs[idx])
2474 break;
2475 if (idx == MAX_WRITERS) {
2476 printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
2477 return -EBUSY;
2480 pd = kmalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2481 if (!pd)
2482 return ret;
2483 memset(pd, 0, sizeof(struct pktcdvd_device));
2485 pd->rb_pool = mempool_create(PKT_RB_POOL_SIZE, pkt_rb_alloc, pkt_rb_free, NULL);
2486 if (!pd->rb_pool)
2487 goto out_mem;
2489 disk = alloc_disk(1);
2490 if (!disk)
2491 goto out_mem;
2492 pd->disk = disk;
2494 spin_lock_init(&pd->lock);
2495 spin_lock_init(&pd->iosched.lock);
2496 sprintf(pd->name, "pktcdvd%d", idx);
2497 init_waitqueue_head(&pd->wqueue);
2498 pd->bio_queue = RB_ROOT;
2500 disk->major = pkt_major;
2501 disk->first_minor = idx;
2502 disk->fops = &pktcdvd_ops;
2503 disk->flags = GENHD_FL_REMOVABLE;
2504 sprintf(disk->disk_name, "pktcdvd%d", idx);
2505 disk->private_data = pd;
2506 disk->queue = blk_alloc_queue(GFP_KERNEL);
2507 if (!disk->queue)
2508 goto out_mem2;
2510 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2511 ret = pkt_new_dev(pd, dev);
2512 if (ret)
2513 goto out_new_dev;
2515 add_disk(disk);
2516 pkt_devs[idx] = pd;
2517 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2518 return 0;
2520 out_new_dev:
2521 blk_put_queue(disk->queue);
2522 out_mem2:
2523 put_disk(disk);
2524 out_mem:
2525 if (pd->rb_pool)
2526 mempool_destroy(pd->rb_pool);
2527 kfree(pd);
2528 return ret;
2532 * Tear down mapping from pktcdvd device to CD-ROM device.
2534 static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2536 struct pktcdvd_device *pd;
2537 int idx;
2538 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2540 for (idx = 0; idx < MAX_WRITERS; idx++) {
2541 pd = pkt_devs[idx];
2542 if (pd && (pd->pkt_dev == pkt_dev))
2543 break;
2545 if (idx == MAX_WRITERS) {
2546 DPRINTK("pktcdvd: dev not setup\n");
2547 return -ENXIO;
2550 if (pd->refcnt > 0)
2551 return -EBUSY;
2553 if (!IS_ERR(pd->cdrw.thread))
2554 kthread_stop(pd->cdrw.thread);
2556 blkdev_put(pd->bdev);
2558 pkt_shrink_pktlist(pd);
2560 remove_proc_entry(pd->name, pkt_proc);
2561 DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
2563 del_gendisk(pd->disk);
2564 blk_put_queue(pd->disk->queue);
2565 put_disk(pd->disk);
2567 pkt_devs[idx] = NULL;
2568 mempool_destroy(pd->rb_pool);
2569 kfree(pd);
2571 /* This is safe: open() is still holding a reference. */
2572 module_put(THIS_MODULE);
2573 return 0;
2576 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2578 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2579 if (pd) {
2580 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2581 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2582 } else {
2583 ctrl_cmd->dev = 0;
2584 ctrl_cmd->pkt_dev = 0;
2586 ctrl_cmd->num_devices = MAX_WRITERS;
2589 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2591 void __user *argp = (void __user *)arg;
2592 struct pkt_ctrl_command ctrl_cmd;
2593 int ret = 0;
2595 if (cmd != PACKET_CTRL_CMD)
2596 return -ENOTTY;
2598 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2599 return -EFAULT;
2601 switch (ctrl_cmd.command) {
2602 case PKT_CTRL_CMD_SETUP:
2603 if (!capable(CAP_SYS_ADMIN))
2604 return -EPERM;
2605 down(&ctl_mutex);
2606 ret = pkt_setup_dev(&ctrl_cmd);
2607 up(&ctl_mutex);
2608 break;
2609 case PKT_CTRL_CMD_TEARDOWN:
2610 if (!capable(CAP_SYS_ADMIN))
2611 return -EPERM;
2612 down(&ctl_mutex);
2613 ret = pkt_remove_dev(&ctrl_cmd);
2614 up(&ctl_mutex);
2615 break;
2616 case PKT_CTRL_CMD_STATUS:
2617 down(&ctl_mutex);
2618 pkt_get_status(&ctrl_cmd);
2619 up(&ctl_mutex);
2620 break;
2621 default:
2622 return -ENOTTY;
2625 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2626 return -EFAULT;
2627 return ret;
2631 static struct file_operations pkt_ctl_fops = {
2632 .ioctl = pkt_ctl_ioctl,
2633 .owner = THIS_MODULE,
2636 static struct miscdevice pkt_misc = {
2637 .minor = MISC_DYNAMIC_MINOR,
2638 .name = "pktcdvd",
2639 .devfs_name = "pktcdvd/control",
2640 .fops = &pkt_ctl_fops
2643 static int __init pkt_init(void)
2645 int ret;
2647 psd_pool = mempool_create(PSD_POOL_SIZE, psd_pool_alloc, psd_pool_free, NULL);
2648 if (!psd_pool)
2649 return -ENOMEM;
2651 ret = register_blkdev(pkt_major, "pktcdvd");
2652 if (ret < 0) {
2653 printk("pktcdvd: Unable to register block device\n");
2654 goto out2;
2656 if (!pkt_major)
2657 pkt_major = ret;
2659 ret = misc_register(&pkt_misc);
2660 if (ret) {
2661 printk("pktcdvd: Unable to register misc device\n");
2662 goto out;
2665 init_MUTEX(&ctl_mutex);
2667 pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
2669 DPRINTK("pktcdvd: %s\n", VERSION_CODE);
2670 return 0;
2672 out:
2673 unregister_blkdev(pkt_major, "pktcdvd");
2674 out2:
2675 mempool_destroy(psd_pool);
2676 return ret;
2679 static void __exit pkt_exit(void)
2681 remove_proc_entry("pktcdvd", proc_root_driver);
2682 misc_deregister(&pkt_misc);
2683 unregister_blkdev(pkt_major, "pktcdvd");
2684 mempool_destroy(psd_pool);
2687 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2688 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2689 MODULE_LICENSE("GPL");
2691 module_init(pkt_init);
2692 module_exit(pkt_exit);