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