1 /*
2  * linux/drivers/ide/ide-tape.c		Version 1.17c	Sep, 2003
3  *
4  * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
5  *
6  * $Header$
7  *
8  * This driver was constructed as a student project in the software laboratory
9  * of the faculty of electrical engineering in the Technion - Israel's
10  * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
11  *
12  * It is hereby placed under the terms of the GNU general public license.
13  * (See linux/COPYING).
14  */
15 
16 /*
17  * IDE ATAPI streaming tape driver.
18  *
19  * This driver is a part of the Linux ide driver and works in co-operation
20  * with linux/drivers/block/ide.c.
21  *
22  * The driver, in co-operation with ide.c, basically traverses the
23  * request-list for the block device interface. The character device
24  * interface, on the other hand, creates new requests, adds them
25  * to the request-list of the block device, and waits for their completion.
26  *
27  * Pipelined operation mode is now supported on both reads and writes.
28  *
29  * The block device major and minor numbers are determined from the
30  * tape's relative position in the ide interfaces, as explained in ide.c.
31  *
32  * The character device interface consists of the following devices:
33  *
34  * ht0		major 37, minor 0	first  IDE tape, rewind on close.
35  * ht1		major 37, minor 1	second IDE tape, rewind on close.
36  * ...
37  * nht0		major 37, minor 128	first  IDE tape, no rewind on close.
38  * nht1		major 37, minor 129	second IDE tape, no rewind on close.
39  * ...
40  *
41  * Run linux/scripts/MAKEDEV.ide to create the above entries.
42  *
43  * The general magnetic tape commands compatible interface, as defined by
44  * include/linux/mtio.h, is accessible through the character device.
45  *
46  * General ide driver configuration options, such as the interrupt-unmask
47  * flag, can be configured by issuing an ioctl to the block device interface,
48  * as any other ide device.
49  *
50  * Our own ide-tape ioctl's can be issued to either the block device or
51  * the character device interface.
52  *
53  * Maximal throughput with minimal bus load will usually be achieved in the
54  * following scenario:
55  *
56  *	1.	ide-tape is operating in the pipelined operation mode.
57  *	2.	No buffering is performed by the user backup program.
58  *
59  * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
60  *
61  * Ver 0.1   Nov  1 95   Pre-working code :-)
62  * Ver 0.2   Nov 23 95   A short backup (few megabytes) and restore procedure
63  *                        was successful ! (Using tar cvf ... on the block
64  *                        device interface).
65  *                       A longer backup resulted in major swapping, bad
66  *                        overall Linux performance and eventually failed as
67  *                        we received non serial read-ahead requests from the
68  *                        buffer cache.
69  * Ver 0.3   Nov 28 95   Long backups are now possible, thanks to the
70  *                        character device interface. Linux's responsiveness
71  *                        and performance doesn't seem to be much affected
72  *                        from the background backup procedure.
73  *                       Some general mtio.h magnetic tape operations are
74  *                        now supported by our character device. As a result,
75  *                        popular tape utilities are starting to work with
76  *                        ide tapes :-)
77  *                       The following configurations were tested:
78  *                       	1. An IDE ATAPI TAPE shares the same interface
79  *                       	   and irq with an IDE ATAPI CDROM.
80  *                        	2. An IDE ATAPI TAPE shares the same interface
81  *                          	   and irq with a normal IDE disk.
82  *                        Both configurations seemed to work just fine !
83  *                        However, to be on the safe side, it is meanwhile
84  *                        recommended to give the IDE TAPE its own interface
85  *                        and irq.
86  *                       The one thing which needs to be done here is to
87  *                        add a "request postpone" feature to ide.c,
88  *                        so that we won't have to wait for the tape to finish
89  *                        performing a long media access (DSC) request (such
90  *                        as a rewind) before we can access the other device
91  *                        on the same interface. This effect doesn't disturb
92  *                        normal operation most of the time because read/write
93  *                        requests are relatively fast, and once we are
94  *                        performing one tape r/w request, a lot of requests
95  *                        from the other device can be queued and ide.c will
96  *			  service all of them after this single tape request.
97  * Ver 1.0   Dec 11 95   Integrated into Linux 1.3.46 development tree.
98  *                       On each read / write request, we now ask the drive
99  *                        if we can transfer a constant number of bytes
100  *                        (a parameter of the drive) only to its buffers,
101  *                        without causing actual media access. If we can't,
102  *                        we just wait until we can by polling the DSC bit.
103  *                        This ensures that while we are not transferring
104  *                        more bytes than the constant referred to above, the
105  *                        interrupt latency will not become too high and
106  *                        we won't cause an interrupt timeout, as happened
107  *                        occasionally in the previous version.
108  *                       While polling for DSC, the current request is
109  *                        postponed and ide.c is free to handle requests from
110  *                        the other device. This is handled transparently to
111  *                        ide.c. The hwgroup locking method which was used
112  *                        in the previous version was removed.
113  *                       Use of new general features which are provided by
114  *                        ide.c for use with atapi devices.
115  *                        (Programming done by Mark Lord)
116  *                       Few potential bug fixes (Again, suggested by Mark)
117  *                       Single character device data transfers are now
118  *                        not limited in size, as they were before.
119  *                       We are asking the tape about its recommended
120  *                        transfer unit and send a larger data transfer
121  *                        as several transfers of the above size.
122  *                        For best results, use an integral number of this
123  *                        basic unit (which is shown during driver
124  *                        initialization). I will soon add an ioctl to get
125  *                        this important parameter.
126  *                       Our data transfer buffer is allocated on startup,
127  *                        rather than before each data transfer. This should
128  *                        ensure that we will indeed have a data buffer.
129  * Ver 1.1   Dec 14 95   Fixed random problems which occurred when the tape
130  *                        shared an interface with another device.
131  *                        (poll_for_dsc was a complete mess).
132  *                       Removed some old (non-active) code which had
133  *                        to do with supporting buffer cache originated
134  *                        requests.
135  *                       The block device interface can now be opened, so
136  *                        that general ide driver features like the unmask
137  *                        interrupts flag can be selected with an ioctl.
138  *                        This is the only use of the block device interface.
139  *                       New fast pipelined operation mode (currently only on
140  *                        writes). When using the pipelined mode, the
141  *                        throughput can potentially reach the maximum
142  *                        tape supported throughput, regardless of the
143  *                        user backup program. On my tape drive, it sometimes
144  *                        boosted performance by a factor of 2. Pipelined
145  *                        mode is enabled by default, but since it has a few
146  *                        downfalls as well, you may want to disable it.
147  *                        A short explanation of the pipelined operation mode
148  *                        is available below.
149  * Ver 1.2   Jan  1 96   Eliminated pipelined mode race condition.
150  *                       Added pipeline read mode. As a result, restores
151  *                        are now as fast as backups.
152  *                       Optimized shared interface behavior. The new behavior
153  *                        typically results in better IDE bus efficiency and
154  *                        higher tape throughput.
155  *                       Pre-calculation of the expected read/write request
156  *                        service time, based on the tape's parameters. In
157  *                        the pipelined operation mode, this allows us to
158  *                        adjust our polling frequency to a much lower value,
159  *                        and thus to dramatically reduce our load on Linux,
160  *                        without any decrease in performance.
161  *                       Implemented additional mtio.h operations.
162  *                       The recommended user block size is returned by
163  *                        the MTIOCGET ioctl.
164  *                       Additional minor changes.
165  * Ver 1.3   Feb  9 96   Fixed pipelined read mode bug which prevented the
166  *                        use of some block sizes during a restore procedure.
167  *                       The character device interface will now present a
168  *                        continuous view of the media - any mix of block sizes
169  *                        during a backup/restore procedure is supported. The
170  *                        driver will buffer the requests internally and
171  *                        convert them to the tape's recommended transfer
172  *                        unit, making performance almost independent of the
173  *                        chosen user block size.
174  *                       Some improvements in error recovery.
175  *                       By cooperating with ide-dma.c, bus mastering DMA can
176  *                        now sometimes be used with IDE tape drives as well.
177  *                        Bus mastering DMA has the potential to dramatically
178  *                        reduce the CPU's overhead when accessing the device,
179  *                        and can be enabled by using hdparm -d1 on the tape's
180  *                        block device interface. For more info, read the
181  *                        comments in ide-dma.c.
182  * Ver 1.4   Mar 13 96   Fixed serialize support.
183  * Ver 1.5   Apr 12 96   Fixed shared interface operation, broken in 1.3.85.
184  *                       Fixed pipelined read mode inefficiency.
185  *                       Fixed nasty null dereferencing bug.
186  * Ver 1.6   Aug 16 96   Fixed FPU usage in the driver.
187  *                       Fixed end of media bug.
188  * Ver 1.7   Sep 10 96   Minor changes for the CONNER CTT8000-A model.
189  * Ver 1.8   Sep 26 96   Attempt to find a better balance between good
190  *                        interactive response and high system throughput.
191  * Ver 1.9   Nov  5 96   Automatically cross encountered filemarks rather
192  *                        than requiring an explicit FSF command.
193  *                       Abort pending requests at end of media.
194  *                       MTTELL was sometimes returning incorrect results.
195  *                       Return the real block size in the MTIOCGET ioctl.
196  *                       Some error recovery bug fixes.
197  * Ver 1.10  Nov  5 96   Major reorganization.
198  *                       Reduced CPU overhead a bit by eliminating internal
199  *                        bounce buffers.
200  *                       Added module support.
201  *                       Added multiple tape drives support.
202  *                       Added partition support.
203  *                       Rewrote DSC handling.
204  *                       Some portability fixes.
205  *                       Removed ide-tape.h.
206  *                       Additional minor changes.
207  * Ver 1.11  Dec  2 96   Bug fix in previous DSC timeout handling.
208  *                       Use ide_stall_queue() for DSC overlap.
209  *                       Use the maximum speed rather than the current speed
210  *                        to compute the request service time.
211  * Ver 1.12  Dec  7 97   Fix random memory overwriting and/or last block data
212  *                        corruption, which could occur if the total number
213  *                        of bytes written to the tape was not an integral
214  *                        number of tape blocks.
215  *                       Add support for INTERRUPT DRQ devices.
216  * Ver 1.13  Jan  2 98   Add "speed == 0" work-around for HP COLORADO 5GB
217  * Ver 1.14  Dec 30 98   Partial fixes for the Sony/AIWA tape drives.
218  *                       Replace cli()/sti() with hwgroup spinlocks.
219  * Ver 1.15  Mar 25 99   Fix SMP race condition by replacing hwgroup
220  *                        spinlock with private per-tape spinlock.
221  * Ver 1.16  Sep  1 99   Add OnStream tape support.
222  *                       Abort read pipeline on EOD.
223  *                       Wait for the tape to become ready in case it returns
224  *                        "in the process of becoming ready" on open().
225  *                       Fix zero padding of the last written block in
226  *                        case the tape block size is larger than PAGE_SIZE.
227  *                       Decrease the default disconnection time to tn.
228  * Ver 1.16e Oct  3 99   Minor fixes.
229  * Ver 1.16e1 Oct 13 99  Patches by Arnold Niessen,
230  *                          niessen@iae.nl / arnold.niessen@philips.com
231  *                   GO-1)  Undefined code in idetape_read_position
232  *				according to Gadi's email
233  *                   AJN-1) Minor fix asc == 11 should be asc == 0x11
234  *                               in idetape_issue_packet_command (did effect
235  *                               debugging output only)
236  *                   AJN-2) Added more debugging output, and
237  *                              added ide-tape: where missing. I would also
238  *				like to add tape->name where possible
239  *                   AJN-3) Added different debug_level's
240  *                              via /proc/ide/hdc/settings
241  * 				"debug_level" determines amount of debugging output;
242  * 				can be changed using /proc/ide/hdx/settings
243  * 				0 : almost no debugging output
244  * 				1 : 0+output errors only
245  * 				2 : 1+output all sensekey/asc
246  * 				3 : 2+follow all chrdev related procedures
247  * 				4 : 3+follow all procedures
248  * 				5 : 4+include pc_stack rq_stack info
249  * 				6 : 5+USE_COUNT updates
250  *                   AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
251  *				from 5 to 10 minutes
252  *                   AJN-5) Changed maximum number of blocks to skip when
253  *                              reading tapes with multiple consecutive write
254  *                              errors from 100 to 1000 in idetape_get_logical_blk
255  *                   Proposed changes to code:
256  *                   1) output "logical_blk_num" via /proc
257  *                   2) output "current_operation" via /proc
258  *                   3) Either solve or document the fact that `mt rewind' is
259  *                      required after reading from /dev/nhtx to be
260  *			able to rmmod the idetape module;
261  *			Also, sometimes an application finishes but the
262  *			device remains `busy' for some time. Same cause ?
263  *                   Proposed changes to release-notes:
264  *		     4) write a simple `quickstart' section in the
265  *                      release notes; I volunteer if you don't want to
266  * 		     5) include a pointer to video4linux in the doc
267  *                      to stimulate video applications
268  *                   6) release notes lines 331 and 362: explain what happens
269  *			if the application data rate is higher than 1100 KB/s;
270  *			similar approach to lower-than-500 kB/s ?
271  *		     7) 6.6 Comparison; wouldn't it be better to allow different
272  *			strategies for read and write ?
273  *			Wouldn't it be better to control the tape buffer
274  *			contents instead of the bandwidth ?
275  *		     8) line 536: replace will by would (if I understand
276  *			this section correctly, a hypothetical and unwanted situation
277  *			 is being described)
278  * Ver 1.16f Dec 15 99   Change place of the secondary OnStream header frames.
279  * Ver 1.17  Nov 2000 / Jan 2001  Marcel Mol, marcel@mesa.nl
280  *			- Add idetape_onstream_mode_sense_tape_parameter_page
281  *			  function to get tape capacity in frames: tape->capacity.
282  *			- Add support for DI-50 drives( or any DI- drive).
283  *			- 'workaround' for read error/blank block arround block 3000.
284  *			- Implement Early warning for end of media for Onstream.
285  *			- Cosmetic code changes for readability.
286  *			- Idetape_position_tape should not use SKIP bit during
287  *			  Onstream read recovery.
288  *			- Add capacity, logical_blk_num and first/last_frame_position
289  *			  to /proc/ide/hd?/settings.
290  *			- Module use count was gone in the Linux 2.4 driver.
291  * Ver 1.17a Apr 2001 Willem Riede osst@riede.org
292  * 			- Get drive's actual block size from mode sense block descriptor
293  * 			- Limit size of pipeline
294  * Ver 1.17b Dec 2002   Alan Stern <stern@rowland.harvard.edu>
295  *			Changed IDETAPE_MIN_PIPELINE_STAGES to 1 and actually used
296  *			 it in the code!
297  *			Actually removed aborted stages in idetape_abort_pipeline
298  *			 instead of just changing the command code.
299  *			Made the transfer byte count for Request Sense equal to the
300  *			 actual length of the data transfer.
301  *			Changed handling of partial data transfers: they do not
302  *			 cause DMA errors.
303  *			Moved initiation of DMA transfers to the correct place.
304  *			Removed reference to unallocated memory.
305  *			Made __idetape_discard_read_pipeline return the number of
306  *			 sectors skipped, not the number of stages.
307  *			Replaced errant kfree() calls with __idetape_kfree_stage().
308  *			Fixed off-by-one error in testing the pipeline length.
309  *			Fixed handling of filemarks in the read pipeline.
310  *			Small code optimization for MTBSF and MTBSFM ioctls.
311  *			Don't try to unlock the door during device close if is
312  *			 already unlocked!
313  *			Cosmetic fixes to miscellaneous debugging output messages.
314  *			Set the minimum /proc/ide/hd?/settings values for "pipeline",
315  *			 "pipeline_min", and "pipeline_max" to 1.
316  * Ver 1.17c Sep 2003	Stuart Hayes <stuart_hayes@dell.com>
317  *			Initialized "feature" in idetape_issue_packet_command
318  *			 (this was causing lockups on certain systems)
319  *
320  * Here are some words from the first releases of hd.c, which are quoted
321  * in ide.c and apply here as well:
322  *
323  * | Special care is recommended.  Have Fun!
324  *
325  */
326 
327 /*
328  * An overview of the pipelined operation mode.
329  *
330  * In the pipelined write mode, we will usually just add requests to our
331  * pipeline and return immediately, before we even start to service them. The
332  * user program will then have enough time to prepare the next request while
333  * we are still busy servicing previous requests. In the pipelined read mode,
334  * the situation is similar - we add read-ahead requests into the pipeline,
335  * before the user even requested them.
336  *
337  * The pipeline can be viewed as a "safety net" which will be activated when
338  * the system load is high and prevents the user backup program from keeping up
339  * with the current tape speed. At this point, the pipeline will get
340  * shorter and shorter but the tape will still be streaming at the same speed.
341  * Assuming we have enough pipeline stages, the system load will hopefully
342  * decrease before the pipeline is completely empty, and the backup program
343  * will be able to "catch up" and refill the pipeline again.
344  *
345  * When using the pipelined mode, it would be best to disable any type of
346  * buffering done by the user program, as ide-tape already provides all the
347  * benefits in the kernel, where it can be done in a more efficient way.
348  * As we will usually not block the user program on a request, the most
349  * efficient user code will then be a simple read-write-read-... cycle.
350  * Any additional logic will usually just slow down the backup process.
351  *
352  * Using the pipelined mode, I get a constant over 400 KBps throughput,
353  * which seems to be the maximum throughput supported by my tape.
354  *
355  * However, there are some downfalls:
356  *
357  *	1.	We use memory (for data buffers) in proportional to the number
358  *		of pipeline stages (each stage is about 26 KB with my tape).
359  *	2.	In the pipelined write mode, we cheat and postpone error codes
360  *		to the user task. In read mode, the actual tape position
361  *		will be a bit further than the last requested block.
362  *
363  * Concerning (1):
364  *
365  *	1.	We allocate stages dynamically only when we need them. When
366  *		we don't need them, we don't consume additional memory. In
367  *		case we can't allocate stages, we just manage without them
368  *		(at the expense of decreased throughput) so when Linux is
369  *		tight in memory, we will not pose additional difficulties.
370  *
371  *	2.	The maximum number of stages (which is, in fact, the maximum
372  *		amount of memory) which we allocate is limited by the compile
373  *		time parameter IDETAPE_MAX_PIPELINE_STAGES.
374  *
375  *	3.	The maximum number of stages is a controlled parameter - We
376  *		don't start from the user defined maximum number of stages
377  *		but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
378  *		will not even allocate this amount of stages if the user
379  *		program can't handle the speed). We then implement a feedback
380  *		loop which checks if the pipeline is empty, and if it is, we
381  *		increase the maximum number of stages as necessary until we
382  *		reach the optimum value which just manages to keep the tape
383  *		busy with minimum allocated memory or until we reach
384  *		IDETAPE_MAX_PIPELINE_STAGES.
385  *
386  * Concerning (2):
387  *
388  *	In pipelined write mode, ide-tape can not return accurate error codes
389  *	to the user program since we usually just add the request to the
390  *      pipeline without waiting for it to be serviced. In case an error
391  *      occurs, I will report it on the next user request.
392  *
393  *	In the pipelined read mode, subsequent read requests or forward
394  *	filemark spacing will perform correctly, as we preserve all blocks
395  *	and filemarks which we encountered during our excess read-ahead.
396  *
397  *	For accurate tape positioning and error reporting, disabling
398  *	pipelined mode might be the best option.
399  *
400  * You can enable/disable/tune the pipelined operation mode by adjusting
401  * the compile time parameters below.
402  */
403 
404 /*
405  *	Possible improvements.
406  *
407  *	1.	Support for the ATAPI overlap protocol.
408  *
409  *		In order to maximize bus throughput, we currently use the DSC
410  *		overlap method which enables ide.c to service requests from the
411  *		other device while the tape is busy executing a command. The
412  *		DSC overlap method involves polling the tape's status register
413  *		for the DSC bit, and servicing the other device while the tape
414  *		isn't ready.
415  *
416  *		In the current QIC development standard (December 1995),
417  *		it is recommended that new tape drives will *in addition*
418  *		implement the ATAPI overlap protocol, which is used for the
419  *		same purpose - efficient use of the IDE bus, but is interrupt
420  *		driven and thus has much less CPU overhead.
421  *
422  *		ATAPI overlap is likely to be supported in most new ATAPI
423  *		devices, including new ATAPI cdroms, and thus provides us
424  *		a method by which we can achieve higher throughput when
425  *		sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
426  */
427 
428 #define IDETAPE_VERSION "1.17c"
429 
430 #include <linux/config.h>
431 #include <linux/module.h>
432 #include <linux/types.h>
433 #include <linux/string.h>
434 #include <linux/kernel.h>
435 #include <linux/delay.h>
436 #include <linux/timer.h>
437 #include <linux/mm.h>
438 #include <linux/interrupt.h>
439 #include <linux/major.h>
440 #include <linux/devfs_fs_kernel.h>
441 #include <linux/errno.h>
442 #include <linux/genhd.h>
443 #include <linux/slab.h>
444 #include <linux/ide.h>
445 #include <linux/smp_lock.h>
446 #include <linux/completion.h>
447 
448 #include <asm/byteorder.h>
449 #include <asm/irq.h>
450 #include <asm/uaccess.h>
451 #include <asm/io.h>
452 #include <asm/unaligned.h>
453 #include <asm/bitops.h>
454 
455 
456 /*
457  *	OnStream support
458  */
459 #define ONSTREAM_DEBUG		(0)
460 #define OS_CONFIG_PARTITION	(0xff)
461 #define OS_DATA_PARTITION	(0)
462 #define OS_PARTITION_VERSION	(1)
463 #define OS_EW			300
464 #define OS_ADR_MINREV		2
465 
466 #define OS_DATA_STARTFRAME1	20
467 #define OS_DATA_ENDFRAME1	2980
468 /*
469  * partition
470  */
471 typedef struct os_partition_s {
472 	__u8	partition_num;
473 	__u8	par_desc_ver;
474 	__u16	wrt_pass_cntr;
475 	__u32	first_frame_addr;
476 	__u32	last_frame_addr;
477 	__u32	eod_frame_addr;
478 } os_partition_t;
479 
480 /*
481  * DAT entry
482  */
483 typedef struct os_dat_entry_s {
484 	__u32	blk_sz;
485 	__u16	blk_cnt;
486 	__u8	flags;
487 	__u8	reserved;
488 } os_dat_entry_t;
489 
490 /*
491  * DAT
492  */
493 #define OS_DAT_FLAGS_DATA	(0xc)
494 #define OS_DAT_FLAGS_MARK	(0x1)
495 
496 typedef struct os_dat_s {
497 	__u8		dat_sz;
498 	__u8		reserved1;
499 	__u8		entry_cnt;
500 	__u8		reserved3;
501 	os_dat_entry_t	dat_list[16];
502 } os_dat_t;
503 
504 /*
505  * Frame types
506  */
507 #define OS_FRAME_TYPE_FILL	(0)
508 #define OS_FRAME_TYPE_EOD	(1 << 0)
509 #define OS_FRAME_TYPE_MARKER	(1 << 1)
510 #define OS_FRAME_TYPE_HEADER	(1 << 3)
511 #define OS_FRAME_TYPE_DATA	(1 << 7)
512 
513 /*
514  * AUX
515  */
516 typedef struct os_aux_s {
517 	__u32		format_id;		/* hardware compability AUX is based on */
518 	char		application_sig[4];	/* driver used to write this media */
519 	__u32		hdwr;			/* reserved */
520 	__u32		update_frame_cntr;	/* for configuration frame */
521 	__u8		frame_type;
522 	__u8		frame_type_reserved;
523 	__u8		reserved_18_19[2];
524 	os_partition_t	partition;
525 	__u8		reserved_36_43[8];
526 	__u32		frame_seq_num;
527 	__u32		logical_blk_num_high;
528 	__u32		logical_blk_num;
529 	os_dat_t	dat;
530 	__u8		reserved188_191[4];
531 	__u32		filemark_cnt;
532 	__u32		phys_fm;
533 	__u32		last_mark_addr;
534 	__u8		reserved204_223[20];
535 
536 	/*
537 	 * __u8		app_specific[32];
538 	 *
539 	 * Linux specific fields:
540 	 */
541 	 __u32		next_mark_addr;		/* when known, points to next marker */
542 	 __u8		linux_specific[28];
543 
544 	__u8		reserved_256_511[256];
545 } os_aux_t;
546 
547 typedef struct os_header_s {
548 	char		ident_str[8];
549 	__u8		major_rev;
550 	__u8		minor_rev;
551 	__u8		reserved10_15[6];
552 	__u8		par_num;
553 	__u8		reserved1_3[3];
554 	os_partition_t	partition;
555 } os_header_t;
556 
557 /*
558  *	OnStream Tape Parameters Page
559  */
560 typedef struct {
561 	unsigned	page_code	:6;	/* Page code - Should be 0x2b */
562 	unsigned	reserved1_6	:1;
563 	unsigned	ps		:1;
564 	__u8		reserved2;
565 	__u8		density;		/* kbpi */
566 	__u8		reserved3,reserved4;
567 	__u16		segtrk;                 /* segment of per track */
568 	__u16		trks;                   /* tracks per tape */
569 	__u8		reserved5,reserved6,reserved7,reserved8,reserved9,reserved10;
570 } onstream_tape_paramtr_page_t;
571 
572 /*
573  * OnStream ADRL frame
574  */
575 #define OS_FRAME_SIZE	(32 * 1024 + 512)
576 #define OS_DATA_SIZE	(32 * 1024)
577 #define OS_AUX_SIZE	(512)
578 
579 /*
580  * internal error codes for onstream
581  */
582 #define OS_PART_ERROR    2
583 #define OS_WRITE_ERROR   1
584 
585 #include <linux/mtio.h>
586 
587 /**************************** Tunable parameters *****************************/
588 
589 
590 /*
591  *	Pipelined mode parameters.
592  *
593  *	We try to use the minimum number of stages which is enough to
594  *	keep the tape constantly streaming. To accomplish that, we implement
595  *	a feedback loop around the maximum number of stages:
596  *
597  *	We start from MIN maximum stages (we will not even use MIN stages
598  *      if we don't need them), increment it by RATE*(MAX-MIN)
599  *	whenever we sense that the pipeline is empty, until we reach
600  *	the optimum value or until we reach MAX.
601  *
602  *	Setting the following parameter to 0 is illegal: the pipelined mode
603  *	cannot be disabled (calculate_speeds() divides by tape->max_stages.)
604  */
605 #define IDETAPE_MIN_PIPELINE_STAGES	  1
606 #define IDETAPE_MAX_PIPELINE_STAGES	400
607 #define IDETAPE_INCREASE_STAGES_RATE	 20
608 
609 /*
610  *	The following are used to debug the driver:
611  *
612  *	Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
613  *	Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
614  *	Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
615  *	some places.
616  *
617  *	Setting them to 0 will restore normal operation mode:
618  *
619  *		1.	Disable logging normal successful operations.
620  *		2.	Disable self-sanity checks.
621  *		3.	Errors will still be logged, of course.
622  *
623  *	All the #if DEBUG code will be removed some day, when the driver
624  *	is verified to be stable enough. This will make it much more
625  *	esthetic.
626  */
627 #define IDETAPE_DEBUG_INFO		0
628 #define IDETAPE_DEBUG_LOG		0
629 #define IDETAPE_DEBUG_LOG_VERBOSE	0
630 #define IDETAPE_DEBUG_BUGS		1
631 
632 /*
633  *	After each failed packet command we issue a request sense command
634  *	and retry the packet command IDETAPE_MAX_PC_RETRIES times.
635  *
636  *	Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
637  */
638 #define IDETAPE_MAX_PC_RETRIES		3
639 
640 /*
641  *	With each packet command, we allocate a buffer of
642  *	IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
643  *	commands (Not for READ/WRITE commands).
644  */
645 #define IDETAPE_PC_BUFFER_SIZE		256
646 
647 /*
648  *	In various places in the driver, we need to allocate storage
649  *	for packet commands and requests, which will remain valid while
650  *	we leave the driver to wait for an interrupt or a timeout event.
651  */
652 #define IDETAPE_PC_STACK		(10 + IDETAPE_MAX_PC_RETRIES)
653 
654 /*
655  *	Some tape drives require a long irq timeout
656  */
657 #define IDETAPE_WAIT_CMD		(60*HZ)
658 
659 /*
660  *	The following parameter is used to select the point in the internal
661  *	tape fifo in which we will start to refill the buffer. Decreasing
662  *	the following parameter will improve the system's latency and
663  *	interactive response, while using a high value might improve sytem
664  *	throughput.
665  */
666 #define IDETAPE_FIFO_THRESHOLD 		2
667 
668 /*
669  *	DSC polling parameters.
670  *
671  *	Polling for DSC (a single bit in the status register) is a very
672  *	important function in ide-tape. There are two cases in which we
673  *	poll for DSC:
674  *
675  *	1.	Before a read/write packet command, to ensure that we
676  *		can transfer data from/to the tape's data buffers, without
677  *		causing an actual media access. In case the tape is not
678  *		ready yet, we take out our request from the device
679  *		request queue, so that ide.c will service requests from
680  *		the other device on the same interface meanwhile.
681  *
682  *	2.	After the successful initialization of a "media access
683  *		packet command", which is a command which can take a long
684  *		time to complete (it can be several seconds or even an hour).
685  *
686  *		Again, we postpone our request in the middle to free the bus
687  *		for the other device. The polling frequency here should be
688  *		lower than the read/write frequency since those media access
689  *		commands are slow. We start from a "fast" frequency -
690  *		IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
691  *		after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
692  *		lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
693  *
694  *	We also set a timeout for the timer, in case something goes wrong.
695  *	The timeout should be longer then the maximum execution time of a
696  *	tape operation.
697  */
698 
699 /*
700  *	DSC timings.
701  */
702 #define IDETAPE_DSC_RW_MIN		5*HZ/100	/* 50 msec */
703 #define IDETAPE_DSC_RW_MAX		40*HZ/100	/* 400 msec */
704 #define IDETAPE_DSC_RW_TIMEOUT		2*60*HZ		/* 2 minutes */
705 #define IDETAPE_DSC_MA_FAST		2*HZ		/* 2 seconds */
706 #define IDETAPE_DSC_MA_THRESHOLD	5*60*HZ		/* 5 minutes */
707 #define IDETAPE_DSC_MA_SLOW		30*HZ		/* 30 seconds */
708 #define IDETAPE_DSC_MA_TIMEOUT		2*60*60*HZ	/* 2 hours */
709 
710 /*************************** End of tunable parameters ***********************/
711 
712 /*
713  *	Debugging/Performance analysis
714  *
715  *	I/O trace support
716  */
717 #define USE_IOTRACE	0
718 #if USE_IOTRACE
719 #include <linux/io_trace.h>
720 #define IO_IDETAPE_FIFO	500
721 #endif
722 
723 /*
724  *	Read/Write error simulation
725  */
726 #define SIMULATE_ERRORS			0
727 
728 /*
729  *	For general magnetic tape device compatibility.
730  */
731 typedef enum {
732 	idetape_direction_none,
733 	idetape_direction_read,
734 	idetape_direction_write
735 } idetape_chrdev_direction_t;
736 
737 /*
738  *	Our view of a packet command.
739  */
740 typedef struct idetape_packet_command_s {
741 	u8 c[12];				/* Actual packet bytes */
742 	int retries;				/* On each retry, we increment retries */
743 	int error;				/* Error code */
744 	int request_transfer;			/* Bytes to transfer */
745 	int actually_transferred;		/* Bytes actually transferred */
746 	int buffer_size;			/* Size of our data buffer */
747 	struct buffer_head *bh;
748 	char *b_data;
749 	int b_count;
750 	u8 *buffer;				/* Data buffer */
751 	u8 *current_position;			/* Pointer into the above buffer */
752 	ide_startstop_t (*callback) (ide_drive_t *);	/* Called when this packet command is completed */
753 	u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE];	/* Temporary buffer */
754 	unsigned long flags;			/* Status/Action bit flags: long for set_bit */
755 } idetape_pc_t;
756 
757 /*
758  *	Packet command flag bits.
759  */
760 /* Set when an error is considered normal - We won't retry */
761 #define	PC_ABORT			0
762 /* 1 When polling for DSC on a media access command */
763 #define PC_WAIT_FOR_DSC			1
764 /* 1 when we prefer to use DMA if possible */
765 #define PC_DMA_RECOMMENDED		2
766 /* 1 while DMA in progress */
767 #define	PC_DMA_IN_PROGRESS		3
768 /* 1 when encountered problem during DMA */
769 #define	PC_DMA_ERROR			4
770 /* Data direction */
771 #define	PC_WRITING			5
772 
773 /*
774  *	Capabilities and Mechanical Status Page
775  */
776 typedef struct {
777 	unsigned	page_code	:6;	/* Page code - Should be 0x2a */
778 	__u8		reserved0_6	:1;
779 	__u8		ps		:1;	/* parameters saveable */
780 	__u8		page_length;		/* Page Length - Should be 0x12 */
781 	__u8		reserved2, reserved3;
782 	unsigned	ro		:1;	/* Read Only Mode */
783 	unsigned	reserved4_1234	:4;
784 	unsigned	sprev		:1;	/* Supports SPACE in the reverse direction */
785 	unsigned	reserved4_67	:2;
786 	unsigned	reserved5_012	:3;
787 	unsigned	efmt		:1;	/* Supports ERASE command initiated formatting */
788 	unsigned	reserved5_4	:1;
789 	unsigned	qfa		:1;	/* Supports the QFA two partition formats */
790 	unsigned	reserved5_67	:2;
791 	unsigned	lock		:1;	/* Supports locking the volume */
792 	unsigned	locked		:1;	/* The volume is locked */
793 	unsigned	prevent		:1;	/* The device defaults in the prevent state after power up */
794 	unsigned	eject		:1;	/* The device can eject the volume */
795 	__u8		disconnect	:1;	/* The device can break request > ctl */
796 	__u8		reserved6_5	:1;
797 	unsigned	ecc		:1;	/* Supports error correction */
798 	unsigned	cmprs		:1;	/* Supports data compression */
799 	unsigned	reserved7_0	:1;
800 	unsigned	blk512		:1;	/* Supports 512 bytes block size */
801 	unsigned	blk1024		:1;	/* Supports 1024 bytes block size */
802 	unsigned	reserved7_3_6	:4;
803 	unsigned	blk32768	:1;	/* slowb - the device restricts the byte count for PIO */
804 						/* transfers for slow buffer memory ??? */
805 						/* Also 32768 block size in some cases */
806 	__u16		max_speed;		/* Maximum speed supported in KBps */
807 	__u8		reserved10, reserved11;
808 	__u16		ctl;			/* Continuous Transfer Limit in blocks */
809 	__u16		speed;			/* Current Speed, in KBps */
810 	__u16		buffer_size;		/* Buffer Size, in 512 bytes */
811 	__u8		reserved18, reserved19;
812 } idetape_capabilities_page_t;
813 
814 /*
815  *	Block Size Page
816  */
817 typedef struct {
818 	unsigned	page_code	:6;	/* Page code - Should be 0x30 */
819 	unsigned	reserved1_6	:1;
820 	unsigned	ps		:1;
821 	__u8		page_length;		/* Page Length - Should be 2 */
822 	__u8		reserved2;
823 	unsigned	play32		:1;
824 	unsigned	play32_5	:1;
825 	unsigned	reserved2_23	:2;
826 	unsigned	record32	:1;
827 	unsigned	record32_5	:1;
828 	unsigned	reserved2_6	:1;
829 	unsigned	one		:1;
830 } idetape_block_size_page_t;
831 
832 /*
833  *	A pipeline stage.
834  */
835 typedef struct idetape_stage_s {
836 	struct request rq;			/* The corresponding request */
837 	struct buffer_head *bh;			/* The data buffers */
838 	struct idetape_stage_s *next;		/* Pointer to the next stage */
839 	os_aux_t *aux;				/* OnStream aux ptr */
840 } idetape_stage_t;
841 
842 /*
843  *	REQUEST SENSE packet command result - Data Format.
844  */
845 typedef struct {
846 #if defined(__LITTLE_ENDIAN_BITFIELD)
847 	unsigned	error_code	:7;	/* Current of deferred errors */
848 	unsigned	valid		:1;	/* The information field conforms to QIC-157C */
849 	__u8		reserved1	:8;	/* Segment Number - Reserved */
850 	unsigned	sense_key	:4;	/* Sense Key */
851 	unsigned	reserved2_4	:1;	/* Reserved */
852 	unsigned	ili		:1;	/* Incorrect Length Indicator */
853 	unsigned	eom		:1;	/* End Of Medium */
854 	unsigned	filemark 	:1;	/* Filemark */
855 #elif defined(__BIG_ENDIAN_BITFIELD)
856 	unsigned	valid		:1;
857 	unsigned	error_code	:7;
858 	__u8		reserved1	:8;
859 	unsigned	filemark	:1;
860 	unsigned	eom		:1;
861 	unsigned	ili		:1;
862 	unsigned	reserved2_4	:1;
863 	unsigned	sense_key	:4;
864 #else
865 #error "Please fix <asm/byteorder.h>"
866 #endif
867 	__u32		information __attribute__ ((packed));
868 	__u8		asl;			/* Additional sense length (n-7) */
869 	__u32		command_specific;	/* Additional command specific information */
870 	__u8		asc;			/* Additional Sense Code */
871 	__u8		ascq;			/* Additional Sense Code Qualifier */
872 	__u8		replaceable_unit_code;	/* Field Replaceable Unit Code */
873 	unsigned	sk_specific1 	:7;	/* Sense Key Specific */
874 	unsigned	sksv		:1;	/* Sense Key Specific information is valid */
875 	__u8		sk_specific2;		/* Sense Key Specific */
876 	__u8		sk_specific3;		/* Sense Key Specific */
877 	__u8		pad[2];			/* Padding to 20 bytes */
878 } idetape_request_sense_result_t;
879 
880 
881 /*
882  *	Most of our global data which we need to save even as we leave the
883  *	driver due to an interrupt or a timer event is stored in a variable
884  *	of type idetape_tape_t, defined below.
885  */
886 typedef struct {
887 	ide_drive_t *drive;
888 	devfs_handle_t de_r, de_n;
889 
890 	/*
891 	 *	Since a typical character device operation requires more
892 	 *	than one packet command, we provide here enough memory
893 	 *	for the maximum of interconnected packet commands.
894 	 *	The packet commands are stored in the circular array pc_stack.
895 	 *	pc_stack_index points to the last used entry, and warps around
896 	 *	to the start when we get to the last array entry.
897 	 *
898 	 *	pc points to the current processed packet command.
899 	 *
900 	 *	failed_pc points to the last failed packet command, or contains
901 	 *	NULL if we do not need to retry any packet command. This is
902 	 *	required since an additional packet command is needed before the
903 	 *	retry, to get detailed information on what went wrong.
904 	 */
905 	/* Current packet command */
906 	idetape_pc_t *pc;
907 	/* Last failed packet command */
908 	idetape_pc_t *failed_pc;
909 	/* Packet command stack */
910 	idetape_pc_t pc_stack[IDETAPE_PC_STACK];
911 	/* Next free packet command storage space */
912 	int pc_stack_index;
913 	struct request rq_stack[IDETAPE_PC_STACK];
914 	/* We implement a circular array */
915 	int rq_stack_index;
916 
917 	/*
918 	 *	DSC polling variables.
919 	 *
920 	 *	While polling for DSC we use postponed_rq to postpone the
921 	 *	current request so that ide.c will be able to service
922 	 *	pending requests on the other device. Note that at most
923 	 *	we will have only one DSC (usually data transfer) request
924 	 *	in the device request queue. Additional requests can be
925 	 *	queued in our internal pipeline, but they will be visible
926 	 *	to ide.c only one at a time.
927 	 */
928 	struct request *postponed_rq;
929 	/* The time in which we started polling for DSC */
930 	unsigned long dsc_polling_start;
931 	/* Timer used to poll for dsc */
932 	struct timer_list dsc_timer;
933 	/* Read/Write dsc polling frequency */
934 	unsigned long best_dsc_rw_frequency;
935 	/* The current polling frequency */
936 	unsigned long dsc_polling_frequency;
937 	/* Maximum waiting time */
938 	unsigned long dsc_timeout;
939 
940 	/*
941 	 *	Read position information
942 	 */
943 	u8 partition;
944 	/* Current block */
945 	unsigned int first_frame_position;
946 	unsigned int last_frame_position;
947 	unsigned int blocks_in_buffer;
948 
949 	/*
950 	 *	Last error information
951 	 */
952 	u8 sense_key, asc, ascq;
953 
954 	/*
955 	 *	Character device operation
956 	 */
957 	unsigned int minor;
958 	/* device name */
959 	char name[4];
960 	/* Current character device data transfer direction */
961 	idetape_chrdev_direction_t chrdev_direction;
962 
963 	/*
964 	 *	Device information
965 	 */
966 	/* Usually 512 or 1024 bytes */
967 	unsigned short tape_block_size;
968 	int user_bs_factor;
969 	/* Copy of the tape's Capabilities and Mechanical Page */
970 	idetape_capabilities_page_t capabilities;
971 
972 	/*
973 	 *	Active data transfer request parameters.
974 	 *
975 	 *	At most, there is only one ide-tape originated data transfer
976 	 *	request in the device request queue. This allows ide.c to
977 	 *	easily service requests from the other device when we
978 	 *	postpone our active request. In the pipelined operation
979 	 *	mode, we use our internal pipeline structure to hold
980 	 *	more data requests.
981 	 *
982 	 *	The data buffer size is chosen based on the tape's
983 	 *	recommendation.
984 	 */
985 	/* Pointer to the request, waiting in the device request queue */
986 	struct request *active_data_request;
987 	/* Data buffer size (chosen based on the tape's recommendation */
988 	int stage_size;
989 	idetape_stage_t *merge_stage;
990 	int merge_stage_size;
991 	struct buffer_head *bh;
992 	char *b_data;
993 	int b_count;
994 
995 	/*
996 	 *	Pipeline parameters.
997 	 *
998 	 *	To accomplish non-pipelined mode, we simply set the following
999 	 *	variables to zero (or NULL, where appropriate).
1000 	 */
1001 	/* Number of currently used stages */
1002 	int nr_stages;
1003 	/* Number of pending stages */
1004 	int nr_pending_stages;
1005 	/* We will not allocate more than this number of stages */
1006 	int max_stages, min_pipeline, max_pipeline;
1007 	/* The first stage which will be removed from the pipeline */
1008 	idetape_stage_t *first_stage;
1009 	/* The currently active stage */
1010 	idetape_stage_t *active_stage;
1011 	/* Will be serviced after the currently active request */
1012 	idetape_stage_t *next_stage;
1013 	/* New requests will be added to the pipeline here */
1014 	idetape_stage_t *last_stage;
1015 	/* Optional free stage which we can use */
1016 	idetape_stage_t *cache_stage;
1017 	int pages_per_stage;
1018 	/* Wasted space in each stage */
1019 	int excess_bh_size;
1020 
1021 	/* Status/Action flags: long for set_bit */
1022 	unsigned long flags;
1023 	/* protects the ide-tape queue */
1024 	spinlock_t spinlock;
1025 
1026 	/*
1027 	 * Measures average tape speed
1028 	 */
1029 	unsigned long avg_time;
1030 	int avg_size;
1031 	int avg_speed;
1032 
1033 	/* last sense information */
1034 	idetape_request_sense_result_t sense;
1035 
1036 	char vendor_id[10];
1037 	char product_id[18];
1038 	char firmware_revision[6];
1039 	int firmware_revision_num;
1040 
1041 	/* the door is currently locked */
1042 	int door_locked;
1043 
1044 	/*
1045 	 * OnStream flags
1046 	 */
1047 	/* the tape is an OnStream tape */
1048 	int onstream;
1049 	/* OnStream raw access (32.5KB block size) */
1050 	int raw;
1051 	/* current number of frames in internal buffer */
1052 	int cur_frames;
1053 	/* max number of frames in internal buffer */
1054 	int max_frames;
1055 	/* logical block number */
1056 	int logical_blk_num;
1057 	/* write pass counter */
1058 	__u16 wrt_pass_cntr;
1059 	/* update frame counter */
1060 	__u32 update_frame_cntr;
1061 	struct completion *waiting;
1062 	/* write error recovery active */
1063 	int onstream_write_error;
1064 	/* header frame verified ok */
1065 	int header_ok;
1066 	/* reading linux-specifc media */
1067 	int linux_media;
1068 	int linux_media_version;
1069 	/* application signature */
1070 	char application_sig[5];
1071 	int filemark_cnt;
1072 	int first_mark_addr;
1073 	int last_mark_addr;
1074 	int eod_frame_addr;
1075 	unsigned long cmd_start_time;
1076 	unsigned long max_cmd_time;
1077 	unsigned capacity;
1078 
1079 	/*
1080 	 * Optimize the number of "buffer filling"
1081 	 * mode sense commands.
1082 	 */
1083 	/* last time in which we issued fill cmd */
1084 	unsigned long last_buffer_fill;
1085 	/* buffer fill command requested */
1086 	int req_buffer_fill;
1087 	int writes_since_buffer_fill;
1088 	int reads_since_buffer_fill;
1089 
1090 	/*
1091 	 * Limit the number of times a request can
1092 	 * be postponed, to avoid an infinite postpone
1093 	 * deadlock.
1094 	 */
1095 	/* request postpone count limit */
1096 	int postpone_cnt;
1097 
1098 	/*
1099 	 * Measures number of frames:
1100 	 *
1101 	 * 1. written/read to/from the driver pipeline (pipeline_head).
1102 	 * 2. written/read to/from the tape buffers (buffer_head).
1103 	 * 3. written/read by the tape to/from the media (tape_head).
1104 	 */
1105 	int pipeline_head;
1106 	int buffer_head;
1107 	int tape_head;
1108 	int last_tape_head;
1109 
1110 	/*
1111 	 * Speed control at the tape buffers input/output
1112 	 */
1113 	unsigned long insert_time;
1114 	int insert_size;
1115 	int insert_speed;
1116 	int max_insert_speed;
1117 	int measure_insert_time;
1118 
1119 	/*
1120 	 * Measure tape still time, in milliseconds
1121 	 */
1122 	unsigned long tape_still_time_begin;
1123 	int tape_still_time;
1124 
1125 	/*
1126 	 * Speed regulation negative feedback loop
1127 	 */
1128 	int speed_control;
1129 	int pipeline_head_speed;
1130 	int controlled_pipeline_head_speed;
1131 	int uncontrolled_pipeline_head_speed;
1132 	int controlled_last_pipeline_head;
1133 	int uncontrolled_last_pipeline_head;
1134 	unsigned long uncontrolled_pipeline_head_time;
1135 	unsigned long controlled_pipeline_head_time;
1136 	int controlled_previous_pipeline_head;
1137 	int uncontrolled_previous_pipeline_head;
1138 	unsigned long controlled_previous_head_time;
1139 	unsigned long uncontrolled_previous_head_time;
1140 	int restart_speed_control_req;
1141 
1142         /*
1143          * Debug_level determines amount of debugging output;
1144          * can be changed using /proc/ide/hdx/settings
1145          * 0 : almost no debugging output
1146          * 1 : 0+output errors only
1147          * 2 : 1+output all sensekey/asc
1148          * 3 : 2+follow all chrdev related procedures
1149          * 4 : 3+follow all procedures
1150          * 5 : 4+include pc_stack rq_stack info
1151          * 6 : 5+USE_COUNT updates
1152          */
1153          int debug_level;
1154 } idetape_tape_t;
1155 
1156 /*
1157  *	Tape door status
1158  */
1159 #define DOOR_UNLOCKED			0
1160 #define DOOR_LOCKED			1
1161 #define DOOR_EXPLICITLY_LOCKED		2
1162 
1163 /*
1164  *	Tape flag bits values.
1165  */
1166 #define IDETAPE_IGNORE_DSC		0
1167 #define IDETAPE_ADDRESS_VALID		1	/* 0 When the tape position is unknown */
1168 #define IDETAPE_BUSY			2	/* Device already opened */
1169 #define IDETAPE_PIPELINE_ERROR		3	/* Error detected in a pipeline stage */
1170 #define IDETAPE_DETECT_BS		4	/* Attempt to auto-detect the current user block size */
1171 #define IDETAPE_FILEMARK		5	/* Currently on a filemark */
1172 #define IDETAPE_DRQ_INTERRUPT		6	/* DRQ interrupt device */
1173 #define IDETAPE_READ_ERROR		7
1174 #define IDETAPE_PIPELINE_ACTIVE		8	/* pipeline active */
1175 
1176 /*
1177  *	Supported ATAPI tape drives packet commands
1178  */
1179 #define IDETAPE_TEST_UNIT_READY_CMD	0x00
1180 #define IDETAPE_REWIND_CMD		0x01
1181 #define IDETAPE_REQUEST_SENSE_CMD	0x03
1182 #define IDETAPE_READ_CMD		0x08
1183 #define IDETAPE_WRITE_CMD		0x0a
1184 #define IDETAPE_WRITE_FILEMARK_CMD	0x10
1185 #define IDETAPE_SPACE_CMD		0x11
1186 #define IDETAPE_INQUIRY_CMD		0x12
1187 #define IDETAPE_ERASE_CMD		0x19
1188 #define IDETAPE_MODE_SENSE_CMD		0x1a
1189 #define IDETAPE_MODE_SELECT_CMD		0x15
1190 #define IDETAPE_LOAD_UNLOAD_CMD		0x1b
1191 #define IDETAPE_PREVENT_CMD		0x1e
1192 #define IDETAPE_LOCATE_CMD		0x2b
1193 #define IDETAPE_READ_POSITION_CMD	0x34
1194 #define IDETAPE_READ_BUFFER_CMD		0x3c
1195 #define IDETAPE_SET_SPEED_CMD		0xbb
1196 
1197 /*
1198  *	Some defines for the READ BUFFER command
1199  */
1200 #define IDETAPE_RETRIEVE_FAULTY_BLOCK	6
1201 
1202 /*
1203  *	Some defines for the SPACE command
1204  */
1205 #define IDETAPE_SPACE_OVER_FILEMARK	1
1206 #define IDETAPE_SPACE_TO_EOD		3
1207 
1208 /*
1209  *	Some defines for the LOAD UNLOAD command
1210  */
1211 #define IDETAPE_LU_LOAD_MASK		1
1212 #define IDETAPE_LU_RETENSION_MASK	2
1213 #define IDETAPE_LU_EOT_MASK		4
1214 
1215 /*
1216  *	Special requests for our block device strategy routine.
1217  *
1218  *	In order to service a character device command, we add special
1219  *	requests to the tail of our block device request queue and wait
1220  *	for their completion.
1221  *
1222  */
1223 #define IDETAPE_FIRST_RQ		90
1224 
1225 /*
1226  * 	IDETAPE_PC_RQ is used to queue a packet command in the request queue.
1227  */
1228 #define IDETAPE_PC_RQ1			90
1229 #define IDETAPE_PC_RQ2			91
1230 
1231 /*
1232  *	IDETAPE_READ_RQ and IDETAPE_WRITE_RQ are used by our
1233  *	character device interface to request read/write operations from
1234  *	our block device interface.
1235  */
1236 #define IDETAPE_READ_RQ			92
1237 #define IDETAPE_WRITE_RQ		93
1238 #define IDETAPE_ABORTED_WRITE_RQ	94
1239 #define IDETAPE_ABORTED_READ_RQ		95
1240 #define IDETAPE_READ_BUFFER_RQ		96
1241 
1242 #define IDETAPE_LAST_RQ			96
1243 
1244 /*
1245  *	A macro which can be used to check if a we support a given
1246  *	request command.
1247  */
1248 #define IDETAPE_RQ_CMD(cmd) 		((cmd >= IDETAPE_FIRST_RQ) && (cmd <= IDETAPE_LAST_RQ))
1249 
1250 /*
1251  *	Error codes which are returned in rq->errors to the higher part
1252  *	of the driver.
1253  */
1254 #define	IDETAPE_ERROR_GENERAL		101
1255 #define	IDETAPE_ERROR_FILEMARK		102
1256 #define	IDETAPE_ERROR_EOD		103
1257 
1258 /*
1259  *	idetape_chrdev_t provides the link between out character device
1260  *	interface and our block device interface and the corresponding
1261  *	ide_drive_t structure.
1262  */
1263 typedef struct {
1264 	ide_drive_t *drive;
1265 } idetape_chrdev_t;
1266 
1267 /*
1268  *	The following is used to format the general configuration word of
1269  *	the ATAPI IDENTIFY DEVICE command.
1270  */
1271 struct idetape_id_gcw {
1272 	unsigned packet_size		:2;	/* Packet Size */
1273 	unsigned reserved234		:3;	/* Reserved */
1274 	unsigned drq_type		:2;	/* Command packet DRQ type */
1275 	unsigned removable		:1;	/* Removable media */
1276 	unsigned device_type		:5;	/* Device type */
1277 	unsigned reserved13		:1;	/* Reserved */
1278 	unsigned protocol		:2;	/* Protocol type */
1279 };
1280 
1281 /*
1282  *	INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
1283  */
1284 typedef struct {
1285 	unsigned	device_type	:5;	/* Peripheral Device Type */
1286 	unsigned	reserved0_765	:3;	/* Peripheral Qualifier - Reserved */
1287 	unsigned	reserved1_6t0	:7;	/* Reserved */
1288 	unsigned	rmb		:1;	/* Removable Medium Bit */
1289 	unsigned	ansi_version	:3;	/* ANSI Version */
1290 	unsigned	ecma_version	:3;	/* ECMA Version */
1291 	unsigned	iso_version	:2;	/* ISO Version */
1292 	unsigned	response_format :4;	/* Response Data Format */
1293 	unsigned	reserved3_45	:2;	/* Reserved */
1294 	unsigned	reserved3_6	:1;	/* TrmIOP - Reserved */
1295 	unsigned	reserved3_7	:1;	/* AENC - Reserved */
1296 	__u8		additional_length;	/* Additional Length (total_length-4) */
1297 	__u8		rsv5, rsv6, rsv7;	/* Reserved */
1298 	__u8		vendor_id[8];		/* Vendor Identification */
1299 	__u8		product_id[16];		/* Product Identification */
1300 	__u8		revision_level[4];	/* Revision Level */
1301 	__u8		vendor_specific[20];	/* Vendor Specific - Optional */
1302 	__u8		reserved56t95[40];	/* Reserved - Optional */
1303 						/* Additional information may be returned */
1304 } idetape_inquiry_result_t;
1305 
1306 /*
1307  *	READ POSITION packet command - Data Format (From Table 6-57)
1308  */
1309 typedef struct {
1310 	unsigned	reserved0_10	:2;	/* Reserved */
1311 	unsigned	bpu		:1;	/* Block Position Unknown */
1312 	unsigned	reserved0_543	:3;	/* Reserved */
1313 	unsigned	eop		:1;	/* End Of Partition */
1314 	unsigned	bop		:1;	/* Beginning Of Partition */
1315 	u8		partition;		/* Partition Number */
1316 	u8		reserved2, reserved3;	/* Reserved */
1317 	u32		first_block;		/* First Block Location */
1318 	u32		last_block;		/* Last Block Location (Optional) */
1319 	u8		reserved12;		/* Reserved */
1320 	u8		blocks_in_buffer[3];	/* Blocks In Buffer - (Optional) */
1321 	u32		bytes_in_buffer;	/* Bytes In Buffer (Optional) */
1322 } idetape_read_position_result_t;
1323 
1324 /*
1325  *	Follows structures which are related to the SELECT SENSE / MODE SENSE
1326  *	packet commands. Those packet commands are still not supported
1327  *	by ide-tape.
1328  */
1329 #define IDETAPE_BLOCK_DESCRIPTOR	0
1330 #define	IDETAPE_CAPABILITIES_PAGE	0x2a
1331 #define IDETAPE_PARAMTR_PAGE		0x2b   /* Onstream DI-x0 only */
1332 #define IDETAPE_BLOCK_SIZE_PAGE		0x30
1333 #define IDETAPE_BUFFER_FILLING_PAGE	0x33
1334 
1335 /*
1336  *	Mode Parameter Header for the MODE SENSE packet command
1337  */
1338 typedef struct {
1339 	__u8	mode_data_length;	/* Length of the following data transfer */
1340 	__u8	medium_type;		/* Medium Type */
1341 	__u8	dsp;			/* Device Specific Parameter */
1342 	__u8	bdl;			/* Block Descriptor Length */
1343 #if 0
1344 	/* data transfer page */
1345 	__u8	page_code	:6;
1346 	__u8	reserved0_6	:1;
1347 	__u8	ps		:1;	/* parameters saveable */
1348 	__u8	page_length;		/* page Length == 0x02 */
1349 	__u8	reserved2;
1350 	__u8	read32k		:1;	/* 32k blk size (data only) */
1351 	__u8	read32k5	:1;	/* 32.5k blk size (data&AUX) */
1352 	__u8	reserved3_23	:2;
1353 	__u8	write32k	:1;	/* 32k blk size (data only) */
1354 	__u8	write32k5	:1;	/* 32.5k blk size (data&AUX) */
1355 	__u8	reserved3_6	:1;
1356 	__u8	streaming	:1;	/* streaming mode enable */
1357 #endif
1358 } idetape_mode_parameter_header_t;
1359 
1360 /*
1361  *	Mode Parameter Block Descriptor the MODE SENSE packet command
1362  *
1363  *	Support for block descriptors is optional.
1364  */
1365 typedef struct {
1366 	__u8		density_code;		/* Medium density code */
1367 	__u8		blocks[3];		/* Number of blocks */
1368 	__u8		reserved4;		/* Reserved */
1369 	__u8		length[3];		/* Block Length */
1370 } idetape_parameter_block_descriptor_t;
1371 
1372 /*
1373  *	The Data Compression Page, as returned by the MODE SENSE packet command.
1374  */
1375 typedef struct {
1376 	unsigned	page_code	:6;	/* Page Code - Should be 0xf */
1377 	unsigned	reserved0	:1;	/* Reserved */
1378 	unsigned	ps		:1;
1379 	__u8		page_length;		/* Page Length - Should be 14 */
1380 	unsigned	reserved2	:6;	/* Reserved */
1381 	unsigned	dcc		:1;	/* Data Compression Capable */
1382 	unsigned	dce		:1;	/* Data Compression Enable */
1383 	unsigned	reserved3	:5;	/* Reserved */
1384 	unsigned	red		:2;	/* Report Exception on Decompression */
1385 	unsigned	dde		:1;	/* Data Decompression Enable */
1386 	__u32		ca;			/* Compression Algorithm */
1387 	__u32		da;			/* Decompression Algorithm */
1388 	__u8		reserved[4];		/* Reserved */
1389 } idetape_data_compression_page_t;
1390 
1391 /*
1392  *	The Medium Partition Page, as returned by the MODE SENSE packet command.
1393  */
1394 typedef struct {
1395 	unsigned	page_code	:6;	/* Page Code - Should be 0x11 */
1396 	unsigned	reserved1_6	:1;	/* Reserved */
1397 	unsigned	ps		:1;
1398 	__u8		page_length;		/* Page Length - Should be 6 */
1399 	__u8		map;			/* Maximum Additional Partitions - Should be 0 */
1400 	__u8		apd;			/* Additional Partitions Defined - Should be 0 */
1401 	unsigned	reserved4_012	:3;	/* Reserved */
1402 	unsigned	psum		:2;	/* Should be 0 */
1403 	unsigned	idp		:1;	/* Should be 0 */
1404 	unsigned	sdp		:1;	/* Should be 0 */
1405 	unsigned	fdp		:1;	/* Fixed Data Partitions */
1406 	__u8		mfr;			/* Medium Format Recognition */
1407 	__u8		reserved[2];		/* Reserved */
1408 } idetape_medium_partition_page_t;
1409 
1410 /*
1411  *	Run time configurable parameters.
1412  */
1413 typedef struct {
1414 	int	dsc_rw_frequency;
1415 	int	dsc_media_access_frequency;
1416 	int	nr_stages;
1417 } idetape_config_t;
1418 
1419 /*
1420  *	The variables below are used for the character device interface.
1421  *	Additional state variables are defined in our ide_drive_t structure.
1422  */
1423 static idetape_chrdev_t idetape_chrdevs[MAX_HWIFS * MAX_DRIVES];
1424 static int idetape_chrdev_present = 0;
1425 
1426 #if IDETAPE_DEBUG_LOG_VERBOSE
1427 
1428 /*
1429  * DO NOT REMOVE, BUILDING A VERBOSE DEBUG SCHEME FOR ATAPI
1430  */
1431 
idetape_sense_key_verbose(u8 idetape_sense_key)1432 char *idetape_sense_key_verbose (u8 idetape_sense_key)
1433 {
1434 	switch (idetape_sense_key) {
1435 		default: {
1436 			char buf[22];
1437 			sprintf(buf, "IDETAPE_SENSE (0x%02x)",
1438 				idetape_sense_key);
1439 			return(buf);
1440 		}
1441 
1442 	}
1443 }
1444 
idetape_command_key_verbose(u8 idetape_command_key)1445 char *idetape_command_key_verbose (u8 idetape_command_key)
1446 {
1447 	switch (idetape_command_key) {
1448 		case IDETAPE_TEST_UNIT_READY_CMD:
1449 			return("TEST_UNIT_READY_CMD");
1450 		case IDETAPE_REWIND_CMD:
1451 			return("REWIND_CMD");
1452 		case IDETAPE_REQUEST_SENSE_CMD:
1453 			return("REQUEST_SENSE_CMD");
1454 		case IDETAPE_READ_CMD:
1455 			return("READ_CMD");
1456 		case IDETAPE_WRITE_CMD:
1457 			return("WRITE_CMD");
1458 		case IDETAPE_WRITE_FILEMARK_CMD:
1459 			return("WRITE_FILEMARK_CMD");
1460 		case IDETAPE_SPACE_CMD:
1461 			return("SPACE_CMD");
1462 		case IDETAPE_INQUIRY_CMD:
1463 			return("INQUIRY_CMD");
1464 		case IDETAPE_ERASE_CMD:
1465 			return("ERASE_CMD");
1466 		case IDETAPE_MODE_SENSE_CMD:
1467 			return("MODE_SENSE_CMD");
1468 		case IDETAPE_MODE_SELECT_CMD:
1469 			return("MODE_SELECT_CMD");
1470 		case IDETAPE_LOAD_UNLOAD_CMD:
1471 			return("LOAD_UNLOAD_CMD");
1472 		case IDETAPE_PREVENT_CMD:
1473 			return("PREVENT_CMD");
1474 		case IDETAPE_LOCATE_CMD:
1475 			return("LOCATE_CMD");
1476 		case IDETAPE_READ_POSITION_CMD:
1477 			return("READ_POSITION_CMD");
1478 		case IDETAPE_READ_BUFFER_CMD:
1479 			return("READ_BUFFER_CMD");
1480 		case IDETAPE_SET_SPEED_CMD:
1481 			return("SET_SPEED_CMD");
1482 		default: {
1483 				char buf[20];
1484 				sprintf(buf, "CMD (0x%02x)",
1485 					idetape_command_key);
1486 				return(buf);
1487 			}
1488 	}
1489 }
1490 #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
1491 
1492 /*
1493  *      Function declarations
1494  *
1495  */
1496 static void idetape_onstream_mode_sense_tape_parameter_page(ide_drive_t *drive, int debug);
1497 static int idetape_chrdev_release (struct inode *inode, struct file *filp);
1498 static void idetape_write_release (struct inode *inode);
1499 
1500 /*
1501  * Too bad. The drive wants to send us data which we are not ready to accept.
1502  * Just throw it away.
1503  */
idetape_discard_data(ide_drive_t * drive,unsigned int bcount)1504 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1505 {
1506 	while (bcount--)
1507 		(void) HWIF(drive)->INB(IDE_DATA_REG);
1508 }
1509 
idetape_input_buffers(ide_drive_t * drive,idetape_pc_t * pc,unsigned int bcount)1510 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1511 {
1512 	struct buffer_head *bh = pc->bh;
1513 	int count;
1514 
1515 	while (bcount) {
1516 #if IDETAPE_DEBUG_BUGS
1517 		if (bh == NULL) {
1518 			printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1519 				__FUNCTION__);
1520 			idetape_discard_data(drive, bcount);
1521 			return;
1522 		}
1523 #endif /* IDETAPE_DEBUG_BUGS */
1524 		count = IDE_MIN(bh->b_size - atomic_read(&bh->b_count), bcount);
1525 		HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
1526 		bcount -= count;
1527 		atomic_add(count, &bh->b_count);
1528 		if (atomic_read(&bh->b_count) == bh->b_size) {
1529 			bh = bh->b_reqnext;
1530 			if (bh)
1531 				atomic_set(&bh->b_count, 0);
1532 		}
1533 	}
1534 	pc->bh = bh;
1535 }
1536 
idetape_output_buffers(ide_drive_t * drive,idetape_pc_t * pc,unsigned int bcount)1537 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1538 {
1539 	struct buffer_head *bh = pc->bh;
1540 	int count;
1541 
1542 	while (bcount) {
1543 #if IDETAPE_DEBUG_BUGS
1544 		if (bh == NULL) {
1545 			printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1546 				__FUNCTION__);
1547 			return;
1548 		}
1549 #endif /* IDETAPE_DEBUG_BUGS */
1550 		count = IDE_MIN(pc->b_count, bcount);
1551 		HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
1552 		bcount -= count;
1553 		pc->b_data += count;
1554 		pc->b_count -= count;
1555 		if (!pc->b_count) {
1556 			pc->bh = bh = bh->b_reqnext;
1557 			if (bh) {
1558 				pc->b_data = bh->b_data;
1559 				pc->b_count = atomic_read(&bh->b_count);
1560 			}
1561 		}
1562 	}
1563 }
1564 
1565 #ifdef CONFIG_BLK_DEV_IDEDMA
idetape_update_buffers(idetape_pc_t * pc)1566 static void idetape_update_buffers (idetape_pc_t *pc)
1567 {
1568 	struct buffer_head *bh = pc->bh;
1569 	int count, bcount = pc->actually_transferred;
1570 
1571 	if (test_bit(PC_WRITING, &pc->flags))
1572 		return;
1573 	while (bcount) {
1574 #if IDETAPE_DEBUG_BUGS
1575 		if (bh == NULL) {
1576 			printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1577 				__FUNCTION__);
1578 			return;
1579 		}
1580 #endif /* IDETAPE_DEBUG_BUGS */
1581 		count = IDE_MIN(bh->b_size, bcount);
1582 		atomic_set(&bh->b_count, count);
1583 		if (atomic_read(&bh->b_count) == bh->b_size)
1584 			bh = bh->b_reqnext;
1585 		bcount -= count;
1586 	}
1587 	pc->bh = bh;
1588 }
1589 #endif /* CONFIG_BLK_DEV_IDEDMA */
1590 
1591 /*
1592  *	idetape_next_pc_storage returns a pointer to a place in which we can
1593  *	safely store a packet command, even though we intend to leave the
1594  *	driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1595  *	commands is allocated at initialization time.
1596  */
idetape_next_pc_storage(ide_drive_t * drive)1597 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1598 {
1599 	idetape_tape_t *tape = drive->driver_data;
1600 
1601 #if IDETAPE_DEBUG_LOG
1602 	if (tape->debug_level >= 5)
1603 		printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
1604 			tape->pc_stack_index);
1605 #endif /* IDETAPE_DEBUG_LOG */
1606 	if (tape->pc_stack_index == IDETAPE_PC_STACK)
1607 		tape->pc_stack_index=0;
1608 	return (&tape->pc_stack[tape->pc_stack_index++]);
1609 }
1610 
1611 /*
1612  *	idetape_next_rq_storage is used along with idetape_next_pc_storage.
1613  *	Since we queue packet commands in the request queue, we need to
1614  *	allocate a request, along with the allocation of a packet command.
1615  */
1616 
1617 /**************************************************************
1618  *                                                            *
1619  *  This should get fixed to use kmalloc(.., GFP_ATOMIC)      *
1620  *  followed later on by kfree().   -ml                       *
1621  *                                                            *
1622  **************************************************************/
1623 
idetape_next_rq_storage(ide_drive_t * drive)1624 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1625 {
1626 	idetape_tape_t *tape = drive->driver_data;
1627 
1628 #if IDETAPE_DEBUG_LOG
1629 	if (tape->debug_level >= 5)
1630 		printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
1631 			tape->rq_stack_index);
1632 #endif /* IDETAPE_DEBUG_LOG */
1633 	if (tape->rq_stack_index == IDETAPE_PC_STACK)
1634 		tape->rq_stack_index=0;
1635 	return (&tape->rq_stack[tape->rq_stack_index++]);
1636 }
1637 
1638 /*
1639  *	idetape_init_pc initializes a packet command.
1640  */
idetape_init_pc(idetape_pc_t * pc)1641 static void idetape_init_pc (idetape_pc_t *pc)
1642 {
1643 	memset(pc->c, 0, 12);
1644 	pc->retries = 0;
1645 	pc->flags = 0;
1646 	pc->request_transfer = 0;
1647 	pc->buffer = pc->pc_buffer;
1648 	pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1649 	pc->bh = NULL;
1650 	pc->b_data = NULL;
1651 }
1652 
1653 /*
1654  *	idetape_analyze_error is called on each failed packet command retry
1655  *	to analyze the request sense. We currently do not utilize this
1656  *	information.
1657  */
idetape_analyze_error(ide_drive_t * drive,idetape_request_sense_result_t * result)1658 static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
1659 {
1660 	idetape_tape_t *tape = drive->driver_data;
1661 	idetape_pc_t *pc = tape->failed_pc;
1662 
1663 	tape->sense     = *result;
1664 	tape->sense_key = result->sense_key;
1665 	tape->asc       = result->asc;
1666 	tape->ascq      = result->ascq;
1667 #if IDETAPE_DEBUG_LOG
1668 	/*
1669 	 *	Without debugging, we only log an error if we decided to
1670 	 *	give up retrying.
1671 	 */
1672 	if (tape->debug_level >= 1)
1673 		printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1674 			"asc = %x, ascq = %x\n",
1675 			pc->c[0], result->sense_key,
1676 			result->asc, result->ascq);
1677 #if IDETAPE_DEBUG_LOG_VERBOSE
1678 	if (tape->debug_level >= 1)
1679 		printk (KERN_INFO "ide-tape: pc = %s, sense key = %x, "
1680 			"asc = %x, ascq = %x\n",
1681 			idetape_command_key_verbose((u8) pc->c[0]),
1682 			result->sense_key,
1683 			result->asc,
1684 			result->ascq);
1685 #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
1686 #endif /* IDETAPE_DEBUG_LOG */
1687 
1688 	if (tape->onstream && result->sense_key == 2 &&
1689 	    result->asc == 0x53 && result->ascq == 2) {
1690 		clear_bit(PC_DMA_ERROR, &pc->flags);
1691 		ide_stall_queue(drive, HZ / 2);
1692 		return;
1693 	}
1694 #ifdef CONFIG_BLK_DEV_IDEDMA
1695 
1696 	/*
1697 	 *	Correct pc->actually_transferred by asking the tape.
1698 	 */
1699 	if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1700 		pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned (&result->information));
1701 		idetape_update_buffers(pc);
1702 	}
1703 #endif /* CONFIG_BLK_DEV_IDEDMA */
1704 	if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1705 		pc->error = IDETAPE_ERROR_FILEMARK;
1706 		set_bit(PC_ABORT, &pc->flags);
1707 	}
1708 	if (pc->c[0] == IDETAPE_WRITE_CMD) {
1709 		if (result->eom || (result->sense_key == 0xd &&
1710 		    result->asc == 0x0 && result->ascq == 0x2)) {
1711 			pc->error = IDETAPE_ERROR_EOD;
1712 			set_bit(PC_ABORT, &pc->flags);
1713 		}
1714 	}
1715 	if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1716 		if (result->sense_key == 8) {
1717 			pc->error = IDETAPE_ERROR_EOD;
1718 			set_bit(PC_ABORT, &pc->flags);
1719 		}
1720 		if (!test_bit(PC_ABORT, &pc->flags) &&
1721 		    (tape->onstream || pc->actually_transferred))
1722 			pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1723 	}
1724 }
1725 
1726 /*
1727  *	idetape_active_next_stage will declare the next stage as "active".
1728  */
idetape_active_next_stage(ide_drive_t * drive)1729 static void idetape_active_next_stage (ide_drive_t *drive)
1730 {
1731 	idetape_tape_t *tape = drive->driver_data;
1732 	idetape_stage_t *stage = tape->next_stage;
1733 	struct request *rq = &stage->rq;
1734 
1735 #if IDETAPE_DEBUG_LOG
1736 	if (tape->debug_level >= 4)
1737 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
1738 #endif /* IDETAPE_DEBUG_LOG */
1739 #if IDETAPE_DEBUG_BUGS
1740 	if (stage == NULL) {
1741 		printk(KERN_ERR "ide-tape: bug: Trying to activate a "
1742 			"non existing stage\n");
1743 		return;
1744 	}
1745 #endif /* IDETAPE_DEBUG_BUGS */
1746 
1747 	rq->buffer = NULL;
1748 	rq->bh = stage->bh;
1749 	tape->active_data_request = rq;
1750 	tape->active_stage = stage;
1751 	tape->next_stage = stage->next;
1752 }
1753 
1754 /*
1755  *	idetape_increase_max_pipeline_stages is a part of the feedback
1756  *	loop which tries to find the optimum number of stages. In the
1757  *	feedback loop, we are starting from a minimum maximum number of
1758  *	stages, and if we sense that the pipeline is empty, we try to
1759  *	increase it, until we reach the user compile time memory limit.
1760  */
idetape_increase_max_pipeline_stages(ide_drive_t * drive)1761 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1762 {
1763 	idetape_tape_t *tape = drive->driver_data;
1764 	int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1765 
1766 #if IDETAPE_DEBUG_LOG
1767 	if (tape->debug_level >= 4)
1768 		printk (KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
1769 #endif /* IDETAPE_DEBUG_LOG */
1770 
1771 	tape->max_stages += IDE_MAX(increase, 1);
1772 	tape->max_stages = IDE_MAX(tape->max_stages, tape->min_pipeline);
1773 	tape->max_stages = IDE_MIN(tape->max_stages, tape->max_pipeline);
1774 }
1775 
1776 /*
1777  *	idetape_kfree_stage calls kfree to completely free a stage, along with
1778  *	its related buffers.
1779  */
__idetape_kfree_stage(idetape_stage_t * stage)1780 static void __idetape_kfree_stage (idetape_stage_t *stage)
1781 {
1782 	struct buffer_head *prev_bh, *bh = stage->bh;
1783 	int size;
1784 
1785 	while (bh != NULL) {
1786 		if (bh->b_data != NULL) {
1787 			size = (int) bh->b_size;
1788 			while (size > 0) {
1789 				free_page((unsigned long) bh->b_data);
1790 				size -= PAGE_SIZE;
1791 				bh->b_data += PAGE_SIZE;
1792 			}
1793 		}
1794 		prev_bh = bh;
1795 		bh = bh->b_reqnext;
1796 		kfree(prev_bh);
1797 	}
1798 	kfree(stage);
1799 }
1800 
idetape_kfree_stage(idetape_tape_t * tape,idetape_stage_t * stage)1801 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1802 {
1803 	__idetape_kfree_stage(stage);
1804 }
1805 
1806 /*
1807  *	idetape_remove_stage_head removes tape->first_stage from the pipeline.
1808  *	The caller should avoid race conditions.
1809  */
idetape_remove_stage_head(ide_drive_t * drive)1810 static void idetape_remove_stage_head (ide_drive_t *drive)
1811 {
1812 	idetape_tape_t *tape = drive->driver_data;
1813 	idetape_stage_t *stage;
1814 
1815 #if IDETAPE_DEBUG_LOG
1816 	if (tape->debug_level >= 4)
1817 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
1818 #endif /* IDETAPE_DEBUG_LOG */
1819 #if IDETAPE_DEBUG_BUGS
1820 	if (tape->first_stage == NULL) {
1821 		printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1822 		return;
1823 	}
1824 	if (tape->active_stage == tape->first_stage) {
1825 		printk(KERN_ERR "ide-tape: bug: Trying to free our "
1826 			"active pipeline stage\n");
1827 		return;
1828 	}
1829 #endif /* IDETAPE_DEBUG_BUGS */
1830 	stage = tape->first_stage;
1831 	tape->first_stage = stage->next;
1832 	idetape_kfree_stage(tape, stage);
1833 	tape->nr_stages--;
1834 	if (tape->first_stage == NULL) {
1835 		tape->last_stage = NULL;
1836 #if IDETAPE_DEBUG_BUGS
1837 		if (tape->next_stage != NULL)
1838 			printk(KERN_ERR "ide-tape: bug: "
1839 				"tape->next_stage != NULL\n");
1840 		if (tape->nr_stages)
1841 			printk(KERN_ERR "ide-tape: bug: nr_stages should "
1842 				"be 0 now\n");
1843 #endif /* IDETAPE_DEBUG_BUGS */
1844 	}
1845 }
1846 
idetape_abort_pipeline(ide_drive_t * drive,idetape_stage_t * last_stage)1847 static void idetape_abort_pipeline (ide_drive_t *drive, idetape_stage_t *last_stage)
1848 {
1849 	idetape_tape_t *tape = drive->driver_data;
1850 	idetape_stage_t *stage = tape->next_stage;
1851 	idetape_stage_t *nstage;
1852 
1853 #if IDETAPE_DEBUG_LOG
1854 	if (tape->debug_level >= 4)
1855 		printk(KERN_INFO "ide-tape: %s: %s called\n",
1856 			tape->name, __FUNCTION__);
1857 #endif
1858 	while (stage) {
1859 		nstage = stage->next;
1860 		idetape_kfree_stage(tape, stage);
1861 		--tape->nr_stages;
1862 		--tape->nr_pending_stages;
1863 		stage = nstage;
1864 	}
1865 	tape->last_stage = last_stage;
1866 	if (last_stage)
1867 		last_stage->next = NULL;
1868 	tape->next_stage = NULL;
1869 }
1870 
1871 /*
1872  *	idetape_end_request is used to finish servicing a request, and to
1873  *	insert a pending pipeline request into the main device queue.
1874  */
idetape_end_request(ide_drive_t * drive,int uptodate)1875 static int idetape_end_request (ide_drive_t *drive, int uptodate)
1876 {
1877 	struct request *rq = HWGROUP(drive)->rq;
1878 	idetape_tape_t *tape = drive->driver_data;
1879 	unsigned long flags;
1880 	int error;
1881 	int remove_stage = 0;
1882 	idetape_stage_t *active_stage;
1883 #if ONSTREAM_DEBUG
1884 	idetape_stage_t *stage;
1885 	os_aux_t *aux;
1886 	unsigned char *p;
1887 #endif
1888 
1889 #if IDETAPE_DEBUG_LOG
1890         if (tape->debug_level >= 4)
1891 		printk (KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
1892 #endif /* IDETAPE_DEBUG_LOG */
1893 
1894 	switch (uptodate) {
1895 		case 0:	error = IDETAPE_ERROR_GENERAL; break;
1896 		case 1: error = 0; break;
1897 		default: error = uptodate;
1898 	}
1899 	rq->errors = error;
1900 	if (error)
1901 		tape->failed_pc = NULL;
1902 
1903 	spin_lock_irqsave(&tape->spinlock, flags);
1904 	if (tape->active_data_request == rq) {
1905 		/* The request was a pipelined data transfer request */
1906 		active_stage = tape->active_stage;
1907 		tape->active_stage = NULL;
1908 		tape->active_data_request = NULL;
1909 		tape->nr_pending_stages--;
1910 		if (rq->cmd == IDETAPE_WRITE_RQ) {
1911 #if ONSTREAM_DEBUG
1912 			if (tape->debug_level >= 2) {
1913 				if (tape->onstream) {
1914 					stage = tape->first_stage;
1915 					aux = stage->aux;
1916 					p = stage->bh->b_data;
1917 					if (ntohl(aux->logical_blk_num) < 11300 && ntohl(aux->logical_blk_num) > 11100)
1918 						printk(KERN_INFO "ide-tape: finished writing logical blk %u (data %x %x %x %x)\n", ntohl(aux->logical_blk_num), *p++, *p++, *p++, *p++);
1919 				}
1920 			}
1921 #endif
1922 			if (tape->onstream && !tape->raw) {
1923 				if (tape->first_frame_position == OS_DATA_ENDFRAME1) {
1924 #if ONSTREAM_DEBUG
1925 					if (tape->debug_level >= 2)
1926 						printk("ide-tape: %s: skipping over config partition.\n", tape->name);
1927 #endif
1928 					tape->onstream_write_error = OS_PART_ERROR;
1929 					if (tape->waiting) {
1930 						rq->waiting = NULL;
1931 						complete(tape->waiting);
1932 					}
1933 				}
1934 			}
1935 			remove_stage = 1;
1936 			if (error) {
1937 				set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1938 				if (error == IDETAPE_ERROR_EOD)
1939 					idetape_abort_pipeline(drive, active_stage);
1940 				if (tape->onstream && !tape->raw &&
1941 				    error == IDETAPE_ERROR_GENERAL &&
1942 				    tape->sense.sense_key == 3) {
1943 					clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1944 					printk(KERN_ERR "ide-tape: %s: write error, enabling error recovery\n", tape->name);
1945 					tape->onstream_write_error = OS_WRITE_ERROR;
1946 					remove_stage = 0;
1947 					tape->nr_pending_stages++;
1948 					tape->next_stage = tape->first_stage;
1949 					rq->current_nr_sectors = rq->nr_sectors;
1950 					if (tape->waiting) {
1951 						rq->waiting = NULL;
1952 						complete(tape->waiting);
1953 					}
1954 				}
1955 			}
1956 		} else if (rq->cmd == IDETAPE_READ_RQ) {
1957 			if (error == IDETAPE_ERROR_EOD) {
1958 				set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1959 				idetape_abort_pipeline(drive, active_stage);
1960 			}
1961 		}
1962 		if (tape->next_stage != NULL && !tape->onstream_write_error) {
1963 			idetape_active_next_stage (drive);
1964 
1965 			/*
1966 			 *	Insert the next request into the request queue.
1967 			 */
1968 			(void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1969 		} else if (!error) {
1970 			if (!tape->onstream)
1971 				idetape_increase_max_pipeline_stages(drive);
1972 		}
1973 	}
1974 	ide_end_drive_cmd(drive, 0, 0);
1975 	if (remove_stage)
1976 		idetape_remove_stage_head(drive);
1977 	if (tape->active_data_request == NULL)
1978 		clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1979 	spin_unlock_irqrestore(&tape->spinlock, flags);
1980 	return 0;
1981 }
1982 
idetape_request_sense_callback(ide_drive_t * drive)1983 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1984 {
1985 	idetape_tape_t *tape = drive->driver_data;
1986 
1987 #if IDETAPE_DEBUG_LOG
1988 	if (tape->debug_level >= 4)
1989 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
1990 #endif /* IDETAPE_DEBUG_LOG */
1991 	if (!tape->pc->error) {
1992 		idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
1993 		idetape_end_request(drive, 1);
1994 	} else {
1995 		printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
1996 			"itself - Aborting request!\n");
1997 		idetape_end_request(drive, 0);
1998 	}
1999 	return ide_stopped;
2000 }
2001 
idetape_create_request_sense_cmd(idetape_pc_t * pc)2002 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
2003 {
2004 	idetape_init_pc(pc);
2005 	pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
2006 	pc->c[4] = 20;
2007 	pc->request_transfer = 20;
2008 	pc->callback = &idetape_request_sense_callback;
2009 }
2010 
2011 /*
2012  *	idetape_queue_pc_head generates a new packet command request in front
2013  *	of the request queue, before the current request, so that it will be
2014  *	processed immediately, on the next pass through the driver.
2015  *
2016  *	idetape_queue_pc_head is called from the request handling part of
2017  *	the driver (the "bottom" part). Safe storage for the request should
2018  *	be allocated with idetape_next_pc_storage and idetape_next_rq_storage
2019  *	before calling idetape_queue_pc_head.
2020  *
2021  *	Memory for those requests is pre-allocated at initialization time, and
2022  *	is limited to IDETAPE_PC_STACK requests. We assume that we have enough
2023  *	space for the maximum possible number of inter-dependent packet commands.
2024  *
2025  *	The higher level of the driver - The ioctl handler and the character
2026  *	device handling functions should queue request to the lower level part
2027  *	and wait for their completion using idetape_queue_pc_tail or
2028  *	idetape_queue_rw_tail.
2029  */
idetape_queue_pc_head(ide_drive_t * drive,idetape_pc_t * pc,struct request * rq)2030 static void idetape_queue_pc_head (ide_drive_t *drive,idetape_pc_t *pc,struct request *rq)
2031 {
2032 	ide_init_drive_cmd(rq);
2033 	rq->buffer = (char *) pc;
2034 	rq->cmd = IDETAPE_PC_RQ1;
2035 	(void) ide_do_drive_cmd(drive, rq, ide_preempt);
2036 }
2037 
2038 /*
2039  *	idetape_retry_pc is called when an error was detected during the
2040  *	last packet command. We queue a request sense packet command in
2041  *	the head of the request list.
2042  */
idetape_retry_pc(ide_drive_t * drive)2043 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
2044 {
2045 	idetape_tape_t *tape = drive->driver_data;
2046 	idetape_pc_t *pc;
2047 	struct request *rq;
2048 	atapi_error_t error;
2049 
2050 	error.all = HWIF(drive)->INB(IDE_ERROR_REG);
2051 	pc = idetape_next_pc_storage (drive);
2052 	rq = idetape_next_rq_storage (drive);
2053 	idetape_create_request_sense_cmd(pc);
2054 	set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2055 	idetape_queue_pc_head(drive, pc, rq);
2056 	return ide_stopped;
2057 }
2058 
2059 /*
2060  *	idetape_postpone_request postpones the current request so that
2061  *	ide.c will be able to service requests from another device on
2062  *	the same hwgroup while we are polling for DSC.
2063  */
idetape_postpone_request(ide_drive_t * drive)2064 static void idetape_postpone_request (ide_drive_t *drive)
2065 {
2066 	idetape_tape_t *tape = drive->driver_data;
2067 
2068 #if IDETAPE_DEBUG_LOG
2069 	if (tape->debug_level >= 4)
2070 		printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
2071 #endif
2072 	tape->postponed_rq = HWGROUP(drive)->rq;
2073 	ide_stall_queue(drive, tape->dsc_polling_frequency);
2074 }
2075 
2076 /*
2077  *	idetape_pc_intr is the usual interrupt handler which will be called
2078  *	during a packet command. We will transfer some of the data (as
2079  *	requested by the drive) and will re-point interrupt handler to us.
2080  *	When data transfer is finished, we will act according to the
2081  *	algorithm described before idetape_issue_packet_command.
2082  *
2083  */
idetape_pc_intr(ide_drive_t * drive)2084 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
2085 {
2086 	idetape_tape_t *tape = drive->driver_data;
2087 	atapi_status_t status;
2088 	atapi_bcount_t bcount;
2089 	atapi_ireason_t ireason;
2090 	idetape_pc_t *pc = tape->pc;
2091 
2092 	unsigned int temp;
2093 	unsigned long cmd_time;
2094 #if SIMULATE_ERRORS
2095 	static int error_sim_count = 0;
2096 #endif
2097 
2098 #if IDETAPE_DEBUG_LOG
2099 	if (tape->debug_level >= 4)
2100 		printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
2101 				"interrupt handler\n");
2102 #endif /* IDETAPE_DEBUG_LOG */
2103 
2104 	/* Clear the interrupt */
2105 	status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2106 
2107 #ifdef CONFIG_BLK_DEV_IDEDMA
2108 	if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
2109 		if (HWIF(drive)->ide_dma_end(drive) || status.b.check) {
2110 			/*
2111 			 * A DMA error is sometimes expected. For example,
2112 			 * if the tape is crossing a filemark during a
2113 			 * READ command, it will issue an irq and position
2114 			 * itself before the filemark, so that only a partial
2115 			 * data transfer will occur (which causes the DMA
2116 			 * error). In that case, we will later ask the tape
2117 			 * how much bytes of the original request were
2118 			 * actually transferred (we can't receive that
2119 			 * information from the DMA engine on most chipsets).
2120 			 */
2121 
2122 			/*
2123 			 * On the contrary, a DMA error is never expected;
2124 			 * it usually indicates a hardware error or abort.
2125 			 * If the tape crosses a filemark during a READ
2126 			 * command, it will issue an irq and position itself
2127 			 * after the filemark (not before). Only a partial
2128 			 * data transfer will occur, but no DMA error.
2129 			 * (AS, 19 Apr 2001)
2130 			 */
2131 			set_bit (PC_DMA_ERROR, &pc->flags);
2132 		} else {
2133 			pc->actually_transferred = pc->request_transfer;
2134 			idetape_update_buffers (pc);
2135 		}
2136 #if IDETAPE_DEBUG_LOG
2137 		if (tape->debug_level >= 4)
2138 			printk (KERN_INFO "ide-tape: DMA finished\n");
2139 #endif /* IDETAPE_DEBUG_LOG */
2140 	}
2141 #endif /* CONFIG_BLK_DEV_IDEDMA */
2142 
2143 	if (!status.b.drq) {			/* No more interrupts */
2144 		cmd_time = (jiffies - tape->cmd_start_time) * 1000 / HZ;
2145 		tape->max_cmd_time = IDE_MAX(cmd_time, tape->max_cmd_time);
2146 #if IDETAPE_DEBUG_LOG
2147 		if (tape->debug_level >= 2)
2148 			printk(KERN_INFO "ide-tape: Packet command completed, "
2149 				"%d bytes transferred\n",
2150 				pc->actually_transferred);
2151 #endif /* IDETAPE_DEBUG_LOG */
2152 		clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
2153 
2154 		local_irq_enable();
2155 
2156 #if SIMULATE_ERRORS
2157 		if ((pc->c[0] == IDETAPE_WRITE_CMD ||
2158 		     pc->c[0] == IDETAPE_READ_CMD) &&
2159 		    (++error_sim_count % 100) == 0) {
2160 			printk(KERN_INFO "ide-tape: %s: simulating error\n",
2161 				tape->name);
2162 			status.b.check = 1;
2163 		}
2164 #endif
2165 		if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
2166 			status.b.check = 0;
2167 		if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) {
2168 			/* Error detected */
2169 			if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2170 				printk(KERN_ERR "ide-tape: I/O error in "
2171 					"request sense command\n");
2172 				return ide_do_reset(drive);
2173 			}
2174 #if IDETAPE_DEBUG_LOG
2175 			if (tape->debug_level >= 1)
2176 				printk(KERN_INFO "ide-tape: [cmd %x]: check "
2177 					"condition\n", pc->c[0]);
2178 #endif
2179 			/* Retry operation */
2180 			return idetape_retry_pc(drive);
2181 		}
2182 		pc->error = 0;
2183 		if (!tape->onstream &&
2184 		    test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
2185 		    !status.b.dsc) {
2186 			/* Media access command */
2187 			tape->dsc_polling_start = jiffies;
2188 			tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
2189 			tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
2190 			/* Allow ide.c to handle other requests */
2191 			idetape_postpone_request(drive);
2192 			return ide_stopped;
2193 		}
2194 		if (tape->failed_pc == pc)
2195 			tape->failed_pc = NULL;
2196 		/* Command finished - Call the callback function */
2197 		return pc->callback(drive);
2198 	}
2199 #ifdef CONFIG_BLK_DEV_IDEDMA
2200 	if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
2201 		printk(KERN_ERR "ide-tape: The tape wants to issue more "
2202 				"interrupts in DMA mode\n");
2203 		printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
2204 		(void) HWIF(drive)->ide_dma_off(drive);
2205 		return ide_do_reset(drive);
2206 	}
2207 #endif /* CONFIG_BLK_DEV_IDEDMA */
2208 	/* Get the number of bytes to transfer */
2209 	bcount.b.high	= HWIF(drive)->INB(IDE_BCOUNTH_REG);
2210 	bcount.b.low	= HWIF(drive)->INB(IDE_BCOUNTL_REG);
2211 	/* on this interrupt */
2212 	ireason.all	= HWIF(drive)->INB(IDE_IREASON_REG);
2213 
2214 	if (ireason.b.cod) {
2215 		printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
2216 		return ide_do_reset(drive);
2217 	}
2218 	if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) {
2219 		/* Hopefully, we will never get here */
2220 		printk(KERN_ERR "ide-tape: We wanted to %s, ",
2221 			ireason.b.io ? "Write":"Read");
2222 		printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
2223 			ireason.b.io ? "Read":"Write");
2224 		return ide_do_reset(drive);
2225 	}
2226 	if (!test_bit(PC_WRITING, &pc->flags)) {
2227 		/* Reading - Check that we have enough space */
2228 		temp = pc->actually_transferred + bcount.all;
2229 		if (temp > pc->request_transfer) {
2230 			if (temp > pc->buffer_size) {
2231 				printk(KERN_ERR "ide-tape: The tape wants to "
2232 					"send us more data than expected "
2233 					"- discarding data\n");
2234 				idetape_discard_data(drive, bcount.all);
2235 				if (HWGROUP(drive)->handler != NULL)
2236 					BUG();
2237 				ide_set_handler(drive,
2238 						&idetape_pc_intr,
2239 						IDETAPE_WAIT_CMD,
2240 						NULL);
2241 				return ide_started;
2242 			}
2243 #if IDETAPE_DEBUG_LOG
2244 			if (tape->debug_level >= 2)
2245 				printk(KERN_NOTICE "ide-tape: The tape wants "
2246 					"to send us more data than expected "
2247 					"- allowing transfer\n");
2248 #endif /* IDETAPE_DEBUG_LOG */
2249 		}
2250 	}
2251 	if (test_bit(PC_WRITING, &pc->flags)) {
2252 		if (pc->bh != NULL)
2253 			idetape_output_buffers(drive, pc, bcount.all);
2254 		else
2255 			/* Write the current buffer */
2256 			HWIF(drive)->atapi_output_bytes(drive,pc->current_position,bcount.all);
2257 	} else {
2258 		if (pc->bh != NULL)
2259 			idetape_input_buffers(drive, pc, bcount.all);
2260 		else
2261 			/* Read the current buffer */
2262 			HWIF(drive)->atapi_input_bytes(drive,pc->current_position,bcount.all);
2263 	}
2264 	/* Update the current position */
2265 	pc->actually_transferred += bcount.all;
2266 	pc->current_position += bcount.all;
2267 #if IDETAPE_DEBUG_LOG
2268 	if (tape->debug_level >= 2)
2269 		printk(KERN_INFO "ide-tape: [cmd %x] done %d\n"
2270 			pc->c[0], bcount.all);
2271 #endif
2272 	if (HWGROUP(drive)->handler != NULL)
2273 		BUG();
2274 	/* And set the interrupt handler again */
2275 	ide_set_handler (drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2276 	return ide_started;
2277 }
2278 
2279 /*
2280  *	Packet Command Interface
2281  *
2282  *	The current Packet Command is available in tape->pc, and will not
2283  *	change until we finish handling it. Each packet command is associated
2284  *	with a callback function that will be called when the command is
2285  *	finished.
2286  *
2287  *	The handling will be done in three stages:
2288  *
2289  *	1.	idetape_issue_packet_command will send the packet command to the
2290  *		drive, and will set the interrupt handler to idetape_pc_intr.
2291  *
2292  *	2.	On each interrupt, idetape_pc_intr will be called. This step
2293  *		will be repeated until the device signals us that no more
2294  *		interrupts will be issued.
2295  *
2296  *	3.	ATAPI Tape media access commands have immediate status with a
2297  *		delayed process. In case of a successful initiation of a
2298  *		media access packet command, the DSC bit will be set when the
2299  *		actual execution of the command is finished.
2300  *		Since the tape drive will not issue an interrupt, we have to
2301  *		poll for this event. In this case, we define the request as
2302  *		"low priority request" by setting rq_status to
2303  *		IDETAPE_RQ_POSTPONED, 	set a timer to poll for DSC and exit
2304  *		the driver.
2305  *
2306  *		ide.c will then give higher priority to requests which
2307  *		originate from the other device, until will change rq_status
2308  *		to RQ_ACTIVE.
2309  *
2310  *	4.	When the packet command is finished, it will be checked for errors.
2311  *
2312  *	5.	In case an error was found, we queue a request sense packet
2313  *		command in front of the request queue and retry the operation
2314  *		upto IDETAPE_MAX_PC_RETRIES times.
2315  *
2316  *	6.	In case no error was found, or we decided to give up and not
2317  *		to retry again, the callback function will be called and then
2318  *		we will handle the next request.
2319  *
2320  */
idetape_transfer_pc(ide_drive_t * drive)2321 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2322 {
2323 	idetape_tape_t *tape = drive->driver_data;
2324 	idetape_pc_t *pc = tape->pc;
2325 	atapi_ireason_t ireason;
2326 	int retries = 100;
2327 	ide_startstop_t startstop;
2328 
2329 	if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2330 		printk(KERN_ERR "ide-tape: Strange, packet command "
2331 			"initiated yet DRQ isn't asserted\n");
2332 		return startstop;
2333 	}
2334 	ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
2335 	while (retries-- && (!ireason.b.cod || ireason.b.io)) {
2336 		printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
2337 				"a packet command, retrying\n");
2338 		udelay(100);
2339 		ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
2340 		if (retries == 0) {
2341 			printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
2342 					"issuing a packet command, ignoring\n");
2343 			ireason.b.cod = 1;
2344 			ireason.b.io = 0;
2345 		}
2346 	}
2347 	if (!ireason.b.cod || ireason.b.io) {
2348 		printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
2349 				"a packet command\n");
2350 		return ide_do_reset(drive);
2351 	}
2352 	tape->cmd_start_time = jiffies;
2353 	if (HWGROUP(drive)->handler != NULL)	/* paranoia check */
2354 		BUG();
2355 	/* Set the interrupt routine */
2356 	ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2357 #ifdef CONFIG_BLK_DEV_IDEDMA
2358 	/* Begin DMA, if necessary */
2359 	if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
2360 		(void) (HWIF(drive)->ide_dma_begin(drive));
2361 #endif
2362 	/* Send the actual packet */
2363 	HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
2364 	return ide_started;
2365 }
2366 
idetape_issue_packet_command(ide_drive_t * drive,idetape_pc_t * pc)2367 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
2368 {
2369 	idetape_tape_t *tape = drive->driver_data;
2370 	atapi_feature_t feature;
2371 	atapi_bcount_t bcount;
2372 
2373 	feature.all = 0;
2374 
2375 #if IDETAPE_DEBUG_BUGS
2376 	if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
2377 	    pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2378 		printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
2379 			"Two request sense in serial were issued\n");
2380 	}
2381 #endif /* IDETAPE_DEBUG_BUGS */
2382 
2383 	if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
2384 		tape->failed_pc = pc;
2385 	/* Set the current packet command */
2386 	tape->pc = pc;
2387 
2388 	if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
2389 	    test_bit(PC_ABORT, &pc->flags)) {
2390 		/*
2391 		 *	We will "abort" retrying a packet command in case
2392 		 *	a legitimate error code was received (crossing a
2393 		 *	filemark, or end of the media, for example).
2394 		 */
2395 		if (!test_bit (PC_ABORT, &pc->flags)) {
2396 			if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
2397 			      tape->sense_key == 2 && tape->asc == 4 &&
2398 			     (tape->ascq == 1 || tape->ascq == 8))) {
2399 				printk(KERN_ERR "ide-tape: %s: I/O error, "
2400 						"pc = %2x, key = %2x, "
2401 						"asc = %2x, ascq = %2x\n",
2402 						tape->name, pc->c[0],
2403 						tape->sense_key, tape->asc,
2404 						tape->ascq);
2405 				if (tape->onstream &&
2406 				    pc->c[0] == IDETAPE_READ_CMD &&
2407 				    tape->sense_key == 3 &&
2408 				    tape->asc == 0x11)
2409 					/* AJN-1: 11 should be 0x11 */
2410 					printk(KERN_ERR "ide-tape: %s: enabling read error recovery\n", tape->name);
2411 			}
2412 			/* Giving up */
2413 			pc->error = IDETAPE_ERROR_GENERAL;
2414 		}
2415 		tape->failed_pc = NULL;
2416 		return pc->callback(drive);
2417 	}
2418 #if IDETAPE_DEBUG_LOG
2419 	if (tape->debug_level >= 2)
2420 		printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n",
2421 				pc->retries, pc->c[0]);
2422 #endif /* IDETAPE_DEBUG_LOG */
2423 
2424 	pc->retries++;
2425 	/* We haven't transferred any data yet */
2426 	pc->actually_transferred = 0;
2427 	pc->current_position = pc->buffer;
2428 	/* Request to transfer the entire buffer at once */
2429 	bcount.all = pc->request_transfer;
2430 
2431 #ifdef CONFIG_BLK_DEV_IDEDMA
2432 	if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
2433 		printk(KERN_WARNING "ide-tape: DMA disabled, "
2434 				"reverting to PIO\n");
2435 		(void) HWIF(drive)->ide_dma_off(drive);
2436 	}
2437 	if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma) {
2438 		if (test_bit(PC_WRITING, &pc->flags)) {
2439 			feature.b.dma = !HWIF(drive)->ide_dma_write(drive);
2440 		} else {
2441 			feature.b.dma = !HWIF(drive)->ide_dma_read(drive);
2442 		}
2443 	}
2444 #endif /* CONFIG_BLK_DEV_IDEDMA */
2445 
2446 	if (IDE_CONTROL_REG)
2447 		HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
2448 	/* Use PIO/DMA */
2449 	HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
2450 	HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
2451 	HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
2452 	HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
2453 #ifdef CONFIG_BLK_DEV_IDEDMA
2454 	if (feature.b.dma)		/* Will begin DMA later */
2455 		set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
2456 #endif /* CONFIG_BLK_DEV_IDEDMA */
2457 	if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
2458 		ide_execute_command(drive, WIN_PACKETCMD,
2459 				&idetape_transfer_pc,
2460 				IDETAPE_WAIT_CMD,
2461 				NULL);
2462 		return ide_started;
2463 	} else {
2464 		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2465 		return idetape_transfer_pc(drive);
2466 	}
2467 }
2468 
2469 /*
2470  *	General packet command callback function.
2471  */
idetape_pc_callback(ide_drive_t * drive)2472 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2473 {
2474 	idetape_tape_t *tape = drive->driver_data;
2475 
2476 #if IDETAPE_DEBUG_LOG
2477 	if (tape->debug_level >= 4)
2478 		printk (KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2479 #endif /* IDETAPE_DEBUG_LOG */
2480 
2481 	idetape_end_request(drive, tape->pc->error ? 0 : 1);
2482 	return ide_stopped;
2483 }
2484 
2485 /*
2486  *	A mode sense command is used to "sense" tape parameters.
2487  */
idetape_create_mode_sense_cmd(idetape_pc_t * pc,u8 page_code)2488 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
2489 {
2490 	idetape_init_pc (pc);
2491 	pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2492 	if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
2493 		pc->c[1] = 8;	/* DBD = 1 - Don't return block descriptors */
2494 	pc->c[2] = page_code;
2495 	pc->c[3] = 255;		/* Don't limit the returned information */
2496 	pc->c[4] = 255;		/* (We will just discard data in that case) */
2497 	if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
2498 		pc->request_transfer = 12;
2499 	else if (page_code == IDETAPE_CAPABILITIES_PAGE)
2500 		pc->request_transfer = 24;
2501 	else
2502 		pc->request_transfer = 50;
2503 	pc->callback = &idetape_pc_callback;
2504 }
2505 
idetape_onstream_buffer_fill_callback(ide_drive_t * drive)2506 static ide_startstop_t idetape_onstream_buffer_fill_callback (ide_drive_t *drive)
2507 {
2508 	idetape_tape_t *tape = drive->driver_data;
2509 
2510 	tape->max_frames = tape->pc->buffer[4 + 2];
2511 	tape->cur_frames = tape->pc->buffer[4 + 3];
2512 	if (tape->chrdev_direction == idetape_direction_write)
2513 		tape->tape_head = tape->buffer_head - tape->cur_frames;
2514 	else
2515 		tape->tape_head = tape->buffer_head + tape->cur_frames;
2516 	if (tape->tape_head != tape->last_tape_head) {
2517 		tape->last_tape_head = tape->tape_head;
2518 		tape->tape_still_time_begin = jiffies;
2519 		if (tape->tape_still_time > 200)
2520 			tape->measure_insert_time = 1;
2521 	}
2522 	tape->tape_still_time = (jiffies - tape->tape_still_time_begin) * 1000 / HZ;
2523 #if USE_IOTRACE
2524 	IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head,
2525 			tape->tape_head, tape->minor);
2526 #endif
2527 #if IDETAPE_DEBUG_LOG
2528 	if (tape->debug_level >= 1)
2529 		printk(KERN_INFO "ide-tape: buffer fill callback, %d/%d\n",
2530 			tape->cur_frames, tape->max_frames);
2531 #endif
2532 	idetape_end_request(drive, tape->pc->error ? 0 : 1);
2533 	return ide_stopped;
2534 }
2535 
idetape_queue_onstream_buffer_fill(ide_drive_t * drive)2536 static void idetape_queue_onstream_buffer_fill (ide_drive_t *drive)
2537 {
2538 	idetape_pc_t *pc;
2539 	struct request *rq;
2540 
2541 	pc = idetape_next_pc_storage (drive);
2542 	rq = idetape_next_rq_storage (drive);
2543 	idetape_create_mode_sense_cmd (pc, IDETAPE_BUFFER_FILLING_PAGE);
2544 	pc->callback = idetape_onstream_buffer_fill_callback;
2545 	idetape_queue_pc_head (drive, pc, rq);
2546 }
2547 
calculate_speeds(ide_drive_t * drive)2548 static void calculate_speeds(ide_drive_t *drive)
2549 {
2550 	idetape_tape_t *tape = drive->driver_data;
2551 	int full = 125, empty = 75;
2552 
2553 	if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
2554 		tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
2555 		tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
2556 		tape->controlled_last_pipeline_head = tape->pipeline_head;
2557 		tape->controlled_pipeline_head_time = jiffies;
2558 	}
2559 	if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
2560 		tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
2561 	else if (time_after(jiffies, tape->controlled_previous_head_time))
2562 		tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
2563 
2564 	if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
2565 		/* -1 for read mode error recovery */
2566 		if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
2567 			tape->uncontrolled_pipeline_head_time = jiffies;
2568 			tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
2569 		}
2570 	} else {
2571 		tape->uncontrolled_previous_head_time = jiffies;
2572 		tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
2573 		if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
2574 			tape->uncontrolled_pipeline_head_time = jiffies;
2575 		}
2576 	}
2577 	tape->pipeline_head_speed = IDE_MAX(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
2578 	if (tape->speed_control == 0) {
2579 		tape->max_insert_speed = 5000;
2580 	} else if (tape->speed_control == 1) {
2581 		if (tape->nr_pending_stages >= tape->max_stages / 2)
2582 			tape->max_insert_speed = tape->pipeline_head_speed +
2583 				(1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
2584 		else
2585 			tape->max_insert_speed = 500 +
2586 				(tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
2587 		if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
2588 			tape->max_insert_speed = 5000;
2589 	} else if (tape->speed_control == 2) {
2590 		tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
2591 			(tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
2592 	} else
2593 		tape->max_insert_speed = tape->speed_control;
2594 	tape->max_insert_speed = IDE_MAX(tape->max_insert_speed, 500);
2595 }
2596 
idetape_media_access_finished(ide_drive_t * drive)2597 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2598 {
2599 	idetape_tape_t *tape = drive->driver_data;
2600 	idetape_pc_t *pc = tape->pc;
2601 	atapi_status_t status;
2602 
2603 	if (tape->onstream)
2604 		printk(KERN_INFO "ide-tape: bug: "
2605 			"onstream, media_access_finished\n");
2606 
2607 	status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2608 
2609 	if (status.b.dsc) {
2610 		if (status.b.check) {
2611 			/* Error detected */
2612 			printk(KERN_ERR "ide-tape: %s: I/O error: "
2613 			    "pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
2614 			    tape->name, pc->c[0],
2615 			    tape->sense_key, tape->asc, tape->ascq);
2616 
2617 			/* Retry operation */
2618 			return idetape_retry_pc(drive);
2619 		}
2620 		pc->error = 0;
2621 		if (tape->failed_pc == pc)
2622 			tape->failed_pc = NULL;
2623 	} else {
2624 		pc->error = IDETAPE_ERROR_GENERAL;
2625 		tape->failed_pc = NULL;
2626 	}
2627 	return pc->callback (drive);
2628 }
2629 
idetape_rw_callback(ide_drive_t * drive)2630 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2631 {
2632 	idetape_tape_t *tape = drive->driver_data;
2633 	struct request *rq = HWGROUP(drive)->rq;
2634 	int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2635 
2636 	tape->avg_size += blocks * tape->tape_block_size;
2637 	tape->insert_size += blocks * tape->tape_block_size;
2638 	if (tape->insert_size > 1024 * 1024)
2639 		tape->measure_insert_time = 1;
2640 	if (tape->measure_insert_time) {
2641 		tape->measure_insert_time = 0;
2642 		tape->insert_time = jiffies;
2643 		tape->insert_size = 0;
2644 	}
2645 	if (time_after(jiffies, tape->insert_time))
2646 		tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2647 	if (jiffies - tape->avg_time >= HZ) {
2648 		tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
2649 		tape->avg_size = 0;
2650 		tape->avg_time = jiffies;
2651 	}
2652 
2653 #if IDETAPE_DEBUG_LOG
2654 	if (tape->debug_level >= 4)
2655 		printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2656 #endif /* IDETAPE_DEBUG_LOG */
2657 
2658 	tape->first_frame_position += blocks;
2659 	rq->current_nr_sectors -= blocks;
2660 
2661 	if (!tape->pc->error)
2662 		idetape_end_request(drive, 1);
2663 	else
2664 		idetape_end_request(drive, tape->pc->error);
2665 	return ide_stopped;
2666 }
2667 
idetape_create_read_cmd(idetape_tape_t * tape,idetape_pc_t * pc,unsigned int length,struct buffer_head * bh)2668 static void idetape_create_read_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2669 {
2670 	struct buffer_head *p = bh;
2671 	idetape_init_pc (pc);
2672 	pc->c[0] = IDETAPE_READ_CMD;
2673 	put_unaligned(htonl (length), (unsigned int *) &pc->c[1]);
2674 	pc->c[1] = 1;
2675 	pc->callback = &idetape_rw_callback;
2676 	pc->bh = bh;
2677 	atomic_set(&bh->b_count, 0);
2678 	pc->buffer = NULL;
2679 	if (tape->onstream) {
2680 		while (p) {
2681 			atomic_set(&p->b_count, 0);
2682 			p = p->b_reqnext;
2683 		}
2684 	}
2685 	if (!tape->onstream) {
2686 		pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2687 		if (pc->request_transfer == tape->stage_size)
2688 			set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2689 	} else  {
2690 		if (length) {
2691 			pc->request_transfer = pc->buffer_size = 32768 + 512;
2692 			set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2693 		} else
2694 			pc->request_transfer = 0;
2695 	}
2696 }
2697 
idetape_create_read_buffer_cmd(idetape_tape_t * tape,idetape_pc_t * pc,unsigned int length,struct buffer_head * bh)2698 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2699 {
2700 	int size = 32768;
2701 
2702 	struct buffer_head *p = bh;
2703 	idetape_init_pc(pc);
2704 	pc->c[0] = IDETAPE_READ_BUFFER_CMD;
2705 	pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
2706 	pc->c[7] = size >> 8;
2707 	pc->c[8] = size & 0xff;
2708 	pc->callback = &idetape_pc_callback;
2709 	pc->bh = bh;
2710 	atomic_set(&bh->b_count, 0);
2711 	pc->buffer = NULL;
2712 	while (p) {
2713 		atomic_set(&p->b_count, 0);
2714 		p = p->b_reqnext;
2715 	}
2716 	pc->request_transfer = pc->buffer_size = size;
2717 }
2718 
idetape_create_write_cmd(idetape_tape_t * tape,idetape_pc_t * pc,unsigned int length,struct buffer_head * bh)2719 static void idetape_create_write_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2720 {
2721 	struct buffer_head *p = bh;
2722 	idetape_init_pc(pc);
2723 	pc->c[0] = IDETAPE_WRITE_CMD;
2724 	put_unaligned(htonl (length), (unsigned int *) &pc->c[1]);
2725 	pc->c[1] = 1;
2726 	pc->callback = &idetape_rw_callback;
2727 	set_bit (PC_WRITING, &pc->flags);
2728 	if (tape->onstream) {
2729 		while (p) {
2730 			atomic_set(&p->b_count, p->b_size);
2731 			p = p->b_reqnext;
2732 		}
2733 	}
2734 	pc->bh = bh;
2735 	pc->b_data = bh->b_data;
2736 	pc->b_count = atomic_read(&bh->b_count);
2737 	pc->buffer = NULL;
2738 	if (!tape->onstream) {
2739 		pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2740 		if (pc->request_transfer == tape->stage_size)
2741 			set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2742 	} else  {
2743 		if (length) {
2744 			pc->request_transfer = pc->buffer_size = 32768 + 512;
2745 			set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2746 		} else
2747 			pc->request_transfer = 0;
2748 	}
2749 }
2750 
2751 /*
2752  * This is our end_request replacement function.
2753  */
idetape_do_end_request(ide_drive_t * drive,int uptodate)2754 static int idetape_do_end_request (ide_drive_t *drive, int uptodate)
2755 {
2756 	struct request *rq;
2757 	unsigned long flags;
2758 	int ret = 1;
2759 
2760 	spin_lock_irqsave(&io_request_lock, flags);
2761 	rq = HWGROUP(drive)->rq;
2762 
2763 	/*
2764 	 * decide whether to reenable DMA -- 3 is a random magic for now,
2765 	 * if we DMA timeout more than 3 times, just stay in PIO
2766 	 */
2767 	if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
2768 		drive->state = 0;
2769 		HWGROUP(drive)->hwif->ide_dma_on(drive);
2770 	}
2771 
2772 	if (!end_that_request_first(rq, uptodate, drive->name)) {
2773 		add_blkdev_randomness(MAJOR(rq->rq_dev));
2774 		blkdev_dequeue_request(rq);
2775 		HWGROUP(drive)->rq = NULL;
2776 		end_that_request_last(rq);
2777 		ret = 0;
2778 	}
2779 	spin_unlock_irqrestore(&io_request_lock, flags);
2780 	return ret;
2781 }
2782 
2783 /*
2784  *	idetape_do_request is our request handling function.
2785  */
idetape_do_request(ide_drive_t * drive,struct request * rq,unsigned long block)2786 static ide_startstop_t idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2787 {
2788 	idetape_tape_t *tape = drive->driver_data;
2789 	idetape_pc_t *pc;
2790 	struct request *postponed_rq = tape->postponed_rq;
2791 	atapi_status_t status;
2792 
2793 #if IDETAPE_DEBUG_LOG
2794 	if (tape->debug_level >= 5)
2795 		printk(KERN_INFO "ide-tape: rq_status: %d, "
2796 			"rq_dev: %u, cmd: %d, errors: %d\n", rq->rq_status,
2797 			(unsigned int) rq->rq_dev, rq->cmd, rq->errors);
2798 	if (tape->debug_level >= 2)
2799 		printk(KERN_INFO "ide-tape: sector: %ld, "
2800 			"nr_sectors: %ld, current_nr_sectors: %ld\n",
2801 			rq->sector, rq->nr_sectors, rq->current_nr_sectors);
2802 #endif /* IDETAPE_DEBUG_LOG */
2803 
2804 	if (!IDETAPE_RQ_CMD(rq->cmd)) {
2805 		/*
2806 		 *	We do not support buffer cache originated requests.
2807 		 */
2808 		printk(KERN_NOTICE "ide-tape: %s: Unsupported command in "
2809 			"request queue (%d)\n", drive->name, rq->cmd);
2810 		idetape_do_end_request(drive, 0);
2811 		return ide_stopped;
2812 	}
2813 
2814 	/*
2815 	 *	Retry a failed packet command
2816 	 */
2817 	if (tape->failed_pc != NULL &&
2818 	    tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2819 		return idetape_issue_packet_command(drive, tape->failed_pc);
2820 	}
2821 #if IDETAPE_DEBUG_BUGS
2822 	if (postponed_rq != NULL)
2823 		if (rq != postponed_rq) {
2824 			printk(KERN_ERR "ide-tape: ide-tape.c bug - "
2825 					"Two DSC requests were queued\n");
2826 			idetape_end_request(drive, 0);
2827 			return ide_stopped;
2828 		}
2829 #endif /* IDETAPE_DEBUG_BUGS */
2830 
2831 	tape->postponed_rq = NULL;
2832 
2833 	/*
2834 	 *	If the tape is still busy, postpone our request and service
2835 	 *	the other device meanwhile.
2836 	 */
2837 	status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2838 
2839 	/*
2840 	 * The OnStream tape drive doesn't support DSC. Assume
2841 	 * that DSC is always set.
2842 	 */
2843 	if (tape->onstream)
2844 		status.b.dsc = 1;
2845 	if (!drive->dsc_overlap && rq->cmd != IDETAPE_PC_RQ2)
2846 		set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2847 
2848 	/*
2849 	 * For the OnStream tape, check the current status of the tape
2850 	 * internal buffer using data gathered from the buffer fill
2851 	 * mode page, and postpone our request, effectively "disconnecting"
2852 	 * from the IDE bus, in case the buffer is full (writing) or
2853 	 * empty (reading), and there is a danger that our request will
2854 	 * hold the IDE bus during actual media access.
2855 	 */
2856 	if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2857 		tape->measure_insert_time = 1;
2858 	if (tape->req_buffer_fill &&
2859 	    (rq->cmd == IDETAPE_WRITE_RQ || rq->cmd == IDETAPE_READ_RQ)) {
2860 		tape->req_buffer_fill = 0;
2861 		tape->writes_since_buffer_fill = 0;
2862 		tape->reads_since_buffer_fill = 0;
2863 		tape->last_buffer_fill = jiffies;
2864 		idetape_queue_onstream_buffer_fill(drive);
2865 		if (time_after(jiffies, tape->insert_time))
2866 			tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2867 		return ide_stopped;
2868 	}
2869 	if (time_after(jiffies, tape->insert_time))
2870 		tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2871 	calculate_speeds(drive);
2872 	if (tape->onstream && tape->max_frames &&
2873 	    ((rq->cmd == IDETAPE_WRITE_RQ &&
2874               ( tape->cur_frames == tape->max_frames ||
2875                 ( tape->speed_control && tape->cur_frames > 5 &&
2876                        (tape->insert_speed > tape->max_insert_speed ||
2877                         (0 /* tape->cur_frames > 30 && tape->tape_still_time > 200 */) ) ) ) ) ||
2878 	     (rq->cmd == IDETAPE_READ_RQ &&
2879 	      ( tape->cur_frames == 0 ||
2880 		( tape->speed_control && (tape->cur_frames < tape->max_frames - 5) &&
2881 			tape->insert_speed > tape->max_insert_speed ) ) && rq->nr_sectors) ) ) {
2882 #if IDETAPE_DEBUG_LOG
2883 		if (tape->debug_level >= 4)
2884 			printk(KERN_INFO "ide-tape: postponing request, "
2885 					"cmd %d, cur %d, max %d\n",
2886 				rq->cmd, tape->cur_frames, tape->max_frames);
2887 #endif
2888 		if (tape->postpone_cnt++ < 500) {
2889 			status.b.dsc = 0;
2890 			tape->req_buffer_fill = 1;
2891 		}
2892 #if ONSTREAM_DEBUG
2893 		else if (tape->debug_level >= 4)
2894 			printk(KERN_INFO "ide-tape: %s: postpone_cnt %d\n",
2895 				tape->name, tape->postpone_cnt);
2896 #endif
2897 	}
2898 	if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2899 	    !status.b.dsc) {
2900 		if (postponed_rq == NULL) {
2901 			tape->dsc_polling_start = jiffies;
2902 			tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2903 			tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2904 		} else if ((signed long) (jiffies - tape->dsc_timeout) > 0) {
2905 			printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2906 				tape->name);
2907 			if (rq->cmd == IDETAPE_PC_RQ2) {
2908 				idetape_media_access_finished(drive);
2909 				return ide_stopped;
2910 			} else {
2911 				return ide_do_reset(drive);
2912 			}
2913 		} else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
2914 			tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2915 		idetape_postpone_request(drive);
2916 		return ide_stopped;
2917 	}
2918 	switch (rq->cmd) {
2919 		case IDETAPE_READ_RQ:
2920 			tape->buffer_head++;
2921 #if USE_IOTRACE
2922 			IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2923 #endif
2924 			tape->postpone_cnt = 0;
2925 			tape->reads_since_buffer_fill++;
2926 			if (tape->onstream) {
2927 				if (tape->cur_frames - tape->reads_since_buffer_fill <= 0)
2928 					tape->req_buffer_fill = 1;
2929 				if (time_after(jiffies, tape->last_buffer_fill + 5 * HZ / 100))
2930 					tape->req_buffer_fill = 1;
2931 			}
2932 			pc = idetape_next_pc_storage(drive);
2933 			idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, rq->bh);
2934 			break;
2935 		case IDETAPE_WRITE_RQ:
2936 			tape->buffer_head++;
2937 #if USE_IOTRACE
2938 			IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2939 #endif
2940 			tape->postpone_cnt = 0;
2941 			tape->writes_since_buffer_fill++;
2942 			if (tape->onstream) {
2943 				if (tape->cur_frames + tape->writes_since_buffer_fill >= tape->max_frames)
2944 					tape->req_buffer_fill = 1;
2945 				if (time_after(jiffies, tape->last_buffer_fill + 5 * HZ / 100))
2946 					tape->req_buffer_fill = 1;
2947 				calculate_speeds(drive);
2948 			}
2949 			pc = idetape_next_pc_storage(drive);
2950 			idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, rq->bh);
2951 			break;
2952 		case IDETAPE_READ_BUFFER_RQ:
2953 			tape->postpone_cnt = 0;
2954 			pc = idetape_next_pc_storage(drive);
2955 			idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, rq->bh);
2956 			break;
2957 		case IDETAPE_ABORTED_WRITE_RQ:
2958 			rq->cmd = IDETAPE_WRITE_RQ;
2959 			idetape_end_request(drive, IDETAPE_ERROR_EOD);
2960 			return ide_stopped;
2961 		case IDETAPE_ABORTED_READ_RQ:
2962 #if IDETAPE_DEBUG_LOG
2963 			if (tape->debug_level >= 2)
2964 				printk(KERN_INFO "ide-tape: %s: detected "
2965 					"aborted read rq\n", tape->name);
2966 #endif
2967 			rq->cmd = IDETAPE_READ_RQ;
2968 			idetape_end_request(drive, IDETAPE_ERROR_EOD);
2969 			return ide_stopped;
2970 		case IDETAPE_PC_RQ1:
2971 			pc = (idetape_pc_t *) rq->buffer;
2972 			rq->cmd = IDETAPE_PC_RQ2;
2973 			break;
2974 		case IDETAPE_PC_RQ2:
2975 			idetape_media_access_finished(drive);
2976 			return ide_stopped;
2977 		default:
2978 			printk(KERN_ERR "ide-tape: bug in "
2979 				"IDETAPE_RQ_CMD macro\n");
2980 			idetape_end_request(drive, 0);
2981 			return ide_stopped;
2982 	}
2983 	return idetape_issue_packet_command(drive, pc);
2984 }
2985 
2986 /*
2987  *	Pipeline related functions
2988  */
idetape_pipeline_active(idetape_tape_t * tape)2989 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2990 {
2991 	int rc1, rc2;
2992 
2993 	rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2994 	rc2 = (tape->active_data_request != NULL);
2995 	return rc1;
2996 }
2997 
2998 /*
2999  *	idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
3000  *	stage, along with all the necessary small buffers which together make
3001  *	a buffer of size tape->stage_size (or a bit more). We attempt to
3002  *	combine sequential pages as much as possible.
3003  *
3004  *	Returns a pointer to the new allocated stage, or NULL if we
3005  *	can't (or don't want to) allocate a stage.
3006  *
3007  *	Pipeline stages are optional and are used to increase performance.
3008  *	If we can't allocate them, we'll manage without them.
3009  */
__idetape_kmalloc_stage(idetape_tape_t * tape,int full,int clear)3010 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
3011 {
3012 	idetape_stage_t *stage;
3013 	struct buffer_head *prev_bh, *bh;
3014 	int pages = tape->pages_per_stage;
3015 	char *b_data;
3016 
3017 	if ((stage = (idetape_stage_t *) kmalloc(sizeof(idetape_stage_t),GFP_KERNEL)) == NULL)
3018 		return NULL;
3019 	stage->next = NULL;
3020 
3021 	bh = stage->bh = (struct buffer_head *) kmalloc(sizeof(struct buffer_head), GFP_KERNEL);
3022 	if (bh == NULL)
3023 		goto abort;
3024 	bh->b_reqnext = NULL;
3025 	if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
3026 		goto abort;
3027 	if (clear)
3028 		memset(bh->b_data, 0, PAGE_SIZE);
3029 	bh->b_size = PAGE_SIZE;
3030 	atomic_set(&bh->b_count, full ? bh->b_size : 0);
3031 	set_bit(BH_Lock, &bh->b_state);
3032 
3033 	while (--pages) {
3034 		if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
3035 			goto abort;
3036 		if (clear)
3037 			memset(b_data, 0, PAGE_SIZE);
3038 		if (bh->b_data == b_data + PAGE_SIZE) {
3039 			bh->b_size += PAGE_SIZE;
3040 			bh->b_data -= PAGE_SIZE;
3041 			if (full)
3042 				atomic_add(PAGE_SIZE, &bh->b_count);
3043 			continue;
3044 		}
3045 		if (b_data == bh->b_data + bh->b_size) {
3046 			bh->b_size += PAGE_SIZE;
3047 			if (full)
3048 				atomic_add(PAGE_SIZE, &bh->b_count);
3049 			continue;
3050 		}
3051 		prev_bh = bh;
3052 		if ((bh = (struct buffer_head *) kmalloc(sizeof(struct buffer_head), GFP_KERNEL)) == NULL) {
3053 			free_page((unsigned long) b_data);
3054 			goto abort;
3055 		}
3056 		bh->b_reqnext = NULL;
3057 		bh->b_data = b_data;
3058 		bh->b_size = PAGE_SIZE;
3059 		atomic_set(&bh->b_count, full ? bh->b_size : 0);
3060 		set_bit(BH_Lock, &bh->b_state);
3061 		prev_bh->b_reqnext = bh;
3062 	}
3063 	bh->b_size -= tape->excess_bh_size;
3064 	if (full)
3065 		atomic_sub(tape->excess_bh_size, &bh->b_count);
3066 	if (tape->onstream)
3067 		stage->aux = (os_aux_t *) (bh->b_data + bh->b_size - OS_AUX_SIZE);
3068 	return stage;
3069 abort:
3070 	__idetape_kfree_stage(stage);
3071 	return NULL;
3072 }
3073 
idetape_kmalloc_stage(idetape_tape_t * tape)3074 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
3075 {
3076 	idetape_stage_t *cache_stage = tape->cache_stage;
3077 
3078 #if IDETAPE_DEBUG_LOG
3079 	if (tape->debug_level >= 4)
3080 		printk (KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
3081 #endif /* IDETAPE_DEBUG_LOG */
3082 
3083 	if (tape->nr_stages >= tape->max_stages)
3084 		return NULL;
3085 	if (cache_stage != NULL) {
3086 		tape->cache_stage = NULL;
3087 		return cache_stage;
3088 	}
3089 	return __idetape_kmalloc_stage(tape, 0, 0);
3090 }
3091 
idetape_copy_stage_from_user(idetape_tape_t * tape,idetape_stage_t * stage,const char * buf,int n)3092 static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char *buf, int n)
3093 {
3094 	struct buffer_head *bh = tape->bh;
3095 	int count;
3096 
3097 	while (n) {
3098 #if IDETAPE_DEBUG_BUGS
3099 		if (bh == NULL) {
3100 			printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
3101 				__FUNCTION__);
3102 			return;
3103 		}
3104 #endif /* IDETAPE_DEBUG_BUGS */
3105 		count = IDE_MIN(bh->b_size - atomic_read(&bh->b_count), n);
3106 		copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count);
3107 		n -= count;
3108 		atomic_add(count, &bh->b_count);
3109 		buf += count;
3110 		if (atomic_read(&bh->b_count) == bh->b_size) {
3111 			bh = bh->b_reqnext;
3112 			if (bh)
3113 				atomic_set(&bh->b_count, 0);
3114 		}
3115 	}
3116 	tape->bh = bh;
3117 }
3118 
idetape_copy_stage_to_user(idetape_tape_t * tape,char * buf,idetape_stage_t * stage,int n)3119 static void idetape_copy_stage_to_user (idetape_tape_t *tape, char *buf, idetape_stage_t *stage, int n)
3120 {
3121 	struct buffer_head *bh = tape->bh;
3122 	int count;
3123 
3124 	while (n) {
3125 #if IDETAPE_DEBUG_BUGS
3126 		if (bh == NULL) {
3127 			printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
3128 				__FUNCTION__);
3129 			return;
3130 		}
3131 #endif /* IDETAPE_DEBUG_BUGS */
3132 		count = IDE_MIN(tape->b_count, n);
3133 		copy_to_user(buf, tape->b_data, count);
3134 		n -= count;
3135 		tape->b_data += count;
3136 		tape->b_count -= count;
3137 		buf += count;
3138 		if (!tape->b_count) {
3139 			tape->bh = bh = bh->b_reqnext;
3140 			if (bh) {
3141 				tape->b_data = bh->b_data;
3142 				tape->b_count = atomic_read(&bh->b_count);
3143 			}
3144 		}
3145 	}
3146 }
3147 
idetape_init_merge_stage(idetape_tape_t * tape)3148 static void idetape_init_merge_stage (idetape_tape_t *tape)
3149 {
3150 	struct buffer_head *bh = tape->merge_stage->bh;
3151 
3152 	tape->bh = bh;
3153 	if (tape->chrdev_direction == idetape_direction_write) {
3154 		atomic_set(&bh->b_count, 0);
3155 	} else {
3156 		tape->b_data = bh->b_data;
3157 		tape->b_count = atomic_read(&bh->b_count);
3158 	}
3159 }
3160 
idetape_switch_buffers(idetape_tape_t * tape,idetape_stage_t * stage)3161 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
3162 {
3163 	struct buffer_head *tmp;
3164 	os_aux_t *tmp_aux;
3165 
3166 	tmp = stage->bh;
3167 	tmp_aux = stage->aux;
3168 	stage->bh = tape->merge_stage->bh;
3169 	stage->aux = tape->merge_stage->aux;
3170 	tape->merge_stage->bh = tmp;
3171 	tape->merge_stage->aux = tmp_aux;
3172 	idetape_init_merge_stage (tape);
3173 }
3174 
3175 /*
3176  *	idetape_add_stage_tail adds a new stage at the end of the pipeline.
3177  */
idetape_add_stage_tail(ide_drive_t * drive,idetape_stage_t * stage)3178 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
3179 {
3180 	idetape_tape_t *tape = drive->driver_data;
3181 	unsigned long flags;
3182 
3183 #if IDETAPE_DEBUG_LOG
3184 	if (tape->debug_level >= 4)
3185 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
3186 #endif /* IDETAPE_DEBUG_LOG */
3187 	spin_lock_irqsave(&tape->spinlock, flags);
3188 	stage->next = NULL;
3189 	if (tape->last_stage != NULL)
3190 		tape->last_stage->next=stage;
3191 	else
3192 		tape->first_stage = tape->next_stage=stage;
3193 	tape->last_stage = stage;
3194 	if (tape->next_stage == NULL)
3195 		tape->next_stage = tape->last_stage;
3196 	tape->nr_stages++;
3197 	tape->nr_pending_stages++;
3198 	spin_unlock_irqrestore(&tape->spinlock, flags);
3199 }
3200 
3201 /*
3202  * Initialize the OnStream AUX
3203  */
idetape_init_stage(ide_drive_t * drive,idetape_stage_t * stage,int frame_type,int logical_blk_num)3204 static void idetape_init_stage (ide_drive_t *drive, idetape_stage_t *stage, int frame_type, int logical_blk_num)
3205 {
3206 	idetape_tape_t *tape = drive->driver_data;
3207 	os_aux_t *aux = stage->aux;
3208 	os_partition_t *par = &aux->partition;
3209 	os_dat_t *dat = &aux->dat;
3210 
3211 	if (!tape->onstream || tape->raw)
3212 		return;
3213 	memset(aux, 0, sizeof(*aux));
3214 	aux->format_id = htonl(0);
3215 	memcpy(aux->application_sig, "LIN3", 4);
3216 	aux->hdwr = htonl(0);
3217 	aux->frame_type = frame_type;
3218 
3219 	if (frame_type == OS_FRAME_TYPE_HEADER) {
3220 		aux->update_frame_cntr = htonl(tape->update_frame_cntr);
3221 		par->partition_num = OS_CONFIG_PARTITION;
3222 		par->par_desc_ver = OS_PARTITION_VERSION;
3223 		par->wrt_pass_cntr = htons(0xffff);
3224 		par->first_frame_addr = htonl(0);
3225 		par->last_frame_addr = htonl(0xbb7); /* 2999 */
3226 		aux->frame_seq_num = htonl(0);
3227 		aux->logical_blk_num_high = htonl(0);
3228 		aux->logical_blk_num = htonl(0);
3229 		aux->next_mark_addr = htonl(tape->first_mark_addr);
3230 	} else {
3231 		aux->update_frame_cntr = htonl(0);
3232 		par->partition_num = OS_DATA_PARTITION;
3233 		par->par_desc_ver = OS_PARTITION_VERSION;
3234 		par->wrt_pass_cntr = htons(tape->wrt_pass_cntr);
3235 		par->first_frame_addr = htonl(OS_DATA_STARTFRAME1);
3236 		par->last_frame_addr = htonl(tape->capacity);
3237 		aux->frame_seq_num = htonl(logical_blk_num);
3238 		aux->logical_blk_num_high = htonl(0);
3239 		aux->logical_blk_num = htonl(logical_blk_num);
3240 		dat->dat_sz = 8;
3241 		dat->reserved1 = 0;
3242 		dat->entry_cnt = 1;
3243 		dat->reserved3 = 0;
3244 		if (frame_type == OS_FRAME_TYPE_DATA)
3245 			dat->dat_list[0].blk_sz = htonl(32 * 1024);
3246 		else
3247 			dat->dat_list[0].blk_sz = 0;
3248 		dat->dat_list[0].blk_cnt = htons(1);
3249 		if (frame_type == OS_FRAME_TYPE_MARKER)
3250 			dat->dat_list[0].flags = OS_DAT_FLAGS_MARK;
3251 		else
3252 			dat->dat_list[0].flags = OS_DAT_FLAGS_DATA;
3253 		dat->dat_list[0].reserved = 0;
3254 	}
3255 	/* shouldn't this be htonl ?? */
3256 	aux->filemark_cnt = ntohl(tape->filemark_cnt);
3257 	/* shouldn't this be htonl ?? */
3258 	aux->phys_fm = ntohl(0xffffffff);
3259 	/* shouldn't this be htonl ?? */
3260 	aux->last_mark_addr = ntohl(tape->last_mark_addr);
3261 }
3262 
3263 /*
3264  *	idetape_wait_for_request installs a completion in a pending request
3265  *	and sleeps until it is serviced.
3266  *
3267  *	The caller should ensure that the request will not be serviced
3268  *	before we install the completion (usually by disabling interrupts).
3269  */
idetape_wait_for_request(ide_drive_t * drive,struct request * rq)3270 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
3271 {
3272 	DECLARE_COMPLETION(wait);
3273 	idetape_tape_t *tape = drive->driver_data;
3274 
3275 #if IDETAPE_DEBUG_BUGS
3276 	if (rq == NULL || !IDETAPE_RQ_CMD (rq->cmd)) {
3277 		printk(KERN_ERR "ide-tape: bug: Trying to sleep on "
3278 			"non-valid request\n");
3279 		return;
3280 	}
3281 #endif /* IDETAPE_DEBUG_BUGS */
3282 	rq->waiting = &wait;
3283 	tape->waiting = &wait;
3284 	spin_unlock(&tape->spinlock);
3285 	wait_for_completion(&wait);
3286 	/* The stage and its struct request have been deallocated */
3287 	tape->waiting = NULL;
3288 	spin_lock_irq(&tape->spinlock);
3289 }
3290 
idetape_read_position_callback(ide_drive_t * drive)3291 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
3292 {
3293 	idetape_tape_t *tape = drive->driver_data;
3294 	idetape_read_position_result_t *result;
3295 
3296 #if IDETAPE_DEBUG_LOG
3297 	if (tape->debug_level >= 4)
3298 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
3299 #endif /* IDETAPE_DEBUG_LOG */
3300 
3301 	if (!tape->pc->error) {
3302 		result = (idetape_read_position_result_t *) tape->pc->buffer;
3303 #if IDETAPE_DEBUG_LOG
3304 		if (tape->debug_level >= 2)
3305 			printk(KERN_INFO "ide-tape: BOP - %s\n",
3306 				result->bop ? "Yes":"No");
3307 		if (tape->debug_level >= 2)
3308 			printk(KERN_INFO "ide-tape: EOP - %s\n",
3309 				result->eop ? "Yes":"No");
3310 #endif /* IDETAPE_DEBUG_LOG */
3311 		if (result->bpu) {
3312 			printk(KERN_INFO "ide-tape: Block location is "
3313 				"unknown to the tape\n");
3314 			clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
3315 			idetape_end_request(drive, 0);
3316 		} else {
3317 #if IDETAPE_DEBUG_LOG
3318 			if (tape->debug_level >= 2)
3319 				printk(KERN_INFO "ide-tape: Block Location "
3320 					"- %u\n", ntohl(result->first_block));
3321 #endif /* IDETAPE_DEBUG_LOG */
3322 			tape->partition = result->partition;
3323 			tape->first_frame_position = ntohl(result->first_block);
3324 			tape->last_frame_position = ntohl(result->last_block);
3325 			tape->blocks_in_buffer = result->blocks_in_buffer[2];
3326 			set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
3327 			idetape_end_request(drive, 1);
3328 		}
3329 	} else {
3330 		idetape_end_request(drive, 0);
3331 	}
3332 	return ide_stopped;
3333 }
3334 
3335 /*
3336  *	idetape_create_write_filemark_cmd will:
3337  *
3338  *		1.	Write a filemark if write_filemark=1.
3339  *		2.	Flush the device buffers without writing a filemark
3340  *			if write_filemark=0.
3341  *
3342  */
idetape_create_write_filemark_cmd(ide_drive_t * drive,idetape_pc_t * pc,int write_filemark)3343 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
3344 {
3345 	idetape_tape_t *tape = drive->driver_data;
3346 
3347 	idetape_init_pc(pc);
3348 	pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
3349 	if (tape->onstream)
3350 		pc->c[1] = 1; /* Immed bit */
3351 	pc->c[4] = write_filemark;  /* not used for OnStream ?? */
3352 	set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3353 	pc->callback = &idetape_pc_callback;
3354 }
3355 
idetape_create_test_unit_ready_cmd(idetape_pc_t * pc)3356 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
3357 {
3358 	idetape_init_pc(pc);
3359 	pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
3360 	pc->callback = &idetape_pc_callback;
3361 }
3362 
3363 /*
3364  *	idetape_queue_pc_tail is based on the following functions:
3365  *
3366  *	ide_do_drive_cmd from ide.c
3367  *	cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
3368  *
3369  *	We add a special packet command request to the tail of the request
3370  *	queue, and wait for it to be serviced.
3371  *
3372  *	This is not to be called from within the request handling part
3373  *	of the driver ! We allocate here data in the stack, and it is valid
3374  *	until the request is finished. This is not the case for the bottom
3375  *	part of the driver, where we are always leaving the functions to wait
3376  *	for an interrupt or a timer event.
3377  *
3378  *	From the bottom part of the driver, we should allocate safe memory
3379  *	using idetape_next_pc_storage and idetape_next_rq_storage, and add
3380  *	the request to the request list without waiting for it to be serviced !
3381  *	In that case, we usually use idetape_queue_pc_head.
3382  */
__idetape_queue_pc_tail(ide_drive_t * drive,idetape_pc_t * pc)3383 static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
3384 {
3385 	struct request rq;
3386 
3387 	ide_init_drive_cmd(&rq);
3388 	rq.buffer = (char *) pc;
3389 	rq.cmd = IDETAPE_PC_RQ1;
3390 	return ide_do_drive_cmd(drive, &rq, ide_wait);
3391 }
3392 
idetape_create_load_unload_cmd(ide_drive_t * drive,idetape_pc_t * pc,int cmd)3393 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
3394 {
3395 	idetape_tape_t *tape = drive->driver_data;
3396 
3397 	idetape_init_pc(pc);
3398 	pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
3399 	pc->c[4] = cmd;
3400 	if (tape->onstream) {
3401 		pc->c[1] = 1;
3402 		if (cmd == !IDETAPE_LU_LOAD_MASK)
3403 			pc->c[4] = 4;
3404 	}
3405 	set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3406 	pc->callback = &idetape_pc_callback;
3407 }
3408 
idetape_wait_ready(ide_drive_t * drive,unsigned long long timeout)3409 static int idetape_wait_ready (ide_drive_t *drive, unsigned long long timeout)
3410 {
3411 	idetape_tape_t *tape = drive->driver_data;
3412 	idetape_pc_t pc;
3413 
3414 	/*
3415 	 * Wait for the tape to become ready
3416 	 */
3417 	timeout += jiffies;
3418 	while (time_before(jiffies, timeout)) {
3419 		idetape_create_test_unit_ready_cmd(&pc);
3420 		if (!__idetape_queue_pc_tail(drive, &pc))
3421 			return 0;
3422 		if (tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) {
3423 			idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3424 			__idetape_queue_pc_tail(drive, &pc);
3425 			idetape_create_test_unit_ready_cmd(&pc);
3426 			if (!__idetape_queue_pc_tail(drive, &pc))
3427 				return 0;
3428 		}
3429 		if (!(tape->sense_key == 2 && tape->asc == 4 &&
3430 		    (tape->ascq == 1 || tape->ascq == 8)))
3431 			break;
3432 		current->state = TASK_INTERRUPTIBLE;
3433   		schedule_timeout(HZ / 10);
3434 	}
3435 	return -EIO;
3436 }
3437 
idetape_queue_pc_tail(ide_drive_t * drive,idetape_pc_t * pc)3438 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
3439 {
3440 	idetape_tape_t *tape = drive->driver_data;
3441 	int rc;
3442 
3443 	rc = __idetape_queue_pc_tail(drive, pc);
3444 	if (rc)
3445 		return rc;
3446 	if (tape->onstream && test_bit(PC_WAIT_FOR_DSC, &pc->flags)) {
3447 		/* AJN-4: Changed from 5 to 10 minutes;
3448 		 * because retension takes approx.
3449 		 * 8:20 with Onstream 30GB tape
3450 		 */
3451 		rc = idetape_wait_ready(drive, 60 * 10 * HZ);
3452 	}
3453 	return rc;
3454 }
3455 
idetape_flush_tape_buffers(ide_drive_t * drive)3456 static int idetape_flush_tape_buffers (ide_drive_t *drive)
3457 {
3458 	idetape_pc_t pc;
3459 	int rc;
3460 
3461 	idetape_create_write_filemark_cmd(drive, &pc, 0);
3462 	if ((rc = idetape_queue_pc_tail (drive, &pc)))
3463 		return rc;
3464 	idetape_wait_ready(drive, 60 * 5 * HZ);
3465 	return 0;
3466 }
3467 
idetape_create_read_position_cmd(idetape_pc_t * pc)3468 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
3469 {
3470 	idetape_init_pc(pc);
3471 	pc->c[0] = IDETAPE_READ_POSITION_CMD;
3472 	pc->request_transfer = 20;
3473 	pc->callback = &idetape_read_position_callback;
3474 }
3475 
idetape_read_position(ide_drive_t * drive)3476 static int idetape_read_position (ide_drive_t *drive)
3477 {
3478 	idetape_tape_t *tape = drive->driver_data;
3479 	idetape_pc_t pc;
3480 	int position;
3481 
3482 #if IDETAPE_DEBUG_LOG
3483         if (tape->debug_level >= 4)
3484 		printk (KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
3485 #endif /* IDETAPE_DEBUG_LOG */
3486 
3487 	idetape_create_read_position_cmd(&pc);
3488 	if (idetape_queue_pc_tail(drive, &pc))
3489 		return -1;
3490 	position = tape->first_frame_position;
3491 	return position;
3492 }
3493 
idetape_create_locate_cmd(ide_drive_t * drive,idetape_pc_t * pc,unsigned int block,u8 partition,int skip)3494 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
3495 {
3496 	idetape_tape_t *tape = drive->driver_data;
3497 
3498 	idetape_init_pc(pc);
3499 	pc->c[0] = IDETAPE_LOCATE_CMD;
3500 	if (tape->onstream)
3501 		pc->c[1] = 1; /* Immediate bit */
3502 	else
3503 		pc->c[1] = 2;
3504 	put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
3505 	pc->c[8] = partition;
3506 	if (tape->onstream)
3507                 /*
3508                  * Set SKIP bit.
3509                  * In case of write error this will write buffered
3510                  * data in the drive to this new position!
3511                  */
3512 		pc->c[9] = skip << 7;
3513 	set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3514 	pc->callback = &idetape_pc_callback;
3515 }
3516 
idetape_create_prevent_cmd(ide_drive_t * drive,idetape_pc_t * pc,int prevent)3517 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
3518 {
3519 	idetape_tape_t *tape = drive->driver_data;
3520 
3521 	if (!tape->capabilities.lock)
3522 		return 0;
3523 
3524 	idetape_init_pc(pc);
3525 	pc->c[0] = IDETAPE_PREVENT_CMD;
3526 	pc->c[4] = prevent;
3527 	pc->callback = &idetape_pc_callback;
3528 	return 1;
3529 }
3530 
__idetape_discard_read_pipeline(ide_drive_t * drive)3531 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
3532 {
3533 	idetape_tape_t *tape = drive->driver_data;
3534 	unsigned long flags;
3535 	int cnt;
3536 
3537 	if (tape->chrdev_direction != idetape_direction_read)
3538 		return 0;
3539 	cnt = tape->merge_stage_size / tape->tape_block_size;
3540 	if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3541 		++cnt;		/* Filemarks count as 1 sector */
3542 	tape->merge_stage_size = 0;
3543 	if (tape->merge_stage != NULL) {
3544 		__idetape_kfree_stage(tape->merge_stage);
3545 		tape->merge_stage = NULL;
3546 	}
3547 	clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3548 	tape->chrdev_direction = idetape_direction_none;
3549 
3550 	if (tape->first_stage == NULL)
3551 		return 0;
3552 
3553 	spin_lock_irqsave(&tape->spinlock, flags);
3554 	tape->next_stage = NULL;
3555 	if (idetape_pipeline_active(tape))
3556 		idetape_wait_for_request(drive, tape->active_data_request);
3557 	spin_unlock_irqrestore(&tape->spinlock, flags);
3558 
3559 	while (tape->first_stage != NULL) {
3560 		struct request *rq_ptr = &tape->first_stage->rq;
3561 
3562 		cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
3563 		if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3564 			++cnt;
3565 		idetape_remove_stage_head(drive);
3566 	}
3567 	tape->nr_pending_stages = 0;
3568 	tape->max_stages = tape->min_pipeline;
3569 	return cnt;
3570 }
3571 
3572 /*
3573  *	idetape_position_tape positions the tape to the requested block
3574  *	using the LOCATE packet command. A READ POSITION command is then
3575  *	issued to check where we are positioned.
3576  *
3577  *	Like all higher level operations, we queue the commands at the tail
3578  *	of the request queue and wait for their completion.
3579  *
3580  */
idetape_position_tape(ide_drive_t * drive,unsigned int block,u8 partition,int skip)3581 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
3582 {
3583 	idetape_tape_t *tape = drive->driver_data;
3584 	int retval;
3585 	idetape_pc_t pc;
3586 
3587 	if (tape->chrdev_direction == idetape_direction_read)
3588 		__idetape_discard_read_pipeline(drive);
3589 	idetape_wait_ready(drive, 60 * 5 * HZ);
3590 	idetape_create_locate_cmd(drive, &pc, block, partition, skip);
3591 	retval = idetape_queue_pc_tail(drive, &pc);
3592 	if (retval)
3593 		return (retval);
3594 
3595 	idetape_create_read_position_cmd(&pc);
3596 	return (idetape_queue_pc_tail(drive, &pc));
3597 }
3598 
idetape_discard_read_pipeline(ide_drive_t * drive,int restore_position)3599 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
3600 {
3601 	idetape_tape_t *tape = drive->driver_data;
3602 	int cnt;
3603 	int seek, position;
3604 
3605 	cnt = __idetape_discard_read_pipeline(drive);
3606 	if (restore_position) {
3607 		position = idetape_read_position(drive);
3608 #if ONSTREAM_DEBUG
3609 		if (tape->debug_level >= 2)
3610 			printk(KERN_INFO "ide-tape: address %u, nr_stages %d\n",
3611 				position, cnt);
3612 #endif
3613 		seek = position > cnt ? position - cnt : 0;
3614 		if (idetape_position_tape(drive, seek, 0, 0)) {
3615 			printk(KERN_INFO "ide-tape: %s: position_tape "
3616 				"failed in discard_pipeline()\n", tape->name);
3617 			return;
3618 		}
3619 	}
3620 }
3621 
idetape_update_stats(ide_drive_t * drive)3622 static void idetape_update_stats (ide_drive_t *drive)
3623 {
3624 	idetape_pc_t pc;
3625 
3626 	idetape_create_mode_sense_cmd(&pc, IDETAPE_BUFFER_FILLING_PAGE);
3627 	pc.callback = idetape_onstream_buffer_fill_callback;
3628 	(void) idetape_queue_pc_tail(drive, &pc);
3629 }
3630 
3631 /*
3632  *	idetape_queue_rw_tail generates a read/write request for the block
3633  *	device interface and wait for it to be serviced.
3634  */
idetape_queue_rw_tail(ide_drive_t * drive,int cmd,int blocks,struct buffer_head * bh)3635 static int idetape_queue_rw_tail (ide_drive_t *drive, int cmd, int blocks, struct buffer_head *bh)
3636 {
3637 	idetape_tape_t *tape = drive->driver_data;
3638 	struct request rq;
3639 
3640 #if IDETAPE_DEBUG_LOG
3641 	if (tape->debug_level >= 2)
3642 		printk(KERN_INFO "ide-tape: %s: cmd=%d\n", __FUNCTION__, cmd);
3643 #endif /* IDETAPE_DEBUG_LOG */
3644 #if IDETAPE_DEBUG_BUGS
3645 	if (idetape_pipeline_active (tape)) {
3646 		printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
3647 			__FUNCTION__);
3648 		return (0);
3649 	}
3650 #endif /* IDETAPE_DEBUG_BUGS */
3651 
3652 	ide_init_drive_cmd(&rq);
3653 	rq.bh = bh;
3654 	rq.cmd = cmd;
3655 	rq.sector = tape->first_frame_position;
3656 	rq.nr_sectors = rq.current_nr_sectors = blocks;
3657 	if (tape->onstream)
3658 		tape->postpone_cnt = 600;
3659 	(void) ide_do_drive_cmd(drive, &rq, ide_wait);
3660 
3661 	if (cmd != IDETAPE_READ_RQ && cmd != IDETAPE_WRITE_RQ)
3662 		return 0;
3663 
3664 	if (tape->merge_stage)
3665 		idetape_init_merge_stage(tape);
3666 	if (rq.errors == IDETAPE_ERROR_GENERAL)
3667 		return -EIO;
3668 	return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
3669 }
3670 
3671 /*
3672  * Read back the drive's internal buffer contents, as a part
3673  * of the write error recovery mechanism for old OnStream
3674  * firmware revisions.
3675  */
idetape_onstream_read_back_buffer(ide_drive_t * drive)3676 static void idetape_onstream_read_back_buffer (ide_drive_t *drive)
3677 {
3678 	idetape_tape_t *tape = drive->driver_data;
3679 	int frames, i, logical_blk_num;
3680 	idetape_stage_t *stage, *first = NULL, *last = NULL;
3681 	os_aux_t *aux;
3682 	struct request *rq;
3683 	unsigned char *p;
3684 	unsigned long flags;
3685 
3686 	idetape_update_stats(drive);
3687 	frames = tape->cur_frames;
3688 	logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num) - frames;
3689 	printk(KERN_INFO "ide-tape: %s: reading back %d frames from the drive's internal buffer\n", tape->name, frames);
3690 	for (i = 0; i < frames; i++) {
3691 		stage = __idetape_kmalloc_stage(tape, 0, 0);
3692 		if (!first)
3693 			first = stage;
3694 		aux = stage->aux;
3695 		p = stage->bh->b_data;
3696 		idetape_queue_rw_tail(drive, IDETAPE_READ_BUFFER_RQ, tape->capabilities.ctl, stage->bh);
3697 #if ONSTREAM_DEBUG
3698 		if (tape->debug_level >= 2)
3699 			printk(KERN_INFO "ide-tape: %s: read back logical block %d, data %x %x %x %x\n", tape->name, logical_blk_num, *p++, *p++, *p++, *p++);
3700 #endif
3701 		rq = &stage->rq;
3702 		ide_init_drive_cmd (rq);
3703 		rq->cmd = IDETAPE_WRITE_RQ;
3704 		rq->sector = tape->first_frame_position;
3705 		rq->nr_sectors = rq->current_nr_sectors = tape->capabilities.ctl;
3706 		idetape_init_stage(drive, stage, OS_FRAME_TYPE_DATA, logical_blk_num++);
3707 		stage->next = NULL;
3708 		if (last)
3709 			last->next = stage;
3710 		last = stage;
3711 	}
3712 	if (frames) {
3713 		spin_lock_irqsave(&tape->spinlock, flags);
3714 		last->next = tape->first_stage;
3715 		tape->next_stage = tape->first_stage = first;
3716 		tape->nr_stages += frames;
3717 		tape->nr_pending_stages += frames;
3718 		spin_unlock_irqrestore(&tape->spinlock, flags);
3719 	}
3720 	idetape_update_stats(drive);
3721 #if ONSTREAM_DEBUG
3722 	if (tape->debug_level >= 2)
3723 		printk(KERN_INFO "ide-tape: %s: frames left in buffer: %d\n",
3724 			tape->name, tape->cur_frames);
3725 #endif
3726 }
3727 
3728 /*
3729  * Error recovery algorithm for the OnStream tape.
3730  */
idetape_onstream_write_error_recovery(ide_drive_t * drive)3731 static void idetape_onstream_write_error_recovery (ide_drive_t *drive)
3732 {
3733 	idetape_tape_t *tape = drive->driver_data;
3734 	unsigned int block;
3735 
3736 	if (tape->onstream_write_error == OS_WRITE_ERROR) {
3737 		printk(KERN_ERR "ide-tape: %s: onstream_write_error_recovery: detected physical bad block at %u, logical %u first frame %u last_frame %u bufblocks %u stages %u skipping %u frames\n",
3738 			tape->name, ntohl(tape->sense.information), tape->logical_blk_num,
3739 			tape->first_frame_position, tape->last_frame_position,
3740 			tape->blocks_in_buffer, tape->nr_stages,
3741  			(ntohl(tape->sense.command_specific) >> 16) & 0xff );
3742 		block = ntohl(tape->sense.information) + ((ntohl(tape->sense.command_specific) >> 16) & 0xff);
3743 		idetape_update_stats(drive);
3744 		printk(KERN_ERR "ide-tape: %s: relocating %d buffered logical blocks to physical block %u\n", tape->name, tape->cur_frames, block);
3745 #if 0  /* isn't once enough ??? MM */
3746 		idetape_update_stats(drive);
3747 #endif
3748 		if (tape->firmware_revision_num >= 106)
3749 			idetape_position_tape(drive, block, 0, 1);
3750 		else {
3751 			idetape_onstream_read_back_buffer(drive);
3752 			idetape_position_tape(drive, block, 0, 0);
3753 		}
3754 #if 0     /* already done in idetape_position_tape MM */
3755 		idetape_read_position(drive);
3756 #endif
3757 #if ONSTREAM_DEBUG
3758 		if (tape->debug_level >= 1)
3759 			printk(KERN_ERR "ide-tape: %s: positioning complete, cur_frames %d, pos %d, tape pos %d\n", tape->name, tape->cur_frames, tape->first_frame_position, tape->last_frame_position);
3760 #endif
3761 	} else if (tape->onstream_write_error == OS_PART_ERROR) {
3762 #if ONSTREAM_DEBUG
3763 		if (tape->debug_level >= 1)
3764 			printk(KERN_INFO "ide-tape: %s: skipping over config partition\n", tape->name);
3765 #endif
3766 		idetape_flush_tape_buffers(drive);
3767 		block = idetape_read_position(drive);
3768 		if (block != OS_DATA_ENDFRAME1)
3769 			printk(KERN_ERR "ide-tape: warning, current position %d, expected %d\n", block, OS_DATA_ENDFRAME1);
3770 		idetape_position_tape(drive, 0xbb8, 0, 0); /* 3000 */
3771 	}
3772 	tape->onstream_write_error = 0;
3773 }
3774 
3775 /*
3776  *	idetape_insert_pipeline_into_queue is used to start servicing the
3777  *	pipeline stages, starting from tape->next_stage.
3778  */
idetape_insert_pipeline_into_queue(ide_drive_t * drive)3779 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
3780 {
3781 	idetape_tape_t *tape = drive->driver_data;
3782 
3783 	if (tape->next_stage == NULL)
3784 		return;
3785 	if (!idetape_pipeline_active(tape)) {
3786 		if (tape->onstream_write_error)
3787 			idetape_onstream_write_error_recovery(drive);
3788 		set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
3789 		idetape_active_next_stage(drive);
3790 		(void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
3791 	}
3792 }
3793 
idetape_create_inquiry_cmd(idetape_pc_t * pc)3794 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
3795 {
3796 	idetape_init_pc(pc);
3797 	pc->c[0] = IDETAPE_INQUIRY_CMD;
3798 	pc->c[4] = pc->request_transfer = 254;
3799 	pc->callback = &idetape_pc_callback;
3800 }
3801 
idetape_create_rewind_cmd(ide_drive_t * drive,idetape_pc_t * pc)3802 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
3803 {
3804 	idetape_tape_t *tape = drive->driver_data;
3805 
3806 	idetape_init_pc(pc);
3807 	pc->c[0] = IDETAPE_REWIND_CMD;
3808 	if (tape->onstream)
3809 		pc->c[1] = 1;
3810 	set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3811 	pc->callback = &idetape_pc_callback;
3812 }
3813 
idetape_create_mode_select_cmd(idetape_pc_t * pc,int length)3814 static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
3815 {
3816 	idetape_init_pc(pc);
3817 	set_bit(PC_WRITING, &pc->flags);
3818 	pc->c[0] = IDETAPE_MODE_SELECT_CMD;
3819 	pc->c[1] = 0x10;
3820 	put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
3821 	pc->request_transfer = 255;
3822 	pc->callback = &idetape_pc_callback;
3823 }
3824 
idetape_create_erase_cmd(idetape_pc_t * pc)3825 static void idetape_create_erase_cmd (idetape_pc_t *pc)
3826 {
3827 	idetape_init_pc(pc);
3828 	pc->c[0] = IDETAPE_ERASE_CMD;
3829 	pc->c[1] = 1;
3830 	set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3831 	pc->callback = &idetape_pc_callback;
3832 }
3833 
idetape_create_space_cmd(idetape_pc_t * pc,int count,u8 cmd)3834 static void idetape_create_space_cmd (idetape_pc_t *pc,int count,u8 cmd)
3835 {
3836 	idetape_init_pc(pc);
3837 	pc->c[0] = IDETAPE_SPACE_CMD;
3838 	put_unaligned(htonl (count), (unsigned int *) &pc->c[1]);
3839 	pc->c[1] = cmd;
3840 	set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3841 	pc->callback = &idetape_pc_callback;
3842 }
3843 
3844 /*
3845  * Verify that we have the correct tape frame
3846  */
idetape_verify_stage(ide_drive_t * drive,idetape_stage_t * stage,int logical_blk_num,int quiet)3847 static int idetape_verify_stage (ide_drive_t *drive, idetape_stage_t *stage, int logical_blk_num, int quiet)
3848 {
3849 	idetape_tape_t *tape = drive->driver_data;
3850 	os_aux_t *aux = stage->aux;
3851 	os_partition_t *par = &aux->partition;
3852 	struct request *rq = &stage->rq;
3853 	struct buffer_head *bh;
3854 
3855 	if (!tape->onstream)
3856 		return 1;
3857 	if (tape->raw) {
3858 		if (rq->errors) {
3859 			bh = stage->bh;
3860 			while (bh) {
3861 				memset(bh->b_data, 0, bh->b_size);
3862 				bh = bh->b_reqnext;
3863 			}
3864 			strcpy(stage->bh->b_data, "READ ERROR ON FRAME");
3865 		}
3866 		return 1;
3867 	}
3868 	if (rq->errors == IDETAPE_ERROR_GENERAL) {
3869 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3870 			"read error\n", tape->name, tape->first_frame_position);
3871 		return 0;
3872 	}
3873 	if (rq->errors == IDETAPE_ERROR_EOD) {
3874 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, eod\n",
3875 			tape->name, tape->first_frame_position);
3876 		return 0;
3877 	}
3878 	if (ntohl(aux->format_id) != 0) {
3879 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3880 			"format_id %u\n", tape->name,
3881 			tape->first_frame_position, ntohl(aux->format_id));
3882 		return 0;
3883 	}
3884 	if (memcmp(aux->application_sig, tape->application_sig, 4) != 0) {
3885 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3886 			"incorrect application signature\n",
3887 			tape->name, tape->first_frame_position);
3888 		return 0;
3889 	}
3890 	if (aux->frame_type != OS_FRAME_TYPE_DATA &&
3891 	    aux->frame_type != OS_FRAME_TYPE_EOD &&
3892 	    aux->frame_type != OS_FRAME_TYPE_MARKER) {
3893 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, frame "
3894 			"type %x\n", tape->name,
3895 			tape->first_frame_position, aux->frame_type);
3896 		return 0;
3897 	}
3898 	if (par->partition_num != OS_DATA_PARTITION) {
3899 		if (!tape->linux_media || tape->linux_media_version != 2) {
3900 			printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3901 				"partition num %d\n", tape->name,
3902 				tape->first_frame_position, par->partition_num);
3903 			return 0;
3904 		}
3905 	}
3906 	if (par->par_desc_ver != OS_PARTITION_VERSION) {
3907 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3908 			"partition version %d\n", tape->name,
3909 			tape->first_frame_position, par->par_desc_ver);
3910 		return 0;
3911 	}
3912 	if (ntohs(par->wrt_pass_cntr) != tape->wrt_pass_cntr) {
3913 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3914 			"wrt_pass_cntr %d (expected %d)(logical_blk_num %u)\n",
3915 			tape->name, tape->first_frame_position,
3916 			ntohs(par->wrt_pass_cntr), tape->wrt_pass_cntr,
3917 			ntohl(aux->logical_blk_num));
3918 		return 0;
3919 	}
3920 	if (aux->frame_seq_num != aux->logical_blk_num) {
3921 		printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3922 			"seq != logical\n", tape->name,
3923 			tape->first_frame_position);
3924 		return 0;
3925 	}
3926 	if (logical_blk_num != -1 &&
3927 	     ntohl(aux->logical_blk_num) != logical_blk_num) {
3928 		if (!quiet)
3929 			printk(KERN_INFO "ide-tape: %s: skipping frame %d, "
3930 				"logical_blk_num %u (expected %d)\n",
3931 				tape->name, tape->first_frame_position,
3932 				ntohl(aux->logical_blk_num), logical_blk_num);
3933 		return 0;
3934 	}
3935 	if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
3936 		rq->errors = IDETAPE_ERROR_FILEMARK;
3937 		rq->current_nr_sectors = rq->nr_sectors;
3938 	}
3939 	return 1;
3940 }
3941 
idetape_wait_first_stage(ide_drive_t * drive)3942 static void idetape_wait_first_stage (ide_drive_t *drive)
3943 {
3944 	idetape_tape_t *tape = drive->driver_data;
3945 	unsigned long flags;
3946 
3947 	if (tape->first_stage == NULL)
3948 		return;
3949 	spin_lock_irqsave(&tape->spinlock, flags);
3950 	if (tape->active_stage == tape->first_stage)
3951 		idetape_wait_for_request(drive, tape->active_data_request);
3952 	spin_unlock_irqrestore(&tape->spinlock, flags);
3953 }
3954 
3955 /*
3956  *	idetape_add_chrdev_write_request tries to add a character device
3957  *	originated write request to our pipeline. In case we don't succeed,
3958  *	we revert to non-pipelined operation mode for this request.
3959  *
3960  *	1.	Try to allocate a new pipeline stage.
3961  *	2.	If we can't, wait for more and more requests to be serviced
3962  *		and try again each time.
3963  *	3.	If we still can't allocate a stage, fallback to
3964  *		non-pipelined operation mode for this request.
3965  */
idetape_add_chrdev_write_request(ide_drive_t * drive,int blocks)3966 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3967 {
3968 	idetape_tape_t *tape = drive->driver_data;
3969 	idetape_stage_t *new_stage;
3970 	unsigned long flags;
3971 	struct request *rq;
3972 
3973 #if IDETAPE_DEBUG_LOG
3974 	if (tape->debug_level >= 3)
3975 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
3976 #endif /* IDETAPE_DEBUG_LOG */
3977 
3978      	/*
3979      	 *	Attempt to allocate a new stage.
3980 	 *	Pay special attention to possible race conditions.
3981 	 */
3982 	while ((new_stage = idetape_kmalloc_stage (tape)) == NULL) {
3983 		spin_lock_irqsave(&tape->spinlock, flags);
3984 		if (idetape_pipeline_active(tape)) {
3985 			idetape_wait_for_request(drive, tape->active_data_request);
3986 			spin_unlock_irqrestore(&tape->spinlock, flags);
3987 		} else {
3988 			spin_unlock_irqrestore(&tape->spinlock, flags);
3989 			idetape_insert_pipeline_into_queue(drive);
3990 			if (idetape_pipeline_active(tape))
3991 				continue;
3992 			/*
3993 			 *	Linux is short on memory. Fallback to
3994 			 *	non-pipelined operation mode for this request.
3995 			 */
3996 			return idetape_queue_rw_tail(drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
3997 		}
3998 	}
3999 	rq = &new_stage->rq;
4000 	ide_init_drive_cmd(rq);
4001 	rq->cmd = IDETAPE_WRITE_RQ;
4002 	/* Doesn't actually matter - We always assume sequential access */
4003 	rq->sector = tape->first_frame_position;
4004 	rq->nr_sectors = rq->current_nr_sectors = blocks;
4005 
4006 	idetape_switch_buffers (tape, new_stage);
4007 	idetape_init_stage(drive, new_stage, OS_FRAME_TYPE_DATA, tape->logical_blk_num);
4008 	tape->logical_blk_num++;
4009 	idetape_add_stage_tail(drive, new_stage);
4010 	tape->pipeline_head++;
4011 #if USE_IOTRACE
4012 	IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
4013 #endif
4014 	calculate_speeds(drive);
4015 
4016 	/*
4017 	 *	Estimate whether the tape has stopped writing by checking
4018 	 *	if our write pipeline is currently empty. If we are not
4019 	 *	writing anymore, wait for the pipeline to be full enough
4020 	 *	(90%) before starting to service requests, so that we will
4021 	 *	be able to keep up with the higher speeds of the tape.
4022 	 *
4023 	 *	For the OnStream drive, we can query the number of pending
4024 	 *	frames in the drive's internal buffer. As long as the tape
4025 	 *	is still writing, it is better to write frames immediately
4026 	 *	rather than gather them in the pipeline. This will give the
4027 	 *	tape's firmware the ability to sense the current incoming
4028 	 *	data rate more accurately, and since the OnStream tape
4029 	 *	supports variable speeds, it can try to adjust itself to the
4030 	 *	incoming data rate.
4031 	 */
4032 	if (!idetape_pipeline_active(tape)) {
4033 		if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
4034 		    tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
4035 			tape->measure_insert_time = 1;
4036 			tape->insert_time = jiffies;
4037 			tape->insert_size = 0;
4038 			tape->insert_speed = 0;
4039 			idetape_insert_pipeline_into_queue (drive);
4040 		} else if (tape->onstream) {
4041 			idetape_update_stats(drive);
4042 			if (tape->cur_frames > 5)
4043 				idetape_insert_pipeline_into_queue(drive);
4044 		}
4045 	}
4046 	if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
4047 		/* Return a deferred error */
4048 		return -EIO;
4049 	return blocks;
4050 }
4051 
4052 /*
4053  *	idetape_wait_for_pipeline will wait until all pending pipeline
4054  *	requests are serviced. Typically called on device close.
4055  */
idetape_wait_for_pipeline(ide_drive_t * drive)4056 static void idetape_wait_for_pipeline (ide_drive_t *drive)
4057 {
4058 	idetape_tape_t *tape = drive->driver_data;
4059 	unsigned long flags;
4060 
4061 	while (tape->next_stage || idetape_pipeline_active(tape)) {
4062 		idetape_insert_pipeline_into_queue(drive);
4063 		spin_lock_irqsave(&tape->spinlock, flags);
4064 		if (idetape_pipeline_active(tape))
4065 			idetape_wait_for_request(drive, tape->active_data_request);
4066 		spin_unlock_irqrestore(&tape->spinlock, flags);
4067 	}
4068 }
4069 
idetape_empty_write_pipeline(ide_drive_t * drive)4070 static void idetape_empty_write_pipeline (ide_drive_t *drive)
4071 {
4072 	idetape_tape_t *tape = drive->driver_data;
4073 	int blocks, i, min;
4074 	struct buffer_head *bh;
4075 
4076 #if IDETAPE_DEBUG_BUGS
4077 	if (tape->chrdev_direction != idetape_direction_write) {
4078 		printk(KERN_ERR "ide-tape: bug: Trying to empty write "
4079 			"pipeline, but we are not writing.\n");
4080 		return;
4081 	}
4082 	if (tape->merge_stage_size > tape->stage_size) {
4083 		printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
4084 		tape->merge_stage_size = tape->stage_size;
4085 	}
4086 #endif /* IDETAPE_DEBUG_BUGS */
4087 	if (tape->merge_stage_size) {
4088 		blocks = tape->merge_stage_size / tape->tape_block_size;
4089 		if (tape->merge_stage_size % tape->tape_block_size) {
4090 			blocks++;
4091 			i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
4092 			bh = tape->bh->b_reqnext;
4093 			while (bh) {
4094 				atomic_set(&bh->b_count, 0);
4095 				bh = bh->b_reqnext;
4096 			}
4097 			bh = tape->bh;
4098 			while (i) {
4099 				if (bh == NULL) {
4100 					printk(KERN_INFO "ide-tape: bug, bh NULL\n");
4101 					break;
4102 				}
4103 				min = IDE_MIN(i, bh->b_size - atomic_read(&bh->b_count));
4104 				memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
4105 				atomic_add(min, &bh->b_count);
4106 				i -= min;
4107 				bh = bh->b_reqnext;
4108 			}
4109 		}
4110 		(void) idetape_add_chrdev_write_request(drive, blocks);
4111 		tape->merge_stage_size = 0;
4112 	}
4113 	idetape_wait_for_pipeline(drive);
4114 	if (tape->merge_stage != NULL) {
4115 		__idetape_kfree_stage(tape->merge_stage);
4116 		tape->merge_stage = NULL;
4117 	}
4118 	clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
4119 	tape->chrdev_direction = idetape_direction_none;
4120 
4121 	/*
4122 	 *	On the next backup, perform the feedback loop again.
4123 	 *	(I don't want to keep sense information between backups,
4124 	 *	 as some systems are constantly on, and the system load
4125 	 *	 can be totally different on the next backup).
4126 	 */
4127 	tape->max_stages = tape->min_pipeline;
4128 #if IDETAPE_DEBUG_BUGS
4129 	if (tape->first_stage != NULL ||
4130 	    tape->next_stage != NULL ||
4131 	    tape->last_stage != NULL ||
4132 	    tape->nr_stages != 0) {
4133 		printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
4134 			"first_stage %p, next_stage %p, last_stage %p, "
4135 			"nr_stages %d\n", tape->first_stage,
4136 			tape->next_stage, tape->last_stage, tape->nr_stages);
4137 	}
4138 #endif /* IDETAPE_DEBUG_BUGS */
4139 }
4140 
idetape_restart_speed_control(ide_drive_t * drive)4141 static void idetape_restart_speed_control (ide_drive_t *drive)
4142 {
4143 	idetape_tape_t *tape = drive->driver_data;
4144 
4145 	tape->restart_speed_control_req = 0;
4146 	tape->pipeline_head = 0;
4147 	tape->buffer_head = tape->tape_head = tape->cur_frames;
4148 	tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
4149 	tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
4150 	tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
4151 	tape->uncontrolled_pipeline_head_speed = 0;
4152 	tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
4153 	tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
4154 }
4155 
idetape_initiate_read(ide_drive_t * drive,int max_stages)4156 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
4157 {
4158 	idetape_tape_t *tape = drive->driver_data;
4159 	idetape_stage_t *new_stage;
4160 	struct request rq;
4161 	int bytes_read;
4162 	int blocks = tape->capabilities.ctl;
4163 
4164 	/* Initialize read operation */
4165 	if (tape->chrdev_direction != idetape_direction_read) {
4166 		if (tape->chrdev_direction == idetape_direction_write) {
4167 			idetape_empty_write_pipeline(drive);
4168 			idetape_flush_tape_buffers(drive);
4169 		}
4170 #if IDETAPE_DEBUG_BUGS
4171 		if (tape->merge_stage || tape->merge_stage_size) {
4172 			printk(KERN_ERR "ide-tape: merge_stage_size "
4173 				"should be 0 now\n");
4174 			tape->merge_stage_size = 0;
4175 		}
4176 #endif /* IDETAPE_DEBUG_BUGS */
4177 		if ((tape->merge_stage = __idetape_kmalloc_stage (tape, 0, 0)) == NULL)
4178 			return -ENOMEM;
4179 		tape->chrdev_direction = idetape_direction_read;
4180 		tape->logical_blk_num = 0;
4181 
4182 		/*
4183 		 *	Issue a read 0 command to ensure that DSC handshake
4184 		 *	is switched from completion mode to buffer available
4185 		 *	mode.
4186 		 */
4187 		bytes_read = idetape_queue_rw_tail(drive, IDETAPE_READ_RQ, 0, tape->merge_stage->bh);
4188 		if (bytes_read < 0) {
4189 			__idetape_kfree_stage(tape->merge_stage);
4190 			tape->merge_stage = NULL;
4191 			tape->chrdev_direction = idetape_direction_none;
4192 			return bytes_read;
4193 		}
4194 	}
4195 	if (tape->restart_speed_control_req)
4196 		idetape_restart_speed_control(drive);
4197 	ide_init_drive_cmd(&rq);
4198 	rq.cmd = IDETAPE_READ_RQ;
4199 	rq.sector = tape->first_frame_position;
4200 	rq.nr_sectors = rq.current_nr_sectors = blocks;
4201 	if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
4202 	    tape->nr_stages < max_stages) {
4203 		new_stage = idetape_kmalloc_stage(tape);
4204 		while (new_stage != NULL) {
4205 			new_stage->rq = rq;
4206 			idetape_add_stage_tail(drive, new_stage);
4207 			if (tape->nr_stages >= max_stages)
4208 				break;
4209 			new_stage = idetape_kmalloc_stage(tape);
4210 		}
4211 	}
4212 	if (!idetape_pipeline_active(tape)) {
4213 		if (tape->nr_pending_stages >= 3 * max_stages / 4) {
4214 			tape->measure_insert_time = 1;
4215 			tape->insert_time = jiffies;
4216 			tape->insert_size = 0;
4217 			tape->insert_speed = 0;
4218 			idetape_insert_pipeline_into_queue(drive);
4219 		} else if (tape->onstream) {
4220 			idetape_update_stats(drive);
4221 			if (tape->cur_frames < tape->max_frames - 5)
4222 				idetape_insert_pipeline_into_queue(drive);
4223 		}
4224 	}
4225 	return 0;
4226 }
4227 
idetape_get_logical_blk(ide_drive_t * drive,int logical_blk_num,int max_stages,int quiet)4228 static int idetape_get_logical_blk (ide_drive_t *drive, int logical_blk_num, int max_stages, int quiet)
4229 {
4230 	idetape_tape_t *tape = drive->driver_data;
4231 	unsigned long flags;
4232 	int cnt = 0, x, position;
4233 
4234 	/*
4235 	 * Search and wait for the next logical tape block
4236 	 */
4237 	while (1) {
4238 		if (cnt++ > 1000) {   /* AJN: was 100 */
4239 			printk(KERN_INFO "ide-tape: %s: couldn't find "
4240 				"logical block %d, aborting\n",
4241 				tape->name, logical_blk_num);
4242 			return 0;
4243 		}
4244 		idetape_initiate_read(drive, max_stages);
4245 		if (tape->first_stage == NULL) {
4246 			if (tape->onstream) {
4247 #if ONSTREAM_DEBUG
4248 				if (tape->debug_level >= 1)
4249 					printk(KERN_INFO "ide-tape: %s: first_stage == NULL, pipeline error %ld\n", tape->name, (long)test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags));
4250 #endif
4251 				clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
4252 				position = idetape_read_position(drive);
4253 				printk(KERN_INFO "ide-tape: %s: blank block "
4254 					"detected at %d\n", tape->name,
4255 					position);
4256 				if (position >= 3000 && position < 3080)
4257 					/* Why is this check and number ??? MM */
4258 					position += 32;
4259 				if (position >= OS_DATA_ENDFRAME1 &&
4260 				    position < 3000)
4261 					position = 3000;
4262 				else
4263 					/*
4264 					 * compensate for write errors that
4265 					 * generally skip 80 frames, expect
4266 					 * around 20 read errors in a row...
4267 					 */
4268 					position += 60;
4269 				if (position >= OS_DATA_ENDFRAME1 &&
4270 				    position < 3000)
4271 					position = 3000;
4272 				printk(KERN_INFO "ide-tape: %s: positioning tape to block %d\n", tape->name, position);
4273 
4274 				/* seems to be needed to correctly position
4275 				 * at block 3000 MM
4276 				 */
4277 				if (position == 3000)
4278 					idetape_position_tape(drive, 0, 0, 0);
4279 				idetape_position_tape(drive, position, 0, 0);
4280 				cnt += 40;
4281 				continue;
4282 			} else
4283 				return 0;
4284 		}
4285 		idetape_wait_first_stage(drive);
4286 		if (idetape_verify_stage(drive, tape->first_stage, logical_blk_num, quiet))
4287 			break;
4288 		if (tape->first_stage->rq.errors == IDETAPE_ERROR_EOD)
4289 			cnt--;
4290 		if (idetape_verify_stage(drive, tape->first_stage, -1, quiet)) {
4291 			x = ntohl(tape->first_stage->aux->logical_blk_num);
4292 			if (x > logical_blk_num) {
4293 				printk(KERN_ERR "ide-tape: %s: couldn't find logical block %d, aborting (block %d found)\n", tape->name, logical_blk_num, x);
4294 				return 0;
4295 			}
4296 		}
4297 		spin_lock_irqsave(&tape->spinlock, flags);
4298 		idetape_remove_stage_head(drive);
4299 		spin_unlock_irqrestore(&tape->spinlock, flags);
4300 	}
4301 	if (tape->onstream)
4302 		tape->logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num);
4303 	return 1;
4304 }
4305 
4306 /*
4307  *	idetape_add_chrdev_read_request is called from idetape_chrdev_read
4308  *	to service a character device read request and add read-ahead
4309  *	requests to our pipeline.
4310  */
idetape_add_chrdev_read_request(ide_drive_t * drive,int blocks)4311 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
4312 {
4313 	idetape_tape_t *tape = drive->driver_data;
4314 	unsigned long flags;
4315 	struct request *rq_ptr;
4316 	int bytes_read;
4317 
4318 #if IDETAPE_DEBUG_LOG
4319 	if (tape->debug_level >= 4)
4320 		printk(KERN_INFO "ide-tape: Reached %s, %d blocks\n",
4321 			__FUNCTION__, blocks);
4322 #endif /* IDETAPE_DEBUG_LOG */
4323 
4324 	/*
4325 	 * If we are at a filemark, return a read length of 0
4326 	 */
4327 	if (test_bit(IDETAPE_FILEMARK, &tape->flags))
4328 		return 0;
4329 
4330 	/*
4331 	 * Wait for the next logical block to be available at the head
4332 	 * of the pipeline
4333 	 */
4334 	if (!idetape_get_logical_blk(drive, tape->logical_blk_num, tape->max_stages, 0)) {
4335 		if (tape->onstream) {
4336 			set_bit(IDETAPE_READ_ERROR, &tape->flags);
4337 			return 0;
4338 		}
4339 		if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
4340 		 	return 0;
4341 		return idetape_queue_rw_tail(drive, IDETAPE_READ_RQ, blocks, tape->merge_stage->bh);
4342 	}
4343 	rq_ptr = &tape->first_stage->rq;
4344 	bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
4345 	rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
4346 
4347 
4348 	if (tape->onstream && !tape->raw &&
4349 	    tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
4350 #if ONSTREAM_DEBUG
4351 		if (tape->debug_level >= 2)
4352 			printk(KERN_INFO "ide-tape: %s: EOD reached\n",
4353 				tape->name);
4354 #endif
4355 		return 0;
4356 	}
4357 	if (rq_ptr->errors == IDETAPE_ERROR_EOD)
4358 		return 0;
4359 	else {
4360 		idetape_switch_buffers(tape, tape->first_stage);
4361 		if (rq_ptr->errors == IDETAPE_ERROR_GENERAL) {
4362 #if ONSTREAM_DEBUG
4363 			if (tape->debug_level >= 1)
4364 				printk(KERN_INFO "ide-tape: error detected, "
4365 					"bytes_read %d\n", bytes_read);
4366 #endif
4367 		}
4368 		if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
4369 			set_bit(IDETAPE_FILEMARK, &tape->flags);
4370 		spin_lock_irqsave(&tape->spinlock, flags);
4371 		idetape_remove_stage_head (drive);
4372 		spin_unlock_irqrestore(&tape->spinlock, flags);
4373 		tape->logical_blk_num++;
4374 		tape->pipeline_head++;
4375 #if USE_IOTRACE
4376 		IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
4377 #endif
4378 		calculate_speeds(drive);
4379 	}
4380 #if IDETAPE_DEBUG_BUGS
4381 	if (bytes_read > blocks * tape->tape_block_size) {
4382 		printk(KERN_ERR "ide-tape: bug: trying to return more "
4383 			"bytes than requested\n");
4384 		bytes_read = blocks * tape->tape_block_size;
4385 	}
4386 #endif /* IDETAPE_DEBUG_BUGS */
4387 	return (bytes_read);
4388 }
4389 
idetape_pad_zeros(ide_drive_t * drive,int bcount)4390 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
4391 {
4392 	idetape_tape_t *tape = drive->driver_data;
4393 	struct buffer_head *bh;
4394 	int count, blocks;
4395 
4396 	while (bcount) {
4397 		bh = tape->merge_stage->bh;
4398 		count = IDE_MIN(tape->stage_size, bcount);
4399 		bcount -= count;
4400 		blocks = count / tape->tape_block_size;
4401 		while (count) {
4402 			atomic_set(&bh->b_count, IDE_MIN (count, bh->b_size));
4403 			memset(bh->b_data, 0, atomic_read(&bh->b_count));
4404 			count -= atomic_read(&bh->b_count);
4405 			bh = bh->b_reqnext;
4406 		}
4407 		idetape_queue_rw_tail(drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
4408 	}
4409 }
4410 
idetape_pipeline_size(ide_drive_t * drive)4411 static int idetape_pipeline_size (ide_drive_t *drive)
4412 {
4413 	idetape_tape_t *tape = drive->driver_data;
4414 	idetape_stage_t *stage;
4415 	struct request *rq;
4416 	int size = 0;
4417 
4418 	idetape_wait_for_pipeline(drive);
4419 	stage = tape->first_stage;
4420 	while (stage != NULL) {
4421 		rq = &stage->rq;
4422 		size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
4423 		if (rq->errors == IDETAPE_ERROR_FILEMARK)
4424 			size += tape->tape_block_size;
4425 		stage = stage->next;
4426 	}
4427 	size += tape->merge_stage_size;
4428 	return size;
4429 }
4430 
4431 /*
4432  *	Rewinds the tape to the Beginning Of the current Partition (BOP).
4433  *
4434  *	We currently support only one partition.
4435  */
idetape_rewind_tape(ide_drive_t * drive)4436 static int idetape_rewind_tape (ide_drive_t *drive)
4437 {
4438 	int retval;
4439 	idetape_pc_t pc;
4440 	idetape_tape_t *tape = drive->driver_data;
4441 #if IDETAPE_DEBUG_LOG
4442 	if (tape->debug_level >= 2)
4443 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
4444 #endif /* IDETAPE_DEBUG_LOG */
4445 
4446 	idetape_create_rewind_cmd(drive, &pc);
4447 	retval = idetape_queue_pc_tail(drive, &pc);
4448 	if (retval)
4449 		return retval;
4450 
4451 	idetape_create_read_position_cmd(&pc);
4452 	retval = idetape_queue_pc_tail(drive, &pc);
4453 	if (retval)
4454 		return retval;
4455 	tape->logical_blk_num = 0;
4456 	return 0;
4457 }
4458 
4459 /*
4460  *	Our special ide-tape ioctl's.
4461  *
4462  *	Currently there aren't any ioctl's.
4463  *	mtio.h compatible commands should be issued to the character device
4464  *	interface.
4465  */
idetape_blkdev_ioctl(ide_drive_t * drive,struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)4466 static int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
4467 				 unsigned int cmd, unsigned long arg)
4468 {
4469 	idetape_tape_t *tape = drive->driver_data;
4470 	idetape_config_t config;
4471 
4472 #if IDETAPE_DEBUG_LOG
4473 	if (tape->debug_level >= 4)
4474 		printk(KERN_INFO "ide-tape: Reached %s\n", __FUNCTION__);
4475 #endif /* IDETAPE_DEBUG_LOG */
4476 	switch (cmd) {
4477 		case 0x0340:
4478 			if (copy_from_user ((char *) &config, (char *) arg, sizeof (idetape_config_t)))
4479 				return -EFAULT;
4480 			tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
4481 			tape->max_stages = config.nr_stages;
4482 			break;
4483 		case 0x0350:
4484 			config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
4485 			config.nr_stages = tape->max_stages;
4486 			if (copy_to_user ((char *) arg, (char *) &config, sizeof (idetape_config_t)))
4487 				return -EFAULT;
4488 			break;
4489 		default:
4490 			return -EIO;
4491 	}
4492 	return 0;
4493 }
4494 
4495 /*
4496  *	The block device interface should not be used for data transfers.
4497  *	However, we still allow opening it so that we can issue general
4498  *	ide driver configuration ioctl's, such as the interrupt unmask feature.
4499  */
idetape_blkdev_open(struct inode * inode,struct file * filp,ide_drive_t * drive)4500 static int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
4501 {
4502 	MOD_INC_USE_COUNT;
4503 #if ONSTREAM_DEBUG
4504         printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in %s\n", __FUNCTION__);
4505 #endif
4506 	return 0;
4507 }
4508 
idetape_blkdev_release(struct inode * inode,struct file * filp,ide_drive_t * drive)4509 static void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
4510 {
4511 	MOD_DEC_USE_COUNT;
4512 #if ONSTREAM_DEBUG
4513         printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in %s\n", __FUNCTION__);
4514 #endif
4515 }
4516 
4517 /*
4518  *	idetape_pre_reset is called before an ATAPI/ATA software reset.
4519  */
idetape_pre_reset(ide_drive_t * drive)4520 static void idetape_pre_reset (ide_drive_t *drive)
4521 {
4522 	idetape_tape_t *tape = drive->driver_data;
4523 	if (tape != NULL)
4524 		set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
4525 }
4526 
4527 /*
4528  *	Character device interface functions
4529  */
get_drive_ptr(kdev_t i_rdev)4530 static ide_drive_t *get_drive_ptr (kdev_t i_rdev)
4531 {
4532 	unsigned int i = MINOR(i_rdev) & ~0xc0;
4533 
4534 	if (i >= MAX_HWIFS * MAX_DRIVES)
4535 		return NULL;
4536 	return (idetape_chrdevs[i].drive);
4537 }
4538 
idetape_onstream_space_over_filemarks_backward(ide_drive_t * drive,short mt_op,int mt_count)4539 static int idetape_onstream_space_over_filemarks_backward (ide_drive_t *drive,short mt_op,int mt_count)
4540 {
4541 	idetape_tape_t *tape = drive->driver_data;
4542 	int cnt = 0;
4543 	int last_mark_addr;
4544 	unsigned long flags;
4545 
4546 	if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4547 		printk(KERN_INFO "ide-tape: %s: couldn't get logical blk "
4548 			"num in space_filemarks_bwd\n", tape->name);
4549 		return -EIO;
4550 	}
4551 	while (cnt != mt_count) {
4552 		last_mark_addr = ntohl(tape->first_stage->aux->last_mark_addr);
4553 		if (last_mark_addr == -1)
4554 			return -EIO;
4555 #if ONSTREAM_DEBUG
4556 		if (tape->debug_level >= 2)
4557 			printk(KERN_INFO "ide-tape: positioning to last "
4558 			"mark at %d\n", last_mark_addr);
4559 #endif
4560 		idetape_position_tape(drive, last_mark_addr, 0, 0);
4561 		cnt++;
4562 		if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4563 			printk(KERN_INFO "ide-tape: %s: couldn't get logical "
4564 				"blk num in space_filemarks\n", tape->name);
4565 			return -EIO;
4566 		}
4567 		if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
4568 			printk(KERN_INFO "ide-tape: %s: expected to find "
4569 				"marker at block %d, not found\n",
4570 				tape->name, last_mark_addr);
4571 			return -EIO;
4572 		}
4573 	}
4574 	if (mt_op == MTBSFM) {
4575 		spin_lock_irqsave(&tape->spinlock, flags);
4576 		idetape_remove_stage_head (drive);
4577 		tape->logical_blk_num++;
4578 		spin_unlock_irqrestore(&tape->spinlock, flags);
4579 	}
4580 	return 0;
4581 }
4582 
4583 /*
4584  * ADRL 1.1 compatible "slow" space filemarks fwd version
4585  *
4586  * Just scans for the filemark sequentially.
4587  */
idetape_onstream_space_over_filemarks_forward_slow(ide_drive_t * drive,short mt_op,int mt_count)4588 static int idetape_onstream_space_over_filemarks_forward_slow (ide_drive_t *drive,short mt_op,int mt_count)
4589 {
4590 	idetape_tape_t *tape = drive->driver_data;
4591 	int cnt = 0;
4592 	unsigned long flags;
4593 
4594 	if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4595 		printk(KERN_INFO "ide-tape: %s: couldn't get logical blk "
4596 			"num in space_filemarks_fwd\n", tape->name);
4597 		return -EIO;
4598 	}
4599 	while (1) {
4600 		if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4601 			printk(KERN_INFO "ide-tape: %s: couldn't get logical "
4602 				"blk num in space_filemarks\n", tape->name);
4603 			return -EIO;
4604 		}
4605 		if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_MARKER)
4606 			cnt++;
4607 		if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
4608 #if ONSTREAM_DEBUG
4609 			if (tape->debug_level >= 2)
4610 				printk(KERN_INFO "ide-tape: %s: space_fwd: "
4611 					"EOD reached\n", tape->name);
4612 #endif
4613 			return -EIO;
4614 		}
4615 		if (cnt == mt_count)
4616 			break;
4617 		spin_lock_irqsave(&tape->spinlock, flags);
4618 		idetape_remove_stage_head(drive);
4619 		spin_unlock_irqrestore(&tape->spinlock, flags);
4620 	}
4621 	if (mt_op == MTFSF) {
4622 		spin_lock_irqsave(&tape->spinlock, flags);
4623 		idetape_remove_stage_head(drive);
4624 		tape->logical_blk_num++;
4625 		spin_unlock_irqrestore(&tape->spinlock, flags);
4626 	}
4627 	return 0;
4628 }
4629 
4630 
4631 /*
4632  * Fast linux specific version of OnStream FSF
4633  */
idetape_onstream_space_over_filemarks_forward_fast(ide_drive_t * drive,short mt_op,int mt_count)4634 static int idetape_onstream_space_over_filemarks_forward_fast (ide_drive_t *drive,short mt_op,int mt_count)
4635 {
4636 	idetape_tape_t *tape = drive->driver_data;
4637 	int cnt = 0, next_mark_addr;
4638 	unsigned long flags;
4639 
4640 	if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4641 		printk(KERN_INFO "ide-tape: %s: couldn't get logical blk "
4642 			"num in space_filemarks_fwd\n", tape->name);
4643 		return -EIO;
4644 	}
4645 
4646 	/*
4647 	 * Find nearest (usually previous) marker
4648 	 */
4649 	while (1) {
4650 		if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_MARKER)
4651 			break;
4652 		if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
4653 #if ONSTREAM_DEBUG
4654 			if (tape->debug_level >= 2)
4655 				printk(KERN_INFO "ide-tape: %s: space_fwd: "
4656 					"EOD reached\n", tape->name);
4657 #endif
4658 			return -EIO;
4659 		}
4660 		if (ntohl(tape->first_stage->aux->filemark_cnt) == 0) {
4661 			if (tape->first_mark_addr == -1) {
4662 				printk(KERN_INFO "ide-tape: %s: reverting to "
4663 					"slow filemark space\n", tape->name);
4664 				return idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count);
4665 			}
4666 			idetape_position_tape(drive, tape->first_mark_addr, 0, 0);
4667 			if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4668 				printk(KERN_INFO "ide-tape: %s: couldn't get "
4669 					"logical blk num in %s\n",							__FUNCTION__, tape->name);
4670 				return -EIO;
4671 			}
4672 			if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
4673 				printk(KERN_INFO "ide-tape: %s: expected to "
4674 					"find filemark at %d\n",
4675 					tape->name, tape->first_mark_addr);
4676 				return -EIO;
4677 			}
4678 		} else {
4679 			if (idetape_onstream_space_over_filemarks_backward(drive, MTBSF, 1) < 0)
4680 				return -EIO;
4681 			mt_count++;
4682 		}
4683 	}
4684 	cnt++;
4685 	while (cnt != mt_count) {
4686 		next_mark_addr = ntohl(tape->first_stage->aux->next_mark_addr);
4687 		if (!next_mark_addr || next_mark_addr > tape->eod_frame_addr) {
4688 			printk(KERN_INFO "ide-tape: %s: reverting to slow "
4689 				"filemark space\n", tape->name);
4690 			return idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count - cnt);
4691 #if ONSTREAM_DEBUG
4692 		} else if (tape->debug_level >= 2) {
4693 		     printk(KERN_INFO "ide-tape: positioning to next mark at %d\n", next_mark_addr);
4694 #endif
4695 		}
4696 		idetape_position_tape(drive, next_mark_addr, 0, 0);
4697 		cnt++;
4698 		if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4699 			printk(KERN_INFO "ide-tape: %s: couldn't get logical "
4700 				"blk num in space_filemarks\n", tape->name);
4701 			return -EIO;
4702 		}
4703 		if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
4704 			printk(KERN_INFO "ide-tape: %s: expected to find "
4705 				"marker at block %d, not found\n",
4706 				tape->name, next_mark_addr);
4707 			return -EIO;
4708 		}
4709 	}
4710 	if (mt_op == MTFSF) {
4711 		spin_lock_irqsave(&tape->spinlock, flags);
4712 		idetape_remove_stage_head(drive);
4713 		tape->logical_blk_num++;
4714 		spin_unlock_irqrestore(&tape->spinlock, flags);
4715 	}
4716 	return 0;
4717 }
4718 
4719 /*
4720  *	idetape_space_over_filemarks is now a bit more complicated than just
4721  *	passing the command to the tape since we may have crossed some
4722  *	filemarks during our pipelined read-ahead mode.
4723  *
4724  *	As a minor side effect, the pipeline enables us to support MTFSFM when
4725  *	the filemark is in our internal pipeline even if the tape doesn't
4726  *	support spacing over filemarks in the reverse direction.
4727  */
idetape_space_over_filemarks(ide_drive_t * drive,short mt_op,int mt_count)4728 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
4729 {
4730 	idetape_tape_t *tape = drive->driver_data;
4731 	idetape_pc_t pc;
4732 	unsigned long flags;
4733 	int retval,count=0;
4734 	int speed_control;
4735 
4736 	if (tape->onstream) {
4737 		if (tape->raw)
4738 			return -EIO;
4739 		speed_control = tape->speed_control;
4740 		tape->speed_control = 0;
4741 		if (mt_op == MTFSF || mt_op == MTFSFM) {
4742 			if (tape->linux_media)
4743 				retval = idetape_onstream_space_over_filemarks_forward_fast(drive, mt_op, mt_count);
4744 			else
4745 				retval = idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count);
4746 		} else
4747 			retval = idetape_onstream_space_over_filemarks_backward(drive, mt_op, mt_count);
4748 		tape->speed_control = speed_control;
4749 		tape->restart_speed_control_req = 1;
4750 		return retval;
4751 	}
4752 
4753 	if (mt_count == 0)
4754 		return 0;
4755 	if (MTBSF == mt_op || MTBSFM == mt_op) {
4756 		if (!tape->capabilities.sprev)
4757 			return -EIO;
4758 		mt_count = - mt_count;
4759 	}
4760 
4761 	if (tape->chrdev_direction == idetape_direction_read) {
4762 		/*
4763 		 *	We have a read-ahead buffer. Scan it for crossed
4764 		 *	filemarks.
4765 		 */
4766 		tape->merge_stage_size = 0;
4767 		if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
4768 			++count;
4769 		while (tape->first_stage != NULL) {
4770 			if (count == mt_count) {
4771 				if (mt_op == MTFSFM)
4772 					set_bit(IDETAPE_FILEMARK, &tape->flags);
4773 				return 0;
4774 			}
4775 			spin_lock_irqsave(&tape->spinlock, flags);
4776 			if (tape->first_stage == tape->active_stage) {
4777 				/*
4778 				 *	We have reached the active stage in the read pipeline.
4779 				 *	There is no point in allowing the drive to continue
4780 				 *	reading any farther, so we stop the pipeline.
4781 				 *
4782 				 *	This section should be moved to a separate subroutine,
4783 				 *	because a similar function is performed in
4784 				 *	__idetape_discard_read_pipeline(), for example.
4785 				 */
4786 				tape->next_stage = NULL;
4787 				spin_unlock_irqrestore(&tape->spinlock, flags);
4788 				idetape_wait_first_stage(drive);
4789 				tape->next_stage = tape->first_stage->next;
4790 			} else
4791 				spin_unlock_irqrestore(&tape->spinlock, flags);
4792 			if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
4793 				++count;
4794 			idetape_remove_stage_head(drive);
4795 		}
4796 		idetape_discard_read_pipeline(drive, 0);
4797 	}
4798 
4799 	/*
4800 	 *	The filemark was not found in our internal pipeline.
4801 	 *	Now we can issue the space command.
4802 	 */
4803 	switch (mt_op) {
4804 		case MTFSF:
4805 		case MTBSF:
4806 			idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
4807 			return (idetape_queue_pc_tail(drive, &pc));
4808 		case MTFSFM:
4809 		case MTBSFM:
4810 			if (!tape->capabilities.sprev)
4811 				return (-EIO);
4812 			retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
4813 			if (retval) return (retval);
4814 			count = (MTBSFM == mt_op ? 1 : -1);
4815 			return (idetape_space_over_filemarks(drive, MTFSF, count));
4816 		default:
4817 			printk(KERN_ERR "ide-tape: MTIO operation %d not "
4818 				"supported\n", mt_op);
4819 			return (-EIO);
4820 	}
4821 }
4822 
4823 
4824 /*
4825  *	Our character device read / write functions.
4826  *
4827  *	The tape is optimized to maximize throughput when it is transferring
4828  *	an integral number of the "continuous transfer limit", which is
4829  *	a parameter of the specific tape (26 KB on my particular tape).
4830  *      (32 kB for Onstream)
4831  *
4832  *	As of version 1.3 of the driver, the character device provides an
4833  *	abstract continuous view of the media - any mix of block sizes (even 1
4834  *	byte) on the same backup/restore procedure is supported. The driver
4835  *	will internally convert the requests to the recommended transfer unit,
4836  *	so that an unmatch between the user's block size to the recommended
4837  *	size will only result in a (slightly) increased driver overhead, but
4838  *	will no longer hit performance.
4839  *      This is not applicable to Onstream.
4840  */
idetape_chrdev_read(struct file * file,char * buf,size_t count,loff_t * ppos)4841 static ssize_t idetape_chrdev_read (struct file *file, char *buf,
4842 				    size_t count, loff_t *ppos)
4843 {
4844 	struct inode *inode = file->f_dentry->d_inode;
4845 	ide_drive_t *drive = get_drive_ptr(inode->i_rdev);
4846 	idetape_tape_t *tape = drive->driver_data;
4847 	ssize_t bytes_read,temp, actually_read = 0, rc;
4848 
4849 	if (ppos != &file->f_pos) {
4850 		/* "A request was outside the capabilities of the device." */
4851 		return -ENXIO;
4852 	}
4853 	if (tape->onstream && (count != tape->tape_block_size)) {
4854 		printk(KERN_ERR "ide-tape: %s: use %d bytes as block size "
4855 			"(%Zd used)\n", tape->name,
4856 			tape->tape_block_size, count);
4857 		return -EINVAL;
4858 	}
4859 #if IDETAPE_DEBUG_LOG
4860 	if (tape->debug_level >= 3)
4861 		printk (KERN_INFO "ide-tape: Reached %s, count %Zd\n",
4862 			__FUNCTION__, count);
4863 #endif /* IDETAPE_DEBUG_LOG */
4864 
4865 	if (tape->chrdev_direction != idetape_direction_read) {
4866 		if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
4867 			if (count > tape->tape_block_size &&
4868 			    (count % tape->tape_block_size) == 0)
4869 				tape->user_bs_factor = count / tape->tape_block_size;
4870 	}
4871 	if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
4872 		return rc;
4873 	if (count == 0)
4874 		return (0);
4875 	if (tape->merge_stage_size) {
4876 		actually_read = IDE_MIN(tape->merge_stage_size, count);
4877 		idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read);
4878 		buf += actually_read;
4879 		tape->merge_stage_size -= actually_read;
4880 		count -= actually_read;
4881 	}
4882 	while (count >= tape->stage_size) {
4883 		bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
4884 		if (bytes_read <= 0)
4885 			goto finish;
4886 		idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read);
4887 		buf += bytes_read;
4888 		count -= bytes_read;
4889 		actually_read += bytes_read;
4890 	}
4891 	if (count) {
4892 		bytes_read=idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
4893 		if (bytes_read <= 0)
4894 			goto finish;
4895 		temp = IDE_MIN(count, bytes_read);
4896 		idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp);
4897 		actually_read += temp;
4898 		tape->merge_stage_size = bytes_read-temp;
4899 	}
4900 finish:
4901 	if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
4902 #if IDETAPE_DEBUG_LOG
4903 		if (tape->debug_level >= 2)
4904 			printk(KERN_INFO "ide-tape: %s: spacing over "
4905 				"filemark\n", tape->name);
4906 #endif
4907 		idetape_space_over_filemarks (drive, MTFSF, 1);
4908 		return 0;
4909 	}
4910 	if (tape->onstream && !actually_read &&
4911 	    test_and_clear_bit(IDETAPE_READ_ERROR, &tape->flags)) {
4912 		printk(KERN_ERR "ide-tape: %s: unrecovered read error on "
4913 			"logical block number %d, skipping\n",
4914 			tape->name, tape->logical_blk_num);
4915 		tape->logical_blk_num++;
4916 		return -EIO;
4917 	}
4918 	return actually_read;
4919 }
4920 
idetape_update_last_marker(ide_drive_t * drive,int last_mark_addr,int next_mark_addr)4921 static void idetape_update_last_marker (ide_drive_t *drive, int last_mark_addr, int next_mark_addr)
4922 {
4923 	idetape_tape_t *tape = drive->driver_data;
4924 	idetape_stage_t *stage;
4925 	os_aux_t *aux;
4926 	int position;
4927 
4928 	if (!tape->onstream || tape->raw)
4929 		return;
4930 	if (last_mark_addr == -1)
4931 		return;
4932 	stage = __idetape_kmalloc_stage(tape, 0, 0);
4933 	if (stage == NULL)
4934 		return;
4935 	idetape_flush_tape_buffers(drive);
4936 	position = idetape_read_position(drive);
4937 #if ONSTREAM_DEBUG
4938 	if (tape->debug_level >= 2)
4939 		printk(KERN_INFO "ide-tape: current position (2) %d, "
4940 			"lblk %d\n", position, tape->logical_blk_num);
4941 	if (tape->debug_level >= 2)
4942 		printk(KERN_INFO "ide-tape: current position (2) tape "
4943 			"block %d\n", tape->last_frame_position);
4944 #endif
4945 	idetape_position_tape(drive, last_mark_addr, 0, 0);
4946 	if (!idetape_queue_rw_tail(drive, IDETAPE_READ_RQ, 1, stage->bh)) {
4947 		printk(KERN_INFO "ide-tape: %s: couldn't read last marker\n",
4948 			tape->name);
4949 		__idetape_kfree_stage(stage);
4950 		idetape_position_tape(drive, position, 0, 0);
4951 		return;
4952 	}
4953 	aux = stage->aux;
4954 	if (aux->frame_type != OS_FRAME_TYPE_MARKER) {
4955 		printk(KERN_INFO "ide-tape: %s: expected to find marker "
4956 			"at addr %d\n", tape->name, last_mark_addr);
4957 		__idetape_kfree_stage(stage);
4958 		idetape_position_tape(drive, position, 0, 0);
4959 		return;
4960 	}
4961 #if ONSTREAM_DEBUG
4962 	if (tape->debug_level >= 2)
4963 		printk(KERN_INFO "ide-tape: writing back marker\n");
4964 #endif
4965 	aux->next_mark_addr = htonl(next_mark_addr);
4966 	idetape_position_tape(drive, last_mark_addr, 0, 0);
4967 	if (!idetape_queue_rw_tail(drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
4968 		printk(KERN_INFO "ide-tape: %s: couldn't write back marker "
4969 			"frame at %d\n", tape->name, last_mark_addr);
4970 		__idetape_kfree_stage(stage);
4971 		idetape_position_tape(drive, position, 0, 0);
4972 		return;
4973 	}
4974 	__idetape_kfree_stage(stage);
4975 	idetape_flush_tape_buffers (drive);
4976 	idetape_position_tape(drive, position, 0, 0);
4977 	return;
4978 }
4979 
idetape_write_filler(ide_drive_t * drive,int block,int cnt)4980 static void idetape_write_filler (ide_drive_t *drive, int block, int cnt)
4981 {
4982 	idetape_tape_t *tape = drive->driver_data;
4983 	idetape_stage_t *stage;
4984 	int rc;
4985 
4986 	if (!tape->onstream || tape->raw)
4987 		return;
4988 	stage = __idetape_kmalloc_stage(tape, 1, 1);
4989 	if (stage == NULL)
4990 		return;
4991 	idetape_init_stage(drive, stage, OS_FRAME_TYPE_FILL, 0);
4992 	idetape_wait_ready(drive, 60 * 5 * HZ);
4993 	rc = idetape_position_tape(drive, block, 0, 0);
4994 #if ONSTREAM_DEBUG
4995 	printk(KERN_INFO "write_filler: positioning failed it returned %d\n", rc);
4996 #endif
4997 	if (rc != 0)
4998 		/* don't write fillers if we cannot position the tape. */
4999 		return;
5000 
5001 	strcpy(stage->bh->b_data, "Filler");
5002 	while (cnt--) {
5003 		if (!idetape_queue_rw_tail(drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
5004 			printk(KERN_INFO "ide-tape: %s: write_filler: "
5005 				"couldn't write header frame\n", tape->name);
5006 			__idetape_kfree_stage(stage);
5007 			return;
5008 		}
5009 	}
5010 	__idetape_kfree_stage(stage);
5011 }
5012 
__idetape_write_header(ide_drive_t * drive,int block,int cnt)5013 static void __idetape_write_header (ide_drive_t *drive, int block, int cnt)
5014 {
5015 	idetape_tape_t *tape = drive->driver_data;
5016 	idetape_stage_t *stage;
5017 	os_header_t header;
5018 
5019 	stage = __idetape_kmalloc_stage(tape, 1, 1);
5020 	if (stage == NULL)
5021 		return;
5022 	idetape_init_stage(drive, stage, OS_FRAME_TYPE_HEADER, tape->logical_blk_num);
5023 	idetape_wait_ready(drive, 60 * 5 * HZ);
5024 	idetape_position_tape(drive, block, 0, 0);
5025 	memset(&header, 0, sizeof(header));
5026 	strcpy(header.ident_str, "ADR_SEQ");
5027 	header.major_rev = 1;
5028 	header.minor_rev = OS_ADR_MINREV;
5029 	header.par_num = 1;
5030 	header.partition.partition_num = OS_DATA_PARTITION;
5031 	header.partition.par_desc_ver = OS_PARTITION_VERSION;
5032 	header.partition.first_frame_addr = htonl(OS_DATA_STARTFRAME1);
5033 	header.partition.last_frame_addr = htonl(tape->capacity);
5034 	header.partition.wrt_pass_cntr = htons(tape->wrt_pass_cntr);
5035 	header.partition.eod_frame_addr = htonl(tape->eod_frame_addr);
5036 	memcpy(stage->bh->b_data, &header, sizeof(header));
5037 	while (cnt--) {
5038 		if (!idetape_queue_rw_tail(drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
5039 			printk(KERN_INFO "ide-tape: %s: couldn't write header frame\n", tape->name);
5040 			__idetape_kfree_stage(stage);
5041 			return;
5042 		}
5043 	}
5044 	__idetape_kfree_stage(stage);
5045 	idetape_flush_tape_buffers(drive);
5046 }
5047 
idetape_write_header(ide_drive_t * drive,int locate_eod)5048 static void idetape_write_header (ide_drive_t *drive, int locate_eod)
5049 {
5050 	idetape_tape_t *tape = drive->driver_data;
5051 
5052 #if ONSTREAM_DEBUG
5053 	if (tape->debug_level >= 2)
5054 		printk(KERN_INFO "ide-tape: %s: writing tape header\n", tape->name);
5055 #endif
5056 	if (!tape->onstream || tape->raw)
5057 		return;
5058 	tape->update_frame_cntr++;
5059 	__idetape_write_header(drive, 5, 5);
5060 	__idetape_write_header(drive, 0xbae, 5); /* 2990 */
5061 	if (locate_eod) {
5062 #if ONSTREAM_DEBUG
5063 		if (tape->debug_level >= 2)
5064 			printk(KERN_INFO "ide-tape: %s: locating back to "
5065 				"eod frame addr %d\n", tape->name,
5066 				tape->eod_frame_addr);
5067 #endif
5068 		idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
5069 	}
5070 }
5071 
idetape_chrdev_write(struct file * file,const char * buf,size_t count,loff_t * ppos)5072 static ssize_t idetape_chrdev_write (struct file *file, const char *buf,
5073 				     size_t count, loff_t *ppos)
5074 {
5075 	struct inode *inode = file->f_dentry->d_inode;
5076 	ide_drive_t *drive = get_drive_ptr(inode->i_rdev);
5077 	idetape_tape_t *tape = drive->driver_data;
5078 	ssize_t retval, actually_written = 0;
5079 	int position;
5080 
5081 	if (ppos != &file->f_pos) {
5082 		/* "A request was outside the capabilities of the device." */
5083 		return -ENXIO;
5084 	}
5085 
5086 #if IDETAPE_DEBUG_LOG
5087 	if (tape->debug_level >= 3)
5088 		printk (KERN_INFO "ide-tape: Reached idetape_chrdev_write, count %Zd\n", count);
5089 #endif /* IDETAPE_DEBUG_LOG */
5090 
5091 	if (tape->onstream) {
5092 		if (count != tape->tape_block_size) {
5093 			printk(KERN_ERR "ide-tape: %s: chrdev_write: "
5094 				"use %d bytes as block size (%Zd used)\n",
5095 				tape->name, tape->tape_block_size, count);
5096 			return -EINVAL;
5097 		}
5098 		/*
5099 		 * Check if we reach the end of the tape. Just assume the whole
5100 		 * pipeline is filled with write requests!
5101 		 */
5102 		if (tape->first_frame_position + tape->nr_stages >= tape->capacity - OS_EW)  {
5103 #if ONSTREAM_DEBUG
5104 			printk(KERN_INFO, "chrdev_write: Write truncated at EOM early warning");
5105 #endif
5106 			if (tape->chrdev_direction == idetape_direction_write)
5107 				idetape_write_release(inode);
5108 			return -ENOSPC;
5109 		}
5110 	}
5111 
5112 	/* Initialize write operation */
5113 	if (tape->chrdev_direction != idetape_direction_write) {
5114 		if (tape->chrdev_direction == idetape_direction_read)
5115 			idetape_discard_read_pipeline(drive, 1);
5116 #if IDETAPE_DEBUG_BUGS
5117 		if (tape->merge_stage || tape->merge_stage_size) {
5118 			printk(KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
5119 			tape->merge_stage_size = 0;
5120 		}
5121 #endif /* IDETAPE_DEBUG_BUGS */
5122 		if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
5123 			return -ENOMEM;
5124 		tape->chrdev_direction = idetape_direction_write;
5125 		idetape_init_merge_stage(tape);
5126 
5127 		if (tape->onstream) {
5128 			position = idetape_read_position(drive);
5129 			if (position <= OS_DATA_STARTFRAME1) {
5130 				tape->logical_blk_num = 0;
5131 				tape->wrt_pass_cntr++;
5132 #if ONSTREAM_DEBUG
5133 				if (tape->debug_level >= 2)
5134 					printk(KERN_INFO "ide-tape: %s: logical block num 0, setting eod to %d\n", tape->name, OS_DATA_STARTFRAME1);
5135 				if (tape->debug_level >= 2)
5136 					printk(KERN_INFO "ide-tape: %s: allocating new write pass counter %d\n", tape->name, tape->wrt_pass_cntr);
5137 #endif
5138 				tape->filemark_cnt = 0;
5139 				tape->eod_frame_addr = OS_DATA_STARTFRAME1;
5140 				tape->first_mark_addr = tape->last_mark_addr = -1;
5141 				idetape_write_header(drive, 1);
5142 			}
5143 #if ONSTREAM_DEBUG
5144 			if (tape->debug_level >= 2)
5145 				printk(KERN_INFO "ide-tape: %s: positioning tape to eod at %d\n", tape->name, tape->eod_frame_addr);
5146 #endif
5147 			position = idetape_read_position(drive);
5148 			if (position != tape->eod_frame_addr)
5149 				idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
5150 #if ONSTREAM_DEBUG
5151 			if (tape->debug_level >= 2)
5152 				printk(KERN_INFO "ide-tape: %s: first_frame_position %d\n", tape->name, tape->first_frame_position);
5153 #endif
5154 		}
5155 
5156 		/*
5157 		 *	Issue a write 0 command to ensure that DSC handshake
5158 		 *	is switched from completion mode to buffer available
5159 		 *	mode.
5160 		 */
5161 		retval = idetape_queue_rw_tail(drive, IDETAPE_WRITE_RQ, 0, tape->merge_stage->bh);
5162 		if (retval < 0) {
5163 			__idetape_kfree_stage(tape->merge_stage);
5164 			tape->merge_stage = NULL;
5165 			tape->chrdev_direction = idetape_direction_none;
5166 			return retval;
5167 		}
5168 #if ONSTREAM_DEBUG
5169 		if (tape->debug_level >= 2)
5170 			printk("ide-tape: first_frame_position %d\n", tape->first_frame_position);
5171 #endif
5172 	}
5173 	if (count == 0)
5174 		return (0);
5175 	if (tape->restart_speed_control_req)
5176 		idetape_restart_speed_control(drive);
5177 	if (tape->merge_stage_size) {
5178 #if IDETAPE_DEBUG_BUGS
5179 		if (tape->merge_stage_size >= tape->stage_size) {
5180 			printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
5181 			tape->merge_stage_size = 0;
5182 		}
5183 #endif /* IDETAPE_DEBUG_BUGS */
5184 		actually_written = IDE_MIN(tape->stage_size - tape->merge_stage_size, count);
5185 		idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written);
5186 		buf += actually_written;
5187 		tape->merge_stage_size += actually_written;
5188 		count -= actually_written;
5189 
5190 		if (tape->merge_stage_size == tape->stage_size) {
5191 			tape->merge_stage_size = 0;
5192 			retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
5193 			if (retval <= 0)
5194 				return (retval);
5195 		}
5196 	}
5197 	while (count >= tape->stage_size) {
5198 		idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size);
5199 		buf += tape->stage_size;
5200 		count -= tape->stage_size;
5201 		retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
5202 		actually_written += tape->stage_size;
5203 		if (retval <= 0)
5204 			return (retval);
5205 	}
5206 	if (count) {
5207 		actually_written += count;
5208 		idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count);
5209 		tape->merge_stage_size += count;
5210 	}
5211 	return (actually_written);
5212 }
5213 
idetape_write_filemark(ide_drive_t * drive)5214 static int idetape_write_filemark (ide_drive_t *drive)
5215 {
5216 	idetape_tape_t *tape = drive->driver_data;
5217 	int last_mark_addr;
5218 	idetape_pc_t pc;
5219 
5220 	if (!tape->onstream) {
5221 		/* Write a filemark */
5222 		idetape_create_write_filemark_cmd(drive, &pc, 1);
5223 		if (idetape_queue_pc_tail(drive, &pc)) {
5224 			printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
5225 			return -EIO;
5226 		}
5227 	} else if (!tape->raw) {
5228 		last_mark_addr = idetape_read_position(drive);
5229 		tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
5230 		if (tape->merge_stage != NULL) {
5231 			idetape_init_stage(drive, tape->merge_stage, OS_FRAME_TYPE_MARKER, tape->logical_blk_num);
5232 			idetape_pad_zeros(drive, tape->stage_size);
5233 			tape->logical_blk_num++;
5234 			__idetape_kfree_stage(tape->merge_stage);
5235 			tape->merge_stage = NULL;
5236 		}
5237 		if (tape->filemark_cnt)
5238 			idetape_update_last_marker(drive, tape->last_mark_addr, last_mark_addr);
5239 		tape->last_mark_addr = last_mark_addr;
5240 		if (tape->filemark_cnt++ == 0)
5241 			tape->first_mark_addr = last_mark_addr;
5242 	}
5243 	return 0;
5244 }
5245 
idetape_write_eod(ide_drive_t * drive)5246 static void idetape_write_eod (ide_drive_t *drive)
5247 {
5248 	idetape_tape_t *tape = drive->driver_data;
5249 
5250 	if (!tape->onstream || tape->raw)
5251 		return;
5252 	tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
5253 	if (tape->merge_stage != NULL) {
5254 		tape->eod_frame_addr = idetape_read_position(drive);
5255 		idetape_init_stage(drive, tape->merge_stage, OS_FRAME_TYPE_EOD, tape->logical_blk_num);
5256 		idetape_pad_zeros(drive, tape->stage_size);
5257 		__idetape_kfree_stage (tape->merge_stage);
5258 		tape->merge_stage = NULL;
5259 	}
5260 	return;
5261 }
5262 
idetape_seek_logical_blk(ide_drive_t * drive,int logical_blk_num)5263 int idetape_seek_logical_blk (ide_drive_t *drive, int logical_blk_num)
5264 {
5265 	idetape_tape_t *tape = drive->driver_data;
5266 	int estimated_address = logical_blk_num + 20;
5267 	int retries = 0;
5268 	int speed_control;
5269 
5270 	speed_control = tape->speed_control;
5271 	tape->speed_control = 0;
5272 	if (logical_blk_num < 0)
5273 		logical_blk_num = 0;
5274 	if (idetape_get_logical_blk(drive, logical_blk_num, 10, 1))
5275 		goto ok;
5276 	while (++retries < 10) {
5277 		idetape_discard_read_pipeline(drive, 0);
5278 		idetape_position_tape(drive, estimated_address, 0, 0);
5279 		if (idetape_get_logical_blk(drive, logical_blk_num, 10, 1))
5280 			goto ok;
5281 		if (!idetape_get_logical_blk(drive, -1, 10, 1))
5282 			goto error;
5283 		if (tape->logical_blk_num < logical_blk_num)
5284 			estimated_address += logical_blk_num - tape->logical_blk_num;
5285 		else
5286 			break;
5287 	}
5288 error:
5289 	tape->speed_control = speed_control;
5290 	tape->restart_speed_control_req = 1;
5291 	printk(KERN_INFO "ide-tape: %s: couldn't seek to logical block %d "
5292 		"(at %d), %d retries\n", tape->name, logical_blk_num,
5293 		tape->logical_blk_num, retries);
5294 	return -EIO;
5295 ok:
5296 	tape->speed_control = speed_control;
5297 	tape->restart_speed_control_req = 1;
5298 	return 0;
5299 }
5300 
5301 /*
5302  *	idetape_mtioctop is called from idetape_chrdev_ioctl when
5303  *	the general mtio MTIOCTOP ioctl is requested.
5304  *
5305  *	We currently support the following mtio.h operations:
5306  *
5307  *	MTFSF	-	Space over mt_count filemarks in the positive direction.
5308  *			The tape is positioned after the last spaced filemark.
5309  *
5310  *	MTFSFM	-	Same as MTFSF, but the tape is positioned before the
5311  *			last filemark.
5312  *
5313  *	MTBSF	-	Steps background over mt_count filemarks, tape is
5314  *			positioned before the last filemark.
5315  *
5316  *	MTBSFM	-	Like MTBSF, only tape is positioned after the last filemark.
5317  *
5318  *	Note:
5319  *
5320  *		MTBSF and MTBSFM are not supported when the tape doesn't
5321  *		supports spacing over filemarks in the reverse direction.
5322  *		In this case, MTFSFM is also usually not supported (it is
5323  *		supported in the rare case in which we crossed the filemark
5324  *		during our read-ahead pipelined operation mode).
5325  *
5326  *	MTWEOF	-	Writes mt_count filemarks. Tape is positioned after
5327  *			the last written filemark.
5328  *
5329  *	MTREW	-	Rewinds tape.
5330  *
5331  *	MTLOAD	-	Loads the tape.
5332  *
5333  *	MTOFFL	-	Puts the tape drive "Offline": Rewinds the tape and
5334  *	MTUNLOAD	prevents further access until the media is replaced.
5335  *
5336  *	MTNOP	-	Flushes tape buffers.
5337  *
5338  *	MTRETEN	-	Retension media. This typically consists of one end
5339  *			to end pass on the media.
5340  *
5341  *	MTEOM	-	Moves to the end of recorded data.
5342  *
5343  *	MTERASE	-	Erases tape.
5344  *
5345  *	MTSETBLK - 	Sets the user block size to mt_count bytes. If
5346  *			mt_count is 0, we will attempt to autodetect
5347  *			the block size.
5348  *
5349  *	MTSEEK	-	Positions the tape in a specific block number, where
5350  *			each block is assumed to contain which user_block_size
5351  *			bytes.
5352  *
5353  *	MTSETPART - 	Switches to another tape partition.
5354  *
5355  *	MTLOCK - 	Locks the tape door.
5356  *
5357  *	MTUNLOCK - 	Unlocks the tape door.
5358  *
5359  *	The following commands are currently not supported:
5360  *
5361  *	MTFSS, MTBSS, MTWSM, MTSETDENSITY,
5362  *	MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
5363  */
idetape_mtioctop(ide_drive_t * drive,short mt_op,int mt_count)5364 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
5365 {
5366 	idetape_tape_t *tape = drive->driver_data;
5367 	idetape_pc_t pc;
5368 	int i,retval;
5369 
5370 #if IDETAPE_DEBUG_LOG
5371 	if (tape->debug_level >= 1)
5372 		printk(KERN_INFO "ide-tape: Handling MTIOCTOP "
5373 			"ioctl: mt_op=%d, mt_count=%d\n", mt_op, mt_count);
5374 #endif /* IDETAPE_DEBUG_LOG */
5375 	/*
5376 	 *	Commands which need our pipelined read-ahead stages.
5377 	 */
5378 	switch (mt_op) {
5379 		case MTFSF:
5380 		case MTFSFM:
5381 		case MTBSF:
5382 		case MTBSFM:
5383 			if (!mt_count)
5384 				return (0);
5385 			return (idetape_space_over_filemarks(drive,mt_op,mt_count));
5386 		default:
5387 			break;
5388 	}
5389 	switch (mt_op) {
5390 		case MTWEOF:
5391 			idetape_discard_read_pipeline(drive, 1);
5392 			for (i = 0; i < mt_count; i++) {
5393 				retval = idetape_write_filemark(drive);
5394 				if (retval) return retval;
5395 			}
5396 			return (0);
5397 		case MTREW:
5398 			idetape_discard_read_pipeline(drive, 0);
5399 			if (idetape_rewind_tape(drive))
5400 				return -EIO;
5401 			if (tape->onstream && !tape->raw)
5402 				return idetape_position_tape(drive, OS_DATA_STARTFRAME1, 0, 0);
5403 			return 0;
5404 		case MTLOAD:
5405 			idetape_discard_read_pipeline(drive, 0);
5406 			idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
5407 			return (idetape_queue_pc_tail(drive, &pc));
5408 		case MTUNLOAD:
5409 		case MTOFFL:
5410 			idetape_discard_read_pipeline(drive, 0);
5411 			idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
5412 			return (idetape_queue_pc_tail(drive, &pc));
5413 		case MTNOP:
5414 			idetape_discard_read_pipeline(drive, 0);
5415 			return (idetape_flush_tape_buffers(drive));
5416 		case MTRETEN:
5417 			idetape_discard_read_pipeline(drive, 0);
5418 			idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
5419 			return (idetape_queue_pc_tail(drive, &pc));
5420 		case MTEOM:
5421 			if (tape->onstream) {
5422 #if ONSTREAM_DEBUG
5423 				if (tape->debug_level >= 2)
5424 					printk(KERN_INFO "ide-tape: %s: positioning tape to eod at %d\n", tape->name, tape->eod_frame_addr);
5425 #endif
5426 				idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
5427 				if (!idetape_get_logical_blk(drive, -1, 10, 0))
5428 					return -EIO;
5429 				if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_EOD)
5430 					return -EIO;
5431 				return 0;
5432 			}
5433 			idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
5434 			return (idetape_queue_pc_tail(drive, &pc));
5435 		case MTERASE:
5436 			if (tape->onstream) {
5437 				tape->eod_frame_addr = OS_DATA_STARTFRAME1;
5438 				tape->logical_blk_num = 0;
5439 				tape->first_mark_addr = tape->last_mark_addr = -1;
5440 				idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
5441 				idetape_write_eod(drive);
5442 				idetape_flush_tape_buffers(drive);
5443 				idetape_write_header(drive, 0);
5444 				/*
5445 				 * write filler frames to the unused frames...
5446 				 * REMOVE WHEN going to LIN4 application type...
5447 				 */
5448 				idetape_write_filler(drive, OS_DATA_STARTFRAME1 - 10, 10);
5449 				idetape_write_filler(drive, OS_DATA_ENDFRAME1, 10);
5450 				idetape_flush_tape_buffers(drive);
5451 				(void) idetape_rewind_tape(drive);
5452 				return 0;
5453 			}
5454 			(void) idetape_rewind_tape(drive);
5455 			idetape_create_erase_cmd(&pc);
5456 			return (idetape_queue_pc_tail(drive, &pc));
5457 		case MTSETBLK:
5458 			if (tape->onstream) {
5459 				if (mt_count != tape->tape_block_size) {
5460 					printk(KERN_INFO "ide-tape: %s: MTSETBLK %d -- only %d bytes block size supported\n", tape->name, mt_count, tape->tape_block_size);
5461 					return -EINVAL;
5462 				}
5463 				return 0;
5464 			}
5465 			if (mt_count) {
5466 				if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
5467 					return -EIO;
5468 				tape->user_bs_factor = mt_count / tape->tape_block_size;
5469 				clear_bit(IDETAPE_DETECT_BS, &tape->flags);
5470 			} else
5471 				set_bit(IDETAPE_DETECT_BS, &tape->flags);
5472 			return 0;
5473 		case MTSEEK:
5474 			if (!tape->onstream || tape->raw) {
5475 				idetape_discard_read_pipeline(drive, 0);
5476 				return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
5477 			}
5478 			return idetape_seek_logical_blk(drive, mt_count);
5479 		case MTSETPART:
5480 			idetape_discard_read_pipeline(drive, 0);
5481 			if (tape->onstream)
5482 				return -EIO;
5483 			return (idetape_position_tape(drive, 0, mt_count, 0));
5484 		case MTFSR:
5485 		case MTBSR:
5486 			if (tape->onstream) {
5487 				if (!idetape_get_logical_blk(drive, -1, 10, 0))
5488 					return -EIO;
5489 				if (mt_op == MTFSR)
5490 					return idetape_seek_logical_blk(drive, tape->logical_blk_num + mt_count);
5491 				else {
5492 					idetape_discard_read_pipeline(drive, 0);
5493 					return idetape_seek_logical_blk(drive, tape->logical_blk_num - mt_count);
5494 				}
5495 			}
5496 		case MTLOCK:
5497 			if (!idetape_create_prevent_cmd(drive, &pc, 1))
5498 				return 0;
5499 			retval = idetape_queue_pc_tail(drive, &pc);
5500 			if (retval) return retval;
5501 			tape->door_locked = DOOR_EXPLICITLY_LOCKED;
5502 			return 0;
5503 		case MTUNLOCK:
5504 			if (!idetape_create_prevent_cmd(drive, &pc, 0))
5505 				return 0;
5506 			retval = idetape_queue_pc_tail(drive, &pc);
5507 			if (retval) return retval;
5508 			tape->door_locked = DOOR_UNLOCKED;
5509 			return 0;
5510 		default:
5511 			printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
5512 			return (-EIO);
5513 	}
5514 }
5515 
5516 /*
5517  *	Our character device ioctls.
5518  *
5519  *	General mtio.h magnetic io commands are supported here, and not in
5520  *	the corresponding block interface.
5521  *
5522  *	The following ioctls are supported:
5523  *
5524  *	MTIOCTOP -	Refer to idetape_mtioctop for detailed description.
5525  *
5526  *	MTIOCGET - 	The mt_dsreg field in the returned mtget structure
5527  *			will be set to (user block size in bytes <<
5528  *			MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
5529  *
5530  *			The mt_blkno is set to the current user block number.
5531  *			The other mtget fields are not supported.
5532  *
5533  *	MTIOCPOS -	The current tape "block position" is returned. We
5534  *			assume that each block contains user_block_size
5535  *			bytes.
5536  *
5537  *	Our own ide-tape ioctls are supported on both interfaces.
5538  */
idetape_chrdev_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)5539 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
5540 {
5541 	ide_drive_t *drive = get_drive_ptr(inode->i_rdev);
5542 	idetape_tape_t *tape = drive->driver_data;
5543 	struct mtop mtop;
5544 	struct mtget mtget;
5545 	struct mtpos mtpos;
5546 	int block_offset = 0, position = tape->first_frame_position;
5547 
5548 #if IDETAPE_DEBUG_LOG
5549 	if (tape->debug_level >= 3)
5550 		printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
5551 #endif /* IDETAPE_DEBUG_LOG */
5552 
5553 	tape->restart_speed_control_req = 1;
5554 	if (tape->chrdev_direction == idetape_direction_write) {
5555 		idetape_empty_write_pipeline(drive);
5556 		idetape_flush_tape_buffers(drive);
5557 	}
5558 	if (cmd == MTIOCGET || cmd == MTIOCPOS) {
5559 		block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
5560 		if ((position = idetape_read_position(drive)) < 0)
5561 			return -EIO;
5562 	}
5563 	switch (cmd) {
5564 		case MTIOCTOP:
5565 			if (copy_from_user((char *) &mtop, (char *) arg, sizeof (struct mtop)))
5566 				return -EFAULT;
5567 			return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
5568 		case MTIOCGET:
5569 			memset(&mtget, 0, sizeof (struct mtget));
5570 			mtget.mt_type = MT_ISSCSI2;
5571 			if (!tape->onstream || tape->raw) {
5572 				mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
5573 			} else {
5574 				if (!idetape_get_logical_blk(drive, -1, 10, 0))
5575 					mtget.mt_blkno = -1;
5576 				else
5577 					mtget.mt_blkno = tape->logical_blk_num;
5578 			}
5579 			mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
5580 			if (tape->onstream) {
5581 				mtget.mt_gstat |= GMT_ONLINE(0xffffffff);
5582 				if (tape->first_stage && tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD)
5583 					mtget.mt_gstat |= GMT_EOD(0xffffffff);
5584 				if (position <= OS_DATA_STARTFRAME1)
5585 					mtget.mt_gstat |= GMT_BOT(0xffffffff);
5586 			}
5587 			if (copy_to_user ((char *) arg,(char *) &mtget, sizeof (struct mtget)))
5588 				return -EFAULT;
5589 			return 0;
5590 		case MTIOCPOS:
5591 			if (tape->onstream && !tape->raw) {
5592 				if (!idetape_get_logical_blk(drive, -1, 10, 0))
5593 					return -EIO;
5594 				mtpos.mt_blkno = tape->logical_blk_num;
5595 			} else
5596 				mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
5597 			if (copy_to_user ((char *) arg,(char *) &mtpos, sizeof (struct mtpos)))
5598 				return -EFAULT;
5599 			return 0;
5600 		default:
5601 			if (tape->chrdev_direction == idetape_direction_read)
5602 				idetape_discard_read_pipeline(drive, 1);
5603 			return (idetape_blkdev_ioctl(drive,inode,file,cmd,arg));
5604 	}
5605 }
5606 
__idetape_analyze_headers(ide_drive_t * drive,int block)5607 static int __idetape_analyze_headers (ide_drive_t *drive, int block)
5608 {
5609 	idetape_tape_t *tape = drive->driver_data;
5610 	idetape_stage_t *stage;
5611 	os_header_t *header;
5612 	os_aux_t *aux;
5613 
5614 	if (!tape->onstream || tape->raw) {
5615 		tape->header_ok = tape->linux_media = 1;
5616 		return 1;
5617 	}
5618 	tape->header_ok = tape->linux_media = 0;
5619 	tape->update_frame_cntr = 0;
5620 	tape->wrt_pass_cntr = 0;
5621 	tape->eod_frame_addr = OS_DATA_STARTFRAME1;
5622 	tape->first_mark_addr = tape->last_mark_addr = -1;
5623 	stage = __idetape_kmalloc_stage(tape, 0, 0);
5624 	if (stage == NULL)
5625 		return 0;
5626 #if ONSTREAM_DEBUG
5627 	if (tape->debug_level >= 2)
5628 		printk(KERN_INFO "ide-tape: %s: reading header\n", tape->name);
5629 #endif
5630 	idetape_position_tape(drive, block, 0, 0);
5631 	if (!idetape_queue_rw_tail(drive, IDETAPE_READ_RQ, 1, stage->bh)) {
5632 		printk(KERN_INFO "ide-tape: %s: couldn't read header frame\n",
5633 			tape->name);
5634 		__idetape_kfree_stage(stage);
5635 		return 0;
5636 	}
5637 	header = (os_header_t *) stage->bh->b_data;
5638 	aux = stage->aux;
5639 	if (strncmp(header->ident_str, "ADR_SEQ", 7) != 0) {
5640 		printk(KERN_INFO "ide-tape: %s: invalid header identification string\n", tape->name);
5641 		__idetape_kfree_stage(stage);
5642 		return 0;
5643 	}
5644 	if (header->major_rev != 1 || (header->minor_rev > OS_ADR_MINREV))
5645 		printk(KERN_INFO "ide-tape: warning: revision %d.%d detected (up to 1.%d supported)\n", header->major_rev, header->minor_rev, OS_ADR_MINREV);
5646 	if (header->par_num != 1)
5647 		printk(KERN_INFO "ide-tape: warning: %d partitions defined, only one supported\n", header->par_num);
5648 	tape->wrt_pass_cntr = ntohs(header->partition.wrt_pass_cntr);
5649 	tape->eod_frame_addr = ntohl(header->partition.eod_frame_addr);
5650 	tape->filemark_cnt = ntohl(aux->filemark_cnt);
5651 	tape->first_mark_addr = ntohl(aux->next_mark_addr);
5652 	tape->last_mark_addr = ntohl(aux->last_mark_addr);
5653 	tape->update_frame_cntr = ntohl(aux->update_frame_cntr);
5654 	memcpy(tape->application_sig, aux->application_sig, 4);
5655 	tape->application_sig[4] = 0;
5656 	if (memcmp(tape->application_sig, "LIN", 3) == 0) {
5657 		tape->linux_media = 1;
5658 		tape->linux_media_version = tape->application_sig[3] - '0';
5659 		if (tape->linux_media_version != 3)
5660 			printk(KERN_INFO "ide-tape: %s: Linux media version "
5661 				"%d detected (current 3)\n",
5662 				 tape->name, tape->linux_media_version);
5663 	} else {
5664 		printk(KERN_INFO "ide-tape: %s: non Linux media detected "
5665 			"(%s)\n", tape->name, tape->application_sig);
5666 		tape->linux_media = 0;
5667 	}
5668 #if ONSTREAM_DEBUG
5669 	if (tape->debug_level >= 2)
5670 		printk(KERN_INFO "ide-tape: %s: detected write pass "
5671 			"counter %d, eod frame addr %d\n", tape->name,
5672 			tape->wrt_pass_cntr, tape->eod_frame_addr);
5673 #endif
5674 	__idetape_kfree_stage(stage);
5675 	return 1;
5676 }
5677 
idetape_analyze_headers(ide_drive_t * drive)5678 static int idetape_analyze_headers (ide_drive_t *drive)
5679 {
5680 	idetape_tape_t *tape = drive->driver_data;
5681 	int position, block;
5682 
5683 	if (!tape->onstream || tape->raw) {
5684 		tape->header_ok = tape->linux_media = 1;
5685 		return 1;
5686 	}
5687 	tape->header_ok = tape->linux_media = 0;
5688 	position = idetape_read_position(drive);
5689 	for (block = 5; block < 10; block++)
5690 		if (__idetape_analyze_headers(drive, block))
5691 			goto ok;
5692 	for (block = 0xbae; block < 0xbb3; block++) /* 2990 - 2994 */
5693 		if (__idetape_analyze_headers(drive, block))
5694 			goto ok;
5695 	printk(KERN_ERR "ide-tape: %s: failed to find valid ADRL header\n",
5696 		tape->name);
5697 	return 0;
5698 ok:
5699 	if (position < OS_DATA_STARTFRAME1)
5700 		position = OS_DATA_STARTFRAME1;
5701 	idetape_position_tape(drive, position, 0, 0);
5702 	tape->header_ok = 1;
5703 	return 1;
5704 }
5705 
5706 /*
5707  *	Our character device open function.
5708  */
idetape_chrdev_open(struct inode * inode,struct file * filp)5709 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
5710 {
5711 	ide_drive_t *drive;
5712 	idetape_tape_t *tape;
5713 	idetape_pc_t pc;
5714 	unsigned int minor = MINOR(inode->i_rdev);
5715 
5716 #if IDETAPE_DEBUG_LOG
5717 	printk (KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
5718 #endif /* IDETAPE_DEBUG_LOG */
5719 
5720 	if ((drive = get_drive_ptr(inode->i_rdev)) == NULL)
5721 		return -ENXIO;
5722 	tape = drive->driver_data;
5723 
5724 	if (test_and_set_bit(IDETAPE_BUSY, &tape->flags))
5725 		return -EBUSY;
5726 	MOD_INC_USE_COUNT;
5727 	if (!tape->onstream) {
5728 		idetape_read_position(drive);
5729 		if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
5730 			(void) idetape_rewind_tape(drive);
5731 	} else {
5732 		if (minor & 64) {
5733 			tape->tape_block_size = tape->stage_size = 32768 + 512;
5734 			tape->raw = 1;
5735 		} else {
5736 			tape->tape_block_size = tape->stage_size = 32768;
5737 			tape->raw = 0;
5738 		}
5739                 idetape_onstream_mode_sense_tape_parameter_page(drive, tape->debug_level);
5740 	}
5741 	if (idetape_wait_ready(drive, 60 * HZ)) {
5742 		clear_bit(IDETAPE_BUSY, &tape->flags);
5743 		printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
5744 		MOD_DEC_USE_COUNT;
5745 		return -EBUSY;
5746 	}
5747 	if (tape->onstream)
5748 		idetape_read_position(drive);
5749 	MOD_DEC_USE_COUNT;
5750 	if (tape->chrdev_direction != idetape_direction_read)
5751 		clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
5752 
5753 	if (tape->chrdev_direction == idetape_direction_none) {
5754 		MOD_INC_USE_COUNT;
5755 		if (idetape_create_prevent_cmd(drive, &pc, 1)) {
5756 			if (!idetape_queue_pc_tail(drive, &pc)) {
5757 				if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
5758 					tape->door_locked = DOOR_LOCKED;
5759 			}
5760 		}
5761 		idetape_analyze_headers(drive);
5762 	}
5763 	tape->max_frames = tape->cur_frames = tape->req_buffer_fill = 0;
5764 	idetape_restart_speed_control(drive);
5765 	tape->restart_speed_control_req = 0;
5766 	return 0;
5767 }
5768 
idetape_write_release(struct inode * inode)5769 static void idetape_write_release (struct inode *inode)
5770 {
5771 	ide_drive_t *drive = get_drive_ptr(inode->i_rdev);
5772 	idetape_tape_t *tape = drive->driver_data;
5773 	unsigned int minor = MINOR(inode->i_rdev);
5774 
5775 	idetape_empty_write_pipeline(drive);
5776 	tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
5777 	if (tape->merge_stage != NULL) {
5778 		idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
5779 		__idetape_kfree_stage(tape->merge_stage);
5780 		tape->merge_stage = NULL;
5781 	}
5782 	idetape_write_filemark(drive);
5783 	idetape_write_eod(drive);
5784 	idetape_flush_tape_buffers(drive);
5785 	idetape_write_header(drive, minor >= 128);
5786 	idetape_flush_tape_buffers(drive);
5787 
5788 	return;
5789 }
5790 
5791 /*
5792  *	Our character device release function.
5793  */
idetape_chrdev_release(struct inode * inode,struct file * filp)5794 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
5795 {
5796 	ide_drive_t *drive = get_drive_ptr(inode->i_rdev);
5797 	idetape_tape_t *tape;
5798 	idetape_pc_t pc;
5799 	unsigned int minor = MINOR(inode->i_rdev);
5800 
5801 	lock_kernel();
5802 	tape = drive->driver_data;
5803 #if IDETAPE_DEBUG_LOG
5804 	if (tape->debug_level >= 3)
5805 		printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
5806 #endif /* IDETAPE_DEBUG_LOG */
5807 
5808 	if (tape->chrdev_direction == idetape_direction_write) {
5809 		idetape_write_release(inode);
5810 	}
5811 	if (tape->chrdev_direction == idetape_direction_read) {
5812 		if (minor < 128)
5813 			idetape_discard_read_pipeline(drive, 1);
5814 		else
5815 			idetape_wait_for_pipeline(drive);
5816 	}
5817 	if (tape->cache_stage != NULL) {
5818 		__idetape_kfree_stage(tape->cache_stage);
5819 		tape->cache_stage = NULL;
5820 	}
5821 	if (minor < 128)
5822 		(void) idetape_rewind_tape(drive);
5823 	if (tape->chrdev_direction == idetape_direction_none) {
5824 		if (tape->door_locked == DOOR_LOCKED) {
5825 			if (idetape_create_prevent_cmd(drive, &pc, 0)) {
5826 				if (!idetape_queue_pc_tail(drive, &pc))
5827 					tape->door_locked = DOOR_UNLOCKED;
5828 			}
5829 		}
5830 		MOD_DEC_USE_COUNT;
5831 	}
5832 	clear_bit(IDETAPE_BUSY, &tape->flags);
5833 	unlock_kernel();
5834 	return 0;
5835 }
5836 
5837 /*
5838  *	idetape_identify_device is called to check the contents of the
5839  *	ATAPI IDENTIFY command results. We return:
5840  *
5841  *	1	If the tape can be supported by us, based on the information
5842  *		we have so far.
5843  *
5844  *	0 	If this tape driver is not currently supported by us.
5845  */
idetape_identify_device(ide_drive_t * drive)5846 static int idetape_identify_device (ide_drive_t *drive)
5847 {
5848 	struct idetape_id_gcw gcw;
5849 	struct hd_driveid *id = drive->id;
5850 #if IDETAPE_DEBUG_INFO
5851 	unsigned short mask,i;
5852 #endif /* IDETAPE_DEBUG_INFO */
5853 
5854 	if(drive->id_read == 0)
5855 		return 1;
5856 
5857 	*((unsigned short *) &gcw) = id->config;
5858 
5859 #if IDETAPE_DEBUG_INFO
5860 	printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
5861 	printk(KERN_INFO "ide-tape: Protocol Type: ");
5862 	switch (gcw.protocol) {
5863 		case 0: case 1: printk("ATA\n");break;
5864 		case 2:	printk("ATAPI\n");break;
5865 		case 3: printk("Reserved (Unknown to ide-tape)\n");break;
5866 	}
5867 	printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
5868 	switch (gcw.device_type) {
5869 		case 0: printk("Direct-access Device\n");break;
5870 		case 1: printk("Streaming Tape Device\n");break;
5871 		case 2: case 3: case 4: printk("Reserved\n");break;
5872 		case 5: printk("CD-ROM Device\n");break;
5873 		case 6: printk("Reserved\n");
5874 		case 7: printk("Optical memory Device\n");break;
5875 		case 0x1f: printk("Unknown or no Device type\n");break;
5876 		default: printk("Reserved\n");
5877 	}
5878 	printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
5879 	printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
5880 	switch (gcw.drq_type) {
5881 		case 0: printk("Microprocessor DRQ\n");break;
5882 		case 1: printk("Interrupt DRQ\n");break;
5883 		case 2: printk("Accelerated DRQ\n");break;
5884 		case 3: printk("Reserved\n");break;
5885 	}
5886 	printk(KERN_INFO "ide-tape: Command Packet Size: ");
5887 	switch (gcw.packet_size) {
5888 		case 0: printk("12 bytes\n");break;
5889 		case 1: printk("16 bytes\n");break;
5890 		default: printk("Reserved\n");break;
5891 	}
5892 	printk(KERN_INFO "ide-tape: Model: %.40s\n",id->model);
5893 	printk(KERN_INFO "ide-tape: Firmware Revision: %.8s\n",id->fw_rev);
5894 	printk(KERN_INFO "ide-tape: Serial Number: %.20s\n",id->serial_no);
5895 	printk(KERN_INFO "ide-tape: Write buffer size: %d bytes\n",id->buf_size*512);
5896 	printk(KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
5897 	printk(KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
5898 	printk(KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
5899 	printk(KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
5900 	printk(KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
5901 	printk(KERN_INFO "ide-tape: PIO Cycle Timing Category: %d\n",id->tPIO);
5902 	printk(KERN_INFO "ide-tape: DMA Cycle Timing Category: %d\n",id->tDMA);
5903 	printk(KERN_INFO "ide-tape: Single Word DMA supported modes: ");
5904 	for (i=0,mask=1;i<8;i++,mask=mask << 1) {
5905 		if (id->dma_1word & mask)
5906 			printk("%d ",i);
5907 		if (id->dma_1word & (mask << 8))
5908 			printk("(active) ");
5909 	}
5910 	printk("\n");
5911 	printk(KERN_INFO "ide-tape: Multi Word DMA supported modes: ");
5912 	for (i=0,mask=1;i<8;i++,mask=mask << 1) {
5913 		if (id->dma_mword & mask)
5914 			printk("%d ",i);
5915 		if (id->dma_mword & (mask << 8))
5916 			printk("(active) ");
5917 	}
5918 	printk("\n");
5919 	if (id->field_valid & 0x0002) {
5920 		printk(KERN_INFO "ide-tape: Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
5921 		printk(KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: ");
5922 		if (id->eide_dma_min == 0)
5923 			printk("Not supported\n");
5924 		else
5925 			printk("%d ns\n",id->eide_dma_min);
5926 
5927 		printk(KERN_INFO "ide-tape: Manufacturer\'s Recommended Multi-word cycle: ");
5928 		if (id->eide_dma_time == 0)
5929 			printk("Not supported\n");
5930 		else
5931 			printk("%d ns\n",id->eide_dma_time);
5932 
5933 		printk(KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: ");
5934 		if (id->eide_pio == 0)
5935 			printk("Not supported\n");
5936 		else
5937 			printk("%d ns\n",id->eide_pio);
5938 
5939 		printk(KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: ");
5940 		if (id->eide_pio_iordy == 0)
5941 			printk("Not supported\n");
5942 		else
5943 			printk("%d ns\n",id->eide_pio_iordy);
5944 
5945 	} else
5946 		printk(KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.\n");
5947 #endif /* IDETAPE_DEBUG_INFO */
5948 
5949 	/* Check that we can support this device */
5950 
5951 	if (gcw.protocol !=2 )
5952 		printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
5953 	else if (gcw.device_type != 1)
5954 		printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
5955 	else if (!gcw.removable)
5956 		printk(KERN_ERR "ide-tape: The removable flag is not set\n");
5957 	else if (gcw.packet_size != 0) {
5958 		printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
5959 		if (gcw.packet_size == 1)
5960 			printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
5961 	} else
5962 		return 1;
5963 	return 0;
5964 }
5965 
5966 /*
5967  * Notify vendor ID to the OnStream tape drive
5968  */
idetape_onstream_set_vendor(ide_drive_t * drive,char * vendor)5969 static void idetape_onstream_set_vendor (ide_drive_t *drive, char *vendor)
5970 {
5971 	idetape_pc_t pc;
5972 	idetape_mode_parameter_header_t *header;
5973 
5974 	idetape_create_mode_select_cmd(&pc, sizeof(*header) + 8);
5975 	pc.buffer[0] = 3 + 8;	/* Mode Data Length */
5976 	pc.buffer[1] = 0;	/* Medium Type - ignoring */
5977 	pc.buffer[2] = 0;	/* Reserved */
5978 	pc.buffer[3] = 0;	/* Block Descriptor Length */
5979 	pc.buffer[4 + 0] = 0x36 | (1 << 7);
5980 	pc.buffer[4 + 1] = 6;
5981 	pc.buffer[4 + 2] = vendor[0];
5982 	pc.buffer[4 + 3] = vendor[1];
5983 	pc.buffer[4 + 4] = vendor[2];
5984 	pc.buffer[4 + 5] = vendor[3];
5985 	pc.buffer[4 + 6] = 0;
5986 	pc.buffer[4 + 7] = 0;
5987 	if (idetape_queue_pc_tail(drive, &pc))
5988 		printk(KERN_ERR "ide-tape: Couldn't set vendor name to %s\n", vendor);
5989 
5990 }
5991 
5992 /*
5993  * Various unused OnStream commands
5994  */
5995 #if ONSTREAM_DEBUG
idetape_onstream_set_retries(ide_drive_t * drive,int retries)5996 static void idetape_onstream_set_retries (ide_drive_t *drive, int retries)
5997 {
5998 	idetape_pc_t pc;
5999 
6000 	idetape_create_mode_select_cmd(&pc, sizeof(idetape_mode_parameter_header_t) + 4);
6001 	pc.buffer[0] = 3 + 4;
6002 	pc.buffer[1] = 0;	/* Medium Type - ignoring */
6003 	pc.buffer[2] = 0;	/* Reserved */
6004 	pc.buffer[3] = 0;	/* Block Descriptor Length */
6005 	pc.buffer[4 + 0] = 0x2f | (1 << 7);
6006 	pc.buffer[4 + 1] = 2;
6007 	pc.buffer[4 + 2] = 4;
6008 	pc.buffer[4 + 3] = retries;
6009 	if (idetape_queue_pc_tail(drive, &pc))
6010 		printk(KERN_ERR "ide-tape: Couldn't set retries to %d\n", retries);
6011 }
6012 #endif
6013 
6014 /*
6015  * Configure 32.5KB block size.
6016  */
idetape_onstream_configure_block_size(ide_drive_t * drive)6017 static void idetape_onstream_configure_block_size (ide_drive_t *drive)
6018 {
6019 	idetape_pc_t pc;
6020 	idetape_mode_parameter_header_t *header;
6021 	idetape_block_size_page_t *bs;
6022 
6023 	/*
6024 	 * Get the current block size from the block size mode page
6025 	 */
6026 	idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_SIZE_PAGE);
6027 	if (idetape_queue_pc_tail(drive, &pc))
6028 		printk(KERN_ERR "ide-tape: can't get tape block size mode page\n");
6029 	header = (idetape_mode_parameter_header_t *) pc.buffer;
6030 	bs = (idetape_block_size_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
6031 
6032 #if IDETAPE_DEBUG_INFO
6033 	printk(KERN_INFO "ide-tape: 32KB play back: %s\n", bs->play32 ? "Yes" : "No");
6034 	printk(KERN_INFO "ide-tape: 32.5KB play back: %s\n", bs->play32_5 ? "Yes" : "No");
6035 	printk(KERN_INFO "ide-tape: 32KB record: %s\n", bs->record32 ? "Yes" : "No");
6036 	printk(KERN_INFO "ide-tape: 32.5KB record: %s\n", bs->record32_5 ? "Yes" : "No");
6037 #endif /* IDETAPE_DEBUG_INFO */
6038 
6039 	/*
6040 	 * Configure default auto columns mode, 32.5KB block size
6041 	 */
6042 	bs->one = 1;
6043 	bs->play32 = 0;
6044 	bs->play32_5 = 1;
6045 	bs->record32 = 0;
6046 	bs->record32_5 = 1;
6047 	idetape_create_mode_select_cmd(&pc, sizeof(*header) + sizeof(*bs));
6048 	if (idetape_queue_pc_tail(drive, &pc))
6049 		printk(KERN_ERR "ide-tape: Couldn't set tape block size mode page\n");
6050 
6051 #if ONSTREAM_DEBUG
6052 	/*
6053 	 * In debug mode, we want to see as many errors as possible
6054 	 * to test the error recovery mechanism.
6055 	 */
6056 	idetape_onstream_set_retries(drive, 0);
6057 #endif
6058 }
6059 
6060 /*
6061  * Use INQUIRY to get the firmware revision
6062  */
idetape_get_inquiry_results(ide_drive_t * drive)6063 static void idetape_get_inquiry_results (ide_drive_t *drive)
6064 {
6065 	char *r;
6066 	idetape_tape_t *tape = drive->driver_data;
6067 	idetape_pc_t pc;
6068 	idetape_inquiry_result_t *inquiry;
6069 
6070 	idetape_create_inquiry_cmd(&pc);
6071 	if (idetape_queue_pc_tail(drive, &pc)) {
6072 		printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
6073 		return;
6074 	}
6075 	inquiry = (idetape_inquiry_result_t *) pc.buffer;
6076 	memcpy(tape->vendor_id, inquiry->vendor_id, 8);
6077 	memcpy(tape->product_id, inquiry->product_id, 16);
6078 	memcpy(tape->firmware_revision, inquiry->revision_level, 4);
6079 	ide_fixstring(tape->vendor_id, 10, 0);
6080 	ide_fixstring(tape->product_id, 18, 0);
6081 	ide_fixstring(tape->firmware_revision, 6, 0);
6082 	r = tape->firmware_revision;
6083 	if (*(r + 1) == '.')
6084 		tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
6085 	else if (tape->onstream)
6086 		tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 1) - '0') * 10 + *(r + 2) - '0';
6087 	printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
6088 }
6089 
6090 /*
6091  * Configure the OnStream ATAPI tape drive for default operation
6092  */
idetape_configure_onstream(ide_drive_t * drive)6093 static void idetape_configure_onstream (ide_drive_t *drive)
6094 {
6095 	idetape_tape_t *tape = drive->driver_data;
6096 
6097 	if (tape->firmware_revision_num < 105) {
6098 		printk(KERN_INFO "ide-tape: %s: Old OnStream firmware revision detected (%s)\n", tape->name, tape->firmware_revision);
6099 		printk(KERN_INFO "ide-tape: %s: An upgrade to version 1.05 or above is recommended\n", tape->name);
6100 	}
6101 
6102 	/*
6103 	 * Configure 32.5KB (data+aux) block size.
6104 	 */
6105 	idetape_onstream_configure_block_size(drive);
6106 
6107 	/*
6108 	 * Set vendor name to 'LIN3' for "Linux support version 3".
6109 	 */
6110 	idetape_onstream_set_vendor(drive, "LIN3");
6111 }
6112 
6113 /*
6114  *	idetape_get_mode_sense_parameters asks the tape about its various
6115  *	parameters. This may work for other drives to???
6116  */
idetape_onstream_mode_sense_tape_parameter_page(ide_drive_t * drive,int debug)6117 static void idetape_onstream_mode_sense_tape_parameter_page(ide_drive_t *drive, int debug)
6118 {
6119 	idetape_tape_t *tape = drive->driver_data;
6120 	idetape_pc_t pc;
6121 	idetape_mode_parameter_header_t *header;
6122 	onstream_tape_paramtr_page_t *prm;
6123 
6124 	idetape_create_mode_sense_cmd(&pc, IDETAPE_PARAMTR_PAGE);
6125 	if (idetape_queue_pc_tail(drive, &pc)) {
6126 		printk(KERN_ERR "ide-tape: Can't get tape parameters page - probably no tape inserted in onstream drive\n");
6127 		return;
6128 	}
6129 	header = (idetape_mode_parameter_header_t *) pc.buffer;
6130 	prm = (onstream_tape_paramtr_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
6131 
6132         tape->capacity = ntohs(prm->segtrk) * ntohs(prm->trks);
6133         if (debug) {
6134 	    printk(KERN_INFO "ide-tape: %s <-> %s: Tape length %dMB (%d frames/track, %d tracks = %d blocks, density: %dKbpi)\n",
6135                drive->name, tape->name, tape->capacity/32, ntohs(prm->segtrk), ntohs(prm->trks), tape->capacity, prm->density);
6136         }
6137 
6138         return;
6139 }
6140 
6141 /*
6142  *	idetape_get_mode_sense_results asks the tape about its various
6143  *	parameters. In particular, we will adjust our data transfer buffer
6144  *	size to the recommended value as returned by the tape.
6145  */
idetape_get_mode_sense_results(ide_drive_t * drive)6146 static void idetape_get_mode_sense_results (ide_drive_t *drive)
6147 {
6148 	idetape_tape_t *tape = drive->driver_data;
6149 	idetape_pc_t pc;
6150 	idetape_mode_parameter_header_t *header;
6151 	idetape_capabilities_page_t *capabilities;
6152 
6153 	idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
6154 	if (idetape_queue_pc_tail (drive, &pc)) {
6155 		printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
6156 		tape->tape_block_size = 512;
6157 		tape->capabilities.ctl = 52;
6158 		tape->capabilities.speed = 450;
6159 		tape->capabilities.buffer_size = 6 * 52;
6160 		return;
6161 	}
6162 	header = (idetape_mode_parameter_header_t *) pc.buffer;
6163 	capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
6164 
6165 	capabilities->max_speed = ntohs(capabilities->max_speed);
6166 	capabilities->ctl = ntohs(capabilities->ctl);
6167 	capabilities->speed = ntohs(capabilities->speed);
6168 	capabilities->buffer_size = ntohs(capabilities->buffer_size);
6169 
6170 	if (!capabilities->speed) {
6171 		printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
6172 		capabilities->speed = 650;
6173 	}
6174 	if (!capabilities->max_speed) {
6175 		printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
6176 		capabilities->max_speed = 650;
6177 	}
6178 	if (!capabilities->ctl) {
6179 		printk(KERN_INFO "ide-tape: %s: overriding capabilities->ctl (assuming 26KB)\n", drive->name);
6180 		capabilities->ctl = 52;
6181 	}
6182 
6183 	tape->capabilities = *capabilities;		/* Save us a copy */
6184 	if (capabilities->blk512)
6185 		tape->tape_block_size = 512;
6186 	else if (capabilities->blk1024)
6187 		tape->tape_block_size = 1024;
6188 	else if (tape->onstream && capabilities->blk32768)
6189 		tape->tape_block_size = 32768;
6190 
6191 #if IDETAPE_DEBUG_INFO
6192 	printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
6193 	printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
6194 	printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
6195 	printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
6196 	printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
6197 	printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
6198 
6199 	printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
6200 	printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
6201 	printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
6202 	printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
6203 	printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
6204 	printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
6205 	printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
6206 	printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
6207 	printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
6208 	printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
6209 	printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
6210 	printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
6211 	printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
6212 	printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
6213 	printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
6214 	printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
6215 	printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
6216 	printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
6217 	printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
6218 	printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
6219 #endif /* IDETAPE_DEBUG_INFO */
6220 }
6221 
6222 /*
6223  *	ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
6224  *	and if it succeeds sets the tape block size with the reported value
6225  */
idetape_get_blocksize_from_block_descriptor(ide_drive_t * drive)6226 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
6227 {
6228 
6229 	idetape_tape_t *tape = drive->driver_data;
6230 	idetape_pc_t pc;
6231 	idetape_mode_parameter_header_t *header;
6232 	idetape_parameter_block_descriptor_t *block_descrp;
6233 
6234 	idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
6235 	if (idetape_queue_pc_tail(drive, &pc)) {
6236 		printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
6237 		return;
6238 	}
6239 	header = (idetape_mode_parameter_header_t *) pc.buffer;
6240 	block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
6241 	tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
6242 #if IDETAPE_DEBUG_INFO
6243 	printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
6244 #endif /* IDETAPE_DEBUG_INFO */
6245 }
6246 
idetape_add_settings(ide_drive_t * drive)6247 static void idetape_add_settings (ide_drive_t *drive)
6248 {
6249 	idetape_tape_t *tape = drive->driver_data;
6250 
6251 /*
6252  *			drive	setting name	read/write	ioctl	ioctl		data type	min			max			mul_factor			div_factor			data pointer				set function
6253  */
6254 	ide_add_setting(drive,	"buffer",	SETTING_READ,	-1,	-1,		TYPE_SHORT,	0,			0xffff,			1,				2,				&tape->capabilities.buffer_size,	NULL);
6255 	ide_add_setting(drive,	"pipeline_min",	SETTING_RW,	-1,	-1,		TYPE_INT,	1,			0xffff,			tape->stage_size / 1024,	1,				&tape->min_pipeline,			NULL);
6256 	ide_add_setting(drive,	"pipeline",	SETTING_RW,	-1,	-1,		TYPE_INT,	1,			0xffff,			tape->stage_size / 1024,	1,				&tape->max_stages,			NULL);
6257 	ide_add_setting(drive,	"pipeline_max",	SETTING_RW,	-1,	-1,		TYPE_INT,	1,			0xffff,			tape->stage_size / 1024,	1,				&tape->max_pipeline,			NULL);
6258 	ide_add_setting(drive,	"pipeline_used",SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			tape->stage_size / 1024,	1,				&tape->nr_stages,			NULL);
6259 	ide_add_setting(drive,	"pipeline_pending",SETTING_READ,-1,	-1,		TYPE_INT,	0,			0xffff,			tape->stage_size / 1024,	1,				&tape->nr_pending_stages,		NULL);
6260 	ide_add_setting(drive,	"speed",	SETTING_READ,	-1,	-1,		TYPE_SHORT,	0,			0xffff,			1,				1,				&tape->capabilities.speed,		NULL);
6261 	ide_add_setting(drive,	"stage",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1024,				&tape->stage_size,			NULL);
6262 	ide_add_setting(drive,	"tdsc",		SETTING_RW,	-1,	-1,		TYPE_INT,	IDETAPE_DSC_RW_MIN,	IDETAPE_DSC_RW_MAX,	1000,				HZ,				&tape->best_dsc_rw_frequency,		NULL);
6263 	ide_add_setting(drive,	"dsc_overlap",	SETTING_RW,	-1,	-1,		TYPE_BYTE,	0,			1,			1,				1,				&drive->dsc_overlap,			NULL);
6264 	ide_add_setting(drive,	"pipeline_head_speed_c",SETTING_READ,	-1,	-1,	TYPE_INT,	0,			0xffff,			1,				1,				&tape->controlled_pipeline_head_speed,	NULL);
6265 	ide_add_setting(drive,	"pipeline_head_speed_u",SETTING_READ,	-1,	-1,	TYPE_INT,	0,			0xffff,			1,				1,				&tape->uncontrolled_pipeline_head_speed,	NULL);
6266 	ide_add_setting(drive,	"avg_speed",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->avg_speed,		NULL);
6267 	ide_add_setting(drive,	"debug_level",SETTING_RW,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->debug_level,		NULL);
6268 	if (tape->onstream) {
6269 		ide_add_setting(drive,	"cur_frames",	SETTING_READ,	-1,	-1,		TYPE_SHORT,	0,			0xffff,			1,				1,				&tape->cur_frames,		NULL);
6270 		ide_add_setting(drive,	"max_frames",	SETTING_READ,	-1,	-1,		TYPE_SHORT,	0,			0xffff,			1,				1,				&tape->max_frames,		NULL);
6271 		ide_add_setting(drive,	"insert_speed",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->insert_speed,		NULL);
6272 		ide_add_setting(drive,	"speed_control",SETTING_RW,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->speed_control,		NULL);
6273 		ide_add_setting(drive,	"tape_still_time",SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->tape_still_time,		NULL);
6274 		ide_add_setting(drive,	"max_insert_speed",SETTING_RW,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->max_insert_speed,	NULL);
6275 		ide_add_setting(drive,	"insert_size",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->insert_size,		NULL);
6276 		ide_add_setting(drive,	"capacity",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->capacity,		NULL);
6277 		ide_add_setting(drive,	"first_frame",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->first_frame_position,		NULL);
6278 		ide_add_setting(drive,	"logical_blk",	SETTING_READ,	-1,	-1,		TYPE_INT,	0,			0xffff,			1,				1,				&tape->logical_blk_num,		NULL);
6279 	}
6280 }
6281 
6282 /*
6283  *	ide_setup is called to:
6284  *
6285  *		1.	Initialize our various state variables.
6286  *		2.	Ask the tape for its capabilities.
6287  *		3.	Allocate a buffer which will be used for data
6288  *			transfer. The buffer size is chosen based on
6289  *			the recommendation which we received in step (2).
6290  *
6291  *	Note that at this point ide.c already assigned us an irq, so that
6292  *	we can queue requests here and wait for their completion.
6293  */
idetape_setup(ide_drive_t * drive,idetape_tape_t * tape,int minor)6294 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
6295 {
6296 	unsigned long t1, tmid, tn, t;
6297 	int speed;
6298 	struct idetape_id_gcw gcw;
6299 	int stage_size;
6300 	struct sysinfo si;
6301 
6302 	memset(tape, 0, sizeof (idetape_tape_t));
6303 	spin_lock_init(&tape->spinlock);
6304 	drive->driver_data = tape;
6305 	/* An ATAPI device ignores DRDY */
6306 	drive->ready_stat = 0;
6307 	if (strstr(drive->id->model, "OnStream DI-"))
6308 		tape->onstream = 1;
6309 	drive->dsc_overlap = (HWIF(drive)->no_dsc) ? 0 : 1;
6310 	if (HWIF(drive)->no_dsc) {
6311 		printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
6312 			tape->name);
6313 		drive->dsc_overlap = 0;
6314 	}
6315 	tape->drive = drive;
6316 	tape->minor = minor;
6317 	tape->name[0] = 'h';
6318 	tape->name[1] = 't';
6319 	tape->name[2] = '0' + minor;
6320 	tape->chrdev_direction = idetape_direction_none;
6321 	tape->pc = tape->pc_stack;
6322 	tape->max_insert_speed = 10000;
6323 	tape->speed_control = 1;
6324 	*((unsigned short *) &gcw) = drive->id->config;
6325 	if (gcw.drq_type == 1)
6326 		set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
6327 
6328 	tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
6329 
6330 	idetape_get_inquiry_results(drive);
6331 	idetape_get_mode_sense_results(drive);
6332 	idetape_get_blocksize_from_block_descriptor(drive);
6333 	if (tape->tape_block_size == 0) {
6334 		printk(KERN_WARNING "ide-tape: Zero block size, using 512\n");
6335 		tape->tape_block_size = 512;
6336 	}
6337 	if (tape->onstream) {
6338 		idetape_onstream_mode_sense_tape_parameter_page(drive, 1);
6339 		idetape_configure_onstream(drive);
6340 	}
6341 	tape->user_bs_factor = 1;
6342 	tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
6343 	while (tape->stage_size > 0xffff) {
6344 		printk (KERN_NOTICE "ide-tape: decreasing stage size\n");
6345 		tape->capabilities.ctl /= 2;
6346 		tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
6347 	}
6348 	stage_size = tape->stage_size;
6349 	if (tape->onstream)
6350 		stage_size = 32768 + 512;
6351 	tape->pages_per_stage = stage_size / PAGE_SIZE;
6352 	if (stage_size % PAGE_SIZE) {
6353 		tape->pages_per_stage++;
6354 		tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
6355 	}
6356 
6357 	/*
6358 	 *	Select the "best" DSC read/write polling frequency
6359 	 *	and pipeline size.
6360 	 */
6361 	speed = IDE_MAX(tape->capabilities.speed, tape->capabilities.max_speed);
6362 
6363 	tape->max_stages = speed * 1000 * 10 / tape->stage_size;
6364 
6365 	/*
6366 	 * 	Limit memory use for pipeline to 10% of physical memory
6367 	 */
6368 	si_meminfo(&si);
6369 	if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
6370 		tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
6371 	tape->max_stages   = IDE_MIN(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
6372 	tape->min_pipeline = IDE_MIN(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
6373 	tape->max_pipeline = IDE_MIN(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
6374 	if (tape->max_stages == 0)
6375 		tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
6376 
6377 	t1 = (tape->stage_size * HZ) / (speed * 1000);
6378 	tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
6379 	tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
6380 
6381 	if (tape->max_stages)
6382 		t = tn;
6383 	else
6384 		t = t1;
6385 
6386 	/*
6387 	 *	Ensure that the number we got makes sense; limit
6388 	 *	it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
6389 	 */
6390 	tape->best_dsc_rw_frequency = IDE_MAX(IDE_MIN(t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
6391 	printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
6392 		"%dkB pipeline, %lums tDSC%s\n",
6393 		drive->name, tape->name, tape->capabilities.speed,
6394 		(tape->capabilities.buffer_size * 512) / tape->stage_size,
6395 		tape->stage_size / 1024,
6396 		tape->max_stages * tape->stage_size / 1024,
6397 		tape->best_dsc_rw_frequency * 1000 / HZ,
6398 		drive->using_dma ? ", DMA":"");
6399 
6400 	idetape_add_settings(drive);
6401 }
6402 
idetape_cleanup(ide_drive_t * drive)6403 static int idetape_cleanup (ide_drive_t *drive)
6404 {
6405 	idetape_tape_t *tape = drive->driver_data;
6406 	int minor = tape->minor;
6407 	unsigned long flags;
6408 
6409 	spin_lock_irqsave(&io_request_lock, flags);
6410 	if (test_bit(IDETAPE_BUSY, &tape->flags) || drive->usage ||
6411 	    tape->first_stage != NULL || tape->merge_stage_size) {
6412 		spin_unlock_irqrestore(&io_request_lock, flags);
6413 		return 1;
6414 	}
6415 	idetape_chrdevs[minor].drive = NULL;
6416 	spin_unlock_irqrestore(&io_request_lock, flags);
6417 	DRIVER(drive)->busy = 0;
6418 	(void) ide_unregister_subdriver(drive);
6419 	drive->driver_data = NULL;
6420 	devfs_unregister(tape->de_r);
6421 	devfs_unregister(tape->de_n);
6422 	kfree(tape);
6423 	for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++)
6424 		if (idetape_chrdevs[minor].drive != NULL)
6425 			return 0;
6426 	devfs_unregister_chrdev(IDETAPE_MAJOR, "ht");
6427 	idetape_chrdev_present = 0;
6428 	return 0;
6429 }
6430 
6431 #ifdef CONFIG_PROC_FS
6432 
proc_idetape_read_name(char * page,char ** start,off_t off,int count,int * eof,void * data)6433 static int proc_idetape_read_name
6434 	(char *page, char **start, off_t off, int count, int *eof, void *data)
6435 {
6436 	ide_drive_t	*drive = (ide_drive_t *) data;
6437 	idetape_tape_t	*tape = drive->driver_data;
6438 	char		*out = page;
6439 	int		len;
6440 
6441 	len = sprintf(out, "%s\n", tape->name);
6442 	PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
6443 }
6444 
6445 static ide_proc_entry_t idetape_proc[] = {
6446 	{ "name",	S_IFREG|S_IRUGO,	proc_idetape_read_name,	NULL },
6447 	{ NULL, 0, NULL, NULL }
6448 };
6449 
6450 #else
6451 
6452 #define	idetape_proc	NULL
6453 
6454 #endif
6455 
6456 int idetape_init (void);
6457 int idetape_attach(ide_drive_t *drive);
6458 
6459 /*
6460  *	IDE subdriver functions, registered with ide.c
6461  */
6462 static ide_driver_t idetape_driver = {
6463 	name:			"ide-tape",
6464 	version:		IDETAPE_VERSION,
6465 	media:			ide_tape,
6466 	busy:			1,
6467 	supports_dma:		1,
6468 	supports_dsc_overlap: 	1,
6469 	cleanup:		idetape_cleanup,
6470 	standby:		NULL,
6471 	suspend:		NULL,
6472 	resume:			NULL,
6473 	flushcache:		NULL,
6474 	do_request:		idetape_do_request,
6475 	end_request:		idetape_end_request,
6476 	sense:			NULL,
6477 	error:			NULL,
6478 	ioctl:			idetape_blkdev_ioctl,
6479 	open:			idetape_blkdev_open,
6480 	release:		idetape_blkdev_release,
6481 	media_change:		NULL,
6482 	revalidate:		NULL,
6483 	pre_reset:		idetape_pre_reset,
6484 	capacity:		NULL,
6485 	special:		NULL,
6486 	proc:			idetape_proc,
6487 	init:			idetape_init,
6488 	attach:			idetape_attach,
6489 	ata_prebuilder:		NULL,
6490 	atapi_prebuilder:	NULL,
6491 };
6492 
6493 static ide_module_t idetape_module = {
6494 	IDE_DRIVER_MODULE,
6495 	idetape_init,
6496 	&idetape_driver,
6497 	NULL
6498 };
6499 
6500 /*
6501  *	Our character device supporting functions, passed to register_chrdev.
6502  */
6503 static struct file_operations idetape_fops = {
6504 	owner:		THIS_MODULE,
6505 	read:		idetape_chrdev_read,
6506 	write:		idetape_chrdev_write,
6507 	ioctl:		idetape_chrdev_ioctl,
6508 	open:		idetape_chrdev_open,
6509 	release:	idetape_chrdev_release,
6510 };
6511 
idetape_attach(ide_drive_t * drive)6512 int idetape_attach (ide_drive_t *drive)
6513 {
6514 	idetape_tape_t *tape;
6515 	int minor = 0, ret = 0;
6516 
6517 	MOD_INC_USE_COUNT;
6518 
6519 	if (idetape_identify_device(drive)) {
6520 		printk(KERN_ERR "ide-tape: %s: not supported by this "
6521 			"version of ide-tape\n", drive->name);
6522 			ret = 1;
6523 			goto bye_game_over;
6524 	}
6525 	if (drive->scsi) {
6526 		if (strstr(drive->id->model, "OnStream DI-30")) {
6527 			printk("ide-tape: ide-scsi emulation is not "
6528 				"supported for %s.\n", drive->id->model);
6529 		} else {
6530 			printk("ide-tape: passing drive %s to ide-scsi "
6531 				"emulation.\n", drive->name);
6532 			ret = 1;
6533 			goto bye_game_over;
6534 		}
6535 	}
6536 	tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
6537 	if (tape == NULL) {
6538 		printk(KERN_ERR "ide-tape: %s: Can't allocate a "
6539 			"tape structure\n", drive->name);
6540 		ret = 1;
6541 		goto bye_game_over;
6542 	}
6543 	if (ide_register_subdriver(drive,
6544 			&idetape_driver, IDE_SUBDRIVER_VERSION)) {
6545 		printk(KERN_ERR "ide-tape: %s: Failed to register the "
6546 				"driver with ide.c\n", drive->name);
6547 		kfree(tape);
6548 		ret = 1;
6549 		goto bye_game_over;
6550 	}
6551 	for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
6552 	idetape_setup (drive, tape, minor);
6553 	idetape_chrdevs[minor].drive = drive;
6554 	tape->de_r = devfs_register(drive->de, "mt", DEVFS_FL_DEFAULT,
6555 				    HWIF(drive)->major, minor,
6556 				    S_IFCHR | S_IRUGO | S_IWUGO,
6557 				    &idetape_fops, NULL);
6558 	tape->de_n = devfs_register(drive->de, "mtn", DEVFS_FL_DEFAULT,
6559 				    HWIF(drive)->major, minor + 128,
6560 				    S_IFCHR | S_IRUGO | S_IWUGO,
6561 				    &idetape_fops, NULL);
6562 	devfs_register_tape(tape->de_r);
6563 
6564 bye_game_over:
6565 	MOD_DEC_USE_COUNT;
6566 
6567 	return ret;
6568 }
6569 
6570 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
6571 MODULE_LICENSE("GPL");
6572 
idetape_exit(void)6573 static void __exit idetape_exit (void)
6574 {
6575 	ide_drive_t *drive;
6576 	int minor;
6577 
6578 	for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++) {
6579 		drive = idetape_chrdevs[minor].drive;
6580 		if (drive) {
6581 			if (idetape_cleanup(drive))
6582 				printk(KERN_ERR "ide-tape: %s: cleanup_module() "
6583 					"called while still busy\n", drive->name);
6584 #ifdef CONFIG_PROC_FS
6585 			if (drive->proc)
6586 				ide_remove_proc_entries(drive->proc, idetape_proc);
6587 #endif
6588 		}
6589 	}
6590 
6591 	ide_unregister_module(&idetape_module);
6592 }
6593 
idetape_init(void)6594 int idetape_init (void)
6595 {
6596 #ifdef CLASSIC_BUILTINS_METHOD
6597 	ide_drive_t *drive;
6598 	idetape_tape_t *tape;
6599 	int minor, failed = 0, supported = 0;
6600 /* DRIVER(drive)->busy++; */
6601 	MOD_INC_USE_COUNT;
6602 #if ONSTREAM_DEBUG
6603 	printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_init\n");
6604 #endif
6605 	if (!idetape_chrdev_present)
6606 		for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
6607 			idetape_chrdevs[minor].drive = NULL;
6608 	if ((drive = ide_scan_devices(ide_tape, idetape_driver.name,
6609 			NULL, failed++)) == NULL) {
6610 		ide_register_module(&idetape_module);
6611 		MOD_DEC_USE_COUNT;
6612 #if ONSTREAM_DEBUG
6613 		printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in "
6614 			"idetape_init\n");
6615 #endif
6616 		return 0;
6617 	}
6618 	if (!idetape_chrdev_present &&
6619 	    devfs_register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
6620 		printk(KERN_ERR "ide-tape: Failed to register character "
6621 			"device interface\n");
6622 		MOD_DEC_USE_COUNT;
6623 #if ONSTREAM_DEBUG
6624 		printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in "
6625 			"idetape_init\n");
6626 #endif
6627 		return -EBUSY;
6628 	}
6629 	do {
6630 		if (!idetape_identify_device(drive)) {
6631 			printk(KERN_ERR "ide-tape: %s: not supported by this "
6632 				"version of ide-tape\n", drive->name);
6633 			continue;
6634 		}
6635 		if (drive->scsi) {
6636 			if (strstr(drive->id->model, "OnStream DI-")) {
6637 				printk("ide-tape: ide-scsi emulation is not "
6638 					"supported for %s.\n",
6639 					drive->id->model);
6640 			} else {
6641 				printk("ide-tape: passing drive %s to "
6642 					"ide-scsi emulation.\n", drive->name);
6643 				continue;
6644 			}
6645 		}
6646 		tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
6647 		if (tape == NULL) {
6648 			printk(KERN_ERR "ide-tape: %s: Can't allocate a tape "
6649 				"structure\n", drive->name);
6650 			continue;
6651 		}
6652 		if (ide_register_subdriver(drive,
6653 				&idetape_driver, IDE_SUBDRIVER_VERSION)) {
6654 			printk(KERN_ERR "ide-tape: %s: Failed to register the "
6655 				"driver with ide.c\n", drive->name);
6656 			kfree(tape);
6657 			continue;
6658 		}
6659 		for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
6660 		idetape_setup(drive, tape, minor);
6661 		idetape_chrdevs[minor].drive = drive;
6662 		tape->de_r =
6663 		    devfs_register(drive->de, "mt", DEVFS_FL_DEFAULT,
6664 				    HWIF(drive)->major, minor,
6665 				    S_IFCHR | S_IRUGO | S_IWUGO,
6666 				    &idetape_fops, NULL);
6667 		tape->de_n =
6668 		    devfs_register(drive->de, "mtn", DEVFS_FL_DEFAULT,
6669 				    HWIF(drive)->major, minor + 128,
6670 				    S_IFCHR | S_IRUGO | S_IWUGO,
6671 				    &idetape_fops, NULL);
6672 		devfs_register_tape(tape->de_r);
6673 		supported++;
6674 		failed--;
6675 	} while ((drive = ide_scan_devices(ide_tape,
6676 			idetape_driver.name, NULL, failed++)) != NULL);
6677 	if (!idetape_chrdev_present && !supported) {
6678 		devfs_unregister_chrdev(IDETAPE_MAJOR, "ht");
6679 	} else
6680 		idetape_chrdev_present = 1;
6681 #else /* ! CLASSIC_BUILTINS_METHOD */
6682 	int minor = 0;
6683 
6684 	if (idetape_chrdev_present)
6685 		return 0;
6686 	idetape_chrdev_present = 1;
6687 	for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
6688 		idetape_chrdevs[minor].drive = NULL;
6689 	devfs_register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops);
6690 	MOD_INC_USE_COUNT;
6691 #endif /* CLASSIC_BUILTINS_METHOD */
6692 	ide_register_module(&idetape_module);
6693 	MOD_DEC_USE_COUNT;
6694 	return 0;
6695 }
6696 
6697 module_init(idetape_init);
6698 module_exit(idetape_exit);
6699