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