blob: ba2f550963bc3e16ffd8159b619ec16aa2c44214 [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 Osterlunda676f8d02005-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 Osterlunda676f8d02005-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 Osterlunda676f8d02005-09-13 01:25:27 -070023 * The purpose of the packet writing driver is to hide these restrictions from
24 * higher layers, such as file systems, and present a block device that can be
25 * randomly read and written using 2kB-sized blocks.
26 *
27 * The lowest layer in the packet writing driver is the packet I/O scheduler.
28 * Its data is defined by the struct packet_iosched and includes two bio
29 * queues with pending read and write requests. These queues are processed
30 * by the pkt_iosched_process_queue() function. The write requests in this
31 * queue are already properly aligned and sized. This layer is responsible for
32 * issuing the flush cache commands and scheduling the I/O in a good order.
33 *
34 * The next layer transforms unaligned write requests to aligned writes. This
35 * transformation requires reading missing pieces of data from the underlying
36 * block device, assembling the pieces to full packets and queuing them to the
37 * packet I/O scheduler.
38 *
39 * At the top layer there is a custom make_request_fn function that forwards
40 * read requests directly to the iosched queue and puts write requests in the
41 * unaligned write queue. A kernel thread performs the necessary read
42 * gathering to convert the unaligned writes to aligned writes and then feeds
43 * them to the packet I/O scheduler.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 *************************************************************************/
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/pktcdvd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/module.h>
49#include <linux/types.h>
50#include <linux/kernel.h>
51#include <linux/kthread.h>
52#include <linux/errno.h>
53#include <linux/spinlock.h>
54#include <linux/file.h>
55#include <linux/proc_fs.h>
56#include <linux/seq_file.h>
57#include <linux/miscdevice.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080058#include <linux/freezer.h>
Jes Sorensen1657f822006-03-23 03:00:25 -080059#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_ioctl.h>
Peter Osterlundcef289632006-02-20 18:28:01 -080062#include <scsi/scsi.h>
Thomas Maier32694852006-12-08 02:36:12 -080063#include <linux/debugfs.h>
64#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/uaccess.h>
67
Thomas Maier78220822006-10-04 02:15:28 -070068#define DRIVER_NAME "pktcdvd"
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#if PACKET_DEBUG
71#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
72#else
73#define DPRINTK(fmt, args...)
74#endif
75
76#if PACKET_DEBUG > 1
77#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
78#else
79#define VPRINTK(fmt, args...)
80#endif
81
82#define MAX_SPEED 0xffff
83
84#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
85
86static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
87static struct proc_dir_entry *pkt_proc;
Thomas Maieradd21662006-10-04 02:15:30 -070088static int pktdev_major;
Thomas Maier0a0fc962006-12-08 02:36:11 -080089static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
90static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
Jes Sorensen1657f822006-03-23 03:00:25 -080091static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static mempool_t *psd_pool;
93
Thomas Maier32694852006-12-08 02:36:12 -080094static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
95static struct dentry *pkt_debugfs_root = NULL; /* /debug/pktcdvd */
96
97/* forward declaration */
98static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
99static int pkt_remove_dev(dev_t pkt_dev);
100static int pkt_seq_show(struct seq_file *m, void *p);
101
102
103
104/*
105 * create and register a pktcdvd kernel object.
106 */
107static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
108 const char* name,
109 struct kobject* parent,
110 struct kobj_type* ktype)
111{
112 struct pktcdvd_kobj *p;
113 p = kzalloc(sizeof(*p), GFP_KERNEL);
114 if (!p)
115 return NULL;
116 kobject_set_name(&p->kobj, "%s", name);
117 p->kobj.parent = parent;
118 p->kobj.ktype = ktype;
119 p->pd = pd;
120 if (kobject_register(&p->kobj) != 0)
121 return NULL;
122 return p;
123}
124/*
125 * remove a pktcdvd kernel object.
126 */
127static void pkt_kobj_remove(struct pktcdvd_kobj *p)
128{
129 if (p)
130 kobject_unregister(&p->kobj);
131}
132/*
133 * default release function for pktcdvd kernel objects.
134 */
135static void pkt_kobj_release(struct kobject *kobj)
136{
137 kfree(to_pktcdvdkobj(kobj));
138}
139
140
141/**********************************************************
142 *
143 * sysfs interface for pktcdvd
144 * by (C) 2006 Thomas Maier <balagi@justmail.de>
145 *
146 **********************************************************/
147
148#define DEF_ATTR(_obj,_name,_mode) \
Tejun Heo7b595752007-06-14 03:45:17 +0900149 static struct attribute _obj = { .name = _name, .mode = _mode }
Thomas Maier32694852006-12-08 02:36:12 -0800150
151/**********************************************************
152 /sys/class/pktcdvd/pktcdvd[0-7]/
153 stat/reset
154 stat/packets_started
155 stat/packets_finished
156 stat/kb_written
157 stat/kb_read
158 stat/kb_read_gather
159 write_queue/size
160 write_queue/congestion_off
161 write_queue/congestion_on
162 **********************************************************/
163
164DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
165DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
166DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
167DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
168DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
169DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
170
171static struct attribute *kobj_pkt_attrs_stat[] = {
172 &kobj_pkt_attr_st1,
173 &kobj_pkt_attr_st2,
174 &kobj_pkt_attr_st3,
175 &kobj_pkt_attr_st4,
176 &kobj_pkt_attr_st5,
177 &kobj_pkt_attr_st6,
178 NULL
179};
180
181DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
182DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
183DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
184
185static struct attribute *kobj_pkt_attrs_wqueue[] = {
186 &kobj_pkt_attr_wq1,
187 &kobj_pkt_attr_wq2,
188 &kobj_pkt_attr_wq3,
189 NULL
190};
191
Thomas Maier32694852006-12-08 02:36:12 -0800192static ssize_t kobj_pkt_show(struct kobject *kobj,
193 struct attribute *attr, char *data)
194{
195 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
196 int n = 0;
197 int v;
198 if (strcmp(attr->name, "packets_started") == 0) {
199 n = sprintf(data, "%lu\n", pd->stats.pkt_started);
200
201 } else if (strcmp(attr->name, "packets_finished") == 0) {
202 n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
203
204 } else if (strcmp(attr->name, "kb_written") == 0) {
205 n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
206
207 } else if (strcmp(attr->name, "kb_read") == 0) {
208 n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
209
210 } else if (strcmp(attr->name, "kb_read_gather") == 0) {
211 n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
212
213 } else if (strcmp(attr->name, "size") == 0) {
214 spin_lock(&pd->lock);
215 v = pd->bio_queue_size;
216 spin_unlock(&pd->lock);
217 n = sprintf(data, "%d\n", v);
218
219 } else if (strcmp(attr->name, "congestion_off") == 0) {
220 spin_lock(&pd->lock);
221 v = pd->write_congestion_off;
222 spin_unlock(&pd->lock);
223 n = sprintf(data, "%d\n", v);
224
225 } else if (strcmp(attr->name, "congestion_on") == 0) {
226 spin_lock(&pd->lock);
227 v = pd->write_congestion_on;
228 spin_unlock(&pd->lock);
229 n = sprintf(data, "%d\n", v);
230 }
231 return n;
232}
233
234static void init_write_congestion_marks(int* lo, int* hi)
235{
236 if (*hi > 0) {
237 *hi = max(*hi, 500);
238 *hi = min(*hi, 1000000);
239 if (*lo <= 0)
240 *lo = *hi - 100;
241 else {
242 *lo = min(*lo, *hi - 100);
243 *lo = max(*lo, 100);
244 }
245 } else {
246 *hi = -1;
247 *lo = -1;
248 }
249}
250
251static ssize_t kobj_pkt_store(struct kobject *kobj,
252 struct attribute *attr,
253 const char *data, size_t len)
254{
255 struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
256 int val;
Thomas Maier32694852006-12-08 02:36:12 -0800257
Thomas Maier83f3aa32007-02-10 01:45:11 -0800258 if (strcmp(attr->name, "reset") == 0 && len > 0) {
Thomas Maier32694852006-12-08 02:36:12 -0800259 pd->stats.pkt_started = 0;
260 pd->stats.pkt_ended = 0;
261 pd->stats.secs_w = 0;
262 pd->stats.secs_rg = 0;
263 pd->stats.secs_r = 0;
264
265 } else if (strcmp(attr->name, "congestion_off") == 0
Thomas Maier83f3aa32007-02-10 01:45:11 -0800266 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800267 spin_lock(&pd->lock);
268 pd->write_congestion_off = val;
269 init_write_congestion_marks(&pd->write_congestion_off,
270 &pd->write_congestion_on);
271 spin_unlock(&pd->lock);
272
273 } else if (strcmp(attr->name, "congestion_on") == 0
Thomas Maier83f3aa32007-02-10 01:45:11 -0800274 && sscanf(data, "%d", &val) == 1) {
Thomas Maier32694852006-12-08 02:36:12 -0800275 spin_lock(&pd->lock);
276 pd->write_congestion_on = val;
277 init_write_congestion_marks(&pd->write_congestion_off,
278 &pd->write_congestion_on);
279 spin_unlock(&pd->lock);
280 }
281 return len;
282}
283
284static struct sysfs_ops kobj_pkt_ops = {
285 .show = kobj_pkt_show,
286 .store = kobj_pkt_store
287};
288static struct kobj_type kobj_pkt_type_stat = {
289 .release = pkt_kobj_release,
290 .sysfs_ops = &kobj_pkt_ops,
291 .default_attrs = kobj_pkt_attrs_stat
292};
293static struct kobj_type kobj_pkt_type_wqueue = {
294 .release = pkt_kobj_release,
295 .sysfs_ops = &kobj_pkt_ops,
296 .default_attrs = kobj_pkt_attrs_wqueue
297};
298
299static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
300{
301 if (class_pktcdvd) {
302 pd->clsdev = class_device_create(class_pktcdvd,
303 NULL, pd->pkt_dev,
304 NULL, "%s", pd->name);
305 if (IS_ERR(pd->clsdev))
306 pd->clsdev = NULL;
307 }
308 if (pd->clsdev) {
309 pd->kobj_stat = pkt_kobj_create(pd, "stat",
310 &pd->clsdev->kobj,
311 &kobj_pkt_type_stat);
312 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
313 &pd->clsdev->kobj,
314 &kobj_pkt_type_wqueue);
315 }
316}
317
318static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
319{
320 pkt_kobj_remove(pd->kobj_stat);
321 pkt_kobj_remove(pd->kobj_wqueue);
322 if (class_pktcdvd)
323 class_device_destroy(class_pktcdvd, pd->pkt_dev);
324}
325
326
327/********************************************************************
328 /sys/class/pktcdvd/
329 add map block device
330 remove unmap packet dev
331 device_map show mappings
332 *******************************************************************/
333
334static void class_pktcdvd_release(struct class *cls)
335{
336 kfree(cls);
337}
338static ssize_t class_pktcdvd_show_map(struct class *c, char *data)
339{
340 int n = 0;
341 int idx;
342 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
343 for (idx = 0; idx < MAX_WRITERS; idx++) {
344 struct pktcdvd_device *pd = pkt_devs[idx];
345 if (!pd)
346 continue;
347 n += sprintf(data+n, "%s %u:%u %u:%u\n",
348 pd->name,
349 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
350 MAJOR(pd->bdev->bd_dev),
351 MINOR(pd->bdev->bd_dev));
352 }
353 mutex_unlock(&ctl_mutex);
354 return n;
355}
356
357static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf,
358 size_t count)
359{
360 unsigned int major, minor;
Thomas Maier83f3aa32007-02-10 01:45:11 -0800361 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Thomas Maier32694852006-12-08 02:36:12 -0800362 pkt_setup_dev(MKDEV(major, minor), NULL);
363 return count;
364 }
365 return -EINVAL;
366}
367
368static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf,
369 size_t count)
370{
371 unsigned int major, minor;
Thomas Maier83f3aa32007-02-10 01:45:11 -0800372 if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
Thomas Maier32694852006-12-08 02:36:12 -0800373 pkt_remove_dev(MKDEV(major, minor));
374 return count;
375 }
376 return -EINVAL;
377}
378
379static struct class_attribute class_pktcdvd_attrs[] = {
380 __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
381 __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
382 __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
383 __ATTR_NULL
384};
385
386
387static int pkt_sysfs_init(void)
388{
389 int ret = 0;
390
391 /*
392 * create control files in sysfs
393 * /sys/class/pktcdvd/...
394 */
395 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
396 if (!class_pktcdvd)
397 return -ENOMEM;
398 class_pktcdvd->name = DRIVER_NAME;
399 class_pktcdvd->owner = THIS_MODULE;
400 class_pktcdvd->class_release = class_pktcdvd_release;
401 class_pktcdvd->class_attrs = class_pktcdvd_attrs;
402 ret = class_register(class_pktcdvd);
403 if (ret) {
404 kfree(class_pktcdvd);
405 class_pktcdvd = NULL;
406 printk(DRIVER_NAME": failed to create class pktcdvd\n");
407 return ret;
408 }
409 return 0;
410}
411
412static void pkt_sysfs_cleanup(void)
413{
414 if (class_pktcdvd)
415 class_destroy(class_pktcdvd);
416 class_pktcdvd = NULL;
417}
418
419/********************************************************************
420 entries in debugfs
421
422 /debugfs/pktcdvd[0-7]/
423 info
424
425 *******************************************************************/
426
427static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
428{
429 return pkt_seq_show(m, p);
430}
431
432static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
433{
434 return single_open(file, pkt_debugfs_seq_show, inode->i_private);
435}
436
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800437static const struct file_operations debug_fops = {
Thomas Maier32694852006-12-08 02:36:12 -0800438 .open = pkt_debugfs_fops_open,
439 .read = seq_read,
440 .llseek = seq_lseek,
441 .release = single_release,
442 .owner = THIS_MODULE,
443};
444
445static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
446{
447 if (!pkt_debugfs_root)
448 return;
449 pd->dfs_f_info = NULL;
450 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
451 if (IS_ERR(pd->dfs_d_root)) {
452 pd->dfs_d_root = NULL;
453 return;
454 }
455 pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
456 pd->dfs_d_root, pd, &debug_fops);
457 if (IS_ERR(pd->dfs_f_info)) {
458 pd->dfs_f_info = NULL;
459 return;
460 }
461}
462
463static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
464{
465 if (!pkt_debugfs_root)
466 return;
467 if (pd->dfs_f_info)
468 debugfs_remove(pd->dfs_f_info);
469 pd->dfs_f_info = NULL;
470 if (pd->dfs_d_root)
471 debugfs_remove(pd->dfs_d_root);
472 pd->dfs_d_root = NULL;
473}
474
475static void pkt_debugfs_init(void)
476{
477 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
478 if (IS_ERR(pkt_debugfs_root)) {
479 pkt_debugfs_root = NULL;
480 return;
481 }
482}
483
484static void pkt_debugfs_cleanup(void)
485{
486 if (!pkt_debugfs_root)
487 return;
488 debugfs_remove(pkt_debugfs_root);
489 pkt_debugfs_root = NULL;
490}
491
492/* ----------------------------------------------------------*/
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495static void pkt_bio_finished(struct pktcdvd_device *pd)
496{
497 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
498 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
Thomas Maier78220822006-10-04 02:15:28 -0700499 VPRINTK(DRIVER_NAME": queue empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 atomic_set(&pd->iosched.attention, 1);
501 wake_up(&pd->wqueue);
502 }
503}
504
505static void pkt_bio_destructor(struct bio *bio)
506{
507 kfree(bio->bi_io_vec);
508 kfree(bio);
509}
510
511static struct bio *pkt_bio_alloc(int nr_iovecs)
512{
513 struct bio_vec *bvl = NULL;
514 struct bio *bio;
515
516 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
517 if (!bio)
518 goto no_bio;
519 bio_init(bio);
520
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700521 bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!bvl)
523 goto no_bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 bio->bi_max_vecs = nr_iovecs;
526 bio->bi_io_vec = bvl;
527 bio->bi_destructor = pkt_bio_destructor;
528
529 return bio;
530
531 no_bvl:
532 kfree(bio);
533 no_bio:
534 return NULL;
535}
536
537/*
538 * Allocate a packet_data struct
539 */
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800540static struct packet_data *pkt_alloc_packet_data(int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int i;
543 struct packet_data *pkt;
544
Peter Osterlund1107d2e2005-09-13 01:25:29 -0700545 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!pkt)
547 goto no_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800549 pkt->frames = frames;
550 pkt->w_bio = pkt_bio_alloc(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!pkt->w_bio)
552 goto no_bio;
553
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800554 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
556 if (!pkt->pages[i])
557 goto no_page;
558 }
559
560 spin_lock_init(&pkt->lock);
561
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800562 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct bio *bio = pkt_bio_alloc(1);
564 if (!bio)
565 goto no_rd_bio;
566 pkt->r_bios[i] = bio;
567 }
568
569 return pkt;
570
571no_rd_bio:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800572 for (i = 0; i < frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct bio *bio = pkt->r_bios[i];
574 if (bio)
575 bio_put(bio);
576 }
577
578no_page:
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800579 for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (pkt->pages[i])
581 __free_page(pkt->pages[i]);
582 bio_put(pkt->w_bio);
583no_bio:
584 kfree(pkt);
585no_pkt:
586 return NULL;
587}
588
589/*
590 * Free a packet_data struct
591 */
592static void pkt_free_packet_data(struct packet_data *pkt)
593{
594 int i;
595
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800596 for (i = 0; i < pkt->frames; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct bio *bio = pkt->r_bios[i];
598 if (bio)
599 bio_put(bio);
600 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800601 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 __free_page(pkt->pages[i]);
603 bio_put(pkt->w_bio);
604 kfree(pkt);
605}
606
607static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
608{
609 struct packet_data *pkt, *next;
610
611 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
612
613 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
614 pkt_free_packet_data(pkt);
615 }
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800616 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
620{
621 struct packet_data *pkt;
622
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800623 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 while (nr_packets > 0) {
Peter Osterlunde1bc89b2006-02-04 23:27:47 -0800626 pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!pkt) {
628 pkt_shrink_pktlist(pd);
629 return 0;
630 }
631 pkt->id = nr_packets;
632 pkt->pd = pd;
633 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
634 nr_packets--;
635 }
636 return 1;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
640{
641 struct rb_node *n = rb_next(&node->rb_node);
642 if (!n)
643 return NULL;
644 return rb_entry(n, struct pkt_rb_node, rb_node);
645}
646
Peter Osterlundac893962006-01-14 13:21:32 -0800647static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 rb_erase(&node->rb_node, &pd->bio_queue);
650 mempool_free(node, pd->rb_pool);
651 pd->bio_queue_size--;
652 BUG_ON(pd->bio_queue_size < 0);
653}
654
655/*
656 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
657 */
658static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
659{
660 struct rb_node *n = pd->bio_queue.rb_node;
661 struct rb_node *next;
662 struct pkt_rb_node *tmp;
663
664 if (!n) {
665 BUG_ON(pd->bio_queue_size > 0);
666 return NULL;
667 }
668
669 for (;;) {
670 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
671 if (s <= tmp->bio->bi_sector)
672 next = n->rb_left;
673 else
674 next = n->rb_right;
675 if (!next)
676 break;
677 n = next;
678 }
679
680 if (s > tmp->bio->bi_sector) {
681 tmp = pkt_rbtree_next(tmp);
682 if (!tmp)
683 return NULL;
684 }
685 BUG_ON(s > tmp->bio->bi_sector);
686 return tmp;
687}
688
689/*
690 * Insert a node into the pd->bio_queue rb tree.
691 */
692static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
693{
694 struct rb_node **p = &pd->bio_queue.rb_node;
695 struct rb_node *parent = NULL;
696 sector_t s = node->bio->bi_sector;
697 struct pkt_rb_node *tmp;
698
699 while (*p) {
700 parent = *p;
701 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
702 if (s < tmp->bio->bi_sector)
703 p = &(*p)->rb_left;
704 else
705 p = &(*p)->rb_right;
706 }
707 rb_link_node(&node->rb_node, parent, p);
708 rb_insert_color(&node->rb_node, &pd->bio_queue);
709 pd->bio_queue_size++;
710}
711
712/*
713 * Add a bio to a single linked list defined by its head and tail pointers.
714 */
Peter Osterlundac893962006-01-14 13:21:32 -0800715static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 bio->bi_next = NULL;
718 if (*list_tail) {
719 BUG_ON((*list_head) == NULL);
720 (*list_tail)->bi_next = bio;
721 (*list_tail) = bio;
722 } else {
723 BUG_ON((*list_head) != NULL);
724 (*list_head) = bio;
725 (*list_tail) = bio;
726 }
727}
728
729/*
730 * Remove and return the first bio from a single linked list defined by its
731 * head and tail pointers.
732 */
733static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
734{
735 struct bio *bio;
736
737 if (*list_head == NULL)
738 return NULL;
739
740 bio = *list_head;
741 *list_head = bio->bi_next;
742 if (*list_head == NULL)
743 *list_tail = NULL;
744
745 bio->bi_next = NULL;
746 return bio;
747}
748
749/*
750 * Send a packet_command to the underlying block device and
751 * wait for completion.
752 */
753static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
754{
Jens Axboe165125e2007-07-24 09:28:11 +0200755 struct request_queue *q = bdev_get_queue(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct request *rq;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800757 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800759 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
760 WRITE : READ, __GFP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800762 if (cgc->buflen) {
763 if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
764 goto out;
765 }
766
Gerhard Dirschl91e4ee32007-02-20 13:57:56 -0800767 rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800768 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
769 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
770 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 rq->timeout = 60*HZ;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200773 rq->cmd_type = REQ_TYPE_BLOCK_PC;
774 rq->cmd_flags |= REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (cgc->quiet)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200776 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800778 blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
Andrew Mortoncbc31a42007-04-25 13:01:21 -0700779 if (rq->errors)
780 ret = -EIO;
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800781out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 blk_put_request(rq);
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800783 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786/*
787 * A generic sense dump / resolve mechanism should be implemented across
788 * all ATAPI + SCSI devices.
789 */
790static void pkt_dump_sense(struct packet_command *cgc)
791{
792 static char *info[9] = { "No sense", "Recovered error", "Not ready",
793 "Medium error", "Hardware error", "Illegal request",
794 "Unit attention", "Data protect", "Blank check" };
795 int i;
796 struct request_sense *sense = cgc->sense;
797
Thomas Maier78220822006-10-04 02:15:28 -0700798 printk(DRIVER_NAME":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 for (i = 0; i < CDROM_PACKET_SIZE; i++)
800 printk(" %02x", cgc->cmd[i]);
801 printk(" - ");
802
803 if (sense == NULL) {
804 printk("no sense\n");
805 return;
806 }
807
808 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
809
810 if (sense->sense_key > 8) {
811 printk(" (INVALID)\n");
812 return;
813 }
814
815 printk(" (%s)\n", info[sense->sense_key]);
816}
817
818/*
819 * flush the drive cache to media
820 */
821static int pkt_flush_cache(struct pktcdvd_device *pd)
822{
823 struct packet_command cgc;
824
825 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
826 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
827 cgc.quiet = 1;
828
829 /*
830 * the IMMED bit -- we default to not setting it, although that
831 * would allow a much faster close, this is safer
832 */
833#if 0
834 cgc.cmd[1] = 1 << 1;
835#endif
836 return pkt_generic_packet(pd, &cgc);
837}
838
839/*
840 * speed is given as the normal factor, e.g. 4 for 4x
841 */
842static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
843{
844 struct packet_command cgc;
845 struct request_sense sense;
846 int ret;
847
848 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
849 cgc.sense = &sense;
850 cgc.cmd[0] = GPCMD_SET_SPEED;
851 cgc.cmd[2] = (read_speed >> 8) & 0xff;
852 cgc.cmd[3] = read_speed & 0xff;
853 cgc.cmd[4] = (write_speed >> 8) & 0xff;
854 cgc.cmd[5] = write_speed & 0xff;
855
856 if ((ret = pkt_generic_packet(pd, &cgc)))
857 pkt_dump_sense(&cgc);
858
859 return ret;
860}
861
862/*
863 * Queue a bio for processing by the low-level CD device. Must be called
864 * from process context.
865 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700866static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 spin_lock(&pd->iosched.lock);
869 if (bio_data_dir(bio) == READ) {
870 pkt_add_list_last(bio, &pd->iosched.read_queue,
871 &pd->iosched.read_queue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else {
873 pkt_add_list_last(bio, &pd->iosched.write_queue,
874 &pd->iosched.write_queue_tail);
875 }
876 spin_unlock(&pd->iosched.lock);
877
878 atomic_set(&pd->iosched.attention, 1);
879 wake_up(&pd->wqueue);
880}
881
882/*
883 * Process the queued read/write requests. This function handles special
884 * requirements for CDRW drives:
885 * - A cache flush command must be inserted before a read request if the
886 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700887 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700889 * - Optimize for throughput at the expense of latency. This means that streaming
890 * writes will never be interrupted by a read, but if the drive has to seek
891 * before the next write, switch to reading instead if there are any pending
892 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * - Set the read speed according to current usage pattern. When only reading
894 * from the device, it's best to use the highest possible read speed, but
895 * when switching often between reading and writing, it's better to have the
896 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
898static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
899{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (atomic_read(&pd->iosched.attention) == 0)
902 return;
903 atomic_set(&pd->iosched.attention, 0);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 for (;;) {
906 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700907 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 spin_lock(&pd->iosched.lock);
910 reads_queued = (pd->iosched.read_queue != NULL);
911 writes_queued = (pd->iosched.write_queue != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_unlock(&pd->iosched.lock);
913
914 if (!reads_queued && !writes_queued)
915 break;
916
917 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700918 int need_write_seek = 1;
919 spin_lock(&pd->iosched.lock);
920 bio = pd->iosched.write_queue;
921 spin_unlock(&pd->iosched.lock);
922 if (bio && (bio->bi_sector == pd->iosched.last_write))
923 need_write_seek = 0;
924 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700926 VPRINTK(DRIVER_NAME": write, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
928 }
929 pkt_flush_cache(pd);
930 pd->iosched.writing = 0;
931 }
932 } else {
933 if (!reads_queued && writes_queued) {
934 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
Thomas Maier78220822006-10-04 02:15:28 -0700935 VPRINTK(DRIVER_NAME": read, waiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 break;
937 }
938 pd->iosched.writing = 1;
939 }
940 }
941
942 spin_lock(&pd->iosched.lock);
943 if (pd->iosched.writing) {
944 bio = pkt_get_list_first(&pd->iosched.write_queue,
945 &pd->iosched.write_queue_tail);
946 } else {
947 bio = pkt_get_list_first(&pd->iosched.read_queue,
948 &pd->iosched.read_queue_tail);
949 }
950 spin_unlock(&pd->iosched.lock);
951
952 if (!bio)
953 continue;
954
955 if (bio_data_dir(bio) == READ)
956 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700957 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700959 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
962 if (pd->read_speed == pd->write_speed) {
963 pd->read_speed = MAX_SPEED;
964 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
965 }
966 } else {
967 if (pd->read_speed != pd->write_speed) {
968 pd->read_speed = pd->write_speed;
969 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
970 }
971 }
972
973 atomic_inc(&pd->cdrw.pending_bios);
974 generic_make_request(bio);
975 }
976}
977
978/*
979 * Special care is needed if the underlying block device has a small
980 * max_phys_segments value.
981 */
Jens Axboe165125e2007-07-24 09:28:11 +0200982static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
985 /*
986 * The cdrom device can handle one segment/frame
987 */
988 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
989 return 0;
990 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
991 /*
992 * We can handle this case at the expense of some extra memory
993 * copies during write operations
994 */
995 set_bit(PACKET_MERGE_SEGS, &pd->flags);
996 return 0;
997 } else {
Thomas Maier78220822006-10-04 02:15:28 -0700998 printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -EIO;
1000 }
1001}
1002
1003/*
1004 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
1005 */
1006static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
1007{
1008 unsigned int copy_size = CD_FRAMESIZE;
1009
1010 while (copy_size > 0) {
1011 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
1012 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
1013 src_bvl->bv_offset + offs;
1014 void *vto = page_address(dst_page) + dst_offs;
1015 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
1016
1017 BUG_ON(len < 0);
1018 memcpy(vto, vfrom, len);
1019 kunmap_atomic(vfrom, KM_USER0);
1020
1021 seg++;
1022 offs = 0;
1023 dst_offs += len;
1024 copy_size -= len;
1025 }
1026}
1027
1028/*
1029 * Copy all data for this packet to pkt->pages[], so that
1030 * a) The number of required segments for the write bio is minimized, which
1031 * is necessary for some scsi controllers.
1032 * b) The data can be used as cache to avoid read requests if we receive a
1033 * new write request for the same zone.
1034 */
Peter Osterlund72772322006-02-14 13:52:56 -08001035static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 int f, p, offs;
1038
1039 /* Copy all data to pkt->pages[] */
1040 p = 0;
1041 offs = 0;
1042 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001043 if (bvec[f].bv_page != pkt->pages[p]) {
1044 void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 void *vto = page_address(pkt->pages[p]) + offs;
1046 memcpy(vto, vfrom, CD_FRAMESIZE);
1047 kunmap_atomic(vfrom, KM_USER0);
Peter Osterlund72772322006-02-14 13:52:56 -08001048 bvec[f].bv_page = pkt->pages[p];
1049 bvec[f].bv_offset = offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 } else {
Peter Osterlund72772322006-02-14 13:52:56 -08001051 BUG_ON(bvec[f].bv_offset != offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 offs += CD_FRAMESIZE;
1054 if (offs >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 offs = 0;
1056 p++;
1057 }
1058 }
1059}
1060
NeilBrown6712ecf2007-09-27 12:47:43 +02001061static void pkt_end_io_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct packet_data *pkt = bio->bi_private;
1064 struct pktcdvd_device *pd = pkt->pd;
1065 BUG_ON(!pd);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1068 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1069
1070 if (err)
1071 atomic_inc(&pkt->io_errors);
1072 if (atomic_dec_and_test(&pkt->io_wait)) {
1073 atomic_inc(&pkt->run_sm);
1074 wake_up(&pd->wqueue);
1075 }
1076 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
NeilBrown6712ecf2007-09-27 12:47:43 +02001079static void pkt_end_io_packet_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct packet_data *pkt = bio->bi_private;
1082 struct pktcdvd_device *pd = pkt->pd;
1083 BUG_ON(!pd);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1086
1087 pd->stats.pkt_ended++;
1088
1089 pkt_bio_finished(pd);
1090 atomic_dec(&pkt->io_wait);
1091 atomic_inc(&pkt->run_sm);
1092 wake_up(&pd->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095/*
1096 * Schedule reads for the holes in a packet
1097 */
1098static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1099{
1100 int frames_read = 0;
1101 struct bio *bio;
1102 int f;
1103 char written[PACKET_MAX_SIZE];
1104
1105 BUG_ON(!pkt->orig_bios);
1106
1107 atomic_set(&pkt->io_wait, 0);
1108 atomic_set(&pkt->io_errors, 0);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * Figure out which frames we need to read before we can write.
1112 */
1113 memset(written, 0, sizeof(written));
1114 spin_lock(&pkt->lock);
1115 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1116 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1117 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001118 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 BUG_ON(first_frame < 0);
1120 BUG_ON(first_frame + num_frames > pkt->frames);
1121 for (f = first_frame; f < first_frame + num_frames; f++)
1122 written[f] = 1;
1123 }
1124 spin_unlock(&pkt->lock);
1125
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001126 if (pkt->cache_valid) {
1127 VPRINTK("pkt_gather_data: zone %llx cached\n",
1128 (unsigned long long)pkt->sector);
1129 goto out_account;
1130 }
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /*
1133 * Schedule reads for missing parts of the packet.
1134 */
1135 for (f = 0; f < pkt->frames; f++) {
Jens Axboe761a15e2007-09-14 13:06:53 +02001136 struct bio_vec *vec;
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int p, offset;
1139 if (written[f])
1140 continue;
1141 bio = pkt->r_bios[f];
Jens Axboe761a15e2007-09-14 13:06:53 +02001142 vec = bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bio_init(bio);
1144 bio->bi_max_vecs = 1;
1145 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1146 bio->bi_bdev = pd->bdev;
1147 bio->bi_end_io = pkt_end_io_read;
1148 bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001149 bio->bi_io_vec = vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1152 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1153 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1154 f, pkt->pages[p], offset);
1155 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1156 BUG();
1157
1158 atomic_inc(&pkt->io_wait);
1159 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001160 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 frames_read++;
1162 }
1163
1164out_account:
1165 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1166 frames_read, (unsigned long long)pkt->sector);
1167 pd->stats.pkt_started++;
1168 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171/*
1172 * Find a packet matching zone, or the least recently used packet if
1173 * there is no match.
1174 */
1175static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1176{
1177 struct packet_data *pkt;
1178
1179 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1180 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1181 list_del_init(&pkt->list);
1182 if (pkt->sector != zone)
1183 pkt->cache_valid = 0;
Peter Osterlund610827d2005-09-13 01:25:29 -07001184 return pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 }
Peter Osterlund610827d2005-09-13 01:25:29 -07001187 BUG();
1188 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1192{
1193 if (pkt->cache_valid) {
1194 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1195 } else {
1196 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1197 }
1198}
1199
1200/*
1201 * recover a failed write, query for relocation if possible
1202 *
1203 * returns 1 if recovery is possible, or 0 if not
1204 *
1205 */
1206static int pkt_start_recovery(struct packet_data *pkt)
1207{
1208 /*
1209 * FIXME. We need help from the file system to implement
1210 * recovery handling.
1211 */
1212 return 0;
1213#if 0
1214 struct request *rq = pkt->rq;
1215 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1216 struct block_device *pkt_bdev;
1217 struct super_block *sb = NULL;
1218 unsigned long old_block, new_block;
1219 sector_t new_sector;
1220
1221 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1222 if (pkt_bdev) {
1223 sb = get_super(pkt_bdev);
1224 bdput(pkt_bdev);
1225 }
1226
1227 if (!sb)
1228 return 0;
1229
1230 if (!sb->s_op || !sb->s_op->relocate_blocks)
1231 goto out;
1232
1233 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1234 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1235 goto out;
1236
1237 new_sector = new_block * (CD_FRAMESIZE >> 9);
1238 pkt->sector = new_sector;
1239
1240 pkt->bio->bi_sector = new_sector;
1241 pkt->bio->bi_next = NULL;
1242 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1243 pkt->bio->bi_idx = 0;
1244
1245 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1246 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1247 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1248 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1249 BUG_ON(pkt->bio->bi_private != pkt);
1250
1251 drop_super(sb);
1252 return 1;
1253
1254out:
1255 drop_super(sb);
1256 return 0;
1257#endif
1258}
1259
1260static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1261{
1262#if PACKET_DEBUG > 1
1263 static const char *state_name[] = {
1264 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1265 };
1266 enum packet_data_state old_state = pkt->state;
1267 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1268 state_name[old_state], state_name[state]);
1269#endif
1270 pkt->state = state;
1271}
1272
1273/*
1274 * Scan the work queue to see if we can start a new packet.
1275 * returns non-zero if any work was done.
1276 */
1277static int pkt_handle_queue(struct pktcdvd_device *pd)
1278{
1279 struct packet_data *pkt, *p;
1280 struct bio *bio = NULL;
1281 sector_t zone = 0; /* Suppress gcc warning */
1282 struct pkt_rb_node *node, *first_node;
1283 struct rb_node *n;
Thomas Maier0a0fc962006-12-08 02:36:11 -08001284 int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 VPRINTK("handle_queue\n");
1287
1288 atomic_set(&pd->scan_queue, 0);
1289
1290 if (list_empty(&pd->cdrw.pkt_free_list)) {
1291 VPRINTK("handle_queue: no pkt\n");
1292 return 0;
1293 }
1294
1295 /*
1296 * Try to find a zone we are not already working on.
1297 */
1298 spin_lock(&pd->lock);
1299 first_node = pkt_rbtree_find(pd, pd->current_sector);
1300 if (!first_node) {
1301 n = rb_first(&pd->bio_queue);
1302 if (n)
1303 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1304 }
1305 node = first_node;
1306 while (node) {
1307 bio = node->bio;
1308 zone = ZONE(bio->bi_sector, pd);
1309 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001310 if (p->sector == zone) {
1311 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 break;
1316try_next_bio:
1317 node = pkt_rbtree_next(node);
1318 if (!node) {
1319 n = rb_first(&pd->bio_queue);
1320 if (n)
1321 node = rb_entry(n, struct pkt_rb_node, rb_node);
1322 }
1323 if (node == first_node)
1324 node = NULL;
1325 }
1326 spin_unlock(&pd->lock);
1327 if (!bio) {
1328 VPRINTK("handle_queue: no bio\n");
1329 return 0;
1330 }
1331
1332 pkt = pkt_get_packet_data(pd, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 pd->current_sector = zone + pd->settings.size;
1335 pkt->sector = zone;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001336 BUG_ON(pkt->frames != pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 pkt->write_size = 0;
1338
1339 /*
1340 * Scan work queue for bios in the same zone and link them
1341 * to this packet.
1342 */
1343 spin_lock(&pd->lock);
1344 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1345 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1346 bio = node->bio;
1347 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1348 (unsigned long long)ZONE(bio->bi_sector, pd));
1349 if (ZONE(bio->bi_sector, pd) != zone)
1350 break;
1351 pkt_rbtree_erase(pd, node);
1352 spin_lock(&pkt->lock);
1353 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1354 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1355 spin_unlock(&pkt->lock);
1356 }
Thomas Maier0a0fc962006-12-08 02:36:11 -08001357 /* check write congestion marks, and if bio_queue_size is
1358 below, wake up any waiters */
1359 wakeup = (pd->write_congestion_on > 0
1360 && pd->bio_queue_size <= pd->write_congestion_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_unlock(&pd->lock);
Thomas Maier0a0fc962006-12-08 02:36:11 -08001362 if (wakeup)
Thomas Maier83f3aa32007-02-10 01:45:11 -08001363 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1366 pkt_set_state(pkt, PACKET_WAITING_STATE);
1367 atomic_set(&pkt->run_sm, 1);
1368
1369 spin_lock(&pd->cdrw.active_list_lock);
1370 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1371 spin_unlock(&pd->cdrw.active_list_lock);
1372
1373 return 1;
1374}
1375
1376/*
1377 * Assemble a bio to write one packet and queue the bio for processing
1378 * by the underlying block device.
1379 */
1380static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1381{
1382 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 int f;
1384 int frames_write;
Peter Osterlund72772322006-02-14 13:52:56 -08001385 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001388 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1389 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 /*
Peter Osterlund72772322006-02-14 13:52:56 -08001393 * Fill-in bvec with data from orig_bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
1395 frames_write = 0;
1396 spin_lock(&pkt->lock);
1397 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1398 int segment = bio->bi_idx;
1399 int src_offs = 0;
1400 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1401 int num_frames = bio->bi_size / CD_FRAMESIZE;
1402 BUG_ON(first_frame < 0);
1403 BUG_ON(first_frame + num_frames > pkt->frames);
1404 for (f = first_frame; f < first_frame + num_frames; f++) {
1405 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1406
1407 while (src_offs >= src_bvl->bv_len) {
1408 src_offs -= src_bvl->bv_len;
1409 segment++;
1410 BUG_ON(segment >= bio->bi_vcnt);
1411 src_bvl = bio_iovec_idx(bio, segment);
1412 }
1413
1414 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
Peter Osterlund72772322006-02-14 13:52:56 -08001415 bvec[f].bv_page = src_bvl->bv_page;
1416 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 } else {
1418 pkt_copy_bio_data(bio, segment, src_offs,
Peter Osterlund72772322006-02-14 13:52:56 -08001419 bvec[f].bv_page, bvec[f].bv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 src_offs += CD_FRAMESIZE;
1422 frames_write++;
1423 }
1424 }
1425 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1426 spin_unlock(&pkt->lock);
1427
1428 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1429 frames_write, (unsigned long long)pkt->sector);
1430 BUG_ON(frames_write != pkt->write_size);
1431
1432 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
Peter Osterlund72772322006-02-14 13:52:56 -08001433 pkt_make_local_copy(pkt, bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 pkt->cache_valid = 1;
1435 } else {
1436 pkt->cache_valid = 0;
1437 }
1438
1439 /* Start the write request */
1440 bio_init(pkt->w_bio);
1441 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1442 pkt->w_bio->bi_sector = pkt->sector;
1443 pkt->w_bio->bi_bdev = pd->bdev;
1444 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1445 pkt->w_bio->bi_private = pkt;
Jens Axboe761a15e2007-09-14 13:06:53 +02001446 pkt->w_bio->bi_io_vec = bvec;
Peter Osterlund72772322006-02-14 13:52:56 -08001447 for (f = 0; f < pkt->frames; f++)
1448 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1449 BUG();
Thomas Maier78220822006-10-04 02:15:28 -07001450 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 atomic_set(&pkt->io_wait, 1);
1453 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001454 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
1457static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1458{
1459 struct bio *bio, *next;
1460
1461 if (!uptodate)
1462 pkt->cache_valid = 0;
1463
1464 /* Finish all bios corresponding to this packet */
1465 bio = pkt->orig_bios;
1466 while (bio) {
1467 next = bio->bi_next;
1468 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001469 bio_endio(bio, uptodate ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 bio = next;
1471 }
1472 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1473}
1474
1475static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1476{
1477 int uptodate;
1478
1479 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1480
1481 for (;;) {
1482 switch (pkt->state) {
1483 case PACKET_WAITING_STATE:
1484 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1485 return;
1486
1487 pkt->sleep_time = 0;
1488 pkt_gather_data(pd, pkt);
1489 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1490 break;
1491
1492 case PACKET_READ_WAIT_STATE:
1493 if (atomic_read(&pkt->io_wait) > 0)
1494 return;
1495
1496 if (atomic_read(&pkt->io_errors) > 0) {
1497 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1498 } else {
1499 pkt_start_write(pd, pkt);
1500 }
1501 break;
1502
1503 case PACKET_WRITE_WAIT_STATE:
1504 if (atomic_read(&pkt->io_wait) > 0)
1505 return;
1506
1507 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1508 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1509 } else {
1510 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1511 }
1512 break;
1513
1514 case PACKET_RECOVERY_STATE:
1515 if (pkt_start_recovery(pkt)) {
1516 pkt_start_write(pd, pkt);
1517 } else {
1518 VPRINTK("No recovery possible\n");
1519 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1520 }
1521 break;
1522
1523 case PACKET_FINISHED_STATE:
1524 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1525 pkt_finish_packet(pkt, uptodate);
1526 return;
1527
1528 default:
1529 BUG();
1530 break;
1531 }
1532 }
1533}
1534
1535static void pkt_handle_packets(struct pktcdvd_device *pd)
1536{
1537 struct packet_data *pkt, *next;
1538
1539 VPRINTK("pkt_handle_packets\n");
1540
1541 /*
1542 * Run state machine for active packets
1543 */
1544 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1545 if (atomic_read(&pkt->run_sm) > 0) {
1546 atomic_set(&pkt->run_sm, 0);
1547 pkt_run_state_machine(pd, pkt);
1548 }
1549 }
1550
1551 /*
1552 * Move no longer active packets to the free list
1553 */
1554 spin_lock(&pd->cdrw.active_list_lock);
1555 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1556 if (pkt->state == PACKET_FINISHED_STATE) {
1557 list_del(&pkt->list);
1558 pkt_put_packet_data(pd, pkt);
1559 pkt_set_state(pkt, PACKET_IDLE_STATE);
1560 atomic_set(&pd->scan_queue, 1);
1561 }
1562 }
1563 spin_unlock(&pd->cdrw.active_list_lock);
1564}
1565
1566static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1567{
1568 struct packet_data *pkt;
1569 int i;
1570
Peter Osterlundae7642b2005-11-13 16:06:36 -08001571 for (i = 0; i < PACKET_NUM_STATES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 states[i] = 0;
1573
1574 spin_lock(&pd->cdrw.active_list_lock);
1575 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1576 states[pkt->state]++;
1577 }
1578 spin_unlock(&pd->cdrw.active_list_lock);
1579}
1580
1581/*
1582 * kcdrwd is woken up when writes have been queued for one of our
1583 * registered devices
1584 */
1585static int kcdrwd(void *foobar)
1586{
1587 struct pktcdvd_device *pd = foobar;
1588 struct packet_data *pkt;
1589 long min_sleep_time, residue;
1590
1591 set_user_nice(current, -20);
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001592 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 for (;;) {
1595 DECLARE_WAITQUEUE(wait, current);
1596
1597 /*
1598 * Wait until there is something to do
1599 */
1600 add_wait_queue(&pd->wqueue, &wait);
1601 for (;;) {
1602 set_current_state(TASK_INTERRUPTIBLE);
1603
1604 /* Check if we need to run pkt_handle_queue */
1605 if (atomic_read(&pd->scan_queue) > 0)
1606 goto work_to_do;
1607
1608 /* Check if we need to run the state machine for some packet */
1609 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1610 if (atomic_read(&pkt->run_sm) > 0)
1611 goto work_to_do;
1612 }
1613
1614 /* Check if we need to process the iosched queues */
1615 if (atomic_read(&pd->iosched.attention) != 0)
1616 goto work_to_do;
1617
1618 /* Otherwise, go to sleep */
1619 if (PACKET_DEBUG > 1) {
1620 int states[PACKET_NUM_STATES];
1621 pkt_count_states(pd, states);
1622 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1623 states[0], states[1], states[2], states[3],
1624 states[4], states[5]);
1625 }
1626
1627 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1628 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1629 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1630 min_sleep_time = pkt->sleep_time;
1631 }
1632
1633 generic_unplug_device(bdev_get_queue(pd->bdev));
1634
1635 VPRINTK("kcdrwd: sleeping\n");
1636 residue = schedule_timeout(min_sleep_time);
1637 VPRINTK("kcdrwd: wake up\n");
1638
1639 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001640 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1643 if (!pkt->sleep_time)
1644 continue;
1645 pkt->sleep_time -= min_sleep_time - residue;
1646 if (pkt->sleep_time <= 0) {
1647 pkt->sleep_time = 0;
1648 atomic_inc(&pkt->run_sm);
1649 }
1650 }
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (kthread_should_stop())
1653 break;
1654 }
1655work_to_do:
1656 set_current_state(TASK_RUNNING);
1657 remove_wait_queue(&pd->wqueue, &wait);
1658
1659 if (kthread_should_stop())
1660 break;
1661
1662 /*
1663 * if pkt_handle_queue returns true, we can queue
1664 * another request.
1665 */
1666 while (pkt_handle_queue(pd))
1667 ;
1668
1669 /*
1670 * Handle packet state machine
1671 */
1672 pkt_handle_packets(pd);
1673
1674 /*
1675 * Handle iosched queues
1676 */
1677 pkt_iosched_process_queue(pd);
1678 }
1679
1680 return 0;
1681}
1682
1683static void pkt_print_settings(struct pktcdvd_device *pd)
1684{
Thomas Maier78220822006-10-04 02:15:28 -07001685 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 printk("%u blocks, ", pd->settings.size >> 2);
1687 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1688}
1689
1690static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1691{
1692 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1693
1694 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1695 cgc->cmd[2] = page_code | (page_control << 6);
1696 cgc->cmd[7] = cgc->buflen >> 8;
1697 cgc->cmd[8] = cgc->buflen & 0xff;
1698 cgc->data_direction = CGC_DATA_READ;
1699 return pkt_generic_packet(pd, cgc);
1700}
1701
1702static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1703{
1704 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1705 memset(cgc->buffer, 0, 2);
1706 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1707 cgc->cmd[1] = 0x10; /* PF */
1708 cgc->cmd[7] = cgc->buflen >> 8;
1709 cgc->cmd[8] = cgc->buflen & 0xff;
1710 cgc->data_direction = CGC_DATA_WRITE;
1711 return pkt_generic_packet(pd, cgc);
1712}
1713
1714static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1715{
1716 struct packet_command cgc;
1717 int ret;
1718
1719 /* set up command and get the disc info */
1720 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1721 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1722 cgc.cmd[8] = cgc.buflen = 2;
1723 cgc.quiet = 1;
1724
1725 if ((ret = pkt_generic_packet(pd, &cgc)))
1726 return ret;
1727
1728 /* not all drives have the same disc_info length, so requeue
1729 * packet with the length the drive tells us it can supply
1730 */
1731 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1732 sizeof(di->disc_information_length);
1733
1734 if (cgc.buflen > sizeof(disc_information))
1735 cgc.buflen = sizeof(disc_information);
1736
1737 cgc.cmd[8] = cgc.buflen;
1738 return pkt_generic_packet(pd, &cgc);
1739}
1740
1741static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1742{
1743 struct packet_command cgc;
1744 int ret;
1745
1746 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1747 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1748 cgc.cmd[1] = type & 3;
1749 cgc.cmd[4] = (track & 0xff00) >> 8;
1750 cgc.cmd[5] = track & 0xff;
1751 cgc.cmd[8] = 8;
1752 cgc.quiet = 1;
1753
1754 if ((ret = pkt_generic_packet(pd, &cgc)))
1755 return ret;
1756
1757 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1758 sizeof(ti->track_information_length);
1759
1760 if (cgc.buflen > sizeof(track_information))
1761 cgc.buflen = sizeof(track_information);
1762
1763 cgc.cmd[8] = cgc.buflen;
1764 return pkt_generic_packet(pd, &cgc);
1765}
1766
1767static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1768{
1769 disc_information di;
1770 track_information ti;
1771 __u32 last_track;
1772 int ret = -1;
1773
1774 if ((ret = pkt_get_disc_info(pd, &di)))
1775 return ret;
1776
1777 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1778 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1779 return ret;
1780
1781 /* if this track is blank, try the previous. */
1782 if (ti.blank) {
1783 last_track--;
1784 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1785 return ret;
1786 }
1787
1788 /* if last recorded field is valid, return it. */
1789 if (ti.lra_v) {
1790 *last_written = be32_to_cpu(ti.last_rec_address);
1791 } else {
1792 /* make it up instead */
1793 *last_written = be32_to_cpu(ti.track_start) +
1794 be32_to_cpu(ti.track_size);
1795 if (ti.free_blocks)
1796 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1797 }
1798 return 0;
1799}
1800
1801/*
1802 * write mode select package based on pd->settings
1803 */
1804static int pkt_set_write_settings(struct pktcdvd_device *pd)
1805{
1806 struct packet_command cgc;
1807 struct request_sense sense;
1808 write_param_page *wp;
1809 char buffer[128];
1810 int ret, size;
1811
1812 /* doesn't apply to DVD+RW or DVD-RAM */
1813 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1814 return 0;
1815
1816 memset(buffer, 0, sizeof(buffer));
1817 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1818 cgc.sense = &sense;
1819 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1820 pkt_dump_sense(&cgc);
1821 return ret;
1822 }
1823
1824 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1825 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1826 if (size > sizeof(buffer))
1827 size = sizeof(buffer);
1828
1829 /*
1830 * now get it all
1831 */
1832 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1833 cgc.sense = &sense;
1834 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1835 pkt_dump_sense(&cgc);
1836 return ret;
1837 }
1838
1839 /*
1840 * write page is offset header + block descriptor length
1841 */
1842 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1843
1844 wp->fp = pd->settings.fp;
1845 wp->track_mode = pd->settings.track_mode;
1846 wp->write_type = pd->settings.write_type;
1847 wp->data_block_type = pd->settings.block_mode;
1848
1849 wp->multi_session = 0;
1850
1851#ifdef PACKET_USE_LS
1852 wp->link_size = 7;
1853 wp->ls_v = 1;
1854#endif
1855
1856 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1857 wp->session_format = 0;
1858 wp->subhdr2 = 0x20;
1859 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1860 wp->session_format = 0x20;
1861 wp->subhdr2 = 8;
1862#if 0
1863 wp->mcn[0] = 0x80;
1864 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1865#endif
1866 } else {
1867 /*
1868 * paranoia
1869 */
Thomas Maier78220822006-10-04 02:15:28 -07001870 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return 1;
1872 }
1873 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1874
1875 cgc.buflen = cgc.cmd[8] = size;
1876 if ((ret = pkt_mode_select(pd, &cgc))) {
1877 pkt_dump_sense(&cgc);
1878 return ret;
1879 }
1880
1881 pkt_print_settings(pd);
1882 return 0;
1883}
1884
1885/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001886 * 1 -- we can write to this track, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001888static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Peter Osterlundab863ec2006-02-20 18:28:04 -08001890 switch (pd->mmc3_profile) {
1891 case 0x1a: /* DVD+RW */
1892 case 0x12: /* DVD-RAM */
1893 /* The track is always writable on DVD+RW/DVD-RAM */
1894 return 1;
1895 default:
1896 break;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Peter Osterlundab863ec2006-02-20 18:28:04 -08001899 if (!ti->packet || !ti->fp)
1900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 /*
1903 * "good" settings as per Mt Fuji.
1904 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001905 if (ti->rt == 0 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001906 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Peter Osterlundab863ec2006-02-20 18:28:04 -08001908 if (ti->rt == 0 && ti->blank == 1)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001909 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Peter Osterlundab863ec2006-02-20 18:28:04 -08001911 if (ti->rt == 1 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001912 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Thomas Maier78220822006-10-04 02:15:28 -07001914 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
1918/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001919 * 1 -- we can write to this disc, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001921static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 switch (pd->mmc3_profile) {
1924 case 0x0a: /* CD-RW */
1925 case 0xffff: /* MMC3 not supported */
1926 break;
1927 case 0x1a: /* DVD+RW */
1928 case 0x13: /* DVD-RW */
1929 case 0x12: /* DVD-RAM */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001930 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 default:
Thomas Maier78220822006-10-04 02:15:28 -07001932 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935
1936 /*
1937 * for disc type 0xff we should probably reserve a new track.
1938 * but i'm not sure, should we leave this to user apps? probably.
1939 */
1940 if (di->disc_type == 0xff) {
Thomas Maier78220822006-10-04 02:15:28 -07001941 printk(DRIVER_NAME": Unknown disc. No track?\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
1945 if (di->disc_type != 0x20 && di->disc_type != 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001946 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
1950 if (di->erasable == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001951 printk(DRIVER_NAME": Disc not erasable\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
1955 if (di->border_status == PACKET_SESSION_RESERVED) {
Thomas Maier78220822006-10-04 02:15:28 -07001956 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
Peter Osterlund7c613d52006-02-20 18:28:02 -08001960 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963static int pkt_probe_settings(struct pktcdvd_device *pd)
1964{
1965 struct packet_command cgc;
1966 unsigned char buf[12];
1967 disc_information di;
1968 track_information ti;
1969 int ret, track;
1970
1971 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1972 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1973 cgc.cmd[8] = 8;
1974 ret = pkt_generic_packet(pd, &cgc);
1975 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1976
1977 memset(&di, 0, sizeof(disc_information));
1978 memset(&ti, 0, sizeof(track_information));
1979
1980 if ((ret = pkt_get_disc_info(pd, &di))) {
1981 printk("failed get_disc\n");
1982 return ret;
1983 }
1984
Peter Osterlund7c613d52006-02-20 18:28:02 -08001985 if (!pkt_writable_disc(pd, &di))
Peter Osterlund9db91542006-02-20 18:28:04 -08001986 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1989
1990 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1991 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
Thomas Maier78220822006-10-04 02:15:28 -07001992 printk(DRIVER_NAME": failed get_track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return ret;
1994 }
1995
Peter Osterlundab863ec2006-02-20 18:28:04 -08001996 if (!pkt_writable_track(pd, &ti)) {
Thomas Maier78220822006-10-04 02:15:28 -07001997 printk(DRIVER_NAME": can't write to this track\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08001998 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000
2001 /*
2002 * we keep packet size in 512 byte units, makes it easier to
2003 * deal with request calculations.
2004 */
2005 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2006 if (pd->settings.size == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07002007 printk(DRIVER_NAME": detected zero packet size!\n");
Phillip Susia460ad62006-02-04 23:27:44 -08002008 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07002010 if (pd->settings.size > PACKET_MAX_SECTORS) {
Thomas Maier78220822006-10-04 02:15:28 -07002011 printk(DRIVER_NAME": packet size is too big\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002012 return -EROFS;
Peter Osterlundd0272e72005-09-13 01:25:27 -07002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 pd->settings.fp = ti.fp;
2015 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2016
2017 if (ti.nwa_v) {
2018 pd->nwa = be32_to_cpu(ti.next_writable);
2019 set_bit(PACKET_NWA_VALID, &pd->flags);
2020 }
2021
2022 /*
2023 * in theory we could use lra on -RW media as well and just zero
2024 * blocks that haven't been written yet, but in practice that
2025 * is just a no-go. we'll use that for -R, naturally.
2026 */
2027 if (ti.lra_v) {
2028 pd->lra = be32_to_cpu(ti.last_rec_address);
2029 set_bit(PACKET_LRA_VALID, &pd->flags);
2030 } else {
2031 pd->lra = 0xffffffff;
2032 set_bit(PACKET_LRA_VALID, &pd->flags);
2033 }
2034
2035 /*
2036 * fine for now
2037 */
2038 pd->settings.link_loss = 7;
2039 pd->settings.write_type = 0; /* packet */
2040 pd->settings.track_mode = ti.track_mode;
2041
2042 /*
2043 * mode1 or mode2 disc
2044 */
2045 switch (ti.data_mode) {
2046 case PACKET_MODE1:
2047 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2048 break;
2049 case PACKET_MODE2:
2050 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2051 break;
2052 default:
Thomas Maier78220822006-10-04 02:15:28 -07002053 printk(DRIVER_NAME": unknown data mode\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002054 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 return 0;
2057}
2058
2059/*
2060 * enable/disable write caching on drive
2061 */
2062static int pkt_write_caching(struct pktcdvd_device *pd, int set)
2063{
2064 struct packet_command cgc;
2065 struct request_sense sense;
2066 unsigned char buf[64];
2067 int ret;
2068
2069 memset(buf, 0, sizeof(buf));
2070 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2071 cgc.sense = &sense;
2072 cgc.buflen = pd->mode_offset + 12;
2073
2074 /*
2075 * caching mode page might not be there, so quiet this command
2076 */
2077 cgc.quiet = 1;
2078
2079 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2080 return ret;
2081
2082 buf[pd->mode_offset + 10] |= (!!set << 2);
2083
2084 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2085 ret = pkt_mode_select(pd, &cgc);
2086 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07002087 printk(DRIVER_NAME": write caching control failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 pkt_dump_sense(&cgc);
2089 } else if (!ret && set)
Thomas Maier78220822006-10-04 02:15:28 -07002090 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return ret;
2092}
2093
2094static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2095{
2096 struct packet_command cgc;
2097
2098 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2099 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2100 cgc.cmd[4] = lockflag ? 1 : 0;
2101 return pkt_generic_packet(pd, &cgc);
2102}
2103
2104/*
2105 * Returns drive maximum write speed
2106 */
2107static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
2108{
2109 struct packet_command cgc;
2110 struct request_sense sense;
2111 unsigned char buf[256+18];
2112 unsigned char *cap_buf;
2113 int ret, offset;
2114
2115 memset(buf, 0, sizeof(buf));
2116 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2117 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2118 cgc.sense = &sense;
2119
2120 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2121 if (ret) {
2122 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2123 sizeof(struct mode_page_header);
2124 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2125 if (ret) {
2126 pkt_dump_sense(&cgc);
2127 return ret;
2128 }
2129 }
2130
2131 offset = 20; /* Obsoleted field, used by older drives */
2132 if (cap_buf[1] >= 28)
2133 offset = 28; /* Current write speed selected */
2134 if (cap_buf[1] >= 30) {
2135 /* If the drive reports at least one "Logical Unit Write
2136 * Speed Performance Descriptor Block", use the information
2137 * in the first block. (contains the highest speed)
2138 */
2139 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2140 if (num_spdb > 0)
2141 offset = 34;
2142 }
2143
2144 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2145 return 0;
2146}
2147
2148/* These tables from cdrecord - I don't have orange book */
2149/* standard speed CD-RW (1-4x) */
2150static char clv_to_speed[16] = {
2151 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2152 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2153};
2154/* high speed CD-RW (-10x) */
2155static char hs_clv_to_speed[16] = {
2156 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2157 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2158};
2159/* ultra high speed CD-RW */
2160static char us_clv_to_speed[16] = {
2161 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2162 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2163};
2164
2165/*
2166 * reads the maximum media speed from ATIP
2167 */
2168static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
2169{
2170 struct packet_command cgc;
2171 struct request_sense sense;
2172 unsigned char buf[64];
2173 unsigned int size, st, sp;
2174 int ret;
2175
2176 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2177 cgc.sense = &sense;
2178 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2179 cgc.cmd[1] = 2;
2180 cgc.cmd[2] = 4; /* READ ATIP */
2181 cgc.cmd[8] = 2;
2182 ret = pkt_generic_packet(pd, &cgc);
2183 if (ret) {
2184 pkt_dump_sense(&cgc);
2185 return ret;
2186 }
2187 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2188 if (size > sizeof(buf))
2189 size = sizeof(buf);
2190
2191 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2192 cgc.sense = &sense;
2193 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2194 cgc.cmd[1] = 2;
2195 cgc.cmd[2] = 4;
2196 cgc.cmd[8] = size;
2197 ret = pkt_generic_packet(pd, &cgc);
2198 if (ret) {
2199 pkt_dump_sense(&cgc);
2200 return ret;
2201 }
2202
2203 if (!buf[6] & 0x40) {
Thomas Maier78220822006-10-04 02:15:28 -07002204 printk(DRIVER_NAME": Disc type is not CD-RW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return 1;
2206 }
2207 if (!buf[6] & 0x4) {
Thomas Maier78220822006-10-04 02:15:28 -07002208 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return 1;
2210 }
2211
2212 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2213
2214 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2215
2216 /* Info from cdrecord */
2217 switch (st) {
2218 case 0: /* standard speed */
2219 *speed = clv_to_speed[sp];
2220 break;
2221 case 1: /* high speed */
2222 *speed = hs_clv_to_speed[sp];
2223 break;
2224 case 2: /* ultra high speed */
2225 *speed = us_clv_to_speed[sp];
2226 break;
2227 default:
Thomas Maier78220822006-10-04 02:15:28 -07002228 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 1;
2230 }
2231 if (*speed) {
Thomas Maier78220822006-10-04 02:15:28 -07002232 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 0;
2234 } else {
Thomas Maier78220822006-10-04 02:15:28 -07002235 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 1;
2237 }
2238}
2239
2240static int pkt_perform_opc(struct pktcdvd_device *pd)
2241{
2242 struct packet_command cgc;
2243 struct request_sense sense;
2244 int ret;
2245
Thomas Maier78220822006-10-04 02:15:28 -07002246 VPRINTK(DRIVER_NAME": Performing OPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2249 cgc.sense = &sense;
2250 cgc.timeout = 60*HZ;
2251 cgc.cmd[0] = GPCMD_SEND_OPC;
2252 cgc.cmd[1] = 1;
2253 if ((ret = pkt_generic_packet(pd, &cgc)))
2254 pkt_dump_sense(&cgc);
2255 return ret;
2256}
2257
2258static int pkt_open_write(struct pktcdvd_device *pd)
2259{
2260 int ret;
2261 unsigned int write_speed, media_write_speed, read_speed;
2262
2263 if ((ret = pkt_probe_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002264 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
Peter Osterlund9db91542006-02-20 18:28:04 -08002265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
2268 if ((ret = pkt_set_write_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002269 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return -EIO;
2271 }
2272
2273 pkt_write_caching(pd, USE_WCACHING);
2274
2275 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2276 write_speed = 16 * 177;
2277 switch (pd->mmc3_profile) {
2278 case 0x13: /* DVD-RW */
2279 case 0x1a: /* DVD+RW */
2280 case 0x12: /* DVD-RAM */
Thomas Maier78220822006-10-04 02:15:28 -07002281 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 break;
2283 default:
2284 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2285 media_write_speed = 16;
2286 write_speed = min(write_speed, media_write_speed * 177);
Thomas Maier78220822006-10-04 02:15:28 -07002287 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 break;
2289 }
2290 read_speed = write_speed;
2291
2292 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
Thomas Maier78220822006-10-04 02:15:28 -07002293 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return -EIO;
2295 }
2296 pd->write_speed = write_speed;
2297 pd->read_speed = read_speed;
2298
2299 if ((ret = pkt_perform_opc(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002300 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
2303 return 0;
2304}
2305
2306/*
2307 * called at open time.
2308 */
2309static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2310{
2311 int ret;
2312 long lba;
Jens Axboe165125e2007-07-24 09:28:11 +02002313 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 /*
2316 * We need to re-open the cdrom device without O_NONBLOCK to be able
2317 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2318 * so bdget() can't fail.
2319 */
2320 bdget(pd->bdev->bd_dev);
2321 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2322 goto out;
2323
Peter Osterlund8382bf22006-01-08 01:02:17 -08002324 if ((ret = bd_claim(pd->bdev, pd)))
2325 goto out_putdev;
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if ((ret = pkt_get_last_written(pd, &lba))) {
Thomas Maier78220822006-10-04 02:15:28 -07002328 printk(DRIVER_NAME": pkt_get_last_written failed\n");
Peter Osterlund8382bf22006-01-08 01:02:17 -08002329 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331
2332 set_capacity(pd->disk, lba << 2);
2333 set_capacity(pd->bdev->bd_disk, lba << 2);
2334 bd_set_size(pd->bdev, (loff_t)lba << 11);
2335
2336 q = bdev_get_queue(pd->bdev);
2337 if (write) {
2338 if ((ret = pkt_open_write(pd)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002339 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /*
2341 * Some CDRW drives can not handle writes larger than one packet,
2342 * even if the size is a multiple of the packet size.
2343 */
2344 spin_lock_irq(q->queue_lock);
2345 blk_queue_max_sectors(q, pd->settings.size);
2346 spin_unlock_irq(q->queue_lock);
2347 set_bit(PACKET_WRITABLE, &pd->flags);
2348 } else {
2349 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2350 clear_bit(PACKET_WRITABLE, &pd->flags);
2351 }
2352
2353 if ((ret = pkt_set_segment_merging(pd, q)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002354 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002356 if (write) {
2357 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
Thomas Maier78220822006-10-04 02:15:28 -07002358 printk(DRIVER_NAME": not enough memory for buffers\n");
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002359 ret = -ENOMEM;
2360 goto out_unclaim;
2361 }
Thomas Maier78220822006-10-04 02:15:28 -07002362 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 return 0;
2366
Peter Osterlund8382bf22006-01-08 01:02:17 -08002367out_unclaim:
2368 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369out_putdev:
2370 blkdev_put(pd->bdev);
2371out:
2372 return ret;
2373}
2374
2375/*
2376 * called when the device is closed. makes sure that the device flushes
2377 * the internal cache before we close.
2378 */
2379static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2380{
2381 if (flush && pkt_flush_cache(pd))
Thomas Maier78220822006-10-04 02:15:28 -07002382 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 pkt_lock_door(pd, 0);
2385
2386 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
Peter Osterlund8382bf22006-01-08 01:02:17 -08002387 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 blkdev_put(pd->bdev);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002389
2390 pkt_shrink_pktlist(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2394{
2395 if (dev_minor >= MAX_WRITERS)
2396 return NULL;
2397 return pkt_devs[dev_minor];
2398}
2399
2400static int pkt_open(struct inode *inode, struct file *file)
2401{
2402 struct pktcdvd_device *pd = NULL;
2403 int ret;
2404
Thomas Maier78220822006-10-04 02:15:28 -07002405 VPRINTK(DRIVER_NAME": entering open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Jes Sorensen1657f822006-03-23 03:00:25 -08002407 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 pd = pkt_find_dev_from_minor(iminor(inode));
2409 if (!pd) {
2410 ret = -ENODEV;
2411 goto out;
2412 }
2413 BUG_ON(pd->refcnt < 0);
2414
2415 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002416 if (pd->refcnt > 1) {
2417 if ((file->f_mode & FMODE_WRITE) &&
2418 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2419 ret = -EBUSY;
2420 goto out_dec;
2421 }
2422 } else {
Peter Osterlund01fd9fd2006-02-14 13:52:55 -08002423 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2424 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /*
2427 * needed here as well, since ext2 (among others) may change
2428 * the blocksize at mount time
2429 */
2430 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2431 }
2432
Jes Sorensen1657f822006-03-23 03:00:25 -08002433 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return 0;
2435
2436out_dec:
2437 pd->refcnt--;
2438out:
Thomas Maier78220822006-10-04 02:15:28 -07002439 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
Jes Sorensen1657f822006-03-23 03:00:25 -08002440 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return ret;
2442}
2443
2444static int pkt_close(struct inode *inode, struct file *file)
2445{
2446 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2447 int ret = 0;
2448
Jes Sorensen1657f822006-03-23 03:00:25 -08002449 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 pd->refcnt--;
2451 BUG_ON(pd->refcnt < 0);
2452 if (pd->refcnt == 0) {
2453 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2454 pkt_release_dev(pd, flush);
2455 }
Jes Sorensen1657f822006-03-23 03:00:25 -08002456 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return ret;
2458}
2459
2460
NeilBrown6712ecf2007-09-27 12:47:43 +02002461static void pkt_end_io_read_cloned(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 struct packet_stacked_data *psd = bio->bi_private;
2464 struct pktcdvd_device *pd = psd->pd;
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +02002467 bio_endio(psd->bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 mempool_free(psd, psd_pool);
2469 pkt_bio_finished(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Jens Axboe165125e2007-07-24 09:28:11 +02002472static int pkt_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 struct pktcdvd_device *pd;
2475 char b[BDEVNAME_SIZE];
2476 sector_t zone;
2477 struct packet_data *pkt;
2478 int was_empty, blocked_bio;
2479 struct pkt_rb_node *node;
2480
2481 pd = q->queuedata;
2482 if (!pd) {
Thomas Maier78220822006-10-04 02:15:28 -07002483 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 goto end_io;
2485 }
2486
2487 /*
2488 * Clone READ bios so we can have our own bi_end_io callback.
2489 */
2490 if (bio_data_dir(bio) == READ) {
2491 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2492 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2493
2494 psd->pd = pd;
2495 psd->bio = bio;
2496 cloned_bio->bi_bdev = pd->bdev;
2497 cloned_bio->bi_private = psd;
2498 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2499 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002500 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return 0;
2502 }
2503
2504 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
Thomas Maier78220822006-10-04 02:15:28 -07002505 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 pd->name, (unsigned long long)bio->bi_sector);
2507 goto end_io;
2508 }
2509
2510 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
Thomas Maier78220822006-10-04 02:15:28 -07002511 printk(DRIVER_NAME": wrong bio size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 goto end_io;
2513 }
2514
2515 blk_queue_bounce(q, &bio);
2516
2517 zone = ZONE(bio->bi_sector, pd);
2518 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2519 (unsigned long long)bio->bi_sector,
2520 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2521
2522 /* Check if we have to split the bio */
2523 {
2524 struct bio_pair *bp;
2525 sector_t last_zone;
2526 int first_sectors;
2527
2528 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2529 if (last_zone != zone) {
2530 BUG_ON(last_zone != zone + pd->settings.size);
2531 first_sectors = last_zone - bio->bi_sector;
2532 bp = bio_split(bio, bio_split_pool, first_sectors);
2533 BUG_ON(!bp);
2534 pkt_make_request(q, &bp->bio1);
2535 pkt_make_request(q, &bp->bio2);
2536 bio_pair_release(bp);
2537 return 0;
2538 }
2539 }
2540
2541 /*
2542 * If we find a matching packet in state WAITING or READ_WAIT, we can
2543 * just append this bio to that packet.
2544 */
2545 spin_lock(&pd->cdrw.active_list_lock);
2546 blocked_bio = 0;
2547 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2548 if (pkt->sector == zone) {
2549 spin_lock(&pkt->lock);
2550 if ((pkt->state == PACKET_WAITING_STATE) ||
2551 (pkt->state == PACKET_READ_WAIT_STATE)) {
2552 pkt_add_list_last(bio, &pkt->orig_bios,
2553 &pkt->orig_bios_tail);
2554 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2555 if ((pkt->write_size >= pkt->frames) &&
2556 (pkt->state == PACKET_WAITING_STATE)) {
2557 atomic_inc(&pkt->run_sm);
2558 wake_up(&pd->wqueue);
2559 }
2560 spin_unlock(&pkt->lock);
2561 spin_unlock(&pd->cdrw.active_list_lock);
2562 return 0;
2563 } else {
2564 blocked_bio = 1;
2565 }
2566 spin_unlock(&pkt->lock);
2567 }
2568 }
2569 spin_unlock(&pd->cdrw.active_list_lock);
2570
Thomas Maier0a0fc962006-12-08 02:36:11 -08002571 /*
2572 * Test if there is enough room left in the bio work queue
2573 * (queue size >= congestion on mark).
2574 * If not, wait till the work queue size is below the congestion off mark.
2575 */
2576 spin_lock(&pd->lock);
2577 if (pd->write_congestion_on > 0
2578 && pd->bio_queue_size >= pd->write_congestion_on) {
Thomas Maier83f3aa32007-02-10 01:45:11 -08002579 set_bdi_congested(&q->backing_dev_info, WRITE);
Thomas Maier0a0fc962006-12-08 02:36:11 -08002580 do {
2581 spin_unlock(&pd->lock);
2582 congestion_wait(WRITE, HZ);
2583 spin_lock(&pd->lock);
2584 } while(pd->bio_queue_size > pd->write_congestion_off);
2585 }
2586 spin_unlock(&pd->lock);
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /*
2589 * No matching packet found. Store the bio in the work queue.
2590 */
2591 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 node->bio = bio;
2593 spin_lock(&pd->lock);
2594 BUG_ON(pd->bio_queue_size < 0);
2595 was_empty = (pd->bio_queue_size == 0);
2596 pkt_rbtree_insert(pd, node);
2597 spin_unlock(&pd->lock);
2598
2599 /*
2600 * Wake up the worker thread.
2601 */
2602 atomic_set(&pd->scan_queue, 1);
2603 if (was_empty) {
2604 /* This wake_up is required for correct operation */
2605 wake_up(&pd->wqueue);
2606 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2607 /*
2608 * This wake up is not required for correct operation,
2609 * but improves performance in some cases.
2610 */
2611 wake_up(&pd->wqueue);
2612 }
2613 return 0;
2614end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02002615 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617}
2618
2619
2620
Jens Axboe165125e2007-07-24 09:28:11 +02002621static int pkt_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 struct pktcdvd_device *pd = q->queuedata;
2624 sector_t zone = ZONE(bio->bi_sector, pd);
2625 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2626 int remaining = (pd->settings.size << 9) - used;
2627 int remaining2;
2628
2629 /*
2630 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2631 * boundary, pkt_make_request() will split the bio.
2632 */
2633 remaining2 = PAGE_SIZE - bio->bi_size;
2634 remaining = max(remaining, remaining2);
2635
2636 BUG_ON(remaining < 0);
2637 return remaining;
2638}
2639
2640static void pkt_init_queue(struct pktcdvd_device *pd)
2641{
Jens Axboe165125e2007-07-24 09:28:11 +02002642 struct request_queue *q = pd->disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 blk_queue_make_request(q, pkt_make_request);
2645 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2646 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2647 blk_queue_merge_bvec(q, pkt_merge_bvec);
2648 q->queuedata = pd;
2649}
2650
2651static int pkt_seq_show(struct seq_file *m, void *p)
2652{
2653 struct pktcdvd_device *pd = m->private;
2654 char *msg;
2655 char bdev_buf[BDEVNAME_SIZE];
2656 int states[PACKET_NUM_STATES];
2657
2658 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2659 bdevname(pd->bdev, bdev_buf));
2660
2661 seq_printf(m, "\nSettings:\n");
2662 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2663
2664 if (pd->settings.write_type == 0)
2665 msg = "Packet";
2666 else
2667 msg = "Unknown";
2668 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2669
2670 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2671 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2672
2673 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2674
2675 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2676 msg = "Mode 1";
2677 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2678 msg = "Mode 2";
2679 else
2680 msg = "Unknown";
2681 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2682
2683 seq_printf(m, "\nStatistics:\n");
2684 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2685 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2686 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2687 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2688 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2689
2690 seq_printf(m, "\nMisc:\n");
2691 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2692 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2693 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2694 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2695 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2696 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2697
2698 seq_printf(m, "\nQueue state:\n");
2699 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2700 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2701 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2702
2703 pkt_count_states(pd, states);
2704 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2705 states[0], states[1], states[2], states[3], states[4], states[5]);
2706
Thomas Maier0a0fc962006-12-08 02:36:11 -08002707 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2708 pd->write_congestion_off,
2709 pd->write_congestion_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return 0;
2711}
2712
2713static int pkt_seq_open(struct inode *inode, struct file *file)
2714{
2715 return single_open(file, pkt_seq_show, PDE(inode)->data);
2716}
2717
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002718static const struct file_operations pkt_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 .open = pkt_seq_open,
2720 .read = seq_read,
2721 .llseek = seq_lseek,
2722 .release = single_release
2723};
2724
2725static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2726{
2727 int i;
2728 int ret = 0;
2729 char b[BDEVNAME_SIZE];
2730 struct proc_dir_entry *proc;
2731 struct block_device *bdev;
2732
2733 if (pd->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002734 printk(DRIVER_NAME": Recursive setup not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return -EBUSY;
2736 }
2737 for (i = 0; i < MAX_WRITERS; i++) {
2738 struct pktcdvd_device *pd2 = pkt_devs[i];
2739 if (!pd2)
2740 continue;
2741 if (pd2->bdev->bd_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002742 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return -EBUSY;
2744 }
2745 if (pd2->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002746 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return -EBUSY;
2748 }
2749 }
2750
2751 bdev = bdget(dev);
2752 if (!bdev)
2753 return -ENOMEM;
2754 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2755 if (ret)
2756 return ret;
2757
2758 /* This is safe, since we have a reference from open(). */
2759 __module_get(THIS_MODULE);
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 pd->bdev = bdev;
2762 set_blocksize(bdev, CD_FRAMESIZE);
2763
2764 pkt_init_queue(pd);
2765
2766 atomic_set(&pd->cdrw.pending_bios, 0);
2767 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2768 if (IS_ERR(pd->cdrw.thread)) {
Thomas Maier78220822006-10-04 02:15:28 -07002769 printk(DRIVER_NAME": can't start kernel thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 ret = -ENOMEM;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002771 goto out_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773
2774 proc = create_proc_entry(pd->name, 0, pkt_proc);
2775 if (proc) {
2776 proc->data = pd;
2777 proc->proc_fops = &pkt_proc_fops;
2778 }
Thomas Maier78220822006-10-04 02:15:28 -07002779 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return 0;
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782out_mem:
2783 blkdev_put(bdev);
2784 /* This is safe: open() is still holding a reference. */
2785 module_put(THIS_MODULE);
2786 return ret;
2787}
2788
2789static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2790{
2791 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2792
2793 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 switch (cmd) {
2796 /*
2797 * forward selected CDROM ioctls to CD-ROM, for UDF
2798 */
2799 case CDROMMULTISESSION:
2800 case CDROMREADTOCENTRY:
2801 case CDROM_LAST_WRITTEN:
2802 case CDROM_SEND_PACKET:
2803 case SCSI_IOCTL_SEND_COMMAND:
Peter Osterlund118326e2005-05-14 00:58:30 -07002804 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 case CDROMEJECT:
2807 /*
2808 * The door gets locked when the device is opened, so we
2809 * have to unlock it or else the eject command fails.
2810 */
Peter Osterlund948423e2006-02-14 13:52:56 -08002811 if (pd->refcnt == 1)
2812 pkt_lock_door(pd, 0);
Peter Osterlund118326e2005-05-14 00:58:30 -07002813 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 default:
Thomas Maier78220822006-10-04 02:15:28 -07002816 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return -ENOTTY;
2818 }
2819
2820 return 0;
2821}
2822
2823static int pkt_media_changed(struct gendisk *disk)
2824{
2825 struct pktcdvd_device *pd = disk->private_data;
2826 struct gendisk *attached_disk;
2827
2828 if (!pd)
2829 return 0;
2830 if (!pd->bdev)
2831 return 0;
2832 attached_disk = pd->bdev->bd_disk;
2833 if (!attached_disk)
2834 return 0;
2835 return attached_disk->fops->media_changed(attached_disk);
2836}
2837
2838static struct block_device_operations pktcdvd_ops = {
2839 .owner = THIS_MODULE,
2840 .open = pkt_open,
2841 .release = pkt_close,
2842 .ioctl = pkt_ioctl,
2843 .media_changed = pkt_media_changed,
2844};
2845
2846/*
2847 * Set up mapping from pktcdvd device to CD-ROM device.
2848 */
Thomas Maieradb92502006-12-08 02:36:10 -08002849static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 int idx;
2852 int ret = -ENOMEM;
2853 struct pktcdvd_device *pd;
2854 struct gendisk *disk;
Thomas Maieradb92502006-12-08 02:36:10 -08002855
2856 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 for (idx = 0; idx < MAX_WRITERS; idx++)
2859 if (!pkt_devs[idx])
2860 break;
2861 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002862 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
Thomas Maieradb92502006-12-08 02:36:10 -08002863 ret = -EBUSY;
2864 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866
Peter Osterlund1107d2e2005-09-13 01:25:29 -07002867 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (!pd)
Thomas Maieradb92502006-12-08 02:36:10 -08002869 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002871 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2872 sizeof(struct pkt_rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (!pd->rb_pool)
2874 goto out_mem;
2875
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002876 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2877 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2878 spin_lock_init(&pd->cdrw.active_list_lock);
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 spin_lock_init(&pd->lock);
2881 spin_lock_init(&pd->iosched.lock);
Thomas Maier78220822006-10-04 02:15:28 -07002882 sprintf(pd->name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 init_waitqueue_head(&pd->wqueue);
2884 pd->bio_queue = RB_ROOT;
2885
Thomas Maier0a0fc962006-12-08 02:36:11 -08002886 pd->write_congestion_on = write_congestion_on;
2887 pd->write_congestion_off = write_congestion_off;
2888
Thomas Maieradb92502006-12-08 02:36:10 -08002889 disk = alloc_disk(1);
2890 if (!disk)
2891 goto out_mem;
2892 pd->disk = disk;
Thomas Maieradd21662006-10-04 02:15:30 -07002893 disk->major = pktdev_major;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 disk->first_minor = idx;
2895 disk->fops = &pktcdvd_ops;
2896 disk->flags = GENHD_FL_REMOVABLE;
Thomas Maieradb92502006-12-08 02:36:10 -08002897 strcpy(disk->disk_name, pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 disk->private_data = pd;
2899 disk->queue = blk_alloc_queue(GFP_KERNEL);
2900 if (!disk->queue)
2901 goto out_mem2;
2902
2903 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2904 ret = pkt_new_dev(pd, dev);
2905 if (ret)
2906 goto out_new_dev;
2907
2908 add_disk(disk);
Thomas Maieradb92502006-12-08 02:36:10 -08002909
Thomas Maier32694852006-12-08 02:36:12 -08002910 pkt_sysfs_dev_new(pd);
2911 pkt_debugfs_dev_new(pd);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 pkt_devs[idx] = pd;
Thomas Maieradb92502006-12-08 02:36:10 -08002914 if (pkt_dev)
2915 *pkt_dev = pd->pkt_dev;
2916
2917 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
2919
2920out_new_dev:
Al Viro1312f402006-03-12 11:02:03 -05002921 blk_cleanup_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922out_mem2:
2923 put_disk(disk);
2924out_mem:
2925 if (pd->rb_pool)
2926 mempool_destroy(pd->rb_pool);
2927 kfree(pd);
Thomas Maieradb92502006-12-08 02:36:10 -08002928out_mutex:
2929 mutex_unlock(&ctl_mutex);
2930 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return ret;
2932}
2933
2934/*
2935 * Tear down mapping from pktcdvd device to CD-ROM device.
2936 */
Thomas Maieradb92502006-12-08 02:36:10 -08002937static int pkt_remove_dev(dev_t pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct pktcdvd_device *pd;
2940 int idx;
Thomas Maieradb92502006-12-08 02:36:10 -08002941 int ret = 0;
2942
2943 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 for (idx = 0; idx < MAX_WRITERS; idx++) {
2946 pd = pkt_devs[idx];
2947 if (pd && (pd->pkt_dev == pkt_dev))
2948 break;
2949 }
2950 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002951 DPRINTK(DRIVER_NAME": dev not setup\n");
Thomas Maieradb92502006-12-08 02:36:10 -08002952 ret = -ENXIO;
2953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
2955
Thomas Maieradb92502006-12-08 02:36:10 -08002956 if (pd->refcnt > 0) {
2957 ret = -EBUSY;
2958 goto out;
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (!IS_ERR(pd->cdrw.thread))
2961 kthread_stop(pd->cdrw.thread);
2962
Thomas Maier32694852006-12-08 02:36:12 -08002963 pkt_devs[idx] = NULL;
2964
2965 pkt_debugfs_dev_remove(pd);
2966 pkt_sysfs_dev_remove(pd);
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 blkdev_put(pd->bdev);
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 remove_proc_entry(pd->name, pkt_proc);
Thomas Maier78220822006-10-04 02:15:28 -07002971 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 del_gendisk(pd->disk);
Al Viro1312f402006-03-12 11:02:03 -05002974 blk_cleanup_queue(pd->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 put_disk(pd->disk);
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 mempool_destroy(pd->rb_pool);
2978 kfree(pd);
2979
2980 /* This is safe: open() is still holding a reference. */
2981 module_put(THIS_MODULE);
Thomas Maieradb92502006-12-08 02:36:10 -08002982
2983out:
2984 mutex_unlock(&ctl_mutex);
2985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2989{
Thomas Maieradb92502006-12-08 02:36:10 -08002990 struct pktcdvd_device *pd;
2991
2992 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2993
2994 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (pd) {
2996 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2997 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2998 } else {
2999 ctrl_cmd->dev = 0;
3000 ctrl_cmd->pkt_dev = 0;
3001 }
3002 ctrl_cmd->num_devices = MAX_WRITERS;
Thomas Maieradb92502006-12-08 02:36:10 -08003003
3004 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
3007static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3008{
3009 void __user *argp = (void __user *)arg;
3010 struct pkt_ctrl_command ctrl_cmd;
3011 int ret = 0;
Thomas Maieradb92502006-12-08 02:36:10 -08003012 dev_t pkt_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 if (cmd != PACKET_CTRL_CMD)
3015 return -ENOTTY;
3016
3017 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3018 return -EFAULT;
3019
3020 switch (ctrl_cmd.command) {
3021 case PKT_CTRL_CMD_SETUP:
3022 if (!capable(CAP_SYS_ADMIN))
3023 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003024 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3025 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 break;
3027 case PKT_CTRL_CMD_TEARDOWN:
3028 if (!capable(CAP_SYS_ADMIN))
3029 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003030 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 break;
3032 case PKT_CTRL_CMD_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 pkt_get_status(&ctrl_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 break;
3035 default:
3036 return -ENOTTY;
3037 }
3038
3039 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3040 return -EFAULT;
3041 return ret;
3042}
3043
3044
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003045static const struct file_operations pkt_ctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 .ioctl = pkt_ctl_ioctl,
3047 .owner = THIS_MODULE,
3048};
3049
3050static struct miscdevice pkt_misc = {
3051 .minor = MISC_DYNAMIC_MINOR,
Thomas Maier78220822006-10-04 02:15:28 -07003052 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 .fops = &pkt_ctl_fops
3054};
3055
3056static int __init pkt_init(void)
3057{
3058 int ret;
3059
Thomas Maier32694852006-12-08 02:36:12 -08003060 mutex_init(&ctl_mutex);
3061
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08003062 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3063 sizeof(struct packet_stacked_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (!psd_pool)
3065 return -ENOMEM;
3066
Thomas Maieradd21662006-10-04 02:15:30 -07003067 ret = register_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (ret < 0) {
Thomas Maier78220822006-10-04 02:15:28 -07003069 printk(DRIVER_NAME": Unable to register block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 goto out2;
3071 }
Thomas Maieradd21662006-10-04 02:15:30 -07003072 if (!pktdev_major)
3073 pktdev_major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Thomas Maier32694852006-12-08 02:36:12 -08003075 ret = pkt_sysfs_init();
3076 if (ret)
3077 goto out;
3078
3079 pkt_debugfs_init();
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 ret = misc_register(&pkt_misc);
3082 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07003083 printk(DRIVER_NAME": Unable to register misc device\n");
Thomas Maier32694852006-12-08 02:36:12 -08003084 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086
Thomas Maier78220822006-10-04 02:15:28 -07003087 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return 0;
3090
Thomas Maier32694852006-12-08 02:36:12 -08003091out_misc:
3092 pkt_debugfs_cleanup();
3093 pkt_sysfs_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094out:
Thomas Maieradd21662006-10-04 02:15:30 -07003095 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096out2:
3097 mempool_destroy(psd_pool);
3098 return ret;
3099}
3100
3101static void __exit pkt_exit(void)
3102{
Thomas Maier78220822006-10-04 02:15:28 -07003103 remove_proc_entry(DRIVER_NAME, proc_root_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 misc_deregister(&pkt_misc);
Thomas Maier32694852006-12-08 02:36:12 -08003105
3106 pkt_debugfs_cleanup();
3107 pkt_sysfs_cleanup();
3108
Thomas Maieradd21662006-10-04 02:15:30 -07003109 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 mempool_destroy(psd_pool);
3111}
3112
3113MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3114MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3115MODULE_LICENSE("GPL");
3116
3117module_init(pkt_init);
3118module_exit(pkt_exit);