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