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