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