Import 2.3.28
[davej-history.git] / drivers / block / ide-tape.c
blob41a3b50456a6b3e046f7f67b38a84718680bacc8
1 /*
2 * linux/drivers/block/ide-tape.c Version 1.15 Jul 4, 1999
4 * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
6 * This driver was constructed as a student project in the software laboratory
7 * of the faculty of electrical engineering in the Technion - Israel's
8 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 * It is hereby placed under the terms of the GNU general public license.
11 * (See linux/COPYING).
15 * IDE ATAPI streaming tape driver.
17 * This driver is a part of the Linux ide driver and works in co-operation
18 * with linux/drivers/block/ide.c.
20 * The driver, in co-operation with ide.c, basically traverses the
21 * request-list for the block device interface. The character device
22 * interface, on the other hand, creates new requests, adds them
23 * to the request-list of the block device, and waits for their completion.
25 * Pipelined operation mode is now supported on both reads and writes.
27 * The block device major and minor numbers are determined from the
28 * tape's relative position in the ide interfaces, as explained in ide.c.
30 * The character device interface consists of the following devices:
32 * ht0 major 37, minor 0 first IDE tape, rewind on close.
33 * ht1 major 37, minor 1 second IDE tape, rewind on close.
34 * ...
35 * nht0 major 37, minor 128 first IDE tape, no rewind on close.
36 * nht1 major 37, minor 129 second IDE tape, no rewind on close.
37 * ...
39 * Run linux/scripts/MAKEDEV.ide to create the above entries.
41 * The general magnetic tape commands compatible interface, as defined by
42 * include/linux/mtio.h, is accessible through the character device.
44 * General ide driver configuration options, such as the interrupt-unmask
45 * flag, can be configured by issuing an ioctl to the block device interface,
46 * as any other ide device.
48 * Our own ide-tape ioctl's can be issued to either the block device or
49 * the character device interface.
51 * Maximal throughput with minimal bus load will usually be achieved in the
52 * following scenario:
54 * 1. ide-tape is operating in the pipelined operation mode.
55 * 2. No buffering is performed by the user backup program.
57 * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
59 * Ver 0.1 Nov 1 95 Pre-working code :-)
60 * Ver 0.2 Nov 23 95 A short backup (few megabytes) and restore procedure
61 * was successful ! (Using tar cvf ... on the block
62 * device interface).
63 * A longer backup resulted in major swapping, bad
64 * overall Linux performance and eventually failed as
65 * we received non serial read-ahead requests from the
66 * buffer cache.
67 * Ver 0.3 Nov 28 95 Long backups are now possible, thanks to the
68 * character device interface. Linux's responsiveness
69 * and performance doesn't seem to be much affected
70 * from the background backup procedure.
71 * Some general mtio.h magnetic tape operations are
72 * now supported by our character device. As a result,
73 * popular tape utilities are starting to work with
74 * ide tapes :-)
75 * The following configurations were tested:
76 * 1. An IDE ATAPI TAPE shares the same interface
77 * and irq with an IDE ATAPI CDROM.
78 * 2. An IDE ATAPI TAPE shares the same interface
79 * and irq with a normal IDE disk.
80 * Both configurations seemed to work just fine !
81 * However, to be on the safe side, it is meanwhile
82 * recommended to give the IDE TAPE its own interface
83 * and irq.
84 * The one thing which needs to be done here is to
85 * add a "request postpone" feature to ide.c,
86 * so that we won't have to wait for the tape to finish
87 * performing a long media access (DSC) request (such
88 * as a rewind) before we can access the other device
89 * on the same interface. This effect doesn't disturb
90 * normal operation most of the time because read/write
91 * requests are relatively fast, and once we are
92 * performing one tape r/w request, a lot of requests
93 * from the other device can be queued and ide.c will
94 * service all of them after this single tape request.
95 * Ver 1.0 Dec 11 95 Integrated into Linux 1.3.46 development tree.
96 * On each read / write request, we now ask the drive
97 * if we can transfer a constant number of bytes
98 * (a parameter of the drive) only to its buffers,
99 * without causing actual media access. If we can't,
100 * we just wait until we can by polling the DSC bit.
101 * This ensures that while we are not transferring
102 * more bytes than the constant referred to above, the
103 * interrupt latency will not become too high and
104 * we won't cause an interrupt timeout, as happened
105 * occasionally in the previous version.
106 * While polling for DSC, the current request is
107 * postponed and ide.c is free to handle requests from
108 * the other device. This is handled transparently to
109 * ide.c. The hwgroup locking method which was used
110 * in the previous version was removed.
111 * Use of new general features which are provided by
112 * ide.c for use with atapi devices.
113 * (Programming done by Mark Lord)
114 * Few potential bug fixes (Again, suggested by Mark)
115 * Single character device data transfers are now
116 * not limited in size, as they were before.
117 * We are asking the tape about its recommended
118 * transfer unit and send a larger data transfer
119 * as several transfers of the above size.
120 * For best results, use an integral number of this
121 * basic unit (which is shown during driver
122 * initialization). I will soon add an ioctl to get
123 * this important parameter.
124 * Our data transfer buffer is allocated on startup,
125 * rather than before each data transfer. This should
126 * ensure that we will indeed have a data buffer.
127 * Ver 1.1 Dec 14 95 Fixed random problems which occurred when the tape
128 * shared an interface with another device.
129 * (poll_for_dsc was a complete mess).
130 * Removed some old (non-active) code which had
131 * to do with supporting buffer cache originated
132 * requests.
133 * The block device interface can now be opened, so
134 * that general ide driver features like the unmask
135 * interrupts flag can be selected with an ioctl.
136 * This is the only use of the block device interface.
137 * New fast pipelined operation mode (currently only on
138 * writes). When using the pipelined mode, the
139 * throughput can potentially reach the maximum
140 * tape supported throughput, regardless of the
141 * user backup program. On my tape drive, it sometimes
142 * boosted performance by a factor of 2. Pipelined
143 * mode is enabled by default, but since it has a few
144 * downfalls as well, you may want to disable it.
145 * A short explanation of the pipelined operation mode
146 * is available below.
147 * Ver 1.2 Jan 1 96 Eliminated pipelined mode race condition.
148 * Added pipeline read mode. As a result, restores
149 * are now as fast as backups.
150 * Optimized shared interface behavior. The new behavior
151 * typically results in better IDE bus efficiency and
152 * higher tape throughput.
153 * Pre-calculation of the expected read/write request
154 * service time, based on the tape's parameters. In
155 * the pipelined operation mode, this allows us to
156 * adjust our polling frequency to a much lower value,
157 * and thus to dramatically reduce our load on Linux,
158 * without any decrease in performance.
159 * Implemented additional mtio.h operations.
160 * The recommended user block size is returned by
161 * the MTIOCGET ioctl.
162 * Additional minor changes.
163 * Ver 1.3 Feb 9 96 Fixed pipelined read mode bug which prevented the
164 * use of some block sizes during a restore procedure.
165 * The character device interface will now present a
166 * continuous view of the media - any mix of block sizes
167 * during a backup/restore procedure is supported. The
168 * driver will buffer the requests internally and
169 * convert them to the tape's recommended transfer
170 * unit, making performance almost independent of the
171 * chosen user block size.
172 * Some improvements in error recovery.
173 * By cooperating with ide-dma.c, bus mastering DMA can
174 * now sometimes be used with IDE tape drives as well.
175 * Bus mastering DMA has the potential to dramatically
176 * reduce the CPU's overhead when accessing the device,
177 * and can be enabled by using hdparm -d1 on the tape's
178 * block device interface. For more info, read the
179 * comments in ide-dma.c.
180 * Ver 1.4 Mar 13 96 Fixed serialize support.
181 * Ver 1.5 Apr 12 96 Fixed shared interface operation, broken in 1.3.85.
182 * Fixed pipelined read mode inefficiency.
183 * Fixed nasty null dereferencing bug.
184 * Ver 1.6 Aug 16 96 Fixed FPU usage in the driver.
185 * Fixed end of media bug.
186 * Ver 1.7 Sep 10 96 Minor changes for the CONNER CTT8000-A model.
187 * Ver 1.8 Sep 26 96 Attempt to find a better balance between good
188 * interactive response and high system throughput.
189 * Ver 1.9 Nov 5 96 Automatically cross encountered filemarks rather
190 * than requiring an explicit FSF command.
191 * Abort pending requests at end of media.
192 * MTTELL was sometimes returning incorrect results.
193 * Return the real block size in the MTIOCGET ioctl.
194 * Some error recovery bug fixes.
195 * Ver 1.10 Nov 5 96 Major reorganization.
196 * Reduced CPU overhead a bit by eliminating internal
197 * bounce buffers.
198 * Added module support.
199 * Added multiple tape drives support.
200 * Added partition support.
201 * Rewrote DSC handling.
202 * Some portability fixes.
203 * Removed ide-tape.h.
204 * Additional minor changes.
205 * Ver 1.11 Dec 2 96 Bug fix in previous DSC timeout handling.
206 * Use ide_stall_queue() for DSC overlap.
207 * Use the maximum speed rather than the current speed
208 * to compute the request service time.
209 * Ver 1.12 Dec 7 97 Fix random memory overwriting and/or last block data
210 * corruption, which could occur if the total number
211 * of bytes written to the tape was not an integral
212 * number of tape blocks.
213 * Add support for INTERRUPT DRQ devices.
214 * Ver 1.13 Jan 2 98 Add "speed == 0" work-around for HP COLORADO 5GB
215 * Ver 1.14 Dec 30 98 Partial fixes for the Sony/AIWA tape drives.
216 * Replace cli()/sti() with hwgroup spinlocks.
217 * Ver 1.15 Mar 25 99 Fix SMP race condition by replacing hwgroup
218 * spinlock with private per-tape spinlock.
219 * Fix use of freed memory.
221 * Here are some words from the first releases of hd.c, which are quoted
222 * in ide.c and apply here as well:
224 * | Special care is recommended. Have Fun!
229 * An overview of the pipelined operation mode.
231 * In the pipelined write mode, we will usually just add requests to our
232 * pipeline and return immediately, before we even start to service them. The
233 * user program will then have enough time to prepare the next request while
234 * we are still busy servicing previous requests. In the pipelined read mode,
235 * the situation is similar - we add read-ahead requests into the pipeline,
236 * before the user even requested them.
238 * The pipeline can be viewed as a "safety net" which will be activated when
239 * the system load is high and prevents the user backup program from keeping up
240 * with the current tape speed. At this point, the pipeline will get
241 * shorter and shorter but the tape will still be streaming at the same speed.
242 * Assuming we have enough pipeline stages, the system load will hopefully
243 * decrease before the pipeline is completely empty, and the backup program
244 * will be able to "catch up" and refill the pipeline again.
246 * When using the pipelined mode, it would be best to disable any type of
247 * buffering done by the user program, as ide-tape already provides all the
248 * benefits in the kernel, where it can be done in a more efficient way.
249 * As we will usually not block the user program on a request, the most
250 * efficient user code will then be a simple read-write-read-... cycle.
251 * Any additional logic will usually just slow down the backup process.
253 * Using the pipelined mode, I get a constant over 400 KBps throughput,
254 * which seems to be the maximum throughput supported by my tape.
256 * However, there are some downfalls:
258 * 1. We use memory (for data buffers) in proportional to the number
259 * of pipeline stages (each stage is about 26 KB with my tape).
260 * 2. In the pipelined write mode, we cheat and postpone error codes
261 * to the user task. In read mode, the actual tape position
262 * will be a bit further than the last requested block.
264 * Concerning (1):
266 * 1. We allocate stages dynamically only when we need them. When
267 * we don't need them, we don't consume additional memory. In
268 * case we can't allocate stages, we just manage without them
269 * (at the expense of decreased throughput) so when Linux is
270 * tight in memory, we will not pose additional difficulties.
272 * 2. The maximum number of stages (which is, in fact, the maximum
273 * amount of memory) which we allocate is limited by the compile
274 * time parameter IDETAPE_MAX_PIPELINE_STAGES.
276 * 3. The maximum number of stages is a controlled parameter - We
277 * don't start from the user defined maximum number of stages
278 * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
279 * will not even allocate this amount of stages if the user
280 * program can't handle the speed). We then implement a feedback
281 * loop which checks if the pipeline is empty, and if it is, we
282 * increase the maximum number of stages as necessary until we
283 * reach the optimum value which just manages to keep the tape
284 * busy with minimum allocated memory or until we reach
285 * IDETAPE_MAX_PIPELINE_STAGES.
287 * Concerning (2):
289 * In pipelined write mode, ide-tape can not return accurate error codes
290 * to the user program since we usually just add the request to the
291 * pipeline without waiting for it to be serviced. In case an error
292 * occurs, I will report it on the next user request.
294 * In the pipelined read mode, subsequent read requests or forward
295 * filemark spacing will perform correctly, as we preserve all blocks
296 * and filemarks which we encountered during our excess read-ahead.
298 * For accurate tape positioning and error reporting, disabling
299 * pipelined mode might be the best option.
301 * You can enable/disable/tune the pipelined operation mode by adjusting
302 * the compile time parameters below.
306 * Possible improvements.
308 * 1. Support for the ATAPI overlap protocol.
310 * In order to maximize bus throughput, we currently use the DSC
311 * overlap method which enables ide.c to service requests from the
312 * other device while the tape is busy executing a command. The
313 * DSC overlap method involves polling the tape's status register
314 * for the DSC bit, and servicing the other device while the tape
315 * isn't ready.
317 * In the current QIC development standard (December 1995),
318 * it is recommended that new tape drives will *in addition*
319 * implement the ATAPI overlap protocol, which is used for the
320 * same purpose - efficient use of the IDE bus, but is interrupt
321 * driven and thus has much less CPU overhead.
323 * ATAPI overlap is likely to be supported in most new ATAPI
324 * devices, including new ATAPI cdroms, and thus provides us
325 * a method by which we can achieve higher throughput when
326 * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
329 #define IDETAPE_VERSION "1.13"
331 #include <linux/config.h>
332 #include <linux/module.h>
333 #include <linux/types.h>
334 #include <linux/string.h>
335 #include <linux/kernel.h>
336 #include <linux/delay.h>
337 #include <linux/timer.h>
338 #include <linux/mm.h>
339 #include <linux/interrupt.h>
340 #include <linux/major.h>
341 #include <linux/errno.h>
342 #include <linux/genhd.h>
343 #include <linux/malloc.h>
344 #include <linux/pci.h>
345 #include <linux/ide.h>
347 #include <asm/byteorder.h>
348 #include <asm/irq.h>
349 #include <asm/uaccess.h>
350 #include <asm/io.h>
351 #include <asm/unaligned.h>
352 #include <asm/bitops.h>
355 * For general magnetic tape device compatibility.
357 #include <linux/mtio.h>
359 /**************************** Tunable parameters *****************************/
362 * Pipelined mode parameters.
364 * We try to use the minimum number of stages which is enough to
365 * keep the tape constantly streaming. To accomplish that, we implement
366 * a feedback loop around the maximum number of stages:
368 * We start from MIN maximum stages (we will not even use MIN stages
369 * if we don't need them), increment it by RATE*(MAX-MIN)
370 * whenever we sense that the pipeline is empty, until we reach
371 * the optimum value or until we reach MAX.
373 * Setting the following parameter to 0 will disable the pipelined mode.
375 #define IDETAPE_MIN_PIPELINE_STAGES 100
376 #define IDETAPE_MAX_PIPELINE_STAGES 200
377 #define IDETAPE_INCREASE_STAGES_RATE 20
380 * Assuming the tape shares an interface with another device, the default
381 * behavior is to service our pending pipeline requests as soon as
382 * possible, but to gracefully postpone them in favor of the other device
383 * when the tape is busy. This has the potential to maximize our
384 * throughput and in the same time, to make efficient use of the IDE bus.
386 * Note that when we transfer data to / from the tape, we co-operate with
387 * the relatively fast tape buffers and the tape will perform the
388 * actual media access in the background, without blocking the IDE
389 * bus. This means that as long as the maximum IDE bus throughput is much
390 * higher than the sum of our maximum throughput and the maximum
391 * throughput of the other device, we should probably leave the default
392 * behavior.
394 * However, if it is still desired to give the other device a share even
395 * in our own (small) bus bandwidth, you can set IDETAPE_LOW_TAPE_PRIORITY
396 * to 1. This will let the other device finish *all* its pending requests
397 * before we even check if we can service our next pending request.
399 #define IDETAPE_LOW_TAPE_PRIORITY 0
402 * The following are used to debug the driver:
404 * Setting IDETAPE_INFO_LOG to 1 will log driver vender information.
405 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
406 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
407 * some places.
409 * Setting them to 0 will restore normal operation mode:
411 * 1. Disable logging normal successful operations.
412 * 2. Disable self-sanity checks.
413 * 3. Errors will still be logged, of course.
415 * All the #if DEBUG code will be removed some day, when the driver
416 * is verified to be stable enough. This will make it much more
417 * esthetic.
419 #define IDETAPE_INFO_LOG 0
420 #define IDETAPE_DEBUG_LOG 0
421 #define IDETAPE_DEBUG_BUGS 1
423 #if IDETAPE_DEBUG_LOG
424 #undef IDETAPE_INFO_LOG
425 #define IDETAPE_INFO_LOG IDETAPE_DEBUG_LOG
426 #endif
429 * After each failed packet command we issue a request sense command
430 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
432 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
434 #define IDETAPE_MAX_PC_RETRIES 3
437 * With each packet command, we allocate a buffer of
438 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
439 * commands (Not for READ/WRITE commands).
441 #define IDETAPE_PC_BUFFER_SIZE 256
444 * In various places in the driver, we need to allocate storage
445 * for packet commands and requests, which will remain valid while
446 * we leave the driver to wait for an interrupt or a timeout event.
448 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
451 * DSC polling parameters.
453 * Polling for DSC (a single bit in the status register) is a very
454 * important function in ide-tape. There are two cases in which we
455 * poll for DSC:
457 * 1. Before a read/write packet command, to ensure that we
458 * can transfer data from/to the tape's data buffers, without
459 * causing an actual media access. In case the tape is not
460 * ready yet, we take out our request from the device
461 * request queue, so that ide.c will service requests from
462 * the other device on the same interface meanwhile.
464 * 2. After the successful initialization of a "media access
465 * packet command", which is a command which can take a long
466 * time to complete (it can be several seconds or even an hour).
468 * Again, we postpone our request in the middle to free the bus
469 * for the other device. The polling frequency here should be
470 * lower than the read/write frequency since those media access
471 * commands are slow. We start from a "fast" frequency -
472 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
473 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
474 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
476 * We also set a timeout for the timer, in case something goes wrong.
477 * The timeout should be longer then the maximum execution time of a
478 * tape operation.
482 * The following parameter is used to select the point in the internal
483 * tape fifo in which we will start to refill the buffer. Decreasing
484 * the following parameter will improve the system's latency and
485 * interactive response, while using a high value might improve sytem
486 * throughput.
488 #define IDETAPE_FIFO_THRESHOLD 2
491 * Some tape drives require a long irq timeout
493 #define IDETAPE_WAIT_CMD (60*HZ)
496 * DSC timings.
498 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
499 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
500 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
501 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
502 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
503 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
504 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
506 /*************************** End of tunable parameters ***********************/
508 typedef enum {
509 idetape_direction_none,
510 idetape_direction_read,
511 idetape_direction_write
512 } idetape_chrdev_direction_t;
515 * Our view of a packet command.
517 typedef struct idetape_packet_command_s {
518 u8 c[12]; /* Actual packet bytes */
519 int retries; /* On each retry, we increment retries */
520 int error; /* Error code */
521 int request_transfer; /* Bytes to transfer */
522 int actually_transferred; /* Bytes actually transferred */
523 int buffer_size; /* Size of our data buffer */
524 struct buffer_head *bh;
525 char *b_data;
526 int b_count;
527 byte *buffer; /* Data buffer */
528 byte *current_position; /* Pointer into the above buffer */
529 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
530 byte pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
531 unsigned int flags; /* Status/Action bit flags */
532 } idetape_pc_t;
535 * Packet command flag bits.
537 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
538 #define PC_WAIT_FOR_DSC 1 /* 1 When polling for DSC on a media access command */
539 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
540 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
541 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
542 #define PC_WRITING 5 /* Data direction */
545 * Capabilities and Mechanical Status Page
547 typedef struct {
548 unsigned page_code :6; /* Page code - Should be 0x2a */
549 unsigned reserved1_67 :2;
550 u8 page_length; /* Page Length - Should be 0x12 */
551 u8 reserved2, reserved3;
552 unsigned ro :1; /* Read Only Mode */
553 unsigned reserved4_1234 :4;
554 unsigned sprev :1; /* Supports SPACE in the reverse direction */
555 unsigned reserved4_67 :2;
556 unsigned reserved5_012 :3;
557 unsigned efmt :1; /* Supports ERASE command initiated formatting */
558 unsigned reserved5_4 :1;
559 unsigned qfa :1; /* Supports the QFA two partition formats */
560 unsigned reserved5_67 :2;
561 unsigned lock :1; /* Supports locking the volume */
562 unsigned locked :1; /* The volume is locked */
563 unsigned prevent :1; /* The device defaults in the prevent state after power up */
564 unsigned eject :1; /* The device can eject the volume */
565 unsigned reserved6_45 :2; /* Reserved */
566 unsigned ecc :1; /* Supports error correction */
567 unsigned cmprs :1; /* Supports data compression */
568 unsigned reserved7_0 :1;
569 unsigned blk512 :1; /* Supports 512 bytes block size */
570 unsigned blk1024 :1; /* Supports 1024 bytes block size */
571 unsigned reserved7_3_6 :4;
572 unsigned slowb :1; /* The device restricts the byte count for PIO */
573 /* transfers for slow buffer memory ??? */
574 u16 max_speed; /* Maximum speed supported in KBps */
575 u8 reserved10, reserved11;
576 u16 ctl; /* Continuous Transfer Limit in blocks */
577 u16 speed; /* Current Speed, in KBps */
578 u16 buffer_size; /* Buffer Size, in 512 bytes */
579 u8 reserved18, reserved19;
580 } idetape_capabilities_page_t;
583 * A pipeline stage.
585 typedef struct idetape_stage_s {
586 struct request rq; /* The corresponding request */
587 struct buffer_head *bh; /* The data buffers */
588 struct idetape_stage_s *next; /* Pointer to the next stage */
589 } idetape_stage_t;
592 * Most of our global data which we need to save even as we leave the
593 * driver due to an interrupt or a timer event is stored in a variable
594 * of type idetape_tape_t, defined below.
596 typedef struct {
597 ide_drive_t *drive;
600 * Since a typical character device operation requires more
601 * than one packet command, we provide here enough memory
602 * for the maximum of interconnected packet commands.
603 * The packet commands are stored in the circular array pc_stack.
604 * pc_stack_index points to the last used entry, and warps around
605 * to the start when we get to the last array entry.
607 * pc points to the current processed packet command.
609 * failed_pc points to the last failed packet command, or contains
610 * NULL if we do not need to retry any packet command. This is
611 * required since an additional packet command is needed before the
612 * retry, to get detailed information on what went wrong.
614 idetape_pc_t *pc; /* Current packet command */
615 idetape_pc_t *failed_pc; /* Last failed packet command */
616 idetape_pc_t pc_stack[IDETAPE_PC_STACK];/* Packet command stack */
617 int pc_stack_index; /* Next free packet command storage space */
618 struct request rq_stack[IDETAPE_PC_STACK];
619 int rq_stack_index; /* We implement a circular array */
622 * DSC polling variables.
624 * While polling for DSC we use postponed_rq to postpone the
625 * current request so that ide.c will be able to service
626 * pending requests on the other device. Note that at most
627 * we will have only one DSC (usually data transfer) request
628 * in the device request queue. Additional requests can be
629 * queued in our internal pipeline, but they will be visible
630 * to ide.c only one at a time.
632 struct request *postponed_rq;
633 unsigned long dsc_polling_start; /* The time in which we started polling for DSC */
634 struct timer_list dsc_timer; /* Timer used to poll for dsc */
635 unsigned long best_dsc_rw_frequency; /* Read/Write dsc polling frequency */
636 unsigned long dsc_polling_frequency; /* The current polling frequency */
637 unsigned long dsc_timeout; /* Maximum waiting time */
640 * Position information
642 byte partition;
643 unsigned int block_address; /* Current block */
646 * Last error information
648 byte sense_key, asc, ascq;
651 * Character device operation
653 unsigned int minor;
654 char name[4]; /* device name */
655 idetape_chrdev_direction_t chrdev_direction; /* Current character device data transfer direction */
658 * Device information
660 unsigned short tape_block_size; /* Usually 512 or 1024 bytes */
661 int user_bs_factor;
662 idetape_capabilities_page_t capabilities; /* Copy of the tape's Capabilities and Mechanical Page */
665 * Active data transfer request parameters.
667 * At most, there is only one ide-tape originated data transfer
668 * request in the device request queue. This allows ide.c to
669 * easily service requests from the other device when we
670 * postpone our active request. In the pipelined operation
671 * mode, we use our internal pipeline structure to hold
672 * more data requests.
674 * The data buffer size is chosen based on the tape's
675 * recommendation.
677 struct request *active_data_request; /* Pointer to the request which is waiting in the device request queue */
678 int stage_size; /* Data buffer size (chosen based on the tape's recommendation */
679 idetape_stage_t *merge_stage;
680 int merge_stage_size;
681 struct buffer_head *bh;
682 char *b_data;
683 int b_count;
686 * Pipeline parameters.
688 * To accomplish non-pipelined mode, we simply set the following
689 * variables to zero (or NULL, where appropriate).
691 int nr_stages; /* Number of currently used stages */
692 int nr_pending_stages; /* Number of pending stages */
693 int max_stages, min_pipeline, max_pipeline; /* We will not allocate more than this number of stages */
694 idetape_stage_t *first_stage; /* The first stage which will be removed from the pipeline */
695 idetape_stage_t *active_stage; /* The currently active stage */
696 idetape_stage_t *next_stage; /* Will be serviced after the currently active request */
697 idetape_stage_t *last_stage; /* New requests will be added to the pipeline here */
698 idetape_stage_t *cache_stage; /* Optional free stage which we can use */
699 int pages_per_stage;
700 int excess_bh_size; /* Wasted space in each stage */
702 unsigned int flags; /* Status/Action flags */
703 spinlock_t spinlock; /* protects the ide-tape queue */
704 } idetape_tape_t;
707 * Tape flag bits values.
709 #define IDETAPE_IGNORE_DSC 0
710 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
711 #define IDETAPE_BUSY 2 /* Device already opened */
712 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
713 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
714 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
715 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
718 * Supported ATAPI tape drives packet commands
720 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
721 #define IDETAPE_REWIND_CMD 0x01
722 #define IDETAPE_REQUEST_SENSE_CMD 0x03
723 #define IDETAPE_READ_CMD 0x08
724 #define IDETAPE_WRITE_CMD 0x0a
725 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
726 #define IDETAPE_SPACE_CMD 0x11
727 #define IDETAPE_INQUIRY_CMD 0x12
728 #define IDETAPE_ERASE_CMD 0x19
729 #define IDETAPE_MODE_SENSE_CMD 0x1a
730 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
731 #define IDETAPE_LOCATE_CMD 0x2b
732 #define IDETAPE_READ_POSITION_CMD 0x34
735 * Some defines for the SPACE command
737 #define IDETAPE_SPACE_OVER_FILEMARK 1
738 #define IDETAPE_SPACE_TO_EOD 3
741 * Some defines for the LOAD UNLOAD command
743 #define IDETAPE_LU_LOAD_MASK 1
744 #define IDETAPE_LU_RETENSION_MASK 2
745 #define IDETAPE_LU_EOT_MASK 4
748 * Special requests for our block device strategy routine.
750 * In order to service a character device command, we add special
751 * requests to the tail of our block device request queue and wait
752 * for their completion.
755 #define IDETAPE_FIRST_RQ 90
758 * IDETAPE_PC_RQ is used to queue a packet command in the request queue.
760 #define IDETAPE_PC_RQ1 90
761 #define IDETAPE_PC_RQ2 91
764 * IDETAPE_READ_RQ and IDETAPE_WRITE_RQ are used by our
765 * character device interface to request read/write operations from
766 * our block device interface.
768 #define IDETAPE_READ_RQ 92
769 #define IDETAPE_WRITE_RQ 93
770 #define IDETAPE_ABORTED_WRITE_RQ 94
772 #define IDETAPE_LAST_RQ 94
775 * A macro which can be used to check if a we support a given
776 * request command.
778 #define IDETAPE_RQ_CMD(cmd) ((cmd >= IDETAPE_FIRST_RQ) && (cmd <= IDETAPE_LAST_RQ))
781 * Error codes which are returned in rq->errors to the higher part
782 * of the driver.
784 #define IDETAPE_ERROR_GENERAL 101
785 #define IDETAPE_ERROR_FILEMARK 102
786 #define IDETAPE_ERROR_EOD 103
789 * The ATAPI Status Register.
791 typedef union {
792 unsigned all :8;
793 struct {
794 unsigned check :1; /* Error occurred */
795 unsigned idx :1; /* Reserved */
796 unsigned corr :1; /* Correctable error occurred */
797 unsigned drq :1; /* Data is request by the device */
798 unsigned dsc :1; /* Buffer availability / Media access command finished */
799 unsigned reserved5 :1; /* Reserved */
800 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
801 unsigned bsy :1; /* The device has access to the command block */
802 } b;
803 } idetape_status_reg_t;
806 * The ATAPI error register.
808 typedef union {
809 unsigned all :8;
810 struct {
811 unsigned ili :1; /* Illegal Length Indication */
812 unsigned eom :1; /* End Of Media Detected */
813 unsigned abrt :1; /* Aborted command - As defined by ATA */
814 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
815 unsigned sense_key :4; /* Sense key of the last failed packet command */
816 } b;
817 } idetape_error_reg_t;
820 * ATAPI Feature Register
822 typedef union {
823 unsigned all :8;
824 struct {
825 unsigned dma :1; /* Using DMA of PIO */
826 unsigned reserved321 :3; /* Reserved */
827 unsigned reserved654 :3; /* Reserved (Tag Type) */
828 unsigned reserved7 :1; /* Reserved */
829 } b;
830 } idetape_feature_reg_t;
833 * ATAPI Byte Count Register.
835 typedef union {
836 unsigned all :16;
837 struct {
838 unsigned low :8; /* LSB */
839 unsigned high :8; /* MSB */
840 } b;
841 } idetape_bcount_reg_t;
844 * ATAPI Interrupt Reason Register.
846 typedef union {
847 unsigned all :8;
848 struct {
849 unsigned cod :1; /* Information transferred is command (1) or data (0) */
850 unsigned io :1; /* The device requests us to read (1) or write (0) */
851 unsigned reserved :6; /* Reserved */
852 } b;
853 } idetape_ireason_reg_t;
856 * ATAPI Drive Select Register
858 typedef union {
859 unsigned all :8;
860 struct {
861 unsigned sam_lun :4; /* Should be zero with ATAPI (not used) */
862 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
863 unsigned one5 :1; /* Should be set to 1 */
864 unsigned reserved6 :1; /* Reserved */
865 unsigned one7 :1; /* Should be set to 1 */
866 } b;
867 } idetape_drivesel_reg_t;
870 * ATAPI Device Control Register
872 typedef union {
873 unsigned all :8;
874 struct {
875 unsigned zero0 :1; /* Should be set to zero */
876 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
877 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
878 unsigned one3 :1; /* Should be set to 1 */
879 unsigned reserved4567 :4; /* Reserved */
880 } b;
881 } idetape_control_reg_t;
884 * idetape_chrdev_t provides the link between out character device
885 * interface and our block device interface and the corresponding
886 * ide_drive_t structure.
888 typedef struct {
889 ide_drive_t *drive;
890 } idetape_chrdev_t;
893 * The following is used to format the general configuration word of
894 * the ATAPI IDENTIFY DEVICE command.
896 struct idetape_id_gcw {
897 unsigned packet_size :2; /* Packet Size */
898 unsigned reserved234 :3; /* Reserved */
899 unsigned drq_type :2; /* Command packet DRQ type */
900 unsigned removable :1; /* Removable media */
901 unsigned device_type :5; /* Device type */
902 unsigned reserved13 :1; /* Reserved */
903 unsigned protocol :2; /* Protocol type */
907 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
909 typedef struct {
910 unsigned device_type :5; /* Peripheral Device Type */
911 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
912 unsigned reserved1_6t0 :7; /* Reserved */
913 unsigned rmb :1; /* Removable Medium Bit */
914 unsigned ansi_version :3; /* ANSI Version */
915 unsigned ecma_version :3; /* ECMA Version */
916 unsigned iso_version :2; /* ISO Version */
917 unsigned response_format :4; /* Response Data Format */
918 unsigned reserved3_45 :2; /* Reserved */
919 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
920 unsigned reserved3_7 :1; /* AENC - Reserved */
921 u8 additional_length; /* Additional Length (total_length-4) */
922 u8 rsv5, rsv6, rsv7; /* Reserved */
923 u8 vendor_id[8]; /* Vendor Identification */
924 u8 product_id[16]; /* Product Identification */
925 u8 revision_level[4]; /* Revision Level */
926 u8 vendor_specific[20]; /* Vendor Specific - Optional */
927 u8 reserved56t95[40]; /* Reserved - Optional */
928 /* Additional information may be returned */
929 } idetape_inquiry_result_t;
932 * READ POSITION packet command - Data Format (From Table 6-57)
934 typedef struct {
935 unsigned reserved0_10 :2; /* Reserved */
936 unsigned bpu :1; /* Block Position Unknown */
937 unsigned reserved0_543 :3; /* Reserved */
938 unsigned eop :1; /* End Of Partition */
939 unsigned bop :1; /* Beginning Of Partition */
940 u8 partition; /* Partition Number */
941 u8 reserved2, reserved3; /* Reserved */
942 u32 first_block; /* First Block Location */
943 u32 last_block; /* Last Block Location (Optional) */
944 u8 reserved12; /* Reserved */
945 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
946 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
947 } idetape_read_position_result_t;
950 * REQUEST SENSE packet command result - Data Format.
952 typedef struct {
953 unsigned error_code :7; /* Current of deferred errors */
954 unsigned valid :1; /* The information field conforms to QIC-157C */
955 u8 reserved1 :8; /* Segment Number - Reserved */
956 unsigned sense_key :4; /* Sense Key */
957 unsigned reserved2_4 :1; /* Reserved */
958 unsigned ili :1; /* Incorrect Length Indicator */
959 unsigned eom :1; /* End Of Medium */
960 unsigned filemark :1; /* Filemark */
961 u32 information __attribute__ ((packed));
962 u8 asl; /* Additional sense length (n-7) */
963 u32 command_specific; /* Additional command specific information */
964 u8 asc; /* Additional Sense Code */
965 u8 ascq; /* Additional Sense Code Qualifier */
966 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
967 unsigned sk_specific1 :7; /* Sense Key Specific */
968 unsigned sksv :1; /* Sense Key Specific information is valid */
969 u8 sk_specific2; /* Sense Key Specific */
970 u8 sk_specific3; /* Sense Key Specific */
971 u8 pad[2]; /* Padding to 20 bytes */
972 } idetape_request_sense_result_t;
975 * Follows structures which are related to the SELECT SENSE / MODE SENSE
976 * packet commands. Those packet commands are still not supported
977 * by ide-tape.
979 #define IDETAPE_CAPABILITIES_PAGE 0x2a
982 * Mode Parameter Header for the MODE SENSE packet command
984 typedef struct {
985 u8 mode_data_length; /* Length of the following data transfer */
986 u8 medium_type; /* Medium Type */
987 u8 dsp; /* Device Specific Parameter */
988 u8 bdl; /* Block Descriptor Length */
989 } idetape_mode_parameter_header_t;
992 * Mode Parameter Block Descriptor the MODE SENSE packet command
994 * Support for block descriptors is optional.
996 typedef struct {
997 u8 density_code; /* Medium density code */
998 u8 blocks[3]; /* Number of blocks */
999 u8 reserved4; /* Reserved */
1000 u8 length[3]; /* Block Length */
1001 } idetape_parameter_block_descriptor_t;
1004 * The Data Compression Page, as returned by the MODE SENSE packet command.
1006 typedef struct {
1007 unsigned page_code :6; /* Page Code - Should be 0xf */
1008 unsigned reserved0 :1; /* Reserved */
1009 unsigned ps :1;
1010 u8 page_length; /* Page Length - Should be 14 */
1011 unsigned reserved2 :6; /* Reserved */
1012 unsigned dcc :1; /* Data Compression Capable */
1013 unsigned dce :1; /* Data Compression Enable */
1014 unsigned reserved3 :5; /* Reserved */
1015 unsigned red :2; /* Report Exception on Decompression */
1016 unsigned dde :1; /* Data Decompression Enable */
1017 u32 ca; /* Compression Algorithm */
1018 u32 da; /* Decompression Algorithm */
1019 u8 reserved[4]; /* Reserved */
1020 } idetape_data_compression_page_t;
1023 * The Medium Partition Page, as returned by the MODE SENSE packet command.
1025 typedef struct {
1026 unsigned page_code :6; /* Page Code - Should be 0x11 */
1027 unsigned reserved1_6 :1; /* Reserved */
1028 unsigned ps :1;
1029 u8 page_length; /* Page Length - Should be 6 */
1030 u8 map; /* Maximum Additional Partitions - Should be 0 */
1031 u8 apd; /* Additional Partitions Defined - Should be 0 */
1032 unsigned reserved4_012 :3; /* Reserved */
1033 unsigned psum :2; /* Should be 0 */
1034 unsigned idp :1; /* Should be 0 */
1035 unsigned sdp :1; /* Should be 0 */
1036 unsigned fdp :1; /* Fixed Data Partitions */
1037 u8 mfr; /* Medium Format Recognition */
1038 u8 reserved[2]; /* Reserved */
1039 } idetape_medium_partition_page_t;
1042 * Run time configurable parameters.
1044 typedef struct {
1045 int dsc_rw_frequency;
1046 int dsc_media_access_frequency;
1047 int nr_stages;
1048 } idetape_config_t;
1051 * The variables below are used for the character device interface.
1052 * Additional state variables are defined in our ide_drive_t structure.
1054 static idetape_chrdev_t idetape_chrdevs[MAX_HWIFS * MAX_DRIVES];
1055 static int idetape_chrdev_present = 0;
1058 * Too bad. The drive wants to send us data which we are not ready to accept.
1059 * Just throw it away.
1061 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1063 while (bcount--)
1064 IN_BYTE (IDE_DATA_REG);
1067 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1069 struct buffer_head *bh = pc->bh;
1070 int count;
1072 while (bcount) {
1073 #if IDETAPE_DEBUG_BUGS
1074 if (bh == NULL) {
1075 printk (KERN_ERR "ide-tape: bh == NULL in idetape_input_buffers\n");
1076 idetape_discard_data (drive, bcount);
1077 return;
1079 #endif /* IDETAPE_DEBUG_BUGS */
1080 count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), bcount);
1081 atapi_input_bytes (drive, bh->b_data + atomic_read(&bh->b_count), count);
1082 bcount -= count; atomic_add(count, &bh->b_count);
1083 if (atomic_read(&bh->b_count) == bh->b_size) {
1084 bh = bh->b_reqnext;
1085 if (bh)
1086 atomic_set(&bh->b_count, 0);
1089 pc->bh = bh;
1092 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1094 struct buffer_head *bh = pc->bh;
1095 int count;
1097 while (bcount) {
1098 #if IDETAPE_DEBUG_BUGS
1099 if (bh == NULL) {
1100 printk (KERN_ERR "ide-tape: bh == NULL in idetape_output_buffers\n");
1101 return;
1103 #endif /* IDETAPE_DEBUG_BUGS */
1104 count = IDE_MIN (pc->b_count, bcount);
1105 atapi_output_bytes (drive, pc->b_data, count);
1106 bcount -= count; pc->b_data += count; pc->b_count -= count;
1107 if (!pc->b_count) {
1108 pc->bh = bh = bh->b_reqnext;
1109 if (bh) {
1110 pc->b_data = bh->b_data;
1111 pc->b_count = atomic_read(&bh->b_count);
1117 #ifdef CONFIG_BLK_DEV_IDEDMA
1118 static void idetape_update_buffers (idetape_pc_t *pc)
1120 struct buffer_head *bh = pc->bh;
1121 int count, bcount = pc->actually_transferred;
1123 if (test_bit (PC_WRITING, &pc->flags))
1124 return;
1125 while (bcount) {
1126 #if IDETAPE_DEBUG_BUGS
1127 if (bh == NULL) {
1128 printk (KERN_ERR "ide-tape: bh == NULL in idetape_update_buffers\n");
1129 return;
1131 #endif /* IDETAPE_DEBUG_BUGS */
1132 count = IDE_MIN (bh->b_size, bcount);
1133 atomic_set(&bh->b_count, count);
1134 if (atomic_read(&bh->b_count) == bh->b_size)
1135 bh = bh->b_reqnext;
1136 bcount -= count;
1138 pc->bh = bh;
1140 #endif /* CONFIG_BLK_DEV_IDEDMA */
1143 * idetape_postpone_request postpones the current request so that
1144 * ide.c will be able to service requests from another device on
1145 * the same hwgroup while we are polling for DSC.
1147 static void idetape_postpone_request (ide_drive_t *drive)
1149 idetape_tape_t *tape = drive->driver_data;
1151 tape->postponed_rq = HWGROUP(drive)->rq;
1152 ide_stall_queue(drive, tape->dsc_polling_frequency);
1156 * idetape_queue_pc_head generates a new packet command request in front
1157 * of the request queue, before the current request, so that it will be
1158 * processed immediately, on the next pass through the driver.
1160 * idetape_queue_pc_head is called from the request handling part of
1161 * the driver (the "bottom" part). Safe storage for the request should
1162 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1163 * before calling idetape_queue_pc_head.
1165 * Memory for those requests is pre-allocated at initialization time, and
1166 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1167 * space for the maximum possible number of inter-dependent packet commands.
1169 * The higher level of the driver - The ioctl handler and the character
1170 * device handling functions should queue request to the lower level part
1171 * and wait for their completion using idetape_queue_pc_tail or
1172 * idetape_queue_rw_tail.
1174 static void idetape_queue_pc_head (ide_drive_t *drive,idetape_pc_t *pc,struct request *rq)
1176 ide_init_drive_cmd (rq);
1177 rq->buffer = (char *) pc;
1178 rq->cmd = IDETAPE_PC_RQ1;
1179 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
1183 * idetape_next_pc_storage returns a pointer to a place in which we can
1184 * safely store a packet command, even though we intend to leave the
1185 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1186 * commands is allocated at initialization time.
1188 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1190 idetape_tape_t *tape = drive->driver_data;
1192 #if IDETAPE_DEBUG_LOG
1193 printk (KERN_INFO "ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
1194 #endif /* IDETAPE_DEBUG_LOG */
1195 if (tape->pc_stack_index==IDETAPE_PC_STACK)
1196 tape->pc_stack_index=0;
1197 return (&tape->pc_stack[tape->pc_stack_index++]);
1201 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1202 * Since we queue packet commands in the request queue, we need to
1203 * allocate a request, along with the allocation of a packet command.
1206 /**************************************************************
1208 * This should get fixed to use kmalloc(GFP_ATOMIC, ..) *
1209 * followed later on by kfree(). -ml *
1211 **************************************************************/
1213 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1215 idetape_tape_t *tape = drive->driver_data;
1217 #if IDETAPE_DEBUG_LOG
1218 printk (KERN_INFO "ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
1219 #endif /* IDETAPE_DEBUG_LOG */
1220 if (tape->rq_stack_index==IDETAPE_PC_STACK)
1221 tape->rq_stack_index=0;
1222 return (&tape->rq_stack[tape->rq_stack_index++]);
1226 * Pipeline related functions
1229 static inline int idetape_pipeline_active (idetape_tape_t *tape)
1231 return tape->active_data_request != NULL;
1235 * idetape_kfree_stage calls kfree to completely free a stage, along with
1236 * its related buffers.
1238 static void __idetape_kfree_stage (idetape_stage_t *stage)
1240 struct buffer_head *prev_bh, *bh = stage->bh;
1241 int size;
1243 while (bh != NULL) {
1244 if (bh->b_data != NULL) {
1245 size = (int) bh->b_size;
1246 while (size > 0) {
1247 free_page ((unsigned long) bh->b_data);
1248 size -= PAGE_SIZE;
1249 bh->b_data += PAGE_SIZE;
1252 prev_bh = bh;
1253 bh = bh->b_reqnext;
1254 kfree (prev_bh);
1256 kfree (stage);
1259 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1261 if (tape->cache_stage == NULL)
1262 tape->cache_stage = stage;
1263 else
1264 __idetape_kfree_stage (stage);
1268 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1269 * stage, along with all the necessary small buffers which together make
1270 * a buffer of size tape->stage_size (or a bit more). We attempt to
1271 * combine sequential pages as much as possible.
1273 * Returns a pointer to the new allocated stage, or NULL if we
1274 * can't (or don't want to) allocate a stage.
1276 * Pipeline stages are optional and are used to increase performance.
1277 * If we can't allocate them, we'll manage without them.
1279 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape)
1281 idetape_stage_t *stage;
1282 struct buffer_head *prev_bh, *bh;
1283 int pages = tape->pages_per_stage;
1284 char *b_data;
1286 if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
1287 return NULL;
1288 stage->next = NULL;
1290 bh = stage->bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL);
1291 if (bh == NULL)
1292 goto abort;
1293 bh->b_reqnext = NULL;
1294 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1295 goto abort;
1296 bh->b_size = PAGE_SIZE;
1297 set_bit (BH_Lock, &bh->b_state);
1299 while (--pages) {
1300 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1301 goto abort;
1302 if (bh->b_data == b_data + PAGE_SIZE && virt_to_bus (bh->b_data) == virt_to_bus (b_data) + PAGE_SIZE) {
1303 bh->b_size += PAGE_SIZE;
1304 bh->b_data -= PAGE_SIZE;
1305 continue;
1307 if (b_data == bh->b_data + bh->b_size && virt_to_bus (b_data) == virt_to_bus (bh->b_data) + bh->b_size) {
1308 bh->b_size += PAGE_SIZE;
1309 continue;
1311 prev_bh = bh;
1312 if ((bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL)) == NULL) {
1313 free_page ((unsigned long) b_data);
1314 goto abort;
1316 bh->b_reqnext = NULL;
1317 bh->b_data = b_data;
1318 bh->b_size = PAGE_SIZE;
1319 set_bit (BH_Lock, &bh->b_state);
1320 prev_bh->b_reqnext = bh;
1322 bh->b_size -= tape->excess_bh_size;
1323 return stage;
1324 abort:
1325 __idetape_kfree_stage (stage);
1326 return NULL;
1329 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1331 idetape_stage_t *cache_stage = tape->cache_stage;
1333 #if IDETAPE_DEBUG_LOG
1334 printk (KERN_INFO "Reached idetape_kmalloc_stage\n");
1335 #endif /* IDETAPE_DEBUG_LOG */
1337 if (tape->nr_stages >= tape->max_stages)
1338 return NULL;
1339 if (cache_stage != NULL) {
1340 tape->cache_stage = NULL;
1341 return cache_stage;
1343 return __idetape_kmalloc_stage (tape);
1346 static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char *buf, int n)
1348 struct buffer_head *bh = tape->bh;
1349 int count;
1351 while (n) {
1352 #if IDETAPE_DEBUG_BUGS
1353 if (bh == NULL) {
1354 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_from_user\n");
1355 return;
1357 #endif /* IDETAPE_DEBUG_BUGS */
1358 count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), n);
1359 copy_from_user (bh->b_data + atomic_read(&bh->b_count), buf, count);
1360 n -= count; atomic_add(count, &bh->b_count); buf += count;
1361 if (atomic_read(&bh->b_count) == bh->b_size) {
1362 bh = bh->b_reqnext;
1363 if (bh)
1364 atomic_set(&bh->b_count, 0);
1367 tape->bh = bh;
1370 static void idetape_copy_stage_to_user (idetape_tape_t *tape, char *buf, idetape_stage_t *stage, int n)
1372 struct buffer_head *bh = tape->bh;
1373 int count;
1375 while (n) {
1376 #if IDETAPE_DEBUG_BUGS
1377 if (bh == NULL) {
1378 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_to_user\n");
1379 return;
1381 #endif /* IDETAPE_DEBUG_BUGS */
1382 count = IDE_MIN (tape->b_count, n);
1383 copy_to_user (buf, tape->b_data, count);
1384 n -= count; tape->b_data += count; tape->b_count -= count; buf += count;
1385 if (!tape->b_count) {
1386 tape->bh = bh = bh->b_reqnext;
1387 if (bh) {
1388 tape->b_data = bh->b_data;
1389 tape->b_count = atomic_read(&bh->b_count);
1395 static void idetape_init_merge_stage (idetape_tape_t *tape)
1397 struct buffer_head *bh = tape->merge_stage->bh;
1399 tape->bh = bh;
1400 if (tape->chrdev_direction == idetape_direction_write)
1401 atomic_set(&bh->b_count, 0);
1402 else {
1403 tape->b_data = bh->b_data;
1404 tape->b_count = atomic_read(&bh->b_count);
1408 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1410 struct buffer_head *tmp;
1412 tmp = stage->bh;
1413 stage->bh = tape->merge_stage->bh;
1414 tape->merge_stage->bh = tmp;
1415 idetape_init_merge_stage (tape);
1419 * idetape_increase_max_pipeline_stages is a part of the feedback
1420 * loop which tries to find the optimum number of stages. In the
1421 * feedback loop, we are starting from a minimum maximum number of
1422 * stages, and if we sense that the pipeline is empty, we try to
1423 * increase it, until we reach the user compile time memory limit.
1425 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1427 idetape_tape_t *tape = drive->driver_data;
1428 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1430 #if IDETAPE_DEBUG_LOG
1431 printk (KERN_INFO "Reached idetape_increase_max_pipeline_stages\n");
1432 #endif /* IDETAPE_DEBUG_LOG */
1434 tape->max_stages += increase;
1435 tape->max_stages = IDE_MAX(tape->max_stages, tape->min_pipeline);
1436 tape->max_stages = IDE_MIN(tape->max_stages, tape->max_pipeline);
1440 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
1442 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1444 idetape_tape_t *tape = drive->driver_data;
1445 unsigned long flags;
1447 #if IDETAPE_DEBUG_LOG
1448 printk (KERN_INFO "Reached idetape_add_stage_tail\n");
1449 #endif /* IDETAPE_DEBUG_LOG */
1450 spin_lock_irqsave(&tape->spinlock, flags);
1451 stage->next=NULL;
1452 if (tape->last_stage != NULL)
1453 tape->last_stage->next=stage;
1454 else
1455 tape->first_stage=tape->next_stage=stage;
1456 tape->last_stage=stage;
1457 if (tape->next_stage == NULL)
1458 tape->next_stage=tape->last_stage;
1459 tape->nr_stages++;
1460 tape->nr_pending_stages++;
1461 spin_unlock_irqrestore(&tape->spinlock, flags);
1465 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1466 * The caller should avoid race conditions.
1468 static void idetape_remove_stage_head (ide_drive_t *drive)
1470 idetape_tape_t *tape = drive->driver_data;
1471 idetape_stage_t *stage;
1473 #if IDETAPE_DEBUG_LOG
1474 printk (KERN_INFO "Reached idetape_remove_stage_head\n");
1475 #endif /* IDETAPE_DEBUG_LOG */
1476 #if IDETAPE_DEBUG_BUGS
1477 if (tape->first_stage == NULL) {
1478 printk (KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1479 return;
1481 if (tape->active_stage == tape->first_stage) {
1482 printk (KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1483 return;
1485 #endif /* IDETAPE_DEBUG_BUGS */
1486 stage=tape->first_stage;
1487 tape->first_stage=stage->next;
1488 idetape_kfree_stage (tape, stage);
1489 tape->nr_stages--;
1490 if (tape->first_stage == NULL) {
1491 tape->last_stage=NULL;
1492 #if IDETAPE_DEBUG_BUGS
1493 if (tape->next_stage != NULL)
1494 printk (KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1495 if (tape->nr_stages)
1496 printk (KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1497 #endif /* IDETAPE_DEBUG_BUGS */
1502 * idetape_active_next_stage will declare the next stage as "active".
1504 static void idetape_active_next_stage (ide_drive_t *drive)
1506 idetape_tape_t *tape = drive->driver_data;
1507 idetape_stage_t *stage=tape->next_stage;
1508 struct request *rq = &stage->rq;
1510 #if IDETAPE_DEBUG_LOG
1511 printk (KERN_INFO "Reached idetape_active_next_stage\n");
1512 #endif /* IDETAPE_DEBUG_LOG */
1513 #if IDETAPE_DEBUG_BUGS
1514 if (stage == NULL) {
1515 printk (KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1516 return;
1518 #endif /* IDETAPE_DEBUG_BUGS */
1520 rq->buffer = NULL;
1521 rq->bh = stage->bh;
1522 tape->active_data_request=rq;
1523 tape->active_stage=stage;
1524 tape->next_stage=stage->next;
1528 * idetape_insert_pipeline_into_queue is used to start servicing the
1529 * pipeline stages, starting from tape->next_stage.
1531 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
1533 idetape_tape_t *tape = drive->driver_data;
1535 if (tape->next_stage == NULL)
1536 return;
1537 if (!idetape_pipeline_active (tape)) {
1538 idetape_active_next_stage (drive);
1539 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
1543 static void idetape_abort_pipeline (ide_drive_t *drive)
1545 idetape_tape_t *tape = drive->driver_data;
1546 idetape_stage_t *stage = tape->next_stage;
1548 while (stage) {
1549 stage->rq.cmd = IDETAPE_ABORTED_WRITE_RQ;
1550 stage = stage->next;
1555 * idetape_end_request is used to finish servicing a request, and to
1556 * insert a pending pipeline request into the main device queue.
1558 static void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
1560 ide_drive_t *drive = hwgroup->drive;
1561 struct request *rq = hwgroup->rq;
1562 idetape_tape_t *tape = drive->driver_data;
1563 unsigned long flags;
1564 int error;
1565 int remove_stage = 0;
1567 #if IDETAPE_DEBUG_LOG
1568 printk (KERN_INFO "Reached idetape_end_request\n");
1569 #endif /* IDETAPE_DEBUG_LOG */
1571 switch (uptodate) {
1572 case 0: error = IDETAPE_ERROR_GENERAL; break;
1573 case 1: error = 0; break;
1574 default: error = uptodate;
1576 rq->errors = error;
1577 if (error)
1578 tape->failed_pc = NULL;
1580 spin_lock_irqsave(&tape->spinlock, flags);
1581 if (tape->active_data_request == rq) { /* The request was a pipelined data transfer request */
1582 tape->active_stage = NULL;
1583 tape->active_data_request = NULL;
1584 tape->nr_pending_stages--;
1585 if (rq->cmd == IDETAPE_WRITE_RQ) {
1586 if (error) {
1587 set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1588 if (error == IDETAPE_ERROR_EOD)
1589 idetape_abort_pipeline (drive);
1591 remove_stage = 1;
1593 if (tape->next_stage != NULL) {
1594 idetape_active_next_stage (drive);
1597 * Insert the next request into the request queue.
1598 * The choice of using ide_next or ide_end is now left to the user.
1600 #if IDETAPE_LOW_TAPE_PRIORITY
1601 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
1602 #else
1603 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_next);
1604 #endif /* IDETAPE_LOW_TAPE_PRIORITY */
1605 } else if (!error)
1606 idetape_increase_max_pipeline_stages (drive);
1608 ide_end_drive_cmd (drive, 0, 0);
1609 if (remove_stage)
1610 idetape_remove_stage_head (drive);
1611 spin_unlock_irqrestore(&tape->spinlock, flags);
1615 * idetape_analyze_error is called on each failed packet command retry
1616 * to analyze the request sense. We currently do not utilize this
1617 * information.
1619 static void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
1621 idetape_tape_t *tape = drive->driver_data;
1622 idetape_pc_t *pc = tape->failed_pc;
1624 tape->sense_key = result->sense_key; tape->asc = result->asc; tape->ascq = result->ascq;
1625 #if IDETAPE_DEBUG_LOG
1627 * Without debugging, we only log an error if we decided to
1628 * give up retrying.
1630 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);
1631 #endif /* IDETAPE_DEBUG_LOG */
1633 #ifdef CONFIG_BLK_DEV_IDEDMA
1636 * Correct pc->actually_transferred by asking the tape.
1638 if (test_bit (PC_DMA_ERROR, &pc->flags)) {
1639 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl (get_unaligned (&result->information));
1640 idetape_update_buffers (pc);
1642 #endif /* CONFIG_BLK_DEV_IDEDMA */
1643 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1644 pc->error = IDETAPE_ERROR_FILEMARK;
1645 set_bit (PC_ABORT, &pc->flags);
1647 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1648 if (result->eom || (result->sense_key == 0xd && result->asc == 0x0 && result->ascq == 0x2)) {
1649 pc->error = IDETAPE_ERROR_EOD;
1650 set_bit (PC_ABORT, &pc->flags);
1653 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1654 if (result->sense_key == 8) {
1655 pc->error = IDETAPE_ERROR_EOD;
1656 set_bit (PC_ABORT, &pc->flags);
1658 if (!test_bit (PC_ABORT, &pc->flags) && pc->actually_transferred)
1659 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1663 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1665 idetape_tape_t *tape = drive->driver_data;
1667 #if IDETAPE_DEBUG_LOG
1668 printk (KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1669 #endif /* IDETAPE_DEBUG_LOG */
1670 if (!tape->pc->error) {
1671 idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
1672 idetape_end_request (1,HWGROUP (drive));
1673 } else {
1674 printk (KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
1675 idetape_end_request (0,HWGROUP (drive));
1677 return ide_stopped;
1681 * idetape_init_pc initializes a packet command.
1683 static void idetape_init_pc (idetape_pc_t *pc)
1685 memset (pc->c, 0, 12);
1686 pc->retries = 0;
1687 pc->flags = 0;
1688 pc->request_transfer = 0;
1689 pc->buffer = pc->pc_buffer;
1690 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1691 pc->bh = NULL;
1692 pc->b_data = NULL;
1695 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1697 idetape_init_pc (pc);
1698 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1699 pc->c[4] = 255;
1700 pc->request_transfer = 18;
1701 pc->callback = &idetape_request_sense_callback;
1705 * idetape_retry_pc is called when an error was detected during the
1706 * last packet command. We queue a request sense packet command in
1707 * the head of the request list.
1709 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1711 idetape_tape_t *tape = drive->driver_data;
1712 idetape_pc_t *pc;
1713 struct request *rq;
1714 idetape_error_reg_t error;
1716 error.all = IN_BYTE (IDE_ERROR_REG);
1717 pc = idetape_next_pc_storage (drive);
1718 rq = idetape_next_rq_storage (drive);
1719 idetape_create_request_sense_cmd (pc);
1720 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
1721 idetape_queue_pc_head (drive, pc, rq);
1722 return ide_stopped;
1726 * idetape_pc_intr is the usual interrupt handler which will be called
1727 * during a packet command. We will transfer some of the data (as
1728 * requested by the drive) and will re-point interrupt handler to us.
1729 * When data transfer is finished, we will act according to the
1730 * algorithm described before idetape_issue_packet_command.
1733 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1735 idetape_tape_t *tape = drive->driver_data;
1736 idetape_status_reg_t status;
1737 idetape_bcount_reg_t bcount;
1738 idetape_ireason_reg_t ireason;
1739 idetape_pc_t *pc=tape->pc;
1740 unsigned int temp;
1742 #if IDETAPE_DEBUG_LOG
1743 printk (KERN_INFO "ide-tape: Reached idetape_pc_intr interrupt handler\n");
1744 #endif /* IDETAPE_DEBUG_LOG */
1746 #ifdef CONFIG_BLK_DEV_IDEDMA
1747 if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
1748 if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
1750 * A DMA error is sometimes expected. For example,
1751 * if the tape is crossing a filemark during a
1752 * READ command, it will issue an irq and position
1753 * itself before the filemark, so that only a partial
1754 * data transfer will occur (which causes the DMA
1755 * error). In that case, we will later ask the tape
1756 * how much bytes of the original request were
1757 * actually transferred (we can't receive that
1758 * information from the DMA engine on most chipsets).
1760 set_bit (PC_DMA_ERROR, &pc->flags);
1761 } else {
1762 pc->actually_transferred=pc->request_transfer;
1763 idetape_update_buffers (pc);
1765 #if IDETAPE_DEBUG_LOG
1766 printk (KERN_INFO "ide-tape: DMA finished\n");
1767 #endif /* IDETAPE_DEBUG_LOG */
1769 #endif /* CONFIG_BLK_DEV_IDEDMA */
1771 status.all = GET_STAT(); /* Clear the interrupt */
1773 if (!status.b.drq) { /* No more interrupts */
1774 #if IDETAPE_DEBUG_LOG
1775 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1776 #endif /* IDETAPE_DEBUG_LOG */
1777 clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
1779 ide__sti(); /* local CPU only */
1781 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1782 status.b.check = 0;
1783 if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
1784 #if IDETAPE_DEBUG_LOG
1785 printk (KERN_INFO "ide-tape: %s: I/O error, ",tape->name);
1786 #endif /* IDETAPE_DEBUG_LOG */
1787 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1788 printk (KERN_ERR "ide-tape: I/O error in request sense command\n");
1789 return ide_do_reset (drive);
1791 return idetape_retry_pc (drive); /* Retry operation */
1793 pc->error = 0;
1794 if (test_bit (PC_WAIT_FOR_DSC, &pc->flags) && !status.b.dsc) { /* Media access command */
1795 tape->dsc_polling_start = jiffies;
1796 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1797 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1798 idetape_postpone_request (drive); /* Allow ide.c to handle other requests */
1799 return ide_stopped;
1801 if (tape->failed_pc == pc)
1802 tape->failed_pc=NULL;
1803 return pc->callback(drive); /* Command finished - Call the callback function */
1805 #ifdef CONFIG_BLK_DEV_IDEDMA
1806 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
1807 printk (KERN_ERR "ide-tape: The tape wants to issue more interrupts in DMA mode\n");
1808 printk (KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1809 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
1810 return ide_do_reset (drive);
1812 #endif /* CONFIG_BLK_DEV_IDEDMA */
1813 bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
1814 bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
1815 ireason.all=IN_BYTE (IDE_IREASON_REG);
1817 if (ireason.b.cod) {
1818 printk (KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1819 return ide_do_reset (drive);
1821 if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
1822 printk (KERN_ERR "ide-tape: We wanted to %s, ", ireason.b.io ? "Write":"Read");
1823 printk (KERN_ERR "but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
1824 return ide_do_reset (drive);
1826 if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
1827 temp = pc->actually_transferred + bcount.all;
1828 if ( temp > pc->request_transfer) {
1829 if (temp > pc->buffer_size) {
1830 printk (KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1831 idetape_discard_data (drive,bcount.all);
1832 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD,NULL);
1833 return ide_started;
1835 #if IDETAPE_DEBUG_LOG
1836 printk (KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1837 #endif /* IDETAPE_DEBUG_LOG */
1840 if (test_bit (PC_WRITING, &pc->flags)) {
1841 if (pc->bh != NULL)
1842 idetape_output_buffers (drive, pc, bcount.all);
1843 else
1844 atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
1845 } else {
1846 if (pc->bh != NULL)
1847 idetape_input_buffers (drive, pc, bcount.all);
1848 else
1849 atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
1851 pc->actually_transferred+=bcount.all; /* Update the current position */
1852 pc->current_position+=bcount.all;
1854 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD,NULL); /* And set the interrupt handler again */
1855 return ide_started;
1859 * Packet Command Interface
1861 * The current Packet Command is available in tape->pc, and will not
1862 * change until we finish handling it. Each packet command is associated
1863 * with a callback function that will be called when the command is
1864 * finished.
1866 * The handling will be done in three stages:
1868 * 1. idetape_issue_packet_command will send the packet command to the
1869 * drive, and will set the interrupt handler to idetape_pc_intr.
1871 * 2. On each interrupt, idetape_pc_intr will be called. This step
1872 * will be repeated until the device signals us that no more
1873 * interrupts will be issued.
1875 * 3. ATAPI Tape media access commands have immediate status with a
1876 * delayed process. In case of a successful initiation of a
1877 * media access packet command, the DSC bit will be set when the
1878 * actual execution of the command is finished.
1879 * Since the tape drive will not issue an interrupt, we have to
1880 * poll for this event. In this case, we define the request as
1881 * "low priority request" by setting rq_status to
1882 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1883 * the driver.
1885 * ide.c will then give higher priority to requests which
1886 * originate from the other device, until will change rq_status
1887 * to RQ_ACTIVE.
1889 * 4. When the packet command is finished, it will be checked for errors.
1891 * 5. In case an error was found, we queue a request sense packet command
1892 * in front of the request queue and retry the operation up to
1893 * IDETAPE_MAX_PC_RETRIES times.
1895 * 6. In case no error was found, or we decided to give up and not
1896 * to retry again, the callback function will be called and then
1897 * we will handle the next request.
1901 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1903 idetape_tape_t *tape = drive->driver_data;
1904 idetape_pc_t *pc = tape->pc;
1905 idetape_ireason_reg_t ireason;
1906 int retries = 100;
1907 ide_startstop_t startstop;
1909 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1910 printk (KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1911 return startstop;
1913 ireason.all=IN_BYTE (IDE_IREASON_REG);
1914 while (retries-- && (!ireason.b.cod || ireason.b.io)) {
1915 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, retrying\n");
1916 udelay(100);
1917 ireason.all = IN_BYTE(IDE_IREASON_REG);
1918 if (retries == 0) {
1919 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, ignoring\n");
1920 ireason.b.cod = 1;
1921 ireason.b.io = 0;
1924 if (!ireason.b.cod || ireason.b.io) {
1925 printk (KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
1926 return ide_do_reset (drive);
1928 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); /* Set the interrupt routine */
1929 atapi_output_bytes (drive,pc->c,12); /* Send the actual packet */
1930 return ide_started;
1933 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1935 idetape_tape_t *tape = drive->driver_data;
1936 idetape_bcount_reg_t bcount;
1937 int dma_ok=0;
1939 #if IDETAPE_DEBUG_BUGS
1940 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1941 printk (KERN_ERR "ide-tape: possible ide-tape.c bug - Two request sense in serial were issued\n");
1943 #endif /* IDETAPE_DEBUG_BUGS */
1945 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1946 tape->failed_pc=pc;
1947 tape->pc=pc; /* Set the current packet command */
1949 if (pc->retries > IDETAPE_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
1951 * We will "abort" retrying a packet command in case
1952 * a legitimate error code was received (crossing a
1953 * filemark, or DMA error in the end of media, for
1954 * example).
1956 if (!test_bit (PC_ABORT, &pc->flags)) {
1957 printk (KERN_ERR "ide-tape: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1958 tape->name, pc->c[0], tape->sense_key, tape->asc, tape->ascq);
1959 pc->error = IDETAPE_ERROR_GENERAL; /* Giving up */
1961 tape->failed_pc=NULL;
1962 return pc->callback(drive);
1964 #if IDETAPE_DEBUG_LOG
1965 printk (KERN_INFO "Retry number - %d\n",pc->retries);
1966 #endif /* IDETAPE_DEBUG_LOG */
1968 pc->retries++;
1969 pc->actually_transferred=0; /* We haven't transferred any data yet */
1970 pc->current_position=pc->buffer;
1971 bcount.all=pc->request_transfer; /* Request to transfer the entire buffer at once */
1973 #ifdef CONFIG_BLK_DEV_IDEDMA
1974 if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
1975 printk (KERN_WARNING "ide-tape: DMA disabled, reverting to PIO\n");
1976 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
1978 if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1979 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
1980 #endif /* CONFIG_BLK_DEV_IDEDMA */
1982 if (IDE_CONTROL_REG)
1983 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
1984 OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG); /* Use PIO/DMA */
1985 OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
1986 OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
1987 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
1988 #ifdef CONFIG_BLK_DEV_IDEDMA
1989 if (dma_ok) { /* Begin DMA, if necessary */
1990 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
1991 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1993 #endif /* CONFIG_BLK_DEV_IDEDMA */
1994 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1995 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
1996 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
1997 return ide_started;
1998 } else {
1999 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
2000 return idetape_transfer_pc(drive);
2004 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2006 idetape_tape_t *tape = drive->driver_data;
2007 idetape_pc_t *pc = tape->pc;
2008 idetape_status_reg_t status;
2010 status.all = GET_STAT();
2011 if (status.b.dsc) {
2012 if (status.b.check) { /* Error detected */
2013 printk (KERN_ERR "ide-tape: %s: I/O error, ",tape->name);
2014 return idetape_retry_pc (drive); /* Retry operation */
2016 pc->error = 0;
2017 if (tape->failed_pc == pc)
2018 tape->failed_pc = NULL;
2019 } else {
2020 pc->error = IDETAPE_ERROR_GENERAL;
2021 tape->failed_pc = NULL;
2023 return pc->callback (drive);
2027 * General packet command callback function.
2029 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2031 idetape_tape_t *tape = drive->driver_data;
2033 #if IDETAPE_DEBUG_LOG
2034 printk (KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2035 #endif /* IDETAPE_DEBUG_LOG */
2037 idetape_end_request (tape->pc->error ? 0:1, HWGROUP(drive));
2038 return ide_stopped;
2041 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2043 idetape_tape_t *tape = drive->driver_data;
2044 struct request *rq = HWGROUP(drive)->rq;
2045 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2047 #if IDETAPE_DEBUG_LOG
2048 printk (KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2049 #endif /* IDETAPE_DEBUG_LOG */
2051 tape->block_address += blocks;
2052 rq->current_nr_sectors -= blocks;
2054 if (!tape->pc->error)
2055 idetape_end_request (1, HWGROUP (drive));
2056 else
2057 idetape_end_request (tape->pc->error, HWGROUP (drive));
2058 return ide_stopped;
2061 static void idetape_create_locate_cmd (idetape_pc_t *pc, unsigned int block, byte partition)
2063 idetape_init_pc (pc);
2064 pc->c[0] = IDETAPE_LOCATE_CMD;
2065 pc->c[1] = 2;
2066 put_unaligned (htonl (block), (unsigned int *) &pc->c[3]);
2067 pc->c[8] = partition;
2068 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2069 pc->callback = &idetape_pc_callback;
2072 static void idetape_create_rewind_cmd (idetape_pc_t *pc)
2074 idetape_init_pc (pc);
2075 pc->c[0] = IDETAPE_REWIND_CMD;
2076 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2077 pc->callback = &idetape_pc_callback;
2081 * A mode sense command is used to "sense" tape parameters.
2083 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, byte page_code)
2085 idetape_init_pc (pc);
2086 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2087 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors for now */
2088 pc->c[2] = page_code;
2089 pc->c[3] = 255; /* Don't limit the returned information */
2090 pc->c[4] = 255; /* (We will just discard data in that case) */
2091 if (page_code == IDETAPE_CAPABILITIES_PAGE)
2092 pc->request_transfer = 24;
2093 #if IDETAPE_DEBUG_BUGS
2094 else
2095 printk (KERN_ERR "ide-tape: unsupported page code in create_mode_sense_cmd\n");
2096 #endif /* IDETAPE_DEBUG_BUGS */
2097 pc->callback = &idetape_pc_callback;
2101 * idetape_create_write_filemark_cmd will:
2103 * 1. Write a filemark if write_filemark=1.
2104 * 2. Flush the device buffers without writing a filemark
2105 * if write_filemark=0.
2108 static void idetape_create_write_filemark_cmd (idetape_pc_t *pc,int write_filemark)
2110 idetape_init_pc (pc);
2111 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2112 pc->c[4] = write_filemark;
2113 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2114 pc->callback = &idetape_pc_callback;
2117 static void idetape_create_load_unload_cmd (idetape_pc_t *pc,int cmd)
2119 idetape_init_pc (pc);
2120 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2121 pc->c[4] = cmd;
2122 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2123 pc->callback = &idetape_pc_callback;
2126 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2128 idetape_init_pc (pc);
2129 pc->c[0] = IDETAPE_ERASE_CMD;
2130 pc->c[1] = 1;
2131 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2132 pc->callback = &idetape_pc_callback;
2135 static void idetape_create_read_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2137 idetape_init_pc (pc);
2138 pc->c[0] = IDETAPE_READ_CMD;
2139 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2140 pc->c[1] = 1;
2141 pc->callback = &idetape_rw_callback;
2142 pc->bh = bh;
2143 atomic_set(&bh->b_count, 0);
2144 pc->buffer = NULL;
2145 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2146 if (pc->request_transfer == tape->stage_size)
2147 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2150 static void idetape_create_space_cmd (idetape_pc_t *pc,int count,byte cmd)
2152 idetape_init_pc (pc);
2153 pc->c[0] = IDETAPE_SPACE_CMD;
2154 put_unaligned (htonl (count), (unsigned int *) &pc->c[1]);
2155 pc->c[1] = cmd;
2156 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
2157 pc->callback = &idetape_pc_callback;
2160 static void idetape_create_write_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2162 idetape_init_pc (pc);
2163 pc->c[0] = IDETAPE_WRITE_CMD;
2164 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2165 pc->c[1] = 1;
2166 pc->callback = &idetape_rw_callback;
2167 set_bit (PC_WRITING, &pc->flags);
2168 pc->bh = bh;
2169 pc->b_data = bh->b_data;
2170 pc->b_count = atomic_read(&bh->b_count);
2171 pc->buffer = NULL;
2172 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2173 if (pc->request_transfer == tape->stage_size)
2174 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2177 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2179 idetape_tape_t *tape = drive->driver_data;
2180 idetape_read_position_result_t *result;
2182 #if IDETAPE_DEBUG_LOG
2183 printk (KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2184 #endif /* IDETAPE_DEBUG_LOG */
2186 if (!tape->pc->error) {
2187 result = (idetape_read_position_result_t *) tape->pc->buffer;
2188 #if IDETAPE_DEBUG_LOG
2189 printk (KERN_INFO "BOP - %s\n",result->bop ? "Yes":"No");
2190 printk (KERN_INFO "EOP - %s\n",result->eop ? "Yes":"No");
2191 #endif /* IDETAPE_DEBUG_LOG */
2192 if (result->bpu) {
2193 printk (KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2194 clear_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
2195 idetape_end_request (0,HWGROUP (drive));
2196 } else {
2197 #if IDETAPE_DEBUG_LOG
2198 printk (KERN_INFO "Block Location - %lu\n", ntohl (result->first_block));
2199 #endif /* IDETAPE_DEBUG_LOG */
2200 tape->partition = result->partition;
2201 tape->block_address = ntohl (result->first_block);
2202 set_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
2203 idetape_end_request (1,HWGROUP (drive));
2205 } else
2206 idetape_end_request (0,HWGROUP (drive));
2207 return ide_stopped;
2210 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2212 idetape_init_pc (pc);
2213 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2214 pc->request_transfer = 20;
2215 pc->callback = &idetape_read_position_callback;
2219 * idetape_do_request is our request handling function.
2221 static ide_startstop_t idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2223 idetape_tape_t *tape = drive->driver_data;
2224 idetape_pc_t *pc;
2225 struct request *postponed_rq = tape->postponed_rq;
2226 idetape_status_reg_t status;
2228 #if IDETAPE_DEBUG_LOG
2229 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);
2230 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2231 #endif /* IDETAPE_DEBUG_LOG */
2233 if (!IDETAPE_RQ_CMD (rq->cmd)) {
2235 * We do not support buffer cache originated requests.
2237 printk (KERN_NOTICE "ide-tape: %s: Unsupported command in request queue (%d)\n", drive->name, rq->cmd);
2238 ide_end_request (0,HWGROUP (drive)); /* Let the common code handle it */
2239 return ide_stopped;
2243 * Retry a failed packet command
2245 if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2246 return idetape_issue_packet_command (drive, tape->failed_pc);
2248 #if IDETAPE_DEBUG_BUGS
2249 if (postponed_rq != NULL)
2250 if (rq != postponed_rq) {
2251 printk (KERN_ERR "ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
2252 idetape_end_request (0,HWGROUP (drive));
2253 return ide_stopped;
2255 #endif /* IDETAPE_DEBUG_BUGS */
2257 tape->postponed_rq = NULL;
2260 * If the tape is still busy, postpone our request and service
2261 * the other device meanwhile.
2263 status.all = GET_STAT();
2264 if (!drive->dsc_overlap && rq->cmd != IDETAPE_PC_RQ2)
2265 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
2266 if (!test_and_clear_bit (IDETAPE_IGNORE_DSC, &tape->flags) && !status.b.dsc) {
2267 if (postponed_rq == NULL) {
2268 tape->dsc_polling_start = jiffies;
2269 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2270 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2271 } else if ((signed long) (jiffies - tape->dsc_timeout) > 0) {
2272 printk (KERN_ERR "ide-tape: %s: DSC timeout\n", tape->name);
2273 if (rq->cmd == IDETAPE_PC_RQ2) {
2274 idetape_media_access_finished (drive);
2275 return ide_stopped;
2276 } else {
2277 return ide_do_reset (drive);
2279 } else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
2280 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2281 idetape_postpone_request (drive);
2282 return ide_stopped;
2284 switch (rq->cmd) {
2285 case IDETAPE_READ_RQ:
2286 pc=idetape_next_pc_storage (drive);
2287 idetape_create_read_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2288 break;
2289 case IDETAPE_WRITE_RQ:
2290 pc=idetape_next_pc_storage (drive);
2291 idetape_create_write_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2292 break;
2293 case IDETAPE_ABORTED_WRITE_RQ:
2294 rq->cmd = IDETAPE_WRITE_RQ;
2295 rq->errors = IDETAPE_ERROR_EOD;
2296 idetape_end_request (1, HWGROUP(drive));
2297 return ide_stopped;
2298 case IDETAPE_PC_RQ1:
2299 pc=(idetape_pc_t *) rq->buffer;
2300 rq->cmd = IDETAPE_PC_RQ2;
2301 break;
2302 case IDETAPE_PC_RQ2:
2303 idetape_media_access_finished (drive);
2304 return ide_stopped;
2305 default:
2306 printk (KERN_ERR "ide-tape: bug in IDETAPE_RQ_CMD macro\n");
2307 idetape_end_request (0,HWGROUP (drive));
2308 return ide_stopped;
2310 return idetape_issue_packet_command (drive, pc);
2314 * idetape_queue_pc_tail is based on the following functions:
2316 * ide_do_drive_cmd from ide.c
2317 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2319 * We add a special packet command request to the tail of the request queue,
2320 * and wait for it to be serviced.
2322 * This is not to be called from within the request handling part
2323 * of the driver ! We allocate here data in the stack, and it is valid
2324 * until the request is finished. This is not the case for the bottom
2325 * part of the driver, where we are always leaving the functions to wait
2326 * for an interrupt or a timer event.
2328 * From the bottom part of the driver, we should allocate safe memory
2329 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2330 * the request to the request list without waiting for it to be serviced !
2331 * In that case, we usually use idetape_queue_pc_head.
2333 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2335 struct request rq;
2337 ide_init_drive_cmd (&rq);
2338 rq.buffer = (char *) pc;
2339 rq.cmd = IDETAPE_PC_RQ1;
2340 return ide_do_drive_cmd (drive, &rq, ide_wait);
2344 * idetape_wait_for_request installs a semaphore in a pending request
2345 * and sleeps until it is serviced.
2347 * The caller should ensure that the request will not be serviced
2348 * before we install the semaphore (usually by disabling interrupts).
2350 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2352 DECLARE_MUTEX_LOCKED(sem);
2353 idetape_tape_t *tape = drive->driver_data;
2355 #if IDETAPE_DEBUG_BUGS
2356 if (rq == NULL || !IDETAPE_RQ_CMD (rq->cmd)) {
2357 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2358 return;
2360 #endif /* IDETAPE_DEBUG_BUGS */
2361 rq->sem = &sem;
2362 spin_unlock(&tape->spinlock);
2363 down(&sem);
2364 spin_lock_irq(&tape->spinlock);
2368 * idetape_queue_rw_tail generates a read/write request for the block
2369 * device interface and wait for it to be serviced.
2371 static int idetape_queue_rw_tail (ide_drive_t *drive, int cmd, int blocks, struct buffer_head *bh)
2373 idetape_tape_t *tape = drive->driver_data;
2374 struct request rq;
2376 #if IDETAPE_DEBUG_LOG
2377 printk (KERN_INFO "idetape_queue_rw_tail: cmd=%d\n",cmd);
2378 #endif /* IDETAPE_DEBUG_LOG */
2379 #if IDETAPE_DEBUG_BUGS
2380 if (idetape_pipeline_active (tape)) {
2381 printk (KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2382 return (0);
2384 #endif /* IDETAPE_DEBUG_BUGS */
2386 ide_init_drive_cmd (&rq);
2387 rq.bh = bh;
2388 rq.cmd = cmd;
2389 rq.sector = tape->block_address;
2390 rq.nr_sectors = rq.current_nr_sectors = blocks;
2391 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2393 idetape_init_merge_stage (tape);
2394 if (rq.errors == IDETAPE_ERROR_GENERAL)
2395 return -EIO;
2396 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2400 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2401 * to service a character device read request and add read-ahead
2402 * requests to our pipeline.
2404 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2406 idetape_tape_t *tape = drive->driver_data;
2407 idetape_stage_t *new_stage;
2408 unsigned long flags;
2409 struct request rq,*rq_ptr;
2410 int bytes_read;
2412 #if IDETAPE_DEBUG_LOG
2413 printk (KERN_INFO "Reached idetape_add_chrdev_read_request\n");
2414 #endif /* IDETAPE_DEBUG_LOG */
2416 ide_init_drive_cmd (&rq);
2417 rq.cmd = IDETAPE_READ_RQ;
2418 rq.sector = tape->block_address;
2419 rq.nr_sectors = rq.current_nr_sectors = blocks;
2421 if (idetape_pipeline_active (tape) || tape->nr_stages <= tape->max_stages / 4) {
2422 new_stage=idetape_kmalloc_stage (tape);
2423 while (new_stage != NULL) {
2424 new_stage->rq=rq;
2425 idetape_add_stage_tail (drive,new_stage);
2426 new_stage=idetape_kmalloc_stage (tape);
2428 if (!idetape_pipeline_active (tape))
2429 idetape_insert_pipeline_into_queue (drive);
2431 if (tape->first_stage == NULL) {
2433 * Linux is short on memory. Revert to non-pipelined
2434 * operation mode for this request.
2436 return (idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, blocks, tape->merge_stage->bh));
2438 spin_lock_irqsave(&tape->spinlock, flags);
2439 if (tape->active_stage == tape->first_stage)
2440 idetape_wait_for_request(drive, tape->active_data_request);
2441 spin_unlock_irqrestore(&tape->spinlock, flags);
2443 rq_ptr = &tape->first_stage->rq;
2444 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2445 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2447 idetape_switch_buffers (tape, tape->first_stage);
2449 if (rq_ptr->errors != IDETAPE_ERROR_FILEMARK) {
2450 clear_bit (IDETAPE_FILEMARK, &tape->flags);
2451 idetape_remove_stage_head (drive);
2452 } else
2453 set_bit (IDETAPE_FILEMARK, &tape->flags);
2454 #if IDETAPE_DEBUG_BUGS
2455 if (bytes_read > blocks*tape->tape_block_size) {
2456 printk (KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2457 bytes_read=blocks*tape->tape_block_size;
2459 #endif /* IDETAPE_DEBUG_BUGS */
2460 return (bytes_read);
2464 * idetape_add_chrdev_write_request tries to add a character device
2465 * originated write request to our pipeline. In case we don't succeed,
2466 * we revert to non-pipelined operation mode for this request.
2468 * 1. Try to allocate a new pipeline stage.
2469 * 2. If we can't, wait for more and more requests to be serviced
2470 * and try again each time.
2471 * 3. If we still can't allocate a stage, fallback to
2472 * non-pipelined operation mode for this request.
2474 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2476 idetape_tape_t *tape = drive->driver_data;
2477 idetape_stage_t *new_stage;
2478 unsigned long flags;
2479 struct request *rq;
2481 #if IDETAPE_DEBUG_LOG
2482 printk (KERN_INFO "Reached idetape_add_chrdev_write_request\n");
2483 #endif /* IDETAPE_DEBUG_LOG */
2486 * Attempt to allocate a new stage.
2487 * Pay special attention to possible race conditions.
2489 while ((new_stage = idetape_kmalloc_stage (tape)) == NULL) {
2490 spin_lock_irqsave(&tape->spinlock, flags);
2491 if (idetape_pipeline_active (tape)) {
2492 idetape_wait_for_request(drive, tape->active_data_request);
2493 spin_unlock_irqrestore(&tape->spinlock, flags);
2494 } else {
2495 spin_unlock_irqrestore(&tape->spinlock, flags);
2496 idetape_insert_pipeline_into_queue (drive);
2497 if (idetape_pipeline_active (tape))
2498 continue;
2500 * Linux is short on memory. Fallback to
2501 * non-pipelined operation mode for this request.
2503 return idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
2506 rq = &new_stage->rq;
2507 ide_init_drive_cmd (rq);
2508 rq->cmd = IDETAPE_WRITE_RQ;
2509 rq->sector = tape->block_address; /* Doesn't actually matter - We always assume sequential access */
2510 rq->nr_sectors = rq->current_nr_sectors = blocks;
2512 idetape_switch_buffers (tape, new_stage);
2513 idetape_add_stage_tail (drive,new_stage);
2516 * Check if we are currently servicing requests in the bottom
2517 * part of the driver.
2519 * If not, wait for the pipeline to be full enough (75%) before
2520 * starting to service requests, so that we will be able to
2521 * keep up with the higher speeds of the tape.
2523 if (!idetape_pipeline_active (tape) && tape->nr_stages >= (3 * tape->max_stages) / 4)
2524 idetape_insert_pipeline_into_queue (drive);
2526 if (test_and_clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags)) /* Return a deferred error */
2527 return -EIO;
2528 return blocks;
2531 static void idetape_discard_read_pipeline (ide_drive_t *drive)
2533 idetape_tape_t *tape = drive->driver_data;
2534 unsigned long flags;
2536 #if IDETAPE_DEBUG_BUGS
2537 if (tape->chrdev_direction != idetape_direction_read) {
2538 printk (KERN_ERR "ide-tape: bug: Trying to discard read pipeline, but we are not reading.\n");
2539 return;
2541 #endif /* IDETAPE_DEBUG_BUGS */
2542 tape->merge_stage_size = 0;
2543 if (tape->merge_stage != NULL) {
2544 __idetape_kfree_stage (tape->merge_stage);
2545 tape->merge_stage = NULL;
2547 tape->chrdev_direction = idetape_direction_none;
2549 if (tape->first_stage == NULL)
2550 return;
2552 spin_lock_irqsave(&tape->spinlock, flags);
2553 tape->next_stage = NULL;
2554 if (idetape_pipeline_active (tape))
2555 idetape_wait_for_request(drive, tape->active_data_request);
2556 spin_unlock_irqrestore(&tape->spinlock, flags);
2558 while (tape->first_stage != NULL)
2559 idetape_remove_stage_head (drive);
2560 tape->nr_pending_stages = 0;
2561 tape->max_stages = tape->min_pipeline;
2565 * idetape_wait_for_pipeline will wait until all pending pipeline
2566 * requests are serviced. Typically called on device close.
2568 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2570 idetape_tape_t *tape = drive->driver_data;
2571 unsigned long flags;
2573 if (!idetape_pipeline_active (tape))
2574 idetape_insert_pipeline_into_queue (drive);
2576 spin_lock_irqsave(&tape->spinlock, flags);
2577 if (!idetape_pipeline_active (tape))
2578 goto abort;
2579 #if IDETAPE_DEBUG_BUGS
2580 if (tape->last_stage == NULL)
2581 printk ("ide-tape: tape->last_stage == NULL\n");
2582 else
2583 #endif /* IDETAPE_DEBUG_BUGS */
2584 idetape_wait_for_request(drive, &tape->last_stage->rq);
2585 abort:
2586 spin_unlock_irqrestore(&tape->spinlock, flags);
2589 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2591 idetape_tape_t *tape = drive->driver_data;
2592 struct buffer_head *bh;
2593 int count, blocks;
2595 while (bcount) {
2596 bh = tape->merge_stage->bh;
2597 count = IDE_MIN (tape->stage_size, bcount);
2598 bcount -= count;
2599 blocks = count / tape->tape_block_size;
2600 while (count) {
2601 atomic_set(&bh->b_count, IDE_MIN (count, bh->b_size));
2602 memset (bh->b_data, 0, atomic_read(&bh->b_count));
2603 count -= atomic_read(&bh->b_count);
2604 bh = bh->b_reqnext;
2606 idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
2610 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2612 idetape_tape_t *tape = drive->driver_data;
2613 int blocks, i;
2615 #if IDETAPE_DEBUG_BUGS
2616 if (tape->chrdev_direction != idetape_direction_write) {
2617 printk (KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2618 return;
2620 if (tape->merge_stage_size > tape->stage_size) {
2621 printk (KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2622 tape->merge_stage_size = tape->stage_size;
2624 #endif /* IDETAPE_DEBUG_BUGS */
2625 if (tape->merge_stage_size) {
2626 blocks=tape->merge_stage_size/tape->tape_block_size;
2627 if (tape->merge_stage_size % tape->tape_block_size) {
2628 blocks++;
2629 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2630 memset (tape->bh->b_data + atomic_read(&tape->bh->b_count), 0, i);
2631 atomic_add(i, &tape->bh->b_count);
2633 (void) idetape_add_chrdev_write_request (drive, blocks);
2634 tape->merge_stage_size = 0;
2636 idetape_wait_for_pipeline (drive);
2637 if (tape->merge_stage != NULL) {
2638 __idetape_kfree_stage (tape->merge_stage);
2639 tape->merge_stage = NULL;
2641 clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
2642 tape->chrdev_direction=idetape_direction_none;
2645 * On the next backup, perform the feedback loop again.
2646 * (I don't want to keep sense information between backups,
2647 * as some systems are constantly on, and the system load
2648 * can be totally different on the next backup).
2650 tape->max_stages = tape->min_pipeline;
2651 #if IDETAPE_DEBUG_BUGS
2652 if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->nr_stages != 0) {
2653 printk (KERN_ERR "ide-tape: ide-tape pipeline bug\n");
2655 #endif /* IDETAPE_DEBUG_BUGS */
2658 static int idetape_pipeline_size (ide_drive_t *drive)
2660 idetape_tape_t *tape = drive->driver_data;
2661 idetape_stage_t *stage;
2662 struct request *rq;
2663 int size = 0;
2665 idetape_wait_for_pipeline (drive);
2666 stage = tape->first_stage;
2667 while (stage != NULL) {
2668 rq = &stage->rq;
2669 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2670 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2671 size += tape->tape_block_size;
2672 stage = stage->next;
2674 size += tape->merge_stage_size;
2675 return size;
2679 * idetape_position_tape positions the tape to the requested block
2680 * using the LOCATE packet command. A READ POSITION command is then
2681 * issued to check where we are positioned.
2683 * Like all higher level operations, we queue the commands at the tail
2684 * of the request queue and wait for their completion.
2687 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, byte partition)
2689 int retval;
2690 idetape_pc_t pc;
2692 idetape_create_locate_cmd (&pc, block, partition);
2693 retval=idetape_queue_pc_tail (drive,&pc);
2694 if (retval) return (retval);
2696 idetape_create_read_position_cmd (&pc);
2697 return (idetape_queue_pc_tail (drive,&pc));
2701 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2703 * We currently support only one partition.
2705 static int idetape_rewind_tape (ide_drive_t *drive)
2707 int retval;
2708 idetape_pc_t pc;
2709 #if IDETAPE_DEBUG_LOG
2710 printk (KERN_INFO "Reached idetape_rewind_tape\n");
2711 #endif /* IDETAPE_DEBUG_LOG */
2713 idetape_create_rewind_cmd (&pc);
2714 retval=idetape_queue_pc_tail (drive,&pc);
2715 if (retval) return (retval);
2717 idetape_create_read_position_cmd (&pc);
2718 return (idetape_queue_pc_tail (drive,&pc));
2721 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2723 idetape_pc_t pc;
2725 idetape_create_write_filemark_cmd (&pc,0);
2726 return (idetape_queue_pc_tail (drive,&pc));
2730 * Our special ide-tape ioctl's.
2732 * Currently there aren't any ioctl's.
2733 * mtio.h compatible commands should be issued to the character device
2734 * interface.
2736 static int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2737 unsigned int cmd, unsigned long arg)
2739 idetape_tape_t *tape = drive->driver_data;
2740 idetape_config_t config;
2742 #if IDETAPE_DEBUG_LOG
2743 printk (KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
2744 #endif /* IDETAPE_DEBUG_LOG */
2745 switch (cmd) {
2746 case 0x0340:
2747 if (copy_from_user ((char *) &config, (char *) arg, sizeof (idetape_config_t)))
2748 return -EFAULT;
2749 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2750 tape->max_stages = config.nr_stages;
2751 break;
2752 case 0x0350:
2753 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2754 config.nr_stages = tape->max_stages;
2755 if (copy_to_user ((char *) arg, (char *) &config, sizeof (idetape_config_t)))
2756 return -EFAULT;
2757 break;
2758 default:
2759 return -EIO;
2761 return 0;
2765 * The block device interface should not be used for data transfers.
2766 * However, we still allow opening it so that we can issue general
2767 * ide driver configuration ioctl's, such as the interrupt unmask feature.
2769 static int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
2771 MOD_INC_USE_COUNT;
2772 return 0;
2775 static void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
2777 MOD_DEC_USE_COUNT;
2781 * idetape_pre_reset is called before an ATAPI/ATA software reset.
2783 static void idetape_pre_reset (ide_drive_t *drive)
2785 idetape_tape_t *tape = drive->driver_data;
2786 if (tape != NULL)
2787 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
2791 * Character device interface functions
2793 static ide_drive_t *get_drive_ptr (kdev_t i_rdev)
2795 unsigned int i = MINOR(i_rdev) & ~0x80;
2797 if (i >= MAX_HWIFS * MAX_DRIVES)
2798 return NULL;
2799 return (idetape_chrdevs[i].drive);
2803 * idetape_space_over_filemarks is now a bit more complicated than just
2804 * passing the command to the tape since we may have crossed some
2805 * filemarks during our pipelined read-ahead mode.
2807 * As a minor side effect, the pipeline enables us to support MTFSFM when
2808 * the filemark is in our internal pipeline even if the tape doesn't
2809 * support spacing over filemarks in the reverse direction.
2811 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2813 idetape_tape_t *tape = drive->driver_data;
2814 idetape_pc_t pc;
2815 unsigned long flags;
2816 int retval,count=0;
2818 if (tape->chrdev_direction == idetape_direction_read) {
2821 * We have a read-ahead buffer. Scan it for crossed
2822 * filemarks.
2824 tape->merge_stage_size = 0;
2825 clear_bit (IDETAPE_FILEMARK, &tape->flags);
2826 while (tape->first_stage != NULL) {
2828 * Wait until the first read-ahead request
2829 * is serviced.
2831 spin_lock_irqsave(&tape->spinlock, flags);
2832 if (tape->active_stage == tape->first_stage)
2833 idetape_wait_for_request(drive, tape->active_data_request);
2834 spin_unlock_irqrestore(&tape->spinlock, flags);
2836 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2837 count++;
2838 if (count == mt_count) {
2839 switch (mt_op) {
2840 case MTFSF:
2841 idetape_remove_stage_head (drive);
2842 case MTFSFM:
2843 return (0);
2844 default:
2845 break;
2848 idetape_remove_stage_head (drive);
2850 idetape_discard_read_pipeline (drive);
2854 * The filemark was not found in our internal pipeline.
2855 * Now we can issue the space command.
2857 switch (mt_op) {
2858 case MTFSF:
2859 idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2860 return (idetape_queue_pc_tail (drive,&pc));
2861 case MTFSFM:
2862 if (!tape->capabilities.sprev)
2863 return (-EIO);
2864 retval = idetape_space_over_filemarks (drive, MTFSF, mt_count-count);
2865 if (retval) return (retval);
2866 return (idetape_space_over_filemarks (drive, MTBSF, 1));
2867 case MTBSF:
2868 if (!tape->capabilities.sprev)
2869 return (-EIO);
2870 idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
2871 return (idetape_queue_pc_tail (drive,&pc));
2872 case MTBSFM:
2873 if (!tape->capabilities.sprev)
2874 return (-EIO);
2875 retval = idetape_space_over_filemarks (drive, MTBSF, mt_count+count);
2876 if (retval) return (retval);
2877 return (idetape_space_over_filemarks (drive, MTFSF, 1));
2878 default:
2879 printk (KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2880 return (-EIO);
2886 * Our character device read / write functions.
2888 * The tape is optimized to maximize throughput when it is transferring
2889 * an integral number of the "continuous transfer limit", which is
2890 * a parameter of the specific tape (26 KB on my particular tape).
2892 * As of version 1.3 of the driver, the character device provides an
2893 * abstract continuous view of the media - any mix of block sizes (even 1
2894 * byte) on the same backup/restore procedure is supported. The driver
2895 * will internally convert the requests to the recommended transfer unit,
2896 * so that an unmatch between the user's block size to the recommended
2897 * size will only result in a (slightly) increased driver overhead, but
2898 * will no longer hit performance.
2900 static ssize_t idetape_chrdev_read (struct file *file, char *buf,
2901 size_t count, loff_t *ppos)
2903 struct inode *inode = file->f_dentry->d_inode;
2904 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
2905 idetape_tape_t *tape = drive->driver_data;
2906 ssize_t bytes_read,temp,actually_read=0;
2908 if (ppos != &file->f_pos) {
2909 /* "A request was outside the capabilities of the device." */
2910 return -ENXIO;
2913 #if IDETAPE_DEBUG_LOG
2914 printk (KERN_INFO "Reached idetape_chrdev_read\n");
2915 #endif /* IDETAPE_DEBUG_LOG */
2917 if (tape->chrdev_direction != idetape_direction_read) { /* Initialize read operation */
2918 if (tape->chrdev_direction == idetape_direction_write) {
2919 idetape_empty_write_pipeline (drive);
2920 idetape_flush_tape_buffers (drive);
2922 #if IDETAPE_DEBUG_BUGS
2923 if (tape->merge_stage || tape->merge_stage_size) {
2924 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2925 tape->merge_stage_size = 0;
2927 #endif /* IDETAPE_DEBUG_BUGS */
2928 if ((tape->merge_stage = __idetape_kmalloc_stage (tape)) == NULL)
2929 return -ENOMEM;
2930 tape->chrdev_direction = idetape_direction_read;
2933 * Issue a read 0 command to ensure that DSC handshake
2934 * is switched from completion mode to buffer available
2935 * mode.
2937 bytes_read = idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 0, tape->merge_stage->bh);
2938 if (bytes_read < 0) {
2939 kfree (tape->merge_stage);
2940 tape->merge_stage = NULL;
2941 tape->chrdev_direction = idetape_direction_none;
2942 return bytes_read;
2944 if (test_bit (IDETAPE_DETECT_BS, &tape->flags))
2945 if (count > tape->tape_block_size && (count % tape->tape_block_size) == 0)
2946 tape->user_bs_factor = count / tape->tape_block_size;
2948 if (count==0)
2949 return (0);
2950 if (tape->merge_stage_size) {
2951 actually_read=IDE_MIN (tape->merge_stage_size,count);
2952 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, actually_read);
2953 buf += actually_read; tape->merge_stage_size -= actually_read; count-=actually_read;
2955 while (count >= tape->stage_size) {
2956 bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
2957 if (bytes_read <= 0)
2958 goto finish;
2959 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, bytes_read);
2960 buf += bytes_read; count -= bytes_read; actually_read += bytes_read;
2962 if (count) {
2963 bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
2964 if (bytes_read <= 0)
2965 goto finish;
2966 temp=IDE_MIN (count,bytes_read);
2967 idetape_copy_stage_to_user (tape, buf, tape->merge_stage, temp);
2968 actually_read+=temp;
2969 tape->merge_stage_size=bytes_read-temp;
2971 finish:
2972 if (!actually_read && test_bit (IDETAPE_FILEMARK, &tape->flags))
2973 idetape_space_over_filemarks (drive, MTFSF, 1);
2974 return (actually_read);
2977 static ssize_t idetape_chrdev_write (struct file *file, const char *buf,
2978 size_t count, loff_t *ppos)
2980 struct inode *inode = file->f_dentry->d_inode;
2981 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
2982 idetape_tape_t *tape = drive->driver_data;
2983 ssize_t retval,actually_written=0;
2985 if (ppos != &file->f_pos) {
2986 /* "A request was outside the capabilities of the device." */
2987 return -ENXIO;
2990 #if IDETAPE_DEBUG_LOG
2991 printk (KERN_INFO "Reached idetape_chrdev_write\n");
2992 #endif /* IDETAPE_DEBUG_LOG */
2994 if (tape->chrdev_direction != idetape_direction_write) { /* Initialize write operation */
2995 if (tape->chrdev_direction == idetape_direction_read)
2996 idetape_discard_read_pipeline (drive);
2997 #if IDETAPE_DEBUG_BUGS
2998 if (tape->merge_stage || tape->merge_stage_size) {
2999 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
3000 tape->merge_stage_size = 0;
3002 #endif /* IDETAPE_DEBUG_BUGS */
3003 if ((tape->merge_stage = __idetape_kmalloc_stage (tape)) == NULL)
3004 return -ENOMEM;
3005 tape->chrdev_direction = idetape_direction_write;
3006 idetape_init_merge_stage (tape);
3009 * Issue a write 0 command to ensure that DSC handshake
3010 * is switched from completion mode to buffer available
3011 * mode.
3013 retval = idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 0, tape->merge_stage->bh);
3014 if (retval < 0) {
3015 kfree (tape->merge_stage);
3016 tape->merge_stage = NULL;
3017 tape->chrdev_direction = idetape_direction_none;
3018 return retval;
3020 if (test_bit (IDETAPE_DETECT_BS, &tape->flags))
3021 if (count > tape->tape_block_size && (count % tape->tape_block_size) == 0)
3022 tape->user_bs_factor = count / tape->tape_block_size;
3024 if (count==0)
3025 return (0);
3026 if (tape->merge_stage_size) {
3027 #if IDETAPE_DEBUG_BUGS
3028 if (tape->merge_stage_size >= tape->stage_size) {
3029 printk (KERN_ERR "ide-tape: bug: merge buffer too big\n");
3030 tape->merge_stage_size=0;
3032 #endif /* IDETAPE_DEBUG_BUGS */
3033 actually_written=IDE_MIN (tape->stage_size-tape->merge_stage_size,count);
3034 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, actually_written);
3035 buf+=actually_written;tape->merge_stage_size+=actually_written;count-=actually_written;
3037 if (tape->merge_stage_size == tape->stage_size) {
3038 tape->merge_stage_size = 0;
3039 retval=idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
3040 if (retval <= 0)
3041 return (retval);
3044 while (count >= tape->stage_size) {
3045 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, tape->stage_size);
3046 buf+=tape->stage_size;count-=tape->stage_size;
3047 retval=idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
3048 actually_written+=tape->stage_size;
3049 if (retval <= 0)
3050 return (retval);
3052 if (count) {
3053 actually_written+=count;
3054 idetape_copy_stage_from_user (tape, tape->merge_stage, buf, count);
3055 tape->merge_stage_size+=count;
3057 return (actually_written);
3061 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3062 * the general mtio MTIOCTOP ioctl is requested.
3064 * We currently support the following mtio.h operations:
3066 * MTFSF - Space over mt_count filemarks in the positive direction.
3067 * The tape is positioned after the last spaced filemark.
3069 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3070 * last filemark.
3072 * MTBSF - Steps background over mt_count filemarks, tape is
3073 * positioned before the last filemark.
3075 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3077 * Note:
3079 * MTBSF and MTBSFM are not supported when the tape doesn't
3080 * supports spacing over filemarks in the reverse direction.
3081 * In this case, MTFSFM is also usually not supported (it is
3082 * supported in the rare case in which we crossed the filemark
3083 * during our read-ahead pipelined operation mode).
3085 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3086 * the last written filemark.
3088 * MTREW - Rewinds tape.
3090 * MTLOAD - Loads the tape.
3092 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3093 * MTUNLOAD prevents further access until the media is replaced.
3095 * MTNOP - Flushes tape buffers.
3097 * MTRETEN - Retension media. This typically consists of one end
3098 * to end pass on the media.
3100 * MTEOM - Moves to the end of recorded data.
3102 * MTERASE - Erases tape.
3104 * MTSETBLK - Sets the user block size to mt_count bytes. If
3105 * mt_count is 0, we will attempt to autodetect
3106 * the block size.
3108 * MTSEEK - Positions the tape in a specific block number, where
3109 * each block is assumed to contain which user_block_size
3110 * bytes.
3112 * MTSETPART - Switches to another tape partition.
3114 * The following commands are currently not supported:
3116 * MTFSR, MTBSR, MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3117 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3119 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3121 idetape_tape_t *tape = drive->driver_data;
3122 idetape_pc_t pc;
3123 int i,retval;
3125 #if IDETAPE_DEBUG_LOG
3126 printk (KERN_INFO "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",mt_op,mt_count);
3127 #endif /* IDETAPE_DEBUG_LOG */
3129 * Commands which need our pipelined read-ahead stages.
3131 switch (mt_op) {
3132 case MTFSF:
3133 case MTFSFM:
3134 case MTBSF:
3135 case MTBSFM:
3136 if (!mt_count)
3137 return (0);
3138 return (idetape_space_over_filemarks (drive,mt_op,mt_count));
3139 default:
3140 break;
3144 * Empty the pipeline.
3146 if (tape->chrdev_direction == idetape_direction_read)
3147 idetape_discard_read_pipeline (drive);
3149 switch (mt_op) {
3150 case MTWEOF:
3151 for (i=0;i<mt_count;i++) {
3152 idetape_create_write_filemark_cmd (&pc,1);
3153 retval=idetape_queue_pc_tail (drive,&pc);
3154 if (retval) return (retval);
3156 return (0);
3157 case MTREW:
3158 return (idetape_rewind_tape (drive));
3159 case MTLOAD:
3160 idetape_create_load_unload_cmd (&pc, IDETAPE_LU_LOAD_MASK);
3161 return (idetape_queue_pc_tail (drive,&pc));
3162 case MTUNLOAD:
3163 case MTOFFL:
3164 idetape_create_load_unload_cmd (&pc,!IDETAPE_LU_LOAD_MASK);
3165 return (idetape_queue_pc_tail (drive,&pc));
3166 case MTNOP:
3167 return (idetape_flush_tape_buffers (drive));
3168 case MTRETEN:
3169 idetape_create_load_unload_cmd (&pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3170 return (idetape_queue_pc_tail (drive,&pc));
3171 case MTEOM:
3172 idetape_create_space_cmd (&pc,0,IDETAPE_SPACE_TO_EOD);
3173 return (idetape_queue_pc_tail (drive,&pc));
3174 case MTERASE:
3175 (void) idetape_rewind_tape (drive);
3176 idetape_create_erase_cmd (&pc);
3177 return (idetape_queue_pc_tail (drive,&pc));
3178 case MTSETBLK:
3179 if (mt_count) {
3180 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3181 return -EIO;
3182 tape->user_bs_factor = mt_count / tape->tape_block_size;
3183 clear_bit (IDETAPE_DETECT_BS, &tape->flags);
3184 } else
3185 set_bit (IDETAPE_DETECT_BS, &tape->flags);
3186 return 0;
3187 case MTSEEK:
3188 return (idetape_position_tape (drive, mt_count * tape->user_bs_factor, tape->partition));
3189 case MTSETPART:
3190 return (idetape_position_tape (drive, 0, mt_count));
3191 default:
3192 printk (KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3193 return (-EIO);
3198 * Our character device ioctls.
3200 * General mtio.h magnetic io commands are supported here, and not in
3201 * the corresponding block interface.
3203 * The following ioctls are supported:
3205 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3207 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3208 * will be set to (user block size in bytes <<
3209 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3211 * The mt_blkno is set to the current user block number.
3212 * The other mtget fields are not supported.
3214 * MTIOCPOS - The current tape "block position" is returned. We
3215 * assume that each block contains user_block_size
3216 * bytes.
3218 * Our own ide-tape ioctls are supported on both interfaces.
3220 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3222 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
3223 idetape_tape_t *tape = drive->driver_data;
3224 idetape_pc_t pc;
3225 struct mtop mtop;
3226 struct mtget mtget;
3227 struct mtpos mtpos;
3228 int retval, block_offset = 0;
3230 #if IDETAPE_DEBUG_LOG
3231 printk (KERN_INFO "Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
3232 #endif /* IDETAPE_DEBUG_LOG */
3234 if (tape->chrdev_direction == idetape_direction_write) {
3235 idetape_empty_write_pipeline (drive);
3236 idetape_flush_tape_buffers (drive);
3238 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3239 block_offset = idetape_pipeline_size (drive) / (tape->tape_block_size * tape->user_bs_factor);
3240 idetape_create_read_position_cmd (&pc);
3241 retval=idetape_queue_pc_tail (drive,&pc);
3242 if (retval) return (retval);
3244 switch (cmd) {
3245 case MTIOCTOP:
3246 if (copy_from_user ((char *) &mtop, (char *) arg, sizeof (struct mtop)))
3247 return -EFAULT;
3248 return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
3249 case MTIOCGET:
3250 memset (&mtget, 0, sizeof (struct mtget));
3251 mtget.mt_blkno = tape->block_address / tape->user_bs_factor - block_offset;
3252 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3253 if (copy_to_user ((char *) arg,(char *) &mtget, sizeof (struct mtget)))
3254 return -EFAULT;
3255 return 0;
3256 case MTIOCPOS:
3257 mtpos.mt_blkno = tape->block_address / tape->user_bs_factor - block_offset;
3258 if (copy_to_user ((char *) arg,(char *) &mtpos, sizeof (struct mtpos)))
3259 return -EFAULT;
3260 return 0;
3261 default:
3262 if (tape->chrdev_direction == idetape_direction_read)
3263 idetape_discard_read_pipeline (drive);
3264 return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
3269 * Our character device open function.
3271 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3273 ide_drive_t *drive;
3274 idetape_tape_t *tape;
3275 idetape_pc_t pc;
3277 #if IDETAPE_DEBUG_LOG
3278 printk (KERN_INFO "Reached idetape_chrdev_open\n");
3279 #endif /* IDETAPE_DEBUG_LOG */
3281 if ((drive = get_drive_ptr (inode->i_rdev)) == NULL)
3282 return -ENXIO;
3283 tape = drive->driver_data;
3285 if (test_and_set_bit (IDETAPE_BUSY, &tape->flags))
3286 return -EBUSY;
3287 MOD_INC_USE_COUNT;
3288 idetape_create_read_position_cmd (&pc);
3289 (void) idetape_queue_pc_tail (drive,&pc);
3290 if (!test_bit (IDETAPE_ADDRESS_VALID, &tape->flags))
3291 (void) idetape_rewind_tape (drive);
3292 MOD_DEC_USE_COUNT;
3294 if (tape->chrdev_direction == idetape_direction_none)
3295 MOD_INC_USE_COUNT;
3296 return 0;
3300 * Our character device release function.
3302 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3304 ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
3305 idetape_tape_t *tape = drive->driver_data;
3306 unsigned int minor=MINOR (inode->i_rdev);
3307 idetape_pc_t pc;
3309 #if IDETAPE_DEBUG_LOG
3310 printk (KERN_INFO "Reached idetape_chrdev_release\n");
3311 #endif /* IDETAPE_DEBUG_LOG */
3313 if (tape->chrdev_direction == idetape_direction_write) {
3314 idetape_empty_write_pipeline (drive);
3315 tape->merge_stage = __idetape_kmalloc_stage (tape);
3316 if (tape->merge_stage != NULL) {
3317 idetape_pad_zeros (drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3318 __idetape_kfree_stage (tape->merge_stage);
3319 tape->merge_stage = NULL;
3321 idetape_create_write_filemark_cmd (&pc,1); /* Write a filemark */
3322 if (idetape_queue_pc_tail (drive,&pc))
3323 printk (KERN_ERR "ide-tape: Couldn't write a filemark\n");
3325 if (tape->chrdev_direction == idetape_direction_read) {
3326 if (minor < 128)
3327 idetape_discard_read_pipeline (drive);
3328 else
3329 idetape_wait_for_pipeline (drive);
3331 if (tape->cache_stage != NULL) {
3332 __idetape_kfree_stage (tape->cache_stage);
3333 tape->cache_stage = NULL;
3335 if (minor < 128)
3336 (void) idetape_rewind_tape (drive);
3338 clear_bit (IDETAPE_BUSY, &tape->flags);
3339 if (tape->chrdev_direction == idetape_direction_none)
3340 MOD_DEC_USE_COUNT;
3341 return 0;
3345 * idetape_identify_device is called to check the contents of the
3346 * ATAPI IDENTIFY command results. We return:
3348 * 1 If the tape can be supported by us, based on the information
3349 * we have so far.
3351 * 0 If this tape driver is not currently supported by us.
3353 static int idetape_identify_device (ide_drive_t *drive,struct hd_driveid *id)
3355 struct idetape_id_gcw gcw;
3356 #if IDETAPE_INFO_LOG
3357 unsigned short mask,i;
3358 #endif /* IDETAPE_INFO_LOG */
3360 if (!id)
3361 return 0;
3363 *((unsigned short *) &gcw) = id->config;
3365 #if IDETAPE_INFO_LOG
3366 printk (KERN_INFO "Dumping ATAPI Identify Device tape parameters\n");
3367 printk (KERN_INFO "Protocol Type: ");
3368 switch (gcw.protocol) {
3369 case 0: case 1: printk (KERN_INFO "ATA\n");break;
3370 case 2: printk (KERN_INFO "ATAPI\n");break;
3371 case 3: printk (KERN_INFO "Reserved (Unknown to ide-tape)\n");break;
3373 printk (KERN_INFO "Device Type: %x - ",gcw.device_type);
3374 switch (gcw.device_type) {
3375 case 0: printk (KERN_INFO "Direct-access Device\n");break;
3376 case 1: printk (KERN_INFO "Streaming Tape Device\n");break;
3377 case 2: case 3: case 4: printk (KERN_INFO "Reserved\n");break;
3378 case 5: printk (KERN_INFO "CD-ROM Device\n");break;
3379 case 6: printk (KERN_INFO "Reserved\n");
3380 case 7: printk (KERN_INFO "Optical memory Device\n");break;
3381 case 0x1f: printk (KERN_INFO "Unknown or no Device type\n");break;
3382 default: printk (KERN_INFO "Reserved\n");
3384 printk (KERN_INFO "Removable: %s",gcw.removable ? "Yes\n":"No\n");
3385 printk (KERN_INFO "Command Packet DRQ Type: ");
3386 switch (gcw.drq_type) {
3387 case 0: printk (KERN_INFO "Microprocessor DRQ\n");break;
3388 case 1: printk (KERN_INFO "Interrupt DRQ\n");break;
3389 case 2: printk (KERN_INFO "Accelerated DRQ\n");break;
3390 case 3: printk (KERN_INFO "Reserved\n");break;
3392 printk (KERN_INFO "Command Packet Size: ");
3393 switch (gcw.packet_size) {
3394 case 0: printk (KERN_INFO "12 bytes\n");break;
3395 case 1: printk (KERN_INFO "16 bytes\n");break;
3396 default: printk (KERN_INFO "Reserved\n");break;
3398 printk (KERN_INFO "Model: %.40s\n",id->model);
3399 printk (KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
3400 printk (KERN_INFO "Serial Number: %.20s\n",id->serial_no);
3401 printk (KERN_INFO "Write buffer size: %d bytes\n",id->buf_size*512);
3402 printk (KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
3403 printk (KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
3404 printk (KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
3405 printk (KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
3406 printk (KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
3407 printk (KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
3408 printk (KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
3409 printk (KERN_INFO "Single Word DMA supported modes: ");
3410 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
3411 if (id->dma_1word & mask)
3412 printk (KERN_INFO "%d ",i);
3413 if (id->dma_1word & (mask << 8))
3414 printk (KERN_INFO "(active) ");
3416 printk (KERN_INFO "\n");
3417 printk (KERN_INFO "Multi Word DMA supported modes: ");
3418 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
3419 if (id->dma_mword & mask)
3420 printk (KERN_INFO "%d ",i);
3421 if (id->dma_mword & (mask << 8))
3422 printk (KERN_INFO "(active) ");
3424 printk (KERN_INFO "\n");
3425 if (id->field_valid & 0x0002) {
3426 printk (KERN_INFO "Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
3427 printk (KERN_INFO "Minimum Multi-word DMA cycle per word: ");
3428 if (id->eide_dma_min == 0)
3429 printk (KERN_INFO "Not supported\n");
3430 else
3431 printk (KERN_INFO "%d ns\n",id->eide_dma_min);
3433 printk (KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: ");
3434 if (id->eide_dma_time == 0)
3435 printk (KERN_INFO "Not supported\n");
3436 else
3437 printk (KERN_INFO "%d ns\n",id->eide_dma_time);
3439 printk (KERN_INFO "Minimum PIO cycle without IORDY: ");
3440 if (id->eide_pio == 0)
3441 printk (KERN_INFO "Not supported\n");
3442 else
3443 printk (KERN_INFO "%d ns\n",id->eide_pio);
3445 printk (KERN_INFO "Minimum PIO cycle with IORDY: ");
3446 if (id->eide_pio_iordy == 0)
3447 printk (KERN_INFO "Not supported\n");
3448 else
3449 printk (KERN_INFO "%d ns\n",id->eide_pio_iordy);
3451 } else
3452 printk (KERN_INFO "According to the device, fields 64-70 are not valid.\n");
3453 #endif /* IDETAPE_INFO_LOG */
3455 /* Check that we can support this device */
3457 if (gcw.protocol !=2 )
3458 printk (KERN_ERR "ide-tape: Protocol is not ATAPI\n");
3459 else if (gcw.device_type != 1)
3460 printk (KERN_ERR "ide-tape: Device type is not set to tape\n");
3461 else if (!gcw.removable)
3462 printk (KERN_ERR "ide-tape: The removable flag is not set\n");
3463 else if (gcw.packet_size != 0) {
3464 printk (KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
3465 if (gcw.packet_size == 1)
3466 printk (KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
3467 } else
3468 return 1;
3469 return 0;
3473 * idetape_get_mode_sense_results asks the tape about its various
3474 * parameters. In particular, we will adjust our data transfer buffer
3475 * size to the recommended value as returned by the tape.
3477 static void idetape_get_mode_sense_results (ide_drive_t *drive)
3479 idetape_tape_t *tape = drive->driver_data;
3480 idetape_pc_t pc;
3481 idetape_mode_parameter_header_t *header;
3482 idetape_capabilities_page_t *capabilities;
3484 idetape_create_mode_sense_cmd (&pc,IDETAPE_CAPABILITIES_PAGE);
3485 if (idetape_queue_pc_tail (drive,&pc)) {
3486 printk (KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
3487 tape->tape_block_size = 512; tape->capabilities.ctl = 52;
3488 tape->capabilities.speed = 450; tape->capabilities.buffer_size = 6 * 52;
3489 return;
3491 header = (idetape_mode_parameter_header_t *) pc.buffer;
3492 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
3494 capabilities->max_speed = ntohs (capabilities->max_speed);
3495 capabilities->ctl = ntohs (capabilities->ctl);
3496 capabilities->speed = ntohs (capabilities->speed);
3497 capabilities->buffer_size = ntohs (capabilities->buffer_size);
3499 if (!capabilities->speed) {
3500 printk("ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
3501 capabilities->speed = 650;
3503 if (!capabilities->max_speed) {
3504 printk("ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
3505 capabilities->max_speed = 650;
3508 tape->capabilities = *capabilities; /* Save us a copy */
3509 tape->tape_block_size = capabilities->blk512 ? 512:1024;
3510 #if IDETAPE_INFO_LOG
3511 printk (KERN_INFO "Dumping the results of the MODE SENSE packet command\n");
3512 printk (KERN_INFO "Mode Parameter Header:\n");
3513 printk (KERN_INFO "Mode Data Length - %d\n",header->mode_data_length);
3514 printk (KERN_INFO "Medium Type - %d\n",header->medium_type);
3515 printk (KERN_INFO "Device Specific Parameter - %d\n",header->dsp);
3516 printk (KERN_INFO "Block Descriptor Length - %d\n",header->bdl);
3518 printk (KERN_INFO "Capabilities and Mechanical Status Page:\n");
3519 printk (KERN_INFO "Page code - %d\n",capabilities->page_code);
3520 printk (KERN_INFO "Page length - %d\n",capabilities->page_length);
3521 printk (KERN_INFO "Read only - %s\n",capabilities->ro ? "Yes":"No");
3522 printk (KERN_INFO "Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
3523 printk (KERN_INFO "Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
3524 printk (KERN_INFO "Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
3525 printk (KERN_INFO "Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
3526 printk (KERN_INFO "The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
3527 printk (KERN_INFO "The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
3528 printk (KERN_INFO "Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
3529 printk (KERN_INFO "Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
3530 printk (KERN_INFO "Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
3531 printk (KERN_INFO "Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
3532 printk (KERN_INFO "Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
3533 printk (KERN_INFO "Restricted byte count for PIO transfers - %s\n",capabilities->slowb ? "Yes":"No");
3534 printk (KERN_INFO "Maximum supported speed in KBps - %d\n",capabilities->max_speed);
3535 printk (KERN_INFO "Continuous transfer limits in blocks - %d\n",capabilities->ctl);
3536 printk (KERN_INFO "Current speed in KBps - %d\n",capabilities->speed);
3537 printk (KERN_INFO "Buffer size - %d\n",capabilities->buffer_size*512);
3538 #endif /* IDETAPE_INFO_LOG */
3541 static void idetape_add_settings(ide_drive_t *drive)
3543 idetape_tape_t *tape = drive->driver_data;
3546 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
3548 ide_add_setting(drive, "buffer", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
3549 ide_add_setting(drive, "pipeline_min", SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3550 ide_add_setting(drive, "pipeline", SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3551 ide_add_setting(drive, "pipeline_max", SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3552 ide_add_setting(drive, "pipeline_used",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3553 ide_add_setting(drive, "speed", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
3554 ide_add_setting(drive, "stage", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3555 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);
3556 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3560 * ide_setup is called to:
3562 * 1. Initialize our various state variables.
3563 * 2. Ask the tape for its capabilities.
3564 * 3. Allocate a buffer which will be used for data
3565 * transfer. The buffer size is chosen based on
3566 * the recommendation which we received in step (2).
3568 * Note that at this point ide.c already assigned us an irq, so that
3569 * we can queue requests here and wait for their completion.
3571 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3573 ide_hwif_t *hwif = HWIF(drive);
3574 unsigned long t1, tmid, tn, t;
3575 u16 speed;
3576 struct idetape_id_gcw gcw;
3578 memset (tape, 0, sizeof (idetape_tape_t));
3579 spin_lock_init(&tape->spinlock);
3580 drive->driver_data = tape;
3581 drive->ready_stat = 0; /* An ATAPI device ignores DRDY */
3582 #ifdef CONFIG_BLK_DEV_IDEPCI
3584 * These two ide-pci host adapters appear to need this disabled.
3586 if (HWIF(drive)->pci_dev != NULL && (
3587 (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
3588 (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343))) {
3589 drive->dsc_overlap = 0;
3590 } else
3591 #endif /* CONFIG_BLK_DEV_IDEPCI */
3593 drive->dsc_overlap = 1;
3595 memset (tape, 0, sizeof (idetape_tape_t));
3596 tape->drive = drive;
3597 tape->minor = minor;
3598 tape->name[0] = 'h'; tape->name[1] = 't'; tape->name[2] = '0' + minor;
3599 tape->chrdev_direction = idetape_direction_none;
3600 tape->pc = tape->pc_stack;
3601 tape->min_pipeline = IDETAPE_MIN_PIPELINE_STAGES;
3602 tape->max_pipeline = IDETAPE_MAX_PIPELINE_STAGES;
3603 tape->max_stages = tape->min_pipeline;
3604 *((unsigned short *) &gcw) = drive->id->config;
3605 if (gcw.drq_type == 1)
3606 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3608 idetape_get_mode_sense_results (drive);
3610 tape->user_bs_factor = 1;
3611 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
3612 while (tape->stage_size > 0xffff) {
3613 printk (KERN_NOTICE "ide-tape: decreasing stage size\n");
3614 tape->capabilities.ctl /= 2;
3615 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
3617 tape->pages_per_stage = tape->stage_size / PAGE_SIZE;
3618 if (tape->stage_size % PAGE_SIZE) {
3619 tape->pages_per_stage++;
3620 tape->excess_bh_size = PAGE_SIZE - tape->stage_size % PAGE_SIZE;
3624 * Select the "best" DSC read/write polling frequency.
3625 * The following algorithm attempts to find a balance between
3626 * good latency and good system throughput. It will be nice to
3627 * have all this configurable in run time at some point.
3629 speed = IDE_MAX (tape->capabilities.speed, tape->capabilities.max_speed);
3630 t1 = (tape->stage_size * HZ) / (speed * 1000);
3631 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
3632 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3634 if (tape->max_stages) {
3635 if (drive->using_dma)
3636 t = tmid;
3637 else {
3638 if (hwif->drives[drive->select.b.unit ^ 1].present || hwif->next != hwif)
3639 t = (tn + tmid) / 2;
3640 else
3641 t = tn;
3643 } else
3644 t = t1;
3645 t = IDE_MIN (t, tmid);
3648 * Ensure that the number we got makes sense.
3650 tape->best_dsc_rw_frequency = IDE_MAX (IDE_MIN (t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3651 if (tape->best_dsc_rw_frequency != t) {
3652 printk (KERN_NOTICE "ide-tape: Although the recommended polling period is %lu jiffies\n", t);
3653 printk (KERN_NOTICE "ide-tape: we will use %lu jiffies\n", tape->best_dsc_rw_frequency);
3655 printk (KERN_INFO "ide-tape: %s <-> %s, %dKBps, %d*%dkB buffer, %dkB pipeline, %lums tDSC%s\n",
3656 drive->name, tape->name, tape->capabilities.speed, (tape->capabilities.buffer_size * 512) / tape->stage_size,
3657 tape->stage_size / 1024, tape->max_stages * tape->stage_size / 1024,
3658 tape->best_dsc_rw_frequency * 1000 / HZ, drive->using_dma ? ", DMA":"");
3660 idetape_add_settings(drive);
3663 static int idetape_cleanup (ide_drive_t *drive)
3665 idetape_tape_t *tape = drive->driver_data;
3666 int minor = tape->minor;
3667 unsigned long flags;
3669 save_flags (flags); /* all CPUs (overkill?) */
3670 cli(); /* all CPUs (overkill?) */
3671 if (test_bit (IDETAPE_BUSY, &tape->flags) || tape->first_stage != NULL || tape->merge_stage_size || drive->usage) {
3672 restore_flags(flags); /* all CPUs (overkill?) */
3673 return 1;
3675 idetape_chrdevs[minor].drive = NULL;
3676 restore_flags (flags); /* all CPUs (overkill?) */
3677 DRIVER(drive)->busy = 0;
3678 (void) ide_unregister_subdriver (drive);
3679 drive->driver_data = NULL;
3680 kfree (tape);
3681 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++)
3682 if (idetape_chrdevs[minor].drive != NULL)
3683 return 0;
3684 unregister_chrdev (IDETAPE_MAJOR, "ht");
3685 idetape_chrdev_present = 0;
3686 return 0;
3689 #ifdef CONFIG_PROC_FS
3691 static int proc_idetape_read_name
3692 (char *page, char **start, off_t off, int count, int *eof, void *data)
3694 ide_drive_t *drive = (ide_drive_t *) data;
3695 idetape_tape_t *tape = drive->driver_data;
3696 char *out = page;
3697 int len;
3699 len = sprintf(out,"%s\n", tape->name);
3700 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
3703 static ide_proc_entry_t idetape_proc[] = {
3704 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3705 { NULL, 0, NULL, NULL }
3708 #else
3710 #define idetape_proc NULL
3712 #endif
3715 * IDE subdriver functions, registered with ide.c
3717 static ide_driver_t idetape_driver = {
3718 "ide-tape", /* name */
3719 IDETAPE_VERSION, /* version */
3720 ide_tape, /* media */
3721 1, /* busy */
3722 1, /* supports_dma */
3723 1, /* supports_dsc_overlap */
3724 idetape_cleanup, /* cleanup */
3725 idetape_do_request, /* do_request */
3726 idetape_end_request, /* end_request */
3727 idetape_blkdev_ioctl, /* ioctl */
3728 idetape_blkdev_open, /* open */
3729 idetape_blkdev_release, /* release */
3730 NULL, /* media_change */
3731 idetape_pre_reset, /* pre_reset */
3732 NULL, /* capacity */
3733 NULL, /* special */
3734 idetape_proc /* proc */
3737 int idetape_init (void);
3738 static ide_module_t idetape_module = {
3739 IDE_DRIVER_MODULE,
3740 idetape_init,
3741 &idetape_driver,
3742 NULL
3746 * Our character device supporting functions, passed to register_chrdev.
3748 static struct file_operations idetape_fops = {
3749 NULL, /* lseek - default */
3750 idetape_chrdev_read, /* read */
3751 idetape_chrdev_write, /* write */
3752 NULL, /* readdir - bad */
3753 NULL, /* poll */
3754 idetape_chrdev_ioctl, /* ioctl */
3755 NULL, /* mmap */
3756 idetape_chrdev_open, /* open */
3757 NULL, /* flush */
3758 idetape_chrdev_release, /* release */
3759 NULL, /* fsync */
3760 NULL, /* fasync */
3761 NULL, /* check_media_change */
3762 NULL /* revalidate */
3766 * idetape_init will register the driver for each tape.
3768 int idetape_init (void)
3770 ide_drive_t *drive;
3771 idetape_tape_t *tape;
3772 int minor, failed = 0, supported = 0;
3774 MOD_INC_USE_COUNT;
3775 if (!idetape_chrdev_present)
3776 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
3777 idetape_chrdevs[minor].drive = NULL;
3779 if ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) == NULL) {
3780 ide_register_module (&idetape_module);
3781 MOD_DEC_USE_COUNT;
3782 return 0;
3784 if (!idetape_chrdev_present && register_chrdev (IDETAPE_MAJOR, "ht", &idetape_fops)) {
3785 printk (KERN_ERR "ide-tape: Failed to register character device interface\n");
3786 MOD_DEC_USE_COUNT;
3787 return -EBUSY;
3789 do {
3790 if (!idetape_identify_device (drive, drive->id)) {
3791 printk (KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3792 continue;
3794 tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
3795 if (tape == NULL) {
3796 printk (KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3797 continue;
3799 if (ide_register_subdriver (drive, &idetape_driver, IDE_SUBDRIVER_VERSION)) {
3800 printk (KERN_ERR "ide-tape: %s: Failed to register the driver with ide.c\n", drive->name);
3801 kfree (tape);
3802 continue;
3804 for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
3805 idetape_setup (drive, tape, minor);
3806 idetape_chrdevs[minor].drive = drive;
3807 supported++; failed--;
3808 } while ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) != NULL);
3809 if (!idetape_chrdev_present && !supported) {
3810 unregister_chrdev (IDETAPE_MAJOR, "ht");
3811 } else
3812 idetape_chrdev_present = 1;
3813 ide_register_module (&idetape_module);
3814 MOD_DEC_USE_COUNT;
3815 return 0;
3818 #ifdef MODULE
3819 int init_module (void)
3821 return idetape_init ();
3824 void cleanup_module (void)
3826 ide_drive_t *drive;
3827 int minor;
3829 for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++) {
3830 drive = idetape_chrdevs[minor].drive;
3831 if (drive) {
3832 if (idetape_cleanup (drive))
3833 printk (KERN_ERR "ide-tape: %s: cleanup_module() called while still busy\n", drive->name);
3834 /* We must remove proc entries defined in this module.
3835 Otherwise we oops while accessing these entries */
3836 if (drive->proc)
3837 ide_remove_proc_entries(drive->proc, idetape_proc);
3840 ide_unregister_module(&idetape_module);
3842 #endif /* MODULE */