Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / drivers / ide / ide-tape.c
blob95e780abf41c69473943c02cb56592a9b9d1ae5f
1 /*
2 * linux/drivers/block/ide-tape.c Version 1.16f Dec 15, 1999
4 * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
6 * This driver was constructed as a student project in the software laboratory
7 * of the faculty of electrical engineering in the Technion - Israel's
8 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 * It is hereby placed under the terms of the GNU general public license.
11 * (See linux/COPYING).
15 * IDE ATAPI streaming tape driver.
17 * This driver is a part of the Linux ide driver and works in co-operation
18 * with linux/drivers/block/ide.c.
20 * The driver, in co-operation with ide.c, basically traverses the
21 * request-list for the block device interface. The character device
22 * interface, on the other hand, creates new requests, adds them
23 * to the request-list of the block device, and waits for their completion.
25 * Pipelined operation mode is now supported on both reads and writes.
27 * The block device major and minor numbers are determined from the
28 * tape's relative position in the ide interfaces, as explained in ide.c.
30 * The character device interface consists of the following devices:
32 * ht0 major 37, minor 0 first IDE tape, rewind on close.
33 * ht1 major 37, minor 1 second IDE tape, rewind on close.
34 * ...
35 * nht0 major 37, minor 128 first IDE tape, no rewind on close.
36 * nht1 major 37, minor 129 second IDE tape, no rewind on close.
37 * ...
39 * Run linux/scripts/MAKEDEV.ide to create the above entries.
41 * The general magnetic tape commands compatible interface, as defined by
42 * include/linux/mtio.h, is accessible through the character device.
44 * General ide driver configuration options, such as the interrupt-unmask
45 * flag, can be configured by issuing an ioctl to the block device interface,
46 * as any other ide device.
48 * Our own ide-tape ioctl's can be issued to either the block device or
49 * the character device interface.
51 * Maximal throughput with minimal bus load will usually be achieved in the
52 * following scenario:
54 * 1. ide-tape is operating in the pipelined operation mode.
55 * 2. No buffering is performed by the user backup program.
57 * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
59 * Ver 0.1 Nov 1 95 Pre-working code :-)
60 * Ver 0.2 Nov 23 95 A short backup (few megabytes) and restore procedure
61 * was successful ! (Using tar cvf ... on the block
62 * device interface).
63 * A longer backup resulted in major swapping, bad
64 * overall Linux performance and eventually failed as
65 * we received non serial read-ahead requests from the
66 * buffer cache.
67 * Ver 0.3 Nov 28 95 Long backups are now possible, thanks to the
68 * character device interface. Linux's responsiveness
69 * and performance doesn't seem to be much affected
70 * from the background backup procedure.
71 * Some general mtio.h magnetic tape operations are
72 * now supported by our character device. As a result,
73 * popular tape utilities are starting to work with
74 * ide tapes :-)
75 * The following configurations were tested:
76 * 1. An IDE ATAPI TAPE shares the same interface
77 * and irq with an IDE ATAPI CDROM.
78 * 2. An IDE ATAPI TAPE shares the same interface
79 * and irq with a normal IDE disk.
80 * Both configurations seemed to work just fine !
81 * However, to be on the safe side, it is meanwhile
82 * recommended to give the IDE TAPE its own interface
83 * and irq.
84 * The one thing which needs to be done here is to
85 * add a "request postpone" feature to ide.c,
86 * so that we won't have to wait for the tape to finish
87 * performing a long media access (DSC) request (such
88 * as a rewind) before we can access the other device
89 * on the same interface. This effect doesn't disturb
90 * normal operation most of the time because read/write
91 * requests are relatively fast, and once we are
92 * performing one tape r/w request, a lot of requests
93 * from the other device can be queued and ide.c will
94 * service all of them after this single tape request.
95 * Ver 1.0 Dec 11 95 Integrated into Linux 1.3.46 development tree.
96 * On each read / write request, we now ask the drive
97 * if we can transfer a constant number of bytes
98 * (a parameter of the drive) only to its buffers,
99 * without causing actual media access. If we can't,
100 * we just wait until we can by polling the DSC bit.
101 * This ensures that while we are not transferring
102 * more bytes than the constant referred to above, the
103 * interrupt latency will not become too high and
104 * we won't cause an interrupt timeout, as happened
105 * occasionally in the previous version.
106 * While polling for DSC, the current request is
107 * postponed and ide.c is free to handle requests from
108 * the other device. This is handled transparently to
109 * ide.c. The hwgroup locking method which was used
110 * in the previous version was removed.
111 * Use of new general features which are provided by
112 * ide.c for use with atapi devices.
113 * (Programming done by Mark Lord)
114 * Few potential bug fixes (Again, suggested by Mark)
115 * Single character device data transfers are now
116 * not limited in size, as they were before.
117 * We are asking the tape about its recommended
118 * transfer unit and send a larger data transfer
119 * as several transfers of the above size.
120 * For best results, use an integral number of this
121 * basic unit (which is shown during driver
122 * initialization). I will soon add an ioctl to get
123 * this important parameter.
124 * Our data transfer buffer is allocated on startup,
125 * rather than before each data transfer. This should
126 * ensure that we will indeed have a data buffer.
127 * Ver 1.1 Dec 14 95 Fixed random problems which occurred when the tape
128 * shared an interface with another device.
129 * (poll_for_dsc was a complete mess).
130 * Removed some old (non-active) code which had
131 * to do with supporting buffer cache originated
132 * requests.
133 * The block device interface can now be opened, so
134 * that general ide driver features like the unmask
135 * interrupts flag can be selected with an ioctl.
136 * This is the only use of the block device interface.
137 * New fast pipelined operation mode (currently only on
138 * writes). When using the pipelined mode, the
139 * throughput can potentially reach the maximum
140 * tape supported throughput, regardless of the
141 * user backup program. On my tape drive, it sometimes
142 * boosted performance by a factor of 2. Pipelined
143 * mode is enabled by default, but since it has a few
144 * downfalls as well, you may want to disable it.
145 * A short explanation of the pipelined operation mode
146 * is available below.
147 * Ver 1.2 Jan 1 96 Eliminated pipelined mode race condition.
148 * Added pipeline read mode. As a result, restores
149 * are now as fast as backups.
150 * Optimized shared interface behavior. The new behavior
151 * typically results in better IDE bus efficiency and
152 * higher tape throughput.
153 * Pre-calculation of the expected read/write request
154 * service time, based on the tape's parameters. In
155 * the pipelined operation mode, this allows us to
156 * adjust our polling frequency to a much lower value,
157 * and thus to dramatically reduce our load on Linux,
158 * without any decrease in performance.
159 * Implemented additional mtio.h operations.
160 * The recommended user block size is returned by
161 * the MTIOCGET ioctl.
162 * Additional minor changes.
163 * Ver 1.3 Feb 9 96 Fixed pipelined read mode bug which prevented the
164 * use of some block sizes during a restore procedure.
165 * The character device interface will now present a
166 * continuous view of the media - any mix of block sizes
167 * during a backup/restore procedure is supported. The
168 * driver will buffer the requests internally and
169 * convert them to the tape's recommended transfer
170 * unit, making performance almost independent of the
171 * chosen user block size.
172 * Some improvements in error recovery.
173 * By cooperating with ide-dma.c, bus mastering DMA can
174 * now sometimes be used with IDE tape drives as well.
175 * Bus mastering DMA has the potential to dramatically
176 * reduce the CPU's overhead when accessing the device,
177 * and can be enabled by using hdparm -d1 on the tape's
178 * block device interface. For more info, read the
179 * comments in ide-dma.c.
180 * Ver 1.4 Mar 13 96 Fixed serialize support.
181 * Ver 1.5 Apr 12 96 Fixed shared interface operation, broken in 1.3.85.
182 * Fixed pipelined read mode inefficiency.
183 * Fixed nasty null dereferencing bug.
184 * Ver 1.6 Aug 16 96 Fixed FPU usage in the driver.
185 * Fixed end of media bug.
186 * Ver 1.7 Sep 10 96 Minor changes for the CONNER CTT8000-A model.
187 * Ver 1.8 Sep 26 96 Attempt to find a better balance between good
188 * interactive response and high system throughput.
189 * Ver 1.9 Nov 5 96 Automatically cross encountered filemarks rather
190 * than requiring an explicit FSF command.
191 * Abort pending requests at end of media.
192 * MTTELL was sometimes returning incorrect results.
193 * Return the real block size in the MTIOCGET ioctl.
194 * Some error recovery bug fixes.
195 * Ver 1.10 Nov 5 96 Major reorganization.
196 * Reduced CPU overhead a bit by eliminating internal
197 * bounce buffers.
198 * Added module support.
199 * Added multiple tape drives support.
200 * Added partition support.
201 * Rewrote DSC handling.
202 * Some portability fixes.
203 * Removed ide-tape.h.
204 * Additional minor changes.
205 * Ver 1.11 Dec 2 96 Bug fix in previous DSC timeout handling.
206 * Use ide_stall_queue() for DSC overlap.
207 * Use the maximum speed rather than the current speed
208 * to compute the request service time.
209 * Ver 1.12 Dec 7 97 Fix random memory overwriting and/or last block data
210 * corruption, which could occur if the total number
211 * of bytes written to the tape was not an integral
212 * number of tape blocks.
213 * Add support for INTERRUPT DRQ devices.
214 * Ver 1.13 Jan 2 98 Add "speed == 0" work-around for HP COLORADO 5GB
215 * Ver 1.14 Dec 30 98 Partial fixes for the Sony/AIWA tape drives.
216 * Replace cli()/sti() with hwgroup spinlocks.
217 * Ver 1.15 Mar 25 99 Fix SMP race condition by replacing hwgroup
218 * spinlock with private per-tape spinlock.
219 * Ver 1.16 Sep 1 99 Add OnStream tape support.
220 * Abort read pipeline on EOD.
221 * Wait for the tape to become ready in case it returns
222 * "in the process of becoming ready" on open().
223 * Fix zero padding of the last written block in
224 * case the tape block size is larger than PAGE_SIZE.
225 * Decrease the default disconnection time to tn.
226 * Ver 1.16e Oct 3 99 Minor fixes.
227 * Ver 1.16e1 Oct 13 99 Patches by Arnold Niessen,
228 * niessen@iae.nl / arnold.niessen@philips.com
229 * GO-1) Undefined code in idetape_read_position
230 * according to Gadi's email
231 * AJN-1) Minor fix asc == 11 should be asc == 0x11
232 * in idetape_issue_packet_command (did effect
233 * debugging output only)
234 * AJN-2) Added more debugging output, and
235 * added ide-tape: where missing. I would also
236 * like to add tape->name where possible
237 * AJN-3) Added different debug_level's
238 * via /proc/ide/hdc/settings
239 * "debug_level" determines amount of debugging output;
240 * can be changed using /proc/ide/hdx/settings
241 * 0 : almost no debugging output
242 * 1 : 0+output errors only
243 * 2 : 1+output all sensekey/asc
244 * 3 : 2+follow all chrdev related procedures
245 * 4 : 3+follow all procedures
246 * 5 : 4+include pc_stack rq_stack info
247 * 6 : 5+USE_COUNT updates
248 * AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
249 * from 5 to 10 minutes
250 * AJN-5) Changed maximum number of blocks to skip when
251 * reading tapes with multiple consecutive write
252 * errors from 100 to 1000 in idetape_get_logical_blk
253 * Proposed changes to code:
254 * 1) output "logical_blk_num" via /proc
255 * 2) output "current_operation" via /proc
256 * 3) Either solve or document the fact that `mt rewind' is
257 * required after reading from /dev/nhtx to be
258 * able to rmmod the idetape module;
259 * Also, sometimes an application finishes but the
260 * device remains `busy' for some time. Same cause ?
261 * Proposed changes to release-notes:
262 * 4) write a simple `quickstart' section in the
263 * release notes; I volunteer if you don't want to
264 * 5) include a pointer to video4linux in the doc
265 * to stimulate video applications
266 * 6) release notes lines 331 and 362: explain what happens
267 * if the application data rate is higher than 1100 KB/s;
268 * similar approach to lower-than-500 kB/s ?
269 * 7) 6.6 Comparison; wouldn't it be better to allow different
270 * strategies for read and write ?
271 * Wouldn't it be better to control the tape buffer
272 * contents instead of the bandwidth ?
273 * 8) line 536: replace will by would (if I understand
274 * this section correctly, a hypothetical and unwanted situation
275 * is being described)
276 * Ver 1.16f Dec 15 99 Change place of the secondary OnStream header frames.
279 * Here are some words from the first releases of hd.c, which are quoted
280 * in ide.c and apply here as well:
282 * | Special care is recommended. Have Fun!
287 * An overview of the pipelined operation mode.
289 * In the pipelined write mode, we will usually just add requests to our
290 * pipeline and return immediately, before we even start to service them. The
291 * user program will then have enough time to prepare the next request while
292 * we are still busy servicing previous requests. In the pipelined read mode,
293 * the situation is similar - we add read-ahead requests into the pipeline,
294 * before the user even requested them.
296 * The pipeline can be viewed as a "safety net" which will be activated when
297 * the system load is high and prevents the user backup program from keeping up
298 * with the current tape speed. At this point, the pipeline will get
299 * shorter and shorter but the tape will still be streaming at the same speed.
300 * Assuming we have enough pipeline stages, the system load will hopefully
301 * decrease before the pipeline is completely empty, and the backup program
302 * will be able to "catch up" and refill the pipeline again.
304 * When using the pipelined mode, it would be best to disable any type of
305 * buffering done by the user program, as ide-tape already provides all the
306 * benefits in the kernel, where it can be done in a more efficient way.
307 * As we will usually not block the user program on a request, the most
308 * efficient user code will then be a simple read-write-read-... cycle.
309 * Any additional logic will usually just slow down the backup process.
311 * Using the pipelined mode, I get a constant over 400 KBps throughput,
312 * which seems to be the maximum throughput supported by my tape.
314 * However, there are some downfalls:
316 * 1. We use memory (for data buffers) in proportional to the number
317 * of pipeline stages (each stage is about 26 KB with my tape).
318 * 2. In the pipelined write mode, we cheat and postpone error codes
319 * to the user task. In read mode, the actual tape position
320 * will be a bit further than the last requested block.
322 * Concerning (1):
324 * 1. We allocate stages dynamically only when we need them. When
325 * we don't need them, we don't consume additional memory. In
326 * case we can't allocate stages, we just manage without them
327 * (at the expense of decreased throughput) so when Linux is
328 * tight in memory, we will not pose additional difficulties.
330 * 2. The maximum number of stages (which is, in fact, the maximum
331 * amount of memory) which we allocate is limited by the compile
332 * time parameter IDETAPE_MAX_PIPELINE_STAGES.
334 * 3. The maximum number of stages is a controlled parameter - We
335 * don't start from the user defined maximum number of stages
336 * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
337 * will not even allocate this amount of stages if the user
338 * program can't handle the speed). We then implement a feedback
339 * loop which checks if the pipeline is empty, and if it is, we
340 * increase the maximum number of stages as necessary until we
341 * reach the optimum value which just manages to keep the tape
342 * busy with minimum allocated memory or until we reach
343 * IDETAPE_MAX_PIPELINE_STAGES.
345 * Concerning (2):
347 * In pipelined write mode, ide-tape can not return accurate error codes
348 * to the user program since we usually just add the request to the
349 * pipeline without waiting for it to be serviced. In case an error
350 * occurs, I will report it on the next user request.
352 * In the pipelined read mode, subsequent read requests or forward
353 * filemark spacing will perform correctly, as we preserve all blocks
354 * and filemarks which we encountered during our excess read-ahead.
356 * For accurate tape positioning and error reporting, disabling
357 * pipelined mode might be the best option.
359 * You can enable/disable/tune the pipelined operation mode by adjusting
360 * the compile time parameters below.
364 * Possible improvements.
366 * 1. Support for the ATAPI overlap protocol.
368 * In order to maximize bus throughput, we currently use the DSC
369 * overlap method which enables ide.c to service requests from the
370 * other device while the tape is busy executing a command. The
371 * DSC overlap method involves polling the tape's status register
372 * for the DSC bit, and servicing the other device while the tape
373 * isn't ready.
375 * In the current QIC development standard (December 1995),
376 * it is recommended that new tape drives will *in addition*
377 * implement the ATAPI overlap protocol, which is used for the
378 * same purpose - efficient use of the IDE bus, but is interrupt
379 * driven and thus has much less CPU overhead.
381 * ATAPI overlap is likely to be supported in most new ATAPI
382 * devices, including new ATAPI cdroms, and thus provides us
383 * a method by which we can achieve higher throughput when
384 * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
387 #define IDETAPE_VERSION "1.16f"
389 #include <linux/config.h>
390 #include <linux/module.h>
391 #include <linux/types.h>
392 #include <linux/string.h>
393 #include <linux/kernel.h>
394 #include <linux/delay.h>
395 #include <linux/timer.h>
396 #include <linux/mm.h>
397 #include <linux/interrupt.h>
398 #include <linux/major.h>
399 #include <linux/devfs_fs_kernel.h>
400 #include <linux/errno.h>
401 #include <linux/genhd.h>
402 #include <linux/malloc.h>
403 #include <linux/pci.h>
404 #include <linux/ide.h>
406 #include <asm/byteorder.h>
407 #include <asm/irq.h>
408 #include <asm/uaccess.h>
409 #include <asm/io.h>
410 #include <asm/unaligned.h>
411 #include <asm/bitops.h>
414 #define NO_LONGER_REQUIRED (1)
417 * OnStream support
419 #define ONSTREAM_DEBUG (0)
420 #define OS_CONFIG_PARTITION (0xff)
421 #define OS_DATA_PARTITION (0)
422 #define OS_PARTITION_VERSION (1)
425 * partition
427 typedef struct os_partition_s {
428 __u8 partition_num;
429 __u8 par_desc_ver;
430 __u16 wrt_pass_cntr;
431 __u32 first_frame_addr;
432 __u32 last_frame_addr;
433 __u32 eod_frame_addr;
434 } os_partition_t;
437 * DAT entry
439 typedef struct os_dat_entry_s {
440 __u32 blk_sz;
441 __u16 blk_cnt;
442 __u8 flags;
443 __u8 reserved;
444 } os_dat_entry_t;
447 * DAT
449 #define OS_DAT_FLAGS_DATA (0xc)
450 #define OS_DAT_FLAGS_MARK (0x1)
452 typedef struct os_dat_s {
453 __u8 dat_sz;
454 __u8 reserved1;
455 __u8 entry_cnt;
456 __u8 reserved3;
457 os_dat_entry_t dat_list[16];
458 } os_dat_t;
461 * Frame types
463 #define OS_FRAME_TYPE_FILL (0)
464 #define OS_FRAME_TYPE_EOD (1 << 0)
465 #define OS_FRAME_TYPE_MARKER (1 << 1)
466 #define OS_FRAME_TYPE_HEADER (1 << 3)
467 #define OS_FRAME_TYPE_DATA (1 << 7)
470 * AUX
472 typedef struct os_aux_s {
473 __u32 format_id; /* hardware compability AUX is based on */
474 char application_sig[4]; /* driver used to write this media */
475 __u32 hdwr; /* reserved */
476 __u32 update_frame_cntr; /* for configuration frame */
477 __u8 frame_type;
478 __u8 frame_type_reserved;
479 __u8 reserved_18_19[2];
480 os_partition_t partition;
481 __u8 reserved_36_43[8];
482 __u32 frame_seq_num;
483 __u32 logical_blk_num_high;
484 __u32 logical_blk_num;
485 os_dat_t dat;
486 __u8 reserved188_191[4];
487 __u32 filemark_cnt;
488 __u32 phys_fm;
489 __u32 last_mark_addr;
490 __u8 reserved204_223[20];
493 * __u8 app_specific[32];
495 * Linux specific fields:
497 __u32 next_mark_addr; /* when known, points to next marker */
498 __u8 linux_specific[28];
500 __u8 reserved_256_511[256];
501 } os_aux_t;
503 typedef struct os_header_s {
504 char ident_str[8];
505 __u8 major_rev;
506 __u8 minor_rev;
507 __u8 reserved10_15[6];
508 __u8 par_num;
509 __u8 reserved1_3[3];
510 os_partition_t partition;
511 } os_header_t;
514 * OnStream ADRL frame
516 #define OS_FRAME_SIZE (32 * 1024 + 512)
517 #define OS_DATA_SIZE (32 * 1024)
518 #define OS_AUX_SIZE (512)
520 #include <linux/mtio.h>
522 /**************************** Tunable parameters *****************************/
526 * Pipelined mode parameters.
528 * We try to use the minimum number of stages which is enough to
529 * keep the tape constantly streaming. To accomplish that, we implement
530 * a feedback loop around the maximum number of stages:
532 * We start from MIN maximum stages (we will not even use MIN stages
533 * if we don't need them), increment it by RATE*(MAX-MIN)
534 * whenever we sense that the pipeline is empty, until we reach
535 * the optimum value or until we reach MAX.
537 * Setting the following parameter to 0 will disable the pipelined mode.
539 #define IDETAPE_MIN_PIPELINE_STAGES 200
540 #define IDETAPE_MAX_PIPELINE_STAGES 400
541 #define IDETAPE_INCREASE_STAGES_RATE 20
544 * The following are used to debug the driver:
546 * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
547 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
548 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
549 * some places.
551 * Setting them to 0 will restore normal operation mode:
553 * 1. Disable logging normal successful operations.
554 * 2. Disable self-sanity checks.
555 * 3. Errors will still be logged, of course.
557 * All the #if DEBUG code will be removed some day, when the driver
558 * is verified to be stable enough. This will make it much more
559 * esthetic.
561 #define IDETAPE_DEBUG_INFO 0
562 #define IDETAPE_DEBUG_LOG 1
563 #define IDETAPE_DEBUG_LOG_VERBOSE 0
564 #define IDETAPE_DEBUG_BUGS 1
567 * After each failed packet command we issue a request sense command
568 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
570 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
572 #define IDETAPE_MAX_PC_RETRIES 3
575 * With each packet command, we allocate a buffer of
576 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
577 * commands (Not for READ/WRITE commands).
579 #define IDETAPE_PC_BUFFER_SIZE 256
582 * In various places in the driver, we need to allocate storage
583 * for packet commands and requests, which will remain valid while
584 * we leave the driver to wait for an interrupt or a timeout event.
586 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
589 * Some tape drives require a long irq timeout
591 #define IDETAPE_WAIT_CMD (60*HZ)
594 * The following parameter is used to select the point in the internal
595 * tape fifo in which we will start to refill the buffer. Decreasing
596 * the following parameter will improve the system's latency and
597 * interactive response, while using a high value might improve sytem
598 * throughput.
600 #define IDETAPE_FIFO_THRESHOLD 2
603 * DSC polling parameters.
605 * Polling for DSC (a single bit in the status register) is a very
606 * important function in ide-tape. There are two cases in which we
607 * poll for DSC:
609 * 1. Before a read/write packet command, to ensure that we
610 * can transfer data from/to the tape's data buffers, without
611 * causing an actual media access. In case the tape is not
612 * ready yet, we take out our request from the device
613 * request queue, so that ide.c will service requests from
614 * the other device on the same interface meanwhile.
616 * 2. After the successful initialization of a "media access
617 * packet command", which is a command which can take a long
618 * time to complete (it can be several seconds or even an hour).
620 * Again, we postpone our request in the middle to free the bus
621 * for the other device. The polling frequency here should be
622 * lower than the read/write frequency since those media access
623 * commands are slow. We start from a "fast" frequency -
624 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
625 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
626 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
628 * We also set a timeout for the timer, in case something goes wrong.
629 * The timeout should be longer then the maximum execution time of a
630 * tape operation.
634 * DSC timings.
636 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
637 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
638 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
639 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
640 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
641 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
642 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
644 /*************************** End of tunable parameters ***********************/
647 * Debugging/Performance analysis
649 * I/O trace support
651 #define USE_IOTRACE 0
652 #if USE_IOTRACE
653 #include <linux/io_trace.h>
654 #define IO_IDETAPE_FIFO 500
655 #endif
658 * Read/Write error simulation
660 #define SIMULATE_ERRORS 0
663 * For general magnetic tape device compatibility.
665 typedef enum {
666 idetape_direction_none,
667 idetape_direction_read,
668 idetape_direction_write
669 } idetape_chrdev_direction_t;
672 * Our view of a packet command.
674 typedef struct idetape_packet_command_s {
675 u8 c[12]; /* Actual packet bytes */
676 int retries; /* On each retry, we increment retries */
677 int error; /* Error code */
678 int request_transfer; /* Bytes to transfer */
679 int actually_transferred; /* Bytes actually transferred */
680 int buffer_size; /* Size of our data buffer */
681 struct buffer_head *bh;
682 char *b_data;
683 int b_count;
684 byte *buffer; /* Data buffer */
685 byte *current_position; /* Pointer into the above buffer */
686 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
687 byte pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
688 unsigned int flags; /* Status/Action bit flags */
689 } idetape_pc_t;
692 * Packet command flag bits.
694 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
695 #define PC_WAIT_FOR_DSC 1 /* 1 When polling for DSC on a media access command */
696 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
697 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
698 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
699 #define PC_WRITING 5 /* Data direction */
702 * Capabilities and Mechanical Status Page
704 typedef struct {
705 unsigned page_code :6; /* Page code - Should be 0x2a */
706 __u8 reserved0_6 :1;
707 __u8 ps :1; /* parameters saveable */
708 __u8 page_length; /* Page Length - Should be 0x12 */
709 __u8 reserved2, reserved3;
710 unsigned ro :1; /* Read Only Mode */
711 unsigned reserved4_1234 :4;
712 unsigned sprev :1; /* Supports SPACE in the reverse direction */
713 unsigned reserved4_67 :2;
714 unsigned reserved5_012 :3;
715 unsigned efmt :1; /* Supports ERASE command initiated formatting */
716 unsigned reserved5_4 :1;
717 unsigned qfa :1; /* Supports the QFA two partition formats */
718 unsigned reserved5_67 :2;
719 unsigned lock :1; /* Supports locking the volume */
720 unsigned locked :1; /* The volume is locked */
721 unsigned prevent :1; /* The device defaults in the prevent state after power up */
722 unsigned eject :1; /* The device can eject the volume */
723 __u8 disconnect :1; /* The device can break request > ctl */
724 __u8 reserved6_5 :1;
725 unsigned ecc :1; /* Supports error correction */
726 unsigned cmprs :1; /* Supports data compression */
727 unsigned reserved7_0 :1;
728 unsigned blk512 :1; /* Supports 512 bytes block size */
729 unsigned blk1024 :1; /* Supports 1024 bytes block size */
730 unsigned reserved7_3_6 :4;
731 unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
732 /* transfers for slow buffer memory ??? */
733 /* Also 32768 block size in some cases */
734 __u16 max_speed; /* Maximum speed supported in KBps */
735 __u8 reserved10, reserved11;
736 __u16 ctl; /* Continuous Transfer Limit in blocks */
737 __u16 speed; /* Current Speed, in KBps */
738 __u16 buffer_size; /* Buffer Size, in 512 bytes */
739 __u8 reserved18, reserved19;
740 } idetape_capabilities_page_t;
743 * Block Size Page
745 typedef struct {
746 unsigned page_code :6; /* Page code - Should be 0x30 */
747 unsigned reserved1_6 :1;
748 unsigned ps :1;
749 __u8 page_length; /* Page Length - Should be 2 */
750 __u8 reserved2;
751 unsigned play32 :1;
752 unsigned play32_5 :1;
753 unsigned reserved2_23 :2;
754 unsigned record32 :1;
755 unsigned record32_5 :1;
756 unsigned reserved2_6 :1;
757 unsigned one :1;
758 } idetape_block_size_page_t;
761 * A pipeline stage.
763 typedef struct idetape_stage_s {
764 struct request rq; /* The corresponding request */
765 struct buffer_head *bh; /* The data buffers */
766 struct idetape_stage_s *next; /* Pointer to the next stage */
767 os_aux_t *aux; /* OnStream aux ptr */
768 } idetape_stage_t;
771 * REQUEST SENSE packet command result - Data Format.
773 typedef struct {
774 unsigned error_code :7; /* Current of deferred errors */
775 unsigned valid :1; /* The information field conforms to QIC-157C */
776 __u8 reserved1 :8; /* Segment Number - Reserved */
777 unsigned sense_key :4; /* Sense Key */
778 unsigned reserved2_4 :1; /* Reserved */
779 unsigned ili :1; /* Incorrect Length Indicator */
780 unsigned eom :1; /* End Of Medium */
781 unsigned filemark :1; /* Filemark */
782 __u32 information __attribute__ ((packed));
783 __u8 asl; /* Additional sense length (n-7) */
784 __u32 command_specific; /* Additional command specific information */
785 __u8 asc; /* Additional Sense Code */
786 __u8 ascq; /* Additional Sense Code Qualifier */
787 __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
788 unsigned sk_specific1 :7; /* Sense Key Specific */
789 unsigned sksv :1; /* Sense Key Specific information is valid */
790 __u8 sk_specific2; /* Sense Key Specific */
791 __u8 sk_specific3; /* Sense Key Specific */
792 __u8 pad[2]; /* Padding to 20 bytes */
793 } idetape_request_sense_result_t;
797 * Most of our global data which we need to save even as we leave the
798 * driver due to an interrupt or a timer event is stored in a variable
799 * of type idetape_tape_t, defined below.
801 typedef struct {
802 ide_drive_t *drive;
803 devfs_handle_t de_r, de_n;
806 * Since a typical character device operation requires more
807 * than one packet command, we provide here enough memory
808 * for the maximum of interconnected packet commands.
809 * The packet commands are stored in the circular array pc_stack.
810 * pc_stack_index points to the last used entry, and warps around
811 * to the start when we get to the last array entry.
813 * pc points to the current processed packet command.
815 * failed_pc points to the last failed packet command, or contains
816 * NULL if we do not need to retry any packet command. This is
817 * required since an additional packet command is needed before the
818 * retry, to get detailed information on what went wrong.
820 idetape_pc_t *pc; /* Current packet command */
821 idetape_pc_t *failed_pc; /* Last failed packet command */
822 idetape_pc_t pc_stack[IDETAPE_PC_STACK];/* Packet command stack */
823 int pc_stack_index; /* Next free packet command storage space */
824 struct request rq_stack[IDETAPE_PC_STACK];
825 int rq_stack_index; /* We implement a circular array */
828 * DSC polling variables.
830 * While polling for DSC we use postponed_rq to postpone the
831 * current request so that ide.c will be able to service
832 * pending requests on the other device. Note that at most
833 * we will have only one DSC (usually data transfer) request
834 * in the device request queue. Additional requests can be
835 * queued in our internal pipeline, but they will be visible
836 * to ide.c only one at a time.
838 struct request *postponed_rq;
839 unsigned long dsc_polling_start; /* The time in which we started polling for DSC */
840 struct timer_list dsc_timer; /* Timer used to poll for dsc */
841 unsigned long best_dsc_rw_frequency; /* Read/Write dsc polling frequency */
842 unsigned long dsc_polling_frequency; /* The current polling frequency */
843 unsigned long dsc_timeout; /* Maximum waiting time */
846 * Read position information
848 byte partition;
849 unsigned int first_frame_position; /* Current block */
850 unsigned int last_frame_position;
851 unsigned int blocks_in_buffer;
854 * Last error information
856 byte sense_key, asc, ascq;
859 * Character device operation
861 unsigned int minor;
862 char name[4]; /* device name */
863 idetape_chrdev_direction_t chrdev_direction; /* Current character device data transfer direction */
866 * Device information
868 unsigned short tape_block_size; /* Usually 512 or 1024 bytes */
869 int user_bs_factor;
870 idetape_capabilities_page_t capabilities; /* Copy of the tape's Capabilities and Mechanical Page */
873 * Active data transfer request parameters.
875 * At most, there is only one ide-tape originated data transfer
876 * request in the device request queue. This allows ide.c to
877 * easily service requests from the other device when we
878 * postpone our active request. In the pipelined operation
879 * mode, we use our internal pipeline structure to hold
880 * more data requests.
882 * The data buffer size is chosen based on the tape's
883 * recommendation.
885 struct request *active_data_request; /* Pointer to the request which is waiting in the device request queue */
886 int stage_size; /* Data buffer size (chosen based on the tape's recommendation */
887 idetape_stage_t *merge_stage;
888 int merge_stage_size;
889 struct buffer_head *bh;
890 char *b_data;
891 int b_count;
894 * Pipeline parameters.
896 * To accomplish non-pipelined mode, we simply set the following
897 * variables to zero (or NULL, where appropriate).
899 int nr_stages; /* Number of currently used stages */
900 int nr_pending_stages; /* Number of pending stages */
901 int max_stages, min_pipeline, max_pipeline; /* We will not allocate more than this number of stages */
902 idetape_stage_t *first_stage; /* The first stage which will be removed from the pipeline */
903 idetape_stage_t *active_stage; /* The currently active stage */
904 idetape_stage_t *next_stage; /* Will be serviced after the currently active request */
905 idetape_stage_t *last_stage; /* New requests will be added to the pipeline here */
906 idetape_stage_t *cache_stage; /* Optional free stage which we can use */
907 int pages_per_stage;
908 int excess_bh_size; /* Wasted space in each stage */
910 unsigned int flags; /* Status/Action flags */
911 spinlock_t spinlock; /* protects the ide-tape queue */
914 * Measures average tape speed
916 unsigned long avg_time;
917 int avg_size;
918 int avg_speed;
920 idetape_request_sense_result_t sense; /* last sense information */
922 char vendor_id[10];
923 char product_id[18];
924 char firmware_revision[6];
925 int firmware_revision_num;
927 int door_locked; /* the door is currently locked */
930 * OnStream flags
932 int onstream; /* the tape is an OnStream tape */
933 int raw; /* OnStream raw access (32.5KB block size) */
934 int cur_frames; /* current number of frames in internal buffer */
935 int max_frames; /* max number of frames in internal buffer */
936 int logical_blk_num; /* logical block number */
937 __u16 wrt_pass_cntr; /* write pass counter */
938 __u32 update_frame_cntr; /* update frame counter */
939 struct semaphore *sem;
940 int onstream_write_error; /* write error recovery active */
941 int header_ok; /* header frame verified ok */
942 int linux_media; /* reading linux-specifc media */
943 int linux_media_version;
944 char application_sig[5]; /* application signature */
945 int filemark_cnt;
946 int first_mark_addr;
947 int last_mark_addr;
948 int eod_frame_addr;
949 unsigned long cmd_start_time;
950 unsigned long max_cmd_time;
953 * Optimize the number of "buffer filling"
954 * mode sense commands.
956 unsigned long last_buffer_fill; /* last time in which we issued fill cmd */
957 int req_buffer_fill; /* buffer fill command requested */
958 int writes_since_buffer_fill;
959 int reads_since_buffer_fill;
962 * Limit the number of times a request can
963 * be postponed, to avoid an infinite postpone
964 * deadlock.
966 int postpone_cnt; /* request postpone count limit */
969 * Measures number of frames:
971 * 1. written/read to/from the driver pipeline (pipeline_head).
972 * 2. written/read to/from the tape buffers (buffer_head).
973 * 3. written/read by the tape to/from the media (tape_head).
975 int pipeline_head;
976 int buffer_head;
977 int tape_head;
978 int last_tape_head;
981 * Speed control at the tape buffers input/output
983 unsigned long insert_time;
984 int insert_size;
985 int insert_speed;
986 int max_insert_speed;
987 int measure_insert_time;
990 * Measure tape still time, in milliseconds
992 unsigned long tape_still_time_begin;
993 int tape_still_time;
996 * Speed regulation negative feedback loop
998 int speed_control;
999 int pipeline_head_speed, controlled_pipeline_head_speed, uncontrolled_pipeline_head_speed;
1000 int controlled_last_pipeline_head, uncontrolled_last_pipeline_head;
1001 unsigned long uncontrolled_pipeline_head_time, controlled_pipeline_head_time;
1002 int controlled_previous_pipeline_head, uncontrolled_previous_pipeline_head;
1003 unsigned long controlled_previous_head_time, uncontrolled_previous_head_time;
1004 int restart_speed_control_req;
1007 * Debug_level determines amount of debugging output;
1008 * can be changed using /proc/ide/hdx/settings
1009 * 0 : almost no debugging output
1010 * 1 : 0+output errors only
1011 * 2 : 1+output all sensekey/asc
1012 * 3 : 2+follow all chrdev related procedures
1013 * 4 : 3+follow all procedures
1014 * 5 : 4+include pc_stack rq_stack info
1015 * 6 : 5+USE_COUNT updates
1017 int debug_level;
1018 } idetape_tape_t;
1021 * Tape door status
1023 #define DOOR_UNLOCKED 0
1024 #define DOOR_LOCKED 1
1025 #define DOOR_EXPLICITLY_LOCKED 2
1028 * Tape flag bits values.
1030 #define IDETAPE_IGNORE_DSC 0
1031 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
1032 #define IDETAPE_BUSY 2 /* Device already opened */
1033 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
1034 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
1035 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
1036 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
1037 #define IDETAPE_READ_ERROR 7
1038 #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
1041 * Supported ATAPI tape drives packet commands
1043 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
1044 #define IDETAPE_REWIND_CMD 0x01
1045 #define IDETAPE_REQUEST_SENSE_CMD 0x03
1046 #define IDETAPE_READ_CMD 0x08
1047 #define IDETAPE_WRITE_CMD 0x0a
1048 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
1049 #define IDETAPE_SPACE_CMD 0x11
1050 #define IDETAPE_INQUIRY_CMD 0x12
1051 #define IDETAPE_ERASE_CMD 0x19
1052 #define IDETAPE_MODE_SENSE_CMD 0x1a
1053 #define IDETAPE_MODE_SELECT_CMD 0x15
1054 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
1055 #define IDETAPE_PREVENT_CMD 0x1e
1056 #define IDETAPE_LOCATE_CMD 0x2b
1057 #define IDETAPE_READ_POSITION_CMD 0x34
1058 #define IDETAPE_READ_BUFFER_CMD 0x3c
1059 #define IDETAPE_SET_SPEED_CMD 0xbb
1062 * Some defines for the READ BUFFER command
1064 #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
1067 * Some defines for the SPACE command
1069 #define IDETAPE_SPACE_OVER_FILEMARK 1
1070 #define IDETAPE_SPACE_TO_EOD 3
1073 * Some defines for the LOAD UNLOAD command
1075 #define IDETAPE_LU_LOAD_MASK 1
1076 #define IDETAPE_LU_RETENSION_MASK 2
1077 #define IDETAPE_LU_EOT_MASK 4
1080 * Special requests for our block device strategy routine.
1082 * In order to service a character device command, we add special
1083 * requests to the tail of our block device request queue and wait
1084 * for their completion.
1087 #define IDETAPE_FIRST_RQ 90
1090 * IDETAPE_PC_RQ is used to queue a packet command in the request queue.
1092 #define IDETAPE_PC_RQ1 90
1093 #define IDETAPE_PC_RQ2 91
1096 * IDETAPE_READ_RQ and IDETAPE_WRITE_RQ are used by our
1097 * character device interface to request read/write operations from
1098 * our block device interface.
1100 #define IDETAPE_READ_RQ 92
1101 #define IDETAPE_WRITE_RQ 93
1102 #define IDETAPE_ABORTED_WRITE_RQ 94
1103 #define IDETAPE_ABORTED_READ_RQ 95
1104 #define IDETAPE_READ_BUFFER_RQ 96
1106 #define IDETAPE_LAST_RQ 96
1109 * A macro which can be used to check if a we support a given
1110 * request command.
1112 #define IDETAPE_RQ_CMD(cmd) ((cmd >= IDETAPE_FIRST_RQ) && (cmd <= IDETAPE_LAST_RQ))
1115 * Error codes which are returned in rq->errors to the higher part
1116 * of the driver.
1118 #define IDETAPE_ERROR_GENERAL 101
1119 #define IDETAPE_ERROR_FILEMARK 102
1120 #define IDETAPE_ERROR_EOD 103
1123 * The ATAPI Status Register.
1125 typedef union {
1126 unsigned all :8;
1127 struct {
1128 unsigned check :1; /* Error occurred */
1129 unsigned idx :1; /* Reserved */
1130 unsigned corr :1; /* Correctable error occurred */
1131 unsigned drq :1; /* Data is request by the device */
1132 unsigned dsc :1; /* Buffer availability / Media access command finished */
1133 unsigned reserved5 :1; /* Reserved */
1134 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
1135 unsigned bsy :1; /* The device has access to the command block */
1136 } b;
1137 } idetape_status_reg_t;
1140 * The ATAPI error register.
1142 typedef union {
1143 unsigned all :8;
1144 struct {
1145 unsigned ili :1; /* Illegal Length Indication */
1146 unsigned eom :1; /* End Of Media Detected */
1147 unsigned abrt :1; /* Aborted command - As defined by ATA */
1148 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
1149 unsigned sense_key :4; /* Sense key of the last failed packet command */
1150 } b;
1151 } idetape_error_reg_t;
1154 * ATAPI Feature Register
1156 typedef union {
1157 unsigned all :8;
1158 struct {
1159 unsigned dma :1; /* Using DMA of PIO */
1160 unsigned reserved321 :3; /* Reserved */
1161 unsigned reserved654 :3; /* Reserved (Tag Type) */
1162 unsigned reserved7 :1; /* Reserved */
1163 } b;
1164 } idetape_feature_reg_t;
1167 * ATAPI Byte Count Register.
1169 typedef union {
1170 unsigned all :16;
1171 struct {
1172 unsigned low :8; /* LSB */
1173 unsigned high :8; /* MSB */
1174 } b;
1175 } idetape_bcount_reg_t;
1178 * ATAPI Interrupt Reason Register.
1180 typedef union {
1181 unsigned all :8;
1182 struct {
1183 unsigned cod :1; /* Information transferred is command (1) or data (0) */
1184 unsigned io :1; /* The device requests us to read (1) or write (0) */
1185 unsigned reserved :6; /* Reserved */
1186 } b;
1187 } idetape_ireason_reg_t;
1190 * ATAPI Drive Select Register
1192 typedef union {
1193 unsigned all :8;
1194 struct {
1195 unsigned sam_lun :4; /* Should be zero with ATAPI (not used) */
1196 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
1197 unsigned one5 :1; /* Should be set to 1 */
1198 unsigned reserved6 :1; /* Reserved */
1199 unsigned one7 :1; /* Should be set to 1 */
1200 } b;
1201 } idetape_drivesel_reg_t;
1204 * ATAPI Device Control Register
1206 typedef union {
1207 unsigned all :8;
1208 struct {
1209 unsigned zero0 :1; /* Should be set to zero */
1210 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
1211 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
1212 unsigned one3 :1; /* Should be set to 1 */
1213 unsigned reserved4567 :4; /* Reserved */
1214 } b;
1215 } idetape_control_reg_t;
1218 * idetape_chrdev_t provides the link between out character device
1219 * interface and our block device interface and the corresponding
1220 * ide_drive_t structure.
1222 typedef struct {
1223 ide_drive_t *drive;
1224 } idetape_chrdev_t;
1227 * The following is used to format the general configuration word of
1228 * the ATAPI IDENTIFY DEVICE command.
1230 struct idetape_id_gcw {
1231 unsigned packet_size :2; /* Packet Size */
1232 unsigned reserved234 :3; /* Reserved */
1233 unsigned drq_type :2; /* Command packet DRQ type */
1234 unsigned removable :1; /* Removable media */
1235 unsigned device_type :5; /* Device type */
1236 unsigned reserved13 :1; /* Reserved */
1237 unsigned protocol :2; /* Protocol type */
1241 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
1243 typedef struct {
1244 unsigned device_type :5; /* Peripheral Device Type */
1245 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
1246 unsigned reserved1_6t0 :7; /* Reserved */
1247 unsigned rmb :1; /* Removable Medium Bit */
1248 unsigned ansi_version :3; /* ANSI Version */
1249 unsigned ecma_version :3; /* ECMA Version */
1250 unsigned iso_version :2; /* ISO Version */
1251 unsigned response_format :4; /* Response Data Format */
1252 unsigned reserved3_45 :2; /* Reserved */
1253 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
1254 unsigned reserved3_7 :1; /* AENC - Reserved */
1255 __u8 additional_length; /* Additional Length (total_length-4) */
1256 __u8 rsv5, rsv6, rsv7; /* Reserved */
1257 __u8 vendor_id[8]; /* Vendor Identification */
1258 __u8 product_id[16]; /* Product Identification */
1259 __u8 revision_level[4]; /* Revision Level */
1260 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
1261 __u8 reserved56t95[40]; /* Reserved - Optional */
1262 /* Additional information may be returned */
1263 } idetape_inquiry_result_t;
1266 * READ POSITION packet command - Data Format (From Table 6-57)
1268 typedef struct {
1269 unsigned reserved0_10 :2; /* Reserved */
1270 unsigned bpu :1; /* Block Position Unknown */
1271 unsigned reserved0_543 :3; /* Reserved */
1272 unsigned eop :1; /* End Of Partition */
1273 unsigned bop :1; /* Beginning Of Partition */
1274 u8 partition; /* Partition Number */
1275 u8 reserved2, reserved3; /* Reserved */
1276 u32 first_block; /* First Block Location */
1277 u32 last_block; /* Last Block Location (Optional) */
1278 u8 reserved12; /* Reserved */
1279 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
1280 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
1281 } idetape_read_position_result_t;
1284 * Follows structures which are related to the SELECT SENSE / MODE SENSE
1285 * packet commands. Those packet commands are still not supported
1286 * by ide-tape.
1288 #define IDETAPE_CAPABILITIES_PAGE 0x2a
1289 #define IDETAPE_BLOCK_SIZE_PAGE 0x30
1292 * Mode Parameter Header for the MODE SENSE packet command
1294 typedef struct {
1295 __u8 mode_data_length; /* Length of the following data transfer */
1296 __u8 medium_type; /* Medium Type */
1297 __u8 dsp; /* Device Specific Parameter */
1298 __u8 bdl; /* Block Descriptor Length */
1299 #if 0
1300 /* data transfer page */
1301 __u8 page_code :6;
1302 __u8 reserved0_6 :1;
1303 __u8 ps :1; /* parameters saveable */
1304 __u8 page_length; /* page Length == 0x02 */
1305 __u8 reserved2;
1306 __u8 read32k :1; /* 32k blk size (data only) */
1307 __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
1308 __u8 reserved3_23 :2;
1309 __u8 write32k :1; /* 32k blk size (data only) */
1310 __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
1311 __u8 reserved3_6 :1;
1312 __u8 streaming :1; /* streaming mode enable */
1313 #endif
1314 } idetape_mode_parameter_header_t;
1317 * Mode Parameter Block Descriptor the MODE SENSE packet command
1319 * Support for block descriptors is optional.
1321 typedef struct {
1322 __u8 density_code; /* Medium density code */
1323 __u8 blocks[3]; /* Number of blocks */
1324 __u8 reserved4; /* Reserved */
1325 __u8 length[3]; /* Block Length */
1326 } idetape_parameter_block_descriptor_t;
1329 * The Data Compression Page, as returned by the MODE SENSE packet command.
1331 typedef struct {
1332 unsigned page_code :6; /* Page Code - Should be 0xf */
1333 unsigned reserved0 :1; /* Reserved */
1334 unsigned ps :1;
1335 __u8 page_length; /* Page Length - Should be 14 */
1336 unsigned reserved2 :6; /* Reserved */
1337 unsigned dcc :1; /* Data Compression Capable */
1338 unsigned dce :1; /* Data Compression Enable */
1339 unsigned reserved3 :5; /* Reserved */
1340 unsigned red :2; /* Report Exception on Decompression */
1341 unsigned dde :1; /* Data Decompression Enable */
1342 __u32 ca; /* Compression Algorithm */
1343 __u32 da; /* Decompression Algorithm */
1344 __u8 reserved[4]; /* Reserved */
1345 } idetape_data_compression_page_t;
1348 * The Medium Partition Page, as returned by the MODE SENSE packet command.
1350 typedef struct {
1351 unsigned page_code :6; /* Page Code - Should be 0x11 */
1352 unsigned reserved1_6 :1; /* Reserved */
1353 unsigned ps :1;
1354 __u8 page_length; /* Page Length - Should be 6 */
1355 __u8 map; /* Maximum Additional Partitions - Should be 0 */
1356 __u8 apd; /* Additional Partitions Defined - Should be 0 */
1357 unsigned reserved4_012 :3; /* Reserved */
1358 unsigned psum :2; /* Should be 0 */
1359 unsigned idp :1; /* Should be 0 */
1360 unsigned sdp :1; /* Should be 0 */
1361 unsigned fdp :1; /* Fixed Data Partitions */
1362 __u8 mfr; /* Medium Format Recognition */
1363 __u8 reserved[2]; /* Reserved */
1364 } idetape_medium_partition_page_t;
1367 * Run time configurable parameters.
1369 typedef struct {
1370 int dsc_rw_frequency;
1371 int dsc_media_access_frequency;
1372 int nr_stages;
1373 } idetape_config_t;
1376 * The variables below are used for the character device interface.
1377 * Additional state variables are defined in our ide_drive_t structure.
1379 static idetape_chrdev_t idetape_chrdevs[MAX_HWIFS * MAX_DRIVES];
1380 static int idetape_chrdev_present = 0;
1382 #if IDETAPE_DEBUG_LOG_VERBOSE
1385 * DO NOT REMOVE, BUILDING A VERBOSE DEBUG SCHEME FOR ATAPI
1388 char *idetape_sense_key_verbose (byte idetape_sense_key)
1390 switch (idetape_sense_key) {
1391 default: {
1392 char buf[22];
1393 sprintf(buf, "IDETAPE_SENSE (0x%02x)", idetape_sense_key);
1394 return(buf);
1400 char *idetape_command_key_verbose (byte idetape_command_key)
1402 switch (idetape_command_key) {
1403 case IDETAPE_TEST_UNIT_READY_CMD: return("TEST_UNIT_READY_CMD");
1404 case IDETAPE_REWIND_CMD: return("REWIND_CMD");
1405 case IDETAPE_REQUEST_SENSE_CMD: return("REQUEST_SENSE_CMD");
1406 case IDETAPE_READ_CMD: return("READ_CMD");
1407 case IDETAPE_WRITE_CMD: return("WRITE_CMD");
1408 case IDETAPE_WRITE_FILEMARK_CMD: return("WRITE_FILEMARK_CMD");
1409 case IDETAPE_SPACE_CMD: return("SPACE_CMD");
1410 case IDETAPE_INQUIRY_CMD: return("INQUIRY_CMD");
1411 case IDETAPE_ERASE_CMD: return("ERASE_CMD")
1412 case IDETAPE_MODE_SENSE_CMD: return("MODE_SENSE_CMD");
1413 case IDETAPE_MODE_SELECT_CMD: return("MODE_SELECT_CMD");
1414 case IDETAPE_LOAD_UNLOAD_CMD: return("LOAD_UNLOAD_CMD");
1415 case IDETAPE_PREVENT_CMD: return("PREVENT_CMD");
1416 case IDETAPE_LOCATE_CMD: return("LOCATE_CMD");
1417 case IDETAPE_READ_POSITION_CMD: return("READ_POSITION_CMD");
1418 case IDETAPE_READ_BUFFER_CMD: return("READ_BUFFER_CMD");
1419 case IDETAPE_SET_SPEED_CMD: return("SET_SPEED_CMD");
1420 default: {
1421 char buf[20];
1422 sprintf(buf, "CMD (0x%02x)", idetape_command_key);
1423 return(buf);
1427 #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
1430 * Too bad. The drive wants to send us data which we are not ready to accept.
1431 * Just throw it away.
1433 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1435 while (bcount--)
1436 IN_BYTE (IDE_DATA_REG);
1439 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1441 struct buffer_head *bh = pc->bh;
1442 int count;
1444 while (bcount) {
1445 #if IDETAPE_DEBUG_BUGS
1446 if (bh == NULL) {
1447 printk (KERN_ERR "ide-tape: bh == NULL in idetape_input_buffers\n");
1448 idetape_discard_data (drive, bcount);
1449 return;
1451 #endif /* IDETAPE_DEBUG_BUGS */
1452 count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), bcount);
1453 atapi_input_bytes (drive, bh->b_data + atomic_read(&bh->b_count), count);
1454 bcount -= count; atomic_add(count, &bh->b_count);
1455 if (atomic_read(&bh->b_count) == bh->b_size) {
1456 bh = bh->b_reqnext;
1457 if (bh)
1458 atomic_set(&bh->b_count, 0);
1461 pc->bh = bh;
1464 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1466 struct buffer_head *bh = pc->bh;
1467 int count;
1469 while (bcount) {
1470 #if IDETAPE_DEBUG_BUGS
1471 if (bh == NULL) {
1472 printk (KERN_ERR "ide-tape: bh == NULL in idetape_output_buffers\n");
1473 return;
1475 #endif /* IDETAPE_DEBUG_BUGS */
1476 count = IDE_MIN (pc->b_count, bcount);
1477 atapi_output_bytes (drive, pc->b_data, count);
1478 bcount -= count; pc->b_data += count; pc->b_count -= count;
1479 if (!pc->b_count) {
1480 pc->bh = bh = bh->b_reqnext;
1481 if (bh) {
1482 pc->b_data = bh->b_data;
1483 pc->b_count = atomic_read(&bh->b_count);
1489 #ifdef CONFIG_BLK_DEV_IDEDMA
1490 static void idetape_update_buffers (idetape_pc_t *pc)
1492 struct buffer_head *bh = pc->bh;
1493 int count, bcount = pc->actually_transferred;
1495 if (test_bit (PC_WRITING, &pc->flags))
1496 return;
1497 while (bcount) {
1498 #if IDETAPE_DEBUG_BUGS
1499 if (bh == NULL) {
1500 printk (KERN_ERR "ide-tape: bh == NULL in idetape_update_buffers\n");
1501 return;
1503 #endif /* IDETAPE_DEBUG_BUGS */
1504 count = IDE_MIN (bh->b_size, bcount);
1505 atomic_set(&bh->b_count, count);
1506 if (atomic_read(&bh->b_count) == bh->b_size)
1507 bh = bh->b_reqnext;
1508 bcount -= count;
1510 pc->bh = bh;
1512 #endif /* CONFIG_BLK_DEV_IDEDMA */
1515 * idetape_next_pc_storage returns a pointer to a place in which we can
1516 * safely store a packet command, even though we intend to leave the
1517 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1518 * commands is allocated at initialization time.
1520 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1522 idetape_tape_t *tape = drive->driver_data;
1524 #if IDETAPE_DEBUG_LOG
1525 if (tape->debug_level >= 5)
1526 printk (KERN_INFO "ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
1527 #endif /* IDETAPE_DEBUG_LOG */
1528 if (tape->pc_stack_index==IDETAPE_PC_STACK)
1529 tape->pc_stack_index=0;
1530 return (&tape->pc_stack[tape->pc_stack_index++]);
1534 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1535 * Since we queue packet commands in the request queue, we need to
1536 * allocate a request, along with the allocation of a packet command.
1539 /**************************************************************
1541 * This should get fixed to use kmalloc(GFP_ATOMIC, ..) *
1542 * followed later on by kfree(). -ml *
1544 **************************************************************/
1546 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1548 idetape_tape_t *tape = drive->driver_data;
1550 #if IDETAPE_DEBUG_LOG
1551 if (tape->debug_level >= 5)
1552 printk (KERN_INFO "ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
1553 #endif /* IDETAPE_DEBUG_LOG */
1554 if (tape->rq_stack_index==IDETAPE_PC_STACK)
1555 tape->rq_stack_index=0;
1556 return (&tape->rq_stack[tape->rq_stack_index++]);
1560 * idetape_init_pc initializes a packet command.
1562 static void idetape_init_pc (idetape_pc_t *pc)
1564 memset (pc->c, 0, 12);
1565 pc->retries = 0;
1566 pc->flags = 0;
1567 pc->request_transfer = 0;
1568 pc->buffer = pc->pc_buffer;
1569 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1570 pc->bh = NULL;
1571 pc->b_data = NULL;
1575 * idetape_analyze_error is called on each failed packet command retry
1576 * to analyze the request sense. We currently do not utilize this
1577 * information.
1579 static void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
1581 idetape_tape_t *tape = drive->driver_data;
1582 idetape_pc_t *pc = tape->failed_pc;
1584 tape->sense = *result;
1585 tape->sense_key = result->sense_key; tape->asc = result->asc; tape->ascq = result->ascq;
1586 #if IDETAPE_DEBUG_LOG
1588 * Without debugging, we only log an error if we decided to
1589 * give up retrying.
1591 if (tape->debug_level >= 1)
1592 printk (KERN_INFO "ide-tape: pc = %x, sense key = %x, asc = %x, ascq = %x\n",pc->c[0],result->sense_key,result->asc,result->ascq);
1593 #if IDETAPE_DEBUG_LOG_VERBOSE
1594 if (tape->debug_level >= 1)
1595 printk (KERN_INFO "ide-tape: pc = %s, sense key = %x, asc = %x, ascq = %x\n",
1596 idetape_command_key_verbose((byte) pc->c[0]),
1597 result->sense_key,
1598 result->asc,
1599 result->ascq);
1600 #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
1601 #endif /* IDETAPE_DEBUG_LOG */
1603 if (tape->onstream && result->sense_key == 2 && result->asc == 0x53 && result->ascq == 2) {
1604 clear_bit(PC_DMA_ERROR, &pc->flags);
1605 ide_stall_queue(drive, HZ / 2);
1606 return;
1608 #ifdef CONFIG_BLK_DEV_IDEDMA
1611 * Correct pc->actually_transferred by asking the tape.
1613 if (test_bit (PC_DMA_ERROR, &pc->flags)) {
1614 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl (get_unaligned (&result->information));
1615 idetape_update_buffers (pc);
1617 #endif /* CONFIG_BLK_DEV_IDEDMA */
1618 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1619 pc->error = IDETAPE_ERROR_FILEMARK;
1620 set_bit (PC_ABORT, &pc->flags);
1622 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1623 if (result->eom || (result->sense_key == 0xd && result->asc == 0x0 && result->ascq == 0x2)) {
1624 pc->error = IDETAPE_ERROR_EOD;
1625 set_bit (PC_ABORT, &pc->flags);
1628 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1629 if (result->sense_key == 8) {
1630 pc->error = IDETAPE_ERROR_EOD;
1631 set_bit (PC_ABORT, &pc->flags);
1633 if (!test_bit (PC_ABORT, &pc->flags) && (tape->onstream || pc->actually_transferred))
1634 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1638 static void idetape_abort_pipeline (ide_drive_t *drive)
1640 idetape_tape_t *tape = drive->driver_data;
1641 idetape_stage_t *stage = tape->next_stage;
1643 #if IDETAPE_DEBUG_LOG
1644 if (tape->debug_level >= 4)
1645 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1646 #endif
1647 while (stage) {
1648 if (stage->rq.cmd == IDETAPE_WRITE_RQ)
1649 stage->rq.cmd = IDETAPE_ABORTED_WRITE_RQ;
1650 else if (stage->rq.cmd == IDETAPE_READ_RQ)
1651 stage->rq.cmd = IDETAPE_ABORTED_READ_RQ;
1652 stage = stage->next;
1657 * idetape_active_next_stage will declare the next stage as "active".
1659 static void idetape_active_next_stage (ide_drive_t *drive)
1661 idetape_tape_t *tape = drive->driver_data;
1662 idetape_stage_t *stage=tape->next_stage;
1663 struct request *rq = &stage->rq;
1665 #if IDETAPE_DEBUG_LOG
1666 if (tape->debug_level >= 4)
1667 printk (KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1668 #endif /* IDETAPE_DEBUG_LOG */
1669 #if IDETAPE_DEBUG_BUGS
1670 if (stage == NULL) {
1671 printk (KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1672 return;
1674 #endif /* IDETAPE_DEBUG_BUGS */
1676 rq->buffer = NULL;
1677 rq->bh = stage->bh;
1678 tape->active_data_request=rq;
1679 tape->active_stage=stage;
1680 tape->next_stage=stage->next;
1684 * idetape_increase_max_pipeline_stages is a part of the feedback
1685 * loop which tries to find the optimum number of stages. In the
1686 * feedback loop, we are starting from a minimum maximum number of
1687 * stages, and if we sense that the pipeline is empty, we try to
1688 * increase it, until we reach the user compile time memory limit.
1690 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1692 idetape_tape_t *tape = drive->driver_data;
1693 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1695 #if IDETAPE_DEBUG_LOG
1696 if (tape->debug_level >= 4)
1697 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1698 #endif /* IDETAPE_DEBUG_LOG */
1700 tape->max_stages += increase;
1701 tape->max_stages = IDE_MAX(tape->max_stages, tape->min_pipeline);
1702 tape->max_stages = IDE_MIN(tape->max_stages, tape->max_pipeline);
1706 * idetape_kfree_stage calls kfree to completely free a stage, along with
1707 * its related buffers.
1709 static void __idetape_kfree_stage (idetape_stage_t *stage)
1711 struct buffer_head *prev_bh, *bh = stage->bh;
1712 int size;
1714 while (bh != NULL) {
1715 if (bh->b_data != NULL) {
1716 size = (int) bh->b_size;
1717 while (size > 0) {
1718 free_page ((unsigned long) bh->b_data);
1719 size -= PAGE_SIZE;
1720 bh->b_data += PAGE_SIZE;
1723 prev_bh = bh;
1724 bh = bh->b_reqnext;
1725 kfree (prev_bh);
1727 kfree (stage);
1730 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1732 __idetape_kfree_stage (stage);
1736 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1737 * The caller should avoid race conditions.
1739 static void idetape_remove_stage_head (ide_drive_t *drive)
1741 idetape_tape_t *tape = drive->driver_data;
1742 idetape_stage_t *stage;
1744 #if IDETAPE_DEBUG_LOG
1745 if (tape->debug_level >= 4)
1746 printk (KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1747 #endif /* IDETAPE_DEBUG_LOG */
1748 #if IDETAPE_DEBUG_BUGS
1749 if (tape->first_stage == NULL) {
1750 printk (KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1751 return;
1753 if (tape->active_stage == tape->first_stage) {
1754 printk (KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1755 return;
1757 #endif /* IDETAPE_DEBUG_BUGS */
1758 stage=tape->first_stage;
1759 tape->first_stage=stage->next;
1760 idetape_kfree_stage (tape, stage);
1761 tape->nr_stages--;
1762 if (tape->first_stage == NULL) {
1763 tape->last_stage=NULL;
1764 #if IDETAPE_DEBUG_BUGS
1765 if (tape->next_stage != NULL)
1766 printk (KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1767 if (tape->nr_stages)
1768 printk (KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1769 #endif /* IDETAPE_DEBUG_BUGS */
1774 * idetape_end_request is used to finish servicing a request, and to
1775 * insert a pending pipeline request into the main device queue.
1777 static void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
1779 ide_drive_t *drive = hwgroup->drive;
1780 struct request *rq = hwgroup->rq;
1781 idetape_tape_t *tape = drive->driver_data;
1782 unsigned long flags;
1783 int error;
1784 int remove_stage = 0;
1785 #if ONSTREAM_DEBUG
1786 idetape_stage_t *stage;
1787 os_aux_t *aux;
1788 unsigned char *p;
1789 #endif
1791 #if IDETAPE_DEBUG_LOG
1792 if (tape->debug_level >= 4)
1793 printk (KERN_INFO "ide-tape: Reached idetape_end_request\n");
1794 #endif /* IDETAPE_DEBUG_LOG */
1796 switch (uptodate) {
1797 case 0: error = IDETAPE_ERROR_GENERAL; break;
1798 case 1: error = 0; break;
1799 default: error = uptodate;
1801 rq->errors = error;
1802 if (error)
1803 tape->failed_pc = NULL;
1805 spin_lock_irqsave(&tape->spinlock, flags);
1806 if (tape->active_data_request == rq) { /* The request was a pipelined data transfer request */
1807 tape->active_stage = NULL;
1808 tape->active_data_request = NULL;
1809 tape->nr_pending_stages--;
1810 if (rq->cmd == IDETAPE_WRITE_RQ) {
1811 #if ONSTREAM_DEBUG
1812 if (tape->debug_level >= 2) {
1813 if (tape->onstream) {
1814 stage = tape->first_stage;
1815 aux = stage->aux;
1816 p = stage->bh->b_data;
1817 if (ntohl(aux->logical_blk_num) < 11300 && ntohl(aux->logical_blk_num) > 11100)
1818 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++);
1821 #endif
1822 if (tape->onstream && !tape->raw) {
1823 if (tape->first_frame_position == 0xba4) {
1824 #if ONSTREAM_DEBUG
1825 if (tape->debug_level >= 2)
1826 printk("ide-tape: %s: skipping over config parition..\n", tape->name);
1827 #endif
1828 tape->onstream_write_error = 2;
1829 if (tape->sem)
1830 up(tape->sem);
1833 remove_stage = 1;
1834 if (error) {
1835 set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1836 if (error == IDETAPE_ERROR_EOD)
1837 idetape_abort_pipeline (drive);
1838 if (tape->onstream && !tape->raw && error == IDETAPE_ERROR_GENERAL && tape->sense.sense_key == 3) {
1839 clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1840 printk(KERN_ERR "ide-tape: %s: write error, enabling error recovery\n", tape->name);
1841 tape->onstream_write_error = 1;
1842 remove_stage = 0;
1843 tape->nr_pending_stages++;
1844 tape->next_stage = tape->first_stage;
1845 rq->current_nr_sectors = rq->nr_sectors;
1846 if (tape->sem)
1847 up(tape->sem);
1850 } else if (rq->cmd == IDETAPE_READ_RQ) {
1851 if (error == IDETAPE_ERROR_EOD) {
1852 set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1853 idetape_abort_pipeline(drive);
1856 if (tape->next_stage != NULL && !tape->onstream_write_error) {
1857 idetape_active_next_stage (drive);
1860 * Insert the next request into the request queue.
1862 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
1863 } else if (!error) {
1864 if (!tape->onstream)
1865 idetape_increase_max_pipeline_stages (drive);
1868 ide_end_drive_cmd (drive, 0, 0);
1869 if (remove_stage)
1870 idetape_remove_stage_head (drive);
1871 if (tape->active_data_request == NULL)
1872 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1873 spin_unlock_irqrestore(&tape->spinlock, flags);
1876 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1878 idetape_tape_t *tape = drive->driver_data;
1880 #if IDETAPE_DEBUG_LOG
1881 if (tape->debug_level >= 4)
1882 printk (KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1883 #endif /* IDETAPE_DEBUG_LOG */
1884 if (!tape->pc->error) {
1885 idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
1886 idetape_end_request (1,HWGROUP (drive));
1887 } else {
1888 printk (KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1889 idetape_end_request (0,HWGROUP (drive));
1891 return ide_stopped;
1894 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1896 idetape_init_pc (pc);
1897 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1898 pc->c[4] = 20;
1899 pc->request_transfer = 18;
1900 pc->callback = &idetape_request_sense_callback;
1904 * idetape_queue_pc_head generates a new packet command request in front
1905 * of the request queue, before the current request, so that it will be
1906 * processed immediately, on the next pass through the driver.
1908 * idetape_queue_pc_head is called from the request handling part of
1909 * the driver (the "bottom" part). Safe storage for the request should
1910 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1911 * before calling idetape_queue_pc_head.
1913 * Memory for those requests is pre-allocated at initialization time, and
1914 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1915 * space for the maximum possible number of inter-dependent packet commands.
1917 * The higher level of the driver - The ioctl handler and the character
1918 * device handling functions should queue request to the lower level part
1919 * and wait for their completion using idetape_queue_pc_tail or
1920 * idetape_queue_rw_tail.
1922 static void idetape_queue_pc_head (ide_drive_t *drive,idetape_pc_t *pc,struct request *rq)
1924 ide_init_drive_cmd (rq);
1925 rq->buffer = (char *) pc;
1926 rq->cmd = IDETAPE_PC_RQ1;
1927 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
1931 * idetape_retry_pc is called when an error was detected during the
1932 * last packet command. We queue a request sense packet command in
1933 * the head of the request list.
1935 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1937 idetape_tape_t *tape = drive->driver_data;
1938 idetape_pc_t *pc;
1939 struct request *rq;
1940 idetape_error_reg_t error;
1942 error.all = IN_BYTE (IDE_ERROR_REG);
1943 pc = idetape_next_pc_storage (drive);
1944 rq = idetape_next_rq_storage (drive);
1945 idetape_create_request_sense_cmd (pc);
1946 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
1947 idetape_queue_pc_head (drive, pc, rq);
1948 return ide_stopped;
1952 * idetape_postpone_request postpones the current request so that
1953 * ide.c will be able to service requests from another device on
1954 * the same hwgroup while we are polling for DSC.
1956 static void idetape_postpone_request (ide_drive_t *drive)
1958 idetape_tape_t *tape = drive->driver_data;
1960 #if IDETAPE_DEBUG_LOG
1961 if (tape->debug_level >= 4)
1962 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1963 #endif
1964 tape->postponed_rq = HWGROUP(drive)->rq;
1965 ide_stall_queue(drive, tape->dsc_polling_frequency);
1969 * idetape_pc_intr is the usual interrupt handler which will be called
1970 * during a packet command. We will transfer some of the data (as
1971 * requested by the drive) and will re-point interrupt handler to us.
1972 * When data transfer is finished, we will act according to the
1973 * algorithm described before idetape_issue_packet_command.
1976 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1978 idetape_tape_t *tape = drive->driver_data;
1979 idetape_status_reg_t status;
1980 idetape_bcount_reg_t bcount;
1981 idetape_ireason_reg_t ireason;
1982 idetape_pc_t *pc=tape->pc;
1984 unsigned int temp;
1985 unsigned long cmd_time;
1986 #if SIMULATE_ERRORS
1987 static int error_sim_count = 0;
1988 #endif
1990 #if IDETAPE_DEBUG_LOG
1991 if (tape->debug_level >= 4)
1992 printk (KERN_INFO "ide-tape: Reached idetape_pc_intr interrupt handler\n");
1993 #endif /* IDETAPE_DEBUG_LOG */
1995 status.all = GET_STAT(); /* Clear the interrupt */
1997 #ifdef CONFIG_BLK_DEV_IDEDMA
1998 if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
1999 if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
2001 * A DMA error is sometimes expected. For example,
2002 * if the tape is crossing a filemark during a
2003 * READ command, it will issue an irq and position
2004 * itself before the filemark, so that only a partial
2005 * data transfer will occur (which causes the DMA
2006 * error). In that case, we will later ask the tape
2007 * how much bytes of the original request were
2008 * actually transferred (we can't receive that
2009 * information from the DMA engine on most chipsets).
2011 set_bit (PC_DMA_ERROR, &pc->flags);
2012 } else if (!status.b.check) {
2013 pc->actually_transferred=pc->request_transfer;
2014 idetape_update_buffers (pc);
2016 #if IDETAPE_DEBUG_LOG
2017 if (tape->debug_level >= 4)
2018 printk (KERN_INFO "ide-tape: DMA finished\n");
2019 #endif /* IDETAPE_DEBUG_LOG */
2021 #endif /* CONFIG_BLK_DEV_IDEDMA */
2023 if (!status.b.drq) { /* No more interrupts */
2024 cmd_time = (jiffies - tape->cmd_start_time) * 1000 / HZ;
2025 tape->max_cmd_time = IDE_MAX(cmd_time, tape->max_cmd_time);
2026 #if IDETAPE_DEBUG_LOG
2027 if (tape->debug_level >= 2)
2028 printk (KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
2029 #endif /* IDETAPE_DEBUG_LOG */
2030 clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
2032 ide__sti(); /* local CPU only */
2034 #if SIMULATE_ERRORS
2035 if ((pc->c[0] == IDETAPE_WRITE_CMD || pc->c[0] == IDETAPE_READ_CMD) && (++error_sim_count % 100) == 0) {
2036 printk(KERN_INFO "ide-tape: %s: simulating error\n", tape->name);
2037 status.b.check = 1;
2039 #endif
2040 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
2041 status.b.check = 0;
2042 if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
2043 #if IDETAPE_DEBUG_LOG
2044 if (tape->debug_level >= 1)
2045 printk (KERN_INFO "ide-tape: %s: I/O error, ",tape->name);
2046 #endif /* IDETAPE_DEBUG_LOG */
2047 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2048 printk (KERN_ERR "ide-tape: I/O error in request sense command\n");
2049 return ide_do_reset (drive);
2051 #if IDETAPE_DEBUG_LOG
2052 if (tape->debug_level >= 1)
2053 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
2054 #endif
2055 return idetape_retry_pc (drive); /* Retry operation */
2057 pc->error = 0;
2058 if (!tape->onstream && test_bit (PC_WAIT_FOR_DSC, &pc->flags) && !status.b.dsc) { /* Media access command */
2059 tape->dsc_polling_start = jiffies;
2060 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
2061 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
2062 idetape_postpone_request (drive); /* Allow ide.c to handle other requests */
2063 return ide_stopped;
2065 if (tape->failed_pc == pc)
2066 tape->failed_pc=NULL;
2067 return pc->callback(drive); /* Command finished - Call the callback function */
2069 #ifdef CONFIG_BLK_DEV_IDEDMA
2070 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
2071 printk (KERN_ERR "ide-tape: The tape wants to issue more interrupts in DMA mode\n");
2072 printk (KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
2073 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
2074 return ide_do_reset (drive);
2076 #endif /* CONFIG_BLK_DEV_IDEDMA */
2077 bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
2078 bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
2079 ireason.all=IN_BYTE (IDE_IREASON_REG);
2081 if (ireason.b.cod) {
2082 printk (KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
2083 return ide_do_reset (drive);
2085 if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
2086 printk (KERN_ERR "ide-tape: We wanted to %s, ", ireason.b.io ? "Write":"Read");
2087 printk (KERN_ERR "ide-tape: but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
2088 return ide_do_reset (drive);
2090 if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
2091 temp = pc->actually_transferred + bcount.all;
2092 if ( temp > pc->request_transfer) {
2093 if (temp > pc->buffer_size) {
2094 printk (KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
2095 idetape_discard_data (drive,bcount.all);
2096 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD,NULL);
2097 return ide_started;
2099 #if IDETAPE_DEBUG_LOG
2100 if (tape->debug_level >= 2)
2101 printk (KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
2102 #endif /* IDETAPE_DEBUG_LOG */
2105 if (test_bit (PC_WRITING, &pc->flags)) {
2106 if (pc->bh != NULL)
2107 idetape_output_buffers (drive, pc, bcount.all);
2108 else
2109 atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
2110 } else {
2111 if (pc->bh != NULL)
2112 idetape_input_buffers (drive, pc, bcount.all);
2113 else
2114 atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
2116 pc->actually_transferred+=bcount.all; /* Update the current position */
2117 pc->current_position+=bcount.all;
2118 #if IDETAPE_DEBUG_LOG
2119 if (tape->debug_level >= 2)
2120 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all);
2121 #endif
2122 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD,NULL); /* And set the interrupt handler again */
2123 return ide_started;
2127 * Packet Command Interface
2129 * The current Packet Command is available in tape->pc, and will not
2130 * change until we finish handling it. Each packet command is associated
2131 * with a callback function that will be called when the command is
2132 * finished.
2134 * The handling will be done in three stages:
2136 * 1. idetape_issue_packet_command will send the packet command to the
2137 * drive, and will set the interrupt handler to idetape_pc_intr.
2139 * 2. On each interrupt, idetape_pc_intr will be called. This step
2140 * will be repeated until the device signals us that no more
2141 * interrupts will be issued.
2143 * 3. ATAPI Tape media access commands have immediate status with a
2144 * delayed process. In case of a successful initiation of a
2145 * media access packet command, the DSC bit will be set when the
2146 * actual execution of the command is finished.
2147 * Since the tape drive will not issue an interrupt, we have to
2148 * poll for this event. In this case, we define the request as
2149 * "low priority request" by setting rq_status to
2150 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
2151 * the driver.
2153 * ide.c will then give higher priority to requests which
2154 * originate from the other device, until will change rq_status
2155 * to RQ_ACTIVE.
2157 * 4. When the packet command is finished, it will be checked for errors.
2159 * 5. In case an error was found, we queue a request sense packet command
2160 * in front of the request queue and retry the operation up to
2161 * IDETAPE_MAX_PC_RETRIES times.
2163 * 6. In case no error was found, or we decided to give up and not
2164 * to retry again, the callback function will be called and then
2165 * we will handle the next request.
2168 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2170 idetape_tape_t *tape = drive->driver_data;
2171 idetape_pc_t *pc = tape->pc;
2172 idetape_ireason_reg_t ireason;
2173 int retries = 100;
2174 ide_startstop_t startstop;
2176 if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2177 printk (KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
2178 return startstop;
2180 ireason.all=IN_BYTE (IDE_IREASON_REG);
2181 while (retries-- && (!ireason.b.cod || ireason.b.io)) {
2182 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, retrying\n");
2183 udelay(100);
2184 ireason.all = IN_BYTE(IDE_IREASON_REG);
2185 if (retries == 0) {
2186 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, ignoring\n");
2187 ireason.b.cod = 1;
2188 ireason.b.io = 0;
2191 if (!ireason.b.cod || ireason.b.io) {
2192 printk (KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
2193 return ide_do_reset (drive);
2195 tape->cmd_start_time = jiffies;
2196 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); /* Set the interrupt routine */
2197 atapi_output_bytes (drive,pc->c,12); /* Send the actual packet */
2198 return ide_started;
2201 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
2203 idetape_tape_t *tape = drive->driver_data;
2204 idetape_bcount_reg_t bcount;
2205 int dma_ok=0;
2207 #if IDETAPE_DEBUG_BUGS
2208 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2209 printk (KERN_ERR "ide-tape: possible ide-tape.c bug - Two request sense in serial were issued\n");
2211 #endif /* IDETAPE_DEBUG_BUGS */
2213 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
2214 tape->failed_pc=pc;
2215 tape->pc=pc; /* Set the current packet command */
2217 if (pc->retries > IDETAPE_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
2219 * We will "abort" retrying a packet command in case
2220 * a legitimate error code was received (crossing a
2221 * filemark, or DMA error in the end of media, for
2222 * example).
2224 if (!test_bit (PC_ABORT, &pc->flags)) {
2225 if (!(pc->c[0] == 0 && tape->sense_key == 2 && tape->asc == 4 && (tape->ascq == 1 || tape->ascq == 8))) {
2226 printk (KERN_ERR "ide-tape: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
2227 tape->name, pc->c[0], tape->sense_key, tape->asc, tape->ascq);
2228 if (tape->onstream && pc->c[0] == 8 && tape->sense_key == 3 && tape->asc == 0x11) /* AJN-1: 11 should be 0x11 */
2229 printk(KERN_ERR "ide-tape: %s: enabling read error recovery\n", tape->name);
2231 pc->error = IDETAPE_ERROR_GENERAL; /* Giving up */
2233 tape->failed_pc=NULL;
2234 return pc->callback(drive);
2236 #if IDETAPE_DEBUG_LOG
2237 if (tape->debug_level >= 2)
2238 printk (KERN_INFO "ide-tape: Retry number - %d\n",pc->retries);
2239 #endif /* IDETAPE_DEBUG_LOG */
2241 pc->retries++;
2242 pc->actually_transferred=0; /* We haven't transferred any data yet */
2243 pc->current_position=pc->buffer;
2244 bcount.all=pc->request_transfer; /* Request to transfer the entire buffer at once */
2246 #ifdef CONFIG_BLK_DEV_IDEDMA
2247 if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
2248 printk (KERN_WARNING "ide-tape: DMA disabled, reverting to PIO\n");
2249 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
2251 if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
2252 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
2253 #endif /* CONFIG_BLK_DEV_IDEDMA */
2255 if (IDE_CONTROL_REG)
2256 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
2257 OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG); /* Use PIO/DMA */
2258 OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
2259 OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
2260 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
2261 #ifdef CONFIG_BLK_DEV_IDEDMA
2262 if (dma_ok) { /* Begin DMA, if necessary */
2263 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
2264 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
2266 #endif /* CONFIG_BLK_DEV_IDEDMA */
2267 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
2268 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
2269 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
2270 return ide_started;
2271 } else {
2272 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
2273 return idetape_transfer_pc(drive);
2278 * General packet command callback function.
2280 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2282 idetape_tape_t *tape = drive->driver_data;
2284 #if IDETAPE_DEBUG_LOG
2285 if (tape->debug_level >= 4)
2286 printk (KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2287 #endif /* IDETAPE_DEBUG_LOG */
2289 idetape_end_request (tape->pc->error ? 0:1, HWGROUP(drive));
2290 return ide_stopped;
2294 * A mode sense command is used to "sense" tape parameters.
2296 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, byte page_code)
2298 idetape_init_pc (pc);
2299 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2300 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors for now */
2301 pc->c[2] = page_code;
2302 pc->c[3] = 255; /* Don't limit the returned information */
2303 pc->c[4] = 255; /* (We will just discard data in that case) */
2304 if (page_code == IDETAPE_CAPABILITIES_PAGE)
2305 pc->request_transfer = 24;
2306 else
2307 pc->request_transfer = 50;
2308 pc->callback = &idetape_pc_callback;
2311 static ide_startstop_t idetape_onstream_buffer_fill_callback (ide_drive_t *drive)
2313 idetape_tape_t *tape = drive->driver_data;
2315 tape->max_frames = tape->pc->buffer[4 + 2];
2316 tape->cur_frames = tape->pc->buffer[4 + 3];
2317 if (tape->chrdev_direction == idetape_direction_write)
2318 tape->tape_head = tape->buffer_head - tape->cur_frames;
2319 else
2320 tape->tape_head = tape->buffer_head + tape->cur_frames;
2321 if (tape->tape_head != tape->last_tape_head) {
2322 tape->last_tape_head = tape->tape_head;
2323 tape->tape_still_time_begin = jiffies;
2324 if (tape->tape_still_time > 200)
2325 tape->measure_insert_time = 1;
2327 tape->tape_still_time = (jiffies - tape->tape_still_time_begin) * 1000 / HZ;
2328 #if USE_IOTRACE
2329 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2330 #endif
2331 #if IDETAPE_DEBUG_LOG
2332 if (tape->debug_level >= 1)
2333 printk(KERN_INFO "ide-tape: buffer fill callback, %d/%d\n", tape->cur_frames, tape->max_frames);
2334 #endif
2335 idetape_end_request (tape->pc->error ? 0:1, HWGROUP(drive));
2336 return ide_stopped;
2339 static void idetape_queue_onstream_buffer_fill (ide_drive_t *drive)
2341 idetape_pc_t *pc;
2342 struct request *rq;
2344 pc = idetape_next_pc_storage (drive);
2345 rq = idetape_next_rq_storage (drive);
2346 idetape_create_mode_sense_cmd (pc, 0x33);
2347 pc->callback = idetape_onstream_buffer_fill_callback;
2348 idetape_queue_pc_head (drive, pc, rq);
2351 static void calculate_speeds(ide_drive_t *drive)
2353 idetape_tape_t *tape = drive->driver_data;
2354 int full = 125, empty = 75;
2356 if (jiffies > tape->controlled_pipeline_head_time + 120 * HZ) {
2357 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
2358 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
2359 tape->controlled_last_pipeline_head = tape->pipeline_head;
2360 tape->controlled_pipeline_head_time = jiffies;
2362 if (jiffies > tape->controlled_pipeline_head_time + 60 * HZ)
2363 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
2364 else if (jiffies > tape->controlled_previous_head_time)
2365 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
2367 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) { /* -1 for read mode error recovery */
2368 if (jiffies > tape->uncontrolled_previous_head_time + 10 * HZ) {
2369 tape->uncontrolled_pipeline_head_time = jiffies;
2370 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
2372 } else {
2373 tape->uncontrolled_previous_head_time = jiffies;
2374 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
2375 if (jiffies > tape->uncontrolled_pipeline_head_time + 30 * HZ) {
2376 tape->uncontrolled_pipeline_head_time = jiffies;
2379 tape->pipeline_head_speed = IDE_MAX(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
2380 if (tape->speed_control == 0) {
2381 tape->max_insert_speed = 5000;
2382 } else if (tape->speed_control == 1) {
2383 if (tape->nr_pending_stages >= tape->max_stages / 2)
2384 tape->max_insert_speed = tape->pipeline_head_speed +
2385 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
2386 else
2387 tape->max_insert_speed = 500 +
2388 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
2389 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
2390 tape->max_insert_speed = 5000;
2391 } else if (tape->speed_control == 2) {
2392 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
2393 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
2394 } else
2395 tape->max_insert_speed = tape->speed_control;
2396 tape->max_insert_speed = IDE_MAX(tape->max_insert_speed, 500);
2399 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2401 idetape_tape_t *tape = drive->driver_data;
2402 idetape_pc_t *pc = tape->pc;
2403 idetape_status_reg_t status;
2405 if (tape->onstream)
2406 printk(KERN_INFO "ide-tape: bug: onstream, media_access_finished\n");
2407 status.all = GET_STAT();
2408 if (status.b.dsc) {
2409 if (status.b.check) { /* Error detected */
2410 printk (KERN_ERR "ide-tape: %s: I/O error, ",tape->name);
2411 return idetape_retry_pc (drive); /* Retry operation */
2413 pc->error = 0;
2414 if (tape->failed_pc == pc)
2415 tape->failed_pc = NULL;
2416 } else {
2417 pc->error = IDETAPE_ERROR_GENERAL;
2418 tape->failed_pc = NULL;
2420 return pc->callback (drive);
2423 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2425 idetape_tape_t *tape = drive->driver_data;
2426 struct request *rq = HWGROUP(drive)->rq;
2427 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2429 tape->avg_size += blocks * tape->tape_block_size;
2430 tape->insert_size += blocks * tape->tape_block_size;
2431 if (tape->insert_size > 1024 * 1024)
2432 tape->measure_insert_time = 1;
2433 if (tape->measure_insert_time) {
2434 tape->measure_insert_time = 0;
2435 tape->insert_time = jiffies;
2436 tape->insert_size = 0;
2438 if (jiffies > tape->insert_time)
2439 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2440 if (jiffies - tape->avg_time >= HZ) {
2441 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
2442 tape->avg_size = 0;
2443 tape->avg_time = jiffies;
2446 #if IDETAPE_DEBUG_LOG
2447 if (tape->debug_level >= 4)
2448 printk (KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2449 #endif /* IDETAPE_DEBUG_LOG */
2451 tape->first_frame_position += blocks;
2452 rq->current_nr_sectors -= blocks;
2454 if (!tape->pc->error)
2455 idetape_end_request (1, HWGROUP (drive));
2456 else
2457 idetape_end_request (tape->pc->error, HWGROUP (drive));
2458 return ide_stopped;
2461 static void idetape_create_read_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2463 struct buffer_head *p = bh;
2464 idetape_init_pc (pc);
2465 pc->c[0] = IDETAPE_READ_CMD;
2466 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2467 pc->c[1] = 1;
2468 pc->callback = &idetape_rw_callback;
2469 pc->bh = bh;
2470 atomic_set(&bh->b_count, 0);
2471 pc->buffer = NULL;
2472 if (tape->onstream) {
2473 while (p) {
2474 atomic_set(&p->b_count, 0);
2475 p = p->b_reqnext;
2478 if (!tape->onstream) {
2479 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2480 if (pc->request_transfer == tape->stage_size)
2481 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2482 } else {
2483 if (length) {
2484 pc->request_transfer = pc->buffer_size = 32768 + 512;
2485 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2486 } else
2487 pc->request_transfer = 0;
2491 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2493 int size = 32768;
2495 struct buffer_head *p = bh;
2496 idetape_init_pc (pc);
2497 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
2498 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
2499 pc->c[7] = size >> 8;
2500 pc->c[8] = size & 0xff;
2501 pc->callback = &idetape_pc_callback;
2502 pc->bh = bh;
2503 atomic_set(&bh->b_count, 0);
2504 pc->buffer = NULL;
2505 while (p) {
2506 atomic_set(&p->b_count, 0);
2507 p = p->b_reqnext;
2509 pc->request_transfer = pc->buffer_size = size;
2512 static void idetape_create_write_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2514 struct buffer_head *p = bh;
2515 idetape_init_pc (pc);
2516 pc->c[0] = IDETAPE_WRITE_CMD;
2517 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2518 pc->c[1] = 1;
2519 pc->callback = &idetape_rw_callback;
2520 set_bit (PC_WRITING, &pc->flags);
2521 if (tape->onstream) {
2522 while (p) {
2523 atomic_set(&p->b_count, p->b_size);
2524 p = p->b_reqnext;
2527 pc->bh = bh;
2528 pc->b_data = bh->b_data;
2529 pc->b_count = atomic_read(&bh->b_count);
2530 pc->buffer = NULL;
2531 if (!tape->onstream) {
2532 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2533 if (pc->request_transfer == tape->stage_size)
2534 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2535 } else {
2536 if (length) {
2537 pc->request_transfer = pc->buffer_size = 32768 + 512;
2538 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2539 } else
2540 pc->request_transfer = 0;
2545 * idetape_do_request is our request handling function.
2547 static ide_startstop_t idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2549 idetape_tape_t *tape = drive->driver_data;
2550 idetape_pc_t *pc;
2551 struct request *postponed_rq = tape->postponed_rq;
2552 idetape_status_reg_t status;
2554 #if IDETAPE_DEBUG_LOG
2555 if (tape->debug_level >= 5)
2556 printk (KERN_INFO "ide-tape: rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
2557 if (tape->debug_level >= 2)
2558 printk (KERN_INFO "ide-tape: sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2559 #endif /* IDETAPE_DEBUG_LOG */
2561 if (!IDETAPE_RQ_CMD (rq->cmd)) {
2563 * We do not support buffer cache originated requests.
2565 printk (KERN_NOTICE "ide-tape: %s: Unsupported command in request queue (%d)\n", drive->name, rq->cmd);
2566 ide_end_request (0,HWGROUP (drive)); /* Let the common code handle it */
2567 return ide_stopped;
2571 * Retry a failed packet command
2573 if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2574 return idetape_issue_packet_command (drive, tape->failed_pc);
2576 #if IDETAPE_DEBUG_BUGS
2577 if (postponed_rq != NULL)
2578 if (rq != postponed_rq) {
2579 printk (KERN_ERR "ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
2580 idetape_end_request (0,HWGROUP (drive));
2581 return ide_stopped;
2583 #endif /* IDETAPE_DEBUG_BUGS */
2585 tape->postponed_rq = NULL;
2588 * If the tape is still busy, postpone our request and service
2589 * the other device meanwhile.
2591 status.all = GET_STAT();
2594 * The OnStream tape drive doesn't support DSC. Assume
2595 * that DSC is always set.
2597 if (tape->onstream)
2598 status.b.dsc = 1;
2599 if (!drive->dsc_overlap && rq->cmd != IDETAPE_PC_RQ2)
2600 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
2603 * For the OnStream tape, check the current status of the tape
2604 * internal buffer using data gathered from the buffer fill
2605 * mode page, and postpone our request, effectively "disconnecting"
2606 * from the IDE bus, in case the buffer is full (writing) or
2607 * empty (reading), and there is a danger that our request will
2608 * hold the IDE bus during actual media access.
2610 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2611 tape->measure_insert_time = 1;
2612 if (tape->req_buffer_fill && (rq->cmd == IDETAPE_WRITE_RQ || rq->cmd == IDETAPE_READ_RQ)) {
2613 tape->req_buffer_fill = 0;
2614 tape->writes_since_buffer_fill = 0;
2615 tape->reads_since_buffer_fill = 0;
2616 tape->last_buffer_fill = jiffies;
2617 idetape_queue_onstream_buffer_fill(drive);
2618 if (jiffies > tape->insert_time)
2619 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2620 return ide_stopped;
2622 if (jiffies > tape->insert_time)
2623 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2624 calculate_speeds(drive);
2625 if (tape->onstream && tape->max_frames &&
2626 ((rq->cmd == IDETAPE_WRITE_RQ && (tape->cur_frames == tape->max_frames || (tape->speed_control && tape->cur_frames > 5 && (tape->insert_speed > tape->max_insert_speed || (0 /* tape->cur_frames > 30 && tape->tape_still_time > 200 */))))) ||
2627 (rq->cmd == IDETAPE_READ_RQ && (tape->cur_frames == 0 || (tape->speed_control && (tape->cur_frames < tape->max_frames - 5) && tape->insert_speed > tape->max_insert_speed)) && rq->nr_sectors))) {
2628 #if IDETAPE_DEBUG_LOG
2629 if (tape->debug_level >= 4)
2630 printk(KERN_INFO "ide-tape: postponing request, cmd %d, cur %d, max %d\n",
2631 rq->cmd, tape->cur_frames, tape->max_frames);
2632 #endif
2633 if (tape->postpone_cnt++ < 500) {
2634 status.b.dsc = 0;
2635 tape->req_buffer_fill = 1;
2637 #if ONSTREAM_DEBUG
2638 else if (tape->debug_level >= 4)
2639 printk(KERN_INFO "ide-tape: %s: postpone_cnt %d\n", tape->name, tape->postpone_cnt);
2640 #endif
2642 if (!test_and_clear_bit (IDETAPE_IGNORE_DSC, &tape->flags) && !status.b.dsc) {
2643 if (postponed_rq == NULL) {
2644 tape->dsc_polling_start = jiffies;
2645 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2646 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2647 } else if ((signed long) (jiffies - tape->dsc_timeout) > 0) {
2648 printk (KERN_ERR "ide-tape: %s: DSC timeout\n", tape->name);
2649 if (rq->cmd == IDETAPE_PC_RQ2) {
2650 idetape_media_access_finished (drive);
2651 return ide_stopped;
2652 } else {
2653 return ide_do_reset (drive);
2655 } else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
2656 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2657 idetape_postpone_request (drive);
2658 return ide_stopped;
2660 switch (rq->cmd) {
2661 case IDETAPE_READ_RQ:
2662 tape->buffer_head++;
2663 #if USE_IOTRACE
2664 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2665 #endif
2666 tape->postpone_cnt = 0;
2667 tape->reads_since_buffer_fill++;
2668 if (tape->onstream) {
2669 if (tape->cur_frames - tape->reads_since_buffer_fill <= 0)
2670 tape->req_buffer_fill = 1;
2671 if (jiffies > tape->last_buffer_fill + 5 * HZ / 100)
2672 tape->req_buffer_fill = 1;
2674 pc=idetape_next_pc_storage (drive);
2675 idetape_create_read_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2676 break;
2677 case IDETAPE_WRITE_RQ:
2678 tape->buffer_head++;
2679 #if USE_IOTRACE
2680 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2681 #endif
2682 tape->postpone_cnt = 0;
2683 tape->writes_since_buffer_fill++;
2684 if (tape->onstream) {
2685 if (tape->cur_frames + tape->writes_since_buffer_fill >= tape->max_frames)
2686 tape->req_buffer_fill = 1;
2687 if (jiffies > tape->last_buffer_fill + 5 * HZ / 100)
2688 tape->req_buffer_fill = 1;
2689 calculate_speeds(drive);
2691 pc=idetape_next_pc_storage (drive);
2692 idetape_create_write_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2693 break;
2694 case IDETAPE_READ_BUFFER_RQ:
2695 tape->postpone_cnt = 0;
2696 pc=idetape_next_pc_storage (drive);
2697 idetape_create_read_buffer_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2698 break;
2699 case IDETAPE_ABORTED_WRITE_RQ:
2700 rq->cmd = IDETAPE_WRITE_RQ;
2701 idetape_end_request (IDETAPE_ERROR_EOD, HWGROUP(drive));
2702 return ide_stopped;
2703 case IDETAPE_ABORTED_READ_RQ:
2704 #if IDETAPE_DEBUG_LOG
2705 if (tape->debug_level >= 2)
2706 printk(KERN_INFO "ide-tape: %s: detected aborted read rq\n", tape->name);
2707 #endif
2708 rq->cmd = IDETAPE_READ_RQ;
2709 idetape_end_request (IDETAPE_ERROR_EOD, HWGROUP(drive));
2710 return ide_stopped;
2711 case IDETAPE_PC_RQ1:
2712 pc=(idetape_pc_t *) rq->buffer;
2713 rq->cmd = IDETAPE_PC_RQ2;
2714 break;
2715 case IDETAPE_PC_RQ2:
2716 idetape_media_access_finished (drive);
2717 return ide_stopped;
2718 default:
2719 printk (KERN_ERR "ide-tape: bug in IDETAPE_RQ_CMD macro\n");
2720 idetape_end_request (0,HWGROUP (drive));
2721 return ide_stopped;
2723 return idetape_issue_packet_command (drive, pc);
2727 * Pipeline related functions
2729 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2731 int rc1, rc2;
2733 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2734 rc2 = (tape->active_data_request != NULL);
2735 return rc1;
2739 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2740 * stage, along with all the necessary small buffers which together make
2741 * a buffer of size tape->stage_size (or a bit more). We attempt to
2742 * combine sequential pages as much as possible.
2744 * Returns a pointer to the new allocated stage, or NULL if we
2745 * can't (or don't want to) allocate a stage.
2747 * Pipeline stages are optional and are used to increase performance.
2748 * If we can't allocate them, we'll manage without them.
2750 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2752 idetape_stage_t *stage;
2753 struct buffer_head *prev_bh, *bh;
2754 int pages = tape->pages_per_stage;
2755 char *b_data;
2757 if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2758 return NULL;
2759 stage->next = NULL;
2761 bh = stage->bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL);
2762 if (bh == NULL)
2763 goto abort;
2764 bh->b_reqnext = NULL;
2765 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2766 goto abort;
2767 if (clear)
2768 memset(bh->b_data, 0, PAGE_SIZE);
2769 bh->b_size = PAGE_SIZE;
2770 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2771 set_bit (BH_Lock, &bh->b_state);
2773 while (--pages) {
2774 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2775 goto abort;
2776 if (clear)
2777 memset(b_data, 0, PAGE_SIZE);
2778 if (bh->b_data == b_data + PAGE_SIZE) {
2779 bh->b_size += PAGE_SIZE;
2780 bh->b_data -= PAGE_SIZE;
2781 if (full)
2782 atomic_add(PAGE_SIZE, &bh->b_count);
2783 continue;
2785 if (b_data == bh->b_data + bh->b_size) {
2786 bh->b_size += PAGE_SIZE;
2787 if (full)
2788 atomic_add(PAGE_SIZE, &bh->b_count);
2789 continue;
2791 prev_bh = bh;
2792 if ((bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL)) == NULL) {
2793 free_page ((unsigned long) b_data);
2794 goto abort;
2796 bh->b_reqnext = NULL;
2797 bh->b_data = b_data;
2798 bh->b_size = PAGE_SIZE;
2799 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2800 set_bit (BH_Lock, &bh->b_state);
2801 prev_bh->b_reqnext = bh;
2803 bh->b_size -= tape->excess_bh_size;
2804 if (full)
2805 atomic_sub(tape->excess_bh_size, &bh->b_count);
2806 if (tape->onstream)
2807 stage->aux = (os_aux_t *) (bh->b_data + bh->b_size - OS_AUX_SIZE);
2808 return stage;
2809 abort:
2810 __idetape_kfree_stage (stage);
2811 return NULL;
2814 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2816 idetape_stage_t *cache_stage = tape->cache_stage;
2818 #if IDETAPE_DEBUG_LOG
2819 if (tape->debug_level >= 4)
2820 printk (KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2821 #endif /* IDETAPE_DEBUG_LOG */
2823 if (tape->nr_stages >= tape->max_stages)
2824 return NULL;
2825 if (cache_stage != NULL) {
2826 tape->cache_stage = NULL;
2827 return cache_stage;
2829 return __idetape_kmalloc_stage (tape, 0, 0);
2832 static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char *buf, int n)
2834 struct buffer_head *bh = tape->bh;
2835 int count;
2837 while (n) {
2838 #if IDETAPE_DEBUG_BUGS
2839 if (bh == NULL) {
2840 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_from_user\n");
2841 return;
2843 #endif /* IDETAPE_DEBUG_BUGS */
2844 count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), n);
2845 copy_from_user (bh->b_data + atomic_read(&bh->b_count), buf, count);
2846 n -= count; atomic_add(count, &bh->b_count); buf += count;
2847 if (atomic_read(&bh->b_count) == bh->b_size) {
2848 bh = bh->b_reqnext;
2849 if (bh)
2850 atomic_set(&bh->b_count, 0);
2853 tape->bh = bh;
2856 static void idetape_copy_stage_to_user (idetape_tape_t *tape, char *buf, idetape_stage_t *stage, int n)
2858 struct buffer_head *bh = tape->bh;
2859 int count;
2861 while (n) {
2862 #if IDETAPE_DEBUG_BUGS
2863 if (bh == NULL) {
2864 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_to_user\n");
2865 return;
2867 #endif /* IDETAPE_DEBUG_BUGS */
2868 count = IDE_MIN (tape->b_count, n);
2869 copy_to_user (buf, tape->b_data, count);
2870 n -= count; tape->b_data += count; tape->b_count -= count; buf += count;
2871 if (!tape->b_count) {
2872 tape->bh = bh = bh->b_reqnext;
2873 if (bh) {
2874 tape->b_data = bh->b_data;
2875 tape->b_count = atomic_read(&bh->b_count);
2881 static void idetape_init_merge_stage (idetape_tape_t *tape)
2883 struct buffer_head *bh = tape->merge_stage->bh;
2885 tape->bh = bh;
2886 if (tape->chrdev_direction == idetape_direction_write)
2887 atomic_set(&bh->b_count, 0);
2888 else {
2889 tape->b_data = bh->b_data;
2890 tape->b_count = atomic_read(&bh->b_count);
2894 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2896 struct buffer_head *tmp;
2897 os_aux_t *tmp_aux;
2899 tmp = stage->bh; tmp_aux = stage->aux;
2900 stage->bh = tape->merge_stage->bh; stage->aux = tape->merge_stage->aux;
2901 tape->merge_stage->bh = tmp; tape->merge_stage->aux = tmp_aux;
2902 idetape_init_merge_stage (tape);
2906 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2908 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2910 idetape_tape_t *tape = drive->driver_data;
2911 unsigned long flags;
2913 #if IDETAPE_DEBUG_LOG
2914 if (tape->debug_level >= 4)
2915 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2916 #endif /* IDETAPE_DEBUG_LOG */
2917 spin_lock_irqsave(&tape->spinlock, flags);
2918 stage->next=NULL;
2919 if (tape->last_stage != NULL)
2920 tape->last_stage->next=stage;
2921 else
2922 tape->first_stage=tape->next_stage=stage;
2923 tape->last_stage=stage;
2924 if (tape->next_stage == NULL)
2925 tape->next_stage=tape->last_stage;
2926 tape->nr_stages++;
2927 tape->nr_pending_stages++;
2928 spin_unlock_irqrestore(&tape->spinlock, flags);
2932 * Initialize the OnStream AUX
2934 static void idetape_init_stage (ide_drive_t *drive, idetape_stage_t *stage, int frame_type, int logical_blk_num)
2936 idetape_tape_t *tape = drive->driver_data;
2937 os_aux_t *aux = stage->aux;
2938 os_partition_t *par = &aux->partition;
2939 os_dat_t *dat = &aux->dat;
2941 if (!tape->onstream || tape->raw)
2942 return;
2943 memset(aux, 0, sizeof(*aux));
2944 aux->format_id = htonl(0);
2945 memcpy(aux->application_sig, "LIN3", 4);
2946 aux->hdwr = htonl(0);
2947 aux->frame_type = frame_type;
2949 if (frame_type == OS_FRAME_TYPE_HEADER) {
2950 aux->update_frame_cntr = htonl(tape->update_frame_cntr);
2951 par->partition_num = OS_CONFIG_PARTITION;
2952 par->par_desc_ver = OS_PARTITION_VERSION;
2953 par->wrt_pass_cntr = htons(0xffff);
2954 par->first_frame_addr = htonl(0);
2955 par->last_frame_addr = htonl(0xbb7);
2956 } else {
2957 aux->update_frame_cntr = htonl(0);
2958 par->partition_num = OS_DATA_PARTITION;
2959 par->par_desc_ver = OS_PARTITION_VERSION;
2960 par->wrt_pass_cntr = htons(tape->wrt_pass_cntr);
2961 par->first_frame_addr = htonl(0x14);
2962 par->last_frame_addr = htonl(19239 * 24);
2964 if (frame_type != OS_FRAME_TYPE_HEADER) {
2965 aux->frame_seq_num = htonl(logical_blk_num);
2966 aux->logical_blk_num_high = htonl(0);
2967 aux->logical_blk_num = htonl(logical_blk_num);
2968 } else {
2969 aux->frame_seq_num = htonl(0);
2970 aux->logical_blk_num_high = htonl(0);
2971 aux->logical_blk_num = htonl(0);
2974 if (frame_type != OS_FRAME_TYPE_HEADER) {
2975 dat->dat_sz = 8;
2976 dat->reserved1 = 0;
2977 dat->entry_cnt = 1;
2978 dat->reserved3 = 0;
2979 if (frame_type == OS_FRAME_TYPE_DATA)
2980 dat->dat_list[0].blk_sz = htonl(32 * 1024);
2981 else
2982 dat->dat_list[0].blk_sz = 0;
2983 dat->dat_list[0].blk_cnt = htons(1);
2984 if (frame_type == OS_FRAME_TYPE_MARKER)
2985 dat->dat_list[0].flags = OS_DAT_FLAGS_MARK;
2986 else
2987 dat->dat_list[0].flags = OS_DAT_FLAGS_DATA;
2988 dat->dat_list[0].reserved = 0;
2989 } else
2990 aux->next_mark_addr = htonl(tape->first_mark_addr);
2991 aux->filemark_cnt = ntohl(tape->filemark_cnt);
2992 aux->phys_fm = ntohl(0xffffffff);
2993 aux->last_mark_addr = ntohl(tape->last_mark_addr);
2997 * idetape_wait_for_request installs a semaphore in a pending request
2998 * and sleeps until it is serviced.
3000 * The caller should ensure that the request will not be serviced
3001 * before we install the semaphore (usually by disabling interrupts).
3003 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
3005 DECLARE_MUTEX_LOCKED(sem);
3006 idetape_tape_t *tape = drive->driver_data;
3008 #if IDETAPE_DEBUG_BUGS
3009 if (rq == NULL || !IDETAPE_RQ_CMD (rq->cmd)) {
3010 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
3011 return;
3013 #endif /* IDETAPE_DEBUG_BUGS */
3014 rq->sem = &sem;
3015 tape->sem = &sem;
3016 spin_unlock(&tape->spinlock);
3017 down(&sem);
3018 rq->sem = NULL;
3019 tape->sem = NULL;
3020 spin_lock_irq(&tape->spinlock);
3023 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
3025 idetape_tape_t *tape = drive->driver_data;
3026 idetape_read_position_result_t *result;
3028 #if IDETAPE_DEBUG_LOG
3029 if (tape->debug_level >= 4)
3030 printk (KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
3031 #endif /* IDETAPE_DEBUG_LOG */
3033 if (!tape->pc->error) {
3034 result = (idetape_read_position_result_t *) tape->pc->buffer;
3035 #if IDETAPE_DEBUG_LOG
3036 if (tape->debug_level >= 2)
3037 printk (KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
3038 if (tape->debug_level >= 2)
3039 printk (KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
3040 #endif /* IDETAPE_DEBUG_LOG */
3041 if (result->bpu) {
3042 printk (KERN_INFO "ide-tape: Block location is unknown to the tape\n");
3043 clear_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
3044 idetape_end_request (0,HWGROUP (drive));
3045 } else {
3046 #if IDETAPE_DEBUG_LOG
3047 if (tape->debug_level >= 2)
3048 printk (KERN_INFO "ide-tape: Block Location - %u\n", ntohl (result->first_block));
3049 #endif /* IDETAPE_DEBUG_LOG */
3050 tape->partition = result->partition;
3051 tape->first_frame_position = ntohl (result->first_block);
3052 tape->last_frame_position = ntohl (result->last_block);
3053 tape->blocks_in_buffer = result->blocks_in_buffer[2];
3054 set_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
3055 idetape_end_request (1,HWGROUP (drive));
3057 } else {
3058 idetape_end_request (0,HWGROUP (drive));
3060 return ide_stopped;
3064 * idetape_create_write_filemark_cmd will:
3066 * 1. Write a filemark if write_filemark=1.
3067 * 2. Flush the device buffers without writing a filemark
3068 * if write_filemark=0.
3071 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
3073 idetape_tape_t *tape = drive->driver_data;
3075 idetape_init_pc (pc);
3076 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
3077 if (tape->onstream)
3078 pc->c[1] = 1;
3079 pc->c[4] = write_filemark;
3080 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3081 pc->callback = &idetape_pc_callback;
3084 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
3086 idetape_init_pc(pc);
3087 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
3088 pc->callback = &idetape_pc_callback;
3092 * idetape_queue_pc_tail is based on the following functions:
3094 * ide_do_drive_cmd from ide.c
3095 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
3097 * We add a special packet command request to the tail of the request queue,
3098 * and wait for it to be serviced.
3100 * This is not to be called from within the request handling part
3101 * of the driver ! We allocate here data in the stack, and it is valid
3102 * until the request is finished. This is not the case for the bottom
3103 * part of the driver, where we are always leaving the functions to wait
3104 * for an interrupt or a timer event.
3106 * From the bottom part of the driver, we should allocate safe memory
3107 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
3108 * the request to the request list without waiting for it to be serviced !
3109 * In that case, we usually use idetape_queue_pc_head.
3111 static int __idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
3113 struct request rq;
3115 ide_init_drive_cmd (&rq);
3116 rq.buffer = (char *) pc;
3117 rq.cmd = IDETAPE_PC_RQ1;
3118 return ide_do_drive_cmd (drive, &rq, ide_wait);
3121 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
3123 idetape_tape_t *tape = drive->driver_data;
3125 idetape_init_pc (pc);
3126 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
3127 pc->c[4] = cmd;
3128 if (tape->onstream)
3129 pc->c[1] = 1;
3130 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3131 pc->callback = &idetape_pc_callback;
3134 static int idetape_wait_ready (ide_drive_t *drive, unsigned long long timeout)
3136 idetape_tape_t *tape = drive->driver_data;
3137 idetape_pc_t pc;
3140 * Wait for the tape to become ready
3142 timeout += jiffies;
3143 while (jiffies < timeout) {
3144 idetape_create_test_unit_ready_cmd(&pc);
3145 if (!__idetape_queue_pc_tail(drive, &pc))
3146 return 0;
3147 if (tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) {
3148 idetape_create_load_unload_cmd (drive, &pc, IDETAPE_LU_LOAD_MASK);
3149 __idetape_queue_pc_tail(drive,&pc);
3150 idetape_create_test_unit_ready_cmd(&pc);
3151 if (!__idetape_queue_pc_tail(drive, &pc))
3152 return 0;
3154 if (!(tape->sense_key == 2 && tape->asc == 4 && (tape->ascq == 1 || tape->ascq == 8)))
3155 break;
3156 current->state = TASK_INTERRUPTIBLE;
3157 schedule_timeout(HZ / 10);
3159 return -EIO;
3162 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
3164 idetape_tape_t *tape = drive->driver_data;
3165 int rc;
3167 rc = __idetape_queue_pc_tail(drive, pc);
3168 if (rc) return rc;
3169 if (tape->onstream && test_bit(PC_WAIT_FOR_DSC, &pc->flags))
3170 rc = idetape_wait_ready(drive, 60 * 10 * HZ); /* AJN-4: Changed from 5 to 10 minutes;
3171 because retension takes approx. 8:20 with Onstream 30GB tape */
3172 return rc;
3175 static int idetape_flush_tape_buffers (ide_drive_t *drive)
3177 idetape_pc_t pc;
3178 int rc;
3180 idetape_create_write_filemark_cmd(drive, &pc, 0);
3181 if ((rc = idetape_queue_pc_tail (drive,&pc)))
3182 return rc;
3183 idetape_wait_ready(drive, 60 * 5 * HZ);
3184 return 0;
3187 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
3189 idetape_init_pc (pc);
3190 pc->c[0] = IDETAPE_READ_POSITION_CMD;
3191 pc->request_transfer = 20;
3192 pc->callback = &idetape_read_position_callback;
3195 static int idetape_read_position (ide_drive_t *drive)
3197 idetape_tape_t *tape = drive->driver_data;
3198 idetape_pc_t pc;
3199 int position;
3201 #ifdef NO_LONGER_REQUIRED
3202 idetape_flush_tape_buffers(drive);
3203 #endif
3204 idetape_create_read_position_cmd(&pc);
3205 if (idetape_queue_pc_tail (drive,&pc))
3206 return -1;
3207 position = tape->first_frame_position;
3208 #ifdef NO_LONGER_REQUIRED
3209 if (tape->onstream) {
3210 if ((position != tape->last_frame_position - tape->blocks_in_buffer) &&
3211 (position != tape->last_frame_position + tape->blocks_in_buffer)) {
3212 if (tape->blocks_in_buffer == 0) {
3213 printk("ide-tape: %s: correcting read position %d, %d, %d\n", tape->name, position, tape->last_frame_position, tape->blocks_in_buffer);
3214 position = tape->last_frame_position;
3215 tape->first_frame_position = position;
3219 #endif
3220 return position;
3223 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, byte partition, int skip)
3225 idetape_tape_t *tape = drive->driver_data;
3227 idetape_init_pc (pc);
3228 pc->c[0] = IDETAPE_LOCATE_CMD;
3229 if (tape->onstream)
3230 pc->c[1] = 1;
3231 else
3232 pc->c[1] = 2;
3233 put_unaligned (htonl (block), (unsigned int *) &pc->c[3]);
3234 pc->c[8] = partition;
3235 if (tape->onstream)
3236 pc->c[9] = skip << 7;
3237 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3238 pc->callback = &idetape_pc_callback;
3241 static void idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
3243 idetape_init_pc(pc);
3244 pc->c[0] = IDETAPE_PREVENT_CMD;
3245 pc->c[4] = prevent;
3246 pc->callback = &idetape_pc_callback;
3249 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
3251 idetape_tape_t *tape = drive->driver_data;
3252 unsigned long flags;
3253 int cnt;
3255 if (tape->chrdev_direction != idetape_direction_read)
3256 return 0;
3257 tape->merge_stage_size = 0;
3258 if (tape->merge_stage != NULL) {
3259 __idetape_kfree_stage (tape->merge_stage);
3260 tape->merge_stage = NULL;
3262 tape->chrdev_direction = idetape_direction_none;
3264 if (tape->first_stage == NULL)
3265 return 0;
3267 spin_lock_irqsave(&tape->spinlock, flags);
3268 tape->next_stage = NULL;
3269 if (idetape_pipeline_active (tape))
3270 idetape_wait_for_request(drive, tape->active_data_request);
3271 spin_unlock_irqrestore(&tape->spinlock, flags);
3273 cnt = tape->nr_stages - tape->nr_pending_stages;
3274 while (tape->first_stage != NULL)
3275 idetape_remove_stage_head (drive);
3276 tape->nr_pending_stages = 0;
3277 tape->max_stages = tape->min_pipeline;
3278 return cnt;
3282 * idetape_position_tape positions the tape to the requested block
3283 * using the LOCATE packet command. A READ POSITION command is then
3284 * issued to check where we are positioned.
3286 * Like all higher level operations, we queue the commands at the tail
3287 * of the request queue and wait for their completion.
3290 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, byte partition, int skip)
3292 idetape_tape_t *tape = drive->driver_data;
3293 int retval;
3294 idetape_pc_t pc;
3296 if (tape->chrdev_direction == idetape_direction_read)
3297 __idetape_discard_read_pipeline(drive);
3298 idetape_wait_ready(drive, 60 * 5 * HZ);
3299 idetape_create_locate_cmd (drive, &pc, block, partition, skip);
3300 retval=idetape_queue_pc_tail (drive,&pc);
3301 if (retval) return (retval);
3303 idetape_create_read_position_cmd (&pc);
3304 return (idetape_queue_pc_tail (drive,&pc));
3307 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
3309 idetape_tape_t *tape = drive->driver_data;
3310 int cnt;
3311 int seek, position;
3313 cnt = __idetape_discard_read_pipeline(drive);
3314 if (restore_position) {
3315 position = idetape_read_position(drive);
3316 #if ONSTREAM_DEBUG
3317 if (tape->debug_level >= 2)
3318 printk(KERN_INFO "ide-tape: address %u, nr_stages %d\n", position, cnt);
3319 #endif
3320 seek = position > cnt ? position - cnt : 0;
3321 if (idetape_position_tape(drive, seek, 0, 0)) {
3322 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
3323 return;
3328 static void idetape_update_stats (ide_drive_t *drive)
3330 idetape_pc_t pc;
3332 idetape_create_mode_sense_cmd (&pc, 0x33);
3333 pc.callback = idetape_onstream_buffer_fill_callback;
3334 (void) idetape_queue_pc_tail(drive, &pc);
3338 * idetape_queue_rw_tail generates a read/write request for the block
3339 * device interface and wait for it to be serviced.
3341 static int idetape_queue_rw_tail (ide_drive_t *drive, int cmd, int blocks, struct buffer_head *bh)
3343 idetape_tape_t *tape = drive->driver_data;
3344 struct request rq;
3346 #if IDETAPE_DEBUG_LOG
3347 if (tape->debug_level >= 2)
3348 printk (KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
3349 #endif /* IDETAPE_DEBUG_LOG */
3350 #if IDETAPE_DEBUG_BUGS
3351 if (idetape_pipeline_active (tape)) {
3352 printk (KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3353 return (0);
3355 #endif /* IDETAPE_DEBUG_BUGS */
3357 ide_init_drive_cmd (&rq);
3358 rq.bh = bh;
3359 rq.cmd = cmd;
3360 rq.sector = tape->first_frame_position;
3361 rq.nr_sectors = rq.current_nr_sectors = blocks;
3362 if (tape->onstream)
3363 tape->postpone_cnt = 600;
3364 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
3366 if (cmd != IDETAPE_READ_RQ && cmd != IDETAPE_WRITE_RQ)
3367 return 0;
3369 if (tape->merge_stage)
3370 idetape_init_merge_stage (tape);
3371 if (rq.errors == IDETAPE_ERROR_GENERAL)
3372 return -EIO;
3373 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
3377 * Read back the drive's internal buffer contents, as a part
3378 * of the write error recovery mechanism for old OnStream
3379 * firmware revisions.
3381 static void idetape_onstream_read_back_buffer (ide_drive_t *drive)
3383 idetape_tape_t *tape = drive->driver_data;
3384 int frames, i, logical_blk_num;
3385 idetape_stage_t *stage, *first = NULL, *last = NULL;
3386 os_aux_t *aux;
3387 struct request *rq;
3388 unsigned char *p;
3389 unsigned long flags;
3391 idetape_update_stats(drive);
3392 frames = tape->cur_frames;
3393 logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num) - frames;
3394 printk(KERN_INFO "ide-tape: %s: reading back %d frames from the drive's internal buffer\n", tape->name, frames);
3395 for (i = 0; i < frames; i++) {
3396 stage = __idetape_kmalloc_stage(tape, 0, 0);
3397 if (!first)
3398 first = stage;
3399 aux = stage->aux;
3400 p = stage->bh->b_data;
3401 idetape_queue_rw_tail(drive, IDETAPE_READ_BUFFER_RQ, tape->capabilities.ctl, stage->bh);
3402 #if ONSTREAM_DEBUG
3403 if (tape->debug_level >= 2)
3404 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++);
3405 #endif
3406 rq = &stage->rq;
3407 ide_init_drive_cmd (rq);
3408 rq->cmd = IDETAPE_WRITE_RQ;
3409 rq->sector = tape->first_frame_position;
3410 rq->nr_sectors = rq->current_nr_sectors = tape->capabilities.ctl;
3411 idetape_init_stage(drive, stage, OS_FRAME_TYPE_DATA, logical_blk_num++);
3412 stage->next = NULL;
3413 if (last)
3414 last->next = stage;
3415 last = stage;
3417 if (frames) {
3418 spin_lock_irqsave(&tape->spinlock, flags);
3419 last->next = tape->first_stage;
3420 tape->next_stage = tape->first_stage = first;
3421 tape->nr_stages += frames;
3422 tape->nr_pending_stages += frames;
3423 spin_unlock_irqrestore(&tape->spinlock, flags);
3425 idetape_update_stats(drive);
3426 #if ONSTREAM_DEBUG
3427 if (tape->debug_level >= 2)
3428 printk(KERN_INFO "ide-tape: %s: frames left in buffer: %d\n", tape->name, tape->cur_frames);
3429 #endif
3433 * Error recovery algorithm for the OnStream tape.
3435 static void idetape_onstream_write_error_recovery (ide_drive_t *drive)
3437 idetape_tape_t *tape = drive->driver_data;
3438 unsigned int block;
3440 if (tape->onstream_write_error == 1) {
3441 printk(KERN_ERR "ide-tape: %s: detected physical bad block at %u\n", tape->name, ntohl(tape->sense.information));
3442 block = ntohl(tape->sense.information) + 80;
3443 idetape_update_stats(drive);
3444 printk(KERN_ERR "ide-tape: %s: relocating %d buffered logical blocks to physical block %u\n", tape->name, tape->cur_frames, block);
3445 idetape_update_stats(drive);
3446 if (tape->firmware_revision_num >= 106)
3447 idetape_position_tape(drive, block, 0, 1);
3448 else {
3449 idetape_onstream_read_back_buffer(drive);
3450 idetape_position_tape(drive, block, 0, 0);
3452 idetape_read_position(drive);
3453 #if ONSTREAM_DEBUG
3454 if (tape->debug_level >= 1)
3455 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);
3456 #endif
3457 } else if (tape->onstream_write_error == 2) {
3458 #if ONSTREAM_DEBUG
3459 if (tape->debug_level >= 1)
3460 printk(KERN_INFO "ide-tape: %s: skipping over config partition\n", tape->name);
3461 #endif
3462 idetape_flush_tape_buffers(drive);
3463 block = idetape_read_position(drive);
3464 if (block != 0xba4)
3465 printk(KERN_ERR "ide-tape: warning, current position %d, expected %d\n", block, 0xba4);
3466 idetape_position_tape(drive, 0xbb8, 0, 0);
3468 tape->onstream_write_error = 0;
3472 * idetape_insert_pipeline_into_queue is used to start servicing the
3473 * pipeline stages, starting from tape->next_stage.
3475 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
3477 idetape_tape_t *tape = drive->driver_data;
3479 if (tape->next_stage == NULL)
3480 return;
3481 if (!idetape_pipeline_active (tape)) {
3482 if (tape->onstream_write_error)
3483 idetape_onstream_write_error_recovery(drive);
3484 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
3485 idetape_active_next_stage (drive);
3486 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
3490 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
3492 idetape_init_pc(pc);
3493 pc->c[0] = IDETAPE_INQUIRY_CMD;
3494 pc->c[4] = pc->request_transfer = 254;
3495 pc->callback = &idetape_pc_callback;
3498 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
3500 idetape_tape_t *tape = drive->driver_data;
3502 idetape_init_pc (pc);
3503 pc->c[0] = IDETAPE_REWIND_CMD;
3504 if (tape->onstream)
3505 pc->c[1] = 1;
3506 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3507 pc->callback = &idetape_pc_callback;
3510 static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
3512 idetape_init_pc (pc);
3513 set_bit (PC_WRITING, &pc->flags);
3514 pc->c[0] = IDETAPE_MODE_SELECT_CMD;
3515 pc->c[1] = 0x10;
3516 put_unaligned (htons(length), (unsigned short *) &pc->c[3]);
3517 pc->request_transfer = 255;
3518 pc->callback = &idetape_pc_callback;
3521 static void idetape_create_erase_cmd (idetape_pc_t *pc)
3523 idetape_init_pc (pc);
3524 pc->c[0] = IDETAPE_ERASE_CMD;
3525 pc->c[1] = 1;
3526 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3527 pc->callback = &idetape_pc_callback;
3530 static void idetape_create_space_cmd (idetape_pc_t *pc,int count,byte cmd)
3532 idetape_init_pc (pc);
3533 pc->c[0] = IDETAPE_SPACE_CMD;
3534 put_unaligned (htonl (count), (unsigned int *) &pc->c[1]);
3535 pc->c[1] = cmd;
3536 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3537 pc->callback = &idetape_pc_callback;
3541 * Verify that we have the correct tape frame
3543 static int idetape_verify_stage (ide_drive_t *drive, idetape_stage_t *stage, int logical_blk_num, int quiet)
3545 idetape_tape_t *tape = drive->driver_data;
3546 os_aux_t *aux = stage->aux;
3547 os_partition_t *par = &aux->partition;
3548 struct request *rq = &stage->rq;
3549 struct buffer_head *bh;
3551 if (!tape->onstream)
3552 return 1;
3553 if (tape->raw) {
3554 if (rq->errors) {
3555 bh = stage->bh;
3556 while (bh) {
3557 memset(bh->b_data, 0, bh->b_size);
3558 bh = bh->b_reqnext;
3560 strcpy(stage->bh->b_data, "READ ERROR ON FRAME");
3562 return 1;
3564 if (rq->errors == IDETAPE_ERROR_GENERAL) {
3565 printk(KERN_INFO "ide-tape: %s: skipping frame, read error\n", tape->name);
3566 return 0;
3568 if (rq->errors == IDETAPE_ERROR_EOD) {
3569 printk(KERN_INFO "ide-tape: %s: skipping frame, eod\n", tape->name);
3570 return 0;
3572 if (ntohl(aux->format_id) != 0) {
3573 printk(KERN_INFO "ide-tape: %s: skipping frame, format_id %u\n", tape->name, ntohl(aux->format_id));
3574 return 0;
3576 if (memcmp(aux->application_sig, tape->application_sig, 4) != 0) {
3577 printk(KERN_INFO "ide-tape: %s: skipping frame, incorrect application signature\n", tape->name);
3578 return 0;
3580 if (aux->frame_type != OS_FRAME_TYPE_DATA &&
3581 aux->frame_type != OS_FRAME_TYPE_EOD &&
3582 aux->frame_type != OS_FRAME_TYPE_MARKER) {
3583 printk(KERN_INFO "ide-tape: %s: skipping frame, frame type %x\n", tape->name, aux->frame_type);
3584 return 0;
3586 if (par->partition_num != OS_DATA_PARTITION) {
3587 if (!tape->linux_media || tape->linux_media_version != 2) {
3588 printk(KERN_INFO "ide-tape: %s: skipping frame, partition num %d\n", tape->name, par->partition_num);
3589 return 0;
3592 if (par->par_desc_ver != OS_PARTITION_VERSION) {
3593 printk(KERN_INFO "ide-tape: %s: skipping frame, partition version %d\n", tape->name, par->par_desc_ver);
3594 return 0;
3596 if (ntohs(par->wrt_pass_cntr) != tape->wrt_pass_cntr) {
3597 printk(KERN_INFO "ide-tape: %s: skipping frame, wrt_pass_cntr %d (expected %d)(logical_blk_num %u)\n", tape->name, ntohs(par->wrt_pass_cntr), tape->wrt_pass_cntr, ntohl(aux->logical_blk_num));
3598 return 0;
3600 if (aux->frame_seq_num != aux->logical_blk_num) {
3601 printk(KERN_INFO "ide-tape: %s: skipping frame, seq != logical\n", tape->name);
3602 return 0;
3604 if (logical_blk_num != -1 && ntohl(aux->logical_blk_num) != logical_blk_num) {
3605 if (!quiet)
3606 printk(KERN_INFO "ide-tape: %s: skipping frame, logical_blk_num %u (expected %d)\n", tape->name, ntohl(aux->logical_blk_num), logical_blk_num);
3607 return 0;
3609 if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
3610 rq->errors = IDETAPE_ERROR_FILEMARK;
3611 rq->current_nr_sectors = rq->nr_sectors;
3613 return 1;
3616 static void idetape_wait_first_stage (ide_drive_t *drive)
3618 idetape_tape_t *tape = drive->driver_data;
3619 unsigned long flags;
3621 if (tape->first_stage == NULL)
3622 return;
3623 spin_lock_irqsave(&tape->spinlock, flags);
3624 if (tape->active_stage == tape->first_stage)
3625 idetape_wait_for_request(drive, tape->active_data_request);
3626 spin_unlock_irqrestore(&tape->spinlock, flags);
3630 * idetape_add_chrdev_write_request tries to add a character device
3631 * originated write request to our pipeline. In case we don't succeed,
3632 * we revert to non-pipelined operation mode for this request.
3634 * 1. Try to allocate a new pipeline stage.
3635 * 2. If we can't, wait for more and more requests to be serviced
3636 * and try again each time.
3637 * 3. If we still can't allocate a stage, fallback to
3638 * non-pipelined operation mode for this request.
3640 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3642 idetape_tape_t *tape = drive->driver_data;
3643 idetape_stage_t *new_stage;
3644 unsigned long flags;
3645 struct request *rq;
3647 #if IDETAPE_DEBUG_LOG
3648 if (tape->debug_level >= 3)
3649 printk (KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
3650 #endif /* IDETAPE_DEBUG_LOG */
3653 * Attempt to allocate a new stage.
3654 * Pay special attention to possible race conditions.
3656 while ((new_stage = idetape_kmalloc_stage (tape)) == NULL) {
3657 spin_lock_irqsave(&tape->spinlock, flags);
3658 if (idetape_pipeline_active (tape)) {
3659 idetape_wait_for_request(drive, tape->active_data_request);
3660 spin_unlock_irqrestore(&tape->spinlock, flags);
3661 } else {
3662 spin_unlock_irqrestore(&tape->spinlock, flags);
3663 idetape_insert_pipeline_into_queue (drive);
3664 if (idetape_pipeline_active (tape))
3665 continue;
3667 * Linux is short on memory. Fallback to
3668 * non-pipelined operation mode for this request.
3670 return idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
3673 rq = &new_stage->rq;
3674 ide_init_drive_cmd (rq);
3675 rq->cmd = IDETAPE_WRITE_RQ;
3676 rq->sector = tape->first_frame_position; /* Doesn't actually matter - We always assume sequential access */
3677 rq->nr_sectors = rq->current_nr_sectors = blocks;
3679 idetape_switch_buffers (tape, new_stage);
3680 idetape_init_stage(drive, new_stage, OS_FRAME_TYPE_DATA, tape->logical_blk_num);
3681 tape->logical_blk_num++;
3682 idetape_add_stage_tail (drive,new_stage);
3683 tape->pipeline_head++;
3684 #if USE_IOTRACE
3685 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3686 #endif
3687 calculate_speeds(drive);
3690 * Estimate whether the tape has stopped writing by checking
3691 * if our write pipeline is currently empty. If we are not
3692 * writing anymore, wait for the pipeline to be full enough
3693 * (90%) before starting to service requests, so that we will
3694 * be able to keep up with the higher speeds of the tape.
3696 * For the OnStream drive, we can query the number of pending
3697 * frames in the drive's internal buffer. As long as the tape
3698 * is still writing, it is better to write frames immediately
3699 * rather than gather them in the pipeline. This will give the
3700 * tape's firmware the ability to sense the current incoming
3701 * data rate more accurately, and since the OnStream tape
3702 * supports variable speeds, it can try to adjust itself to the
3703 * incoming data rate.
3705 if (!idetape_pipeline_active(tape)) {
3706 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
3707 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
3708 tape->measure_insert_time = 1;
3709 tape->insert_time = jiffies;
3710 tape->insert_size = 0;
3711 tape->insert_speed = 0;
3712 idetape_insert_pipeline_into_queue (drive);
3713 } else if (tape->onstream) {
3714 idetape_update_stats(drive);
3715 if (tape->cur_frames > 5)
3716 idetape_insert_pipeline_into_queue (drive);
3719 if (test_and_clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags)) /* Return a deferred error */
3720 return -EIO;
3721 return blocks;
3725 * idetape_wait_for_pipeline will wait until all pending pipeline
3726 * requests are serviced. Typically called on device close.
3728 static void idetape_wait_for_pipeline (ide_drive_t *drive)
3730 idetape_tape_t *tape = drive->driver_data;
3731 unsigned long flags;
3733 while (tape->next_stage || idetape_pipeline_active(tape)) {
3734 idetape_insert_pipeline_into_queue (drive);
3735 spin_lock_irqsave(&tape->spinlock, flags);
3736 if (idetape_pipeline_active(tape))
3737 idetape_wait_for_request(drive, tape->active_data_request);
3738 spin_unlock_irqrestore(&tape->spinlock, flags);
3742 static void idetape_empty_write_pipeline (ide_drive_t *drive)
3744 idetape_tape_t *tape = drive->driver_data;
3745 int blocks, i, min;
3746 struct buffer_head *bh;
3748 #if IDETAPE_DEBUG_BUGS
3749 if (tape->chrdev_direction != idetape_direction_write) {
3750 printk (KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3751 return;
3753 if (tape->merge_stage_size > tape->stage_size) {
3754 printk (KERN_ERR "ide-tape: bug: merge_buffer too big\n");
3755 tape->merge_stage_size = tape->stage_size;
3757 #endif /* IDETAPE_DEBUG_BUGS */
3758 if (tape->merge_stage_size) {
3759 blocks=tape->merge_stage_size/tape->tape_block_size;
3760 if (tape->merge_stage_size % tape->tape_block_size) {
3761 blocks++;
3762 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
3763 bh = tape->bh->b_reqnext;
3764 while (bh) {
3765 atomic_set(&bh->b_count, 0);
3766 bh = bh->b_reqnext;
3768 bh = tape->bh;
3769 while (i) {
3770 if (bh == NULL) {
3771 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
3772 break;
3774 min = IDE_MIN(i, bh->b_size - atomic_read(&bh->b_count));
3775 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
3776 atomic_add(min, &bh->b_count);
3777 i -= min;
3778 bh = bh->b_reqnext;
3781 (void) idetape_add_chrdev_write_request (drive, blocks);
3782 tape->merge_stage_size = 0;
3784 idetape_wait_for_pipeline (drive);
3785 if (tape->merge_stage != NULL) {
3786 __idetape_kfree_stage (tape->merge_stage);
3787 tape->merge_stage = NULL;
3789 clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
3790 tape->chrdev_direction=idetape_direction_none;
3793 * On the next backup, perform the feedback loop again.
3794 * (I don't want to keep sense information between backups,
3795 * as some systems are constantly on, and the system load
3796 * can be totally different on the next backup).
3798 tape->max_stages = tape->min_pipeline;
3799 #if IDETAPE_DEBUG_BUGS
3800 if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->nr_stages != 0) {
3801 printk (KERN_ERR "ide-tape: ide-tape pipeline bug, "
3802 "first_stage %p, next_stage %p, last_stage %p, nr_stages %d\n",
3803 tape->first_stage, tape->next_stage, tape->last_stage, tape->nr_stages);
3805 #endif /* IDETAPE_DEBUG_BUGS */
3808 static void idetape_restart_speed_control (ide_drive_t *drive)
3810 idetape_tape_t *tape = drive->driver_data;
3812 tape->restart_speed_control_req = 0;
3813 tape->pipeline_head = 0;
3814 tape->buffer_head = tape->tape_head = tape->cur_frames;
3815 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
3816 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
3817 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
3818 tape->uncontrolled_pipeline_head_speed = 0;
3819 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
3820 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
3823 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
3825 idetape_tape_t *tape = drive->driver_data;
3826 idetape_stage_t *new_stage;
3827 struct request rq;
3828 int bytes_read;
3829 int blocks = tape->capabilities.ctl;
3831 if (tape->chrdev_direction != idetape_direction_read) { /* Initialize read operation */
3832 if (tape->chrdev_direction == idetape_direction_write) {
3833 idetape_empty_write_pipeline (drive);
3834 idetape_flush_tape_buffers (drive);
3836 #if IDETAPE_DEBUG_BUGS
3837 if (tape->merge_stage || tape->merge_stage_size) {
3838 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
3839 tape->merge_stage_size = 0;
3841 #endif /* IDETAPE_DEBUG_BUGS */
3842 if ((tape->merge_stage = __idetape_kmalloc_stage (tape, 0, 0)) == NULL)
3843 return -ENOMEM;
3844 tape->chrdev_direction = idetape_direction_read;
3845 tape->logical_blk_num = 0;
3848 * Issue a read 0 command to ensure that DSC handshake
3849 * is switched from completion mode to buffer available
3850 * mode.
3852 bytes_read = idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 0, tape->merge_stage->bh);
3853 if (bytes_read < 0) {
3854 kfree (tape->merge_stage);
3855 tape->merge_stage = NULL;
3856 tape->chrdev_direction = idetape_direction_none;
3857 return bytes_read;
3860 if (tape->restart_speed_control_req)
3861 idetape_restart_speed_control(drive);
3862 ide_init_drive_cmd (&rq);
3863 rq.cmd = IDETAPE_READ_RQ;
3864 rq.sector = tape->first_frame_position;
3865 rq.nr_sectors = rq.current_nr_sectors = blocks;
3866 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) && tape->nr_stages <= max_stages) {
3867 new_stage=idetape_kmalloc_stage (tape);
3868 while (new_stage != NULL) {
3869 new_stage->rq=rq;
3870 idetape_add_stage_tail (drive,new_stage);
3871 if (tape->nr_stages >= max_stages)
3872 break;
3873 new_stage=idetape_kmalloc_stage (tape);
3876 if (!idetape_pipeline_active(tape)) {
3877 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
3878 tape->measure_insert_time = 1;
3879 tape->insert_time = jiffies;
3880 tape->insert_size = 0;
3881 tape->insert_speed = 0;
3882 idetape_insert_pipeline_into_queue (drive);
3883 } else if (tape->onstream) {
3884 idetape_update_stats(drive);
3885 if (tape->cur_frames < tape->max_frames - 5)
3886 idetape_insert_pipeline_into_queue (drive);
3889 return 0;
3892 static int idetape_get_logical_blk (ide_drive_t *drive, int logical_blk_num, int max_stages, int quiet)
3894 idetape_tape_t *tape = drive->driver_data;
3895 unsigned long flags;
3896 int cnt = 0, x, position;
3899 * Search and wait for the next logical tape block
3901 while (1) {
3902 if (cnt++ > 1000) { /* AJN: was 100 */
3903 printk(KERN_INFO "ide-tape: %s: couldn't find logical block %d, aborting\n", tape->name, logical_blk_num);
3904 return 0;
3906 idetape_initiate_read(drive, max_stages);
3907 if (tape->first_stage == NULL) {
3908 if (tape->onstream) {
3909 #if ONSTREAM_DEBUG
3910 if (tape->debug_level >= 1)
3911 printk(KERN_INFO "ide-tape: %s: first_stage == NULL, pipeline error %ld\n", tape->name, (long)test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags));
3912 #endif
3913 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3914 position = idetape_read_position(drive);
3915 printk(KERN_INFO "ide-tape: %s: blank block detected, positioning tape to block %d\n", tape->name, position + 60);
3916 idetape_position_tape(drive, position + 60, 0, 1);
3917 cnt += 40;
3918 continue;
3919 } else
3920 return 0;
3922 idetape_wait_first_stage(drive);
3923 if (idetape_verify_stage(drive, tape->first_stage, logical_blk_num, quiet))
3924 break;
3925 if (tape->first_stage->rq.errors == IDETAPE_ERROR_EOD)
3926 cnt--;
3927 if (idetape_verify_stage(drive, tape->first_stage, -1, quiet)) {
3928 x = ntohl(tape->first_stage->aux->logical_blk_num);
3929 if (x > logical_blk_num) {
3930 printk(KERN_ERR "ide-tape: %s: couldn't find logical block %d, aborting (block %d found)\n", tape->name, logical_blk_num, x);
3931 return 0;
3934 spin_lock_irqsave(&tape->spinlock, flags);
3935 idetape_remove_stage_head(drive);
3936 spin_unlock_irqrestore(&tape->spinlock, flags);
3938 if (tape->onstream)
3939 tape->logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num);
3940 return 1;
3944 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
3945 * to service a character device read request and add read-ahead
3946 * requests to our pipeline.
3948 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
3950 idetape_tape_t *tape = drive->driver_data;
3951 unsigned long flags;
3952 struct request *rq_ptr;
3953 int bytes_read;
3955 #if IDETAPE_DEBUG_LOG
3956 if (tape->debug_level >= 4)
3957 printk (KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
3958 #endif /* IDETAPE_DEBUG_LOG */
3961 * Wait for the next logical block to be available at the head
3962 * of the pipeline
3964 if (!idetape_get_logical_blk(drive, tape->logical_blk_num, tape->max_stages, 0)) {
3965 if (tape->onstream) {
3966 set_bit(IDETAPE_READ_ERROR, &tape->flags);
3967 return 0;
3969 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3970 return 0;
3971 return idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, blocks, tape->merge_stage->bh);
3973 rq_ptr = &tape->first_stage->rq;
3974 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
3975 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
3978 if (tape->onstream && !tape->raw && tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
3979 #if ONSTREAM_DEBUG
3980 if (tape->debug_level >= 2)
3981 printk(KERN_INFO "ide-tape: %s: EOD reached\n", tape->name);
3982 #endif
3983 return 0;
3985 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
3986 return 0;
3987 else if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3988 set_bit (IDETAPE_FILEMARK, &tape->flags);
3989 else {
3990 idetape_switch_buffers (tape, tape->first_stage);
3991 if (rq_ptr->errors == IDETAPE_ERROR_GENERAL) {
3992 #if ONSTREAM_DEBUG
3993 if (tape->debug_level >= 1)
3994 printk(KERN_INFO "ide-tape: error detected, bytes_read %d\n", bytes_read);
3995 #endif
3997 clear_bit (IDETAPE_FILEMARK, &tape->flags);
3998 spin_lock_irqsave(&tape->spinlock, flags);
3999 idetape_remove_stage_head (drive);
4000 spin_unlock_irqrestore(&tape->spinlock, flags);
4001 tape->logical_blk_num++;
4002 tape->pipeline_head++;
4003 #if USE_IOTRACE
4004 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
4005 #endif
4006 calculate_speeds(drive);
4008 #if IDETAPE_DEBUG_BUGS
4009 if (bytes_read > blocks*tape->tape_block_size) {
4010 printk (KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
4011 bytes_read=blocks*tape->tape_block_size;
4013 #endif /* IDETAPE_DEBUG_BUGS */
4014 return (bytes_read);
4017 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
4019 idetape_tape_t *tape = drive->driver_data;
4020 struct buffer_head *bh;
4021 int count, blocks;
4023 while (bcount) {
4024 bh = tape->merge_stage->bh;
4025 count = IDE_MIN (tape->stage_size, bcount);
4026 bcount -= count;
4027 blocks = count / tape->tape_block_size;
4028 while (count) {
4029 atomic_set(&bh->b_count, IDE_MIN (count, bh->b_size));
4030 memset (bh->b_data, 0, atomic_read(&bh->b_count));
4031 count -= atomic_read(&bh->b_count);
4032 bh = bh->b_reqnext;
4034 idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
4038 static int idetape_pipeline_size (ide_drive_t *drive)
4040 idetape_tape_t *tape = drive->driver_data;
4041 idetape_stage_t *stage;
4042 struct request *rq;
4043 int size = 0;
4045 idetape_wait_for_pipeline (drive);
4046 stage = tape->first_stage;
4047 while (stage != NULL) {
4048 rq = &stage->rq;
4049 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
4050 if (rq->errors == IDETAPE_ERROR_FILEMARK)
4051 size += tape->tape_block_size;
4052 stage = stage->next;
4054 size += tape->merge_stage_size;
4055 return size;
4059 * Rewinds the tape to the Beginning Of the current Partition (BOP).
4061 * We currently support only one partition.
4063 static int idetape_rewind_tape (ide_drive_t *drive)
4065 int retval;
4066 idetape_pc_t pc;
4067 idetape_tape_t *tape = drive->driver_data;
4068 #if IDETAPE_DEBUG_LOG
4069 if (tape->debug_level >= 2)
4070 printk (KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
4071 #endif /* IDETAPE_DEBUG_LOG */
4073 idetape_create_rewind_cmd (drive, &pc);
4074 retval=idetape_queue_pc_tail (drive,&pc);
4075 if (retval) return retval;
4077 idetape_create_read_position_cmd (&pc);
4078 retval = idetape_queue_pc_tail (drive,&pc);
4079 if (retval) return retval;
4080 tape->logical_blk_num = 0;
4081 return 0;
4085 * Our special ide-tape ioctl's.
4087 * Currently there aren't any ioctl's.
4088 * mtio.h compatible commands should be issued to the character device
4089 * interface.
4091 static int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
4092 unsigned int cmd, unsigned long arg)
4094 idetape_tape_t *tape = drive->driver_data;
4095 idetape_config_t config;
4097 #if IDETAPE_DEBUG_LOG
4098 if (tape->debug_level >= 4)
4099 printk (KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
4100 #endif /* IDETAPE_DEBUG_LOG */
4101 switch (cmd) {
4102 case 0x0340:
4103 if (copy_from_user ((char *) &config, (char *) arg, sizeof (idetape_config_t)))
4104 return -EFAULT;
4105 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
4106 tape->max_stages = config.nr_stages;
4107 break;
4108 case 0x0350:
4109 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
4110 config.nr_stages = tape->max_stages;
4111 if (copy_to_user ((char *) arg, (char *) &config, sizeof (idetape_config_t)))
4112 return -EFAULT;
4113 break;
4114 default:
4115 return -EIO;
4117 return 0;
4121 * The block device interface should not be used for data transfers.
4122 * However, we still allow opening it so that we can issue general
4123 * ide driver configuration ioctl's, such as the interrupt unmask feature.
4125 static int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
4127 MOD_INC_USE_COUNT;
4128 #if ONSTREAM_DEBUG
4129 printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_blkdev_open\n");
4130 #endif
4131 return 0;
4134 static void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
4136 MOD_DEC_USE_COUNT;
4137 #if ONSTREAM_DEBUG
4138 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_blkdev_release\n");
4139 #endif
4143 * idetape_pre_reset is called before an ATAPI/ATA software reset.
4145 static void idetape_pre_reset (ide_drive_t *drive)
4147 idetape_tape_t *tape = drive->driver_data;
4148 if (tape != NULL)
4149 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
4153 * Character device interface functions
4155 static ide_drive_t *get_drive_ptr (kdev_t i_rdev)
4157 unsigned int i = MINOR(i_rdev) & ~0xc0;
4159 if (i >= MAX_HWIFS * MAX_DRIVES)
4160 return NULL;
4161 return (idetape_chrdevs[i].drive);
4164 static int idetape_onstream_space_over_filemarks_backward (ide_drive_t *drive,short mt_op,int mt_count)
4166 idetape_tape_t *tape = drive->driver_data;
4167 int cnt = 0;
4168 int last_mark_addr;
4169 unsigned long flags;
4171 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4172 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_bwd\n", tape->name);
4173 return -EIO;
4175 while (cnt != mt_count) {
4176 last_mark_addr = ntohl(tape->first_stage->aux->last_mark_addr);
4177 if (last_mark_addr == -1)
4178 return -EIO;
4179 #if ONSTREAM_DEBUG
4180 if (tape->debug_level >= 2)
4181 printk(KERN_INFO "ide-tape: positioning to last mark at %d\n", last_mark_addr);
4182 #endif
4183 idetape_position_tape(drive, last_mark_addr, 0, 0);
4184 cnt++;
4185 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4186 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks\n", tape->name);
4187 return -EIO;
4189 if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
4190 printk(KERN_INFO "ide-tape: %s: expected to find marker at block %d, not found\n", tape->name, last_mark_addr);
4191 return -EIO;
4194 if (mt_op == MTBSFM) {
4195 spin_lock_irqsave(&tape->spinlock, flags);
4196 idetape_remove_stage_head (drive);
4197 tape->logical_blk_num++;
4198 spin_unlock_irqrestore(&tape->spinlock, flags);
4200 return 0;
4204 * ADRL 1.1 compatible "slow" space filemarks fwd version
4206 * Just scans for the filemark sequentially.
4208 static int idetape_onstream_space_over_filemarks_forward_slow (ide_drive_t *drive,short mt_op,int mt_count)
4210 idetape_tape_t *tape = drive->driver_data;
4211 int cnt = 0;
4212 unsigned long flags;
4214 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4215 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_fwd\n", tape->name);
4216 return -EIO;
4218 while (1) {
4219 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4220 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks\n", tape->name);
4221 return -EIO;
4223 if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_MARKER)
4224 cnt++;
4225 if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
4226 #if ONSTREAM_DEBUG
4227 if (tape->debug_level >= 2)
4228 printk(KERN_INFO "ide-tape: %s: space_fwd: EOD reached\n", tape->name);
4229 #endif
4230 return -EIO;
4232 if (cnt == mt_count)
4233 break;
4234 spin_lock_irqsave(&tape->spinlock, flags);
4235 idetape_remove_stage_head (drive);
4236 spin_unlock_irqrestore(&tape->spinlock, flags);
4238 if (mt_op == MTFSF) {
4239 spin_lock_irqsave(&tape->spinlock, flags);
4240 idetape_remove_stage_head (drive);
4241 tape->logical_blk_num++;
4242 spin_unlock_irqrestore(&tape->spinlock, flags);
4244 return 0;
4249 * Fast linux specific version of OnStream FSF
4251 static int idetape_onstream_space_over_filemarks_forward_fast (ide_drive_t *drive,short mt_op,int mt_count)
4253 idetape_tape_t *tape = drive->driver_data;
4254 int cnt = 0, next_mark_addr;
4255 unsigned long flags;
4257 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4258 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_fwd\n", tape->name);
4259 return -EIO;
4263 * Find nearest (usually previous) marker
4265 while (1) {
4266 if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_MARKER)
4267 break;
4268 if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
4269 #if ONSTREAM_DEBUG
4270 if (tape->debug_level >= 2)
4271 printk(KERN_INFO "ide-tape: %s: space_fwd: EOD reached\n", tape->name);
4272 #endif
4273 return -EIO;
4275 if (ntohl(tape->first_stage->aux->filemark_cnt) == 0) {
4276 if (tape->first_mark_addr == -1) {
4277 printk(KERN_INFO "ide-tape: %s: reverting to slow filemark space\n", tape->name);
4278 return idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count);
4280 idetape_position_tape(drive, tape->first_mark_addr, 0, 0);
4281 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4282 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_fwd_fast\n", tape->name);
4283 return -EIO;
4285 if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
4286 printk(KERN_INFO "ide-tape: %s: expected to find filemark at %d\n", tape->name, tape->first_mark_addr);
4287 return -EIO;
4289 } else {
4290 if (idetape_onstream_space_over_filemarks_backward(drive, MTBSF, 1) < 0)
4291 return -EIO;
4292 mt_count++;
4295 cnt++;
4296 while (cnt != mt_count) {
4297 next_mark_addr = ntohl(tape->first_stage->aux->next_mark_addr);
4298 if (!next_mark_addr || next_mark_addr > tape->eod_frame_addr) {
4299 printk(KERN_INFO "ide-tape: %s: reverting to slow filemark space\n", tape->name);
4300 return idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count - cnt);
4301 #if ONSTREAM_DEBUG
4302 } else if (tape->debug_level >= 2) {
4303 printk(KERN_INFO "ide-tape: positioning to next mark at %d\n", next_mark_addr);
4304 #endif
4306 idetape_position_tape(drive, next_mark_addr, 0, 0);
4307 cnt++;
4308 if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
4309 printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks\n", tape->name);
4310 return -EIO;
4312 if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
4313 printk(KERN_INFO "ide-tape: %s: expected to find marker at block %d, not found\n", tape->name, next_mark_addr);
4314 return -EIO;
4317 if (mt_op == MTFSF) {
4318 spin_lock_irqsave(&tape->spinlock, flags);
4319 idetape_remove_stage_head (drive);
4320 tape->logical_blk_num++;
4321 spin_unlock_irqrestore(&tape->spinlock, flags);
4323 return 0;
4327 * idetape_space_over_filemarks is now a bit more complicated than just
4328 * passing the command to the tape since we may have crossed some
4329 * filemarks during our pipelined read-ahead mode.
4331 * As a minor side effect, the pipeline enables us to support MTFSFM when
4332 * the filemark is in our internal pipeline even if the tape doesn't
4333 * support spacing over filemarks in the reverse direction.
4335 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
4337 idetape_tape_t *tape = drive->driver_data;
4338 idetape_pc_t pc;
4339 unsigned long flags;
4340 int retval,count=0;
4341 int speed_control;
4343 if (tape->onstream) {
4344 if (tape->raw)
4345 return -EIO;
4346 speed_control = tape->speed_control;
4347 tape->speed_control = 0;
4348 if (mt_op == MTFSF || mt_op == MTFSFM) {
4349 if (tape->linux_media)
4350 retval = idetape_onstream_space_over_filemarks_forward_fast(drive, mt_op, mt_count);
4351 else
4352 retval = idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count);
4353 } else
4354 retval = idetape_onstream_space_over_filemarks_backward(drive, mt_op, mt_count);
4355 tape->speed_control = speed_control;
4356 tape->restart_speed_control_req = 1;
4357 return retval;
4360 if (tape->chrdev_direction == idetape_direction_read) {
4362 * We have a read-ahead buffer. Scan it for crossed
4363 * filemarks.
4365 tape->merge_stage_size = 0;
4366 clear_bit (IDETAPE_FILEMARK, &tape->flags);
4367 while (tape->first_stage != NULL) {
4368 idetape_wait_first_stage(drive);
4369 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
4370 count++;
4371 if (count == mt_count) {
4372 switch (mt_op) {
4373 case MTFSF:
4374 spin_lock_irqsave(&tape->spinlock, flags);
4375 idetape_remove_stage_head (drive);
4376 spin_unlock_irqrestore(&tape->spinlock, flags);
4377 case MTFSFM:
4378 return (0);
4379 default:
4380 break;
4383 spin_lock_irqsave(&tape->spinlock, flags);
4384 idetape_remove_stage_head (drive);
4385 spin_unlock_irqrestore(&tape->spinlock, flags);
4387 idetape_discard_read_pipeline (drive, 1);
4391 * The filemark was not found in our internal pipeline.
4392 * Now we can issue the space command.
4394 switch (mt_op) {
4395 case MTFSF:
4396 idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
4397 return (idetape_queue_pc_tail (drive,&pc));
4398 case MTFSFM:
4399 if (!tape->capabilities.sprev)
4400 return (-EIO);
4401 retval = idetape_space_over_filemarks (drive, MTFSF, mt_count-count);
4402 if (retval) return (retval);
4403 return (idetape_space_over_filemarks (drive, MTBSF, 1));
4404 case MTBSF:
4405 if (!tape->capabilities.sprev)
4406 return (-EIO);
4407 idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
4408 return (idetape_queue_pc_tail (drive,&pc));
4409 case MTBSFM:
4410 if (!tape->capabilities.sprev)
4411 return (-EIO);
4412 retval = idetape_space_over_filemarks (drive, MTBSF, mt_count+count);
4413 if (retval) return (retval);
4414 return (idetape_space_over_filemarks (drive, MTFSF, 1));
4415 default:
4416 printk (KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
4417 return (-EIO);
4423 * Our character device read / write functions.
4425 * The tape is optimized to maximize throughput when it is transferring
4426 * an integral number of the "continuous transfer limit", which is
4427 * a parameter of the specific tape (26 KB on my particular tape).
4428 * (32 kB for Onstream)
4430 * As of version 1.3 of the driver, the character device provides an
4431 * abstract continuous view of the media - any mix of block sizes (even 1
4432 * byte) on the same backup/restore procedure is supported. The driver
4433 * will internally convert the requests to the recommended transfer unit,
4434 * so that an unmatch between the user's block size to the recommended
4435 * size will only result in a (slightly) increased driver overhead, but
4436 * will no longer hit performance.
4437 * This is not applicable to Onstream.
4439 static ssize_t idetape_chrdev_read (struct file *file, char *buf,
4440 size_t count, loff_t *ppos)
4442 struct inode *inode = file->f_dentry->d_inode;
4443 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
4444 idetape_tape_t *tape = drive->driver_data;
4445 ssize_t bytes_read,temp,actually_read=0, rc;
4447 if (ppos != &file->f_pos) {
4448 /* "A request was outside the capabilities of the device." */
4449 return -ENXIO;
4451 if (tape->onstream && (count != tape->tape_block_size)) {
4452 printk(KERN_ERR "ide-tape: %s: use %d bytes as block size (%Zd used)\n", tape->name, tape->tape_block_size, count);
4453 return -EINVAL;
4455 #if IDETAPE_DEBUG_LOG
4456 if (tape->debug_level >= 3)
4457 printk (KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
4458 #endif /* IDETAPE_DEBUG_LOG */
4460 if (tape->chrdev_direction != idetape_direction_read) {
4461 if (test_bit (IDETAPE_DETECT_BS, &tape->flags))
4462 if (count > tape->tape_block_size && (count % tape->tape_block_size) == 0)
4463 tape->user_bs_factor = count / tape->tape_block_size;
4465 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
4466 return rc;
4467 if (count==0)
4468 return (0);
4469 if (tape->merge_stage_size) {
4470 actually_read=IDE_MIN (tape->merge_stage_size,count);
4471 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, actually_read);
4472 buf += actually_read; tape->merge_stage_size -= actually_read; count-=actually_read;
4474 while (count >= tape->stage_size) {
4475 bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
4476 if (bytes_read <= 0)
4477 goto finish;
4478 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, bytes_read);
4479 buf += bytes_read; count -= bytes_read; actually_read += bytes_read;
4481 if (count) {
4482 bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
4483 if (bytes_read <= 0)
4484 goto finish;
4485 temp=IDE_MIN (count,bytes_read);
4486 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, temp);
4487 actually_read+=temp;
4488 tape->merge_stage_size=bytes_read-temp;
4490 finish:
4491 if (!actually_read && test_bit (IDETAPE_FILEMARK, &tape->flags)) {
4492 #if IDETAPE_DEBUG_LOG
4493 if (tape->debug_level >= 2)
4494 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
4495 #endif
4496 idetape_space_over_filemarks (drive, MTFSF, 1);
4497 return 0;
4499 if (tape->onstream && !actually_read && test_and_clear_bit(IDETAPE_READ_ERROR, &tape->flags)) {
4500 printk(KERN_ERR "ide-tape: %s: unrecovered read error on logical block number %d, skipping\n", tape->name, tape->logical_blk_num);
4501 tape->logical_blk_num++;
4502 return -EIO;
4504 return actually_read;
4507 static void idetape_update_last_marker (ide_drive_t *drive, int last_mark_addr, int next_mark_addr)
4509 idetape_tape_t *tape = drive->driver_data;
4510 idetape_stage_t *stage;
4511 os_aux_t *aux;
4512 int position;
4514 if (!tape->onstream || tape->raw)
4515 return;
4516 if (last_mark_addr == -1)
4517 return;
4518 stage = __idetape_kmalloc_stage(tape, 0, 0);
4519 if (stage == NULL)
4520 return;
4521 idetape_flush_tape_buffers(drive);
4522 position = idetape_read_position(drive);
4523 #if ONSTREAM_DEBUG
4524 if (tape->debug_level >= 2)
4525 printk(KERN_INFO "ide-tape: current position (2) %d, lblk %d\n", position, tape->logical_blk_num);
4526 if (tape->debug_level >= 2)
4527 printk(KERN_INFO "ide-tape: current position (2) tape block %d\n", tape->last_frame_position);
4528 #endif
4529 idetape_position_tape(drive, last_mark_addr, 0, 0);
4530 if (!idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 1, stage->bh)) {
4531 printk(KERN_INFO "ide-tape: %s: couldn't read last marker\n", tape->name);
4532 __idetape_kfree_stage (stage);
4533 idetape_position_tape(drive, position, 0, 0);
4534 return;
4536 aux = stage->aux;
4537 if (aux->frame_type != OS_FRAME_TYPE_MARKER) {
4538 printk(KERN_INFO "ide-tape: %s: expected to find marker at addr %d\n", tape->name, last_mark_addr);
4539 __idetape_kfree_stage (stage);
4540 idetape_position_tape(drive, position, 0, 0);
4541 return;
4543 #if ONSTREAM_DEBUG
4544 if (tape->debug_level >= 2)
4545 printk(KERN_INFO "ide-tape: writing back marker\n");
4546 #endif
4547 aux->next_mark_addr = htonl(next_mark_addr);
4548 idetape_position_tape(drive, last_mark_addr, 0, 0);
4549 if (!idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
4550 printk(KERN_INFO "ide-tape: %s: couldn't write back marker frame at %d\n", tape->name, last_mark_addr);
4551 __idetape_kfree_stage (stage);
4552 idetape_position_tape(drive, position, 0, 0);
4553 return;
4555 __idetape_kfree_stage (stage);
4556 idetape_flush_tape_buffers (drive);
4557 idetape_position_tape(drive, position, 0, 0);
4558 return;
4561 static void __idetape_write_header (ide_drive_t *drive, int block, int cnt)
4563 idetape_tape_t *tape = drive->driver_data;
4564 idetape_stage_t *stage;
4565 os_header_t header;
4567 stage = __idetape_kmalloc_stage(tape, 1, 1);
4568 if (stage == NULL)
4569 return;
4570 idetape_init_stage(drive, stage, OS_FRAME_TYPE_HEADER, tape->logical_blk_num);
4571 idetape_wait_ready(drive, 60 * 5 * HZ);
4572 idetape_position_tape(drive, block, 0, 0);
4573 memset(&header, 0, sizeof(header));
4574 strcpy(header.ident_str, "ADR_SEQ");
4575 header.major_rev = 1;
4576 header.minor_rev = 2;
4577 header.par_num = 1;
4578 header.partition.partition_num = OS_DATA_PARTITION;
4579 header.partition.par_desc_ver = OS_PARTITION_VERSION;
4580 header.partition.first_frame_addr = htonl(0x14);
4581 header.partition.last_frame_addr = htonl(19239 * 24);
4582 header.partition.wrt_pass_cntr = htons(tape->wrt_pass_cntr);
4583 header.partition.eod_frame_addr = htonl(tape->eod_frame_addr);
4584 memcpy(stage->bh->b_data, &header, sizeof(header));
4585 while (cnt--) {
4586 if (!idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
4587 printk(KERN_INFO "ide-tape: %s: couldn't write header frame\n", tape->name);
4588 __idetape_kfree_stage (stage);
4589 return;
4592 __idetape_kfree_stage (stage);
4593 idetape_flush_tape_buffers (drive);
4596 static void idetape_write_header (ide_drive_t *drive, int locate_eod)
4598 idetape_tape_t *tape = drive->driver_data;
4600 #if ONSTREAM_DEBUG
4601 if (tape->debug_level >= 2)
4602 printk(KERN_INFO "ide-tape: %s: writing tape header\n", tape->name);
4603 #endif
4604 if (!tape->onstream || tape->raw)
4605 return;
4606 tape->update_frame_cntr++;
4607 __idetape_write_header(drive, 5, 5);
4608 __idetape_write_header(drive, 0xbae, 5);
4609 if (locate_eod) {
4610 #if ONSTREAM_DEBUG
4611 if (tape->debug_level >= 2)
4612 printk(KERN_INFO "ide-tape: %s: locating back to eod frame addr %d\n", tape->name, tape->eod_frame_addr);
4613 #endif
4614 idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
4618 static ssize_t idetape_chrdev_write (struct file *file, const char *buf,
4619 size_t count, loff_t *ppos)
4621 struct inode *inode = file->f_dentry->d_inode;
4622 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
4623 idetape_tape_t *tape = drive->driver_data;
4624 ssize_t retval,actually_written=0;
4625 int position;
4627 if (ppos != &file->f_pos) {
4628 /* "A request was outside the capabilities of the device." */
4629 return -ENXIO;
4631 if (tape->onstream && (count != tape->tape_block_size)) {
4632 printk(KERN_ERR "ide-tape: %s: use %d bytes as block size (%Zd used)\n", tape->name, tape->tape_block_size, count);
4633 return -EINVAL;
4635 #if IDETAPE_DEBUG_LOG
4636 if (tape->debug_level >= 3)
4637 printk (KERN_INFO "ide-tape: Reached idetape_chrdev_write, count %Zd\n", count);
4638 #endif /* IDETAPE_DEBUG_LOG */
4640 if (tape->chrdev_direction != idetape_direction_write) { /* Initialize write operation */
4641 if (tape->chrdev_direction == idetape_direction_read)
4642 idetape_discard_read_pipeline (drive, 1);
4643 #if IDETAPE_DEBUG_BUGS
4644 if (tape->merge_stage || tape->merge_stage_size) {
4645 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
4646 tape->merge_stage_size = 0;
4648 #endif /* IDETAPE_DEBUG_BUGS */
4649 if ((tape->merge_stage = __idetape_kmalloc_stage (tape, 0, 0)) == NULL)
4650 return -ENOMEM;
4651 tape->chrdev_direction = idetape_direction_write;
4652 idetape_init_merge_stage (tape);
4654 if (tape->onstream) {
4655 position = idetape_read_position(drive);
4656 if (position <= 20) {
4657 tape->logical_blk_num = 0;
4658 tape->wrt_pass_cntr++;
4659 #if ONSTREAM_DEBUG
4660 if (tape->debug_level >= 2)
4661 printk(KERN_INFO "ide-tape: %s: logical block num 0, setting eod to 20\n", tape->name);
4662 if (tape->debug_level >= 2)
4663 printk(KERN_INFO "ide-tape: %s: allocating new write pass counter %d\n", tape->name, tape->wrt_pass_cntr);
4664 #endif
4665 tape->filemark_cnt = 0;
4666 tape->eod_frame_addr = 20;
4667 tape->first_mark_addr = tape->last_mark_addr = -1;
4668 idetape_write_header(drive, 1);
4670 #if ONSTREAM_DEBUG
4671 if (tape->debug_level >= 2)
4672 printk(KERN_INFO "ide-tape: %s: positioning tape to eod at %d\n", tape->name, tape->eod_frame_addr);
4673 #endif
4674 position = idetape_read_position(drive);
4675 if (position != tape->eod_frame_addr)
4676 idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
4677 #if ONSTREAM_DEBUG
4678 if (tape->debug_level >= 2)
4679 printk(KERN_INFO "ide-tape: %s: first_frame_position %d\n", tape->name, tape->first_frame_position);
4680 #endif
4684 * Issue a write 0 command to ensure that DSC handshake
4685 * is switched from completion mode to buffer available
4686 * mode.
4688 retval = idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 0, tape->merge_stage->bh);
4689 if (retval < 0) {
4690 kfree (tape->merge_stage);
4691 tape->merge_stage = NULL;
4692 tape->chrdev_direction = idetape_direction_none;
4693 return retval;
4695 #if ONSTREAM_DEBUG
4696 if (tape->debug_level >= 2)
4697 printk("ide-tape: first_frame_position %d\n", tape->first_frame_position);
4698 #endif
4700 if (count==0)
4701 return (0);
4702 if (tape->restart_speed_control_req)
4703 idetape_restart_speed_control(drive);
4704 if (tape->merge_stage_size) {
4705 #if IDETAPE_DEBUG_BUGS
4706 if (tape->merge_stage_size >= tape->stage_size) {
4707 printk (KERN_ERR "ide-tape: bug: merge buffer too big\n");
4708 tape->merge_stage_size=0;
4710 #endif /* IDETAPE_DEBUG_BUGS */
4711 actually_written=IDE_MIN (tape->stage_size-tape->merge_stage_size,count);
4712 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, actually_written);
4713 buf+=actually_written;tape->merge_stage_size+=actually_written;count-=actually_written;
4715 if (tape->merge_stage_size == tape->stage_size) {
4716 tape->merge_stage_size = 0;
4717 retval=idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
4718 if (retval <= 0)
4719 return (retval);
4722 while (count >= tape->stage_size) {
4723 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, tape->stage_size);
4724 buf+=tape->stage_size;count-=tape->stage_size;
4725 retval=idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
4726 actually_written+=tape->stage_size;
4727 if (retval <= 0)
4728 return (retval);
4730 if (count) {
4731 actually_written+=count;
4732 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, count);
4733 tape->merge_stage_size+=count;
4735 return (actually_written);
4738 static int idetape_write_filemark (ide_drive_t *drive)
4740 idetape_tape_t *tape = drive->driver_data;
4741 int last_mark_addr;
4742 idetape_pc_t pc;
4744 if (!tape->onstream) {
4745 idetape_create_write_filemark_cmd(drive, &pc,1); /* Write a filemark */
4746 if (idetape_queue_pc_tail (drive,&pc)) {
4747 printk (KERN_ERR "ide-tape: Couldn't write a filemark\n");
4748 return -EIO;
4750 } else if (!tape->raw) {
4751 last_mark_addr = idetape_read_position(drive);
4752 tape->merge_stage = __idetape_kmalloc_stage (tape, 1, 0);
4753 if (tape->merge_stage != NULL) {
4754 idetape_init_stage(drive, tape->merge_stage, OS_FRAME_TYPE_MARKER, tape->logical_blk_num);
4755 idetape_pad_zeros (drive, tape->stage_size);
4756 tape->logical_blk_num++;
4757 __idetape_kfree_stage (tape->merge_stage);
4758 tape->merge_stage = NULL;
4760 if (tape->filemark_cnt)
4761 idetape_update_last_marker(drive, tape->last_mark_addr, last_mark_addr);
4762 tape->last_mark_addr = last_mark_addr;
4763 if (tape->filemark_cnt++ == 0)
4764 tape->first_mark_addr = last_mark_addr;
4766 return 0;
4769 static void idetape_write_eod (ide_drive_t *drive)
4771 idetape_tape_t *tape = drive->driver_data;
4773 if (!tape->onstream || tape->raw)
4774 return;
4775 tape->merge_stage = __idetape_kmalloc_stage (tape, 1, 0);
4776 if (tape->merge_stage != NULL) {
4777 tape->eod_frame_addr = idetape_read_position(drive);
4778 idetape_init_stage(drive, tape->merge_stage, OS_FRAME_TYPE_EOD, tape->logical_blk_num);
4779 idetape_pad_zeros (drive, tape->stage_size);
4780 __idetape_kfree_stage (tape->merge_stage);
4781 tape->merge_stage = NULL;
4783 return;
4786 int idetape_seek_logical_blk (ide_drive_t *drive, int logical_blk_num)
4788 idetape_tape_t *tape = drive->driver_data;
4789 int estimated_address = logical_blk_num + 20;
4790 int retries = 0;
4791 int speed_control;
4793 speed_control = tape->speed_control;
4794 tape->speed_control = 0;
4795 if (logical_blk_num < 0)
4796 logical_blk_num = 0;
4797 if (idetape_get_logical_blk(drive, logical_blk_num, 10, 1))
4798 goto ok;
4799 while (++retries < 10) {
4800 idetape_discard_read_pipeline(drive, 0);
4801 idetape_position_tape(drive, estimated_address, 0, 0);
4802 if (idetape_get_logical_blk(drive, logical_blk_num, 10, 1))
4803 goto ok;
4804 if (!idetape_get_logical_blk(drive, -1, 10, 1))
4805 goto error;
4806 if (tape->logical_blk_num < logical_blk_num)
4807 estimated_address += logical_blk_num - tape->logical_blk_num;
4808 else
4809 break;
4811 error:
4812 tape->speed_control = speed_control;
4813 tape->restart_speed_control_req = 1;
4814 printk(KERN_INFO "ide-tape: %s: couldn't seek to logical block %d (at %d), %d retries\n", tape->name, logical_blk_num, tape->logical_blk_num, retries);
4815 return -EIO;
4817 tape->speed_control = speed_control;
4818 tape->restart_speed_control_req = 1;
4819 return 0;
4823 * idetape_mtioctop is called from idetape_chrdev_ioctl when
4824 * the general mtio MTIOCTOP ioctl is requested.
4826 * We currently support the following mtio.h operations:
4828 * MTFSF - Space over mt_count filemarks in the positive direction.
4829 * The tape is positioned after the last spaced filemark.
4831 * MTFSFM - Same as MTFSF, but the tape is positioned before the
4832 * last filemark.
4834 * MTBSF - Steps background over mt_count filemarks, tape is
4835 * positioned before the last filemark.
4837 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
4839 * Note:
4841 * MTBSF and MTBSFM are not supported when the tape doesn't
4842 * supports spacing over filemarks in the reverse direction.
4843 * In this case, MTFSFM is also usually not supported (it is
4844 * supported in the rare case in which we crossed the filemark
4845 * during our read-ahead pipelined operation mode).
4847 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
4848 * the last written filemark.
4850 * MTREW - Rewinds tape.
4852 * MTLOAD - Loads the tape.
4854 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
4855 * MTUNLOAD prevents further access until the media is replaced.
4857 * MTNOP - Flushes tape buffers.
4859 * MTRETEN - Retension media. This typically consists of one end
4860 * to end pass on the media.
4862 * MTEOM - Moves to the end of recorded data.
4864 * MTERASE - Erases tape.
4866 * MTSETBLK - Sets the user block size to mt_count bytes. If
4867 * mt_count is 0, we will attempt to autodetect
4868 * the block size.
4870 * MTSEEK - Positions the tape in a specific block number, where
4871 * each block is assumed to contain which user_block_size
4872 * bytes.
4874 * MTSETPART - Switches to another tape partition.
4876 * MTLOCK - Locks the tape door.
4878 * MTUNLOCK - Unlocks the tape door.
4880 * The following commands are currently not supported:
4882 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
4883 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
4885 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
4887 idetape_tape_t *tape = drive->driver_data;
4888 idetape_pc_t pc;
4889 int i,retval;
4891 #if IDETAPE_DEBUG_LOG
4892 if (tape->debug_level >= 1)
4893 printk (KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",mt_op,mt_count);
4894 #endif /* IDETAPE_DEBUG_LOG */
4896 * Commands which need our pipelined read-ahead stages.
4898 switch (mt_op) {
4899 case MTFSF:
4900 case MTFSFM:
4901 case MTBSF:
4902 case MTBSFM:
4903 if (!mt_count)
4904 return (0);
4905 return (idetape_space_over_filemarks (drive,mt_op,mt_count));
4906 default:
4907 break;
4909 switch (mt_op) {
4910 case MTWEOF:
4911 idetape_discard_read_pipeline (drive, 1);
4912 for (i = 0; i < mt_count; i++) {
4913 retval = idetape_write_filemark(drive);
4914 if (retval) return retval;
4916 return (0);
4917 case MTREW:
4918 idetape_discard_read_pipeline (drive, 0);
4919 if (idetape_rewind_tape(drive))
4920 return -EIO;
4921 if (tape->onstream && !tape->raw)
4922 return idetape_position_tape(drive, 20, 0, 0);
4923 return 0;
4924 case MTLOAD:
4925 idetape_discard_read_pipeline (drive, 0);
4926 idetape_create_load_unload_cmd (drive, &pc, IDETAPE_LU_LOAD_MASK);
4927 return (idetape_queue_pc_tail (drive,&pc));
4928 case MTUNLOAD:
4929 case MTOFFL:
4930 idetape_discard_read_pipeline (drive, 0);
4931 idetape_create_load_unload_cmd (drive, &pc,!IDETAPE_LU_LOAD_MASK);
4932 return (idetape_queue_pc_tail (drive,&pc));
4933 case MTNOP:
4934 idetape_discard_read_pipeline (drive, 0);
4935 return (idetape_flush_tape_buffers (drive));
4936 case MTRETEN:
4937 idetape_discard_read_pipeline (drive, 0);
4938 idetape_create_load_unload_cmd (drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
4939 return (idetape_queue_pc_tail (drive,&pc));
4940 case MTEOM:
4941 if (tape->onstream) {
4942 #if ONSTREAM_DEBUG
4943 if (tape->debug_level >= 2)
4944 printk(KERN_INFO "ide-tape: %s: positioning tape to eod at %d\n", tape->name, tape->eod_frame_addr);
4945 #endif
4946 idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
4947 if (!idetape_get_logical_blk(drive, -1, 10, 0))
4948 return -EIO;
4949 if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_EOD)
4950 return -EIO;
4951 return 0;
4953 idetape_create_space_cmd (&pc,0,IDETAPE_SPACE_TO_EOD);
4954 return (idetape_queue_pc_tail (drive,&pc));
4955 case MTERASE:
4956 if (tape->onstream) {
4957 tape->eod_frame_addr = 20;
4958 tape->logical_blk_num = 0;
4959 tape->first_mark_addr = tape->last_mark_addr = -1;
4960 idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
4961 idetape_write_eod(drive);
4962 idetape_flush_tape_buffers (drive);
4963 idetape_write_header(drive, 0);
4964 idetape_flush_tape_buffers (drive);
4965 (void) idetape_rewind_tape (drive);
4966 return 0;
4968 (void) idetape_rewind_tape (drive);
4969 idetape_create_erase_cmd (&pc);
4970 return (idetape_queue_pc_tail (drive,&pc));
4971 case MTSETBLK:
4972 if (tape->onstream) {
4973 if (mt_count != tape->tape_block_size) {
4974 printk(KERN_INFO "ide-tape: %s: MTSETBLK %d -- only %d bytes block size supported\n", tape->name, mt_count, tape->tape_block_size);
4975 return -EINVAL;
4977 return 0;
4979 if (mt_count) {
4980 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
4981 return -EIO;
4982 tape->user_bs_factor = mt_count / tape->tape_block_size;
4983 clear_bit (IDETAPE_DETECT_BS, &tape->flags);
4984 } else
4985 set_bit (IDETAPE_DETECT_BS, &tape->flags);
4986 return 0;
4987 case MTSEEK:
4988 if (!tape->onstream || tape->raw) {
4989 idetape_discard_read_pipeline (drive, 0);
4990 return idetape_position_tape (drive, mt_count * tape->user_bs_factor, tape->partition, 0);
4992 return idetape_seek_logical_blk(drive, mt_count);
4993 case MTSETPART:
4994 idetape_discard_read_pipeline (drive, 0);
4995 if (tape->onstream)
4996 return -EIO;
4997 return (idetape_position_tape (drive, 0, mt_count, 0));
4998 case MTFSR:
4999 case MTBSR:
5000 if (tape->onstream) {
5001 if (!idetape_get_logical_blk(drive, -1, 10, 0))
5002 return -EIO;
5003 if (mt_op == MTFSR)
5004 return idetape_seek_logical_blk(drive, tape->logical_blk_num + mt_count);
5005 else {
5006 idetape_discard_read_pipeline (drive, 0);
5007 return idetape_seek_logical_blk(drive, tape->logical_blk_num - mt_count);
5010 case MTLOCK:
5011 idetape_create_prevent_cmd(drive, &pc, 1);
5012 retval = idetape_queue_pc_tail (drive,&pc);
5013 if (retval) return retval;
5014 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
5015 return 0;
5016 case MTUNLOCK:
5017 idetape_create_prevent_cmd(drive, &pc, 0);
5018 retval = idetape_queue_pc_tail (drive,&pc);
5019 if (retval) return retval;
5020 tape->door_locked = DOOR_UNLOCKED;
5021 return 0;
5022 default:
5023 printk (KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
5024 return (-EIO);
5029 * Our character device ioctls.
5031 * General mtio.h magnetic io commands are supported here, and not in
5032 * the corresponding block interface.
5034 * The following ioctls are supported:
5036 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
5038 * MTIOCGET - The mt_dsreg field in the returned mtget structure
5039 * will be set to (user block size in bytes <<
5040 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
5042 * The mt_blkno is set to the current user block number.
5043 * The other mtget fields are not supported.
5045 * MTIOCPOS - The current tape "block position" is returned. We
5046 * assume that each block contains user_block_size
5047 * bytes.
5049 * Our own ide-tape ioctls are supported on both interfaces.
5051 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
5053 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
5054 idetape_tape_t *tape = drive->driver_data;
5055 struct mtop mtop;
5056 struct mtget mtget;
5057 struct mtpos mtpos;
5058 int block_offset = 0, position = tape->first_frame_position;
5060 #if IDETAPE_DEBUG_LOG
5061 if (tape->debug_level >= 3)
5062 printk (KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
5063 #endif /* IDETAPE_DEBUG_LOG */
5065 tape->restart_speed_control_req = 1;
5066 if (tape->chrdev_direction == idetape_direction_write) {
5067 idetape_empty_write_pipeline (drive);
5068 idetape_flush_tape_buffers (drive);
5070 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
5071 block_offset = idetape_pipeline_size (drive) / (tape->tape_block_size * tape->user_bs_factor);
5072 if ((position = idetape_read_position(drive)) < 0)
5073 return -EIO;
5075 switch (cmd) {
5076 case MTIOCTOP:
5077 if (copy_from_user ((char *) &mtop, (char *) arg, sizeof (struct mtop)))
5078 return -EFAULT;
5079 return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
5080 case MTIOCGET:
5081 memset (&mtget, 0, sizeof (struct mtget));
5082 mtget.mt_type = MT_ISSCSI2;
5083 if (!tape->onstream || tape->raw)
5084 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
5085 else {
5086 if (!idetape_get_logical_blk(drive, -1, 10, 0))
5087 mtget.mt_blkno = -1;
5088 else
5089 mtget.mt_blkno = tape->logical_blk_num;
5091 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
5092 if (tape->onstream) {
5093 mtget.mt_gstat |= GMT_ONLINE(0xffffffff);
5094 if (tape->first_stage && tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD)
5095 mtget.mt_gstat |= GMT_EOD(0xffffffff);
5096 if (position <= 20)
5097 mtget.mt_gstat |= GMT_BOT(0xffffffff);
5099 if (copy_to_user ((char *) arg,(char *) &mtget, sizeof (struct mtget)))
5100 return -EFAULT;
5101 return 0;
5102 case MTIOCPOS:
5103 if (tape->onstream && !tape->raw) {
5104 if (!idetape_get_logical_blk(drive, -1, 10, 0))
5105 return -EIO;
5106 mtpos.mt_blkno = tape->logical_blk_num;
5107 } else
5108 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
5109 if (copy_to_user ((char *) arg,(char *) &mtpos, sizeof (struct mtpos)))
5110 return -EFAULT;
5111 return 0;
5112 default:
5113 if (tape->chrdev_direction == idetape_direction_read)
5114 idetape_discard_read_pipeline (drive, 1);
5115 return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
5119 static int __idetape_analyze_headers (ide_drive_t *drive, int block)
5121 idetape_tape_t *tape = drive->driver_data;
5122 idetape_stage_t *stage;
5123 os_header_t *header;
5124 os_aux_t *aux;
5126 if (!tape->onstream || tape->raw) {
5127 tape->header_ok = tape->linux_media = 1;
5128 return 1;
5130 tape->header_ok = tape->linux_media = 0;
5131 tape->update_frame_cntr = 0;
5132 tape->wrt_pass_cntr = 0;
5133 tape->eod_frame_addr = 20;
5134 tape->first_mark_addr = tape->last_mark_addr = -1;
5135 stage = __idetape_kmalloc_stage (tape, 0, 0);
5136 if (stage == NULL)
5137 return 0;
5138 #if ONSTREAM_DEBUG
5139 if (tape->debug_level >= 2)
5140 printk(KERN_INFO "ide-tape: %s: reading header\n", tape->name);
5141 #endif
5142 idetape_position_tape(drive, block, 0, 0);
5143 if (!idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 1, stage->bh)) {
5144 printk(KERN_INFO "ide-tape: %s: couldn't read header frame\n", tape->name);
5145 __idetape_kfree_stage (stage);
5146 return 0;
5148 header = (os_header_t *) stage->bh->b_data;
5149 aux = stage->aux;
5150 if (strncmp(header->ident_str, "ADR_SEQ", 7) != 0) {
5151 printk(KERN_INFO "ide-tape: %s: invalid header identification string\n", tape->name);
5152 __idetape_kfree_stage (stage);
5153 return 0;
5155 if (header->major_rev != 1 || (header->minor_rev != 1 && header->minor_rev != 2))
5156 printk(KERN_INFO "ide-tape: warning: revision %d.%d detected (1.1/1.2 supported)\n", header->major_rev, header->minor_rev);
5157 if (header->par_num != 1)
5158 printk(KERN_INFO "ide-tape: warning: %d partitions defined, only one supported\n", header->par_num);
5159 tape->wrt_pass_cntr = ntohs(header->partition.wrt_pass_cntr);
5160 tape->eod_frame_addr = ntohl(header->partition.eod_frame_addr);
5161 tape->filemark_cnt = ntohl(aux->filemark_cnt);
5162 tape->first_mark_addr = ntohl(aux->next_mark_addr);
5163 tape->last_mark_addr = ntohl(aux->last_mark_addr);
5164 tape->update_frame_cntr = ntohl(aux->update_frame_cntr);
5165 memcpy(tape->application_sig, aux->application_sig, 4); tape->application_sig[4] = 0;
5166 if (memcmp(tape->application_sig, "LIN", 3) == 0) {
5167 tape->linux_media = 1;
5168 tape->linux_media_version = tape->application_sig[3] - '0';
5169 if (tape->linux_media_version != 3)
5170 printk(KERN_INFO "ide-tape: %s: Linux media version %d detected (current 3)\n", tape->name, tape->linux_media_version);
5171 } else {
5172 printk(KERN_INFO "ide-tape: %s: non Linux media detected (%s)\n", tape->name, tape->application_sig);
5173 tape->linux_media = 0;
5175 #if ONSTREAM_DEBUG
5176 if (tape->debug_level >= 2)
5177 printk(KERN_INFO "ide-tape: %s: detected write pass counter %d, eod frame addr %d\n", tape->name, tape->wrt_pass_cntr, tape->eod_frame_addr);
5178 #endif
5179 __idetape_kfree_stage (stage);
5180 return 1;
5183 static int idetape_analyze_headers (ide_drive_t *drive)
5185 idetape_tape_t *tape = drive->driver_data;
5186 int position, block;
5188 if (!tape->onstream || tape->raw) {
5189 tape->header_ok = tape->linux_media = 1;
5190 return 1;
5192 tape->header_ok = tape->linux_media = 0;
5193 position = idetape_read_position(drive);
5194 for (block = 5; block < 10; block++)
5195 if (__idetape_analyze_headers(drive, block))
5196 goto ok;
5197 #if 0
5198 for (block = 0xbae; block < 0xbb8; block++)
5199 #else
5200 for (block = 0xbae; block < 0xbb3; block++)
5201 #endif
5202 if (__idetape_analyze_headers(drive, block))
5203 goto ok;
5204 printk(KERN_ERR "ide-tape: %s: failed to find valid ADRL header\n", tape->name);
5205 return 0;
5207 if (position < 20)
5208 position = 20;
5209 idetape_position_tape(drive, position, 0, 0);
5210 tape->header_ok = 1;
5211 return 1;
5215 * Our character device open function.
5217 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
5219 ide_drive_t *drive;
5220 idetape_tape_t *tape;
5221 idetape_pc_t pc;
5222 unsigned int minor=MINOR (inode->i_rdev);
5224 #if IDETAPE_DEBUG_LOG
5225 printk (KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
5226 #endif /* IDETAPE_DEBUG_LOG */
5228 if ((drive = get_drive_ptr (inode->i_rdev)) == NULL)
5229 return -ENXIO;
5230 tape = drive->driver_data;
5232 if (test_and_set_bit (IDETAPE_BUSY, &tape->flags))
5233 return -EBUSY;
5234 MOD_INC_USE_COUNT;
5235 #if ONSTREAM_DEBUG
5236 if (tape->debug_level >= 6)
5237 printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_chrdev_open-1\n");
5238 #endif
5239 if (!tape->onstream) {
5240 idetape_read_position(drive);
5241 if (!test_bit (IDETAPE_ADDRESS_VALID, &tape->flags))
5242 (void) idetape_rewind_tape (drive);
5243 } else {
5244 if (minor & 64) {
5245 tape->tape_block_size = tape->stage_size = 32768 + 512;
5246 tape->raw = 1;
5247 } else {
5248 tape->tape_block_size = tape->stage_size = 32768;
5249 tape->raw = 0;
5252 if (idetape_wait_ready(drive, 60 * HZ)) {
5253 clear_bit(IDETAPE_BUSY, &tape->flags);
5254 MOD_DEC_USE_COUNT;
5255 #if ONSTREAM_DEBUG
5256 if (tape->debug_level >= 6)
5257 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_chrdev_open-1\n");
5258 #endif
5259 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
5260 return -EBUSY;
5262 idetape_read_position(drive);
5263 MOD_DEC_USE_COUNT;
5264 #if ONSTREAM_DEBUG
5265 if (tape->debug_level >= 6)
5266 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_chrdev_open-2\n");
5267 #endif
5268 clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
5270 if (tape->chrdev_direction == idetape_direction_none) {
5271 MOD_INC_USE_COUNT;
5272 #if ONSTREAM_DEBUG
5273 if (tape->debug_level >= 6)
5274 printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_chrdev_open-2\n");
5275 #endif
5276 idetape_create_prevent_cmd(drive, &pc, 1);
5277 if (!idetape_queue_pc_tail (drive,&pc)) {
5278 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
5279 tape->door_locked = DOOR_LOCKED;
5281 idetape_analyze_headers(drive);
5283 tape->max_frames = tape->cur_frames = tape->req_buffer_fill = 0;
5284 idetape_restart_speed_control(drive);
5285 tape->restart_speed_control_req = 0;
5286 return 0;
5290 * Our character device release function.
5292 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
5294 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
5295 idetape_tape_t *tape = drive->driver_data;
5296 idetape_pc_t pc;
5297 unsigned int minor=MINOR (inode->i_rdev);
5299 #if IDETAPE_DEBUG_LOG
5300 if (tape->debug_level >= 3)
5301 printk (KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
5302 #endif /* IDETAPE_DEBUG_LOG */
5304 if (tape->chrdev_direction == idetape_direction_write) {
5305 idetape_empty_write_pipeline (drive);
5306 tape->merge_stage = __idetape_kmalloc_stage (tape, 1, 0);
5307 if (tape->merge_stage != NULL) {
5308 idetape_pad_zeros (drive, tape->tape_block_size * (tape->user_bs_factor - 1));
5309 __idetape_kfree_stage (tape->merge_stage);
5310 tape->merge_stage = NULL;
5312 idetape_write_filemark(drive);
5313 idetape_write_eod(drive);
5314 idetape_flush_tape_buffers (drive);
5315 idetape_write_header(drive, minor >= 128);
5316 idetape_flush_tape_buffers (drive);
5318 if (tape->chrdev_direction == idetape_direction_read) {
5319 if (minor < 128)
5320 idetape_discard_read_pipeline (drive, 1);
5321 else
5322 idetape_wait_for_pipeline (drive);
5324 if (tape->cache_stage != NULL) {
5325 __idetape_kfree_stage (tape->cache_stage);
5326 tape->cache_stage = NULL;
5328 if (minor < 128)
5329 (void) idetape_rewind_tape (drive);
5330 if (tape->chrdev_direction == idetape_direction_none) {
5331 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) {
5332 idetape_create_prevent_cmd(drive, &pc, 0);
5333 if (!idetape_queue_pc_tail (drive,&pc))
5334 tape->door_locked = DOOR_UNLOCKED;
5336 MOD_DEC_USE_COUNT;
5337 #if ONSTREAM_DEBUG
5338 if (tape->debug_level >= 6)
5339 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_chrdev_release\n");
5340 #endif
5342 clear_bit (IDETAPE_BUSY, &tape->flags);
5343 return 0;
5347 * idetape_identify_device is called to check the contents of the
5348 * ATAPI IDENTIFY command results. We return:
5350 * 1 If the tape can be supported by us, based on the information
5351 * we have so far.
5353 * 0 If this tape driver is not currently supported by us.
5355 static int idetape_identify_device (ide_drive_t *drive,struct hd_driveid *id)
5357 struct idetape_id_gcw gcw;
5358 #if IDETAPE_DEBUG_INFO
5359 unsigned short mask,i;
5360 #endif /* IDETAPE_DEBUG_INFO */
5362 if (!id)
5363 return 0;
5365 *((unsigned short *) &gcw) = id->config;
5367 #if IDETAPE_DEBUG_INFO
5368 printk (KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
5369 printk (KERN_INFO "ide-tape: Protocol Type: ");
5370 switch (gcw.protocol) {
5371 case 0: case 1: printk (KERN_INFO "ATA\n");break;
5372 case 2: printk (KERN_INFO "ATAPI\n");break;
5373 case 3: printk (KERN_INFO "Reserved (Unknown to ide-tape)\n");break;
5375 printk (KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
5376 switch (gcw.device_type) {
5377 case 0: printk (KERN_INFO "Direct-access Device\n");break;
5378 case 1: printk (KERN_INFO "Streaming Tape Device\n");break;
5379 case 2: case 3: case 4: printk (KERN_INFO "Reserved\n");break;
5380 case 5: printk (KERN_INFO "CD-ROM Device\n");break;
5381 case 6: printk (KERN_INFO "Reserved\n");
5382 case 7: printk (KERN_INFO "Optical memory Device\n");break;
5383 case 0x1f: printk (KERN_INFO "Unknown or no Device type\n");break;
5384 default: printk (KERN_INFO "Reserved\n");
5386 printk (KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
5387 printk (KERN_INFO "ide-tape: Command Packet DRQ Type: ");
5388 switch (gcw.drq_type) {
5389 case 0: printk (KERN_INFO "Microprocessor DRQ\n");break;
5390 case 1: printk (KERN_INFO "Interrupt DRQ\n");break;
5391 case 2: printk (KERN_INFO "Accelerated DRQ\n");break;
5392 case 3: printk (KERN_INFO "Reserved\n");break;
5394 printk (KERN_INFO "ide-tape: Command Packet Size: ");
5395 switch (gcw.packet_size) {
5396 case 0: printk (KERN_INFO "12 bytes\n");break;
5397 case 1: printk (KERN_INFO "16 bytes\n");break;
5398 default: printk (KERN_INFO "Reserved\n");break;
5400 printk (KERN_INFO "ide-tape: Model: %.40s\n",id->model);
5401 printk (KERN_INFO "ide-tape: Firmware Revision: %.8s\n",id->fw_rev);
5402 printk (KERN_INFO "ide-tape: Serial Number: %.20s\n",id->serial_no);
5403 printk (KERN_INFO "ide-tape: Write buffer size: %d bytes\n",id->buf_size*512);
5404 printk (KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
5405 printk (KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
5406 printk (KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
5407 printk (KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
5408 printk (KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
5409 printk (KERN_INFO "ide-tape: PIO Cycle Timing Category: %d\n",id->tPIO);
5410 printk (KERN_INFO "ide-tape: DMA Cycle Timing Category: %d\n",id->tDMA);
5411 printk (KERN_INFO "ide-tape: Single Word DMA supported modes: ");
5412 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
5413 if (id->dma_1word & mask)
5414 printk (KERN_INFO "%d ",i);
5415 if (id->dma_1word & (mask << 8))
5416 printk (KERN_INFO "(active) ");
5418 printk (KERN_INFO "\n");
5419 printk (KERN_INFO "ide-tape: Multi Word DMA supported modes: ");
5420 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
5421 if (id->dma_mword & mask)
5422 printk (KERN_INFO "%d ",i);
5423 if (id->dma_mword & (mask << 8))
5424 printk (KERN_INFO "(active) ");
5426 printk (KERN_INFO "\n");
5427 if (id->field_valid & 0x0002) {
5428 printk (KERN_INFO "ide-tape: Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
5429 printk (KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: ");
5430 if (id->eide_dma_min == 0)
5431 printk (KERN_INFO "Not supported\n");
5432 else
5433 printk (KERN_INFO "%d ns\n",id->eide_dma_min);
5435 printk (KERN_INFO "ide-tape: Manufacturer\'s Recommended Multi-word cycle: ");
5436 if (id->eide_dma_time == 0)
5437 printk (KERN_INFO "Not supported\n");
5438 else
5439 printk (KERN_INFO "%d ns\n",id->eide_dma_time);
5441 printk (KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: ");
5442 if (id->eide_pio == 0)
5443 printk (KERN_INFO "Not supported\n");
5444 else
5445 printk (KERN_INFO "%d ns\n",id->eide_pio);
5447 printk (KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: ");
5448 if (id->eide_pio_iordy == 0)
5449 printk (KERN_INFO "Not supported\n");
5450 else
5451 printk (KERN_INFO "%d ns\n",id->eide_pio_iordy);
5453 } else
5454 printk (KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.\n");
5455 #endif /* IDETAPE_DEBUG_INFO */
5457 /* Check that we can support this device */
5459 if (gcw.protocol !=2 )
5460 printk (KERN_ERR "ide-tape: Protocol is not ATAPI\n");
5461 else if (gcw.device_type != 1)
5462 printk (KERN_ERR "ide-tape: Device type is not set to tape\n");
5463 else if (!gcw.removable)
5464 printk (KERN_ERR "ide-tape: The removable flag is not set\n");
5465 else if (gcw.packet_size != 0) {
5466 printk (KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
5467 if (gcw.packet_size == 1)
5468 printk (KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
5469 } else
5470 return 1;
5471 return 0;
5475 * Notify vendor ID to the OnStream tape drive
5477 static void idetape_onstream_set_vendor (ide_drive_t *drive, char *vendor)
5479 idetape_pc_t pc;
5480 idetape_mode_parameter_header_t *header;
5482 idetape_create_mode_select_cmd(&pc, sizeof(*header) + 8);
5483 pc.buffer[0] = 3 + 8; /* Mode Data Length */
5484 pc.buffer[1] = 0; /* Medium Type - ignoring */
5485 pc.buffer[2] = 0; /* Reserved */
5486 pc.buffer[3] = 0; /* Block Descriptor Length */
5487 pc.buffer[4 + 0] = 0x36 | (1 << 7);
5488 pc.buffer[4 + 1] = 6;
5489 pc.buffer[4 + 2] = vendor[0];
5490 pc.buffer[4 + 3] = vendor[1];
5491 pc.buffer[4 + 4] = vendor[2];
5492 pc.buffer[4 + 5] = vendor[3];
5493 pc.buffer[4 + 6] = 0;
5494 pc.buffer[4 + 7] = 0;
5495 if (idetape_queue_pc_tail (drive,&pc))
5496 printk (KERN_ERR "ide-tape: Couldn't set vendor name to %s\n", vendor);
5501 * Various unused OnStream commands
5503 #if ONSTREAM_DEBUG
5504 static void idetape_onstream_set_retries (ide_drive_t *drive, int retries)
5506 idetape_pc_t pc;
5508 idetape_create_mode_select_cmd(&pc, sizeof(idetape_mode_parameter_header_t) + 4);
5509 pc.buffer[0] = 3 + 4;
5510 pc.buffer[1] = 0; /* Medium Type - ignoring */
5511 pc.buffer[2] = 0; /* Reserved */
5512 pc.buffer[3] = 0; /* Block Descriptor Length */
5513 pc.buffer[4 + 0] = 0x2f | (1 << 7);
5514 pc.buffer[4 + 1] = 2;
5515 pc.buffer[4 + 2] = 4;
5516 pc.buffer[4 + 3] = retries;
5517 if (idetape_queue_pc_tail (drive,&pc))
5518 printk (KERN_ERR "ide-tape: Couldn't set retries to %d\n", retries);
5520 #endif
5523 * Configure 32.5KB block size.
5525 static void idetape_onstream_configure_block_size (ide_drive_t *drive)
5527 idetape_pc_t pc;
5528 idetape_mode_parameter_header_t *header;
5529 idetape_block_size_page_t *bs;
5532 * Get the current block size from the block size mode page
5534 idetape_create_mode_sense_cmd (&pc,IDETAPE_BLOCK_SIZE_PAGE);
5535 if (idetape_queue_pc_tail (drive,&pc))
5536 printk (KERN_ERR "ide-tape: can't get tape block size mode page\n");
5537 header = (idetape_mode_parameter_header_t *) pc.buffer;
5538 bs = (idetape_block_size_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
5540 #if IDETAPE_DEBUG_INFO
5541 printk(KERN_INFO "ide-tape: 32KB play back: %s\n", bs->play32 ? "Yes" : "No");
5542 printk(KERN_INFO "ide-tape: 32.5KB play back: %s\n", bs->play32_5 ? "Yes" : "No");
5543 printk(KERN_INFO "ide-tape: 32KB record: %s\n", bs->record32 ? "Yes" : "No");
5544 printk(KERN_INFO "ide-tape: 32.5KB record: %s\n", bs->record32_5 ? "Yes" : "No");
5545 #endif /* IDETAPE_DEBUG_INFO */
5548 * Configure default auto columns mode, 32.5KB block size
5550 bs->one = 1;
5551 bs->play32 = 0;
5552 bs->play32_5 = 1;
5553 bs->record32 = 0;
5554 bs->record32_5 = 1;
5555 idetape_create_mode_select_cmd(&pc, sizeof(*header) + sizeof(*bs));
5556 if (idetape_queue_pc_tail (drive,&pc))
5557 printk (KERN_ERR "ide-tape: Couldn't set tape block size mode page\n");
5559 #if ONSTREAM_DEBUG
5561 * In debug mode, we want to see as many errors as possible
5562 * to test the error recovery mechanism.
5564 idetape_onstream_set_retries(drive, 0);
5565 #endif
5569 * Use INQUIRY to get the firmware revision
5571 static void idetape_get_inquiry_results (ide_drive_t *drive)
5573 char *r;
5574 idetape_tape_t *tape = drive->driver_data;
5575 idetape_pc_t pc;
5576 idetape_inquiry_result_t *inquiry;
5578 idetape_create_inquiry_cmd(&pc);
5579 if (idetape_queue_pc_tail (drive,&pc)) {
5580 printk (KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
5581 return;
5583 inquiry = (idetape_inquiry_result_t *) pc.buffer;
5584 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
5585 memcpy(tape->product_id, inquiry->product_id, 16);
5586 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
5587 ide_fixstring(tape->vendor_id, 10, 0);
5588 ide_fixstring(tape->product_id, 18, 0);
5589 ide_fixstring(tape->firmware_revision, 6, 0);
5590 r = tape->firmware_revision;
5591 if (*(r + 1) == '.')
5592 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
5593 else if (tape->onstream)
5594 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 1) - '0') * 10 + *(r + 2) - '0';
5595 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);
5599 * Configure the OnStream ATAPI tape drive for default operation
5601 static void idetape_configure_onstream (ide_drive_t *drive)
5603 idetape_tape_t *tape = drive->driver_data;
5605 if (tape->firmware_revision_num < 105) {
5606 printk(KERN_INFO "ide-tape: %s: Old OnStream firmware revision detected (%s)\n", tape->name, tape->firmware_revision);
5607 printk(KERN_INFO "ide-tape: %s: An upgrade to version 1.05 or above is recommended\n", tape->name);
5611 * Configure 32.5KB (data+aux) block size.
5613 idetape_onstream_configure_block_size(drive);
5616 * Set vendor name to 'LIN3' for "Linux support version 3".
5618 idetape_onstream_set_vendor(drive, "LIN3");
5622 * idetape_get_mode_sense_results asks the tape about its various
5623 * parameters. In particular, we will adjust our data transfer buffer
5624 * size to the recommended value as returned by the tape.
5626 static void idetape_get_mode_sense_results (ide_drive_t *drive)
5628 idetape_tape_t *tape = drive->driver_data;
5629 idetape_pc_t pc;
5630 idetape_mode_parameter_header_t *header;
5631 idetape_capabilities_page_t *capabilities;
5633 idetape_create_mode_sense_cmd (&pc,IDETAPE_CAPABILITIES_PAGE);
5634 if (idetape_queue_pc_tail (drive,&pc)) {
5635 printk (KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
5636 tape->tape_block_size = 512; tape->capabilities.ctl = 52;
5637 tape->capabilities.speed = 450; tape->capabilities.buffer_size = 6 * 52;
5638 return;
5640 header = (idetape_mode_parameter_header_t *) pc.buffer;
5641 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
5643 capabilities->max_speed = ntohs (capabilities->max_speed);
5644 capabilities->ctl = ntohs (capabilities->ctl);
5645 capabilities->speed = ntohs (capabilities->speed);
5646 capabilities->buffer_size = ntohs (capabilities->buffer_size);
5648 if (!capabilities->speed) {
5649 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
5650 capabilities->speed = 650;
5652 if (!capabilities->max_speed) {
5653 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
5654 capabilities->max_speed = 650;
5657 tape->capabilities = *capabilities; /* Save us a copy */
5658 if (capabilities->blk512)
5659 tape->tape_block_size = 512;
5660 else if (capabilities->blk1024)
5661 tape->tape_block_size = 1024;
5662 else if (tape->onstream && capabilities->blk32768)
5663 tape->tape_block_size = 32768;
5665 #if IDETAPE_DEBUG_INFO
5666 printk (KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
5667 printk (KERN_INFO "ide-tape: Mode Parameter Header:\n");
5668 printk (KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
5669 printk (KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
5670 printk (KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
5671 printk (KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
5673 printk (KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
5674 printk (KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
5675 printk (KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
5676 printk (KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
5677 printk (KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
5678 printk (KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
5679 printk (KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
5680 printk (KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
5681 printk (KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
5682 printk (KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
5683 printk (KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
5684 printk (KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
5685 printk (KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
5686 printk (KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
5687 printk (KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
5688 printk (KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
5689 printk (KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
5690 printk (KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
5691 printk (KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
5692 printk (KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
5693 #endif /* IDETAPE_DEBUG_INFO */
5696 static void idetape_add_settings (ide_drive_t *drive)
5698 idetape_tape_t *tape = drive->driver_data;
5701 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
5703 ide_add_setting(drive, "buffer", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
5704 ide_add_setting(drive, "pipeline_min", SETTING_RW, -1, -1, TYPE_INT, 2, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
5705 ide_add_setting(drive, "pipeline", SETTING_RW, -1, -1, TYPE_INT, 2, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
5706 ide_add_setting(drive, "pipeline_max", SETTING_RW, -1, -1, TYPE_INT, 2, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
5707 ide_add_setting(drive, "pipeline_used",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
5708 ide_add_setting(drive, "pipeline_pending",SETTING_READ,-1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
5709 ide_add_setting(drive, "speed", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
5710 ide_add_setting(drive, "stage", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
5711 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);
5712 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
5713 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
5714 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed, NULL);
5715 ide_add_setting(drive, "avg_speed", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
5716 if (tape->onstream) {
5717 ide_add_setting(drive, "cur_frames", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->cur_frames, NULL);
5718 ide_add_setting(drive, "max_frames", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->max_frames, NULL);
5719 ide_add_setting(drive, "insert_speed", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->insert_speed, NULL);
5720 ide_add_setting(drive, "speed_control",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->speed_control, NULL);
5721 ide_add_setting(drive, "debug_level",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
5722 ide_add_setting(drive, "tape_still_time",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->tape_still_time, NULL);
5723 ide_add_setting(drive, "max_insert_speed",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->max_insert_speed, NULL);
5724 ide_add_setting(drive, "insert_size", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->insert_size, NULL);
5729 * ide_setup is called to:
5731 * 1. Initialize our various state variables.
5732 * 2. Ask the tape for its capabilities.
5733 * 3. Allocate a buffer which will be used for data
5734 * transfer. The buffer size is chosen based on
5735 * the recommendation which we received in step (2).
5737 * Note that at this point ide.c already assigned us an irq, so that
5738 * we can queue requests here and wait for their completion.
5740 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
5742 unsigned long t1, tmid, tn, t;
5743 int speed;
5744 struct idetape_id_gcw gcw;
5745 int stage_size;
5747 memset (tape, 0, sizeof (idetape_tape_t));
5748 spin_lock_init(&tape->spinlock);
5749 drive->driver_data = tape;
5750 drive->ready_stat = 0; /* An ATAPI device ignores DRDY */
5751 if (strstr(drive->id->model, "OnStream DI-30"))
5752 tape->onstream = 1;
5753 drive->dsc_overlap = 1;
5754 #ifdef CONFIG_BLK_DEV_IDEPCI
5755 if (!tape->onstream && HWIF(drive)->pci_dev != NULL) {
5757 * These two ide-pci host adapters appear to need DSC overlap disabled.
5758 * This probably needs further analysis.
5760 if ((HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
5761 (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) {
5762 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", tape->name);
5763 drive->dsc_overlap = 0;
5766 #endif /* CONFIG_BLK_DEV_IDEPCI */
5767 tape->drive = drive;
5768 tape->minor = minor;
5769 tape->name[0] = 'h'; tape->name[1] = 't'; tape->name[2] = '0' + minor;
5770 tape->chrdev_direction = idetape_direction_none;
5771 tape->pc = tape->pc_stack;
5772 tape->max_insert_speed = 10000;
5773 tape->speed_control = 1;
5774 *((unsigned short *) &gcw) = drive->id->config;
5775 if (gcw.drq_type == 1)
5776 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
5778 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
5780 idetape_get_inquiry_results(drive);
5781 idetape_get_mode_sense_results(drive);
5782 if (tape->onstream)
5783 idetape_configure_onstream(drive);
5785 tape->user_bs_factor = 1;
5786 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
5787 while (tape->stage_size > 0xffff) {
5788 printk (KERN_NOTICE "ide-tape: decreasing stage size\n");
5789 tape->capabilities.ctl /= 2;
5790 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
5792 stage_size = tape->stage_size;
5793 if (tape->onstream)
5794 stage_size = 32768 + 512;
5795 tape->pages_per_stage = stage_size / PAGE_SIZE;
5796 if (stage_size % PAGE_SIZE) {
5797 tape->pages_per_stage++;
5798 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
5802 * Select the "best" DSC read/write polling frequency
5803 * and pipeline size.
5805 speed = IDE_MAX (tape->capabilities.speed, tape->capabilities.max_speed);
5807 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
5808 tape->min_pipeline = tape->max_stages;
5809 tape->max_pipeline = tape->max_stages * 2;
5811 t1 = (tape->stage_size * HZ) / (speed * 1000);
5812 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
5813 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
5815 if (tape->max_stages)
5816 t = tn;
5817 else
5818 t = t1;
5821 * Ensure that the number we got makes sense; limit
5822 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
5824 tape->best_dsc_rw_frequency = IDE_MAX (IDE_MIN (t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
5825 printk (KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, %dkB pipeline, %lums tDSC%s\n",
5826 drive->name, tape->name, tape->capabilities.speed, (tape->capabilities.buffer_size * 512) / tape->stage_size,
5827 tape->stage_size / 1024, tape->max_stages * tape->stage_size / 1024,
5828 tape->best_dsc_rw_frequency * 1000 / HZ, drive->using_dma ? ", DMA":"");
5830 idetape_add_settings(drive);
5833 static int idetape_cleanup (ide_drive_t *drive)
5835 idetape_tape_t *tape = drive->driver_data;
5836 int minor = tape->minor;
5837 unsigned long flags;
5839 save_flags (flags); /* all CPUs (overkill?) */
5840 cli(); /* all CPUs (overkill?) */
5841 if (test_bit (IDETAPE_BUSY, &tape->flags) || tape->first_stage != NULL || tape->merge_stage_size || drive->usage) {
5842 restore_flags(flags); /* all CPUs (overkill?) */
5843 return 1;
5845 idetape_chrdevs[minor].drive = NULL;
5846 restore_flags (flags); /* all CPUs (overkill?) */
5847 DRIVER(drive)->busy = 0;
5848 (void) ide_unregister_subdriver (drive);
5849 drive->driver_data = NULL;
5850 devfs_unregister (tape->de_r);
5851 devfs_unregister (tape->de_n);
5852 kfree (tape);
5853 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++)
5854 if (idetape_chrdevs[minor].drive != NULL)
5855 return 0;
5856 devfs_unregister_chrdev (IDETAPE_MAJOR, "ht");
5857 idetape_chrdev_present = 0;
5858 return 0;
5861 #ifdef CONFIG_PROC_FS
5863 static int proc_idetape_read_name
5864 (char *page, char **start, off_t off, int count, int *eof, void *data)
5866 ide_drive_t *drive = (ide_drive_t *) data;
5867 idetape_tape_t *tape = drive->driver_data;
5868 char *out = page;
5869 int len;
5871 len = sprintf(out,"%s\n", tape->name);
5872 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
5875 static ide_proc_entry_t idetape_proc[] = {
5876 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
5877 { NULL, 0, NULL, NULL }
5880 #else
5882 #define idetape_proc NULL
5884 #endif
5887 * IDE subdriver functions, registered with ide.c
5889 static ide_driver_t idetape_driver = {
5890 "ide-tape", /* name */
5891 IDETAPE_VERSION, /* version */
5892 ide_tape, /* media */
5893 1, /* busy */
5894 1, /* supports_dma */
5895 1, /* supports_dsc_overlap */
5896 idetape_cleanup, /* cleanup */
5897 idetape_do_request, /* do_request */
5898 idetape_end_request, /* end_request */
5899 idetape_blkdev_ioctl, /* ioctl */
5900 idetape_blkdev_open, /* open */
5901 idetape_blkdev_release, /* release */
5902 NULL, /* media_change */
5903 idetape_pre_reset, /* pre_reset */
5904 NULL, /* capacity */
5905 NULL, /* special */
5906 idetape_proc /* proc */
5909 int idetape_init (void);
5910 static ide_module_t idetape_module = {
5911 IDE_DRIVER_MODULE,
5912 idetape_init,
5913 &idetape_driver,
5914 NULL
5918 * Our character device supporting functions, passed to register_chrdev.
5920 static struct file_operations idetape_fops = {
5921 read: idetape_chrdev_read,
5922 write: idetape_chrdev_write,
5923 ioctl: idetape_chrdev_ioctl,
5924 open: idetape_chrdev_open,
5925 release: idetape_chrdev_release,
5929 * idetape_init will register the driver for each tape.
5931 int idetape_init (void)
5933 ide_drive_t *drive;
5934 idetape_tape_t *tape;
5935 int minor, failed = 0, supported = 0;
5937 MOD_INC_USE_COUNT;
5938 #if ONSTREAM_DEBUG
5939 printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_init\n");
5940 #endif
5941 if (!idetape_chrdev_present)
5942 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
5943 idetape_chrdevs[minor].drive = NULL;
5945 if ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) == NULL) {
5946 ide_register_module (&idetape_module);
5947 MOD_DEC_USE_COUNT;
5948 #if ONSTREAM_DEBUG
5949 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_init\n");
5950 #endif
5951 return 0;
5953 if (!idetape_chrdev_present &&
5954 devfs_register_chrdev (IDETAPE_MAJOR, "ht", &idetape_fops)) {
5955 printk (KERN_ERR "ide-tape: Failed to register character device interface\n");
5956 MOD_DEC_USE_COUNT;
5957 #if ONSTREAM_DEBUG
5958 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_init\n");
5959 #endif
5960 return -EBUSY;
5962 do {
5963 if (!idetape_identify_device (drive, drive->id)) {
5964 printk (KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
5965 continue;
5967 if (drive->scsi) {
5968 if (strstr(drive->id->model, "OnStream DI-30")) {
5969 printk("ide-tape: ide-scsi emulation is not supported for %s.\n", drive->id->model);
5970 } else {
5971 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
5972 continue;
5975 tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
5976 if (tape == NULL) {
5977 printk (KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
5978 continue;
5980 if (ide_register_subdriver (drive, &idetape_driver, IDE_SUBDRIVER_VERSION)) {
5981 printk (KERN_ERR "ide-tape: %s: Failed to register the driver with ide.c\n", drive->name);
5982 kfree (tape);
5983 continue;
5985 for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
5986 idetape_setup (drive, tape, minor);
5987 idetape_chrdevs[minor].drive = drive;
5988 tape->de_r =
5989 devfs_register (drive->de, "mt", 2, DEVFS_FL_DEFAULT,
5990 HWIF(drive)->major, minor,
5991 S_IFCHR | S_IRUGO | S_IWUGO, 0, 0,
5992 &idetape_fops, NULL);
5993 tape->de_n =
5994 devfs_register (drive->de, "mtn", 3, DEVFS_FL_DEFAULT,
5995 HWIF(drive)->major, minor + 128,
5996 S_IFCHR | S_IRUGO | S_IWUGO, 0, 0,
5997 &idetape_fops, NULL);
5998 devfs_register_tape (tape->de_r);
5999 supported++; failed--;
6000 } while ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) != NULL);
6001 if (!idetape_chrdev_present && !supported) {
6002 devfs_unregister_chrdev (IDETAPE_MAJOR, "ht");
6003 } else
6004 idetape_chrdev_present = 1;
6005 ide_register_module (&idetape_module);
6006 MOD_DEC_USE_COUNT;
6007 #if ONSTREAM_DEBUG
6008 printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_init\n");
6009 #endif
6010 return 0;
6013 #ifdef MODULE
6014 int init_module (void)
6016 return idetape_init ();
6019 void cleanup_module (void)
6021 ide_drive_t *drive;
6022 int minor;
6024 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++) {
6025 drive = idetape_chrdevs[minor].drive;
6026 if (drive != NULL && idetape_cleanup (drive))
6027 printk (KERN_ERR "ide-tape: %s: cleanup_module() called while still busy\n", drive->name);
6029 ide_unregister_module(&idetape_module);
6031 #endif /* MODULE */