Import 2.3.9pre5
[davej-history.git] / drivers / block / ide-tape.c
blob3b2b5afb9d6a8fd7670946d39459d59cfb4efa87
1 /*
2 * linux/drivers/block/ide-tape.c Version 1.14 Dec 30, 1998
4 * Copyright (C) 1995 - 1998 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.
218 * Here are some words from the first releases of hd.c, which are quoted
219 * in ide.c and apply here as well:
221 * | Special care is recommended. Have Fun!
226 * An overview of the pipelined operation mode.
228 * In the pipelined write mode, we will usually just add requests to our
229 * pipeline and return immediately, before we even start to service them. The
230 * user program will then have enough time to prepare the next request while
231 * we are still busy servicing previous requests. In the pipelined read mode,
232 * the situation is similar - we add read-ahead requests into the pipeline,
233 * before the user even requested them.
235 * The pipeline can be viewed as a "safety net" which will be activated when
236 * the system load is high and prevents the user backup program from keeping up
237 * with the current tape speed. At this point, the pipeline will get
238 * shorter and shorter but the tape will still be streaming at the same speed.
239 * Assuming we have enough pipeline stages, the system load will hopefully
240 * decrease before the pipeline is completely empty, and the backup program
241 * will be able to "catch up" and refill the pipeline again.
243 * When using the pipelined mode, it would be best to disable any type of
244 * buffering done by the user program, as ide-tape already provides all the
245 * benefits in the kernel, where it can be done in a more efficient way.
246 * As we will usually not block the user program on a request, the most
247 * efficient user code will then be a simple read-write-read-... cycle.
248 * Any additional logic will usually just slow down the backup process.
250 * Using the pipelined mode, I get a constant over 400 KBps throughput,
251 * which seems to be the maximum throughput supported by my tape.
253 * However, there are some downfalls:
255 * 1. We use memory (for data buffers) in proportional to the number
256 * of pipeline stages (each stage is about 26 KB with my tape).
257 * 2. In the pipelined write mode, we cheat and postpone error codes
258 * to the user task. In read mode, the actual tape position
259 * will be a bit further than the last requested block.
261 * Concerning (1):
263 * 1. We allocate stages dynamically only when we need them. When
264 * we don't need them, we don't consume additional memory. In
265 * case we can't allocate stages, we just manage without them
266 * (at the expense of decreased throughput) so when Linux is
267 * tight in memory, we will not pose additional difficulties.
269 * 2. The maximum number of stages (which is, in fact, the maximum
270 * amount of memory) which we allocate is limited by the compile
271 * time parameter IDETAPE_MAX_PIPELINE_STAGES.
273 * 3. The maximum number of stages is a controlled parameter - We
274 * don't start from the user defined maximum number of stages
275 * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
276 * will not even allocate this amount of stages if the user
277 * program can't handle the speed). We then implement a feedback
278 * loop which checks if the pipeline is empty, and if it is, we
279 * increase the maximum number of stages as necessary until we
280 * reach the optimum value which just manages to keep the tape
281 * busy with minimum allocated memory or until we reach
282 * IDETAPE_MAX_PIPELINE_STAGES.
284 * Concerning (2):
286 * In pipelined write mode, ide-tape can not return accurate error codes
287 * to the user program since we usually just add the request to the
288 * pipeline without waiting for it to be serviced. In case an error
289 * occurs, I will report it on the next user request.
291 * In the pipelined read mode, subsequent read requests or forward
292 * filemark spacing will perform correctly, as we preserve all blocks
293 * and filemarks which we encountered during our excess read-ahead.
295 * For accurate tape positioning and error reporting, disabling
296 * pipelined mode might be the best option.
298 * You can enable/disable/tune the pipelined operation mode by adjusting
299 * the compile time parameters below.
303 * Possible improvements.
305 * 1. Support for the ATAPI overlap protocol.
307 * In order to maximize bus throughput, we currently use the DSC
308 * overlap method which enables ide.c to service requests from the
309 * other device while the tape is busy executing a command. The
310 * DSC overlap method involves polling the tape's status register
311 * for the DSC bit, and servicing the other device while the tape
312 * isn't ready.
314 * In the current QIC development standard (December 1995),
315 * it is recommended that new tape drives will *in addition*
316 * implement the ATAPI overlap protocol, which is used for the
317 * same purpose - efficient use of the IDE bus, but is interrupt
318 * driven and thus has much less CPU overhead.
320 * ATAPI overlap is likely to be supported in most new ATAPI
321 * devices, including new ATAPI cdroms, and thus provides us
322 * a method by which we can achieve higher throughput when
323 * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
326 #define IDETAPE_VERSION "1.13"
328 #include <linux/config.h>
329 #include <linux/module.h>
330 #include <linux/types.h>
331 #include <linux/string.h>
332 #include <linux/kernel.h>
333 #include <linux/delay.h>
334 #include <linux/timer.h>
335 #include <linux/mm.h>
336 #include <linux/interrupt.h>
337 #include <linux/major.h>
338 #include <linux/errno.h>
339 #include <linux/genhd.h>
340 #include <linux/malloc.h>
341 #include <linux/pci.h>
342 #include <linux/ide.h>
344 #include <asm/byteorder.h>
345 #include <asm/irq.h>
346 #include <asm/uaccess.h>
347 #include <asm/io.h>
348 #include <asm/unaligned.h>
349 #include <asm/bitops.h>
352 * For general magnetic tape device compatibility.
354 #include <linux/mtio.h>
356 /**************************** Tunable parameters *****************************/
359 * Pipelined mode parameters.
361 * We try to use the minimum number of stages which is enough to
362 * keep the tape constantly streaming. To accomplish that, we implement
363 * a feedback loop around the maximum number of stages:
365 * We start from MIN maximum stages (we will not even use MIN stages
366 * if we don't need them), increment it by RATE*(MAX-MIN)
367 * whenever we sense that the pipeline is empty, until we reach
368 * the optimum value or until we reach MAX.
370 * Setting the following parameter to 0 will disable the pipelined mode.
372 #define IDETAPE_MIN_PIPELINE_STAGES 100
373 #define IDETAPE_MAX_PIPELINE_STAGES 200
374 #define IDETAPE_INCREASE_STAGES_RATE 20
377 * Assuming the tape shares an interface with another device, the default
378 * behavior is to service our pending pipeline requests as soon as
379 * possible, but to gracefully postpone them in favor of the other device
380 * when the tape is busy. This has the potential to maximize our
381 * throughput and in the same time, to make efficient use of the IDE bus.
383 * Note that when we transfer data to / from the tape, we co-operate with
384 * the relatively fast tape buffers and the tape will perform the
385 * actual media access in the background, without blocking the IDE
386 * bus. This means that as long as the maximum IDE bus throughput is much
387 * higher than the sum of our maximum throughput and the maximum
388 * throughput of the other device, we should probably leave the default
389 * behavior.
391 * However, if it is still desired to give the other device a share even
392 * in our own (small) bus bandwidth, you can set IDETAPE_LOW_TAPE_PRIORITY
393 * to 1. This will let the other device finish *all* its pending requests
394 * before we even check if we can service our next pending request.
396 #define IDETAPE_LOW_TAPE_PRIORITY 0
399 * The following are used to debug the driver:
401 * Setting IDETAPE_INFO_LOG to 1 will log driver vender information.
402 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
403 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
404 * some places.
406 * Setting them to 0 will restore normal operation mode:
408 * 1. Disable logging normal successful operations.
409 * 2. Disable self-sanity checks.
410 * 3. Errors will still be logged, of course.
412 * All the #if DEBUG code will be removed some day, when the driver
413 * is verified to be stable enough. This will make it much more
414 * esthetic.
416 #define IDETAPE_INFO_LOG 0
417 #define IDETAPE_DEBUG_LOG 0
418 #define IDETAPE_DEBUG_BUGS 1
420 #if IDETAPE_DEBUG_LOG
421 #undef IDETAPE_INFO_LOG
422 #define IDETAPE_INFO_LOG IDETAPE_DEBUG_LOG
423 #endif
426 * After each failed packet command we issue a request sense command
427 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
429 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
431 #define IDETAPE_MAX_PC_RETRIES 3
434 * With each packet command, we allocate a buffer of
435 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
436 * commands (Not for READ/WRITE commands).
438 #define IDETAPE_PC_BUFFER_SIZE 256
441 * In various places in the driver, we need to allocate storage
442 * for packet commands and requests, which will remain valid while
443 * we leave the driver to wait for an interrupt or a timeout event.
445 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
448 * DSC polling parameters.
450 * Polling for DSC (a single bit in the status register) is a very
451 * important function in ide-tape. There are two cases in which we
452 * poll for DSC:
454 * 1. Before a read/write packet command, to ensure that we
455 * can transfer data from/to the tape's data buffers, without
456 * causing an actual media access. In case the tape is not
457 * ready yet, we take out our request from the device
458 * request queue, so that ide.c will service requests from
459 * the other device on the same interface meanwhile.
461 * 2. After the successful initialization of a "media access
462 * packet command", which is a command which can take a long
463 * time to complete (it can be several seconds or even an hour).
465 * Again, we postpone our request in the middle to free the bus
466 * for the other device. The polling frequency here should be
467 * lower than the read/write frequency since those media access
468 * commands are slow. We start from a "fast" frequency -
469 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
470 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
471 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
473 * We also set a timeout for the timer, in case something goes wrong.
474 * The timeout should be longer then the maximum execution time of a
475 * tape operation.
479 * The following parameter is used to select the point in the internal
480 * tape fifo in which we will start to refill the buffer. Decreasing
481 * the following parameter will improve the system's latency and
482 * interactive response, while using a high value might improve sytem
483 * throughput.
485 #define IDETAPE_FIFO_THRESHOLD 2
488 * Some tape drives require a long irq timeout
490 #define IDETAPE_WAIT_CMD (60*HZ)
493 * DSC timings.
495 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
496 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
497 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
498 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
499 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
500 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
501 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
503 /*************************** End of tunable parameters ***********************/
505 typedef enum {
506 idetape_direction_none,
507 idetape_direction_read,
508 idetape_direction_write
509 } idetape_chrdev_direction_t;
512 * Our view of a packet command.
514 typedef struct idetape_packet_command_s {
515 u8 c[12]; /* Actual packet bytes */
516 int retries; /* On each retry, we increment retries */
517 int error; /* Error code */
518 int request_transfer; /* Bytes to transfer */
519 int actually_transferred; /* Bytes actually transferred */
520 int buffer_size; /* Size of our data buffer */
521 struct buffer_head *bh;
522 char *b_data;
523 int b_count;
524 byte *buffer; /* Data buffer */
525 byte *current_position; /* Pointer into the above buffer */
526 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
527 byte pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
528 unsigned int flags; /* Status/Action bit flags */
529 } idetape_pc_t;
532 * Packet command flag bits.
534 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
535 #define PC_WAIT_FOR_DSC 1 /* 1 When polling for DSC on a media access command */
536 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
537 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
538 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
539 #define PC_WRITING 5 /* Data direction */
542 * Capabilities and Mechanical Status Page
544 typedef struct {
545 unsigned page_code :6; /* Page code - Should be 0x2a */
546 unsigned reserved1_67 :2;
547 u8 page_length; /* Page Length - Should be 0x12 */
548 u8 reserved2, reserved3;
549 unsigned ro :1; /* Read Only Mode */
550 unsigned reserved4_1234 :4;
551 unsigned sprev :1; /* Supports SPACE in the reverse direction */
552 unsigned reserved4_67 :2;
553 unsigned reserved5_012 :3;
554 unsigned efmt :1; /* Supports ERASE command initiated formatting */
555 unsigned reserved5_4 :1;
556 unsigned qfa :1; /* Supports the QFA two partition formats */
557 unsigned reserved5_67 :2;
558 unsigned lock :1; /* Supports locking the volume */
559 unsigned locked :1; /* The volume is locked */
560 unsigned prevent :1; /* The device defaults in the prevent state after power up */
561 unsigned eject :1; /* The device can eject the volume */
562 unsigned reserved6_45 :2; /* Reserved */
563 unsigned ecc :1; /* Supports error correction */
564 unsigned cmprs :1; /* Supports data compression */
565 unsigned reserved7_0 :1;
566 unsigned blk512 :1; /* Supports 512 bytes block size */
567 unsigned blk1024 :1; /* Supports 1024 bytes block size */
568 unsigned reserved7_3_6 :4;
569 unsigned slowb :1; /* The device restricts the byte count for PIO */
570 /* transfers for slow buffer memory ??? */
571 u16 max_speed; /* Maximum speed supported in KBps */
572 u8 reserved10, reserved11;
573 u16 ctl; /* Continuous Transfer Limit in blocks */
574 u16 speed; /* Current Speed, in KBps */
575 u16 buffer_size; /* Buffer Size, in 512 bytes */
576 u8 reserved18, reserved19;
577 } idetape_capabilities_page_t;
580 * A pipeline stage.
582 typedef struct idetape_stage_s {
583 struct request rq; /* The corresponding request */
584 struct buffer_head *bh; /* The data buffers */
585 struct idetape_stage_s *next; /* Pointer to the next stage */
586 } idetape_stage_t;
589 * Most of our global data which we need to save even as we leave the
590 * driver due to an interrupt or a timer event is stored in a variable
591 * of type idetape_tape_t, defined below.
593 typedef struct {
594 ide_drive_t *drive;
597 * Since a typical character device operation requires more
598 * than one packet command, we provide here enough memory
599 * for the maximum of interconnected packet commands.
600 * The packet commands are stored in the circular array pc_stack.
601 * pc_stack_index points to the last used entry, and warps around
602 * to the start when we get to the last array entry.
604 * pc points to the current processed packet command.
606 * failed_pc points to the last failed packet command, or contains
607 * NULL if we do not need to retry any packet command. This is
608 * required since an additional packet command is needed before the
609 * retry, to get detailed information on what went wrong.
611 idetape_pc_t *pc; /* Current packet command */
612 idetape_pc_t *failed_pc; /* Last failed packet command */
613 idetape_pc_t pc_stack[IDETAPE_PC_STACK];/* Packet command stack */
614 int pc_stack_index; /* Next free packet command storage space */
615 struct request rq_stack[IDETAPE_PC_STACK];
616 int rq_stack_index; /* We implement a circular array */
619 * DSC polling variables.
621 * While polling for DSC we use postponed_rq to postpone the
622 * current request so that ide.c will be able to service
623 * pending requests on the other device. Note that at most
624 * we will have only one DSC (usually data transfer) request
625 * in the device request queue. Additional requests can be
626 * queued in our internal pipeline, but they will be visible
627 * to ide.c only one at a time.
629 struct request *postponed_rq;
630 unsigned long dsc_polling_start; /* The time in which we started polling for DSC */
631 struct timer_list dsc_timer; /* Timer used to poll for dsc */
632 unsigned long best_dsc_rw_frequency; /* Read/Write dsc polling frequency */
633 unsigned long dsc_polling_frequency; /* The current polling frequency */
634 unsigned long dsc_timeout; /* Maximum waiting time */
637 * Position information
639 byte partition;
640 unsigned int block_address; /* Current block */
643 * Last error information
645 byte sense_key, asc, ascq;
648 * Character device operation
650 unsigned int minor;
651 char name[4]; /* device name */
652 idetape_chrdev_direction_t chrdev_direction; /* Current character device data transfer direction */
655 * Device information
657 unsigned short tape_block_size; /* Usually 512 or 1024 bytes */
658 int user_bs_factor;
659 idetape_capabilities_page_t capabilities; /* Copy of the tape's Capabilities and Mechanical Page */
662 * Active data transfer request parameters.
664 * At most, there is only one ide-tape originated data transfer
665 * request in the device request queue. This allows ide.c to
666 * easily service requests from the other device when we
667 * postpone our active request. In the pipelined operation
668 * mode, we use our internal pipeline structure to hold
669 * more data requests.
671 * The data buffer size is chosen based on the tape's
672 * recommendation.
674 struct request *active_data_request; /* Pointer to the request which is waiting in the device request queue */
675 int stage_size; /* Data buffer size (chosen based on the tape's recommendation */
676 idetape_stage_t *merge_stage;
677 int merge_stage_size;
678 struct buffer_head *bh;
679 char *b_data;
680 int b_count;
683 * Pipeline parameters.
685 * To accomplish non-pipelined mode, we simply set the following
686 * variables to zero (or NULL, where appropriate).
688 int nr_stages; /* Number of currently used stages */
689 int nr_pending_stages; /* Number of pending stages */
690 int max_stages, min_pipeline, max_pipeline; /* We will not allocate more than this number of stages */
691 idetape_stage_t *first_stage; /* The first stage which will be removed from the pipeline */
692 idetape_stage_t *active_stage; /* The currently active stage */
693 idetape_stage_t *next_stage; /* Will be serviced after the currently active request */
694 idetape_stage_t *last_stage; /* New requests will be added to the pipeline here */
695 idetape_stage_t *cache_stage; /* Optional free stage which we can use */
696 int pages_per_stage;
697 int excess_bh_size; /* Wasted space in each stage */
699 unsigned int flags; /* Status/Action flags */
700 } idetape_tape_t;
703 * Tape flag bits values.
705 #define IDETAPE_IGNORE_DSC 0
706 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
707 #define IDETAPE_BUSY 2 /* Device already opened */
708 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
709 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
710 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
711 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
714 * Supported ATAPI tape drives packet commands
716 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
717 #define IDETAPE_REWIND_CMD 0x01
718 #define IDETAPE_REQUEST_SENSE_CMD 0x03
719 #define IDETAPE_READ_CMD 0x08
720 #define IDETAPE_WRITE_CMD 0x0a
721 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
722 #define IDETAPE_SPACE_CMD 0x11
723 #define IDETAPE_INQUIRY_CMD 0x12
724 #define IDETAPE_ERASE_CMD 0x19
725 #define IDETAPE_MODE_SENSE_CMD 0x1a
726 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
727 #define IDETAPE_LOCATE_CMD 0x2b
728 #define IDETAPE_READ_POSITION_CMD 0x34
731 * Some defines for the SPACE command
733 #define IDETAPE_SPACE_OVER_FILEMARK 1
734 #define IDETAPE_SPACE_TO_EOD 3
737 * Some defines for the LOAD UNLOAD command
739 #define IDETAPE_LU_LOAD_MASK 1
740 #define IDETAPE_LU_RETENSION_MASK 2
741 #define IDETAPE_LU_EOT_MASK 4
744 * Special requests for our block device strategy routine.
746 * In order to service a character device command, we add special
747 * requests to the tail of our block device request queue and wait
748 * for their completion.
751 #define IDETAPE_FIRST_RQ 90
754 * IDETAPE_PC_RQ is used to queue a packet command in the request queue.
756 #define IDETAPE_PC_RQ1 90
757 #define IDETAPE_PC_RQ2 91
760 * IDETAPE_READ_RQ and IDETAPE_WRITE_RQ are used by our
761 * character device interface to request read/write operations from
762 * our block device interface.
764 #define IDETAPE_READ_RQ 92
765 #define IDETAPE_WRITE_RQ 93
766 #define IDETAPE_ABORTED_WRITE_RQ 94
768 #define IDETAPE_LAST_RQ 94
771 * A macro which can be used to check if a we support a given
772 * request command.
774 #define IDETAPE_RQ_CMD(cmd) ((cmd >= IDETAPE_FIRST_RQ) && (cmd <= IDETAPE_LAST_RQ))
777 * Error codes which are returned in rq->errors to the higher part
778 * of the driver.
780 #define IDETAPE_ERROR_GENERAL 101
781 #define IDETAPE_ERROR_FILEMARK 102
782 #define IDETAPE_ERROR_EOD 103
785 * The ATAPI Status Register.
787 typedef union {
788 unsigned all :8;
789 struct {
790 unsigned check :1; /* Error occurred */
791 unsigned idx :1; /* Reserved */
792 unsigned corr :1; /* Correctable error occurred */
793 unsigned drq :1; /* Data is request by the device */
794 unsigned dsc :1; /* Buffer availability / Media access command finished */
795 unsigned reserved5 :1; /* Reserved */
796 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
797 unsigned bsy :1; /* The device has access to the command block */
798 } b;
799 } idetape_status_reg_t;
802 * The ATAPI error register.
804 typedef union {
805 unsigned all :8;
806 struct {
807 unsigned ili :1; /* Illegal Length Indication */
808 unsigned eom :1; /* End Of Media Detected */
809 unsigned abrt :1; /* Aborted command - As defined by ATA */
810 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
811 unsigned sense_key :4; /* Sense key of the last failed packet command */
812 } b;
813 } idetape_error_reg_t;
816 * ATAPI Feature Register
818 typedef union {
819 unsigned all :8;
820 struct {
821 unsigned dma :1; /* Using DMA of PIO */
822 unsigned reserved321 :3; /* Reserved */
823 unsigned reserved654 :3; /* Reserved (Tag Type) */
824 unsigned reserved7 :1; /* Reserved */
825 } b;
826 } idetape_feature_reg_t;
829 * ATAPI Byte Count Register.
831 typedef union {
832 unsigned all :16;
833 struct {
834 unsigned low :8; /* LSB */
835 unsigned high :8; /* MSB */
836 } b;
837 } idetape_bcount_reg_t;
840 * ATAPI Interrupt Reason Register.
842 typedef union {
843 unsigned all :8;
844 struct {
845 unsigned cod :1; /* Information transferred is command (1) or data (0) */
846 unsigned io :1; /* The device requests us to read (1) or write (0) */
847 unsigned reserved :6; /* Reserved */
848 } b;
849 } idetape_ireason_reg_t;
852 * ATAPI Drive Select Register
854 typedef union {
855 unsigned all :8;
856 struct {
857 unsigned sam_lun :4; /* Should be zero with ATAPI (not used) */
858 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
859 unsigned one5 :1; /* Should be set to 1 */
860 unsigned reserved6 :1; /* Reserved */
861 unsigned one7 :1; /* Should be set to 1 */
862 } b;
863 } idetape_drivesel_reg_t;
866 * ATAPI Device Control Register
868 typedef union {
869 unsigned all :8;
870 struct {
871 unsigned zero0 :1; /* Should be set to zero */
872 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
873 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
874 unsigned one3 :1; /* Should be set to 1 */
875 unsigned reserved4567 :4; /* Reserved */
876 } b;
877 } idetape_control_reg_t;
880 * idetape_chrdev_t provides the link between out character device
881 * interface and our block device interface and the corresponding
882 * ide_drive_t structure.
884 typedef struct {
885 ide_drive_t *drive;
886 } idetape_chrdev_t;
889 * The following is used to format the general configuration word of
890 * the ATAPI IDENTIFY DEVICE command.
892 struct idetape_id_gcw {
893 unsigned packet_size :2; /* Packet Size */
894 unsigned reserved234 :3; /* Reserved */
895 unsigned drq_type :2; /* Command packet DRQ type */
896 unsigned removable :1; /* Removable media */
897 unsigned device_type :5; /* Device type */
898 unsigned reserved13 :1; /* Reserved */
899 unsigned protocol :2; /* Protocol type */
903 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
905 typedef struct {
906 unsigned device_type :5; /* Peripheral Device Type */
907 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
908 unsigned reserved1_6t0 :7; /* Reserved */
909 unsigned rmb :1; /* Removable Medium Bit */
910 unsigned ansi_version :3; /* ANSI Version */
911 unsigned ecma_version :3; /* ECMA Version */
912 unsigned iso_version :2; /* ISO Version */
913 unsigned response_format :4; /* Response Data Format */
914 unsigned reserved3_45 :2; /* Reserved */
915 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
916 unsigned reserved3_7 :1; /* AENC - Reserved */
917 u8 additional_length; /* Additional Length (total_length-4) */
918 u8 rsv5, rsv6, rsv7; /* Reserved */
919 u8 vendor_id[8]; /* Vendor Identification */
920 u8 product_id[16]; /* Product Identification */
921 u8 revision_level[4]; /* Revision Level */
922 u8 vendor_specific[20]; /* Vendor Specific - Optional */
923 u8 reserved56t95[40]; /* Reserved - Optional */
924 /* Additional information may be returned */
925 } idetape_inquiry_result_t;
928 * READ POSITION packet command - Data Format (From Table 6-57)
930 typedef struct {
931 unsigned reserved0_10 :2; /* Reserved */
932 unsigned bpu :1; /* Block Position Unknown */
933 unsigned reserved0_543 :3; /* Reserved */
934 unsigned eop :1; /* End Of Partition */
935 unsigned bop :1; /* Beginning Of Partition */
936 u8 partition; /* Partition Number */
937 u8 reserved2, reserved3; /* Reserved */
938 u32 first_block; /* First Block Location */
939 u32 last_block; /* Last Block Location (Optional) */
940 u8 reserved12; /* Reserved */
941 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
942 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
943 } idetape_read_position_result_t;
946 * REQUEST SENSE packet command result - Data Format.
948 typedef struct {
949 unsigned error_code :7; /* Current of deferred errors */
950 unsigned valid :1; /* The information field conforms to QIC-157C */
951 u8 reserved1 :8; /* Segment Number - Reserved */
952 unsigned sense_key :4; /* Sense Key */
953 unsigned reserved2_4 :1; /* Reserved */
954 unsigned ili :1; /* Incorrect Length Indicator */
955 unsigned eom :1; /* End Of Medium */
956 unsigned filemark :1; /* Filemark */
957 u32 information __attribute__ ((packed));
958 u8 asl; /* Additional sense length (n-7) */
959 u32 command_specific; /* Additional command specific information */
960 u8 asc; /* Additional Sense Code */
961 u8 ascq; /* Additional Sense Code Qualifier */
962 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
963 unsigned sk_specific1 :7; /* Sense Key Specific */
964 unsigned sksv :1; /* Sense Key Specific information is valid */
965 u8 sk_specific2; /* Sense Key Specific */
966 u8 sk_specific3; /* Sense Key Specific */
967 u8 pad[2]; /* Padding to 20 bytes */
968 } idetape_request_sense_result_t;
971 * Follows structures which are related to the SELECT SENSE / MODE SENSE
972 * packet commands. Those packet commands are still not supported
973 * by ide-tape.
975 #define IDETAPE_CAPABILITIES_PAGE 0x2a
978 * Mode Parameter Header for the MODE SENSE packet command
980 typedef struct {
981 u8 mode_data_length; /* Length of the following data transfer */
982 u8 medium_type; /* Medium Type */
983 u8 dsp; /* Device Specific Parameter */
984 u8 bdl; /* Block Descriptor Length */
985 } idetape_mode_parameter_header_t;
988 * Mode Parameter Block Descriptor the MODE SENSE packet command
990 * Support for block descriptors is optional.
992 typedef struct {
993 u8 density_code; /* Medium density code */
994 u8 blocks[3]; /* Number of blocks */
995 u8 reserved4; /* Reserved */
996 u8 length[3]; /* Block Length */
997 } idetape_parameter_block_descriptor_t;
1000 * The Data Compression Page, as returned by the MODE SENSE packet command.
1002 typedef struct {
1003 unsigned page_code :6; /* Page Code - Should be 0xf */
1004 unsigned reserved0 :1; /* Reserved */
1005 unsigned ps :1;
1006 u8 page_length; /* Page Length - Should be 14 */
1007 unsigned reserved2 :6; /* Reserved */
1008 unsigned dcc :1; /* Data Compression Capable */
1009 unsigned dce :1; /* Data Compression Enable */
1010 unsigned reserved3 :5; /* Reserved */
1011 unsigned red :2; /* Report Exception on Decompression */
1012 unsigned dde :1; /* Data Decompression Enable */
1013 u32 ca; /* Compression Algorithm */
1014 u32 da; /* Decompression Algorithm */
1015 u8 reserved[4]; /* Reserved */
1016 } idetape_data_compression_page_t;
1019 * The Medium Partition Page, as returned by the MODE SENSE packet command.
1021 typedef struct {
1022 unsigned page_code :6; /* Page Code - Should be 0x11 */
1023 unsigned reserved1_6 :1; /* Reserved */
1024 unsigned ps :1;
1025 u8 page_length; /* Page Length - Should be 6 */
1026 u8 map; /* Maximum Additional Partitions - Should be 0 */
1027 u8 apd; /* Additional Partitions Defined - Should be 0 */
1028 unsigned reserved4_012 :3; /* Reserved */
1029 unsigned psum :2; /* Should be 0 */
1030 unsigned idp :1; /* Should be 0 */
1031 unsigned sdp :1; /* Should be 0 */
1032 unsigned fdp :1; /* Fixed Data Partitions */
1033 u8 mfr; /* Medium Format Recognition */
1034 u8 reserved[2]; /* Reserved */
1035 } idetape_medium_partition_page_t;
1038 * Run time configurable parameters.
1040 typedef struct {
1041 int dsc_rw_frequency;
1042 int dsc_media_access_frequency;
1043 int nr_stages;
1044 } idetape_config_t;
1047 * The variables below are used for the character device interface.
1048 * Additional state variables are defined in our ide_drive_t structure.
1050 static idetape_chrdev_t idetape_chrdevs[MAX_HWIFS * MAX_DRIVES];
1051 static int idetape_chrdev_present = 0;
1054 * Too bad. The drive wants to send us data which we are not ready to accept.
1055 * Just throw it away.
1057 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1059 while (bcount--)
1060 IN_BYTE (IDE_DATA_REG);
1063 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1065 struct buffer_head *bh = pc->bh;
1066 int count;
1068 while (bcount) {
1069 #if IDETAPE_DEBUG_BUGS
1070 if (bh == NULL) {
1071 printk (KERN_ERR "ide-tape: bh == NULL in idetape_input_buffers\n");
1072 idetape_discard_data (drive, bcount);
1073 return;
1075 #endif /* IDETAPE_DEBUG_BUGS */
1076 count = IDE_MIN (bh->b_size - bh->b_count, bcount);
1077 atapi_input_bytes (drive, bh->b_data + bh->b_count, count);
1078 bcount -= count; bh->b_count += count;
1079 if (bh->b_count == bh->b_size) {
1080 bh = bh->b_reqnext;
1081 if (bh)
1082 bh->b_count = 0;
1085 pc->bh = bh;
1088 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1090 struct buffer_head *bh = pc->bh;
1091 int count;
1093 while (bcount) {
1094 #if IDETAPE_DEBUG_BUGS
1095 if (bh == NULL) {
1096 printk (KERN_ERR "ide-tape: bh == NULL in idetape_output_buffers\n");
1097 return;
1099 #endif /* IDETAPE_DEBUG_BUGS */
1100 count = IDE_MIN (pc->b_count, bcount);
1101 atapi_output_bytes (drive, pc->b_data, count);
1102 bcount -= count; pc->b_data += count; pc->b_count -= count;
1103 if (!pc->b_count) {
1104 pc->bh = bh = bh->b_reqnext;
1105 if (bh) {
1106 pc->b_data = bh->b_data;
1107 pc->b_count = bh->b_count;
1113 #ifdef CONFIG_BLK_DEV_IDEDMA
1114 static void idetape_update_buffers (idetape_pc_t *pc)
1116 struct buffer_head *bh = pc->bh;
1117 int count, bcount = pc->actually_transferred;
1119 if (test_bit (PC_WRITING, &pc->flags))
1120 return;
1121 while (bcount) {
1122 #if IDETAPE_DEBUG_BUGS
1123 if (bh == NULL) {
1124 printk (KERN_ERR "ide-tape: bh == NULL in idetape_update_buffers\n");
1125 return;
1127 #endif /* IDETAPE_DEBUG_BUGS */
1128 count = IDE_MIN (bh->b_size, bcount);
1129 bh->b_count = count;
1130 if (bh->b_count == bh->b_size)
1131 bh = bh->b_reqnext;
1132 bcount -= count;
1134 pc->bh = bh;
1136 #endif /* CONFIG_BLK_DEV_IDEDMA */
1139 * idetape_postpone_request postpones the current request so that
1140 * ide.c will be able to service requests from another device on
1141 * the same hwgroup while we are polling for DSC.
1143 static void idetape_postpone_request (ide_drive_t *drive)
1145 idetape_tape_t *tape = drive->driver_data;
1147 tape->postponed_rq = HWGROUP(drive)->rq;
1148 ide_stall_queue(drive, tape->dsc_polling_frequency);
1152 * idetape_queue_pc_head generates a new packet command request in front
1153 * of the request queue, before the current request, so that it will be
1154 * processed immediately, on the next pass through the driver.
1156 * idetape_queue_pc_head is called from the request handling part of
1157 * the driver (the "bottom" part). Safe storage for the request should
1158 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1159 * before calling idetape_queue_pc_head.
1161 * Memory for those requests is pre-allocated at initialization time, and
1162 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1163 * space for the maximum possible number of inter-dependent packet commands.
1165 * The higher level of the driver - The ioctl handler and the character
1166 * device handling functions should queue request to the lower level part
1167 * and wait for their completion using idetape_queue_pc_tail or
1168 * idetape_queue_rw_tail.
1170 static void idetape_queue_pc_head (ide_drive_t *drive,idetape_pc_t *pc,struct request *rq)
1172 ide_init_drive_cmd (rq);
1173 rq->buffer = (char *) pc;
1174 rq->cmd = IDETAPE_PC_RQ1;
1175 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
1179 * idetape_next_pc_storage returns a pointer to a place in which we can
1180 * safely store a packet command, even though we intend to leave the
1181 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1182 * commands is allocated at initialization time.
1184 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1186 idetape_tape_t *tape = drive->driver_data;
1188 #if IDETAPE_DEBUG_LOG
1189 printk (KERN_INFO "ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
1190 #endif /* IDETAPE_DEBUG_LOG */
1191 if (tape->pc_stack_index==IDETAPE_PC_STACK)
1192 tape->pc_stack_index=0;
1193 return (&tape->pc_stack[tape->pc_stack_index++]);
1197 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1198 * Since we queue packet commands in the request queue, we need to
1199 * allocate a request, along with the allocation of a packet command.
1202 /**************************************************************
1204 * This should get fixed to use kmalloc(GFP_ATOMIC, ..) *
1205 * followed later on by kfree(). -ml *
1207 **************************************************************/
1209 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1211 idetape_tape_t *tape = drive->driver_data;
1213 #if IDETAPE_DEBUG_LOG
1214 printk (KERN_INFO "ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
1215 #endif /* IDETAPE_DEBUG_LOG */
1216 if (tape->rq_stack_index==IDETAPE_PC_STACK)
1217 tape->rq_stack_index=0;
1218 return (&tape->rq_stack[tape->rq_stack_index++]);
1222 * Pipeline related functions
1225 static inline int idetape_pipeline_active (idetape_tape_t *tape)
1227 return tape->active_data_request != NULL;
1231 * idetape_kfree_stage calls kfree to completely free a stage, along with
1232 * its related buffers.
1234 static void __idetape_kfree_stage (idetape_stage_t *stage)
1236 struct buffer_head *prev_bh, *bh = stage->bh;
1237 int size;
1239 while (bh != NULL) {
1240 if (bh->b_data != NULL) {
1241 size = (int) bh->b_size;
1242 while (size > 0) {
1243 free_page ((unsigned long) bh->b_data);
1244 size -= PAGE_SIZE;
1245 bh->b_data += PAGE_SIZE;
1248 prev_bh = bh;
1249 bh = bh->b_reqnext;
1250 kfree (prev_bh);
1252 kfree (stage);
1255 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1257 if (tape->cache_stage == NULL)
1258 tape->cache_stage = stage;
1259 else
1260 __idetape_kfree_stage (stage);
1264 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1265 * stage, along with all the necessary small buffers which together make
1266 * a buffer of size tape->stage_size (or a bit more). We attempt to
1267 * combine sequential pages as much as possible.
1269 * Returns a pointer to the new allocated stage, or NULL if we
1270 * can't (or don't want to) allocate a stage.
1272 * Pipeline stages are optional and are used to increase performance.
1273 * If we can't allocate them, we'll manage without them.
1275 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape)
1277 idetape_stage_t *stage;
1278 struct buffer_head *prev_bh, *bh;
1279 int pages = tape->pages_per_stage;
1280 char *b_data;
1282 if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
1283 return NULL;
1284 stage->next = NULL;
1286 bh = stage->bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL);
1287 if (bh == NULL)
1288 goto abort;
1289 bh->b_reqnext = NULL;
1290 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1291 goto abort;
1292 bh->b_size = PAGE_SIZE;
1293 set_bit (BH_Lock, &bh->b_state);
1295 while (--pages) {
1296 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1297 goto abort;
1298 if (bh->b_data == b_data + PAGE_SIZE && virt_to_bus (bh->b_data) == virt_to_bus (b_data) + PAGE_SIZE) {
1299 bh->b_size += PAGE_SIZE;
1300 bh->b_data -= PAGE_SIZE;
1301 continue;
1303 if (b_data == bh->b_data + bh->b_size && virt_to_bus (b_data) == virt_to_bus (bh->b_data) + bh->b_size) {
1304 bh->b_size += PAGE_SIZE;
1305 continue;
1307 prev_bh = bh;
1308 if ((bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL)) == NULL) {
1309 free_page ((unsigned long) b_data);
1310 goto abort;
1312 bh->b_reqnext = NULL;
1313 bh->b_data = b_data;
1314 bh->b_size = PAGE_SIZE;
1315 set_bit (BH_Lock, &bh->b_state);
1316 prev_bh->b_reqnext = bh;
1318 bh->b_size -= tape->excess_bh_size;
1319 return stage;
1320 abort:
1321 __idetape_kfree_stage (stage);
1322 return NULL;
1325 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1327 idetape_stage_t *cache_stage = tape->cache_stage;
1329 #if IDETAPE_DEBUG_LOG
1330 printk (KERN_INFO "Reached idetape_kmalloc_stage\n");
1331 #endif /* IDETAPE_DEBUG_LOG */
1333 if (tape->nr_stages >= tape->max_stages)
1334 return NULL;
1335 if (cache_stage != NULL) {
1336 tape->cache_stage = NULL;
1337 return cache_stage;
1339 return __idetape_kmalloc_stage (tape);
1342 static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char *buf, int n)
1344 struct buffer_head *bh = tape->bh;
1345 int count;
1347 while (n) {
1348 #if IDETAPE_DEBUG_BUGS
1349 if (bh == NULL) {
1350 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_from_user\n");
1351 return;
1353 #endif /* IDETAPE_DEBUG_BUGS */
1354 count = IDE_MIN (bh->b_size - bh->b_count, n);
1355 copy_from_user (bh->b_data + bh->b_count, buf, count);
1356 n -= count; bh->b_count += count; buf += count;
1357 if (bh->b_count == bh->b_size) {
1358 bh = bh->b_reqnext;
1359 if (bh)
1360 bh->b_count = 0;
1363 tape->bh = bh;
1366 static void idetape_copy_stage_to_user (idetape_tape_t *tape, char *buf, idetape_stage_t *stage, int n)
1368 struct buffer_head *bh = tape->bh;
1369 int count;
1371 while (n) {
1372 #if IDETAPE_DEBUG_BUGS
1373 if (bh == NULL) {
1374 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_to_user\n");
1375 return;
1377 #endif /* IDETAPE_DEBUG_BUGS */
1378 count = IDE_MIN (tape->b_count, n);
1379 copy_to_user (buf, tape->b_data, count);
1380 n -= count; tape->b_data += count; tape->b_count -= count; buf += count;
1381 if (!tape->b_count) {
1382 tape->bh = bh = bh->b_reqnext;
1383 if (bh) {
1384 tape->b_data = bh->b_data;
1385 tape->b_count = bh->b_count;
1391 static void idetape_init_merge_stage (idetape_tape_t *tape)
1393 struct buffer_head *bh = tape->merge_stage->bh;
1395 tape->bh = bh;
1396 if (tape->chrdev_direction == idetape_direction_write)
1397 bh->b_count = 0;
1398 else {
1399 tape->b_data = bh->b_data;
1400 tape->b_count = bh->b_count;
1404 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1406 struct buffer_head *tmp;
1408 tmp = stage->bh;
1409 stage->bh = tape->merge_stage->bh;
1410 tape->merge_stage->bh = tmp;
1411 idetape_init_merge_stage (tape);
1415 * idetape_increase_max_pipeline_stages is a part of the feedback
1416 * loop which tries to find the optimum number of stages. In the
1417 * feedback loop, we are starting from a minimum maximum number of
1418 * stages, and if we sense that the pipeline is empty, we try to
1419 * increase it, until we reach the user compile time memory limit.
1421 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1423 idetape_tape_t *tape = drive->driver_data;
1424 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1426 #if IDETAPE_DEBUG_LOG
1427 printk (KERN_INFO "Reached idetape_increase_max_pipeline_stages\n");
1428 #endif /* IDETAPE_DEBUG_LOG */
1430 tape->max_stages += increase;
1431 tape->max_stages = IDE_MAX(tape->max_stages, tape->min_pipeline);
1432 tape->max_stages = IDE_MIN(tape->max_stages, tape->max_pipeline);
1436 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
1438 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1440 idetape_tape_t *tape = drive->driver_data;
1441 unsigned long flags;
1443 #if IDETAPE_DEBUG_LOG
1444 printk (KERN_INFO "Reached idetape_add_stage_tail\n");
1445 #endif /* IDETAPE_DEBUG_LOG */
1446 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
1447 stage->next=NULL;
1448 if (tape->last_stage != NULL)
1449 tape->last_stage->next=stage;
1450 else
1451 tape->first_stage=tape->next_stage=stage;
1452 tape->last_stage=stage;
1453 if (tape->next_stage == NULL)
1454 tape->next_stage=tape->last_stage;
1455 tape->nr_stages++;
1456 tape->nr_pending_stages++;
1457 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
1461 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1462 * The caller should avoid race conditions.
1464 static void idetape_remove_stage_head (ide_drive_t *drive)
1466 idetape_tape_t *tape = drive->driver_data;
1467 idetape_stage_t *stage;
1469 #if IDETAPE_DEBUG_LOG
1470 printk (KERN_INFO "Reached idetape_remove_stage_head\n");
1471 #endif /* IDETAPE_DEBUG_LOG */
1472 #if IDETAPE_DEBUG_BUGS
1473 if (tape->first_stage == NULL) {
1474 printk (KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1475 return;
1477 if (tape->active_stage == tape->first_stage) {
1478 printk (KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1479 return;
1481 #endif /* IDETAPE_DEBUG_BUGS */
1482 stage=tape->first_stage;
1483 tape->first_stage=stage->next;
1484 idetape_kfree_stage (tape, stage);
1485 tape->nr_stages--;
1486 if (tape->first_stage == NULL) {
1487 tape->last_stage=NULL;
1488 #if IDETAPE_DEBUG_BUGS
1489 if (tape->next_stage != NULL)
1490 printk (KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1491 if (tape->nr_stages)
1492 printk (KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1493 #endif /* IDETAPE_DEBUG_BUGS */
1498 * idetape_active_next_stage will declare the next stage as "active".
1500 static void idetape_active_next_stage (ide_drive_t *drive)
1502 idetape_tape_t *tape = drive->driver_data;
1503 idetape_stage_t *stage=tape->next_stage;
1504 struct request *rq = &stage->rq;
1506 #if IDETAPE_DEBUG_LOG
1507 printk (KERN_INFO "Reached idetape_active_next_stage\n");
1508 #endif /* IDETAPE_DEBUG_LOG */
1509 #if IDETAPE_DEBUG_BUGS
1510 if (stage == NULL) {
1511 printk (KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1512 return;
1514 #endif /* IDETAPE_DEBUG_BUGS */
1516 rq->buffer = NULL;
1517 rq->bh = stage->bh;
1518 tape->active_data_request=rq;
1519 tape->active_stage=stage;
1520 tape->next_stage=stage->next;
1524 * idetape_insert_pipeline_into_queue is used to start servicing the
1525 * pipeline stages, starting from tape->next_stage.
1527 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
1529 idetape_tape_t *tape = drive->driver_data;
1531 if (tape->next_stage == NULL)
1532 return;
1533 if (!idetape_pipeline_active (tape)) {
1534 idetape_active_next_stage (drive);
1535 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
1539 static void idetape_abort_pipeline (ide_drive_t *drive)
1541 idetape_tape_t *tape = drive->driver_data;
1542 idetape_stage_t *stage = tape->next_stage;
1544 while (stage) {
1545 stage->rq.cmd = IDETAPE_ABORTED_WRITE_RQ;
1546 stage = stage->next;
1551 * idetape_end_request is used to finish servicing a request, and to
1552 * insert a pending pipeline request into the main device queue.
1554 static void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
1556 ide_drive_t *drive = hwgroup->drive;
1557 struct request *rq = hwgroup->rq;
1558 idetape_tape_t *tape = drive->driver_data;
1559 int error;
1561 #if IDETAPE_DEBUG_LOG
1562 printk (KERN_INFO "Reached idetape_end_request\n");
1563 #endif /* IDETAPE_DEBUG_LOG */
1565 switch (uptodate) {
1566 case 0: error = IDETAPE_ERROR_GENERAL; break;
1567 case 1: error = 0; break;
1568 default: error = uptodate;
1570 rq->errors = error;
1571 if (error)
1572 tape->failed_pc = NULL;
1574 if (tape->active_data_request == rq) { /* The request was a pipelined data transfer request */
1575 tape->active_stage = NULL;
1576 tape->active_data_request = NULL;
1577 tape->nr_pending_stages--;
1578 if (rq->cmd == IDETAPE_WRITE_RQ) {
1579 if (error) {
1580 set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1581 if (error == IDETAPE_ERROR_EOD)
1582 idetape_abort_pipeline (drive);
1584 idetape_remove_stage_head (drive);
1586 if (tape->next_stage != NULL) {
1587 idetape_active_next_stage (drive);
1590 * Insert the next request into the request queue.
1591 * The choice of using ide_next or ide_end is now left to the user.
1593 #if IDETAPE_LOW_TAPE_PRIORITY
1594 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
1595 #else
1596 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_next);
1597 #endif /* IDETAPE_LOW_TAPE_PRIORITY */
1598 } else if (!error)
1599 idetape_increase_max_pipeline_stages (drive);
1601 ide_end_drive_cmd (drive, 0, 0);
1605 * idetape_analyze_error is called on each failed packet command retry
1606 * to analyze the request sense. We currently do not utilize this
1607 * information.
1609 static void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
1611 idetape_tape_t *tape = drive->driver_data;
1612 idetape_pc_t *pc = tape->failed_pc;
1614 tape->sense_key = result->sense_key; tape->asc = result->asc; tape->ascq = result->ascq;
1615 #if IDETAPE_DEBUG_LOG
1617 * Without debugging, we only log an error if we decided to
1618 * give up retrying.
1620 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);
1621 #endif /* IDETAPE_DEBUG_LOG */
1623 #ifdef CONFIG_BLK_DEV_IDEDMA
1626 * Correct pc->actually_transferred by asking the tape.
1628 if (test_bit (PC_DMA_ERROR, &pc->flags)) {
1629 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl (get_unaligned (&result->information));
1630 idetape_update_buffers (pc);
1632 #endif /* CONFIG_BLK_DEV_IDEDMA */
1633 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1634 pc->error = IDETAPE_ERROR_FILEMARK;
1635 set_bit (PC_ABORT, &pc->flags);
1637 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1638 if (result->eom || (result->sense_key == 0xd && result->asc == 0x0 && result->ascq == 0x2)) {
1639 pc->error = IDETAPE_ERROR_EOD;
1640 set_bit (PC_ABORT, &pc->flags);
1643 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1644 if (result->sense_key == 8) {
1645 pc->error = IDETAPE_ERROR_EOD;
1646 set_bit (PC_ABORT, &pc->flags);
1648 if (!test_bit (PC_ABORT, &pc->flags) && pc->actually_transferred)
1649 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1653 static void idetape_request_sense_callback (ide_drive_t *drive)
1655 idetape_tape_t *tape = drive->driver_data;
1657 #if IDETAPE_DEBUG_LOG
1658 printk (KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1659 #endif /* IDETAPE_DEBUG_LOG */
1660 if (!tape->pc->error) {
1661 idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
1662 idetape_end_request (1,HWGROUP (drive));
1663 } else {
1664 printk (KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
1665 idetape_end_request (0,HWGROUP (drive));
1670 * idetape_init_pc initializes a packet command.
1672 static void idetape_init_pc (idetape_pc_t *pc)
1674 memset (pc->c, 0, 12);
1675 pc->retries = 0;
1676 pc->flags = 0;
1677 pc->request_transfer = 0;
1678 pc->buffer = pc->pc_buffer;
1679 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1680 pc->bh = NULL;
1681 pc->b_data = NULL;
1684 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1686 idetape_init_pc (pc);
1687 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1688 pc->c[4] = 255;
1689 pc->request_transfer = 18;
1690 pc->callback = &idetape_request_sense_callback;
1694 * idetape_retry_pc is called when an error was detected during the
1695 * last packet command. We queue a request sense packet command in
1696 * the head of the request list.
1698 static void idetape_retry_pc (ide_drive_t *drive)
1700 idetape_tape_t *tape = drive->driver_data;
1701 idetape_pc_t *pc;
1702 struct request *rq;
1703 idetape_error_reg_t error;
1705 error.all = IN_BYTE (IDE_ERROR_REG);
1706 pc = idetape_next_pc_storage (drive);
1707 rq = idetape_next_rq_storage (drive);
1708 idetape_create_request_sense_cmd (pc);
1709 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
1710 idetape_queue_pc_head (drive, pc, rq);
1714 * idetape_pc_intr is the usual interrupt handler which will be called
1715 * during a packet command. We will transfer some of the data (as
1716 * requested by the drive) and will re-point interrupt handler to us.
1717 * When data transfer is finished, we will act according to the
1718 * algorithm described before idetape_issue_packet_command.
1721 static void idetape_pc_intr (ide_drive_t *drive)
1723 idetape_tape_t *tape = drive->driver_data;
1724 idetape_status_reg_t status;
1725 idetape_bcount_reg_t bcount;
1726 idetape_ireason_reg_t ireason;
1727 idetape_pc_t *pc=tape->pc;
1728 unsigned int temp;
1730 #if IDETAPE_DEBUG_LOG
1731 printk (KERN_INFO "ide-tape: Reached idetape_pc_intr interrupt handler\n");
1732 #endif /* IDETAPE_DEBUG_LOG */
1734 #ifdef CONFIG_BLK_DEV_IDEDMA
1735 if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
1736 if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
1738 * A DMA error is sometimes expected. For example,
1739 * if the tape is crossing a filemark during a
1740 * READ command, it will issue an irq and position
1741 * itself before the filemark, so that only a partial
1742 * data transfer will occur (which causes the DMA
1743 * error). In that case, we will later ask the tape
1744 * how much bytes of the original request were
1745 * actually transferred (we can't receive that
1746 * information from the DMA engine on most chipsets).
1748 set_bit (PC_DMA_ERROR, &pc->flags);
1749 } else {
1750 pc->actually_transferred=pc->request_transfer;
1751 idetape_update_buffers (pc);
1753 #if IDETAPE_DEBUG_LOG
1754 printk (KERN_INFO "ide-tape: DMA finished\n");
1755 #endif /* IDETAPE_DEBUG_LOG */
1757 #endif /* CONFIG_BLK_DEV_IDEDMA */
1759 status.all = GET_STAT(); /* Clear the interrupt */
1761 if (!status.b.drq) { /* No more interrupts */
1762 #if IDETAPE_DEBUG_LOG
1763 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1764 #endif /* IDETAPE_DEBUG_LOG */
1765 clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
1767 ide__sti(); /* local CPU only */
1769 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1770 status.b.check = 0;
1771 if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
1772 #if IDETAPE_DEBUG_LOG
1773 printk (KERN_INFO "ide-tape: %s: I/O error, ",tape->name);
1774 #endif /* IDETAPE_DEBUG_LOG */
1775 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1776 printk (KERN_ERR "ide-tape: I/O error in request sense command\n");
1777 ide_do_reset (drive);
1778 return;
1780 idetape_retry_pc (drive); /* Retry operation */
1781 return;
1783 pc->error = 0;
1784 if (test_bit (PC_WAIT_FOR_DSC, &pc->flags) && !status.b.dsc) { /* Media access command */
1785 tape->dsc_polling_start = jiffies;
1786 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1787 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1788 idetape_postpone_request (drive); /* Allow ide.c to handle other requests */
1789 return;
1791 if (tape->failed_pc == pc)
1792 tape->failed_pc=NULL;
1793 pc->callback(drive); /* Command finished - Call the callback function */
1794 return;
1796 #ifdef CONFIG_BLK_DEV_IDEDMA
1797 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
1798 printk (KERN_ERR "ide-tape: The tape wants to issue more interrupts in DMA mode\n");
1799 printk (KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1800 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
1801 ide_do_reset (drive);
1802 return;
1804 #endif /* CONFIG_BLK_DEV_IDEDMA */
1805 bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
1806 bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
1807 ireason.all=IN_BYTE (IDE_IREASON_REG);
1809 if (ireason.b.cod) {
1810 printk (KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1811 ide_do_reset (drive);
1812 return;
1814 if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
1815 printk (KERN_ERR "ide-tape: We wanted to %s, ", ireason.b.io ? "Write":"Read");
1816 printk (KERN_ERR "but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
1817 ide_do_reset (drive);
1818 return;
1820 if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
1821 temp = pc->actually_transferred + bcount.all;
1822 if ( temp > pc->request_transfer) {
1823 if (temp > pc->buffer_size) {
1824 printk (KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1825 idetape_discard_data (drive,bcount.all);
1826 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD);
1827 return;
1829 #if IDETAPE_DEBUG_LOG
1830 printk (KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1831 #endif /* IDETAPE_DEBUG_LOG */
1834 if (test_bit (PC_WRITING, &pc->flags)) {
1835 if (pc->bh != NULL)
1836 idetape_output_buffers (drive, pc, bcount.all);
1837 else
1838 atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
1839 } else {
1840 if (pc->bh != NULL)
1841 idetape_input_buffers (drive, pc, bcount.all);
1842 else
1843 atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
1845 pc->actually_transferred+=bcount.all; /* Update the current position */
1846 pc->current_position+=bcount.all;
1848 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD); /* And set the interrupt handler again */
1852 * Packet Command Interface
1854 * The current Packet Command is available in tape->pc, and will not
1855 * change until we finish handling it. Each packet command is associated
1856 * with a callback function that will be called when the command is
1857 * finished.
1859 * The handling will be done in three stages:
1861 * 1. idetape_issue_packet_command will send the packet command to the
1862 * drive, and will set the interrupt handler to idetape_pc_intr.
1864 * 2. On each interrupt, idetape_pc_intr will be called. This step
1865 * will be repeated until the device signals us that no more
1866 * interrupts will be issued.
1868 * 3. ATAPI Tape media access commands have immediate status with a
1869 * delayed process. In case of a successful initiation of a
1870 * media access packet command, the DSC bit will be set when the
1871 * actual execution of the command is finished.
1872 * Since the tape drive will not issue an interrupt, we have to
1873 * poll for this event. In this case, we define the request as
1874 * "low priority request" by setting rq_status to
1875 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1876 * the driver.
1878 * ide.c will then give higher priority to requests which
1879 * originate from the other device, until will change rq_status
1880 * to RQ_ACTIVE.
1882 * 4. When the packet command is finished, it will be checked for errors.
1884 * 5. In case an error was found, we queue a request sense packet command
1885 * in front of the request queue and retry the operation up to
1886 * IDETAPE_MAX_PC_RETRIES times.
1888 * 6. In case no error was found, or we decided to give up and not
1889 * to retry again, the callback function will be called and then
1890 * we will handle the next request.
1894 static void idetape_transfer_pc(ide_drive_t *drive)
1896 idetape_tape_t *tape = drive->driver_data;
1897 idetape_pc_t *pc = tape->pc;
1898 idetape_ireason_reg_t ireason;
1899 int retries = 100;
1901 if (ide_wait_stat (drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1902 printk (KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1903 return;
1905 ireason.all=IN_BYTE (IDE_IREASON_REG);
1906 while (retries-- && (!ireason.b.cod || ireason.b.io)) {
1907 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, retrying\n");
1908 udelay(100);
1909 ireason.all = IN_BYTE(IDE_IREASON_REG);
1910 if (retries == 0) {
1911 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, ignoring\n");
1912 ireason.b.cod = 1;
1913 ireason.b.io = 0;
1916 if (!ireason.b.cod || ireason.b.io) {
1917 printk (KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
1918 ide_do_reset (drive);
1919 return;
1921 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD); /* Set the interrupt routine */
1922 atapi_output_bytes (drive,pc->c,12); /* Send the actual packet */
1925 static void idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1927 idetape_tape_t *tape = drive->driver_data;
1928 idetape_bcount_reg_t bcount;
1929 int dma_ok=0;
1931 #if IDETAPE_DEBUG_BUGS
1932 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1933 printk (KERN_ERR "ide-tape: possible ide-tape.c bug - Two request sense in serial were issued\n");
1935 #endif /* IDETAPE_DEBUG_BUGS */
1937 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1938 tape->failed_pc=pc;
1939 tape->pc=pc; /* Set the current packet command */
1941 if (pc->retries > IDETAPE_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
1943 * We will "abort" retrying a packet command in case
1944 * a legitimate error code was received (crossing a
1945 * filemark, or DMA error in the end of media, for
1946 * example).
1948 if (!test_bit (PC_ABORT, &pc->flags)) {
1949 printk (KERN_ERR "ide-tape: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1950 tape->name, pc->c[0], tape->sense_key, tape->asc, tape->ascq);
1951 pc->error = IDETAPE_ERROR_GENERAL; /* Giving up */
1953 tape->failed_pc=NULL;
1954 pc->callback(drive);
1955 return;
1957 #if IDETAPE_DEBUG_LOG
1958 printk (KERN_INFO "Retry number - %d\n",pc->retries);
1959 #endif /* IDETAPE_DEBUG_LOG */
1961 pc->retries++;
1962 pc->actually_transferred=0; /* We haven't transferred any data yet */
1963 pc->current_position=pc->buffer;
1964 bcount.all=pc->request_transfer; /* Request to transfer the entire buffer at once */
1966 #ifdef CONFIG_BLK_DEV_IDEDMA
1967 if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
1968 printk (KERN_WARNING "ide-tape: DMA disabled, reverting to PIO\n");
1969 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
1971 if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1972 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
1973 #endif /* CONFIG_BLK_DEV_IDEDMA */
1975 if (IDE_CONTROL_REG)
1976 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
1977 OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG); /* Use PIO/DMA */
1978 OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
1979 OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
1980 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
1981 #ifdef CONFIG_BLK_DEV_IDEDMA
1982 if (dma_ok) { /* Begin DMA, if necessary */
1983 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
1984 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1986 #endif /* CONFIG_BLK_DEV_IDEDMA */
1987 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1988 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD);
1989 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
1990 } else {
1991 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
1992 idetape_transfer_pc(drive);
1996 static void idetape_media_access_finished (ide_drive_t *drive)
1998 idetape_tape_t *tape = drive->driver_data;
1999 idetape_pc_t *pc = tape->pc;
2000 idetape_status_reg_t status;
2002 status.all = GET_STAT();
2003 if (status.b.dsc) {
2004 if (status.b.check) { /* Error detected */
2005 printk (KERN_ERR "ide-tape: %s: I/O error, ",tape->name);
2006 idetape_retry_pc (drive); /* Retry operation */
2007 return;
2009 pc->error = 0;
2010 if (tape->failed_pc == pc)
2011 tape->failed_pc = NULL;
2012 } else {
2013 pc->error = IDETAPE_ERROR_GENERAL;
2014 tape->failed_pc = NULL;
2016 pc->callback (drive);
2020 * General packet command callback function.
2022 static void idetape_pc_callback (ide_drive_t *drive)
2024 idetape_tape_t *tape = drive->driver_data;
2026 #if IDETAPE_DEBUG_LOG
2027 printk (KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2028 #endif /* IDETAPE_DEBUG_LOG */
2030 idetape_end_request (tape->pc->error ? 0:1, HWGROUP(drive));
2033 static void idetape_rw_callback (ide_drive_t *drive)
2035 idetape_tape_t *tape = drive->driver_data;
2036 struct request *rq = HWGROUP(drive)->rq;
2037 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2039 #if IDETAPE_DEBUG_LOG
2040 printk (KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2041 #endif /* IDETAPE_DEBUG_LOG */
2043 tape->block_address += blocks;
2044 rq->current_nr_sectors -= blocks;
2046 if (!tape->pc->error)
2047 idetape_end_request (1, HWGROUP (drive));
2048 else
2049 idetape_end_request (tape->pc->error, HWGROUP (drive));
2052 static void idetape_create_locate_cmd (idetape_pc_t *pc, unsigned int block, byte partition)
2054 idetape_init_pc (pc);
2055 pc->c[0] = IDETAPE_LOCATE_CMD;
2056 pc->c[1] = 2;
2057 put_unaligned (htonl (block), (unsigned int *) &pc->c[3]);
2058 pc->c[8] = partition;
2059 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2060 pc->callback = &idetape_pc_callback;
2063 static void idetape_create_rewind_cmd (idetape_pc_t *pc)
2065 idetape_init_pc (pc);
2066 pc->c[0] = IDETAPE_REWIND_CMD;
2067 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2068 pc->callback = &idetape_pc_callback;
2072 * A mode sense command is used to "sense" tape parameters.
2074 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, byte page_code)
2076 idetape_init_pc (pc);
2077 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2078 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors for now */
2079 pc->c[2] = page_code;
2080 pc->c[3] = 255; /* Don't limit the returned information */
2081 pc->c[4] = 255; /* (We will just discard data in that case) */
2082 if (page_code == IDETAPE_CAPABILITIES_PAGE)
2083 pc->request_transfer = 24;
2084 #if IDETAPE_DEBUG_BUGS
2085 else
2086 printk (KERN_ERR "ide-tape: unsupported page code in create_mode_sense_cmd\n");
2087 #endif /* IDETAPE_DEBUG_BUGS */
2088 pc->callback = &idetape_pc_callback;
2092 * idetape_create_write_filemark_cmd will:
2094 * 1. Write a filemark if write_filemark=1.
2095 * 2. Flush the device buffers without writing a filemark
2096 * if write_filemark=0.
2099 static void idetape_create_write_filemark_cmd (idetape_pc_t *pc,int write_filemark)
2101 idetape_init_pc (pc);
2102 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2103 pc->c[4] = write_filemark;
2104 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2105 pc->callback = &idetape_pc_callback;
2108 static void idetape_create_load_unload_cmd (idetape_pc_t *pc,int cmd)
2110 idetape_init_pc (pc);
2111 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2112 pc->c[4] = cmd;
2113 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2114 pc->callback = &idetape_pc_callback;
2117 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2119 idetape_init_pc (pc);
2120 pc->c[0] = IDETAPE_ERASE_CMD;
2121 pc->c[1] = 1;
2122 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2123 pc->callback = &idetape_pc_callback;
2126 static void idetape_create_read_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2128 idetape_init_pc (pc);
2129 pc->c[0] = IDETAPE_READ_CMD;
2130 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2131 pc->c[1] = 1;
2132 pc->callback = &idetape_rw_callback;
2133 pc->bh = bh;
2134 bh->b_count = 0;
2135 pc->buffer = NULL;
2136 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2137 if (pc->request_transfer == tape->stage_size)
2138 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2141 static void idetape_create_space_cmd (idetape_pc_t *pc,int count,byte cmd)
2143 idetape_init_pc (pc);
2144 pc->c[0] = IDETAPE_SPACE_CMD;
2145 put_unaligned (htonl (count), (unsigned int *) &pc->c[1]);
2146 pc->c[1] = cmd;
2147 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2148 pc->callback = &idetape_pc_callback;
2151 static void idetape_create_write_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2153 idetape_init_pc (pc);
2154 pc->c[0] = IDETAPE_WRITE_CMD;
2155 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2156 pc->c[1] = 1;
2157 pc->callback = &idetape_rw_callback;
2158 set_bit (PC_WRITING, &pc->flags);
2159 pc->bh = bh;
2160 pc->b_data = bh->b_data;
2161 pc->b_count = bh->b_count;
2162 pc->buffer = NULL;
2163 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2164 if (pc->request_transfer == tape->stage_size)
2165 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2168 static void idetape_read_position_callback (ide_drive_t *drive)
2170 idetape_tape_t *tape = drive->driver_data;
2171 idetape_read_position_result_t *result;
2173 #if IDETAPE_DEBUG_LOG
2174 printk (KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2175 #endif /* IDETAPE_DEBUG_LOG */
2177 if (!tape->pc->error) {
2178 result = (idetape_read_position_result_t *) tape->pc->buffer;
2179 #if IDETAPE_DEBUG_LOG
2180 printk (KERN_INFO "BOP - %s\n",result->bop ? "Yes":"No");
2181 printk (KERN_INFO "EOP - %s\n",result->eop ? "Yes":"No");
2182 #endif /* IDETAPE_DEBUG_LOG */
2183 if (result->bpu) {
2184 printk (KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2185 clear_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
2186 idetape_end_request (0,HWGROUP (drive));
2187 } else {
2188 #if IDETAPE_DEBUG_LOG
2189 printk (KERN_INFO "Block Location - %lu\n", ntohl (result->first_block));
2190 #endif /* IDETAPE_DEBUG_LOG */
2191 tape->partition = result->partition;
2192 tape->block_address = ntohl (result->first_block);
2193 set_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
2194 idetape_end_request (1,HWGROUP (drive));
2196 } else
2197 idetape_end_request (0,HWGROUP (drive));
2200 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2202 idetape_init_pc (pc);
2203 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2204 pc->request_transfer = 20;
2205 pc->callback = &idetape_read_position_callback;
2209 * idetape_do_request is our request handling function.
2211 static void idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2213 idetape_tape_t *tape = drive->driver_data;
2214 idetape_pc_t *pc;
2215 struct request *postponed_rq = tape->postponed_rq;
2216 idetape_status_reg_t status;
2218 #if IDETAPE_DEBUG_LOG
2219 printk (KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
2220 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2221 #endif /* IDETAPE_DEBUG_LOG */
2223 if (!IDETAPE_RQ_CMD (rq->cmd)) {
2225 * We do not support buffer cache originated requests.
2227 printk (KERN_NOTICE "ide-tape: %s: Unsupported command in request queue\n", drive->name);
2228 ide_end_request (0,HWGROUP (drive)); /* Let the common code handle it */
2229 return;
2233 * Retry a failed packet command
2235 if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2236 idetape_issue_packet_command (drive, tape->failed_pc);
2237 return;
2239 #if IDETAPE_DEBUG_BUGS
2240 if (postponed_rq != NULL)
2241 if (rq != postponed_rq) {
2242 printk (KERN_ERR "ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
2243 idetape_end_request (0,HWGROUP (drive));
2244 return;
2246 #endif /* IDETAPE_DEBUG_BUGS */
2248 tape->postponed_rq = NULL;
2251 * If the tape is still busy, postpone our request and service
2252 * the other device meanwhile.
2254 status.all = GET_STAT();
2255 if (!drive->dsc_overlap && rq->cmd != IDETAPE_PC_RQ2)
2256 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
2257 if (!test_and_clear_bit (IDETAPE_IGNORE_DSC, &tape->flags) && !status.b.dsc) {
2258 if (postponed_rq == NULL) {
2259 tape->dsc_polling_start = jiffies;
2260 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2261 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2262 } else if ((signed long) (jiffies - tape->dsc_timeout) > 0) {
2263 printk (KERN_ERR "ide-tape: %s: DSC timeout\n", tape->name);
2264 if (rq->cmd == IDETAPE_PC_RQ2)
2265 idetape_media_access_finished (drive);
2266 else
2267 ide_do_reset (drive);
2268 return;
2269 } else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
2270 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2271 idetape_postpone_request (drive);
2272 return;
2274 switch (rq->cmd) {
2275 case IDETAPE_READ_RQ:
2276 pc=idetape_next_pc_storage (drive);
2277 idetape_create_read_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2278 break;
2279 case IDETAPE_WRITE_RQ:
2280 pc=idetape_next_pc_storage (drive);
2281 idetape_create_write_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2282 break;
2283 case IDETAPE_ABORTED_WRITE_RQ:
2284 rq->cmd = IDETAPE_WRITE_RQ;
2285 rq->errors = IDETAPE_ERROR_EOD;
2286 idetape_end_request (1, HWGROUP(drive));
2287 return;
2288 case IDETAPE_PC_RQ1:
2289 pc=(idetape_pc_t *) rq->buffer;
2290 rq->cmd = IDETAPE_PC_RQ2;
2291 break;
2292 case IDETAPE_PC_RQ2:
2293 idetape_media_access_finished (drive);
2294 return;
2295 default:
2296 printk (KERN_ERR "ide-tape: bug in IDETAPE_RQ_CMD macro\n");
2297 idetape_end_request (0,HWGROUP (drive));
2298 return;
2300 idetape_issue_packet_command (drive, pc);
2304 * idetape_queue_pc_tail is based on the following functions:
2306 * ide_do_drive_cmd from ide.c
2307 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2309 * We add a special packet command request to the tail of the request queue,
2310 * and wait for it to be serviced.
2312 * This is not to be called from within the request handling part
2313 * of the driver ! We allocate here data in the stack, and it is valid
2314 * until the request is finished. This is not the case for the bottom
2315 * part of the driver, where we are always leaving the functions to wait
2316 * for an interrupt or a timer event.
2318 * From the bottom part of the driver, we should allocate safe memory
2319 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2320 * the request to the request list without waiting for it to be serviced !
2321 * In that case, we usually use idetape_queue_pc_head.
2323 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2325 struct request rq;
2327 ide_init_drive_cmd (&rq);
2328 rq.buffer = (char *) pc;
2329 rq.cmd = IDETAPE_PC_RQ1;
2330 return ide_do_drive_cmd (drive, &rq, ide_wait);
2334 * idetape_wait_for_request installs a semaphore in a pending request
2335 * and sleeps until it is serviced.
2337 * The caller should ensure that the request will not be serviced
2338 * before we install the semaphore (usually by disabling interrupts).
2340 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2342 DECLARE_MUTEX_LOCKED(sem);
2344 #if IDETAPE_DEBUG_BUGS
2345 if (rq == NULL || !IDETAPE_RQ_CMD (rq->cmd)) {
2346 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2347 return;
2349 #endif /* IDETAPE_DEBUG_BUGS */
2350 rq->sem = &sem;
2351 spin_unlock(&HWGROUP(drive)->spinlock);
2352 down(&sem);
2353 spin_lock_irq(&HWGROUP(drive)->spinlock);
2357 * idetape_queue_rw_tail generates a read/write request for the block
2358 * device interface and wait for it to be serviced.
2360 static int idetape_queue_rw_tail (ide_drive_t *drive, int cmd, int blocks, struct buffer_head *bh)
2362 idetape_tape_t *tape = drive->driver_data;
2363 struct request rq;
2365 #if IDETAPE_DEBUG_LOG
2366 printk (KERN_INFO "idetape_queue_rw_tail: cmd=%d\n",cmd);
2367 #endif /* IDETAPE_DEBUG_LOG */
2368 #if IDETAPE_DEBUG_BUGS
2369 if (idetape_pipeline_active (tape)) {
2370 printk (KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2371 return (0);
2373 #endif /* IDETAPE_DEBUG_BUGS */
2375 ide_init_drive_cmd (&rq);
2376 rq.bh = bh;
2377 rq.cmd = cmd;
2378 rq.sector = tape->block_address;
2379 rq.nr_sectors = rq.current_nr_sectors = blocks;
2380 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2382 idetape_init_merge_stage (tape);
2383 if (rq.errors == IDETAPE_ERROR_GENERAL)
2384 return -EIO;
2385 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2389 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2390 * to service a character device read request and add read-ahead
2391 * requests to our pipeline.
2393 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2395 idetape_tape_t *tape = drive->driver_data;
2396 idetape_stage_t *new_stage;
2397 unsigned long flags;
2398 struct request rq,*rq_ptr;
2399 int bytes_read;
2401 #if IDETAPE_DEBUG_LOG
2402 printk (KERN_INFO "Reached idetape_add_chrdev_read_request\n");
2403 #endif /* IDETAPE_DEBUG_LOG */
2405 ide_init_drive_cmd (&rq);
2406 rq.cmd = IDETAPE_READ_RQ;
2407 rq.sector = tape->block_address;
2408 rq.nr_sectors = rq.current_nr_sectors = blocks;
2410 if (idetape_pipeline_active (tape) || tape->nr_stages <= tape->max_stages / 4) {
2411 new_stage=idetape_kmalloc_stage (tape);
2412 while (new_stage != NULL) {
2413 new_stage->rq=rq;
2414 idetape_add_stage_tail (drive,new_stage);
2415 new_stage=idetape_kmalloc_stage (tape);
2417 if (!idetape_pipeline_active (tape))
2418 idetape_insert_pipeline_into_queue (drive);
2420 if (tape->first_stage == NULL) {
2422 * Linux is short on memory. Revert to non-pipelined
2423 * operation mode for this request.
2425 return (idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, blocks, tape->merge_stage->bh));
2427 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2428 if (tape->active_stage == tape->first_stage)
2429 idetape_wait_for_request(drive, tape->active_data_request);
2430 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2432 rq_ptr = &tape->first_stage->rq;
2433 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2434 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2436 idetape_switch_buffers (tape, tape->first_stage);
2438 if (rq_ptr->errors != IDETAPE_ERROR_FILEMARK) {
2439 clear_bit (IDETAPE_FILEMARK, &tape->flags);
2440 idetape_remove_stage_head (drive);
2441 } else
2442 set_bit (IDETAPE_FILEMARK, &tape->flags);
2443 #if IDETAPE_DEBUG_BUGS
2444 if (bytes_read > blocks*tape->tape_block_size) {
2445 printk (KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2446 bytes_read=blocks*tape->tape_block_size;
2448 #endif /* IDETAPE_DEBUG_BUGS */
2449 return (bytes_read);
2453 * idetape_add_chrdev_write_request tries to add a character device
2454 * originated write request to our pipeline. In case we don't succeed,
2455 * we revert to non-pipelined operation mode for this request.
2457 * 1. Try to allocate a new pipeline stage.
2458 * 2. If we can't, wait for more and more requests to be serviced
2459 * and try again each time.
2460 * 3. If we still can't allocate a stage, fallback to
2461 * non-pipelined operation mode for this request.
2463 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2465 idetape_tape_t *tape = drive->driver_data;
2466 idetape_stage_t *new_stage;
2467 unsigned long flags;
2468 struct request *rq;
2470 #if IDETAPE_DEBUG_LOG
2471 printk (KERN_INFO "Reached idetape_add_chrdev_write_request\n");
2472 #endif /* IDETAPE_DEBUG_LOG */
2475 * Attempt to allocate a new stage.
2476 * Pay special attention to possible race conditions.
2478 while ((new_stage = idetape_kmalloc_stage (tape)) == NULL) {
2479 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2480 if (idetape_pipeline_active (tape)) {
2481 idetape_wait_for_request(drive, tape->active_data_request);
2482 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2483 } else {
2484 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2485 idetape_insert_pipeline_into_queue (drive);
2486 if (idetape_pipeline_active (tape))
2487 continue;
2489 * Linux is short on memory. Fallback to
2490 * non-pipelined operation mode for this request.
2492 return idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
2495 rq = &new_stage->rq;
2496 ide_init_drive_cmd (rq);
2497 rq->cmd = IDETAPE_WRITE_RQ;
2498 rq->sector = tape->block_address; /* Doesn't actually matter - We always assume sequential access */
2499 rq->nr_sectors = rq->current_nr_sectors = blocks;
2501 idetape_switch_buffers (tape, new_stage);
2502 idetape_add_stage_tail (drive,new_stage);
2505 * Check if we are currently servicing requests in the bottom
2506 * part of the driver.
2508 * If not, wait for the pipeline to be full enough (75%) before
2509 * starting to service requests, so that we will be able to
2510 * keep up with the higher speeds of the tape.
2512 if (!idetape_pipeline_active (tape) && tape->nr_stages >= (3 * tape->max_stages) / 4)
2513 idetape_insert_pipeline_into_queue (drive);
2515 if (test_and_clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags)) /* Return a deferred error */
2516 return -EIO;
2517 return blocks;
2520 static void idetape_discard_read_pipeline (ide_drive_t *drive)
2522 idetape_tape_t *tape = drive->driver_data;
2523 unsigned long flags;
2525 #if IDETAPE_DEBUG_BUGS
2526 if (tape->chrdev_direction != idetape_direction_read) {
2527 printk (KERN_ERR "ide-tape: bug: Trying to discard read pipeline, but we are not reading.\n");
2528 return;
2530 #endif /* IDETAPE_DEBUG_BUGS */
2531 tape->merge_stage_size = 0;
2532 if (tape->merge_stage != NULL) {
2533 __idetape_kfree_stage (tape->merge_stage);
2534 tape->merge_stage = NULL;
2536 tape->chrdev_direction = idetape_direction_none;
2538 if (tape->first_stage == NULL)
2539 return;
2541 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2542 tape->next_stage = NULL;
2543 if (idetape_pipeline_active (tape))
2544 idetape_wait_for_request(drive, tape->active_data_request);
2545 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2547 while (tape->first_stage != NULL)
2548 idetape_remove_stage_head (drive);
2549 tape->nr_pending_stages = 0;
2550 tape->max_stages = tape->min_pipeline;
2554 * idetape_wait_for_pipeline will wait until all pending pipeline
2555 * requests are serviced. Typically called on device close.
2557 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2559 idetape_tape_t *tape = drive->driver_data;
2560 unsigned long flags;
2562 if (!idetape_pipeline_active (tape))
2563 idetape_insert_pipeline_into_queue (drive);
2565 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2566 if (!idetape_pipeline_active (tape))
2567 goto abort;
2568 #if IDETAPE_DEBUG_BUGS
2569 if (tape->last_stage == NULL)
2570 printk ("ide-tape: tape->last_stage == NULL\n");
2571 else
2572 #endif /* IDETAPE_DEBUG_BUGS */
2573 idetape_wait_for_request(drive, &tape->last_stage->rq);
2574 abort:
2575 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2578 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2580 idetape_tape_t *tape = drive->driver_data;
2581 struct buffer_head *bh;
2582 int count, blocks;
2584 while (bcount) {
2585 bh = tape->merge_stage->bh;
2586 count = IDE_MIN (tape->stage_size, bcount);
2587 bcount -= count;
2588 blocks = count / tape->tape_block_size;
2589 while (count) {
2590 bh->b_count = IDE_MIN (count, bh->b_size);
2591 memset (bh->b_data, 0, bh->b_count);
2592 count -= bh->b_count;
2593 bh = bh->b_reqnext;
2595 idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
2599 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2601 idetape_tape_t *tape = drive->driver_data;
2602 int blocks, i;
2604 #if IDETAPE_DEBUG_BUGS
2605 if (tape->chrdev_direction != idetape_direction_write) {
2606 printk (KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2607 return;
2609 if (tape->merge_stage_size > tape->stage_size) {
2610 printk (KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2611 tape->merge_stage_size = tape->stage_size;
2613 #endif /* IDETAPE_DEBUG_BUGS */
2614 if (tape->merge_stage_size) {
2615 blocks=tape->merge_stage_size/tape->tape_block_size;
2616 if (tape->merge_stage_size % tape->tape_block_size) {
2617 blocks++;
2618 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2619 memset (tape->bh->b_data + tape->bh->b_count, 0, i);
2620 tape->bh->b_count += i;
2622 (void) idetape_add_chrdev_write_request (drive, blocks);
2623 tape->merge_stage_size = 0;
2625 idetape_wait_for_pipeline (drive);
2626 if (tape->merge_stage != NULL) {
2627 __idetape_kfree_stage (tape->merge_stage);
2628 tape->merge_stage = NULL;
2630 clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
2631 tape->chrdev_direction=idetape_direction_none;
2634 * On the next backup, perform the feedback loop again.
2635 * (I don't want to keep sense information between backups,
2636 * as some systems are constantly on, and the system load
2637 * can be totally different on the next backup).
2639 tape->max_stages = tape->min_pipeline;
2640 #if IDETAPE_DEBUG_BUGS
2641 if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->nr_stages != 0) {
2642 printk (KERN_ERR "ide-tape: ide-tape pipeline bug\n");
2644 #endif /* IDETAPE_DEBUG_BUGS */
2647 static int idetape_pipeline_size (ide_drive_t *drive)
2649 idetape_tape_t *tape = drive->driver_data;
2650 idetape_stage_t *stage;
2651 struct request *rq;
2652 int size = 0;
2654 idetape_wait_for_pipeline (drive);
2655 stage = tape->first_stage;
2656 while (stage != NULL) {
2657 rq = &stage->rq;
2658 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2659 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2660 size += tape->tape_block_size;
2661 stage = stage->next;
2663 size += tape->merge_stage_size;
2664 return size;
2668 * idetape_position_tape positions the tape to the requested block
2669 * using the LOCATE packet command. A READ POSITION command is then
2670 * issued to check where we are positioned.
2672 * Like all higher level operations, we queue the commands at the tail
2673 * of the request queue and wait for their completion.
2676 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, byte partition)
2678 int retval;
2679 idetape_pc_t pc;
2681 idetape_create_locate_cmd (&pc, block, partition);
2682 retval=idetape_queue_pc_tail (drive,&pc);
2683 if (retval) return (retval);
2685 idetape_create_read_position_cmd (&pc);
2686 return (idetape_queue_pc_tail (drive,&pc));
2690 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2692 * We currently support only one partition.
2694 static int idetape_rewind_tape (ide_drive_t *drive)
2696 int retval;
2697 idetape_pc_t pc;
2698 #if IDETAPE_DEBUG_LOG
2699 printk (KERN_INFO "Reached idetape_rewind_tape\n");
2700 #endif /* IDETAPE_DEBUG_LOG */
2702 idetape_create_rewind_cmd (&pc);
2703 retval=idetape_queue_pc_tail (drive,&pc);
2704 if (retval) return (retval);
2706 idetape_create_read_position_cmd (&pc);
2707 return (idetape_queue_pc_tail (drive,&pc));
2710 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2712 idetape_pc_t pc;
2714 idetape_create_write_filemark_cmd (&pc,0);
2715 return (idetape_queue_pc_tail (drive,&pc));
2719 * Our special ide-tape ioctl's.
2721 * Currently there aren't any ioctl's.
2722 * mtio.h compatible commands should be issued to the character device
2723 * interface.
2725 static int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2726 unsigned int cmd, unsigned long arg)
2728 idetape_tape_t *tape = drive->driver_data;
2729 idetape_config_t config;
2731 #if IDETAPE_DEBUG_LOG
2732 printk (KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
2733 #endif /* IDETAPE_DEBUG_LOG */
2734 switch (cmd) {
2735 case 0x0340:
2736 if (copy_from_user ((char *) &config, (char *) arg, sizeof (idetape_config_t)))
2737 return -EFAULT;
2738 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2739 tape->max_stages = config.nr_stages;
2740 break;
2741 case 0x0350:
2742 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2743 config.nr_stages = tape->max_stages;
2744 if (copy_to_user ((char *) arg, (char *) &config, sizeof (idetape_config_t)))
2745 return -EFAULT;
2746 break;
2747 default:
2748 return -EIO;
2750 return 0;
2754 * The block device interface should not be used for data transfers.
2755 * However, we still allow opening it so that we can issue general
2756 * ide driver configuration ioctl's, such as the interrupt unmask feature.
2758 static int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
2760 MOD_INC_USE_COUNT;
2761 return 0;
2764 static void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
2766 MOD_DEC_USE_COUNT;
2770 * idetape_pre_reset is called before an ATAPI/ATA software reset.
2772 static void idetape_pre_reset (ide_drive_t *drive)
2774 idetape_tape_t *tape = drive->driver_data;
2775 if (tape != NULL)
2776 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
2780 * Character device interface functions
2782 static ide_drive_t *get_drive_ptr (kdev_t i_rdev)
2784 unsigned int i = MINOR(i_rdev) & ~0x80;
2786 if (i >= MAX_HWIFS * MAX_DRIVES)
2787 return NULL;
2788 return (idetape_chrdevs[i].drive);
2792 * idetape_space_over_filemarks is now a bit more complicated than just
2793 * passing the command to the tape since we may have crossed some
2794 * filemarks during our pipelined read-ahead mode.
2796 * As a minor side effect, the pipeline enables us to support MTFSFM when
2797 * the filemark is in our internal pipeline even if the tape doesn't
2798 * support spacing over filemarks in the reverse direction.
2800 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2802 idetape_tape_t *tape = drive->driver_data;
2803 idetape_pc_t pc;
2804 unsigned long flags;
2805 int retval,count=0;
2807 if (tape->chrdev_direction == idetape_direction_read) {
2810 * We have a read-ahead buffer. Scan it for crossed
2811 * filemarks.
2813 tape->merge_stage_size = 0;
2814 clear_bit (IDETAPE_FILEMARK, &tape->flags);
2815 while (tape->first_stage != NULL) {
2817 * Wait until the first read-ahead request
2818 * is serviced.
2820 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2821 if (tape->active_stage == tape->first_stage)
2822 idetape_wait_for_request(drive, tape->active_data_request);
2823 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2825 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2826 count++;
2827 if (count == mt_count) {
2828 switch (mt_op) {
2829 case MTFSF:
2830 idetape_remove_stage_head (drive);
2831 case MTFSFM:
2832 return (0);
2833 default:
2834 break;
2837 idetape_remove_stage_head (drive);
2839 idetape_discard_read_pipeline (drive);
2843 * The filemark was not found in our internal pipeline.
2844 * Now we can issue the space command.
2846 switch (mt_op) {
2847 case MTFSF:
2848 idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2849 return (idetape_queue_pc_tail (drive,&pc));
2850 case MTFSFM:
2851 if (!tape->capabilities.sprev)
2852 return (-EIO);
2853 retval = idetape_space_over_filemarks (drive, MTFSF, mt_count-count);
2854 if (retval) return (retval);
2855 return (idetape_space_over_filemarks (drive, MTBSF, 1));
2856 case MTBSF:
2857 if (!tape->capabilities.sprev)
2858 return (-EIO);
2859 idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
2860 return (idetape_queue_pc_tail (drive,&pc));
2861 case MTBSFM:
2862 if (!tape->capabilities.sprev)
2863 return (-EIO);
2864 retval = idetape_space_over_filemarks (drive, MTBSF, mt_count+count);
2865 if (retval) return (retval);
2866 return (idetape_space_over_filemarks (drive, MTFSF, 1));
2867 default:
2868 printk (KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2869 return (-EIO);
2875 * Our character device read / write functions.
2877 * The tape is optimized to maximize throughput when it is transferring
2878 * an integral number of the "continuous transfer limit", which is
2879 * a parameter of the specific tape (26 KB on my particular tape).
2881 * As of version 1.3 of the driver, the character device provides an
2882 * abstract continuous view of the media - any mix of block sizes (even 1
2883 * byte) on the same backup/restore procedure is supported. The driver
2884 * will internally convert the requests to the recommended transfer unit,
2885 * so that an unmatch between the user's block size to the recommended
2886 * size will only result in a (slightly) increased driver overhead, but
2887 * will no longer hit performance.
2889 static ssize_t idetape_chrdev_read (struct file *file, char *buf,
2890 size_t count, loff_t *ppos)
2892 struct inode *inode = file->f_dentry->d_inode;
2893 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
2894 idetape_tape_t *tape = drive->driver_data;
2895 ssize_t bytes_read,temp,actually_read=0;
2897 if (ppos != &file->f_pos) {
2898 /* "A request was outside the capabilities of the device." */
2899 return -ENXIO;
2902 #if IDETAPE_DEBUG_LOG
2903 printk (KERN_INFO "Reached idetape_chrdev_read\n");
2904 #endif /* IDETAPE_DEBUG_LOG */
2906 if (tape->chrdev_direction != idetape_direction_read) { /* Initialize read operation */
2907 if (tape->chrdev_direction == idetape_direction_write) {
2908 idetape_empty_write_pipeline (drive);
2909 idetape_flush_tape_buffers (drive);
2911 #if IDETAPE_DEBUG_BUGS
2912 if (tape->merge_stage || tape->merge_stage_size) {
2913 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2914 tape->merge_stage_size = 0;
2916 #endif /* IDETAPE_DEBUG_BUGS */
2917 if ((tape->merge_stage = __idetape_kmalloc_stage (tape)) == NULL)
2918 return -ENOMEM;
2919 tape->chrdev_direction = idetape_direction_read;
2922 * Issue a read 0 command to ensure that DSC handshake
2923 * is switched from completion mode to buffer available
2924 * mode.
2926 bytes_read = idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 0, tape->merge_stage->bh);
2927 if (bytes_read < 0) {
2928 kfree (tape->merge_stage);
2929 tape->merge_stage = NULL;
2930 tape->chrdev_direction = idetape_direction_none;
2931 return bytes_read;
2933 if (test_bit (IDETAPE_DETECT_BS, &tape->flags))
2934 if (count > tape->tape_block_size && (count % tape->tape_block_size) == 0)
2935 tape->user_bs_factor = count / tape->tape_block_size;
2937 if (count==0)
2938 return (0);
2939 if (tape->merge_stage_size) {
2940 actually_read=IDE_MIN (tape->merge_stage_size,count);
2941 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, actually_read);
2942 buf += actually_read; tape->merge_stage_size -= actually_read; count-=actually_read;
2944 while (count >= tape->stage_size) {
2945 bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
2946 if (bytes_read <= 0)
2947 goto finish;
2948 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, bytes_read);
2949 buf += bytes_read; count -= bytes_read; actually_read += bytes_read;
2951 if (count) {
2952 bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
2953 if (bytes_read <= 0)
2954 goto finish;
2955 temp=IDE_MIN (count,bytes_read);
2956 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, temp);
2957 actually_read+=temp;
2958 tape->merge_stage_size=bytes_read-temp;
2960 finish:
2961 if (!actually_read && test_bit (IDETAPE_FILEMARK, &tape->flags))
2962 idetape_space_over_filemarks (drive, MTFSF, 1);
2963 return (actually_read);
2966 static ssize_t idetape_chrdev_write (struct file *file, const char *buf,
2967 size_t count, loff_t *ppos)
2969 struct inode *inode = file->f_dentry->d_inode;
2970 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
2971 idetape_tape_t *tape = drive->driver_data;
2972 ssize_t retval,actually_written=0;
2974 if (ppos != &file->f_pos) {
2975 /* "A request was outside the capabilities of the device." */
2976 return -ENXIO;
2979 #if IDETAPE_DEBUG_LOG
2980 printk (KERN_INFO "Reached idetape_chrdev_write\n");
2981 #endif /* IDETAPE_DEBUG_LOG */
2983 if (tape->chrdev_direction != idetape_direction_write) { /* Initialize write operation */
2984 if (tape->chrdev_direction == idetape_direction_read)
2985 idetape_discard_read_pipeline (drive);
2986 #if IDETAPE_DEBUG_BUGS
2987 if (tape->merge_stage || tape->merge_stage_size) {
2988 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2989 tape->merge_stage_size = 0;
2991 #endif /* IDETAPE_DEBUG_BUGS */
2992 if ((tape->merge_stage = __idetape_kmalloc_stage (tape)) == NULL)
2993 return -ENOMEM;
2994 tape->chrdev_direction = idetape_direction_write;
2995 idetape_init_merge_stage (tape);
2998 * Issue a write 0 command to ensure that DSC handshake
2999 * is switched from completion mode to buffer available
3000 * mode.
3002 retval = idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 0, tape->merge_stage->bh);
3003 if (retval < 0) {
3004 kfree (tape->merge_stage);
3005 tape->merge_stage = NULL;
3006 tape->chrdev_direction = idetape_direction_none;
3007 return retval;
3009 if (test_bit (IDETAPE_DETECT_BS, &tape->flags))
3010 if (count > tape->tape_block_size && (count % tape->tape_block_size) == 0)
3011 tape->user_bs_factor = count / tape->tape_block_size;
3013 if (count==0)
3014 return (0);
3015 if (tape->merge_stage_size) {
3016 #if IDETAPE_DEBUG_BUGS
3017 if (tape->merge_stage_size >= tape->stage_size) {
3018 printk (KERN_ERR "ide-tape: bug: merge buffer too big\n");
3019 tape->merge_stage_size=0;
3021 #endif /* IDETAPE_DEBUG_BUGS */
3022 actually_written=IDE_MIN (tape->stage_size-tape->merge_stage_size,count);
3023 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, actually_written);
3024 buf+=actually_written;tape->merge_stage_size+=actually_written;count-=actually_written;
3026 if (tape->merge_stage_size == tape->stage_size) {
3027 tape->merge_stage_size = 0;
3028 retval=idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
3029 if (retval <= 0)
3030 return (retval);
3033 while (count >= tape->stage_size) {
3034 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, tape->stage_size);
3035 buf+=tape->stage_size;count-=tape->stage_size;
3036 retval=idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
3037 actually_written+=tape->stage_size;
3038 if (retval <= 0)
3039 return (retval);
3041 if (count) {
3042 actually_written+=count;
3043 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, count);
3044 tape->merge_stage_size+=count;
3046 return (actually_written);
3050 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3051 * the general mtio MTIOCTOP ioctl is requested.
3053 * We currently support the following mtio.h operations:
3055 * MTFSF - Space over mt_count filemarks in the positive direction.
3056 * The tape is positioned after the last spaced filemark.
3058 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3059 * last filemark.
3061 * MTBSF - Steps background over mt_count filemarks, tape is
3062 * positioned before the last filemark.
3064 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3066 * Note:
3068 * MTBSF and MTBSFM are not supported when the tape doesn't
3069 * supports spacing over filemarks in the reverse direction.
3070 * In this case, MTFSFM is also usually not supported (it is
3071 * supported in the rare case in which we crossed the filemark
3072 * during our read-ahead pipelined operation mode).
3074 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3075 * the last written filemark.
3077 * MTREW - Rewinds tape.
3079 * MTLOAD - Loads the tape.
3081 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3082 * MTUNLOAD prevents further access until the media is replaced.
3084 * MTNOP - Flushes tape buffers.
3086 * MTRETEN - Retension media. This typically consists of one end
3087 * to end pass on the media.
3089 * MTEOM - Moves to the end of recorded data.
3091 * MTERASE - Erases tape.
3093 * MTSETBLK - Sets the user block size to mt_count bytes. If
3094 * mt_count is 0, we will attempt to autodetect
3095 * the block size.
3097 * MTSEEK - Positions the tape in a specific block number, where
3098 * each block is assumed to contain which user_block_size
3099 * bytes.
3101 * MTSETPART - Switches to another tape partition.
3103 * The following commands are currently not supported:
3105 * MTFSR, MTBSR, MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3106 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3108 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3110 idetape_tape_t *tape = drive->driver_data;
3111 idetape_pc_t pc;
3112 int i,retval;
3114 #if IDETAPE_DEBUG_LOG
3115 printk (KERN_INFO "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",mt_op,mt_count);
3116 #endif /* IDETAPE_DEBUG_LOG */
3118 * Commands which need our pipelined read-ahead stages.
3120 switch (mt_op) {
3121 case MTFSF:
3122 case MTFSFM:
3123 case MTBSF:
3124 case MTBSFM:
3125 if (!mt_count)
3126 return (0);
3127 return (idetape_space_over_filemarks (drive,mt_op,mt_count));
3128 default:
3129 break;
3133 * Empty the pipeline.
3135 if (tape->chrdev_direction == idetape_direction_read)
3136 idetape_discard_read_pipeline (drive);
3138 switch (mt_op) {
3139 case MTWEOF:
3140 for (i=0;i<mt_count;i++) {
3141 idetape_create_write_filemark_cmd (&pc,1);
3142 retval=idetape_queue_pc_tail (drive,&pc);
3143 if (retval) return (retval);
3145 return (0);
3146 case MTREW:
3147 return (idetape_rewind_tape (drive));
3148 case MTLOAD:
3149 idetape_create_load_unload_cmd (&pc, IDETAPE_LU_LOAD_MASK);
3150 return (idetape_queue_pc_tail (drive,&pc));
3151 case MTUNLOAD:
3152 case MTOFFL:
3153 idetape_create_load_unload_cmd (&pc,!IDETAPE_LU_LOAD_MASK);
3154 return (idetape_queue_pc_tail (drive,&pc));
3155 case MTNOP:
3156 return (idetape_flush_tape_buffers (drive));
3157 case MTRETEN:
3158 idetape_create_load_unload_cmd (&pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3159 return (idetape_queue_pc_tail (drive,&pc));
3160 case MTEOM:
3161 idetape_create_space_cmd (&pc,0,IDETAPE_SPACE_TO_EOD);
3162 return (idetape_queue_pc_tail (drive,&pc));
3163 case MTERASE:
3164 (void) idetape_rewind_tape (drive);
3165 idetape_create_erase_cmd (&pc);
3166 return (idetape_queue_pc_tail (drive,&pc));
3167 case MTSETBLK:
3168 if (mt_count) {
3169 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3170 return -EIO;
3171 tape->user_bs_factor = mt_count / tape->tape_block_size;
3172 clear_bit (IDETAPE_DETECT_BS, &tape->flags);
3173 } else
3174 set_bit (IDETAPE_DETECT_BS, &tape->flags);
3175 return 0;
3176 case MTSEEK:
3177 return (idetape_position_tape (drive, mt_count * tape->user_bs_factor, tape->partition));
3178 case MTSETPART:
3179 return (idetape_position_tape (drive, 0, mt_count));
3180 default:
3181 printk (KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3182 return (-EIO);
3187 * Our character device ioctls.
3189 * General mtio.h magnetic io commands are supported here, and not in
3190 * the corresponding block interface.
3192 * The following ioctls are supported:
3194 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3196 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3197 * will be set to (user block size in bytes <<
3198 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3200 * The mt_blkno is set to the current user block number.
3201 * The other mtget fields are not supported.
3203 * MTIOCPOS - The current tape "block position" is returned. We
3204 * assume that each block contains user_block_size
3205 * bytes.
3207 * Our own ide-tape ioctls are supported on both interfaces.
3209 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3211 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
3212 idetape_tape_t *tape = drive->driver_data;
3213 idetape_pc_t pc;
3214 struct mtop mtop;
3215 struct mtget mtget;
3216 struct mtpos mtpos;
3217 int retval, block_offset = 0;
3219 #if IDETAPE_DEBUG_LOG
3220 printk (KERN_INFO "Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
3221 #endif /* IDETAPE_DEBUG_LOG */
3223 if (tape->chrdev_direction == idetape_direction_write) {
3224 idetape_empty_write_pipeline (drive);
3225 idetape_flush_tape_buffers (drive);
3227 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3228 block_offset = idetape_pipeline_size (drive) / (tape->tape_block_size * tape->user_bs_factor);
3229 idetape_create_read_position_cmd (&pc);
3230 retval=idetape_queue_pc_tail (drive,&pc);
3231 if (retval) return (retval);
3233 switch (cmd) {
3234 case MTIOCTOP:
3235 if (copy_from_user ((char *) &mtop, (char *) arg, sizeof (struct mtop)))
3236 return -EFAULT;
3237 return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
3238 case MTIOCGET:
3239 memset (&mtget, 0, sizeof (struct mtget));
3240 mtget.mt_blkno = tape->block_address / tape->user_bs_factor - block_offset;
3241 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3242 if (copy_to_user ((char *) arg,(char *) &mtget, sizeof (struct mtget)))
3243 return -EFAULT;
3244 return 0;
3245 case MTIOCPOS:
3246 mtpos.mt_blkno = tape->block_address / tape->user_bs_factor - block_offset;
3247 if (copy_to_user ((char *) arg,(char *) &mtpos, sizeof (struct mtpos)))
3248 return -EFAULT;
3249 return 0;
3250 default:
3251 if (tape->chrdev_direction == idetape_direction_read)
3252 idetape_discard_read_pipeline (drive);
3253 return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
3258 * Our character device open function.
3260 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3262 ide_drive_t *drive;
3263 idetape_tape_t *tape;
3264 idetape_pc_t pc;
3266 #if IDETAPE_DEBUG_LOG
3267 printk (KERN_INFO "Reached idetape_chrdev_open\n");
3268 #endif /* IDETAPE_DEBUG_LOG */
3270 if ((drive = get_drive_ptr (inode->i_rdev)) == NULL)
3271 return -ENXIO;
3272 tape = drive->driver_data;
3274 if (test_and_set_bit (IDETAPE_BUSY, &tape->flags))
3275 return -EBUSY;
3276 MOD_INC_USE_COUNT;
3277 idetape_create_read_position_cmd (&pc);
3278 (void) idetape_queue_pc_tail (drive,&pc);
3279 if (!test_bit (IDETAPE_ADDRESS_VALID, &tape->flags))
3280 (void) idetape_rewind_tape (drive);
3281 MOD_DEC_USE_COUNT;
3283 if (tape->chrdev_direction == idetape_direction_none)
3284 MOD_INC_USE_COUNT;
3285 return 0;
3289 * Our character device release function.
3291 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3293 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
3294 idetape_tape_t *tape = drive->driver_data;
3295 unsigned int minor=MINOR (inode->i_rdev);
3296 idetape_pc_t pc;
3298 #if IDETAPE_DEBUG_LOG
3299 printk (KERN_INFO "Reached idetape_chrdev_release\n");
3300 #endif /* IDETAPE_DEBUG_LOG */
3302 if (tape->chrdev_direction == idetape_direction_write) {
3303 idetape_empty_write_pipeline (drive);
3304 tape->merge_stage = __idetape_kmalloc_stage (tape);
3305 if (tape->merge_stage != NULL) {
3306 idetape_pad_zeros (drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3307 __idetape_kfree_stage (tape->merge_stage);
3308 tape->merge_stage = NULL;
3310 idetape_create_write_filemark_cmd (&pc,1); /* Write a filemark */
3311 if (idetape_queue_pc_tail (drive,&pc))
3312 printk (KERN_ERR "ide-tape: Couldn't write a filemark\n");
3314 if (tape->chrdev_direction == idetape_direction_read) {
3315 if (minor < 128)
3316 idetape_discard_read_pipeline (drive);
3317 else
3318 idetape_wait_for_pipeline (drive);
3320 if (tape->cache_stage != NULL) {
3321 __idetape_kfree_stage (tape->cache_stage);
3322 tape->cache_stage = NULL;
3324 if (minor < 128)
3325 (void) idetape_rewind_tape (drive);
3327 clear_bit (IDETAPE_BUSY, &tape->flags);
3328 if (tape->chrdev_direction == idetape_direction_none)
3329 MOD_DEC_USE_COUNT;
3330 return 0;
3334 * idetape_identify_device is called to check the contents of the
3335 * ATAPI IDENTIFY command results. We return:
3337 * 1 If the tape can be supported by us, based on the information
3338 * we have so far.
3340 * 0 If this tape driver is not currently supported by us.
3342 static int idetape_identify_device (ide_drive_t *drive,struct hd_driveid *id)
3344 struct idetape_id_gcw gcw;
3345 #if IDETAPE_INFO_LOG
3346 unsigned short mask,i;
3347 #endif /* IDETAPE_INFO_LOG */
3349 if (!id)
3350 return 0;
3352 *((unsigned short *) &gcw) = id->config;
3354 #if IDETAPE_INFO_LOG
3355 printk (KERN_INFO "Dumping ATAPI Identify Device tape parameters\n");
3356 printk (KERN_INFO "Protocol Type: ");
3357 switch (gcw.protocol) {
3358 case 0: case 1: printk (KERN_INFO "ATA\n");break;
3359 case 2: printk (KERN_INFO "ATAPI\n");break;
3360 case 3: printk (KERN_INFO "Reserved (Unknown to ide-tape)\n");break;
3362 printk (KERN_INFO "Device Type: %x - ",gcw.device_type);
3363 switch (gcw.device_type) {
3364 case 0: printk (KERN_INFO "Direct-access Device\n");break;
3365 case 1: printk (KERN_INFO "Streaming Tape Device\n");break;
3366 case 2: case 3: case 4: printk (KERN_INFO "Reserved\n");break;
3367 case 5: printk (KERN_INFO "CD-ROM Device\n");break;
3368 case 6: printk (KERN_INFO "Reserved\n");
3369 case 7: printk (KERN_INFO "Optical memory Device\n");break;
3370 case 0x1f: printk (KERN_INFO "Unknown or no Device type\n");break;
3371 default: printk (KERN_INFO "Reserved\n");
3373 printk (KERN_INFO "Removable: %s",gcw.removable ? "Yes\n":"No\n");
3374 printk (KERN_INFO "Command Packet DRQ Type: ");
3375 switch (gcw.drq_type) {
3376 case 0: printk (KERN_INFO "Microprocessor DRQ\n");break;
3377 case 1: printk (KERN_INFO "Interrupt DRQ\n");break;
3378 case 2: printk (KERN_INFO "Accelerated DRQ\n");break;
3379 case 3: printk (KERN_INFO "Reserved\n");break;
3381 printk (KERN_INFO "Command Packet Size: ");
3382 switch (gcw.packet_size) {
3383 case 0: printk (KERN_INFO "12 bytes\n");break;
3384 case 1: printk (KERN_INFO "16 bytes\n");break;
3385 default: printk (KERN_INFO "Reserved\n");break;
3387 printk (KERN_INFO "Model: %.40s\n",id->model);
3388 printk (KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
3389 printk (KERN_INFO "Serial Number: %.20s\n",id->serial_no);
3390 printk (KERN_INFO "Write buffer size: %d bytes\n",id->buf_size*512);
3391 printk (KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
3392 printk (KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
3393 printk (KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
3394 printk (KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
3395 printk (KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
3396 printk (KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
3397 printk (KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
3398 printk (KERN_INFO "Single Word DMA supported modes: ");
3399 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
3400 if (id->dma_1word & mask)
3401 printk (KERN_INFO "%d ",i);
3402 if (id->dma_1word & (mask << 8))
3403 printk (KERN_INFO "(active) ");
3405 printk (KERN_INFO "\n");
3406 printk (KERN_INFO "Multi Word DMA supported modes: ");
3407 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
3408 if (id->dma_mword & mask)
3409 printk (KERN_INFO "%d ",i);
3410 if (id->dma_mword & (mask << 8))
3411 printk (KERN_INFO "(active) ");
3413 printk (KERN_INFO "\n");
3414 if (id->field_valid & 0x0002) {
3415 printk (KERN_INFO "Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
3416 printk (KERN_INFO "Minimum Multi-word DMA cycle per word: ");
3417 if (id->eide_dma_min == 0)
3418 printk (KERN_INFO "Not supported\n");
3419 else
3420 printk (KERN_INFO "%d ns\n",id->eide_dma_min);
3422 printk (KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: ");
3423 if (id->eide_dma_time == 0)
3424 printk (KERN_INFO "Not supported\n");
3425 else
3426 printk (KERN_INFO "%d ns\n",id->eide_dma_time);
3428 printk (KERN_INFO "Minimum PIO cycle without IORDY: ");
3429 if (id->eide_pio == 0)
3430 printk (KERN_INFO "Not supported\n");
3431 else
3432 printk (KERN_INFO "%d ns\n",id->eide_pio);
3434 printk (KERN_INFO "Minimum PIO cycle with IORDY: ");
3435 if (id->eide_pio_iordy == 0)
3436 printk (KERN_INFO "Not supported\n");
3437 else
3438 printk (KERN_INFO "%d ns\n",id->eide_pio_iordy);
3440 } else
3441 printk (KERN_INFO "According to the device, fields 64-70 are not valid.\n");
3442 #endif /* IDETAPE_INFO_LOG */
3444 /* Check that we can support this device */
3446 if (gcw.protocol !=2 )
3447 printk (KERN_ERR "ide-tape: Protocol is not ATAPI\n");
3448 else if (gcw.device_type != 1)
3449 printk (KERN_ERR "ide-tape: Device type is not set to tape\n");
3450 else if (!gcw.removable)
3451 printk (KERN_ERR "ide-tape: The removable flag is not set\n");
3452 else if (gcw.packet_size != 0) {
3453 printk (KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
3454 if (gcw.packet_size == 1)
3455 printk (KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
3456 } else
3457 return 1;
3458 return 0;
3462 * idetape_get_mode_sense_results asks the tape about its various
3463 * parameters. In particular, we will adjust our data transfer buffer
3464 * size to the recommended value as returned by the tape.
3466 static void idetape_get_mode_sense_results (ide_drive_t *drive)
3468 idetape_tape_t *tape = drive->driver_data;
3469 idetape_pc_t pc;
3470 idetape_mode_parameter_header_t *header;
3471 idetape_capabilities_page_t *capabilities;
3473 idetape_create_mode_sense_cmd (&pc,IDETAPE_CAPABILITIES_PAGE);
3474 if (idetape_queue_pc_tail (drive,&pc)) {
3475 printk (KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
3476 tape->tape_block_size = 512; tape->capabilities.ctl = 52;
3477 tape->capabilities.speed = 450; tape->capabilities.buffer_size = 6 * 52;
3478 return;
3480 header = (idetape_mode_parameter_header_t *) pc.buffer;
3481 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
3483 capabilities->max_speed = ntohs (capabilities->max_speed);
3484 capabilities->ctl = ntohs (capabilities->ctl);
3485 capabilities->speed = ntohs (capabilities->speed);
3486 capabilities->buffer_size = ntohs (capabilities->buffer_size);
3488 if (!capabilities->speed) {
3489 printk("ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
3490 capabilities->speed = 650;
3492 if (!capabilities->max_speed) {
3493 printk("ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
3494 capabilities->max_speed = 650;
3497 tape->capabilities = *capabilities; /* Save us a copy */
3498 tape->tape_block_size = capabilities->blk512 ? 512:1024;
3499 #if IDETAPE_INFO_LOG
3500 printk (KERN_INFO "Dumping the results of the MODE SENSE packet command\n");
3501 printk (KERN_INFO "Mode Parameter Header:\n");
3502 printk (KERN_INFO "Mode Data Length - %d\n",header->mode_data_length);
3503 printk (KERN_INFO "Medium Type - %d\n",header->medium_type);
3504 printk (KERN_INFO "Device Specific Parameter - %d\n",header->dsp);
3505 printk (KERN_INFO "Block Descriptor Length - %d\n",header->bdl);
3507 printk (KERN_INFO "Capabilities and Mechanical Status Page:\n");
3508 printk (KERN_INFO "Page code - %d\n",capabilities->page_code);
3509 printk (KERN_INFO "Page length - %d\n",capabilities->page_length);
3510 printk (KERN_INFO "Read only - %s\n",capabilities->ro ? "Yes":"No");
3511 printk (KERN_INFO "Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
3512 printk (KERN_INFO "Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
3513 printk (KERN_INFO "Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
3514 printk (KERN_INFO "Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
3515 printk (KERN_INFO "The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
3516 printk (KERN_INFO "The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
3517 printk (KERN_INFO "Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
3518 printk (KERN_INFO "Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
3519 printk (KERN_INFO "Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
3520 printk (KERN_INFO "Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
3521 printk (KERN_INFO "Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
3522 printk (KERN_INFO "Restricted byte count for PIO transfers - %s\n",capabilities->slowb ? "Yes":"No");
3523 printk (KERN_INFO "Maximum supported speed in KBps - %d\n",capabilities->max_speed);
3524 printk (KERN_INFO "Continuous transfer limits in blocks - %d\n",capabilities->ctl);
3525 printk (KERN_INFO "Current speed in KBps - %d\n",capabilities->speed);
3526 printk (KERN_INFO "Buffer size - %d\n",capabilities->buffer_size*512);
3527 #endif /* IDETAPE_INFO_LOG */
3530 static void idetape_add_settings(ide_drive_t *drive)
3532 idetape_tape_t *tape = drive->driver_data;
3535 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
3537 ide_add_setting(drive, "buffer", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
3538 ide_add_setting(drive, "pipeline_min", SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3539 ide_add_setting(drive, "pipeline", SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3540 ide_add_setting(drive, "pipeline_max", SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3541 ide_add_setting(drive, "pipeline_used",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3542 ide_add_setting(drive, "speed", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
3543 ide_add_setting(drive, "stage", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3544 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);
3545 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3549 * ide_setup is called to:
3551 * 1. Initialize our various state variables.
3552 * 2. Ask the tape for its capabilities.
3553 * 3. Allocate a buffer which will be used for data
3554 * transfer. The buffer size is chosen based on
3555 * the recommendation which we received in step (2).
3557 * Note that at this point ide.c already assigned us an irq, so that
3558 * we can queue requests here and wait for their completion.
3560 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3562 ide_hwif_t *hwif = HWIF(drive);
3563 unsigned long t1, tmid, tn, t;
3564 u16 speed;
3565 struct idetape_id_gcw gcw;
3567 drive->driver_data = tape;
3568 drive->ready_stat = 0; /* An ATAPI device ignores DRDY */
3569 #ifdef CONFIG_BLK_DEV_IDEPCI
3571 * These two ide-pci host adapters appear to need this disabled.
3573 if ((hwif->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
3574 (hwif->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) {
3575 drive->dsc_overlap = 0;
3576 } else
3577 #endif /* CONFIG_BLK_DEV_IDEPCI */
3579 drive->dsc_overlap = 1;
3581 memset (tape, 0, sizeof (idetape_tape_t));
3582 tape->drive = drive;
3583 tape->minor = minor;
3584 tape->name[0] = 'h'; tape->name[1] = 't'; tape->name[2] = '0' + minor;
3585 tape->chrdev_direction = idetape_direction_none;
3586 tape->pc = tape->pc_stack;
3587 tape->min_pipeline = IDETAPE_MIN_PIPELINE_STAGES;
3588 tape->max_pipeline = IDETAPE_MAX_PIPELINE_STAGES;
3589 tape->max_stages = tape->min_pipeline;
3590 *((unsigned short *) &gcw) = drive->id->config;
3591 if (gcw.drq_type == 1)
3592 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3594 idetape_get_mode_sense_results (drive);
3596 tape->user_bs_factor = 1;
3597 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
3598 while (tape->stage_size > 0xffff) {
3599 printk (KERN_NOTICE "ide-tape: decreasing stage size\n");
3600 tape->capabilities.ctl /= 2;
3601 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
3603 tape->pages_per_stage = tape->stage_size / PAGE_SIZE;
3604 if (tape->stage_size % PAGE_SIZE) {
3605 tape->pages_per_stage++;
3606 tape->excess_bh_size = PAGE_SIZE - tape->stage_size % PAGE_SIZE;
3610 * Select the "best" DSC read/write polling frequency.
3611 * The following algorithm attempts to find a balance between
3612 * good latency and good system throughput. It will be nice to
3613 * have all this configurable in run time at some point.
3615 speed = IDE_MAX (tape->capabilities.speed, tape->capabilities.max_speed);
3616 t1 = (tape->stage_size * HZ) / (speed * 1000);
3617 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
3618 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3620 if (tape->max_stages) {
3621 if (drive->using_dma)
3622 t = tmid;
3623 else {
3624 if (hwif->drives[drive->select.b.unit ^ 1].present || hwif->next != hwif)
3625 t = (tn + tmid) / 2;
3626 else
3627 t = tn;
3629 } else
3630 t = t1;
3631 t = IDE_MIN (t, tmid);
3634 * Ensure that the number we got makes sense.
3636 tape->best_dsc_rw_frequency = IDE_MAX (IDE_MIN (t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3637 if (tape->best_dsc_rw_frequency != t) {
3638 printk (KERN_NOTICE "ide-tape: Although the recommended polling period is %lu jiffies\n", t);
3639 printk (KERN_NOTICE "ide-tape: we will use %lu jiffies\n", tape->best_dsc_rw_frequency);
3641 printk (KERN_INFO "ide-tape: %s <-> %s, %dKBps, %d*%dkB buffer, %dkB pipeline, %lums tDSC%s\n",
3642 drive->name, tape->name, tape->capabilities.speed, (tape->capabilities.buffer_size * 512) / tape->stage_size,
3643 tape->stage_size / 1024, tape->max_stages * tape->stage_size / 1024,
3644 tape->best_dsc_rw_frequency * 1000 / HZ, drive->using_dma ? ", DMA":"");
3646 idetape_add_settings(drive);
3649 static int idetape_cleanup (ide_drive_t *drive)
3651 idetape_tape_t *tape = drive->driver_data;
3652 int minor = tape->minor;
3653 unsigned long flags;
3655 save_flags (flags); /* all CPUs (overkill?) */
3656 cli(); /* all CPUs (overkill?) */
3657 if (test_bit (IDETAPE_BUSY, &tape->flags) || tape->first_stage != NULL || tape->merge_stage_size || drive->usage) {
3658 restore_flags(flags); /* all CPUs (overkill?) */
3659 return 1;
3661 idetape_chrdevs[minor].drive = NULL;
3662 restore_flags (flags); /* all CPUs (overkill?) */
3663 DRIVER(drive)->busy = 0;
3664 (void) ide_unregister_subdriver (drive);
3665 drive->driver_data = NULL;
3666 kfree (tape);
3667 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++)
3668 if (idetape_chrdevs[minor].drive != NULL)
3669 return 0;
3670 unregister_chrdev (IDETAPE_MAJOR, "ht");
3671 idetape_chrdev_present = 0;
3672 return 0;
3675 #ifdef CONFIG_PROC_FS
3677 static int proc_idetape_read_name
3678 (char *page, char **start, off_t off, int count, int *eof, void *data)
3680 ide_drive_t *drive = (ide_drive_t *) data;
3681 idetape_tape_t *tape = drive->driver_data;
3682 char *out = page;
3683 int len;
3685 len = sprintf(out,"%s\n", tape->name);
3686 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
3689 static ide_proc_entry_t idetape_proc[] = {
3690 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3691 { NULL, 0, NULL, NULL }
3694 #else
3696 #define idetape_proc NULL
3698 #endif
3701 * IDE subdriver functions, registered with ide.c
3703 static ide_driver_t idetape_driver = {
3704 "ide-tape", /* name */
3705 IDETAPE_VERSION, /* version */
3706 ide_tape, /* media */
3707 1, /* busy */
3708 1, /* supports_dma */
3709 1, /* supports_dsc_overlap */
3710 idetape_cleanup, /* cleanup */
3711 idetape_do_request, /* do_request */
3712 idetape_end_request, /* end_request */
3713 idetape_blkdev_ioctl, /* ioctl */
3714 idetape_blkdev_open, /* open */
3715 idetape_blkdev_release, /* release */
3716 NULL, /* media_change */
3717 idetape_pre_reset, /* pre_reset */
3718 NULL, /* capacity */
3719 NULL, /* special */
3720 idetape_proc /* proc */
3723 int idetape_init (void);
3724 static ide_module_t idetape_module = {
3725 IDE_DRIVER_MODULE,
3726 idetape_init,
3727 &idetape_driver,
3728 NULL
3732 * Our character device supporting functions, passed to register_chrdev.
3734 static struct file_operations idetape_fops = {
3735 NULL, /* lseek - default */
3736 idetape_chrdev_read, /* read */
3737 idetape_chrdev_write, /* write */
3738 NULL, /* readdir - bad */
3739 NULL, /* poll */
3740 idetape_chrdev_ioctl, /* ioctl */
3741 NULL, /* mmap */
3742 idetape_chrdev_open, /* open */
3743 NULL, /* flush */
3744 idetape_chrdev_release, /* release */
3745 NULL, /* fsync */
3746 NULL, /* fasync */
3747 NULL, /* check_media_change */
3748 NULL /* revalidate */
3752 * idetape_init will register the driver for each tape.
3754 int idetape_init (void)
3756 ide_drive_t *drive;
3757 idetape_tape_t *tape;
3758 int minor, failed = 0, supported = 0;
3760 MOD_INC_USE_COUNT;
3761 if (!idetape_chrdev_present)
3762 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
3763 idetape_chrdevs[minor].drive = NULL;
3765 if ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) == NULL) {
3766 ide_register_module (&idetape_module);
3767 MOD_DEC_USE_COUNT;
3768 return 0;
3770 if (!idetape_chrdev_present && register_chrdev (IDETAPE_MAJOR, "ht", &idetape_fops)) {
3771 printk (KERN_ERR "ide-tape: Failed to register character device interface\n");
3772 MOD_DEC_USE_COUNT;
3773 return -EBUSY;
3775 do {
3776 if (!idetape_identify_device (drive, drive->id)) {
3777 printk (KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3778 continue;
3780 tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
3781 if (tape == NULL) {
3782 printk (KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3783 continue;
3785 if (ide_register_subdriver (drive, &idetape_driver, IDE_SUBDRIVER_VERSION)) {
3786 printk (KERN_ERR "ide-tape: %s: Failed to register the driver with ide.c\n", drive->name);
3787 kfree (tape);
3788 continue;
3790 for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
3791 idetape_setup (drive, tape, minor);
3792 idetape_chrdevs[minor].drive = drive;
3793 supported++; failed--;
3794 } while ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) != NULL);
3795 if (!idetape_chrdev_present && !supported) {
3796 unregister_chrdev (IDETAPE_MAJOR, "ht");
3797 } else
3798 idetape_chrdev_present = 1;
3799 ide_register_module (&idetape_module);
3800 MOD_DEC_USE_COUNT;
3801 return 0;
3804 #ifdef MODULE
3805 int init_module (void)
3807 return idetape_init ();
3810 void cleanup_module (void)
3812 ide_drive_t *drive;
3813 int minor;
3815 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++) {
3816 drive = idetape_chrdevs[minor].drive;
3817 if (drive) {
3818 if (idetape_cleanup (drive))
3819 printk (KERN_ERR "ide-tape: %s: cleanup_module() called while still busy\n", drive->name);
3820 /* We must remove proc entries defined in this module.
3821 Otherwise we oops while accessing these entries */
3822 if (drive->proc)
3823 ide_remove_proc_entries(drive->proc, idetape_proc);
3826 ide_unregister_module(&idetape_module);
3828 #endif /* MODULE */