Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 Jens Axboe <axboe@suse.de> |
| 3 | * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com> |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 4 | * Copyright (C) 2006 Thomas Maier <balagi@justmail.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * May be copied or modified under the terms of the GNU General Public |
| 7 | * License. See linux/COPYING for more information. |
| 8 | * |
Peter Osterlund | a676f8d0 | 2005-09-13 01:25:27 -0700 | [diff] [blame] | 9 | * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and |
| 10 | * DVD-RAM devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * Theory of operation: |
| 13 | * |
Peter Osterlund | a676f8d0 | 2005-09-13 01:25:27 -0700 | [diff] [blame] | 14 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * |
Peter Osterlund | a676f8d0 | 2005-09-13 01:25:27 -0700 | [diff] [blame] | 23 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * |
| 45 | *************************************************************************/ |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/pktcdvd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #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 Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 58 | #include <linux/freezer.h> |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 59 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <scsi/scsi_cmnd.h> |
| 61 | #include <scsi/scsi_ioctl.h> |
Peter Osterlund | cef28963 | 2006-02-20 18:28:01 -0800 | [diff] [blame] | 62 | #include <scsi/scsi.h> |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 63 | #include <linux/debugfs.h> |
| 64 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | #include <asm/uaccess.h> |
| 67 | |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 68 | #define DRIVER_NAME "pktcdvd" |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #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 | |
| 86 | static struct pktcdvd_device *pkt_devs[MAX_WRITERS]; |
| 87 | static struct proc_dir_entry *pkt_proc; |
Thomas Maier | add2166 | 2006-10-04 02:15:30 -0700 | [diff] [blame] | 88 | static int pktdev_major; |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 89 | static int write_congestion_on = PKT_WRITE_CONGESTION_ON; |
| 90 | static int write_congestion_off = PKT_WRITE_CONGESTION_OFF; |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 91 | static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | static mempool_t *psd_pool; |
| 93 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 94 | static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */ |
| 95 | static struct dentry *pkt_debugfs_root = NULL; /* /debug/pktcdvd */ |
| 96 | |
| 97 | /* forward declaration */ |
| 98 | static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev); |
| 99 | static int pkt_remove_dev(dev_t pkt_dev); |
| 100 | static int pkt_seq_show(struct seq_file *m, void *p); |
| 101 | |
| 102 | |
| 103 | |
| 104 | /* |
| 105 | * create and register a pktcdvd kernel object. |
| 106 | */ |
| 107 | static 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 | */ |
| 127 | static 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 | */ |
| 135 | static 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) \ |
| 149 | static struct attribute _obj = { \ |
| 150 | .name = _name, .owner = THIS_MODULE, .mode = _mode } |
| 151 | |
| 152 | /********************************************************** |
| 153 | /sys/class/pktcdvd/pktcdvd[0-7]/ |
| 154 | stat/reset |
| 155 | stat/packets_started |
| 156 | stat/packets_finished |
| 157 | stat/kb_written |
| 158 | stat/kb_read |
| 159 | stat/kb_read_gather |
| 160 | write_queue/size |
| 161 | write_queue/congestion_off |
| 162 | write_queue/congestion_on |
| 163 | **********************************************************/ |
| 164 | |
| 165 | DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200); |
| 166 | DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444); |
| 167 | DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444); |
| 168 | DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444); |
| 169 | DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444); |
| 170 | DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444); |
| 171 | |
| 172 | static struct attribute *kobj_pkt_attrs_stat[] = { |
| 173 | &kobj_pkt_attr_st1, |
| 174 | &kobj_pkt_attr_st2, |
| 175 | &kobj_pkt_attr_st3, |
| 176 | &kobj_pkt_attr_st4, |
| 177 | &kobj_pkt_attr_st5, |
| 178 | &kobj_pkt_attr_st6, |
| 179 | NULL |
| 180 | }; |
| 181 | |
| 182 | DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444); |
| 183 | DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644); |
| 184 | DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644); |
| 185 | |
| 186 | static struct attribute *kobj_pkt_attrs_wqueue[] = { |
| 187 | &kobj_pkt_attr_wq1, |
| 188 | &kobj_pkt_attr_wq2, |
| 189 | &kobj_pkt_attr_wq3, |
| 190 | NULL |
| 191 | }; |
| 192 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 193 | static ssize_t kobj_pkt_show(struct kobject *kobj, |
| 194 | struct attribute *attr, char *data) |
| 195 | { |
| 196 | struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd; |
| 197 | int n = 0; |
| 198 | int v; |
| 199 | if (strcmp(attr->name, "packets_started") == 0) { |
| 200 | n = sprintf(data, "%lu\n", pd->stats.pkt_started); |
| 201 | |
| 202 | } else if (strcmp(attr->name, "packets_finished") == 0) { |
| 203 | n = sprintf(data, "%lu\n", pd->stats.pkt_ended); |
| 204 | |
| 205 | } else if (strcmp(attr->name, "kb_written") == 0) { |
| 206 | n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1); |
| 207 | |
| 208 | } else if (strcmp(attr->name, "kb_read") == 0) { |
| 209 | n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1); |
| 210 | |
| 211 | } else if (strcmp(attr->name, "kb_read_gather") == 0) { |
| 212 | n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1); |
| 213 | |
| 214 | } else if (strcmp(attr->name, "size") == 0) { |
| 215 | spin_lock(&pd->lock); |
| 216 | v = pd->bio_queue_size; |
| 217 | spin_unlock(&pd->lock); |
| 218 | n = sprintf(data, "%d\n", v); |
| 219 | |
| 220 | } else if (strcmp(attr->name, "congestion_off") == 0) { |
| 221 | spin_lock(&pd->lock); |
| 222 | v = pd->write_congestion_off; |
| 223 | spin_unlock(&pd->lock); |
| 224 | n = sprintf(data, "%d\n", v); |
| 225 | |
| 226 | } else if (strcmp(attr->name, "congestion_on") == 0) { |
| 227 | spin_lock(&pd->lock); |
| 228 | v = pd->write_congestion_on; |
| 229 | spin_unlock(&pd->lock); |
| 230 | n = sprintf(data, "%d\n", v); |
| 231 | } |
| 232 | return n; |
| 233 | } |
| 234 | |
| 235 | static void init_write_congestion_marks(int* lo, int* hi) |
| 236 | { |
| 237 | if (*hi > 0) { |
| 238 | *hi = max(*hi, 500); |
| 239 | *hi = min(*hi, 1000000); |
| 240 | if (*lo <= 0) |
| 241 | *lo = *hi - 100; |
| 242 | else { |
| 243 | *lo = min(*lo, *hi - 100); |
| 244 | *lo = max(*lo, 100); |
| 245 | } |
| 246 | } else { |
| 247 | *hi = -1; |
| 248 | *lo = -1; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static ssize_t kobj_pkt_store(struct kobject *kobj, |
| 253 | struct attribute *attr, |
| 254 | const char *data, size_t len) |
| 255 | { |
| 256 | struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd; |
| 257 | int val; |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 258 | |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 259 | if (strcmp(attr->name, "reset") == 0 && len > 0) { |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 260 | pd->stats.pkt_started = 0; |
| 261 | pd->stats.pkt_ended = 0; |
| 262 | pd->stats.secs_w = 0; |
| 263 | pd->stats.secs_rg = 0; |
| 264 | pd->stats.secs_r = 0; |
| 265 | |
| 266 | } else if (strcmp(attr->name, "congestion_off") == 0 |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 267 | && sscanf(data, "%d", &val) == 1) { |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 268 | spin_lock(&pd->lock); |
| 269 | pd->write_congestion_off = val; |
| 270 | init_write_congestion_marks(&pd->write_congestion_off, |
| 271 | &pd->write_congestion_on); |
| 272 | spin_unlock(&pd->lock); |
| 273 | |
| 274 | } else if (strcmp(attr->name, "congestion_on") == 0 |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 275 | && sscanf(data, "%d", &val) == 1) { |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 276 | spin_lock(&pd->lock); |
| 277 | pd->write_congestion_on = val; |
| 278 | init_write_congestion_marks(&pd->write_congestion_off, |
| 279 | &pd->write_congestion_on); |
| 280 | spin_unlock(&pd->lock); |
| 281 | } |
| 282 | return len; |
| 283 | } |
| 284 | |
| 285 | static struct sysfs_ops kobj_pkt_ops = { |
| 286 | .show = kobj_pkt_show, |
| 287 | .store = kobj_pkt_store |
| 288 | }; |
| 289 | static struct kobj_type kobj_pkt_type_stat = { |
| 290 | .release = pkt_kobj_release, |
| 291 | .sysfs_ops = &kobj_pkt_ops, |
| 292 | .default_attrs = kobj_pkt_attrs_stat |
| 293 | }; |
| 294 | static struct kobj_type kobj_pkt_type_wqueue = { |
| 295 | .release = pkt_kobj_release, |
| 296 | .sysfs_ops = &kobj_pkt_ops, |
| 297 | .default_attrs = kobj_pkt_attrs_wqueue |
| 298 | }; |
| 299 | |
| 300 | static void pkt_sysfs_dev_new(struct pktcdvd_device *pd) |
| 301 | { |
| 302 | if (class_pktcdvd) { |
| 303 | pd->clsdev = class_device_create(class_pktcdvd, |
| 304 | NULL, pd->pkt_dev, |
| 305 | NULL, "%s", pd->name); |
| 306 | if (IS_ERR(pd->clsdev)) |
| 307 | pd->clsdev = NULL; |
| 308 | } |
| 309 | if (pd->clsdev) { |
| 310 | pd->kobj_stat = pkt_kobj_create(pd, "stat", |
| 311 | &pd->clsdev->kobj, |
| 312 | &kobj_pkt_type_stat); |
| 313 | pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue", |
| 314 | &pd->clsdev->kobj, |
| 315 | &kobj_pkt_type_wqueue); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd) |
| 320 | { |
| 321 | pkt_kobj_remove(pd->kobj_stat); |
| 322 | pkt_kobj_remove(pd->kobj_wqueue); |
| 323 | if (class_pktcdvd) |
| 324 | class_device_destroy(class_pktcdvd, pd->pkt_dev); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /******************************************************************** |
| 329 | /sys/class/pktcdvd/ |
| 330 | add map block device |
| 331 | remove unmap packet dev |
| 332 | device_map show mappings |
| 333 | *******************************************************************/ |
| 334 | |
| 335 | static void class_pktcdvd_release(struct class *cls) |
| 336 | { |
| 337 | kfree(cls); |
| 338 | } |
| 339 | static ssize_t class_pktcdvd_show_map(struct class *c, char *data) |
| 340 | { |
| 341 | int n = 0; |
| 342 | int idx; |
| 343 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 344 | for (idx = 0; idx < MAX_WRITERS; idx++) { |
| 345 | struct pktcdvd_device *pd = pkt_devs[idx]; |
| 346 | if (!pd) |
| 347 | continue; |
| 348 | n += sprintf(data+n, "%s %u:%u %u:%u\n", |
| 349 | pd->name, |
| 350 | MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev), |
| 351 | MAJOR(pd->bdev->bd_dev), |
| 352 | MINOR(pd->bdev->bd_dev)); |
| 353 | } |
| 354 | mutex_unlock(&ctl_mutex); |
| 355 | return n; |
| 356 | } |
| 357 | |
| 358 | static ssize_t class_pktcdvd_store_add(struct class *c, const char *buf, |
| 359 | size_t count) |
| 360 | { |
| 361 | unsigned int major, minor; |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 362 | if (sscanf(buf, "%u:%u", &major, &minor) == 2) { |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 363 | pkt_setup_dev(MKDEV(major, minor), NULL); |
| 364 | return count; |
| 365 | } |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | |
| 369 | static ssize_t class_pktcdvd_store_remove(struct class *c, const char *buf, |
| 370 | size_t count) |
| 371 | { |
| 372 | unsigned int major, minor; |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 373 | if (sscanf(buf, "%u:%u", &major, &minor) == 2) { |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 374 | pkt_remove_dev(MKDEV(major, minor)); |
| 375 | return count; |
| 376 | } |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | static struct class_attribute class_pktcdvd_attrs[] = { |
| 381 | __ATTR(add, 0200, NULL, class_pktcdvd_store_add), |
| 382 | __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove), |
| 383 | __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL), |
| 384 | __ATTR_NULL |
| 385 | }; |
| 386 | |
| 387 | |
| 388 | static int pkt_sysfs_init(void) |
| 389 | { |
| 390 | int ret = 0; |
| 391 | |
| 392 | /* |
| 393 | * create control files in sysfs |
| 394 | * /sys/class/pktcdvd/... |
| 395 | */ |
| 396 | class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL); |
| 397 | if (!class_pktcdvd) |
| 398 | return -ENOMEM; |
| 399 | class_pktcdvd->name = DRIVER_NAME; |
| 400 | class_pktcdvd->owner = THIS_MODULE; |
| 401 | class_pktcdvd->class_release = class_pktcdvd_release; |
| 402 | class_pktcdvd->class_attrs = class_pktcdvd_attrs; |
| 403 | ret = class_register(class_pktcdvd); |
| 404 | if (ret) { |
| 405 | kfree(class_pktcdvd); |
| 406 | class_pktcdvd = NULL; |
| 407 | printk(DRIVER_NAME": failed to create class pktcdvd\n"); |
| 408 | return ret; |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static void pkt_sysfs_cleanup(void) |
| 414 | { |
| 415 | if (class_pktcdvd) |
| 416 | class_destroy(class_pktcdvd); |
| 417 | class_pktcdvd = NULL; |
| 418 | } |
| 419 | |
| 420 | /******************************************************************** |
| 421 | entries in debugfs |
| 422 | |
| 423 | /debugfs/pktcdvd[0-7]/ |
| 424 | info |
| 425 | |
| 426 | *******************************************************************/ |
| 427 | |
| 428 | static int pkt_debugfs_seq_show(struct seq_file *m, void *p) |
| 429 | { |
| 430 | return pkt_seq_show(m, p); |
| 431 | } |
| 432 | |
| 433 | static int pkt_debugfs_fops_open(struct inode *inode, struct file *file) |
| 434 | { |
| 435 | return single_open(file, pkt_debugfs_seq_show, inode->i_private); |
| 436 | } |
| 437 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 438 | static const struct file_operations debug_fops = { |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 439 | .open = pkt_debugfs_fops_open, |
| 440 | .read = seq_read, |
| 441 | .llseek = seq_lseek, |
| 442 | .release = single_release, |
| 443 | .owner = THIS_MODULE, |
| 444 | }; |
| 445 | |
| 446 | static void pkt_debugfs_dev_new(struct pktcdvd_device *pd) |
| 447 | { |
| 448 | if (!pkt_debugfs_root) |
| 449 | return; |
| 450 | pd->dfs_f_info = NULL; |
| 451 | pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root); |
| 452 | if (IS_ERR(pd->dfs_d_root)) { |
| 453 | pd->dfs_d_root = NULL; |
| 454 | return; |
| 455 | } |
| 456 | pd->dfs_f_info = debugfs_create_file("info", S_IRUGO, |
| 457 | pd->dfs_d_root, pd, &debug_fops); |
| 458 | if (IS_ERR(pd->dfs_f_info)) { |
| 459 | pd->dfs_f_info = NULL; |
| 460 | return; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd) |
| 465 | { |
| 466 | if (!pkt_debugfs_root) |
| 467 | return; |
| 468 | if (pd->dfs_f_info) |
| 469 | debugfs_remove(pd->dfs_f_info); |
| 470 | pd->dfs_f_info = NULL; |
| 471 | if (pd->dfs_d_root) |
| 472 | debugfs_remove(pd->dfs_d_root); |
| 473 | pd->dfs_d_root = NULL; |
| 474 | } |
| 475 | |
| 476 | static void pkt_debugfs_init(void) |
| 477 | { |
| 478 | pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL); |
| 479 | if (IS_ERR(pkt_debugfs_root)) { |
| 480 | pkt_debugfs_root = NULL; |
| 481 | return; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | static void pkt_debugfs_cleanup(void) |
| 486 | { |
| 487 | if (!pkt_debugfs_root) |
| 488 | return; |
| 489 | debugfs_remove(pkt_debugfs_root); |
| 490 | pkt_debugfs_root = NULL; |
| 491 | } |
| 492 | |
| 493 | /* ----------------------------------------------------------*/ |
| 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | static void pkt_bio_finished(struct pktcdvd_device *pd) |
| 497 | { |
| 498 | BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0); |
| 499 | if (atomic_dec_and_test(&pd->cdrw.pending_bios)) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 500 | VPRINTK(DRIVER_NAME": queue empty\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | atomic_set(&pd->iosched.attention, 1); |
| 502 | wake_up(&pd->wqueue); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static void pkt_bio_destructor(struct bio *bio) |
| 507 | { |
| 508 | kfree(bio->bi_io_vec); |
| 509 | kfree(bio); |
| 510 | } |
| 511 | |
| 512 | static struct bio *pkt_bio_alloc(int nr_iovecs) |
| 513 | { |
| 514 | struct bio_vec *bvl = NULL; |
| 515 | struct bio *bio; |
| 516 | |
| 517 | bio = kmalloc(sizeof(struct bio), GFP_KERNEL); |
| 518 | if (!bio) |
| 519 | goto no_bio; |
| 520 | bio_init(bio); |
| 521 | |
Peter Osterlund | 1107d2e | 2005-09-13 01:25:29 -0700 | [diff] [blame] | 522 | bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (!bvl) |
| 524 | goto no_bvl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | bio->bi_max_vecs = nr_iovecs; |
| 527 | bio->bi_io_vec = bvl; |
| 528 | bio->bi_destructor = pkt_bio_destructor; |
| 529 | |
| 530 | return bio; |
| 531 | |
| 532 | no_bvl: |
| 533 | kfree(bio); |
| 534 | no_bio: |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Allocate a packet_data struct |
| 540 | */ |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 541 | static struct packet_data *pkt_alloc_packet_data(int frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
| 543 | int i; |
| 544 | struct packet_data *pkt; |
| 545 | |
Peter Osterlund | 1107d2e | 2005-09-13 01:25:29 -0700 | [diff] [blame] | 546 | pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (!pkt) |
| 548 | goto no_pkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 550 | pkt->frames = frames; |
| 551 | pkt->w_bio = pkt_bio_alloc(frames); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (!pkt->w_bio) |
| 553 | goto no_bio; |
| 554 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 555 | for (i = 0; i < frames / FRAMES_PER_PAGE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO); |
| 557 | if (!pkt->pages[i]) |
| 558 | goto no_page; |
| 559 | } |
| 560 | |
| 561 | spin_lock_init(&pkt->lock); |
| 562 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 563 | for (i = 0; i < frames; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | struct bio *bio = pkt_bio_alloc(1); |
| 565 | if (!bio) |
| 566 | goto no_rd_bio; |
| 567 | pkt->r_bios[i] = bio; |
| 568 | } |
| 569 | |
| 570 | return pkt; |
| 571 | |
| 572 | no_rd_bio: |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 573 | for (i = 0; i < frames; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | struct bio *bio = pkt->r_bios[i]; |
| 575 | if (bio) |
| 576 | bio_put(bio); |
| 577 | } |
| 578 | |
| 579 | no_page: |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 580 | for (i = 0; i < frames / FRAMES_PER_PAGE; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (pkt->pages[i]) |
| 582 | __free_page(pkt->pages[i]); |
| 583 | bio_put(pkt->w_bio); |
| 584 | no_bio: |
| 585 | kfree(pkt); |
| 586 | no_pkt: |
| 587 | return NULL; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Free a packet_data struct |
| 592 | */ |
| 593 | static void pkt_free_packet_data(struct packet_data *pkt) |
| 594 | { |
| 595 | int i; |
| 596 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 597 | for (i = 0; i < pkt->frames; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | struct bio *bio = pkt->r_bios[i]; |
| 599 | if (bio) |
| 600 | bio_put(bio); |
| 601 | } |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 602 | for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | __free_page(pkt->pages[i]); |
| 604 | bio_put(pkt->w_bio); |
| 605 | kfree(pkt); |
| 606 | } |
| 607 | |
| 608 | static void pkt_shrink_pktlist(struct pktcdvd_device *pd) |
| 609 | { |
| 610 | struct packet_data *pkt, *next; |
| 611 | |
| 612 | BUG_ON(!list_empty(&pd->cdrw.pkt_active_list)); |
| 613 | |
| 614 | list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) { |
| 615 | pkt_free_packet_data(pkt); |
| 616 | } |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 617 | INIT_LIST_HEAD(&pd->cdrw.pkt_free_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets) |
| 621 | { |
| 622 | struct packet_data *pkt; |
| 623 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 624 | BUG_ON(!list_empty(&pd->cdrw.pkt_free_list)); |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | while (nr_packets > 0) { |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 627 | pkt = pkt_alloc_packet_data(pd->settings.size >> 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (!pkt) { |
| 629 | pkt_shrink_pktlist(pd); |
| 630 | return 0; |
| 631 | } |
| 632 | pkt->id = nr_packets; |
| 633 | pkt->pd = pd; |
| 634 | list_add(&pkt->list, &pd->cdrw.pkt_free_list); |
| 635 | nr_packets--; |
| 636 | } |
| 637 | return 1; |
| 638 | } |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node) |
| 641 | { |
| 642 | struct rb_node *n = rb_next(&node->rb_node); |
| 643 | if (!n) |
| 644 | return NULL; |
| 645 | return rb_entry(n, struct pkt_rb_node, rb_node); |
| 646 | } |
| 647 | |
Peter Osterlund | ac89396 | 2006-01-14 13:21:32 -0800 | [diff] [blame] | 648 | static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
| 650 | rb_erase(&node->rb_node, &pd->bio_queue); |
| 651 | mempool_free(node, pd->rb_pool); |
| 652 | pd->bio_queue_size--; |
| 653 | BUG_ON(pd->bio_queue_size < 0); |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Find the first node in the pd->bio_queue rb tree with a starting sector >= s. |
| 658 | */ |
| 659 | static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s) |
| 660 | { |
| 661 | struct rb_node *n = pd->bio_queue.rb_node; |
| 662 | struct rb_node *next; |
| 663 | struct pkt_rb_node *tmp; |
| 664 | |
| 665 | if (!n) { |
| 666 | BUG_ON(pd->bio_queue_size > 0); |
| 667 | return NULL; |
| 668 | } |
| 669 | |
| 670 | for (;;) { |
| 671 | tmp = rb_entry(n, struct pkt_rb_node, rb_node); |
| 672 | if (s <= tmp->bio->bi_sector) |
| 673 | next = n->rb_left; |
| 674 | else |
| 675 | next = n->rb_right; |
| 676 | if (!next) |
| 677 | break; |
| 678 | n = next; |
| 679 | } |
| 680 | |
| 681 | if (s > tmp->bio->bi_sector) { |
| 682 | tmp = pkt_rbtree_next(tmp); |
| 683 | if (!tmp) |
| 684 | return NULL; |
| 685 | } |
| 686 | BUG_ON(s > tmp->bio->bi_sector); |
| 687 | return tmp; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Insert a node into the pd->bio_queue rb tree. |
| 692 | */ |
| 693 | static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node) |
| 694 | { |
| 695 | struct rb_node **p = &pd->bio_queue.rb_node; |
| 696 | struct rb_node *parent = NULL; |
| 697 | sector_t s = node->bio->bi_sector; |
| 698 | struct pkt_rb_node *tmp; |
| 699 | |
| 700 | while (*p) { |
| 701 | parent = *p; |
| 702 | tmp = rb_entry(parent, struct pkt_rb_node, rb_node); |
| 703 | if (s < tmp->bio->bi_sector) |
| 704 | p = &(*p)->rb_left; |
| 705 | else |
| 706 | p = &(*p)->rb_right; |
| 707 | } |
| 708 | rb_link_node(&node->rb_node, parent, p); |
| 709 | rb_insert_color(&node->rb_node, &pd->bio_queue); |
| 710 | pd->bio_queue_size++; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Add a bio to a single linked list defined by its head and tail pointers. |
| 715 | */ |
Peter Osterlund | ac89396 | 2006-01-14 13:21:32 -0800 | [diff] [blame] | 716 | static void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
| 718 | bio->bi_next = NULL; |
| 719 | if (*list_tail) { |
| 720 | BUG_ON((*list_head) == NULL); |
| 721 | (*list_tail)->bi_next = bio; |
| 722 | (*list_tail) = bio; |
| 723 | } else { |
| 724 | BUG_ON((*list_head) != NULL); |
| 725 | (*list_head) = bio; |
| 726 | (*list_tail) = bio; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Remove and return the first bio from a single linked list defined by its |
| 732 | * head and tail pointers. |
| 733 | */ |
| 734 | static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail) |
| 735 | { |
| 736 | struct bio *bio; |
| 737 | |
| 738 | if (*list_head == NULL) |
| 739 | return NULL; |
| 740 | |
| 741 | bio = *list_head; |
| 742 | *list_head = bio->bi_next; |
| 743 | if (*list_head == NULL) |
| 744 | *list_tail = NULL; |
| 745 | |
| 746 | bio->bi_next = NULL; |
| 747 | return bio; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Send a packet_command to the underlying block device and |
| 752 | * wait for completion. |
| 753 | */ |
| 754 | static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc) |
| 755 | { |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 756 | request_queue_t *q = bdev_get_queue(pd->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | struct request *rq; |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 758 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 760 | rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? |
| 761 | WRITE : READ, __GFP_WAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 763 | if (cgc->buflen) { |
| 764 | if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT)) |
| 765 | goto out; |
| 766 | } |
| 767 | |
Gerhard Dirschl | 91e4ee3 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 768 | rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]); |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 769 | memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE); |
| 770 | if (sizeof(rq->cmd) > CDROM_PACKET_SIZE) |
| 771 | memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE); |
| 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | rq->timeout = 60*HZ; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 774 | rq->cmd_type = REQ_TYPE_BLOCK_PC; |
| 775 | rq->cmd_flags |= REQ_HARDBARRIER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | if (cgc->quiet) |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 777 | rq->cmd_flags |= REQ_QUIET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 779 | blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0); |
Andrew Morton | cbc31a4 | 2007-04-25 13:01:21 -0700 | [diff] [blame] | 780 | if (rq->errors) |
| 781 | ret = -EIO; |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 782 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | blk_put_request(rq); |
Christoph Hellwig | 406c9b6 | 2007-01-05 16:36:26 -0800 | [diff] [blame] | 784 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* |
| 788 | * A generic sense dump / resolve mechanism should be implemented across |
| 789 | * all ATAPI + SCSI devices. |
| 790 | */ |
| 791 | static void pkt_dump_sense(struct packet_command *cgc) |
| 792 | { |
| 793 | static char *info[9] = { "No sense", "Recovered error", "Not ready", |
| 794 | "Medium error", "Hardware error", "Illegal request", |
| 795 | "Unit attention", "Data protect", "Blank check" }; |
| 796 | int i; |
| 797 | struct request_sense *sense = cgc->sense; |
| 798 | |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 799 | printk(DRIVER_NAME":"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | for (i = 0; i < CDROM_PACKET_SIZE; i++) |
| 801 | printk(" %02x", cgc->cmd[i]); |
| 802 | printk(" - "); |
| 803 | |
| 804 | if (sense == NULL) { |
| 805 | printk("no sense\n"); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq); |
| 810 | |
| 811 | if (sense->sense_key > 8) { |
| 812 | printk(" (INVALID)\n"); |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | printk(" (%s)\n", info[sense->sense_key]); |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * flush the drive cache to media |
| 821 | */ |
| 822 | static int pkt_flush_cache(struct pktcdvd_device *pd) |
| 823 | { |
| 824 | struct packet_command cgc; |
| 825 | |
| 826 | init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); |
| 827 | cgc.cmd[0] = GPCMD_FLUSH_CACHE; |
| 828 | cgc.quiet = 1; |
| 829 | |
| 830 | /* |
| 831 | * the IMMED bit -- we default to not setting it, although that |
| 832 | * would allow a much faster close, this is safer |
| 833 | */ |
| 834 | #if 0 |
| 835 | cgc.cmd[1] = 1 << 1; |
| 836 | #endif |
| 837 | return pkt_generic_packet(pd, &cgc); |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * speed is given as the normal factor, e.g. 4 for 4x |
| 842 | */ |
| 843 | static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed) |
| 844 | { |
| 845 | struct packet_command cgc; |
| 846 | struct request_sense sense; |
| 847 | int ret; |
| 848 | |
| 849 | init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); |
| 850 | cgc.sense = &sense; |
| 851 | cgc.cmd[0] = GPCMD_SET_SPEED; |
| 852 | cgc.cmd[2] = (read_speed >> 8) & 0xff; |
| 853 | cgc.cmd[3] = read_speed & 0xff; |
| 854 | cgc.cmd[4] = (write_speed >> 8) & 0xff; |
| 855 | cgc.cmd[5] = write_speed & 0xff; |
| 856 | |
| 857 | if ((ret = pkt_generic_packet(pd, &cgc))) |
| 858 | pkt_dump_sense(&cgc); |
| 859 | |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Queue a bio for processing by the low-level CD device. Must be called |
| 865 | * from process context. |
| 866 | */ |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 867 | static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
| 869 | spin_lock(&pd->iosched.lock); |
| 870 | if (bio_data_dir(bio) == READ) { |
| 871 | pkt_add_list_last(bio, &pd->iosched.read_queue, |
| 872 | &pd->iosched.read_queue_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } else { |
| 874 | pkt_add_list_last(bio, &pd->iosched.write_queue, |
| 875 | &pd->iosched.write_queue_tail); |
| 876 | } |
| 877 | spin_unlock(&pd->iosched.lock); |
| 878 | |
| 879 | atomic_set(&pd->iosched.attention, 1); |
| 880 | wake_up(&pd->wqueue); |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Process the queued read/write requests. This function handles special |
| 885 | * requirements for CDRW drives: |
| 886 | * - A cache flush command must be inserted before a read request if the |
| 887 | * previous request was a write. |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 888 | * - Switching between reading and writing is slow, so don't do it more often |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | * than necessary. |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 890 | * - Optimize for throughput at the expense of latency. This means that streaming |
| 891 | * writes will never be interrupted by a read, but if the drive has to seek |
| 892 | * before the next write, switch to reading instead if there are any pending |
| 893 | * read requests. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | * - Set the read speed according to current usage pattern. When only reading |
| 895 | * from the device, it's best to use the highest possible read speed, but |
| 896 | * when switching often between reading and writing, it's better to have the |
| 897 | * same read and write speeds. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | */ |
| 899 | static void pkt_iosched_process_queue(struct pktcdvd_device *pd) |
| 900 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
| 902 | if (atomic_read(&pd->iosched.attention) == 0) |
| 903 | return; |
| 904 | atomic_set(&pd->iosched.attention, 0); |
| 905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | for (;;) { |
| 907 | struct bio *bio; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 908 | int reads_queued, writes_queued; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
| 910 | spin_lock(&pd->iosched.lock); |
| 911 | reads_queued = (pd->iosched.read_queue != NULL); |
| 912 | writes_queued = (pd->iosched.write_queue != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | spin_unlock(&pd->iosched.lock); |
| 914 | |
| 915 | if (!reads_queued && !writes_queued) |
| 916 | break; |
| 917 | |
| 918 | if (pd->iosched.writing) { |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 919 | int need_write_seek = 1; |
| 920 | spin_lock(&pd->iosched.lock); |
| 921 | bio = pd->iosched.write_queue; |
| 922 | spin_unlock(&pd->iosched.lock); |
| 923 | if (bio && (bio->bi_sector == pd->iosched.last_write)) |
| 924 | need_write_seek = 0; |
| 925 | if (need_write_seek && reads_queued) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | if (atomic_read(&pd->cdrw.pending_bios) > 0) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 927 | VPRINTK(DRIVER_NAME": write, waiting\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | pkt_flush_cache(pd); |
| 931 | pd->iosched.writing = 0; |
| 932 | } |
| 933 | } else { |
| 934 | if (!reads_queued && writes_queued) { |
| 935 | if (atomic_read(&pd->cdrw.pending_bios) > 0) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 936 | VPRINTK(DRIVER_NAME": read, waiting\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | break; |
| 938 | } |
| 939 | pd->iosched.writing = 1; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | spin_lock(&pd->iosched.lock); |
| 944 | if (pd->iosched.writing) { |
| 945 | bio = pkt_get_list_first(&pd->iosched.write_queue, |
| 946 | &pd->iosched.write_queue_tail); |
| 947 | } else { |
| 948 | bio = pkt_get_list_first(&pd->iosched.read_queue, |
| 949 | &pd->iosched.read_queue_tail); |
| 950 | } |
| 951 | spin_unlock(&pd->iosched.lock); |
| 952 | |
| 953 | if (!bio) |
| 954 | continue; |
| 955 | |
| 956 | if (bio_data_dir(bio) == READ) |
| 957 | pd->iosched.successive_reads += bio->bi_size >> 10; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 958 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | pd->iosched.successive_reads = 0; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 960 | pd->iosched.last_write = bio->bi_sector + bio_sectors(bio); |
| 961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) { |
| 963 | if (pd->read_speed == pd->write_speed) { |
| 964 | pd->read_speed = MAX_SPEED; |
| 965 | pkt_set_speed(pd, pd->write_speed, pd->read_speed); |
| 966 | } |
| 967 | } else { |
| 968 | if (pd->read_speed != pd->write_speed) { |
| 969 | pd->read_speed = pd->write_speed; |
| 970 | pkt_set_speed(pd, pd->write_speed, pd->read_speed); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | atomic_inc(&pd->cdrw.pending_bios); |
| 975 | generic_make_request(bio); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Special care is needed if the underlying block device has a small |
| 981 | * max_phys_segments value. |
| 982 | */ |
| 983 | static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q) |
| 984 | { |
| 985 | if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) { |
| 986 | /* |
| 987 | * The cdrom device can handle one segment/frame |
| 988 | */ |
| 989 | clear_bit(PACKET_MERGE_SEGS, &pd->flags); |
| 990 | return 0; |
| 991 | } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) { |
| 992 | /* |
| 993 | * We can handle this case at the expense of some extra memory |
| 994 | * copies during write operations |
| 995 | */ |
| 996 | set_bit(PACKET_MERGE_SEGS, &pd->flags); |
| 997 | return 0; |
| 998 | } else { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 999 | printk(DRIVER_NAME": cdrom max_phys_segments too small\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | return -EIO; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Copy CD_FRAMESIZE bytes from src_bio into a destination page |
| 1006 | */ |
| 1007 | static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs) |
| 1008 | { |
| 1009 | unsigned int copy_size = CD_FRAMESIZE; |
| 1010 | |
| 1011 | while (copy_size > 0) { |
| 1012 | struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg); |
| 1013 | void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) + |
| 1014 | src_bvl->bv_offset + offs; |
| 1015 | void *vto = page_address(dst_page) + dst_offs; |
| 1016 | int len = min_t(int, copy_size, src_bvl->bv_len - offs); |
| 1017 | |
| 1018 | BUG_ON(len < 0); |
| 1019 | memcpy(vto, vfrom, len); |
| 1020 | kunmap_atomic(vfrom, KM_USER0); |
| 1021 | |
| 1022 | seg++; |
| 1023 | offs = 0; |
| 1024 | dst_offs += len; |
| 1025 | copy_size -= len; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Copy all data for this packet to pkt->pages[], so that |
| 1031 | * a) The number of required segments for the write bio is minimized, which |
| 1032 | * is necessary for some scsi controllers. |
| 1033 | * b) The data can be used as cache to avoid read requests if we receive a |
| 1034 | * new write request for the same zone. |
| 1035 | */ |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1036 | static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | { |
| 1038 | int f, p, offs; |
| 1039 | |
| 1040 | /* Copy all data to pkt->pages[] */ |
| 1041 | p = 0; |
| 1042 | offs = 0; |
| 1043 | for (f = 0; f < pkt->frames; f++) { |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1044 | if (bvec[f].bv_page != pkt->pages[p]) { |
| 1045 | void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | void *vto = page_address(pkt->pages[p]) + offs; |
| 1047 | memcpy(vto, vfrom, CD_FRAMESIZE); |
| 1048 | kunmap_atomic(vfrom, KM_USER0); |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1049 | bvec[f].bv_page = pkt->pages[p]; |
| 1050 | bvec[f].bv_offset = offs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } else { |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1052 | BUG_ON(bvec[f].bv_offset != offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | offs += CD_FRAMESIZE; |
| 1055 | if (offs >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | offs = 0; |
| 1057 | p++; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err) |
| 1063 | { |
| 1064 | struct packet_data *pkt = bio->bi_private; |
| 1065 | struct pktcdvd_device *pd = pkt->pd; |
| 1066 | BUG_ON(!pd); |
| 1067 | |
| 1068 | if (bio->bi_size) |
| 1069 | return 1; |
| 1070 | |
| 1071 | VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio, |
| 1072 | (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err); |
| 1073 | |
| 1074 | if (err) |
| 1075 | atomic_inc(&pkt->io_errors); |
| 1076 | if (atomic_dec_and_test(&pkt->io_wait)) { |
| 1077 | atomic_inc(&pkt->run_sm); |
| 1078 | wake_up(&pd->wqueue); |
| 1079 | } |
| 1080 | pkt_bio_finished(pd); |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err) |
| 1086 | { |
| 1087 | struct packet_data *pkt = bio->bi_private; |
| 1088 | struct pktcdvd_device *pd = pkt->pd; |
| 1089 | BUG_ON(!pd); |
| 1090 | |
| 1091 | if (bio->bi_size) |
| 1092 | return 1; |
| 1093 | |
| 1094 | VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err); |
| 1095 | |
| 1096 | pd->stats.pkt_ended++; |
| 1097 | |
| 1098 | pkt_bio_finished(pd); |
| 1099 | atomic_dec(&pkt->io_wait); |
| 1100 | atomic_inc(&pkt->run_sm); |
| 1101 | wake_up(&pd->wqueue); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * Schedule reads for the holes in a packet |
| 1107 | */ |
| 1108 | static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt) |
| 1109 | { |
| 1110 | int frames_read = 0; |
| 1111 | struct bio *bio; |
| 1112 | int f; |
| 1113 | char written[PACKET_MAX_SIZE]; |
| 1114 | |
| 1115 | BUG_ON(!pkt->orig_bios); |
| 1116 | |
| 1117 | atomic_set(&pkt->io_wait, 0); |
| 1118 | atomic_set(&pkt->io_errors, 0); |
| 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | /* |
| 1121 | * Figure out which frames we need to read before we can write. |
| 1122 | */ |
| 1123 | memset(written, 0, sizeof(written)); |
| 1124 | spin_lock(&pkt->lock); |
| 1125 | for (bio = pkt->orig_bios; bio; bio = bio->bi_next) { |
| 1126 | int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9); |
| 1127 | int num_frames = bio->bi_size / CD_FRAMESIZE; |
Peter Osterlund | 06e7ab5 | 2005-09-13 01:25:28 -0700 | [diff] [blame] | 1128 | pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | BUG_ON(first_frame < 0); |
| 1130 | BUG_ON(first_frame + num_frames > pkt->frames); |
| 1131 | for (f = first_frame; f < first_frame + num_frames; f++) |
| 1132 | written[f] = 1; |
| 1133 | } |
| 1134 | spin_unlock(&pkt->lock); |
| 1135 | |
Peter Osterlund | 06e7ab5 | 2005-09-13 01:25:28 -0700 | [diff] [blame] | 1136 | if (pkt->cache_valid) { |
| 1137 | VPRINTK("pkt_gather_data: zone %llx cached\n", |
| 1138 | (unsigned long long)pkt->sector); |
| 1139 | goto out_account; |
| 1140 | } |
| 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | /* |
| 1143 | * Schedule reads for missing parts of the packet. |
| 1144 | */ |
| 1145 | for (f = 0; f < pkt->frames; f++) { |
| 1146 | int p, offset; |
| 1147 | if (written[f]) |
| 1148 | continue; |
| 1149 | bio = pkt->r_bios[f]; |
| 1150 | bio_init(bio); |
| 1151 | bio->bi_max_vecs = 1; |
| 1152 | bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9); |
| 1153 | bio->bi_bdev = pd->bdev; |
| 1154 | bio->bi_end_io = pkt_end_io_read; |
| 1155 | bio->bi_private = pkt; |
| 1156 | |
| 1157 | p = (f * CD_FRAMESIZE) / PAGE_SIZE; |
| 1158 | offset = (f * CD_FRAMESIZE) % PAGE_SIZE; |
| 1159 | VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n", |
| 1160 | f, pkt->pages[p], offset); |
| 1161 | if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset)) |
| 1162 | BUG(); |
| 1163 | |
| 1164 | atomic_inc(&pkt->io_wait); |
| 1165 | bio->bi_rw = READ; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 1166 | pkt_queue_bio(pd, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | frames_read++; |
| 1168 | } |
| 1169 | |
| 1170 | out_account: |
| 1171 | VPRINTK("pkt_gather_data: need %d frames for zone %llx\n", |
| 1172 | frames_read, (unsigned long long)pkt->sector); |
| 1173 | pd->stats.pkt_started++; |
| 1174 | pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Find a packet matching zone, or the least recently used packet if |
| 1179 | * there is no match. |
| 1180 | */ |
| 1181 | static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone) |
| 1182 | { |
| 1183 | struct packet_data *pkt; |
| 1184 | |
| 1185 | list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) { |
| 1186 | if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) { |
| 1187 | list_del_init(&pkt->list); |
| 1188 | if (pkt->sector != zone) |
| 1189 | pkt->cache_valid = 0; |
Peter Osterlund | 610827d | 2005-09-13 01:25:29 -0700 | [diff] [blame] | 1190 | return pkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
| 1192 | } |
Peter Osterlund | 610827d | 2005-09-13 01:25:29 -0700 | [diff] [blame] | 1193 | BUG(); |
| 1194 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt) |
| 1198 | { |
| 1199 | if (pkt->cache_valid) { |
| 1200 | list_add(&pkt->list, &pd->cdrw.pkt_free_list); |
| 1201 | } else { |
| 1202 | list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * recover a failed write, query for relocation if possible |
| 1208 | * |
| 1209 | * returns 1 if recovery is possible, or 0 if not |
| 1210 | * |
| 1211 | */ |
| 1212 | static int pkt_start_recovery(struct packet_data *pkt) |
| 1213 | { |
| 1214 | /* |
| 1215 | * FIXME. We need help from the file system to implement |
| 1216 | * recovery handling. |
| 1217 | */ |
| 1218 | return 0; |
| 1219 | #if 0 |
| 1220 | struct request *rq = pkt->rq; |
| 1221 | struct pktcdvd_device *pd = rq->rq_disk->private_data; |
| 1222 | struct block_device *pkt_bdev; |
| 1223 | struct super_block *sb = NULL; |
| 1224 | unsigned long old_block, new_block; |
| 1225 | sector_t new_sector; |
| 1226 | |
| 1227 | pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev)); |
| 1228 | if (pkt_bdev) { |
| 1229 | sb = get_super(pkt_bdev); |
| 1230 | bdput(pkt_bdev); |
| 1231 | } |
| 1232 | |
| 1233 | if (!sb) |
| 1234 | return 0; |
| 1235 | |
| 1236 | if (!sb->s_op || !sb->s_op->relocate_blocks) |
| 1237 | goto out; |
| 1238 | |
| 1239 | old_block = pkt->sector / (CD_FRAMESIZE >> 9); |
| 1240 | if (sb->s_op->relocate_blocks(sb, old_block, &new_block)) |
| 1241 | goto out; |
| 1242 | |
| 1243 | new_sector = new_block * (CD_FRAMESIZE >> 9); |
| 1244 | pkt->sector = new_sector; |
| 1245 | |
| 1246 | pkt->bio->bi_sector = new_sector; |
| 1247 | pkt->bio->bi_next = NULL; |
| 1248 | pkt->bio->bi_flags = 1 << BIO_UPTODATE; |
| 1249 | pkt->bio->bi_idx = 0; |
| 1250 | |
| 1251 | BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW)); |
| 1252 | BUG_ON(pkt->bio->bi_vcnt != pkt->frames); |
| 1253 | BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE); |
| 1254 | BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write); |
| 1255 | BUG_ON(pkt->bio->bi_private != pkt); |
| 1256 | |
| 1257 | drop_super(sb); |
| 1258 | return 1; |
| 1259 | |
| 1260 | out: |
| 1261 | drop_super(sb); |
| 1262 | return 0; |
| 1263 | #endif |
| 1264 | } |
| 1265 | |
| 1266 | static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state) |
| 1267 | { |
| 1268 | #if PACKET_DEBUG > 1 |
| 1269 | static const char *state_name[] = { |
| 1270 | "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED" |
| 1271 | }; |
| 1272 | enum packet_data_state old_state = pkt->state; |
| 1273 | VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector, |
| 1274 | state_name[old_state], state_name[state]); |
| 1275 | #endif |
| 1276 | pkt->state = state; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * Scan the work queue to see if we can start a new packet. |
| 1281 | * returns non-zero if any work was done. |
| 1282 | */ |
| 1283 | static int pkt_handle_queue(struct pktcdvd_device *pd) |
| 1284 | { |
| 1285 | struct packet_data *pkt, *p; |
| 1286 | struct bio *bio = NULL; |
| 1287 | sector_t zone = 0; /* Suppress gcc warning */ |
| 1288 | struct pkt_rb_node *node, *first_node; |
| 1289 | struct rb_node *n; |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 1290 | int wakeup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| 1292 | VPRINTK("handle_queue\n"); |
| 1293 | |
| 1294 | atomic_set(&pd->scan_queue, 0); |
| 1295 | |
| 1296 | if (list_empty(&pd->cdrw.pkt_free_list)) { |
| 1297 | VPRINTK("handle_queue: no pkt\n"); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * Try to find a zone we are not already working on. |
| 1303 | */ |
| 1304 | spin_lock(&pd->lock); |
| 1305 | first_node = pkt_rbtree_find(pd, pd->current_sector); |
| 1306 | if (!first_node) { |
| 1307 | n = rb_first(&pd->bio_queue); |
| 1308 | if (n) |
| 1309 | first_node = rb_entry(n, struct pkt_rb_node, rb_node); |
| 1310 | } |
| 1311 | node = first_node; |
| 1312 | while (node) { |
| 1313 | bio = node->bio; |
| 1314 | zone = ZONE(bio->bi_sector, pd); |
| 1315 | list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) { |
Peter Osterlund | 7baeb6a | 2005-05-16 21:53:42 -0700 | [diff] [blame] | 1316 | if (p->sector == zone) { |
| 1317 | bio = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | goto try_next_bio; |
Peter Osterlund | 7baeb6a | 2005-05-16 21:53:42 -0700 | [diff] [blame] | 1319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 1321 | break; |
| 1322 | try_next_bio: |
| 1323 | node = pkt_rbtree_next(node); |
| 1324 | if (!node) { |
| 1325 | n = rb_first(&pd->bio_queue); |
| 1326 | if (n) |
| 1327 | node = rb_entry(n, struct pkt_rb_node, rb_node); |
| 1328 | } |
| 1329 | if (node == first_node) |
| 1330 | node = NULL; |
| 1331 | } |
| 1332 | spin_unlock(&pd->lock); |
| 1333 | if (!bio) { |
| 1334 | VPRINTK("handle_queue: no bio\n"); |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | pkt = pkt_get_packet_data(pd, zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
| 1340 | pd->current_sector = zone + pd->settings.size; |
| 1341 | pkt->sector = zone; |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 1342 | BUG_ON(pkt->frames != pd->settings.size >> 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | pkt->write_size = 0; |
| 1344 | |
| 1345 | /* |
| 1346 | * Scan work queue for bios in the same zone and link them |
| 1347 | * to this packet. |
| 1348 | */ |
| 1349 | spin_lock(&pd->lock); |
| 1350 | VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone); |
| 1351 | while ((node = pkt_rbtree_find(pd, zone)) != NULL) { |
| 1352 | bio = node->bio; |
| 1353 | VPRINTK("pkt_handle_queue: found zone=%llx\n", |
| 1354 | (unsigned long long)ZONE(bio->bi_sector, pd)); |
| 1355 | if (ZONE(bio->bi_sector, pd) != zone) |
| 1356 | break; |
| 1357 | pkt_rbtree_erase(pd, node); |
| 1358 | spin_lock(&pkt->lock); |
| 1359 | pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail); |
| 1360 | pkt->write_size += bio->bi_size / CD_FRAMESIZE; |
| 1361 | spin_unlock(&pkt->lock); |
| 1362 | } |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 1363 | /* check write congestion marks, and if bio_queue_size is |
| 1364 | below, wake up any waiters */ |
| 1365 | wakeup = (pd->write_congestion_on > 0 |
| 1366 | && pd->bio_queue_size <= pd->write_congestion_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | spin_unlock(&pd->lock); |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 1368 | if (wakeup) |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 1369 | clear_bdi_congested(&pd->disk->queue->backing_dev_info, WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
| 1371 | pkt->sleep_time = max(PACKET_WAIT_TIME, 1); |
| 1372 | pkt_set_state(pkt, PACKET_WAITING_STATE); |
| 1373 | atomic_set(&pkt->run_sm, 1); |
| 1374 | |
| 1375 | spin_lock(&pd->cdrw.active_list_lock); |
| 1376 | list_add(&pkt->list, &pd->cdrw.pkt_active_list); |
| 1377 | spin_unlock(&pd->cdrw.active_list_lock); |
| 1378 | |
| 1379 | return 1; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
| 1383 | * Assemble a bio to write one packet and queue the bio for processing |
| 1384 | * by the underlying block device. |
| 1385 | */ |
| 1386 | static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt) |
| 1387 | { |
| 1388 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | int f; |
| 1390 | int frames_write; |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1391 | struct bio_vec *bvec = pkt->w_bio->bi_io_vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
| 1393 | for (f = 0; f < pkt->frames; f++) { |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1394 | bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE]; |
| 1395 | bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | /* |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1399 | * Fill-in bvec with data from orig_bios. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | */ |
| 1401 | frames_write = 0; |
| 1402 | spin_lock(&pkt->lock); |
| 1403 | for (bio = pkt->orig_bios; bio; bio = bio->bi_next) { |
| 1404 | int segment = bio->bi_idx; |
| 1405 | int src_offs = 0; |
| 1406 | int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9); |
| 1407 | int num_frames = bio->bi_size / CD_FRAMESIZE; |
| 1408 | BUG_ON(first_frame < 0); |
| 1409 | BUG_ON(first_frame + num_frames > pkt->frames); |
| 1410 | for (f = first_frame; f < first_frame + num_frames; f++) { |
| 1411 | struct bio_vec *src_bvl = bio_iovec_idx(bio, segment); |
| 1412 | |
| 1413 | while (src_offs >= src_bvl->bv_len) { |
| 1414 | src_offs -= src_bvl->bv_len; |
| 1415 | segment++; |
| 1416 | BUG_ON(segment >= bio->bi_vcnt); |
| 1417 | src_bvl = bio_iovec_idx(bio, segment); |
| 1418 | } |
| 1419 | |
| 1420 | if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) { |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1421 | bvec[f].bv_page = src_bvl->bv_page; |
| 1422 | bvec[f].bv_offset = src_bvl->bv_offset + src_offs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } else { |
| 1424 | pkt_copy_bio_data(bio, segment, src_offs, |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1425 | bvec[f].bv_page, bvec[f].bv_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } |
| 1427 | src_offs += CD_FRAMESIZE; |
| 1428 | frames_write++; |
| 1429 | } |
| 1430 | } |
| 1431 | pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE); |
| 1432 | spin_unlock(&pkt->lock); |
| 1433 | |
| 1434 | VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n", |
| 1435 | frames_write, (unsigned long long)pkt->sector); |
| 1436 | BUG_ON(frames_write != pkt->write_size); |
| 1437 | |
| 1438 | if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) { |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1439 | pkt_make_local_copy(pkt, bvec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | pkt->cache_valid = 1; |
| 1441 | } else { |
| 1442 | pkt->cache_valid = 0; |
| 1443 | } |
| 1444 | |
| 1445 | /* Start the write request */ |
| 1446 | bio_init(pkt->w_bio); |
| 1447 | pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE; |
| 1448 | pkt->w_bio->bi_sector = pkt->sector; |
| 1449 | pkt->w_bio->bi_bdev = pd->bdev; |
| 1450 | pkt->w_bio->bi_end_io = pkt_end_io_packet_write; |
| 1451 | pkt->w_bio->bi_private = pkt; |
Peter Osterlund | 7277232 | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 1452 | for (f = 0; f < pkt->frames; f++) |
| 1453 | if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset)) |
| 1454 | BUG(); |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1455 | VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
| 1457 | atomic_set(&pkt->io_wait, 1); |
| 1458 | pkt->w_bio->bi_rw = WRITE; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 1459 | pkt_queue_bio(pd, pkt->w_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | static void pkt_finish_packet(struct packet_data *pkt, int uptodate) |
| 1463 | { |
| 1464 | struct bio *bio, *next; |
| 1465 | |
| 1466 | if (!uptodate) |
| 1467 | pkt->cache_valid = 0; |
| 1468 | |
| 1469 | /* Finish all bios corresponding to this packet */ |
| 1470 | bio = pkt->orig_bios; |
| 1471 | while (bio) { |
| 1472 | next = bio->bi_next; |
| 1473 | bio->bi_next = NULL; |
| 1474 | bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO); |
| 1475 | bio = next; |
| 1476 | } |
| 1477 | pkt->orig_bios = pkt->orig_bios_tail = NULL; |
| 1478 | } |
| 1479 | |
| 1480 | static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt) |
| 1481 | { |
| 1482 | int uptodate; |
| 1483 | |
| 1484 | VPRINTK("run_state_machine: pkt %d\n", pkt->id); |
| 1485 | |
| 1486 | for (;;) { |
| 1487 | switch (pkt->state) { |
| 1488 | case PACKET_WAITING_STATE: |
| 1489 | if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0)) |
| 1490 | return; |
| 1491 | |
| 1492 | pkt->sleep_time = 0; |
| 1493 | pkt_gather_data(pd, pkt); |
| 1494 | pkt_set_state(pkt, PACKET_READ_WAIT_STATE); |
| 1495 | break; |
| 1496 | |
| 1497 | case PACKET_READ_WAIT_STATE: |
| 1498 | if (atomic_read(&pkt->io_wait) > 0) |
| 1499 | return; |
| 1500 | |
| 1501 | if (atomic_read(&pkt->io_errors) > 0) { |
| 1502 | pkt_set_state(pkt, PACKET_RECOVERY_STATE); |
| 1503 | } else { |
| 1504 | pkt_start_write(pd, pkt); |
| 1505 | } |
| 1506 | break; |
| 1507 | |
| 1508 | case PACKET_WRITE_WAIT_STATE: |
| 1509 | if (atomic_read(&pkt->io_wait) > 0) |
| 1510 | return; |
| 1511 | |
| 1512 | if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) { |
| 1513 | pkt_set_state(pkt, PACKET_FINISHED_STATE); |
| 1514 | } else { |
| 1515 | pkt_set_state(pkt, PACKET_RECOVERY_STATE); |
| 1516 | } |
| 1517 | break; |
| 1518 | |
| 1519 | case PACKET_RECOVERY_STATE: |
| 1520 | if (pkt_start_recovery(pkt)) { |
| 1521 | pkt_start_write(pd, pkt); |
| 1522 | } else { |
| 1523 | VPRINTK("No recovery possible\n"); |
| 1524 | pkt_set_state(pkt, PACKET_FINISHED_STATE); |
| 1525 | } |
| 1526 | break; |
| 1527 | |
| 1528 | case PACKET_FINISHED_STATE: |
| 1529 | uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags); |
| 1530 | pkt_finish_packet(pkt, uptodate); |
| 1531 | return; |
| 1532 | |
| 1533 | default: |
| 1534 | BUG(); |
| 1535 | break; |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | static void pkt_handle_packets(struct pktcdvd_device *pd) |
| 1541 | { |
| 1542 | struct packet_data *pkt, *next; |
| 1543 | |
| 1544 | VPRINTK("pkt_handle_packets\n"); |
| 1545 | |
| 1546 | /* |
| 1547 | * Run state machine for active packets |
| 1548 | */ |
| 1549 | list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { |
| 1550 | if (atomic_read(&pkt->run_sm) > 0) { |
| 1551 | atomic_set(&pkt->run_sm, 0); |
| 1552 | pkt_run_state_machine(pd, pkt); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * Move no longer active packets to the free list |
| 1558 | */ |
| 1559 | spin_lock(&pd->cdrw.active_list_lock); |
| 1560 | list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) { |
| 1561 | if (pkt->state == PACKET_FINISHED_STATE) { |
| 1562 | list_del(&pkt->list); |
| 1563 | pkt_put_packet_data(pd, pkt); |
| 1564 | pkt_set_state(pkt, PACKET_IDLE_STATE); |
| 1565 | atomic_set(&pd->scan_queue, 1); |
| 1566 | } |
| 1567 | } |
| 1568 | spin_unlock(&pd->cdrw.active_list_lock); |
| 1569 | } |
| 1570 | |
| 1571 | static void pkt_count_states(struct pktcdvd_device *pd, int *states) |
| 1572 | { |
| 1573 | struct packet_data *pkt; |
| 1574 | int i; |
| 1575 | |
Peter Osterlund | ae7642b | 2005-11-13 16:06:36 -0800 | [diff] [blame] | 1576 | for (i = 0; i < PACKET_NUM_STATES; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | states[i] = 0; |
| 1578 | |
| 1579 | spin_lock(&pd->cdrw.active_list_lock); |
| 1580 | list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { |
| 1581 | states[pkt->state]++; |
| 1582 | } |
| 1583 | spin_unlock(&pd->cdrw.active_list_lock); |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * kcdrwd is woken up when writes have been queued for one of our |
| 1588 | * registered devices |
| 1589 | */ |
| 1590 | static int kcdrwd(void *foobar) |
| 1591 | { |
| 1592 | struct pktcdvd_device *pd = foobar; |
| 1593 | struct packet_data *pkt; |
| 1594 | long min_sleep_time, residue; |
| 1595 | |
| 1596 | set_user_nice(current, -20); |
| 1597 | |
| 1598 | for (;;) { |
| 1599 | DECLARE_WAITQUEUE(wait, current); |
| 1600 | |
| 1601 | /* |
| 1602 | * Wait until there is something to do |
| 1603 | */ |
| 1604 | add_wait_queue(&pd->wqueue, &wait); |
| 1605 | for (;;) { |
| 1606 | set_current_state(TASK_INTERRUPTIBLE); |
| 1607 | |
| 1608 | /* Check if we need to run pkt_handle_queue */ |
| 1609 | if (atomic_read(&pd->scan_queue) > 0) |
| 1610 | goto work_to_do; |
| 1611 | |
| 1612 | /* Check if we need to run the state machine for some packet */ |
| 1613 | list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { |
| 1614 | if (atomic_read(&pkt->run_sm) > 0) |
| 1615 | goto work_to_do; |
| 1616 | } |
| 1617 | |
| 1618 | /* Check if we need to process the iosched queues */ |
| 1619 | if (atomic_read(&pd->iosched.attention) != 0) |
| 1620 | goto work_to_do; |
| 1621 | |
| 1622 | /* Otherwise, go to sleep */ |
| 1623 | if (PACKET_DEBUG > 1) { |
| 1624 | int states[PACKET_NUM_STATES]; |
| 1625 | pkt_count_states(pd, states); |
| 1626 | VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n", |
| 1627 | states[0], states[1], states[2], states[3], |
| 1628 | states[4], states[5]); |
| 1629 | } |
| 1630 | |
| 1631 | min_sleep_time = MAX_SCHEDULE_TIMEOUT; |
| 1632 | list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { |
| 1633 | if (pkt->sleep_time && pkt->sleep_time < min_sleep_time) |
| 1634 | min_sleep_time = pkt->sleep_time; |
| 1635 | } |
| 1636 | |
| 1637 | generic_unplug_device(bdev_get_queue(pd->bdev)); |
| 1638 | |
| 1639 | VPRINTK("kcdrwd: sleeping\n"); |
| 1640 | residue = schedule_timeout(min_sleep_time); |
| 1641 | VPRINTK("kcdrwd: wake up\n"); |
| 1642 | |
| 1643 | /* make swsusp happy with our thread */ |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 1644 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
| 1646 | list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { |
| 1647 | if (!pkt->sleep_time) |
| 1648 | continue; |
| 1649 | pkt->sleep_time -= min_sleep_time - residue; |
| 1650 | if (pkt->sleep_time <= 0) { |
| 1651 | pkt->sleep_time = 0; |
| 1652 | atomic_inc(&pkt->run_sm); |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | if (signal_pending(current)) { |
| 1657 | flush_signals(current); |
| 1658 | } |
| 1659 | if (kthread_should_stop()) |
| 1660 | break; |
| 1661 | } |
| 1662 | work_to_do: |
| 1663 | set_current_state(TASK_RUNNING); |
| 1664 | remove_wait_queue(&pd->wqueue, &wait); |
| 1665 | |
| 1666 | if (kthread_should_stop()) |
| 1667 | break; |
| 1668 | |
| 1669 | /* |
| 1670 | * if pkt_handle_queue returns true, we can queue |
| 1671 | * another request. |
| 1672 | */ |
| 1673 | while (pkt_handle_queue(pd)) |
| 1674 | ; |
| 1675 | |
| 1676 | /* |
| 1677 | * Handle packet state machine |
| 1678 | */ |
| 1679 | pkt_handle_packets(pd); |
| 1680 | |
| 1681 | /* |
| 1682 | * Handle iosched queues |
| 1683 | */ |
| 1684 | pkt_iosched_process_queue(pd); |
| 1685 | } |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static void pkt_print_settings(struct pktcdvd_device *pd) |
| 1691 | { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1692 | printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | printk("%u blocks, ", pd->settings.size >> 2); |
| 1694 | printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2'); |
| 1695 | } |
| 1696 | |
| 1697 | static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control) |
| 1698 | { |
| 1699 | memset(cgc->cmd, 0, sizeof(cgc->cmd)); |
| 1700 | |
| 1701 | cgc->cmd[0] = GPCMD_MODE_SENSE_10; |
| 1702 | cgc->cmd[2] = page_code | (page_control << 6); |
| 1703 | cgc->cmd[7] = cgc->buflen >> 8; |
| 1704 | cgc->cmd[8] = cgc->buflen & 0xff; |
| 1705 | cgc->data_direction = CGC_DATA_READ; |
| 1706 | return pkt_generic_packet(pd, cgc); |
| 1707 | } |
| 1708 | |
| 1709 | static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc) |
| 1710 | { |
| 1711 | memset(cgc->cmd, 0, sizeof(cgc->cmd)); |
| 1712 | memset(cgc->buffer, 0, 2); |
| 1713 | cgc->cmd[0] = GPCMD_MODE_SELECT_10; |
| 1714 | cgc->cmd[1] = 0x10; /* PF */ |
| 1715 | cgc->cmd[7] = cgc->buflen >> 8; |
| 1716 | cgc->cmd[8] = cgc->buflen & 0xff; |
| 1717 | cgc->data_direction = CGC_DATA_WRITE; |
| 1718 | return pkt_generic_packet(pd, cgc); |
| 1719 | } |
| 1720 | |
| 1721 | static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di) |
| 1722 | { |
| 1723 | struct packet_command cgc; |
| 1724 | int ret; |
| 1725 | |
| 1726 | /* set up command and get the disc info */ |
| 1727 | init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ); |
| 1728 | cgc.cmd[0] = GPCMD_READ_DISC_INFO; |
| 1729 | cgc.cmd[8] = cgc.buflen = 2; |
| 1730 | cgc.quiet = 1; |
| 1731 | |
| 1732 | if ((ret = pkt_generic_packet(pd, &cgc))) |
| 1733 | return ret; |
| 1734 | |
| 1735 | /* not all drives have the same disc_info length, so requeue |
| 1736 | * packet with the length the drive tells us it can supply |
| 1737 | */ |
| 1738 | cgc.buflen = be16_to_cpu(di->disc_information_length) + |
| 1739 | sizeof(di->disc_information_length); |
| 1740 | |
| 1741 | if (cgc.buflen > sizeof(disc_information)) |
| 1742 | cgc.buflen = sizeof(disc_information); |
| 1743 | |
| 1744 | cgc.cmd[8] = cgc.buflen; |
| 1745 | return pkt_generic_packet(pd, &cgc); |
| 1746 | } |
| 1747 | |
| 1748 | static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti) |
| 1749 | { |
| 1750 | struct packet_command cgc; |
| 1751 | int ret; |
| 1752 | |
| 1753 | init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ); |
| 1754 | cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO; |
| 1755 | cgc.cmd[1] = type & 3; |
| 1756 | cgc.cmd[4] = (track & 0xff00) >> 8; |
| 1757 | cgc.cmd[5] = track & 0xff; |
| 1758 | cgc.cmd[8] = 8; |
| 1759 | cgc.quiet = 1; |
| 1760 | |
| 1761 | if ((ret = pkt_generic_packet(pd, &cgc))) |
| 1762 | return ret; |
| 1763 | |
| 1764 | cgc.buflen = be16_to_cpu(ti->track_information_length) + |
| 1765 | sizeof(ti->track_information_length); |
| 1766 | |
| 1767 | if (cgc.buflen > sizeof(track_information)) |
| 1768 | cgc.buflen = sizeof(track_information); |
| 1769 | |
| 1770 | cgc.cmd[8] = cgc.buflen; |
| 1771 | return pkt_generic_packet(pd, &cgc); |
| 1772 | } |
| 1773 | |
| 1774 | static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written) |
| 1775 | { |
| 1776 | disc_information di; |
| 1777 | track_information ti; |
| 1778 | __u32 last_track; |
| 1779 | int ret = -1; |
| 1780 | |
| 1781 | if ((ret = pkt_get_disc_info(pd, &di))) |
| 1782 | return ret; |
| 1783 | |
| 1784 | last_track = (di.last_track_msb << 8) | di.last_track_lsb; |
| 1785 | if ((ret = pkt_get_track_info(pd, last_track, 1, &ti))) |
| 1786 | return ret; |
| 1787 | |
| 1788 | /* if this track is blank, try the previous. */ |
| 1789 | if (ti.blank) { |
| 1790 | last_track--; |
| 1791 | if ((ret = pkt_get_track_info(pd, last_track, 1, &ti))) |
| 1792 | return ret; |
| 1793 | } |
| 1794 | |
| 1795 | /* if last recorded field is valid, return it. */ |
| 1796 | if (ti.lra_v) { |
| 1797 | *last_written = be32_to_cpu(ti.last_rec_address); |
| 1798 | } else { |
| 1799 | /* make it up instead */ |
| 1800 | *last_written = be32_to_cpu(ti.track_start) + |
| 1801 | be32_to_cpu(ti.track_size); |
| 1802 | if (ti.free_blocks) |
| 1803 | *last_written -= (be32_to_cpu(ti.free_blocks) + 7); |
| 1804 | } |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | * write mode select package based on pd->settings |
| 1810 | */ |
| 1811 | static int pkt_set_write_settings(struct pktcdvd_device *pd) |
| 1812 | { |
| 1813 | struct packet_command cgc; |
| 1814 | struct request_sense sense; |
| 1815 | write_param_page *wp; |
| 1816 | char buffer[128]; |
| 1817 | int ret, size; |
| 1818 | |
| 1819 | /* doesn't apply to DVD+RW or DVD-RAM */ |
| 1820 | if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12)) |
| 1821 | return 0; |
| 1822 | |
| 1823 | memset(buffer, 0, sizeof(buffer)); |
| 1824 | init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ); |
| 1825 | cgc.sense = &sense; |
| 1826 | if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) { |
| 1827 | pkt_dump_sense(&cgc); |
| 1828 | return ret; |
| 1829 | } |
| 1830 | |
| 1831 | size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff)); |
| 1832 | pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff); |
| 1833 | if (size > sizeof(buffer)) |
| 1834 | size = sizeof(buffer); |
| 1835 | |
| 1836 | /* |
| 1837 | * now get it all |
| 1838 | */ |
| 1839 | init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ); |
| 1840 | cgc.sense = &sense; |
| 1841 | if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) { |
| 1842 | pkt_dump_sense(&cgc); |
| 1843 | return ret; |
| 1844 | } |
| 1845 | |
| 1846 | /* |
| 1847 | * write page is offset header + block descriptor length |
| 1848 | */ |
| 1849 | wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset]; |
| 1850 | |
| 1851 | wp->fp = pd->settings.fp; |
| 1852 | wp->track_mode = pd->settings.track_mode; |
| 1853 | wp->write_type = pd->settings.write_type; |
| 1854 | wp->data_block_type = pd->settings.block_mode; |
| 1855 | |
| 1856 | wp->multi_session = 0; |
| 1857 | |
| 1858 | #ifdef PACKET_USE_LS |
| 1859 | wp->link_size = 7; |
| 1860 | wp->ls_v = 1; |
| 1861 | #endif |
| 1862 | |
| 1863 | if (wp->data_block_type == PACKET_BLOCK_MODE1) { |
| 1864 | wp->session_format = 0; |
| 1865 | wp->subhdr2 = 0x20; |
| 1866 | } else if (wp->data_block_type == PACKET_BLOCK_MODE2) { |
| 1867 | wp->session_format = 0x20; |
| 1868 | wp->subhdr2 = 8; |
| 1869 | #if 0 |
| 1870 | wp->mcn[0] = 0x80; |
| 1871 | memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1); |
| 1872 | #endif |
| 1873 | } else { |
| 1874 | /* |
| 1875 | * paranoia |
| 1876 | */ |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1877 | printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | return 1; |
| 1879 | } |
| 1880 | wp->packet_size = cpu_to_be32(pd->settings.size >> 2); |
| 1881 | |
| 1882 | cgc.buflen = cgc.cmd[8] = size; |
| 1883 | if ((ret = pkt_mode_select(pd, &cgc))) { |
| 1884 | pkt_dump_sense(&cgc); |
| 1885 | return ret; |
| 1886 | } |
| 1887 | |
| 1888 | pkt_print_settings(pd); |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
| 1892 | /* |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1893 | * 1 -- we can write to this track, 0 -- we can't |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | */ |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1895 | static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | { |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1897 | switch (pd->mmc3_profile) { |
| 1898 | case 0x1a: /* DVD+RW */ |
| 1899 | case 0x12: /* DVD-RAM */ |
| 1900 | /* The track is always writable on DVD+RW/DVD-RAM */ |
| 1901 | return 1; |
| 1902 | default: |
| 1903 | break; |
| 1904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1906 | if (!ti->packet || !ti->fp) |
| 1907 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
| 1909 | /* |
| 1910 | * "good" settings as per Mt Fuji. |
| 1911 | */ |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1912 | if (ti->rt == 0 && ti->blank == 0) |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1913 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1915 | if (ti->rt == 0 && ti->blank == 1) |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1916 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1918 | if (ti->rt == 1 && ti->blank == 0) |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1919 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1921 | printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet); |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1922 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | /* |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1926 | * 1 -- we can write to this disc, 0 -- we can't |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | */ |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1928 | static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | { |
| 1930 | switch (pd->mmc3_profile) { |
| 1931 | case 0x0a: /* CD-RW */ |
| 1932 | case 0xffff: /* MMC3 not supported */ |
| 1933 | break; |
| 1934 | case 0x1a: /* DVD+RW */ |
| 1935 | case 0x13: /* DVD-RW */ |
| 1936 | case 0x12: /* DVD-RAM */ |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1937 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | default: |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1939 | VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile); |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1940 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * for disc type 0xff we should probably reserve a new track. |
| 1945 | * but i'm not sure, should we leave this to user apps? probably. |
| 1946 | */ |
| 1947 | if (di->disc_type == 0xff) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1948 | printk(DRIVER_NAME": Unknown disc. No track?\n"); |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1949 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | if (di->disc_type != 0x20 && di->disc_type != 0) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1953 | printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type); |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1954 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | if (di->erasable == 0) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1958 | printk(DRIVER_NAME": Disc not erasable\n"); |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1959 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | if (di->border_status == PACKET_SESSION_RESERVED) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1963 | printk(DRIVER_NAME": Can't write to last track (reserved)\n"); |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1964 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1967 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | static int pkt_probe_settings(struct pktcdvd_device *pd) |
| 1971 | { |
| 1972 | struct packet_command cgc; |
| 1973 | unsigned char buf[12]; |
| 1974 | disc_information di; |
| 1975 | track_information ti; |
| 1976 | int ret, track; |
| 1977 | |
| 1978 | init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ); |
| 1979 | cgc.cmd[0] = GPCMD_GET_CONFIGURATION; |
| 1980 | cgc.cmd[8] = 8; |
| 1981 | ret = pkt_generic_packet(pd, &cgc); |
| 1982 | pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7]; |
| 1983 | |
| 1984 | memset(&di, 0, sizeof(disc_information)); |
| 1985 | memset(&ti, 0, sizeof(track_information)); |
| 1986 | |
| 1987 | if ((ret = pkt_get_disc_info(pd, &di))) { |
| 1988 | printk("failed get_disc\n"); |
| 1989 | return ret; |
| 1990 | } |
| 1991 | |
Peter Osterlund | 7c613d5 | 2006-02-20 18:28:02 -0800 | [diff] [blame] | 1992 | if (!pkt_writable_disc(pd, &di)) |
Peter Osterlund | 9db9154 | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 1993 | return -EROFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR; |
| 1996 | |
| 1997 | track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */ |
| 1998 | if ((ret = pkt_get_track_info(pd, track, 1, &ti))) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 1999 | printk(DRIVER_NAME": failed get_track\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | return ret; |
| 2001 | } |
| 2002 | |
Peter Osterlund | ab863ec | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 2003 | if (!pkt_writable_track(pd, &ti)) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2004 | printk(DRIVER_NAME": can't write to this track\n"); |
Peter Osterlund | 9db9154 | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 2005 | return -EROFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * we keep packet size in 512 byte units, makes it easier to |
| 2010 | * deal with request calculations. |
| 2011 | */ |
| 2012 | pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2; |
| 2013 | if (pd->settings.size == 0) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2014 | printk(DRIVER_NAME": detected zero packet size!\n"); |
Phillip Susi | a460ad6 | 2006-02-04 23:27:44 -0800 | [diff] [blame] | 2015 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | } |
Peter Osterlund | d0272e7 | 2005-09-13 01:25:27 -0700 | [diff] [blame] | 2017 | if (pd->settings.size > PACKET_MAX_SECTORS) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2018 | printk(DRIVER_NAME": packet size is too big\n"); |
Peter Osterlund | 9db9154 | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 2019 | return -EROFS; |
Peter Osterlund | d0272e7 | 2005-09-13 01:25:27 -0700 | [diff] [blame] | 2020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | pd->settings.fp = ti.fp; |
| 2022 | pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1); |
| 2023 | |
| 2024 | if (ti.nwa_v) { |
| 2025 | pd->nwa = be32_to_cpu(ti.next_writable); |
| 2026 | set_bit(PACKET_NWA_VALID, &pd->flags); |
| 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * in theory we could use lra on -RW media as well and just zero |
| 2031 | * blocks that haven't been written yet, but in practice that |
| 2032 | * is just a no-go. we'll use that for -R, naturally. |
| 2033 | */ |
| 2034 | if (ti.lra_v) { |
| 2035 | pd->lra = be32_to_cpu(ti.last_rec_address); |
| 2036 | set_bit(PACKET_LRA_VALID, &pd->flags); |
| 2037 | } else { |
| 2038 | pd->lra = 0xffffffff; |
| 2039 | set_bit(PACKET_LRA_VALID, &pd->flags); |
| 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * fine for now |
| 2044 | */ |
| 2045 | pd->settings.link_loss = 7; |
| 2046 | pd->settings.write_type = 0; /* packet */ |
| 2047 | pd->settings.track_mode = ti.track_mode; |
| 2048 | |
| 2049 | /* |
| 2050 | * mode1 or mode2 disc |
| 2051 | */ |
| 2052 | switch (ti.data_mode) { |
| 2053 | case PACKET_MODE1: |
| 2054 | pd->settings.block_mode = PACKET_BLOCK_MODE1; |
| 2055 | break; |
| 2056 | case PACKET_MODE2: |
| 2057 | pd->settings.block_mode = PACKET_BLOCK_MODE2; |
| 2058 | break; |
| 2059 | default: |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2060 | printk(DRIVER_NAME": unknown data mode\n"); |
Peter Osterlund | 9db9154 | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 2061 | return -EROFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | } |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | /* |
| 2067 | * enable/disable write caching on drive |
| 2068 | */ |
| 2069 | static int pkt_write_caching(struct pktcdvd_device *pd, int set) |
| 2070 | { |
| 2071 | struct packet_command cgc; |
| 2072 | struct request_sense sense; |
| 2073 | unsigned char buf[64]; |
| 2074 | int ret; |
| 2075 | |
| 2076 | memset(buf, 0, sizeof(buf)); |
| 2077 | init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ); |
| 2078 | cgc.sense = &sense; |
| 2079 | cgc.buflen = pd->mode_offset + 12; |
| 2080 | |
| 2081 | /* |
| 2082 | * caching mode page might not be there, so quiet this command |
| 2083 | */ |
| 2084 | cgc.quiet = 1; |
| 2085 | |
| 2086 | if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0))) |
| 2087 | return ret; |
| 2088 | |
| 2089 | buf[pd->mode_offset + 10] |= (!!set << 2); |
| 2090 | |
| 2091 | cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff)); |
| 2092 | ret = pkt_mode_select(pd, &cgc); |
| 2093 | if (ret) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2094 | printk(DRIVER_NAME": write caching control failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | pkt_dump_sense(&cgc); |
| 2096 | } else if (!ret && set) |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2097 | printk(DRIVER_NAME": enabled write caching on %s\n", pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | return ret; |
| 2099 | } |
| 2100 | |
| 2101 | static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag) |
| 2102 | { |
| 2103 | struct packet_command cgc; |
| 2104 | |
| 2105 | init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); |
| 2106 | cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL; |
| 2107 | cgc.cmd[4] = lockflag ? 1 : 0; |
| 2108 | return pkt_generic_packet(pd, &cgc); |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * Returns drive maximum write speed |
| 2113 | */ |
| 2114 | static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed) |
| 2115 | { |
| 2116 | struct packet_command cgc; |
| 2117 | struct request_sense sense; |
| 2118 | unsigned char buf[256+18]; |
| 2119 | unsigned char *cap_buf; |
| 2120 | int ret, offset; |
| 2121 | |
| 2122 | memset(buf, 0, sizeof(buf)); |
| 2123 | cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset]; |
| 2124 | init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN); |
| 2125 | cgc.sense = &sense; |
| 2126 | |
| 2127 | ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0); |
| 2128 | if (ret) { |
| 2129 | cgc.buflen = pd->mode_offset + cap_buf[1] + 2 + |
| 2130 | sizeof(struct mode_page_header); |
| 2131 | ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0); |
| 2132 | if (ret) { |
| 2133 | pkt_dump_sense(&cgc); |
| 2134 | return ret; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | offset = 20; /* Obsoleted field, used by older drives */ |
| 2139 | if (cap_buf[1] >= 28) |
| 2140 | offset = 28; /* Current write speed selected */ |
| 2141 | if (cap_buf[1] >= 30) { |
| 2142 | /* If the drive reports at least one "Logical Unit Write |
| 2143 | * Speed Performance Descriptor Block", use the information |
| 2144 | * in the first block. (contains the highest speed) |
| 2145 | */ |
| 2146 | int num_spdb = (cap_buf[30] << 8) + cap_buf[31]; |
| 2147 | if (num_spdb > 0) |
| 2148 | offset = 34; |
| 2149 | } |
| 2150 | |
| 2151 | *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1]; |
| 2152 | return 0; |
| 2153 | } |
| 2154 | |
| 2155 | /* These tables from cdrecord - I don't have orange book */ |
| 2156 | /* standard speed CD-RW (1-4x) */ |
| 2157 | static char clv_to_speed[16] = { |
| 2158 | /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ |
| 2159 | 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 2160 | }; |
| 2161 | /* high speed CD-RW (-10x) */ |
| 2162 | static char hs_clv_to_speed[16] = { |
| 2163 | /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ |
| 2164 | 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 2165 | }; |
| 2166 | /* ultra high speed CD-RW */ |
| 2167 | static char us_clv_to_speed[16] = { |
| 2168 | /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ |
| 2169 | 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0 |
| 2170 | }; |
| 2171 | |
| 2172 | /* |
| 2173 | * reads the maximum media speed from ATIP |
| 2174 | */ |
| 2175 | static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed) |
| 2176 | { |
| 2177 | struct packet_command cgc; |
| 2178 | struct request_sense sense; |
| 2179 | unsigned char buf[64]; |
| 2180 | unsigned int size, st, sp; |
| 2181 | int ret; |
| 2182 | |
| 2183 | init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ); |
| 2184 | cgc.sense = &sense; |
| 2185 | cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP; |
| 2186 | cgc.cmd[1] = 2; |
| 2187 | cgc.cmd[2] = 4; /* READ ATIP */ |
| 2188 | cgc.cmd[8] = 2; |
| 2189 | ret = pkt_generic_packet(pd, &cgc); |
| 2190 | if (ret) { |
| 2191 | pkt_dump_sense(&cgc); |
| 2192 | return ret; |
| 2193 | } |
| 2194 | size = ((unsigned int) buf[0]<<8) + buf[1] + 2; |
| 2195 | if (size > sizeof(buf)) |
| 2196 | size = sizeof(buf); |
| 2197 | |
| 2198 | init_cdrom_command(&cgc, buf, size, CGC_DATA_READ); |
| 2199 | cgc.sense = &sense; |
| 2200 | cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP; |
| 2201 | cgc.cmd[1] = 2; |
| 2202 | cgc.cmd[2] = 4; |
| 2203 | cgc.cmd[8] = size; |
| 2204 | ret = pkt_generic_packet(pd, &cgc); |
| 2205 | if (ret) { |
| 2206 | pkt_dump_sense(&cgc); |
| 2207 | return ret; |
| 2208 | } |
| 2209 | |
| 2210 | if (!buf[6] & 0x40) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2211 | printk(DRIVER_NAME": Disc type is not CD-RW\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | return 1; |
| 2213 | } |
| 2214 | if (!buf[6] & 0x4) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2215 | printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | return 1; |
| 2217 | } |
| 2218 | |
| 2219 | st = (buf[6] >> 3) & 0x7; /* disc sub-type */ |
| 2220 | |
| 2221 | sp = buf[16] & 0xf; /* max speed from ATIP A1 field */ |
| 2222 | |
| 2223 | /* Info from cdrecord */ |
| 2224 | switch (st) { |
| 2225 | case 0: /* standard speed */ |
| 2226 | *speed = clv_to_speed[sp]; |
| 2227 | break; |
| 2228 | case 1: /* high speed */ |
| 2229 | *speed = hs_clv_to_speed[sp]; |
| 2230 | break; |
| 2231 | case 2: /* ultra high speed */ |
| 2232 | *speed = us_clv_to_speed[sp]; |
| 2233 | break; |
| 2234 | default: |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2235 | printk(DRIVER_NAME": Unknown disc sub-type %d\n",st); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | return 1; |
| 2237 | } |
| 2238 | if (*speed) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2239 | printk(DRIVER_NAME": Max. media speed: %d\n",*speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | return 0; |
| 2241 | } else { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2242 | printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | return 1; |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | static int pkt_perform_opc(struct pktcdvd_device *pd) |
| 2248 | { |
| 2249 | struct packet_command cgc; |
| 2250 | struct request_sense sense; |
| 2251 | int ret; |
| 2252 | |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2253 | VPRINTK(DRIVER_NAME": Performing OPC\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | |
| 2255 | init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); |
| 2256 | cgc.sense = &sense; |
| 2257 | cgc.timeout = 60*HZ; |
| 2258 | cgc.cmd[0] = GPCMD_SEND_OPC; |
| 2259 | cgc.cmd[1] = 1; |
| 2260 | if ((ret = pkt_generic_packet(pd, &cgc))) |
| 2261 | pkt_dump_sense(&cgc); |
| 2262 | return ret; |
| 2263 | } |
| 2264 | |
| 2265 | static int pkt_open_write(struct pktcdvd_device *pd) |
| 2266 | { |
| 2267 | int ret; |
| 2268 | unsigned int write_speed, media_write_speed, read_speed; |
| 2269 | |
| 2270 | if ((ret = pkt_probe_settings(pd))) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2271 | VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name); |
Peter Osterlund | 9db9154 | 2006-02-20 18:28:04 -0800 | [diff] [blame] | 2272 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | if ((ret = pkt_set_write_settings(pd))) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2276 | DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | return -EIO; |
| 2278 | } |
| 2279 | |
| 2280 | pkt_write_caching(pd, USE_WCACHING); |
| 2281 | |
| 2282 | if ((ret = pkt_get_max_speed(pd, &write_speed))) |
| 2283 | write_speed = 16 * 177; |
| 2284 | switch (pd->mmc3_profile) { |
| 2285 | case 0x13: /* DVD-RW */ |
| 2286 | case 0x1a: /* DVD+RW */ |
| 2287 | case 0x12: /* DVD-RAM */ |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2288 | DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | break; |
| 2290 | default: |
| 2291 | if ((ret = pkt_media_speed(pd, &media_write_speed))) |
| 2292 | media_write_speed = 16; |
| 2293 | write_speed = min(write_speed, media_write_speed * 177); |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2294 | DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | break; |
| 2296 | } |
| 2297 | read_speed = write_speed; |
| 2298 | |
| 2299 | if ((ret = pkt_set_speed(pd, write_speed, read_speed))) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2300 | DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | return -EIO; |
| 2302 | } |
| 2303 | pd->write_speed = write_speed; |
| 2304 | pd->read_speed = read_speed; |
| 2305 | |
| 2306 | if ((ret = pkt_perform_opc(pd))) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2307 | DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | return 0; |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * called at open time. |
| 2315 | */ |
| 2316 | static int pkt_open_dev(struct pktcdvd_device *pd, int write) |
| 2317 | { |
| 2318 | int ret; |
| 2319 | long lba; |
| 2320 | request_queue_t *q; |
| 2321 | |
| 2322 | /* |
| 2323 | * We need to re-open the cdrom device without O_NONBLOCK to be able |
| 2324 | * to read/write from/to it. It is already opened in O_NONBLOCK mode |
| 2325 | * so bdget() can't fail. |
| 2326 | */ |
| 2327 | bdget(pd->bdev->bd_dev); |
| 2328 | if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY))) |
| 2329 | goto out; |
| 2330 | |
Peter Osterlund | 8382bf2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 2331 | if ((ret = bd_claim(pd->bdev, pd))) |
| 2332 | goto out_putdev; |
| 2333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | if ((ret = pkt_get_last_written(pd, &lba))) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2335 | printk(DRIVER_NAME": pkt_get_last_written failed\n"); |
Peter Osterlund | 8382bf2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 2336 | goto out_unclaim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | set_capacity(pd->disk, lba << 2); |
| 2340 | set_capacity(pd->bdev->bd_disk, lba << 2); |
| 2341 | bd_set_size(pd->bdev, (loff_t)lba << 11); |
| 2342 | |
| 2343 | q = bdev_get_queue(pd->bdev); |
| 2344 | if (write) { |
| 2345 | if ((ret = pkt_open_write(pd))) |
Peter Osterlund | 8382bf2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 2346 | goto out_unclaim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | /* |
| 2348 | * Some CDRW drives can not handle writes larger than one packet, |
| 2349 | * even if the size is a multiple of the packet size. |
| 2350 | */ |
| 2351 | spin_lock_irq(q->queue_lock); |
| 2352 | blk_queue_max_sectors(q, pd->settings.size); |
| 2353 | spin_unlock_irq(q->queue_lock); |
| 2354 | set_bit(PACKET_WRITABLE, &pd->flags); |
| 2355 | } else { |
| 2356 | pkt_set_speed(pd, MAX_SPEED, MAX_SPEED); |
| 2357 | clear_bit(PACKET_WRITABLE, &pd->flags); |
| 2358 | } |
| 2359 | |
| 2360 | if ((ret = pkt_set_segment_merging(pd, q))) |
Peter Osterlund | 8382bf2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 2361 | goto out_unclaim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 2363 | if (write) { |
| 2364 | if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2365 | printk(DRIVER_NAME": not enough memory for buffers\n"); |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 2366 | ret = -ENOMEM; |
| 2367 | goto out_unclaim; |
| 2368 | } |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2369 | printk(DRIVER_NAME": %lukB available on disc\n", lba << 1); |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 2370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | |
| 2372 | return 0; |
| 2373 | |
Peter Osterlund | 8382bf2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 2374 | out_unclaim: |
| 2375 | bd_release(pd->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | out_putdev: |
| 2377 | blkdev_put(pd->bdev); |
| 2378 | out: |
| 2379 | return ret; |
| 2380 | } |
| 2381 | |
| 2382 | /* |
| 2383 | * called when the device is closed. makes sure that the device flushes |
| 2384 | * the internal cache before we close. |
| 2385 | */ |
| 2386 | static void pkt_release_dev(struct pktcdvd_device *pd, int flush) |
| 2387 | { |
| 2388 | if (flush && pkt_flush_cache(pd)) |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2389 | DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | |
| 2391 | pkt_lock_door(pd, 0); |
| 2392 | |
| 2393 | pkt_set_speed(pd, MAX_SPEED, MAX_SPEED); |
Peter Osterlund | 8382bf2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 2394 | bd_release(pd->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | blkdev_put(pd->bdev); |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 2396 | |
| 2397 | pkt_shrink_pktlist(pd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor) |
| 2401 | { |
| 2402 | if (dev_minor >= MAX_WRITERS) |
| 2403 | return NULL; |
| 2404 | return pkt_devs[dev_minor]; |
| 2405 | } |
| 2406 | |
| 2407 | static int pkt_open(struct inode *inode, struct file *file) |
| 2408 | { |
| 2409 | struct pktcdvd_device *pd = NULL; |
| 2410 | int ret; |
| 2411 | |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2412 | VPRINTK(DRIVER_NAME": entering open\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 2414 | mutex_lock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | pd = pkt_find_dev_from_minor(iminor(inode)); |
| 2416 | if (!pd) { |
| 2417 | ret = -ENODEV; |
| 2418 | goto out; |
| 2419 | } |
| 2420 | BUG_ON(pd->refcnt < 0); |
| 2421 | |
| 2422 | pd->refcnt++; |
Peter Osterlund | 46f4e1b | 2005-05-20 13:59:06 -0700 | [diff] [blame] | 2423 | if (pd->refcnt > 1) { |
| 2424 | if ((file->f_mode & FMODE_WRITE) && |
| 2425 | !test_bit(PACKET_WRITABLE, &pd->flags)) { |
| 2426 | ret = -EBUSY; |
| 2427 | goto out_dec; |
| 2428 | } |
| 2429 | } else { |
Peter Osterlund | 01fd9fd | 2006-02-14 13:52:55 -0800 | [diff] [blame] | 2430 | ret = pkt_open_dev(pd, file->f_mode & FMODE_WRITE); |
| 2431 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | goto out_dec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | /* |
| 2434 | * needed here as well, since ext2 (among others) may change |
| 2435 | * the blocksize at mount time |
| 2436 | */ |
| 2437 | set_blocksize(inode->i_bdev, CD_FRAMESIZE); |
| 2438 | } |
| 2439 | |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 2440 | mutex_unlock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | return 0; |
| 2442 | |
| 2443 | out_dec: |
| 2444 | pd->refcnt--; |
| 2445 | out: |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2446 | VPRINTK(DRIVER_NAME": failed open (%d)\n", ret); |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 2447 | mutex_unlock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | return ret; |
| 2449 | } |
| 2450 | |
| 2451 | static int pkt_close(struct inode *inode, struct file *file) |
| 2452 | { |
| 2453 | struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data; |
| 2454 | int ret = 0; |
| 2455 | |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 2456 | mutex_lock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | pd->refcnt--; |
| 2458 | BUG_ON(pd->refcnt < 0); |
| 2459 | if (pd->refcnt == 0) { |
| 2460 | int flush = test_bit(PACKET_WRITABLE, &pd->flags); |
| 2461 | pkt_release_dev(pd, flush); |
| 2462 | } |
Jes Sorensen | 1657f82 | 2006-03-23 03:00:25 -0800 | [diff] [blame] | 2463 | mutex_unlock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | return ret; |
| 2465 | } |
| 2466 | |
| 2467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err) |
| 2469 | { |
| 2470 | struct packet_stacked_data *psd = bio->bi_private; |
| 2471 | struct pktcdvd_device *pd = psd->pd; |
| 2472 | |
| 2473 | if (bio->bi_size) |
| 2474 | return 1; |
| 2475 | |
| 2476 | bio_put(bio); |
| 2477 | bio_endio(psd->bio, psd->bio->bi_size, err); |
| 2478 | mempool_free(psd, psd_pool); |
| 2479 | pkt_bio_finished(pd); |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | static int pkt_make_request(request_queue_t *q, struct bio *bio) |
| 2484 | { |
| 2485 | struct pktcdvd_device *pd; |
| 2486 | char b[BDEVNAME_SIZE]; |
| 2487 | sector_t zone; |
| 2488 | struct packet_data *pkt; |
| 2489 | int was_empty, blocked_bio; |
| 2490 | struct pkt_rb_node *node; |
| 2491 | |
| 2492 | pd = q->queuedata; |
| 2493 | if (!pd) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2494 | printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | goto end_io; |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Clone READ bios so we can have our own bi_end_io callback. |
| 2500 | */ |
| 2501 | if (bio_data_dir(bio) == READ) { |
| 2502 | struct bio *cloned_bio = bio_clone(bio, GFP_NOIO); |
| 2503 | struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO); |
| 2504 | |
| 2505 | psd->pd = pd; |
| 2506 | psd->bio = bio; |
| 2507 | cloned_bio->bi_bdev = pd->bdev; |
| 2508 | cloned_bio->bi_private = psd; |
| 2509 | cloned_bio->bi_end_io = pkt_end_io_read_cloned; |
| 2510 | pd->stats.secs_r += bio->bi_size >> 9; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 2511 | pkt_queue_bio(pd, cloned_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | return 0; |
| 2513 | } |
| 2514 | |
| 2515 | if (!test_bit(PACKET_WRITABLE, &pd->flags)) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2516 | printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | pd->name, (unsigned long long)bio->bi_sector); |
| 2518 | goto end_io; |
| 2519 | } |
| 2520 | |
| 2521 | if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2522 | printk(DRIVER_NAME": wrong bio size\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | goto end_io; |
| 2524 | } |
| 2525 | |
| 2526 | blk_queue_bounce(q, &bio); |
| 2527 | |
| 2528 | zone = ZONE(bio->bi_sector, pd); |
| 2529 | VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n", |
| 2530 | (unsigned long long)bio->bi_sector, |
| 2531 | (unsigned long long)(bio->bi_sector + bio_sectors(bio))); |
| 2532 | |
| 2533 | /* Check if we have to split the bio */ |
| 2534 | { |
| 2535 | struct bio_pair *bp; |
| 2536 | sector_t last_zone; |
| 2537 | int first_sectors; |
| 2538 | |
| 2539 | last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd); |
| 2540 | if (last_zone != zone) { |
| 2541 | BUG_ON(last_zone != zone + pd->settings.size); |
| 2542 | first_sectors = last_zone - bio->bi_sector; |
| 2543 | bp = bio_split(bio, bio_split_pool, first_sectors); |
| 2544 | BUG_ON(!bp); |
| 2545 | pkt_make_request(q, &bp->bio1); |
| 2546 | pkt_make_request(q, &bp->bio2); |
| 2547 | bio_pair_release(bp); |
| 2548 | return 0; |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | /* |
| 2553 | * If we find a matching packet in state WAITING or READ_WAIT, we can |
| 2554 | * just append this bio to that packet. |
| 2555 | */ |
| 2556 | spin_lock(&pd->cdrw.active_list_lock); |
| 2557 | blocked_bio = 0; |
| 2558 | list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { |
| 2559 | if (pkt->sector == zone) { |
| 2560 | spin_lock(&pkt->lock); |
| 2561 | if ((pkt->state == PACKET_WAITING_STATE) || |
| 2562 | (pkt->state == PACKET_READ_WAIT_STATE)) { |
| 2563 | pkt_add_list_last(bio, &pkt->orig_bios, |
| 2564 | &pkt->orig_bios_tail); |
| 2565 | pkt->write_size += bio->bi_size / CD_FRAMESIZE; |
| 2566 | if ((pkt->write_size >= pkt->frames) && |
| 2567 | (pkt->state == PACKET_WAITING_STATE)) { |
| 2568 | atomic_inc(&pkt->run_sm); |
| 2569 | wake_up(&pd->wqueue); |
| 2570 | } |
| 2571 | spin_unlock(&pkt->lock); |
| 2572 | spin_unlock(&pd->cdrw.active_list_lock); |
| 2573 | return 0; |
| 2574 | } else { |
| 2575 | blocked_bio = 1; |
| 2576 | } |
| 2577 | spin_unlock(&pkt->lock); |
| 2578 | } |
| 2579 | } |
| 2580 | spin_unlock(&pd->cdrw.active_list_lock); |
| 2581 | |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 2582 | /* |
| 2583 | * Test if there is enough room left in the bio work queue |
| 2584 | * (queue size >= congestion on mark). |
| 2585 | * If not, wait till the work queue size is below the congestion off mark. |
| 2586 | */ |
| 2587 | spin_lock(&pd->lock); |
| 2588 | if (pd->write_congestion_on > 0 |
| 2589 | && pd->bio_queue_size >= pd->write_congestion_on) { |
Thomas Maier | 83f3aa3 | 2007-02-10 01:45:11 -0800 | [diff] [blame] | 2590 | set_bdi_congested(&q->backing_dev_info, WRITE); |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 2591 | do { |
| 2592 | spin_unlock(&pd->lock); |
| 2593 | congestion_wait(WRITE, HZ); |
| 2594 | spin_lock(&pd->lock); |
| 2595 | } while(pd->bio_queue_size > pd->write_congestion_off); |
| 2596 | } |
| 2597 | spin_unlock(&pd->lock); |
| 2598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | /* |
| 2600 | * No matching packet found. Store the bio in the work queue. |
| 2601 | */ |
| 2602 | node = mempool_alloc(pd->rb_pool, GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | node->bio = bio; |
| 2604 | spin_lock(&pd->lock); |
| 2605 | BUG_ON(pd->bio_queue_size < 0); |
| 2606 | was_empty = (pd->bio_queue_size == 0); |
| 2607 | pkt_rbtree_insert(pd, node); |
| 2608 | spin_unlock(&pd->lock); |
| 2609 | |
| 2610 | /* |
| 2611 | * Wake up the worker thread. |
| 2612 | */ |
| 2613 | atomic_set(&pd->scan_queue, 1); |
| 2614 | if (was_empty) { |
| 2615 | /* This wake_up is required for correct operation */ |
| 2616 | wake_up(&pd->wqueue); |
| 2617 | } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) { |
| 2618 | /* |
| 2619 | * This wake up is not required for correct operation, |
| 2620 | * but improves performance in some cases. |
| 2621 | */ |
| 2622 | wake_up(&pd->wqueue); |
| 2623 | } |
| 2624 | return 0; |
| 2625 | end_io: |
| 2626 | bio_io_error(bio, bio->bi_size); |
| 2627 | return 0; |
| 2628 | } |
| 2629 | |
| 2630 | |
| 2631 | |
| 2632 | static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec) |
| 2633 | { |
| 2634 | struct pktcdvd_device *pd = q->queuedata; |
| 2635 | sector_t zone = ZONE(bio->bi_sector, pd); |
| 2636 | int used = ((bio->bi_sector - zone) << 9) + bio->bi_size; |
| 2637 | int remaining = (pd->settings.size << 9) - used; |
| 2638 | int remaining2; |
| 2639 | |
| 2640 | /* |
| 2641 | * A bio <= PAGE_SIZE must be allowed. If it crosses a packet |
| 2642 | * boundary, pkt_make_request() will split the bio. |
| 2643 | */ |
| 2644 | remaining2 = PAGE_SIZE - bio->bi_size; |
| 2645 | remaining = max(remaining, remaining2); |
| 2646 | |
| 2647 | BUG_ON(remaining < 0); |
| 2648 | return remaining; |
| 2649 | } |
| 2650 | |
| 2651 | static void pkt_init_queue(struct pktcdvd_device *pd) |
| 2652 | { |
| 2653 | request_queue_t *q = pd->disk->queue; |
| 2654 | |
| 2655 | blk_queue_make_request(q, pkt_make_request); |
| 2656 | blk_queue_hardsect_size(q, CD_FRAMESIZE); |
| 2657 | blk_queue_max_sectors(q, PACKET_MAX_SECTORS); |
| 2658 | blk_queue_merge_bvec(q, pkt_merge_bvec); |
| 2659 | q->queuedata = pd; |
| 2660 | } |
| 2661 | |
| 2662 | static int pkt_seq_show(struct seq_file *m, void *p) |
| 2663 | { |
| 2664 | struct pktcdvd_device *pd = m->private; |
| 2665 | char *msg; |
| 2666 | char bdev_buf[BDEVNAME_SIZE]; |
| 2667 | int states[PACKET_NUM_STATES]; |
| 2668 | |
| 2669 | seq_printf(m, "Writer %s mapped to %s:\n", pd->name, |
| 2670 | bdevname(pd->bdev, bdev_buf)); |
| 2671 | |
| 2672 | seq_printf(m, "\nSettings:\n"); |
| 2673 | seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2); |
| 2674 | |
| 2675 | if (pd->settings.write_type == 0) |
| 2676 | msg = "Packet"; |
| 2677 | else |
| 2678 | msg = "Unknown"; |
| 2679 | seq_printf(m, "\twrite type:\t\t%s\n", msg); |
| 2680 | |
| 2681 | seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable"); |
| 2682 | seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss); |
| 2683 | |
| 2684 | seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode); |
| 2685 | |
| 2686 | if (pd->settings.block_mode == PACKET_BLOCK_MODE1) |
| 2687 | msg = "Mode 1"; |
| 2688 | else if (pd->settings.block_mode == PACKET_BLOCK_MODE2) |
| 2689 | msg = "Mode 2"; |
| 2690 | else |
| 2691 | msg = "Unknown"; |
| 2692 | seq_printf(m, "\tblock mode:\t\t%s\n", msg); |
| 2693 | |
| 2694 | seq_printf(m, "\nStatistics:\n"); |
| 2695 | seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started); |
| 2696 | seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended); |
| 2697 | seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1); |
| 2698 | seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1); |
| 2699 | seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1); |
| 2700 | |
| 2701 | seq_printf(m, "\nMisc:\n"); |
| 2702 | seq_printf(m, "\treference count:\t%d\n", pd->refcnt); |
| 2703 | seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags); |
| 2704 | seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed); |
| 2705 | seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed); |
| 2706 | seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset); |
| 2707 | seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset); |
| 2708 | |
| 2709 | seq_printf(m, "\nQueue state:\n"); |
| 2710 | seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size); |
| 2711 | seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios)); |
| 2712 | seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector); |
| 2713 | |
| 2714 | pkt_count_states(pd, states); |
| 2715 | seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n", |
| 2716 | states[0], states[1], states[2], states[3], states[4], states[5]); |
| 2717 | |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 2718 | seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n", |
| 2719 | pd->write_congestion_off, |
| 2720 | pd->write_congestion_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | return 0; |
| 2722 | } |
| 2723 | |
| 2724 | static int pkt_seq_open(struct inode *inode, struct file *file) |
| 2725 | { |
| 2726 | return single_open(file, pkt_seq_show, PDE(inode)->data); |
| 2727 | } |
| 2728 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 2729 | static const struct file_operations pkt_proc_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | .open = pkt_seq_open, |
| 2731 | .read = seq_read, |
| 2732 | .llseek = seq_lseek, |
| 2733 | .release = single_release |
| 2734 | }; |
| 2735 | |
| 2736 | static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev) |
| 2737 | { |
| 2738 | int i; |
| 2739 | int ret = 0; |
| 2740 | char b[BDEVNAME_SIZE]; |
| 2741 | struct proc_dir_entry *proc; |
| 2742 | struct block_device *bdev; |
| 2743 | |
| 2744 | if (pd->pkt_dev == dev) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2745 | printk(DRIVER_NAME": Recursive setup not allowed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | return -EBUSY; |
| 2747 | } |
| 2748 | for (i = 0; i < MAX_WRITERS; i++) { |
| 2749 | struct pktcdvd_device *pd2 = pkt_devs[i]; |
| 2750 | if (!pd2) |
| 2751 | continue; |
| 2752 | if (pd2->bdev->bd_dev == dev) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2753 | printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | return -EBUSY; |
| 2755 | } |
| 2756 | if (pd2->pkt_dev == dev) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2757 | printk(DRIVER_NAME": Can't chain pktcdvd devices\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | return -EBUSY; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | bdev = bdget(dev); |
| 2763 | if (!bdev) |
| 2764 | return -ENOMEM; |
| 2765 | ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK); |
| 2766 | if (ret) |
| 2767 | return ret; |
| 2768 | |
| 2769 | /* This is safe, since we have a reference from open(). */ |
| 2770 | __module_get(THIS_MODULE); |
| 2771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | pd->bdev = bdev; |
| 2773 | set_blocksize(bdev, CD_FRAMESIZE); |
| 2774 | |
| 2775 | pkt_init_queue(pd); |
| 2776 | |
| 2777 | atomic_set(&pd->cdrw.pending_bios, 0); |
| 2778 | pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name); |
| 2779 | if (IS_ERR(pd->cdrw.thread)) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2780 | printk(DRIVER_NAME": can't start kernel thread\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | ret = -ENOMEM; |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 2782 | goto out_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | proc = create_proc_entry(pd->name, 0, pkt_proc); |
| 2786 | if (proc) { |
| 2787 | proc->data = pd; |
| 2788 | proc->proc_fops = &pkt_proc_fops; |
| 2789 | } |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2790 | DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | return 0; |
| 2792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | out_mem: |
| 2794 | blkdev_put(bdev); |
| 2795 | /* This is safe: open() is still holding a reference. */ |
| 2796 | module_put(THIS_MODULE); |
| 2797 | return ret; |
| 2798 | } |
| 2799 | |
| 2800 | static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) |
| 2801 | { |
| 2802 | struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data; |
| 2803 | |
| 2804 | VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | |
| 2806 | switch (cmd) { |
| 2807 | /* |
| 2808 | * forward selected CDROM ioctls to CD-ROM, for UDF |
| 2809 | */ |
| 2810 | case CDROMMULTISESSION: |
| 2811 | case CDROMREADTOCENTRY: |
| 2812 | case CDROM_LAST_WRITTEN: |
| 2813 | case CDROM_SEND_PACKET: |
| 2814 | case SCSI_IOCTL_SEND_COMMAND: |
Peter Osterlund | 118326e | 2005-05-14 00:58:30 -0700 | [diff] [blame] | 2815 | return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | |
| 2817 | case CDROMEJECT: |
| 2818 | /* |
| 2819 | * The door gets locked when the device is opened, so we |
| 2820 | * have to unlock it or else the eject command fails. |
| 2821 | */ |
Peter Osterlund | 948423e | 2006-02-14 13:52:56 -0800 | [diff] [blame] | 2822 | if (pd->refcnt == 1) |
| 2823 | pkt_lock_door(pd, 0); |
Peter Osterlund | 118326e | 2005-05-14 00:58:30 -0700 | [diff] [blame] | 2824 | return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | |
| 2826 | default: |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2827 | VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | return -ENOTTY; |
| 2829 | } |
| 2830 | |
| 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | static int pkt_media_changed(struct gendisk *disk) |
| 2835 | { |
| 2836 | struct pktcdvd_device *pd = disk->private_data; |
| 2837 | struct gendisk *attached_disk; |
| 2838 | |
| 2839 | if (!pd) |
| 2840 | return 0; |
| 2841 | if (!pd->bdev) |
| 2842 | return 0; |
| 2843 | attached_disk = pd->bdev->bd_disk; |
| 2844 | if (!attached_disk) |
| 2845 | return 0; |
| 2846 | return attached_disk->fops->media_changed(attached_disk); |
| 2847 | } |
| 2848 | |
| 2849 | static struct block_device_operations pktcdvd_ops = { |
| 2850 | .owner = THIS_MODULE, |
| 2851 | .open = pkt_open, |
| 2852 | .release = pkt_close, |
| 2853 | .ioctl = pkt_ioctl, |
| 2854 | .media_changed = pkt_media_changed, |
| 2855 | }; |
| 2856 | |
| 2857 | /* |
| 2858 | * Set up mapping from pktcdvd device to CD-ROM device. |
| 2859 | */ |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2860 | static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | { |
| 2862 | int idx; |
| 2863 | int ret = -ENOMEM; |
| 2864 | struct pktcdvd_device *pd; |
| 2865 | struct gendisk *disk; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2866 | |
| 2867 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
| 2869 | for (idx = 0; idx < MAX_WRITERS; idx++) |
| 2870 | if (!pkt_devs[idx]) |
| 2871 | break; |
| 2872 | if (idx == MAX_WRITERS) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2873 | printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS); |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2874 | ret = -EBUSY; |
| 2875 | goto out_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Peter Osterlund | 1107d2e | 2005-09-13 01:25:29 -0700 | [diff] [blame] | 2878 | pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | if (!pd) |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2880 | goto out_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 2882 | pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE, |
| 2883 | sizeof(struct pkt_rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | if (!pd->rb_pool) |
| 2885 | goto out_mem; |
| 2886 | |
Peter Osterlund | e1bc89b | 2006-02-04 23:27:47 -0800 | [diff] [blame] | 2887 | INIT_LIST_HEAD(&pd->cdrw.pkt_free_list); |
| 2888 | INIT_LIST_HEAD(&pd->cdrw.pkt_active_list); |
| 2889 | spin_lock_init(&pd->cdrw.active_list_lock); |
| 2890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | spin_lock_init(&pd->lock); |
| 2892 | spin_lock_init(&pd->iosched.lock); |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2893 | sprintf(pd->name, DRIVER_NAME"%d", idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | init_waitqueue_head(&pd->wqueue); |
| 2895 | pd->bio_queue = RB_ROOT; |
| 2896 | |
Thomas Maier | 0a0fc96 | 2006-12-08 02:36:11 -0800 | [diff] [blame] | 2897 | pd->write_congestion_on = write_congestion_on; |
| 2898 | pd->write_congestion_off = write_congestion_off; |
| 2899 | |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2900 | disk = alloc_disk(1); |
| 2901 | if (!disk) |
| 2902 | goto out_mem; |
| 2903 | pd->disk = disk; |
Thomas Maier | add2166 | 2006-10-04 02:15:30 -0700 | [diff] [blame] | 2904 | disk->major = pktdev_major; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | disk->first_minor = idx; |
| 2906 | disk->fops = &pktcdvd_ops; |
| 2907 | disk->flags = GENHD_FL_REMOVABLE; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2908 | strcpy(disk->disk_name, pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | disk->private_data = pd; |
| 2910 | disk->queue = blk_alloc_queue(GFP_KERNEL); |
| 2911 | if (!disk->queue) |
| 2912 | goto out_mem2; |
| 2913 | |
| 2914 | pd->pkt_dev = MKDEV(disk->major, disk->first_minor); |
| 2915 | ret = pkt_new_dev(pd, dev); |
| 2916 | if (ret) |
| 2917 | goto out_new_dev; |
| 2918 | |
| 2919 | add_disk(disk); |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2920 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 2921 | pkt_sysfs_dev_new(pd); |
| 2922 | pkt_debugfs_dev_new(pd); |
| 2923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | pkt_devs[idx] = pd; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2925 | if (pkt_dev) |
| 2926 | *pkt_dev = pd->pkt_dev; |
| 2927 | |
| 2928 | mutex_unlock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | return 0; |
| 2930 | |
| 2931 | out_new_dev: |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 2932 | blk_cleanup_queue(disk->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | out_mem2: |
| 2934 | put_disk(disk); |
| 2935 | out_mem: |
| 2936 | if (pd->rb_pool) |
| 2937 | mempool_destroy(pd->rb_pool); |
| 2938 | kfree(pd); |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2939 | out_mutex: |
| 2940 | mutex_unlock(&ctl_mutex); |
| 2941 | printk(DRIVER_NAME": setup of pktcdvd device failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | return ret; |
| 2943 | } |
| 2944 | |
| 2945 | /* |
| 2946 | * Tear down mapping from pktcdvd device to CD-ROM device. |
| 2947 | */ |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2948 | static int pkt_remove_dev(dev_t pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | { |
| 2950 | struct pktcdvd_device *pd; |
| 2951 | int idx; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2952 | int ret = 0; |
| 2953 | |
| 2954 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | |
| 2956 | for (idx = 0; idx < MAX_WRITERS; idx++) { |
| 2957 | pd = pkt_devs[idx]; |
| 2958 | if (pd && (pd->pkt_dev == pkt_dev)) |
| 2959 | break; |
| 2960 | } |
| 2961 | if (idx == MAX_WRITERS) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2962 | DPRINTK(DRIVER_NAME": dev not setup\n"); |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2963 | ret = -ENXIO; |
| 2964 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2967 | if (pd->refcnt > 0) { |
| 2968 | ret = -EBUSY; |
| 2969 | goto out; |
| 2970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | if (!IS_ERR(pd->cdrw.thread)) |
| 2972 | kthread_stop(pd->cdrw.thread); |
| 2973 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 2974 | pkt_devs[idx] = NULL; |
| 2975 | |
| 2976 | pkt_debugfs_dev_remove(pd); |
| 2977 | pkt_sysfs_dev_remove(pd); |
| 2978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | blkdev_put(pd->bdev); |
| 2980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | remove_proc_entry(pd->name, pkt_proc); |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 2982 | DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | |
| 2984 | del_gendisk(pd->disk); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 2985 | blk_cleanup_queue(pd->disk->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | put_disk(pd->disk); |
| 2987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | mempool_destroy(pd->rb_pool); |
| 2989 | kfree(pd); |
| 2990 | |
| 2991 | /* This is safe: open() is still holding a reference. */ |
| 2992 | module_put(THIS_MODULE); |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 2993 | |
| 2994 | out: |
| 2995 | mutex_unlock(&ctl_mutex); |
| 2996 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd) |
| 3000 | { |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 3001 | struct pktcdvd_device *pd; |
| 3002 | |
| 3003 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 3004 | |
| 3005 | pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | if (pd) { |
| 3007 | ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev); |
| 3008 | ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev); |
| 3009 | } else { |
| 3010 | ctrl_cmd->dev = 0; |
| 3011 | ctrl_cmd->pkt_dev = 0; |
| 3012 | } |
| 3013 | ctrl_cmd->num_devices = MAX_WRITERS; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 3014 | |
| 3015 | mutex_unlock(&ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) |
| 3019 | { |
| 3020 | void __user *argp = (void __user *)arg; |
| 3021 | struct pkt_ctrl_command ctrl_cmd; |
| 3022 | int ret = 0; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 3023 | dev_t pkt_dev = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | |
| 3025 | if (cmd != PACKET_CTRL_CMD) |
| 3026 | return -ENOTTY; |
| 3027 | |
| 3028 | if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command))) |
| 3029 | return -EFAULT; |
| 3030 | |
| 3031 | switch (ctrl_cmd.command) { |
| 3032 | case PKT_CTRL_CMD_SETUP: |
| 3033 | if (!capable(CAP_SYS_ADMIN)) |
| 3034 | return -EPERM; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 3035 | ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev); |
| 3036 | ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | break; |
| 3038 | case PKT_CTRL_CMD_TEARDOWN: |
| 3039 | if (!capable(CAP_SYS_ADMIN)) |
| 3040 | return -EPERM; |
Thomas Maier | adb9250 | 2006-12-08 02:36:10 -0800 | [diff] [blame] | 3041 | ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | break; |
| 3043 | case PKT_CTRL_CMD_STATUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | pkt_get_status(&ctrl_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | break; |
| 3046 | default: |
| 3047 | return -ENOTTY; |
| 3048 | } |
| 3049 | |
| 3050 | if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command))) |
| 3051 | return -EFAULT; |
| 3052 | return ret; |
| 3053 | } |
| 3054 | |
| 3055 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 3056 | static const struct file_operations pkt_ctl_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | .ioctl = pkt_ctl_ioctl, |
| 3058 | .owner = THIS_MODULE, |
| 3059 | }; |
| 3060 | |
| 3061 | static struct miscdevice pkt_misc = { |
| 3062 | .minor = MISC_DYNAMIC_MINOR, |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 3063 | .name = DRIVER_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | .fops = &pkt_ctl_fops |
| 3065 | }; |
| 3066 | |
| 3067 | static int __init pkt_init(void) |
| 3068 | { |
| 3069 | int ret; |
| 3070 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 3071 | mutex_init(&ctl_mutex); |
| 3072 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 3073 | psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE, |
| 3074 | sizeof(struct packet_stacked_data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | if (!psd_pool) |
| 3076 | return -ENOMEM; |
| 3077 | |
Thomas Maier | add2166 | 2006-10-04 02:15:30 -0700 | [diff] [blame] | 3078 | ret = register_blkdev(pktdev_major, DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | if (ret < 0) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 3080 | printk(DRIVER_NAME": Unable to register block device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | goto out2; |
| 3082 | } |
Thomas Maier | add2166 | 2006-10-04 02:15:30 -0700 | [diff] [blame] | 3083 | if (!pktdev_major) |
| 3084 | pktdev_major = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 3086 | ret = pkt_sysfs_init(); |
| 3087 | if (ret) |
| 3088 | goto out; |
| 3089 | |
| 3090 | pkt_debugfs_init(); |
| 3091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | ret = misc_register(&pkt_misc); |
| 3093 | if (ret) { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 3094 | printk(DRIVER_NAME": Unable to register misc device\n"); |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 3095 | goto out_misc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 3098 | pkt_proc = proc_mkdir(DRIVER_NAME, proc_root_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | return 0; |
| 3101 | |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 3102 | out_misc: |
| 3103 | pkt_debugfs_cleanup(); |
| 3104 | pkt_sysfs_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | out: |
Thomas Maier | add2166 | 2006-10-04 02:15:30 -0700 | [diff] [blame] | 3106 | unregister_blkdev(pktdev_major, DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | out2: |
| 3108 | mempool_destroy(psd_pool); |
| 3109 | return ret; |
| 3110 | } |
| 3111 | |
| 3112 | static void __exit pkt_exit(void) |
| 3113 | { |
Thomas Maier | 7822082 | 2006-10-04 02:15:28 -0700 | [diff] [blame] | 3114 | remove_proc_entry(DRIVER_NAME, proc_root_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | misc_deregister(&pkt_misc); |
Thomas Maier | 3269485 | 2006-12-08 02:36:12 -0800 | [diff] [blame] | 3116 | |
| 3117 | pkt_debugfs_cleanup(); |
| 3118 | pkt_sysfs_cleanup(); |
| 3119 | |
Thomas Maier | add2166 | 2006-10-04 02:15:30 -0700 | [diff] [blame] | 3120 | unregister_blkdev(pktdev_major, DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | mempool_destroy(psd_pool); |
| 3122 | } |
| 3123 | |
| 3124 | MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives"); |
| 3125 | MODULE_AUTHOR("Jens Axboe <axboe@suse.de>"); |
| 3126 | MODULE_LICENSE("GPL"); |
| 3127 | |
| 3128 | module_init(pkt_init); |
| 3129 | module_exit(pkt_exit); |