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