blob: 109db859112a6f7f0482aa7b44e1d4e95275069b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
4 *
5 * May be copied or modified under the terms of the GNU General Public
6 * License. See linux/COPYING for more information.
7 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -07008 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
9 * DVD-RAM devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Theory of operation:
12 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070013 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070022 * 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.
25 *
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.
32 *
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.
37 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 *************************************************************************/
45
46#define VERSION_CODE "v0.2.0a 2004-07-14 Jens Axboe (axboe@suse.de) and petero2@telia.com"
47
48#include <linux/pktcdvd.h>
49#include <linux/config.h>
50#include <linux/module.h>
51#include <linux/types.h>
52#include <linux/kernel.h>
53#include <linux/kthread.h>
54#include <linux/errno.h>
55#include <linux/spinlock.h>
56#include <linux/file.h>
57#include <linux/proc_fs.h>
58#include <linux/seq_file.h>
59#include <linux/miscdevice.h>
60#include <linux/suspend.h>
61#include <scsi/scsi_cmnd.h>
62#include <scsi/scsi_ioctl.h>
63
64#include <asm/uaccess.h>
65
66#if PACKET_DEBUG
67#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
68#else
69#define DPRINTK(fmt, args...)
70#endif
71
72#if PACKET_DEBUG > 1
73#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
74#else
75#define VPRINTK(fmt, args...)
76#endif
77
78#define MAX_SPEED 0xffff
79
80#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
81
82static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
83static struct proc_dir_entry *pkt_proc;
84static int pkt_major;
85static struct semaphore ctl_mutex; /* Serialize open/close/setup/teardown */
86static mempool_t *psd_pool;
87
88
89static void pkt_bio_finished(struct pktcdvd_device *pd)
90{
91 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
92 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
93 VPRINTK("pktcdvd: queue empty\n");
94 atomic_set(&pd->iosched.attention, 1);
95 wake_up(&pd->wqueue);
96 }
97}
98
99static void pkt_bio_destructor(struct bio *bio)
100{
101 kfree(bio->bi_io_vec);
102 kfree(bio);
103}
104
105static struct bio *pkt_bio_alloc(int nr_iovecs)
106{
107 struct bio_vec *bvl = NULL;
108 struct bio *bio;
109
110 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
111 if (!bio)
112 goto no_bio;
113 bio_init(bio);
114
115 bvl = kmalloc(nr_iovecs * sizeof(struct bio_vec), GFP_KERNEL);
116 if (!bvl)
117 goto no_bvl;
118 memset(bvl, 0, nr_iovecs * sizeof(struct bio_vec));
119
120 bio->bi_max_vecs = nr_iovecs;
121 bio->bi_io_vec = bvl;
122 bio->bi_destructor = pkt_bio_destructor;
123
124 return bio;
125
126 no_bvl:
127 kfree(bio);
128 no_bio:
129 return NULL;
130}
131
132/*
133 * Allocate a packet_data struct
134 */
135static struct packet_data *pkt_alloc_packet_data(void)
136{
137 int i;
138 struct packet_data *pkt;
139
140 pkt = kmalloc(sizeof(struct packet_data), GFP_KERNEL);
141 if (!pkt)
142 goto no_pkt;
143 memset(pkt, 0, sizeof(struct packet_data));
144
145 pkt->w_bio = pkt_bio_alloc(PACKET_MAX_SIZE);
146 if (!pkt->w_bio)
147 goto no_bio;
148
149 for (i = 0; i < PAGES_PER_PACKET; i++) {
150 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
151 if (!pkt->pages[i])
152 goto no_page;
153 }
154
155 spin_lock_init(&pkt->lock);
156
157 for (i = 0; i < PACKET_MAX_SIZE; i++) {
158 struct bio *bio = pkt_bio_alloc(1);
159 if (!bio)
160 goto no_rd_bio;
161 pkt->r_bios[i] = bio;
162 }
163
164 return pkt;
165
166no_rd_bio:
167 for (i = 0; i < PACKET_MAX_SIZE; i++) {
168 struct bio *bio = pkt->r_bios[i];
169 if (bio)
170 bio_put(bio);
171 }
172
173no_page:
174 for (i = 0; i < PAGES_PER_PACKET; i++)
175 if (pkt->pages[i])
176 __free_page(pkt->pages[i]);
177 bio_put(pkt->w_bio);
178no_bio:
179 kfree(pkt);
180no_pkt:
181 return NULL;
182}
183
184/*
185 * Free a packet_data struct
186 */
187static void pkt_free_packet_data(struct packet_data *pkt)
188{
189 int i;
190
191 for (i = 0; i < PACKET_MAX_SIZE; i++) {
192 struct bio *bio = pkt->r_bios[i];
193 if (bio)
194 bio_put(bio);
195 }
196 for (i = 0; i < PAGES_PER_PACKET; i++)
197 __free_page(pkt->pages[i]);
198 bio_put(pkt->w_bio);
199 kfree(pkt);
200}
201
202static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
203{
204 struct packet_data *pkt, *next;
205
206 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
207
208 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
209 pkt_free_packet_data(pkt);
210 }
211}
212
213static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
214{
215 struct packet_data *pkt;
216
217 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
218 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
219 spin_lock_init(&pd->cdrw.active_list_lock);
220 while (nr_packets > 0) {
221 pkt = pkt_alloc_packet_data();
222 if (!pkt) {
223 pkt_shrink_pktlist(pd);
224 return 0;
225 }
226 pkt->id = nr_packets;
227 pkt->pd = pd;
228 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
229 nr_packets--;
230 }
231 return 1;
232}
233
234static void *pkt_rb_alloc(unsigned int __nocast gfp_mask, void *data)
235{
236 return kmalloc(sizeof(struct pkt_rb_node), gfp_mask);
237}
238
239static void pkt_rb_free(void *ptr, void *data)
240{
241 kfree(ptr);
242}
243
244static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
245{
246 struct rb_node *n = rb_next(&node->rb_node);
247 if (!n)
248 return NULL;
249 return rb_entry(n, struct pkt_rb_node, rb_node);
250}
251
252static inline void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
253{
254 rb_erase(&node->rb_node, &pd->bio_queue);
255 mempool_free(node, pd->rb_pool);
256 pd->bio_queue_size--;
257 BUG_ON(pd->bio_queue_size < 0);
258}
259
260/*
261 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
262 */
263static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
264{
265 struct rb_node *n = pd->bio_queue.rb_node;
266 struct rb_node *next;
267 struct pkt_rb_node *tmp;
268
269 if (!n) {
270 BUG_ON(pd->bio_queue_size > 0);
271 return NULL;
272 }
273
274 for (;;) {
275 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
276 if (s <= tmp->bio->bi_sector)
277 next = n->rb_left;
278 else
279 next = n->rb_right;
280 if (!next)
281 break;
282 n = next;
283 }
284
285 if (s > tmp->bio->bi_sector) {
286 tmp = pkt_rbtree_next(tmp);
287 if (!tmp)
288 return NULL;
289 }
290 BUG_ON(s > tmp->bio->bi_sector);
291 return tmp;
292}
293
294/*
295 * Insert a node into the pd->bio_queue rb tree.
296 */
297static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
298{
299 struct rb_node **p = &pd->bio_queue.rb_node;
300 struct rb_node *parent = NULL;
301 sector_t s = node->bio->bi_sector;
302 struct pkt_rb_node *tmp;
303
304 while (*p) {
305 parent = *p;
306 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
307 if (s < tmp->bio->bi_sector)
308 p = &(*p)->rb_left;
309 else
310 p = &(*p)->rb_right;
311 }
312 rb_link_node(&node->rb_node, parent, p);
313 rb_insert_color(&node->rb_node, &pd->bio_queue);
314 pd->bio_queue_size++;
315}
316
317/*
318 * Add a bio to a single linked list defined by its head and tail pointers.
319 */
320static inline void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
321{
322 bio->bi_next = NULL;
323 if (*list_tail) {
324 BUG_ON((*list_head) == NULL);
325 (*list_tail)->bi_next = bio;
326 (*list_tail) = bio;
327 } else {
328 BUG_ON((*list_head) != NULL);
329 (*list_head) = bio;
330 (*list_tail) = bio;
331 }
332}
333
334/*
335 * Remove and return the first bio from a single linked list defined by its
336 * head and tail pointers.
337 */
338static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
339{
340 struct bio *bio;
341
342 if (*list_head == NULL)
343 return NULL;
344
345 bio = *list_head;
346 *list_head = bio->bi_next;
347 if (*list_head == NULL)
348 *list_tail = NULL;
349
350 bio->bi_next = NULL;
351 return bio;
352}
353
354/*
355 * Send a packet_command to the underlying block device and
356 * wait for completion.
357 */
358static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
359{
360 char sense[SCSI_SENSE_BUFFERSIZE];
361 request_queue_t *q;
362 struct request *rq;
363 DECLARE_COMPLETION(wait);
364 int err = 0;
365
366 q = bdev_get_queue(pd->bdev);
367
368 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
369 __GFP_WAIT);
370 rq->errors = 0;
371 rq->rq_disk = pd->bdev->bd_disk;
372 rq->bio = NULL;
373 rq->buffer = NULL;
374 rq->timeout = 60*HZ;
375 rq->data = cgc->buffer;
376 rq->data_len = cgc->buflen;
377 rq->sense = sense;
378 memset(sense, 0, sizeof(sense));
379 rq->sense_len = 0;
380 rq->flags |= REQ_BLOCK_PC | REQ_HARDBARRIER;
381 if (cgc->quiet)
382 rq->flags |= REQ_QUIET;
383 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
384 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
385 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
386
387 rq->ref_count++;
388 rq->flags |= REQ_NOMERGE;
389 rq->waiting = &wait;
390 rq->end_io = blk_end_sync_rq;
391 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
392 generic_unplug_device(q);
393 wait_for_completion(&wait);
394
395 if (rq->errors)
396 err = -EIO;
397
398 blk_put_request(rq);
399 return err;
400}
401
402/*
403 * A generic sense dump / resolve mechanism should be implemented across
404 * all ATAPI + SCSI devices.
405 */
406static void pkt_dump_sense(struct packet_command *cgc)
407{
408 static char *info[9] = { "No sense", "Recovered error", "Not ready",
409 "Medium error", "Hardware error", "Illegal request",
410 "Unit attention", "Data protect", "Blank check" };
411 int i;
412 struct request_sense *sense = cgc->sense;
413
414 printk("pktcdvd:");
415 for (i = 0; i < CDROM_PACKET_SIZE; i++)
416 printk(" %02x", cgc->cmd[i]);
417 printk(" - ");
418
419 if (sense == NULL) {
420 printk("no sense\n");
421 return;
422 }
423
424 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
425
426 if (sense->sense_key > 8) {
427 printk(" (INVALID)\n");
428 return;
429 }
430
431 printk(" (%s)\n", info[sense->sense_key]);
432}
433
434/*
435 * flush the drive cache to media
436 */
437static int pkt_flush_cache(struct pktcdvd_device *pd)
438{
439 struct packet_command cgc;
440
441 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
442 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
443 cgc.quiet = 1;
444
445 /*
446 * the IMMED bit -- we default to not setting it, although that
447 * would allow a much faster close, this is safer
448 */
449#if 0
450 cgc.cmd[1] = 1 << 1;
451#endif
452 return pkt_generic_packet(pd, &cgc);
453}
454
455/*
456 * speed is given as the normal factor, e.g. 4 for 4x
457 */
458static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
459{
460 struct packet_command cgc;
461 struct request_sense sense;
462 int ret;
463
464 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
465 cgc.sense = &sense;
466 cgc.cmd[0] = GPCMD_SET_SPEED;
467 cgc.cmd[2] = (read_speed >> 8) & 0xff;
468 cgc.cmd[3] = read_speed & 0xff;
469 cgc.cmd[4] = (write_speed >> 8) & 0xff;
470 cgc.cmd[5] = write_speed & 0xff;
471
472 if ((ret = pkt_generic_packet(pd, &cgc)))
473 pkt_dump_sense(&cgc);
474
475 return ret;
476}
477
478/*
479 * Queue a bio for processing by the low-level CD device. Must be called
480 * from process context.
481 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700482static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 spin_lock(&pd->iosched.lock);
485 if (bio_data_dir(bio) == READ) {
486 pkt_add_list_last(bio, &pd->iosched.read_queue,
487 &pd->iosched.read_queue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
489 pkt_add_list_last(bio, &pd->iosched.write_queue,
490 &pd->iosched.write_queue_tail);
491 }
492 spin_unlock(&pd->iosched.lock);
493
494 atomic_set(&pd->iosched.attention, 1);
495 wake_up(&pd->wqueue);
496}
497
498/*
499 * Process the queued read/write requests. This function handles special
500 * requirements for CDRW drives:
501 * - A cache flush command must be inserted before a read request if the
502 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700503 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700505 * - Optimize for throughput at the expense of latency. This means that streaming
506 * writes will never be interrupted by a read, but if the drive has to seek
507 * before the next write, switch to reading instead if there are any pending
508 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * - Set the read speed according to current usage pattern. When only reading
510 * from the device, it's best to use the highest possible read speed, but
511 * when switching often between reading and writing, it's better to have the
512 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
514static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
515{
516 request_queue_t *q;
517
518 if (atomic_read(&pd->iosched.attention) == 0)
519 return;
520 atomic_set(&pd->iosched.attention, 0);
521
522 q = bdev_get_queue(pd->bdev);
523
524 for (;;) {
525 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700526 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 spin_lock(&pd->iosched.lock);
529 reads_queued = (pd->iosched.read_queue != NULL);
530 writes_queued = (pd->iosched.write_queue != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 spin_unlock(&pd->iosched.lock);
532
533 if (!reads_queued && !writes_queued)
534 break;
535
536 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700537 int need_write_seek = 1;
538 spin_lock(&pd->iosched.lock);
539 bio = pd->iosched.write_queue;
540 spin_unlock(&pd->iosched.lock);
541 if (bio && (bio->bi_sector == pd->iosched.last_write))
542 need_write_seek = 0;
543 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
545 VPRINTK("pktcdvd: write, waiting\n");
546 break;
547 }
548 pkt_flush_cache(pd);
549 pd->iosched.writing = 0;
550 }
551 } else {
552 if (!reads_queued && writes_queued) {
553 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
554 VPRINTK("pktcdvd: read, waiting\n");
555 break;
556 }
557 pd->iosched.writing = 1;
558 }
559 }
560
561 spin_lock(&pd->iosched.lock);
562 if (pd->iosched.writing) {
563 bio = pkt_get_list_first(&pd->iosched.write_queue,
564 &pd->iosched.write_queue_tail);
565 } else {
566 bio = pkt_get_list_first(&pd->iosched.read_queue,
567 &pd->iosched.read_queue_tail);
568 }
569 spin_unlock(&pd->iosched.lock);
570
571 if (!bio)
572 continue;
573
574 if (bio_data_dir(bio) == READ)
575 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700576 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700578 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
581 if (pd->read_speed == pd->write_speed) {
582 pd->read_speed = MAX_SPEED;
583 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
584 }
585 } else {
586 if (pd->read_speed != pd->write_speed) {
587 pd->read_speed = pd->write_speed;
588 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
589 }
590 }
591
592 atomic_inc(&pd->cdrw.pending_bios);
593 generic_make_request(bio);
594 }
595}
596
597/*
598 * Special care is needed if the underlying block device has a small
599 * max_phys_segments value.
600 */
601static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
602{
603 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
604 /*
605 * The cdrom device can handle one segment/frame
606 */
607 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
608 return 0;
609 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
610 /*
611 * We can handle this case at the expense of some extra memory
612 * copies during write operations
613 */
614 set_bit(PACKET_MERGE_SEGS, &pd->flags);
615 return 0;
616 } else {
617 printk("pktcdvd: cdrom max_phys_segments too small\n");
618 return -EIO;
619 }
620}
621
622/*
623 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
624 */
625static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
626{
627 unsigned int copy_size = CD_FRAMESIZE;
628
629 while (copy_size > 0) {
630 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
631 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
632 src_bvl->bv_offset + offs;
633 void *vto = page_address(dst_page) + dst_offs;
634 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
635
636 BUG_ON(len < 0);
637 memcpy(vto, vfrom, len);
638 kunmap_atomic(vfrom, KM_USER0);
639
640 seg++;
641 offs = 0;
642 dst_offs += len;
643 copy_size -= len;
644 }
645}
646
647/*
648 * Copy all data for this packet to pkt->pages[], so that
649 * a) The number of required segments for the write bio is minimized, which
650 * is necessary for some scsi controllers.
651 * b) The data can be used as cache to avoid read requests if we receive a
652 * new write request for the same zone.
653 */
654static void pkt_make_local_copy(struct packet_data *pkt, struct page **pages, int *offsets)
655{
656 int f, p, offs;
657
658 /* Copy all data to pkt->pages[] */
659 p = 0;
660 offs = 0;
661 for (f = 0; f < pkt->frames; f++) {
662 if (pages[f] != pkt->pages[p]) {
663 void *vfrom = kmap_atomic(pages[f], KM_USER0) + offsets[f];
664 void *vto = page_address(pkt->pages[p]) + offs;
665 memcpy(vto, vfrom, CD_FRAMESIZE);
666 kunmap_atomic(vfrom, KM_USER0);
667 pages[f] = pkt->pages[p];
668 offsets[f] = offs;
669 } else {
670 BUG_ON(offsets[f] != offs);
671 }
672 offs += CD_FRAMESIZE;
673 if (offs >= PAGE_SIZE) {
674 BUG_ON(offs > PAGE_SIZE);
675 offs = 0;
676 p++;
677 }
678 }
679}
680
681static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
682{
683 struct packet_data *pkt = bio->bi_private;
684 struct pktcdvd_device *pd = pkt->pd;
685 BUG_ON(!pd);
686
687 if (bio->bi_size)
688 return 1;
689
690 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
691 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
692
693 if (err)
694 atomic_inc(&pkt->io_errors);
695 if (atomic_dec_and_test(&pkt->io_wait)) {
696 atomic_inc(&pkt->run_sm);
697 wake_up(&pd->wqueue);
698 }
699 pkt_bio_finished(pd);
700
701 return 0;
702}
703
704static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
705{
706 struct packet_data *pkt = bio->bi_private;
707 struct pktcdvd_device *pd = pkt->pd;
708 BUG_ON(!pd);
709
710 if (bio->bi_size)
711 return 1;
712
713 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
714
715 pd->stats.pkt_ended++;
716
717 pkt_bio_finished(pd);
718 atomic_dec(&pkt->io_wait);
719 atomic_inc(&pkt->run_sm);
720 wake_up(&pd->wqueue);
721 return 0;
722}
723
724/*
725 * Schedule reads for the holes in a packet
726 */
727static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
728{
729 int frames_read = 0;
730 struct bio *bio;
731 int f;
732 char written[PACKET_MAX_SIZE];
733
734 BUG_ON(!pkt->orig_bios);
735
736 atomic_set(&pkt->io_wait, 0);
737 atomic_set(&pkt->io_errors, 0);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
740 * Figure out which frames we need to read before we can write.
741 */
742 memset(written, 0, sizeof(written));
743 spin_lock(&pkt->lock);
744 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
745 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
746 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -0700747 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 BUG_ON(first_frame < 0);
749 BUG_ON(first_frame + num_frames > pkt->frames);
750 for (f = first_frame; f < first_frame + num_frames; f++)
751 written[f] = 1;
752 }
753 spin_unlock(&pkt->lock);
754
Peter Osterlund06e7ab52005-09-13 01:25:28 -0700755 if (pkt->cache_valid) {
756 VPRINTK("pkt_gather_data: zone %llx cached\n",
757 (unsigned long long)pkt->sector);
758 goto out_account;
759 }
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * Schedule reads for missing parts of the packet.
763 */
764 for (f = 0; f < pkt->frames; f++) {
765 int p, offset;
766 if (written[f])
767 continue;
768 bio = pkt->r_bios[f];
769 bio_init(bio);
770 bio->bi_max_vecs = 1;
771 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
772 bio->bi_bdev = pd->bdev;
773 bio->bi_end_io = pkt_end_io_read;
774 bio->bi_private = pkt;
775
776 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
777 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
778 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
779 f, pkt->pages[p], offset);
780 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
781 BUG();
782
783 atomic_inc(&pkt->io_wait);
784 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700785 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 frames_read++;
787 }
788
789out_account:
790 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
791 frames_read, (unsigned long long)pkt->sector);
792 pd->stats.pkt_started++;
793 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
797 * Find a packet matching zone, or the least recently used packet if
798 * there is no match.
799 */
800static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
801{
802 struct packet_data *pkt;
803
804 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
805 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
806 list_del_init(&pkt->list);
807 if (pkt->sector != zone)
808 pkt->cache_valid = 0;
809 break;
810 }
811 }
812 return pkt;
813}
814
815static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
816{
817 if (pkt->cache_valid) {
818 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
819 } else {
820 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
821 }
822}
823
824/*
825 * recover a failed write, query for relocation if possible
826 *
827 * returns 1 if recovery is possible, or 0 if not
828 *
829 */
830static int pkt_start_recovery(struct packet_data *pkt)
831{
832 /*
833 * FIXME. We need help from the file system to implement
834 * recovery handling.
835 */
836 return 0;
837#if 0
838 struct request *rq = pkt->rq;
839 struct pktcdvd_device *pd = rq->rq_disk->private_data;
840 struct block_device *pkt_bdev;
841 struct super_block *sb = NULL;
842 unsigned long old_block, new_block;
843 sector_t new_sector;
844
845 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
846 if (pkt_bdev) {
847 sb = get_super(pkt_bdev);
848 bdput(pkt_bdev);
849 }
850
851 if (!sb)
852 return 0;
853
854 if (!sb->s_op || !sb->s_op->relocate_blocks)
855 goto out;
856
857 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
858 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
859 goto out;
860
861 new_sector = new_block * (CD_FRAMESIZE >> 9);
862 pkt->sector = new_sector;
863
864 pkt->bio->bi_sector = new_sector;
865 pkt->bio->bi_next = NULL;
866 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
867 pkt->bio->bi_idx = 0;
868
869 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
870 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
871 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
872 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
873 BUG_ON(pkt->bio->bi_private != pkt);
874
875 drop_super(sb);
876 return 1;
877
878out:
879 drop_super(sb);
880 return 0;
881#endif
882}
883
884static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
885{
886#if PACKET_DEBUG > 1
887 static const char *state_name[] = {
888 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
889 };
890 enum packet_data_state old_state = pkt->state;
891 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
892 state_name[old_state], state_name[state]);
893#endif
894 pkt->state = state;
895}
896
897/*
898 * Scan the work queue to see if we can start a new packet.
899 * returns non-zero if any work was done.
900 */
901static int pkt_handle_queue(struct pktcdvd_device *pd)
902{
903 struct packet_data *pkt, *p;
904 struct bio *bio = NULL;
905 sector_t zone = 0; /* Suppress gcc warning */
906 struct pkt_rb_node *node, *first_node;
907 struct rb_node *n;
908
909 VPRINTK("handle_queue\n");
910
911 atomic_set(&pd->scan_queue, 0);
912
913 if (list_empty(&pd->cdrw.pkt_free_list)) {
914 VPRINTK("handle_queue: no pkt\n");
915 return 0;
916 }
917
918 /*
919 * Try to find a zone we are not already working on.
920 */
921 spin_lock(&pd->lock);
922 first_node = pkt_rbtree_find(pd, pd->current_sector);
923 if (!first_node) {
924 n = rb_first(&pd->bio_queue);
925 if (n)
926 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
927 }
928 node = first_node;
929 while (node) {
930 bio = node->bio;
931 zone = ZONE(bio->bi_sector, pd);
932 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -0700933 if (p->sector == zone) {
934 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -0700936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 break;
939try_next_bio:
940 node = pkt_rbtree_next(node);
941 if (!node) {
942 n = rb_first(&pd->bio_queue);
943 if (n)
944 node = rb_entry(n, struct pkt_rb_node, rb_node);
945 }
946 if (node == first_node)
947 node = NULL;
948 }
949 spin_unlock(&pd->lock);
950 if (!bio) {
951 VPRINTK("handle_queue: no bio\n");
952 return 0;
953 }
954
955 pkt = pkt_get_packet_data(pd, zone);
956 BUG_ON(!pkt);
957
958 pd->current_sector = zone + pd->settings.size;
959 pkt->sector = zone;
960 pkt->frames = pd->settings.size >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 pkt->write_size = 0;
962
963 /*
964 * Scan work queue for bios in the same zone and link them
965 * to this packet.
966 */
967 spin_lock(&pd->lock);
968 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
969 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
970 bio = node->bio;
971 VPRINTK("pkt_handle_queue: found zone=%llx\n",
972 (unsigned long long)ZONE(bio->bi_sector, pd));
973 if (ZONE(bio->bi_sector, pd) != zone)
974 break;
975 pkt_rbtree_erase(pd, node);
976 spin_lock(&pkt->lock);
977 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
978 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
979 spin_unlock(&pkt->lock);
980 }
981 spin_unlock(&pd->lock);
982
983 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
984 pkt_set_state(pkt, PACKET_WAITING_STATE);
985 atomic_set(&pkt->run_sm, 1);
986
987 spin_lock(&pd->cdrw.active_list_lock);
988 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
989 spin_unlock(&pd->cdrw.active_list_lock);
990
991 return 1;
992}
993
994/*
995 * Assemble a bio to write one packet and queue the bio for processing
996 * by the underlying block device.
997 */
998static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
999{
1000 struct bio *bio;
1001 struct page *pages[PACKET_MAX_SIZE];
1002 int offsets[PACKET_MAX_SIZE];
1003 int f;
1004 int frames_write;
1005
1006 for (f = 0; f < pkt->frames; f++) {
1007 pages[f] = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1008 offsets[f] = (f * CD_FRAMESIZE) % PAGE_SIZE;
1009 }
1010
1011 /*
1012 * Fill-in pages[] and offsets[] with data from orig_bios.
1013 */
1014 frames_write = 0;
1015 spin_lock(&pkt->lock);
1016 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1017 int segment = bio->bi_idx;
1018 int src_offs = 0;
1019 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1020 int num_frames = bio->bi_size / CD_FRAMESIZE;
1021 BUG_ON(first_frame < 0);
1022 BUG_ON(first_frame + num_frames > pkt->frames);
1023 for (f = first_frame; f < first_frame + num_frames; f++) {
1024 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1025
1026 while (src_offs >= src_bvl->bv_len) {
1027 src_offs -= src_bvl->bv_len;
1028 segment++;
1029 BUG_ON(segment >= bio->bi_vcnt);
1030 src_bvl = bio_iovec_idx(bio, segment);
1031 }
1032
1033 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1034 pages[f] = src_bvl->bv_page;
1035 offsets[f] = src_bvl->bv_offset + src_offs;
1036 } else {
1037 pkt_copy_bio_data(bio, segment, src_offs,
1038 pages[f], offsets[f]);
1039 }
1040 src_offs += CD_FRAMESIZE;
1041 frames_write++;
1042 }
1043 }
1044 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1045 spin_unlock(&pkt->lock);
1046
1047 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1048 frames_write, (unsigned long long)pkt->sector);
1049 BUG_ON(frames_write != pkt->write_size);
1050
1051 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1052 pkt_make_local_copy(pkt, pages, offsets);
1053 pkt->cache_valid = 1;
1054 } else {
1055 pkt->cache_valid = 0;
1056 }
1057
1058 /* Start the write request */
1059 bio_init(pkt->w_bio);
1060 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1061 pkt->w_bio->bi_sector = pkt->sector;
1062 pkt->w_bio->bi_bdev = pd->bdev;
1063 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1064 pkt->w_bio->bi_private = pkt;
1065 for (f = 0; f < pkt->frames; f++) {
1066 if ((f + 1 < pkt->frames) && (pages[f + 1] == pages[f]) &&
1067 (offsets[f + 1] = offsets[f] + CD_FRAMESIZE)) {
1068 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE * 2, offsets[f]))
1069 BUG();
1070 f++;
1071 } else {
1072 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE, offsets[f]))
1073 BUG();
1074 }
1075 }
1076 VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
1077
1078 atomic_set(&pkt->io_wait, 1);
1079 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001080 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1084{
1085 struct bio *bio, *next;
1086
1087 if (!uptodate)
1088 pkt->cache_valid = 0;
1089
1090 /* Finish all bios corresponding to this packet */
1091 bio = pkt->orig_bios;
1092 while (bio) {
1093 next = bio->bi_next;
1094 bio->bi_next = NULL;
1095 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1096 bio = next;
1097 }
1098 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1099}
1100
1101static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1102{
1103 int uptodate;
1104
1105 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1106
1107 for (;;) {
1108 switch (pkt->state) {
1109 case PACKET_WAITING_STATE:
1110 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1111 return;
1112
1113 pkt->sleep_time = 0;
1114 pkt_gather_data(pd, pkt);
1115 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1116 break;
1117
1118 case PACKET_READ_WAIT_STATE:
1119 if (atomic_read(&pkt->io_wait) > 0)
1120 return;
1121
1122 if (atomic_read(&pkt->io_errors) > 0) {
1123 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1124 } else {
1125 pkt_start_write(pd, pkt);
1126 }
1127 break;
1128
1129 case PACKET_WRITE_WAIT_STATE:
1130 if (atomic_read(&pkt->io_wait) > 0)
1131 return;
1132
1133 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1134 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1135 } else {
1136 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1137 }
1138 break;
1139
1140 case PACKET_RECOVERY_STATE:
1141 if (pkt_start_recovery(pkt)) {
1142 pkt_start_write(pd, pkt);
1143 } else {
1144 VPRINTK("No recovery possible\n");
1145 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1146 }
1147 break;
1148
1149 case PACKET_FINISHED_STATE:
1150 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1151 pkt_finish_packet(pkt, uptodate);
1152 return;
1153
1154 default:
1155 BUG();
1156 break;
1157 }
1158 }
1159}
1160
1161static void pkt_handle_packets(struct pktcdvd_device *pd)
1162{
1163 struct packet_data *pkt, *next;
1164
1165 VPRINTK("pkt_handle_packets\n");
1166
1167 /*
1168 * Run state machine for active packets
1169 */
1170 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1171 if (atomic_read(&pkt->run_sm) > 0) {
1172 atomic_set(&pkt->run_sm, 0);
1173 pkt_run_state_machine(pd, pkt);
1174 }
1175 }
1176
1177 /*
1178 * Move no longer active packets to the free list
1179 */
1180 spin_lock(&pd->cdrw.active_list_lock);
1181 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1182 if (pkt->state == PACKET_FINISHED_STATE) {
1183 list_del(&pkt->list);
1184 pkt_put_packet_data(pd, pkt);
1185 pkt_set_state(pkt, PACKET_IDLE_STATE);
1186 atomic_set(&pd->scan_queue, 1);
1187 }
1188 }
1189 spin_unlock(&pd->cdrw.active_list_lock);
1190}
1191
1192static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1193{
1194 struct packet_data *pkt;
1195 int i;
1196
1197 for (i = 0; i <= PACKET_NUM_STATES; i++)
1198 states[i] = 0;
1199
1200 spin_lock(&pd->cdrw.active_list_lock);
1201 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1202 states[pkt->state]++;
1203 }
1204 spin_unlock(&pd->cdrw.active_list_lock);
1205}
1206
1207/*
1208 * kcdrwd is woken up when writes have been queued for one of our
1209 * registered devices
1210 */
1211static int kcdrwd(void *foobar)
1212{
1213 struct pktcdvd_device *pd = foobar;
1214 struct packet_data *pkt;
1215 long min_sleep_time, residue;
1216
1217 set_user_nice(current, -20);
1218
1219 for (;;) {
1220 DECLARE_WAITQUEUE(wait, current);
1221
1222 /*
1223 * Wait until there is something to do
1224 */
1225 add_wait_queue(&pd->wqueue, &wait);
1226 for (;;) {
1227 set_current_state(TASK_INTERRUPTIBLE);
1228
1229 /* Check if we need to run pkt_handle_queue */
1230 if (atomic_read(&pd->scan_queue) > 0)
1231 goto work_to_do;
1232
1233 /* Check if we need to run the state machine for some packet */
1234 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1235 if (atomic_read(&pkt->run_sm) > 0)
1236 goto work_to_do;
1237 }
1238
1239 /* Check if we need to process the iosched queues */
1240 if (atomic_read(&pd->iosched.attention) != 0)
1241 goto work_to_do;
1242
1243 /* Otherwise, go to sleep */
1244 if (PACKET_DEBUG > 1) {
1245 int states[PACKET_NUM_STATES];
1246 pkt_count_states(pd, states);
1247 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1248 states[0], states[1], states[2], states[3],
1249 states[4], states[5]);
1250 }
1251
1252 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1253 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1254 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1255 min_sleep_time = pkt->sleep_time;
1256 }
1257
1258 generic_unplug_device(bdev_get_queue(pd->bdev));
1259
1260 VPRINTK("kcdrwd: sleeping\n");
1261 residue = schedule_timeout(min_sleep_time);
1262 VPRINTK("kcdrwd: wake up\n");
1263
1264 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001265 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1268 if (!pkt->sleep_time)
1269 continue;
1270 pkt->sleep_time -= min_sleep_time - residue;
1271 if (pkt->sleep_time <= 0) {
1272 pkt->sleep_time = 0;
1273 atomic_inc(&pkt->run_sm);
1274 }
1275 }
1276
1277 if (signal_pending(current)) {
1278 flush_signals(current);
1279 }
1280 if (kthread_should_stop())
1281 break;
1282 }
1283work_to_do:
1284 set_current_state(TASK_RUNNING);
1285 remove_wait_queue(&pd->wqueue, &wait);
1286
1287 if (kthread_should_stop())
1288 break;
1289
1290 /*
1291 * if pkt_handle_queue returns true, we can queue
1292 * another request.
1293 */
1294 while (pkt_handle_queue(pd))
1295 ;
1296
1297 /*
1298 * Handle packet state machine
1299 */
1300 pkt_handle_packets(pd);
1301
1302 /*
1303 * Handle iosched queues
1304 */
1305 pkt_iosched_process_queue(pd);
1306 }
1307
1308 return 0;
1309}
1310
1311static void pkt_print_settings(struct pktcdvd_device *pd)
1312{
1313 printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1314 printk("%u blocks, ", pd->settings.size >> 2);
1315 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1316}
1317
1318static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1319{
1320 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1321
1322 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1323 cgc->cmd[2] = page_code | (page_control << 6);
1324 cgc->cmd[7] = cgc->buflen >> 8;
1325 cgc->cmd[8] = cgc->buflen & 0xff;
1326 cgc->data_direction = CGC_DATA_READ;
1327 return pkt_generic_packet(pd, cgc);
1328}
1329
1330static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1331{
1332 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1333 memset(cgc->buffer, 0, 2);
1334 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1335 cgc->cmd[1] = 0x10; /* PF */
1336 cgc->cmd[7] = cgc->buflen >> 8;
1337 cgc->cmd[8] = cgc->buflen & 0xff;
1338 cgc->data_direction = CGC_DATA_WRITE;
1339 return pkt_generic_packet(pd, cgc);
1340}
1341
1342static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1343{
1344 struct packet_command cgc;
1345 int ret;
1346
1347 /* set up command and get the disc info */
1348 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1349 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1350 cgc.cmd[8] = cgc.buflen = 2;
1351 cgc.quiet = 1;
1352
1353 if ((ret = pkt_generic_packet(pd, &cgc)))
1354 return ret;
1355
1356 /* not all drives have the same disc_info length, so requeue
1357 * packet with the length the drive tells us it can supply
1358 */
1359 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1360 sizeof(di->disc_information_length);
1361
1362 if (cgc.buflen > sizeof(disc_information))
1363 cgc.buflen = sizeof(disc_information);
1364
1365 cgc.cmd[8] = cgc.buflen;
1366 return pkt_generic_packet(pd, &cgc);
1367}
1368
1369static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1370{
1371 struct packet_command cgc;
1372 int ret;
1373
1374 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1375 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1376 cgc.cmd[1] = type & 3;
1377 cgc.cmd[4] = (track & 0xff00) >> 8;
1378 cgc.cmd[5] = track & 0xff;
1379 cgc.cmd[8] = 8;
1380 cgc.quiet = 1;
1381
1382 if ((ret = pkt_generic_packet(pd, &cgc)))
1383 return ret;
1384
1385 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1386 sizeof(ti->track_information_length);
1387
1388 if (cgc.buflen > sizeof(track_information))
1389 cgc.buflen = sizeof(track_information);
1390
1391 cgc.cmd[8] = cgc.buflen;
1392 return pkt_generic_packet(pd, &cgc);
1393}
1394
1395static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1396{
1397 disc_information di;
1398 track_information ti;
1399 __u32 last_track;
1400 int ret = -1;
1401
1402 if ((ret = pkt_get_disc_info(pd, &di)))
1403 return ret;
1404
1405 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1406 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1407 return ret;
1408
1409 /* if this track is blank, try the previous. */
1410 if (ti.blank) {
1411 last_track--;
1412 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1413 return ret;
1414 }
1415
1416 /* if last recorded field is valid, return it. */
1417 if (ti.lra_v) {
1418 *last_written = be32_to_cpu(ti.last_rec_address);
1419 } else {
1420 /* make it up instead */
1421 *last_written = be32_to_cpu(ti.track_start) +
1422 be32_to_cpu(ti.track_size);
1423 if (ti.free_blocks)
1424 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1425 }
1426 return 0;
1427}
1428
1429/*
1430 * write mode select package based on pd->settings
1431 */
1432static int pkt_set_write_settings(struct pktcdvd_device *pd)
1433{
1434 struct packet_command cgc;
1435 struct request_sense sense;
1436 write_param_page *wp;
1437 char buffer[128];
1438 int ret, size;
1439
1440 /* doesn't apply to DVD+RW or DVD-RAM */
1441 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1442 return 0;
1443
1444 memset(buffer, 0, sizeof(buffer));
1445 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1446 cgc.sense = &sense;
1447 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1448 pkt_dump_sense(&cgc);
1449 return ret;
1450 }
1451
1452 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1453 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1454 if (size > sizeof(buffer))
1455 size = sizeof(buffer);
1456
1457 /*
1458 * now get it all
1459 */
1460 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1461 cgc.sense = &sense;
1462 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1463 pkt_dump_sense(&cgc);
1464 return ret;
1465 }
1466
1467 /*
1468 * write page is offset header + block descriptor length
1469 */
1470 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1471
1472 wp->fp = pd->settings.fp;
1473 wp->track_mode = pd->settings.track_mode;
1474 wp->write_type = pd->settings.write_type;
1475 wp->data_block_type = pd->settings.block_mode;
1476
1477 wp->multi_session = 0;
1478
1479#ifdef PACKET_USE_LS
1480 wp->link_size = 7;
1481 wp->ls_v = 1;
1482#endif
1483
1484 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1485 wp->session_format = 0;
1486 wp->subhdr2 = 0x20;
1487 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1488 wp->session_format = 0x20;
1489 wp->subhdr2 = 8;
1490#if 0
1491 wp->mcn[0] = 0x80;
1492 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1493#endif
1494 } else {
1495 /*
1496 * paranoia
1497 */
1498 printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
1499 return 1;
1500 }
1501 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1502
1503 cgc.buflen = cgc.cmd[8] = size;
1504 if ((ret = pkt_mode_select(pd, &cgc))) {
1505 pkt_dump_sense(&cgc);
1506 return ret;
1507 }
1508
1509 pkt_print_settings(pd);
1510 return 0;
1511}
1512
1513/*
1514 * 0 -- we can write to this track, 1 -- we can't
1515 */
1516static int pkt_good_track(track_information *ti)
1517{
1518 /*
1519 * only good for CD-RW at the moment, not DVD-RW
1520 */
1521
1522 /*
1523 * FIXME: only for FP
1524 */
1525 if (ti->fp == 0)
1526 return 0;
1527
1528 /*
1529 * "good" settings as per Mt Fuji.
1530 */
1531 if (ti->rt == 0 && ti->blank == 0 && ti->packet == 1)
1532 return 0;
1533
1534 if (ti->rt == 0 && ti->blank == 1 && ti->packet == 1)
1535 return 0;
1536
1537 if (ti->rt == 1 && ti->blank == 0 && ti->packet == 1)
1538 return 0;
1539
1540 printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1541 return 1;
1542}
1543
1544/*
1545 * 0 -- we can write to this disc, 1 -- we can't
1546 */
1547static int pkt_good_disc(struct pktcdvd_device *pd, disc_information *di)
1548{
1549 switch (pd->mmc3_profile) {
1550 case 0x0a: /* CD-RW */
1551 case 0xffff: /* MMC3 not supported */
1552 break;
1553 case 0x1a: /* DVD+RW */
1554 case 0x13: /* DVD-RW */
1555 case 0x12: /* DVD-RAM */
1556 return 0;
1557 default:
1558 printk("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
1559 return 1;
1560 }
1561
1562 /*
1563 * for disc type 0xff we should probably reserve a new track.
1564 * but i'm not sure, should we leave this to user apps? probably.
1565 */
1566 if (di->disc_type == 0xff) {
1567 printk("pktcdvd: Unknown disc. No track?\n");
1568 return 1;
1569 }
1570
1571 if (di->disc_type != 0x20 && di->disc_type != 0) {
1572 printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
1573 return 1;
1574 }
1575
1576 if (di->erasable == 0) {
1577 printk("pktcdvd: Disc not erasable\n");
1578 return 1;
1579 }
1580
1581 if (di->border_status == PACKET_SESSION_RESERVED) {
1582 printk("pktcdvd: Can't write to last track (reserved)\n");
1583 return 1;
1584 }
1585
1586 return 0;
1587}
1588
1589static int pkt_probe_settings(struct pktcdvd_device *pd)
1590{
1591 struct packet_command cgc;
1592 unsigned char buf[12];
1593 disc_information di;
1594 track_information ti;
1595 int ret, track;
1596
1597 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1598 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1599 cgc.cmd[8] = 8;
1600 ret = pkt_generic_packet(pd, &cgc);
1601 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1602
1603 memset(&di, 0, sizeof(disc_information));
1604 memset(&ti, 0, sizeof(track_information));
1605
1606 if ((ret = pkt_get_disc_info(pd, &di))) {
1607 printk("failed get_disc\n");
1608 return ret;
1609 }
1610
1611 if (pkt_good_disc(pd, &di))
1612 return -ENXIO;
1613
1614 switch (pd->mmc3_profile) {
1615 case 0x1a: /* DVD+RW */
1616 printk("pktcdvd: inserted media is DVD+RW\n");
1617 break;
1618 case 0x13: /* DVD-RW */
1619 printk("pktcdvd: inserted media is DVD-RW\n");
1620 break;
1621 case 0x12: /* DVD-RAM */
1622 printk("pktcdvd: inserted media is DVD-RAM\n");
1623 break;
1624 default:
1625 printk("pktcdvd: inserted media is CD-R%s\n", di.erasable ? "W" : "");
1626 break;
1627 }
1628 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1629
1630 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1631 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1632 printk("pktcdvd: failed get_track\n");
1633 return ret;
1634 }
1635
1636 if (pkt_good_track(&ti)) {
1637 printk("pktcdvd: can't write to this track\n");
1638 return -ENXIO;
1639 }
1640
1641 /*
1642 * we keep packet size in 512 byte units, makes it easier to
1643 * deal with request calculations.
1644 */
1645 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1646 if (pd->settings.size == 0) {
1647 printk("pktcdvd: detected zero packet size!\n");
1648 pd->settings.size = 128;
1649 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07001650 if (pd->settings.size > PACKET_MAX_SECTORS) {
1651 printk("pktcdvd: packet size is too big\n");
1652 return -ENXIO;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 pd->settings.fp = ti.fp;
1655 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1656
1657 if (ti.nwa_v) {
1658 pd->nwa = be32_to_cpu(ti.next_writable);
1659 set_bit(PACKET_NWA_VALID, &pd->flags);
1660 }
1661
1662 /*
1663 * in theory we could use lra on -RW media as well and just zero
1664 * blocks that haven't been written yet, but in practice that
1665 * is just a no-go. we'll use that for -R, naturally.
1666 */
1667 if (ti.lra_v) {
1668 pd->lra = be32_to_cpu(ti.last_rec_address);
1669 set_bit(PACKET_LRA_VALID, &pd->flags);
1670 } else {
1671 pd->lra = 0xffffffff;
1672 set_bit(PACKET_LRA_VALID, &pd->flags);
1673 }
1674
1675 /*
1676 * fine for now
1677 */
1678 pd->settings.link_loss = 7;
1679 pd->settings.write_type = 0; /* packet */
1680 pd->settings.track_mode = ti.track_mode;
1681
1682 /*
1683 * mode1 or mode2 disc
1684 */
1685 switch (ti.data_mode) {
1686 case PACKET_MODE1:
1687 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1688 break;
1689 case PACKET_MODE2:
1690 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1691 break;
1692 default:
1693 printk("pktcdvd: unknown data mode\n");
1694 return 1;
1695 }
1696 return 0;
1697}
1698
1699/*
1700 * enable/disable write caching on drive
1701 */
1702static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1703{
1704 struct packet_command cgc;
1705 struct request_sense sense;
1706 unsigned char buf[64];
1707 int ret;
1708
1709 memset(buf, 0, sizeof(buf));
1710 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1711 cgc.sense = &sense;
1712 cgc.buflen = pd->mode_offset + 12;
1713
1714 /*
1715 * caching mode page might not be there, so quiet this command
1716 */
1717 cgc.quiet = 1;
1718
1719 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1720 return ret;
1721
1722 buf[pd->mode_offset + 10] |= (!!set << 2);
1723
1724 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1725 ret = pkt_mode_select(pd, &cgc);
1726 if (ret) {
1727 printk("pktcdvd: write caching control failed\n");
1728 pkt_dump_sense(&cgc);
1729 } else if (!ret && set)
1730 printk("pktcdvd: enabled write caching on %s\n", pd->name);
1731 return ret;
1732}
1733
1734static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1735{
1736 struct packet_command cgc;
1737
1738 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1739 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1740 cgc.cmd[4] = lockflag ? 1 : 0;
1741 return pkt_generic_packet(pd, &cgc);
1742}
1743
1744/*
1745 * Returns drive maximum write speed
1746 */
1747static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1748{
1749 struct packet_command cgc;
1750 struct request_sense sense;
1751 unsigned char buf[256+18];
1752 unsigned char *cap_buf;
1753 int ret, offset;
1754
1755 memset(buf, 0, sizeof(buf));
1756 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1757 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1758 cgc.sense = &sense;
1759
1760 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1761 if (ret) {
1762 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1763 sizeof(struct mode_page_header);
1764 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1765 if (ret) {
1766 pkt_dump_sense(&cgc);
1767 return ret;
1768 }
1769 }
1770
1771 offset = 20; /* Obsoleted field, used by older drives */
1772 if (cap_buf[1] >= 28)
1773 offset = 28; /* Current write speed selected */
1774 if (cap_buf[1] >= 30) {
1775 /* If the drive reports at least one "Logical Unit Write
1776 * Speed Performance Descriptor Block", use the information
1777 * in the first block. (contains the highest speed)
1778 */
1779 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1780 if (num_spdb > 0)
1781 offset = 34;
1782 }
1783
1784 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1785 return 0;
1786}
1787
1788/* These tables from cdrecord - I don't have orange book */
1789/* standard speed CD-RW (1-4x) */
1790static char clv_to_speed[16] = {
1791 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1792 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1793};
1794/* high speed CD-RW (-10x) */
1795static char hs_clv_to_speed[16] = {
1796 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1797 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1798};
1799/* ultra high speed CD-RW */
1800static char us_clv_to_speed[16] = {
1801 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1802 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1803};
1804
1805/*
1806 * reads the maximum media speed from ATIP
1807 */
1808static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1809{
1810 struct packet_command cgc;
1811 struct request_sense sense;
1812 unsigned char buf[64];
1813 unsigned int size, st, sp;
1814 int ret;
1815
1816 init_cdrom_command(&cgc, buf, 2, 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; /* READ ATIP */
1821 cgc.cmd[8] = 2;
1822 ret = pkt_generic_packet(pd, &cgc);
1823 if (ret) {
1824 pkt_dump_sense(&cgc);
1825 return ret;
1826 }
1827 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1828 if (size > sizeof(buf))
1829 size = sizeof(buf);
1830
1831 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1832 cgc.sense = &sense;
1833 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1834 cgc.cmd[1] = 2;
1835 cgc.cmd[2] = 4;
1836 cgc.cmd[8] = size;
1837 ret = pkt_generic_packet(pd, &cgc);
1838 if (ret) {
1839 pkt_dump_sense(&cgc);
1840 return ret;
1841 }
1842
1843 if (!buf[6] & 0x40) {
1844 printk("pktcdvd: Disc type is not CD-RW\n");
1845 return 1;
1846 }
1847 if (!buf[6] & 0x4) {
1848 printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
1849 return 1;
1850 }
1851
1852 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1853
1854 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1855
1856 /* Info from cdrecord */
1857 switch (st) {
1858 case 0: /* standard speed */
1859 *speed = clv_to_speed[sp];
1860 break;
1861 case 1: /* high speed */
1862 *speed = hs_clv_to_speed[sp];
1863 break;
1864 case 2: /* ultra high speed */
1865 *speed = us_clv_to_speed[sp];
1866 break;
1867 default:
1868 printk("pktcdvd: Unknown disc sub-type %d\n",st);
1869 return 1;
1870 }
1871 if (*speed) {
1872 printk("pktcdvd: Max. media speed: %d\n",*speed);
1873 return 0;
1874 } else {
1875 printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
1876 return 1;
1877 }
1878}
1879
1880static int pkt_perform_opc(struct pktcdvd_device *pd)
1881{
1882 struct packet_command cgc;
1883 struct request_sense sense;
1884 int ret;
1885
1886 VPRINTK("pktcdvd: Performing OPC\n");
1887
1888 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1889 cgc.sense = &sense;
1890 cgc.timeout = 60*HZ;
1891 cgc.cmd[0] = GPCMD_SEND_OPC;
1892 cgc.cmd[1] = 1;
1893 if ((ret = pkt_generic_packet(pd, &cgc)))
1894 pkt_dump_sense(&cgc);
1895 return ret;
1896}
1897
1898static int pkt_open_write(struct pktcdvd_device *pd)
1899{
1900 int ret;
1901 unsigned int write_speed, media_write_speed, read_speed;
1902
1903 if ((ret = pkt_probe_settings(pd))) {
1904 DPRINTK("pktcdvd: %s failed probe\n", pd->name);
1905 return -EIO;
1906 }
1907
1908 if ((ret = pkt_set_write_settings(pd))) {
1909 DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
1910 return -EIO;
1911 }
1912
1913 pkt_write_caching(pd, USE_WCACHING);
1914
1915 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1916 write_speed = 16 * 177;
1917 switch (pd->mmc3_profile) {
1918 case 0x13: /* DVD-RW */
1919 case 0x1a: /* DVD+RW */
1920 case 0x12: /* DVD-RAM */
1921 DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
1922 break;
1923 default:
1924 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1925 media_write_speed = 16;
1926 write_speed = min(write_speed, media_write_speed * 177);
1927 DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
1928 break;
1929 }
1930 read_speed = write_speed;
1931
1932 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1933 DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
1934 return -EIO;
1935 }
1936 pd->write_speed = write_speed;
1937 pd->read_speed = read_speed;
1938
1939 if ((ret = pkt_perform_opc(pd))) {
1940 DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
1941 }
1942
1943 return 0;
1944}
1945
1946/*
1947 * called at open time.
1948 */
1949static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1950{
1951 int ret;
1952 long lba;
1953 request_queue_t *q;
1954
1955 /*
1956 * We need to re-open the cdrom device without O_NONBLOCK to be able
1957 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1958 * so bdget() can't fail.
1959 */
1960 bdget(pd->bdev->bd_dev);
1961 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1962 goto out;
1963
1964 if ((ret = pkt_get_last_written(pd, &lba))) {
1965 printk("pktcdvd: pkt_get_last_written failed\n");
1966 goto out_putdev;
1967 }
1968
1969 set_capacity(pd->disk, lba << 2);
1970 set_capacity(pd->bdev->bd_disk, lba << 2);
1971 bd_set_size(pd->bdev, (loff_t)lba << 11);
1972
1973 q = bdev_get_queue(pd->bdev);
1974 if (write) {
1975 if ((ret = pkt_open_write(pd)))
1976 goto out_putdev;
1977 /*
1978 * Some CDRW drives can not handle writes larger than one packet,
1979 * even if the size is a multiple of the packet size.
1980 */
1981 spin_lock_irq(q->queue_lock);
1982 blk_queue_max_sectors(q, pd->settings.size);
1983 spin_unlock_irq(q->queue_lock);
1984 set_bit(PACKET_WRITABLE, &pd->flags);
1985 } else {
1986 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1987 clear_bit(PACKET_WRITABLE, &pd->flags);
1988 }
1989
1990 if ((ret = pkt_set_segment_merging(pd, q)))
1991 goto out_putdev;
1992
1993 if (write)
1994 printk("pktcdvd: %lukB available on disc\n", lba << 1);
1995
1996 return 0;
1997
1998out_putdev:
1999 blkdev_put(pd->bdev);
2000out:
2001 return ret;
2002}
2003
2004/*
2005 * called when the device is closed. makes sure that the device flushes
2006 * the internal cache before we close.
2007 */
2008static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2009{
2010 if (flush && pkt_flush_cache(pd))
2011 DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
2012
2013 pkt_lock_door(pd, 0);
2014
2015 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2016 blkdev_put(pd->bdev);
2017}
2018
2019static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2020{
2021 if (dev_minor >= MAX_WRITERS)
2022 return NULL;
2023 return pkt_devs[dev_minor];
2024}
2025
2026static int pkt_open(struct inode *inode, struct file *file)
2027{
2028 struct pktcdvd_device *pd = NULL;
2029 int ret;
2030
2031 VPRINTK("pktcdvd: entering open\n");
2032
2033 down(&ctl_mutex);
2034 pd = pkt_find_dev_from_minor(iminor(inode));
2035 if (!pd) {
2036 ret = -ENODEV;
2037 goto out;
2038 }
2039 BUG_ON(pd->refcnt < 0);
2040
2041 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002042 if (pd->refcnt > 1) {
2043 if ((file->f_mode & FMODE_WRITE) &&
2044 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2045 ret = -EBUSY;
2046 goto out_dec;
2047 }
2048 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (pkt_open_dev(pd, file->f_mode & FMODE_WRITE)) {
2050 ret = -EIO;
2051 goto out_dec;
2052 }
2053 /*
2054 * needed here as well, since ext2 (among others) may change
2055 * the blocksize at mount time
2056 */
2057 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2058 }
2059
2060 up(&ctl_mutex);
2061 return 0;
2062
2063out_dec:
2064 pd->refcnt--;
2065out:
2066 VPRINTK("pktcdvd: failed open (%d)\n", ret);
2067 up(&ctl_mutex);
2068 return ret;
2069}
2070
2071static int pkt_close(struct inode *inode, struct file *file)
2072{
2073 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2074 int ret = 0;
2075
2076 down(&ctl_mutex);
2077 pd->refcnt--;
2078 BUG_ON(pd->refcnt < 0);
2079 if (pd->refcnt == 0) {
2080 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2081 pkt_release_dev(pd, flush);
2082 }
2083 up(&ctl_mutex);
2084 return ret;
2085}
2086
2087
2088static void *psd_pool_alloc(unsigned int __nocast gfp_mask, void *data)
2089{
2090 return kmalloc(sizeof(struct packet_stacked_data), gfp_mask);
2091}
2092
2093static void psd_pool_free(void *ptr, void *data)
2094{
2095 kfree(ptr);
2096}
2097
2098static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2099{
2100 struct packet_stacked_data *psd = bio->bi_private;
2101 struct pktcdvd_device *pd = psd->pd;
2102
2103 if (bio->bi_size)
2104 return 1;
2105
2106 bio_put(bio);
2107 bio_endio(psd->bio, psd->bio->bi_size, err);
2108 mempool_free(psd, psd_pool);
2109 pkt_bio_finished(pd);
2110 return 0;
2111}
2112
2113static int pkt_make_request(request_queue_t *q, struct bio *bio)
2114{
2115 struct pktcdvd_device *pd;
2116 char b[BDEVNAME_SIZE];
2117 sector_t zone;
2118 struct packet_data *pkt;
2119 int was_empty, blocked_bio;
2120 struct pkt_rb_node *node;
2121
2122 pd = q->queuedata;
2123 if (!pd) {
2124 printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2125 goto end_io;
2126 }
2127
2128 /*
2129 * Clone READ bios so we can have our own bi_end_io callback.
2130 */
2131 if (bio_data_dir(bio) == READ) {
2132 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2133 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2134
2135 psd->pd = pd;
2136 psd->bio = bio;
2137 cloned_bio->bi_bdev = pd->bdev;
2138 cloned_bio->bi_private = psd;
2139 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2140 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002141 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143 }
2144
2145 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2146 printk("pktcdvd: WRITE for ro device %s (%llu)\n",
2147 pd->name, (unsigned long long)bio->bi_sector);
2148 goto end_io;
2149 }
2150
2151 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2152 printk("pktcdvd: wrong bio size\n");
2153 goto end_io;
2154 }
2155
2156 blk_queue_bounce(q, &bio);
2157
2158 zone = ZONE(bio->bi_sector, pd);
2159 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2160 (unsigned long long)bio->bi_sector,
2161 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2162
2163 /* Check if we have to split the bio */
2164 {
2165 struct bio_pair *bp;
2166 sector_t last_zone;
2167 int first_sectors;
2168
2169 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2170 if (last_zone != zone) {
2171 BUG_ON(last_zone != zone + pd->settings.size);
2172 first_sectors = last_zone - bio->bi_sector;
2173 bp = bio_split(bio, bio_split_pool, first_sectors);
2174 BUG_ON(!bp);
2175 pkt_make_request(q, &bp->bio1);
2176 pkt_make_request(q, &bp->bio2);
2177 bio_pair_release(bp);
2178 return 0;
2179 }
2180 }
2181
2182 /*
2183 * If we find a matching packet in state WAITING or READ_WAIT, we can
2184 * just append this bio to that packet.
2185 */
2186 spin_lock(&pd->cdrw.active_list_lock);
2187 blocked_bio = 0;
2188 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2189 if (pkt->sector == zone) {
2190 spin_lock(&pkt->lock);
2191 if ((pkt->state == PACKET_WAITING_STATE) ||
2192 (pkt->state == PACKET_READ_WAIT_STATE)) {
2193 pkt_add_list_last(bio, &pkt->orig_bios,
2194 &pkt->orig_bios_tail);
2195 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2196 if ((pkt->write_size >= pkt->frames) &&
2197 (pkt->state == PACKET_WAITING_STATE)) {
2198 atomic_inc(&pkt->run_sm);
2199 wake_up(&pd->wqueue);
2200 }
2201 spin_unlock(&pkt->lock);
2202 spin_unlock(&pd->cdrw.active_list_lock);
2203 return 0;
2204 } else {
2205 blocked_bio = 1;
2206 }
2207 spin_unlock(&pkt->lock);
2208 }
2209 }
2210 spin_unlock(&pd->cdrw.active_list_lock);
2211
2212 /*
2213 * No matching packet found. Store the bio in the work queue.
2214 */
2215 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2216 BUG_ON(!node);
2217 node->bio = bio;
2218 spin_lock(&pd->lock);
2219 BUG_ON(pd->bio_queue_size < 0);
2220 was_empty = (pd->bio_queue_size == 0);
2221 pkt_rbtree_insert(pd, node);
2222 spin_unlock(&pd->lock);
2223
2224 /*
2225 * Wake up the worker thread.
2226 */
2227 atomic_set(&pd->scan_queue, 1);
2228 if (was_empty) {
2229 /* This wake_up is required for correct operation */
2230 wake_up(&pd->wqueue);
2231 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2232 /*
2233 * This wake up is not required for correct operation,
2234 * but improves performance in some cases.
2235 */
2236 wake_up(&pd->wqueue);
2237 }
2238 return 0;
2239end_io:
2240 bio_io_error(bio, bio->bi_size);
2241 return 0;
2242}
2243
2244
2245
2246static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2247{
2248 struct pktcdvd_device *pd = q->queuedata;
2249 sector_t zone = ZONE(bio->bi_sector, pd);
2250 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2251 int remaining = (pd->settings.size << 9) - used;
2252 int remaining2;
2253
2254 /*
2255 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2256 * boundary, pkt_make_request() will split the bio.
2257 */
2258 remaining2 = PAGE_SIZE - bio->bi_size;
2259 remaining = max(remaining, remaining2);
2260
2261 BUG_ON(remaining < 0);
2262 return remaining;
2263}
2264
2265static void pkt_init_queue(struct pktcdvd_device *pd)
2266{
2267 request_queue_t *q = pd->disk->queue;
2268
2269 blk_queue_make_request(q, pkt_make_request);
2270 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2271 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2272 blk_queue_merge_bvec(q, pkt_merge_bvec);
2273 q->queuedata = pd;
2274}
2275
2276static int pkt_seq_show(struct seq_file *m, void *p)
2277{
2278 struct pktcdvd_device *pd = m->private;
2279 char *msg;
2280 char bdev_buf[BDEVNAME_SIZE];
2281 int states[PACKET_NUM_STATES];
2282
2283 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2284 bdevname(pd->bdev, bdev_buf));
2285
2286 seq_printf(m, "\nSettings:\n");
2287 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2288
2289 if (pd->settings.write_type == 0)
2290 msg = "Packet";
2291 else
2292 msg = "Unknown";
2293 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2294
2295 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2296 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2297
2298 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2299
2300 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2301 msg = "Mode 1";
2302 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2303 msg = "Mode 2";
2304 else
2305 msg = "Unknown";
2306 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2307
2308 seq_printf(m, "\nStatistics:\n");
2309 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2310 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2311 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2312 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2313 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2314
2315 seq_printf(m, "\nMisc:\n");
2316 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2317 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2318 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2319 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2320 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2321 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2322
2323 seq_printf(m, "\nQueue state:\n");
2324 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2325 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2326 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2327
2328 pkt_count_states(pd, states);
2329 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2330 states[0], states[1], states[2], states[3], states[4], states[5]);
2331
2332 return 0;
2333}
2334
2335static int pkt_seq_open(struct inode *inode, struct file *file)
2336{
2337 return single_open(file, pkt_seq_show, PDE(inode)->data);
2338}
2339
2340static struct file_operations pkt_proc_fops = {
2341 .open = pkt_seq_open,
2342 .read = seq_read,
2343 .llseek = seq_lseek,
2344 .release = single_release
2345};
2346
2347static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2348{
2349 int i;
2350 int ret = 0;
2351 char b[BDEVNAME_SIZE];
2352 struct proc_dir_entry *proc;
2353 struct block_device *bdev;
2354
2355 if (pd->pkt_dev == dev) {
2356 printk("pktcdvd: Recursive setup not allowed\n");
2357 return -EBUSY;
2358 }
2359 for (i = 0; i < MAX_WRITERS; i++) {
2360 struct pktcdvd_device *pd2 = pkt_devs[i];
2361 if (!pd2)
2362 continue;
2363 if (pd2->bdev->bd_dev == dev) {
2364 printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
2365 return -EBUSY;
2366 }
2367 if (pd2->pkt_dev == dev) {
2368 printk("pktcdvd: Can't chain pktcdvd devices\n");
2369 return -EBUSY;
2370 }
2371 }
2372
2373 bdev = bdget(dev);
2374 if (!bdev)
2375 return -ENOMEM;
2376 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2377 if (ret)
2378 return ret;
2379
2380 /* This is safe, since we have a reference from open(). */
2381 __module_get(THIS_MODULE);
2382
2383 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2384 printk("pktcdvd: not enough memory for buffers\n");
2385 ret = -ENOMEM;
2386 goto out_mem;
2387 }
2388
2389 pd->bdev = bdev;
2390 set_blocksize(bdev, CD_FRAMESIZE);
2391
2392 pkt_init_queue(pd);
2393
2394 atomic_set(&pd->cdrw.pending_bios, 0);
2395 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2396 if (IS_ERR(pd->cdrw.thread)) {
2397 printk("pktcdvd: can't start kernel thread\n");
2398 ret = -ENOMEM;
2399 goto out_thread;
2400 }
2401
2402 proc = create_proc_entry(pd->name, 0, pkt_proc);
2403 if (proc) {
2404 proc->data = pd;
2405 proc->proc_fops = &pkt_proc_fops;
2406 }
2407 DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2408 return 0;
2409
2410out_thread:
2411 pkt_shrink_pktlist(pd);
2412out_mem:
2413 blkdev_put(bdev);
2414 /* This is safe: open() is still holding a reference. */
2415 module_put(THIS_MODULE);
2416 return ret;
2417}
2418
2419static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2420{
2421 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2422
2423 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2424 BUG_ON(!pd);
2425
2426 switch (cmd) {
2427 /*
2428 * forward selected CDROM ioctls to CD-ROM, for UDF
2429 */
2430 case CDROMMULTISESSION:
2431 case CDROMREADTOCENTRY:
2432 case CDROM_LAST_WRITTEN:
2433 case CDROM_SEND_PACKET:
2434 case SCSI_IOCTL_SEND_COMMAND:
Peter Osterlund118326e2005-05-14 00:58:30 -07002435 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 case CDROMEJECT:
2438 /*
2439 * The door gets locked when the device is opened, so we
2440 * have to unlock it or else the eject command fails.
2441 */
2442 pkt_lock_door(pd, 0);
Peter Osterlund118326e2005-05-14 00:58:30 -07002443 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 default:
2446 printk("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
2447 return -ENOTTY;
2448 }
2449
2450 return 0;
2451}
2452
2453static int pkt_media_changed(struct gendisk *disk)
2454{
2455 struct pktcdvd_device *pd = disk->private_data;
2456 struct gendisk *attached_disk;
2457
2458 if (!pd)
2459 return 0;
2460 if (!pd->bdev)
2461 return 0;
2462 attached_disk = pd->bdev->bd_disk;
2463 if (!attached_disk)
2464 return 0;
2465 return attached_disk->fops->media_changed(attached_disk);
2466}
2467
2468static struct block_device_operations pktcdvd_ops = {
2469 .owner = THIS_MODULE,
2470 .open = pkt_open,
2471 .release = pkt_close,
2472 .ioctl = pkt_ioctl,
2473 .media_changed = pkt_media_changed,
2474};
2475
2476/*
2477 * Set up mapping from pktcdvd device to CD-ROM device.
2478 */
2479static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2480{
2481 int idx;
2482 int ret = -ENOMEM;
2483 struct pktcdvd_device *pd;
2484 struct gendisk *disk;
2485 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2486
2487 for (idx = 0; idx < MAX_WRITERS; idx++)
2488 if (!pkt_devs[idx])
2489 break;
2490 if (idx == MAX_WRITERS) {
2491 printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
2492 return -EBUSY;
2493 }
2494
2495 pd = kmalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2496 if (!pd)
2497 return ret;
2498 memset(pd, 0, sizeof(struct pktcdvd_device));
2499
2500 pd->rb_pool = mempool_create(PKT_RB_POOL_SIZE, pkt_rb_alloc, pkt_rb_free, NULL);
2501 if (!pd->rb_pool)
2502 goto out_mem;
2503
2504 disk = alloc_disk(1);
2505 if (!disk)
2506 goto out_mem;
2507 pd->disk = disk;
2508
2509 spin_lock_init(&pd->lock);
2510 spin_lock_init(&pd->iosched.lock);
2511 sprintf(pd->name, "pktcdvd%d", idx);
2512 init_waitqueue_head(&pd->wqueue);
2513 pd->bio_queue = RB_ROOT;
2514
2515 disk->major = pkt_major;
2516 disk->first_minor = idx;
2517 disk->fops = &pktcdvd_ops;
2518 disk->flags = GENHD_FL_REMOVABLE;
2519 sprintf(disk->disk_name, "pktcdvd%d", idx);
2520 disk->private_data = pd;
2521 disk->queue = blk_alloc_queue(GFP_KERNEL);
2522 if (!disk->queue)
2523 goto out_mem2;
2524
2525 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2526 ret = pkt_new_dev(pd, dev);
2527 if (ret)
2528 goto out_new_dev;
2529
2530 add_disk(disk);
2531 pkt_devs[idx] = pd;
2532 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2533 return 0;
2534
2535out_new_dev:
2536 blk_put_queue(disk->queue);
2537out_mem2:
2538 put_disk(disk);
2539out_mem:
2540 if (pd->rb_pool)
2541 mempool_destroy(pd->rb_pool);
2542 kfree(pd);
2543 return ret;
2544}
2545
2546/*
2547 * Tear down mapping from pktcdvd device to CD-ROM device.
2548 */
2549static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2550{
2551 struct pktcdvd_device *pd;
2552 int idx;
2553 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2554
2555 for (idx = 0; idx < MAX_WRITERS; idx++) {
2556 pd = pkt_devs[idx];
2557 if (pd && (pd->pkt_dev == pkt_dev))
2558 break;
2559 }
2560 if (idx == MAX_WRITERS) {
2561 DPRINTK("pktcdvd: dev not setup\n");
2562 return -ENXIO;
2563 }
2564
2565 if (pd->refcnt > 0)
2566 return -EBUSY;
2567
2568 if (!IS_ERR(pd->cdrw.thread))
2569 kthread_stop(pd->cdrw.thread);
2570
2571 blkdev_put(pd->bdev);
2572
2573 pkt_shrink_pktlist(pd);
2574
2575 remove_proc_entry(pd->name, pkt_proc);
2576 DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
2577
2578 del_gendisk(pd->disk);
2579 blk_put_queue(pd->disk->queue);
2580 put_disk(pd->disk);
2581
2582 pkt_devs[idx] = NULL;
2583 mempool_destroy(pd->rb_pool);
2584 kfree(pd);
2585
2586 /* This is safe: open() is still holding a reference. */
2587 module_put(THIS_MODULE);
2588 return 0;
2589}
2590
2591static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2592{
2593 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2594 if (pd) {
2595 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2596 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2597 } else {
2598 ctrl_cmd->dev = 0;
2599 ctrl_cmd->pkt_dev = 0;
2600 }
2601 ctrl_cmd->num_devices = MAX_WRITERS;
2602}
2603
2604static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2605{
2606 void __user *argp = (void __user *)arg;
2607 struct pkt_ctrl_command ctrl_cmd;
2608 int ret = 0;
2609
2610 if (cmd != PACKET_CTRL_CMD)
2611 return -ENOTTY;
2612
2613 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2614 return -EFAULT;
2615
2616 switch (ctrl_cmd.command) {
2617 case PKT_CTRL_CMD_SETUP:
2618 if (!capable(CAP_SYS_ADMIN))
2619 return -EPERM;
2620 down(&ctl_mutex);
2621 ret = pkt_setup_dev(&ctrl_cmd);
2622 up(&ctl_mutex);
2623 break;
2624 case PKT_CTRL_CMD_TEARDOWN:
2625 if (!capable(CAP_SYS_ADMIN))
2626 return -EPERM;
2627 down(&ctl_mutex);
2628 ret = pkt_remove_dev(&ctrl_cmd);
2629 up(&ctl_mutex);
2630 break;
2631 case PKT_CTRL_CMD_STATUS:
2632 down(&ctl_mutex);
2633 pkt_get_status(&ctrl_cmd);
2634 up(&ctl_mutex);
2635 break;
2636 default:
2637 return -ENOTTY;
2638 }
2639
2640 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2641 return -EFAULT;
2642 return ret;
2643}
2644
2645
2646static struct file_operations pkt_ctl_fops = {
2647 .ioctl = pkt_ctl_ioctl,
2648 .owner = THIS_MODULE,
2649};
2650
2651static struct miscdevice pkt_misc = {
2652 .minor = MISC_DYNAMIC_MINOR,
2653 .name = "pktcdvd",
2654 .devfs_name = "pktcdvd/control",
2655 .fops = &pkt_ctl_fops
2656};
2657
2658static int __init pkt_init(void)
2659{
2660 int ret;
2661
2662 psd_pool = mempool_create(PSD_POOL_SIZE, psd_pool_alloc, psd_pool_free, NULL);
2663 if (!psd_pool)
2664 return -ENOMEM;
2665
2666 ret = register_blkdev(pkt_major, "pktcdvd");
2667 if (ret < 0) {
2668 printk("pktcdvd: Unable to register block device\n");
2669 goto out2;
2670 }
2671 if (!pkt_major)
2672 pkt_major = ret;
2673
2674 ret = misc_register(&pkt_misc);
2675 if (ret) {
2676 printk("pktcdvd: Unable to register misc device\n");
2677 goto out;
2678 }
2679
2680 init_MUTEX(&ctl_mutex);
2681
2682 pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
2683
2684 DPRINTK("pktcdvd: %s\n", VERSION_CODE);
2685 return 0;
2686
2687out:
2688 unregister_blkdev(pkt_major, "pktcdvd");
2689out2:
2690 mempool_destroy(psd_pool);
2691 return ret;
2692}
2693
2694static void __exit pkt_exit(void)
2695{
2696 remove_proc_entry("pktcdvd", proc_root_driver);
2697 misc_deregister(&pkt_misc);
2698 unregister_blkdev(pkt_major, "pktcdvd");
2699 mempool_destroy(psd_pool);
2700}
2701
2702MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2703MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2704MODULE_LICENSE("GPL");
2705
2706module_init(pkt_init);
2707module_exit(pkt_exit);