blob: f2904f67af4711484fc0ca99c74d8dc462103d76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
4 *
5 * May be copied or modified under the terms of the GNU General Public
6 * License. See linux/COPYING for more information.
7 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -07008 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
9 * DVD-RAM devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Theory of operation:
12 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070013 * At the lowest level, there is the standard driver for the CD/DVD device,
14 * typically ide-cd.c or sr.c. This driver can handle read and write requests,
15 * but it doesn't know anything about the special restrictions that apply to
16 * packet writing. One restriction is that write requests must be aligned to
17 * packet boundaries on the physical media, and the size of a write request
18 * must be equal to the packet size. Another restriction is that a
19 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
20 * command, if the previous command was a write.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070022 * The purpose of the packet writing driver is to hide these restrictions from
23 * higher layers, such as file systems, and present a block device that can be
24 * randomly read and written using 2kB-sized blocks.
25 *
26 * The lowest layer in the packet writing driver is the packet I/O scheduler.
27 * Its data is defined by the struct packet_iosched and includes two bio
28 * queues with pending read and write requests. These queues are processed
29 * by the pkt_iosched_process_queue() function. The write requests in this
30 * queue are already properly aligned and sized. This layer is responsible for
31 * issuing the flush cache commands and scheduling the I/O in a good order.
32 *
33 * The next layer transforms unaligned write requests to aligned writes. This
34 * transformation requires reading missing pieces of data from the underlying
35 * block device, assembling the pieces to full packets and queuing them to the
36 * packet I/O scheduler.
37 *
38 * At the top layer there is a custom make_request_fn function that forwards
39 * read requests directly to the iosched queue and puts write requests in the
40 * unaligned write queue. A kernel thread performs the necessary read
41 * gathering to convert the unaligned writes to aligned writes and then feeds
42 * them to the packet I/O scheduler.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 *************************************************************************/
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/pktcdvd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/kthread.h>
51#include <linux/errno.h>
52#include <linux/spinlock.h>
53#include <linux/file.h>
54#include <linux/proc_fs.h>
55#include <linux/seq_file.h>
56#include <linux/miscdevice.h>
57#include <linux/suspend.h>
Jes Sorensen1657f822006-03-23 03:00:25 -080058#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#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
Thomas Maier78220822006-10-04 02:15:28 -070065#define DRIVER_NAME "pktcdvd"
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#if PACKET_DEBUG
68#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
69#else
70#define DPRINTK(fmt, args...)
71#endif
72
73#if PACKET_DEBUG > 1
74#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
75#else
76#define VPRINTK(fmt, args...)
77#endif
78
79#define MAX_SPEED 0xffff
80
81#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
82
83static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
84static struct proc_dir_entry *pkt_proc;
Thomas Maieradd21662006-10-04 02:15:30 -070085static int pktdev_major;
Jes Sorensen1657f822006-03-23 03:00:25 -080086static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static mempool_t *psd_pool;
88
89
90static void pkt_bio_finished(struct pktcdvd_device *pd)
91{
92 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
93 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
Thomas Maier78220822006-10-04 02:15:28 -070094 VPRINTK(DRIVER_NAME": queue empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 atomic_set(&pd->iosched.attention, 1);
96 wake_up(&pd->wqueue);
97 }
98}
99
100static void pkt_bio_destructor(struct bio *bio)
101{
102 kfree(bio->bi_io_vec);
103 kfree(bio);
104}
105
106static struct bio *pkt_bio_alloc(int nr_iovecs)
107{
108 struct bio_vec *bvl = NULL;
109 struct bio *bio;
110
111 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
112 if (!bio)
113 goto no_bio;
114 bio_init(bio);
115
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700116 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (!bvl)
118 goto no_bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 bio->bi_max_vecs = nr_iovecs;
121 bio->bi_io_vec = bvl;
122 bio->bi_destructor = pkt_bio_destructor;
123
124 return bio;
125
126 no_bvl:
127 kfree(bio);
128 no_bio:
129 return NULL;
130}
131
132/*
133 * Allocate a packet_data struct
134 */
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800135static struct packet_data *pkt_alloc_packet_data(int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int i;
138 struct packet_data *pkt;
139
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700140 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!pkt)
142 goto no_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800144 pkt->frames = frames;
145 pkt->w_bio = pkt_bio_alloc(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!pkt->w_bio)
147 goto no_bio;
148
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800149 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
151 if (!pkt->pages[i])
152 goto no_page;
153 }
154
155 spin_lock_init(&pkt->lock);
156
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800157 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct bio *bio = pkt_bio_alloc(1);
159 if (!bio)
160 goto no_rd_bio;
161 pkt->r_bios[i] = bio;
162 }
163
164 return pkt;
165
166no_rd_bio:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800167 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct bio *bio = pkt->r_bios[i];
169 if (bio)
170 bio_put(bio);
171 }
172
173no_page:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800174 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (pkt->pages[i])
176 __free_page(pkt->pages[i]);
177 bio_put(pkt->w_bio);
178no_bio:
179 kfree(pkt);
180no_pkt:
181 return NULL;
182}
183
184/*
185 * Free a packet_data struct
186 */
187static void pkt_free_packet_data(struct packet_data *pkt)
188{
189 int i;
190
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800191 for (i = 0; i < pkt->frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct bio *bio = pkt->r_bios[i];
193 if (bio)
194 bio_put(bio);
195 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800196 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 __free_page(pkt->pages[i]);
198 bio_put(pkt->w_bio);
199 kfree(pkt);
200}
201
202static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
203{
204 struct packet_data *pkt, *next;
205
206 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
207
208 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
209 pkt_free_packet_data(pkt);
210 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800211 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
215{
216 struct packet_data *pkt;
217
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800218 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 while (nr_packets > 0) {
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800221 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!pkt) {
223 pkt_shrink_pktlist(pd);
224 return 0;
225 }
226 pkt->id = nr_packets;
227 pkt->pd = pd;
228 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
229 nr_packets--;
230 }
231 return 1;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
235{
236 struct rb_node *n = rb_next(&node->rb_node);
237 if (!n)
238 return NULL;
239 return rb_entry(n, struct pkt_rb_node, rb_node);
240}
241
Peter Osterlundac893962006-01-14 13:21:32 -0800242static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 rb_erase(&node->rb_node, &pd->bio_queue);
245 mempool_free(node, pd->rb_pool);
246 pd->bio_queue_size--;
247 BUG_ON(pd->bio_queue_size < 0);
248}
249
250/*
251 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
252 */
253static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
254{
255 struct rb_node *n = pd->bio_queue.rb_node;
256 struct rb_node *next;
257 struct pkt_rb_node *tmp;
258
259 if (!n) {
260 BUG_ON(pd->bio_queue_size > 0);
261 return NULL;
262 }
263
264 for (;;) {
265 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
266 if (s <= tmp->bio->bi_sector)
267 next = n->rb_left;
268 else
269 next = n->rb_right;
270 if (!next)
271 break;
272 n = next;
273 }
274
275 if (s > tmp->bio->bi_sector) {
276 tmp = pkt_rbtree_next(tmp);
277 if (!tmp)
278 return NULL;
279 }
280 BUG_ON(s > tmp->bio->bi_sector);
281 return tmp;
282}
283
284/*
285 * Insert a node into the pd->bio_queue rb tree.
286 */
287static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
288{
289 struct rb_node **p = &pd->bio_queue.rb_node;
290 struct rb_node *parent = NULL;
291 sector_t s = node->bio->bi_sector;
292 struct pkt_rb_node *tmp;
293
294 while (*p) {
295 parent = *p;
296 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
297 if (s < tmp->bio->bi_sector)
298 p = &(*p)->rb_left;
299 else
300 p = &(*p)->rb_right;
301 }
302 rb_link_node(&node->rb_node, parent, p);
303 rb_insert_color(&node->rb_node, &pd->bio_queue);
304 pd->bio_queue_size++;
305}
306
307/*
308 * Add a bio to a single linked list defined by its head and tail pointers.
309 */
Peter Osterlundac893962006-01-14 13:21:32 -0800310static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 bio->bi_next = NULL;
313 if (*list_tail) {
314 BUG_ON((*list_head) == NULL);
315 (*list_tail)->bi_next = bio;
316 (*list_tail) = bio;
317 } else {
318 BUG_ON((*list_head) != NULL);
319 (*list_head) = bio;
320 (*list_tail) = bio;
321 }
322}
323
324/*
325 * Remove and return the first bio from a single linked list defined by its
326 * head and tail pointers.
327 */
328static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
329{
330 struct bio *bio;
331
332 if (*list_head == NULL)
333 return NULL;
334
335 bio = *list_head;
336 *list_head = bio->bi_next;
337 if (*list_head == NULL)
338 *list_tail = NULL;
339
340 bio->bi_next = NULL;
341 return bio;
342}
343
344/*
345 * Send a packet_command to the underlying block device and
346 * wait for completion.
347 */
348static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
349{
350 char sense[SCSI_SENSE_BUFFERSIZE];
351 request_queue_t *q;
352 struct request *rq;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700353 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int err = 0;
355
356 q = bdev_get_queue(pd->bdev);
357
358 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
359 __GFP_WAIT);
360 rq->errors = 0;
361 rq->rq_disk = pd->bdev->bd_disk;
362 rq->bio = NULL;
363 rq->buffer = NULL;
364 rq->timeout = 60*HZ;
365 rq->data = cgc->buffer;
366 rq->data_len = cgc->buflen;
367 rq->sense = sense;
368 memset(sense, 0, sizeof(sense));
369 rq->sense_len = 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200370 rq->cmd_type = REQ_TYPE_BLOCK_PC;
371 rq->cmd_flags |= REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (cgc->quiet)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200373 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
375 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
376 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
Peter Osterlundcef289632006-02-20 18:28:01 -0800377 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 rq->ref_count++;
Jens Axboec00895a2006-09-30 20:29:12 +0200380 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rq->end_io = blk_end_sync_rq;
382 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
383 generic_unplug_device(q);
384 wait_for_completion(&wait);
385
386 if (rq->errors)
387 err = -EIO;
388
389 blk_put_request(rq);
390 return err;
391}
392
393/*
394 * A generic sense dump / resolve mechanism should be implemented across
395 * all ATAPI + SCSI devices.
396 */
397static void pkt_dump_sense(struct packet_command *cgc)
398{
399 static char *info[9] = { "No sense", "Recovered error", "Not ready",
400 "Medium error", "Hardware error", "Illegal request",
401 "Unit attention", "Data protect", "Blank check" };
402 int i;
403 struct request_sense *sense = cgc->sense;
404
Thomas Maier78220822006-10-04 02:15:28 -0700405 printk(DRIVER_NAME":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 for (i = 0; i < CDROM_PACKET_SIZE; i++)
407 printk(" %02x", cgc->cmd[i]);
408 printk(" - ");
409
410 if (sense == NULL) {
411 printk("no sense\n");
412 return;
413 }
414
415 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
416
417 if (sense->sense_key > 8) {
418 printk(" (INVALID)\n");
419 return;
420 }
421
422 printk(" (%s)\n", info[sense->sense_key]);
423}
424
425/*
426 * flush the drive cache to media
427 */
428static int pkt_flush_cache(struct pktcdvd_device *pd)
429{
430 struct packet_command cgc;
431
432 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
433 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
434 cgc.quiet = 1;
435
436 /*
437 * the IMMED bit -- we default to not setting it, although that
438 * would allow a much faster close, this is safer
439 */
440#if 0
441 cgc.cmd[1] = 1 << 1;
442#endif
443 return pkt_generic_packet(pd, &cgc);
444}
445
446/*
447 * speed is given as the normal factor, e.g. 4 for 4x
448 */
449static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
450{
451 struct packet_command cgc;
452 struct request_sense sense;
453 int ret;
454
455 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
456 cgc.sense = &sense;
457 cgc.cmd[0] = GPCMD_SET_SPEED;
458 cgc.cmd[2] = (read_speed >> 8) & 0xff;
459 cgc.cmd[3] = read_speed & 0xff;
460 cgc.cmd[4] = (write_speed >> 8) & 0xff;
461 cgc.cmd[5] = write_speed & 0xff;
462
463 if ((ret = pkt_generic_packet(pd, &cgc)))
464 pkt_dump_sense(&cgc);
465
466 return ret;
467}
468
469/*
470 * Queue a bio for processing by the low-level CD device. Must be called
471 * from process context.
472 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700473static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 spin_lock(&pd->iosched.lock);
476 if (bio_data_dir(bio) == READ) {
477 pkt_add_list_last(bio, &pd->iosched.read_queue,
478 &pd->iosched.read_queue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else {
480 pkt_add_list_last(bio, &pd->iosched.write_queue,
481 &pd->iosched.write_queue_tail);
482 }
483 spin_unlock(&pd->iosched.lock);
484
485 atomic_set(&pd->iosched.attention, 1);
486 wake_up(&pd->wqueue);
487}
488
489/*
490 * Process the queued read/write requests. This function handles special
491 * requirements for CDRW drives:
492 * - A cache flush command must be inserted before a read request if the
493 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700494 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700496 * - Optimize for throughput at the expense of latency. This means that streaming
497 * writes will never be interrupted by a read, but if the drive has to seek
498 * before the next write, switch to reading instead if there are any pending
499 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * - Set the read speed according to current usage pattern. When only reading
501 * from the device, it's best to use the highest possible read speed, but
502 * when switching often between reading and writing, it's better to have the
503 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
505static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (atomic_read(&pd->iosched.attention) == 0)
509 return;
510 atomic_set(&pd->iosched.attention, 0);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 for (;;) {
513 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700514 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 spin_lock(&pd->iosched.lock);
517 reads_queued = (pd->iosched.read_queue != NULL);
518 writes_queued = (pd->iosched.write_queue != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 spin_unlock(&pd->iosched.lock);
520
521 if (!reads_queued && !writes_queued)
522 break;
523
524 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700525 int need_write_seek = 1;
526 spin_lock(&pd->iosched.lock);
527 bio = pd->iosched.write_queue;
528 spin_unlock(&pd->iosched.lock);
529 if (bio && (bio->bi_sector == pd->iosched.last_write))
530 need_write_seek = 0;
531 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700533 VPRINTK(DRIVER_NAME": write, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 break;
535 }
536 pkt_flush_cache(pd);
537 pd->iosched.writing = 0;
538 }
539 } else {
540 if (!reads_queued && writes_queued) {
541 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700542 VPRINTK(DRIVER_NAME": read, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544 }
545 pd->iosched.writing = 1;
546 }
547 }
548
549 spin_lock(&pd->iosched.lock);
550 if (pd->iosched.writing) {
551 bio = pkt_get_list_first(&pd->iosched.write_queue,
552 &pd->iosched.write_queue_tail);
553 } else {
554 bio = pkt_get_list_first(&pd->iosched.read_queue,
555 &pd->iosched.read_queue_tail);
556 }
557 spin_unlock(&pd->iosched.lock);
558
559 if (!bio)
560 continue;
561
562 if (bio_data_dir(bio) == READ)
563 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700564 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700566 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
569 if (pd->read_speed == pd->write_speed) {
570 pd->read_speed = MAX_SPEED;
571 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
572 }
573 } else {
574 if (pd->read_speed != pd->write_speed) {
575 pd->read_speed = pd->write_speed;
576 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
577 }
578 }
579
580 atomic_inc(&pd->cdrw.pending_bios);
581 generic_make_request(bio);
582 }
583}
584
585/*
586 * Special care is needed if the underlying block device has a small
587 * max_phys_segments value.
588 */
589static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
590{
591 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
592 /*
593 * The cdrom device can handle one segment/frame
594 */
595 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
596 return 0;
597 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
598 /*
599 * We can handle this case at the expense of some extra memory
600 * copies during write operations
601 */
602 set_bit(PACKET_MERGE_SEGS, &pd->flags);
603 return 0;
604 } else {
Thomas Maier78220822006-10-04 02:15:28 -0700605 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EIO;
607 }
608}
609
610/*
611 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
612 */
613static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
614{
615 unsigned int copy_size = CD_FRAMESIZE;
616
617 while (copy_size > 0) {
618 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
619 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
620 src_bvl->bv_offset + offs;
621 void *vto = page_address(dst_page) + dst_offs;
622 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
623
624 BUG_ON(len < 0);
625 memcpy(vto, vfrom, len);
626 kunmap_atomic(vfrom, KM_USER0);
627
628 seg++;
629 offs = 0;
630 dst_offs += len;
631 copy_size -= len;
632 }
633}
634
635/*
636 * Copy all data for this packet to pkt->pages[], so that
637 * a) The number of required segments for the write bio is minimized, which
638 * is necessary for some scsi controllers.
639 * b) The data can be used as cache to avoid read requests if we receive a
640 * new write request for the same zone.
641 */
Peter Osterlund72772322006-02-14 13:52:56 -0800642static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 int f, p, offs;
645
646 /* Copy all data to pkt->pages[] */
647 p = 0;
648 offs = 0;
649 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -0800650 if (bvec[f].bv_page != pkt->pages[p]) {
651 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 void *vto = page_address(pkt->pages[p]) + offs;
653 memcpy(vto, vfrom, CD_FRAMESIZE);
654 kunmap_atomic(vfrom, KM_USER0);
Peter Osterlund72772322006-02-14 13:52:56 -0800655 bvec[f].bv_page = pkt->pages[p];
656 bvec[f].bv_offset = offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
Peter Osterlund72772322006-02-14 13:52:56 -0800658 BUG_ON(bvec[f].bv_offset != offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 offs += CD_FRAMESIZE;
661 if (offs >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 offs = 0;
663 p++;
664 }
665 }
666}
667
668static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
669{
670 struct packet_data *pkt = bio->bi_private;
671 struct pktcdvd_device *pd = pkt->pd;
672 BUG_ON(!pd);
673
674 if (bio->bi_size)
675 return 1;
676
677 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
678 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
679
680 if (err)
681 atomic_inc(&pkt->io_errors);
682 if (atomic_dec_and_test(&pkt->io_wait)) {
683 atomic_inc(&pkt->run_sm);
684 wake_up(&pd->wqueue);
685 }
686 pkt_bio_finished(pd);
687
688 return 0;
689}
690
691static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
692{
693 struct packet_data *pkt = bio->bi_private;
694 struct pktcdvd_device *pd = pkt->pd;
695 BUG_ON(!pd);
696
697 if (bio->bi_size)
698 return 1;
699
700 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
701
702 pd->stats.pkt_ended++;
703
704 pkt_bio_finished(pd);
705 atomic_dec(&pkt->io_wait);
706 atomic_inc(&pkt->run_sm);
707 wake_up(&pd->wqueue);
708 return 0;
709}
710
711/*
712 * Schedule reads for the holes in a packet
713 */
714static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
715{
716 int frames_read = 0;
717 struct bio *bio;
718 int f;
719 char written[PACKET_MAX_SIZE];
720
721 BUG_ON(!pkt->orig_bios);
722
723 atomic_set(&pkt->io_wait, 0);
724 atomic_set(&pkt->io_errors, 0);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Figure out which frames we need to read before we can write.
728 */
729 memset(written, 0, sizeof(written));
730 spin_lock(&pkt->lock);
731 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
732 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
733 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -0700734 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 BUG_ON(first_frame < 0);
736 BUG_ON(first_frame + num_frames > pkt->frames);
737 for (f = first_frame; f < first_frame + num_frames; f++)
738 written[f] = 1;
739 }
740 spin_unlock(&pkt->lock);
741
Peter Osterlund06e7ab52005-09-13 01:25:28 -0700742 if (pkt->cache_valid) {
743 VPRINTK("pkt_gather_data: zone %llx cached\n",
744 (unsigned long long)pkt->sector);
745 goto out_account;
746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Schedule reads for missing parts of the packet.
750 */
751 for (f = 0; f < pkt->frames; f++) {
752 int p, offset;
753 if (written[f])
754 continue;
755 bio = pkt->r_bios[f];
756 bio_init(bio);
757 bio->bi_max_vecs = 1;
758 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
759 bio->bi_bdev = pd->bdev;
760 bio->bi_end_io = pkt_end_io_read;
761 bio->bi_private = pkt;
762
763 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
764 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
765 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
766 f, pkt->pages[p], offset);
767 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
768 BUG();
769
770 atomic_inc(&pkt->io_wait);
771 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700772 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 frames_read++;
774 }
775
776out_account:
777 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
778 frames_read, (unsigned long long)pkt->sector);
779 pd->stats.pkt_started++;
780 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/*
784 * Find a packet matching zone, or the least recently used packet if
785 * there is no match.
786 */
787static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
788{
789 struct packet_data *pkt;
790
791 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
792 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
793 list_del_init(&pkt->list);
794 if (pkt->sector != zone)
795 pkt->cache_valid = 0;
Peter Osterlund610827d2005-09-13 01:25:29 -0700796 return pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 }
Peter Osterlund610827d2005-09-13 01:25:29 -0700799 BUG();
800 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
804{
805 if (pkt->cache_valid) {
806 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
807 } else {
808 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
809 }
810}
811
812/*
813 * recover a failed write, query for relocation if possible
814 *
815 * returns 1 if recovery is possible, or 0 if not
816 *
817 */
818static int pkt_start_recovery(struct packet_data *pkt)
819{
820 /*
821 * FIXME. We need help from the file system to implement
822 * recovery handling.
823 */
824 return 0;
825#if 0
826 struct request *rq = pkt->rq;
827 struct pktcdvd_device *pd = rq->rq_disk->private_data;
828 struct block_device *pkt_bdev;
829 struct super_block *sb = NULL;
830 unsigned long old_block, new_block;
831 sector_t new_sector;
832
833 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
834 if (pkt_bdev) {
835 sb = get_super(pkt_bdev);
836 bdput(pkt_bdev);
837 }
838
839 if (!sb)
840 return 0;
841
842 if (!sb->s_op || !sb->s_op->relocate_blocks)
843 goto out;
844
845 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
846 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
847 goto out;
848
849 new_sector = new_block * (CD_FRAMESIZE >> 9);
850 pkt->sector = new_sector;
851
852 pkt->bio->bi_sector = new_sector;
853 pkt->bio->bi_next = NULL;
854 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
855 pkt->bio->bi_idx = 0;
856
857 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
858 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
859 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
860 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
861 BUG_ON(pkt->bio->bi_private != pkt);
862
863 drop_super(sb);
864 return 1;
865
866out:
867 drop_super(sb);
868 return 0;
869#endif
870}
871
872static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
873{
874#if PACKET_DEBUG > 1
875 static const char *state_name[] = {
876 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
877 };
878 enum packet_data_state old_state = pkt->state;
879 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
880 state_name[old_state], state_name[state]);
881#endif
882 pkt->state = state;
883}
884
885/*
886 * Scan the work queue to see if we can start a new packet.
887 * returns non-zero if any work was done.
888 */
889static int pkt_handle_queue(struct pktcdvd_device *pd)
890{
891 struct packet_data *pkt, *p;
892 struct bio *bio = NULL;
893 sector_t zone = 0; /* Suppress gcc warning */
894 struct pkt_rb_node *node, *first_node;
895 struct rb_node *n;
896
897 VPRINTK("handle_queue\n");
898
899 atomic_set(&pd->scan_queue, 0);
900
901 if (list_empty(&pd->cdrw.pkt_free_list)) {
902 VPRINTK("handle_queue: no pkt\n");
903 return 0;
904 }
905
906 /*
907 * Try to find a zone we are not already working on.
908 */
909 spin_lock(&pd->lock);
910 first_node = pkt_rbtree_find(pd, pd->current_sector);
911 if (!first_node) {
912 n = rb_first(&pd->bio_queue);
913 if (n)
914 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
915 }
916 node = first_node;
917 while (node) {
918 bio = node->bio;
919 zone = ZONE(bio->bi_sector, pd);
920 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -0700921 if (p->sector == zone) {
922 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -0700924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 break;
927try_next_bio:
928 node = pkt_rbtree_next(node);
929 if (!node) {
930 n = rb_first(&pd->bio_queue);
931 if (n)
932 node = rb_entry(n, struct pkt_rb_node, rb_node);
933 }
934 if (node == first_node)
935 node = NULL;
936 }
937 spin_unlock(&pd->lock);
938 if (!bio) {
939 VPRINTK("handle_queue: no bio\n");
940 return 0;
941 }
942
943 pkt = pkt_get_packet_data(pd, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 pd->current_sector = zone + pd->settings.size;
946 pkt->sector = zone;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800947 BUG_ON(pkt->frames != pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 pkt->write_size = 0;
949
950 /*
951 * Scan work queue for bios in the same zone and link them
952 * to this packet.
953 */
954 spin_lock(&pd->lock);
955 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
956 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
957 bio = node->bio;
958 VPRINTK("pkt_handle_queue: found zone=%llx\n",
959 (unsigned long long)ZONE(bio->bi_sector, pd));
960 if (ZONE(bio->bi_sector, pd) != zone)
961 break;
962 pkt_rbtree_erase(pd, node);
963 spin_lock(&pkt->lock);
964 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
965 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
966 spin_unlock(&pkt->lock);
967 }
968 spin_unlock(&pd->lock);
969
970 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
971 pkt_set_state(pkt, PACKET_WAITING_STATE);
972 atomic_set(&pkt->run_sm, 1);
973
974 spin_lock(&pd->cdrw.active_list_lock);
975 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
976 spin_unlock(&pd->cdrw.active_list_lock);
977
978 return 1;
979}
980
981/*
982 * Assemble a bio to write one packet and queue the bio for processing
983 * by the underlying block device.
984 */
985static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
986{
987 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int f;
989 int frames_write;
Peter Osterlund72772322006-02-14 13:52:56 -0800990 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -0800993 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
994 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 /*
Peter Osterlund72772322006-02-14 13:52:56 -0800998 * Fill-in bvec with data from orig_bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
1000 frames_write = 0;
1001 spin_lock(&pkt->lock);
1002 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1003 int segment = bio->bi_idx;
1004 int src_offs = 0;
1005 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1006 int num_frames = bio->bi_size / CD_FRAMESIZE;
1007 BUG_ON(first_frame < 0);
1008 BUG_ON(first_frame + num_frames > pkt->frames);
1009 for (f = first_frame; f < first_frame + num_frames; f++) {
1010 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1011
1012 while (src_offs >= src_bvl->bv_len) {
1013 src_offs -= src_bvl->bv_len;
1014 segment++;
1015 BUG_ON(segment >= bio->bi_vcnt);
1016 src_bvl = bio_iovec_idx(bio, segment);
1017 }
1018
1019 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
Peter Osterlund72772322006-02-14 13:52:56 -08001020 bvec[f].bv_page = src_bvl->bv_page;
1021 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
1023 pkt_copy_bio_data(bio, segment, src_offs,
Peter Osterlund72772322006-02-14 13:52:56 -08001024 bvec[f].bv_page, bvec[f].bv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 src_offs += CD_FRAMESIZE;
1027 frames_write++;
1028 }
1029 }
1030 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1031 spin_unlock(&pkt->lock);
1032
1033 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1034 frames_write, (unsigned long long)pkt->sector);
1035 BUG_ON(frames_write != pkt->write_size);
1036
1037 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
Peter Osterlund72772322006-02-14 13:52:56 -08001038 pkt_make_local_copy(pkt, bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 pkt->cache_valid = 1;
1040 } else {
1041 pkt->cache_valid = 0;
1042 }
1043
1044 /* Start the write request */
1045 bio_init(pkt->w_bio);
1046 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1047 pkt->w_bio->bi_sector = pkt->sector;
1048 pkt->w_bio->bi_bdev = pd->bdev;
1049 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1050 pkt->w_bio->bi_private = pkt;
Peter Osterlund72772322006-02-14 13:52:56 -08001051 for (f = 0; f < pkt->frames; f++)
1052 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1053 BUG();
Thomas Maier78220822006-10-04 02:15:28 -07001054 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 atomic_set(&pkt->io_wait, 1);
1057 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001058 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1062{
1063 struct bio *bio, *next;
1064
1065 if (!uptodate)
1066 pkt->cache_valid = 0;
1067
1068 /* Finish all bios corresponding to this packet */
1069 bio = pkt->orig_bios;
1070 while (bio) {
1071 next = bio->bi_next;
1072 bio->bi_next = NULL;
1073 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1074 bio = next;
1075 }
1076 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1077}
1078
1079static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1080{
1081 int uptodate;
1082
1083 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1084
1085 for (;;) {
1086 switch (pkt->state) {
1087 case PACKET_WAITING_STATE:
1088 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1089 return;
1090
1091 pkt->sleep_time = 0;
1092 pkt_gather_data(pd, pkt);
1093 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1094 break;
1095
1096 case PACKET_READ_WAIT_STATE:
1097 if (atomic_read(&pkt->io_wait) > 0)
1098 return;
1099
1100 if (atomic_read(&pkt->io_errors) > 0) {
1101 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1102 } else {
1103 pkt_start_write(pd, pkt);
1104 }
1105 break;
1106
1107 case PACKET_WRITE_WAIT_STATE:
1108 if (atomic_read(&pkt->io_wait) > 0)
1109 return;
1110
1111 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1112 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1113 } else {
1114 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1115 }
1116 break;
1117
1118 case PACKET_RECOVERY_STATE:
1119 if (pkt_start_recovery(pkt)) {
1120 pkt_start_write(pd, pkt);
1121 } else {
1122 VPRINTK("No recovery possible\n");
1123 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1124 }
1125 break;
1126
1127 case PACKET_FINISHED_STATE:
1128 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1129 pkt_finish_packet(pkt, uptodate);
1130 return;
1131
1132 default:
1133 BUG();
1134 break;
1135 }
1136 }
1137}
1138
1139static void pkt_handle_packets(struct pktcdvd_device *pd)
1140{
1141 struct packet_data *pkt, *next;
1142
1143 VPRINTK("pkt_handle_packets\n");
1144
1145 /*
1146 * Run state machine for active packets
1147 */
1148 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1149 if (atomic_read(&pkt->run_sm) > 0) {
1150 atomic_set(&pkt->run_sm, 0);
1151 pkt_run_state_machine(pd, pkt);
1152 }
1153 }
1154
1155 /*
1156 * Move no longer active packets to the free list
1157 */
1158 spin_lock(&pd->cdrw.active_list_lock);
1159 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1160 if (pkt->state == PACKET_FINISHED_STATE) {
1161 list_del(&pkt->list);
1162 pkt_put_packet_data(pd, pkt);
1163 pkt_set_state(pkt, PACKET_IDLE_STATE);
1164 atomic_set(&pd->scan_queue, 1);
1165 }
1166 }
1167 spin_unlock(&pd->cdrw.active_list_lock);
1168}
1169
1170static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1171{
1172 struct packet_data *pkt;
1173 int i;
1174
Peter Osterlundae7642b2005-11-13 16:06:36 -08001175 for (i = 0; i < PACKET_NUM_STATES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 states[i] = 0;
1177
1178 spin_lock(&pd->cdrw.active_list_lock);
1179 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1180 states[pkt->state]++;
1181 }
1182 spin_unlock(&pd->cdrw.active_list_lock);
1183}
1184
1185/*
1186 * kcdrwd is woken up when writes have been queued for one of our
1187 * registered devices
1188 */
1189static int kcdrwd(void *foobar)
1190{
1191 struct pktcdvd_device *pd = foobar;
1192 struct packet_data *pkt;
1193 long min_sleep_time, residue;
1194
1195 set_user_nice(current, -20);
1196
1197 for (;;) {
1198 DECLARE_WAITQUEUE(wait, current);
1199
1200 /*
1201 * Wait until there is something to do
1202 */
1203 add_wait_queue(&pd->wqueue, &wait);
1204 for (;;) {
1205 set_current_state(TASK_INTERRUPTIBLE);
1206
1207 /* Check if we need to run pkt_handle_queue */
1208 if (atomic_read(&pd->scan_queue) > 0)
1209 goto work_to_do;
1210
1211 /* Check if we need to run the state machine for some packet */
1212 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1213 if (atomic_read(&pkt->run_sm) > 0)
1214 goto work_to_do;
1215 }
1216
1217 /* Check if we need to process the iosched queues */
1218 if (atomic_read(&pd->iosched.attention) != 0)
1219 goto work_to_do;
1220
1221 /* Otherwise, go to sleep */
1222 if (PACKET_DEBUG > 1) {
1223 int states[PACKET_NUM_STATES];
1224 pkt_count_states(pd, states);
1225 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1226 states[0], states[1], states[2], states[3],
1227 states[4], states[5]);
1228 }
1229
1230 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1231 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1232 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1233 min_sleep_time = pkt->sleep_time;
1234 }
1235
1236 generic_unplug_device(bdev_get_queue(pd->bdev));
1237
1238 VPRINTK("kcdrwd: sleeping\n");
1239 residue = schedule_timeout(min_sleep_time);
1240 VPRINTK("kcdrwd: wake up\n");
1241
1242 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001243 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1246 if (!pkt->sleep_time)
1247 continue;
1248 pkt->sleep_time -= min_sleep_time - residue;
1249 if (pkt->sleep_time <= 0) {
1250 pkt->sleep_time = 0;
1251 atomic_inc(&pkt->run_sm);
1252 }
1253 }
1254
1255 if (signal_pending(current)) {
1256 flush_signals(current);
1257 }
1258 if (kthread_should_stop())
1259 break;
1260 }
1261work_to_do:
1262 set_current_state(TASK_RUNNING);
1263 remove_wait_queue(&pd->wqueue, &wait);
1264
1265 if (kthread_should_stop())
1266 break;
1267
1268 /*
1269 * if pkt_handle_queue returns true, we can queue
1270 * another request.
1271 */
1272 while (pkt_handle_queue(pd))
1273 ;
1274
1275 /*
1276 * Handle packet state machine
1277 */
1278 pkt_handle_packets(pd);
1279
1280 /*
1281 * Handle iosched queues
1282 */
1283 pkt_iosched_process_queue(pd);
1284 }
1285
1286 return 0;
1287}
1288
1289static void pkt_print_settings(struct pktcdvd_device *pd)
1290{
Thomas Maier78220822006-10-04 02:15:28 -07001291 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 printk("%u blocks, ", pd->settings.size >> 2);
1293 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1294}
1295
1296static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1297{
1298 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1299
1300 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1301 cgc->cmd[2] = page_code | (page_control << 6);
1302 cgc->cmd[7] = cgc->buflen >> 8;
1303 cgc->cmd[8] = cgc->buflen & 0xff;
1304 cgc->data_direction = CGC_DATA_READ;
1305 return pkt_generic_packet(pd, cgc);
1306}
1307
1308static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1309{
1310 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1311 memset(cgc->buffer, 0, 2);
1312 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1313 cgc->cmd[1] = 0x10; /* PF */
1314 cgc->cmd[7] = cgc->buflen >> 8;
1315 cgc->cmd[8] = cgc->buflen & 0xff;
1316 cgc->data_direction = CGC_DATA_WRITE;
1317 return pkt_generic_packet(pd, cgc);
1318}
1319
1320static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1321{
1322 struct packet_command cgc;
1323 int ret;
1324
1325 /* set up command and get the disc info */
1326 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1327 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1328 cgc.cmd[8] = cgc.buflen = 2;
1329 cgc.quiet = 1;
1330
1331 if ((ret = pkt_generic_packet(pd, &cgc)))
1332 return ret;
1333
1334 /* not all drives have the same disc_info length, so requeue
1335 * packet with the length the drive tells us it can supply
1336 */
1337 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1338 sizeof(di->disc_information_length);
1339
1340 if (cgc.buflen > sizeof(disc_information))
1341 cgc.buflen = sizeof(disc_information);
1342
1343 cgc.cmd[8] = cgc.buflen;
1344 return pkt_generic_packet(pd, &cgc);
1345}
1346
1347static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1348{
1349 struct packet_command cgc;
1350 int ret;
1351
1352 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1353 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1354 cgc.cmd[1] = type & 3;
1355 cgc.cmd[4] = (track & 0xff00) >> 8;
1356 cgc.cmd[5] = track & 0xff;
1357 cgc.cmd[8] = 8;
1358 cgc.quiet = 1;
1359
1360 if ((ret = pkt_generic_packet(pd, &cgc)))
1361 return ret;
1362
1363 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1364 sizeof(ti->track_information_length);
1365
1366 if (cgc.buflen > sizeof(track_information))
1367 cgc.buflen = sizeof(track_information);
1368
1369 cgc.cmd[8] = cgc.buflen;
1370 return pkt_generic_packet(pd, &cgc);
1371}
1372
1373static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1374{
1375 disc_information di;
1376 track_information ti;
1377 __u32 last_track;
1378 int ret = -1;
1379
1380 if ((ret = pkt_get_disc_info(pd, &di)))
1381 return ret;
1382
1383 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1384 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1385 return ret;
1386
1387 /* if this track is blank, try the previous. */
1388 if (ti.blank) {
1389 last_track--;
1390 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1391 return ret;
1392 }
1393
1394 /* if last recorded field is valid, return it. */
1395 if (ti.lra_v) {
1396 *last_written = be32_to_cpu(ti.last_rec_address);
1397 } else {
1398 /* make it up instead */
1399 *last_written = be32_to_cpu(ti.track_start) +
1400 be32_to_cpu(ti.track_size);
1401 if (ti.free_blocks)
1402 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1403 }
1404 return 0;
1405}
1406
1407/*
1408 * write mode select package based on pd->settings
1409 */
1410static int pkt_set_write_settings(struct pktcdvd_device *pd)
1411{
1412 struct packet_command cgc;
1413 struct request_sense sense;
1414 write_param_page *wp;
1415 char buffer[128];
1416 int ret, size;
1417
1418 /* doesn't apply to DVD+RW or DVD-RAM */
1419 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1420 return 0;
1421
1422 memset(buffer, 0, sizeof(buffer));
1423 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1424 cgc.sense = &sense;
1425 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1426 pkt_dump_sense(&cgc);
1427 return ret;
1428 }
1429
1430 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1431 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1432 if (size > sizeof(buffer))
1433 size = sizeof(buffer);
1434
1435 /*
1436 * now get it all
1437 */
1438 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1439 cgc.sense = &sense;
1440 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1441 pkt_dump_sense(&cgc);
1442 return ret;
1443 }
1444
1445 /*
1446 * write page is offset header + block descriptor length
1447 */
1448 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1449
1450 wp->fp = pd->settings.fp;
1451 wp->track_mode = pd->settings.track_mode;
1452 wp->write_type = pd->settings.write_type;
1453 wp->data_block_type = pd->settings.block_mode;
1454
1455 wp->multi_session = 0;
1456
1457#ifdef PACKET_USE_LS
1458 wp->link_size = 7;
1459 wp->ls_v = 1;
1460#endif
1461
1462 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1463 wp->session_format = 0;
1464 wp->subhdr2 = 0x20;
1465 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1466 wp->session_format = 0x20;
1467 wp->subhdr2 = 8;
1468#if 0
1469 wp->mcn[0] = 0x80;
1470 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1471#endif
1472 } else {
1473 /*
1474 * paranoia
1475 */
Thomas Maier78220822006-10-04 02:15:28 -07001476 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 1;
1478 }
1479 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1480
1481 cgc.buflen = cgc.cmd[8] = size;
1482 if ((ret = pkt_mode_select(pd, &cgc))) {
1483 pkt_dump_sense(&cgc);
1484 return ret;
1485 }
1486
1487 pkt_print_settings(pd);
1488 return 0;
1489}
1490
1491/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001492 * 1 -- we can write to this track, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001494static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Peter Osterlundab863ec2006-02-20 18:28:04 -08001496 switch (pd->mmc3_profile) {
1497 case 0x1a: /* DVD+RW */
1498 case 0x12: /* DVD-RAM */
1499 /* The track is always writable on DVD+RW/DVD-RAM */
1500 return 1;
1501 default:
1502 break;
1503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Peter Osterlundab863ec2006-02-20 18:28:04 -08001505 if (!ti->packet || !ti->fp)
1506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 /*
1509 * "good" settings as per Mt Fuji.
1510 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001511 if (ti->rt == 0 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001512 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Peter Osterlundab863ec2006-02-20 18:28:04 -08001514 if (ti->rt == 0 && ti->blank == 1)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001515 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Peter Osterlundab863ec2006-02-20 18:28:04 -08001517 if (ti->rt == 1 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001518 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Thomas Maier78220822006-10-04 02:15:28 -07001520 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001525 * 1 -- we can write to this disc, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001527static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 switch (pd->mmc3_profile) {
1530 case 0x0a: /* CD-RW */
1531 case 0xffff: /* MMC3 not supported */
1532 break;
1533 case 0x1a: /* DVD+RW */
1534 case 0x13: /* DVD-RW */
1535 case 0x12: /* DVD-RAM */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001536 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 default:
Thomas Maier78220822006-10-04 02:15:28 -07001538 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
1542 /*
1543 * for disc type 0xff we should probably reserve a new track.
1544 * but i'm not sure, should we leave this to user apps? probably.
1545 */
1546 if (di->disc_type == 0xff) {
Thomas Maier78220822006-10-04 02:15:28 -07001547 printk(DRIVER_NAME": Unknown disc. No track?\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
1551 if (di->disc_type != 0x20 && di->disc_type != 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001552 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
1556 if (di->erasable == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001557 printk(DRIVER_NAME": Disc not erasable\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
1561 if (di->border_status == PACKET_SESSION_RESERVED) {
Thomas Maier78220822006-10-04 02:15:28 -07001562 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565
Peter Osterlund7c613d52006-02-20 18:28:02 -08001566 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
1569static int pkt_probe_settings(struct pktcdvd_device *pd)
1570{
1571 struct packet_command cgc;
1572 unsigned char buf[12];
1573 disc_information di;
1574 track_information ti;
1575 int ret, track;
1576
1577 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1578 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1579 cgc.cmd[8] = 8;
1580 ret = pkt_generic_packet(pd, &cgc);
1581 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1582
1583 memset(&di, 0, sizeof(disc_information));
1584 memset(&ti, 0, sizeof(track_information));
1585
1586 if ((ret = pkt_get_disc_info(pd, &di))) {
1587 printk("failed get_disc\n");
1588 return ret;
1589 }
1590
Peter Osterlund7c613d52006-02-20 18:28:02 -08001591 if (!pkt_writable_disc(pd, &di))
Peter Osterlund9db91542006-02-20 18:28:04 -08001592 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1595
1596 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1597 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
Thomas Maier78220822006-10-04 02:15:28 -07001598 printk(DRIVER_NAME": failed get_track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return ret;
1600 }
1601
Peter Osterlundab863ec2006-02-20 18:28:04 -08001602 if (!pkt_writable_track(pd, &ti)) {
Thomas Maier78220822006-10-04 02:15:28 -07001603 printk(DRIVER_NAME": can't write to this track\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08001604 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
1607 /*
1608 * we keep packet size in 512 byte units, makes it easier to
1609 * deal with request calculations.
1610 */
1611 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1612 if (pd->settings.size == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001613 printk(DRIVER_NAME": detected zero packet size!\n");
Phillip Susia460ad62006-02-04 23:27:44 -08001614 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07001616 if (pd->settings.size > PACKET_MAX_SECTORS) {
Thomas Maier78220822006-10-04 02:15:28 -07001617 printk(DRIVER_NAME": packet size is too big\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08001618 return -EROFS;
Peter Osterlundd0272e72005-09-13 01:25:27 -07001619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 pd->settings.fp = ti.fp;
1621 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1622
1623 if (ti.nwa_v) {
1624 pd->nwa = be32_to_cpu(ti.next_writable);
1625 set_bit(PACKET_NWA_VALID, &pd->flags);
1626 }
1627
1628 /*
1629 * in theory we could use lra on -RW media as well and just zero
1630 * blocks that haven't been written yet, but in practice that
1631 * is just a no-go. we'll use that for -R, naturally.
1632 */
1633 if (ti.lra_v) {
1634 pd->lra = be32_to_cpu(ti.last_rec_address);
1635 set_bit(PACKET_LRA_VALID, &pd->flags);
1636 } else {
1637 pd->lra = 0xffffffff;
1638 set_bit(PACKET_LRA_VALID, &pd->flags);
1639 }
1640
1641 /*
1642 * fine for now
1643 */
1644 pd->settings.link_loss = 7;
1645 pd->settings.write_type = 0; /* packet */
1646 pd->settings.track_mode = ti.track_mode;
1647
1648 /*
1649 * mode1 or mode2 disc
1650 */
1651 switch (ti.data_mode) {
1652 case PACKET_MODE1:
1653 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1654 break;
1655 case PACKET_MODE2:
1656 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1657 break;
1658 default:
Thomas Maier78220822006-10-04 02:15:28 -07001659 printk(DRIVER_NAME": unknown data mode\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08001660 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662 return 0;
1663}
1664
1665/*
1666 * enable/disable write caching on drive
1667 */
1668static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1669{
1670 struct packet_command cgc;
1671 struct request_sense sense;
1672 unsigned char buf[64];
1673 int ret;
1674
1675 memset(buf, 0, sizeof(buf));
1676 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1677 cgc.sense = &sense;
1678 cgc.buflen = pd->mode_offset + 12;
1679
1680 /*
1681 * caching mode page might not be there, so quiet this command
1682 */
1683 cgc.quiet = 1;
1684
1685 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1686 return ret;
1687
1688 buf[pd->mode_offset + 10] |= (!!set << 2);
1689
1690 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1691 ret = pkt_mode_select(pd, &cgc);
1692 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07001693 printk(DRIVER_NAME": write caching control failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 pkt_dump_sense(&cgc);
1695 } else if (!ret && set)
Thomas Maier78220822006-10-04 02:15:28 -07001696 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return ret;
1698}
1699
1700static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1701{
1702 struct packet_command cgc;
1703
1704 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1705 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1706 cgc.cmd[4] = lockflag ? 1 : 0;
1707 return pkt_generic_packet(pd, &cgc);
1708}
1709
1710/*
1711 * Returns drive maximum write speed
1712 */
1713static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1714{
1715 struct packet_command cgc;
1716 struct request_sense sense;
1717 unsigned char buf[256+18];
1718 unsigned char *cap_buf;
1719 int ret, offset;
1720
1721 memset(buf, 0, sizeof(buf));
1722 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1723 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1724 cgc.sense = &sense;
1725
1726 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1727 if (ret) {
1728 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1729 sizeof(struct mode_page_header);
1730 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1731 if (ret) {
1732 pkt_dump_sense(&cgc);
1733 return ret;
1734 }
1735 }
1736
1737 offset = 20; /* Obsoleted field, used by older drives */
1738 if (cap_buf[1] >= 28)
1739 offset = 28; /* Current write speed selected */
1740 if (cap_buf[1] >= 30) {
1741 /* If the drive reports at least one "Logical Unit Write
1742 * Speed Performance Descriptor Block", use the information
1743 * in the first block. (contains the highest speed)
1744 */
1745 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1746 if (num_spdb > 0)
1747 offset = 34;
1748 }
1749
1750 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1751 return 0;
1752}
1753
1754/* These tables from cdrecord - I don't have orange book */
1755/* standard speed CD-RW (1-4x) */
1756static char clv_to_speed[16] = {
1757 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1758 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1759};
1760/* high speed CD-RW (-10x) */
1761static char hs_clv_to_speed[16] = {
1762 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1763 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1764};
1765/* ultra high speed CD-RW */
1766static char us_clv_to_speed[16] = {
1767 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1768 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1769};
1770
1771/*
1772 * reads the maximum media speed from ATIP
1773 */
1774static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1775{
1776 struct packet_command cgc;
1777 struct request_sense sense;
1778 unsigned char buf[64];
1779 unsigned int size, st, sp;
1780 int ret;
1781
1782 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1783 cgc.sense = &sense;
1784 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1785 cgc.cmd[1] = 2;
1786 cgc.cmd[2] = 4; /* READ ATIP */
1787 cgc.cmd[8] = 2;
1788 ret = pkt_generic_packet(pd, &cgc);
1789 if (ret) {
1790 pkt_dump_sense(&cgc);
1791 return ret;
1792 }
1793 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1794 if (size > sizeof(buf))
1795 size = sizeof(buf);
1796
1797 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1798 cgc.sense = &sense;
1799 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1800 cgc.cmd[1] = 2;
1801 cgc.cmd[2] = 4;
1802 cgc.cmd[8] = size;
1803 ret = pkt_generic_packet(pd, &cgc);
1804 if (ret) {
1805 pkt_dump_sense(&cgc);
1806 return ret;
1807 }
1808
1809 if (!buf[6] & 0x40) {
Thomas Maier78220822006-10-04 02:15:28 -07001810 printk(DRIVER_NAME": Disc type is not CD-RW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 1;
1812 }
1813 if (!buf[6] & 0x4) {
Thomas Maier78220822006-10-04 02:15:28 -07001814 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 1;
1816 }
1817
1818 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1819
1820 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1821
1822 /* Info from cdrecord */
1823 switch (st) {
1824 case 0: /* standard speed */
1825 *speed = clv_to_speed[sp];
1826 break;
1827 case 1: /* high speed */
1828 *speed = hs_clv_to_speed[sp];
1829 break;
1830 case 2: /* ultra high speed */
1831 *speed = us_clv_to_speed[sp];
1832 break;
1833 default:
Thomas Maier78220822006-10-04 02:15:28 -07001834 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 1;
1836 }
1837 if (*speed) {
Thomas Maier78220822006-10-04 02:15:28 -07001838 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
1840 } else {
Thomas Maier78220822006-10-04 02:15:28 -07001841 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 1;
1843 }
1844}
1845
1846static int pkt_perform_opc(struct pktcdvd_device *pd)
1847{
1848 struct packet_command cgc;
1849 struct request_sense sense;
1850 int ret;
1851
Thomas Maier78220822006-10-04 02:15:28 -07001852 VPRINTK(DRIVER_NAME": Performing OPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1855 cgc.sense = &sense;
1856 cgc.timeout = 60*HZ;
1857 cgc.cmd[0] = GPCMD_SEND_OPC;
1858 cgc.cmd[1] = 1;
1859 if ((ret = pkt_generic_packet(pd, &cgc)))
1860 pkt_dump_sense(&cgc);
1861 return ret;
1862}
1863
1864static int pkt_open_write(struct pktcdvd_device *pd)
1865{
1866 int ret;
1867 unsigned int write_speed, media_write_speed, read_speed;
1868
1869 if ((ret = pkt_probe_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07001870 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
Peter Osterlund9db91542006-02-20 18:28:04 -08001871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873
1874 if ((ret = pkt_set_write_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07001875 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return -EIO;
1877 }
1878
1879 pkt_write_caching(pd, USE_WCACHING);
1880
1881 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1882 write_speed = 16 * 177;
1883 switch (pd->mmc3_profile) {
1884 case 0x13: /* DVD-RW */
1885 case 0x1a: /* DVD+RW */
1886 case 0x12: /* DVD-RAM */
Thomas Maier78220822006-10-04 02:15:28 -07001887 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 break;
1889 default:
1890 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1891 media_write_speed = 16;
1892 write_speed = min(write_speed, media_write_speed * 177);
Thomas Maier78220822006-10-04 02:15:28 -07001893 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895 }
1896 read_speed = write_speed;
1897
1898 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
Thomas Maier78220822006-10-04 02:15:28 -07001899 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EIO;
1901 }
1902 pd->write_speed = write_speed;
1903 pd->read_speed = read_speed;
1904
1905 if ((ret = pkt_perform_opc(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07001906 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
1909 return 0;
1910}
1911
1912/*
1913 * called at open time.
1914 */
1915static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1916{
1917 int ret;
1918 long lba;
1919 request_queue_t *q;
1920
1921 /*
1922 * We need to re-open the cdrom device without O_NONBLOCK to be able
1923 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1924 * so bdget() can't fail.
1925 */
1926 bdget(pd->bdev->bd_dev);
1927 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1928 goto out;
1929
Peter Osterlund8382bf22006-01-08 01:02:17 -08001930 if ((ret = bd_claim(pd->bdev, pd)))
1931 goto out_putdev;
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if ((ret = pkt_get_last_written(pd, &lba))) {
Thomas Maier78220822006-10-04 02:15:28 -07001934 printk(DRIVER_NAME": pkt_get_last_written failed\n");
Peter Osterlund8382bf22006-01-08 01:02:17 -08001935 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
1938 set_capacity(pd->disk, lba << 2);
1939 set_capacity(pd->bdev->bd_disk, lba << 2);
1940 bd_set_size(pd->bdev, (loff_t)lba << 11);
1941
1942 q = bdev_get_queue(pd->bdev);
1943 if (write) {
1944 if ((ret = pkt_open_write(pd)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08001945 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /*
1947 * Some CDRW drives can not handle writes larger than one packet,
1948 * even if the size is a multiple of the packet size.
1949 */
1950 spin_lock_irq(q->queue_lock);
1951 blk_queue_max_sectors(q, pd->settings.size);
1952 spin_unlock_irq(q->queue_lock);
1953 set_bit(PACKET_WRITABLE, &pd->flags);
1954 } else {
1955 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1956 clear_bit(PACKET_WRITABLE, &pd->flags);
1957 }
1958
1959 if ((ret = pkt_set_segment_merging(pd, q)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08001960 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001962 if (write) {
1963 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
Thomas Maier78220822006-10-04 02:15:28 -07001964 printk(DRIVER_NAME": not enough memory for buffers\n");
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001965 ret = -ENOMEM;
1966 goto out_unclaim;
1967 }
Thomas Maier78220822006-10-04 02:15:28 -07001968 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 return 0;
1972
Peter Osterlund8382bf22006-01-08 01:02:17 -08001973out_unclaim:
1974 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975out_putdev:
1976 blkdev_put(pd->bdev);
1977out:
1978 return ret;
1979}
1980
1981/*
1982 * called when the device is closed. makes sure that the device flushes
1983 * the internal cache before we close.
1984 */
1985static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
1986{
1987 if (flush && pkt_flush_cache(pd))
Thomas Maier78220822006-10-04 02:15:28 -07001988 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 pkt_lock_door(pd, 0);
1991
1992 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
Peter Osterlund8382bf22006-01-08 01:02:17 -08001993 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 blkdev_put(pd->bdev);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001995
1996 pkt_shrink_pktlist(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2000{
2001 if (dev_minor >= MAX_WRITERS)
2002 return NULL;
2003 return pkt_devs[dev_minor];
2004}
2005
2006static int pkt_open(struct inode *inode, struct file *file)
2007{
2008 struct pktcdvd_device *pd = NULL;
2009 int ret;
2010
Thomas Maier78220822006-10-04 02:15:28 -07002011 VPRINTK(DRIVER_NAME": entering open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Jes Sorensen1657f822006-03-23 03:00:25 -08002013 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 pd = pkt_find_dev_from_minor(iminor(inode));
2015 if (!pd) {
2016 ret = -ENODEV;
2017 goto out;
2018 }
2019 BUG_ON(pd->refcnt < 0);
2020
2021 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002022 if (pd->refcnt > 1) {
2023 if ((file->f_mode & FMODE_WRITE) &&
2024 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2025 ret = -EBUSY;
2026 goto out_dec;
2027 }
2028 } else {
Peter Osterlund01fd9fd2006-02-14 13:52:55 -08002029 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2030 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /*
2033 * needed here as well, since ext2 (among others) may change
2034 * the blocksize at mount time
2035 */
2036 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2037 }
2038
Jes Sorensen1657f822006-03-23 03:00:25 -08002039 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return 0;
2041
2042out_dec:
2043 pd->refcnt--;
2044out:
Thomas Maier78220822006-10-04 02:15:28 -07002045 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
Jes Sorensen1657f822006-03-23 03:00:25 -08002046 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return ret;
2048}
2049
2050static int pkt_close(struct inode *inode, struct file *file)
2051{
2052 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2053 int ret = 0;
2054
Jes Sorensen1657f822006-03-23 03:00:25 -08002055 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 pd->refcnt--;
2057 BUG_ON(pd->refcnt < 0);
2058 if (pd->refcnt == 0) {
2059 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2060 pkt_release_dev(pd, flush);
2061 }
Jes Sorensen1657f822006-03-23 03:00:25 -08002062 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return ret;
2064}
2065
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2068{
2069 struct packet_stacked_data *psd = bio->bi_private;
2070 struct pktcdvd_device *pd = psd->pd;
2071
2072 if (bio->bi_size)
2073 return 1;
2074
2075 bio_put(bio);
2076 bio_endio(psd->bio, psd->bio->bi_size, err);
2077 mempool_free(psd, psd_pool);
2078 pkt_bio_finished(pd);
2079 return 0;
2080}
2081
2082static int pkt_make_request(request_queue_t *q, struct bio *bio)
2083{
2084 struct pktcdvd_device *pd;
2085 char b[BDEVNAME_SIZE];
2086 sector_t zone;
2087 struct packet_data *pkt;
2088 int was_empty, blocked_bio;
2089 struct pkt_rb_node *node;
2090
2091 pd = q->queuedata;
2092 if (!pd) {
Thomas Maier78220822006-10-04 02:15:28 -07002093 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 goto end_io;
2095 }
2096
2097 /*
2098 * Clone READ bios so we can have our own bi_end_io callback.
2099 */
2100 if (bio_data_dir(bio) == READ) {
2101 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2102 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2103
2104 psd->pd = pd;
2105 psd->bio = bio;
2106 cloned_bio->bi_bdev = pd->bdev;
2107 cloned_bio->bi_private = psd;
2108 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2109 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002110 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return 0;
2112 }
2113
2114 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
Thomas Maier78220822006-10-04 02:15:28 -07002115 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 pd->name, (unsigned long long)bio->bi_sector);
2117 goto end_io;
2118 }
2119
2120 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
Thomas Maier78220822006-10-04 02:15:28 -07002121 printk(DRIVER_NAME": wrong bio size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto end_io;
2123 }
2124
2125 blk_queue_bounce(q, &bio);
2126
2127 zone = ZONE(bio->bi_sector, pd);
2128 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2129 (unsigned long long)bio->bi_sector,
2130 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2131
2132 /* Check if we have to split the bio */
2133 {
2134 struct bio_pair *bp;
2135 sector_t last_zone;
2136 int first_sectors;
2137
2138 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2139 if (last_zone != zone) {
2140 BUG_ON(last_zone != zone + pd->settings.size);
2141 first_sectors = last_zone - bio->bi_sector;
2142 bp = bio_split(bio, bio_split_pool, first_sectors);
2143 BUG_ON(!bp);
2144 pkt_make_request(q, &bp->bio1);
2145 pkt_make_request(q, &bp->bio2);
2146 bio_pair_release(bp);
2147 return 0;
2148 }
2149 }
2150
2151 /*
2152 * If we find a matching packet in state WAITING or READ_WAIT, we can
2153 * just append this bio to that packet.
2154 */
2155 spin_lock(&pd->cdrw.active_list_lock);
2156 blocked_bio = 0;
2157 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2158 if (pkt->sector == zone) {
2159 spin_lock(&pkt->lock);
2160 if ((pkt->state == PACKET_WAITING_STATE) ||
2161 (pkt->state == PACKET_READ_WAIT_STATE)) {
2162 pkt_add_list_last(bio, &pkt->orig_bios,
2163 &pkt->orig_bios_tail);
2164 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2165 if ((pkt->write_size >= pkt->frames) &&
2166 (pkt->state == PACKET_WAITING_STATE)) {
2167 atomic_inc(&pkt->run_sm);
2168 wake_up(&pd->wqueue);
2169 }
2170 spin_unlock(&pkt->lock);
2171 spin_unlock(&pd->cdrw.active_list_lock);
2172 return 0;
2173 } else {
2174 blocked_bio = 1;
2175 }
2176 spin_unlock(&pkt->lock);
2177 }
2178 }
2179 spin_unlock(&pd->cdrw.active_list_lock);
2180
2181 /*
2182 * No matching packet found. Store the bio in the work queue.
2183 */
2184 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 node->bio = bio;
2186 spin_lock(&pd->lock);
2187 BUG_ON(pd->bio_queue_size < 0);
2188 was_empty = (pd->bio_queue_size == 0);
2189 pkt_rbtree_insert(pd, node);
2190 spin_unlock(&pd->lock);
2191
2192 /*
2193 * Wake up the worker thread.
2194 */
2195 atomic_set(&pd->scan_queue, 1);
2196 if (was_empty) {
2197 /* This wake_up is required for correct operation */
2198 wake_up(&pd->wqueue);
2199 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2200 /*
2201 * This wake up is not required for correct operation,
2202 * but improves performance in some cases.
2203 */
2204 wake_up(&pd->wqueue);
2205 }
2206 return 0;
2207end_io:
2208 bio_io_error(bio, bio->bi_size);
2209 return 0;
2210}
2211
2212
2213
2214static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2215{
2216 struct pktcdvd_device *pd = q->queuedata;
2217 sector_t zone = ZONE(bio->bi_sector, pd);
2218 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2219 int remaining = (pd->settings.size << 9) - used;
2220 int remaining2;
2221
2222 /*
2223 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2224 * boundary, pkt_make_request() will split the bio.
2225 */
2226 remaining2 = PAGE_SIZE - bio->bi_size;
2227 remaining = max(remaining, remaining2);
2228
2229 BUG_ON(remaining < 0);
2230 return remaining;
2231}
2232
2233static void pkt_init_queue(struct pktcdvd_device *pd)
2234{
2235 request_queue_t *q = pd->disk->queue;
2236
2237 blk_queue_make_request(q, pkt_make_request);
2238 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2239 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2240 blk_queue_merge_bvec(q, pkt_merge_bvec);
2241 q->queuedata = pd;
2242}
2243
2244static int pkt_seq_show(struct seq_file *m, void *p)
2245{
2246 struct pktcdvd_device *pd = m->private;
2247 char *msg;
2248 char bdev_buf[BDEVNAME_SIZE];
2249 int states[PACKET_NUM_STATES];
2250
2251 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2252 bdevname(pd->bdev, bdev_buf));
2253
2254 seq_printf(m, "\nSettings:\n");
2255 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2256
2257 if (pd->settings.write_type == 0)
2258 msg = "Packet";
2259 else
2260 msg = "Unknown";
2261 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2262
2263 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2264 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2265
2266 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2267
2268 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2269 msg = "Mode 1";
2270 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2271 msg = "Mode 2";
2272 else
2273 msg = "Unknown";
2274 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2275
2276 seq_printf(m, "\nStatistics:\n");
2277 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2278 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2279 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2280 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2281 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2282
2283 seq_printf(m, "\nMisc:\n");
2284 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2285 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2286 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2287 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2288 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2289 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2290
2291 seq_printf(m, "\nQueue state:\n");
2292 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2293 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2294 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2295
2296 pkt_count_states(pd, states);
2297 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2298 states[0], states[1], states[2], states[3], states[4], states[5]);
2299
2300 return 0;
2301}
2302
2303static int pkt_seq_open(struct inode *inode, struct file *file)
2304{
2305 return single_open(file, pkt_seq_show, PDE(inode)->data);
2306}
2307
2308static struct file_operations pkt_proc_fops = {
2309 .open = pkt_seq_open,
2310 .read = seq_read,
2311 .llseek = seq_lseek,
2312 .release = single_release
2313};
2314
2315static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2316{
2317 int i;
2318 int ret = 0;
2319 char b[BDEVNAME_SIZE];
2320 struct proc_dir_entry *proc;
2321 struct block_device *bdev;
2322
2323 if (pd->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002324 printk(DRIVER_NAME": Recursive setup not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return -EBUSY;
2326 }
2327 for (i = 0; i < MAX_WRITERS; i++) {
2328 struct pktcdvd_device *pd2 = pkt_devs[i];
2329 if (!pd2)
2330 continue;
2331 if (pd2->bdev->bd_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002332 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return -EBUSY;
2334 }
2335 if (pd2->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002336 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return -EBUSY;
2338 }
2339 }
2340
2341 bdev = bdget(dev);
2342 if (!bdev)
2343 return -ENOMEM;
2344 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2345 if (ret)
2346 return ret;
2347
2348 /* This is safe, since we have a reference from open(). */
2349 __module_get(THIS_MODULE);
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 pd->bdev = bdev;
2352 set_blocksize(bdev, CD_FRAMESIZE);
2353
2354 pkt_init_queue(pd);
2355
2356 atomic_set(&pd->cdrw.pending_bios, 0);
2357 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2358 if (IS_ERR(pd->cdrw.thread)) {
Thomas Maier78220822006-10-04 02:15:28 -07002359 printk(DRIVER_NAME": can't start kernel thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 ret = -ENOMEM;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002361 goto out_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363
2364 proc = create_proc_entry(pd->name, 0, pkt_proc);
2365 if (proc) {
2366 proc->data = pd;
2367 proc->proc_fops = &pkt_proc_fops;
2368 }
Thomas Maier78220822006-10-04 02:15:28 -07002369 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return 0;
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372out_mem:
2373 blkdev_put(bdev);
2374 /* This is safe: open() is still holding a reference. */
2375 module_put(THIS_MODULE);
2376 return ret;
2377}
2378
2379static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2380{
2381 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2382
2383 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 switch (cmd) {
2386 /*
2387 * forward selected CDROM ioctls to CD-ROM, for UDF
2388 */
2389 case CDROMMULTISESSION:
2390 case CDROMREADTOCENTRY:
2391 case CDROM_LAST_WRITTEN:
2392 case CDROM_SEND_PACKET:
2393 case SCSI_IOCTL_SEND_COMMAND:
Peter Osterlund118326e2005-05-14 00:58:30 -07002394 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 case CDROMEJECT:
2397 /*
2398 * The door gets locked when the device is opened, so we
2399 * have to unlock it or else the eject command fails.
2400 */
Peter Osterlund948423e2006-02-14 13:52:56 -08002401 if (pd->refcnt == 1)
2402 pkt_lock_door(pd, 0);
Peter Osterlund118326e2005-05-14 00:58:30 -07002403 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 default:
Thomas Maier78220822006-10-04 02:15:28 -07002406 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return -ENOTTY;
2408 }
2409
2410 return 0;
2411}
2412
2413static int pkt_media_changed(struct gendisk *disk)
2414{
2415 struct pktcdvd_device *pd = disk->private_data;
2416 struct gendisk *attached_disk;
2417
2418 if (!pd)
2419 return 0;
2420 if (!pd->bdev)
2421 return 0;
2422 attached_disk = pd->bdev->bd_disk;
2423 if (!attached_disk)
2424 return 0;
2425 return attached_disk->fops->media_changed(attached_disk);
2426}
2427
2428static struct block_device_operations pktcdvd_ops = {
2429 .owner = THIS_MODULE,
2430 .open = pkt_open,
2431 .release = pkt_close,
2432 .ioctl = pkt_ioctl,
2433 .media_changed = pkt_media_changed,
2434};
2435
2436/*
2437 * Set up mapping from pktcdvd device to CD-ROM device.
2438 */
2439static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2440{
2441 int idx;
2442 int ret = -ENOMEM;
2443 struct pktcdvd_device *pd;
2444 struct gendisk *disk;
2445 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2446
2447 for (idx = 0; idx < MAX_WRITERS; idx++)
2448 if (!pkt_devs[idx])
2449 break;
2450 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002451 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return -EBUSY;
2453 }
2454
Peter Osterlund1107d2e2005-09-13 01:25:29 -07002455 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (!pd)
2457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002459 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2460 sizeof(struct pkt_rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (!pd->rb_pool)
2462 goto out_mem;
2463
2464 disk = alloc_disk(1);
2465 if (!disk)
2466 goto out_mem;
2467 pd->disk = disk;
2468
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002469 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2470 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2471 spin_lock_init(&pd->cdrw.active_list_lock);
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 spin_lock_init(&pd->lock);
2474 spin_lock_init(&pd->iosched.lock);
Thomas Maier78220822006-10-04 02:15:28 -07002475 sprintf(pd->name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 init_waitqueue_head(&pd->wqueue);
2477 pd->bio_queue = RB_ROOT;
2478
Thomas Maieradd21662006-10-04 02:15:30 -07002479 disk->major = pktdev_major;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 disk->first_minor = idx;
2481 disk->fops = &pktcdvd_ops;
2482 disk->flags = GENHD_FL_REMOVABLE;
Thomas Maier78220822006-10-04 02:15:28 -07002483 sprintf(disk->disk_name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 disk->private_data = pd;
2485 disk->queue = blk_alloc_queue(GFP_KERNEL);
2486 if (!disk->queue)
2487 goto out_mem2;
2488
2489 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2490 ret = pkt_new_dev(pd, dev);
2491 if (ret)
2492 goto out_new_dev;
2493
2494 add_disk(disk);
2495 pkt_devs[idx] = pd;
2496 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2497 return 0;
2498
2499out_new_dev:
Al Viro1312f402006-03-12 11:02:03 -05002500 blk_cleanup_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501out_mem2:
2502 put_disk(disk);
2503out_mem:
2504 if (pd->rb_pool)
2505 mempool_destroy(pd->rb_pool);
2506 kfree(pd);
2507 return ret;
2508}
2509
2510/*
2511 * Tear down mapping from pktcdvd device to CD-ROM device.
2512 */
2513static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2514{
2515 struct pktcdvd_device *pd;
2516 int idx;
2517 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2518
2519 for (idx = 0; idx < MAX_WRITERS; idx++) {
2520 pd = pkt_devs[idx];
2521 if (pd && (pd->pkt_dev == pkt_dev))
2522 break;
2523 }
2524 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002525 DPRINTK(DRIVER_NAME": dev not setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return -ENXIO;
2527 }
2528
2529 if (pd->refcnt > 0)
2530 return -EBUSY;
2531
2532 if (!IS_ERR(pd->cdrw.thread))
2533 kthread_stop(pd->cdrw.thread);
2534
2535 blkdev_put(pd->bdev);
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 remove_proc_entry(pd->name, pkt_proc);
Thomas Maier78220822006-10-04 02:15:28 -07002538 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 del_gendisk(pd->disk);
Al Viro1312f402006-03-12 11:02:03 -05002541 blk_cleanup_queue(pd->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 put_disk(pd->disk);
2543
2544 pkt_devs[idx] = NULL;
2545 mempool_destroy(pd->rb_pool);
2546 kfree(pd);
2547
2548 /* This is safe: open() is still holding a reference. */
2549 module_put(THIS_MODULE);
2550 return 0;
2551}
2552
2553static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2554{
2555 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2556 if (pd) {
2557 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2558 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2559 } else {
2560 ctrl_cmd->dev = 0;
2561 ctrl_cmd->pkt_dev = 0;
2562 }
2563 ctrl_cmd->num_devices = MAX_WRITERS;
2564}
2565
2566static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2567{
2568 void __user *argp = (void __user *)arg;
2569 struct pkt_ctrl_command ctrl_cmd;
2570 int ret = 0;
2571
2572 if (cmd != PACKET_CTRL_CMD)
2573 return -ENOTTY;
2574
2575 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2576 return -EFAULT;
2577
2578 switch (ctrl_cmd.command) {
2579 case PKT_CTRL_CMD_SETUP:
2580 if (!capable(CAP_SYS_ADMIN))
2581 return -EPERM;
Arjan van de Ven8e185d82006-07-30 03:03:30 -07002582 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 ret = pkt_setup_dev(&ctrl_cmd);
Jes Sorensen1657f822006-03-23 03:00:25 -08002584 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 break;
2586 case PKT_CTRL_CMD_TEARDOWN:
2587 if (!capable(CAP_SYS_ADMIN))
2588 return -EPERM;
Arjan van de Ven8e185d82006-07-30 03:03:30 -07002589 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 ret = pkt_remove_dev(&ctrl_cmd);
Jes Sorensen1657f822006-03-23 03:00:25 -08002591 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 break;
2593 case PKT_CTRL_CMD_STATUS:
Arjan van de Ven8e185d82006-07-30 03:03:30 -07002594 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 pkt_get_status(&ctrl_cmd);
Jes Sorensen1657f822006-03-23 03:00:25 -08002596 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 break;
2598 default:
2599 return -ENOTTY;
2600 }
2601
2602 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2603 return -EFAULT;
2604 return ret;
2605}
2606
2607
2608static struct file_operations pkt_ctl_fops = {
2609 .ioctl = pkt_ctl_ioctl,
2610 .owner = THIS_MODULE,
2611};
2612
2613static struct miscdevice pkt_misc = {
2614 .minor = MISC_DYNAMIC_MINOR,
Thomas Maier78220822006-10-04 02:15:28 -07002615 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 .fops = &pkt_ctl_fops
2617};
2618
2619static int __init pkt_init(void)
2620{
2621 int ret;
2622
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002623 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
2624 sizeof(struct packet_stacked_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (!psd_pool)
2626 return -ENOMEM;
2627
Thomas Maieradd21662006-10-04 02:15:30 -07002628 ret = register_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (ret < 0) {
Thomas Maier78220822006-10-04 02:15:28 -07002630 printk(DRIVER_NAME": Unable to register block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 goto out2;
2632 }
Thomas Maieradd21662006-10-04 02:15:30 -07002633 if (!pktdev_major)
2634 pktdev_major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 ret = misc_register(&pkt_misc);
2637 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07002638 printk(DRIVER_NAME": Unable to register misc device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 goto out;
2640 }
2641
Jes Sorensen1657f822006-03-23 03:00:25 -08002642 mutex_init(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Thomas Maier78220822006-10-04 02:15:28 -07002644 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return 0;
2647
2648out:
Thomas Maieradd21662006-10-04 02:15:30 -07002649 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650out2:
2651 mempool_destroy(psd_pool);
2652 return ret;
2653}
2654
2655static void __exit pkt_exit(void)
2656{
Thomas Maier78220822006-10-04 02:15:28 -07002657 remove_proc_entry(DRIVER_NAME, proc_root_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 misc_deregister(&pkt_misc);
Thomas Maieradd21662006-10-04 02:15:30 -07002659 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 mempool_destroy(psd_pool);
2661}
2662
2663MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2664MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2665MODULE_LICENSE("GPL");
2666
2667module_init(pkt_init);
2668module_exit(pkt_exit);