blob: 3ba1df93e9e32e8fb3c8641f7f11b9a33a575cc2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
Thomas Maieradb92502006-12-08 02:36:10 -08004 * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * May be copied or modified under the terms of the GNU General Public
7 * License. See linux/COPYING for more information.
8 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -07009 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
10 * DVD-RAM devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Theory of operation:
13 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070014 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070023 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 *************************************************************************/
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/pktcdvd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080058#include <linux/freezer.h>
Jes Sorensen1657f822006-03-23 03:00:25 -080059#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_ioctl.h>
Peter Osterlundcef289632006-02-20 18:28:01 -080062#include <scsi/scsi.h>
Thomas Maier32694852006-12-08 02:36:12 -080063#include <linux/debugfs.h>
64#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/uaccess.h>
67
Thomas Maier78220822006-10-04 02:15:28 -070068#define DRIVER_NAME "pktcdvd"
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#if PACKET_DEBUG
71#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
72#else
73#define DPRINTK(fmt, args...)
74#endif
75
76#if PACKET_DEBUG > 1
77#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
78#else
79#define VPRINTK(fmt, args...)
80#endif
81
82#define MAX_SPEED 0xffff
83
84#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
85
86static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
87static struct proc_dir_entry *pkt_proc;
Thomas Maieradd21662006-10-04 02:15:30 -070088static int pktdev_major;
Thomas Maier0a0fc962006-12-08 02:36:11 -080089static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
90static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
Jes Sorensen1657f822006-03-23 03:00:25 -080091static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static mempool_t *psd_pool;
93
Thomas Maier32694852006-12-08 02:36:12 -080094static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
95static struct dentry *pkt_debugfs_root = NULL; /* /debug/pktcdvd */
96
97/* forward declaration */
98static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
99static int pkt_remove_dev(dev_t pkt_dev);
100static int pkt_seq_show(struct seq_file *m, void *p);
101
102
103
104/*
105 * create and register a pktcdvd kernel object.
106 */
107static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
108 const char* name,
109 struct kobject* parent,
110 struct kobj_type* ktype)
111{
112 struct pktcdvd_kobj *p;
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400113 int error;
114
Thomas Maier32694852006-12-08 02:36:12 -0800115 p = kzalloc(sizeof(*p), GFP_KERNEL);
116 if (!p)
117 return NULL;
Thomas Maier32694852006-12-08 02:36:12 -0800118 p->pd = pd;
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400119 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
120 if (error) {
Dave Youngd17a18d2007-12-17 16:20:00 -0800121 kobject_put(&p->kobj);
Thomas Maier32694852006-12-08 02:36:12 -0800122 return NULL;
Dave Youngd17a18d2007-12-17 16:20:00 -0800123 }
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400124 kobject_uevent(&p->kobj, KOBJ_ADD);
Thomas Maier32694852006-12-08 02:36:12 -0800125 return p;
126}
127/*
128 * remove a pktcdvd kernel object.
129 */
130static void pkt_kobj_remove(struct pktcdvd_kobj *p)
131{
132 if (p)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800133 kobject_put(&p->kobj);
Thomas Maier32694852006-12-08 02:36:12 -0800134}
135/*
136 * default release function for pktcdvd kernel objects.
137 */
138static void pkt_kobj_release(struct kobject *kobj)
139{
140 kfree(to_pktcdvdkobj(kobj));
141}
142
143
144/**********************************************************
145 *
146 * sysfs interface for pktcdvd
147 * by (C) 2006 Thomas Maier <balagi@justmail.de>
148 *
149 **********************************************************/
150
151#define DEF_ATTR(_obj,_name,_mode) \
Tejun Heo7b595752007-06-14 03:45:17 +0900152 static struct attribute _obj = { .name = _name, .mode = _mode }
Thomas Maier32694852006-12-08 02:36:12 -0800153
154/**********************************************************
155 /sys/class/pktcdvd/pktcdvd[0-7]/
156 stat/reset
157 stat/packets_started
158 stat/packets_finished
159 stat/kb_written
160 stat/kb_read
161 stat/kb_read_gather
162 write_queue/size
163 write_queue/congestion_off
164 write_queue/congestion_on
165 **********************************************************/
166
167DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
168DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
169DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
170DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
171DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
172DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
173
174static struct attribute *kobj_pkt_attrs_stat[] = {
175 &kobj_pkt_attr_st1,
176 &kobj_pkt_attr_st2,
177 &kobj_pkt_attr_st3,
178 &kobj_pkt_attr_st4,
179 &kobj_pkt_attr_st5,
180 &kobj_pkt_attr_st6,
181 NULL
182};
183
184DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
185DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
186DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
187
188static struct attribute *kobj_pkt_attrs_wqueue[] = {
189 &kobj_pkt_attr_wq1,
190 &kobj_pkt_attr_wq2,
191 &kobj_pkt_attr_wq3,
192 NULL
193};
194
Thomas Maier32694852006-12-08 02:36:12 -0800195static ssize_t kobj_pkt_show(struct kobject *kobj,
196 struct attribute *attr, char *data)
197{
198 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
199 int n = 0;
200 int v;
201 if (strcmp(attr->name, "packets_started") == 0) {
202 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
203
204 } else if (strcmp(attr->name, "packets_finished") == 0) {
205 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
206
207 } else if (strcmp(attr->name, "kb_written") == 0) {
208 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
209
210 } else if (strcmp(attr->name, "kb_read") == 0) {
211 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
212
213 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
214 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
215
216 } else if (strcmp(attr->name, "size") == 0) {
217 spin_lock(&pd->lock);
218 v = pd->bio_queue_size;
219 spin_unlock(&pd->lock);
220 n = sprintf(data, "%d\n", v);
221
222 } else if (strcmp(attr->name, "congestion_off") == 0) {
223 spin_lock(&pd->lock);
224 v = pd->write_congestion_off;
225 spin_unlock(&pd->lock);
226 n = sprintf(data, "%d\n", v);
227
228 } else if (strcmp(attr->name, "congestion_on") == 0) {
229 spin_lock(&pd->lock);
230 v = pd->write_congestion_on;
231 spin_unlock(&pd->lock);
232 n = sprintf(data, "%d\n", v);
233 }
234 return n;
235}
236
237static void init_write_congestion_marks(int* lo, int* hi)
238{
239 if (*hi > 0) {
240 *hi = max(*hi, 500);
241 *hi = min(*hi, 1000000);
242 if (*lo <= 0)
243 *lo = *hi - 100;
244 else {
245 *lo = min(*lo, *hi - 100);
246 *lo = max(*lo, 100);
247 }
248 } else {
249 *hi = -1;
250 *lo = -1;
251 }
252}
253
254static ssize_t kobj_pkt_store(struct kobject *kobj,
255 struct attribute *attr,
256 const char *data, size_t len)
257{
258 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
259 int val;
Thomas Maier32694852006-12-08 02:36:12 -0800260
Thomas Maier83f3aa32007-02-10 01:45:11 -0800261 if (strcmp(attr->name, "reset") == 0 && len > 0) {
Thomas Maier32694852006-12-08 02:36:12 -0800262 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
Thomas Maier83f3aa32007-02-10 01:45:11 -0800269 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800270 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
Thomas Maier83f3aa32007-02-10 01:45:11 -0800277 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800278 spin_lock(&pd->lock);
279 pd->write_congestion_on = val;
280 init_write_congestion_marks(&pd->write_congestion_off,
281 &pd->write_congestion_on);
282 spin_unlock(&pd->lock);
283 }
284 return len;
285}
286
287static struct sysfs_ops kobj_pkt_ops = {
288 .show = kobj_pkt_show,
289 .store = kobj_pkt_store
290};
291static struct kobj_type kobj_pkt_type_stat = {
292 .release = pkt_kobj_release,
293 .sysfs_ops = &kobj_pkt_ops,
294 .default_attrs = kobj_pkt_attrs_stat
295};
296static struct kobj_type kobj_pkt_type_wqueue = {
297 .release = pkt_kobj_release,
298 .sysfs_ops = &kobj_pkt_ops,
299 .default_attrs = kobj_pkt_attrs_wqueue
300};
301
302static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
303{
304 if (class_pktcdvd) {
Tony Jones6013c122007-09-25 02:03:03 +0200305 pd->dev = device_create(class_pktcdvd, NULL, pd->pkt_dev, "%s", pd->name);
306 if (IS_ERR(pd->dev))
307 pd->dev = NULL;
Thomas Maier32694852006-12-08 02:36:12 -0800308 }
Tony Jones6013c122007-09-25 02:03:03 +0200309 if (pd->dev) {
Thomas Maier32694852006-12-08 02:36:12 -0800310 pd->kobj_stat = pkt_kobj_create(pd, "stat",
Tony Jones6013c122007-09-25 02:03:03 +0200311 &pd->dev->kobj,
Thomas Maier32694852006-12-08 02:36:12 -0800312 &kobj_pkt_type_stat);
313 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
Tony Jones6013c122007-09-25 02:03:03 +0200314 &pd->dev->kobj,
Thomas Maier32694852006-12-08 02:36:12 -0800315 &kobj_pkt_type_wqueue);
316 }
317}
318
319static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
320{
321 pkt_kobj_remove(pd->kobj_stat);
322 pkt_kobj_remove(pd->kobj_wqueue);
323 if (class_pktcdvd)
Tony Jones6013c122007-09-25 02:03:03 +0200324 device_destroy(class_pktcdvd, pd->pkt_dev);
Thomas Maier32694852006-12-08 02:36:12 -0800325}
326
327
328/********************************************************************
329 /sys/class/pktcdvd/
330 add map block device
331 remove unmap packet dev
332 device_map show mappings
333 *******************************************************************/
334
335static void class_pktcdvd_release(struct class *cls)
336{
337 kfree(cls);
338}
339static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
340{
341 int n = 0;
342 int idx;
343 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
344 for (idx = 0; idx < MAX_WRITERS; idx++) {
345 struct pktcdvd_device *pd = pkt_devs[idx];
346 if (!pd)
347 continue;
348 n += sprintf(data+n, "%s %u:%u %u:%u\n",
349 pd->name,
350 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
351 MAJOR(pd->bdev->bd_dev),
352 MINOR(pd->bdev->bd_dev));
353 }
354 mutex_unlock(&ctl_mutex);
355 return n;
356}
357
358static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
359 size_t count)
360{
361 unsigned int major, minor;
Tejun Heofffe4872007-11-08 08:00:24 +0100362
Thomas Maier83f3aa32007-02-10 01:45:11 -0800363 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Tejun Heofffe4872007-11-08 08:00:24 +0100364 /* pkt_setup_dev() expects caller to hold reference to self */
365 if (!try_module_get(THIS_MODULE))
366 return -ENODEV;
367
Thomas Maier32694852006-12-08 02:36:12 -0800368 pkt_setup_dev(MKDEV(major, minor), NULL);
Tejun Heofffe4872007-11-08 08:00:24 +0100369
370 module_put(THIS_MODULE);
371
Thomas Maier32694852006-12-08 02:36:12 -0800372 return count;
373 }
Tejun Heofffe4872007-11-08 08:00:24 +0100374
Thomas Maier32694852006-12-08 02:36:12 -0800375 return -EINVAL;
376}
377
378static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
379 size_t count)
380{
381 unsigned int major, minor;
Thomas Maier83f3aa32007-02-10 01:45:11 -0800382 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Thomas Maier32694852006-12-08 02:36:12 -0800383 pkt_remove_dev(MKDEV(major, minor));
384 return count;
385 }
386 return -EINVAL;
387}
388
389static struct class_attribute class_pktcdvd_attrs[] = {
390 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
391 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
392 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
393 __ATTR_NULL
394};
395
396
397static int pkt_sysfs_init(void)
398{
399 int ret = 0;
400
401 /*
402 * create control files in sysfs
403 * /sys/class/pktcdvd/...
404 */
405 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
406 if (!class_pktcdvd)
407 return -ENOMEM;
408 class_pktcdvd->name = DRIVER_NAME;
409 class_pktcdvd->owner = THIS_MODULE;
410 class_pktcdvd->class_release = class_pktcdvd_release;
411 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
412 ret = class_register(class_pktcdvd);
413 if (ret) {
414 kfree(class_pktcdvd);
415 class_pktcdvd = NULL;
416 printk(DRIVER_NAME": failed to create class pktcdvd\n");
417 return ret;
418 }
419 return 0;
420}
421
422static void pkt_sysfs_cleanup(void)
423{
424 if (class_pktcdvd)
425 class_destroy(class_pktcdvd);
426 class_pktcdvd = NULL;
427}
428
429/********************************************************************
430 entries in debugfs
431
432 /debugfs/pktcdvd[0-7]/
433 info
434
435 *******************************************************************/
436
437static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
438{
439 return pkt_seq_show(m, p);
440}
441
442static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
443{
444 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
445}
446
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800447static const struct file_operations debug_fops = {
Thomas Maier32694852006-12-08 02:36:12 -0800448 .open = pkt_debugfs_fops_open,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = single_release,
452 .owner = THIS_MODULE,
453};
454
455static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
456{
457 if (!pkt_debugfs_root)
458 return;
459 pd->dfs_f_info = NULL;
460 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
461 if (IS_ERR(pd->dfs_d_root)) {
462 pd->dfs_d_root = NULL;
463 return;
464 }
465 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
466 pd->dfs_d_root, pd, &debug_fops);
467 if (IS_ERR(pd->dfs_f_info)) {
468 pd->dfs_f_info = NULL;
469 return;
470 }
471}
472
473static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
474{
475 if (!pkt_debugfs_root)
476 return;
477 if (pd->dfs_f_info)
478 debugfs_remove(pd->dfs_f_info);
479 pd->dfs_f_info = NULL;
480 if (pd->dfs_d_root)
481 debugfs_remove(pd->dfs_d_root);
482 pd->dfs_d_root = NULL;
483}
484
485static void pkt_debugfs_init(void)
486{
487 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
488 if (IS_ERR(pkt_debugfs_root)) {
489 pkt_debugfs_root = NULL;
490 return;
491 }
492}
493
494static void pkt_debugfs_cleanup(void)
495{
496 if (!pkt_debugfs_root)
497 return;
498 debugfs_remove(pkt_debugfs_root);
499 pkt_debugfs_root = NULL;
500}
501
502/* ----------------------------------------------------------*/
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505static void pkt_bio_finished(struct pktcdvd_device *pd)
506{
507 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
508 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
Thomas Maier78220822006-10-04 02:15:28 -0700509 VPRINTK(DRIVER_NAME": queue empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 atomic_set(&pd->iosched.attention, 1);
511 wake_up(&pd->wqueue);
512 }
513}
514
515static void pkt_bio_destructor(struct bio *bio)
516{
517 kfree(bio->bi_io_vec);
518 kfree(bio);
519}
520
521static struct bio *pkt_bio_alloc(int nr_iovecs)
522{
523 struct bio_vec *bvl = NULL;
524 struct bio *bio;
525
526 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
527 if (!bio)
528 goto no_bio;
529 bio_init(bio);
530
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700531 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!bvl)
533 goto no_bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 bio->bi_max_vecs = nr_iovecs;
536 bio->bi_io_vec = bvl;
537 bio->bi_destructor = pkt_bio_destructor;
538
539 return bio;
540
541 no_bvl:
542 kfree(bio);
543 no_bio:
544 return NULL;
545}
546
547/*
548 * Allocate a packet_data struct
549 */
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800550static struct packet_data *pkt_alloc_packet_data(int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 int i;
553 struct packet_data *pkt;
554
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700555 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!pkt)
557 goto no_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800559 pkt->frames = frames;
560 pkt->w_bio = pkt_bio_alloc(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (!pkt->w_bio)
562 goto no_bio;
563
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800564 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
566 if (!pkt->pages[i])
567 goto no_page;
568 }
569
570 spin_lock_init(&pkt->lock);
571
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800572 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct bio *bio = pkt_bio_alloc(1);
574 if (!bio)
575 goto no_rd_bio;
576 pkt->r_bios[i] = bio;
577 }
578
579 return pkt;
580
581no_rd_bio:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800582 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct bio *bio = pkt->r_bios[i];
584 if (bio)
585 bio_put(bio);
586 }
587
588no_page:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800589 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (pkt->pages[i])
591 __free_page(pkt->pages[i]);
592 bio_put(pkt->w_bio);
593no_bio:
594 kfree(pkt);
595no_pkt:
596 return NULL;
597}
598
599/*
600 * Free a packet_data struct
601 */
602static void pkt_free_packet_data(struct packet_data *pkt)
603{
604 int i;
605
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800606 for (i = 0; i < pkt->frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct bio *bio = pkt->r_bios[i];
608 if (bio)
609 bio_put(bio);
610 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800611 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 __free_page(pkt->pages[i]);
613 bio_put(pkt->w_bio);
614 kfree(pkt);
615}
616
617static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
618{
619 struct packet_data *pkt, *next;
620
621 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
622
623 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
624 pkt_free_packet_data(pkt);
625 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800626 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
630{
631 struct packet_data *pkt;
632
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800633 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 while (nr_packets > 0) {
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800636 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!pkt) {
638 pkt_shrink_pktlist(pd);
639 return 0;
640 }
641 pkt->id = nr_packets;
642 pkt->pd = pd;
643 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
644 nr_packets--;
645 }
646 return 1;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
650{
651 struct rb_node *n = rb_next(&node->rb_node);
652 if (!n)
653 return NULL;
654 return rb_entry(n, struct pkt_rb_node, rb_node);
655}
656
Peter Osterlundac893962006-01-14 13:21:32 -0800657static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 rb_erase(&node->rb_node, &pd->bio_queue);
660 mempool_free(node, pd->rb_pool);
661 pd->bio_queue_size--;
662 BUG_ON(pd->bio_queue_size < 0);
663}
664
665/*
666 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
667 */
668static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
669{
670 struct rb_node *n = pd->bio_queue.rb_node;
671 struct rb_node *next;
672 struct pkt_rb_node *tmp;
673
674 if (!n) {
675 BUG_ON(pd->bio_queue_size > 0);
676 return NULL;
677 }
678
679 for (;;) {
680 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
681 if (s <= tmp->bio->bi_sector)
682 next = n->rb_left;
683 else
684 next = n->rb_right;
685 if (!next)
686 break;
687 n = next;
688 }
689
690 if (s > tmp->bio->bi_sector) {
691 tmp = pkt_rbtree_next(tmp);
692 if (!tmp)
693 return NULL;
694 }
695 BUG_ON(s > tmp->bio->bi_sector);
696 return tmp;
697}
698
699/*
700 * Insert a node into the pd->bio_queue rb tree.
701 */
702static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
703{
704 struct rb_node **p = &pd->bio_queue.rb_node;
705 struct rb_node *parent = NULL;
706 sector_t s = node->bio->bi_sector;
707 struct pkt_rb_node *tmp;
708
709 while (*p) {
710 parent = *p;
711 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
712 if (s < tmp->bio->bi_sector)
713 p = &(*p)->rb_left;
714 else
715 p = &(*p)->rb_right;
716 }
717 rb_link_node(&node->rb_node, parent, p);
718 rb_insert_color(&node->rb_node, &pd->bio_queue);
719 pd->bio_queue_size++;
720}
721
722/*
723 * Add a bio to a single linked list defined by its head and tail pointers.
724 */
Peter Osterlundac893962006-01-14 13:21:32 -0800725static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 bio->bi_next = NULL;
728 if (*list_tail) {
729 BUG_ON((*list_head) == NULL);
730 (*list_tail)->bi_next = bio;
731 (*list_tail) = bio;
732 } else {
733 BUG_ON((*list_head) != NULL);
734 (*list_head) = bio;
735 (*list_tail) = bio;
736 }
737}
738
739/*
740 * Remove and return the first bio from a single linked list defined by its
741 * head and tail pointers.
742 */
743static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
744{
745 struct bio *bio;
746
747 if (*list_head == NULL)
748 return NULL;
749
750 bio = *list_head;
751 *list_head = bio->bi_next;
752 if (*list_head == NULL)
753 *list_tail = NULL;
754
755 bio->bi_next = NULL;
756 return bio;
757}
758
759/*
760 * Send a packet_command to the underlying block device and
761 * wait for completion.
762 */
763static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
764{
Jens Axboe165125e2007-07-24 09:28:11 +0200765 struct request_queue *q = bdev_get_queue(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct request *rq;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800767 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800769 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
770 WRITE : READ, __GFP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800772 if (cgc->buflen) {
773 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
774 goto out;
775 }
776
Gerhard Dirschl91e4ee32007-02-20 13:57:56 -0800777 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800778 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 rq->timeout = 60*HZ;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200781 rq->cmd_type = REQ_TYPE_BLOCK_PC;
782 rq->cmd_flags |= REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (cgc->quiet)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200784 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800786 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
Andrew Mortoncbc31a42007-04-25 13:01:21 -0700787 if (rq->errors)
788 ret = -EIO;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800789out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 blk_put_request(rq);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800791 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794/*
795 * A generic sense dump / resolve mechanism should be implemented across
796 * all ATAPI + SCSI devices.
797 */
798static void pkt_dump_sense(struct packet_command *cgc)
799{
800 static char *info[9] = { "No sense", "Recovered error", "Not ready",
801 "Medium error", "Hardware error", "Illegal request",
802 "Unit attention", "Data protect", "Blank check" };
803 int i;
804 struct request_sense *sense = cgc->sense;
805
Thomas Maier78220822006-10-04 02:15:28 -0700806 printk(DRIVER_NAME":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 for (i = 0; i < CDROM_PACKET_SIZE; i++)
808 printk(" %02x", cgc->cmd[i]);
809 printk(" - ");
810
811 if (sense == NULL) {
812 printk("no sense\n");
813 return;
814 }
815
816 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
817
818 if (sense->sense_key > 8) {
819 printk(" (INVALID)\n");
820 return;
821 }
822
823 printk(" (%s)\n", info[sense->sense_key]);
824}
825
826/*
827 * flush the drive cache to media
828 */
829static int pkt_flush_cache(struct pktcdvd_device *pd)
830{
831 struct packet_command cgc;
832
833 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
834 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
835 cgc.quiet = 1;
836
837 /*
838 * the IMMED bit -- we default to not setting it, although that
839 * would allow a much faster close, this is safer
840 */
841#if 0
842 cgc.cmd[1] = 1 << 1;
843#endif
844 return pkt_generic_packet(pd, &cgc);
845}
846
847/*
848 * speed is given as the normal factor, e.g. 4 for 4x
849 */
Peter Osterlund05680d82008-03-04 14:28:41 -0800850static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
851 unsigned write_speed, unsigned read_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct packet_command cgc;
854 struct request_sense sense;
855 int ret;
856
857 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
858 cgc.sense = &sense;
859 cgc.cmd[0] = GPCMD_SET_SPEED;
860 cgc.cmd[2] = (read_speed >> 8) & 0xff;
861 cgc.cmd[3] = read_speed & 0xff;
862 cgc.cmd[4] = (write_speed >> 8) & 0xff;
863 cgc.cmd[5] = write_speed & 0xff;
864
865 if ((ret = pkt_generic_packet(pd, &cgc)))
866 pkt_dump_sense(&cgc);
867
868 return ret;
869}
870
871/*
872 * Queue a bio for processing by the low-level CD device. Must be called
873 * from process context.
874 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700875static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 spin_lock(&pd->iosched.lock);
878 if (bio_data_dir(bio) == READ) {
879 pkt_add_list_last(bio, &pd->iosched.read_queue,
880 &pd->iosched.read_queue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 } else {
882 pkt_add_list_last(bio, &pd->iosched.write_queue,
883 &pd->iosched.write_queue_tail);
884 }
885 spin_unlock(&pd->iosched.lock);
886
887 atomic_set(&pd->iosched.attention, 1);
888 wake_up(&pd->wqueue);
889}
890
891/*
892 * Process the queued read/write requests. This function handles special
893 * requirements for CDRW drives:
894 * - A cache flush command must be inserted before a read request if the
895 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700896 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700898 * - Optimize for throughput at the expense of latency. This means that streaming
899 * writes will never be interrupted by a read, but if the drive has to seek
900 * before the next write, switch to reading instead if there are any pending
901 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * - Set the read speed according to current usage pattern. When only reading
903 * from the device, it's best to use the highest possible read speed, but
904 * when switching often between reading and writing, it's better to have the
905 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
907static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
908{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (atomic_read(&pd->iosched.attention) == 0)
911 return;
912 atomic_set(&pd->iosched.attention, 0);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 for (;;) {
915 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700916 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 spin_lock(&pd->iosched.lock);
919 reads_queued = (pd->iosched.read_queue != NULL);
920 writes_queued = (pd->iosched.write_queue != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 spin_unlock(&pd->iosched.lock);
922
923 if (!reads_queued && !writes_queued)
924 break;
925
926 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700927 int need_write_seek = 1;
928 spin_lock(&pd->iosched.lock);
929 bio = pd->iosched.write_queue;
930 spin_unlock(&pd->iosched.lock);
931 if (bio && (bio->bi_sector == pd->iosched.last_write))
932 need_write_seek = 0;
933 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700935 VPRINTK(DRIVER_NAME": write, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 break;
937 }
938 pkt_flush_cache(pd);
939 pd->iosched.writing = 0;
940 }
941 } else {
942 if (!reads_queued && writes_queued) {
943 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700944 VPRINTK(DRIVER_NAME": read, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
946 }
947 pd->iosched.writing = 1;
948 }
949 }
950
951 spin_lock(&pd->iosched.lock);
952 if (pd->iosched.writing) {
953 bio = pkt_get_list_first(&pd->iosched.write_queue,
954 &pd->iosched.write_queue_tail);
955 } else {
956 bio = pkt_get_list_first(&pd->iosched.read_queue,
957 &pd->iosched.read_queue_tail);
958 }
959 spin_unlock(&pd->iosched.lock);
960
961 if (!bio)
962 continue;
963
964 if (bio_data_dir(bio) == READ)
965 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700966 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700968 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
971 if (pd->read_speed == pd->write_speed) {
972 pd->read_speed = MAX_SPEED;
973 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
974 }
975 } else {
976 if (pd->read_speed != pd->write_speed) {
977 pd->read_speed = pd->write_speed;
978 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
979 }
980 }
981
982 atomic_inc(&pd->cdrw.pending_bios);
983 generic_make_request(bio);
984 }
985}
986
987/*
988 * Special care is needed if the underlying block device has a small
989 * max_phys_segments value.
990 */
Jens Axboe165125e2007-07-24 09:28:11 +0200991static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
994 /*
995 * The cdrom device can handle one segment/frame
996 */
997 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
998 return 0;
999 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
1000 /*
1001 * We can handle this case at the expense of some extra memory
1002 * copies during write operations
1003 */
1004 set_bit(PACKET_MERGE_SEGS, &pd->flags);
1005 return 0;
1006 } else {
Thomas Maier78220822006-10-04 02:15:28 -07001007 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EIO;
1009 }
1010}
1011
1012/*
1013 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1014 */
1015static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
1016{
1017 unsigned int copy_size = CD_FRAMESIZE;
1018
1019 while (copy_size > 0) {
1020 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
1021 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
1022 src_bvl->bv_offset + offs;
1023 void *vto = page_address(dst_page) + dst_offs;
1024 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
1025
1026 BUG_ON(len < 0);
1027 memcpy(vto, vfrom, len);
1028 kunmap_atomic(vfrom, KM_USER0);
1029
1030 seg++;
1031 offs = 0;
1032 dst_offs += len;
1033 copy_size -= len;
1034 }
1035}
1036
1037/*
1038 * Copy all data for this packet to pkt->pages[], so that
1039 * a) The number of required segments for the write bio is minimized, which
1040 * is necessary for some scsi controllers.
1041 * b) The data can be used as cache to avoid read requests if we receive a
1042 * new write request for the same zone.
1043 */
Peter Osterlund72772322006-02-14 13:52:56 -08001044static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 int f, p, offs;
1047
1048 /* Copy all data to pkt->pages[] */
1049 p = 0;
1050 offs = 0;
1051 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001052 if (bvec[f].bv_page != pkt->pages[p]) {
1053 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 void *vto = page_address(pkt->pages[p]) + offs;
1055 memcpy(vto, vfrom, CD_FRAMESIZE);
1056 kunmap_atomic(vfrom, KM_USER0);
Peter Osterlund72772322006-02-14 13:52:56 -08001057 bvec[f].bv_page = pkt->pages[p];
1058 bvec[f].bv_offset = offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 } else {
Peter Osterlund72772322006-02-14 13:52:56 -08001060 BUG_ON(bvec[f].bv_offset != offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 offs += CD_FRAMESIZE;
1063 if (offs >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 offs = 0;
1065 p++;
1066 }
1067 }
1068}
1069
NeilBrown6712ecf2007-09-27 12:47:43 +02001070static void pkt_end_io_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct packet_data *pkt = bio->bi_private;
1073 struct pktcdvd_device *pd = pkt->pd;
1074 BUG_ON(!pd);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1077 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1078
1079 if (err)
1080 atomic_inc(&pkt->io_errors);
1081 if (atomic_dec_and_test(&pkt->io_wait)) {
1082 atomic_inc(&pkt->run_sm);
1083 wake_up(&pd->wqueue);
1084 }
1085 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
NeilBrown6712ecf2007-09-27 12:47:43 +02001088static void pkt_end_io_packet_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 struct packet_data *pkt = bio->bi_private;
1091 struct pktcdvd_device *pd = pkt->pd;
1092 BUG_ON(!pd);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1095
1096 pd->stats.pkt_ended++;
1097
1098 pkt_bio_finished(pd);
1099 atomic_dec(&pkt->io_wait);
1100 atomic_inc(&pkt->run_sm);
1101 wake_up(&pd->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*
1105 * Schedule reads for the holes in a packet
1106 */
1107static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1108{
1109 int frames_read = 0;
1110 struct bio *bio;
1111 int f;
1112 char written[PACKET_MAX_SIZE];
1113
1114 BUG_ON(!pkt->orig_bios);
1115
1116 atomic_set(&pkt->io_wait, 0);
1117 atomic_set(&pkt->io_errors, 0);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Figure out which frames we need to read before we can write.
1121 */
1122 memset(written, 0, sizeof(written));
1123 spin_lock(&pkt->lock);
1124 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1125 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1126 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001127 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 BUG_ON(first_frame < 0);
1129 BUG_ON(first_frame + num_frames > pkt->frames);
1130 for (f = first_frame; f < first_frame + num_frames; f++)
1131 written[f] = 1;
1132 }
1133 spin_unlock(&pkt->lock);
1134
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001135 if (pkt->cache_valid) {
1136 VPRINTK("pkt_gather_data: zone %llx cached\n",
1137 (unsigned long long)pkt->sector);
1138 goto out_account;
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /*
1142 * Schedule reads for missing parts of the packet.
1143 */
1144 for (f = 0; f < pkt->frames; f++) {
Jens Axboe761a15e2007-09-14 13:06:53 +02001145 struct bio_vec *vec;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int p, offset;
1148 if (written[f])
1149 continue;
1150 bio = pkt->r_bios[f];
Jens Axboe761a15e2007-09-14 13:06:53 +02001151 vec = bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 bio_init(bio);
1153 bio->bi_max_vecs = 1;
1154 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1155 bio->bi_bdev = pd->bdev;
1156 bio->bi_end_io = pkt_end_io_read;
1157 bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001158 bio->bi_io_vec = vec;
Laurent Riffard7e3da6c2007-09-21 08:32:28 +02001159 bio->bi_destructor = pkt_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1162 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1163 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1164 f, pkt->pages[p], offset);
1165 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1166 BUG();
1167
1168 atomic_inc(&pkt->io_wait);
1169 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001170 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 frames_read++;
1172 }
1173
1174out_account:
1175 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1176 frames_read, (unsigned long long)pkt->sector);
1177 pd->stats.pkt_started++;
1178 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181/*
1182 * Find a packet matching zone, or the least recently used packet if
1183 * there is no match.
1184 */
1185static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1186{
1187 struct packet_data *pkt;
1188
1189 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1190 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1191 list_del_init(&pkt->list);
1192 if (pkt->sector != zone)
1193 pkt->cache_valid = 0;
Peter Osterlund610827d2005-09-13 01:25:29 -07001194 return pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 }
Peter Osterlund610827d2005-09-13 01:25:29 -07001197 BUG();
1198 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1202{
1203 if (pkt->cache_valid) {
1204 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1205 } else {
1206 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1207 }
1208}
1209
1210/*
1211 * recover a failed write, query for relocation if possible
1212 *
1213 * returns 1 if recovery is possible, or 0 if not
1214 *
1215 */
1216static int pkt_start_recovery(struct packet_data *pkt)
1217{
1218 /*
1219 * FIXME. We need help from the file system to implement
1220 * recovery handling.
1221 */
1222 return 0;
1223#if 0
1224 struct request *rq = pkt->rq;
1225 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1226 struct block_device *pkt_bdev;
1227 struct super_block *sb = NULL;
1228 unsigned long old_block, new_block;
1229 sector_t new_sector;
1230
1231 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1232 if (pkt_bdev) {
1233 sb = get_super(pkt_bdev);
1234 bdput(pkt_bdev);
1235 }
1236
1237 if (!sb)
1238 return 0;
1239
1240 if (!sb->s_op || !sb->s_op->relocate_blocks)
1241 goto out;
1242
1243 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1244 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1245 goto out;
1246
1247 new_sector = new_block * (CD_FRAMESIZE >> 9);
1248 pkt->sector = new_sector;
1249
1250 pkt->bio->bi_sector = new_sector;
1251 pkt->bio->bi_next = NULL;
1252 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1253 pkt->bio->bi_idx = 0;
1254
1255 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1256 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1257 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1258 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1259 BUG_ON(pkt->bio->bi_private != pkt);
1260
1261 drop_super(sb);
1262 return 1;
1263
1264out:
1265 drop_super(sb);
1266 return 0;
1267#endif
1268}
1269
1270static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1271{
1272#if PACKET_DEBUG > 1
1273 static const char *state_name[] = {
1274 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1275 };
1276 enum packet_data_state old_state = pkt->state;
1277 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1278 state_name[old_state], state_name[state]);
1279#endif
1280 pkt->state = state;
1281}
1282
1283/*
1284 * Scan the work queue to see if we can start a new packet.
1285 * returns non-zero if any work was done.
1286 */
1287static int pkt_handle_queue(struct pktcdvd_device *pd)
1288{
1289 struct packet_data *pkt, *p;
1290 struct bio *bio = NULL;
1291 sector_t zone = 0; /* Suppress gcc warning */
1292 struct pkt_rb_node *node, *first_node;
1293 struct rb_node *n;
Thomas Maier0a0fc962006-12-08 02:36:11 -08001294 int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 VPRINTK("handle_queue\n");
1297
1298 atomic_set(&pd->scan_queue, 0);
1299
1300 if (list_empty(&pd->cdrw.pkt_free_list)) {
1301 VPRINTK("handle_queue: no pkt\n");
1302 return 0;
1303 }
1304
1305 /*
1306 * Try to find a zone we are not already working on.
1307 */
1308 spin_lock(&pd->lock);
1309 first_node = pkt_rbtree_find(pd, pd->current_sector);
1310 if (!first_node) {
1311 n = rb_first(&pd->bio_queue);
1312 if (n)
1313 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1314 }
1315 node = first_node;
1316 while (node) {
1317 bio = node->bio;
1318 zone = ZONE(bio->bi_sector, pd);
1319 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001320 if (p->sector == zone) {
1321 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 break;
1326try_next_bio:
1327 node = pkt_rbtree_next(node);
1328 if (!node) {
1329 n = rb_first(&pd->bio_queue);
1330 if (n)
1331 node = rb_entry(n, struct pkt_rb_node, rb_node);
1332 }
1333 if (node == first_node)
1334 node = NULL;
1335 }
1336 spin_unlock(&pd->lock);
1337 if (!bio) {
1338 VPRINTK("handle_queue: no bio\n");
1339 return 0;
1340 }
1341
1342 pkt = pkt_get_packet_data(pd, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 pd->current_sector = zone + pd->settings.size;
1345 pkt->sector = zone;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001346 BUG_ON(pkt->frames != pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 pkt->write_size = 0;
1348
1349 /*
1350 * Scan work queue for bios in the same zone and link them
1351 * to this packet.
1352 */
1353 spin_lock(&pd->lock);
1354 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1355 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1356 bio = node->bio;
1357 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1358 (unsigned long long)ZONE(bio->bi_sector, pd));
1359 if (ZONE(bio->bi_sector, pd) != zone)
1360 break;
1361 pkt_rbtree_erase(pd, node);
1362 spin_lock(&pkt->lock);
1363 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1364 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1365 spin_unlock(&pkt->lock);
1366 }
Thomas Maier0a0fc962006-12-08 02:36:11 -08001367 /* check write congestion marks, and if bio_queue_size is
1368 below, wake up any waiters */
1369 wakeup = (pd->write_congestion_on > 0
1370 && pd->bio_queue_size <= pd->write_congestion_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 spin_unlock(&pd->lock);
Thomas Maier0a0fc962006-12-08 02:36:11 -08001372 if (wakeup)
Thomas Maier83f3aa32007-02-10 01:45:11 -08001373 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1376 pkt_set_state(pkt, PACKET_WAITING_STATE);
1377 atomic_set(&pkt->run_sm, 1);
1378
1379 spin_lock(&pd->cdrw.active_list_lock);
1380 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1381 spin_unlock(&pd->cdrw.active_list_lock);
1382
1383 return 1;
1384}
1385
1386/*
1387 * Assemble a bio to write one packet and queue the bio for processing
1388 * by the underlying block device.
1389 */
1390static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1391{
1392 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int f;
1394 int frames_write;
Peter Osterlund72772322006-02-14 13:52:56 -08001395 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001398 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1399 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 /*
Peter Osterlund72772322006-02-14 13:52:56 -08001403 * Fill-in bvec with data from orig_bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405 frames_write = 0;
1406 spin_lock(&pkt->lock);
1407 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1408 int segment = bio->bi_idx;
1409 int src_offs = 0;
1410 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1411 int num_frames = bio->bi_size / CD_FRAMESIZE;
1412 BUG_ON(first_frame < 0);
1413 BUG_ON(first_frame + num_frames > pkt->frames);
1414 for (f = first_frame; f < first_frame + num_frames; f++) {
1415 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1416
1417 while (src_offs >= src_bvl->bv_len) {
1418 src_offs -= src_bvl->bv_len;
1419 segment++;
1420 BUG_ON(segment >= bio->bi_vcnt);
1421 src_bvl = bio_iovec_idx(bio, segment);
1422 }
1423
1424 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
Peter Osterlund72772322006-02-14 13:52:56 -08001425 bvec[f].bv_page = src_bvl->bv_page;
1426 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 } else {
1428 pkt_copy_bio_data(bio, segment, src_offs,
Peter Osterlund72772322006-02-14 13:52:56 -08001429 bvec[f].bv_page, bvec[f].bv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431 src_offs += CD_FRAMESIZE;
1432 frames_write++;
1433 }
1434 }
1435 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1436 spin_unlock(&pkt->lock);
1437
1438 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1439 frames_write, (unsigned long long)pkt->sector);
1440 BUG_ON(frames_write != pkt->write_size);
1441
1442 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
Peter Osterlund72772322006-02-14 13:52:56 -08001443 pkt_make_local_copy(pkt, bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 pkt->cache_valid = 1;
1445 } else {
1446 pkt->cache_valid = 0;
1447 }
1448
1449 /* Start the write request */
1450 bio_init(pkt->w_bio);
1451 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1452 pkt->w_bio->bi_sector = pkt->sector;
1453 pkt->w_bio->bi_bdev = pd->bdev;
1454 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1455 pkt->w_bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001456 pkt->w_bio->bi_io_vec = bvec;
Laurent Riffard7e3da6c2007-09-21 08:32:28 +02001457 pkt->w_bio->bi_destructor = pkt_bio_destructor;
Peter Osterlund72772322006-02-14 13:52:56 -08001458 for (f = 0; f < pkt->frames; f++)
1459 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1460 BUG();
Thomas Maier78220822006-10-04 02:15:28 -07001461 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 atomic_set(&pkt->io_wait, 1);
1464 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001465 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1469{
1470 struct bio *bio, *next;
1471
1472 if (!uptodate)
1473 pkt->cache_valid = 0;
1474
1475 /* Finish all bios corresponding to this packet */
1476 bio = pkt->orig_bios;
1477 while (bio) {
1478 next = bio->bi_next;
1479 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001480 bio_endio(bio, uptodate ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 bio = next;
1482 }
1483 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1484}
1485
1486static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1487{
1488 int uptodate;
1489
1490 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1491
1492 for (;;) {
1493 switch (pkt->state) {
1494 case PACKET_WAITING_STATE:
1495 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1496 return;
1497
1498 pkt->sleep_time = 0;
1499 pkt_gather_data(pd, pkt);
1500 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1501 break;
1502
1503 case PACKET_READ_WAIT_STATE:
1504 if (atomic_read(&pkt->io_wait) > 0)
1505 return;
1506
1507 if (atomic_read(&pkt->io_errors) > 0) {
1508 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1509 } else {
1510 pkt_start_write(pd, pkt);
1511 }
1512 break;
1513
1514 case PACKET_WRITE_WAIT_STATE:
1515 if (atomic_read(&pkt->io_wait) > 0)
1516 return;
1517
1518 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1519 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1520 } else {
1521 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1522 }
1523 break;
1524
1525 case PACKET_RECOVERY_STATE:
1526 if (pkt_start_recovery(pkt)) {
1527 pkt_start_write(pd, pkt);
1528 } else {
1529 VPRINTK("No recovery possible\n");
1530 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1531 }
1532 break;
1533
1534 case PACKET_FINISHED_STATE:
1535 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1536 pkt_finish_packet(pkt, uptodate);
1537 return;
1538
1539 default:
1540 BUG();
1541 break;
1542 }
1543 }
1544}
1545
1546static void pkt_handle_packets(struct pktcdvd_device *pd)
1547{
1548 struct packet_data *pkt, *next;
1549
1550 VPRINTK("pkt_handle_packets\n");
1551
1552 /*
1553 * Run state machine for active packets
1554 */
1555 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1556 if (atomic_read(&pkt->run_sm) > 0) {
1557 atomic_set(&pkt->run_sm, 0);
1558 pkt_run_state_machine(pd, pkt);
1559 }
1560 }
1561
1562 /*
1563 * Move no longer active packets to the free list
1564 */
1565 spin_lock(&pd->cdrw.active_list_lock);
1566 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1567 if (pkt->state == PACKET_FINISHED_STATE) {
1568 list_del(&pkt->list);
1569 pkt_put_packet_data(pd, pkt);
1570 pkt_set_state(pkt, PACKET_IDLE_STATE);
1571 atomic_set(&pd->scan_queue, 1);
1572 }
1573 }
1574 spin_unlock(&pd->cdrw.active_list_lock);
1575}
1576
1577static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1578{
1579 struct packet_data *pkt;
1580 int i;
1581
Peter Osterlundae7642b2005-11-13 16:06:36 -08001582 for (i = 0; i < PACKET_NUM_STATES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 states[i] = 0;
1584
1585 spin_lock(&pd->cdrw.active_list_lock);
1586 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1587 states[pkt->state]++;
1588 }
1589 spin_unlock(&pd->cdrw.active_list_lock);
1590}
1591
1592/*
1593 * kcdrwd is woken up when writes have been queued for one of our
1594 * registered devices
1595 */
1596static int kcdrwd(void *foobar)
1597{
1598 struct pktcdvd_device *pd = foobar;
1599 struct packet_data *pkt;
1600 long min_sleep_time, residue;
1601
1602 set_user_nice(current, -20);
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001603 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 for (;;) {
1606 DECLARE_WAITQUEUE(wait, current);
1607
1608 /*
1609 * Wait until there is something to do
1610 */
1611 add_wait_queue(&pd->wqueue, &wait);
1612 for (;;) {
1613 set_current_state(TASK_INTERRUPTIBLE);
1614
1615 /* Check if we need to run pkt_handle_queue */
1616 if (atomic_read(&pd->scan_queue) > 0)
1617 goto work_to_do;
1618
1619 /* Check if we need to run the state machine for some packet */
1620 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1621 if (atomic_read(&pkt->run_sm) > 0)
1622 goto work_to_do;
1623 }
1624
1625 /* Check if we need to process the iosched queues */
1626 if (atomic_read(&pd->iosched.attention) != 0)
1627 goto work_to_do;
1628
1629 /* Otherwise, go to sleep */
1630 if (PACKET_DEBUG > 1) {
1631 int states[PACKET_NUM_STATES];
1632 pkt_count_states(pd, states);
1633 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1634 states[0], states[1], states[2], states[3],
1635 states[4], states[5]);
1636 }
1637
1638 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1639 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1640 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1641 min_sleep_time = pkt->sleep_time;
1642 }
1643
1644 generic_unplug_device(bdev_get_queue(pd->bdev));
1645
1646 VPRINTK("kcdrwd: sleeping\n");
1647 residue = schedule_timeout(min_sleep_time);
1648 VPRINTK("kcdrwd: wake up\n");
1649
1650 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001651 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1654 if (!pkt->sleep_time)
1655 continue;
1656 pkt->sleep_time -= min_sleep_time - residue;
1657 if (pkt->sleep_time <= 0) {
1658 pkt->sleep_time = 0;
1659 atomic_inc(&pkt->run_sm);
1660 }
1661 }
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (kthread_should_stop())
1664 break;
1665 }
1666work_to_do:
1667 set_current_state(TASK_RUNNING);
1668 remove_wait_queue(&pd->wqueue, &wait);
1669
1670 if (kthread_should_stop())
1671 break;
1672
1673 /*
1674 * if pkt_handle_queue returns true, we can queue
1675 * another request.
1676 */
1677 while (pkt_handle_queue(pd))
1678 ;
1679
1680 /*
1681 * Handle packet state machine
1682 */
1683 pkt_handle_packets(pd);
1684
1685 /*
1686 * Handle iosched queues
1687 */
1688 pkt_iosched_process_queue(pd);
1689 }
1690
1691 return 0;
1692}
1693
1694static void pkt_print_settings(struct pktcdvd_device *pd)
1695{
Thomas Maier78220822006-10-04 02:15:28 -07001696 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 printk("%u blocks, ", pd->settings.size >> 2);
1698 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1699}
1700
1701static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1702{
1703 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1704
1705 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1706 cgc->cmd[2] = page_code | (page_control << 6);
1707 cgc->cmd[7] = cgc->buflen >> 8;
1708 cgc->cmd[8] = cgc->buflen & 0xff;
1709 cgc->data_direction = CGC_DATA_READ;
1710 return pkt_generic_packet(pd, cgc);
1711}
1712
1713static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1714{
1715 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1716 memset(cgc->buffer, 0, 2);
1717 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1718 cgc->cmd[1] = 0x10; /* PF */
1719 cgc->cmd[7] = cgc->buflen >> 8;
1720 cgc->cmd[8] = cgc->buflen & 0xff;
1721 cgc->data_direction = CGC_DATA_WRITE;
1722 return pkt_generic_packet(pd, cgc);
1723}
1724
1725static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1726{
1727 struct packet_command cgc;
1728 int ret;
1729
1730 /* set up command and get the disc info */
1731 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1732 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1733 cgc.cmd[8] = cgc.buflen = 2;
1734 cgc.quiet = 1;
1735
1736 if ((ret = pkt_generic_packet(pd, &cgc)))
1737 return ret;
1738
1739 /* not all drives have the same disc_info length, so requeue
1740 * packet with the length the drive tells us it can supply
1741 */
1742 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1743 sizeof(di->disc_information_length);
1744
1745 if (cgc.buflen > sizeof(disc_information))
1746 cgc.buflen = sizeof(disc_information);
1747
1748 cgc.cmd[8] = cgc.buflen;
1749 return pkt_generic_packet(pd, &cgc);
1750}
1751
1752static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1753{
1754 struct packet_command cgc;
1755 int ret;
1756
1757 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1758 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1759 cgc.cmd[1] = type & 3;
1760 cgc.cmd[4] = (track & 0xff00) >> 8;
1761 cgc.cmd[5] = track & 0xff;
1762 cgc.cmd[8] = 8;
1763 cgc.quiet = 1;
1764
1765 if ((ret = pkt_generic_packet(pd, &cgc)))
1766 return ret;
1767
1768 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1769 sizeof(ti->track_information_length);
1770
1771 if (cgc.buflen > sizeof(track_information))
1772 cgc.buflen = sizeof(track_information);
1773
1774 cgc.cmd[8] = cgc.buflen;
1775 return pkt_generic_packet(pd, &cgc);
1776}
1777
Peter Osterlund05680d82008-03-04 14:28:41 -08001778static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1779 long *last_written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 disc_information di;
1782 track_information ti;
1783 __u32 last_track;
1784 int ret = -1;
1785
1786 if ((ret = pkt_get_disc_info(pd, &di)))
1787 return ret;
1788
1789 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1790 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1791 return ret;
1792
1793 /* if this track is blank, try the previous. */
1794 if (ti.blank) {
1795 last_track--;
1796 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1797 return ret;
1798 }
1799
1800 /* if last recorded field is valid, return it. */
1801 if (ti.lra_v) {
1802 *last_written = be32_to_cpu(ti.last_rec_address);
1803 } else {
1804 /* make it up instead */
1805 *last_written = be32_to_cpu(ti.track_start) +
1806 be32_to_cpu(ti.track_size);
1807 if (ti.free_blocks)
1808 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1809 }
1810 return 0;
1811}
1812
1813/*
1814 * write mode select package based on pd->settings
1815 */
Peter Osterlund05680d82008-03-04 14:28:41 -08001816static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct packet_command cgc;
1819 struct request_sense sense;
1820 write_param_page *wp;
1821 char buffer[128];
1822 int ret, size;
1823
1824 /* doesn't apply to DVD+RW or DVD-RAM */
1825 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1826 return 0;
1827
1828 memset(buffer, 0, sizeof(buffer));
1829 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1830 cgc.sense = &sense;
1831 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1832 pkt_dump_sense(&cgc);
1833 return ret;
1834 }
1835
1836 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1837 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1838 if (size > sizeof(buffer))
1839 size = sizeof(buffer);
1840
1841 /*
1842 * now get it all
1843 */
1844 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1845 cgc.sense = &sense;
1846 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1847 pkt_dump_sense(&cgc);
1848 return ret;
1849 }
1850
1851 /*
1852 * write page is offset header + block descriptor length
1853 */
1854 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1855
1856 wp->fp = pd->settings.fp;
1857 wp->track_mode = pd->settings.track_mode;
1858 wp->write_type = pd->settings.write_type;
1859 wp->data_block_type = pd->settings.block_mode;
1860
1861 wp->multi_session = 0;
1862
1863#ifdef PACKET_USE_LS
1864 wp->link_size = 7;
1865 wp->ls_v = 1;
1866#endif
1867
1868 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1869 wp->session_format = 0;
1870 wp->subhdr2 = 0x20;
1871 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1872 wp->session_format = 0x20;
1873 wp->subhdr2 = 8;
1874#if 0
1875 wp->mcn[0] = 0x80;
1876 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1877#endif
1878 } else {
1879 /*
1880 * paranoia
1881 */
Thomas Maier78220822006-10-04 02:15:28 -07001882 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 1;
1884 }
1885 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1886
1887 cgc.buflen = cgc.cmd[8] = size;
1888 if ((ret = pkt_mode_select(pd, &cgc))) {
1889 pkt_dump_sense(&cgc);
1890 return ret;
1891 }
1892
1893 pkt_print_settings(pd);
1894 return 0;
1895}
1896
1897/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001898 * 1 -- we can write to this track, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001900static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Peter Osterlundab863ec2006-02-20 18:28:04 -08001902 switch (pd->mmc3_profile) {
1903 case 0x1a: /* DVD+RW */
1904 case 0x12: /* DVD-RAM */
1905 /* The track is always writable on DVD+RW/DVD-RAM */
1906 return 1;
1907 default:
1908 break;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Peter Osterlundab863ec2006-02-20 18:28:04 -08001911 if (!ti->packet || !ti->fp)
1912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /*
1915 * "good" settings as per Mt Fuji.
1916 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001917 if (ti->rt == 0 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001918 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Peter Osterlundab863ec2006-02-20 18:28:04 -08001920 if (ti->rt == 0 && ti->blank == 1)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001921 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Peter Osterlundab863ec2006-02-20 18:28:04 -08001923 if (ti->rt == 1 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001924 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Thomas Maier78220822006-10-04 02:15:28 -07001926 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
1930/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001931 * 1 -- we can write to this disc, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001933static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 switch (pd->mmc3_profile) {
1936 case 0x0a: /* CD-RW */
1937 case 0xffff: /* MMC3 not supported */
1938 break;
1939 case 0x1a: /* DVD+RW */
1940 case 0x13: /* DVD-RW */
1941 case 0x12: /* DVD-RAM */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001942 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 default:
Thomas Maier78220822006-10-04 02:15:28 -07001944 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
1948 /*
1949 * for disc type 0xff we should probably reserve a new track.
1950 * but i'm not sure, should we leave this to user apps? probably.
1951 */
1952 if (di->disc_type == 0xff) {
Thomas Maier78220822006-10-04 02:15:28 -07001953 printk(DRIVER_NAME": Unknown disc. No track?\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
1957 if (di->disc_type != 0x20 && di->disc_type != 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001958 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
1962 if (di->erasable == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001963 printk(DRIVER_NAME": Disc not erasable\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
1967 if (di->border_status == PACKET_SESSION_RESERVED) {
Thomas Maier78220822006-10-04 02:15:28 -07001968 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971
Peter Osterlund7c613d52006-02-20 18:28:02 -08001972 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Peter Osterlund05680d82008-03-04 14:28:41 -08001975static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct packet_command cgc;
1978 unsigned char buf[12];
1979 disc_information di;
1980 track_information ti;
1981 int ret, track;
1982
1983 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1984 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1985 cgc.cmd[8] = 8;
1986 ret = pkt_generic_packet(pd, &cgc);
1987 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1988
1989 memset(&di, 0, sizeof(disc_information));
1990 memset(&ti, 0, sizeof(track_information));
1991
1992 if ((ret = pkt_get_disc_info(pd, &di))) {
1993 printk("failed get_disc\n");
1994 return ret;
1995 }
1996
Peter Osterlund7c613d52006-02-20 18:28:02 -08001997 if (!pkt_writable_disc(pd, &di))
Peter Osterlund9db91542006-02-20 18:28:04 -08001998 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
2001
2002 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
2003 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
Thomas Maier78220822006-10-04 02:15:28 -07002004 printk(DRIVER_NAME": failed get_track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return ret;
2006 }
2007
Peter Osterlundab863ec2006-02-20 18:28:04 -08002008 if (!pkt_writable_track(pd, &ti)) {
Thomas Maier78220822006-10-04 02:15:28 -07002009 printk(DRIVER_NAME": can't write to this track\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002010 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 /*
2014 * we keep packet size in 512 byte units, makes it easier to
2015 * deal with request calculations.
2016 */
2017 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2018 if (pd->settings.size == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07002019 printk(DRIVER_NAME": detected zero packet size!\n");
Phillip Susia460ad62006-02-04 23:27:44 -08002020 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07002022 if (pd->settings.size > PACKET_MAX_SECTORS) {
Thomas Maier78220822006-10-04 02:15:28 -07002023 printk(DRIVER_NAME": packet size is too big\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002024 return -EROFS;
Peter Osterlundd0272e72005-09-13 01:25:27 -07002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 pd->settings.fp = ti.fp;
2027 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2028
2029 if (ti.nwa_v) {
2030 pd->nwa = be32_to_cpu(ti.next_writable);
2031 set_bit(PACKET_NWA_VALID, &pd->flags);
2032 }
2033
2034 /*
2035 * in theory we could use lra on -RW media as well and just zero
2036 * blocks that haven't been written yet, but in practice that
2037 * is just a no-go. we'll use that for -R, naturally.
2038 */
2039 if (ti.lra_v) {
2040 pd->lra = be32_to_cpu(ti.last_rec_address);
2041 set_bit(PACKET_LRA_VALID, &pd->flags);
2042 } else {
2043 pd->lra = 0xffffffff;
2044 set_bit(PACKET_LRA_VALID, &pd->flags);
2045 }
2046
2047 /*
2048 * fine for now
2049 */
2050 pd->settings.link_loss = 7;
2051 pd->settings.write_type = 0; /* packet */
2052 pd->settings.track_mode = ti.track_mode;
2053
2054 /*
2055 * mode1 or mode2 disc
2056 */
2057 switch (ti.data_mode) {
2058 case PACKET_MODE1:
2059 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2060 break;
2061 case PACKET_MODE2:
2062 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2063 break;
2064 default:
Thomas Maier78220822006-10-04 02:15:28 -07002065 printk(DRIVER_NAME": unknown data mode\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002066 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 return 0;
2069}
2070
2071/*
2072 * enable/disable write caching on drive
2073 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002074static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2075 int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 struct packet_command cgc;
2078 struct request_sense sense;
2079 unsigned char buf[64];
2080 int ret;
2081
2082 memset(buf, 0, sizeof(buf));
2083 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2084 cgc.sense = &sense;
2085 cgc.buflen = pd->mode_offset + 12;
2086
2087 /*
2088 * caching mode page might not be there, so quiet this command
2089 */
2090 cgc.quiet = 1;
2091
2092 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2093 return ret;
2094
2095 buf[pd->mode_offset + 10] |= (!!set << 2);
2096
2097 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2098 ret = pkt_mode_select(pd, &cgc);
2099 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07002100 printk(DRIVER_NAME": write caching control failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 pkt_dump_sense(&cgc);
2102 } else if (!ret && set)
Thomas Maier78220822006-10-04 02:15:28 -07002103 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return ret;
2105}
2106
2107static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2108{
2109 struct packet_command cgc;
2110
2111 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2112 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2113 cgc.cmd[4] = lockflag ? 1 : 0;
2114 return pkt_generic_packet(pd, &cgc);
2115}
2116
2117/*
2118 * Returns drive maximum write speed
2119 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002120static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2121 unsigned *write_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct packet_command cgc;
2124 struct request_sense sense;
2125 unsigned char buf[256+18];
2126 unsigned char *cap_buf;
2127 int ret, offset;
2128
2129 memset(buf, 0, sizeof(buf));
2130 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2131 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2132 cgc.sense = &sense;
2133
2134 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2135 if (ret) {
2136 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2137 sizeof(struct mode_page_header);
2138 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2139 if (ret) {
2140 pkt_dump_sense(&cgc);
2141 return ret;
2142 }
2143 }
2144
2145 offset = 20; /* Obsoleted field, used by older drives */
2146 if (cap_buf[1] >= 28)
2147 offset = 28; /* Current write speed selected */
2148 if (cap_buf[1] >= 30) {
2149 /* If the drive reports at least one "Logical Unit Write
2150 * Speed Performance Descriptor Block", use the information
2151 * in the first block. (contains the highest speed)
2152 */
2153 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2154 if (num_spdb > 0)
2155 offset = 34;
2156 }
2157
2158 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2159 return 0;
2160}
2161
2162/* These tables from cdrecord - I don't have orange book */
2163/* standard speed CD-RW (1-4x) */
2164static char clv_to_speed[16] = {
2165 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2166 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2167};
2168/* high speed CD-RW (-10x) */
2169static char hs_clv_to_speed[16] = {
2170 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2171 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2172};
2173/* ultra high speed CD-RW */
2174static char us_clv_to_speed[16] = {
2175 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2176 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2177};
2178
2179/*
2180 * reads the maximum media speed from ATIP
2181 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002182static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2183 unsigned *speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 struct packet_command cgc;
2186 struct request_sense sense;
2187 unsigned char buf[64];
2188 unsigned int size, st, sp;
2189 int ret;
2190
2191 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2192 cgc.sense = &sense;
2193 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2194 cgc.cmd[1] = 2;
2195 cgc.cmd[2] = 4; /* READ ATIP */
2196 cgc.cmd[8] = 2;
2197 ret = pkt_generic_packet(pd, &cgc);
2198 if (ret) {
2199 pkt_dump_sense(&cgc);
2200 return ret;
2201 }
2202 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2203 if (size > sizeof(buf))
2204 size = sizeof(buf);
2205
2206 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2207 cgc.sense = &sense;
2208 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2209 cgc.cmd[1] = 2;
2210 cgc.cmd[2] = 4;
2211 cgc.cmd[8] = size;
2212 ret = pkt_generic_packet(pd, &cgc);
2213 if (ret) {
2214 pkt_dump_sense(&cgc);
2215 return ret;
2216 }
2217
Alexey Dobriyaneaa0ff12008-02-06 01:36:06 -08002218 if (!(buf[6] & 0x40)) {
Thomas Maier78220822006-10-04 02:15:28 -07002219 printk(DRIVER_NAME": Disc type is not CD-RW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return 1;
2221 }
Alexey Dobriyaneaa0ff12008-02-06 01:36:06 -08002222 if (!(buf[6] & 0x4)) {
Thomas Maier78220822006-10-04 02:15:28 -07002223 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return 1;
2225 }
2226
2227 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2228
2229 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2230
2231 /* Info from cdrecord */
2232 switch (st) {
2233 case 0: /* standard speed */
2234 *speed = clv_to_speed[sp];
2235 break;
2236 case 1: /* high speed */
2237 *speed = hs_clv_to_speed[sp];
2238 break;
2239 case 2: /* ultra high speed */
2240 *speed = us_clv_to_speed[sp];
2241 break;
2242 default:
Thomas Maier78220822006-10-04 02:15:28 -07002243 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 1;
2245 }
2246 if (*speed) {
Thomas Maier78220822006-10-04 02:15:28 -07002247 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
2249 } else {
Thomas Maier78220822006-10-04 02:15:28 -07002250 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 1;
2252 }
2253}
2254
Peter Osterlund05680d82008-03-04 14:28:41 -08002255static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 struct packet_command cgc;
2258 struct request_sense sense;
2259 int ret;
2260
Thomas Maier78220822006-10-04 02:15:28 -07002261 VPRINTK(DRIVER_NAME": Performing OPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2264 cgc.sense = &sense;
2265 cgc.timeout = 60*HZ;
2266 cgc.cmd[0] = GPCMD_SEND_OPC;
2267 cgc.cmd[1] = 1;
2268 if ((ret = pkt_generic_packet(pd, &cgc)))
2269 pkt_dump_sense(&cgc);
2270 return ret;
2271}
2272
2273static int pkt_open_write(struct pktcdvd_device *pd)
2274{
2275 int ret;
2276 unsigned int write_speed, media_write_speed, read_speed;
2277
2278 if ((ret = pkt_probe_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002279 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
Peter Osterlund9db91542006-02-20 18:28:04 -08002280 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282
2283 if ((ret = pkt_set_write_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002284 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return -EIO;
2286 }
2287
2288 pkt_write_caching(pd, USE_WCACHING);
2289
2290 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2291 write_speed = 16 * 177;
2292 switch (pd->mmc3_profile) {
2293 case 0x13: /* DVD-RW */
2294 case 0x1a: /* DVD+RW */
2295 case 0x12: /* DVD-RAM */
Thomas Maier78220822006-10-04 02:15:28 -07002296 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 break;
2298 default:
2299 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2300 media_write_speed = 16;
2301 write_speed = min(write_speed, media_write_speed * 177);
Thomas Maier78220822006-10-04 02:15:28 -07002302 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 break;
2304 }
2305 read_speed = write_speed;
2306
2307 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
Thomas Maier78220822006-10-04 02:15:28 -07002308 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return -EIO;
2310 }
2311 pd->write_speed = write_speed;
2312 pd->read_speed = read_speed;
2313
2314 if ((ret = pkt_perform_opc(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002315 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317
2318 return 0;
2319}
2320
2321/*
2322 * called at open time.
2323 */
2324static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2325{
2326 int ret;
2327 long lba;
Jens Axboe165125e2007-07-24 09:28:11 +02002328 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 /*
2331 * We need to re-open the cdrom device without O_NONBLOCK to be able
2332 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2333 * so bdget() can't fail.
2334 */
2335 bdget(pd->bdev->bd_dev);
2336 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2337 goto out;
2338
Peter Osterlund8382bf22006-01-08 01:02:17 -08002339 if ((ret = bd_claim(pd->bdev, pd)))
2340 goto out_putdev;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if ((ret = pkt_get_last_written(pd, &lba))) {
Thomas Maier78220822006-10-04 02:15:28 -07002343 printk(DRIVER_NAME": pkt_get_last_written failed\n");
Peter Osterlund8382bf22006-01-08 01:02:17 -08002344 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346
2347 set_capacity(pd->disk, lba << 2);
2348 set_capacity(pd->bdev->bd_disk, lba << 2);
2349 bd_set_size(pd->bdev, (loff_t)lba << 11);
2350
2351 q = bdev_get_queue(pd->bdev);
2352 if (write) {
2353 if ((ret = pkt_open_write(pd)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002354 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /*
2356 * Some CDRW drives can not handle writes larger than one packet,
2357 * even if the size is a multiple of the packet size.
2358 */
2359 spin_lock_irq(q->queue_lock);
2360 blk_queue_max_sectors(q, pd->settings.size);
2361 spin_unlock_irq(q->queue_lock);
2362 set_bit(PACKET_WRITABLE, &pd->flags);
2363 } else {
2364 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2365 clear_bit(PACKET_WRITABLE, &pd->flags);
2366 }
2367
2368 if ((ret = pkt_set_segment_merging(pd, q)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002369 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002371 if (write) {
2372 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
Thomas Maier78220822006-10-04 02:15:28 -07002373 printk(DRIVER_NAME": not enough memory for buffers\n");
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002374 ret = -ENOMEM;
2375 goto out_unclaim;
2376 }
Thomas Maier78220822006-10-04 02:15:28 -07002377 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 return 0;
2381
Peter Osterlund8382bf22006-01-08 01:02:17 -08002382out_unclaim:
2383 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384out_putdev:
2385 blkdev_put(pd->bdev);
2386out:
2387 return ret;
2388}
2389
2390/*
2391 * called when the device is closed. makes sure that the device flushes
2392 * the internal cache before we close.
2393 */
2394static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2395{
2396 if (flush && pkt_flush_cache(pd))
Thomas Maier78220822006-10-04 02:15:28 -07002397 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 pkt_lock_door(pd, 0);
2400
2401 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
Peter Osterlund8382bf22006-01-08 01:02:17 -08002402 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 blkdev_put(pd->bdev);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002404
2405 pkt_shrink_pktlist(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2409{
2410 if (dev_minor >= MAX_WRITERS)
2411 return NULL;
2412 return pkt_devs[dev_minor];
2413}
2414
2415static int pkt_open(struct inode *inode, struct file *file)
2416{
2417 struct pktcdvd_device *pd = NULL;
2418 int ret;
2419
Thomas Maier78220822006-10-04 02:15:28 -07002420 VPRINTK(DRIVER_NAME": entering open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Jes Sorensen1657f822006-03-23 03:00:25 -08002422 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 pd = pkt_find_dev_from_minor(iminor(inode));
2424 if (!pd) {
2425 ret = -ENODEV;
2426 goto out;
2427 }
2428 BUG_ON(pd->refcnt < 0);
2429
2430 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002431 if (pd->refcnt > 1) {
2432 if ((file->f_mode & FMODE_WRITE) &&
2433 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2434 ret = -EBUSY;
2435 goto out_dec;
2436 }
2437 } else {
Peter Osterlund01fd9fd2006-02-14 13:52:55 -08002438 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2439 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 /*
2442 * needed here as well, since ext2 (among others) may change
2443 * the blocksize at mount time
2444 */
2445 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2446 }
2447
Jes Sorensen1657f822006-03-23 03:00:25 -08002448 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return 0;
2450
2451out_dec:
2452 pd->refcnt--;
2453out:
Thomas Maier78220822006-10-04 02:15:28 -07002454 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
Jes Sorensen1657f822006-03-23 03:00:25 -08002455 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return ret;
2457}
2458
2459static int pkt_close(struct inode *inode, struct file *file)
2460{
2461 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2462 int ret = 0;
2463
Jes Sorensen1657f822006-03-23 03:00:25 -08002464 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 pd->refcnt--;
2466 BUG_ON(pd->refcnt < 0);
2467 if (pd->refcnt == 0) {
2468 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2469 pkt_release_dev(pd, flush);
2470 }
Jes Sorensen1657f822006-03-23 03:00:25 -08002471 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return ret;
2473}
2474
2475
NeilBrown6712ecf2007-09-27 12:47:43 +02002476static void pkt_end_io_read_cloned(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 struct packet_stacked_data *psd = bio->bi_private;
2479 struct pktcdvd_device *pd = psd->pd;
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +02002482 bio_endio(psd->bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 mempool_free(psd, psd_pool);
2484 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Jens Axboe165125e2007-07-24 09:28:11 +02002487static int pkt_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 struct pktcdvd_device *pd;
2490 char b[BDEVNAME_SIZE];
2491 sector_t zone;
2492 struct packet_data *pkt;
2493 int was_empty, blocked_bio;
2494 struct pkt_rb_node *node;
2495
2496 pd = q->queuedata;
2497 if (!pd) {
Thomas Maier78220822006-10-04 02:15:28 -07002498 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto end_io;
2500 }
2501
2502 /*
2503 * Clone READ bios so we can have our own bi_end_io callback.
2504 */
2505 if (bio_data_dir(bio) == READ) {
2506 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2507 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2508
2509 psd->pd = pd;
2510 psd->bio = bio;
2511 cloned_bio->bi_bdev = pd->bdev;
2512 cloned_bio->bi_private = psd;
2513 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2514 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002515 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
2517 }
2518
2519 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
Thomas Maier78220822006-10-04 02:15:28 -07002520 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 pd->name, (unsigned long long)bio->bi_sector);
2522 goto end_io;
2523 }
2524
2525 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
Thomas Maier78220822006-10-04 02:15:28 -07002526 printk(DRIVER_NAME": wrong bio size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 goto end_io;
2528 }
2529
2530 blk_queue_bounce(q, &bio);
2531
2532 zone = ZONE(bio->bi_sector, pd);
2533 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2534 (unsigned long long)bio->bi_sector,
2535 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2536
2537 /* Check if we have to split the bio */
2538 {
2539 struct bio_pair *bp;
2540 sector_t last_zone;
2541 int first_sectors;
2542
2543 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2544 if (last_zone != zone) {
2545 BUG_ON(last_zone != zone + pd->settings.size);
2546 first_sectors = last_zone - bio->bi_sector;
2547 bp = bio_split(bio, bio_split_pool, first_sectors);
2548 BUG_ON(!bp);
2549 pkt_make_request(q, &bp->bio1);
2550 pkt_make_request(q, &bp->bio2);
2551 bio_pair_release(bp);
2552 return 0;
2553 }
2554 }
2555
2556 /*
2557 * If we find a matching packet in state WAITING or READ_WAIT, we can
2558 * just append this bio to that packet.
2559 */
2560 spin_lock(&pd->cdrw.active_list_lock);
2561 blocked_bio = 0;
2562 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2563 if (pkt->sector == zone) {
2564 spin_lock(&pkt->lock);
2565 if ((pkt->state == PACKET_WAITING_STATE) ||
2566 (pkt->state == PACKET_READ_WAIT_STATE)) {
2567 pkt_add_list_last(bio, &pkt->orig_bios,
2568 &pkt->orig_bios_tail);
2569 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2570 if ((pkt->write_size >= pkt->frames) &&
2571 (pkt->state == PACKET_WAITING_STATE)) {
2572 atomic_inc(&pkt->run_sm);
2573 wake_up(&pd->wqueue);
2574 }
2575 spin_unlock(&pkt->lock);
2576 spin_unlock(&pd->cdrw.active_list_lock);
2577 return 0;
2578 } else {
2579 blocked_bio = 1;
2580 }
2581 spin_unlock(&pkt->lock);
2582 }
2583 }
2584 spin_unlock(&pd->cdrw.active_list_lock);
2585
Thomas Maier0a0fc962006-12-08 02:36:11 -08002586 /*
2587 * Test if there is enough room left in the bio work queue
2588 * (queue size >= congestion on mark).
2589 * If not, wait till the work queue size is below the congestion off mark.
2590 */
2591 spin_lock(&pd->lock);
2592 if (pd->write_congestion_on > 0
2593 && pd->bio_queue_size >= pd->write_congestion_on) {
Thomas Maier83f3aa32007-02-10 01:45:11 -08002594 set_bdi_congested(&q->backing_dev_info, WRITE);
Thomas Maier0a0fc962006-12-08 02:36:11 -08002595 do {
2596 spin_unlock(&pd->lock);
2597 congestion_wait(WRITE, HZ);
2598 spin_lock(&pd->lock);
2599 } while(pd->bio_queue_size > pd->write_congestion_off);
2600 }
2601 spin_unlock(&pd->lock);
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /*
2604 * No matching packet found. Store the bio in the work queue.
2605 */
2606 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 node->bio = bio;
2608 spin_lock(&pd->lock);
2609 BUG_ON(pd->bio_queue_size < 0);
2610 was_empty = (pd->bio_queue_size == 0);
2611 pkt_rbtree_insert(pd, node);
2612 spin_unlock(&pd->lock);
2613
2614 /*
2615 * Wake up the worker thread.
2616 */
2617 atomic_set(&pd->scan_queue, 1);
2618 if (was_empty) {
2619 /* This wake_up is required for correct operation */
2620 wake_up(&pd->wqueue);
2621 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2622 /*
2623 * This wake up is not required for correct operation,
2624 * but improves performance in some cases.
2625 */
2626 wake_up(&pd->wqueue);
2627 }
2628 return 0;
2629end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02002630 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return 0;
2632}
2633
2634
2635
Jens Axboe165125e2007-07-24 09:28:11 +02002636static int pkt_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 struct pktcdvd_device *pd = q->queuedata;
2639 sector_t zone = ZONE(bio->bi_sector, pd);
2640 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2641 int remaining = (pd->settings.size << 9) - used;
2642 int remaining2;
2643
2644 /*
2645 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2646 * boundary, pkt_make_request() will split the bio.
2647 */
2648 remaining2 = PAGE_SIZE - bio->bi_size;
2649 remaining = max(remaining, remaining2);
2650
2651 BUG_ON(remaining < 0);
2652 return remaining;
2653}
2654
2655static void pkt_init_queue(struct pktcdvd_device *pd)
2656{
Jens Axboe165125e2007-07-24 09:28:11 +02002657 struct request_queue *q = pd->disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 blk_queue_make_request(q, pkt_make_request);
2660 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2661 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2662 blk_queue_merge_bvec(q, pkt_merge_bvec);
2663 q->queuedata = pd;
2664}
2665
2666static int pkt_seq_show(struct seq_file *m, void *p)
2667{
2668 struct pktcdvd_device *pd = m->private;
2669 char *msg;
2670 char bdev_buf[BDEVNAME_SIZE];
2671 int states[PACKET_NUM_STATES];
2672
2673 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2674 bdevname(pd->bdev, bdev_buf));
2675
2676 seq_printf(m, "\nSettings:\n");
2677 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2678
2679 if (pd->settings.write_type == 0)
2680 msg = "Packet";
2681 else
2682 msg = "Unknown";
2683 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2684
2685 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2686 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2687
2688 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2689
2690 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2691 msg = "Mode 1";
2692 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2693 msg = "Mode 2";
2694 else
2695 msg = "Unknown";
2696 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2697
2698 seq_printf(m, "\nStatistics:\n");
2699 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2700 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2701 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2702 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2703 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2704
2705 seq_printf(m, "\nMisc:\n");
2706 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2707 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2708 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2709 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2710 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2711 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2712
2713 seq_printf(m, "\nQueue state:\n");
2714 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2715 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2716 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2717
2718 pkt_count_states(pd, states);
2719 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2720 states[0], states[1], states[2], states[3], states[4], states[5]);
2721
Thomas Maier0a0fc962006-12-08 02:36:11 -08002722 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2723 pd->write_congestion_off,
2724 pd->write_congestion_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return 0;
2726}
2727
2728static int pkt_seq_open(struct inode *inode, struct file *file)
2729{
2730 return single_open(file, pkt_seq_show, PDE(inode)->data);
2731}
2732
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002733static const struct file_operations pkt_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .open = pkt_seq_open,
2735 .read = seq_read,
2736 .llseek = seq_lseek,
2737 .release = single_release
2738};
2739
2740static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2741{
2742 int i;
2743 int ret = 0;
2744 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 struct block_device *bdev;
2746
2747 if (pd->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002748 printk(DRIVER_NAME": Recursive setup not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return -EBUSY;
2750 }
2751 for (i = 0; i < MAX_WRITERS; i++) {
2752 struct pktcdvd_device *pd2 = pkt_devs[i];
2753 if (!pd2)
2754 continue;
2755 if (pd2->bdev->bd_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002756 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return -EBUSY;
2758 }
2759 if (pd2->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002760 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return -EBUSY;
2762 }
2763 }
2764
2765 bdev = bdget(dev);
2766 if (!bdev)
2767 return -ENOMEM;
2768 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2769 if (ret)
2770 return ret;
2771
2772 /* This is safe, since we have a reference from open(). */
2773 __module_get(THIS_MODULE);
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 pd->bdev = bdev;
2776 set_blocksize(bdev, CD_FRAMESIZE);
2777
2778 pkt_init_queue(pd);
2779
2780 atomic_set(&pd->cdrw.pending_bios, 0);
2781 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2782 if (IS_ERR(pd->cdrw.thread)) {
Thomas Maier78220822006-10-04 02:15:28 -07002783 printk(DRIVER_NAME": can't start kernel thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 ret = -ENOMEM;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002785 goto out_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
2787
Denis V. Lunevc7705f32008-04-29 01:02:35 -07002788 proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
Thomas Maier78220822006-10-04 02:15:28 -07002789 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return 0;
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792out_mem:
2793 blkdev_put(bdev);
2794 /* This is safe: open() is still holding a reference. */
2795 module_put(THIS_MODULE);
2796 return ret;
2797}
2798
2799static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2800{
2801 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2802
2803 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 switch (cmd) {
2806 /*
2807 * forward selected CDROM ioctls to CD-ROM, for UDF
2808 */
2809 case CDROMMULTISESSION:
2810 case CDROMREADTOCENTRY:
2811 case CDROM_LAST_WRITTEN:
2812 case CDROM_SEND_PACKET:
2813 case SCSI_IOCTL_SEND_COMMAND:
Peter Osterlund118326e2005-05-14 00:58:30 -07002814 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 case CDROMEJECT:
2817 /*
2818 * The door gets locked when the device is opened, so we
2819 * have to unlock it or else the eject command fails.
2820 */
Peter Osterlund948423e2006-02-14 13:52:56 -08002821 if (pd->refcnt == 1)
2822 pkt_lock_door(pd, 0);
Peter Osterlund118326e2005-05-14 00:58:30 -07002823 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 default:
Thomas Maier78220822006-10-04 02:15:28 -07002826 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return -ENOTTY;
2828 }
2829
2830 return 0;
2831}
2832
2833static int pkt_media_changed(struct gendisk *disk)
2834{
2835 struct pktcdvd_device *pd = disk->private_data;
2836 struct gendisk *attached_disk;
2837
2838 if (!pd)
2839 return 0;
2840 if (!pd->bdev)
2841 return 0;
2842 attached_disk = pd->bdev->bd_disk;
2843 if (!attached_disk)
2844 return 0;
2845 return attached_disk->fops->media_changed(attached_disk);
2846}
2847
2848static struct block_device_operations pktcdvd_ops = {
2849 .owner = THIS_MODULE,
2850 .open = pkt_open,
2851 .release = pkt_close,
2852 .ioctl = pkt_ioctl,
2853 .media_changed = pkt_media_changed,
2854};
2855
2856/*
2857 * Set up mapping from pktcdvd device to CD-ROM device.
2858 */
Thomas Maieradb92502006-12-08 02:36:10 -08002859static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 int idx;
2862 int ret = -ENOMEM;
2863 struct pktcdvd_device *pd;
2864 struct gendisk *disk;
Thomas Maieradb92502006-12-08 02:36:10 -08002865
2866 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 for (idx = 0; idx < MAX_WRITERS; idx++)
2869 if (!pkt_devs[idx])
2870 break;
2871 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002872 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
Thomas Maieradb92502006-12-08 02:36:10 -08002873 ret = -EBUSY;
2874 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876
Peter Osterlund1107d2e2005-09-13 01:25:29 -07002877 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (!pd)
Thomas Maieradb92502006-12-08 02:36:10 -08002879 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002881 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2882 sizeof(struct pkt_rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!pd->rb_pool)
2884 goto out_mem;
2885
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002886 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2887 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2888 spin_lock_init(&pd->cdrw.active_list_lock);
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 spin_lock_init(&pd->lock);
2891 spin_lock_init(&pd->iosched.lock);
Thomas Maier78220822006-10-04 02:15:28 -07002892 sprintf(pd->name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 init_waitqueue_head(&pd->wqueue);
2894 pd->bio_queue = RB_ROOT;
2895
Thomas Maier0a0fc962006-12-08 02:36:11 -08002896 pd->write_congestion_on = write_congestion_on;
2897 pd->write_congestion_off = write_congestion_off;
2898
Thomas Maieradb92502006-12-08 02:36:10 -08002899 disk = alloc_disk(1);
2900 if (!disk)
2901 goto out_mem;
2902 pd->disk = disk;
Thomas Maieradd21662006-10-04 02:15:30 -07002903 disk->major = pktdev_major;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 disk->first_minor = idx;
2905 disk->fops = &pktcdvd_ops;
2906 disk->flags = GENHD_FL_REMOVABLE;
Thomas Maieradb92502006-12-08 02:36:10 -08002907 strcpy(disk->disk_name, pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 disk->private_data = pd;
2909 disk->queue = blk_alloc_queue(GFP_KERNEL);
2910 if (!disk->queue)
2911 goto out_mem2;
2912
2913 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2914 ret = pkt_new_dev(pd, dev);
2915 if (ret)
2916 goto out_new_dev;
2917
2918 add_disk(disk);
Thomas Maieradb92502006-12-08 02:36:10 -08002919
Thomas Maier32694852006-12-08 02:36:12 -08002920 pkt_sysfs_dev_new(pd);
2921 pkt_debugfs_dev_new(pd);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 pkt_devs[idx] = pd;
Thomas Maieradb92502006-12-08 02:36:10 -08002924 if (pkt_dev)
2925 *pkt_dev = pd->pkt_dev;
2926
2927 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return 0;
2929
2930out_new_dev:
Al Viro1312f402006-03-12 11:02:03 -05002931 blk_cleanup_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932out_mem2:
2933 put_disk(disk);
2934out_mem:
2935 if (pd->rb_pool)
2936 mempool_destroy(pd->rb_pool);
2937 kfree(pd);
Thomas Maieradb92502006-12-08 02:36:10 -08002938out_mutex:
2939 mutex_unlock(&ctl_mutex);
2940 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return ret;
2942}
2943
2944/*
2945 * Tear down mapping from pktcdvd device to CD-ROM device.
2946 */
Thomas Maieradb92502006-12-08 02:36:10 -08002947static int pkt_remove_dev(dev_t pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 struct pktcdvd_device *pd;
2950 int idx;
Thomas Maieradb92502006-12-08 02:36:10 -08002951 int ret = 0;
2952
2953 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 for (idx = 0; idx < MAX_WRITERS; idx++) {
2956 pd = pkt_devs[idx];
2957 if (pd && (pd->pkt_dev == pkt_dev))
2958 break;
2959 }
2960 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002961 DPRINTK(DRIVER_NAME": dev not setup\n");
Thomas Maieradb92502006-12-08 02:36:10 -08002962 ret = -ENXIO;
2963 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
Thomas Maieradb92502006-12-08 02:36:10 -08002966 if (pd->refcnt > 0) {
2967 ret = -EBUSY;
2968 goto out;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (!IS_ERR(pd->cdrw.thread))
2971 kthread_stop(pd->cdrw.thread);
2972
Thomas Maier32694852006-12-08 02:36:12 -08002973 pkt_devs[idx] = NULL;
2974
2975 pkt_debugfs_dev_remove(pd);
2976 pkt_sysfs_dev_remove(pd);
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 blkdev_put(pd->bdev);
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 remove_proc_entry(pd->name, pkt_proc);
Thomas Maier78220822006-10-04 02:15:28 -07002981 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 del_gendisk(pd->disk);
Al Viro1312f402006-03-12 11:02:03 -05002984 blk_cleanup_queue(pd->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 put_disk(pd->disk);
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 mempool_destroy(pd->rb_pool);
2988 kfree(pd);
2989
2990 /* This is safe: open() is still holding a reference. */
2991 module_put(THIS_MODULE);
Thomas Maieradb92502006-12-08 02:36:10 -08002992
2993out:
2994 mutex_unlock(&ctl_mutex);
2995 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
2998static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2999{
Thomas Maieradb92502006-12-08 02:36:10 -08003000 struct pktcdvd_device *pd;
3001
3002 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3003
3004 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (pd) {
3006 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
3007 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
3008 } else {
3009 ctrl_cmd->dev = 0;
3010 ctrl_cmd->pkt_dev = 0;
3011 }
3012 ctrl_cmd->num_devices = MAX_WRITERS;
Thomas Maieradb92502006-12-08 02:36:10 -08003013
3014 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3018{
3019 void __user *argp = (void __user *)arg;
3020 struct pkt_ctrl_command ctrl_cmd;
3021 int ret = 0;
Thomas Maieradb92502006-12-08 02:36:10 -08003022 dev_t pkt_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 if (cmd != PACKET_CTRL_CMD)
3025 return -ENOTTY;
3026
3027 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3028 return -EFAULT;
3029
3030 switch (ctrl_cmd.command) {
3031 case PKT_CTRL_CMD_SETUP:
3032 if (!capable(CAP_SYS_ADMIN))
3033 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003034 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3035 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 break;
3037 case PKT_CTRL_CMD_TEARDOWN:
3038 if (!capable(CAP_SYS_ADMIN))
3039 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003040 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 break;
3042 case PKT_CTRL_CMD_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 pkt_get_status(&ctrl_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 break;
3045 default:
3046 return -ENOTTY;
3047 }
3048
3049 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3050 return -EFAULT;
3051 return ret;
3052}
3053
3054
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003055static const struct file_operations pkt_ctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .ioctl = pkt_ctl_ioctl,
3057 .owner = THIS_MODULE,
3058};
3059
3060static struct miscdevice pkt_misc = {
3061 .minor = MISC_DYNAMIC_MINOR,
Thomas Maier78220822006-10-04 02:15:28 -07003062 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 .fops = &pkt_ctl_fops
3064};
3065
3066static int __init pkt_init(void)
3067{
3068 int ret;
3069
Thomas Maier32694852006-12-08 02:36:12 -08003070 mutex_init(&ctl_mutex);
3071
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08003072 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3073 sizeof(struct packet_stacked_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (!psd_pool)
3075 return -ENOMEM;
3076
Thomas Maieradd21662006-10-04 02:15:30 -07003077 ret = register_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (ret < 0) {
Thomas Maier78220822006-10-04 02:15:28 -07003079 printk(DRIVER_NAME": Unable to register block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 goto out2;
3081 }
Thomas Maieradd21662006-10-04 02:15:30 -07003082 if (!pktdev_major)
3083 pktdev_major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Thomas Maier32694852006-12-08 02:36:12 -08003085 ret = pkt_sysfs_init();
3086 if (ret)
3087 goto out;
3088
3089 pkt_debugfs_init();
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 ret = misc_register(&pkt_misc);
3092 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07003093 printk(DRIVER_NAME": Unable to register misc device\n");
Thomas Maier32694852006-12-08 02:36:12 -08003094 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
Alexey Dobriyan928b4d82008-04-29 01:01:44 -07003097 pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return 0;
3100
Thomas Maier32694852006-12-08 02:36:12 -08003101out_misc:
3102 pkt_debugfs_cleanup();
3103 pkt_sysfs_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104out:
Thomas Maieradd21662006-10-04 02:15:30 -07003105 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106out2:
3107 mempool_destroy(psd_pool);
3108 return ret;
3109}
3110
3111static void __exit pkt_exit(void)
3112{
Alexey Dobriyan928b4d82008-04-29 01:01:44 -07003113 remove_proc_entry("driver/"DRIVER_NAME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 misc_deregister(&pkt_misc);
Thomas Maier32694852006-12-08 02:36:12 -08003115
3116 pkt_debugfs_cleanup();
3117 pkt_sysfs_cleanup();
3118
Thomas Maieradd21662006-10-04 02:15:30 -07003119 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 mempool_destroy(psd_pool);
3121}
3122
3123MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3124MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3125MODULE_LICENSE("GPL");
3126
3127module_init(pkt_init);
3128module_exit(pkt_exit);