blob: 158eed4d516188deb82b50b9f60ddbbd502b8302 [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>
Alan Cox5b6155e2008-07-04 09:29:16 +020052#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/errno.h>
54#include <linux/spinlock.h>
55#include <linux/file.h>
56#include <linux/proc_fs.h>
57#include <linux/seq_file.h>
58#include <linux/miscdevice.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080059#include <linux/freezer.h>
Jes Sorensen1657f822006-03-23 03:00:25 -080060#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <scsi/scsi_cmnd.h>
62#include <scsi/scsi_ioctl.h>
Peter Osterlundcef289632006-02-20 18:28:01 -080063#include <scsi/scsi.h>
Thomas Maier32694852006-12-08 02:36:12 -080064#include <linux/debugfs.h>
65#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <asm/uaccess.h>
68
Thomas Maier78220822006-10-04 02:15:28 -070069#define DRIVER_NAME "pktcdvd"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#if PACKET_DEBUG
72#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
73#else
74#define DPRINTK(fmt, args...)
75#endif
76
77#if PACKET_DEBUG > 1
78#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
79#else
80#define VPRINTK(fmt, args...)
81#endif
82
83#define MAX_SPEED 0xffff
84
85#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
86
87static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
88static struct proc_dir_entry *pkt_proc;
Thomas Maieradd21662006-10-04 02:15:30 -070089static int pktdev_major;
Thomas Maier0a0fc962006-12-08 02:36:11 -080090static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
91static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
Jes Sorensen1657f822006-03-23 03:00:25 -080092static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static mempool_t *psd_pool;
94
Thomas Maier32694852006-12-08 02:36:12 -080095static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
96static struct dentry *pkt_debugfs_root = NULL; /* /debug/pktcdvd */
97
98/* forward declaration */
99static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
100static int pkt_remove_dev(dev_t pkt_dev);
101static int pkt_seq_show(struct seq_file *m, void *p);
102
103
104
105/*
106 * create and register a pktcdvd kernel object.
107 */
108static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
109 const char* name,
110 struct kobject* parent,
111 struct kobj_type* ktype)
112{
113 struct pktcdvd_kobj *p;
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400114 int error;
115
Thomas Maier32694852006-12-08 02:36:12 -0800116 p = kzalloc(sizeof(*p), GFP_KERNEL);
117 if (!p)
118 return NULL;
Thomas Maier32694852006-12-08 02:36:12 -0800119 p->pd = pd;
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400120 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
121 if (error) {
Dave Youngd17a18d2007-12-17 16:20:00 -0800122 kobject_put(&p->kobj);
Thomas Maier32694852006-12-08 02:36:12 -0800123 return NULL;
Dave Youngd17a18d2007-12-17 16:20:00 -0800124 }
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400125 kobject_uevent(&p->kobj, KOBJ_ADD);
Thomas Maier32694852006-12-08 02:36:12 -0800126 return p;
127}
128/*
129 * remove a pktcdvd kernel object.
130 */
131static void pkt_kobj_remove(struct pktcdvd_kobj *p)
132{
133 if (p)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800134 kobject_put(&p->kobj);
Thomas Maier32694852006-12-08 02:36:12 -0800135}
136/*
137 * default release function for pktcdvd kernel objects.
138 */
139static void pkt_kobj_release(struct kobject *kobj)
140{
141 kfree(to_pktcdvdkobj(kobj));
142}
143
144
145/**********************************************************
146 *
147 * sysfs interface for pktcdvd
148 * by (C) 2006 Thomas Maier <balagi@justmail.de>
149 *
150 **********************************************************/
151
152#define DEF_ATTR(_obj,_name,_mode) \
Tejun Heo7b595752007-06-14 03:45:17 +0900153 static struct attribute _obj = { .name = _name, .mode = _mode }
Thomas Maier32694852006-12-08 02:36:12 -0800154
155/**********************************************************
156 /sys/class/pktcdvd/pktcdvd[0-7]/
157 stat/reset
158 stat/packets_started
159 stat/packets_finished
160 stat/kb_written
161 stat/kb_read
162 stat/kb_read_gather
163 write_queue/size
164 write_queue/congestion_off
165 write_queue/congestion_on
166 **********************************************************/
167
168DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
169DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
170DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
171DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
172DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
173DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
174
175static struct attribute *kobj_pkt_attrs_stat[] = {
176 &kobj_pkt_attr_st1,
177 &kobj_pkt_attr_st2,
178 &kobj_pkt_attr_st3,
179 &kobj_pkt_attr_st4,
180 &kobj_pkt_attr_st5,
181 &kobj_pkt_attr_st6,
182 NULL
183};
184
185DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
186DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
187DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
188
189static struct attribute *kobj_pkt_attrs_wqueue[] = {
190 &kobj_pkt_attr_wq1,
191 &kobj_pkt_attr_wq2,
192 &kobj_pkt_attr_wq3,
193 NULL
194};
195
Thomas Maier32694852006-12-08 02:36:12 -0800196static ssize_t kobj_pkt_show(struct kobject *kobj,
197 struct attribute *attr, char *data)
198{
199 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
200 int n = 0;
201 int v;
202 if (strcmp(attr->name, "packets_started") == 0) {
203 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
204
205 } else if (strcmp(attr->name, "packets_finished") == 0) {
206 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
207
208 } else if (strcmp(attr->name, "kb_written") == 0) {
209 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
210
211 } else if (strcmp(attr->name, "kb_read") == 0) {
212 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
213
214 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
215 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
216
217 } else if (strcmp(attr->name, "size") == 0) {
218 spin_lock(&pd->lock);
219 v = pd->bio_queue_size;
220 spin_unlock(&pd->lock);
221 n = sprintf(data, "%d\n", v);
222
223 } else if (strcmp(attr->name, "congestion_off") == 0) {
224 spin_lock(&pd->lock);
225 v = pd->write_congestion_off;
226 spin_unlock(&pd->lock);
227 n = sprintf(data, "%d\n", v);
228
229 } else if (strcmp(attr->name, "congestion_on") == 0) {
230 spin_lock(&pd->lock);
231 v = pd->write_congestion_on;
232 spin_unlock(&pd->lock);
233 n = sprintf(data, "%d\n", v);
234 }
235 return n;
236}
237
238static void init_write_congestion_marks(int* lo, int* hi)
239{
240 if (*hi > 0) {
241 *hi = max(*hi, 500);
242 *hi = min(*hi, 1000000);
243 if (*lo <= 0)
244 *lo = *hi - 100;
245 else {
246 *lo = min(*lo, *hi - 100);
247 *lo = max(*lo, 100);
248 }
249 } else {
250 *hi = -1;
251 *lo = -1;
252 }
253}
254
255static ssize_t kobj_pkt_store(struct kobject *kobj,
256 struct attribute *attr,
257 const char *data, size_t len)
258{
259 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
260 int val;
Thomas Maier32694852006-12-08 02:36:12 -0800261
Thomas Maier83f3aa32007-02-10 01:45:11 -0800262 if (strcmp(attr->name, "reset") == 0 && len > 0) {
Thomas Maier32694852006-12-08 02:36:12 -0800263 pd->stats.pkt_started = 0;
264 pd->stats.pkt_ended = 0;
265 pd->stats.secs_w = 0;
266 pd->stats.secs_rg = 0;
267 pd->stats.secs_r = 0;
268
269 } else if (strcmp(attr->name, "congestion_off") == 0
Thomas Maier83f3aa32007-02-10 01:45:11 -0800270 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800271 spin_lock(&pd->lock);
272 pd->write_congestion_off = val;
273 init_write_congestion_marks(&pd->write_congestion_off,
274 &pd->write_congestion_on);
275 spin_unlock(&pd->lock);
276
277 } else if (strcmp(attr->name, "congestion_on") == 0
Thomas Maier83f3aa32007-02-10 01:45:11 -0800278 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800279 spin_lock(&pd->lock);
280 pd->write_congestion_on = val;
281 init_write_congestion_marks(&pd->write_congestion_off,
282 &pd->write_congestion_on);
283 spin_unlock(&pd->lock);
284 }
285 return len;
286}
287
288static struct sysfs_ops kobj_pkt_ops = {
289 .show = kobj_pkt_show,
290 .store = kobj_pkt_store
291};
292static struct kobj_type kobj_pkt_type_stat = {
293 .release = pkt_kobj_release,
294 .sysfs_ops = &kobj_pkt_ops,
295 .default_attrs = kobj_pkt_attrs_stat
296};
297static struct kobj_type kobj_pkt_type_wqueue = {
298 .release = pkt_kobj_release,
299 .sysfs_ops = &kobj_pkt_ops,
300 .default_attrs = kobj_pkt_attrs_wqueue
301};
302
303static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
304{
305 if (class_pktcdvd) {
Greg Kroah-Hartmanf79f0602008-05-21 12:52:33 -0700306 pd->dev = device_create_drvdata(class_pktcdvd, NULL,
307 pd->pkt_dev, NULL,
308 "%s", pd->name);
Tony Jones6013c122007-09-25 02:03:03 +0200309 if (IS_ERR(pd->dev))
310 pd->dev = NULL;
Thomas Maier32694852006-12-08 02:36:12 -0800311 }
Tony Jones6013c122007-09-25 02:03:03 +0200312 if (pd->dev) {
Thomas Maier32694852006-12-08 02:36:12 -0800313 pd->kobj_stat = pkt_kobj_create(pd, "stat",
Tony Jones6013c122007-09-25 02:03:03 +0200314 &pd->dev->kobj,
Thomas Maier32694852006-12-08 02:36:12 -0800315 &kobj_pkt_type_stat);
316 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
Tony Jones6013c122007-09-25 02:03:03 +0200317 &pd->dev->kobj,
Thomas Maier32694852006-12-08 02:36:12 -0800318 &kobj_pkt_type_wqueue);
319 }
320}
321
322static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
323{
324 pkt_kobj_remove(pd->kobj_stat);
325 pkt_kobj_remove(pd->kobj_wqueue);
326 if (class_pktcdvd)
Tony Jones6013c122007-09-25 02:03:03 +0200327 device_destroy(class_pktcdvd, pd->pkt_dev);
Thomas Maier32694852006-12-08 02:36:12 -0800328}
329
330
331/********************************************************************
332 /sys/class/pktcdvd/
333 add map block device
334 remove unmap packet dev
335 device_map show mappings
336 *******************************************************************/
337
338static void class_pktcdvd_release(struct class *cls)
339{
340 kfree(cls);
341}
342static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
343{
344 int n = 0;
345 int idx;
346 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
347 for (idx = 0; idx < MAX_WRITERS; idx++) {
348 struct pktcdvd_device *pd = pkt_devs[idx];
349 if (!pd)
350 continue;
351 n += sprintf(data+n, "%s %u:%u %u:%u\n",
352 pd->name,
353 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
354 MAJOR(pd->bdev->bd_dev),
355 MINOR(pd->bdev->bd_dev));
356 }
357 mutex_unlock(&ctl_mutex);
358 return n;
359}
360
361static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
362 size_t count)
363{
364 unsigned int major, minor;
Tejun Heofffe4872007-11-08 08:00:24 +0100365
Thomas Maier83f3aa32007-02-10 01:45:11 -0800366 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Tejun Heofffe4872007-11-08 08:00:24 +0100367 /* pkt_setup_dev() expects caller to hold reference to self */
368 if (!try_module_get(THIS_MODULE))
369 return -ENODEV;
370
Thomas Maier32694852006-12-08 02:36:12 -0800371 pkt_setup_dev(MKDEV(major, minor), NULL);
Tejun Heofffe4872007-11-08 08:00:24 +0100372
373 module_put(THIS_MODULE);
374
Thomas Maier32694852006-12-08 02:36:12 -0800375 return count;
376 }
Tejun Heofffe4872007-11-08 08:00:24 +0100377
Thomas Maier32694852006-12-08 02:36:12 -0800378 return -EINVAL;
379}
380
381static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
382 size_t count)
383{
384 unsigned int major, minor;
Thomas Maier83f3aa32007-02-10 01:45:11 -0800385 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Thomas Maier32694852006-12-08 02:36:12 -0800386 pkt_remove_dev(MKDEV(major, minor));
387 return count;
388 }
389 return -EINVAL;
390}
391
392static struct class_attribute class_pktcdvd_attrs[] = {
393 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
394 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
395 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
396 __ATTR_NULL
397};
398
399
400static int pkt_sysfs_init(void)
401{
402 int ret = 0;
403
404 /*
405 * create control files in sysfs
406 * /sys/class/pktcdvd/...
407 */
408 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
409 if (!class_pktcdvd)
410 return -ENOMEM;
411 class_pktcdvd->name = DRIVER_NAME;
412 class_pktcdvd->owner = THIS_MODULE;
413 class_pktcdvd->class_release = class_pktcdvd_release;
414 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
415 ret = class_register(class_pktcdvd);
416 if (ret) {
417 kfree(class_pktcdvd);
418 class_pktcdvd = NULL;
419 printk(DRIVER_NAME": failed to create class pktcdvd\n");
420 return ret;
421 }
422 return 0;
423}
424
425static void pkt_sysfs_cleanup(void)
426{
427 if (class_pktcdvd)
428 class_destroy(class_pktcdvd);
429 class_pktcdvd = NULL;
430}
431
432/********************************************************************
433 entries in debugfs
434
435 /debugfs/pktcdvd[0-7]/
436 info
437
438 *******************************************************************/
439
440static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
441{
442 return pkt_seq_show(m, p);
443}
444
445static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
446{
447 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
448}
449
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800450static const struct file_operations debug_fops = {
Thomas Maier32694852006-12-08 02:36:12 -0800451 .open = pkt_debugfs_fops_open,
452 .read = seq_read,
453 .llseek = seq_lseek,
454 .release = single_release,
455 .owner = THIS_MODULE,
456};
457
458static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
459{
460 if (!pkt_debugfs_root)
461 return;
462 pd->dfs_f_info = NULL;
463 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
464 if (IS_ERR(pd->dfs_d_root)) {
465 pd->dfs_d_root = NULL;
466 return;
467 }
468 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
469 pd->dfs_d_root, pd, &debug_fops);
470 if (IS_ERR(pd->dfs_f_info)) {
471 pd->dfs_f_info = NULL;
472 return;
473 }
474}
475
476static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
477{
478 if (!pkt_debugfs_root)
479 return;
480 if (pd->dfs_f_info)
481 debugfs_remove(pd->dfs_f_info);
482 pd->dfs_f_info = NULL;
483 if (pd->dfs_d_root)
484 debugfs_remove(pd->dfs_d_root);
485 pd->dfs_d_root = NULL;
486}
487
488static void pkt_debugfs_init(void)
489{
490 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
491 if (IS_ERR(pkt_debugfs_root)) {
492 pkt_debugfs_root = NULL;
493 return;
494 }
495}
496
497static void pkt_debugfs_cleanup(void)
498{
499 if (!pkt_debugfs_root)
500 return;
501 debugfs_remove(pkt_debugfs_root);
502 pkt_debugfs_root = NULL;
503}
504
505/* ----------------------------------------------------------*/
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508static void pkt_bio_finished(struct pktcdvd_device *pd)
509{
510 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
511 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
Thomas Maier78220822006-10-04 02:15:28 -0700512 VPRINTK(DRIVER_NAME": queue empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 atomic_set(&pd->iosched.attention, 1);
514 wake_up(&pd->wqueue);
515 }
516}
517
518static void pkt_bio_destructor(struct bio *bio)
519{
520 kfree(bio->bi_io_vec);
521 kfree(bio);
522}
523
524static struct bio *pkt_bio_alloc(int nr_iovecs)
525{
526 struct bio_vec *bvl = NULL;
527 struct bio *bio;
528
529 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
530 if (!bio)
531 goto no_bio;
532 bio_init(bio);
533
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700534 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!bvl)
536 goto no_bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 bio->bi_max_vecs = nr_iovecs;
539 bio->bi_io_vec = bvl;
540 bio->bi_destructor = pkt_bio_destructor;
541
542 return bio;
543
544 no_bvl:
545 kfree(bio);
546 no_bio:
547 return NULL;
548}
549
550/*
551 * Allocate a packet_data struct
552 */
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800553static struct packet_data *pkt_alloc_packet_data(int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 int i;
556 struct packet_data *pkt;
557
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700558 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!pkt)
560 goto no_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800562 pkt->frames = frames;
563 pkt->w_bio = pkt_bio_alloc(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!pkt->w_bio)
565 goto no_bio;
566
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800567 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
569 if (!pkt->pages[i])
570 goto no_page;
571 }
572
573 spin_lock_init(&pkt->lock);
574
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800575 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct bio *bio = pkt_bio_alloc(1);
577 if (!bio)
578 goto no_rd_bio;
579 pkt->r_bios[i] = bio;
580 }
581
582 return pkt;
583
584no_rd_bio:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800585 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct bio *bio = pkt->r_bios[i];
587 if (bio)
588 bio_put(bio);
589 }
590
591no_page:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800592 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (pkt->pages[i])
594 __free_page(pkt->pages[i]);
595 bio_put(pkt->w_bio);
596no_bio:
597 kfree(pkt);
598no_pkt:
599 return NULL;
600}
601
602/*
603 * Free a packet_data struct
604 */
605static void pkt_free_packet_data(struct packet_data *pkt)
606{
607 int i;
608
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800609 for (i = 0; i < pkt->frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct bio *bio = pkt->r_bios[i];
611 if (bio)
612 bio_put(bio);
613 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800614 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 __free_page(pkt->pages[i]);
616 bio_put(pkt->w_bio);
617 kfree(pkt);
618}
619
620static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
621{
622 struct packet_data *pkt, *next;
623
624 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
625
626 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
627 pkt_free_packet_data(pkt);
628 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800629 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
633{
634 struct packet_data *pkt;
635
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800636 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 while (nr_packets > 0) {
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800639 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!pkt) {
641 pkt_shrink_pktlist(pd);
642 return 0;
643 }
644 pkt->id = nr_packets;
645 pkt->pd = pd;
646 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
647 nr_packets--;
648 }
649 return 1;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
653{
654 struct rb_node *n = rb_next(&node->rb_node);
655 if (!n)
656 return NULL;
657 return rb_entry(n, struct pkt_rb_node, rb_node);
658}
659
Peter Osterlundac893962006-01-14 13:21:32 -0800660static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 rb_erase(&node->rb_node, &pd->bio_queue);
663 mempool_free(node, pd->rb_pool);
664 pd->bio_queue_size--;
665 BUG_ON(pd->bio_queue_size < 0);
666}
667
668/*
669 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
670 */
671static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
672{
673 struct rb_node *n = pd->bio_queue.rb_node;
674 struct rb_node *next;
675 struct pkt_rb_node *tmp;
676
677 if (!n) {
678 BUG_ON(pd->bio_queue_size > 0);
679 return NULL;
680 }
681
682 for (;;) {
683 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
684 if (s <= tmp->bio->bi_sector)
685 next = n->rb_left;
686 else
687 next = n->rb_right;
688 if (!next)
689 break;
690 n = next;
691 }
692
693 if (s > tmp->bio->bi_sector) {
694 tmp = pkt_rbtree_next(tmp);
695 if (!tmp)
696 return NULL;
697 }
698 BUG_ON(s > tmp->bio->bi_sector);
699 return tmp;
700}
701
702/*
703 * Insert a node into the pd->bio_queue rb tree.
704 */
705static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
706{
707 struct rb_node **p = &pd->bio_queue.rb_node;
708 struct rb_node *parent = NULL;
709 sector_t s = node->bio->bi_sector;
710 struct pkt_rb_node *tmp;
711
712 while (*p) {
713 parent = *p;
714 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
715 if (s < tmp->bio->bi_sector)
716 p = &(*p)->rb_left;
717 else
718 p = &(*p)->rb_right;
719 }
720 rb_link_node(&node->rb_node, parent, p);
721 rb_insert_color(&node->rb_node, &pd->bio_queue);
722 pd->bio_queue_size++;
723}
724
725/*
726 * Add a bio to a single linked list defined by its head and tail pointers.
727 */
Peter Osterlundac893962006-01-14 13:21:32 -0800728static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 bio->bi_next = NULL;
731 if (*list_tail) {
732 BUG_ON((*list_head) == NULL);
733 (*list_tail)->bi_next = bio;
734 (*list_tail) = bio;
735 } else {
736 BUG_ON((*list_head) != NULL);
737 (*list_head) = bio;
738 (*list_tail) = bio;
739 }
740}
741
742/*
743 * Remove and return the first bio from a single linked list defined by its
744 * head and tail pointers.
745 */
746static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
747{
748 struct bio *bio;
749
750 if (*list_head == NULL)
751 return NULL;
752
753 bio = *list_head;
754 *list_head = bio->bi_next;
755 if (*list_head == NULL)
756 *list_tail = NULL;
757
758 bio->bi_next = NULL;
759 return bio;
760}
761
762/*
763 * Send a packet_command to the underlying block device and
764 * wait for completion.
765 */
766static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
767{
Jens Axboe165125e2007-07-24 09:28:11 +0200768 struct request_queue *q = bdev_get_queue(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct request *rq;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800770 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800772 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
773 WRITE : READ, __GFP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800775 if (cgc->buflen) {
776 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
777 goto out;
778 }
779
Gerhard Dirschl91e4ee32007-02-20 13:57:56 -0800780 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800781 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 rq->timeout = 60*HZ;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200784 rq->cmd_type = REQ_TYPE_BLOCK_PC;
785 rq->cmd_flags |= REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (cgc->quiet)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200787 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800789 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
Andrew Mortoncbc31a42007-04-25 13:01:21 -0700790 if (rq->errors)
791 ret = -EIO;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800792out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 blk_put_request(rq);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797/*
798 * A generic sense dump / resolve mechanism should be implemented across
799 * all ATAPI + SCSI devices.
800 */
801static void pkt_dump_sense(struct packet_command *cgc)
802{
803 static char *info[9] = { "No sense", "Recovered error", "Not ready",
804 "Medium error", "Hardware error", "Illegal request",
805 "Unit attention", "Data protect", "Blank check" };
806 int i;
807 struct request_sense *sense = cgc->sense;
808
Thomas Maier78220822006-10-04 02:15:28 -0700809 printk(DRIVER_NAME":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 for (i = 0; i < CDROM_PACKET_SIZE; i++)
811 printk(" %02x", cgc->cmd[i]);
812 printk(" - ");
813
814 if (sense == NULL) {
815 printk("no sense\n");
816 return;
817 }
818
819 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
820
821 if (sense->sense_key > 8) {
822 printk(" (INVALID)\n");
823 return;
824 }
825
826 printk(" (%s)\n", info[sense->sense_key]);
827}
828
829/*
830 * flush the drive cache to media
831 */
832static int pkt_flush_cache(struct pktcdvd_device *pd)
833{
834 struct packet_command cgc;
835
836 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
837 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
838 cgc.quiet = 1;
839
840 /*
841 * the IMMED bit -- we default to not setting it, although that
842 * would allow a much faster close, this is safer
843 */
844#if 0
845 cgc.cmd[1] = 1 << 1;
846#endif
847 return pkt_generic_packet(pd, &cgc);
848}
849
850/*
851 * speed is given as the normal factor, e.g. 4 for 4x
852 */
Peter Osterlund05680d82008-03-04 14:28:41 -0800853static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
854 unsigned write_speed, unsigned read_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct packet_command cgc;
857 struct request_sense sense;
858 int ret;
859
860 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
861 cgc.sense = &sense;
862 cgc.cmd[0] = GPCMD_SET_SPEED;
863 cgc.cmd[2] = (read_speed >> 8) & 0xff;
864 cgc.cmd[3] = read_speed & 0xff;
865 cgc.cmd[4] = (write_speed >> 8) & 0xff;
866 cgc.cmd[5] = write_speed & 0xff;
867
868 if ((ret = pkt_generic_packet(pd, &cgc)))
869 pkt_dump_sense(&cgc);
870
871 return ret;
872}
873
874/*
875 * Queue a bio for processing by the low-level CD device. Must be called
876 * from process context.
877 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700878static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 spin_lock(&pd->iosched.lock);
881 if (bio_data_dir(bio) == READ) {
882 pkt_add_list_last(bio, &pd->iosched.read_queue,
883 &pd->iosched.read_queue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
885 pkt_add_list_last(bio, &pd->iosched.write_queue,
886 &pd->iosched.write_queue_tail);
887 }
888 spin_unlock(&pd->iosched.lock);
889
890 atomic_set(&pd->iosched.attention, 1);
891 wake_up(&pd->wqueue);
892}
893
894/*
895 * Process the queued read/write requests. This function handles special
896 * requirements for CDRW drives:
897 * - A cache flush command must be inserted before a read request if the
898 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700899 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700901 * - Optimize for throughput at the expense of latency. This means that streaming
902 * writes will never be interrupted by a read, but if the drive has to seek
903 * before the next write, switch to reading instead if there are any pending
904 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * - Set the read speed according to current usage pattern. When only reading
906 * from the device, it's best to use the highest possible read speed, but
907 * when switching often between reading and writing, it's better to have the
908 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
910static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
911{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (atomic_read(&pd->iosched.attention) == 0)
914 return;
915 atomic_set(&pd->iosched.attention, 0);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 for (;;) {
918 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700919 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 spin_lock(&pd->iosched.lock);
922 reads_queued = (pd->iosched.read_queue != NULL);
923 writes_queued = (pd->iosched.write_queue != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 spin_unlock(&pd->iosched.lock);
925
926 if (!reads_queued && !writes_queued)
927 break;
928
929 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700930 int need_write_seek = 1;
931 spin_lock(&pd->iosched.lock);
932 bio = pd->iosched.write_queue;
933 spin_unlock(&pd->iosched.lock);
934 if (bio && (bio->bi_sector == pd->iosched.last_write))
935 need_write_seek = 0;
936 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700938 VPRINTK(DRIVER_NAME": write, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940 }
941 pkt_flush_cache(pd);
942 pd->iosched.writing = 0;
943 }
944 } else {
945 if (!reads_queued && writes_queued) {
946 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700947 VPRINTK(DRIVER_NAME": read, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
949 }
950 pd->iosched.writing = 1;
951 }
952 }
953
954 spin_lock(&pd->iosched.lock);
955 if (pd->iosched.writing) {
956 bio = pkt_get_list_first(&pd->iosched.write_queue,
957 &pd->iosched.write_queue_tail);
958 } else {
959 bio = pkt_get_list_first(&pd->iosched.read_queue,
960 &pd->iosched.read_queue_tail);
961 }
962 spin_unlock(&pd->iosched.lock);
963
964 if (!bio)
965 continue;
966
967 if (bio_data_dir(bio) == READ)
968 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700969 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700971 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
974 if (pd->read_speed == pd->write_speed) {
975 pd->read_speed = MAX_SPEED;
976 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
977 }
978 } else {
979 if (pd->read_speed != pd->write_speed) {
980 pd->read_speed = pd->write_speed;
981 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
982 }
983 }
984
985 atomic_inc(&pd->cdrw.pending_bios);
986 generic_make_request(bio);
987 }
988}
989
990/*
991 * Special care is needed if the underlying block device has a small
992 * max_phys_segments value.
993 */
Jens Axboe165125e2007-07-24 09:28:11 +0200994static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
997 /*
998 * The cdrom device can handle one segment/frame
999 */
1000 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
1001 return 0;
1002 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
1003 /*
1004 * We can handle this case at the expense of some extra memory
1005 * copies during write operations
1006 */
1007 set_bit(PACKET_MERGE_SEGS, &pd->flags);
1008 return 0;
1009 } else {
Thomas Maier78220822006-10-04 02:15:28 -07001010 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return -EIO;
1012 }
1013}
1014
1015/*
1016 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1017 */
1018static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
1019{
1020 unsigned int copy_size = CD_FRAMESIZE;
1021
1022 while (copy_size > 0) {
1023 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
1024 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
1025 src_bvl->bv_offset + offs;
1026 void *vto = page_address(dst_page) + dst_offs;
1027 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
1028
1029 BUG_ON(len < 0);
1030 memcpy(vto, vfrom, len);
1031 kunmap_atomic(vfrom, KM_USER0);
1032
1033 seg++;
1034 offs = 0;
1035 dst_offs += len;
1036 copy_size -= len;
1037 }
1038}
1039
1040/*
1041 * Copy all data for this packet to pkt->pages[], so that
1042 * a) The number of required segments for the write bio is minimized, which
1043 * is necessary for some scsi controllers.
1044 * b) The data can be used as cache to avoid read requests if we receive a
1045 * new write request for the same zone.
1046 */
Peter Osterlund72772322006-02-14 13:52:56 -08001047static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int f, p, offs;
1050
1051 /* Copy all data to pkt->pages[] */
1052 p = 0;
1053 offs = 0;
1054 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001055 if (bvec[f].bv_page != pkt->pages[p]) {
1056 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 void *vto = page_address(pkt->pages[p]) + offs;
1058 memcpy(vto, vfrom, CD_FRAMESIZE);
1059 kunmap_atomic(vfrom, KM_USER0);
Peter Osterlund72772322006-02-14 13:52:56 -08001060 bvec[f].bv_page = pkt->pages[p];
1061 bvec[f].bv_offset = offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 } else {
Peter Osterlund72772322006-02-14 13:52:56 -08001063 BUG_ON(bvec[f].bv_offset != offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 offs += CD_FRAMESIZE;
1066 if (offs >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 offs = 0;
1068 p++;
1069 }
1070 }
1071}
1072
NeilBrown6712ecf2007-09-27 12:47:43 +02001073static void pkt_end_io_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct packet_data *pkt = bio->bi_private;
1076 struct pktcdvd_device *pd = pkt->pd;
1077 BUG_ON(!pd);
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1080 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1081
1082 if (err)
1083 atomic_inc(&pkt->io_errors);
1084 if (atomic_dec_and_test(&pkt->io_wait)) {
1085 atomic_inc(&pkt->run_sm);
1086 wake_up(&pd->wqueue);
1087 }
1088 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
NeilBrown6712ecf2007-09-27 12:47:43 +02001091static void pkt_end_io_packet_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct packet_data *pkt = bio->bi_private;
1094 struct pktcdvd_device *pd = pkt->pd;
1095 BUG_ON(!pd);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1098
1099 pd->stats.pkt_ended++;
1100
1101 pkt_bio_finished(pd);
1102 atomic_dec(&pkt->io_wait);
1103 atomic_inc(&pkt->run_sm);
1104 wake_up(&pd->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/*
1108 * Schedule reads for the holes in a packet
1109 */
1110static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1111{
1112 int frames_read = 0;
1113 struct bio *bio;
1114 int f;
1115 char written[PACKET_MAX_SIZE];
1116
1117 BUG_ON(!pkt->orig_bios);
1118
1119 atomic_set(&pkt->io_wait, 0);
1120 atomic_set(&pkt->io_errors, 0);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
1123 * Figure out which frames we need to read before we can write.
1124 */
1125 memset(written, 0, sizeof(written));
1126 spin_lock(&pkt->lock);
1127 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1128 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1129 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001130 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 BUG_ON(first_frame < 0);
1132 BUG_ON(first_frame + num_frames > pkt->frames);
1133 for (f = first_frame; f < first_frame + num_frames; f++)
1134 written[f] = 1;
1135 }
1136 spin_unlock(&pkt->lock);
1137
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001138 if (pkt->cache_valid) {
1139 VPRINTK("pkt_gather_data: zone %llx cached\n",
1140 (unsigned long long)pkt->sector);
1141 goto out_account;
1142 }
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * Schedule reads for missing parts of the packet.
1146 */
1147 for (f = 0; f < pkt->frames; f++) {
Jens Axboe761a15e2007-09-14 13:06:53 +02001148 struct bio_vec *vec;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int p, offset;
1151 if (written[f])
1152 continue;
1153 bio = pkt->r_bios[f];
Jens Axboe761a15e2007-09-14 13:06:53 +02001154 vec = bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 bio_init(bio);
1156 bio->bi_max_vecs = 1;
1157 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1158 bio->bi_bdev = pd->bdev;
1159 bio->bi_end_io = pkt_end_io_read;
1160 bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001161 bio->bi_io_vec = vec;
Laurent Riffard7e3da6c2007-09-21 08:32:28 +02001162 bio->bi_destructor = pkt_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1165 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1166 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1167 f, pkt->pages[p], offset);
1168 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1169 BUG();
1170
1171 atomic_inc(&pkt->io_wait);
1172 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001173 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 frames_read++;
1175 }
1176
1177out_account:
1178 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1179 frames_read, (unsigned long long)pkt->sector);
1180 pd->stats.pkt_started++;
1181 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184/*
1185 * Find a packet matching zone, or the least recently used packet if
1186 * there is no match.
1187 */
1188static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1189{
1190 struct packet_data *pkt;
1191
1192 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1193 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1194 list_del_init(&pkt->list);
1195 if (pkt->sector != zone)
1196 pkt->cache_valid = 0;
Peter Osterlund610827d2005-09-13 01:25:29 -07001197 return pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199 }
Peter Osterlund610827d2005-09-13 01:25:29 -07001200 BUG();
1201 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1205{
1206 if (pkt->cache_valid) {
1207 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1208 } else {
1209 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1210 }
1211}
1212
1213/*
1214 * recover a failed write, query for relocation if possible
1215 *
1216 * returns 1 if recovery is possible, or 0 if not
1217 *
1218 */
1219static int pkt_start_recovery(struct packet_data *pkt)
1220{
1221 /*
1222 * FIXME. We need help from the file system to implement
1223 * recovery handling.
1224 */
1225 return 0;
1226#if 0
1227 struct request *rq = pkt->rq;
1228 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1229 struct block_device *pkt_bdev;
1230 struct super_block *sb = NULL;
1231 unsigned long old_block, new_block;
1232 sector_t new_sector;
1233
1234 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1235 if (pkt_bdev) {
1236 sb = get_super(pkt_bdev);
1237 bdput(pkt_bdev);
1238 }
1239
1240 if (!sb)
1241 return 0;
1242
1243 if (!sb->s_op || !sb->s_op->relocate_blocks)
1244 goto out;
1245
1246 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1247 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1248 goto out;
1249
1250 new_sector = new_block * (CD_FRAMESIZE >> 9);
1251 pkt->sector = new_sector;
1252
1253 pkt->bio->bi_sector = new_sector;
1254 pkt->bio->bi_next = NULL;
1255 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1256 pkt->bio->bi_idx = 0;
1257
1258 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1259 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1260 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1261 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1262 BUG_ON(pkt->bio->bi_private != pkt);
1263
1264 drop_super(sb);
1265 return 1;
1266
1267out:
1268 drop_super(sb);
1269 return 0;
1270#endif
1271}
1272
1273static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1274{
1275#if PACKET_DEBUG > 1
1276 static const char *state_name[] = {
1277 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1278 };
1279 enum packet_data_state old_state = pkt->state;
1280 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1281 state_name[old_state], state_name[state]);
1282#endif
1283 pkt->state = state;
1284}
1285
1286/*
1287 * Scan the work queue to see if we can start a new packet.
1288 * returns non-zero if any work was done.
1289 */
1290static int pkt_handle_queue(struct pktcdvd_device *pd)
1291{
1292 struct packet_data *pkt, *p;
1293 struct bio *bio = NULL;
1294 sector_t zone = 0; /* Suppress gcc warning */
1295 struct pkt_rb_node *node, *first_node;
1296 struct rb_node *n;
Thomas Maier0a0fc962006-12-08 02:36:11 -08001297 int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 VPRINTK("handle_queue\n");
1300
1301 atomic_set(&pd->scan_queue, 0);
1302
1303 if (list_empty(&pd->cdrw.pkt_free_list)) {
1304 VPRINTK("handle_queue: no pkt\n");
1305 return 0;
1306 }
1307
1308 /*
1309 * Try to find a zone we are not already working on.
1310 */
1311 spin_lock(&pd->lock);
1312 first_node = pkt_rbtree_find(pd, pd->current_sector);
1313 if (!first_node) {
1314 n = rb_first(&pd->bio_queue);
1315 if (n)
1316 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1317 }
1318 node = first_node;
1319 while (node) {
1320 bio = node->bio;
1321 zone = ZONE(bio->bi_sector, pd);
1322 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001323 if (p->sector == zone) {
1324 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 break;
1329try_next_bio:
1330 node = pkt_rbtree_next(node);
1331 if (!node) {
1332 n = rb_first(&pd->bio_queue);
1333 if (n)
1334 node = rb_entry(n, struct pkt_rb_node, rb_node);
1335 }
1336 if (node == first_node)
1337 node = NULL;
1338 }
1339 spin_unlock(&pd->lock);
1340 if (!bio) {
1341 VPRINTK("handle_queue: no bio\n");
1342 return 0;
1343 }
1344
1345 pkt = pkt_get_packet_data(pd, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 pd->current_sector = zone + pd->settings.size;
1348 pkt->sector = zone;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001349 BUG_ON(pkt->frames != pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 pkt->write_size = 0;
1351
1352 /*
1353 * Scan work queue for bios in the same zone and link them
1354 * to this packet.
1355 */
1356 spin_lock(&pd->lock);
1357 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1358 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1359 bio = node->bio;
1360 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1361 (unsigned long long)ZONE(bio->bi_sector, pd));
1362 if (ZONE(bio->bi_sector, pd) != zone)
1363 break;
1364 pkt_rbtree_erase(pd, node);
1365 spin_lock(&pkt->lock);
1366 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1367 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1368 spin_unlock(&pkt->lock);
1369 }
Thomas Maier0a0fc962006-12-08 02:36:11 -08001370 /* check write congestion marks, and if bio_queue_size is
1371 below, wake up any waiters */
1372 wakeup = (pd->write_congestion_on > 0
1373 && pd->bio_queue_size <= pd->write_congestion_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 spin_unlock(&pd->lock);
Thomas Maier0a0fc962006-12-08 02:36:11 -08001375 if (wakeup)
Thomas Maier83f3aa32007-02-10 01:45:11 -08001376 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1379 pkt_set_state(pkt, PACKET_WAITING_STATE);
1380 atomic_set(&pkt->run_sm, 1);
1381
1382 spin_lock(&pd->cdrw.active_list_lock);
1383 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1384 spin_unlock(&pd->cdrw.active_list_lock);
1385
1386 return 1;
1387}
1388
1389/*
1390 * Assemble a bio to write one packet and queue the bio for processing
1391 * by the underlying block device.
1392 */
1393static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1394{
1395 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int f;
1397 int frames_write;
Peter Osterlund72772322006-02-14 13:52:56 -08001398 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001401 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1402 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 /*
Peter Osterlund72772322006-02-14 13:52:56 -08001406 * Fill-in bvec with data from orig_bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
1408 frames_write = 0;
1409 spin_lock(&pkt->lock);
1410 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1411 int segment = bio->bi_idx;
1412 int src_offs = 0;
1413 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1414 int num_frames = bio->bi_size / CD_FRAMESIZE;
1415 BUG_ON(first_frame < 0);
1416 BUG_ON(first_frame + num_frames > pkt->frames);
1417 for (f = first_frame; f < first_frame + num_frames; f++) {
1418 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1419
1420 while (src_offs >= src_bvl->bv_len) {
1421 src_offs -= src_bvl->bv_len;
1422 segment++;
1423 BUG_ON(segment >= bio->bi_vcnt);
1424 src_bvl = bio_iovec_idx(bio, segment);
1425 }
1426
1427 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
Peter Osterlund72772322006-02-14 13:52:56 -08001428 bvec[f].bv_page = src_bvl->bv_page;
1429 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 } else {
1431 pkt_copy_bio_data(bio, segment, src_offs,
Peter Osterlund72772322006-02-14 13:52:56 -08001432 bvec[f].bv_page, bvec[f].bv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 src_offs += CD_FRAMESIZE;
1435 frames_write++;
1436 }
1437 }
1438 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1439 spin_unlock(&pkt->lock);
1440
1441 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1442 frames_write, (unsigned long long)pkt->sector);
1443 BUG_ON(frames_write != pkt->write_size);
1444
1445 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
Peter Osterlund72772322006-02-14 13:52:56 -08001446 pkt_make_local_copy(pkt, bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 pkt->cache_valid = 1;
1448 } else {
1449 pkt->cache_valid = 0;
1450 }
1451
1452 /* Start the write request */
1453 bio_init(pkt->w_bio);
1454 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1455 pkt->w_bio->bi_sector = pkt->sector;
1456 pkt->w_bio->bi_bdev = pd->bdev;
1457 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1458 pkt->w_bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001459 pkt->w_bio->bi_io_vec = bvec;
Laurent Riffard7e3da6c2007-09-21 08:32:28 +02001460 pkt->w_bio->bi_destructor = pkt_bio_destructor;
Peter Osterlund72772322006-02-14 13:52:56 -08001461 for (f = 0; f < pkt->frames; f++)
1462 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1463 BUG();
Thomas Maier78220822006-10-04 02:15:28 -07001464 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 atomic_set(&pkt->io_wait, 1);
1467 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001468 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1472{
1473 struct bio *bio, *next;
1474
1475 if (!uptodate)
1476 pkt->cache_valid = 0;
1477
1478 /* Finish all bios corresponding to this packet */
1479 bio = pkt->orig_bios;
1480 while (bio) {
1481 next = bio->bi_next;
1482 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001483 bio_endio(bio, uptodate ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 bio = next;
1485 }
1486 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1487}
1488
1489static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1490{
1491 int uptodate;
1492
1493 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1494
1495 for (;;) {
1496 switch (pkt->state) {
1497 case PACKET_WAITING_STATE:
1498 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1499 return;
1500
1501 pkt->sleep_time = 0;
1502 pkt_gather_data(pd, pkt);
1503 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1504 break;
1505
1506 case PACKET_READ_WAIT_STATE:
1507 if (atomic_read(&pkt->io_wait) > 0)
1508 return;
1509
1510 if (atomic_read(&pkt->io_errors) > 0) {
1511 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1512 } else {
1513 pkt_start_write(pd, pkt);
1514 }
1515 break;
1516
1517 case PACKET_WRITE_WAIT_STATE:
1518 if (atomic_read(&pkt->io_wait) > 0)
1519 return;
1520
1521 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1522 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1523 } else {
1524 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1525 }
1526 break;
1527
1528 case PACKET_RECOVERY_STATE:
1529 if (pkt_start_recovery(pkt)) {
1530 pkt_start_write(pd, pkt);
1531 } else {
1532 VPRINTK("No recovery possible\n");
1533 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1534 }
1535 break;
1536
1537 case PACKET_FINISHED_STATE:
1538 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1539 pkt_finish_packet(pkt, uptodate);
1540 return;
1541
1542 default:
1543 BUG();
1544 break;
1545 }
1546 }
1547}
1548
1549static void pkt_handle_packets(struct pktcdvd_device *pd)
1550{
1551 struct packet_data *pkt, *next;
1552
1553 VPRINTK("pkt_handle_packets\n");
1554
1555 /*
1556 * Run state machine for active packets
1557 */
1558 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1559 if (atomic_read(&pkt->run_sm) > 0) {
1560 atomic_set(&pkt->run_sm, 0);
1561 pkt_run_state_machine(pd, pkt);
1562 }
1563 }
1564
1565 /*
1566 * Move no longer active packets to the free list
1567 */
1568 spin_lock(&pd->cdrw.active_list_lock);
1569 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1570 if (pkt->state == PACKET_FINISHED_STATE) {
1571 list_del(&pkt->list);
1572 pkt_put_packet_data(pd, pkt);
1573 pkt_set_state(pkt, PACKET_IDLE_STATE);
1574 atomic_set(&pd->scan_queue, 1);
1575 }
1576 }
1577 spin_unlock(&pd->cdrw.active_list_lock);
1578}
1579
1580static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1581{
1582 struct packet_data *pkt;
1583 int i;
1584
Peter Osterlundae7642b2005-11-13 16:06:36 -08001585 for (i = 0; i < PACKET_NUM_STATES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 states[i] = 0;
1587
1588 spin_lock(&pd->cdrw.active_list_lock);
1589 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1590 states[pkt->state]++;
1591 }
1592 spin_unlock(&pd->cdrw.active_list_lock);
1593}
1594
1595/*
1596 * kcdrwd is woken up when writes have been queued for one of our
1597 * registered devices
1598 */
1599static int kcdrwd(void *foobar)
1600{
1601 struct pktcdvd_device *pd = foobar;
1602 struct packet_data *pkt;
1603 long min_sleep_time, residue;
1604
1605 set_user_nice(current, -20);
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001606 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 for (;;) {
1609 DECLARE_WAITQUEUE(wait, current);
1610
1611 /*
1612 * Wait until there is something to do
1613 */
1614 add_wait_queue(&pd->wqueue, &wait);
1615 for (;;) {
1616 set_current_state(TASK_INTERRUPTIBLE);
1617
1618 /* Check if we need to run pkt_handle_queue */
1619 if (atomic_read(&pd->scan_queue) > 0)
1620 goto work_to_do;
1621
1622 /* Check if we need to run the state machine for some packet */
1623 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1624 if (atomic_read(&pkt->run_sm) > 0)
1625 goto work_to_do;
1626 }
1627
1628 /* Check if we need to process the iosched queues */
1629 if (atomic_read(&pd->iosched.attention) != 0)
1630 goto work_to_do;
1631
1632 /* Otherwise, go to sleep */
1633 if (PACKET_DEBUG > 1) {
1634 int states[PACKET_NUM_STATES];
1635 pkt_count_states(pd, states);
1636 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1637 states[0], states[1], states[2], states[3],
1638 states[4], states[5]);
1639 }
1640
1641 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1642 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1643 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1644 min_sleep_time = pkt->sleep_time;
1645 }
1646
1647 generic_unplug_device(bdev_get_queue(pd->bdev));
1648
1649 VPRINTK("kcdrwd: sleeping\n");
1650 residue = schedule_timeout(min_sleep_time);
1651 VPRINTK("kcdrwd: wake up\n");
1652
1653 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001654 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1657 if (!pkt->sleep_time)
1658 continue;
1659 pkt->sleep_time -= min_sleep_time - residue;
1660 if (pkt->sleep_time <= 0) {
1661 pkt->sleep_time = 0;
1662 atomic_inc(&pkt->run_sm);
1663 }
1664 }
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (kthread_should_stop())
1667 break;
1668 }
1669work_to_do:
1670 set_current_state(TASK_RUNNING);
1671 remove_wait_queue(&pd->wqueue, &wait);
1672
1673 if (kthread_should_stop())
1674 break;
1675
1676 /*
1677 * if pkt_handle_queue returns true, we can queue
1678 * another request.
1679 */
1680 while (pkt_handle_queue(pd))
1681 ;
1682
1683 /*
1684 * Handle packet state machine
1685 */
1686 pkt_handle_packets(pd);
1687
1688 /*
1689 * Handle iosched queues
1690 */
1691 pkt_iosched_process_queue(pd);
1692 }
1693
1694 return 0;
1695}
1696
1697static void pkt_print_settings(struct pktcdvd_device *pd)
1698{
Thomas Maier78220822006-10-04 02:15:28 -07001699 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 printk("%u blocks, ", pd->settings.size >> 2);
1701 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1702}
1703
1704static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1705{
1706 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1707
1708 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1709 cgc->cmd[2] = page_code | (page_control << 6);
1710 cgc->cmd[7] = cgc->buflen >> 8;
1711 cgc->cmd[8] = cgc->buflen & 0xff;
1712 cgc->data_direction = CGC_DATA_READ;
1713 return pkt_generic_packet(pd, cgc);
1714}
1715
1716static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1717{
1718 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1719 memset(cgc->buffer, 0, 2);
1720 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1721 cgc->cmd[1] = 0x10; /* PF */
1722 cgc->cmd[7] = cgc->buflen >> 8;
1723 cgc->cmd[8] = cgc->buflen & 0xff;
1724 cgc->data_direction = CGC_DATA_WRITE;
1725 return pkt_generic_packet(pd, cgc);
1726}
1727
1728static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1729{
1730 struct packet_command cgc;
1731 int ret;
1732
1733 /* set up command and get the disc info */
1734 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1735 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1736 cgc.cmd[8] = cgc.buflen = 2;
1737 cgc.quiet = 1;
1738
1739 if ((ret = pkt_generic_packet(pd, &cgc)))
1740 return ret;
1741
1742 /* not all drives have the same disc_info length, so requeue
1743 * packet with the length the drive tells us it can supply
1744 */
1745 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1746 sizeof(di->disc_information_length);
1747
1748 if (cgc.buflen > sizeof(disc_information))
1749 cgc.buflen = sizeof(disc_information);
1750
1751 cgc.cmd[8] = cgc.buflen;
1752 return pkt_generic_packet(pd, &cgc);
1753}
1754
1755static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1756{
1757 struct packet_command cgc;
1758 int ret;
1759
1760 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1761 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1762 cgc.cmd[1] = type & 3;
1763 cgc.cmd[4] = (track & 0xff00) >> 8;
1764 cgc.cmd[5] = track & 0xff;
1765 cgc.cmd[8] = 8;
1766 cgc.quiet = 1;
1767
1768 if ((ret = pkt_generic_packet(pd, &cgc)))
1769 return ret;
1770
1771 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1772 sizeof(ti->track_information_length);
1773
1774 if (cgc.buflen > sizeof(track_information))
1775 cgc.buflen = sizeof(track_information);
1776
1777 cgc.cmd[8] = cgc.buflen;
1778 return pkt_generic_packet(pd, &cgc);
1779}
1780
Peter Osterlund05680d82008-03-04 14:28:41 -08001781static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1782 long *last_written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 disc_information di;
1785 track_information ti;
1786 __u32 last_track;
1787 int ret = -1;
1788
1789 if ((ret = pkt_get_disc_info(pd, &di)))
1790 return ret;
1791
1792 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1793 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1794 return ret;
1795
1796 /* if this track is blank, try the previous. */
1797 if (ti.blank) {
1798 last_track--;
1799 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1800 return ret;
1801 }
1802
1803 /* if last recorded field is valid, return it. */
1804 if (ti.lra_v) {
1805 *last_written = be32_to_cpu(ti.last_rec_address);
1806 } else {
1807 /* make it up instead */
1808 *last_written = be32_to_cpu(ti.track_start) +
1809 be32_to_cpu(ti.track_size);
1810 if (ti.free_blocks)
1811 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1812 }
1813 return 0;
1814}
1815
1816/*
1817 * write mode select package based on pd->settings
1818 */
Peter Osterlund05680d82008-03-04 14:28:41 -08001819static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct packet_command cgc;
1822 struct request_sense sense;
1823 write_param_page *wp;
1824 char buffer[128];
1825 int ret, size;
1826
1827 /* doesn't apply to DVD+RW or DVD-RAM */
1828 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1829 return 0;
1830
1831 memset(buffer, 0, sizeof(buffer));
1832 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1833 cgc.sense = &sense;
1834 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1835 pkt_dump_sense(&cgc);
1836 return ret;
1837 }
1838
1839 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1840 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1841 if (size > sizeof(buffer))
1842 size = sizeof(buffer);
1843
1844 /*
1845 * now get it all
1846 */
1847 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1848 cgc.sense = &sense;
1849 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1850 pkt_dump_sense(&cgc);
1851 return ret;
1852 }
1853
1854 /*
1855 * write page is offset header + block descriptor length
1856 */
1857 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1858
1859 wp->fp = pd->settings.fp;
1860 wp->track_mode = pd->settings.track_mode;
1861 wp->write_type = pd->settings.write_type;
1862 wp->data_block_type = pd->settings.block_mode;
1863
1864 wp->multi_session = 0;
1865
1866#ifdef PACKET_USE_LS
1867 wp->link_size = 7;
1868 wp->ls_v = 1;
1869#endif
1870
1871 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1872 wp->session_format = 0;
1873 wp->subhdr2 = 0x20;
1874 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1875 wp->session_format = 0x20;
1876 wp->subhdr2 = 8;
1877#if 0
1878 wp->mcn[0] = 0x80;
1879 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1880#endif
1881 } else {
1882 /*
1883 * paranoia
1884 */
Thomas Maier78220822006-10-04 02:15:28 -07001885 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return 1;
1887 }
1888 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1889
1890 cgc.buflen = cgc.cmd[8] = size;
1891 if ((ret = pkt_mode_select(pd, &cgc))) {
1892 pkt_dump_sense(&cgc);
1893 return ret;
1894 }
1895
1896 pkt_print_settings(pd);
1897 return 0;
1898}
1899
1900/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001901 * 1 -- we can write to this track, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001903static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Peter Osterlundab863ec2006-02-20 18:28:04 -08001905 switch (pd->mmc3_profile) {
1906 case 0x1a: /* DVD+RW */
1907 case 0x12: /* DVD-RAM */
1908 /* The track is always writable on DVD+RW/DVD-RAM */
1909 return 1;
1910 default:
1911 break;
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Peter Osterlundab863ec2006-02-20 18:28:04 -08001914 if (!ti->packet || !ti->fp)
1915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /*
1918 * "good" settings as per Mt Fuji.
1919 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001920 if (ti->rt == 0 && ti->blank == 0)
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 == 0 && ti->blank == 1)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001924 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Peter Osterlundab863ec2006-02-20 18:28:04 -08001926 if (ti->rt == 1 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001927 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Thomas Maier78220822006-10-04 02:15:28 -07001929 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001934 * 1 -- we can write to this disc, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001936static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 switch (pd->mmc3_profile) {
1939 case 0x0a: /* CD-RW */
1940 case 0xffff: /* MMC3 not supported */
1941 break;
1942 case 0x1a: /* DVD+RW */
1943 case 0x13: /* DVD-RW */
1944 case 0x12: /* DVD-RAM */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001945 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 default:
Thomas Maier78220822006-10-04 02:15:28 -07001947 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950
1951 /*
1952 * for disc type 0xff we should probably reserve a new track.
1953 * but i'm not sure, should we leave this to user apps? probably.
1954 */
1955 if (di->disc_type == 0xff) {
Thomas Maier78220822006-10-04 02:15:28 -07001956 printk(DRIVER_NAME": Unknown disc. No track?\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
1960 if (di->disc_type != 0x20 && di->disc_type != 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001961 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
1965 if (di->erasable == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001966 printk(DRIVER_NAME": Disc not erasable\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
1970 if (di->border_status == PACKET_SESSION_RESERVED) {
Thomas Maier78220822006-10-04 02:15:28 -07001971 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974
Peter Osterlund7c613d52006-02-20 18:28:02 -08001975 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Peter Osterlund05680d82008-03-04 14:28:41 -08001978static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct packet_command cgc;
1981 unsigned char buf[12];
1982 disc_information di;
1983 track_information ti;
1984 int ret, track;
1985
1986 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1987 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1988 cgc.cmd[8] = 8;
1989 ret = pkt_generic_packet(pd, &cgc);
1990 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1991
1992 memset(&di, 0, sizeof(disc_information));
1993 memset(&ti, 0, sizeof(track_information));
1994
1995 if ((ret = pkt_get_disc_info(pd, &di))) {
1996 printk("failed get_disc\n");
1997 return ret;
1998 }
1999
Peter Osterlund7c613d52006-02-20 18:28:02 -08002000 if (!pkt_writable_disc(pd, &di))
Peter Osterlund9db91542006-02-20 18:28:04 -08002001 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
2004
2005 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
2006 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
Thomas Maier78220822006-10-04 02:15:28 -07002007 printk(DRIVER_NAME": failed get_track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return ret;
2009 }
2010
Peter Osterlundab863ec2006-02-20 18:28:04 -08002011 if (!pkt_writable_track(pd, &ti)) {
Thomas Maier78220822006-10-04 02:15:28 -07002012 printk(DRIVER_NAME": can't write to this track\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002013 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
2016 /*
2017 * we keep packet size in 512 byte units, makes it easier to
2018 * deal with request calculations.
2019 */
2020 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2021 if (pd->settings.size == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07002022 printk(DRIVER_NAME": detected zero packet size!\n");
Phillip Susia460ad62006-02-04 23:27:44 -08002023 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07002025 if (pd->settings.size > PACKET_MAX_SECTORS) {
Thomas Maier78220822006-10-04 02:15:28 -07002026 printk(DRIVER_NAME": packet size is too big\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002027 return -EROFS;
Peter Osterlundd0272e72005-09-13 01:25:27 -07002028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 pd->settings.fp = ti.fp;
2030 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2031
2032 if (ti.nwa_v) {
2033 pd->nwa = be32_to_cpu(ti.next_writable);
2034 set_bit(PACKET_NWA_VALID, &pd->flags);
2035 }
2036
2037 /*
2038 * in theory we could use lra on -RW media as well and just zero
2039 * blocks that haven't been written yet, but in practice that
2040 * is just a no-go. we'll use that for -R, naturally.
2041 */
2042 if (ti.lra_v) {
2043 pd->lra = be32_to_cpu(ti.last_rec_address);
2044 set_bit(PACKET_LRA_VALID, &pd->flags);
2045 } else {
2046 pd->lra = 0xffffffff;
2047 set_bit(PACKET_LRA_VALID, &pd->flags);
2048 }
2049
2050 /*
2051 * fine for now
2052 */
2053 pd->settings.link_loss = 7;
2054 pd->settings.write_type = 0; /* packet */
2055 pd->settings.track_mode = ti.track_mode;
2056
2057 /*
2058 * mode1 or mode2 disc
2059 */
2060 switch (ti.data_mode) {
2061 case PACKET_MODE1:
2062 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2063 break;
2064 case PACKET_MODE2:
2065 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2066 break;
2067 default:
Thomas Maier78220822006-10-04 02:15:28 -07002068 printk(DRIVER_NAME": unknown data mode\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002069 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 return 0;
2072}
2073
2074/*
2075 * enable/disable write caching on drive
2076 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002077static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2078 int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct packet_command cgc;
2081 struct request_sense sense;
2082 unsigned char buf[64];
2083 int ret;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2086 cgc.sense = &sense;
2087 cgc.buflen = pd->mode_offset + 12;
2088
2089 /*
2090 * caching mode page might not be there, so quiet this command
2091 */
2092 cgc.quiet = 1;
2093
2094 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2095 return ret;
2096
2097 buf[pd->mode_offset + 10] |= (!!set << 2);
2098
2099 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2100 ret = pkt_mode_select(pd, &cgc);
2101 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07002102 printk(DRIVER_NAME": write caching control failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 pkt_dump_sense(&cgc);
2104 } else if (!ret && set)
Thomas Maier78220822006-10-04 02:15:28 -07002105 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return ret;
2107}
2108
2109static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2110{
2111 struct packet_command cgc;
2112
2113 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2114 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2115 cgc.cmd[4] = lockflag ? 1 : 0;
2116 return pkt_generic_packet(pd, &cgc);
2117}
2118
2119/*
2120 * Returns drive maximum write speed
2121 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002122static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2123 unsigned *write_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 struct packet_command cgc;
2126 struct request_sense sense;
2127 unsigned char buf[256+18];
2128 unsigned char *cap_buf;
2129 int ret, offset;
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2132 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2133 cgc.sense = &sense;
2134
2135 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2136 if (ret) {
2137 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2138 sizeof(struct mode_page_header);
2139 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2140 if (ret) {
2141 pkt_dump_sense(&cgc);
2142 return ret;
2143 }
2144 }
2145
2146 offset = 20; /* Obsoleted field, used by older drives */
2147 if (cap_buf[1] >= 28)
2148 offset = 28; /* Current write speed selected */
2149 if (cap_buf[1] >= 30) {
2150 /* If the drive reports at least one "Logical Unit Write
2151 * Speed Performance Descriptor Block", use the information
2152 * in the first block. (contains the highest speed)
2153 */
2154 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2155 if (num_spdb > 0)
2156 offset = 34;
2157 }
2158
2159 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2160 return 0;
2161}
2162
2163/* These tables from cdrecord - I don't have orange book */
2164/* standard speed CD-RW (1-4x) */
2165static char clv_to_speed[16] = {
2166 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2167 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2168};
2169/* high speed CD-RW (-10x) */
2170static char hs_clv_to_speed[16] = {
2171 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2172 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2173};
2174/* ultra high speed CD-RW */
2175static char us_clv_to_speed[16] = {
2176 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2177 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2178};
2179
2180/*
2181 * reads the maximum media speed from ATIP
2182 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002183static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2184 unsigned *speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct packet_command cgc;
2187 struct request_sense sense;
2188 unsigned char buf[64];
2189 unsigned int size, st, sp;
2190 int ret;
2191
2192 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2193 cgc.sense = &sense;
2194 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2195 cgc.cmd[1] = 2;
2196 cgc.cmd[2] = 4; /* READ ATIP */
2197 cgc.cmd[8] = 2;
2198 ret = pkt_generic_packet(pd, &cgc);
2199 if (ret) {
2200 pkt_dump_sense(&cgc);
2201 return ret;
2202 }
2203 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2204 if (size > sizeof(buf))
2205 size = sizeof(buf);
2206
2207 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2208 cgc.sense = &sense;
2209 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2210 cgc.cmd[1] = 2;
2211 cgc.cmd[2] = 4;
2212 cgc.cmd[8] = size;
2213 ret = pkt_generic_packet(pd, &cgc);
2214 if (ret) {
2215 pkt_dump_sense(&cgc);
2216 return ret;
2217 }
2218
Alexey Dobriyaneaa0ff12008-02-06 01:36:06 -08002219 if (!(buf[6] & 0x40)) {
Thomas Maier78220822006-10-04 02:15:28 -07002220 printk(DRIVER_NAME": Disc type is not CD-RW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return 1;
2222 }
Alexey Dobriyaneaa0ff12008-02-06 01:36:06 -08002223 if (!(buf[6] & 0x4)) {
Thomas Maier78220822006-10-04 02:15:28 -07002224 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 1;
2226 }
2227
2228 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2229
2230 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2231
2232 /* Info from cdrecord */
2233 switch (st) {
2234 case 0: /* standard speed */
2235 *speed = clv_to_speed[sp];
2236 break;
2237 case 1: /* high speed */
2238 *speed = hs_clv_to_speed[sp];
2239 break;
2240 case 2: /* ultra high speed */
2241 *speed = us_clv_to_speed[sp];
2242 break;
2243 default:
Thomas Maier78220822006-10-04 02:15:28 -07002244 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return 1;
2246 }
2247 if (*speed) {
Thomas Maier78220822006-10-04 02:15:28 -07002248 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 0;
2250 } else {
Thomas Maier78220822006-10-04 02:15:28 -07002251 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return 1;
2253 }
2254}
2255
Peter Osterlund05680d82008-03-04 14:28:41 -08002256static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 struct packet_command cgc;
2259 struct request_sense sense;
2260 int ret;
2261
Thomas Maier78220822006-10-04 02:15:28 -07002262 VPRINTK(DRIVER_NAME": Performing OPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2265 cgc.sense = &sense;
2266 cgc.timeout = 60*HZ;
2267 cgc.cmd[0] = GPCMD_SEND_OPC;
2268 cgc.cmd[1] = 1;
2269 if ((ret = pkt_generic_packet(pd, &cgc)))
2270 pkt_dump_sense(&cgc);
2271 return ret;
2272}
2273
2274static int pkt_open_write(struct pktcdvd_device *pd)
2275{
2276 int ret;
2277 unsigned int write_speed, media_write_speed, read_speed;
2278
2279 if ((ret = pkt_probe_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002280 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
Peter Osterlund9db91542006-02-20 18:28:04 -08002281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283
2284 if ((ret = pkt_set_write_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002285 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return -EIO;
2287 }
2288
2289 pkt_write_caching(pd, USE_WCACHING);
2290
2291 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2292 write_speed = 16 * 177;
2293 switch (pd->mmc3_profile) {
2294 case 0x13: /* DVD-RW */
2295 case 0x1a: /* DVD+RW */
2296 case 0x12: /* DVD-RAM */
Thomas Maier78220822006-10-04 02:15:28 -07002297 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 break;
2299 default:
2300 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2301 media_write_speed = 16;
2302 write_speed = min(write_speed, media_write_speed * 177);
Thomas Maier78220822006-10-04 02:15:28 -07002303 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 break;
2305 }
2306 read_speed = write_speed;
2307
2308 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
Thomas Maier78220822006-10-04 02:15:28 -07002309 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return -EIO;
2311 }
2312 pd->write_speed = write_speed;
2313 pd->read_speed = read_speed;
2314
2315 if ((ret = pkt_perform_opc(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002316 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318
2319 return 0;
2320}
2321
2322/*
2323 * called at open time.
2324 */
2325static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2326{
2327 int ret;
2328 long lba;
Jens Axboe165125e2007-07-24 09:28:11 +02002329 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /*
2332 * We need to re-open the cdrom device without O_NONBLOCK to be able
2333 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2334 * so bdget() can't fail.
2335 */
2336 bdget(pd->bdev->bd_dev);
2337 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2338 goto out;
2339
Peter Osterlund8382bf22006-01-08 01:02:17 -08002340 if ((ret = bd_claim(pd->bdev, pd)))
2341 goto out_putdev;
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if ((ret = pkt_get_last_written(pd, &lba))) {
Thomas Maier78220822006-10-04 02:15:28 -07002344 printk(DRIVER_NAME": pkt_get_last_written failed\n");
Peter Osterlund8382bf22006-01-08 01:02:17 -08002345 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 set_capacity(pd->disk, lba << 2);
2349 set_capacity(pd->bdev->bd_disk, lba << 2);
2350 bd_set_size(pd->bdev, (loff_t)lba << 11);
2351
2352 q = bdev_get_queue(pd->bdev);
2353 if (write) {
2354 if ((ret = pkt_open_write(pd)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002355 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /*
2357 * Some CDRW drives can not handle writes larger than one packet,
2358 * even if the size is a multiple of the packet size.
2359 */
2360 spin_lock_irq(q->queue_lock);
2361 blk_queue_max_sectors(q, pd->settings.size);
2362 spin_unlock_irq(q->queue_lock);
2363 set_bit(PACKET_WRITABLE, &pd->flags);
2364 } else {
2365 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2366 clear_bit(PACKET_WRITABLE, &pd->flags);
2367 }
2368
2369 if ((ret = pkt_set_segment_merging(pd, q)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002370 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002372 if (write) {
2373 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
Thomas Maier78220822006-10-04 02:15:28 -07002374 printk(DRIVER_NAME": not enough memory for buffers\n");
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002375 ret = -ENOMEM;
2376 goto out_unclaim;
2377 }
Thomas Maier78220822006-10-04 02:15:28 -07002378 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 return 0;
2382
Peter Osterlund8382bf22006-01-08 01:02:17 -08002383out_unclaim:
2384 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385out_putdev:
2386 blkdev_put(pd->bdev);
2387out:
2388 return ret;
2389}
2390
2391/*
2392 * called when the device is closed. makes sure that the device flushes
2393 * the internal cache before we close.
2394 */
2395static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2396{
2397 if (flush && pkt_flush_cache(pd))
Thomas Maier78220822006-10-04 02:15:28 -07002398 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 pkt_lock_door(pd, 0);
2401
2402 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
Peter Osterlund8382bf22006-01-08 01:02:17 -08002403 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 blkdev_put(pd->bdev);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002405
2406 pkt_shrink_pktlist(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2410{
2411 if (dev_minor >= MAX_WRITERS)
2412 return NULL;
2413 return pkt_devs[dev_minor];
2414}
2415
2416static int pkt_open(struct inode *inode, struct file *file)
2417{
2418 struct pktcdvd_device *pd = NULL;
2419 int ret;
2420
Thomas Maier78220822006-10-04 02:15:28 -07002421 VPRINTK(DRIVER_NAME": entering open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Jes Sorensen1657f822006-03-23 03:00:25 -08002423 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 pd = pkt_find_dev_from_minor(iminor(inode));
2425 if (!pd) {
2426 ret = -ENODEV;
2427 goto out;
2428 }
2429 BUG_ON(pd->refcnt < 0);
2430
2431 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002432 if (pd->refcnt > 1) {
2433 if ((file->f_mode & FMODE_WRITE) &&
2434 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2435 ret = -EBUSY;
2436 goto out_dec;
2437 }
2438 } else {
Peter Osterlund01fd9fd2006-02-14 13:52:55 -08002439 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2440 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /*
2443 * needed here as well, since ext2 (among others) may change
2444 * the blocksize at mount time
2445 */
2446 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2447 }
2448
Jes Sorensen1657f822006-03-23 03:00:25 -08002449 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return 0;
2451
2452out_dec:
2453 pd->refcnt--;
2454out:
Thomas Maier78220822006-10-04 02:15:28 -07002455 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
Jes Sorensen1657f822006-03-23 03:00:25 -08002456 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return ret;
2458}
2459
2460static int pkt_close(struct inode *inode, struct file *file)
2461{
2462 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2463 int ret = 0;
2464
Jes Sorensen1657f822006-03-23 03:00:25 -08002465 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 pd->refcnt--;
2467 BUG_ON(pd->refcnt < 0);
2468 if (pd->refcnt == 0) {
2469 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2470 pkt_release_dev(pd, flush);
2471 }
Jes Sorensen1657f822006-03-23 03:00:25 -08002472 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return ret;
2474}
2475
2476
NeilBrown6712ecf2007-09-27 12:47:43 +02002477static void pkt_end_io_read_cloned(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 struct packet_stacked_data *psd = bio->bi_private;
2480 struct pktcdvd_device *pd = psd->pd;
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +02002483 bio_endio(psd->bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 mempool_free(psd, psd_pool);
2485 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Jens Axboe165125e2007-07-24 09:28:11 +02002488static int pkt_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
2490 struct pktcdvd_device *pd;
2491 char b[BDEVNAME_SIZE];
2492 sector_t zone;
2493 struct packet_data *pkt;
2494 int was_empty, blocked_bio;
2495 struct pkt_rb_node *node;
2496
2497 pd = q->queuedata;
2498 if (!pd) {
Thomas Maier78220822006-10-04 02:15:28 -07002499 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto end_io;
2501 }
2502
2503 /*
2504 * Clone READ bios so we can have our own bi_end_io callback.
2505 */
2506 if (bio_data_dir(bio) == READ) {
2507 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2508 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2509
2510 psd->pd = pd;
2511 psd->bio = bio;
2512 cloned_bio->bi_bdev = pd->bdev;
2513 cloned_bio->bi_private = psd;
2514 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2515 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002516 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
2518 }
2519
2520 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
Thomas Maier78220822006-10-04 02:15:28 -07002521 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 pd->name, (unsigned long long)bio->bi_sector);
2523 goto end_io;
2524 }
2525
2526 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
Thomas Maier78220822006-10-04 02:15:28 -07002527 printk(DRIVER_NAME": wrong bio size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto end_io;
2529 }
2530
2531 blk_queue_bounce(q, &bio);
2532
2533 zone = ZONE(bio->bi_sector, pd);
2534 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2535 (unsigned long long)bio->bi_sector,
2536 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2537
2538 /* Check if we have to split the bio */
2539 {
2540 struct bio_pair *bp;
2541 sector_t last_zone;
2542 int first_sectors;
2543
2544 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2545 if (last_zone != zone) {
2546 BUG_ON(last_zone != zone + pd->settings.size);
2547 first_sectors = last_zone - bio->bi_sector;
2548 bp = bio_split(bio, bio_split_pool, first_sectors);
2549 BUG_ON(!bp);
2550 pkt_make_request(q, &bp->bio1);
2551 pkt_make_request(q, &bp->bio2);
2552 bio_pair_release(bp);
2553 return 0;
2554 }
2555 }
2556
2557 /*
2558 * If we find a matching packet in state WAITING or READ_WAIT, we can
2559 * just append this bio to that packet.
2560 */
2561 spin_lock(&pd->cdrw.active_list_lock);
2562 blocked_bio = 0;
2563 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2564 if (pkt->sector == zone) {
2565 spin_lock(&pkt->lock);
2566 if ((pkt->state == PACKET_WAITING_STATE) ||
2567 (pkt->state == PACKET_READ_WAIT_STATE)) {
2568 pkt_add_list_last(bio, &pkt->orig_bios,
2569 &pkt->orig_bios_tail);
2570 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2571 if ((pkt->write_size >= pkt->frames) &&
2572 (pkt->state == PACKET_WAITING_STATE)) {
2573 atomic_inc(&pkt->run_sm);
2574 wake_up(&pd->wqueue);
2575 }
2576 spin_unlock(&pkt->lock);
2577 spin_unlock(&pd->cdrw.active_list_lock);
2578 return 0;
2579 } else {
2580 blocked_bio = 1;
2581 }
2582 spin_unlock(&pkt->lock);
2583 }
2584 }
2585 spin_unlock(&pd->cdrw.active_list_lock);
2586
Thomas Maier0a0fc962006-12-08 02:36:11 -08002587 /*
2588 * Test if there is enough room left in the bio work queue
2589 * (queue size >= congestion on mark).
2590 * If not, wait till the work queue size is below the congestion off mark.
2591 */
2592 spin_lock(&pd->lock);
2593 if (pd->write_congestion_on > 0
2594 && pd->bio_queue_size >= pd->write_congestion_on) {
Thomas Maier83f3aa32007-02-10 01:45:11 -08002595 set_bdi_congested(&q->backing_dev_info, WRITE);
Thomas Maier0a0fc962006-12-08 02:36:11 -08002596 do {
2597 spin_unlock(&pd->lock);
2598 congestion_wait(WRITE, HZ);
2599 spin_lock(&pd->lock);
2600 } while(pd->bio_queue_size > pd->write_congestion_off);
2601 }
2602 spin_unlock(&pd->lock);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /*
2605 * No matching packet found. Store the bio in the work queue.
2606 */
2607 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 node->bio = bio;
2609 spin_lock(&pd->lock);
2610 BUG_ON(pd->bio_queue_size < 0);
2611 was_empty = (pd->bio_queue_size == 0);
2612 pkt_rbtree_insert(pd, node);
2613 spin_unlock(&pd->lock);
2614
2615 /*
2616 * Wake up the worker thread.
2617 */
2618 atomic_set(&pd->scan_queue, 1);
2619 if (was_empty) {
2620 /* This wake_up is required for correct operation */
2621 wake_up(&pd->wqueue);
2622 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2623 /*
2624 * This wake up is not required for correct operation,
2625 * but improves performance in some cases.
2626 */
2627 wake_up(&pd->wqueue);
2628 }
2629 return 0;
2630end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02002631 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return 0;
2633}
2634
2635
2636
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02002637static int pkt_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2638 struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 struct pktcdvd_device *pd = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02002641 sector_t zone = ZONE(bmd->bi_sector, pd);
2642 int used = ((bmd->bi_sector - zone) << 9) + bmd->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 int remaining = (pd->settings.size << 9) - used;
2644 int remaining2;
2645
2646 /*
2647 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2648 * boundary, pkt_make_request() will split the bio.
2649 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02002650 remaining2 = PAGE_SIZE - bmd->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 remaining = max(remaining, remaining2);
2652
2653 BUG_ON(remaining < 0);
2654 return remaining;
2655}
2656
2657static void pkt_init_queue(struct pktcdvd_device *pd)
2658{
Jens Axboe165125e2007-07-24 09:28:11 +02002659 struct request_queue *q = pd->disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 blk_queue_make_request(q, pkt_make_request);
2662 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2663 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2664 blk_queue_merge_bvec(q, pkt_merge_bvec);
2665 q->queuedata = pd;
2666}
2667
2668static int pkt_seq_show(struct seq_file *m, void *p)
2669{
2670 struct pktcdvd_device *pd = m->private;
2671 char *msg;
2672 char bdev_buf[BDEVNAME_SIZE];
2673 int states[PACKET_NUM_STATES];
2674
2675 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2676 bdevname(pd->bdev, bdev_buf));
2677
2678 seq_printf(m, "\nSettings:\n");
2679 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2680
2681 if (pd->settings.write_type == 0)
2682 msg = "Packet";
2683 else
2684 msg = "Unknown";
2685 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2686
2687 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2688 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2689
2690 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2691
2692 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2693 msg = "Mode 1";
2694 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2695 msg = "Mode 2";
2696 else
2697 msg = "Unknown";
2698 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2699
2700 seq_printf(m, "\nStatistics:\n");
2701 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2702 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2703 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2704 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2705 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2706
2707 seq_printf(m, "\nMisc:\n");
2708 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2709 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2710 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2711 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2712 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2713 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2714
2715 seq_printf(m, "\nQueue state:\n");
2716 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2717 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2718 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2719
2720 pkt_count_states(pd, states);
2721 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2722 states[0], states[1], states[2], states[3], states[4], states[5]);
2723
Thomas Maier0a0fc962006-12-08 02:36:11 -08002724 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2725 pd->write_congestion_off,
2726 pd->write_congestion_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return 0;
2728}
2729
2730static int pkt_seq_open(struct inode *inode, struct file *file)
2731{
2732 return single_open(file, pkt_seq_show, PDE(inode)->data);
2733}
2734
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002735static const struct file_operations pkt_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 .open = pkt_seq_open,
2737 .read = seq_read,
2738 .llseek = seq_lseek,
2739 .release = single_release
2740};
2741
2742static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2743{
2744 int i;
2745 int ret = 0;
2746 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 struct block_device *bdev;
2748
2749 if (pd->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002750 printk(DRIVER_NAME": Recursive setup not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return -EBUSY;
2752 }
2753 for (i = 0; i < MAX_WRITERS; i++) {
2754 struct pktcdvd_device *pd2 = pkt_devs[i];
2755 if (!pd2)
2756 continue;
2757 if (pd2->bdev->bd_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002758 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return -EBUSY;
2760 }
2761 if (pd2->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002762 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return -EBUSY;
2764 }
2765 }
2766
2767 bdev = bdget(dev);
2768 if (!bdev)
2769 return -ENOMEM;
2770 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2771 if (ret)
2772 return ret;
2773
2774 /* This is safe, since we have a reference from open(). */
2775 __module_get(THIS_MODULE);
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 pd->bdev = bdev;
2778 set_blocksize(bdev, CD_FRAMESIZE);
2779
2780 pkt_init_queue(pd);
2781
2782 atomic_set(&pd->cdrw.pending_bios, 0);
2783 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2784 if (IS_ERR(pd->cdrw.thread)) {
Thomas Maier78220822006-10-04 02:15:28 -07002785 printk(DRIVER_NAME": can't start kernel thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 ret = -ENOMEM;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002787 goto out_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789
Denis V. Lunevc7705f32008-04-29 01:02:35 -07002790 proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
Thomas Maier78220822006-10-04 02:15:28 -07002791 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794out_mem:
2795 blkdev_put(bdev);
2796 /* This is safe: open() is still holding a reference. */
2797 module_put(THIS_MODULE);
2798 return ret;
2799}
2800
Alan Cox5b6155e2008-07-04 09:29:16 +02002801static long pkt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Alan Cox5b6155e2008-07-04 09:29:16 +02002803 struct inode *inode = file->f_path.dentry->d_inode;
2804 struct pktcdvd_device *pd;
2805 long ret;
2806
2807 lock_kernel();
2808 pd = inode->i_bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 switch (cmd) {
2813 /*
2814 * forward selected CDROM ioctls to CD-ROM, for UDF
2815 */
2816 case CDROMMULTISESSION:
2817 case CDROMREADTOCENTRY:
2818 case CDROM_LAST_WRITTEN:
2819 case CDROM_SEND_PACKET:
2820 case SCSI_IOCTL_SEND_COMMAND:
Alan Cox5b6155e2008-07-04 09:29:16 +02002821 ret = blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 case CDROMEJECT:
2825 /*
2826 * The door gets locked when the device is opened, so we
2827 * have to unlock it or else the eject command fails.
2828 */
Peter Osterlund948423e2006-02-14 13:52:56 -08002829 if (pd->refcnt == 1)
2830 pkt_lock_door(pd, 0);
Alan Cox5b6155e2008-07-04 09:29:16 +02002831 ret = blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
2832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 default:
Thomas Maier78220822006-10-04 02:15:28 -07002835 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
Alan Cox5b6155e2008-07-04 09:29:16 +02002836 ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
Alan Cox5b6155e2008-07-04 09:29:16 +02002838 unlock_kernel();
2839 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842static int pkt_media_changed(struct gendisk *disk)
2843{
2844 struct pktcdvd_device *pd = disk->private_data;
2845 struct gendisk *attached_disk;
2846
2847 if (!pd)
2848 return 0;
2849 if (!pd->bdev)
2850 return 0;
2851 attached_disk = pd->bdev->bd_disk;
2852 if (!attached_disk)
2853 return 0;
2854 return attached_disk->fops->media_changed(attached_disk);
2855}
2856
2857static struct block_device_operations pktcdvd_ops = {
2858 .owner = THIS_MODULE,
2859 .open = pkt_open,
2860 .release = pkt_close,
Alan Cox5b6155e2008-07-04 09:29:16 +02002861 .unlocked_ioctl = pkt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 .media_changed = pkt_media_changed,
2863};
2864
2865/*
2866 * Set up mapping from pktcdvd device to CD-ROM device.
2867 */
Thomas Maieradb92502006-12-08 02:36:10 -08002868static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 int idx;
2871 int ret = -ENOMEM;
2872 struct pktcdvd_device *pd;
2873 struct gendisk *disk;
Thomas Maieradb92502006-12-08 02:36:10 -08002874
2875 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 for (idx = 0; idx < MAX_WRITERS; idx++)
2878 if (!pkt_devs[idx])
2879 break;
2880 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002881 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
Thomas Maieradb92502006-12-08 02:36:10 -08002882 ret = -EBUSY;
2883 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885
Peter Osterlund1107d2e2005-09-13 01:25:29 -07002886 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!pd)
Thomas Maieradb92502006-12-08 02:36:10 -08002888 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002890 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2891 sizeof(struct pkt_rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (!pd->rb_pool)
2893 goto out_mem;
2894
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002895 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2896 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2897 spin_lock_init(&pd->cdrw.active_list_lock);
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 spin_lock_init(&pd->lock);
2900 spin_lock_init(&pd->iosched.lock);
Thomas Maier78220822006-10-04 02:15:28 -07002901 sprintf(pd->name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 init_waitqueue_head(&pd->wqueue);
2903 pd->bio_queue = RB_ROOT;
2904
Thomas Maier0a0fc962006-12-08 02:36:11 -08002905 pd->write_congestion_on = write_congestion_on;
2906 pd->write_congestion_off = write_congestion_off;
2907
Thomas Maieradb92502006-12-08 02:36:10 -08002908 disk = alloc_disk(1);
2909 if (!disk)
2910 goto out_mem;
2911 pd->disk = disk;
Thomas Maieradd21662006-10-04 02:15:30 -07002912 disk->major = pktdev_major;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 disk->first_minor = idx;
2914 disk->fops = &pktcdvd_ops;
2915 disk->flags = GENHD_FL_REMOVABLE;
Thomas Maieradb92502006-12-08 02:36:10 -08002916 strcpy(disk->disk_name, pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 disk->private_data = pd;
2918 disk->queue = blk_alloc_queue(GFP_KERNEL);
2919 if (!disk->queue)
2920 goto out_mem2;
2921
2922 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2923 ret = pkt_new_dev(pd, dev);
2924 if (ret)
2925 goto out_new_dev;
2926
2927 add_disk(disk);
Thomas Maieradb92502006-12-08 02:36:10 -08002928
Thomas Maier32694852006-12-08 02:36:12 -08002929 pkt_sysfs_dev_new(pd);
2930 pkt_debugfs_dev_new(pd);
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 pkt_devs[idx] = pd;
Thomas Maieradb92502006-12-08 02:36:10 -08002933 if (pkt_dev)
2934 *pkt_dev = pd->pkt_dev;
2935
2936 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return 0;
2938
2939out_new_dev:
Al Viro1312f402006-03-12 11:02:03 -05002940 blk_cleanup_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941out_mem2:
2942 put_disk(disk);
2943out_mem:
2944 if (pd->rb_pool)
2945 mempool_destroy(pd->rb_pool);
2946 kfree(pd);
Thomas Maieradb92502006-12-08 02:36:10 -08002947out_mutex:
2948 mutex_unlock(&ctl_mutex);
2949 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return ret;
2951}
2952
2953/*
2954 * Tear down mapping from pktcdvd device to CD-ROM device.
2955 */
Thomas Maieradb92502006-12-08 02:36:10 -08002956static int pkt_remove_dev(dev_t pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 struct pktcdvd_device *pd;
2959 int idx;
Thomas Maieradb92502006-12-08 02:36:10 -08002960 int ret = 0;
2961
2962 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
2964 for (idx = 0; idx < MAX_WRITERS; idx++) {
2965 pd = pkt_devs[idx];
2966 if (pd && (pd->pkt_dev == pkt_dev))
2967 break;
2968 }
2969 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002970 DPRINTK(DRIVER_NAME": dev not setup\n");
Thomas Maieradb92502006-12-08 02:36:10 -08002971 ret = -ENXIO;
2972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974
Thomas Maieradb92502006-12-08 02:36:10 -08002975 if (pd->refcnt > 0) {
2976 ret = -EBUSY;
2977 goto out;
2978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!IS_ERR(pd->cdrw.thread))
2980 kthread_stop(pd->cdrw.thread);
2981
Thomas Maier32694852006-12-08 02:36:12 -08002982 pkt_devs[idx] = NULL;
2983
2984 pkt_debugfs_dev_remove(pd);
2985 pkt_sysfs_dev_remove(pd);
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 blkdev_put(pd->bdev);
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 remove_proc_entry(pd->name, pkt_proc);
Thomas Maier78220822006-10-04 02:15:28 -07002990 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 del_gendisk(pd->disk);
Al Viro1312f402006-03-12 11:02:03 -05002993 blk_cleanup_queue(pd->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 put_disk(pd->disk);
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 mempool_destroy(pd->rb_pool);
2997 kfree(pd);
2998
2999 /* This is safe: open() is still holding a reference. */
3000 module_put(THIS_MODULE);
Thomas Maieradb92502006-12-08 02:36:10 -08003001
3002out:
3003 mutex_unlock(&ctl_mutex);
3004 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
3007static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
3008{
Thomas Maieradb92502006-12-08 02:36:10 -08003009 struct pktcdvd_device *pd;
3010
3011 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3012
3013 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (pd) {
3015 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
3016 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
3017 } else {
3018 ctrl_cmd->dev = 0;
3019 ctrl_cmd->pkt_dev = 0;
3020 }
3021 ctrl_cmd->num_devices = MAX_WRITERS;
Thomas Maieradb92502006-12-08 02:36:10 -08003022
3023 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
Alan Cox5b6155e2008-07-04 09:29:16 +02003026static long pkt_ctl_ioctl(struct file *file, unsigned int cmd,
3027 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 void __user *argp = (void __user *)arg;
3030 struct pkt_ctrl_command ctrl_cmd;
3031 int ret = 0;
Thomas Maieradb92502006-12-08 02:36:10 -08003032 dev_t pkt_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 if (cmd != PACKET_CTRL_CMD)
3035 return -ENOTTY;
3036
3037 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3038 return -EFAULT;
3039
3040 switch (ctrl_cmd.command) {
3041 case PKT_CTRL_CMD_SETUP:
3042 if (!capable(CAP_SYS_ADMIN))
3043 return -EPERM;
Alan Cox5b6155e2008-07-04 09:29:16 +02003044 lock_kernel();
Thomas Maieradb92502006-12-08 02:36:10 -08003045 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3046 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
Alan Cox5b6155e2008-07-04 09:29:16 +02003047 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 break;
3049 case PKT_CTRL_CMD_TEARDOWN:
3050 if (!capable(CAP_SYS_ADMIN))
3051 return -EPERM;
Alan Cox5b6155e2008-07-04 09:29:16 +02003052 lock_kernel();
Thomas Maieradb92502006-12-08 02:36:10 -08003053 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
Alan Cox5b6155e2008-07-04 09:29:16 +02003054 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 break;
3056 case PKT_CTRL_CMD_STATUS:
Alan Cox5b6155e2008-07-04 09:29:16 +02003057 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 pkt_get_status(&ctrl_cmd);
Alan Cox5b6155e2008-07-04 09:29:16 +02003059 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 break;
3061 default:
3062 return -ENOTTY;
3063 }
3064
3065 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3066 return -EFAULT;
3067 return ret;
3068}
3069
3070
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003071static const struct file_operations pkt_ctl_fops = {
Alan Cox5b6155e2008-07-04 09:29:16 +02003072 .unlocked_ioctl = pkt_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 .owner = THIS_MODULE,
3074};
3075
3076static struct miscdevice pkt_misc = {
3077 .minor = MISC_DYNAMIC_MINOR,
Thomas Maier78220822006-10-04 02:15:28 -07003078 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 .fops = &pkt_ctl_fops
3080};
3081
3082static int __init pkt_init(void)
3083{
3084 int ret;
3085
Thomas Maier32694852006-12-08 02:36:12 -08003086 mutex_init(&ctl_mutex);
3087
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08003088 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3089 sizeof(struct packet_stacked_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (!psd_pool)
3091 return -ENOMEM;
3092
Thomas Maieradd21662006-10-04 02:15:30 -07003093 ret = register_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (ret < 0) {
Thomas Maier78220822006-10-04 02:15:28 -07003095 printk(DRIVER_NAME": Unable to register block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 goto out2;
3097 }
Thomas Maieradd21662006-10-04 02:15:30 -07003098 if (!pktdev_major)
3099 pktdev_major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Thomas Maier32694852006-12-08 02:36:12 -08003101 ret = pkt_sysfs_init();
3102 if (ret)
3103 goto out;
3104
3105 pkt_debugfs_init();
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 ret = misc_register(&pkt_misc);
3108 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07003109 printk(DRIVER_NAME": Unable to register misc device\n");
Thomas Maier32694852006-12-08 02:36:12 -08003110 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112
Alexey Dobriyan928b4d82008-04-29 01:01:44 -07003113 pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return 0;
3116
Thomas Maier32694852006-12-08 02:36:12 -08003117out_misc:
3118 pkt_debugfs_cleanup();
3119 pkt_sysfs_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120out:
Thomas Maieradd21662006-10-04 02:15:30 -07003121 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122out2:
3123 mempool_destroy(psd_pool);
3124 return ret;
3125}
3126
3127static void __exit pkt_exit(void)
3128{
Alexey Dobriyan928b4d82008-04-29 01:01:44 -07003129 remove_proc_entry("driver/"DRIVER_NAME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 misc_deregister(&pkt_misc);
Thomas Maier32694852006-12-08 02:36:12 -08003131
3132 pkt_debugfs_cleanup();
3133 pkt_sysfs_cleanup();
3134
Thomas Maieradd21662006-10-04 02:15:30 -07003135 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 mempool_destroy(psd_pool);
3137}
3138
3139MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3140MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3141MODULE_LICENSE("GPL");
3142
3143module_init(pkt_init);
3144module_exit(pkt_exit);