blob: b72935b8f2036e3ea954b5560fe88fd3875f5d96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
Thomas Maieradb92502006-12-08 02:36:10 -08004 * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * May be copied or modified under the terms of the GNU General Public
7 * License. See linux/COPYING for more information.
8 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -07009 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
10 * DVD-RAM devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Theory of operation:
13 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070014 * At the lowest level, there is the standard driver for the CD/DVD device,
15 * typically ide-cd.c or sr.c. This driver can handle read and write requests,
16 * but it doesn't know anything about the special restrictions that apply to
17 * packet writing. One restriction is that write requests must be aligned to
18 * packet boundaries on the physical media, and the size of a write request
19 * must be equal to the packet size. Another restriction is that a
20 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
21 * command, if the previous command was a write.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070023 * The purpose of the packet writing driver is to hide these restrictions from
24 * higher layers, such as file systems, and present a block device that can be
25 * randomly read and written using 2kB-sized blocks.
26 *
27 * The lowest layer in the packet writing driver is the packet I/O scheduler.
28 * Its data is defined by the struct packet_iosched and includes two bio
29 * queues with pending read and write requests. These queues are processed
30 * by the pkt_iosched_process_queue() function. The write requests in this
31 * queue are already properly aligned and sized. This layer is responsible for
32 * issuing the flush cache commands and scheduling the I/O in a good order.
33 *
34 * The next layer transforms unaligned write requests to aligned writes. This
35 * transformation requires reading missing pieces of data from the underlying
36 * block device, assembling the pieces to full packets and queuing them to the
37 * packet I/O scheduler.
38 *
39 * At the top layer there is a custom make_request_fn function that forwards
40 * read requests directly to the iosched queue and puts write requests in the
41 * unaligned write queue. A kernel thread performs the necessary read
42 * gathering to convert the unaligned writes to aligned writes and then feeds
43 * them to the packet I/O scheduler.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 *************************************************************************/
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/pktcdvd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/module.h>
49#include <linux/types.h>
50#include <linux/kernel.h>
51#include <linux/kthread.h>
52#include <linux/errno.h>
53#include <linux/spinlock.h>
54#include <linux/file.h>
55#include <linux/proc_fs.h>
56#include <linux/seq_file.h>
57#include <linux/miscdevice.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080058#include <linux/freezer.h>
Jes Sorensen1657f822006-03-23 03:00:25 -080059#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_ioctl.h>
Peter Osterlundcef28962006-02-20 18:28:01 -080062#include <scsi/scsi.h>
Thomas Maier32694852006-12-08 02:36:12 -080063#include <linux/debugfs.h>
64#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/uaccess.h>
67
Thomas Maier78220822006-10-04 02:15:28 -070068#define DRIVER_NAME "pktcdvd"
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#if PACKET_DEBUG
71#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
72#else
73#define DPRINTK(fmt, args...)
74#endif
75
76#if PACKET_DEBUG > 1
77#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
78#else
79#define VPRINTK(fmt, args...)
80#endif
81
82#define MAX_SPEED 0xffff
83
84#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
85
86static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
87static struct proc_dir_entry *pkt_proc;
Thomas Maieradd21662006-10-04 02:15:30 -070088static int pktdev_major;
Thomas Maier0a0fc962006-12-08 02:36:11 -080089static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
90static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
Jes Sorensen1657f822006-03-23 03:00:25 -080091static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static mempool_t *psd_pool;
93
Thomas Maier32694852006-12-08 02:36:12 -080094static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
GeunSik Limea5ffff2009-09-07 21:39:25 +090095static struct dentry *pkt_debugfs_root = NULL; /* /sys/kernel/debug/pktcdvd */
Thomas Maier32694852006-12-08 02:36:12 -080096
97/* forward declaration */
98static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
99static int pkt_remove_dev(dev_t pkt_dev);
100static int pkt_seq_show(struct seq_file *m, void *p);
101
102
103
104/*
105 * create and register a pktcdvd kernel object.
106 */
107static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
108 const char* name,
109 struct kobject* parent,
110 struct kobj_type* ktype)
111{
112 struct pktcdvd_kobj *p;
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400113 int error;
114
Thomas Maier32694852006-12-08 02:36:12 -0800115 p = kzalloc(sizeof(*p), GFP_KERNEL);
116 if (!p)
117 return NULL;
Thomas Maier32694852006-12-08 02:36:12 -0800118 p->pd = pd;
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400119 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
120 if (error) {
Dave Youngd17a18d2007-12-17 16:20:00 -0800121 kobject_put(&p->kobj);
Thomas Maier32694852006-12-08 02:36:12 -0800122 return NULL;
Dave Youngd17a18d2007-12-17 16:20:00 -0800123 }
Greg Kroah-Hartman89c42602007-12-17 15:54:39 -0400124 kobject_uevent(&p->kobj, KOBJ_ADD);
Thomas Maier32694852006-12-08 02:36:12 -0800125 return p;
126}
127/*
128 * remove a pktcdvd kernel object.
129 */
130static void pkt_kobj_remove(struct pktcdvd_kobj *p)
131{
132 if (p)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800133 kobject_put(&p->kobj);
Thomas Maier32694852006-12-08 02:36:12 -0800134}
135/*
136 * default release function for pktcdvd kernel objects.
137 */
138static void pkt_kobj_release(struct kobject *kobj)
139{
140 kfree(to_pktcdvdkobj(kobj));
141}
142
143
144/**********************************************************
145 *
146 * sysfs interface for pktcdvd
147 * by (C) 2006 Thomas Maier <balagi@justmail.de>
148 *
149 **********************************************************/
150
151#define DEF_ATTR(_obj,_name,_mode) \
Tejun Heo7b595752007-06-14 03:45:17 +0900152 static struct attribute _obj = { .name = _name, .mode = _mode }
Thomas Maier32694852006-12-08 02:36:12 -0800153
154/**********************************************************
155 /sys/class/pktcdvd/pktcdvd[0-7]/
156 stat/reset
157 stat/packets_started
158 stat/packets_finished
159 stat/kb_written
160 stat/kb_read
161 stat/kb_read_gather
162 write_queue/size
163 write_queue/congestion_off
164 write_queue/congestion_on
165 **********************************************************/
166
167DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
168DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
169DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
170DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
171DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
172DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
173
174static struct attribute *kobj_pkt_attrs_stat[] = {
175 &kobj_pkt_attr_st1,
176 &kobj_pkt_attr_st2,
177 &kobj_pkt_attr_st3,
178 &kobj_pkt_attr_st4,
179 &kobj_pkt_attr_st5,
180 &kobj_pkt_attr_st6,
181 NULL
182};
183
184DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
185DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
186DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
187
188static struct attribute *kobj_pkt_attrs_wqueue[] = {
189 &kobj_pkt_attr_wq1,
190 &kobj_pkt_attr_wq2,
191 &kobj_pkt_attr_wq3,
192 NULL
193};
194
Thomas Maier32694852006-12-08 02:36:12 -0800195static ssize_t kobj_pkt_show(struct kobject *kobj,
196 struct attribute *attr, char *data)
197{
198 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
199 int n = 0;
200 int v;
201 if (strcmp(attr->name, "packets_started") == 0) {
202 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
203
204 } else if (strcmp(attr->name, "packets_finished") == 0) {
205 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
206
207 } else if (strcmp(attr->name, "kb_written") == 0) {
208 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
209
210 } else if (strcmp(attr->name, "kb_read") == 0) {
211 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
212
213 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
214 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
215
216 } else if (strcmp(attr->name, "size") == 0) {
217 spin_lock(&pd->lock);
218 v = pd->bio_queue_size;
219 spin_unlock(&pd->lock);
220 n = sprintf(data, "%d\n", v);
221
222 } else if (strcmp(attr->name, "congestion_off") == 0) {
223 spin_lock(&pd->lock);
224 v = pd->write_congestion_off;
225 spin_unlock(&pd->lock);
226 n = sprintf(data, "%d\n", v);
227
228 } else if (strcmp(attr->name, "congestion_on") == 0) {
229 spin_lock(&pd->lock);
230 v = pd->write_congestion_on;
231 spin_unlock(&pd->lock);
232 n = sprintf(data, "%d\n", v);
233 }
234 return n;
235}
236
237static void init_write_congestion_marks(int* lo, int* hi)
238{
239 if (*hi > 0) {
240 *hi = max(*hi, 500);
241 *hi = min(*hi, 1000000);
242 if (*lo <= 0)
243 *lo = *hi - 100;
244 else {
245 *lo = min(*lo, *hi - 100);
246 *lo = max(*lo, 100);
247 }
248 } else {
249 *hi = -1;
250 *lo = -1;
251 }
252}
253
254static ssize_t kobj_pkt_store(struct kobject *kobj,
255 struct attribute *attr,
256 const char *data, size_t len)
257{
258 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
259 int val;
Thomas Maier32694852006-12-08 02:36:12 -0800260
Thomas Maier83f3aa32007-02-10 01:45:11 -0800261 if (strcmp(attr->name, "reset") == 0 && len > 0) {
Thomas Maier32694852006-12-08 02:36:12 -0800262 pd->stats.pkt_started = 0;
263 pd->stats.pkt_ended = 0;
264 pd->stats.secs_w = 0;
265 pd->stats.secs_rg = 0;
266 pd->stats.secs_r = 0;
267
268 } else if (strcmp(attr->name, "congestion_off") == 0
Thomas Maier83f3aa32007-02-10 01:45:11 -0800269 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800270 spin_lock(&pd->lock);
271 pd->write_congestion_off = val;
272 init_write_congestion_marks(&pd->write_congestion_off,
273 &pd->write_congestion_on);
274 spin_unlock(&pd->lock);
275
276 } else if (strcmp(attr->name, "congestion_on") == 0
Thomas Maier83f3aa32007-02-10 01:45:11 -0800277 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800278 spin_lock(&pd->lock);
279 pd->write_congestion_on = val;
280 init_write_congestion_marks(&pd->write_congestion_off,
281 &pd->write_congestion_on);
282 spin_unlock(&pd->lock);
283 }
284 return len;
285}
286
287static struct sysfs_ops kobj_pkt_ops = {
288 .show = kobj_pkt_show,
289 .store = kobj_pkt_store
290};
291static struct kobj_type kobj_pkt_type_stat = {
292 .release = pkt_kobj_release,
293 .sysfs_ops = &kobj_pkt_ops,
294 .default_attrs = kobj_pkt_attrs_stat
295};
296static struct kobj_type kobj_pkt_type_wqueue = {
297 .release = pkt_kobj_release,
298 .sysfs_ops = &kobj_pkt_ops,
299 .default_attrs = kobj_pkt_attrs_wqueue
300};
301
302static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
303{
304 if (class_pktcdvd) {
Kay Sieverscba76712008-12-06 04:38:11 +0100305 pd->dev = device_create(class_pktcdvd, NULL, MKDEV(0, 0), NULL,
Greg Kroah-Hartman1ff9f542008-07-21 20:03:34 -0700306 "%s", pd->name);
Tony Jones6013c122007-09-25 02:03:03 +0200307 if (IS_ERR(pd->dev))
308 pd->dev = NULL;
Thomas Maier32694852006-12-08 02:36:12 -0800309 }
Tony Jones6013c122007-09-25 02:03:03 +0200310 if (pd->dev) {
Thomas Maier32694852006-12-08 02:36:12 -0800311 pd->kobj_stat = pkt_kobj_create(pd, "stat",
Tony Jones6013c122007-09-25 02:03:03 +0200312 &pd->dev->kobj,
Thomas Maier32694852006-12-08 02:36:12 -0800313 &kobj_pkt_type_stat);
314 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
Tony Jones6013c122007-09-25 02:03:03 +0200315 &pd->dev->kobj,
Thomas Maier32694852006-12-08 02:36:12 -0800316 &kobj_pkt_type_wqueue);
317 }
318}
319
320static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
321{
322 pkt_kobj_remove(pd->kobj_stat);
323 pkt_kobj_remove(pd->kobj_wqueue);
324 if (class_pktcdvd)
Thadeu Lima de Souza Cascardoca0bf642010-02-02 13:44:17 -0800325 device_unregister(pd->dev);
Thomas Maier32694852006-12-08 02:36:12 -0800326}
327
328
329/********************************************************************
330 /sys/class/pktcdvd/
331 add map block device
332 remove unmap packet dev
333 device_map show mappings
334 *******************************************************************/
335
336static void class_pktcdvd_release(struct class *cls)
337{
338 kfree(cls);
339}
340static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
341{
342 int n = 0;
343 int idx;
344 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
345 for (idx = 0; idx < MAX_WRITERS; idx++) {
346 struct pktcdvd_device *pd = pkt_devs[idx];
347 if (!pd)
348 continue;
349 n += sprintf(data+n, "%s %u:%u %u:%u\n",
350 pd->name,
351 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
352 MAJOR(pd->bdev->bd_dev),
353 MINOR(pd->bdev->bd_dev));
354 }
355 mutex_unlock(&ctl_mutex);
356 return n;
357}
358
359static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
360 size_t count)
361{
362 unsigned int major, minor;
Tejun Heofffe4872007-11-08 08:00:24 +0100363
Thomas Maier83f3aa32007-02-10 01:45:11 -0800364 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Tejun Heofffe4872007-11-08 08:00:24 +0100365 /* pkt_setup_dev() expects caller to hold reference to self */
366 if (!try_module_get(THIS_MODULE))
367 return -ENODEV;
368
Thomas Maier32694852006-12-08 02:36:12 -0800369 pkt_setup_dev(MKDEV(major, minor), NULL);
Tejun Heofffe4872007-11-08 08:00:24 +0100370
371 module_put(THIS_MODULE);
372
Thomas Maier32694852006-12-08 02:36:12 -0800373 return count;
374 }
Tejun Heofffe4872007-11-08 08:00:24 +0100375
Thomas Maier32694852006-12-08 02:36:12 -0800376 return -EINVAL;
377}
378
379static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
380 size_t count)
381{
382 unsigned int major, minor;
Thomas Maier83f3aa32007-02-10 01:45:11 -0800383 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Thomas Maier32694852006-12-08 02:36:12 -0800384 pkt_remove_dev(MKDEV(major, minor));
385 return count;
386 }
387 return -EINVAL;
388}
389
390static struct class_attribute class_pktcdvd_attrs[] = {
391 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
392 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
393 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
394 __ATTR_NULL
395};
396
397
398static int pkt_sysfs_init(void)
399{
400 int ret = 0;
401
402 /*
403 * create control files in sysfs
404 * /sys/class/pktcdvd/...
405 */
406 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
407 if (!class_pktcdvd)
408 return -ENOMEM;
409 class_pktcdvd->name = DRIVER_NAME;
410 class_pktcdvd->owner = THIS_MODULE;
411 class_pktcdvd->class_release = class_pktcdvd_release;
412 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
413 ret = class_register(class_pktcdvd);
414 if (ret) {
415 kfree(class_pktcdvd);
416 class_pktcdvd = NULL;
417 printk(DRIVER_NAME": failed to create class pktcdvd\n");
418 return ret;
419 }
420 return 0;
421}
422
423static void pkt_sysfs_cleanup(void)
424{
425 if (class_pktcdvd)
426 class_destroy(class_pktcdvd);
427 class_pktcdvd = NULL;
428}
429
430/********************************************************************
431 entries in debugfs
432
GeunSik Lim156f5a72009-06-02 15:01:37 +0900433 /sys/kernel/debug/pktcdvd[0-7]/
Thomas Maier32694852006-12-08 02:36:12 -0800434 info
435
436 *******************************************************************/
437
438static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
439{
440 return pkt_seq_show(m, p);
441}
442
443static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
444{
445 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
446}
447
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800448static const struct file_operations debug_fops = {
Thomas Maier32694852006-12-08 02:36:12 -0800449 .open = pkt_debugfs_fops_open,
450 .read = seq_read,
451 .llseek = seq_lseek,
452 .release = single_release,
453 .owner = THIS_MODULE,
454};
455
456static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
457{
458 if (!pkt_debugfs_root)
459 return;
460 pd->dfs_f_info = NULL;
461 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
462 if (IS_ERR(pd->dfs_d_root)) {
463 pd->dfs_d_root = NULL;
464 return;
465 }
466 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
467 pd->dfs_d_root, pd, &debug_fops);
468 if (IS_ERR(pd->dfs_f_info)) {
469 pd->dfs_f_info = NULL;
470 return;
471 }
472}
473
474static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
475{
476 if (!pkt_debugfs_root)
477 return;
478 if (pd->dfs_f_info)
479 debugfs_remove(pd->dfs_f_info);
480 pd->dfs_f_info = NULL;
481 if (pd->dfs_d_root)
482 debugfs_remove(pd->dfs_d_root);
483 pd->dfs_d_root = NULL;
484}
485
486static void pkt_debugfs_init(void)
487{
488 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
489 if (IS_ERR(pkt_debugfs_root)) {
490 pkt_debugfs_root = NULL;
491 return;
492 }
493}
494
495static void pkt_debugfs_cleanup(void)
496{
497 if (!pkt_debugfs_root)
498 return;
499 debugfs_remove(pkt_debugfs_root);
500 pkt_debugfs_root = NULL;
501}
502
503/* ----------------------------------------------------------*/
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506static void pkt_bio_finished(struct pktcdvd_device *pd)
507{
508 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
509 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
Thomas Maier78220822006-10-04 02:15:28 -0700510 VPRINTK(DRIVER_NAME": queue empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 atomic_set(&pd->iosched.attention, 1);
512 wake_up(&pd->wqueue);
513 }
514}
515
516static void pkt_bio_destructor(struct bio *bio)
517{
518 kfree(bio->bi_io_vec);
519 kfree(bio);
520}
521
522static struct bio *pkt_bio_alloc(int nr_iovecs)
523{
524 struct bio_vec *bvl = NULL;
525 struct bio *bio;
526
527 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
528 if (!bio)
529 goto no_bio;
530 bio_init(bio);
531
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700532 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!bvl)
534 goto no_bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 bio->bi_max_vecs = nr_iovecs;
537 bio->bi_io_vec = bvl;
538 bio->bi_destructor = pkt_bio_destructor;
539
540 return bio;
541
542 no_bvl:
543 kfree(bio);
544 no_bio:
545 return NULL;
546}
547
548/*
549 * Allocate a packet_data struct
550 */
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800551static struct packet_data *pkt_alloc_packet_data(int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 int i;
554 struct packet_data *pkt;
555
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700556 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!pkt)
558 goto no_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800560 pkt->frames = frames;
561 pkt->w_bio = pkt_bio_alloc(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (!pkt->w_bio)
563 goto no_bio;
564
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800565 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
567 if (!pkt->pages[i])
568 goto no_page;
569 }
570
571 spin_lock_init(&pkt->lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +0100572 bio_list_init(&pkt->orig_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800574 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct bio *bio = pkt_bio_alloc(1);
576 if (!bio)
577 goto no_rd_bio;
578 pkt->r_bios[i] = bio;
579 }
580
581 return pkt;
582
583no_rd_bio:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800584 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct bio *bio = pkt->r_bios[i];
586 if (bio)
587 bio_put(bio);
588 }
589
590no_page:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800591 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (pkt->pages[i])
593 __free_page(pkt->pages[i]);
594 bio_put(pkt->w_bio);
595no_bio:
596 kfree(pkt);
597no_pkt:
598 return NULL;
599}
600
601/*
602 * Free a packet_data struct
603 */
604static void pkt_free_packet_data(struct packet_data *pkt)
605{
606 int i;
607
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800608 for (i = 0; i < pkt->frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct bio *bio = pkt->r_bios[i];
610 if (bio)
611 bio_put(bio);
612 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800613 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 __free_page(pkt->pages[i]);
615 bio_put(pkt->w_bio);
616 kfree(pkt);
617}
618
619static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
620{
621 struct packet_data *pkt, *next;
622
623 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
624
625 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
626 pkt_free_packet_data(pkt);
627 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800628 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
632{
633 struct packet_data *pkt;
634
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800635 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 while (nr_packets > 0) {
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800638 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!pkt) {
640 pkt_shrink_pktlist(pd);
641 return 0;
642 }
643 pkt->id = nr_packets;
644 pkt->pd = pd;
645 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
646 nr_packets--;
647 }
648 return 1;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
652{
653 struct rb_node *n = rb_next(&node->rb_node);
654 if (!n)
655 return NULL;
656 return rb_entry(n, struct pkt_rb_node, rb_node);
657}
658
Peter Osterlundac893962006-01-14 13:21:32 -0800659static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 rb_erase(&node->rb_node, &pd->bio_queue);
662 mempool_free(node, pd->rb_pool);
663 pd->bio_queue_size--;
664 BUG_ON(pd->bio_queue_size < 0);
665}
666
667/*
668 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
669 */
670static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
671{
672 struct rb_node *n = pd->bio_queue.rb_node;
673 struct rb_node *next;
674 struct pkt_rb_node *tmp;
675
676 if (!n) {
677 BUG_ON(pd->bio_queue_size > 0);
678 return NULL;
679 }
680
681 for (;;) {
682 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
683 if (s <= tmp->bio->bi_sector)
684 next = n->rb_left;
685 else
686 next = n->rb_right;
687 if (!next)
688 break;
689 n = next;
690 }
691
692 if (s > tmp->bio->bi_sector) {
693 tmp = pkt_rbtree_next(tmp);
694 if (!tmp)
695 return NULL;
696 }
697 BUG_ON(s > tmp->bio->bi_sector);
698 return tmp;
699}
700
701/*
702 * Insert a node into the pd->bio_queue rb tree.
703 */
704static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
705{
706 struct rb_node **p = &pd->bio_queue.rb_node;
707 struct rb_node *parent = NULL;
708 sector_t s = node->bio->bi_sector;
709 struct pkt_rb_node *tmp;
710
711 while (*p) {
712 parent = *p;
713 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
714 if (s < tmp->bio->bi_sector)
715 p = &(*p)->rb_left;
716 else
717 p = &(*p)->rb_right;
718 }
719 rb_link_node(&node->rb_node, parent, p);
720 rb_insert_color(&node->rb_node, &pd->bio_queue);
721 pd->bio_queue_size++;
722}
723
724/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * Send a packet_command to the underlying block device and
726 * wait for completion.
727 */
728static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
729{
Jens Axboe165125e2007-07-24 09:28:11 +0200730 struct request_queue *q = bdev_get_queue(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct request *rq;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800732 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800734 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
735 WRITE : READ, __GFP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800737 if (cgc->buflen) {
738 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
739 goto out;
740 }
741
Gerhard Dirschl91e4ee32007-02-20 13:57:56 -0800742 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800743 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rq->timeout = 60*HZ;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200746 rq->cmd_type = REQ_TYPE_BLOCK_PC;
747 rq->cmd_flags |= REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (cgc->quiet)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200749 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800751 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
Andrew Mortoncbc31a42007-04-25 13:01:21 -0700752 if (rq->errors)
753 ret = -EIO;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800754out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 blk_put_request(rq);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
760 * A generic sense dump / resolve mechanism should be implemented across
761 * all ATAPI + SCSI devices.
762 */
763static void pkt_dump_sense(struct packet_command *cgc)
764{
765 static char *info[9] = { "No sense", "Recovered error", "Not ready",
766 "Medium error", "Hardware error", "Illegal request",
767 "Unit attention", "Data protect", "Blank check" };
768 int i;
769 struct request_sense *sense = cgc->sense;
770
Thomas Maier78220822006-10-04 02:15:28 -0700771 printk(DRIVER_NAME":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 for (i = 0; i < CDROM_PACKET_SIZE; i++)
773 printk(" %02x", cgc->cmd[i]);
774 printk(" - ");
775
776 if (sense == NULL) {
777 printk("no sense\n");
778 return;
779 }
780
781 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
782
783 if (sense->sense_key > 8) {
784 printk(" (INVALID)\n");
785 return;
786 }
787
788 printk(" (%s)\n", info[sense->sense_key]);
789}
790
791/*
792 * flush the drive cache to media
793 */
794static int pkt_flush_cache(struct pktcdvd_device *pd)
795{
796 struct packet_command cgc;
797
798 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
799 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
800 cgc.quiet = 1;
801
802 /*
803 * the IMMED bit -- we default to not setting it, although that
804 * would allow a much faster close, this is safer
805 */
806#if 0
807 cgc.cmd[1] = 1 << 1;
808#endif
809 return pkt_generic_packet(pd, &cgc);
810}
811
812/*
813 * speed is given as the normal factor, e.g. 4 for 4x
814 */
Peter Osterlund05680d82008-03-04 14:28:41 -0800815static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
816 unsigned write_speed, unsigned read_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 struct packet_command cgc;
819 struct request_sense sense;
820 int ret;
821
822 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
823 cgc.sense = &sense;
824 cgc.cmd[0] = GPCMD_SET_SPEED;
825 cgc.cmd[2] = (read_speed >> 8) & 0xff;
826 cgc.cmd[3] = read_speed & 0xff;
827 cgc.cmd[4] = (write_speed >> 8) & 0xff;
828 cgc.cmd[5] = write_speed & 0xff;
829
830 if ((ret = pkt_generic_packet(pd, &cgc)))
831 pkt_dump_sense(&cgc);
832
833 return ret;
834}
835
836/*
837 * Queue a bio for processing by the low-level CD device. Must be called
838 * from process context.
839 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700840static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 spin_lock(&pd->iosched.lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +0100843 if (bio_data_dir(bio) == READ)
844 bio_list_add(&pd->iosched.read_queue, bio);
845 else
846 bio_list_add(&pd->iosched.write_queue, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 spin_unlock(&pd->iosched.lock);
848
849 atomic_set(&pd->iosched.attention, 1);
850 wake_up(&pd->wqueue);
851}
852
853/*
854 * Process the queued read/write requests. This function handles special
855 * requirements for CDRW drives:
856 * - A cache flush command must be inserted before a read request if the
857 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700858 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700860 * - Optimize for throughput at the expense of latency. This means that streaming
861 * writes will never be interrupted by a read, but if the drive has to seek
862 * before the next write, switch to reading instead if there are any pending
863 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * - Set the read speed according to current usage pattern. When only reading
865 * from the device, it's best to use the highest possible read speed, but
866 * when switching often between reading and writing, it's better to have the
867 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
870{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (atomic_read(&pd->iosched.attention) == 0)
873 return;
874 atomic_set(&pd->iosched.attention, 0);
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 for (;;) {
877 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700878 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 spin_lock(&pd->iosched.lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +0100881 reads_queued = !bio_list_empty(&pd->iosched.read_queue);
882 writes_queued = !bio_list_empty(&pd->iosched.write_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 spin_unlock(&pd->iosched.lock);
884
885 if (!reads_queued && !writes_queued)
886 break;
887
888 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700889 int need_write_seek = 1;
890 spin_lock(&pd->iosched.lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +0100891 bio = bio_list_peek(&pd->iosched.write_queue);
Peter Osterlund46c271b2005-06-23 00:10:02 -0700892 spin_unlock(&pd->iosched.lock);
893 if (bio && (bio->bi_sector == pd->iosched.last_write))
894 need_write_seek = 0;
895 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700897 VPRINTK(DRIVER_NAME": write, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 }
900 pkt_flush_cache(pd);
901 pd->iosched.writing = 0;
902 }
903 } else {
904 if (!reads_queued && writes_queued) {
905 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700906 VPRINTK(DRIVER_NAME": read, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 break;
908 }
909 pd->iosched.writing = 1;
910 }
911 }
912
913 spin_lock(&pd->iosched.lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +0100914 if (pd->iosched.writing)
915 bio = bio_list_pop(&pd->iosched.write_queue);
916 else
917 bio = bio_list_pop(&pd->iosched.read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 spin_unlock(&pd->iosched.lock);
919
920 if (!bio)
921 continue;
922
923 if (bio_data_dir(bio) == READ)
924 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700925 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700927 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
930 if (pd->read_speed == pd->write_speed) {
931 pd->read_speed = MAX_SPEED;
932 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
933 }
934 } else {
935 if (pd->read_speed != pd->write_speed) {
936 pd->read_speed = pd->write_speed;
937 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
938 }
939 }
940
941 atomic_inc(&pd->cdrw.pending_bios);
942 generic_make_request(bio);
943 }
944}
945
946/*
947 * Special care is needed if the underlying block device has a small
948 * max_phys_segments value.
949 */
Jens Axboe165125e2007-07-24 09:28:11 +0200950static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400952 if ((pd->settings.size << 9) / CD_FRAMESIZE
Martin K. Petersen8a783622010-02-26 00:20:39 -0500953 <= queue_max_segments(q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * The cdrom device can handle one segment/frame
956 */
957 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
958 return 0;
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400959 } else if ((pd->settings.size << 9) / PAGE_SIZE
Martin K. Petersen8a783622010-02-26 00:20:39 -0500960 <= queue_max_segments(q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /*
962 * We can handle this case at the expense of some extra memory
963 * copies during write operations
964 */
965 set_bit(PACKET_MERGE_SEGS, &pd->flags);
966 return 0;
967 } else {
Thomas Maier78220822006-10-04 02:15:28 -0700968 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -EIO;
970 }
971}
972
973/*
974 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
975 */
976static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
977{
978 unsigned int copy_size = CD_FRAMESIZE;
979
980 while (copy_size > 0) {
981 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
982 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
983 src_bvl->bv_offset + offs;
984 void *vto = page_address(dst_page) + dst_offs;
985 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
986
987 BUG_ON(len < 0);
988 memcpy(vto, vfrom, len);
989 kunmap_atomic(vfrom, KM_USER0);
990
991 seg++;
992 offs = 0;
993 dst_offs += len;
994 copy_size -= len;
995 }
996}
997
998/*
999 * Copy all data for this packet to pkt->pages[], so that
1000 * a) The number of required segments for the write bio is minimized, which
1001 * is necessary for some scsi controllers.
1002 * b) The data can be used as cache to avoid read requests if we receive a
1003 * new write request for the same zone.
1004 */
Peter Osterlund72772322006-02-14 13:52:56 -08001005static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 int f, p, offs;
1008
1009 /* Copy all data to pkt->pages[] */
1010 p = 0;
1011 offs = 0;
1012 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001013 if (bvec[f].bv_page != pkt->pages[p]) {
1014 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 void *vto = page_address(pkt->pages[p]) + offs;
1016 memcpy(vto, vfrom, CD_FRAMESIZE);
1017 kunmap_atomic(vfrom, KM_USER0);
Peter Osterlund72772322006-02-14 13:52:56 -08001018 bvec[f].bv_page = pkt->pages[p];
1019 bvec[f].bv_offset = offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else {
Peter Osterlund72772322006-02-14 13:52:56 -08001021 BUG_ON(bvec[f].bv_offset != offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 offs += CD_FRAMESIZE;
1024 if (offs >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 offs = 0;
1026 p++;
1027 }
1028 }
1029}
1030
NeilBrown6712ecf2007-09-27 12:47:43 +02001031static void pkt_end_io_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 struct packet_data *pkt = bio->bi_private;
1034 struct pktcdvd_device *pd = pkt->pd;
1035 BUG_ON(!pd);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1038 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1039
1040 if (err)
1041 atomic_inc(&pkt->io_errors);
1042 if (atomic_dec_and_test(&pkt->io_wait)) {
1043 atomic_inc(&pkt->run_sm);
1044 wake_up(&pd->wqueue);
1045 }
1046 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
NeilBrown6712ecf2007-09-27 12:47:43 +02001049static void pkt_end_io_packet_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 struct packet_data *pkt = bio->bi_private;
1052 struct pktcdvd_device *pd = pkt->pd;
1053 BUG_ON(!pd);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1056
1057 pd->stats.pkt_ended++;
1058
1059 pkt_bio_finished(pd);
1060 atomic_dec(&pkt->io_wait);
1061 atomic_inc(&pkt->run_sm);
1062 wake_up(&pd->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
1066 * Schedule reads for the holes in a packet
1067 */
1068static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1069{
1070 int frames_read = 0;
1071 struct bio *bio;
1072 int f;
1073 char written[PACKET_MAX_SIZE];
1074
Akinobu Mitac5ecc482010-02-24 08:30:08 +01001075 BUG_ON(bio_list_empty(&pkt->orig_bios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 atomic_set(&pkt->io_wait, 0);
1078 atomic_set(&pkt->io_errors, 0);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * Figure out which frames we need to read before we can write.
1082 */
1083 memset(written, 0, sizeof(written));
1084 spin_lock(&pkt->lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +01001085 bio_list_for_each(bio, &pkt->orig_bios) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1087 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001088 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 BUG_ON(first_frame < 0);
1090 BUG_ON(first_frame + num_frames > pkt->frames);
1091 for (f = first_frame; f < first_frame + num_frames; f++)
1092 written[f] = 1;
1093 }
1094 spin_unlock(&pkt->lock);
1095
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001096 if (pkt->cache_valid) {
1097 VPRINTK("pkt_gather_data: zone %llx cached\n",
1098 (unsigned long long)pkt->sector);
1099 goto out_account;
1100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * Schedule reads for missing parts of the packet.
1104 */
1105 for (f = 0; f < pkt->frames; f++) {
Jens Axboe761a15e2007-09-14 13:06:53 +02001106 struct bio_vec *vec;
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int p, offset;
1109 if (written[f])
1110 continue;
1111 bio = pkt->r_bios[f];
Jens Axboe761a15e2007-09-14 13:06:53 +02001112 vec = bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 bio_init(bio);
1114 bio->bi_max_vecs = 1;
1115 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1116 bio->bi_bdev = pd->bdev;
1117 bio->bi_end_io = pkt_end_io_read;
1118 bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001119 bio->bi_io_vec = vec;
Laurent Riffard7e3da6c2007-09-21 08:32:28 +02001120 bio->bi_destructor = pkt_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1123 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1124 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1125 f, pkt->pages[p], offset);
1126 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1127 BUG();
1128
1129 atomic_inc(&pkt->io_wait);
1130 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001131 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 frames_read++;
1133 }
1134
1135out_account:
1136 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1137 frames_read, (unsigned long long)pkt->sector);
1138 pd->stats.pkt_started++;
1139 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142/*
1143 * Find a packet matching zone, or the least recently used packet if
1144 * there is no match.
1145 */
1146static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1147{
1148 struct packet_data *pkt;
1149
1150 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1151 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1152 list_del_init(&pkt->list);
1153 if (pkt->sector != zone)
1154 pkt->cache_valid = 0;
Peter Osterlund610827d2005-09-13 01:25:29 -07001155 return pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 }
Peter Osterlund610827d2005-09-13 01:25:29 -07001158 BUG();
1159 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1163{
1164 if (pkt->cache_valid) {
1165 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1166 } else {
1167 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1168 }
1169}
1170
1171/*
1172 * recover a failed write, query for relocation if possible
1173 *
1174 * returns 1 if recovery is possible, or 0 if not
1175 *
1176 */
1177static int pkt_start_recovery(struct packet_data *pkt)
1178{
1179 /*
1180 * FIXME. We need help from the file system to implement
1181 * recovery handling.
1182 */
1183 return 0;
1184#if 0
1185 struct request *rq = pkt->rq;
1186 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1187 struct block_device *pkt_bdev;
1188 struct super_block *sb = NULL;
1189 unsigned long old_block, new_block;
1190 sector_t new_sector;
1191
1192 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1193 if (pkt_bdev) {
1194 sb = get_super(pkt_bdev);
1195 bdput(pkt_bdev);
1196 }
1197
1198 if (!sb)
1199 return 0;
1200
1201 if (!sb->s_op || !sb->s_op->relocate_blocks)
1202 goto out;
1203
1204 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1205 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1206 goto out;
1207
1208 new_sector = new_block * (CD_FRAMESIZE >> 9);
1209 pkt->sector = new_sector;
1210
1211 pkt->bio->bi_sector = new_sector;
1212 pkt->bio->bi_next = NULL;
1213 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1214 pkt->bio->bi_idx = 0;
1215
1216 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1217 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1218 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1219 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1220 BUG_ON(pkt->bio->bi_private != pkt);
1221
1222 drop_super(sb);
1223 return 1;
1224
1225out:
1226 drop_super(sb);
1227 return 0;
1228#endif
1229}
1230
1231static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1232{
1233#if PACKET_DEBUG > 1
1234 static const char *state_name[] = {
1235 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1236 };
1237 enum packet_data_state old_state = pkt->state;
1238 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1239 state_name[old_state], state_name[state]);
1240#endif
1241 pkt->state = state;
1242}
1243
1244/*
1245 * Scan the work queue to see if we can start a new packet.
1246 * returns non-zero if any work was done.
1247 */
1248static int pkt_handle_queue(struct pktcdvd_device *pd)
1249{
1250 struct packet_data *pkt, *p;
1251 struct bio *bio = NULL;
1252 sector_t zone = 0; /* Suppress gcc warning */
1253 struct pkt_rb_node *node, *first_node;
1254 struct rb_node *n;
Thomas Maier0a0fc962006-12-08 02:36:11 -08001255 int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 VPRINTK("handle_queue\n");
1258
1259 atomic_set(&pd->scan_queue, 0);
1260
1261 if (list_empty(&pd->cdrw.pkt_free_list)) {
1262 VPRINTK("handle_queue: no pkt\n");
1263 return 0;
1264 }
1265
1266 /*
1267 * Try to find a zone we are not already working on.
1268 */
1269 spin_lock(&pd->lock);
1270 first_node = pkt_rbtree_find(pd, pd->current_sector);
1271 if (!first_node) {
1272 n = rb_first(&pd->bio_queue);
1273 if (n)
1274 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1275 }
1276 node = first_node;
1277 while (node) {
1278 bio = node->bio;
1279 zone = ZONE(bio->bi_sector, pd);
1280 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001281 if (p->sector == zone) {
1282 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 break;
1287try_next_bio:
1288 node = pkt_rbtree_next(node);
1289 if (!node) {
1290 n = rb_first(&pd->bio_queue);
1291 if (n)
1292 node = rb_entry(n, struct pkt_rb_node, rb_node);
1293 }
1294 if (node == first_node)
1295 node = NULL;
1296 }
1297 spin_unlock(&pd->lock);
1298 if (!bio) {
1299 VPRINTK("handle_queue: no bio\n");
1300 return 0;
1301 }
1302
1303 pkt = pkt_get_packet_data(pd, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 pd->current_sector = zone + pd->settings.size;
1306 pkt->sector = zone;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001307 BUG_ON(pkt->frames != pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 pkt->write_size = 0;
1309
1310 /*
1311 * Scan work queue for bios in the same zone and link them
1312 * to this packet.
1313 */
1314 spin_lock(&pd->lock);
1315 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1316 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1317 bio = node->bio;
1318 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1319 (unsigned long long)ZONE(bio->bi_sector, pd));
1320 if (ZONE(bio->bi_sector, pd) != zone)
1321 break;
1322 pkt_rbtree_erase(pd, node);
1323 spin_lock(&pkt->lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +01001324 bio_list_add(&pkt->orig_bios, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1326 spin_unlock(&pkt->lock);
1327 }
Thomas Maier0a0fc962006-12-08 02:36:11 -08001328 /* check write congestion marks, and if bio_queue_size is
1329 below, wake up any waiters */
1330 wakeup = (pd->write_congestion_on > 0
1331 && pd->bio_queue_size <= pd->write_congestion_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 spin_unlock(&pd->lock);
Jens Axboe8aa7e842009-07-09 14:52:32 +02001333 if (wakeup) {
1334 clear_bdi_congested(&pd->disk->queue->backing_dev_info,
1335 BLK_RW_ASYNC);
1336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1339 pkt_set_state(pkt, PACKET_WAITING_STATE);
1340 atomic_set(&pkt->run_sm, 1);
1341
1342 spin_lock(&pd->cdrw.active_list_lock);
1343 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1344 spin_unlock(&pd->cdrw.active_list_lock);
1345
1346 return 1;
1347}
1348
1349/*
1350 * Assemble a bio to write one packet and queue the bio for processing
1351 * by the underlying block device.
1352 */
1353static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1354{
1355 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int f;
1357 int frames_write;
Peter Osterlund72772322006-02-14 13:52:56 -08001358 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001361 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1362 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
1365 /*
Peter Osterlund72772322006-02-14 13:52:56 -08001366 * Fill-in bvec with data from orig_bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
1368 frames_write = 0;
1369 spin_lock(&pkt->lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +01001370 bio_list_for_each(bio, &pkt->orig_bios) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 int segment = bio->bi_idx;
1372 int src_offs = 0;
1373 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1374 int num_frames = bio->bi_size / CD_FRAMESIZE;
1375 BUG_ON(first_frame < 0);
1376 BUG_ON(first_frame + num_frames > pkt->frames);
1377 for (f = first_frame; f < first_frame + num_frames; f++) {
1378 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1379
1380 while (src_offs >= src_bvl->bv_len) {
1381 src_offs -= src_bvl->bv_len;
1382 segment++;
1383 BUG_ON(segment >= bio->bi_vcnt);
1384 src_bvl = bio_iovec_idx(bio, segment);
1385 }
1386
1387 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
Peter Osterlund72772322006-02-14 13:52:56 -08001388 bvec[f].bv_page = src_bvl->bv_page;
1389 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 } else {
1391 pkt_copy_bio_data(bio, segment, src_offs,
Peter Osterlund72772322006-02-14 13:52:56 -08001392 bvec[f].bv_page, bvec[f].bv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 src_offs += CD_FRAMESIZE;
1395 frames_write++;
1396 }
1397 }
1398 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1399 spin_unlock(&pkt->lock);
1400
1401 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1402 frames_write, (unsigned long long)pkt->sector);
1403 BUG_ON(frames_write != pkt->write_size);
1404
1405 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
Peter Osterlund72772322006-02-14 13:52:56 -08001406 pkt_make_local_copy(pkt, bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 pkt->cache_valid = 1;
1408 } else {
1409 pkt->cache_valid = 0;
1410 }
1411
1412 /* Start the write request */
1413 bio_init(pkt->w_bio);
1414 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1415 pkt->w_bio->bi_sector = pkt->sector;
1416 pkt->w_bio->bi_bdev = pd->bdev;
1417 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1418 pkt->w_bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001419 pkt->w_bio->bi_io_vec = bvec;
Laurent Riffard7e3da6c2007-09-21 08:32:28 +02001420 pkt->w_bio->bi_destructor = pkt_bio_destructor;
Peter Osterlund72772322006-02-14 13:52:56 -08001421 for (f = 0; f < pkt->frames; f++)
1422 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1423 BUG();
Thomas Maier78220822006-10-04 02:15:28 -07001424 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 atomic_set(&pkt->io_wait, 1);
1427 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001428 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1432{
Akinobu Mitac5ecc482010-02-24 08:30:08 +01001433 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (!uptodate)
1436 pkt->cache_valid = 0;
1437
1438 /* Finish all bios corresponding to this packet */
Akinobu Mitac5ecc482010-02-24 08:30:08 +01001439 while ((bio = bio_list_pop(&pkt->orig_bios)))
NeilBrown6712ecf2007-09-27 12:47:43 +02001440 bio_endio(bio, uptodate ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1444{
1445 int uptodate;
1446
1447 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1448
1449 for (;;) {
1450 switch (pkt->state) {
1451 case PACKET_WAITING_STATE:
1452 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1453 return;
1454
1455 pkt->sleep_time = 0;
1456 pkt_gather_data(pd, pkt);
1457 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1458 break;
1459
1460 case PACKET_READ_WAIT_STATE:
1461 if (atomic_read(&pkt->io_wait) > 0)
1462 return;
1463
1464 if (atomic_read(&pkt->io_errors) > 0) {
1465 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1466 } else {
1467 pkt_start_write(pd, pkt);
1468 }
1469 break;
1470
1471 case PACKET_WRITE_WAIT_STATE:
1472 if (atomic_read(&pkt->io_wait) > 0)
1473 return;
1474
1475 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1476 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1477 } else {
1478 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1479 }
1480 break;
1481
1482 case PACKET_RECOVERY_STATE:
1483 if (pkt_start_recovery(pkt)) {
1484 pkt_start_write(pd, pkt);
1485 } else {
1486 VPRINTK("No recovery possible\n");
1487 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1488 }
1489 break;
1490
1491 case PACKET_FINISHED_STATE:
1492 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1493 pkt_finish_packet(pkt, uptodate);
1494 return;
1495
1496 default:
1497 BUG();
1498 break;
1499 }
1500 }
1501}
1502
1503static void pkt_handle_packets(struct pktcdvd_device *pd)
1504{
1505 struct packet_data *pkt, *next;
1506
1507 VPRINTK("pkt_handle_packets\n");
1508
1509 /*
1510 * Run state machine for active packets
1511 */
1512 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1513 if (atomic_read(&pkt->run_sm) > 0) {
1514 atomic_set(&pkt->run_sm, 0);
1515 pkt_run_state_machine(pd, pkt);
1516 }
1517 }
1518
1519 /*
1520 * Move no longer active packets to the free list
1521 */
1522 spin_lock(&pd->cdrw.active_list_lock);
1523 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1524 if (pkt->state == PACKET_FINISHED_STATE) {
1525 list_del(&pkt->list);
1526 pkt_put_packet_data(pd, pkt);
1527 pkt_set_state(pkt, PACKET_IDLE_STATE);
1528 atomic_set(&pd->scan_queue, 1);
1529 }
1530 }
1531 spin_unlock(&pd->cdrw.active_list_lock);
1532}
1533
1534static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1535{
1536 struct packet_data *pkt;
1537 int i;
1538
Peter Osterlundae7642b2005-11-13 16:06:36 -08001539 for (i = 0; i < PACKET_NUM_STATES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 states[i] = 0;
1541
1542 spin_lock(&pd->cdrw.active_list_lock);
1543 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1544 states[pkt->state]++;
1545 }
1546 spin_unlock(&pd->cdrw.active_list_lock);
1547}
1548
1549/*
1550 * kcdrwd is woken up when writes have been queued for one of our
1551 * registered devices
1552 */
1553static int kcdrwd(void *foobar)
1554{
1555 struct pktcdvd_device *pd = foobar;
1556 struct packet_data *pkt;
1557 long min_sleep_time, residue;
1558
1559 set_user_nice(current, -20);
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001560 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 for (;;) {
1563 DECLARE_WAITQUEUE(wait, current);
1564
1565 /*
1566 * Wait until there is something to do
1567 */
1568 add_wait_queue(&pd->wqueue, &wait);
1569 for (;;) {
1570 set_current_state(TASK_INTERRUPTIBLE);
1571
1572 /* Check if we need to run pkt_handle_queue */
1573 if (atomic_read(&pd->scan_queue) > 0)
1574 goto work_to_do;
1575
1576 /* Check if we need to run the state machine for some packet */
1577 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1578 if (atomic_read(&pkt->run_sm) > 0)
1579 goto work_to_do;
1580 }
1581
1582 /* Check if we need to process the iosched queues */
1583 if (atomic_read(&pd->iosched.attention) != 0)
1584 goto work_to_do;
1585
1586 /* Otherwise, go to sleep */
1587 if (PACKET_DEBUG > 1) {
1588 int states[PACKET_NUM_STATES];
1589 pkt_count_states(pd, states);
1590 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1591 states[0], states[1], states[2], states[3],
1592 states[4], states[5]);
1593 }
1594
1595 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1596 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1597 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1598 min_sleep_time = pkt->sleep_time;
1599 }
1600
1601 generic_unplug_device(bdev_get_queue(pd->bdev));
1602
1603 VPRINTK("kcdrwd: sleeping\n");
1604 residue = schedule_timeout(min_sleep_time);
1605 VPRINTK("kcdrwd: wake up\n");
1606
1607 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001608 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1611 if (!pkt->sleep_time)
1612 continue;
1613 pkt->sleep_time -= min_sleep_time - residue;
1614 if (pkt->sleep_time <= 0) {
1615 pkt->sleep_time = 0;
1616 atomic_inc(&pkt->run_sm);
1617 }
1618 }
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (kthread_should_stop())
1621 break;
1622 }
1623work_to_do:
1624 set_current_state(TASK_RUNNING);
1625 remove_wait_queue(&pd->wqueue, &wait);
1626
1627 if (kthread_should_stop())
1628 break;
1629
1630 /*
1631 * if pkt_handle_queue returns true, we can queue
1632 * another request.
1633 */
1634 while (pkt_handle_queue(pd))
1635 ;
1636
1637 /*
1638 * Handle packet state machine
1639 */
1640 pkt_handle_packets(pd);
1641
1642 /*
1643 * Handle iosched queues
1644 */
1645 pkt_iosched_process_queue(pd);
1646 }
1647
1648 return 0;
1649}
1650
1651static void pkt_print_settings(struct pktcdvd_device *pd)
1652{
Thomas Maier78220822006-10-04 02:15:28 -07001653 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 printk("%u blocks, ", pd->settings.size >> 2);
1655 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1656}
1657
1658static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1659{
1660 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1661
1662 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1663 cgc->cmd[2] = page_code | (page_control << 6);
1664 cgc->cmd[7] = cgc->buflen >> 8;
1665 cgc->cmd[8] = cgc->buflen & 0xff;
1666 cgc->data_direction = CGC_DATA_READ;
1667 return pkt_generic_packet(pd, cgc);
1668}
1669
1670static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1671{
1672 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1673 memset(cgc->buffer, 0, 2);
1674 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1675 cgc->cmd[1] = 0x10; /* PF */
1676 cgc->cmd[7] = cgc->buflen >> 8;
1677 cgc->cmd[8] = cgc->buflen & 0xff;
1678 cgc->data_direction = CGC_DATA_WRITE;
1679 return pkt_generic_packet(pd, cgc);
1680}
1681
1682static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1683{
1684 struct packet_command cgc;
1685 int ret;
1686
1687 /* set up command and get the disc info */
1688 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1689 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1690 cgc.cmd[8] = cgc.buflen = 2;
1691 cgc.quiet = 1;
1692
1693 if ((ret = pkt_generic_packet(pd, &cgc)))
1694 return ret;
1695
1696 /* not all drives have the same disc_info length, so requeue
1697 * packet with the length the drive tells us it can supply
1698 */
1699 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1700 sizeof(di->disc_information_length);
1701
1702 if (cgc.buflen > sizeof(disc_information))
1703 cgc.buflen = sizeof(disc_information);
1704
1705 cgc.cmd[8] = cgc.buflen;
1706 return pkt_generic_packet(pd, &cgc);
1707}
1708
1709static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1710{
1711 struct packet_command cgc;
1712 int ret;
1713
1714 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1715 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1716 cgc.cmd[1] = type & 3;
1717 cgc.cmd[4] = (track & 0xff00) >> 8;
1718 cgc.cmd[5] = track & 0xff;
1719 cgc.cmd[8] = 8;
1720 cgc.quiet = 1;
1721
1722 if ((ret = pkt_generic_packet(pd, &cgc)))
1723 return ret;
1724
1725 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1726 sizeof(ti->track_information_length);
1727
1728 if (cgc.buflen > sizeof(track_information))
1729 cgc.buflen = sizeof(track_information);
1730
1731 cgc.cmd[8] = cgc.buflen;
1732 return pkt_generic_packet(pd, &cgc);
1733}
1734
Peter Osterlund05680d82008-03-04 14:28:41 -08001735static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
1736 long *last_written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 disc_information di;
1739 track_information ti;
1740 __u32 last_track;
1741 int ret = -1;
1742
1743 if ((ret = pkt_get_disc_info(pd, &di)))
1744 return ret;
1745
1746 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1747 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1748 return ret;
1749
1750 /* if this track is blank, try the previous. */
1751 if (ti.blank) {
1752 last_track--;
1753 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1754 return ret;
1755 }
1756
1757 /* if last recorded field is valid, return it. */
1758 if (ti.lra_v) {
1759 *last_written = be32_to_cpu(ti.last_rec_address);
1760 } else {
1761 /* make it up instead */
1762 *last_written = be32_to_cpu(ti.track_start) +
1763 be32_to_cpu(ti.track_size);
1764 if (ti.free_blocks)
1765 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1766 }
1767 return 0;
1768}
1769
1770/*
1771 * write mode select package based on pd->settings
1772 */
Peter Osterlund05680d82008-03-04 14:28:41 -08001773static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct packet_command cgc;
1776 struct request_sense sense;
1777 write_param_page *wp;
1778 char buffer[128];
1779 int ret, size;
1780
1781 /* doesn't apply to DVD+RW or DVD-RAM */
1782 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1783 return 0;
1784
1785 memset(buffer, 0, sizeof(buffer));
1786 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1787 cgc.sense = &sense;
1788 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1789 pkt_dump_sense(&cgc);
1790 return ret;
1791 }
1792
1793 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1794 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1795 if (size > sizeof(buffer))
1796 size = sizeof(buffer);
1797
1798 /*
1799 * now get it all
1800 */
1801 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1802 cgc.sense = &sense;
1803 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1804 pkt_dump_sense(&cgc);
1805 return ret;
1806 }
1807
1808 /*
1809 * write page is offset header + block descriptor length
1810 */
1811 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1812
1813 wp->fp = pd->settings.fp;
1814 wp->track_mode = pd->settings.track_mode;
1815 wp->write_type = pd->settings.write_type;
1816 wp->data_block_type = pd->settings.block_mode;
1817
1818 wp->multi_session = 0;
1819
1820#ifdef PACKET_USE_LS
1821 wp->link_size = 7;
1822 wp->ls_v = 1;
1823#endif
1824
1825 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1826 wp->session_format = 0;
1827 wp->subhdr2 = 0x20;
1828 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1829 wp->session_format = 0x20;
1830 wp->subhdr2 = 8;
1831#if 0
1832 wp->mcn[0] = 0x80;
1833 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1834#endif
1835 } else {
1836 /*
1837 * paranoia
1838 */
Thomas Maier78220822006-10-04 02:15:28 -07001839 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 1;
1841 }
1842 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1843
1844 cgc.buflen = cgc.cmd[8] = size;
1845 if ((ret = pkt_mode_select(pd, &cgc))) {
1846 pkt_dump_sense(&cgc);
1847 return ret;
1848 }
1849
1850 pkt_print_settings(pd);
1851 return 0;
1852}
1853
1854/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001855 * 1 -- we can write to this track, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001857static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Peter Osterlundab863ec2006-02-20 18:28:04 -08001859 switch (pd->mmc3_profile) {
1860 case 0x1a: /* DVD+RW */
1861 case 0x12: /* DVD-RAM */
1862 /* The track is always writable on DVD+RW/DVD-RAM */
1863 return 1;
1864 default:
1865 break;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Peter Osterlundab863ec2006-02-20 18:28:04 -08001868 if (!ti->packet || !ti->fp)
1869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /*
1872 * "good" settings as per Mt Fuji.
1873 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001874 if (ti->rt == 0 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001875 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Peter Osterlundab863ec2006-02-20 18:28:04 -08001877 if (ti->rt == 0 && ti->blank == 1)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001878 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Peter Osterlundab863ec2006-02-20 18:28:04 -08001880 if (ti->rt == 1 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001881 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Thomas Maier78220822006-10-04 02:15:28 -07001883 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001888 * 1 -- we can write to this disc, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001890static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 switch (pd->mmc3_profile) {
1893 case 0x0a: /* CD-RW */
1894 case 0xffff: /* MMC3 not supported */
1895 break;
1896 case 0x1a: /* DVD+RW */
1897 case 0x13: /* DVD-RW */
1898 case 0x12: /* DVD-RAM */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001899 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 default:
Thomas Maier78220822006-10-04 02:15:28 -07001901 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
1905 /*
1906 * for disc type 0xff we should probably reserve a new track.
1907 * but i'm not sure, should we leave this to user apps? probably.
1908 */
1909 if (di->disc_type == 0xff) {
Thomas Maier78220822006-10-04 02:15:28 -07001910 printk(DRIVER_NAME": Unknown disc. No track?\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
1914 if (di->disc_type != 0x20 && di->disc_type != 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001915 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918
1919 if (di->erasable == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001920 printk(DRIVER_NAME": Disc not erasable\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923
1924 if (di->border_status == PACKET_SESSION_RESERVED) {
Thomas Maier78220822006-10-04 02:15:28 -07001925 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
Peter Osterlund7c613d52006-02-20 18:28:02 -08001929 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Peter Osterlund05680d82008-03-04 14:28:41 -08001932static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct packet_command cgc;
1935 unsigned char buf[12];
1936 disc_information di;
1937 track_information ti;
1938 int ret, track;
1939
1940 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1941 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1942 cgc.cmd[8] = 8;
1943 ret = pkt_generic_packet(pd, &cgc);
1944 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1945
1946 memset(&di, 0, sizeof(disc_information));
1947 memset(&ti, 0, sizeof(track_information));
1948
1949 if ((ret = pkt_get_disc_info(pd, &di))) {
1950 printk("failed get_disc\n");
1951 return ret;
1952 }
1953
Peter Osterlund7c613d52006-02-20 18:28:02 -08001954 if (!pkt_writable_disc(pd, &di))
Peter Osterlund9db91542006-02-20 18:28:04 -08001955 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1958
1959 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1960 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
Thomas Maier78220822006-10-04 02:15:28 -07001961 printk(DRIVER_NAME": failed get_track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return ret;
1963 }
1964
Peter Osterlundab863ec2006-02-20 18:28:04 -08001965 if (!pkt_writable_track(pd, &ti)) {
Thomas Maier78220822006-10-04 02:15:28 -07001966 printk(DRIVER_NAME": can't write to this track\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08001967 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
1970 /*
1971 * we keep packet size in 512 byte units, makes it easier to
1972 * deal with request calculations.
1973 */
1974 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1975 if (pd->settings.size == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001976 printk(DRIVER_NAME": detected zero packet size!\n");
Phillip Susia460ad62006-02-04 23:27:44 -08001977 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07001979 if (pd->settings.size > PACKET_MAX_SECTORS) {
Thomas Maier78220822006-10-04 02:15:28 -07001980 printk(DRIVER_NAME": packet size is too big\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08001981 return -EROFS;
Peter Osterlundd0272e72005-09-13 01:25:27 -07001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 pd->settings.fp = ti.fp;
1984 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1985
1986 if (ti.nwa_v) {
1987 pd->nwa = be32_to_cpu(ti.next_writable);
1988 set_bit(PACKET_NWA_VALID, &pd->flags);
1989 }
1990
1991 /*
1992 * in theory we could use lra on -RW media as well and just zero
1993 * blocks that haven't been written yet, but in practice that
1994 * is just a no-go. we'll use that for -R, naturally.
1995 */
1996 if (ti.lra_v) {
1997 pd->lra = be32_to_cpu(ti.last_rec_address);
1998 set_bit(PACKET_LRA_VALID, &pd->flags);
1999 } else {
2000 pd->lra = 0xffffffff;
2001 set_bit(PACKET_LRA_VALID, &pd->flags);
2002 }
2003
2004 /*
2005 * fine for now
2006 */
2007 pd->settings.link_loss = 7;
2008 pd->settings.write_type = 0; /* packet */
2009 pd->settings.track_mode = ti.track_mode;
2010
2011 /*
2012 * mode1 or mode2 disc
2013 */
2014 switch (ti.data_mode) {
2015 case PACKET_MODE1:
2016 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2017 break;
2018 case PACKET_MODE2:
2019 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2020 break;
2021 default:
Thomas Maier78220822006-10-04 02:15:28 -07002022 printk(DRIVER_NAME": unknown data mode\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002023 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 return 0;
2026}
2027
2028/*
2029 * enable/disable write caching on drive
2030 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002031static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
2032 int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 struct packet_command cgc;
2035 struct request_sense sense;
2036 unsigned char buf[64];
2037 int ret;
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2040 cgc.sense = &sense;
2041 cgc.buflen = pd->mode_offset + 12;
2042
2043 /*
2044 * caching mode page might not be there, so quiet this command
2045 */
2046 cgc.quiet = 1;
2047
2048 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2049 return ret;
2050
2051 buf[pd->mode_offset + 10] |= (!!set << 2);
2052
2053 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2054 ret = pkt_mode_select(pd, &cgc);
2055 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07002056 printk(DRIVER_NAME": write caching control failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 pkt_dump_sense(&cgc);
2058 } else if (!ret && set)
Thomas Maier78220822006-10-04 02:15:28 -07002059 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return ret;
2061}
2062
2063static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2064{
2065 struct packet_command cgc;
2066
2067 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2068 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2069 cgc.cmd[4] = lockflag ? 1 : 0;
2070 return pkt_generic_packet(pd, &cgc);
2071}
2072
2073/*
2074 * Returns drive maximum write speed
2075 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002076static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
2077 unsigned *write_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct packet_command cgc;
2080 struct request_sense sense;
2081 unsigned char buf[256+18];
2082 unsigned char *cap_buf;
2083 int ret, offset;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2086 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2087 cgc.sense = &sense;
2088
2089 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2090 if (ret) {
2091 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2092 sizeof(struct mode_page_header);
2093 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2094 if (ret) {
2095 pkt_dump_sense(&cgc);
2096 return ret;
2097 }
2098 }
2099
2100 offset = 20; /* Obsoleted field, used by older drives */
2101 if (cap_buf[1] >= 28)
2102 offset = 28; /* Current write speed selected */
2103 if (cap_buf[1] >= 30) {
2104 /* If the drive reports at least one "Logical Unit Write
2105 * Speed Performance Descriptor Block", use the information
2106 * in the first block. (contains the highest speed)
2107 */
2108 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2109 if (num_spdb > 0)
2110 offset = 34;
2111 }
2112
2113 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2114 return 0;
2115}
2116
2117/* These tables from cdrecord - I don't have orange book */
2118/* standard speed CD-RW (1-4x) */
2119static char clv_to_speed[16] = {
2120 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2121 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2122};
2123/* high speed CD-RW (-10x) */
2124static char hs_clv_to_speed[16] = {
2125 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2126 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2127};
2128/* ultra high speed CD-RW */
2129static char us_clv_to_speed[16] = {
2130 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2131 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2132};
2133
2134/*
2135 * reads the maximum media speed from ATIP
2136 */
Peter Osterlund05680d82008-03-04 14:28:41 -08002137static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
2138 unsigned *speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 struct packet_command cgc;
2141 struct request_sense sense;
2142 unsigned char buf[64];
2143 unsigned int size, st, sp;
2144 int ret;
2145
2146 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2147 cgc.sense = &sense;
2148 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2149 cgc.cmd[1] = 2;
2150 cgc.cmd[2] = 4; /* READ ATIP */
2151 cgc.cmd[8] = 2;
2152 ret = pkt_generic_packet(pd, &cgc);
2153 if (ret) {
2154 pkt_dump_sense(&cgc);
2155 return ret;
2156 }
2157 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2158 if (size > sizeof(buf))
2159 size = sizeof(buf);
2160
2161 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2162 cgc.sense = &sense;
2163 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2164 cgc.cmd[1] = 2;
2165 cgc.cmd[2] = 4;
2166 cgc.cmd[8] = size;
2167 ret = pkt_generic_packet(pd, &cgc);
2168 if (ret) {
2169 pkt_dump_sense(&cgc);
2170 return ret;
2171 }
2172
Alexey Dobriyaneaa0ff12008-02-06 01:36:06 -08002173 if (!(buf[6] & 0x40)) {
Thomas Maier78220822006-10-04 02:15:28 -07002174 printk(DRIVER_NAME": Disc type is not CD-RW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return 1;
2176 }
Alexey Dobriyaneaa0ff12008-02-06 01:36:06 -08002177 if (!(buf[6] & 0x4)) {
Thomas Maier78220822006-10-04 02:15:28 -07002178 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 1;
2180 }
2181
2182 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2183
2184 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2185
2186 /* Info from cdrecord */
2187 switch (st) {
2188 case 0: /* standard speed */
2189 *speed = clv_to_speed[sp];
2190 break;
2191 case 1: /* high speed */
2192 *speed = hs_clv_to_speed[sp];
2193 break;
2194 case 2: /* ultra high speed */
2195 *speed = us_clv_to_speed[sp];
2196 break;
2197 default:
Thomas Maier78220822006-10-04 02:15:28 -07002198 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 1;
2200 }
2201 if (*speed) {
Thomas Maier78220822006-10-04 02:15:28 -07002202 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return 0;
2204 } else {
Thomas Maier78220822006-10-04 02:15:28 -07002205 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return 1;
2207 }
2208}
2209
Peter Osterlund05680d82008-03-04 14:28:41 -08002210static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct packet_command cgc;
2213 struct request_sense sense;
2214 int ret;
2215
Thomas Maier78220822006-10-04 02:15:28 -07002216 VPRINTK(DRIVER_NAME": Performing OPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2219 cgc.sense = &sense;
2220 cgc.timeout = 60*HZ;
2221 cgc.cmd[0] = GPCMD_SEND_OPC;
2222 cgc.cmd[1] = 1;
2223 if ((ret = pkt_generic_packet(pd, &cgc)))
2224 pkt_dump_sense(&cgc);
2225 return ret;
2226}
2227
2228static int pkt_open_write(struct pktcdvd_device *pd)
2229{
2230 int ret;
2231 unsigned int write_speed, media_write_speed, read_speed;
2232
2233 if ((ret = pkt_probe_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002234 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
Peter Osterlund9db91542006-02-20 18:28:04 -08002235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237
2238 if ((ret = pkt_set_write_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002239 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return -EIO;
2241 }
2242
2243 pkt_write_caching(pd, USE_WCACHING);
2244
2245 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2246 write_speed = 16 * 177;
2247 switch (pd->mmc3_profile) {
2248 case 0x13: /* DVD-RW */
2249 case 0x1a: /* DVD+RW */
2250 case 0x12: /* DVD-RAM */
Thomas Maier78220822006-10-04 02:15:28 -07002251 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 break;
2253 default:
2254 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2255 media_write_speed = 16;
2256 write_speed = min(write_speed, media_write_speed * 177);
Thomas Maier78220822006-10-04 02:15:28 -07002257 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 break;
2259 }
2260 read_speed = write_speed;
2261
2262 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
Thomas Maier78220822006-10-04 02:15:28 -07002263 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return -EIO;
2265 }
2266 pd->write_speed = write_speed;
2267 pd->read_speed = read_speed;
2268
2269 if ((ret = pkt_perform_opc(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002270 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272
2273 return 0;
2274}
2275
2276/*
2277 * called at open time.
2278 */
Al Viroaeb5d722008-09-02 15:28:45 -04002279static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 int ret;
2282 long lba;
Jens Axboe165125e2007-07-24 09:28:11 +02002283 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 /*
2286 * We need to re-open the cdrom device without O_NONBLOCK to be able
2287 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2288 * so bdget() can't fail.
2289 */
2290 bdget(pd->bdev->bd_dev);
Al Viro572c4892007-10-08 13:24:05 -04002291 if ((ret = blkdev_get(pd->bdev, FMODE_READ)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto out;
2293
Peter Osterlund8382bf22006-01-08 01:02:17 -08002294 if ((ret = bd_claim(pd->bdev, pd)))
2295 goto out_putdev;
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if ((ret = pkt_get_last_written(pd, &lba))) {
Thomas Maier78220822006-10-04 02:15:28 -07002298 printk(DRIVER_NAME": pkt_get_last_written failed\n");
Peter Osterlund8382bf22006-01-08 01:02:17 -08002299 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301
2302 set_capacity(pd->disk, lba << 2);
2303 set_capacity(pd->bdev->bd_disk, lba << 2);
2304 bd_set_size(pd->bdev, (loff_t)lba << 11);
2305
2306 q = bdev_get_queue(pd->bdev);
2307 if (write) {
2308 if ((ret = pkt_open_write(pd)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002309 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * Some CDRW drives can not handle writes larger than one packet,
2312 * even if the size is a multiple of the packet size.
2313 */
2314 spin_lock_irq(q->queue_lock);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002315 blk_queue_max_hw_sectors(q, pd->settings.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 spin_unlock_irq(q->queue_lock);
2317 set_bit(PACKET_WRITABLE, &pd->flags);
2318 } else {
2319 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2320 clear_bit(PACKET_WRITABLE, &pd->flags);
2321 }
2322
2323 if ((ret = pkt_set_segment_merging(pd, q)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002324 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002326 if (write) {
2327 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
Thomas Maier78220822006-10-04 02:15:28 -07002328 printk(DRIVER_NAME": not enough memory for buffers\n");
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002329 ret = -ENOMEM;
2330 goto out_unclaim;
2331 }
Thomas Maier78220822006-10-04 02:15:28 -07002332 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 return 0;
2336
Peter Osterlund8382bf22006-01-08 01:02:17 -08002337out_unclaim:
2338 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339out_putdev:
Al Viro9a1c3542008-02-22 20:40:24 -05002340 blkdev_put(pd->bdev, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341out:
2342 return ret;
2343}
2344
2345/*
2346 * called when the device is closed. makes sure that the device flushes
2347 * the internal cache before we close.
2348 */
2349static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2350{
2351 if (flush && pkt_flush_cache(pd))
Thomas Maier78220822006-10-04 02:15:28 -07002352 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 pkt_lock_door(pd, 0);
2355
2356 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
Peter Osterlund8382bf22006-01-08 01:02:17 -08002357 bd_release(pd->bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05002358 blkdev_put(pd->bdev, FMODE_READ);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002359
2360 pkt_shrink_pktlist(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2364{
2365 if (dev_minor >= MAX_WRITERS)
2366 return NULL;
2367 return pkt_devs[dev_minor];
2368}
2369
Al Viro5e5e0072008-03-02 10:15:10 -05002370static int pkt_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 struct pktcdvd_device *pd = NULL;
2373 int ret;
2374
Thomas Maier78220822006-10-04 02:15:28 -07002375 VPRINTK(DRIVER_NAME": entering open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Jes Sorensen1657f822006-03-23 03:00:25 -08002377 mutex_lock(&ctl_mutex);
Al Viro5e5e0072008-03-02 10:15:10 -05002378 pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (!pd) {
2380 ret = -ENODEV;
2381 goto out;
2382 }
2383 BUG_ON(pd->refcnt < 0);
2384
2385 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002386 if (pd->refcnt > 1) {
Al Viro5e5e0072008-03-02 10:15:10 -05002387 if ((mode & FMODE_WRITE) &&
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002388 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2389 ret = -EBUSY;
2390 goto out_dec;
2391 }
2392 } else {
Al Viro5e5e0072008-03-02 10:15:10 -05002393 ret = pkt_open_dev(pd, mode & FMODE_WRITE);
Peter Osterlund01fd9fd2006-02-14 13:52:55 -08002394 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /*
2397 * needed here as well, since ext2 (among others) may change
2398 * the blocksize at mount time
2399 */
Al Viro5e5e0072008-03-02 10:15:10 -05002400 set_blocksize(bdev, CD_FRAMESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
Jes Sorensen1657f822006-03-23 03:00:25 -08002403 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return 0;
2405
2406out_dec:
2407 pd->refcnt--;
2408out:
Thomas Maier78220822006-10-04 02:15:28 -07002409 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
Jes Sorensen1657f822006-03-23 03:00:25 -08002410 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return ret;
2412}
2413
Al Viro5e5e0072008-03-02 10:15:10 -05002414static int pkt_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Al Viro5e5e0072008-03-02 10:15:10 -05002416 struct pktcdvd_device *pd = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 int ret = 0;
2418
Jes Sorensen1657f822006-03-23 03:00:25 -08002419 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 pd->refcnt--;
2421 BUG_ON(pd->refcnt < 0);
2422 if (pd->refcnt == 0) {
2423 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2424 pkt_release_dev(pd, flush);
2425 }
Jes Sorensen1657f822006-03-23 03:00:25 -08002426 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return ret;
2428}
2429
2430
NeilBrown6712ecf2007-09-27 12:47:43 +02002431static void pkt_end_io_read_cloned(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 struct packet_stacked_data *psd = bio->bi_private;
2434 struct pktcdvd_device *pd = psd->pd;
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +02002437 bio_endio(psd->bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 mempool_free(psd, psd_pool);
2439 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Jens Axboe165125e2007-07-24 09:28:11 +02002442static int pkt_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 struct pktcdvd_device *pd;
2445 char b[BDEVNAME_SIZE];
2446 sector_t zone;
2447 struct packet_data *pkt;
2448 int was_empty, blocked_bio;
2449 struct pkt_rb_node *node;
2450
2451 pd = q->queuedata;
2452 if (!pd) {
Thomas Maier78220822006-10-04 02:15:28 -07002453 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto end_io;
2455 }
2456
2457 /*
2458 * Clone READ bios so we can have our own bi_end_io callback.
2459 */
2460 if (bio_data_dir(bio) == READ) {
2461 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2462 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2463
2464 psd->pd = pd;
2465 psd->bio = bio;
2466 cloned_bio->bi_bdev = pd->bdev;
2467 cloned_bio->bi_private = psd;
2468 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2469 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002470 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return 0;
2472 }
2473
2474 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
Thomas Maier78220822006-10-04 02:15:28 -07002475 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 pd->name, (unsigned long long)bio->bi_sector);
2477 goto end_io;
2478 }
2479
2480 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
Thomas Maier78220822006-10-04 02:15:28 -07002481 printk(DRIVER_NAME": wrong bio size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 goto end_io;
2483 }
2484
2485 blk_queue_bounce(q, &bio);
2486
2487 zone = ZONE(bio->bi_sector, pd);
2488 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2489 (unsigned long long)bio->bi_sector,
2490 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2491
2492 /* Check if we have to split the bio */
2493 {
2494 struct bio_pair *bp;
2495 sector_t last_zone;
2496 int first_sectors;
2497
2498 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2499 if (last_zone != zone) {
2500 BUG_ON(last_zone != zone + pd->settings.size);
2501 first_sectors = last_zone - bio->bi_sector;
Denis ChengRq6feef532008-10-09 08:57:05 +02002502 bp = bio_split(bio, first_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 BUG_ON(!bp);
2504 pkt_make_request(q, &bp->bio1);
2505 pkt_make_request(q, &bp->bio2);
2506 bio_pair_release(bp);
2507 return 0;
2508 }
2509 }
2510
2511 /*
2512 * If we find a matching packet in state WAITING or READ_WAIT, we can
2513 * just append this bio to that packet.
2514 */
2515 spin_lock(&pd->cdrw.active_list_lock);
2516 blocked_bio = 0;
2517 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2518 if (pkt->sector == zone) {
2519 spin_lock(&pkt->lock);
2520 if ((pkt->state == PACKET_WAITING_STATE) ||
2521 (pkt->state == PACKET_READ_WAIT_STATE)) {
Akinobu Mitac5ecc482010-02-24 08:30:08 +01002522 bio_list_add(&pkt->orig_bios, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2524 if ((pkt->write_size >= pkt->frames) &&
2525 (pkt->state == PACKET_WAITING_STATE)) {
2526 atomic_inc(&pkt->run_sm);
2527 wake_up(&pd->wqueue);
2528 }
2529 spin_unlock(&pkt->lock);
2530 spin_unlock(&pd->cdrw.active_list_lock);
2531 return 0;
2532 } else {
2533 blocked_bio = 1;
2534 }
2535 spin_unlock(&pkt->lock);
2536 }
2537 }
2538 spin_unlock(&pd->cdrw.active_list_lock);
2539
Thomas Maier0a0fc962006-12-08 02:36:11 -08002540 /*
2541 * Test if there is enough room left in the bio work queue
2542 * (queue size >= congestion on mark).
2543 * If not, wait till the work queue size is below the congestion off mark.
2544 */
2545 spin_lock(&pd->lock);
2546 if (pd->write_congestion_on > 0
2547 && pd->bio_queue_size >= pd->write_congestion_on) {
Jens Axboe8aa7e842009-07-09 14:52:32 +02002548 set_bdi_congested(&q->backing_dev_info, BLK_RW_ASYNC);
Thomas Maier0a0fc962006-12-08 02:36:11 -08002549 do {
2550 spin_unlock(&pd->lock);
Jens Axboe8aa7e842009-07-09 14:52:32 +02002551 congestion_wait(BLK_RW_ASYNC, HZ);
Thomas Maier0a0fc962006-12-08 02:36:11 -08002552 spin_lock(&pd->lock);
2553 } while(pd->bio_queue_size > pd->write_congestion_off);
2554 }
2555 spin_unlock(&pd->lock);
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /*
2558 * No matching packet found. Store the bio in the work queue.
2559 */
2560 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 node->bio = bio;
2562 spin_lock(&pd->lock);
2563 BUG_ON(pd->bio_queue_size < 0);
2564 was_empty = (pd->bio_queue_size == 0);
2565 pkt_rbtree_insert(pd, node);
2566 spin_unlock(&pd->lock);
2567
2568 /*
2569 * Wake up the worker thread.
2570 */
2571 atomic_set(&pd->scan_queue, 1);
2572 if (was_empty) {
2573 /* This wake_up is required for correct operation */
2574 wake_up(&pd->wqueue);
2575 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2576 /*
2577 * This wake up is not required for correct operation,
2578 * but improves performance in some cases.
2579 */
2580 wake_up(&pd->wqueue);
2581 }
2582 return 0;
2583end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02002584 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return 0;
2586}
2587
2588
2589
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02002590static int pkt_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2591 struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct pktcdvd_device *pd = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02002594 sector_t zone = ZONE(bmd->bi_sector, pd);
2595 int used = ((bmd->bi_sector - zone) << 9) + bmd->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 int remaining = (pd->settings.size << 9) - used;
2597 int remaining2;
2598
2599 /*
2600 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2601 * boundary, pkt_make_request() will split the bio.
2602 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02002603 remaining2 = PAGE_SIZE - bmd->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 remaining = max(remaining, remaining2);
2605
2606 BUG_ON(remaining < 0);
2607 return remaining;
2608}
2609
2610static void pkt_init_queue(struct pktcdvd_device *pd)
2611{
Jens Axboe165125e2007-07-24 09:28:11 +02002612 struct request_queue *q = pd->disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 blk_queue_make_request(q, pkt_make_request);
Martin K. Petersene1defc42009-05-22 17:17:49 -04002615 blk_queue_logical_block_size(q, CD_FRAMESIZE);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002616 blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 blk_queue_merge_bvec(q, pkt_merge_bvec);
2618 q->queuedata = pd;
2619}
2620
2621static int pkt_seq_show(struct seq_file *m, void *p)
2622{
2623 struct pktcdvd_device *pd = m->private;
2624 char *msg;
2625 char bdev_buf[BDEVNAME_SIZE];
2626 int states[PACKET_NUM_STATES];
2627
2628 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2629 bdevname(pd->bdev, bdev_buf));
2630
2631 seq_printf(m, "\nSettings:\n");
2632 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2633
2634 if (pd->settings.write_type == 0)
2635 msg = "Packet";
2636 else
2637 msg = "Unknown";
2638 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2639
2640 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2641 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2642
2643 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2644
2645 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2646 msg = "Mode 1";
2647 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2648 msg = "Mode 2";
2649 else
2650 msg = "Unknown";
2651 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2652
2653 seq_printf(m, "\nStatistics:\n");
2654 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2655 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2656 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2657 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2658 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2659
2660 seq_printf(m, "\nMisc:\n");
2661 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2662 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2663 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2664 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2665 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2666 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2667
2668 seq_printf(m, "\nQueue state:\n");
2669 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2670 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2671 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2672
2673 pkt_count_states(pd, states);
2674 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2675 states[0], states[1], states[2], states[3], states[4], states[5]);
2676
Thomas Maier0a0fc962006-12-08 02:36:11 -08002677 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2678 pd->write_congestion_off,
2679 pd->write_congestion_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return 0;
2681}
2682
2683static int pkt_seq_open(struct inode *inode, struct file *file)
2684{
2685 return single_open(file, pkt_seq_show, PDE(inode)->data);
2686}
2687
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002688static const struct file_operations pkt_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 .open = pkt_seq_open,
2690 .read = seq_read,
2691 .llseek = seq_lseek,
2692 .release = single_release
2693};
2694
2695static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2696{
2697 int i;
2698 int ret = 0;
2699 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 struct block_device *bdev;
2701
2702 if (pd->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002703 printk(DRIVER_NAME": Recursive setup not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return -EBUSY;
2705 }
2706 for (i = 0; i < MAX_WRITERS; i++) {
2707 struct pktcdvd_device *pd2 = pkt_devs[i];
2708 if (!pd2)
2709 continue;
2710 if (pd2->bdev->bd_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002711 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return -EBUSY;
2713 }
2714 if (pd2->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002715 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return -EBUSY;
2717 }
2718 }
2719
2720 bdev = bdget(dev);
2721 if (!bdev)
2722 return -ENOMEM;
Al Viro572c4892007-10-08 13:24:05 -04002723 ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (ret)
2725 return ret;
2726
2727 /* This is safe, since we have a reference from open(). */
2728 __module_get(THIS_MODULE);
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 pd->bdev = bdev;
2731 set_blocksize(bdev, CD_FRAMESIZE);
2732
2733 pkt_init_queue(pd);
2734
2735 atomic_set(&pd->cdrw.pending_bios, 0);
2736 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2737 if (IS_ERR(pd->cdrw.thread)) {
Thomas Maier78220822006-10-04 02:15:28 -07002738 printk(DRIVER_NAME": can't start kernel thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ret = -ENOMEM;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002740 goto out_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742
Denis V. Lunevc7705f32008-04-29 01:02:35 -07002743 proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
Thomas Maier78220822006-10-04 02:15:28 -07002744 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return 0;
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747out_mem:
Al Viro2cbed892008-11-30 01:33:57 -05002748 blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* This is safe: open() is still holding a reference. */
2750 module_put(THIS_MODULE);
2751 return ret;
2752}
2753
Al Viro5e5e0072008-03-02 10:15:10 -05002754static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Al Viro5e5e0072008-03-02 10:15:10 -05002756 struct pktcdvd_device *pd = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Al Viro5e5e0072008-03-02 10:15:10 -05002758 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd,
2759 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 switch (cmd) {
Al Viroa0eb62a2007-08-29 00:56:32 -04002762 case CDROMEJECT:
2763 /*
2764 * The door gets locked when the device is opened, so we
2765 * have to unlock it or else the eject command fails.
2766 */
2767 if (pd->refcnt == 1)
2768 pkt_lock_door(pd, 0);
2769 /* fallthru */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
2771 * forward selected CDROM ioctls to CD-ROM, for UDF
2772 */
2773 case CDROMMULTISESSION:
2774 case CDROMREADTOCENTRY:
2775 case CDROM_LAST_WRITTEN:
2776 case CDROM_SEND_PACKET:
2777 case SCSI_IOCTL_SEND_COMMAND:
Al Viro5e5e0072008-03-02 10:15:10 -05002778 return __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 default:
Thomas Maier78220822006-10-04 02:15:28 -07002781 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
Linus Torvalds8560c652008-08-27 13:35:31 -07002782 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Linus Torvalds8560c652008-08-27 13:35:31 -07002784
2785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
2788static int pkt_media_changed(struct gendisk *disk)
2789{
2790 struct pktcdvd_device *pd = disk->private_data;
2791 struct gendisk *attached_disk;
2792
2793 if (!pd)
2794 return 0;
2795 if (!pd->bdev)
2796 return 0;
2797 attached_disk = pd->bdev->bd_disk;
2798 if (!attached_disk)
2799 return 0;
2800 return attached_disk->fops->media_changed(attached_disk);
2801}
2802
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002803static const struct block_device_operations pktcdvd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 .owner = THIS_MODULE,
Al Viro5e5e0072008-03-02 10:15:10 -05002805 .open = pkt_open,
2806 .release = pkt_close,
2807 .locked_ioctl = pkt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .media_changed = pkt_media_changed,
2809};
2810
Kay Sieverse454cea2009-09-18 23:01:12 +02002811static char *pktcdvd_devnode(struct gendisk *gd, mode_t *mode)
Kay Sieversb03f38b2009-04-30 15:23:42 +02002812{
2813 return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
2814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/*
2817 * Set up mapping from pktcdvd device to CD-ROM device.
2818 */
Thomas Maieradb92502006-12-08 02:36:10 -08002819static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 int idx;
2822 int ret = -ENOMEM;
2823 struct pktcdvd_device *pd;
2824 struct gendisk *disk;
Thomas Maieradb92502006-12-08 02:36:10 -08002825
2826 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 for (idx = 0; idx < MAX_WRITERS; idx++)
2829 if (!pkt_devs[idx])
2830 break;
2831 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002832 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
Thomas Maieradb92502006-12-08 02:36:10 -08002833 ret = -EBUSY;
2834 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
Peter Osterlund1107d2e2005-09-13 01:25:29 -07002837 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (!pd)
Thomas Maieradb92502006-12-08 02:36:10 -08002839 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002841 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2842 sizeof(struct pkt_rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (!pd->rb_pool)
2844 goto out_mem;
2845
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002846 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2847 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2848 spin_lock_init(&pd->cdrw.active_list_lock);
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 spin_lock_init(&pd->lock);
2851 spin_lock_init(&pd->iosched.lock);
Akinobu Mitac5ecc482010-02-24 08:30:08 +01002852 bio_list_init(&pd->iosched.read_queue);
2853 bio_list_init(&pd->iosched.write_queue);
Thomas Maier78220822006-10-04 02:15:28 -07002854 sprintf(pd->name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 init_waitqueue_head(&pd->wqueue);
2856 pd->bio_queue = RB_ROOT;
2857
Thomas Maier0a0fc962006-12-08 02:36:11 -08002858 pd->write_congestion_on = write_congestion_on;
2859 pd->write_congestion_off = write_congestion_off;
2860
Thomas Maieradb92502006-12-08 02:36:10 -08002861 disk = alloc_disk(1);
2862 if (!disk)
2863 goto out_mem;
2864 pd->disk = disk;
Thomas Maieradd21662006-10-04 02:15:30 -07002865 disk->major = pktdev_major;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 disk->first_minor = idx;
2867 disk->fops = &pktcdvd_ops;
2868 disk->flags = GENHD_FL_REMOVABLE;
Thomas Maieradb92502006-12-08 02:36:10 -08002869 strcpy(disk->disk_name, pd->name);
Kay Sieverse454cea2009-09-18 23:01:12 +02002870 disk->devnode = pktcdvd_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 disk->private_data = pd;
2872 disk->queue = blk_alloc_queue(GFP_KERNEL);
2873 if (!disk->queue)
2874 goto out_mem2;
2875
Tejun Heof331c022008-09-03 09:01:48 +02002876 pd->pkt_dev = MKDEV(pktdev_major, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 ret = pkt_new_dev(pd, dev);
2878 if (ret)
2879 goto out_new_dev;
2880
2881 add_disk(disk);
Thomas Maieradb92502006-12-08 02:36:10 -08002882
Thomas Maier32694852006-12-08 02:36:12 -08002883 pkt_sysfs_dev_new(pd);
2884 pkt_debugfs_dev_new(pd);
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 pkt_devs[idx] = pd;
Thomas Maieradb92502006-12-08 02:36:10 -08002887 if (pkt_dev)
2888 *pkt_dev = pd->pkt_dev;
2889
2890 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return 0;
2892
2893out_new_dev:
Al Viro1312f402006-03-12 11:02:03 -05002894 blk_cleanup_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895out_mem2:
2896 put_disk(disk);
2897out_mem:
2898 if (pd->rb_pool)
2899 mempool_destroy(pd->rb_pool);
2900 kfree(pd);
Thomas Maieradb92502006-12-08 02:36:10 -08002901out_mutex:
2902 mutex_unlock(&ctl_mutex);
2903 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return ret;
2905}
2906
2907/*
2908 * Tear down mapping from pktcdvd device to CD-ROM device.
2909 */
Thomas Maieradb92502006-12-08 02:36:10 -08002910static int pkt_remove_dev(dev_t pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct pktcdvd_device *pd;
2913 int idx;
Thomas Maieradb92502006-12-08 02:36:10 -08002914 int ret = 0;
2915
2916 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 for (idx = 0; idx < MAX_WRITERS; idx++) {
2919 pd = pkt_devs[idx];
2920 if (pd && (pd->pkt_dev == pkt_dev))
2921 break;
2922 }
2923 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002924 DPRINTK(DRIVER_NAME": dev not setup\n");
Thomas Maieradb92502006-12-08 02:36:10 -08002925 ret = -ENXIO;
2926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928
Thomas Maieradb92502006-12-08 02:36:10 -08002929 if (pd->refcnt > 0) {
2930 ret = -EBUSY;
2931 goto out;
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (!IS_ERR(pd->cdrw.thread))
2934 kthread_stop(pd->cdrw.thread);
2935
Thomas Maier32694852006-12-08 02:36:12 -08002936 pkt_devs[idx] = NULL;
2937
2938 pkt_debugfs_dev_remove(pd);
2939 pkt_sysfs_dev_remove(pd);
2940
Al Viro2cbed892008-11-30 01:33:57 -05002941 blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 remove_proc_entry(pd->name, pkt_proc);
Thomas Maier78220822006-10-04 02:15:28 -07002944 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 del_gendisk(pd->disk);
Al Viro1312f402006-03-12 11:02:03 -05002947 blk_cleanup_queue(pd->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 put_disk(pd->disk);
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 mempool_destroy(pd->rb_pool);
2951 kfree(pd);
2952
2953 /* This is safe: open() is still holding a reference. */
2954 module_put(THIS_MODULE);
Thomas Maieradb92502006-12-08 02:36:10 -08002955
2956out:
2957 mutex_unlock(&ctl_mutex);
2958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
2961static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2962{
Thomas Maieradb92502006-12-08 02:36:10 -08002963 struct pktcdvd_device *pd;
2964
2965 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2966
2967 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (pd) {
2969 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2970 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2971 } else {
2972 ctrl_cmd->dev = 0;
2973 ctrl_cmd->pkt_dev = 0;
2974 }
2975 ctrl_cmd->num_devices = MAX_WRITERS;
Thomas Maieradb92502006-12-08 02:36:10 -08002976
2977 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Linus Torvalds8560c652008-08-27 13:35:31 -07002980static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 void __user *argp = (void __user *)arg;
2983 struct pkt_ctrl_command ctrl_cmd;
2984 int ret = 0;
Thomas Maieradb92502006-12-08 02:36:10 -08002985 dev_t pkt_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 if (cmd != PACKET_CTRL_CMD)
2988 return -ENOTTY;
2989
2990 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2991 return -EFAULT;
2992
2993 switch (ctrl_cmd.command) {
2994 case PKT_CTRL_CMD_SETUP:
2995 if (!capable(CAP_SYS_ADMIN))
2996 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08002997 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
2998 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 break;
3000 case PKT_CTRL_CMD_TEARDOWN:
3001 if (!capable(CAP_SYS_ADMIN))
3002 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003003 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 break;
3005 case PKT_CTRL_CMD_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 pkt_get_status(&ctrl_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 break;
3008 default:
3009 return -ENOTTY;
3010 }
3011
3012 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3013 return -EFAULT;
3014 return ret;
3015}
3016
3017
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003018static const struct file_operations pkt_ctl_fops = {
Linus Torvalds8560c652008-08-27 13:35:31 -07003019 .ioctl = pkt_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 .owner = THIS_MODULE,
3021};
3022
3023static struct miscdevice pkt_misc = {
3024 .minor = MISC_DYNAMIC_MINOR,
Thomas Maier78220822006-10-04 02:15:28 -07003025 .name = DRIVER_NAME,
Kay Sieverse454cea2009-09-18 23:01:12 +02003026 .nodename = "pktcdvd/control",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 .fops = &pkt_ctl_fops
3028};
3029
3030static int __init pkt_init(void)
3031{
3032 int ret;
3033
Thomas Maier32694852006-12-08 02:36:12 -08003034 mutex_init(&ctl_mutex);
3035
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08003036 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3037 sizeof(struct packet_stacked_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (!psd_pool)
3039 return -ENOMEM;
3040
Thomas Maieradd21662006-10-04 02:15:30 -07003041 ret = register_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (ret < 0) {
Thomas Maier78220822006-10-04 02:15:28 -07003043 printk(DRIVER_NAME": Unable to register block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 goto out2;
3045 }
Thomas Maieradd21662006-10-04 02:15:30 -07003046 if (!pktdev_major)
3047 pktdev_major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Thomas Maier32694852006-12-08 02:36:12 -08003049 ret = pkt_sysfs_init();
3050 if (ret)
3051 goto out;
3052
3053 pkt_debugfs_init();
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 ret = misc_register(&pkt_misc);
3056 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07003057 printk(DRIVER_NAME": Unable to register misc device\n");
Thomas Maier32694852006-12-08 02:36:12 -08003058 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060
Alexey Dobriyan928b4d82008-04-29 01:01:44 -07003061 pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return 0;
3064
Thomas Maier32694852006-12-08 02:36:12 -08003065out_misc:
3066 pkt_debugfs_cleanup();
3067 pkt_sysfs_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068out:
Thomas Maieradd21662006-10-04 02:15:30 -07003069 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070out2:
3071 mempool_destroy(psd_pool);
3072 return ret;
3073}
3074
3075static void __exit pkt_exit(void)
3076{
Alexey Dobriyan928b4d82008-04-29 01:01:44 -07003077 remove_proc_entry("driver/"DRIVER_NAME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 misc_deregister(&pkt_misc);
Thomas Maier32694852006-12-08 02:36:12 -08003079
3080 pkt_debugfs_cleanup();
3081 pkt_sysfs_cleanup();
3082
Thomas Maieradd21662006-10-04 02:15:30 -07003083 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 mempool_destroy(psd_pool);
3085}
3086
3087MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3088MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3089MODULE_LICENSE("GPL");
3090
3091module_init(pkt_init);
3092module_exit(pkt_exit);