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