xref: /linux/include/linux/pktcdvd.h (revision 4413e16d9d21673bb5048a2e542f1aaa00015c2e)
1 /*
2  * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3  * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
4  *
5  * May be copied or modified under the terms of the GNU General Public
6  * License.  See linux/COPYING for more information.
7  *
8  * Packet writing layer for ATAPI and SCSI CD-R, CD-RW, DVD-R, and
9  * DVD-RW devices.
10  *
11  */
12 #ifndef __PKTCDVD_H
13 #define __PKTCDVD_H
14 
15 #include <linux/types.h>
16 
17 /*
18  * 1 for normal debug messages, 2 is very verbose. 0 to turn it off.
19  */
20 #define PACKET_DEBUG		1
21 
22 #define	MAX_WRITERS		8
23 
24 #define PKT_RB_POOL_SIZE	512
25 
26 /*
27  * How long we should hold a non-full packet before starting data gathering.
28  */
29 #define PACKET_WAIT_TIME	(HZ * 5 / 1000)
30 
31 /*
32  * use drive write caching -- we need deferred error handling to be
33  * able to successfully recover with this option (drive will return good
34  * status as soon as the cdb is validated).
35  */
36 #if defined(CONFIG_CDROM_PKTCDVD_WCACHE)
37 #define USE_WCACHING		1
38 #else
39 #define USE_WCACHING		0
40 #endif
41 
42 /*
43  * No user-servicable parts beyond this point ->
44  */
45 
46 /*
47  * device types
48  */
49 #define PACKET_CDR		1
50 #define	PACKET_CDRW		2
51 #define PACKET_DVDR		3
52 #define PACKET_DVDRW		4
53 
54 /*
55  * flags
56  */
57 #define PACKET_WRITABLE		1	/* pd is writable */
58 #define PACKET_NWA_VALID	2	/* next writable address valid */
59 #define PACKET_LRA_VALID	3	/* last recorded address valid */
60 #define PACKET_MERGE_SEGS	4	/* perform segment merging to keep */
61 					/* underlying cdrom device happy */
62 
63 /*
64  * Disc status -- from READ_DISC_INFO
65  */
66 #define PACKET_DISC_EMPTY	0
67 #define PACKET_DISC_INCOMPLETE	1
68 #define PACKET_DISC_COMPLETE	2
69 #define PACKET_DISC_OTHER	3
70 
71 /*
72  * write type, and corresponding data block type
73  */
74 #define PACKET_MODE1		1
75 #define PACKET_MODE2		2
76 #define PACKET_BLOCK_MODE1	8
77 #define PACKET_BLOCK_MODE2	10
78 
79 /*
80  * Last session/border status
81  */
82 #define PACKET_SESSION_EMPTY		0
83 #define PACKET_SESSION_INCOMPLETE	1
84 #define PACKET_SESSION_RESERVED		2
85 #define PACKET_SESSION_COMPLETE		3
86 
87 #define PACKET_MCN			"4a656e734178626f65323030300000"
88 
89 #undef PACKET_USE_LS
90 
91 #define PKT_CTRL_CMD_SETUP	0
92 #define PKT_CTRL_CMD_TEARDOWN	1
93 #define PKT_CTRL_CMD_STATUS	2
94 
95 struct pkt_ctrl_command {
96 	__u32 command;				/* in: Setup, teardown, status */
97 	__u32 dev_index;			/* in/out: Device index */
98 	__u32 dev;				/* in/out: Device nr for cdrw device */
99 	__u32 pkt_dev;				/* in/out: Device nr for packet device */
100 	__u32 num_devices;			/* out: Largest device index + 1 */
101 	__u32 padding;				/* Not used */
102 };
103 
104 /*
105  * packet ioctls
106  */
107 #define PACKET_IOCTL_MAGIC	('X')
108 #define PACKET_CTRL_CMD		_IOWR(PACKET_IOCTL_MAGIC, 1, struct pkt_ctrl_command)
109 
110 #ifdef __KERNEL__
111 #include <linux/blkdev.h>
112 #include <linux/completion.h>
113 #include <linux/cdrom.h>
114 #include <linux/kobject.h>
115 #include <linux/sysfs.h>
116 #include <linux/mempool.h>
117 
118 /* default bio write queue congestion marks */
119 #define PKT_WRITE_CONGESTION_ON    10000
120 #define PKT_WRITE_CONGESTION_OFF   9000
121 
122 
123 struct packet_settings
124 {
125 	__u32			size;		/* packet size in (512 byte) sectors */
126 	__u8			fp;		/* fixed packets */
127 	__u8			link_loss;	/* the rest is specified
128 						 * as per Mt Fuji */
129 	__u8			write_type;
130 	__u8			track_mode;
131 	__u8			block_mode;
132 };
133 
134 /*
135  * Very crude stats for now
136  */
137 struct packet_stats
138 {
139 	unsigned long		pkt_started;
140 	unsigned long		pkt_ended;
141 	unsigned long		secs_w;
142 	unsigned long		secs_rg;
143 	unsigned long		secs_r;
144 };
145 
146 struct packet_cdrw
147 {
148 	struct list_head	pkt_free_list;
149 	struct list_head	pkt_active_list;
150 	spinlock_t		active_list_lock; /* Serialize access to pkt_active_list */
151 	struct task_struct	*thread;
152 	atomic_t		pending_bios;
153 };
154 
155 /*
156  * Switch to high speed reading after reading this many kilobytes
157  * with no interspersed writes.
158  */
159 #define HI_SPEED_SWITCH 512
160 
161 struct packet_iosched
162 {
163 	atomic_t		attention;	/* Set to non-zero when queue processing is needed */
164 	int			writing;	/* Non-zero when writing, zero when reading */
165 	spinlock_t		lock;		/* Protecting read/write queue manipulations */
166 	struct bio_list		read_queue;
167 	struct bio_list		write_queue;
168 	sector_t		last_write;	/* The sector where the last write ended */
169 	int			successive_reads;
170 };
171 
172 /*
173  * 32 buffers of 2048 bytes
174  */
175 #if (PAGE_SIZE % CD_FRAMESIZE) != 0
176 #error "PAGE_SIZE must be a multiple of CD_FRAMESIZE"
177 #endif
178 #define PACKET_MAX_SIZE		128
179 #define FRAMES_PER_PAGE		(PAGE_SIZE / CD_FRAMESIZE)
180 #define PACKET_MAX_SECTORS	(PACKET_MAX_SIZE * CD_FRAMESIZE >> 9)
181 
182 enum packet_data_state {
183 	PACKET_IDLE_STATE,			/* Not used at the moment */
184 	PACKET_WAITING_STATE,			/* Waiting for more bios to arrive, so */
185 						/* we don't have to do as much */
186 						/* data gathering */
187 	PACKET_READ_WAIT_STATE,			/* Waiting for reads to fill in holes */
188 	PACKET_WRITE_WAIT_STATE,		/* Waiting for the write to complete */
189 	PACKET_RECOVERY_STATE,			/* Recover after read/write errors */
190 	PACKET_FINISHED_STATE,			/* After write has finished */
191 
192 	PACKET_NUM_STATES			/* Number of possible states */
193 };
194 
195 /*
196  * Information needed for writing a single packet
197  */
198 struct pktcdvd_device;
199 
200 struct packet_data
201 {
202 	struct list_head	list;
203 
204 	spinlock_t		lock;		/* Lock protecting state transitions and */
205 						/* orig_bios list */
206 
207 	struct bio_list		orig_bios;	/* Original bios passed to pkt_make_request */
208 						/* that will be handled by this packet */
209 	int			write_size;	/* Total size of all bios in the orig_bios */
210 						/* list, measured in number of frames */
211 
212 	struct bio		*w_bio;		/* The bio we will send to the real CD */
213 						/* device once we have all data for the */
214 						/* packet we are going to write */
215 	sector_t		sector;		/* First sector in this packet */
216 	int			frames;		/* Number of frames in this packet */
217 
218 	enum packet_data_state	state;		/* Current state */
219 	atomic_t		run_sm;		/* Incremented whenever the state */
220 						/* machine needs to be run */
221 	long			sleep_time;	/* Set this to non-zero to make the state */
222 						/* machine run after this many jiffies. */
223 
224 	atomic_t		io_wait;	/* Number of pending IO operations */
225 	atomic_t		io_errors;	/* Number of read/write errors during IO */
226 
227 	struct bio		*r_bios[PACKET_MAX_SIZE]; /* bios to use during data gathering */
228 	struct page		*pages[PACKET_MAX_SIZE / FRAMES_PER_PAGE];
229 
230 	int			cache_valid;	/* If non-zero, the data for the zone defined */
231 						/* by the sector variable is completely cached */
232 						/* in the pages[] vector. */
233 
234 	int			id;		/* ID number for debugging */
235 	struct pktcdvd_device	*pd;
236 };
237 
238 struct pkt_rb_node {
239 	struct rb_node		rb_node;
240 	struct bio		*bio;
241 };
242 
243 struct packet_stacked_data
244 {
245 	struct bio		*bio;		/* Original read request bio */
246 	struct pktcdvd_device	*pd;
247 };
248 #define PSD_POOL_SIZE		64
249 
250 struct pktcdvd_kobj
251 {
252 	struct kobject		kobj;
253 	struct pktcdvd_device	*pd;
254 };
255 #define to_pktcdvdkobj(_k) \
256   ((struct pktcdvd_kobj*)container_of(_k,struct pktcdvd_kobj,kobj))
257 
258 struct pktcdvd_device
259 {
260 	struct block_device	*bdev;		/* dev attached */
261 	dev_t			pkt_dev;	/* our dev */
262 	char			name[20];
263 	struct packet_settings	settings;
264 	struct packet_stats	stats;
265 	int			refcnt;		/* Open count */
266 	int			write_speed;	/* current write speed, kB/s */
267 	int			read_speed;	/* current read speed, kB/s */
268 	unsigned long		offset;		/* start offset */
269 	__u8			mode_offset;	/* 0 / 8 */
270 	__u8			type;
271 	unsigned long		flags;
272 	__u16			mmc3_profile;
273 	__u32			nwa;		/* next writable address */
274 	__u32			lra;		/* last recorded address */
275 	struct packet_cdrw	cdrw;
276 	wait_queue_head_t	wqueue;
277 
278 	spinlock_t		lock;		/* Serialize access to bio_queue */
279 	struct rb_root		bio_queue;	/* Work queue of bios we need to handle */
280 	int			bio_queue_size;	/* Number of nodes in bio_queue */
281 	sector_t		current_sector;	/* Keep track of where the elevator is */
282 	atomic_t		scan_queue;	/* Set to non-zero when pkt_handle_queue */
283 						/* needs to be run. */
284 	mempool_t		*rb_pool;	/* mempool for pkt_rb_node allocations */
285 
286 	struct packet_iosched   iosched;
287 	struct gendisk		*disk;
288 
289 	int			write_congestion_off;
290 	int			write_congestion_on;
291 
292 	struct device		*dev;		/* sysfs pktcdvd[0-7] dev */
293 	struct pktcdvd_kobj	*kobj_stat;	/* sysfs pktcdvd[0-7]/stat/     */
294 	struct pktcdvd_kobj	*kobj_wqueue;	/* sysfs pktcdvd[0-7]/write_queue/ */
295 
296 	struct dentry		*dfs_d_root;	/* debugfs: devname directory */
297 	struct dentry		*dfs_f_info;	/* debugfs: info file */
298 };
299 
300 #endif /* __KERNEL__ */
301 
302 #endif /* __PKTCDVD_H */
303