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