Kobject: convert drivers/* from kobject_unregister() to kobject_put()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / block / pktcdvd.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
adb9250a 4 * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
1da177e4
LT
5 *
6 * May be copied or modified under the terms of the GNU General Public
7 * License. See linux/COPYING for more information.
8 *
a676f8d0
PO
9 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
10 * DVD-RAM devices.
1da177e4
LT
11 *
12 * Theory of operation:
13 *
a676f8d0
PO
14 * At the lowest level, there is the standard driver for the CD/DVD device,
15 * typically ide-cd.c or sr.c. This driver can handle read and write requests,
16 * but it doesn't know anything about the special restrictions that apply to
17 * packet writing. One restriction is that write requests must be aligned to
18 * packet boundaries on the physical media, and the size of a write request
19 * must be equal to the packet size. Another restriction is that a
20 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
21 * command, if the previous command was a write.
22 *
23 * The purpose of the packet writing driver is to hide these restrictions from
24 * higher layers, such as file systems, and present a block device that can be
25 * randomly read and written using 2kB-sized blocks.
26 *
27 * The lowest layer in the packet writing driver is the packet I/O scheduler.
28 * Its data is defined by the struct packet_iosched and includes two bio
29 * queues with pending read and write requests. These queues are processed
30 * by the pkt_iosched_process_queue() function. The write requests in this
31 * queue are already properly aligned and sized. This layer is responsible for
32 * issuing the flush cache commands and scheduling the I/O in a good order.
33 *
34 * The next layer transforms unaligned write requests to aligned writes. This
35 * transformation requires reading missing pieces of data from the underlying
36 * block device, assembling the pieces to full packets and queuing them to the
37 * packet I/O scheduler.
38 *
39 * At the top layer there is a custom make_request_fn function that forwards
40 * read requests directly to the iosched queue and puts write requests in the
41 * unaligned write queue. A kernel thread performs the necessary read
42 * gathering to convert the unaligned writes to aligned writes and then feeds
43 * them to the packet I/O scheduler.
1da177e4
LT
44 *
45 *************************************************************************/
46
1da177e4 47#include <linux/pktcdvd.h>
1da177e4
LT
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>
7dfb7103 58#include <linux/freezer.h>
1657f824 59#include <linux/mutex.h>
1da177e4
LT
60#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_ioctl.h>
cef28963 62#include <scsi/scsi.h>
32694850
TM
63#include <linux/debugfs.h>
64#include <linux/device.h>
1da177e4
LT
65
66#include <asm/uaccess.h>
67
7822082d
TM
68#define DRIVER_NAME "pktcdvd"
69
1da177e4
LT
70#if PACKET_DEBUG
71#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
72#else
73#define DPRINTK(fmt, args...)
74#endif
75
76#if PACKET_DEBUG > 1
77#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
78#else
79#define VPRINTK(fmt, args...)
80#endif
81
82#define MAX_SPEED 0xffff
83
84#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
85
86static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
87static struct proc_dir_entry *pkt_proc;
add21660 88static int pktdev_major;
0a0fc960
TM
89static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
90static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
1657f824 91static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
1da177e4
LT
92static mempool_t *psd_pool;
93
32694850
TM
94static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
95static struct dentry *pkt_debugfs_root = NULL; /* /debug/pktcdvd */
96
97/* forward declaration */
98static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
99static int pkt_remove_dev(dev_t pkt_dev);
100static int pkt_seq_show(struct seq_file *m, void *p);
101
102
103
104/*
105 * create and register a pktcdvd kernel object.
106 */
107static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
108 const char* name,
109 struct kobject* parent,
110 struct kobj_type* ktype)
111{
112 struct pktcdvd_kobj *p;
89c42606
GKH
113 int error;
114
32694850
TM
115 p = kzalloc(sizeof(*p), GFP_KERNEL);
116 if (!p)
117 return NULL;
32694850 118 p->pd = pd;
89c42606
GKH
119 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
120 if (error) {
d17a18dd 121 kobject_put(&p->kobj);
32694850 122 return NULL;
d17a18dd 123 }
89c42606 124 kobject_uevent(&p->kobj, KOBJ_ADD);
32694850
TM
125 return p;
126}
127/*
128 * remove a pktcdvd kernel object.
129 */
130static void pkt_kobj_remove(struct pktcdvd_kobj *p)
131{
132 if (p)
c10997f6 133 kobject_put(&p->kobj);
32694850
TM
134}
135/*
136 * default release function for pktcdvd kernel objects.
137 */
138static void pkt_kobj_release(struct kobject *kobj)
139{
140 kfree(to_pktcdvdkobj(kobj));
141}
142
143
144/**********************************************************
145 *
146 * sysfs interface for pktcdvd
147 * by (C) 2006 Thomas Maier <balagi@justmail.de>
148 *
149 **********************************************************/
150
151#define DEF_ATTR(_obj,_name,_mode) \
7b595756 152 static struct attribute _obj = { .name = _name, .mode = _mode }
32694850
TM
153
154/**********************************************************
155 /sys/class/pktcdvd/pktcdvd[0-7]/
156 stat/reset
157 stat/packets_started
158 stat/packets_finished
159 stat/kb_written
160 stat/kb_read
161 stat/kb_read_gather
162 write_queue/size
163 write_queue/congestion_off
164 write_queue/congestion_on
165 **********************************************************/
166
167DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
168DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
169DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
170DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
171DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
172DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
173
174static struct attribute *kobj_pkt_attrs_stat[] = {
175 &kobj_pkt_attr_st1,
176 &kobj_pkt_attr_st2,
177 &kobj_pkt_attr_st3,
178 &kobj_pkt_attr_st4,
179 &kobj_pkt_attr_st5,
180 &kobj_pkt_attr_st6,
181 NULL
182};
183
184DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
185DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
186DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
187
188static struct attribute *kobj_pkt_attrs_wqueue[] = {
189 &kobj_pkt_attr_wq1,
190 &kobj_pkt_attr_wq2,
191 &kobj_pkt_attr_wq3,
192 NULL
193};
194
32694850
TM
195static ssize_t kobj_pkt_show(struct kobject *kobj,
196 struct attribute *attr, char *data)
197{
198 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
199 int n = 0;
200 int v;
201 if (strcmp(attr->name, "packets_started") == 0) {
202 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
203
204 } else if (strcmp(attr->name, "packets_finished") == 0) {
205 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
206
207 } else if (strcmp(attr->name, "kb_written") == 0) {
208 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
209
210 } else if (strcmp(attr->name, "kb_read") == 0) {
211 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
212
213 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
214 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
215
216 } else if (strcmp(attr->name, "size") == 0) {
217 spin_lock(&pd->lock);
218 v = pd->bio_queue_size;
219 spin_unlock(&pd->lock);
220 n = sprintf(data, "%d\n", v);
221
222 } else if (strcmp(attr->name, "congestion_off") == 0) {
223 spin_lock(&pd->lock);
224 v = pd->write_congestion_off;
225 spin_unlock(&pd->lock);
226 n = sprintf(data, "%d\n", v);
227
228 } else if (strcmp(attr->name, "congestion_on") == 0) {
229 spin_lock(&pd->lock);
230 v = pd->write_congestion_on;
231 spin_unlock(&pd->lock);
232 n = sprintf(data, "%d\n", v);
233 }
234 return n;
235}
236
237static void init_write_congestion_marks(int* lo, int* hi)
238{
239 if (*hi > 0) {
240 *hi = max(*hi, 500);
241 *hi = min(*hi, 1000000);
242 if (*lo <= 0)
243 *lo = *hi - 100;
244 else {
245 *lo = min(*lo, *hi - 100);
246 *lo = max(*lo, 100);
247 }
248 } else {
249 *hi = -1;
250 *lo = -1;
251 }
252}
253
254static ssize_t kobj_pkt_store(struct kobject *kobj,
255 struct attribute *attr,
256 const char *data, size_t len)
257{
258 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
259 int val;
32694850 260
83f3aa3d 261 if (strcmp(attr->name, "reset") == 0 && len > 0) {
32694850
TM
262 pd->stats.pkt_started = 0;
263 pd->stats.pkt_ended = 0;
264 pd->stats.secs_w = 0;
265 pd->stats.secs_rg = 0;
266 pd->stats.secs_r = 0;
267
268 } else if (strcmp(attr->name, "congestion_off") == 0
83f3aa3d 269 && sscanf(data, "%d", &val) == 1) {
32694850
TM
270 spin_lock(&pd->lock);
271 pd->write_congestion_off = val;
272 init_write_congestion_marks(&pd->write_congestion_off,
273 &pd->write_congestion_on);
274 spin_unlock(&pd->lock);
275
276 } else if (strcmp(attr->name, "congestion_on") == 0
83f3aa3d 277 && sscanf(data, "%d", &val) == 1) {
32694850
TM
278 spin_lock(&pd->lock);
279 pd->write_congestion_on = val;
280 init_write_congestion_marks(&pd->write_congestion_off,
281 &pd->write_congestion_on);
282 spin_unlock(&pd->lock);
283 }
284 return len;
285}
286
287static struct sysfs_ops kobj_pkt_ops = {
288 .show = kobj_pkt_show,
289 .store = kobj_pkt_store
290};
291static struct kobj_type kobj_pkt_type_stat = {
292 .release = pkt_kobj_release,
293 .sysfs_ops = &kobj_pkt_ops,
294 .default_attrs = kobj_pkt_attrs_stat
295};
296static struct kobj_type kobj_pkt_type_wqueue = {
297 .release = pkt_kobj_release,
298 .sysfs_ops = &kobj_pkt_ops,
299 .default_attrs = kobj_pkt_attrs_wqueue
300};
301
302static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
303{
304 if (class_pktcdvd) {
6013c12b
TJ
305 pd->dev = device_create(class_pktcdvd, NULL, pd->pkt_dev, "%s", pd->name);
306 if (IS_ERR(pd->dev))
307 pd->dev = NULL;
32694850 308 }
6013c12b 309 if (pd->dev) {
32694850 310 pd->kobj_stat = pkt_kobj_create(pd, "stat",
6013c12b 311 &pd->dev->kobj,
32694850
TM
312 &kobj_pkt_type_stat);
313 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
6013c12b 314 &pd->dev->kobj,
32694850
TM
315 &kobj_pkt_type_wqueue);
316 }
317}
318
319static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
320{
321 pkt_kobj_remove(pd->kobj_stat);
322 pkt_kobj_remove(pd->kobj_wqueue);
323 if (class_pktcdvd)
6013c12b 324 device_destroy(class_pktcdvd, pd->pkt_dev);
32694850
TM
325}
326
327
328/********************************************************************
329 /sys/class/pktcdvd/
330 add map block device
331 remove unmap packet dev
332 device_map show mappings
333 *******************************************************************/
334
335static void class_pktcdvd_release(struct class *cls)
336{
337 kfree(cls);
338}
339static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
340{
341 int n = 0;
342 int idx;
343 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
344 for (idx = 0; idx < MAX_WRITERS; idx++) {
345 struct pktcdvd_device *pd = pkt_devs[idx];
346 if (!pd)
347 continue;
348 n += sprintf(data+n, "%s %u:%u %u:%u\n",
349 pd->name,
350 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
351 MAJOR(pd->bdev->bd_dev),
352 MINOR(pd->bdev->bd_dev));
353 }
354 mutex_unlock(&ctl_mutex);
355 return n;
356}
357
358static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
359 size_t count)
360{
361 unsigned int major, minor;
fffe487d 362
83f3aa3d 363 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
fffe487d
TH
364 /* pkt_setup_dev() expects caller to hold reference to self */
365 if (!try_module_get(THIS_MODULE))
366 return -ENODEV;
367
32694850 368 pkt_setup_dev(MKDEV(major, minor), NULL);
fffe487d
TH
369
370 module_put(THIS_MODULE);
371
32694850
TM
372 return count;
373 }
fffe487d 374
32694850
TM
375 return -EINVAL;
376}
377
378static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
379 size_t count)
380{
381 unsigned int major, minor;
83f3aa3d 382 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
32694850
TM
383 pkt_remove_dev(MKDEV(major, minor));
384 return count;
385 }
386 return -EINVAL;
387}
388
389static struct class_attribute class_pktcdvd_attrs[] = {
390 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
391 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
392 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
393 __ATTR_NULL
394};
395
396
397static int pkt_sysfs_init(void)
398{
399 int ret = 0;
400
401 /*
402 * create control files in sysfs
403 * /sys/class/pktcdvd/...
404 */
405 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
406 if (!class_pktcdvd)
407 return -ENOMEM;
408 class_pktcdvd->name = DRIVER_NAME;
409 class_pktcdvd->owner = THIS_MODULE;
410 class_pktcdvd->class_release = class_pktcdvd_release;
411 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
412 ret = class_register(class_pktcdvd);
413 if (ret) {
414 kfree(class_pktcdvd);
415 class_pktcdvd = NULL;
416 printk(DRIVER_NAME": failed to create class pktcdvd\n");
417 return ret;
418 }
419 return 0;
420}
421
422static void pkt_sysfs_cleanup(void)
423{
424 if (class_pktcdvd)
425 class_destroy(class_pktcdvd);
426 class_pktcdvd = NULL;
427}
428
429/********************************************************************
430 entries in debugfs
431
432 /debugfs/pktcdvd[0-7]/
433 info
434
435 *******************************************************************/
436
437static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
438{
439 return pkt_seq_show(m, p);
440}
441
442static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
443{
444 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
445}
446
2b8693c0 447static const struct file_operations debug_fops = {
32694850
TM
448 .open = pkt_debugfs_fops_open,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = single_release,
452 .owner = THIS_MODULE,
453};
454
455static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
456{
457 if (!pkt_debugfs_root)
458 return;
459 pd->dfs_f_info = NULL;
460 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
461 if (IS_ERR(pd->dfs_d_root)) {
462 pd->dfs_d_root = NULL;
463 return;
464 }
465 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
466 pd->dfs_d_root, pd, &debug_fops);
467 if (IS_ERR(pd->dfs_f_info)) {
468 pd->dfs_f_info = NULL;
469 return;
470 }
471}
472
473static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
474{
475 if (!pkt_debugfs_root)
476 return;
477 if (pd->dfs_f_info)
478 debugfs_remove(pd->dfs_f_info);
479 pd->dfs_f_info = NULL;
480 if (pd->dfs_d_root)
481 debugfs_remove(pd->dfs_d_root);
482 pd->dfs_d_root = NULL;
483}
484
485static void pkt_debugfs_init(void)
486{
487 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
488 if (IS_ERR(pkt_debugfs_root)) {
489 pkt_debugfs_root = NULL;
490 return;
491 }
492}
493
494static void pkt_debugfs_cleanup(void)
495{
496 if (!pkt_debugfs_root)
497 return;
498 debugfs_remove(pkt_debugfs_root);
499 pkt_debugfs_root = NULL;
500}
501
502/* ----------------------------------------------------------*/
503
1da177e4
LT
504
505static void pkt_bio_finished(struct pktcdvd_device *pd)
506{
507 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
508 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
7822082d 509 VPRINTK(DRIVER_NAME": queue empty\n");
1da177e4
LT
510 atomic_set(&pd->iosched.attention, 1);
511 wake_up(&pd->wqueue);
512 }
513}
514
515static void pkt_bio_destructor(struct bio *bio)
516{
517 kfree(bio->bi_io_vec);
518 kfree(bio);
519}
520
521static struct bio *pkt_bio_alloc(int nr_iovecs)
522{
523 struct bio_vec *bvl = NULL;
524 struct bio *bio;
525
526 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
527 if (!bio)
528 goto no_bio;
529 bio_init(bio);
530
1107d2e0 531 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
1da177e4
LT
532 if (!bvl)
533 goto no_bvl;
1da177e4
LT
534
535 bio->bi_max_vecs = nr_iovecs;
536 bio->bi_io_vec = bvl;
537 bio->bi_destructor = pkt_bio_destructor;
538
539 return bio;
540
541 no_bvl:
542 kfree(bio);
543 no_bio:
544 return NULL;
545}
546
547/*
548 * Allocate a packet_data struct
549 */
e1bc89bc 550static struct packet_data *pkt_alloc_packet_data(int frames)
1da177e4
LT
551{
552 int i;
553 struct packet_data *pkt;
554
1107d2e0 555 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
1da177e4
LT
556 if (!pkt)
557 goto no_pkt;
1da177e4 558
e1bc89bc
PO
559 pkt->frames = frames;
560 pkt->w_bio = pkt_bio_alloc(frames);
1da177e4
LT
561 if (!pkt->w_bio)
562 goto no_bio;
563
e1bc89bc 564 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
1da177e4
LT
565 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
566 if (!pkt->pages[i])
567 goto no_page;
568 }
569
570 spin_lock_init(&pkt->lock);
571
e1bc89bc 572 for (i = 0; i < frames; i++) {
1da177e4
LT
573 struct bio *bio = pkt_bio_alloc(1);
574 if (!bio)
575 goto no_rd_bio;
576 pkt->r_bios[i] = bio;
577 }
578
579 return pkt;
580
581no_rd_bio:
e1bc89bc 582 for (i = 0; i < frames; i++) {
1da177e4
LT
583 struct bio *bio = pkt->r_bios[i];
584 if (bio)
585 bio_put(bio);
586 }
587
588no_page:
e1bc89bc 589 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
1da177e4
LT
590 if (pkt->pages[i])
591 __free_page(pkt->pages[i]);
592 bio_put(pkt->w_bio);
593no_bio:
594 kfree(pkt);
595no_pkt:
596 return NULL;
597}
598
599/*
600 * Free a packet_data struct
601 */
602static void pkt_free_packet_data(struct packet_data *pkt)
603{
604 int i;
605
e1bc89bc 606 for (i = 0; i < pkt->frames; i++) {
1da177e4
LT
607 struct bio *bio = pkt->r_bios[i];
608 if (bio)
609 bio_put(bio);
610 }
e1bc89bc 611 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
1da177e4
LT
612 __free_page(pkt->pages[i]);
613 bio_put(pkt->w_bio);
614 kfree(pkt);
615}
616
617static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
618{
619 struct packet_data *pkt, *next;
620
621 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
622
623 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
624 pkt_free_packet_data(pkt);
625 }
e1bc89bc 626 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
1da177e4
LT
627}
628
629static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
630{
631 struct packet_data *pkt;
632
e1bc89bc
PO
633 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
634
1da177e4 635 while (nr_packets > 0) {
e1bc89bc 636 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
1da177e4
LT
637 if (!pkt) {
638 pkt_shrink_pktlist(pd);
639 return 0;
640 }
641 pkt->id = nr_packets;
642 pkt->pd = pd;
643 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
644 nr_packets--;
645 }
646 return 1;
647}
648
1da177e4
LT
649static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
650{
651 struct rb_node *n = rb_next(&node->rb_node);
652 if (!n)
653 return NULL;
654 return rb_entry(n, struct pkt_rb_node, rb_node);
655}
656
ac893963 657static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
1da177e4
LT
658{
659 rb_erase(&node->rb_node, &pd->bio_queue);
660 mempool_free(node, pd->rb_pool);
661 pd->bio_queue_size--;
662 BUG_ON(pd->bio_queue_size < 0);
663}
664
665/*
666 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
667 */
668static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
669{
670 struct rb_node *n = pd->bio_queue.rb_node;
671 struct rb_node *next;
672 struct pkt_rb_node *tmp;
673
674 if (!n) {
675 BUG_ON(pd->bio_queue_size > 0);
676 return NULL;
677 }
678
679 for (;;) {
680 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
681 if (s <= tmp->bio->bi_sector)
682 next = n->rb_left;
683 else
684 next = n->rb_right;
685 if (!next)
686 break;
687 n = next;
688 }
689
690 if (s > tmp->bio->bi_sector) {
691 tmp = pkt_rbtree_next(tmp);
692 if (!tmp)
693 return NULL;
694 }
695 BUG_ON(s > tmp->bio->bi_sector);
696 return tmp;
697}
698
699/*
700 * Insert a node into the pd->bio_queue rb tree.
701 */
702static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
703{
704 struct rb_node **p = &pd->bio_queue.rb_node;
705 struct rb_node *parent = NULL;
706 sector_t s = node->bio->bi_sector;
707 struct pkt_rb_node *tmp;
708
709 while (*p) {
710 parent = *p;
711 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
712 if (s < tmp->bio->bi_sector)
713 p = &(*p)->rb_left;
714 else
715 p = &(*p)->rb_right;
716 }
717 rb_link_node(&node->rb_node, parent, p);
718 rb_insert_color(&node->rb_node, &pd->bio_queue);
719 pd->bio_queue_size++;
720}
721
722/*
723 * Add a bio to a single linked list defined by its head and tail pointers.
724 */
ac893963 725static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
1da177e4
LT
726{
727 bio->bi_next = NULL;
728 if (*list_tail) {
729 BUG_ON((*list_head) == NULL);
730 (*list_tail)->bi_next = bio;
731 (*list_tail) = bio;
732 } else {
733 BUG_ON((*list_head) != NULL);
734 (*list_head) = bio;
735 (*list_tail) = bio;
736 }
737}
738
739/*
740 * Remove and return the first bio from a single linked list defined by its
741 * head and tail pointers.
742 */
743static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
744{
745 struct bio *bio;
746
747 if (*list_head == NULL)
748 return NULL;
749
750 bio = *list_head;
751 *list_head = bio->bi_next;
752 if (*list_head == NULL)
753 *list_tail = NULL;
754
755 bio->bi_next = NULL;
756 return bio;
757}
758
759/*
760 * Send a packet_command to the underlying block device and
761 * wait for completion.
762 */
763static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
764{
165125e1 765 struct request_queue *q = bdev_get_queue(pd->bdev);
1da177e4 766 struct request *rq;
406c9b60
CH
767 int ret = 0;
768
769 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
770 WRITE : READ, __GFP_WAIT);
771
772 if (cgc->buflen) {
773 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
774 goto out;
775 }
1da177e4 776
91e4ee38 777 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
406c9b60
CH
778 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
779 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
780 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
1da177e4 781
1da177e4 782 rq->timeout = 60*HZ;
4aff5e23
JA
783 rq->cmd_type = REQ_TYPE_BLOCK_PC;
784 rq->cmd_flags |= REQ_HARDBARRIER;
1da177e4 785 if (cgc->quiet)
4aff5e23 786 rq->cmd_flags |= REQ_QUIET;
1da177e4 787
406c9b60 788 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
cbc31a47
AM
789 if (rq->errors)
790 ret = -EIO;
406c9b60 791out:
1da177e4 792 blk_put_request(rq);
406c9b60 793 return ret;
1da177e4
LT
794}
795
796/*
797 * A generic sense dump / resolve mechanism should be implemented across
798 * all ATAPI + SCSI devices.
799 */
800static void pkt_dump_sense(struct packet_command *cgc)
801{
802 static char *info[9] = { "No sense", "Recovered error", "Not ready",
803 "Medium error", "Hardware error", "Illegal request",
804 "Unit attention", "Data protect", "Blank check" };
805 int i;
806 struct request_sense *sense = cgc->sense;
807
7822082d 808 printk(DRIVER_NAME":");
1da177e4
LT
809 for (i = 0; i < CDROM_PACKET_SIZE; i++)
810 printk(" %02x", cgc->cmd[i]);
811 printk(" - ");
812
813 if (sense == NULL) {
814 printk("no sense\n");
815 return;
816 }
817
818 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
819
820 if (sense->sense_key > 8) {
821 printk(" (INVALID)\n");
822 return;
823 }
824
825 printk(" (%s)\n", info[sense->sense_key]);
826}
827
828/*
829 * flush the drive cache to media
830 */
831static int pkt_flush_cache(struct pktcdvd_device *pd)
832{
833 struct packet_command cgc;
834
835 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
836 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
837 cgc.quiet = 1;
838
839 /*
840 * the IMMED bit -- we default to not setting it, although that
841 * would allow a much faster close, this is safer
842 */
843#if 0
844 cgc.cmd[1] = 1 << 1;
845#endif
846 return pkt_generic_packet(pd, &cgc);
847}
848
849/*
850 * speed is given as the normal factor, e.g. 4 for 4x
851 */
852static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
853{
854 struct packet_command cgc;
855 struct request_sense sense;
856 int ret;
857
858 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
859 cgc.sense = &sense;
860 cgc.cmd[0] = GPCMD_SET_SPEED;
861 cgc.cmd[2] = (read_speed >> 8) & 0xff;
862 cgc.cmd[3] = read_speed & 0xff;
863 cgc.cmd[4] = (write_speed >> 8) & 0xff;
864 cgc.cmd[5] = write_speed & 0xff;
865
866 if ((ret = pkt_generic_packet(pd, &cgc)))
867 pkt_dump_sense(&cgc);
868
869 return ret;
870}
871
872/*
873 * Queue a bio for processing by the low-level CD device. Must be called
874 * from process context.
875 */
46c271be 876static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
1da177e4
LT
877{
878 spin_lock(&pd->iosched.lock);
879 if (bio_data_dir(bio) == READ) {
880 pkt_add_list_last(bio, &pd->iosched.read_queue,
881 &pd->iosched.read_queue_tail);
1da177e4
LT
882 } else {
883 pkt_add_list_last(bio, &pd->iosched.write_queue,
884 &pd->iosched.write_queue_tail);
885 }
886 spin_unlock(&pd->iosched.lock);
887
888 atomic_set(&pd->iosched.attention, 1);
889 wake_up(&pd->wqueue);
890}
891
892/*
893 * Process the queued read/write requests. This function handles special
894 * requirements for CDRW drives:
895 * - A cache flush command must be inserted before a read request if the
896 * previous request was a write.
46c271be 897 * - Switching between reading and writing is slow, so don't do it more often
1da177e4 898 * than necessary.
46c271be
PO
899 * - Optimize for throughput at the expense of latency. This means that streaming
900 * writes will never be interrupted by a read, but if the drive has to seek
901 * before the next write, switch to reading instead if there are any pending
902 * read requests.
1da177e4
LT
903 * - Set the read speed according to current usage pattern. When only reading
904 * from the device, it's best to use the highest possible read speed, but
905 * when switching often between reading and writing, it's better to have the
906 * same read and write speeds.
1da177e4
LT
907 */
908static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
909{
1da177e4
LT
910
911 if (atomic_read(&pd->iosched.attention) == 0)
912 return;
913 atomic_set(&pd->iosched.attention, 0);
914
1da177e4
LT
915 for (;;) {
916 struct bio *bio;
46c271be 917 int reads_queued, writes_queued;
1da177e4
LT
918
919 spin_lock(&pd->iosched.lock);
920 reads_queued = (pd->iosched.read_queue != NULL);
921 writes_queued = (pd->iosched.write_queue != NULL);
1da177e4
LT
922 spin_unlock(&pd->iosched.lock);
923
924 if (!reads_queued && !writes_queued)
925 break;
926
927 if (pd->iosched.writing) {
46c271be
PO
928 int need_write_seek = 1;
929 spin_lock(&pd->iosched.lock);
930 bio = pd->iosched.write_queue;
931 spin_unlock(&pd->iosched.lock);
932 if (bio && (bio->bi_sector == pd->iosched.last_write))
933 need_write_seek = 0;
934 if (need_write_seek && reads_queued) {
1da177e4 935 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
7822082d 936 VPRINTK(DRIVER_NAME": write, waiting\n");
1da177e4
LT
937 break;
938 }
939 pkt_flush_cache(pd);
940 pd->iosched.writing = 0;
941 }
942 } else {
943 if (!reads_queued && writes_queued) {
944 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
7822082d 945 VPRINTK(DRIVER_NAME": read, waiting\n");
1da177e4
LT
946 break;
947 }
948 pd->iosched.writing = 1;
949 }
950 }
951
952 spin_lock(&pd->iosched.lock);
953 if (pd->iosched.writing) {
954 bio = pkt_get_list_first(&pd->iosched.write_queue,
955 &pd->iosched.write_queue_tail);
956 } else {
957 bio = pkt_get_list_first(&pd->iosched.read_queue,
958 &pd->iosched.read_queue_tail);
959 }
960 spin_unlock(&pd->iosched.lock);
961
962 if (!bio)
963 continue;
964
965 if (bio_data_dir(bio) == READ)
966 pd->iosched.successive_reads += bio->bi_size >> 10;
46c271be 967 else {
1da177e4 968 pd->iosched.successive_reads = 0;
46c271be
PO
969 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
970 }
1da177e4
LT
971 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
972 if (pd->read_speed == pd->write_speed) {
973 pd->read_speed = MAX_SPEED;
974 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
975 }
976 } else {
977 if (pd->read_speed != pd->write_speed) {
978 pd->read_speed = pd->write_speed;
979 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
980 }
981 }
982
983 atomic_inc(&pd->cdrw.pending_bios);
984 generic_make_request(bio);
985 }
986}
987
988/*
989 * Special care is needed if the underlying block device has a small
990 * max_phys_segments value.
991 */
165125e1 992static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
1da177e4
LT
993{
994 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
995 /*
996 * The cdrom device can handle one segment/frame
997 */
998 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
999 return 0;
1000 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
1001 /*
1002 * We can handle this case at the expense of some extra memory
1003 * copies during write operations
1004 */
1005 set_bit(PACKET_MERGE_SEGS, &pd->flags);
1006 return 0;
1007 } else {
7822082d 1008 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
1da177e4
LT
1009 return -EIO;
1010 }
1011}
1012
1013/*
1014 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1015 */
1016static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
1017{
1018 unsigned int copy_size = CD_FRAMESIZE;
1019
1020 while (copy_size > 0) {
1021 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
1022 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
1023 src_bvl->bv_offset + offs;
1024 void *vto = page_address(dst_page) + dst_offs;
1025 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
1026
1027 BUG_ON(len < 0);
1028 memcpy(vto, vfrom, len);
1029 kunmap_atomic(vfrom, KM_USER0);
1030
1031 seg++;
1032 offs = 0;
1033 dst_offs += len;
1034 copy_size -= len;
1035 }
1036}
1037
1038/*
1039 * Copy all data for this packet to pkt->pages[], so that
1040 * a) The number of required segments for the write bio is minimized, which
1041 * is necessary for some scsi controllers.
1042 * b) The data can be used as cache to avoid read requests if we receive a
1043 * new write request for the same zone.
1044 */
72772323 1045static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
1da177e4
LT
1046{
1047 int f, p, offs;
1048
1049 /* Copy all data to pkt->pages[] */
1050 p = 0;
1051 offs = 0;
1052 for (f = 0; f < pkt->frames; f++) {
72772323
PO
1053 if (bvec[f].bv_page != pkt->pages[p]) {
1054 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
1da177e4
LT
1055 void *vto = page_address(pkt->pages[p]) + offs;
1056 memcpy(vto, vfrom, CD_FRAMESIZE);
1057 kunmap_atomic(vfrom, KM_USER0);
72772323
PO
1058 bvec[f].bv_page = pkt->pages[p];
1059 bvec[f].bv_offset = offs;
1da177e4 1060 } else {
72772323 1061 BUG_ON(bvec[f].bv_offset != offs);
1da177e4
LT
1062 }
1063 offs += CD_FRAMESIZE;
1064 if (offs >= PAGE_SIZE) {
1da177e4
LT
1065 offs = 0;
1066 p++;
1067 }
1068 }
1069}
1070
6712ecf8 1071static void pkt_end_io_read(struct bio *bio, int err)
1da177e4
LT
1072{
1073 struct packet_data *pkt = bio->bi_private;
1074 struct pktcdvd_device *pd = pkt->pd;
1075 BUG_ON(!pd);
1076
1da177e4
LT
1077 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1078 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1079
1080 if (err)
1081 atomic_inc(&pkt->io_errors);
1082 if (atomic_dec_and_test(&pkt->io_wait)) {
1083 atomic_inc(&pkt->run_sm);
1084 wake_up(&pd->wqueue);
1085 }
1086 pkt_bio_finished(pd);
1da177e4
LT
1087}
1088
6712ecf8 1089static void pkt_end_io_packet_write(struct bio *bio, int err)
1da177e4
LT
1090{
1091 struct packet_data *pkt = bio->bi_private;
1092 struct pktcdvd_device *pd = pkt->pd;
1093 BUG_ON(!pd);
1094
1da177e4
LT
1095 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1096
1097 pd->stats.pkt_ended++;
1098
1099 pkt_bio_finished(pd);
1100 atomic_dec(&pkt->io_wait);
1101 atomic_inc(&pkt->run_sm);
1102 wake_up(&pd->wqueue);
1da177e4
LT
1103}
1104
1105/*
1106 * Schedule reads for the holes in a packet
1107 */
1108static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1109{
1110 int frames_read = 0;
1111 struct bio *bio;
1112 int f;
1113 char written[PACKET_MAX_SIZE];
1114
1115 BUG_ON(!pkt->orig_bios);
1116
1117 atomic_set(&pkt->io_wait, 0);
1118 atomic_set(&pkt->io_errors, 0);
1119
1da177e4
LT
1120 /*
1121 * Figure out which frames we need to read before we can write.
1122 */
1123 memset(written, 0, sizeof(written));
1124 spin_lock(&pkt->lock);
1125 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1126 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1127 int num_frames = bio->bi_size / CD_FRAMESIZE;
06e7ab53 1128 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
1da177e4
LT
1129 BUG_ON(first_frame < 0);
1130 BUG_ON(first_frame + num_frames > pkt->frames);
1131 for (f = first_frame; f < first_frame + num_frames; f++)
1132 written[f] = 1;
1133 }
1134 spin_unlock(&pkt->lock);
1135
06e7ab53
PO
1136 if (pkt->cache_valid) {
1137 VPRINTK("pkt_gather_data: zone %llx cached\n",
1138 (unsigned long long)pkt->sector);
1139 goto out_account;
1140 }
1141
1da177e4
LT
1142 /*
1143 * Schedule reads for missing parts of the packet.
1144 */
1145 for (f = 0; f < pkt->frames; f++) {
761a15e7
JA
1146 struct bio_vec *vec;
1147
1da177e4
LT
1148 int p, offset;
1149 if (written[f])
1150 continue;
1151 bio = pkt->r_bios[f];
761a15e7 1152 vec = bio->bi_io_vec;
1da177e4
LT
1153 bio_init(bio);
1154 bio->bi_max_vecs = 1;
1155 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1156 bio->bi_bdev = pd->bdev;
1157 bio->bi_end_io = pkt_end_io_read;
1158 bio->bi_private = pkt;
761a15e7 1159 bio->bi_io_vec = vec;
7e3da6c4 1160 bio->bi_destructor = pkt_bio_destructor;
1da177e4
LT
1161
1162 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1163 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1164 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1165 f, pkt->pages[p], offset);
1166 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1167 BUG();
1168
1169 atomic_inc(&pkt->io_wait);
1170 bio->bi_rw = READ;
46c271be 1171 pkt_queue_bio(pd, bio);
1da177e4
LT
1172 frames_read++;
1173 }
1174
1175out_account:
1176 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1177 frames_read, (unsigned long long)pkt->sector);
1178 pd->stats.pkt_started++;
1179 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
1da177e4
LT
1180}
1181
1182/*
1183 * Find a packet matching zone, or the least recently used packet if
1184 * there is no match.
1185 */
1186static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1187{
1188 struct packet_data *pkt;
1189
1190 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1191 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1192 list_del_init(&pkt->list);
1193 if (pkt->sector != zone)
1194 pkt->cache_valid = 0;
610827de 1195 return pkt;
1da177e4
LT
1196 }
1197 }
610827de
PO
1198 BUG();
1199 return NULL;
1da177e4
LT
1200}
1201
1202static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1203{
1204 if (pkt->cache_valid) {
1205 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1206 } else {
1207 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1208 }
1209}
1210
1211/*
1212 * recover a failed write, query for relocation if possible
1213 *
1214 * returns 1 if recovery is possible, or 0 if not
1215 *
1216 */
1217static int pkt_start_recovery(struct packet_data *pkt)
1218{
1219 /*
1220 * FIXME. We need help from the file system to implement
1221 * recovery handling.
1222 */
1223 return 0;
1224#if 0
1225 struct request *rq = pkt->rq;
1226 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1227 struct block_device *pkt_bdev;
1228 struct super_block *sb = NULL;
1229 unsigned long old_block, new_block;
1230 sector_t new_sector;
1231
1232 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1233 if (pkt_bdev) {
1234 sb = get_super(pkt_bdev);
1235 bdput(pkt_bdev);
1236 }
1237
1238 if (!sb)
1239 return 0;
1240
1241 if (!sb->s_op || !sb->s_op->relocate_blocks)
1242 goto out;
1243
1244 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1245 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1246 goto out;
1247
1248 new_sector = new_block * (CD_FRAMESIZE >> 9);
1249 pkt->sector = new_sector;
1250
1251 pkt->bio->bi_sector = new_sector;
1252 pkt->bio->bi_next = NULL;
1253 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1254 pkt->bio->bi_idx = 0;
1255
1256 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1257 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1258 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1259 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1260 BUG_ON(pkt->bio->bi_private != pkt);
1261
1262 drop_super(sb);
1263 return 1;
1264
1265out:
1266 drop_super(sb);
1267 return 0;
1268#endif
1269}
1270
1271static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1272{
1273#if PACKET_DEBUG > 1
1274 static const char *state_name[] = {
1275 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1276 };
1277 enum packet_data_state old_state = pkt->state;
1278 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1279 state_name[old_state], state_name[state]);
1280#endif
1281 pkt->state = state;
1282}
1283
1284/*
1285 * Scan the work queue to see if we can start a new packet.
1286 * returns non-zero if any work was done.
1287 */
1288static int pkt_handle_queue(struct pktcdvd_device *pd)
1289{
1290 struct packet_data *pkt, *p;
1291 struct bio *bio = NULL;
1292 sector_t zone = 0; /* Suppress gcc warning */
1293 struct pkt_rb_node *node, *first_node;
1294 struct rb_node *n;
0a0fc960 1295 int wakeup;
1da177e4
LT
1296
1297 VPRINTK("handle_queue\n");
1298
1299 atomic_set(&pd->scan_queue, 0);
1300
1301 if (list_empty(&pd->cdrw.pkt_free_list)) {
1302 VPRINTK("handle_queue: no pkt\n");
1303 return 0;
1304 }
1305
1306 /*
1307 * Try to find a zone we are not already working on.
1308 */
1309 spin_lock(&pd->lock);
1310 first_node = pkt_rbtree_find(pd, pd->current_sector);
1311 if (!first_node) {
1312 n = rb_first(&pd->bio_queue);
1313 if (n)
1314 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1315 }
1316 node = first_node;
1317 while (node) {
1318 bio = node->bio;
1319 zone = ZONE(bio->bi_sector, pd);
1320 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
7baeb6a5
PO
1321 if (p->sector == zone) {
1322 bio = NULL;
1da177e4 1323 goto try_next_bio;
7baeb6a5 1324 }
1da177e4
LT
1325 }
1326 break;
1327try_next_bio:
1328 node = pkt_rbtree_next(node);
1329 if (!node) {
1330 n = rb_first(&pd->bio_queue);
1331 if (n)
1332 node = rb_entry(n, struct pkt_rb_node, rb_node);
1333 }
1334 if (node == first_node)
1335 node = NULL;
1336 }
1337 spin_unlock(&pd->lock);
1338 if (!bio) {
1339 VPRINTK("handle_queue: no bio\n");
1340 return 0;
1341 }
1342
1343 pkt = pkt_get_packet_data(pd, zone);
1da177e4
LT
1344
1345 pd->current_sector = zone + pd->settings.size;
1346 pkt->sector = zone;
e1bc89bc 1347 BUG_ON(pkt->frames != pd->settings.size >> 2);
1da177e4
LT
1348 pkt->write_size = 0;
1349
1350 /*
1351 * Scan work queue for bios in the same zone and link them
1352 * to this packet.
1353 */
1354 spin_lock(&pd->lock);
1355 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1356 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1357 bio = node->bio;
1358 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1359 (unsigned long long)ZONE(bio->bi_sector, pd));
1360 if (ZONE(bio->bi_sector, pd) != zone)
1361 break;
1362 pkt_rbtree_erase(pd, node);
1363 spin_lock(&pkt->lock);
1364 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1365 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1366 spin_unlock(&pkt->lock);
1367 }
0a0fc960
TM
1368 /* check write congestion marks, and if bio_queue_size is
1369 below, wake up any waiters */
1370 wakeup = (pd->write_congestion_on > 0
1371 && pd->bio_queue_size <= pd->write_congestion_off);
1da177e4 1372 spin_unlock(&pd->lock);
0a0fc960 1373 if (wakeup)
83f3aa3d 1374 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
1da177e4
LT
1375
1376 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1377 pkt_set_state(pkt, PACKET_WAITING_STATE);
1378 atomic_set(&pkt->run_sm, 1);
1379
1380 spin_lock(&pd->cdrw.active_list_lock);
1381 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1382 spin_unlock(&pd->cdrw.active_list_lock);
1383
1384 return 1;
1385}
1386
1387/*
1388 * Assemble a bio to write one packet and queue the bio for processing
1389 * by the underlying block device.
1390 */
1391static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1392{
1393 struct bio *bio;
1da177e4
LT
1394 int f;
1395 int frames_write;
72772323 1396 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
1da177e4
LT
1397
1398 for (f = 0; f < pkt->frames; f++) {
72772323
PO
1399 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1400 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1da177e4
LT
1401 }
1402
1403 /*
72772323 1404 * Fill-in bvec with data from orig_bios.
1da177e4
LT
1405 */
1406 frames_write = 0;
1407 spin_lock(&pkt->lock);
1408 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1409 int segment = bio->bi_idx;
1410 int src_offs = 0;
1411 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1412 int num_frames = bio->bi_size / CD_FRAMESIZE;
1413 BUG_ON(first_frame < 0);
1414 BUG_ON(first_frame + num_frames > pkt->frames);
1415 for (f = first_frame; f < first_frame + num_frames; f++) {
1416 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1417
1418 while (src_offs >= src_bvl->bv_len) {
1419 src_offs -= src_bvl->bv_len;
1420 segment++;
1421 BUG_ON(segment >= bio->bi_vcnt);
1422 src_bvl = bio_iovec_idx(bio, segment);
1423 }
1424
1425 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
72772323
PO
1426 bvec[f].bv_page = src_bvl->bv_page;
1427 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
1da177e4
LT
1428 } else {
1429 pkt_copy_bio_data(bio, segment, src_offs,
72772323 1430 bvec[f].bv_page, bvec[f].bv_offset);
1da177e4
LT
1431 }
1432 src_offs += CD_FRAMESIZE;
1433 frames_write++;
1434 }
1435 }
1436 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1437 spin_unlock(&pkt->lock);
1438
1439 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1440 frames_write, (unsigned long long)pkt->sector);
1441 BUG_ON(frames_write != pkt->write_size);
1442
1443 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
72772323 1444 pkt_make_local_copy(pkt, bvec);
1da177e4
LT
1445 pkt->cache_valid = 1;
1446 } else {
1447 pkt->cache_valid = 0;
1448 }
1449
1450 /* Start the write request */
1451 bio_init(pkt->w_bio);
1452 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1453 pkt->w_bio->bi_sector = pkt->sector;
1454 pkt->w_bio->bi_bdev = pd->bdev;
1455 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1456 pkt->w_bio->bi_private = pkt;
761a15e7 1457 pkt->w_bio->bi_io_vec = bvec;
7e3da6c4 1458 pkt->w_bio->bi_destructor = pkt_bio_destructor;
72772323
PO
1459 for (f = 0; f < pkt->frames; f++)
1460 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1461 BUG();
7822082d 1462 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
1da177e4
LT
1463
1464 atomic_set(&pkt->io_wait, 1);
1465 pkt->w_bio->bi_rw = WRITE;
46c271be 1466 pkt_queue_bio(pd, pkt->w_bio);
1da177e4
LT
1467}
1468
1469static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1470{
1471 struct bio *bio, *next;
1472
1473 if (!uptodate)
1474 pkt->cache_valid = 0;
1475
1476 /* Finish all bios corresponding to this packet */
1477 bio = pkt->orig_bios;
1478 while (bio) {
1479 next = bio->bi_next;
1480 bio->bi_next = NULL;
6712ecf8 1481 bio_endio(bio, uptodate ? 0 : -EIO);
1da177e4
LT
1482 bio = next;
1483 }
1484 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1485}
1486
1487static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1488{
1489 int uptodate;
1490
1491 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1492
1493 for (;;) {
1494 switch (pkt->state) {
1495 case PACKET_WAITING_STATE:
1496 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1497 return;
1498
1499 pkt->sleep_time = 0;
1500 pkt_gather_data(pd, pkt);
1501 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1502 break;
1503
1504 case PACKET_READ_WAIT_STATE:
1505 if (atomic_read(&pkt->io_wait) > 0)
1506 return;
1507
1508 if (atomic_read(&pkt->io_errors) > 0) {
1509 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1510 } else {
1511 pkt_start_write(pd, pkt);
1512 }
1513 break;
1514
1515 case PACKET_WRITE_WAIT_STATE:
1516 if (atomic_read(&pkt->io_wait) > 0)
1517 return;
1518
1519 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1520 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1521 } else {
1522 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1523 }
1524 break;
1525
1526 case PACKET_RECOVERY_STATE:
1527 if (pkt_start_recovery(pkt)) {
1528 pkt_start_write(pd, pkt);
1529 } else {
1530 VPRINTK("No recovery possible\n");
1531 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1532 }
1533 break;
1534
1535 case PACKET_FINISHED_STATE:
1536 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1537 pkt_finish_packet(pkt, uptodate);
1538 return;
1539
1540 default:
1541 BUG();
1542 break;
1543 }
1544 }
1545}
1546
1547static void pkt_handle_packets(struct pktcdvd_device *pd)
1548{
1549 struct packet_data *pkt, *next;
1550
1551 VPRINTK("pkt_handle_packets\n");
1552
1553 /*
1554 * Run state machine for active packets
1555 */
1556 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1557 if (atomic_read(&pkt->run_sm) > 0) {
1558 atomic_set(&pkt->run_sm, 0);
1559 pkt_run_state_machine(pd, pkt);
1560 }
1561 }
1562
1563 /*
1564 * Move no longer active packets to the free list
1565 */
1566 spin_lock(&pd->cdrw.active_list_lock);
1567 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1568 if (pkt->state == PACKET_FINISHED_STATE) {
1569 list_del(&pkt->list);
1570 pkt_put_packet_data(pd, pkt);
1571 pkt_set_state(pkt, PACKET_IDLE_STATE);
1572 atomic_set(&pd->scan_queue, 1);
1573 }
1574 }
1575 spin_unlock(&pd->cdrw.active_list_lock);
1576}
1577
1578static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1579{
1580 struct packet_data *pkt;
1581 int i;
1582
ae7642bb 1583 for (i = 0; i < PACKET_NUM_STATES; i++)
1da177e4
LT
1584 states[i] = 0;
1585
1586 spin_lock(&pd->cdrw.active_list_lock);
1587 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1588 states[pkt->state]++;
1589 }
1590 spin_unlock(&pd->cdrw.active_list_lock);
1591}
1592
1593/*
1594 * kcdrwd is woken up when writes have been queued for one of our
1595 * registered devices
1596 */
1597static int kcdrwd(void *foobar)
1598{
1599 struct pktcdvd_device *pd = foobar;
1600 struct packet_data *pkt;
1601 long min_sleep_time, residue;
1602
1603 set_user_nice(current, -20);
83144186 1604 set_freezable();
1da177e4
LT
1605
1606 for (;;) {
1607 DECLARE_WAITQUEUE(wait, current);
1608
1609 /*
1610 * Wait until there is something to do
1611 */
1612 add_wait_queue(&pd->wqueue, &wait);
1613 for (;;) {
1614 set_current_state(TASK_INTERRUPTIBLE);
1615
1616 /* Check if we need to run pkt_handle_queue */
1617 if (atomic_read(&pd->scan_queue) > 0)
1618 goto work_to_do;
1619
1620 /* Check if we need to run the state machine for some packet */
1621 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1622 if (atomic_read(&pkt->run_sm) > 0)
1623 goto work_to_do;
1624 }
1625
1626 /* Check if we need to process the iosched queues */
1627 if (atomic_read(&pd->iosched.attention) != 0)
1628 goto work_to_do;
1629
1630 /* Otherwise, go to sleep */
1631 if (PACKET_DEBUG > 1) {
1632 int states[PACKET_NUM_STATES];
1633 pkt_count_states(pd, states);
1634 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1635 states[0], states[1], states[2], states[3],
1636 states[4], states[5]);
1637 }
1638
1639 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1640 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1641 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1642 min_sleep_time = pkt->sleep_time;
1643 }
1644
1645 generic_unplug_device(bdev_get_queue(pd->bdev));
1646
1647 VPRINTK("kcdrwd: sleeping\n");
1648 residue = schedule_timeout(min_sleep_time);
1649 VPRINTK("kcdrwd: wake up\n");
1650
1651 /* make swsusp happy with our thread */
3e1d1d28 1652 try_to_freeze();
1da177e4
LT
1653
1654 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1655 if (!pkt->sleep_time)
1656 continue;
1657 pkt->sleep_time -= min_sleep_time - residue;
1658 if (pkt->sleep_time <= 0) {
1659 pkt->sleep_time = 0;
1660 atomic_inc(&pkt->run_sm);
1661 }
1662 }
1663
1da177e4
LT
1664 if (kthread_should_stop())
1665 break;
1666 }
1667work_to_do:
1668 set_current_state(TASK_RUNNING);
1669 remove_wait_queue(&pd->wqueue, &wait);
1670
1671 if (kthread_should_stop())
1672 break;
1673
1674 /*
1675 * if pkt_handle_queue returns true, we can queue
1676 * another request.
1677 */
1678 while (pkt_handle_queue(pd))
1679 ;
1680
1681 /*
1682 * Handle packet state machine
1683 */
1684 pkt_handle_packets(pd);
1685
1686 /*
1687 * Handle iosched queues
1688 */
1689 pkt_iosched_process_queue(pd);
1690 }
1691
1692 return 0;
1693}
1694
1695static void pkt_print_settings(struct pktcdvd_device *pd)
1696{
7822082d 1697 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1da177e4
LT
1698 printk("%u blocks, ", pd->settings.size >> 2);
1699 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1700}
1701
1702static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1703{
1704 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1705
1706 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1707 cgc->cmd[2] = page_code | (page_control << 6);
1708 cgc->cmd[7] = cgc->buflen >> 8;
1709 cgc->cmd[8] = cgc->buflen & 0xff;
1710 cgc->data_direction = CGC_DATA_READ;
1711 return pkt_generic_packet(pd, cgc);
1712}
1713
1714static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1715{
1716 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1717 memset(cgc->buffer, 0, 2);
1718 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1719 cgc->cmd[1] = 0x10; /* PF */
1720 cgc->cmd[7] = cgc->buflen >> 8;
1721 cgc->cmd[8] = cgc->buflen & 0xff;
1722 cgc->data_direction = CGC_DATA_WRITE;
1723 return pkt_generic_packet(pd, cgc);
1724}
1725
1726static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1727{
1728 struct packet_command cgc;
1729 int ret;
1730
1731 /* set up command and get the disc info */
1732 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1733 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1734 cgc.cmd[8] = cgc.buflen = 2;
1735 cgc.quiet = 1;
1736
1737 if ((ret = pkt_generic_packet(pd, &cgc)))
1738 return ret;
1739
1740 /* not all drives have the same disc_info length, so requeue
1741 * packet with the length the drive tells us it can supply
1742 */
1743 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1744 sizeof(di->disc_information_length);
1745
1746 if (cgc.buflen > sizeof(disc_information))
1747 cgc.buflen = sizeof(disc_information);
1748
1749 cgc.cmd[8] = cgc.buflen;
1750 return pkt_generic_packet(pd, &cgc);
1751}
1752
1753static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1754{
1755 struct packet_command cgc;
1756 int ret;
1757
1758 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1759 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1760 cgc.cmd[1] = type & 3;
1761 cgc.cmd[4] = (track & 0xff00) >> 8;
1762 cgc.cmd[5] = track & 0xff;
1763 cgc.cmd[8] = 8;
1764 cgc.quiet = 1;
1765
1766 if ((ret = pkt_generic_packet(pd, &cgc)))
1767 return ret;
1768
1769 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1770 sizeof(ti->track_information_length);
1771
1772 if (cgc.buflen > sizeof(track_information))
1773 cgc.buflen = sizeof(track_information);
1774
1775 cgc.cmd[8] = cgc.buflen;
1776 return pkt_generic_packet(pd, &cgc);
1777}
1778
1779static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1780{
1781 disc_information di;
1782 track_information ti;
1783 __u32 last_track;
1784 int ret = -1;
1785
1786 if ((ret = pkt_get_disc_info(pd, &di)))
1787 return ret;
1788
1789 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1790 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1791 return ret;
1792
1793 /* if this track is blank, try the previous. */
1794 if (ti.blank) {
1795 last_track--;
1796 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1797 return ret;
1798 }
1799
1800 /* if last recorded field is valid, return it. */
1801 if (ti.lra_v) {
1802 *last_written = be32_to_cpu(ti.last_rec_address);
1803 } else {
1804 /* make it up instead */
1805 *last_written = be32_to_cpu(ti.track_start) +
1806 be32_to_cpu(ti.track_size);
1807 if (ti.free_blocks)
1808 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1809 }
1810 return 0;
1811}
1812
1813/*
1814 * write mode select package based on pd->settings
1815 */
1816static int pkt_set_write_settings(struct pktcdvd_device *pd)
1817{
1818 struct packet_command cgc;
1819 struct request_sense sense;
1820 write_param_page *wp;
1821 char buffer[128];
1822 int ret, size;
1823
1824 /* doesn't apply to DVD+RW or DVD-RAM */
1825 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1826 return 0;
1827
1828 memset(buffer, 0, sizeof(buffer));
1829 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1830 cgc.sense = &sense;
1831 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1832 pkt_dump_sense(&cgc);
1833 return ret;
1834 }
1835
1836 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1837 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1838 if (size > sizeof(buffer))
1839 size = sizeof(buffer);
1840
1841 /*
1842 * now get it all
1843 */
1844 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1845 cgc.sense = &sense;
1846 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1847 pkt_dump_sense(&cgc);
1848 return ret;
1849 }
1850
1851 /*
1852 * write page is offset header + block descriptor length
1853 */
1854 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1855
1856 wp->fp = pd->settings.fp;
1857 wp->track_mode = pd->settings.track_mode;
1858 wp->write_type = pd->settings.write_type;
1859 wp->data_block_type = pd->settings.block_mode;
1860
1861 wp->multi_session = 0;
1862
1863#ifdef PACKET_USE_LS
1864 wp->link_size = 7;
1865 wp->ls_v = 1;
1866#endif
1867
1868 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1869 wp->session_format = 0;
1870 wp->subhdr2 = 0x20;
1871 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1872 wp->session_format = 0x20;
1873 wp->subhdr2 = 8;
1874#if 0
1875 wp->mcn[0] = 0x80;
1876 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1877#endif
1878 } else {
1879 /*
1880 * paranoia
1881 */
7822082d 1882 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
1da177e4
LT
1883 return 1;
1884 }
1885 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1886
1887 cgc.buflen = cgc.cmd[8] = size;
1888 if ((ret = pkt_mode_select(pd, &cgc))) {
1889 pkt_dump_sense(&cgc);
1890 return ret;
1891 }
1892
1893 pkt_print_settings(pd);
1894 return 0;
1895}
1896
1897/*
7c613d59 1898 * 1 -- we can write to this track, 0 -- we can't
1da177e4 1899 */
ab863ec3 1900static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
1da177e4 1901{
ab863ec3
PO
1902 switch (pd->mmc3_profile) {
1903 case 0x1a: /* DVD+RW */
1904 case 0x12: /* DVD-RAM */
1905 /* The track is always writable on DVD+RW/DVD-RAM */
1906 return 1;
1907 default:
1908 break;
1909 }
1da177e4 1910
ab863ec3
PO
1911 if (!ti->packet || !ti->fp)
1912 return 0;
1da177e4
LT
1913
1914 /*
1915 * "good" settings as per Mt Fuji.
1916 */
ab863ec3 1917 if (ti->rt == 0 && ti->blank == 0)
7c613d59 1918 return 1;
1da177e4 1919
ab863ec3 1920 if (ti->rt == 0 && ti->blank == 1)
7c613d59 1921 return 1;
1da177e4 1922
ab863ec3 1923 if (ti->rt == 1 && ti->blank == 0)
7c613d59 1924 return 1;
1da177e4 1925
7822082d 1926 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
7c613d59 1927 return 0;
1da177e4
LT
1928}
1929
1930/*
7c613d59 1931 * 1 -- we can write to this disc, 0 -- we can't
1da177e4 1932 */
7c613d59 1933static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
1da177e4
LT
1934{
1935 switch (pd->mmc3_profile) {
1936 case 0x0a: /* CD-RW */
1937 case 0xffff: /* MMC3 not supported */
1938 break;
1939 case 0x1a: /* DVD+RW */
1940 case 0x13: /* DVD-RW */
1941 case 0x12: /* DVD-RAM */
7c613d59 1942 return 1;
1da177e4 1943 default:
7822082d 1944 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
7c613d59 1945 return 0;
1da177e4
LT
1946 }
1947
1948 /*
1949 * for disc type 0xff we should probably reserve a new track.
1950 * but i'm not sure, should we leave this to user apps? probably.
1951 */
1952 if (di->disc_type == 0xff) {
7822082d 1953 printk(DRIVER_NAME": Unknown disc. No track?\n");
7c613d59 1954 return 0;
1da177e4
LT
1955 }
1956
1957 if (di->disc_type != 0x20 && di->disc_type != 0) {
7822082d 1958 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
7c613d59 1959 return 0;
1da177e4
LT
1960 }
1961
1962 if (di->erasable == 0) {
7822082d 1963 printk(DRIVER_NAME": Disc not erasable\n");
7c613d59 1964 return 0;
1da177e4
LT
1965 }
1966
1967 if (di->border_status == PACKET_SESSION_RESERVED) {
7822082d 1968 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
7c613d59 1969 return 0;
1da177e4
LT
1970 }
1971
7c613d59 1972 return 1;
1da177e4
LT
1973}
1974
1975static int pkt_probe_settings(struct pktcdvd_device *pd)
1976{
1977 struct packet_command cgc;
1978 unsigned char buf[12];
1979 disc_information di;
1980 track_information ti;
1981 int ret, track;
1982
1983 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1984 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1985 cgc.cmd[8] = 8;
1986 ret = pkt_generic_packet(pd, &cgc);
1987 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1988
1989 memset(&di, 0, sizeof(disc_information));
1990 memset(&ti, 0, sizeof(track_information));
1991
1992 if ((ret = pkt_get_disc_info(pd, &di))) {
1993 printk("failed get_disc\n");
1994 return ret;
1995 }
1996
7c613d59 1997 if (!pkt_writable_disc(pd, &di))
9db91546 1998 return -EROFS;
1da177e4 1999
1da177e4
LT
2000 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
2001
2002 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
2003 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
7822082d 2004 printk(DRIVER_NAME": failed get_track\n");
1da177e4
LT
2005 return ret;
2006 }
2007
ab863ec3 2008 if (!pkt_writable_track(pd, &ti)) {
7822082d 2009 printk(DRIVER_NAME": can't write to this track\n");
9db91546 2010 return -EROFS;
1da177e4
LT
2011 }
2012
2013 /*
2014 * we keep packet size in 512 byte units, makes it easier to
2015 * deal with request calculations.
2016 */
2017 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2018 if (pd->settings.size == 0) {
7822082d 2019 printk(DRIVER_NAME": detected zero packet size!\n");
a460ad62 2020 return -ENXIO;
1da177e4 2021 }
d0272e78 2022 if (pd->settings.size > PACKET_MAX_SECTORS) {
7822082d 2023 printk(DRIVER_NAME": packet size is too big\n");
9db91546 2024 return -EROFS;
d0272e78 2025 }
1da177e4
LT
2026 pd->settings.fp = ti.fp;
2027 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2028
2029 if (ti.nwa_v) {
2030 pd->nwa = be32_to_cpu(ti.next_writable);
2031 set_bit(PACKET_NWA_VALID, &pd->flags);
2032 }
2033
2034 /*
2035 * in theory we could use lra on -RW media as well and just zero
2036 * blocks that haven't been written yet, but in practice that
2037 * is just a no-go. we'll use that for -R, naturally.
2038 */
2039 if (ti.lra_v) {
2040 pd->lra = be32_to_cpu(ti.last_rec_address);
2041 set_bit(PACKET_LRA_VALID, &pd->flags);
2042 } else {
2043 pd->lra = 0xffffffff;
2044 set_bit(PACKET_LRA_VALID, &pd->flags);
2045 }
2046
2047 /*
2048 * fine for now
2049 */
2050 pd->settings.link_loss = 7;
2051 pd->settings.write_type = 0; /* packet */
2052 pd->settings.track_mode = ti.track_mode;
2053
2054 /*
2055 * mode1 or mode2 disc
2056 */
2057 switch (ti.data_mode) {
2058 case PACKET_MODE1:
2059 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2060 break;
2061 case PACKET_MODE2:
2062 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2063 break;
2064 default:
7822082d 2065 printk(DRIVER_NAME": unknown data mode\n");
9db91546 2066 return -EROFS;
1da177e4
LT
2067 }
2068 return 0;
2069}
2070
2071/*
2072 * enable/disable write caching on drive
2073 */
2074static int pkt_write_caching(struct pktcdvd_device *pd, int set)
2075{
2076 struct packet_command cgc;
2077 struct request_sense sense;
2078 unsigned char buf[64];
2079 int ret;
2080
2081 memset(buf, 0, sizeof(buf));
2082 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2083 cgc.sense = &sense;
2084 cgc.buflen = pd->mode_offset + 12;
2085
2086 /*
2087 * caching mode page might not be there, so quiet this command
2088 */
2089 cgc.quiet = 1;
2090
2091 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2092 return ret;
2093
2094 buf[pd->mode_offset + 10] |= (!!set << 2);
2095
2096 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2097 ret = pkt_mode_select(pd, &cgc);
2098 if (ret) {
7822082d 2099 printk(DRIVER_NAME": write caching control failed\n");
1da177e4
LT
2100 pkt_dump_sense(&cgc);
2101 } else if (!ret && set)
7822082d 2102 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
1da177e4
LT
2103 return ret;
2104}
2105
2106static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2107{
2108 struct packet_command cgc;
2109
2110 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2111 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2112 cgc.cmd[4] = lockflag ? 1 : 0;
2113 return pkt_generic_packet(pd, &cgc);
2114}
2115
2116/*
2117 * Returns drive maximum write speed
2118 */
2119static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
2120{
2121 struct packet_command cgc;
2122 struct request_sense sense;
2123 unsigned char buf[256+18];
2124 unsigned char *cap_buf;
2125 int ret, offset;
2126
2127 memset(buf, 0, sizeof(buf));
2128 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2129 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2130 cgc.sense = &sense;
2131
2132 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2133 if (ret) {
2134 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2135 sizeof(struct mode_page_header);
2136 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2137 if (ret) {
2138 pkt_dump_sense(&cgc);
2139 return ret;
2140 }
2141 }
2142
2143 offset = 20; /* Obsoleted field, used by older drives */
2144 if (cap_buf[1] >= 28)
2145 offset = 28; /* Current write speed selected */
2146 if (cap_buf[1] >= 30) {
2147 /* If the drive reports at least one "Logical Unit Write
2148 * Speed Performance Descriptor Block", use the information
2149 * in the first block. (contains the highest speed)
2150 */
2151 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2152 if (num_spdb > 0)
2153 offset = 34;
2154 }
2155
2156 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2157 return 0;
2158}
2159
2160/* These tables from cdrecord - I don't have orange book */
2161/* standard speed CD-RW (1-4x) */
2162static char clv_to_speed[16] = {
2163 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2164 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2165};
2166/* high speed CD-RW (-10x) */
2167static char hs_clv_to_speed[16] = {
2168 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2169 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2170};
2171/* ultra high speed CD-RW */
2172static char us_clv_to_speed[16] = {
2173 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2174 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2175};
2176
2177/*
2178 * reads the maximum media speed from ATIP
2179 */
2180static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
2181{
2182 struct packet_command cgc;
2183 struct request_sense sense;
2184 unsigned char buf[64];
2185 unsigned int size, st, sp;
2186 int ret;
2187
2188 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2189 cgc.sense = &sense;
2190 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2191 cgc.cmd[1] = 2;
2192 cgc.cmd[2] = 4; /* READ ATIP */
2193 cgc.cmd[8] = 2;
2194 ret = pkt_generic_packet(pd, &cgc);
2195 if (ret) {
2196 pkt_dump_sense(&cgc);
2197 return ret;
2198 }
2199 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2200 if (size > sizeof(buf))
2201 size = sizeof(buf);
2202
2203 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2204 cgc.sense = &sense;
2205 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2206 cgc.cmd[1] = 2;
2207 cgc.cmd[2] = 4;
2208 cgc.cmd[8] = size;
2209 ret = pkt_generic_packet(pd, &cgc);
2210 if (ret) {
2211 pkt_dump_sense(&cgc);
2212 return ret;
2213 }
2214
2215 if (!buf[6] & 0x40) {
7822082d 2216 printk(DRIVER_NAME": Disc type is not CD-RW\n");
1da177e4
LT
2217 return 1;
2218 }
2219 if (!buf[6] & 0x4) {
7822082d 2220 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
1da177e4
LT
2221 return 1;
2222 }
2223
2224 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2225
2226 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2227
2228 /* Info from cdrecord */
2229 switch (st) {
2230 case 0: /* standard speed */
2231 *speed = clv_to_speed[sp];
2232 break;
2233 case 1: /* high speed */
2234 *speed = hs_clv_to_speed[sp];
2235 break;
2236 case 2: /* ultra high speed */
2237 *speed = us_clv_to_speed[sp];
2238 break;
2239 default:
7822082d 2240 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
1da177e4
LT
2241 return 1;
2242 }
2243 if (*speed) {
7822082d 2244 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
1da177e4
LT
2245 return 0;
2246 } else {
7822082d 2247 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
1da177e4
LT
2248 return 1;
2249 }
2250}
2251
2252static int pkt_perform_opc(struct pktcdvd_device *pd)
2253{
2254 struct packet_command cgc;
2255 struct request_sense sense;
2256 int ret;
2257
7822082d 2258 VPRINTK(DRIVER_NAME": Performing OPC\n");
1da177e4
LT
2259
2260 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2261 cgc.sense = &sense;
2262 cgc.timeout = 60*HZ;
2263 cgc.cmd[0] = GPCMD_SEND_OPC;
2264 cgc.cmd[1] = 1;
2265 if ((ret = pkt_generic_packet(pd, &cgc)))
2266 pkt_dump_sense(&cgc);
2267 return ret;
2268}
2269
2270static int pkt_open_write(struct pktcdvd_device *pd)
2271{
2272 int ret;
2273 unsigned int write_speed, media_write_speed, read_speed;
2274
2275 if ((ret = pkt_probe_settings(pd))) {
7822082d 2276 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
9db91546 2277 return ret;
1da177e4
LT
2278 }
2279
2280 if ((ret = pkt_set_write_settings(pd))) {
7822082d 2281 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
1da177e4
LT
2282 return -EIO;
2283 }
2284
2285 pkt_write_caching(pd, USE_WCACHING);
2286
2287 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2288 write_speed = 16 * 177;
2289 switch (pd->mmc3_profile) {
2290 case 0x13: /* DVD-RW */
2291 case 0x1a: /* DVD+RW */
2292 case 0x12: /* DVD-RAM */
7822082d 2293 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
1da177e4
LT
2294 break;
2295 default:
2296 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2297 media_write_speed = 16;
2298 write_speed = min(write_speed, media_write_speed * 177);
7822082d 2299 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
1da177e4
LT
2300 break;
2301 }
2302 read_speed = write_speed;
2303
2304 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
7822082d 2305 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
1da177e4
LT
2306 return -EIO;
2307 }
2308 pd->write_speed = write_speed;
2309 pd->read_speed = read_speed;
2310
2311 if ((ret = pkt_perform_opc(pd))) {
7822082d 2312 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
1da177e4
LT
2313 }
2314
2315 return 0;
2316}
2317
2318/*
2319 * called at open time.
2320 */
2321static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2322{
2323 int ret;
2324 long lba;
165125e1 2325 struct request_queue *q;
1da177e4
LT
2326
2327 /*
2328 * We need to re-open the cdrom device without O_NONBLOCK to be able
2329 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2330 * so bdget() can't fail.
2331 */
2332 bdget(pd->bdev->bd_dev);
2333 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2334 goto out;
2335
8382bf2e
PO
2336 if ((ret = bd_claim(pd->bdev, pd)))
2337 goto out_putdev;
2338
1da177e4 2339 if ((ret = pkt_get_last_written(pd, &lba))) {
7822082d 2340 printk(DRIVER_NAME": pkt_get_last_written failed\n");
8382bf2e 2341 goto out_unclaim;
1da177e4
LT
2342 }
2343
2344 set_capacity(pd->disk, lba << 2);
2345 set_capacity(pd->bdev->bd_disk, lba << 2);
2346 bd_set_size(pd->bdev, (loff_t)lba << 11);
2347
2348 q = bdev_get_queue(pd->bdev);
2349 if (write) {
2350 if ((ret = pkt_open_write(pd)))
8382bf2e 2351 goto out_unclaim;
1da177e4
LT
2352 /*
2353 * Some CDRW drives can not handle writes larger than one packet,
2354 * even if the size is a multiple of the packet size.
2355 */
2356 spin_lock_irq(q->queue_lock);
2357 blk_queue_max_sectors(q, pd->settings.size);
2358 spin_unlock_irq(q->queue_lock);
2359 set_bit(PACKET_WRITABLE, &pd->flags);
2360 } else {
2361 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2362 clear_bit(PACKET_WRITABLE, &pd->flags);
2363 }
2364
2365 if ((ret = pkt_set_segment_merging(pd, q)))
8382bf2e 2366 goto out_unclaim;
1da177e4 2367
e1bc89bc
PO
2368 if (write) {
2369 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
7822082d 2370 printk(DRIVER_NAME": not enough memory for buffers\n");
e1bc89bc
PO
2371 ret = -ENOMEM;
2372 goto out_unclaim;
2373 }
7822082d 2374 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
e1bc89bc 2375 }
1da177e4
LT
2376
2377 return 0;
2378
8382bf2e
PO
2379out_unclaim:
2380 bd_release(pd->bdev);
1da177e4
LT
2381out_putdev:
2382 blkdev_put(pd->bdev);
2383out:
2384 return ret;
2385}
2386
2387/*
2388 * called when the device is closed. makes sure that the device flushes
2389 * the internal cache before we close.
2390 */
2391static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2392{
2393 if (flush && pkt_flush_cache(pd))
7822082d 2394 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
1da177e4
LT
2395
2396 pkt_lock_door(pd, 0);
2397
2398 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
8382bf2e 2399 bd_release(pd->bdev);
1da177e4 2400 blkdev_put(pd->bdev);
e1bc89bc
PO
2401
2402 pkt_shrink_pktlist(pd);
1da177e4
LT
2403}
2404
2405static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2406{
2407 if (dev_minor >= MAX_WRITERS)
2408 return NULL;
2409 return pkt_devs[dev_minor];
2410}
2411
2412static int pkt_open(struct inode *inode, struct file *file)
2413{
2414 struct pktcdvd_device *pd = NULL;
2415 int ret;
2416
7822082d 2417 VPRINTK(DRIVER_NAME": entering open\n");
1da177e4 2418
1657f824 2419 mutex_lock(&ctl_mutex);
1da177e4
LT
2420 pd = pkt_find_dev_from_minor(iminor(inode));
2421 if (!pd) {
2422 ret = -ENODEV;
2423 goto out;
2424 }
2425 BUG_ON(pd->refcnt < 0);
2426
2427 pd->refcnt++;
46f4e1b7
PO
2428 if (pd->refcnt > 1) {
2429 if ((file->f_mode & FMODE_WRITE) &&
2430 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2431 ret = -EBUSY;
2432 goto out_dec;
2433 }
2434 } else {
01fd9fda
PO
2435 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2436 if (ret)
1da177e4 2437 goto out_dec;
1da177e4
LT
2438 /*
2439 * needed here as well, since ext2 (among others) may change
2440 * the blocksize at mount time
2441 */
2442 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2443 }
2444
1657f824 2445 mutex_unlock(&ctl_mutex);
1da177e4
LT
2446 return 0;
2447
2448out_dec:
2449 pd->refcnt--;
2450out:
7822082d 2451 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
1657f824 2452 mutex_unlock(&ctl_mutex);
1da177e4
LT
2453 return ret;
2454}
2455
2456static int pkt_close(struct inode *inode, struct file *file)
2457{
2458 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2459 int ret = 0;
2460
1657f824 2461 mutex_lock(&ctl_mutex);
1da177e4
LT
2462 pd->refcnt--;
2463 BUG_ON(pd->refcnt < 0);
2464 if (pd->refcnt == 0) {
2465 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2466 pkt_release_dev(pd, flush);
2467 }
1657f824 2468 mutex_unlock(&ctl_mutex);
1da177e4
LT
2469 return ret;
2470}
2471
2472
6712ecf8 2473static void pkt_end_io_read_cloned(struct bio *bio, int err)
1da177e4
LT
2474{
2475 struct packet_stacked_data *psd = bio->bi_private;
2476 struct pktcdvd_device *pd = psd->pd;
2477
1da177e4 2478 bio_put(bio);
6712ecf8 2479 bio_endio(psd->bio, err);
1da177e4
LT
2480 mempool_free(psd, psd_pool);
2481 pkt_bio_finished(pd);
1da177e4
LT
2482}
2483
165125e1 2484static int pkt_make_request(struct request_queue *q, struct bio *bio)
1da177e4
LT
2485{
2486 struct pktcdvd_device *pd;
2487 char b[BDEVNAME_SIZE];
2488 sector_t zone;
2489 struct packet_data *pkt;
2490 int was_empty, blocked_bio;
2491 struct pkt_rb_node *node;
2492
2493 pd = q->queuedata;
2494 if (!pd) {
7822082d 2495 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
1da177e4
LT
2496 goto end_io;
2497 }
2498
2499 /*
2500 * Clone READ bios so we can have our own bi_end_io callback.
2501 */
2502 if (bio_data_dir(bio) == READ) {
2503 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2504 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2505
2506 psd->pd = pd;
2507 psd->bio = bio;
2508 cloned_bio->bi_bdev = pd->bdev;
2509 cloned_bio->bi_private = psd;
2510 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2511 pd->stats.secs_r += bio->bi_size >> 9;
46c271be 2512 pkt_queue_bio(pd, cloned_bio);
1da177e4
LT
2513 return 0;
2514 }
2515
2516 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
7822082d 2517 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
1da177e4
LT
2518 pd->name, (unsigned long long)bio->bi_sector);
2519 goto end_io;
2520 }
2521
2522 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
7822082d 2523 printk(DRIVER_NAME": wrong bio size\n");
1da177e4
LT
2524 goto end_io;
2525 }
2526
2527 blk_queue_bounce(q, &bio);
2528
2529 zone = ZONE(bio->bi_sector, pd);
2530 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2531 (unsigned long long)bio->bi_sector,
2532 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2533
2534 /* Check if we have to split the bio */
2535 {
2536 struct bio_pair *bp;
2537 sector_t last_zone;
2538 int first_sectors;
2539
2540 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2541 if (last_zone != zone) {
2542 BUG_ON(last_zone != zone + pd->settings.size);
2543 first_sectors = last_zone - bio->bi_sector;
2544 bp = bio_split(bio, bio_split_pool, first_sectors);
2545 BUG_ON(!bp);
2546 pkt_make_request(q, &bp->bio1);
2547 pkt_make_request(q, &bp->bio2);
2548 bio_pair_release(bp);
2549 return 0;
2550 }
2551 }
2552
2553 /*
2554 * If we find a matching packet in state WAITING or READ_WAIT, we can
2555 * just append this bio to that packet.
2556 */
2557 spin_lock(&pd->cdrw.active_list_lock);
2558 blocked_bio = 0;
2559 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2560 if (pkt->sector == zone) {
2561 spin_lock(&pkt->lock);
2562 if ((pkt->state == PACKET_WAITING_STATE) ||
2563 (pkt->state == PACKET_READ_WAIT_STATE)) {
2564 pkt_add_list_last(bio, &pkt->orig_bios,
2565 &pkt->orig_bios_tail);
2566 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2567 if ((pkt->write_size >= pkt->frames) &&
2568 (pkt->state == PACKET_WAITING_STATE)) {
2569 atomic_inc(&pkt->run_sm);
2570 wake_up(&pd->wqueue);
2571 }
2572 spin_unlock(&pkt->lock);
2573 spin_unlock(&pd->cdrw.active_list_lock);
2574 return 0;
2575 } else {
2576 blocked_bio = 1;
2577 }
2578 spin_unlock(&pkt->lock);
2579 }
2580 }
2581 spin_unlock(&pd->cdrw.active_list_lock);
2582
0a0fc960
TM
2583 /*
2584 * Test if there is enough room left in the bio work queue
2585 * (queue size >= congestion on mark).
2586 * If not, wait till the work queue size is below the congestion off mark.
2587 */
2588 spin_lock(&pd->lock);
2589 if (pd->write_congestion_on > 0
2590 && pd->bio_queue_size >= pd->write_congestion_on) {
83f3aa3d 2591 set_bdi_congested(&q->backing_dev_info, WRITE);
0a0fc960
TM
2592 do {
2593 spin_unlock(&pd->lock);
2594 congestion_wait(WRITE, HZ);
2595 spin_lock(&pd->lock);
2596 } while(pd->bio_queue_size > pd->write_congestion_off);
2597 }
2598 spin_unlock(&pd->lock);
2599
1da177e4
LT
2600 /*
2601 * No matching packet found. Store the bio in the work queue.
2602 */
2603 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
1da177e4
LT
2604 node->bio = bio;
2605 spin_lock(&pd->lock);
2606 BUG_ON(pd->bio_queue_size < 0);
2607 was_empty = (pd->bio_queue_size == 0);
2608 pkt_rbtree_insert(pd, node);
2609 spin_unlock(&pd->lock);
2610
2611 /*
2612 * Wake up the worker thread.
2613 */
2614 atomic_set(&pd->scan_queue, 1);
2615 if (was_empty) {
2616 /* This wake_up is required for correct operation */
2617 wake_up(&pd->wqueue);
2618 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2619 /*
2620 * This wake up is not required for correct operation,
2621 * but improves performance in some cases.
2622 */
2623 wake_up(&pd->wqueue);
2624 }
2625 return 0;
2626end_io:
6712ecf8 2627 bio_io_error(bio);
1da177e4
LT
2628 return 0;
2629}
2630
2631
2632
165125e1 2633static int pkt_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
1da177e4
LT
2634{
2635 struct pktcdvd_device *pd = q->queuedata;
2636 sector_t zone = ZONE(bio->bi_sector, pd);
2637 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2638 int remaining = (pd->settings.size << 9) - used;
2639 int remaining2;
2640
2641 /*
2642 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2643 * boundary, pkt_make_request() will split the bio.
2644 */
2645 remaining2 = PAGE_SIZE - bio->bi_size;
2646 remaining = max(remaining, remaining2);
2647
2648 BUG_ON(remaining < 0);
2649 return remaining;
2650}
2651
2652static void pkt_init_queue(struct pktcdvd_device *pd)
2653{
165125e1 2654 struct request_queue *q = pd->disk->queue;
1da177e4
LT
2655
2656 blk_queue_make_request(q, pkt_make_request);
2657 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2658 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2659 blk_queue_merge_bvec(q, pkt_merge_bvec);
2660 q->queuedata = pd;
2661}
2662
2663static int pkt_seq_show(struct seq_file *m, void *p)
2664{
2665 struct pktcdvd_device *pd = m->private;
2666 char *msg;
2667 char bdev_buf[BDEVNAME_SIZE];
2668 int states[PACKET_NUM_STATES];
2669
2670 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2671 bdevname(pd->bdev, bdev_buf));
2672
2673 seq_printf(m, "\nSettings:\n");
2674 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2675
2676 if (pd->settings.write_type == 0)
2677 msg = "Packet";
2678 else
2679 msg = "Unknown";
2680 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2681
2682 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2683 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2684
2685 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2686
2687 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2688 msg = "Mode 1";
2689 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2690 msg = "Mode 2";
2691 else
2692 msg = "Unknown";
2693 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2694
2695 seq_printf(m, "\nStatistics:\n");
2696 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2697 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2698 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2699 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2700 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2701
2702 seq_printf(m, "\nMisc:\n");
2703 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2704 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2705 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2706 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2707 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2708 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2709
2710 seq_printf(m, "\nQueue state:\n");
2711 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2712 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2713 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2714
2715 pkt_count_states(pd, states);
2716 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2717 states[0], states[1], states[2], states[3], states[4], states[5]);
2718
0a0fc960
TM
2719 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2720 pd->write_congestion_off,
2721 pd->write_congestion_on);
1da177e4
LT
2722 return 0;
2723}
2724
2725static int pkt_seq_open(struct inode *inode, struct file *file)
2726{
2727 return single_open(file, pkt_seq_show, PDE(inode)->data);
2728}
2729
2b8693c0 2730static const struct file_operations pkt_proc_fops = {
1da177e4
LT
2731 .open = pkt_seq_open,
2732 .read = seq_read,
2733 .llseek = seq_lseek,
2734 .release = single_release
2735};
2736
2737static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2738{
2739 int i;
2740 int ret = 0;
2741 char b[BDEVNAME_SIZE];
2742 struct proc_dir_entry *proc;
2743 struct block_device *bdev;
2744
2745 if (pd->pkt_dev == dev) {
7822082d 2746 printk(DRIVER_NAME": Recursive setup not allowed\n");
1da177e4
LT
2747 return -EBUSY;
2748 }
2749 for (i = 0; i < MAX_WRITERS; i++) {
2750 struct pktcdvd_device *pd2 = pkt_devs[i];
2751 if (!pd2)
2752 continue;
2753 if (pd2->bdev->bd_dev == dev) {
7822082d 2754 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
1da177e4
LT
2755 return -EBUSY;
2756 }
2757 if (pd2->pkt_dev == dev) {
7822082d 2758 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
1da177e4
LT
2759 return -EBUSY;
2760 }
2761 }
2762
2763 bdev = bdget(dev);
2764 if (!bdev)
2765 return -ENOMEM;
2766 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2767 if (ret)
2768 return ret;
2769
2770 /* This is safe, since we have a reference from open(). */
2771 __module_get(THIS_MODULE);
2772
1da177e4
LT
2773 pd->bdev = bdev;
2774 set_blocksize(bdev, CD_FRAMESIZE);
2775
2776 pkt_init_queue(pd);
2777
2778 atomic_set(&pd->cdrw.pending_bios, 0);
2779 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2780 if (IS_ERR(pd->cdrw.thread)) {
7822082d 2781 printk(DRIVER_NAME": can't start kernel thread\n");
1da177e4 2782 ret = -ENOMEM;
e1bc89bc 2783 goto out_mem;
1da177e4
LT
2784 }
2785
2786 proc = create_proc_entry(pd->name, 0, pkt_proc);
2787 if (proc) {
2788 proc->data = pd;
2789 proc->proc_fops = &pkt_proc_fops;
2790 }
7822082d 2791 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
1da177e4
LT
2792 return 0;
2793
1da177e4
LT
2794out_mem:
2795 blkdev_put(bdev);
2796 /* This is safe: open() is still holding a reference. */
2797 module_put(THIS_MODULE);
2798 return ret;
2799}
2800
2801static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2802{
2803 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2804
2805 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
1da177e4
LT
2806
2807 switch (cmd) {
2808 /*
2809 * forward selected CDROM ioctls to CD-ROM, for UDF
2810 */
2811 case CDROMMULTISESSION:
2812 case CDROMREADTOCENTRY:
2813 case CDROM_LAST_WRITTEN:
2814 case CDROM_SEND_PACKET:
2815 case SCSI_IOCTL_SEND_COMMAND:
118326e9 2816 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
1da177e4
LT
2817
2818 case CDROMEJECT:
2819 /*
2820 * The door gets locked when the device is opened, so we
2821 * have to unlock it or else the eject command fails.
2822 */
948423e5
PO
2823 if (pd->refcnt == 1)
2824 pkt_lock_door(pd, 0);
118326e9 2825 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
1da177e4
LT
2826
2827 default:
7822082d 2828 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
1da177e4
LT
2829 return -ENOTTY;
2830 }
2831
2832 return 0;
2833}
2834
2835static int pkt_media_changed(struct gendisk *disk)
2836{
2837 struct pktcdvd_device *pd = disk->private_data;
2838 struct gendisk *attached_disk;
2839
2840 if (!pd)
2841 return 0;
2842 if (!pd->bdev)
2843 return 0;
2844 attached_disk = pd->bdev->bd_disk;
2845 if (!attached_disk)
2846 return 0;
2847 return attached_disk->fops->media_changed(attached_disk);
2848}
2849
2850static struct block_device_operations pktcdvd_ops = {
2851 .owner = THIS_MODULE,
2852 .open = pkt_open,
2853 .release = pkt_close,
2854 .ioctl = pkt_ioctl,
2855 .media_changed = pkt_media_changed,
2856};
2857
2858/*
2859 * Set up mapping from pktcdvd device to CD-ROM device.
2860 */
adb9250a 2861static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
1da177e4
LT
2862{
2863 int idx;
2864 int ret = -ENOMEM;
2865 struct pktcdvd_device *pd;
2866 struct gendisk *disk;
adb9250a
TM
2867
2868 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1da177e4
LT
2869
2870 for (idx = 0; idx < MAX_WRITERS; idx++)
2871 if (!pkt_devs[idx])
2872 break;
2873 if (idx == MAX_WRITERS) {
7822082d 2874 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
adb9250a
TM
2875 ret = -EBUSY;
2876 goto out_mutex;
1da177e4
LT
2877 }
2878
1107d2e0 2879 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
1da177e4 2880 if (!pd)
adb9250a 2881 goto out_mutex;
1da177e4 2882
0eaae62a
MD
2883 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2884 sizeof(struct pkt_rb_node));
1da177e4
LT
2885 if (!pd->rb_pool)
2886 goto out_mem;
2887
e1bc89bc
PO
2888 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2889 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2890 spin_lock_init(&pd->cdrw.active_list_lock);
2891
1da177e4
LT
2892 spin_lock_init(&pd->lock);
2893 spin_lock_init(&pd->iosched.lock);
7822082d 2894 sprintf(pd->name, DRIVER_NAME"%d", idx);
1da177e4
LT
2895 init_waitqueue_head(&pd->wqueue);
2896 pd->bio_queue = RB_ROOT;
2897
0a0fc960
TM
2898 pd->write_congestion_on = write_congestion_on;
2899 pd->write_congestion_off = write_congestion_off;
2900
adb9250a
TM
2901 disk = alloc_disk(1);
2902 if (!disk)
2903 goto out_mem;
2904 pd->disk = disk;
add21660 2905 disk->major = pktdev_major;
1da177e4
LT
2906 disk->first_minor = idx;
2907 disk->fops = &pktcdvd_ops;
2908 disk->flags = GENHD_FL_REMOVABLE;
adb9250a 2909 strcpy(disk->disk_name, pd->name);
1da177e4
LT
2910 disk->private_data = pd;
2911 disk->queue = blk_alloc_queue(GFP_KERNEL);
2912 if (!disk->queue)
2913 goto out_mem2;
2914
2915 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2916 ret = pkt_new_dev(pd, dev);
2917 if (ret)
2918 goto out_new_dev;
2919
2920 add_disk(disk);
adb9250a 2921
32694850
TM
2922 pkt_sysfs_dev_new(pd);
2923 pkt_debugfs_dev_new(pd);
2924
1da177e4 2925 pkt_devs[idx] = pd;
adb9250a
TM
2926 if (pkt_dev)
2927 *pkt_dev = pd->pkt_dev;
2928
2929 mutex_unlock(&ctl_mutex);
1da177e4
LT
2930 return 0;
2931
2932out_new_dev:
1312f40e 2933 blk_cleanup_queue(disk->queue);
1da177e4
LT
2934out_mem2:
2935 put_disk(disk);
2936out_mem:
2937 if (pd->rb_pool)
2938 mempool_destroy(pd->rb_pool);
2939 kfree(pd);
adb9250a
TM
2940out_mutex:
2941 mutex_unlock(&ctl_mutex);
2942 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
1da177e4
LT
2943 return ret;
2944}
2945
2946/*
2947 * Tear down mapping from pktcdvd device to CD-ROM device.
2948 */
adb9250a 2949static int pkt_remove_dev(dev_t pkt_dev)
1da177e4
LT
2950{
2951 struct pktcdvd_device *pd;
2952 int idx;
adb9250a
TM
2953 int ret = 0;
2954
2955 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1da177e4
LT
2956
2957 for (idx = 0; idx < MAX_WRITERS; idx++) {
2958 pd = pkt_devs[idx];
2959 if (pd && (pd->pkt_dev == pkt_dev))
2960 break;
2961 }
2962 if (idx == MAX_WRITERS) {
7822082d 2963 DPRINTK(DRIVER_NAME": dev not setup\n");
adb9250a
TM
2964 ret = -ENXIO;
2965 goto out;
1da177e4
LT
2966 }
2967
adb9250a
TM
2968 if (pd->refcnt > 0) {
2969 ret = -EBUSY;
2970 goto out;
2971 }
1da177e4
LT
2972 if (!IS_ERR(pd->cdrw.thread))
2973 kthread_stop(pd->cdrw.thread);
2974
32694850
TM
2975 pkt_devs[idx] = NULL;
2976
2977 pkt_debugfs_dev_remove(pd);
2978 pkt_sysfs_dev_remove(pd);
2979
1da177e4
LT
2980 blkdev_put(pd->bdev);
2981
1da177e4 2982 remove_proc_entry(pd->name, pkt_proc);
7822082d 2983 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
1da177e4
LT
2984
2985 del_gendisk(pd->disk);
1312f40e 2986 blk_cleanup_queue(pd->disk->queue);
1da177e4
LT
2987 put_disk(pd->disk);
2988
1da177e4
LT
2989 mempool_destroy(pd->rb_pool);
2990 kfree(pd);
2991
2992 /* This is safe: open() is still holding a reference. */
2993 module_put(THIS_MODULE);
adb9250a
TM
2994
2995out:
2996 mutex_unlock(&ctl_mutex);
2997 return ret;
1da177e4
LT
2998}
2999
3000static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
3001{
adb9250a
TM
3002 struct pktcdvd_device *pd;
3003
3004 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3005
3006 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
1da177e4
LT
3007 if (pd) {
3008 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
3009 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
3010 } else {
3011 ctrl_cmd->dev = 0;
3012 ctrl_cmd->pkt_dev = 0;
3013 }
3014 ctrl_cmd->num_devices = MAX_WRITERS;
adb9250a
TM
3015
3016 mutex_unlock(&ctl_mutex);
1da177e4
LT
3017}
3018
3019static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3020{
3021 void __user *argp = (void __user *)arg;
3022 struct pkt_ctrl_command ctrl_cmd;
3023 int ret = 0;
adb9250a 3024 dev_t pkt_dev = 0;
1da177e4
LT
3025
3026 if (cmd != PACKET_CTRL_CMD)
3027 return -ENOTTY;
3028
3029 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3030 return -EFAULT;
3031
3032 switch (ctrl_cmd.command) {
3033 case PKT_CTRL_CMD_SETUP:
3034 if (!capable(CAP_SYS_ADMIN))
3035 return -EPERM;
adb9250a
TM
3036 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3037 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
1da177e4
LT
3038 break;
3039 case PKT_CTRL_CMD_TEARDOWN:
3040 if (!capable(CAP_SYS_ADMIN))
3041 return -EPERM;
adb9250a 3042 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
1da177e4
LT
3043 break;
3044 case PKT_CTRL_CMD_STATUS:
1da177e4 3045 pkt_get_status(&ctrl_cmd);
1da177e4
LT
3046 break;
3047 default:
3048 return -ENOTTY;
3049 }
3050
3051 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3052 return -EFAULT;
3053 return ret;
3054}
3055
3056
2b8693c0 3057static const struct file_operations pkt_ctl_fops = {
1da177e4
LT
3058 .ioctl = pkt_ctl_ioctl,
3059 .owner = THIS_MODULE,
3060};
3061
3062static struct miscdevice pkt_misc = {
3063 .minor = MISC_DYNAMIC_MINOR,
7822082d 3064 .name = DRIVER_NAME,
1da177e4
LT
3065 .fops = &pkt_ctl_fops
3066};
3067
3068static int __init pkt_init(void)
3069{
3070 int ret;
3071
32694850
TM
3072 mutex_init(&ctl_mutex);
3073
0eaae62a
MD
3074 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3075 sizeof(struct packet_stacked_data));
1da177e4
LT
3076 if (!psd_pool)
3077 return -ENOMEM;
3078
add21660 3079 ret = register_blkdev(pktdev_major, DRIVER_NAME);
1da177e4 3080 if (ret < 0) {
7822082d 3081 printk(DRIVER_NAME": Unable to register block device\n");
1da177e4
LT
3082 goto out2;
3083 }
add21660
TM
3084 if (!pktdev_major)
3085 pktdev_major = ret;
1da177e4 3086
32694850
TM
3087 ret = pkt_sysfs_init();
3088 if (ret)
3089 goto out;
3090
3091 pkt_debugfs_init();
3092
1da177e4
LT
3093 ret = misc_register(&pkt_misc);
3094 if (ret) {
7822082d 3095 printk(DRIVER_NAME": Unable to register misc device\n");
32694850 3096 goto out_misc;
1da177e4
LT
3097 }
3098
7822082d 3099 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
1da177e4 3100
1da177e4
LT
3101 return 0;
3102
32694850
TM
3103out_misc:
3104 pkt_debugfs_cleanup();
3105 pkt_sysfs_cleanup();
1da177e4 3106out:
add21660 3107 unregister_blkdev(pktdev_major, DRIVER_NAME);
1da177e4
LT
3108out2:
3109 mempool_destroy(psd_pool);
3110 return ret;
3111}
3112
3113static void __exit pkt_exit(void)
3114{
7822082d 3115 remove_proc_entry(DRIVER_NAME, proc_root_driver);
1da177e4 3116 misc_deregister(&pkt_misc);
32694850
TM
3117
3118 pkt_debugfs_cleanup();
3119 pkt_sysfs_cleanup();
3120
add21660 3121 unregister_blkdev(pktdev_major, DRIVER_NAME);
1da177e4
LT
3122 mempool_destroy(psd_pool);
3123}
3124
3125MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3126MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3127MODULE_LICENSE("GPL");
3128
3129module_init(pkt_init);
3130module_exit(pkt_exit);