blob: ce64e86d6ffbb9d4205a96f0e189dec7519632ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
Thomas Maieradb92502006-12-08 02:36:10 -08004 * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * May be copied or modified under the terms of the GNU General Public
7 * License. See linux/COPYING for more information.
8 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -07009 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
10 * DVD-RAM devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Theory of operation:
13 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070014 * At the lowest level, there is the standard driver for the CD/DVD device,
15 * typically ide-cd.c or sr.c. This driver can handle read and write requests,
16 * but it doesn't know anything about the special restrictions that apply to
17 * packet writing. One restriction is that write requests must be aligned to
18 * packet boundaries on the physical media, and the size of a write request
19 * must be equal to the packet size. Another restriction is that a
20 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
21 * command, if the previous command was a write.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
Peter Osterlunda676f8d2005-09-13 01:25:27 -070023 * The purpose of the packet writing driver is to hide these restrictions from
24 * higher layers, such as file systems, and present a block device that can be
25 * randomly read and written using 2kB-sized blocks.
26 *
27 * The lowest layer in the packet writing driver is the packet I/O scheduler.
28 * Its data is defined by the struct packet_iosched and includes two bio
29 * queues with pending read and write requests. These queues are processed
30 * by the pkt_iosched_process_queue() function. The write requests in this
31 * queue are already properly aligned and sized. This layer is responsible for
32 * issuing the flush cache commands and scheduling the I/O in a good order.
33 *
34 * The next layer transforms unaligned write requests to aligned writes. This
35 * transformation requires reading missing pieces of data from the underlying
36 * block device, assembling the pieces to full packets and queuing them to the
37 * packet I/O scheduler.
38 *
39 * At the top layer there is a custom make_request_fn function that forwards
40 * read requests directly to the iosched queue and puts write requests in the
41 * unaligned write queue. A kernel thread performs the necessary read
42 * gathering to convert the unaligned writes to aligned writes and then feeds
43 * them to the packet I/O scheduler.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 *************************************************************************/
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/pktcdvd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/module.h>
49#include <linux/types.h>
50#include <linux/kernel.h>
51#include <linux/kthread.h>
52#include <linux/errno.h>
53#include <linux/spinlock.h>
54#include <linux/file.h>
55#include <linux/proc_fs.h>
56#include <linux/seq_file.h>
57#include <linux/miscdevice.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080058#include <linux/freezer.h>
Jes Sorensen1657f822006-03-23 03:00:25 -080059#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_ioctl.h>
Peter Osterlundcef289632006-02-20 18:28:01 -080062#include <scsi/scsi.h>
Thomas Maier32694852006-12-08 02:36:12 -080063#include <linux/debugfs.h>
64#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/uaccess.h>
67
Thomas Maier78220822006-10-04 02:15:28 -070068#define DRIVER_NAME "pktcdvd"
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#if PACKET_DEBUG
71#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
72#else
73#define DPRINTK(fmt, args...)
74#endif
75
76#if PACKET_DEBUG > 1
77#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
78#else
79#define VPRINTK(fmt, args...)
80#endif
81
82#define MAX_SPEED 0xffff
83
84#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
85
86static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
87static struct proc_dir_entry *pkt_proc;
Thomas Maieradd21662006-10-04 02:15:30 -070088static int pktdev_major;
Thomas Maier0a0fc962006-12-08 02:36:11 -080089static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
90static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
Jes Sorensen1657f822006-03-23 03:00:25 -080091static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static mempool_t *psd_pool;
93
Thomas Maier32694852006-12-08 02:36:12 -080094static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
95static struct dentry *pkt_debugfs_root = NULL; /* /debug/pktcdvd */
96
97/* forward declaration */
98static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
99static int pkt_remove_dev(dev_t pkt_dev);
100static int pkt_seq_show(struct seq_file *m, void *p);
101
102
103
104/*
105 * create and register a pktcdvd kernel object.
106 */
107static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
108 const char* name,
109 struct kobject* parent,
110 struct kobj_type* ktype)
111{
112 struct pktcdvd_kobj *p;
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{
Christoph Hellwig406c9b62007-01-05 16:36:26 -0800755 request_queue_t *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 */
982static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
983{
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
1061static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
1062{
1063 struct packet_data *pkt = bio->bi_private;
1064 struct pktcdvd_device *pd = pkt->pd;
1065 BUG_ON(!pd);
1066
1067 if (bio->bi_size)
1068 return 1;
1069
1070 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
1071 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
1072
1073 if (err)
1074 atomic_inc(&pkt->io_errors);
1075 if (atomic_dec_and_test(&pkt->io_wait)) {
1076 atomic_inc(&pkt->run_sm);
1077 wake_up(&pd->wqueue);
1078 }
1079 pkt_bio_finished(pd);
1080
1081 return 0;
1082}
1083
1084static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
1085{
1086 struct packet_data *pkt = bio->bi_private;
1087 struct pktcdvd_device *pd = pkt->pd;
1088 BUG_ON(!pd);
1089
1090 if (bio->bi_size)
1091 return 1;
1092
1093 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
1094
1095 pd->stats.pkt_ended++;
1096
1097 pkt_bio_finished(pd);
1098 atomic_dec(&pkt->io_wait);
1099 atomic_inc(&pkt->run_sm);
1100 wake_up(&pd->wqueue);
1101 return 0;
1102}
1103
1104/*
1105 * Schedule reads for the holes in a packet
1106 */
1107static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1108{
1109 int frames_read = 0;
1110 struct bio *bio;
1111 int f;
1112 char written[PACKET_MAX_SIZE];
1113
1114 BUG_ON(!pkt->orig_bios);
1115
1116 atomic_set(&pkt->io_wait, 0);
1117 atomic_set(&pkt->io_errors, 0);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Figure out which frames we need to read before we can write.
1121 */
1122 memset(written, 0, sizeof(written));
1123 spin_lock(&pkt->lock);
1124 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1125 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1126 int num_frames = bio->bi_size / CD_FRAMESIZE;
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001127 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 BUG_ON(first_frame < 0);
1129 BUG_ON(first_frame + num_frames > pkt->frames);
1130 for (f = first_frame; f < first_frame + num_frames; f++)
1131 written[f] = 1;
1132 }
1133 spin_unlock(&pkt->lock);
1134
Peter Osterlund06e7ab52005-09-13 01:25:28 -07001135 if (pkt->cache_valid) {
1136 VPRINTK("pkt_gather_data: zone %llx cached\n",
1137 (unsigned long long)pkt->sector);
1138 goto out_account;
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /*
1142 * Schedule reads for missing parts of the packet.
1143 */
1144 for (f = 0; f < pkt->frames; f++) {
1145 int p, offset;
1146 if (written[f])
1147 continue;
1148 bio = pkt->r_bios[f];
1149 bio_init(bio);
1150 bio->bi_max_vecs = 1;
1151 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
1152 bio->bi_bdev = pd->bdev;
1153 bio->bi_end_io = pkt_end_io_read;
1154 bio->bi_private = pkt;
1155
1156 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
1157 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
1158 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
1159 f, pkt->pages[p], offset);
1160 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
1161 BUG();
1162
1163 atomic_inc(&pkt->io_wait);
1164 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001165 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 frames_read++;
1167 }
1168
1169out_account:
1170 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
1171 frames_read, (unsigned long long)pkt->sector);
1172 pd->stats.pkt_started++;
1173 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176/*
1177 * Find a packet matching zone, or the least recently used packet if
1178 * there is no match.
1179 */
1180static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
1181{
1182 struct packet_data *pkt;
1183
1184 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
1185 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
1186 list_del_init(&pkt->list);
1187 if (pkt->sector != zone)
1188 pkt->cache_valid = 0;
Peter Osterlund610827d2005-09-13 01:25:29 -07001189 return pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 }
Peter Osterlund610827d2005-09-13 01:25:29 -07001192 BUG();
1193 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
1197{
1198 if (pkt->cache_valid) {
1199 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
1200 } else {
1201 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
1202 }
1203}
1204
1205/*
1206 * recover a failed write, query for relocation if possible
1207 *
1208 * returns 1 if recovery is possible, or 0 if not
1209 *
1210 */
1211static int pkt_start_recovery(struct packet_data *pkt)
1212{
1213 /*
1214 * FIXME. We need help from the file system to implement
1215 * recovery handling.
1216 */
1217 return 0;
1218#if 0
1219 struct request *rq = pkt->rq;
1220 struct pktcdvd_device *pd = rq->rq_disk->private_data;
1221 struct block_device *pkt_bdev;
1222 struct super_block *sb = NULL;
1223 unsigned long old_block, new_block;
1224 sector_t new_sector;
1225
1226 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
1227 if (pkt_bdev) {
1228 sb = get_super(pkt_bdev);
1229 bdput(pkt_bdev);
1230 }
1231
1232 if (!sb)
1233 return 0;
1234
1235 if (!sb->s_op || !sb->s_op->relocate_blocks)
1236 goto out;
1237
1238 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
1239 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
1240 goto out;
1241
1242 new_sector = new_block * (CD_FRAMESIZE >> 9);
1243 pkt->sector = new_sector;
1244
1245 pkt->bio->bi_sector = new_sector;
1246 pkt->bio->bi_next = NULL;
1247 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
1248 pkt->bio->bi_idx = 0;
1249
1250 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
1251 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
1252 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
1253 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
1254 BUG_ON(pkt->bio->bi_private != pkt);
1255
1256 drop_super(sb);
1257 return 1;
1258
1259out:
1260 drop_super(sb);
1261 return 0;
1262#endif
1263}
1264
1265static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
1266{
1267#if PACKET_DEBUG > 1
1268 static const char *state_name[] = {
1269 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
1270 };
1271 enum packet_data_state old_state = pkt->state;
1272 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
1273 state_name[old_state], state_name[state]);
1274#endif
1275 pkt->state = state;
1276}
1277
1278/*
1279 * Scan the work queue to see if we can start a new packet.
1280 * returns non-zero if any work was done.
1281 */
1282static int pkt_handle_queue(struct pktcdvd_device *pd)
1283{
1284 struct packet_data *pkt, *p;
1285 struct bio *bio = NULL;
1286 sector_t zone = 0; /* Suppress gcc warning */
1287 struct pkt_rb_node *node, *first_node;
1288 struct rb_node *n;
Thomas Maier0a0fc962006-12-08 02:36:11 -08001289 int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 VPRINTK("handle_queue\n");
1292
1293 atomic_set(&pd->scan_queue, 0);
1294
1295 if (list_empty(&pd->cdrw.pkt_free_list)) {
1296 VPRINTK("handle_queue: no pkt\n");
1297 return 0;
1298 }
1299
1300 /*
1301 * Try to find a zone we are not already working on.
1302 */
1303 spin_lock(&pd->lock);
1304 first_node = pkt_rbtree_find(pd, pd->current_sector);
1305 if (!first_node) {
1306 n = rb_first(&pd->bio_queue);
1307 if (n)
1308 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
1309 }
1310 node = first_node;
1311 while (node) {
1312 bio = node->bio;
1313 zone = ZONE(bio->bi_sector, pd);
1314 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001315 if (p->sector == zone) {
1316 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -07001318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 break;
1321try_next_bio:
1322 node = pkt_rbtree_next(node);
1323 if (!node) {
1324 n = rb_first(&pd->bio_queue);
1325 if (n)
1326 node = rb_entry(n, struct pkt_rb_node, rb_node);
1327 }
1328 if (node == first_node)
1329 node = NULL;
1330 }
1331 spin_unlock(&pd->lock);
1332 if (!bio) {
1333 VPRINTK("handle_queue: no bio\n");
1334 return 0;
1335 }
1336
1337 pkt = pkt_get_packet_data(pd, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 pd->current_sector = zone + pd->settings.size;
1340 pkt->sector = zone;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08001341 BUG_ON(pkt->frames != pd->settings.size >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 pkt->write_size = 0;
1343
1344 /*
1345 * Scan work queue for bios in the same zone and link them
1346 * to this packet.
1347 */
1348 spin_lock(&pd->lock);
1349 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
1350 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
1351 bio = node->bio;
1352 VPRINTK("pkt_handle_queue: found zone=%llx\n",
1353 (unsigned long long)ZONE(bio->bi_sector, pd));
1354 if (ZONE(bio->bi_sector, pd) != zone)
1355 break;
1356 pkt_rbtree_erase(pd, node);
1357 spin_lock(&pkt->lock);
1358 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
1359 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
1360 spin_unlock(&pkt->lock);
1361 }
Thomas Maier0a0fc962006-12-08 02:36:11 -08001362 /* check write congestion marks, and if bio_queue_size is
1363 below, wake up any waiters */
1364 wakeup = (pd->write_congestion_on > 0
1365 && pd->bio_queue_size <= pd->write_congestion_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 spin_unlock(&pd->lock);
Thomas Maier0a0fc962006-12-08 02:36:11 -08001367 if (wakeup)
Thomas Maier83f3aa32007-02-10 01:45:11 -08001368 clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
1371 pkt_set_state(pkt, PACKET_WAITING_STATE);
1372 atomic_set(&pkt->run_sm, 1);
1373
1374 spin_lock(&pd->cdrw.active_list_lock);
1375 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
1376 spin_unlock(&pd->cdrw.active_list_lock);
1377
1378 return 1;
1379}
1380
1381/*
1382 * Assemble a bio to write one packet and queue the bio for processing
1383 * by the underlying block device.
1384 */
1385static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
1386{
1387 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int f;
1389 int frames_write;
Peter Osterlund72772322006-02-14 13:52:56 -08001390 struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 for (f = 0; f < pkt->frames; f++) {
Peter Osterlund72772322006-02-14 13:52:56 -08001393 bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
1394 bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
1397 /*
Peter Osterlund72772322006-02-14 13:52:56 -08001398 * Fill-in bvec with data from orig_bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400 frames_write = 0;
1401 spin_lock(&pkt->lock);
1402 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1403 int segment = bio->bi_idx;
1404 int src_offs = 0;
1405 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1406 int num_frames = bio->bi_size / CD_FRAMESIZE;
1407 BUG_ON(first_frame < 0);
1408 BUG_ON(first_frame + num_frames > pkt->frames);
1409 for (f = first_frame; f < first_frame + num_frames; f++) {
1410 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1411
1412 while (src_offs >= src_bvl->bv_len) {
1413 src_offs -= src_bvl->bv_len;
1414 segment++;
1415 BUG_ON(segment >= bio->bi_vcnt);
1416 src_bvl = bio_iovec_idx(bio, segment);
1417 }
1418
1419 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
Peter Osterlund72772322006-02-14 13:52:56 -08001420 bvec[f].bv_page = src_bvl->bv_page;
1421 bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 } else {
1423 pkt_copy_bio_data(bio, segment, src_offs,
Peter Osterlund72772322006-02-14 13:52:56 -08001424 bvec[f].bv_page, bvec[f].bv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426 src_offs += CD_FRAMESIZE;
1427 frames_write++;
1428 }
1429 }
1430 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1431 spin_unlock(&pkt->lock);
1432
1433 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1434 frames_write, (unsigned long long)pkt->sector);
1435 BUG_ON(frames_write != pkt->write_size);
1436
1437 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
Peter Osterlund72772322006-02-14 13:52:56 -08001438 pkt_make_local_copy(pkt, bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 pkt->cache_valid = 1;
1440 } else {
1441 pkt->cache_valid = 0;
1442 }
1443
1444 /* Start the write request */
1445 bio_init(pkt->w_bio);
1446 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1447 pkt->w_bio->bi_sector = pkt->sector;
1448 pkt->w_bio->bi_bdev = pd->bdev;
1449 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1450 pkt->w_bio->bi_private = pkt;
Peter Osterlund72772322006-02-14 13:52:56 -08001451 for (f = 0; f < pkt->frames; f++)
1452 if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
1453 BUG();
Thomas Maier78220822006-10-04 02:15:28 -07001454 VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 atomic_set(&pkt->io_wait, 1);
1457 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001458 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1462{
1463 struct bio *bio, *next;
1464
1465 if (!uptodate)
1466 pkt->cache_valid = 0;
1467
1468 /* Finish all bios corresponding to this packet */
1469 bio = pkt->orig_bios;
1470 while (bio) {
1471 next = bio->bi_next;
1472 bio->bi_next = NULL;
1473 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1474 bio = next;
1475 }
1476 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1477}
1478
1479static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1480{
1481 int uptodate;
1482
1483 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1484
1485 for (;;) {
1486 switch (pkt->state) {
1487 case PACKET_WAITING_STATE:
1488 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1489 return;
1490
1491 pkt->sleep_time = 0;
1492 pkt_gather_data(pd, pkt);
1493 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1494 break;
1495
1496 case PACKET_READ_WAIT_STATE:
1497 if (atomic_read(&pkt->io_wait) > 0)
1498 return;
1499
1500 if (atomic_read(&pkt->io_errors) > 0) {
1501 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1502 } else {
1503 pkt_start_write(pd, pkt);
1504 }
1505 break;
1506
1507 case PACKET_WRITE_WAIT_STATE:
1508 if (atomic_read(&pkt->io_wait) > 0)
1509 return;
1510
1511 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1512 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1513 } else {
1514 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1515 }
1516 break;
1517
1518 case PACKET_RECOVERY_STATE:
1519 if (pkt_start_recovery(pkt)) {
1520 pkt_start_write(pd, pkt);
1521 } else {
1522 VPRINTK("No recovery possible\n");
1523 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1524 }
1525 break;
1526
1527 case PACKET_FINISHED_STATE:
1528 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1529 pkt_finish_packet(pkt, uptodate);
1530 return;
1531
1532 default:
1533 BUG();
1534 break;
1535 }
1536 }
1537}
1538
1539static void pkt_handle_packets(struct pktcdvd_device *pd)
1540{
1541 struct packet_data *pkt, *next;
1542
1543 VPRINTK("pkt_handle_packets\n");
1544
1545 /*
1546 * Run state machine for active packets
1547 */
1548 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1549 if (atomic_read(&pkt->run_sm) > 0) {
1550 atomic_set(&pkt->run_sm, 0);
1551 pkt_run_state_machine(pd, pkt);
1552 }
1553 }
1554
1555 /*
1556 * Move no longer active packets to the free list
1557 */
1558 spin_lock(&pd->cdrw.active_list_lock);
1559 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1560 if (pkt->state == PACKET_FINISHED_STATE) {
1561 list_del(&pkt->list);
1562 pkt_put_packet_data(pd, pkt);
1563 pkt_set_state(pkt, PACKET_IDLE_STATE);
1564 atomic_set(&pd->scan_queue, 1);
1565 }
1566 }
1567 spin_unlock(&pd->cdrw.active_list_lock);
1568}
1569
1570static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1571{
1572 struct packet_data *pkt;
1573 int i;
1574
Peter Osterlundae7642b2005-11-13 16:06:36 -08001575 for (i = 0; i < PACKET_NUM_STATES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 states[i] = 0;
1577
1578 spin_lock(&pd->cdrw.active_list_lock);
1579 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1580 states[pkt->state]++;
1581 }
1582 spin_unlock(&pd->cdrw.active_list_lock);
1583}
1584
1585/*
1586 * kcdrwd is woken up when writes have been queued for one of our
1587 * registered devices
1588 */
1589static int kcdrwd(void *foobar)
1590{
1591 struct pktcdvd_device *pd = foobar;
1592 struct packet_data *pkt;
1593 long min_sleep_time, residue;
1594
1595 set_user_nice(current, -20);
1596
1597 for (;;) {
1598 DECLARE_WAITQUEUE(wait, current);
1599
1600 /*
1601 * Wait until there is something to do
1602 */
1603 add_wait_queue(&pd->wqueue, &wait);
1604 for (;;) {
1605 set_current_state(TASK_INTERRUPTIBLE);
1606
1607 /* Check if we need to run pkt_handle_queue */
1608 if (atomic_read(&pd->scan_queue) > 0)
1609 goto work_to_do;
1610
1611 /* Check if we need to run the state machine for some packet */
1612 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1613 if (atomic_read(&pkt->run_sm) > 0)
1614 goto work_to_do;
1615 }
1616
1617 /* Check if we need to process the iosched queues */
1618 if (atomic_read(&pd->iosched.attention) != 0)
1619 goto work_to_do;
1620
1621 /* Otherwise, go to sleep */
1622 if (PACKET_DEBUG > 1) {
1623 int states[PACKET_NUM_STATES];
1624 pkt_count_states(pd, states);
1625 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1626 states[0], states[1], states[2], states[3],
1627 states[4], states[5]);
1628 }
1629
1630 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1631 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1632 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1633 min_sleep_time = pkt->sleep_time;
1634 }
1635
1636 generic_unplug_device(bdev_get_queue(pd->bdev));
1637
1638 VPRINTK("kcdrwd: sleeping\n");
1639 residue = schedule_timeout(min_sleep_time);
1640 VPRINTK("kcdrwd: wake up\n");
1641
1642 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001643 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1646 if (!pkt->sleep_time)
1647 continue;
1648 pkt->sleep_time -= min_sleep_time - residue;
1649 if (pkt->sleep_time <= 0) {
1650 pkt->sleep_time = 0;
1651 atomic_inc(&pkt->run_sm);
1652 }
1653 }
1654
1655 if (signal_pending(current)) {
1656 flush_signals(current);
1657 }
1658 if (kthread_should_stop())
1659 break;
1660 }
1661work_to_do:
1662 set_current_state(TASK_RUNNING);
1663 remove_wait_queue(&pd->wqueue, &wait);
1664
1665 if (kthread_should_stop())
1666 break;
1667
1668 /*
1669 * if pkt_handle_queue returns true, we can queue
1670 * another request.
1671 */
1672 while (pkt_handle_queue(pd))
1673 ;
1674
1675 /*
1676 * Handle packet state machine
1677 */
1678 pkt_handle_packets(pd);
1679
1680 /*
1681 * Handle iosched queues
1682 */
1683 pkt_iosched_process_queue(pd);
1684 }
1685
1686 return 0;
1687}
1688
1689static void pkt_print_settings(struct pktcdvd_device *pd)
1690{
Thomas Maier78220822006-10-04 02:15:28 -07001691 printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 printk("%u blocks, ", pd->settings.size >> 2);
1693 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1694}
1695
1696static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1697{
1698 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1699
1700 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1701 cgc->cmd[2] = page_code | (page_control << 6);
1702 cgc->cmd[7] = cgc->buflen >> 8;
1703 cgc->cmd[8] = cgc->buflen & 0xff;
1704 cgc->data_direction = CGC_DATA_READ;
1705 return pkt_generic_packet(pd, cgc);
1706}
1707
1708static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1709{
1710 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1711 memset(cgc->buffer, 0, 2);
1712 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1713 cgc->cmd[1] = 0x10; /* PF */
1714 cgc->cmd[7] = cgc->buflen >> 8;
1715 cgc->cmd[8] = cgc->buflen & 0xff;
1716 cgc->data_direction = CGC_DATA_WRITE;
1717 return pkt_generic_packet(pd, cgc);
1718}
1719
1720static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1721{
1722 struct packet_command cgc;
1723 int ret;
1724
1725 /* set up command and get the disc info */
1726 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1727 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1728 cgc.cmd[8] = cgc.buflen = 2;
1729 cgc.quiet = 1;
1730
1731 if ((ret = pkt_generic_packet(pd, &cgc)))
1732 return ret;
1733
1734 /* not all drives have the same disc_info length, so requeue
1735 * packet with the length the drive tells us it can supply
1736 */
1737 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1738 sizeof(di->disc_information_length);
1739
1740 if (cgc.buflen > sizeof(disc_information))
1741 cgc.buflen = sizeof(disc_information);
1742
1743 cgc.cmd[8] = cgc.buflen;
1744 return pkt_generic_packet(pd, &cgc);
1745}
1746
1747static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1748{
1749 struct packet_command cgc;
1750 int ret;
1751
1752 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1753 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1754 cgc.cmd[1] = type & 3;
1755 cgc.cmd[4] = (track & 0xff00) >> 8;
1756 cgc.cmd[5] = track & 0xff;
1757 cgc.cmd[8] = 8;
1758 cgc.quiet = 1;
1759
1760 if ((ret = pkt_generic_packet(pd, &cgc)))
1761 return ret;
1762
1763 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1764 sizeof(ti->track_information_length);
1765
1766 if (cgc.buflen > sizeof(track_information))
1767 cgc.buflen = sizeof(track_information);
1768
1769 cgc.cmd[8] = cgc.buflen;
1770 return pkt_generic_packet(pd, &cgc);
1771}
1772
1773static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1774{
1775 disc_information di;
1776 track_information ti;
1777 __u32 last_track;
1778 int ret = -1;
1779
1780 if ((ret = pkt_get_disc_info(pd, &di)))
1781 return ret;
1782
1783 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1784 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1785 return ret;
1786
1787 /* if this track is blank, try the previous. */
1788 if (ti.blank) {
1789 last_track--;
1790 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1791 return ret;
1792 }
1793
1794 /* if last recorded field is valid, return it. */
1795 if (ti.lra_v) {
1796 *last_written = be32_to_cpu(ti.last_rec_address);
1797 } else {
1798 /* make it up instead */
1799 *last_written = be32_to_cpu(ti.track_start) +
1800 be32_to_cpu(ti.track_size);
1801 if (ti.free_blocks)
1802 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1803 }
1804 return 0;
1805}
1806
1807/*
1808 * write mode select package based on pd->settings
1809 */
1810static int pkt_set_write_settings(struct pktcdvd_device *pd)
1811{
1812 struct packet_command cgc;
1813 struct request_sense sense;
1814 write_param_page *wp;
1815 char buffer[128];
1816 int ret, size;
1817
1818 /* doesn't apply to DVD+RW or DVD-RAM */
1819 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1820 return 0;
1821
1822 memset(buffer, 0, sizeof(buffer));
1823 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1824 cgc.sense = &sense;
1825 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1826 pkt_dump_sense(&cgc);
1827 return ret;
1828 }
1829
1830 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1831 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1832 if (size > sizeof(buffer))
1833 size = sizeof(buffer);
1834
1835 /*
1836 * now get it all
1837 */
1838 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1839 cgc.sense = &sense;
1840 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1841 pkt_dump_sense(&cgc);
1842 return ret;
1843 }
1844
1845 /*
1846 * write page is offset header + block descriptor length
1847 */
1848 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1849
1850 wp->fp = pd->settings.fp;
1851 wp->track_mode = pd->settings.track_mode;
1852 wp->write_type = pd->settings.write_type;
1853 wp->data_block_type = pd->settings.block_mode;
1854
1855 wp->multi_session = 0;
1856
1857#ifdef PACKET_USE_LS
1858 wp->link_size = 7;
1859 wp->ls_v = 1;
1860#endif
1861
1862 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1863 wp->session_format = 0;
1864 wp->subhdr2 = 0x20;
1865 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1866 wp->session_format = 0x20;
1867 wp->subhdr2 = 8;
1868#if 0
1869 wp->mcn[0] = 0x80;
1870 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1871#endif
1872 } else {
1873 /*
1874 * paranoia
1875 */
Thomas Maier78220822006-10-04 02:15:28 -07001876 printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 1;
1878 }
1879 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1880
1881 cgc.buflen = cgc.cmd[8] = size;
1882 if ((ret = pkt_mode_select(pd, &cgc))) {
1883 pkt_dump_sense(&cgc);
1884 return ret;
1885 }
1886
1887 pkt_print_settings(pd);
1888 return 0;
1889}
1890
1891/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001892 * 1 -- we can write to this track, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001894static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Peter Osterlundab863ec2006-02-20 18:28:04 -08001896 switch (pd->mmc3_profile) {
1897 case 0x1a: /* DVD+RW */
1898 case 0x12: /* DVD-RAM */
1899 /* The track is always writable on DVD+RW/DVD-RAM */
1900 return 1;
1901 default:
1902 break;
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Peter Osterlundab863ec2006-02-20 18:28:04 -08001905 if (!ti->packet || !ti->fp)
1906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /*
1909 * "good" settings as per Mt Fuji.
1910 */
Peter Osterlundab863ec2006-02-20 18:28:04 -08001911 if (ti->rt == 0 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001912 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Peter Osterlundab863ec2006-02-20 18:28:04 -08001914 if (ti->rt == 0 && ti->blank == 1)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001915 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Peter Osterlundab863ec2006-02-20 18:28:04 -08001917 if (ti->rt == 1 && ti->blank == 0)
Peter Osterlund7c613d52006-02-20 18:28:02 -08001918 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Thomas Maier78220822006-10-04 02:15:28 -07001920 printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
1924/*
Peter Osterlund7c613d52006-02-20 18:28:02 -08001925 * 1 -- we can write to this disc, 0 -- we can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001927static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 switch (pd->mmc3_profile) {
1930 case 0x0a: /* CD-RW */
1931 case 0xffff: /* MMC3 not supported */
1932 break;
1933 case 0x1a: /* DVD+RW */
1934 case 0x13: /* DVD-RW */
1935 case 0x12: /* DVD-RAM */
Peter Osterlund7c613d52006-02-20 18:28:02 -08001936 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 default:
Thomas Maier78220822006-10-04 02:15:28 -07001938 VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
1942 /*
1943 * for disc type 0xff we should probably reserve a new track.
1944 * but i'm not sure, should we leave this to user apps? probably.
1945 */
1946 if (di->disc_type == 0xff) {
Thomas Maier78220822006-10-04 02:15:28 -07001947 printk(DRIVER_NAME": Unknown disc. No track?\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950
1951 if (di->disc_type != 0x20 && di->disc_type != 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001952 printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
Peter Osterlund7c613d52006-02-20 18:28:02 -08001953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
1956 if (di->erasable == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07001957 printk(DRIVER_NAME": Disc not erasable\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
1961 if (di->border_status == PACKET_SESSION_RESERVED) {
Thomas Maier78220822006-10-04 02:15:28 -07001962 printk(DRIVER_NAME": Can't write to last track (reserved)\n");
Peter Osterlund7c613d52006-02-20 18:28:02 -08001963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
Peter Osterlund7c613d52006-02-20 18:28:02 -08001966 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969static int pkt_probe_settings(struct pktcdvd_device *pd)
1970{
1971 struct packet_command cgc;
1972 unsigned char buf[12];
1973 disc_information di;
1974 track_information ti;
1975 int ret, track;
1976
1977 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1978 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1979 cgc.cmd[8] = 8;
1980 ret = pkt_generic_packet(pd, &cgc);
1981 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1982
1983 memset(&di, 0, sizeof(disc_information));
1984 memset(&ti, 0, sizeof(track_information));
1985
1986 if ((ret = pkt_get_disc_info(pd, &di))) {
1987 printk("failed get_disc\n");
1988 return ret;
1989 }
1990
Peter Osterlund7c613d52006-02-20 18:28:02 -08001991 if (!pkt_writable_disc(pd, &di))
Peter Osterlund9db91542006-02-20 18:28:04 -08001992 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1995
1996 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1997 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
Thomas Maier78220822006-10-04 02:15:28 -07001998 printk(DRIVER_NAME": failed get_track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return ret;
2000 }
2001
Peter Osterlundab863ec2006-02-20 18:28:04 -08002002 if (!pkt_writable_track(pd, &ti)) {
Thomas Maier78220822006-10-04 02:15:28 -07002003 printk(DRIVER_NAME": can't write to this track\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002004 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006
2007 /*
2008 * we keep packet size in 512 byte units, makes it easier to
2009 * deal with request calculations.
2010 */
2011 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
2012 if (pd->settings.size == 0) {
Thomas Maier78220822006-10-04 02:15:28 -07002013 printk(DRIVER_NAME": detected zero packet size!\n");
Phillip Susia460ad62006-02-04 23:27:44 -08002014 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07002016 if (pd->settings.size > PACKET_MAX_SECTORS) {
Thomas Maier78220822006-10-04 02:15:28 -07002017 printk(DRIVER_NAME": packet size is too big\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002018 return -EROFS;
Peter Osterlundd0272e72005-09-13 01:25:27 -07002019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 pd->settings.fp = ti.fp;
2021 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
2022
2023 if (ti.nwa_v) {
2024 pd->nwa = be32_to_cpu(ti.next_writable);
2025 set_bit(PACKET_NWA_VALID, &pd->flags);
2026 }
2027
2028 /*
2029 * in theory we could use lra on -RW media as well and just zero
2030 * blocks that haven't been written yet, but in practice that
2031 * is just a no-go. we'll use that for -R, naturally.
2032 */
2033 if (ti.lra_v) {
2034 pd->lra = be32_to_cpu(ti.last_rec_address);
2035 set_bit(PACKET_LRA_VALID, &pd->flags);
2036 } else {
2037 pd->lra = 0xffffffff;
2038 set_bit(PACKET_LRA_VALID, &pd->flags);
2039 }
2040
2041 /*
2042 * fine for now
2043 */
2044 pd->settings.link_loss = 7;
2045 pd->settings.write_type = 0; /* packet */
2046 pd->settings.track_mode = ti.track_mode;
2047
2048 /*
2049 * mode1 or mode2 disc
2050 */
2051 switch (ti.data_mode) {
2052 case PACKET_MODE1:
2053 pd->settings.block_mode = PACKET_BLOCK_MODE1;
2054 break;
2055 case PACKET_MODE2:
2056 pd->settings.block_mode = PACKET_BLOCK_MODE2;
2057 break;
2058 default:
Thomas Maier78220822006-10-04 02:15:28 -07002059 printk(DRIVER_NAME": unknown data mode\n");
Peter Osterlund9db91542006-02-20 18:28:04 -08002060 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 return 0;
2063}
2064
2065/*
2066 * enable/disable write caching on drive
2067 */
2068static int pkt_write_caching(struct pktcdvd_device *pd, int set)
2069{
2070 struct packet_command cgc;
2071 struct request_sense sense;
2072 unsigned char buf[64];
2073 int ret;
2074
2075 memset(buf, 0, sizeof(buf));
2076 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
2077 cgc.sense = &sense;
2078 cgc.buflen = pd->mode_offset + 12;
2079
2080 /*
2081 * caching mode page might not be there, so quiet this command
2082 */
2083 cgc.quiet = 1;
2084
2085 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
2086 return ret;
2087
2088 buf[pd->mode_offset + 10] |= (!!set << 2);
2089
2090 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
2091 ret = pkt_mode_select(pd, &cgc);
2092 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07002093 printk(DRIVER_NAME": write caching control failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 pkt_dump_sense(&cgc);
2095 } else if (!ret && set)
Thomas Maier78220822006-10-04 02:15:28 -07002096 printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return ret;
2098}
2099
2100static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
2101{
2102 struct packet_command cgc;
2103
2104 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2105 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2106 cgc.cmd[4] = lockflag ? 1 : 0;
2107 return pkt_generic_packet(pd, &cgc);
2108}
2109
2110/*
2111 * Returns drive maximum write speed
2112 */
2113static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
2114{
2115 struct packet_command cgc;
2116 struct request_sense sense;
2117 unsigned char buf[256+18];
2118 unsigned char *cap_buf;
2119 int ret, offset;
2120
2121 memset(buf, 0, sizeof(buf));
2122 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
2123 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
2124 cgc.sense = &sense;
2125
2126 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2127 if (ret) {
2128 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
2129 sizeof(struct mode_page_header);
2130 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2131 if (ret) {
2132 pkt_dump_sense(&cgc);
2133 return ret;
2134 }
2135 }
2136
2137 offset = 20; /* Obsoleted field, used by older drives */
2138 if (cap_buf[1] >= 28)
2139 offset = 28; /* Current write speed selected */
2140 if (cap_buf[1] >= 30) {
2141 /* If the drive reports at least one "Logical Unit Write
2142 * Speed Performance Descriptor Block", use the information
2143 * in the first block. (contains the highest speed)
2144 */
2145 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
2146 if (num_spdb > 0)
2147 offset = 34;
2148 }
2149
2150 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
2151 return 0;
2152}
2153
2154/* These tables from cdrecord - I don't have orange book */
2155/* standard speed CD-RW (1-4x) */
2156static char clv_to_speed[16] = {
2157 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2158 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2159};
2160/* high speed CD-RW (-10x) */
2161static char hs_clv_to_speed[16] = {
2162 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2163 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2164};
2165/* ultra high speed CD-RW */
2166static char us_clv_to_speed[16] = {
2167 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
2168 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
2169};
2170
2171/*
2172 * reads the maximum media speed from ATIP
2173 */
2174static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
2175{
2176 struct packet_command cgc;
2177 struct request_sense sense;
2178 unsigned char buf[64];
2179 unsigned int size, st, sp;
2180 int ret;
2181
2182 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
2183 cgc.sense = &sense;
2184 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2185 cgc.cmd[1] = 2;
2186 cgc.cmd[2] = 4; /* READ ATIP */
2187 cgc.cmd[8] = 2;
2188 ret = pkt_generic_packet(pd, &cgc);
2189 if (ret) {
2190 pkt_dump_sense(&cgc);
2191 return ret;
2192 }
2193 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
2194 if (size > sizeof(buf))
2195 size = sizeof(buf);
2196
2197 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
2198 cgc.sense = &sense;
2199 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2200 cgc.cmd[1] = 2;
2201 cgc.cmd[2] = 4;
2202 cgc.cmd[8] = size;
2203 ret = pkt_generic_packet(pd, &cgc);
2204 if (ret) {
2205 pkt_dump_sense(&cgc);
2206 return ret;
2207 }
2208
2209 if (!buf[6] & 0x40) {
Thomas Maier78220822006-10-04 02:15:28 -07002210 printk(DRIVER_NAME": Disc type is not CD-RW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return 1;
2212 }
2213 if (!buf[6] & 0x4) {
Thomas Maier78220822006-10-04 02:15:28 -07002214 printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return 1;
2216 }
2217
2218 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
2219
2220 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
2221
2222 /* Info from cdrecord */
2223 switch (st) {
2224 case 0: /* standard speed */
2225 *speed = clv_to_speed[sp];
2226 break;
2227 case 1: /* high speed */
2228 *speed = hs_clv_to_speed[sp];
2229 break;
2230 case 2: /* ultra high speed */
2231 *speed = us_clv_to_speed[sp];
2232 break;
2233 default:
Thomas Maier78220822006-10-04 02:15:28 -07002234 printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 1;
2236 }
2237 if (*speed) {
Thomas Maier78220822006-10-04 02:15:28 -07002238 printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return 0;
2240 } else {
Thomas Maier78220822006-10-04 02:15:28 -07002241 printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return 1;
2243 }
2244}
2245
2246static int pkt_perform_opc(struct pktcdvd_device *pd)
2247{
2248 struct packet_command cgc;
2249 struct request_sense sense;
2250 int ret;
2251
Thomas Maier78220822006-10-04 02:15:28 -07002252 VPRINTK(DRIVER_NAME": Performing OPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
2255 cgc.sense = &sense;
2256 cgc.timeout = 60*HZ;
2257 cgc.cmd[0] = GPCMD_SEND_OPC;
2258 cgc.cmd[1] = 1;
2259 if ((ret = pkt_generic_packet(pd, &cgc)))
2260 pkt_dump_sense(&cgc);
2261 return ret;
2262}
2263
2264static int pkt_open_write(struct pktcdvd_device *pd)
2265{
2266 int ret;
2267 unsigned int write_speed, media_write_speed, read_speed;
2268
2269 if ((ret = pkt_probe_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002270 VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
Peter Osterlund9db91542006-02-20 18:28:04 -08002271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273
2274 if ((ret = pkt_set_write_settings(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002275 DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -EIO;
2277 }
2278
2279 pkt_write_caching(pd, USE_WCACHING);
2280
2281 if ((ret = pkt_get_max_speed(pd, &write_speed)))
2282 write_speed = 16 * 177;
2283 switch (pd->mmc3_profile) {
2284 case 0x13: /* DVD-RW */
2285 case 0x1a: /* DVD+RW */
2286 case 0x12: /* DVD-RAM */
Thomas Maier78220822006-10-04 02:15:28 -07002287 DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 break;
2289 default:
2290 if ((ret = pkt_media_speed(pd, &media_write_speed)))
2291 media_write_speed = 16;
2292 write_speed = min(write_speed, media_write_speed * 177);
Thomas Maier78220822006-10-04 02:15:28 -07002293 DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 break;
2295 }
2296 read_speed = write_speed;
2297
2298 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
Thomas Maier78220822006-10-04 02:15:28 -07002299 DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return -EIO;
2301 }
2302 pd->write_speed = write_speed;
2303 pd->read_speed = read_speed;
2304
2305 if ((ret = pkt_perform_opc(pd))) {
Thomas Maier78220822006-10-04 02:15:28 -07002306 DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308
2309 return 0;
2310}
2311
2312/*
2313 * called at open time.
2314 */
2315static int pkt_open_dev(struct pktcdvd_device *pd, int write)
2316{
2317 int ret;
2318 long lba;
2319 request_queue_t *q;
2320
2321 /*
2322 * We need to re-open the cdrom device without O_NONBLOCK to be able
2323 * to read/write from/to it. It is already opened in O_NONBLOCK mode
2324 * so bdget() can't fail.
2325 */
2326 bdget(pd->bdev->bd_dev);
2327 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
2328 goto out;
2329
Peter Osterlund8382bf22006-01-08 01:02:17 -08002330 if ((ret = bd_claim(pd->bdev, pd)))
2331 goto out_putdev;
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if ((ret = pkt_get_last_written(pd, &lba))) {
Thomas Maier78220822006-10-04 02:15:28 -07002334 printk(DRIVER_NAME": pkt_get_last_written failed\n");
Peter Osterlund8382bf22006-01-08 01:02:17 -08002335 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
2338 set_capacity(pd->disk, lba << 2);
2339 set_capacity(pd->bdev->bd_disk, lba << 2);
2340 bd_set_size(pd->bdev, (loff_t)lba << 11);
2341
2342 q = bdev_get_queue(pd->bdev);
2343 if (write) {
2344 if ((ret = pkt_open_write(pd)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002345 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /*
2347 * Some CDRW drives can not handle writes larger than one packet,
2348 * even if the size is a multiple of the packet size.
2349 */
2350 spin_lock_irq(q->queue_lock);
2351 blk_queue_max_sectors(q, pd->settings.size);
2352 spin_unlock_irq(q->queue_lock);
2353 set_bit(PACKET_WRITABLE, &pd->flags);
2354 } else {
2355 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2356 clear_bit(PACKET_WRITABLE, &pd->flags);
2357 }
2358
2359 if ((ret = pkt_set_segment_merging(pd, q)))
Peter Osterlund8382bf22006-01-08 01:02:17 -08002360 goto out_unclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002362 if (write) {
2363 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
Thomas Maier78220822006-10-04 02:15:28 -07002364 printk(DRIVER_NAME": not enough memory for buffers\n");
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002365 ret = -ENOMEM;
2366 goto out_unclaim;
2367 }
Thomas Maier78220822006-10-04 02:15:28 -07002368 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 return 0;
2372
Peter Osterlund8382bf22006-01-08 01:02:17 -08002373out_unclaim:
2374 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375out_putdev:
2376 blkdev_put(pd->bdev);
2377out:
2378 return ret;
2379}
2380
2381/*
2382 * called when the device is closed. makes sure that the device flushes
2383 * the internal cache before we close.
2384 */
2385static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2386{
2387 if (flush && pkt_flush_cache(pd))
Thomas Maier78220822006-10-04 02:15:28 -07002388 DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 pkt_lock_door(pd, 0);
2391
2392 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
Peter Osterlund8382bf22006-01-08 01:02:17 -08002393 bd_release(pd->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 blkdev_put(pd->bdev);
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002395
2396 pkt_shrink_pktlist(pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2400{
2401 if (dev_minor >= MAX_WRITERS)
2402 return NULL;
2403 return pkt_devs[dev_minor];
2404}
2405
2406static int pkt_open(struct inode *inode, struct file *file)
2407{
2408 struct pktcdvd_device *pd = NULL;
2409 int ret;
2410
Thomas Maier78220822006-10-04 02:15:28 -07002411 VPRINTK(DRIVER_NAME": entering open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Jes Sorensen1657f822006-03-23 03:00:25 -08002413 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 pd = pkt_find_dev_from_minor(iminor(inode));
2415 if (!pd) {
2416 ret = -ENODEV;
2417 goto out;
2418 }
2419 BUG_ON(pd->refcnt < 0);
2420
2421 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002422 if (pd->refcnt > 1) {
2423 if ((file->f_mode & FMODE_WRITE) &&
2424 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2425 ret = -EBUSY;
2426 goto out_dec;
2427 }
2428 } else {
Peter Osterlund01fd9fd2006-02-14 13:52:55 -08002429 ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE);
2430 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /*
2433 * needed here as well, since ext2 (among others) may change
2434 * the blocksize at mount time
2435 */
2436 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2437 }
2438
Jes Sorensen1657f822006-03-23 03:00:25 -08002439 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return 0;
2441
2442out_dec:
2443 pd->refcnt--;
2444out:
Thomas Maier78220822006-10-04 02:15:28 -07002445 VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
Jes Sorensen1657f822006-03-23 03:00:25 -08002446 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return ret;
2448}
2449
2450static int pkt_close(struct inode *inode, struct file *file)
2451{
2452 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2453 int ret = 0;
2454
Jes Sorensen1657f822006-03-23 03:00:25 -08002455 mutex_lock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 pd->refcnt--;
2457 BUG_ON(pd->refcnt < 0);
2458 if (pd->refcnt == 0) {
2459 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2460 pkt_release_dev(pd, flush);
2461 }
Jes Sorensen1657f822006-03-23 03:00:25 -08002462 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return ret;
2464}
2465
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2468{
2469 struct packet_stacked_data *psd = bio->bi_private;
2470 struct pktcdvd_device *pd = psd->pd;
2471
2472 if (bio->bi_size)
2473 return 1;
2474
2475 bio_put(bio);
2476 bio_endio(psd->bio, psd->bio->bi_size, err);
2477 mempool_free(psd, psd_pool);
2478 pkt_bio_finished(pd);
2479 return 0;
2480}
2481
2482static int pkt_make_request(request_queue_t *q, struct bio *bio)
2483{
2484 struct pktcdvd_device *pd;
2485 char b[BDEVNAME_SIZE];
2486 sector_t zone;
2487 struct packet_data *pkt;
2488 int was_empty, blocked_bio;
2489 struct pkt_rb_node *node;
2490
2491 pd = q->queuedata;
2492 if (!pd) {
Thomas Maier78220822006-10-04 02:15:28 -07002493 printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 goto end_io;
2495 }
2496
2497 /*
2498 * Clone READ bios so we can have our own bi_end_io callback.
2499 */
2500 if (bio_data_dir(bio) == READ) {
2501 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2502 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2503
2504 psd->pd = pd;
2505 psd->bio = bio;
2506 cloned_bio->bi_bdev = pd->bdev;
2507 cloned_bio->bi_private = psd;
2508 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2509 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002510 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return 0;
2512 }
2513
2514 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
Thomas Maier78220822006-10-04 02:15:28 -07002515 printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 pd->name, (unsigned long long)bio->bi_sector);
2517 goto end_io;
2518 }
2519
2520 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
Thomas Maier78220822006-10-04 02:15:28 -07002521 printk(DRIVER_NAME": wrong bio size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 goto end_io;
2523 }
2524
2525 blk_queue_bounce(q, &bio);
2526
2527 zone = ZONE(bio->bi_sector, pd);
2528 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2529 (unsigned long long)bio->bi_sector,
2530 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2531
2532 /* Check if we have to split the bio */
2533 {
2534 struct bio_pair *bp;
2535 sector_t last_zone;
2536 int first_sectors;
2537
2538 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2539 if (last_zone != zone) {
2540 BUG_ON(last_zone != zone + pd->settings.size);
2541 first_sectors = last_zone - bio->bi_sector;
2542 bp = bio_split(bio, bio_split_pool, first_sectors);
2543 BUG_ON(!bp);
2544 pkt_make_request(q, &bp->bio1);
2545 pkt_make_request(q, &bp->bio2);
2546 bio_pair_release(bp);
2547 return 0;
2548 }
2549 }
2550
2551 /*
2552 * If we find a matching packet in state WAITING or READ_WAIT, we can
2553 * just append this bio to that packet.
2554 */
2555 spin_lock(&pd->cdrw.active_list_lock);
2556 blocked_bio = 0;
2557 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2558 if (pkt->sector == zone) {
2559 spin_lock(&pkt->lock);
2560 if ((pkt->state == PACKET_WAITING_STATE) ||
2561 (pkt->state == PACKET_READ_WAIT_STATE)) {
2562 pkt_add_list_last(bio, &pkt->orig_bios,
2563 &pkt->orig_bios_tail);
2564 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2565 if ((pkt->write_size >= pkt->frames) &&
2566 (pkt->state == PACKET_WAITING_STATE)) {
2567 atomic_inc(&pkt->run_sm);
2568 wake_up(&pd->wqueue);
2569 }
2570 spin_unlock(&pkt->lock);
2571 spin_unlock(&pd->cdrw.active_list_lock);
2572 return 0;
2573 } else {
2574 blocked_bio = 1;
2575 }
2576 spin_unlock(&pkt->lock);
2577 }
2578 }
2579 spin_unlock(&pd->cdrw.active_list_lock);
2580
Thomas Maier0a0fc962006-12-08 02:36:11 -08002581 /*
2582 * Test if there is enough room left in the bio work queue
2583 * (queue size >= congestion on mark).
2584 * If not, wait till the work queue size is below the congestion off mark.
2585 */
2586 spin_lock(&pd->lock);
2587 if (pd->write_congestion_on > 0
2588 && pd->bio_queue_size >= pd->write_congestion_on) {
Thomas Maier83f3aa32007-02-10 01:45:11 -08002589 set_bdi_congested(&q->backing_dev_info, WRITE);
Thomas Maier0a0fc962006-12-08 02:36:11 -08002590 do {
2591 spin_unlock(&pd->lock);
2592 congestion_wait(WRITE, HZ);
2593 spin_lock(&pd->lock);
2594 } while(pd->bio_queue_size > pd->write_congestion_off);
2595 }
2596 spin_unlock(&pd->lock);
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /*
2599 * No matching packet found. Store the bio in the work queue.
2600 */
2601 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 node->bio = bio;
2603 spin_lock(&pd->lock);
2604 BUG_ON(pd->bio_queue_size < 0);
2605 was_empty = (pd->bio_queue_size == 0);
2606 pkt_rbtree_insert(pd, node);
2607 spin_unlock(&pd->lock);
2608
2609 /*
2610 * Wake up the worker thread.
2611 */
2612 atomic_set(&pd->scan_queue, 1);
2613 if (was_empty) {
2614 /* This wake_up is required for correct operation */
2615 wake_up(&pd->wqueue);
2616 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2617 /*
2618 * This wake up is not required for correct operation,
2619 * but improves performance in some cases.
2620 */
2621 wake_up(&pd->wqueue);
2622 }
2623 return 0;
2624end_io:
2625 bio_io_error(bio, bio->bi_size);
2626 return 0;
2627}
2628
2629
2630
2631static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2632{
2633 struct pktcdvd_device *pd = q->queuedata;
2634 sector_t zone = ZONE(bio->bi_sector, pd);
2635 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2636 int remaining = (pd->settings.size << 9) - used;
2637 int remaining2;
2638
2639 /*
2640 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2641 * boundary, pkt_make_request() will split the bio.
2642 */
2643 remaining2 = PAGE_SIZE - bio->bi_size;
2644 remaining = max(remaining, remaining2);
2645
2646 BUG_ON(remaining < 0);
2647 return remaining;
2648}
2649
2650static void pkt_init_queue(struct pktcdvd_device *pd)
2651{
2652 request_queue_t *q = pd->disk->queue;
2653
2654 blk_queue_make_request(q, pkt_make_request);
2655 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2656 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2657 blk_queue_merge_bvec(q, pkt_merge_bvec);
2658 q->queuedata = pd;
2659}
2660
2661static int pkt_seq_show(struct seq_file *m, void *p)
2662{
2663 struct pktcdvd_device *pd = m->private;
2664 char *msg;
2665 char bdev_buf[BDEVNAME_SIZE];
2666 int states[PACKET_NUM_STATES];
2667
2668 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2669 bdevname(pd->bdev, bdev_buf));
2670
2671 seq_printf(m, "\nSettings:\n");
2672 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2673
2674 if (pd->settings.write_type == 0)
2675 msg = "Packet";
2676 else
2677 msg = "Unknown";
2678 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2679
2680 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2681 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2682
2683 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2684
2685 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2686 msg = "Mode 1";
2687 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2688 msg = "Mode 2";
2689 else
2690 msg = "Unknown";
2691 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2692
2693 seq_printf(m, "\nStatistics:\n");
2694 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2695 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2696 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2697 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2698 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2699
2700 seq_printf(m, "\nMisc:\n");
2701 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2702 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2703 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2704 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2705 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2706 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2707
2708 seq_printf(m, "\nQueue state:\n");
2709 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2710 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2711 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2712
2713 pkt_count_states(pd, states);
2714 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2715 states[0], states[1], states[2], states[3], states[4], states[5]);
2716
Thomas Maier0a0fc962006-12-08 02:36:11 -08002717 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
2718 pd->write_congestion_off,
2719 pd->write_congestion_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return 0;
2721}
2722
2723static int pkt_seq_open(struct inode *inode, struct file *file)
2724{
2725 return single_open(file, pkt_seq_show, PDE(inode)->data);
2726}
2727
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002728static const struct file_operations pkt_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .open = pkt_seq_open,
2730 .read = seq_read,
2731 .llseek = seq_lseek,
2732 .release = single_release
2733};
2734
2735static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2736{
2737 int i;
2738 int ret = 0;
2739 char b[BDEVNAME_SIZE];
2740 struct proc_dir_entry *proc;
2741 struct block_device *bdev;
2742
2743 if (pd->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002744 printk(DRIVER_NAME": Recursive setup not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return -EBUSY;
2746 }
2747 for (i = 0; i < MAX_WRITERS; i++) {
2748 struct pktcdvd_device *pd2 = pkt_devs[i];
2749 if (!pd2)
2750 continue;
2751 if (pd2->bdev->bd_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002752 printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return -EBUSY;
2754 }
2755 if (pd2->pkt_dev == dev) {
Thomas Maier78220822006-10-04 02:15:28 -07002756 printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return -EBUSY;
2758 }
2759 }
2760
2761 bdev = bdget(dev);
2762 if (!bdev)
2763 return -ENOMEM;
2764 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2765 if (ret)
2766 return ret;
2767
2768 /* This is safe, since we have a reference from open(). */
2769 __module_get(THIS_MODULE);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 pd->bdev = bdev;
2772 set_blocksize(bdev, CD_FRAMESIZE);
2773
2774 pkt_init_queue(pd);
2775
2776 atomic_set(&pd->cdrw.pending_bios, 0);
2777 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2778 if (IS_ERR(pd->cdrw.thread)) {
Thomas Maier78220822006-10-04 02:15:28 -07002779 printk(DRIVER_NAME": can't start kernel thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 ret = -ENOMEM;
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002781 goto out_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
2783
2784 proc = create_proc_entry(pd->name, 0, pkt_proc);
2785 if (proc) {
2786 proc->data = pd;
2787 proc->proc_fops = &pkt_proc_fops;
2788 }
Thomas Maier78220822006-10-04 02:15:28 -07002789 DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return 0;
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792out_mem:
2793 blkdev_put(bdev);
2794 /* This is safe: open() is still holding a reference. */
2795 module_put(THIS_MODULE);
2796 return ret;
2797}
2798
2799static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2800{
2801 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2802
2803 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 switch (cmd) {
2806 /*
2807 * forward selected CDROM ioctls to CD-ROM, for UDF
2808 */
2809 case CDROMMULTISESSION:
2810 case CDROMREADTOCENTRY:
2811 case CDROM_LAST_WRITTEN:
2812 case CDROM_SEND_PACKET:
2813 case SCSI_IOCTL_SEND_COMMAND:
Peter Osterlund118326e2005-05-14 00:58:30 -07002814 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 case CDROMEJECT:
2817 /*
2818 * The door gets locked when the device is opened, so we
2819 * have to unlock it or else the eject command fails.
2820 */
Peter Osterlund948423e2006-02-14 13:52:56 -08002821 if (pd->refcnt == 1)
2822 pkt_lock_door(pd, 0);
Peter Osterlund118326e2005-05-14 00:58:30 -07002823 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 default:
Thomas Maier78220822006-10-04 02:15:28 -07002826 VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return -ENOTTY;
2828 }
2829
2830 return 0;
2831}
2832
2833static int pkt_media_changed(struct gendisk *disk)
2834{
2835 struct pktcdvd_device *pd = disk->private_data;
2836 struct gendisk *attached_disk;
2837
2838 if (!pd)
2839 return 0;
2840 if (!pd->bdev)
2841 return 0;
2842 attached_disk = pd->bdev->bd_disk;
2843 if (!attached_disk)
2844 return 0;
2845 return attached_disk->fops->media_changed(attached_disk);
2846}
2847
2848static struct block_device_operations pktcdvd_ops = {
2849 .owner = THIS_MODULE,
2850 .open = pkt_open,
2851 .release = pkt_close,
2852 .ioctl = pkt_ioctl,
2853 .media_changed = pkt_media_changed,
2854};
2855
2856/*
2857 * Set up mapping from pktcdvd device to CD-ROM device.
2858 */
Thomas Maieradb92502006-12-08 02:36:10 -08002859static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 int idx;
2862 int ret = -ENOMEM;
2863 struct pktcdvd_device *pd;
2864 struct gendisk *disk;
Thomas Maieradb92502006-12-08 02:36:10 -08002865
2866 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 for (idx = 0; idx < MAX_WRITERS; idx++)
2869 if (!pkt_devs[idx])
2870 break;
2871 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002872 printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
Thomas Maieradb92502006-12-08 02:36:10 -08002873 ret = -EBUSY;
2874 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876
Peter Osterlund1107d2e2005-09-13 01:25:29 -07002877 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (!pd)
Thomas Maieradb92502006-12-08 02:36:10 -08002879 goto out_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002881 pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
2882 sizeof(struct pkt_rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!pd->rb_pool)
2884 goto out_mem;
2885
Peter Osterlunde1bc89b2006-02-04 23:27:47 -08002886 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
2887 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
2888 spin_lock_init(&pd->cdrw.active_list_lock);
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 spin_lock_init(&pd->lock);
2891 spin_lock_init(&pd->iosched.lock);
Thomas Maier78220822006-10-04 02:15:28 -07002892 sprintf(pd->name, DRIVER_NAME"%d", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 init_waitqueue_head(&pd->wqueue);
2894 pd->bio_queue = RB_ROOT;
2895
Thomas Maier0a0fc962006-12-08 02:36:11 -08002896 pd->write_congestion_on = write_congestion_on;
2897 pd->write_congestion_off = write_congestion_off;
2898
Thomas Maieradb92502006-12-08 02:36:10 -08002899 disk = alloc_disk(1);
2900 if (!disk)
2901 goto out_mem;
2902 pd->disk = disk;
Thomas Maieradd21662006-10-04 02:15:30 -07002903 disk->major = pktdev_major;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 disk->first_minor = idx;
2905 disk->fops = &pktcdvd_ops;
2906 disk->flags = GENHD_FL_REMOVABLE;
Thomas Maieradb92502006-12-08 02:36:10 -08002907 strcpy(disk->disk_name, pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 disk->private_data = pd;
2909 disk->queue = blk_alloc_queue(GFP_KERNEL);
2910 if (!disk->queue)
2911 goto out_mem2;
2912
2913 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2914 ret = pkt_new_dev(pd, dev);
2915 if (ret)
2916 goto out_new_dev;
2917
2918 add_disk(disk);
Thomas Maieradb92502006-12-08 02:36:10 -08002919
Thomas Maier32694852006-12-08 02:36:12 -08002920 pkt_sysfs_dev_new(pd);
2921 pkt_debugfs_dev_new(pd);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 pkt_devs[idx] = pd;
Thomas Maieradb92502006-12-08 02:36:10 -08002924 if (pkt_dev)
2925 *pkt_dev = pd->pkt_dev;
2926
2927 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return 0;
2929
2930out_new_dev:
Al Viro1312f402006-03-12 11:02:03 -05002931 blk_cleanup_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932out_mem2:
2933 put_disk(disk);
2934out_mem:
2935 if (pd->rb_pool)
2936 mempool_destroy(pd->rb_pool);
2937 kfree(pd);
Thomas Maieradb92502006-12-08 02:36:10 -08002938out_mutex:
2939 mutex_unlock(&ctl_mutex);
2940 printk(DRIVER_NAME": setup of pktcdvd device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return ret;
2942}
2943
2944/*
2945 * Tear down mapping from pktcdvd device to CD-ROM device.
2946 */
Thomas Maieradb92502006-12-08 02:36:10 -08002947static int pkt_remove_dev(dev_t pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 struct pktcdvd_device *pd;
2950 int idx;
Thomas Maieradb92502006-12-08 02:36:10 -08002951 int ret = 0;
2952
2953 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 for (idx = 0; idx < MAX_WRITERS; idx++) {
2956 pd = pkt_devs[idx];
2957 if (pd && (pd->pkt_dev == pkt_dev))
2958 break;
2959 }
2960 if (idx == MAX_WRITERS) {
Thomas Maier78220822006-10-04 02:15:28 -07002961 DPRINTK(DRIVER_NAME": dev not setup\n");
Thomas Maieradb92502006-12-08 02:36:10 -08002962 ret = -ENXIO;
2963 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
Thomas Maieradb92502006-12-08 02:36:10 -08002966 if (pd->refcnt > 0) {
2967 ret = -EBUSY;
2968 goto out;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (!IS_ERR(pd->cdrw.thread))
2971 kthread_stop(pd->cdrw.thread);
2972
Thomas Maier32694852006-12-08 02:36:12 -08002973 pkt_devs[idx] = NULL;
2974
2975 pkt_debugfs_dev_remove(pd);
2976 pkt_sysfs_dev_remove(pd);
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 blkdev_put(pd->bdev);
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 remove_proc_entry(pd->name, pkt_proc);
Thomas Maier78220822006-10-04 02:15:28 -07002981 DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 del_gendisk(pd->disk);
Al Viro1312f402006-03-12 11:02:03 -05002984 blk_cleanup_queue(pd->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 put_disk(pd->disk);
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 mempool_destroy(pd->rb_pool);
2988 kfree(pd);
2989
2990 /* This is safe: open() is still holding a reference. */
2991 module_put(THIS_MODULE);
Thomas Maieradb92502006-12-08 02:36:10 -08002992
2993out:
2994 mutex_unlock(&ctl_mutex);
2995 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
2998static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2999{
Thomas Maieradb92502006-12-08 02:36:10 -08003000 struct pktcdvd_device *pd;
3001
3002 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3003
3004 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (pd) {
3006 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
3007 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
3008 } else {
3009 ctrl_cmd->dev = 0;
3010 ctrl_cmd->pkt_dev = 0;
3011 }
3012 ctrl_cmd->num_devices = MAX_WRITERS;
Thomas Maieradb92502006-12-08 02:36:10 -08003013
3014 mutex_unlock(&ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3018{
3019 void __user *argp = (void __user *)arg;
3020 struct pkt_ctrl_command ctrl_cmd;
3021 int ret = 0;
Thomas Maieradb92502006-12-08 02:36:10 -08003022 dev_t pkt_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 if (cmd != PACKET_CTRL_CMD)
3025 return -ENOTTY;
3026
3027 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
3028 return -EFAULT;
3029
3030 switch (ctrl_cmd.command) {
3031 case PKT_CTRL_CMD_SETUP:
3032 if (!capable(CAP_SYS_ADMIN))
3033 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003034 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
3035 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 break;
3037 case PKT_CTRL_CMD_TEARDOWN:
3038 if (!capable(CAP_SYS_ADMIN))
3039 return -EPERM;
Thomas Maieradb92502006-12-08 02:36:10 -08003040 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 break;
3042 case PKT_CTRL_CMD_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 pkt_get_status(&ctrl_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 break;
3045 default:
3046 return -ENOTTY;
3047 }
3048
3049 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
3050 return -EFAULT;
3051 return ret;
3052}
3053
3054
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003055static const struct file_operations pkt_ctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .ioctl = pkt_ctl_ioctl,
3057 .owner = THIS_MODULE,
3058};
3059
3060static struct miscdevice pkt_misc = {
3061 .minor = MISC_DYNAMIC_MINOR,
Thomas Maier78220822006-10-04 02:15:28 -07003062 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 .fops = &pkt_ctl_fops
3064};
3065
3066static int __init pkt_init(void)
3067{
3068 int ret;
3069
Thomas Maier32694852006-12-08 02:36:12 -08003070 mutex_init(&ctl_mutex);
3071
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08003072 psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
3073 sizeof(struct packet_stacked_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (!psd_pool)
3075 return -ENOMEM;
3076
Thomas Maieradd21662006-10-04 02:15:30 -07003077 ret = register_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (ret < 0) {
Thomas Maier78220822006-10-04 02:15:28 -07003079 printk(DRIVER_NAME": Unable to register block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 goto out2;
3081 }
Thomas Maieradd21662006-10-04 02:15:30 -07003082 if (!pktdev_major)
3083 pktdev_major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Thomas Maier32694852006-12-08 02:36:12 -08003085 ret = pkt_sysfs_init();
3086 if (ret)
3087 goto out;
3088
3089 pkt_debugfs_init();
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 ret = misc_register(&pkt_misc);
3092 if (ret) {
Thomas Maier78220822006-10-04 02:15:28 -07003093 printk(DRIVER_NAME": Unable to register misc device\n");
Thomas Maier32694852006-12-08 02:36:12 -08003094 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
Thomas Maier78220822006-10-04 02:15:28 -07003097 pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return 0;
3100
Thomas Maier32694852006-12-08 02:36:12 -08003101out_misc:
3102 pkt_debugfs_cleanup();
3103 pkt_sysfs_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104out:
Thomas Maieradd21662006-10-04 02:15:30 -07003105 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106out2:
3107 mempool_destroy(psd_pool);
3108 return ret;
3109}
3110
3111static void __exit pkt_exit(void)
3112{
Thomas Maier78220822006-10-04 02:15:28 -07003113 remove_proc_entry(DRIVER_NAME, proc_root_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 misc_deregister(&pkt_misc);
Thomas Maier32694852006-12-08 02:36:12 -08003115
3116 pkt_debugfs_cleanup();
3117 pkt_sysfs_cleanup();
3118
Thomas Maieradd21662006-10-04 02:15:30 -07003119 unregister_blkdev(pktdev_major, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 mempool_destroy(psd_pool);
3121}
3122
3123MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
3124MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
3125MODULE_LICENSE("GPL");
3126
3127module_init(pkt_init);
3128module_exit(pkt_exit);