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