1 /*
2 * linux/drivers/ide/ide-tape.c Version 1.16f Dec 15, 1999
3 *
4 * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 *
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.
9 *
10 * It is hereby placed under the terms of the GNU general public license.
11 * (See linux/COPYING).
12 */
13
14 /*
15 * IDE ATAPI streaming tape driver.
16 *
17 * This driver is a part of the Linux ide driver and works in co-operation
18 * with linux/drivers/block/ide.c.
19 *
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.
24 *
25 * Pipelined operation mode is now supported on both reads and writes.
26 *
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.
29 *
30 * The character device interface consists of the following devices:
31 *
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 * ...
38 *
39 * Run linux/scripts/MAKEDEV.ide to create the above entries.
40 *
41 * The general magnetic tape commands compatible interface, as defined by
42 * include/linux/mtio.h, is accessible through the character device.
43 *
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.
47 *
48 * Our own ide-tape ioctl's can be issued to either the block device or
49 * the character device interface.
50 *
51 * Maximal throughput with minimal bus load will usually be achieved in the
52 * following scenario:
53 *
54 * 1. ide-tape is operating in the pipelined operation mode.
55 * 2. No buffering is performed by the user backup program.
56 *
57 * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
58 *
59 * Ver 0.1 Nov 1 95 Pre-working code :-)
60 * Ver 0.2 Nov 23 95 A short backup (few megabytes) and restore procedure
61 * was successful ! (Using tar cvf ... on the block
62 * device interface).
63 * A longer backup resulted in major swapping, bad
64 * overall Linux performance and eventually failed as
65 * we received non serial read-ahead requests from the
66 * buffer cache.
67 * Ver 0.3 Nov 28 95 Long backups are now possible, thanks to the
68 * character device interface. Linux's responsiveness
69 * and performance doesn't seem to be much affected
70 * from the background backup procedure.
71 * Some general mtio.h magnetic tape operations are
72 * now supported by our character device. As a result,
73 * popular tape utilities are starting to work with
74 * ide tapes :-)
75 * The following configurations were tested:
76 * 1. An IDE ATAPI TAPE shares the same interface
77 * and irq with an IDE ATAPI CDROM.
78 * 2. An IDE ATAPI TAPE shares the same interface
79 * and irq with a normal IDE disk.
80 * Both configurations seemed to work just fine !
81 * However, to be on the safe side, it is meanwhile
82 * recommended to give the IDE TAPE its own interface
83 * and irq.
84 * The one thing which needs to be done here is to
85 * add a "request postpone" feature to ide.c,
86 * so that we won't have to wait for the tape to finish
87 * performing a long media access (DSC) request (such
88 * as a rewind) before we can access the other device
89 * on the same interface. This effect doesn't disturb
90 * normal operation most of the time because read/write
91 * requests are relatively fast, and once we are
92 * performing one tape r/w request, a lot of requests
93 * from the other device can be queued and ide.c will
94 * service all of them after this single tape request.
95 * Ver 1.0 Dec 11 95 Integrated into Linux 1.3.46 development tree.
96 * On each read / write request, we now ask the drive
97 * if we can transfer a constant number of bytes
98 * (a parameter of the drive) only to its buffers,
99 * without causing actual media access. If we can't,
100 * we just wait until we can by polling the DSC bit.
101 * This ensures that while we are not transferring
102 * more bytes than the constant referred to above, the
103 * interrupt latency will not become too high and
104 * we won't cause an interrupt timeout, as happened
105 * occasionally in the previous version.
106 * While polling for DSC, the current request is
107 * postponed and ide.c is free to handle requests from
108 * the other device. This is handled transparently to
109 * ide.c. The hwgroup locking method which was used
110 * in the previous version was removed.
111 * Use of new general features which are provided by
112 * ide.c for use with atapi devices.
113 * (Programming done by Mark Lord)
114 * Few potential bug fixes (Again, suggested by Mark)
115 * Single character device data transfers are now
116 * not limited in size, as they were before.
117 * We are asking the tape about its recommended
118 * transfer unit and send a larger data transfer
119 * as several transfers of the above size.
120 * For best results, use an integral number of this
121 * basic unit (which is shown during driver
122 * initialization). I will soon add an ioctl to get
123 * this important parameter.
124 * Our data transfer buffer is allocated on startup,
125 * rather than before each data transfer. This should
126 * ensure that we will indeed have a data buffer.
127 * Ver 1.1 Dec 14 95 Fixed random problems which occurred when the tape
128 * shared an interface with another device.
129 * (poll_for_dsc was a complete mess).
130 * Removed some old (non-active) code which had
131 * to do with supporting buffer cache originated
132 * requests.
133 * The block device interface can now be opened, so
134 * that general ide driver features like the unmask
135 * interrupts flag can be selected with an ioctl.
136 * This is the only use of the block device interface.
137 * New fast pipelined operation mode (currently only on
138 * writes). When using the pipelined mode, the
139 * throughput can potentially reach the maximum
140 * tape supported throughput, regardless of the
141 * user backup program. On my tape drive, it sometimes
142 * boosted performance by a factor of 2. Pipelined
143 * mode is enabled by default, but since it has a few
144 * downfalls as well, you may want to disable it.
145 * A short explanation of the pipelined operation mode
146 * is available below.
147 * Ver 1.2 Jan 1 96 Eliminated pipelined mode race condition.
148 * Added pipeline read mode. As a result, restores
149 * are now as fast as backups.
150 * Optimized shared interface behavior. The new behavior
151 * typically results in better IDE bus efficiency and
152 * higher tape throughput.
153 * Pre-calculation of the expected read/write request
154 * service time, based on the tape's parameters. In
155 * the pipelined operation mode, this allows us to
156 * adjust our polling frequency to a much lower value,
157 * and thus to dramatically reduce our load on Linux,
158 * without any decrease in performance.
159 * Implemented additional mtio.h operations.
160 * The recommended user block size is returned by
161 * the MTIOCGET ioctl.
162 * Additional minor changes.
163 * Ver 1.3 Feb 9 96 Fixed pipelined read mode bug which prevented the
164 * use of some block sizes during a restore procedure.
165 * The character device interface will now present a
166 * continuous view of the media - any mix of block sizes
167 * during a backup/restore procedure is supported. The
168 * driver will buffer the requests internally and
169 * convert them to the tape's recommended transfer
170 * unit, making performance almost independent of the
171 * chosen user block size.
172 * Some improvements in error recovery.
173 * By cooperating with ide-dma.c, bus mastering DMA can
174 * now sometimes be used with IDE tape drives as well.
175 * Bus mastering DMA has the potential to dramatically
176 * reduce the CPU's overhead when accessing the device,
177 * and can be enabled by using hdparm -d1 on the tape's
178 * block device interface. For more info, read the
179 * comments in ide-dma.c.
180 * Ver 1.4 Mar 13 96 Fixed serialize support.
181 * Ver 1.5 Apr 12 96 Fixed shared interface operation, broken in 1.3.85.
182 * Fixed pipelined read mode inefficiency.
183 * Fixed nasty null dereferencing bug.
184 * Ver 1.6 Aug 16 96 Fixed FPU usage in the driver.
185 * Fixed end of media bug.
186 * Ver 1.7 Sep 10 96 Minor changes for the CONNER CTT8000-A model.
187 * Ver 1.8 Sep 26 96 Attempt to find a better balance between good
188 * interactive response and high system throughput.
189 * Ver 1.9 Nov 5 96 Automatically cross encountered filemarks rather
190 * than requiring an explicit FSF command.
191 * Abort pending requests at end of media.
192 * MTTELL was sometimes returning incorrect results.
193 * Return the real block size in the MTIOCGET ioctl.
194 * Some error recovery bug fixes.
195 * Ver 1.10 Nov 5 96 Major reorganization.
196 * Reduced CPU overhead a bit by eliminating internal
197 * bounce buffers.
198 * Added module support.
199 * Added multiple tape drives support.
200 * Added partition support.
201 * Rewrote DSC handling.
202 * Some portability fixes.
203 * Removed ide-tape.h.
204 * Additional minor changes.
205 * Ver 1.11 Dec 2 96 Bug fix in previous DSC timeout handling.
206 * Use ide_stall_queue() for DSC overlap.
207 * Use the maximum speed rather than the current speed
208 * to compute the request service time.
209 * Ver 1.12 Dec 7 97 Fix random memory overwriting and/or last block data
210 * corruption, which could occur if the total number
211 * of bytes written to the tape was not an integral
212 * number of tape blocks.
213 * Add support for INTERRUPT DRQ devices.
214 * Ver 1.13 Jan 2 98 Add "speed == 0" work-around for HP COLORADO 5GB
215 * Ver 1.14 Dec 30 98 Partial fixes for the Sony/AIWA tape drives.
216 * Replace cli()/sti() with hwgroup spinlocks.
217 * Ver 1.15 Mar 25 99 Fix SMP race condition by replacing hwgroup
218 * spinlock with private per-tape spinlock.
219 * Ver 1.16 Sep 1 99 Add OnStream tape support.
220 * Abort read pipeline on EOD.
221 * Wait for the tape to become ready in case it returns
222 * "in the process of becoming ready" on open().
223 * Fix zero padding of the last written block in
224 * case the tape block size is larger than PAGE_SIZE.
225 * Decrease the default disconnection time to tn.
226 * Ver 1.16e Oct 3 99 Minor fixes.
227 * Ver 1.16e1 Oct 13 99 Patches by Arnold Niessen,
228 * niessen@iae.nl / arnold.niessen@philips.com
229 * GO-1) Undefined code in idetape_read_position
230 * according to Gadi's email
231 * AJN-1) Minor fix asc == 11 should be asc == 0x11
232 * in idetape_issue_packet_command (did effect
233 * debugging output only)
234 * AJN-2) Added more debugging output, and
235 * added ide-tape: where missing. I would also
236 * like to add tape->name where possible
237 * AJN-3) Added different debug_level's
238 * via /proc/ide/hdc/settings
239 * "debug_level" determines amount of debugging output;
240 * can be changed using /proc/ide/hdx/settings
241 * 0 : almost no debugging output
242 * 1 : 0+output errors only
243 * 2 : 1+output all sensekey/asc
244 * 3 : 2+follow all chrdev related procedures
245 * 4 : 3+follow all procedures
246 * 5 : 4+include pc_stack rq_stack info
247 * 6 : 5+USE_COUNT updates
248 * AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
249 * from 5 to 10 minutes
250 * AJN-5) Changed maximum number of blocks to skip when
251 * reading tapes with multiple consecutive write
252 * errors from 100 to 1000 in idetape_get_logical_blk
253 * Proposed changes to code:
254 * 1) output "logical_blk_num" via /proc
255 * 2) output "current_operation" via /proc
256 * 3) Either solve or document the fact that `mt rewind' is
257 * required after reading from /dev/nhtx to be
258 * able to rmmod the idetape module;
259 * Also, sometimes an application finishes but the
260 * device remains `busy' for some time. Same cause ?
261 * Proposed changes to release-notes:
262 * 4) write a simple `quickstart' section in the
263 * release notes; I volunteer if you don't want to
264 * 5) include a pointer to video4linux in the doc
265 * to stimulate video applications
266 * 6) release notes lines 331 and 362: explain what happens
267 * if the application data rate is higher than 1100 KB/s;
268 * similar approach to lower-than-500 kB/s ?
269 * 7) 6.6 Comparison; wouldn't it be better to allow different
270 * strategies for read and write ?
271 * Wouldn't it be better to control the tape buffer
272 * contents instead of the bandwidth ?
273 * 8) line 536: replace will by would (if I understand
274 * this section correctly, a hypothetical and unwanted situation
275 * is being described)
276 * Ver 1.16f Dec 15 99 Change place of the secondary OnStream header frames.
277 *
278 *
279 * Here are some words from the first releases of hd.c, which are quoted
280 * in ide.c and apply here as well:
281 *
282 * | Special care is recommended. Have Fun!
283 *
284 */
285
286 /*
287 * An overview of the pipelined operation mode.
288 *
289 * In the pipelined write mode, we will usually just add requests to our
290 * pipeline and return immediately, before we even start to service them. The
291 * user program will then have enough time to prepare the next request while
292 * we are still busy servicing previous requests. In the pipelined read mode,
293 * the situation is similar - we add read-ahead requests into the pipeline,
294 * before the user even requested them.
295 *
296 * The pipeline can be viewed as a "safety net" which will be activated when
297 * the system load is high and prevents the user backup program from keeping up
298 * with the current tape speed. At this point, the pipeline will get
299 * shorter and shorter but the tape will still be streaming at the same speed.
300 * Assuming we have enough pipeline stages, the system load will hopefully
301 * decrease before the pipeline is completely empty, and the backup program
302 * will be able to "catch up" and refill the pipeline again.
303 *
304 * When using the pipelined mode, it would be best to disable any type of
305 * buffering done by the user program, as ide-tape already provides all the
306 * benefits in the kernel, where it can be done in a more efficient way.
307 * As we will usually not block the user program on a request, the most
308 * efficient user code will then be a simple read-write-read-... cycle.
309 * Any additional logic will usually just slow down the backup process.
310 *
311 * Using the pipelined mode, I get a constant over 400 KBps throughput,
312 * which seems to be the maximum throughput supported by my tape.
313 *
314 * However, there are some downfalls:
315 *
316 * 1. We use memory (for data buffers) in proportional to the number
317 * of pipeline stages (each stage is about 26 KB with my tape).
318 * 2. In the pipelined write mode, we cheat and postpone error codes
319 * to the user task. In read mode, the actual tape position
320 * will be a bit further than the last requested block.
321 *
322 * Concerning (1):
323 *
324 * 1. We allocate stages dynamically only when we need them. When
325 * we don't need them, we don't consume additional memory. In
326 * case we can't allocate stages, we just manage without them
327 * (at the expense of decreased throughput) so when Linux is
328 * tight in memory, we will not pose additional difficulties.
329 *
330 * 2. The maximum number of stages (which is, in fact, the maximum
331 * amount of memory) which we allocate is limited by the compile
332 * time parameter IDETAPE_MAX_PIPELINE_STAGES.
333 *
334 * 3. The maximum number of stages is a controlled parameter - We
335 * don't start from the user defined maximum number of stages
336 * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
337 * will not even allocate this amount of stages if the user
338 * program can't handle the speed). We then implement a feedback
339 * loop which checks if the pipeline is empty, and if it is, we
340 * increase the maximum number of stages as necessary until we
341 * reach the optimum value which just manages to keep the tape
342 * busy with minimum allocated memory or until we reach
343 * IDETAPE_MAX_PIPELINE_STAGES.
344 *
345 * Concerning (2):
346 *
347 * In pipelined write mode, ide-tape can not return accurate error codes
348 * to the user program since we usually just add the request to the
349 * pipeline without waiting for it to be serviced. In case an error
350 * occurs, I will report it on the next user request.
351 *
352 * In the pipelined read mode, subsequent read requests or forward
353 * filemark spacing will perform correctly, as we preserve all blocks
354 * and filemarks which we encountered during our excess read-ahead.
355 *
356 * For accurate tape positioning and error reporting, disabling
357 * pipelined mode might be the best option.
358 *
359 * You can enable/disable/tune the pipelined operation mode by adjusting
360 * the compile time parameters below.
361 */
362
363 /*
364 * Possible improvements.
365 *
366 * 1. Support for the ATAPI overlap protocol.
367 *
368 * In order to maximize bus throughput, we currently use the DSC
369 * overlap method which enables ide.c to service requests from the
370 * other device while the tape is busy executing a command. The
371 * DSC overlap method involves polling the tape's status register
372 * for the DSC bit, and servicing the other device while the tape
373 * isn't ready.
374 *
375 * In the current QIC development standard (December 1995),
376 * it is recommended that new tape drives will *in addition*
377 * implement the ATAPI overlap protocol, which is used for the
378 * same purpose - efficient use of the IDE bus, but is interrupt
379 * driven and thus has much less CPU overhead.
380 *
381 * ATAPI overlap is likely to be supported in most new ATAPI
382 * devices, including new ATAPI cdroms, and thus provides us
383 * a method by which we can achieve higher throughput when
384 * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
385 */
386
387 #define IDETAPE_VERSION "1.16f"
388
389 #include <linux/config.h>
390 #include <linux/module.h>
391 #include <linux/types.h>
392 #include <linux/string.h>
393 #include <linux/kernel.h>
394 #include <linux/delay.h>
395 #include <linux/timer.h>
396 #include <linux/mm.h>
397 #include <linux/interrupt.h>
398 #include <linux/major.h>
399 #include <linux/devfs_fs_kernel.h>
400 #include <linux/errno.h>
401 #include <linux/genhd.h>
402 #include <linux/malloc.h>
403 #include <linux/pci.h>
404 #include <linux/ide.h>
405 #include <linux/smp_lock.h>
406
407 #include <asm/byteorder.h>
408 #include <asm/irq.h>
409 #include <asm/uaccess.h>
410 #include <asm/io.h>
411 #include <asm/unaligned.h>
412 #include <asm/bitops.h>
413
414
415 #define NO_LONGER_REQUIRED (1)
416
417 /*
418 * OnStream support
419 */
420 #define ONSTREAM_DEBUG (0)
421 #define OS_CONFIG_PARTITION (0xff)
422 #define OS_DATA_PARTITION (0)
423 #define OS_PARTITION_VERSION (1)
424
425 /*
426 * partition
427 */
428 typedef struct os_partition_s {
429 __u8 partition_num;
430 __u8 par_desc_ver;
431 __u16 wrt_pass_cntr;
432 __u32 first_frame_addr;
433 __u32 last_frame_addr;
434 __u32 eod_frame_addr;
435 } os_partition_t;
436
437 /*
438 * DAT entry
439 */
440 typedef struct os_dat_entry_s {
441 __u32 blk_sz;
442 __u16 blk_cnt;
443 __u8 flags;
444 __u8 reserved;
445 } os_dat_entry_t;
446
447 /*
448 * DAT
449 */
450 #define OS_DAT_FLAGS_DATA (0xc)
451 #define OS_DAT_FLAGS_MARK (0x1)
452
453 typedef struct os_dat_s {
454 __u8 dat_sz;
455 __u8 reserved1;
456 __u8 entry_cnt;
457 __u8 reserved3;
458 os_dat_entry_t dat_list[16];
459 } os_dat_t;
460
461 /*
462 * Frame types
463 */
464 #define OS_FRAME_TYPE_FILL (0)
465 #define OS_FRAME_TYPE_EOD (1 << 0)
466 #define OS_FRAME_TYPE_MARKER (1 << 1)
467 #define OS_FRAME_TYPE_HEADER (1 << 3)
468 #define OS_FRAME_TYPE_DATA (1 << 7)
469
470 /*
471 * AUX
472 */
473 typedef struct os_aux_s {
474 __u32 format_id; /* hardware compability AUX is based on */
475 char application_sig[4]; /* driver used to write this media */
476 __u32 hdwr; /* reserved */
477 __u32 update_frame_cntr; /* for configuration frame */
478 __u8 frame_type;
479 __u8 frame_type_reserved;
480 __u8 reserved_18_19[2];
481 os_partition_t partition;
482 __u8 reserved_36_43[8];
483 __u32 frame_seq_num;
484 __u32 logical_blk_num_high;
485 __u32 logical_blk_num;
486 os_dat_t dat;
487 __u8 reserved188_191[4];
488 __u32 filemark_cnt;
489 __u32 phys_fm;
490 __u32 last_mark_addr;
491 __u8 reserved204_223[20];
492
493 /*
494 * __u8 app_specific[32];
495 *
496 * Linux specific fields:
497 */
498 __u32 next_mark_addr; /* when known, points to next marker */
499 __u8 linux_specific[28];
500
501 __u8 reserved_256_511[256];
502 } os_aux_t;
503
504 typedef struct os_header_s {
505 char ident_str[8];
506 __u8 major_rev;
507 __u8 minor_rev;
508 __u8 reserved10_15[6];
509 __u8 par_num;
510 __u8 reserved1_3[3];
511 os_partition_t partition;
512 } os_header_t;
513
514 /*
515 * OnStream ADRL frame
516 */
517 #define OS_FRAME_SIZE (32 * 1024 + 512)
518 #define OS_DATA_SIZE (32 * 1024)
519 #define OS_AUX_SIZE (512)
520
521 #include <linux/mtio.h>
522
523 /**************************** Tunable parameters *****************************/
524
525
526 /*
527 * Pipelined mode parameters.
528 *
529 * We try to use the minimum number of stages which is enough to
530 * keep the tape constantly streaming. To accomplish that, we implement
531 * a feedback loop around the maximum number of stages:
532 *
533 * We start from MIN maximum stages (we will not even use MIN stages
534 * if we don't need them), increment it by RATE*(MAX-MIN)
535 * whenever we sense that the pipeline is empty, until we reach
536 * the optimum value or until we reach MAX.
537 *
538 * Setting the following parameter to 0 will disable the pipelined mode.
539 */
540 #define IDETAPE_MIN_PIPELINE_STAGES 200
541 #define IDETAPE_MAX_PIPELINE_STAGES 400
542 #define IDETAPE_INCREASE_STAGES_RATE 20
543
544 /*
545 * The following are used to debug the driver:
546 *
547 * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
548 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
549 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
550 * some places.
551 *
552 * Setting them to 0 will restore normal operation mode:
553 *
554 * 1. Disable logging normal successful operations.
555 * 2. Disable self-sanity checks.
556 * 3. Errors will still be logged, of course.
557 *
558 * All the #if DEBUG code will be removed some day, when the driver
559 * is verified to be stable enough. This will make it much more
560 * esthetic.
561 */
562 #define IDETAPE_DEBUG_INFO 0
563 #define IDETAPE_DEBUG_LOG 1
564 #define IDETAPE_DEBUG_LOG_VERBOSE 0
565 #define IDETAPE_DEBUG_BUGS 1
566
567 /*
568 * After each failed packet command we issue a request sense command
569 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
570 *
571 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
572 */
573 #define IDETAPE_MAX_PC_RETRIES 3
574
575 /*
576 * With each packet command, we allocate a buffer of
577 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
578 * commands (Not for READ/WRITE commands).
579 */
580 #define IDETAPE_PC_BUFFER_SIZE 256
581
582 /*
583 * In various places in the driver, we need to allocate storage
584 * for packet commands and requests, which will remain valid while
585 * we leave the driver to wait for an interrupt or a timeout event.
586 */
587 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
588
589 /*
590 * Some tape drives require a long irq timeout
591 */
592 #define IDETAPE_WAIT_CMD (60*HZ)
593
594 /*
595 * The following parameter is used to select the point in the internal
596 * tape fifo in which we will start to refill the buffer. Decreasing
597 * the following parameter will improve the system's latency and
598 * interactive response, while using a high value might improve sytem
599 * throughput.
600 */
601 #define IDETAPE_FIFO_THRESHOLD 2
602
603 /*
604 * DSC polling parameters.
605 *
606 * Polling for DSC (a single bit in the status register) is a very
607 * important function in ide-tape. There are two cases in which we
608 * poll for DSC:
609 *
610 * 1. Before a read/write packet command, to ensure that we
611 * can transfer data from/to the tape's data buffers, without
612 * causing an actual media access. In case the tape is not
613 * ready yet, we take out our request from the device
614 * request queue, so that ide.c will service requests from
615 * the other device on the same interface meanwhile.
616 *
617 * 2. After the successful initialization of a "media access
618 * packet command", which is a command which can take a long
619 * time to complete (it can be several seconds or even an hour).
620 *
621 * Again, we postpone our request in the middle to free the bus
622 * for the other device. The polling frequency here should be
623 * lower than the read/write frequency since those media access
624 * commands are slow. We start from a "fast" frequency -
625 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
626 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
627 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
628 *
629 * We also set a timeout for the timer, in case something goes wrong.
630 * The timeout should be longer then the maximum execution time of a
631 * tape operation.
632 */
633
634 /*
635 * DSC timings.
636 */
637 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
638 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
639 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
640 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
641 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
642 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
643 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
644
645 /*************************** End of tunable parameters ***********************/
646
647 /*
648 * Debugging/Performance analysis
649 *
650 * I/O trace support
651 */
652 #define USE_IOTRACE 0
653 #if USE_IOTRACE
654 #include <linux/io_trace.h>
655 #define IO_IDETAPE_FIFO 500
656 #endif
657
658 /*
659 * Read/Write error simulation
660 */
661 #define SIMULATE_ERRORS 0
662
663 /*
664 * For general magnetic tape device compatibility.
665 */
666 typedef enum {
667 idetape_direction_none,
668 idetape_direction_read,
669 idetape_direction_write
670 } idetape_chrdev_direction_t;
671
672 /*
673 * Our view of a packet command.
674 */
675 typedef struct idetape_packet_command_s {
676 u8 c[12]; /* Actual packet bytes */
677 int retries; /* On each retry, we increment retries */
678 int error; /* Error code */
679 int request_transfer; /* Bytes to transfer */
680 int actually_transferred; /* Bytes actually transferred */
681 int buffer_size; /* Size of our data buffer */
682 struct buffer_head *bh;
683 char *b_data;
684 int b_count;
685 byte *buffer; /* Data buffer */
686 byte *current_position; /* Pointer into the above buffer */
687 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
688 byte pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
689 unsigned long flags; /* Status/Action bit flags: long for set_bit */
690 } idetape_pc_t;
691
692 /*
693 * Packet command flag bits.
694 */
695 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
696 #define PC_WAIT_FOR_DSC 1 /* 1 When polling for DSC on a media access command */
697 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
698 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
699 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
700 #define PC_WRITING 5 /* Data direction */
701
702 /*
703 * Capabilities and Mechanical Status Page
704 */
705 typedef struct {
706 unsigned page_code :6; /* Page code - Should be 0x2a */
707 __u8 reserved0_6 :1;
708 __u8 ps :1; /* parameters saveable */
709 __u8 page_length; /* Page Length - Should be 0x12 */
710 __u8 reserved2, reserved3;
711 unsigned ro :1; /* Read Only Mode */
712 unsigned reserved4_1234 :4;
713 unsigned sprev :1; /* Supports SPACE in the reverse direction */
714 unsigned reserved4_67 :2;
715 unsigned reserved5_012 :3;
716 unsigned efmt :1; /* Supports ERASE command initiated formatting */
717 unsigned reserved5_4 :1;
718 unsigned qfa :1; /* Supports the QFA two partition formats */
719 unsigned reserved5_67 :2;
720 unsigned lock :1; /* Supports locking the volume */
721 unsigned locked :1; /* The volume is locked */
722 unsigned prevent :1; /* The device defaults in the prevent state after power up */
723 unsigned eject :1; /* The device can eject the volume */
724 __u8 disconnect :1; /* The device can break request > ctl */
725 __u8 reserved6_5 :1;
726 unsigned ecc :1; /* Supports error correction */
727 unsigned cmprs :1; /* Supports data compression */
728 unsigned reserved7_0 :1;
729 unsigned blk512 :1; /* Supports 512 bytes block size */
730 unsigned blk1024 :1; /* Supports 1024 bytes block size */
731 unsigned reserved7_3_6 :4;
732 unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
733 /* transfers for slow buffer memory ??? */
734 /* Also 32768 block size in some cases */
735 __u16 max_speed; /* Maximum speed supported in KBps */
736 __u8 reserved10, reserved11;
737 __u16 ctl; /* Continuous Transfer Limit in blocks */
738 __u16 speed; /* Current Speed, in KBps */
739 __u16 buffer_size; /* Buffer Size, in 512 bytes */
740 __u8 reserved18, reserved19;
741 } idetape_capabilities_page_t;
742
743 /*
744 * Block Size Page
745 */
746 typedef struct {
747 unsigned page_code :6; /* Page code - Should be 0x30 */
748 unsigned reserved1_6 :1;
749 unsigned ps :1;
750 __u8 page_length; /* Page Length - Should be 2 */
751 __u8 reserved2;
752 unsigned play32 :1;
753 unsigned play32_5 :1;
754 unsigned reserved2_23 :2;
755 unsigned record32 :1;
756 unsigned record32_5 :1;
757 unsigned reserved2_6 :1;
758 unsigned one :1;
759 } idetape_block_size_page_t;
760
761 /*
762 * A pipeline stage.
763 */
764 typedef struct idetape_stage_s {
765 struct request rq; /* The corresponding request */
766 struct buffer_head *bh; /* The data buffers */
767 struct idetape_stage_s *next; /* Pointer to the next stage */
768 os_aux_t *aux; /* OnStream aux ptr */
769 } idetape_stage_t;
770
771 /*
772 * REQUEST SENSE packet command result - Data Format.
773 */
774 typedef struct {
775 unsigned error_code :7; /* Current of deferred errors */
776 unsigned valid :1; /* The information field conforms to QIC-157C */
777 __u8 reserved1 :8; /* Segment Number - Reserved */
778 unsigned sense_key :4; /* Sense Key */
779 unsigned reserved2_4 :1; /* Reserved */
780 unsigned ili :1; /* Incorrect Length Indicator */
781 unsigned eom :1; /* End Of Medium */
782 unsigned filemark :1; /* Filemark */
783 __u32 information __attribute__ ((packed));
784 __u8 asl; /* Additional sense length (n-7) */
785 __u32 command_specific; /* Additional command specific information */
786 __u8 asc; /* Additional Sense Code */
787 __u8 ascq; /* Additional Sense Code Qualifier */
788 __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
789 unsigned sk_specific1 :7; /* Sense Key Specific */
790 unsigned sksv :1; /* Sense Key Specific information is valid */
791 __u8 sk_specific2; /* Sense Key Specific */
792 __u8 sk_specific3; /* Sense Key Specific */
793 __u8 pad[2]; /* Padding to 20 bytes */
794 } idetape_request_sense_result_t;
795
796
797 /*
798 * Most of our global data which we need to save even as we leave the
799 * driver due to an interrupt or a timer event is stored in a variable
800 * of type idetape_tape_t, defined below.
801 */
802 typedef struct {
803 ide_drive_t *drive;
804 devfs_handle_t de_r, de_n;
805
806 /*
807 * Since a typical character device operation requires more
808 * than one packet command, we provide here enough memory
809 * for the maximum of interconnected packet commands.
810 * The packet commands are stored in the circular array pc_stack.
811 * pc_stack_index points to the last used entry, and warps around
812 * to the start when we get to the last array entry.
813 *
814 * pc points to the current processed packet command.
815 *
816 * failed_pc points to the last failed packet command, or contains
817 * NULL if we do not need to retry any packet command. This is
818 * required since an additional packet command is needed before the
819 * retry, to get detailed information on what went wrong.
820 */
821 idetape_pc_t *pc; /* Current packet command */
822 idetape_pc_t *failed_pc; /* Last failed packet command */
823 idetape_pc_t pc_stack[IDETAPE_PC_STACK];/* Packet command stack */
824 int pc_stack_index; /* Next free packet command storage space */
825 struct request rq_stack[IDETAPE_PC_STACK];
826 int rq_stack_index; /* We implement a circular array */
827
828 /*
829 * DSC polling variables.
830 *
831 * While polling for DSC we use postponed_rq to postpone the
832 * current request so that ide.c will be able to service
833 * pending requests on the other device. Note that at most
834 * we will have only one DSC (usually data transfer) request
835 * in the device request queue. Additional requests can be
836 * queued in our internal pipeline, but they will be visible
837 * to ide.c only one at a time.
838 */
839 struct request *postponed_rq;
840 unsigned long dsc_polling_start; /* The time in which we started polling for DSC */
841 struct timer_list dsc_timer; /* Timer used to poll for dsc */
842 unsigned long best_dsc_rw_frequency; /* Read/Write dsc polling frequency */
843 unsigned long dsc_polling_frequency; /* The current polling frequency */
844 unsigned long dsc_timeout; /* Maximum waiting time */
845
846 /*
847 * Read position information
848 */
849 byte partition;
850 unsigned int first_frame_position; /* Current block */
851 unsigned int last_frame_position;
852 unsigned int blocks_in_buffer;
853
854 /*
855 * Last error information
856 */
857 byte sense_key, asc, ascq;
858
859 /*
860 * Character device operation
861 */
862 unsigned int minor;
863 char name[4]; /* device name */
864 idetape_chrdev_direction_t chrdev_direction; /* Current character device data transfer direction */
865
866 /*
867 * Device information
868 */
869 unsigned short tape_block_size; /* Usually 512 or 1024 bytes */
870 int user_bs_factor;
871 idetape_capabilities_page_t capabilities; /* Copy of the tape's Capabilities and Mechanical Page */
872
873 /*
874 * Active data transfer request parameters.
875 *
876 * At most, there is only one ide-tape originated data transfer
877 * request in the device request queue. This allows ide.c to
878 * easily service requests from the other device when we
879 * postpone our active request. In the pipelined operation
880 * mode, we use our internal pipeline structure to hold
881 * more data requests.
882 *
883 * The data buffer size is chosen based on the tape's
884 * recommendation.
885 */
886 struct request *active_data_request; /* Pointer to the request which is waiting in the device request queue */
887 int stage_size; /* Data buffer size (chosen based on the tape's recommendation */
888 idetape_stage_t *merge_stage;
889 int merge_stage_size;
890 struct buffer_head *bh;
891 char *b_data;
892 int b_count;
893
894 /*
895 * Pipeline parameters.
896 *
897 * To accomplish non-pipelined mode, we simply set the following
898 * variables to zero (or NULL, where appropriate).
899 */
900 int nr_stages; /* Number of currently used stages */
901 int nr_pending_stages; /* Number of pending stages */
902 int max_stages, min_pipeline, max_pipeline; /* We will not allocate more than this number of stages */
903 idetape_stage_t *first_stage; /* The first stage which will be removed from the pipeline */
904 idetape_stage_t *active_stage; /* The currently active stage */
905 idetape_stage_t *next_stage; /* Will be serviced after the currently active request */
906 idetape_stage_t *last_stage; /* New requests will be added to the pipeline here */
907 idetape_stage_t *cache_stage; /* Optional free stage which we can use */
908 int pages_per_stage;
909 int excess_bh_size; /* Wasted space in each stage */
910
911 unsigned long flags; /* Status/Action flags: long for set_bit */
912 spinlock_t spinlock; /* protects the ide-tape queue */
913
914 /*
915 * Measures average tape speed
916 */
917 unsigned long avg_time;
918 int avg_size;
919 int avg_speed;
920
921 idetape_request_sense_result_t sense; /* last sense information */
922
923 char vendor_id[10];
924 char product_id[18];
925 char firmware_revision[6];
926 int firmware_revision_num;
927
928 int door_locked; /* the door is currently locked */
929
930 /*
931 * OnStream flags
932 */
933 int onstream; /* the tape is an OnStream tape */
934 int raw; /* OnStream raw access (32.5KB block size) */
935 int cur_frames; /* current number of frames in internal buffer */
936 int max_frames; /* max number of frames in internal buffer */
937 int logical_blk_num; /* logical block number */
938 __u16 wrt_pass_cntr; /* write pass counter */
939 __u32 update_frame_cntr; /* update frame counter */
940 struct semaphore *sem;
941 int onstream_write_error; /* write error recovery active */
942 int header_ok; /* header frame verified ok */
943 int linux_media; /* reading linux-specifc media */
944 int linux_media_version;
945 char application_sig[5]; /* application signature */
946 int filemark_cnt;
947 int first_mark_addr;
948 int last_mark_addr;
949 int eod_frame_addr;
950 unsigned long cmd_start_time;
951 unsigned long max_cmd_time;
952
953 /*
954 * Optimize the number of "buffer filling"
955 * mode sense commands.
956 */
957 unsigned long last_buffer_fill; /* last time in which we issued fill cmd */
958 int req_buffer_fill; /* buffer fill command requested */
959 int writes_since_buffer_fill;
960 int reads_since_buffer_fill;
961
962 /*
963 * Limit the number of times a request can
964 * be postponed, to avoid an infinite postpone
965 * deadlock.
966 */
967 int postpone_cnt; /* request postpone count limit */
968
969 /*
970 * Measures number of frames:
971 *
972 * 1. written/read to/from the driver pipeline (pipeline_head).
973 * 2. written/read to/from the tape buffers (buffer_head).
974 * 3. written/read by the tape to/from the media (tape_head).
975 */
976 int pipeline_head;
977 int buffer_head;
978 int tape_head;
979 int last_tape_head;
980
981 /*
982 * Speed control at the tape buffers input/output
983 */
984 unsigned long insert_time;
985 int insert_size;
986 int insert_speed;
987 int max_insert_speed;
988 int measure_insert_time;
989
990 /*
991 * Measure tape still time, in milliseconds
992 */
993 unsigned long tape_still_time_begin;
994 int tape_still_time;
995
996 /*
997 * Speed regulation negative feedback loop
998 */
999 int speed_control;
1000 int pipeline_head_speed, controlled_pipeline_head_speed, uncontrolled_pipeline_head_speed;
1001 int controlled_last_pipeline_head, uncontrolled_last_pipeline_head;
1002 unsigned long uncontrolled_pipeline_head_time, controlled_pipeline_head_time;
1003 int controlled_previous_pipeline_head, uncontrolled_previous_pipeline_head;
1004 unsigned long controlled_previous_head_time, uncontrolled_previous_head_time;
1005 int restart_speed_control_req;
1006
1007 /*
1008 * Debug_level determines amount of debugging output;
1009 * can be changed using /proc/ide/hdx/settings
1010 * 0 : almost no debugging output
1011 * 1 : 0+output errors only
1012 * 2 : 1+output all sensekey/asc
1013 * 3 : 2+follow all chrdev related procedures
1014 * 4 : 3+follow all procedures
1015 * 5 : 4+include pc_stack rq_stack info
1016 * 6 : 5+USE_COUNT updates
1017 */
1018 int debug_level;
1019 } idetape_tape_t;
1020
1021 /*
1022 * Tape door status
1023 */
1024 #define DOOR_UNLOCKED 0
1025 #define DOOR_LOCKED 1
1026 #define DOOR_EXPLICITLY_LOCKED 2
1027
1028 /*
1029 * Tape flag bits values.
1030 */
1031 #define IDETAPE_IGNORE_DSC 0
1032 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
1033 #define IDETAPE_BUSY 2 /* Device already opened */
1034 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
1035 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
1036 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
1037 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
1038 #define IDETAPE_READ_ERROR 7
1039 #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
1040
1041 /*
1042 * Supported ATAPI tape drives packet commands
1043 */
1044 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
1045 #define IDETAPE_REWIND_CMD 0x01
1046 #define IDETAPE_REQUEST_SENSE_CMD 0x03
1047 #define IDETAPE_READ_CMD 0x08
1048 #define IDETAPE_WRITE_CMD 0x0a
1049 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
1050 #define IDETAPE_SPACE_CMD 0x11
1051 #define IDETAPE_INQUIRY_CMD 0x12
1052 #define IDETAPE_ERASE_CMD 0x19
1053 #define IDETAPE_MODE_SENSE_CMD 0x1a
1054 #define IDETAPE_MODE_SELECT_CMD 0x15
1055 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
1056 #define IDETAPE_PREVENT_CMD 0x1e
1057 #define IDETAPE_LOCATE_CMD 0x2b
1058 #define IDETAPE_READ_POSITION_CMD 0x34
1059 #define IDETAPE_READ_BUFFER_CMD 0x3c
1060 #define IDETAPE_SET_SPEED_CMD 0xbb
1061
1062 /*
1063 * Some defines for the READ BUFFER command
1064 */
1065 #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
1066
1067 /*
1068 * Some defines for the SPACE command
1069 */
1070 #define IDETAPE_SPACE_OVER_FILEMARK 1
1071 #define IDETAPE_SPACE_TO_EOD 3
1072
1073 /*
1074 * Some defines for the LOAD UNLOAD command
1075 */
1076 #define IDETAPE_LU_LOAD_MASK 1
1077 #define IDETAPE_LU_RETENSION_MASK 2
1078 #define IDETAPE_LU_EOT_MASK 4
1079
1080 /*
1081 * Special requests for our block device strategy routine.
1082 *
1083 * In order to service a character device command, we add special
1084 * requests to the tail of our block device request queue and wait
1085 * for their completion.
1086 *
1087 */
1088 #define IDETAPE_FIRST_RQ 90
1089
1090 /*
1091 * IDETAPE_PC_RQ is used to queue a packet command in the request queue.
1092 */
1093 #define IDETAPE_PC_RQ1 90
1094 #define IDETAPE_PC_RQ2 91
1095
1096 /*
1097 * IDETAPE_READ_RQ and IDETAPE_WRITE_RQ are used by our
1098 * character device interface to request read/write operations from
1099 * our block device interface.
1100 */
1101 #define IDETAPE_READ_RQ 92
1102 #define IDETAPE_WRITE_RQ 93
1103 #define IDETAPE_ABORTED_WRITE_RQ 94
1104 #define IDETAPE_ABORTED_READ_RQ 95
1105 #define IDETAPE_READ_BUFFER_RQ 96
1106
1107 #define IDETAPE_LAST_RQ 96
1108
1109 /*
1110 * A macro which can be used to check if a we support a given
1111 * request command.
1112 */
1113 #define IDETAPE_RQ_CMD(cmd) ((cmd >= IDETAPE_FIRST_RQ) && (cmd <= IDETAPE_LAST_RQ))
1114
1115 /*
1116 * Error codes which are returned in rq->errors to the higher part
1117 * of the driver.
1118 */
1119 #define IDETAPE_ERROR_GENERAL 101
1120 #define IDETAPE_ERROR_FILEMARK 102
1121 #define IDETAPE_ERROR_EOD 103
1122
1123 /*
1124 * The ATAPI Status Register.
1125 */
1126 typedef union {
1127 unsigned all :8;
1128 struct {
1129 unsigned check :1; /* Error occurred */
1130 unsigned idx :1; /* Reserved */
1131 unsigned corr :1; /* Correctable error occurred */
1132 unsigned drq :1; /* Data is request by the device */
1133 unsigned dsc :1; /* Buffer availability / Media access command finished */
1134 unsigned reserved5 :1; /* Reserved */
1135 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
1136 unsigned bsy :1; /* The device has access to the command block */
1137 } b;
1138 } idetape_status_reg_t;
1139
1140 /*
1141 * The ATAPI error register.
1142 */
1143 typedef union {
1144 unsigned all :8;
1145 struct {
1146 unsigned ili :1; /* Illegal Length Indication */
1147 unsigned eom :1; /* End Of Media Detected */
1148 unsigned abrt :1; /* Aborted command - As defined by ATA */
1149 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
1150 unsigned sense_key :4; /* Sense key of the last failed packet command */
1151 } b;
1152 } idetape_error_reg_t;
1153
1154 /*
1155 * ATAPI Feature Register
1156 */
1157 typedef union {
1158 unsigned all :8;
1159 struct {
1160 unsigned dma :1; /* Using DMA of PIO */
1161 unsigned reserved321 :3; /* Reserved */
1162 unsigned reserved654 :3; /* Reserved (Tag Type) */
1163 unsigned reserved7 :1; /* Reserved */
1164 } b;
1165 } idetape_feature_reg_t;
1166
1167 /*
1168 * ATAPI Byte Count Register.
1169 */
1170 typedef union {
1171 unsigned all :16;
1172 struct {
1173 unsigned low :8; /* LSB */
1174 unsigned high :8; /* MSB */
1175 } b;
1176 } idetape_bcount_reg_t;
1177
1178 /*
1179 * ATAPI Interrupt Reason Register.
1180 */
1181 typedef union {
1182 unsigned all :8;
1183 struct {
1184 unsigned cod :1; /* Information transferred is command (1) or data (0) */
1185 unsigned io :1; /* The device requests us to read (1) or write (0) */
1186 unsigned reserved :6; /* Reserved */
1187 } b;
1188 } idetape_ireason_reg_t;
1189
1190 /*
1191 * ATAPI Drive Select Register
1192 */
1193 typedef union {
1194 unsigned all :8;
1195 struct {
1196 unsigned sam_lun :4; /* Should be zero with ATAPI (not used) */
1197 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
1198 unsigned one5 :1; /* Should be set to 1 */
1199 unsigned reserved6 :1; /* Reserved */
1200 unsigned one7 :1; /* Should be set to 1 */
1201 } b;
1202 } idetape_drivesel_reg_t;
1203
1204 /*
1205 * ATAPI Device Control Register
1206 */
1207 typedef union {
1208 unsigned all :8;
1209 struct {
1210 unsigned zero0 :1; /* Should be set to zero */
1211 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
1212 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
1213 unsigned one3 :1; /* Should be set to 1 */
1214 unsigned reserved4567 :4; /* Reserved */
1215 } b;
1216 } idetape_control_reg_t;
1217
1218 /*
1219 * idetape_chrdev_t provides the link between out character device
1220 * interface and our block device interface and the corresponding
1221 * ide_drive_t structure.
1222 */
1223 typedef struct {
1224 ide_drive_t *drive;
1225 } idetape_chrdev_t;
1226
1227 /*
1228 * The following is used to format the general configuration word of
1229 * the ATAPI IDENTIFY DEVICE command.
1230 */
1231 struct idetape_id_gcw {
1232 unsigned packet_size :2; /* Packet Size */
1233 unsigned reserved234 :3; /* Reserved */
1234 unsigned drq_type :2; /* Command packet DRQ type */
1235 unsigned removable :1; /* Removable media */
1236 unsigned device_type :5; /* Device type */
1237 unsigned reserved13 :1; /* Reserved */
1238 unsigned protocol :2; /* Protocol type */
1239 };
1240
1241 /*
1242 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
1243 */
1244 typedef struct {
1245 unsigned device_type :5; /* Peripheral Device Type */
1246 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
1247 unsigned reserved1_6t0 :7; /* Reserved */
1248 unsigned rmb :1; /* Removable Medium Bit */
1249 unsigned ansi_version :3; /* ANSI Version */
1250 unsigned ecma_version :3; /* ECMA Version */
1251 unsigned iso_version :2; /* ISO Version */
1252 unsigned response_format :4; /* Response Data Format */
1253 unsigned reserved3_45 :2; /* Reserved */
1254 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
1255 unsigned reserved3_7 :1; /* AENC - Reserved */
1256 __u8 additional_length; /* Additional Length (total_length-4) */
1257 __u8 rsv5, rsv6, rsv7; /* Reserved */
1258 __u8 vendor_id[8]; /* Vendor Identification */
1259 __u8 product_id[16]; /* Product Identification */
1260 __u8 revision_level[4]; /* Revision Level */
1261 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
1262 __u8 reserved56t95[40]; /* Reserved - Optional */
1263 /* Additional information may be returned */
1264 } idetape_inquiry_result_t;
1265
1266 /*
1267 * READ POSITION packet command - Data Format (From Table 6-57)
1268 */
1269 typedef struct {
1270 unsigned reserved0_10 :2; /* Reserved */
1271 unsigned bpu :1; /* Block Position Unknown */
1272 unsigned reserved0_543 :3; /* Reserved */
1273 unsigned eop :1; /* End Of Partition */
1274 unsigned bop :1; /* Beginning Of Partition */
1275 u8 partition; /* Partition Number */
1276 u8 reserved2, reserved3; /* Reserved */
1277 u32 first_block; /* First Block Location */
1278 u32 last_block; /* Last Block Location (Optional) */
1279 u8 reserved12; /* Reserved */
1280 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
1281 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
1282 } idetape_read_position_result_t;
1283
1284 /*
1285 * Follows structures which are related to the SELECT SENSE / MODE SENSE
1286 * packet commands. Those packet commands are still not supported
1287 * by ide-tape.
1288 */
1289 #define IDETAPE_CAPABILITIES_PAGE 0x2a
1290 #define IDETAPE_BLOCK_SIZE_PAGE 0x30
1291
1292 /*
1293 * Mode Parameter Header for the MODE SENSE packet command
1294 */
1295 typedef struct {
1296 __u8 mode_data_length; /* Length of the following data transfer */
1297 __u8 medium_type; /* Medium Type */
1298 __u8 dsp; /* Device Specific Parameter */
1299 __u8 bdl; /* Block Descriptor Length */
1300 #if 0
1301 /* data transfer page */
1302 __u8 page_code :6;
1303 __u8 reserved0_6 :1;
1304 __u8 ps :1; /* parameters saveable */
1305 __u8 page_length; /* page Length == 0x02 */
1306 __u8 reserved2;
1307 __u8 read32k :1; /* 32k blk size (data only) */
1308 __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
1309 __u8 reserved3_23 :2;
1310 __u8 write32k :1; /* 32k blk size (data only) */
1311 __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
1312 __u8 reserved3_6 :1;
1313 __u8 streaming :1; /* streaming mode enable */
1314 #endif
1315 } idetape_mode_parameter_header_t;
1316
1317 /*
1318 * Mode Parameter Block Descriptor the MODE SENSE packet command
1319 *
1320 * Support for block descriptors is optional.
1321 */
1322 typedef struct {
1323 __u8 density_code; /* Medium density code */
1324 __u8 blocks[3]; /* Number of blocks */
1325 __u8 reserved4; /* Reserved */
1326 __u8 length[3]; /* Block Length */
1327 } idetape_parameter_block_descriptor_t;
1328
1329 /*
1330 * The Data Compression Page, as returned by the MODE SENSE packet command.
1331 */
1332 typedef struct {
1333 unsigned page_code :6; /* Page Code - Should be 0xf */
1334 unsigned reserved0 :1; /* Reserved */
1335 unsigned ps :1;
1336 __u8 page_length; /* Page Length - Should be 14 */
1337 unsigned reserved2 :6; /* Reserved */
1338 unsigned dcc :1; /* Data Compression Capable */
1339 unsigned dce :1; /* Data Compression Enable */
1340 unsigned reserved3 :5; /* Reserved */
1341 unsigned red :2; /* Report Exception on Decompression */
1342 unsigned dde :1; /* Data Decompression Enable */
1343 __u32 ca; /* Compression Algorithm */
1344 __u32 da; /* Decompression Algorithm */
1345 __u8 reserved[4]; /* Reserved */
1346 } idetape_data_compression_page_t;
1347
1348 /*
1349 * The Medium Partition Page, as returned by the MODE SENSE packet command.
1350 */
1351 typedef struct {
1352 unsigned page_code :6; /* Page Code - Should be 0x11 */
1353 unsigned reserved1_6 :1; /* Reserved */
1354 unsigned ps :1;
1355 __u8 page_length; /* Page Length - Should be 6 */
1356 __u8 map; /* Maximum Additional Partitions - Should be 0 */
1357 __u8 apd; /* Additional Partitions Defined - Should be 0 */
1358 unsigned reserved4_012 :3; /* Reserved */
1359 unsigned psum :2; /* Should be 0 */
1360 unsigned idp :1; /* Should be 0 */
1361 unsigned sdp :1; /* Should be 0 */
1362 unsigned fdp :1; /* Fixed Data Partitions */
1363 __u8 mfr; /* Medium Format Recognition */
1364 __u8 reserved[2]; /* Reserved */
1365 } idetape_medium_partition_page_t;
1366
1367 /*
1368 * Run time configurable parameters.
1369 */
1370 typedef struct {
1371 int dsc_rw_frequency;
1372 int dsc_media_access_frequency;
1373 int nr_stages;
1374 } idetape_config_t;
1375
1376 /*
1377 * The variables below are used for the character device interface.
1378 * Additional state variables are defined in our ide_drive_t structure.
1379 */
1380 static idetape_chrdev_t idetape_chrdevs[MAX_HWIFS * MAX_DRIVES];
1381 static int idetape_chrdev_present = 0;
1382
1383 #if IDETAPE_DEBUG_LOG_VERBOSE
1384
1385 /*
1386 * DO NOT REMOVE, BUILDING A VERBOSE DEBUG SCHEME FOR ATAPI
1387 */
1388
1389 char *idetape_sense_key_verbose (byte idetape_sense_key)
1390 {
1391 switch (idetape_sense_key) {
1392 default: {
1393 char buf[22];
1394 sprintf(buf, "IDETAPE_SENSE (0x%02x)", idetape_sense_key);
1395 return(buf);
1396 }
1397
1398 }
1399 }
1400
1401 char *idetape_command_key_verbose (byte idetape_command_key)
1402 {
1403 switch (idetape_command_key) {
1404 case IDETAPE_TEST_UNIT_READY_CMD: return("TEST_UNIT_READY_CMD");
1405 case IDETAPE_REWIND_CMD: return("REWIND_CMD");
1406 case IDETAPE_REQUEST_SENSE_CMD: return("REQUEST_SENSE_CMD");
1407 case IDETAPE_READ_CMD: return("READ_CMD");
1408 case IDETAPE_WRITE_CMD: return("WRITE_CMD");
1409 case IDETAPE_WRITE_FILEMARK_CMD: return("WRITE_FILEMARK_CMD");
1410 case IDETAPE_SPACE_CMD: return("SPACE_CMD");
1411 case IDETAPE_INQUIRY_CMD: return("INQUIRY_CMD");
1412 case IDETAPE_ERASE_CMD: return("ERASE_CMD")
1413 case IDETAPE_MODE_SENSE_CMD: return("MODE_SENSE_CMD");
1414 case IDETAPE_MODE_SELECT_CMD: return("MODE_SELECT_CMD");
1415 case IDETAPE_LOAD_UNLOAD_CMD: return("LOAD_UNLOAD_CMD");
1416 case IDETAPE_PREVENT_CMD: return("PREVENT_CMD");
1417 case IDETAPE_LOCATE_CMD: return("LOCATE_CMD");
1418 case IDETAPE_READ_POSITION_CMD: return("READ_POSITION_CMD");
1419 case IDETAPE_READ_BUFFER_CMD: return("READ_BUFFER_CMD");
1420 case IDETAPE_SET_SPEED_CMD: return("SET_SPEED_CMD");
1421 default: {
1422 char buf[20];
1423 sprintf(buf, "CMD (0x%02x)", idetape_command_key);
1424 return(buf);
1425 }
1426 }
1427 }
1428 #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
1429
1430 /*
1431 * Too bad. The drive wants to send us data which we are not ready to accept.
1432 * Just throw it away.
1433 */
1434 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1435 {
1436 while (bcount--)
1437 IN_BYTE (IDE_DATA_REG);
1438 }
1439
1440 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1441 {
1442 struct buffer_head *bh = pc->bh;
1443 int count;
1444
1445 while (bcount) {
1446 #if IDETAPE_DEBUG_BUGS
1447 if (bh == NULL) {
1448 printk (KERN_ERR "ide-tape: bh == NULL in idetape_input_buffers\n");
1449 idetape_discard_data (drive, bcount);
1450 return;
1451 }
1452 #endif /* IDETAPE_DEBUG_BUGS */
1453 count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), bcount);
1454 atapi_input_bytes (drive, bh->b_data + atomic_read(&bh->b_count), count);
1455 bcount -= count; atomic_add(count, &bh->b_count);
1456 if (atomic_read(&bh->b_count) == bh->b_size) {
1457 bh = bh->b_reqnext;
1458 if (bh)
1459 atomic_set(&bh->b_count, 0);
1460 }
1461 }
1462 pc->bh = bh;
1463 }
1464
1465 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1466 {
1467 struct buffer_head *bh = pc->bh;
1468 int count;
1469
1470 while (bcount) {
1471 #if IDETAPE_DEBUG_BUGS
1472 if (bh == NULL) {
1473 printk (KERN_ERR "ide-tape: bh == NULL in idetape_output_buffers\n");
1474 return;
1475 }
1476 #endif /* IDETAPE_DEBUG_BUGS */
1477 count = IDE_MIN (pc->b_count, bcount);
1478 atapi_output_bytes (drive, pc->b_data, count);
1479 bcount -= count; pc->b_data += count; pc->b_count -= count;
1480 if (!pc->b_count) {
1481 pc->bh = bh = bh->b_reqnext;
1482 if (bh) {
1483 pc->b_data = bh->b_data;
1484 pc->b_count = atomic_read(&bh->b_count);
1485 }
1486 }
1487 }
1488 }
1489
1490 #ifdef CONFIG_BLK_DEV_IDEDMA
1491 static void idetape_update_buffers (idetape_pc_t *pc)
1492 {
1493 struct buffer_head *bh = pc->bh;
1494 int count, bcount = pc->actually_transferred;
1495
1496 if (test_bit (PC_WRITING, &pc->flags))
1497 return;
1498 while (bcount) {
1499 #if IDETAPE_DEBUG_BUGS
1500 if (bh == NULL) {
1501 printk (KERN_ERR "ide-tape: bh == NULL in idetape_update_buffers\n");
1502 return;
1503 }
1504 #endif /* IDETAPE_DEBUG_BUGS */
1505 count = IDE_MIN (bh->b_size, bcount);
1506 atomic_set(&bh->b_count, count);
1507 if (atomic_read(&bh->b_count) == bh->b_size)
1508 bh = bh->b_reqnext;
1509 bcount -= count;
1510 }
1511 pc->bh = bh;
1512 }
1513 #endif /* CONFIG_BLK_DEV_IDEDMA */
1514
1515 /*
1516 * idetape_next_pc_storage returns a pointer to a place in which we can
1517 * safely store a packet command, even though we intend to leave the
1518 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1519 * commands is allocated at initialization time.
1520 */
1521 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1522 {
1523 idetape_tape_t *tape = drive->driver_data;
1524
1525 #if IDETAPE_DEBUG_LOG
1526 if (tape->debug_level >= 5)
1527 printk (KERN_INFO "ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
1528 #endif /* IDETAPE_DEBUG_LOG */
1529 if (tape->pc_stack_index==IDETAPE_PC_STACK)
1530 tape->pc_stack_index=0;
1531 return (&tape->pc_stack[tape->pc_stack_index++]);
1532 }
1533
1534 /*
1535 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1536 * Since we queue packet commands in the request queue, we need to
1537 * allocate a request, along with the allocation of a packet command.
1538 */
1539
1540 /**************************************************************
1541 * *
1542 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
1543 * followed later on by kfree(). -ml *
1544 * *
1545 **************************************************************/
1546
1547 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1548 {
1549 idetape_tape_t *tape = drive->driver_data;
1550
1551 #if IDETAPE_DEBUG_LOG
1552 if (tape->debug_level >= 5)
1553 printk (KERN_INFO "ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
1554 #endif /* IDETAPE_DEBUG_LOG */
1555 if (tape->rq_stack_index==IDETAPE_PC_STACK)
1556 tape->rq_stack_index=0;
1557 return (&tape->rq_stack[tape->rq_stack_index++]);
1558 }
1559
1560 /*
1561 * idetape_init_pc initializes a packet command.
1562 */
1563 static void idetape_init_pc (idetape_pc_t *pc)
1564 {
1565 memset (pc->c, 0, 12);
1566 pc->retries = 0;
1567 pc->flags = 0;
1568 pc->request_transfer = 0;
1569 pc->buffer = pc->pc_buffer;
1570 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1571 pc->bh = NULL;
1572 pc->b_data = NULL;
1573 }
1574
1575 /*
1576 * idetape_analyze_error is called on each failed packet command retry
1577 * to analyze the request sense. We currently do not utilize this
1578 * information.
1579 */
1580 static void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
1581 {
1582 idetape_tape_t *tape = drive->driver_data;
1583 idetape_pc_t *pc = tape->failed_pc;
1584
1585 tape->sense = *result;
1586 tape->sense_key = result->sense_key; tape->asc = result->asc; tape->ascq = result->ascq;
1587 #if IDETAPE_DEBUG_LOG
1588 /*
1589 * Without debugging, we only log an error if we decided to
1590 * give up retrying.
1591 */
1592 if (tape->debug_level >= 1)
1593 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);
1594 #if IDETAPE_DEBUG_LOG_VERBOSE
1595 if (tape->debug_level >= 1)
1596 printk (KERN_INFO "ide-tape: pc = %s, sense key = %x, asc = %x, ascq = %x\n",
1597 idetape_command_key_verbose((byte) pc->c[0]),
1598 result->sense_key,
1599 result->asc,
1600 result->ascq);
1601 #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
1602 #endif /* IDETAPE_DEBUG_LOG */
1603
1604 if (tape->onstream && result->sense_key == 2 && result->asc == 0x53 && result->ascq == 2) {
1605 clear_bit(PC_DMA_ERROR, &pc->flags);
1606 ide_stall_queue(drive, HZ / 2);
1607 return;
1608 }
1609 #ifdef CONFIG_BLK_DEV_IDEDMA
1610
1611 /*
1612 * Correct pc->actually_transferred by asking the tape.
1613 */
1614 if (test_bit (PC_DMA_ERROR, &pc->flags)) {
1615 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl (get_unaligned (&result->information));
1616 idetape_update_buffers (pc);
1617 }
1618 #endif /* CONFIG_BLK_DEV_IDEDMA */
1619 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1620 pc->error = IDETAPE_ERROR_FILEMARK;
1621 set_bit (PC_ABORT, &pc->flags);
1622 }
1623 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1624 if (result->eom || (result->sense_key == 0xd && result->asc == 0x0 && result->ascq == 0x2)) {
1625 pc->error = IDETAPE_ERROR_EOD;
1626 set_bit (PC_ABORT, &pc->flags);
1627 }
1628 }
1629 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1630 if (result->sense_key == 8) {
1631 pc->error = IDETAPE_ERROR_EOD;
1632 set_bit (PC_ABORT, &pc->flags);
1633 }
1634 if (!test_bit (PC_ABORT, &pc->flags) && (tape->onstream || pc->actually_transferred))
1635 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1636 }
1637 }
1638
1639 static void idetape_abort_pipeline (ide_drive_t *drive)
1640 {
1641 idetape_tape_t *tape = drive->driver_data;
1642 idetape_stage_t *stage = tape->next_stage;
1643
1644 #if IDETAPE_DEBUG_LOG
1645 if (tape->debug_level >= 4)
1646 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1647 #endif
1648 while (stage) {
1649 if (stage->rq.cmd == IDETAPE_WRITE_RQ)
1650 stage->rq.cmd = IDETAPE_ABORTED_WRITE_RQ;
1651 else if (stage->rq.cmd == IDETAPE_READ_RQ)
1652 stage->rq.cmd = IDETAPE_ABORTED_READ_RQ;
1653 stage = stage->next;
1654 }
1655 }
1656
1657 /*
1658 * idetape_active_next_stage will declare the next stage as "active".
1659 */
1660 static void idetape_active_next_stage (ide_drive_t *drive)
1661 {
1662 idetape_tape_t *tape = drive->driver_data;
1663 idetape_stage_t *stage=tape->next_stage;
1664 struct request *rq = &stage->rq;
1665
1666 #if IDETAPE_DEBUG_LOG
1667 if (tape->debug_level >= 4)
1668 printk (KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1669 #endif /* IDETAPE_DEBUG_LOG */
1670 #if IDETAPE_DEBUG_BUGS
1671 if (stage == NULL) {
1672 printk (KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1673 return;
1674 }
1675 #endif /* IDETAPE_DEBUG_BUGS */
1676
1677 rq->buffer = NULL;
1678 rq->bh = stage->bh;
1679 tape->active_data_request=rq;
1680 tape->active_stage=stage;
1681 tape->next_stage=stage->next;
1682 }
1683
1684 /*
1685 * idetape_increase_max_pipeline_stages is a part of the feedback
1686 * loop which tries to find the optimum number of stages. In the
1687 * feedback loop, we are starting from a minimum maximum number of
1688 * stages, and if we sense that the pipeline is empty, we try to
1689 * increase it, until we reach the user compile time memory limit.
1690 */
1691 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1692 {
1693 idetape_tape_t *tape = drive->driver_data;
1694 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1695
1696 #if IDETAPE_DEBUG_LOG
1697 if (tape->debug_level >= 4)
1698 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1699 #endif /* IDETAPE_DEBUG_LOG */
1700
1701 tape->max_stages += increase;
1702 tape->max_stages = IDE_MAX(tape->max_stages, tape->min_pipeline);
1703 tape->max_stages = IDE_MIN(tape->max_stages, tape->max_pipeline);
1704 }
1705
1706 /*
1707 * idetape_kfree_stage calls kfree to completely free a stage, along with
1708 * its related buffers.
1709 */
1710 static void __idetape_kfree_stage (idetape_stage_t *stage)
1711 {
1712 struct buffer_head *prev_bh, *bh = stage->bh;
1713 int size;
1714
1715 while (bh != NULL) {
1716 if (bh->b_data != NULL) {
1717 size = (int) bh->b_size;
1718 while (size > 0) {
1719 free_page ((unsigned long) bh->b_data);
1720 size -= PAGE_SIZE;
1721 bh->b_data += PAGE_SIZE;
1722 }
1723 }
1724 prev_bh = bh;
1725 bh = bh->b_reqnext;
1726 kfree (prev_bh);
1727 }
1728 kfree (stage);
1729 }
1730
1731 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1732 {
1733 __idetape_kfree_stage (stage);
1734 }
1735
1736 /*
1737 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1738 * The caller should avoid race conditions.
1739 */
1740 static void idetape_remove_stage_head (ide_drive_t *drive)
1741 {
1742 idetape_tape_t *tape = drive->driver_data;
1743 idetape_stage_t *stage;
1744
1745 #if IDETAPE_DEBUG_LOG
1746 if (tape->debug_level >= 4)
1747 printk (KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1748 #endif /* IDETAPE_DEBUG_LOG */
1749 #if IDETAPE_DEBUG_BUGS
1750 if (tape->first_stage == NULL) {
1751 printk (KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1752 return;
1753 }
1754 if (tape->active_stage == tape->first_stage) {
1755 printk (KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1756 return;
1757 }
1758 #endif /* IDETAPE_DEBUG_BUGS */
1759 stage=tape->first_stage;
1760 tape->first_stage=stage->next;
1761 idetape_kfree_stage (tape, stage);
1762 tape->nr_stages--;
1763 if (tape->first_stage == NULL) {
1764 tape->last_stage=NULL;
1765 #if IDETAPE_DEBUG_BUGS
1766 if (tape->next_stage != NULL)
1767 printk (KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1768 if (tape->nr_stages)
1769 printk (KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1770 #endif /* IDETAPE_DEBUG_BUGS */
1771 }
1772 }
1773
1774 /*
1775 * idetape_end_request is used to finish servicing a request, and to
1776 * insert a pending pipeline request into the main device queue.
1777 */
1778 static void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
1779 {
1780 ide_drive_t *drive = hwgroup->drive;
1781 struct request *rq = hwgroup->rq;
1782 idetape_tape_t *tape = drive->driver_data;
1783 unsigned long flags;
1784 int error;
1785 int remove_stage = 0;
1786 #if ONSTREAM_DEBUG
1787 idetape_stage_t *stage;
1788 os_aux_t *aux;
1789 unsigned char *p;
1790 #endif
1791
1792 #if IDETAPE_DEBUG_LOG
1793 if (tape->debug_level >= 4)
1794 printk (KERN_INFO "ide-tape: Reached idetape_end_request\n");
1795 #endif /* IDETAPE_DEBUG_LOG */
1796
1797 switch (uptodate) {
1798 case 0: error = IDETAPE_ERROR_GENERAL; break;
1799 case 1: error = 0; break;
1800 default: error = uptodate;
1801 }
1802 rq->errors = error;
1803 if (error)
1804 tape->failed_pc = NULL;
1805
1806 spin_lock_irqsave(&tape->spinlock, flags);
1807 if (tape->active_data_request == rq) { /* The request was a pipelined data transfer request */
1808 tape->active_stage = NULL;
1809 tape->active_data_request = NULL;
1810 tape->nr_pending_stages--;
1811 if (rq->cmd == IDETAPE_WRITE_RQ) {
1812 #if ONSTREAM_DEBUG
1813 if (tape->debug_level >= 2) {
1814 if (tape->onstream) {
1815 stage = tape->first_stage;
1816 aux = stage->aux;
1817 p = stage->bh->b_data;
1818 if (ntohl(aux->logical_blk_num) < 11300 && ntohl(aux->logical_blk_num) > 11100)
1819 printk(KERN_INFO "ide-tape: finished writing logical blk %u (data %x %x %x %x)\n", ntohl(aux->logical_blk_num), *p++, *p++, *p++, *p++);
1820 }
1821 }
1822 #endif
1823 if (tape->onstream && !tape->raw) {
1824 if (tape->first_frame_position == 0xba4) {
1825 #if ONSTREAM_DEBUG
1826 if (tape->debug_level >= 2)
1827 printk("ide-tape: %s: skipping over config parition..\n", tape->name);
1828 #endif
1829 tape->onstream_write_error = 2;
1830 if (tape->sem)
1831 up(tape->sem);
1832 }
1833 }
1834 remove_stage = 1;
1835 if (error) {
1836 set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1837 if (error == IDETAPE_ERROR_EOD)
1838 idetape_abort_pipeline (drive);
1839 if (tape->onstream && !tape->raw && error == IDETAPE_ERROR_GENERAL && tape->sense.sense_key == 3) {
1840 clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1841 printk(KERN_ERR "ide-tape: %s: write error, enabling error recovery\n", tape->name);
1842 tape->onstream_write_error = 1;
1843 remove_stage = 0;
1844 tape->nr_pending_stages++;
1845 tape->next_stage = tape->first_stage;
1846 rq->current_nr_sectors = rq->nr_sectors;
1847 if (tape->sem)
1848 up(tape->sem);
1849 }
1850 }
1851 } else if (rq->cmd == IDETAPE_READ_RQ) {
1852 if (error == IDETAPE_ERROR_EOD) {
1853 set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
1854 idetape_abort_pipeline(drive);
1855 }
1856 }
1857 if (tape->next_stage != NULL && !tape->onstream_write_error) {
1858 idetape_active_next_stage (drive);
1859
1860 /*
1861 * Insert the next request into the request queue.
1862 */
1863 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
1864 } else if (!error) {
1865 if (!tape->onstream)
1866 idetape_increase_max_pipeline_stages (drive);
1867 }
1868 }
1869 ide_end_drive_cmd (drive, 0, 0);
1870 if (remove_stage)
1871 idetape_remove_stage_head (drive);
1872 if (tape->active_data_request == NULL)
1873 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1874 spin_unlock_irqrestore(&tape->spinlock, flags);
1875 }
1876
1877 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1878 {
1879 idetape_tape_t *tape = drive->driver_data;
1880
1881 #if IDETAPE_DEBUG_LOG
1882 if (tape->debug_level >= 4)
1883 printk (KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1884 #endif /* IDETAPE_DEBUG_LOG */
1885 if (!tape->pc->error) {
1886 idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
1887 idetape_end_request (1,HWGROUP (drive));
1888 } else {
1889 printk (KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1890 idetape_end_request (0,HWGROUP (drive));
1891 }
1892 return ide_stopped;
1893 }
1894
1895 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1896 {
1897 idetape_init_pc (pc);
1898 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1899 pc->c[4] = 20;
1900 pc->request_transfer = 18;
1901 pc->callback = &idetape_request_sense_callback;
1902 }
1903
1904 /*
1905 * idetape_queue_pc_head generates a new packet command request in front
1906 * of the request queue, before the current request, so that it will be
1907 * processed immediately, on the next pass through the driver.
1908 *
1909 * idetape_queue_pc_head is called from the request handling part of
1910 * the driver (the "bottom" part). Safe storage for the request should
1911 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1912 * before calling idetape_queue_pc_head.
1913 *
1914 * Memory for those requests is pre-allocated at initialization time, and
1915 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1916 * space for the maximum possible number of inter-dependent packet commands.
1917 *
1918 * The higher level of the driver - The ioctl handler and the character
1919 * device handling functions should queue request to the lower level part
1920 * and wait for their completion using idetape_queue_pc_tail or
1921 * idetape_queue_rw_tail.
1922 */
1923 static void idetape_queue_pc_head (ide_drive_t *drive,idetape_pc_t *pc,struct request *rq)
1924 {
1925 ide_init_drive_cmd (rq);
1926 rq->buffer = (char *) pc;
1927 rq->cmd = IDETAPE_PC_RQ1;
1928 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
1929 }
1930
1931 /*
1932 * idetape_retry_pc is called when an error was detected during the
1933 * last packet command. We queue a request sense packet command in
1934 * the head of the request list.
1935 */
1936 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1937 {
1938 idetape_tape_t *tape = drive->driver_data;
1939 idetape_pc_t *pc;
1940 struct request *rq;
1941 idetape_error_reg_t error;
1942
1943 error.all = IN_BYTE (IDE_ERROR_REG);
1944 pc = idetape_next_pc_storage (drive);
1945 rq = idetape_next_rq_storage (drive);
1946 idetape_create_request_sense_cmd (pc);
1947 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
1948 idetape_queue_pc_head (drive, pc, rq);
1949 return ide_stopped;
1950 }
1951
1952 /*
1953 * idetape_postpone_request postpones the current request so that
1954 * ide.c will be able to service requests from another device on
1955 * the same hwgroup while we are polling for DSC.
1956 */
1957 static void idetape_postpone_request (ide_drive_t *drive)
1958 {
1959 idetape_tape_t *tape = drive->driver_data;
1960
1961 #if IDETAPE_DEBUG_LOG
1962 if (tape->debug_level >= 4)
1963 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1964 #endif
1965 tape->postponed_rq = HWGROUP(drive)->rq;
1966 ide_stall_queue(drive, tape->dsc_polling_frequency);
1967 }
1968
1969 /*
1970 * idetape_pc_intr is the usual interrupt handler which will be called
1971 * during a packet command. We will transfer some of the data (as
1972 * requested by the drive) and will re-point interrupt handler to us.
1973 * When data transfer is finished, we will act according to the
1974 * algorithm described before idetape_issue_packet_command.
1975 *
1976 */
1977 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1978 {
1979 idetape_tape_t *tape = drive->driver_data;
1980 idetape_status_reg_t status;
1981 idetape_bcount_reg_t bcount;
1982 idetape_ireason_reg_t ireason;
1983 idetape_pc_t *pc=tape->pc;
1984
1985 unsigned int temp;
1986 unsigned long cmd_time;
1987 #if SIMULATE_ERRORS
1988 static int error_sim_count = 0;
1989 #endif
1990
1991 #if IDETAPE_DEBUG_LOG
1992 if (tape->debug_level >= 4)
1993 printk (KERN_INFO "ide-tape: Reached idetape_pc_intr interrupt handler\n");
1994 #endif /* IDETAPE_DEBUG_LOG */
1995
1996 status.all = GET_STAT(); /* Clear the interrupt */
1997
1998 #ifdef CONFIG_BLK_DEV_IDEDMA
1999 if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
2000 if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
2001 /*
2002 * A DMA error is sometimes expected. For example,
2003 * if the tape is crossing a filemark during a
2004 * READ command, it will issue an irq and position
2005 * itself before the filemark, so that only a partial
2006 * data transfer will occur (which causes the DMA
2007 * error). In that case, we will later ask the tape
2008 * how much bytes of the original request were
2009 * actually transferred (we can't receive that
2010 * information from the DMA engine on most chipsets).
2011 */
2012 set_bit (PC_DMA_ERROR, &pc->flags);
2013 } else if (!status.b.check) {
2014 pc->actually_transferred=pc->request_transfer;
2015 idetape_update_buffers (pc);
2016 }
2017 #if IDETAPE_DEBUG_LOG
2018 if (tape->debug_level >= 4)
2019 printk (KERN_INFO "ide-tape: DMA finished\n");
2020 #endif /* IDETAPE_DEBUG_LOG */
2021 }
2022 #endif /* CONFIG_BLK_DEV_IDEDMA */
2023
2024 if (!status.b.drq) { /* No more interrupts */
2025 cmd_time = (jiffies - tape->cmd_start_time) * 1000 / HZ;
2026 tape->max_cmd_time = IDE_MAX(cmd_time, tape->max_cmd_time);
2027 #if IDETAPE_DEBUG_LOG
2028 if (tape->debug_level >= 2)
2029 printk (KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
2030 #endif /* IDETAPE_DEBUG_LOG */
2031 clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
2032
2033 ide__sti(); /* local CPU only */
2034
2035 #if SIMULATE_ERRORS
2036 if ((pc->c[0] == IDETAPE_WRITE_CMD || pc->c[0] == IDETAPE_READ_CMD) && (++error_sim_count % 100) == 0) {
2037 printk(KERN_INFO "ide-tape: %s: simulating error\n", tape->name);
2038 status.b.check = 1;
2039 }
2040 #endif
2041 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
2042 status.b.check = 0;
2043 if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
2044 #if IDETAPE_DEBUG_LOG
2045 if (tape->debug_level >= 1)
2046 printk (KERN_INFO "ide-tape: %s: I/O error, ",tape->name);
2047 #endif /* IDETAPE_DEBUG_LOG */
2048 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2049 printk (KERN_ERR "ide-tape: I/O error in request sense command\n");
2050 return ide_do_reset (drive);
2051 }
2052 #if IDETAPE_DEBUG_LOG
2053 if (tape->debug_level >= 1)
2054 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
2055 #endif
2056 return idetape_retry_pc (drive); /* Retry operation */
2057 }
2058 pc->error = 0;
2059 if (!tape->onstream && test_bit (PC_WAIT_FOR_DSC, &pc->flags) && !status.b.dsc) { /* Media access command */
2060 tape->dsc_polling_start = jiffies;
2061 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
2062 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
2063 idetape_postpone_request (drive); /* Allow ide.c to handle other requests */
2064 return ide_stopped;
2065 }
2066 if (tape->failed_pc == pc)
2067 tape->failed_pc=NULL;
2068 return pc->callback(drive); /* Command finished - Call the callback function */
2069 }
2070 #ifdef CONFIG_BLK_DEV_IDEDMA
2071 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
2072 printk (KERN_ERR "ide-tape: The tape wants to issue more interrupts in DMA mode\n");
2073 printk (KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
2074 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
2075 return ide_do_reset (drive);
2076 }
2077 #endif /* CONFIG_BLK_DEV_IDEDMA */
2078 bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
2079 bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
2080 ireason.all=IN_BYTE (IDE_IREASON_REG);
2081
2082 if (ireason.b.cod) {
2083 printk (KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
2084 return ide_do_reset (drive);
2085 }
2086 if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
2087 printk (KERN_ERR "ide-tape: We wanted to %s, ", ireason.b.io ? "Write":"Read");
2088 printk (KERN_ERR "ide-tape: but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
2089 return ide_do_reset (drive);
2090 }
2091 if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
2092 temp = pc->actually_transferred + bcount.all;
2093 if ( temp > pc->request_transfer) {
2094 if (temp > pc->buffer_size) {
2095 printk (KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
2096 idetape_discard_data (drive,bcount.all);
2097 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD,NULL);
2098 return ide_started;
2099 }
2100 #if IDETAPE_DEBUG_LOG
2101 if (tape->debug_level >= 2)
2102 printk (KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
2103 #endif /* IDETAPE_DEBUG_LOG */
2104 }
2105 }
2106 if (test_bit (PC_WRITING, &pc->flags)) {
2107 if (pc->bh != NULL)
2108 idetape_output_buffers (drive, pc, bcount.all);
2109 else
2110 atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
2111 } else {
2112 if (pc->bh != NULL)
2113 idetape_input_buffers (drive, pc, bcount.all);
2114 else
2115 atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
2116 }
2117 pc->actually_transferred+=bcount.all; /* Update the current position */
2118 pc->current_position+=bcount.all;
2119 #if IDETAPE_DEBUG_LOG
2120 if (tape->debug_level >= 2)
2121 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all);
2122 #endif
2123 ide_set_handler (drive,&idetape_pc_intr,IDETAPE_WAIT_CMD,NULL); /* And set the interrupt handler again */
2124 return ide_started;
2125 }
2126
2127 /*
2128 * Packet Command Interface
2129 *
2130 * The current Packet Command is available in tape->pc, and will not
2131 * change until we finish handling it. Each packet command is associated
2132 * with a callback function that will be called when the command is
2133 * finished.
2134 *
2135 * The handling will be done in three stages:
2136 *
2137 * 1. idetape_issue_packet_command will send the packet command to the
2138 * drive, and will set the interrupt handler to idetape_pc_intr.
2139 *
2140 * 2. On each interrupt, idetape_pc_intr will be called. This step
2141 * will be repeated until the device signals us that no more
2142 * interrupts will be issued.
2143 *
2144 * 3. ATAPI Tape media access commands have immediate status with a
2145 * delayed process. In case of a successful initiation of a
2146 * media access packet command, the DSC bit will be set when the
2147 * actual execution of the command is finished.
2148 * Since the tape drive will not issue an interrupt, we have to
2149 * poll for this event. In this case, we define the request as
2150 * "low priority request" by setting rq_status to
2151 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
2152 * the driver.
2153 *
2154 * ide.c will then give higher priority to requests which
2155 * originate from the other device, until will change rq_status
2156 * to RQ_ACTIVE.
2157 *
2158 * 4. When the packet command is finished, it will be checked for errors.
2159 *
2160 * 5. In case an error was found, we queue a request sense packet command
2161 * in front of the request queue and retry the operation up to
2162 * IDETAPE_MAX_PC_RETRIES times.
2163 *
2164 * 6. In case no error was found, or we decided to give up and not
2165 * to retry again, the callback function will be called and then
2166 * we will handle the next request.
2167 *
2168 */
2169 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2170 {
2171 idetape_tape_t *tape = drive->driver_data;
2172 idetape_pc_t *pc = tape->pc;
2173 idetape_ireason_reg_t ireason;
2174 int retries = 100;
2175 ide_startstop_t startstop;
2176
2177 if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2178 printk (KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
2179 return startstop;
2180 }
2181 ireason.all=IN_BYTE (IDE_IREASON_REG);
2182 while (retries-- && (!ireason.b.cod || ireason.b.io)) {
2183 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, retrying\n");
2184 udelay(100);
2185 ireason.all = IN_BYTE(IDE_IREASON_REG);
2186 if (retries == 0) {
2187 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, ignoring\n");
2188 ireason.b.cod = 1;
2189 ireason.b.io = 0;
2190 }
2191 }
2192 if (!ireason.b.cod || ireason.b.io) {
2193 printk (KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
2194 return ide_do_reset (drive);
2195 }
2196 tape->cmd_start_time = jiffies;
2197 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); /* Set the interrupt routine */
2198 atapi_output_bytes (drive,pc->c,12); /* Send the actual packet */
2199 return ide_started;
2200 }
2201
2202 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
2203 {
2204 idetape_tape_t *tape = drive->driver_data;
2205 idetape_bcount_reg_t bcount;
2206 int dma_ok=0;
2207
2208 #if IDETAPE_DEBUG_BUGS
2209 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2210 printk (KERN_ERR "ide-tape: possible ide-tape.c bug - Two request sense in serial were issued\n");
2211 }
2212 #endif /* IDETAPE_DEBUG_BUGS */
2213
2214 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
2215 tape->failed_pc=pc;
2216 tape->pc=pc; /* Set the current packet command */
2217
2218 if (pc->retries > IDETAPE_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
2219 /*
2220 * We will "abort" retrying a packet command in case
2221 * a legitimate error code was received (crossing a
2222 * filemark, or DMA error in the end of media, for
2223 * example).
2224 */
2225 if (!test_bit (PC_ABORT, &pc->flags)) {
2226 if (!(pc->c[0] == 0 && tape->sense_key == 2 && tape->asc == 4 && (tape->ascq == 1 || tape->ascq == 8))) {
2227 printk (KERN_ERR "ide-tape: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
2228 tape->name, pc->c[0], tape->sense_key, tape->asc, tape->ascq);
2229 if (tape->onstream && pc->c[0] == 8 && tape->sense_key == 3 && tape->asc == 0x11) /* AJN-1: 11 should be 0x11 */
2230 printk(KERN_ERR "ide-tape: %s: enabling read error recovery\n", tape->name);
2231 }
2232 pc->error = IDETAPE_ERROR_GENERAL; /* Giving up */
2233 }
2234 tape->failed_pc=NULL;
2235 return pc->callback(drive);
2236 }
2237 #if IDETAPE_DEBUG_LOG
2238 if (tape->debug_level >= 2)
2239 printk (KERN_INFO "ide-tape: Retry number - %d\n",pc->retries);
2240 #endif /* IDETAPE_DEBUG_LOG */
2241
2242 pc->retries++;
2243 pc->actually_transferred=0; /* We haven't transferred any data yet */
2244 pc->current_position=pc->buffer;
2245 bcount.all=pc->request_transfer; /* Request to transfer the entire buffer at once */
2246
2247 #ifdef CONFIG_BLK_DEV_IDEDMA
2248 if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
2249 printk (KERN_WARNING "ide-tape: DMA disabled, reverting to PIO\n");
2250 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
2251 }
2252 if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
2253 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
2254 #endif /* CONFIG_BLK_DEV_IDEDMA */
2255
2256 if (IDE_CONTROL_REG)
2257 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
2258 OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG); /* Use PIO/DMA */
2259 OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
2260 OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
2261 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
2262 #ifdef CONFIG_BLK_DEV_IDEDMA
2263 if (dma_ok) { /* Begin DMA, if necessary */
2264 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
2265 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
2266 }
2267 #endif /* CONFIG_BLK_DEV_IDEDMA */
2268 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
2269 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
2270 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
2271 return ide_started;
2272 } else {
2273 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
2274 return idetape_transfer_pc(drive);
2275 }
2276 }
2277
2278 /*
2279 * General packet command callback function.
2280 */
2281 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2282 {
2283 idetape_tape_t *tape = drive->driver_data;
2284
2285 #if IDETAPE_DEBUG_LOG
2286 if (tape->debug_level >= 4)
2287 printk (KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2288 #endif /* IDETAPE_DEBUG_LOG */
2289
2290 idetape_end_request (tape->pc->error ? 0:1, HWGROUP(drive));
2291 return ide_stopped;
2292 }
2293
2294 /*
2295 * A mode sense command is used to "sense" tape parameters.
2296 */
2297 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, byte page_code)
2298 {
2299 idetape_init_pc (pc);
2300 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2301 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors for now */
2302 pc->c[2] = page_code;
2303 pc->c[3] = 255; /* Don't limit the returned information */
2304 pc->c[4] = 255; /* (We will just discard data in that case) */
2305 if (page_code == IDETAPE_CAPABILITIES_PAGE)
2306 pc->request_transfer = 24;
2307 else
2308 pc->request_transfer = 50;
2309 pc->callback = &idetape_pc_callback;
2310 }
2311
2312 static ide_startstop_t idetape_onstream_buffer_fill_callback (ide_drive_t *drive)
2313 {
2314 idetape_tape_t *tape = drive->driver_data;
2315
2316 tape->max_frames = tape->pc->buffer[4 + 2];
2317 tape->cur_frames = tape->pc->buffer[4 + 3];
2318 if (tape->chrdev_direction == idetape_direction_write)
2319 tape->tape_head = tape->buffer_head - tape->cur_frames;
2320 else
2321 tape->tape_head = tape->buffer_head + tape->cur_frames;
2322 if (tape->tape_head != tape->last_tape_head) {
2323 tape->last_tape_head = tape->tape_head;
2324 tape->tape_still_time_begin = jiffies;
2325 if (tape->tape_still_time > 200)
2326 tape->measure_insert_time = 1;
2327 }
2328 tape->tape_still_time = (jiffies - tape->tape_still_time_begin) * 1000 / HZ;
2329 #if USE_IOTRACE
2330 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2331 #endif
2332 #if IDETAPE_DEBUG_LOG
2333 if (tape->debug_level >= 1)
2334 printk(KERN_INFO "ide-tape: buffer fill callback, %d/%d\n", tape->cur_frames, tape->max_frames);
2335 #endif
2336 idetape_end_request (tape->pc->error ? 0:1, HWGROUP(drive));
2337 return ide_stopped;
2338 }
2339
2340 static void idetape_queue_onstream_buffer_fill (ide_drive_t *drive)
2341 {
2342 idetape_pc_t *pc;
2343 struct request *rq;
2344
2345 pc = idetape_next_pc_storage (drive);
2346 rq = idetape_next_rq_storage (drive);
2347 idetape_create_mode_sense_cmd (pc, 0x33);
2348 pc->callback = idetape_onstream_buffer_fill_callback;
2349 idetape_queue_pc_head (drive, pc, rq);
2350 }
2351
2352 static void calculate_speeds(ide_drive_t *drive)
2353 {
2354 idetape_tape_t *tape = drive->driver_data;
2355 int full = 125, empty = 75;
2356
2357 if (jiffies > tape->controlled_pipeline_head_time + 120 * HZ) {
2358 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
2359 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
2360 tape->controlled_last_pipeline_head = tape->pipeline_head;
2361 tape->controlled_pipeline_head_time = jiffies;
2362 }
2363 if (jiffies > tape->controlled_pipeline_head_time + 60 * HZ)
2364 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
2365 else if (jiffies > tape->controlled_previous_head_time)
2366 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
2367
2368 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) { /* -1 for read mode error recovery */
2369 if (jiffies > tape->uncontrolled_previous_head_time + 10 * HZ) {
2370 tape->uncontrolled_pipeline_head_time = jiffies;
2371 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
2372 }
2373 } else {
2374 tape->uncontrolled_previous_head_time = jiffies;
2375 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
2376 if (jiffies > tape->uncontrolled_pipeline_head_time + 30 * HZ) {
2377 tape->uncontrolled_pipeline_head_time = jiffies;
2378 }
2379 }
2380 tape->pipeline_head_speed = IDE_MAX(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
2381 if (tape->speed_control == 0) {
2382 tape->max_insert_speed = 5000;
2383 } else if (tape->speed_control == 1) {
2384 if (tape->nr_pending_stages >= tape->max_stages / 2)
2385 tape->max_insert_speed = tape->pipeline_head_speed +
2386 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
2387 else
2388 tape->max_insert_speed = 500 +
2389 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
2390 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
2391 tape->max_insert_speed = 5000;
2392 } else if (tape->speed_control == 2) {
2393 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
2394 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
2395 } else
2396 tape->max_insert_speed = tape->speed_control;
2397 tape->max_insert_speed = IDE_MAX(tape->max_insert_speed, 500);
2398 }
2399
2400 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2401 {
2402 idetape_tape_t *tape = drive->driver_data;
2403 idetape_pc_t *pc = tape->pc;
2404 idetape_status_reg_t status;
2405
2406 if (tape->onstream)
2407 printk(KERN_INFO "ide-tape: bug: onstream, media_access_finished\n");
2408 status.all = GET_STAT();
2409 if (status.b.dsc) {
2410 if (status.b.check) { /* Error detected */
2411 printk (KERN_ERR "ide-tape: %s: I/O error, ",tape->name);
2412 return idetape_retry_pc (drive); /* Retry operation */
2413 }
2414 pc->error = 0;
2415 if (tape->failed_pc == pc)
2416 tape->failed_pc = NULL;
2417 } else {
2418 pc->error = IDETAPE_ERROR_GENERAL;
2419 tape->failed_pc = NULL;
2420 }
2421 return pc->callback (drive);
2422 }
2423
2424 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2425 {
2426 idetape_tape_t *tape = drive->driver_data;
2427 struct request *rq = HWGROUP(drive)->rq;
2428 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2429
2430 tape->avg_size += blocks * tape->tape_block_size;
2431 tape->insert_size += blocks * tape->tape_block_size;
2432 if (tape->insert_size > 1024 * 1024)
2433 tape->measure_insert_time = 1;
2434 if (tape->measure_insert_time) {
2435 tape->measure_insert_time = 0;
2436 tape->insert_time = jiffies;
2437 tape->insert_size = 0;
2438 }
2439 if (jiffies > tape->insert_time)
2440 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2441 if (jiffies - tape->avg_time >= HZ) {
2442 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
2443 tape->avg_size = 0;
2444 tape->avg_time = jiffies;
2445 }
2446
2447 #if IDETAPE_DEBUG_LOG
2448 if (tape->debug_level >= 4)
2449 printk (KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2450 #endif /* IDETAPE_DEBUG_LOG */
2451
2452 tape->first_frame_position += blocks;
2453 rq->current_nr_sectors -= blocks;
2454
2455 if (!tape->pc->error)
2456 idetape_end_request (1, HWGROUP (drive));
2457 else
2458 idetape_end_request (tape->pc->error, HWGROUP (drive));
2459 return ide_stopped;
2460 }
2461
2462 static void idetape_create_read_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2463 {
2464 struct buffer_head *p = bh;
2465 idetape_init_pc (pc);
2466 pc->c[0] = IDETAPE_READ_CMD;
2467 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2468 pc->c[1] = 1;
2469 pc->callback = &idetape_rw_callback;
2470 pc->bh = bh;
2471 atomic_set(&bh->b_count, 0);
2472 pc->buffer = NULL;
2473 if (tape->onstream) {
2474 while (p) {
2475 atomic_set(&p->b_count, 0);
2476 p = p->b_reqnext;
2477 }
2478 }
2479 if (!tape->onstream) {
2480 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2481 if (pc->request_transfer == tape->stage_size)
2482 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2483 } else {
2484 if (length) {
2485 pc->request_transfer = pc->buffer_size = 32768 + 512;
2486 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2487 } else
2488 pc->request_transfer = 0;
2489 }
2490 }
2491
2492 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2493 {
2494 int size = 32768;
2495
2496 struct buffer_head *p = bh;
2497 idetape_init_pc (pc);
2498 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
2499 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
2500 pc->c[7] = size >> 8;
2501 pc->c[8] = size & 0xff;
2502 pc->callback = &idetape_pc_callback;
2503 pc->bh = bh;
2504 atomic_set(&bh->b_count, 0);
2505 pc->buffer = NULL;
2506 while (p) {
2507 atomic_set(&p->b_count, 0);
2508 p = p->b_reqnext;
2509 }
2510 pc->request_transfer = pc->buffer_size = size;
2511 }
2512
2513 static void idetape_create_write_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
2514 {
2515 struct buffer_head *p = bh;
2516 idetape_init_pc (pc);
2517 pc->c[0] = IDETAPE_WRITE_CMD;
2518 put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
2519 pc->c[1] = 1;
2520 pc->callback = &idetape_rw_callback;
2521 set_bit (PC_WRITING, &pc->flags);
2522 if (tape->onstream) {
2523 while (p) {
2524 atomic_set(&p->b_count, p->b_size);
2525 p = p->b_reqnext;
2526 }
2527 }
2528 pc->bh = bh;
2529 pc->b_data = bh->b_data;
2530 pc->b_count = atomic_read(&bh->b_count);
2531 pc->buffer = NULL;
2532 if (!tape->onstream) {
2533 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2534 if (pc->request_transfer == tape->stage_size)
2535 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2536 } else {
2537 if (length) {
2538 pc->request_transfer = pc->buffer_size = 32768 + 512;
2539 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
2540 } else
2541 pc->request_transfer = 0;
2542 }
2543 }
2544
2545 /*
2546 * idetape_do_request is our request handling function.
2547 */
2548 static ide_startstop_t idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2549 {
2550 idetape_tape_t *tape = drive->driver_data;
2551 idetape_pc_t *pc;
2552 struct request *postponed_rq = tape->postponed_rq;
2553 idetape_status_reg_t status;
2554
2555 #if IDETAPE_DEBUG_LOG
2556 if (tape->debug_level >= 5)
2557 printk (KERN_INFO "ide-tape: rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
2558 if (tape->debug_level >= 2)
2559 printk (KERN_INFO "ide-tape: sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2560 #endif /* IDETAPE_DEBUG_LOG */
2561
2562 if (!IDETAPE_RQ_CMD (rq->cmd)) {
2563 /*
2564 * We do not support buffer cache originated requests.
2565 */
2566 printk (KERN_NOTICE "ide-tape: %s: Unsupported command in request queue (%d)\n", drive->name, rq->cmd);
2567 ide_end_request (0,HWGROUP (drive)); /* Let the common code handle it */
2568 return ide_stopped;
2569 }
2570
2571 /*
2572 * Retry a failed packet command
2573 */
2574 if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2575 return idetape_issue_packet_command (drive, tape->failed_pc);
2576 }
2577 #if IDETAPE_DEBUG_BUGS
2578 if (postponed_rq != NULL)
2579 if (rq != postponed_rq) {
2580 printk (KERN_ERR "ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
2581 idetape_end_request (0,HWGROUP (drive));
2582 return ide_stopped;
2583 }
2584 #endif /* IDETAPE_DEBUG_BUGS */
2585
2586 tape->postponed_rq = NULL;
2587
2588 /*
2589 * If the tape is still busy, postpone our request and service
2590 * the other device meanwhile.
2591 */
2592 status.all = GET_STAT();
2593
2594 /*
2595 * The OnStream tape drive doesn't support DSC. Assume
2596 * that DSC is always set.
2597 */
2598 if (tape->onstream)
2599 status.b.dsc = 1;
2600 if (!drive->dsc_overlap && rq->cmd != IDETAPE_PC_RQ2)
2601 set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
2602
2603 /*
2604 * For the OnStream tape, check the current status of the tape
2605 * internal buffer using data gathered from the buffer fill
2606 * mode page, and postpone our request, effectively "disconnecting"
2607 * from the IDE bus, in case the buffer is full (writing) or
2608 * empty (reading), and there is a danger that our request will
2609 * hold the IDE bus during actual media access.
2610 */
2611 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2612 tape->measure_insert_time = 1;
2613 if (tape->req_buffer_fill && (rq->cmd == IDETAPE_WRITE_RQ || rq->cmd == IDETAPE_READ_RQ)) {
2614 tape->req_buffer_fill = 0;
2615 tape->writes_since_buffer_fill = 0;
2616 tape->reads_since_buffer_fill = 0;
2617 tape->last_buffer_fill = jiffies;
2618 idetape_queue_onstream_buffer_fill(drive);
2619 if (jiffies > tape->insert_time)
2620 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2621 return ide_stopped;
2622 }
2623 if (jiffies > tape->insert_time)
2624 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2625 calculate_speeds(drive);
2626 if (tape->onstream && tape->max_frames &&
2627 ((rq->cmd == IDETAPE_WRITE_RQ && (tape->cur_frames == tape->max_frames || (tape->speed_control && tape->cur_frames > 5 && (tape->insert_speed > tape->max_insert_speed || (0 /* tape->cur_frames > 30 && tape->tape_still_time > 200 */))))) ||
2628 (rq->cmd == IDETAPE_READ_RQ && (tape->cur_frames == 0 || (tape->speed_control && (tape->cur_frames < tape->max_frames - 5) && tape->insert_speed > tape->max_insert_speed)) && rq->nr_sectors))) {
2629 #if IDETAPE_DEBUG_LOG
2630 if (tape->debug_level >= 4)
2631 printk(KERN_INFO "ide-tape: postponing request, cmd %d, cur %d, max %d\n",
2632 rq->cmd, tape->cur_frames, tape->max_frames);
2633 #endif
2634 if (tape->postpone_cnt++ < 500) {
2635 status.b.dsc = 0;
2636 tape->req_buffer_fill = 1;
2637 }
2638 #if ONSTREAM_DEBUG
2639 else if (tape->debug_level >= 4)
2640 printk(KERN_INFO "ide-tape: %s: postpone_cnt %d\n", tape->name, tape->postpone_cnt);
2641 #endif
2642 }
2643 if (!test_and_clear_bit (IDETAPE_IGNORE_DSC, &tape->flags) && !status.b.dsc) {
2644 if (postponed_rq == NULL) {
2645 tape->dsc_polling_start = jiffies;
2646 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2647 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2648 } else if ((signed long) (jiffies - tape->dsc_timeout) > 0) {
2649 printk (KERN_ERR "ide-tape: %s: DSC timeout\n", tape->name);
2650 if (rq->cmd == IDETAPE_PC_RQ2) {
2651 idetape_media_access_finished (drive);
2652 return ide_stopped;
2653 } else {
2654 return ide_do_reset (drive);
2655 }
2656 } else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
2657 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2658 idetape_postpone_request (drive);
2659 return ide_stopped;
2660 }
2661 switch (rq->cmd) {
2662 case IDETAPE_READ_RQ:
2663 tape->buffer_head++;
2664 #if USE_IOTRACE
2665 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2666 #endif
2667 tape->postpone_cnt = 0;
2668 tape->reads_since_buffer_fill++;
2669 if (tape->onstream) {
2670 if (tape->cur_frames - tape->reads_since_buffer_fill <= 0)
2671 tape->req_buffer_fill = 1;
2672 if (jiffies > tape->last_buffer_fill + 5 * HZ / 100)
2673 tape->req_buffer_fill = 1;
2674 }
2675 pc=idetape_next_pc_storage (drive);
2676 idetape_create_read_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2677 break;
2678 case IDETAPE_WRITE_RQ:
2679 tape->buffer_head++;
2680 #if USE_IOTRACE
2681 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2682 #endif
2683 tape->postpone_cnt = 0;
2684 tape->writes_since_buffer_fill++;
2685 if (tape->onstream) {
2686 if (tape->cur_frames + tape->writes_since_buffer_fill >= tape->max_frames)
2687 tape->req_buffer_fill = 1;
2688 if (jiffies > tape->last_buffer_fill + 5 * HZ / 100)
2689 tape->req_buffer_fill = 1;
2690 calculate_speeds(drive);
2691 }
2692 pc=idetape_next_pc_storage (drive);
2693 idetape_create_write_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2694 break;
2695 case IDETAPE_READ_BUFFER_RQ:
2696 tape->postpone_cnt = 0;
2697 pc=idetape_next_pc_storage (drive);
2698 idetape_create_read_buffer_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
2699 break;
2700 case IDETAPE_ABORTED_WRITE_RQ:
2701 rq->cmd = IDETAPE_WRITE_RQ;
2702 idetape_end_request (IDETAPE_ERROR_EOD, HWGROUP(drive));
2703 return ide_stopped;
2704 case IDETAPE_ABORTED_READ_RQ:
2705 #if IDETAPE_DEBUG_LOG
2706 if (tape->debug_level >= 2)
2707 printk(KERN_INFO "ide-tape: %s: detected aborted read rq\n", tape->name);
2708 #endif
2709 rq->cmd = IDETAPE_READ_RQ;
2710 idetape_end_request (IDETAPE_ERROR_EOD, HWGROUP(drive));
2711 return ide_stopped;
2712 case IDETAPE_PC_RQ1:
2713 pc=(idetape_pc_t *) rq->buffer;
2714 rq->cmd = IDETAPE_PC_RQ2;
2715 break;
2716 case IDETAPE_PC_RQ2:
2717 idetape_media_access_finished (drive);
2718 return ide_stopped;
2719 default:
2720 printk (KERN_ERR "ide-tape: bug in IDETAPE_RQ_CMD macro\n");
2721 idetape_end_request (0,HWGROUP (drive));
2722 return ide_stopped;
2723 }
2724 return idetape_issue_packet_command (drive, pc);
2725 }
2726
2727 /*
2728 * Pipeline related functions
2729 */
2730 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2731 {
2732 int rc1, rc2;
2733
2734 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2735 rc2 = (tape->active_data_request != NULL);
2736 return rc1;
2737 }
2738
2739 /*
2740 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2741 * stage, along with all the necessary small buffers which together make
2742 * a buffer of size tape->stage_size (or a bit more). We attempt to
2743 * combine sequential pages as much as possible.
2744 *
2745 * Returns a pointer to the new allocated stage, or NULL if we
2746 * can't (or don't want to) allocate a stage.
2747 *
2748 * Pipeline stages are optional and are used to increase performance.
2749 * If we can't allocate them, we'll manage without them.
2750 */
2751 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2752 {
2753 idetape_stage_t *stage;
2754 struct buffer_head *prev_bh, *bh;
2755 int pages = tape->pages_per_stage;
2756 char *b_data;
2757
2758 if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2759 return NULL;
2760 stage->next = NULL;
2761
2762 bh = stage->bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL);
2763 if (bh == NULL)
2764 goto abort;
2765 bh->b_reqnext = NULL;
2766 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2767 goto abort;
2768 if (clear)
2769 memset(bh->b_data, 0, PAGE_SIZE);
2770 bh->b_size = PAGE_SIZE;
2771 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2772 set_bit (BH_Lock, &bh->b_state);
2773
2774 while (--pages) {
2775 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2776 goto abort;
2777 if (clear)
2778 memset(b_data, 0, PAGE_SIZE);
2779 if (bh->b_data == b_data + PAGE_SIZE) {
2780 bh->b_size += PAGE_SIZE;
2781 bh->b_data -= PAGE_SIZE;
2782 if (full)
2783 atomic_add(PAGE_SIZE, &bh->b_count);
2784 continue;
2785 }
2786 if (b_data == bh->b_data + bh->b_size) {
2787 bh->b_size += PAGE_SIZE;
2788 if (full)
2789 atomic_add(PAGE_SIZE, &bh->b_count);
2790 continue;
2791 }
2792 prev_bh = bh;
2793 if ((bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL)) == NULL) {
2794 free_page ((unsigned long) b_data);
2795 goto abort;
2796 }
2797 bh->b_reqnext = NULL;
2798 bh->b_data = b_data;
2799 bh->b_size = PAGE_SIZE;
2800 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2801 set_bit (BH_Lock, &bh->b_state);
2802 prev_bh->b_reqnext = bh;
2803 }
2804 bh->b_size -= tape->excess_bh_size;
2805 if (full)
2806 atomic_sub(tape->excess_bh_size, &bh->b_count);
2807 if (tape->onstream)
2808 stage->aux = (os_aux_t *) (bh->b_data + bh->b_size - OS_AUX_SIZE);
2809 return stage;
2810 abort:
2811 __idetape_kfree_stage (stage);
2812 return NULL;
2813 }
2814
2815 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2816 {
2817 idetape_stage_t *cache_stage = tape->cache_stage;
2818
2819 #if IDETAPE_DEBUG_LOG
2820 if (tape->debug_level >= 4)
2821 printk (KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2822 #endif /* IDETAPE_DEBUG_LOG */
2823
2824 if (tape->nr_stages >= tape->max_stages)
2825 return NULL;
2826 if (cache_stage != NULL) {
2827 tape->cache_stage = NULL;
2828 return cache_stage;
2829 }
2830 return __idetape_kmalloc_stage (tape, 0, 0);
2831 }
2832
2833 static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char *buf, int n)
2834 {
2835 struct buffer_head *bh = tape->bh;
2836 int count;
2837
2838 while (n) {
2839 #if IDETAPE_DEBUG_BUGS
2840 if (bh == NULL) {
2841 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_from_user\n");
2842 return;
2843 }
2844 #endif /* IDETAPE_DEBUG_BUGS */
2845 count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), n);
2846 copy_from_user (bh->b_data + atomic_read(&bh->b_count), buf, count);
2847 n -= count; atomic_add(count, &bh->b_count); buf += count;
2848 if (atomic_read(&bh->b_count) == bh->b_size) {
2849 bh = bh->b_reqnext;
2850 if (bh)
2851 atomic_set(&bh->b_count, 0);
2852 }
2853 }
2854 tape->bh = bh;
2855 }
2856
2857 static void idetape_copy_stage_to_user (idetape_tape_t *tape, char *buf, idetape_stage_t *stage, int n)
2858 {
2859 struct buffer_head *bh = tape->bh;
2860 int count;
2861
2862 while (n) {
2863 #if IDETAPE_DEBUG_BUGS
2864 if (bh == NULL) {
2865 printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_to_user\n");
2866 return;
2867 }
2868 #endif /* IDETAPE_DEBUG_BUGS */
2869 count = IDE_MIN (tape->b_count, n);
2870 copy_to_user (buf, tape->b_data, count);
2871 n -= count; tape->b_data += count; tape->b_count -= count; buf += count;
2872 if (!tape->b_count) {
2873 tape->bh = bh = bh->b_reqnext;
2874 if (bh) {
2875 tape->b_data = bh->b_data;
2876 tape->b_count = atomic_read(&bh->b_count);
2877 }
2878 }
2879 }
2880 }
2881
2882 static void idetape_init_merge_stage (idetape_tape_t *tape)
2883 {
2884 struct buffer_head *bh = tape->merge_stage->bh;
2885
2886 tape->bh = bh;
2887 if (tape->chrdev_direction == idetape_direction_write)
2888 atomic_set(&bh->b_count, 0);
2889 else {
2890 tape->b_data = bh->b_data;
2891 tape->b_count = atomic_read(&bh->b_count);
2892 }
2893 }
2894
2895 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2896 {
2897 struct buffer_head *tmp;
2898 os_aux_t *tmp_aux;
2899
2900 tmp = stage->bh; tmp_aux = stage->aux;
2901 stage->bh = tape->merge_stage->bh; stage->aux = tape->merge_stage->aux;
2902 tape->merge_stage->bh = tmp; tape->merge_stage->aux = tmp_aux;
2903 idetape_init_merge_stage (tape);
2904 }
2905
2906 /*
2907 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2908 */
2909 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2910 {
2911 idetape_tape_t *tape = drive->driver_data;
2912 unsigned long flags;
2913
2914 #if IDETAPE_DEBUG_LOG
2915 if (tape->debug_level >= 4)
2916 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2917 #endif /* IDETAPE_DEBUG_LOG */
2918 spin_lock_irqsave(&tape->spinlock, flags);
2919 stage->next=NULL;
2920 if (tape->last_stage != NULL)
2921 tape->last_stage->next=stage;
2922 else
2923 tape->first_stage=tape->next_stage=stage;
2924 tape->last_stage=stage;
2925 if (tape->next_stage == NULL)
2926 tape->next_stage=tape->last_stage;
2927 tape->nr_stages++;
2928 tape->nr_pending_stages++;
2929 spin_unlock_irqrestore(&tape->spinlock, flags);
2930 }
2931
2932 /*
2933 * Initialize the OnStream AUX
2934 */
2935 static void idetape_init_stage (ide_drive_t *drive, idetape_stage_t *stage, int frame_type, int logical_blk_num)
2936 {
2937 idetape_tape_t *tape = drive->driver_data;
2938 os_aux_t *aux = stage->aux;
2939 os_partition_t *par = &aux->partition;
2940 os_dat_t *dat = &aux->dat;
2941
2942 if (!tape->onstream || tape->raw)
2943 return;
2944 memset(aux, 0, sizeof(*aux));
2945 aux->format_id = htonl(0);
2946 memcpy(aux->application_sig, "LIN3", 4);
2947 aux->hdwr = htonl(0);
2948 aux->frame_type = frame_type;
2949
2950 if (frame_type == OS_FRAME_TYPE_HEADER) {
2951 aux->update_frame_cntr = htonl(tape->update_frame_cntr);
2952 par->partition_num = OS_CONFIG_PARTITION;
2953 par->par_desc_ver = OS_PARTITION_VERSION;
2954 par->wrt_pass_cntr = htons(0xffff);
2955 par->first_frame_addr = htonl(0);
2956 par->last_frame_addr = htonl(0xbb7);
2957 } else {
2958 aux->update_frame_cntr = htonl(0);
2959 par->partition_num = OS_DATA_PARTITION;
2960 par->par_desc_ver = OS_PARTITION_VERSION;
2961 par->wrt_pass_cntr = htons(tape->wrt_pass_cntr);
2962 par->first_frame_addr = htonl(0x14);
2963 par->last_frame_addr = htonl(19239 * 24);
2964 }
2965 if (frame_type != OS_FRAME_TYPE_HEADER) {
2966 aux->frame_seq_num = htonl(logical_blk_num);
2967 aux->logical_blk_num_high = htonl(0);
2968 aux->logical_blk_num = htonl(logical_blk_num);
2969 } else {
2970 aux->frame_seq_num = htonl(0);
2971 aux->logical_blk_num_high = htonl(0);
2972 aux->logical_blk_num = htonl(0);
2973 }
2974
2975 if (frame_type != OS_FRAME_TYPE_HEADER) {
2976 dat->dat_sz = 8;
2977 dat->reserved1 = 0;
2978 dat->entry_cnt = 1;
2979 dat->reserved3 = 0;
2980 if (frame_type == OS_FRAME_TYPE_DATA)
2981 dat->dat_list[0].blk_sz = htonl(32 * 1024);
2982 else
2983 dat->dat_list[0].blk_sz = 0;
2984 dat->dat_list[0].blk_cnt = htons(1);
2985 if (frame_type == OS_FRAME_TYPE_MARKER)
2986 dat->dat_list[0].flags = OS_DAT_FLAGS_MARK;
2987 else
2988 dat->dat_list[0].flags = OS_DAT_FLAGS_DATA;
2989 dat->dat_list[0].reserved = 0;
2990 } else
2991 aux->next_mark_addr = htonl(tape->first_mark_addr);
2992 aux->filemark_cnt = ntohl(tape->filemark_cnt);
2993 aux->phys_fm = ntohl(0xffffffff);
2994 aux->last_mark_addr = ntohl(tape->last_mark_addr);
2995 }
2996
2997 /*
2998 * idetape_wait_for_request installs a semaphore in a pending request
2999 * and sleeps until it is serviced.
3000 *
3001 * The caller should ensure that the request will not be serviced
3002 * before we install the semaphore (usually by disabling interrupts).
3003 */
3004 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
3005 {
3006 DECLARE_MUTEX_LOCKED(sem);
3007 idetape_tape_t *tape = drive->driver_data;
3008
3009 #if IDETAPE_DEBUG_BUGS
3010 if (rq == NULL || !IDETAPE_RQ_CMD (rq->cmd)) {
3011 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
3012 return;
3013 }
3014 #endif /* IDETAPE_DEBUG_BUGS */
3015 rq->sem = &sem;
3016 tape->sem = &sem;
3017 spin_unlock(&tape->spinlock);
3018 down(&sem);
3019 rq->sem = NULL;
3020 tape->sem = NULL;
3021 spin_lock_irq(&tape->spinlock);
3022 }
3023
3024 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
3025 {
3026 idetape_tape_t *tape = drive->driver_data;
3027 idetape_read_position_result_t *result;
3028
3029 #if IDETAPE_DEBUG_LOG
3030 if (tape->debug_level >= 4)
3031 printk (KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
3032 #endif /* IDETAPE_DEBUG_LOG */
3033
3034 if (!tape->pc->error) {
3035 result = (idetape_read_position_result_t *) tape->pc->buffer;
3036 #if IDETAPE_DEBUG_LOG
3037 if (tape->debug_level >= 2)
3038 printk (KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
3039 if (tape->debug_level >= 2)
3040 printk (KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
3041 #endif /* IDETAPE_DEBUG_LOG */
3042 if (result->bpu) {
3043 printk (KERN_INFO "ide-tape: Block location is unknown to the tape\n");
3044 clear_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
3045 idetape_end_request (0,HWGROUP (drive));
3046 } else {
3047 #if IDETAPE_DEBUG_LOG
3048 if (tape->debug_level >= 2)
3049 printk (KERN_INFO "ide-tape: Block Location - %u\n", ntohl (result->first_block));
3050 #endif /* IDETAPE_DEBUG_LOG */
3051 tape->partition = result->partition;
3052 tape->first_frame_position = ntohl (result->first_block);
3053 tape->last_frame_position = ntohl (result->last_block);
3054 tape->blocks_in_buffer = result->blocks_in_buffer[2];
3055 set_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
3056 idetape_end_request (1,HWGROUP (drive));
3057 }
3058 } else {
3059 idetape_end_request (0,HWGROUP (drive));
3060 }
3061 return ide_stopped;
3062 }
3063
3064 /*
3065 * idetape_create_write_filemark_cmd will:
3066 *
3067 * 1. Write a filemark if write_filemark=1.
3068 * 2. Flush the device buffers without writing a filemark
3069 * if write_filemark=0.
3070 *
3071 */
3072 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
3073 {
3074 idetape_tape_t *tape = drive->driver_data;
3075
3076 idetape_init_pc (pc);
3077 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
3078 if (tape->onstream)
3079 pc->c[1] = 1;
3080 pc->c[4] = write_filemark;
3081 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3082 pc->callback = &idetape_pc_callback;
3083 }
3084
3085 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
3086 {
3087 idetape_init_pc(pc);
3088 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
3089 pc->callback = &idetape_pc_callback;
3090 }
3091
3092 /*
3093 * idetape_queue_pc_tail is based on the following functions:
3094 *
3095 * ide_do_drive_cmd from ide.c
3096 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
3097 *
3098 * We add a special packet command request to the tail of the request queue,
3099 * and wait for it to be serviced.
3100 *
3101 * This is not to be called from within the request handling part
3102 * of the driver ! We allocate here data in the stack, and it is valid
3103 * until the request is finished. This is not the case for the bottom
3104 * part of the driver, where we are always leaving the functions to wait
3105 * for an interrupt or a timer event.
3106 *
3107 * From the bottom part of the driver, we should allocate safe memory
3108 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
3109 * the request to the request list without waiting for it to be serviced !
3110 * In that case, we usually use idetape_queue_pc_head.
3111 */
3112 static int __idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
3113 {
3114 struct request rq;
3115
3116 ide_init_drive_cmd (&rq);
3117 rq.buffer = (char *) pc;
3118 rq.cmd = IDETAPE_PC_RQ1;
3119 return ide_do_drive_cmd (drive, &rq, ide_wait);
3120 }
3121
3122 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
3123 {
3124 idetape_tape_t *tape = drive->driver_data;
3125
3126 idetape_init_pc (pc);
3127 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
3128 pc->c[4] = cmd;
3129 if (tape->onstream) {
3130 pc->c[1] = 1;
3131 if (cmd == !IDETAPE_LU_LOAD_MASK)
3132 pc->c[4] = 4;
3133 }
3134 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3135 pc->callback = &idetape_pc_callback;
3136 }
3137
3138 static int idetape_wait_ready (ide_drive_t *drive, unsigned long long timeout)
3139 {
3140 idetape_tape_t *tape = drive->driver_data;
3141 idetape_pc_t pc;
3142
3143 /*
3144 * Wait for the tape to become ready
3145 */
3146 timeout += jiffies;
3147 while (jiffies < timeout) {
3148 idetape_create_test_unit_ready_cmd(&pc);
3149 if (!__idetape_queue_pc_tail(drive, &pc))
3150 return 0;
3151 if (tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) {
3152 idetape_create_load_unload_cmd (drive, &pc, IDETAPE_LU_LOAD_MASK);
3153 __idetape_queue_pc_tail(drive,&pc);
3154 idetape_create_test_unit_ready_cmd(&pc);
3155 if (!__idetape_queue_pc_tail(drive, &pc))
3156 return 0;
3157 }
3158 if (!(tape->sense_key == 2 && tape->asc == 4 && (tape->ascq == 1 || tape->ascq == 8)))
3159 break;
3160 current->state = TASK_INTERRUPTIBLE;
3161 schedule_timeout(HZ / 10);
3162 }
3163 return -EIO;
3164 }
3165
3166 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
3167 {
3168 idetape_tape_t *tape = drive->driver_data;
3169 int rc;
3170
3171 rc = __idetape_queue_pc_tail(drive, pc);
3172 if (rc) return rc;
3173 if (tape->onstream && test_bit(PC_WAIT_FOR_DSC, &pc->flags))
3174 rc = idetape_wait_ready(drive, 60 * 10 * HZ); /* AJN-4: Changed from 5 to 10 minutes;
3175 because retension takes approx. 8:20 with Onstream 30GB tape */
3176 return rc;
3177 }
3178
3179 static int idetape_flush_tape_buffers (ide_drive_t *drive)
3180 {
3181 idetape_pc_t pc;
3182 int rc;
3183
3184 idetape_create_write_filemark_cmd(drive, &pc, 0);
3185 if ((rc = idetape_queue_pc_tail (drive,&pc)))
3186 return rc;
3187 idetape_wait_ready(drive, 60 * 5 * HZ);
3188 return 0;
3189 }
3190
3191 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
3192 {
3193 idetape_init_pc (pc);
3194 pc->c[0] = IDETAPE_READ_POSITION_CMD;
3195 pc->request_transfer = 20;
3196 pc->callback = &idetape_read_position_callback;
3197 }
3198
3199 static int idetape_read_position (ide_drive_t *drive)
3200 {
3201 idetape_tape_t *tape = drive->driver_data;
3202 idetape_pc_t pc;
3203 int position;
3204
3205 #ifdef NO_LONGER_REQUIRED
3206 idetape_flush_tape_buffers(drive);
3207 #endif
3208 idetape_create_read_position_cmd(&pc);
3209 if (idetape_queue_pc_tail (drive,&pc))
3210 return -1;
3211 position = tape->first_frame_position;
3212 #ifdef NO_LONGER_REQUIRED
3213 if (tape->onstream) {
3214 if ((position != tape->last_frame_position - tape->blocks_in_buffer) &&
3215 (position != tape->last_frame_position + tape->blocks_in_buffer)) {
3216 if (tape->blocks_in_buffer == 0) {
3217 printk("ide-tape: %s: correcting read position %d, %d, %d\n", tape->name, position, tape->last_frame_position, tape->blocks_in_buffer);
3218 position = tape->last_frame_position;
3219 tape->first_frame_position = position;
3220 }
3221 }
3222 }
3223 #endif
3224 return position;
3225 }
3226
3227 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, byte partition, int skip)
3228 {
3229 idetape_tape_t *tape = drive->driver_data;
3230
3231 idetape_init_pc (pc);
3232 pc->c[0] = IDETAPE_LOCATE_CMD;
3233 if (tape->onstream)
3234 pc->c[1] = 1;
3235 else
3236 pc->c[1] = 2;
3237 put_unaligned (htonl (block), (unsigned int *) &pc->c[3]);
3238 pc->c[8] = partition;
3239 if (tape->onstream)
3240 pc->c[9] = skip << 7;
3241 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3242 pc->callback = &idetape_pc_callback;
3243 }
3244
3245 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
3246 {
3247 idetape_tape_t *tape = drive->driver_data;
3248
3249 if (!tape->capabilities.lock)
3250 return 0;
3251
3252 idetape_init_pc(pc);
3253 pc->c[0] = IDETAPE_PREVENT_CMD;
3254 pc->c[4] = prevent;
3255 pc->callback = &idetape_pc_callback;
3256 return 1;
3257 }
3258
3259 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
3260 {
3261 idetape_tape_t *tape = drive->driver_data;
3262 unsigned long flags;
3263 int cnt;
3264
3265 if (tape->chrdev_direction != idetape_direction_read)
3266 return 0;
3267 tape->merge_stage_size = 0;
3268 if (tape->merge_stage != NULL) {
3269 __idetape_kfree_stage (tape->merge_stage);
3270 tape->merge_stage = NULL;
3271 }
3272 tape->chrdev_direction = idetape_direction_none;
3273
3274 if (tape->first_stage == NULL)
3275 return 0;
3276
3277 spin_lock_irqsave(&tape->spinlock, flags);
3278 tape->next_stage = NULL;
3279 if (idetape_pipeline_active (tape))
3280 idetape_wait_for_request(drive, tape->active_data_request);
3281 spin_unlock_irqrestore(&tape->spinlock, flags);
3282
3283 cnt = tape->nr_stages - tape->nr_pending_stages;
3284 while (tape->first_stage != NULL)
3285 idetape_remove_stage_head (drive);
3286 tape->nr_pending_stages = 0;
3287 tape->max_stages = tape->min_pipeline;
3288 return cnt;
3289 }
3290
3291 /*
3292 * idetape_position_tape positions the tape to the requested block
3293 * using the LOCATE packet command. A READ POSITION command is then
3294 * issued to check where we are positioned.
3295 *
3296 * Like all higher level operations, we queue the commands at the tail
3297 * of the request queue and wait for their completion.
3298 *
3299 */
3300 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, byte partition, int skip)
3301 {
3302 idetape_tape_t *tape = drive->driver_data;
3303 int retval;
3304 idetape_pc_t pc;
3305
3306 if (tape->chrdev_direction == idetape_direction_read)
3307 __idetape_discard_read_pipeline(drive);
3308 idetape_wait_ready(drive, 60 * 5 * HZ);
3309 idetape_create_locate_cmd (drive, &pc, block, partition, skip);
3310 retval=idetape_queue_pc_tail (drive,&pc);
3311 if (retval) return (retval);
3312
3313 idetape_create_read_position_cmd (&pc);
3314 return (idetape_queue_pc_tail (drive,&pc));
3315 }
3316
3317 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
3318 {
3319 idetape_tape_t *tape = drive->driver_data;
3320 int cnt;
3321 int seek, position;
3322
3323 cnt = __idetape_discard_read_pipeline(drive);
3324 if (restore_position) {
3325 position = idetape_read_position(drive);
3326 #if ONSTREAM_DEBUG
3327 if (tape->debug_level >= 2)
3328 printk(KERN_INFO "ide-tape: address %u, nr_stages %d\n", position, cnt);
3329 #endif
3330 seek = position > cnt ? position - cnt : 0;
3331 if (idetape_position_tape(drive, seek, 0, 0)) {
3332 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
3333 return;
3334 }
3335 }
3336 }
3337
3338 static void idetape_update_stats (ide_drive_t *drive)
3339 {
3340 idetape_pc_t pc;
3341
3342 idetape_create_mode_sense_cmd (&pc, 0x33);
3343 pc.callback = idetape_onstream_buffer_fill_callback;
3344 (void) idetape_queue_pc_tail(drive, &pc);
3345 }
3346
3347 /*
3348 * idetape_queue_rw_tail generates a read/write request for the block
3349 * device interface and wait for it to be serviced.
3350 */
3351 static int idetape_queue_rw_tail (ide_drive_t *drive, int cmd, int blocks, struct buffer_head *bh)
3352 {
3353 idetape_tape_t *tape = drive->driver_data;
3354 struct request rq;
3355
3356 #if IDETAPE_DEBUG_LOG
3357 if (tape->debug_level >= 2)
3358 printk (KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
3359 #endif /* IDETAPE_DEBUG_LOG */
3360 #if IDETAPE_DEBUG_BUGS
3361 if (idetape_pipeline_active (tape)) {
3362 printk (KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3363 return (0);
3364 }
3365 #endif /* IDETAPE_DEBUG_BUGS */
3366
3367 ide_init_drive_cmd (&rq);
3368 rq.bh = bh;
3369 rq.cmd = cmd;
3370 rq.sector = tape->first_frame_position;
3371 rq.nr_sectors = rq.current_nr_sectors = blocks;
3372 if (tape->onstream)
3373 tape->postpone_cnt = 600;
3374 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
3375
3376 if (cmd != IDETAPE_READ_RQ && cmd != IDETAPE_WRITE_RQ)
3377 return 0;
3378
3379 if (tape->merge_stage)
3380 idetape_init_merge_stage (tape);
3381 if (rq.errors == IDETAPE_ERROR_GENERAL)
3382 return -EIO;
3383 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
3384 }
3385
3386 /*
3387 * Read back the drive's internal buffer contents, as a part
3388 * of the write error recovery mechanism for old OnStream
3389 * firmware revisions.
3390 */
3391 static void idetape_onstream_read_back_buffer (ide_drive_t *drive)
3392 {
3393 idetape_tape_t *tape = drive->driver_data;
3394 int frames, i, logical_blk_num;
3395 idetape_stage_t *stage, *first = NULL, *last = NULL;
3396 os_aux_t *aux;
3397 struct request *rq;
3398 unsigned char *p;
3399 unsigned long flags;
3400
3401 idetape_update_stats(drive);
3402 frames = tape->cur_frames;
3403 logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num) - frames;
3404 printk(KERN_INFO "ide-tape: %s: reading back %d frames from the drive's internal buffer\n", tape->name, frames);
3405 for (i = 0; i < frames; i++) {
3406 stage = __idetape_kmalloc_stage(tape, 0, 0);
3407 if (!first)
3408 first = stage;
3409 aux = stage->aux;
3410 p = stage->bh->b_data;
3411 idetape_queue_rw_tail(drive, IDETAPE_READ_BUFFER_RQ, tape->capabilities.ctl, stage->bh);
3412 #if ONSTREAM_DEBUG
3413 if (tape->debug_level >= 2)
3414 printk(KERN_INFO "ide-tape: %s: read back logical block %d, data %x %x %x %x\n", tape->name, logical_blk_num, *p++, *p++, *p++, *p++);
3415 #endif
3416 rq = &stage->rq;
3417 ide_init_drive_cmd (rq);
3418 rq->cmd = IDETAPE_WRITE_RQ;
3419 rq->sector = tape->first_frame_position;
3420 rq->nr_sectors = rq->current_nr_sectors = tape->capabilities.ctl;
3421 idetape_init_stage(drive, stage, OS_FRAME_TYPE_DATA, logical_blk_num++);
3422 stage->next = NULL;
3423 if (last)
3424 last->next = stage;
3425 last = stage;
3426 }
3427 if (frames) {
3428 spin_lock_irqsave(&tape->spinlock, flags);
3429 last->next = tape->first_stage;
3430 tape->next_stage = tape->first_stage = first;
3431 tape->nr_stages += frames;
3432 tape->nr_pending_stages += frames;
3433 spin_unlock_irqrestore(&tape->spinlock, flags);
3434 }
3435 idetape_update_stats(drive);
3436 #if ONSTREAM_DEBUG
3437 if (tape->debug_level >= 2)
3438 printk(KERN_INFO "ide-tape: %s: frames left in buffer: %d\n", tape->name, tape->cur_frames);
3439 #endif
3440 }
3441
3442 /*
3443 * Error recovery algorithm for the OnStream tape.
3444 */
3445 static void idetape_onstream_write_error_recovery (ide_drive_t *drive)
3446 {
3447 idetape_tape_t *tape = drive->driver_data;
3448 unsigned int block;
3449
3450 if (tape->onstream_write_error == 1) {
3451 printk(KERN_ERR "ide-tape: %s: detected physical bad block at %u\n", tape->name, ntohl(tape->sense.information));
3452 block = ntohl(tape->sense.information) + 80;
3453 idetape_update_stats(drive);
3454 printk(KERN_ERR "ide-tape: %s: relocating %d buffered logical blocks to physical block %u\n", tape->name, tape->cur_frames, block);
3455 idetape_update_stats(drive);
3456 if (tape->firmware_revision_num >= 106)
3457 idetape_position_tape(drive, block, 0, 1);
3458 else {
3459 idetape_onstream_read_back_buffer(drive);
3460 idetape_position_tape(drive, block, 0, 0);
3461 }
3462 idetape_read_position(drive);
3463 #if ONSTREAM_DEBUG
3464 if (tape->debug_level >= 1)
3465 printk(KERN_ERR "ide-tape: %s: positioning complete, cur_frames %d, pos %d, tape pos %d\n", tape->name, tape->cur_frames, tape->first_frame_position, tape->last_frame_position);
3466 #endif
3467 } else if (tape->onstream_write_error == 2) {
3468 #if ONSTREAM_DEBUG
3469 if (tape->debug_level >= 1)
3470 printk(KERN_INFO "ide-tape: %s: skipping over config partition\n", tape->name);
3471 #endif
3472 idetape_flush_tape_buffers(drive);
3473 block = idetape_read_position(drive);
3474 if (block != 0xba4)
3475 printk(KERN_ERR "ide-tape: warning, current position %d, expected %d\n", block, 0xba4);
3476 idetape_position_tape(drive, 0xbb8, 0, 0);
3477 }
3478 tape->onstream_write_error = 0;
3479 }
3480
3481 /*
3482 * idetape_insert_pipeline_into_queue is used to start servicing the
3483 * pipeline stages, starting from tape->next_stage.
3484 */
3485 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
3486 {
3487 idetape_tape_t *tape = drive->driver_data;
3488
3489 if (tape->next_stage == NULL)
3490 return;
3491 if (!idetape_pipeline_active (tape)) {
3492 if (tape->onstream_write_error)
3493 idetape_onstream_write_error_recovery(drive);
3494 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
3495 idetape_active_next_stage (drive);
3496 (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
3497 }
3498 }
3499
3500 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
3501 {
3502 idetape_init_pc(pc);
3503 pc->c[0] = IDETAPE_INQUIRY_CMD;
3504 pc->c[4] = pc->request_transfer = 254;
3505 pc->callback = &idetape_pc_callback;
3506 }
3507
3508 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
3509 {
3510 idetape_tape_t *tape = drive->driver_data;
3511
3512 idetape_init_pc (pc);
3513 pc->c[0] = IDETAPE_REWIND_CMD;
3514 if (tape->onstream)
3515 pc->c[1] = 1;
3516 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3517 pc->callback = &idetape_pc_callback;
3518 }
3519
3520 static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
3521 {
3522 idetape_init_pc (pc);
3523 set_bit (PC_WRITING, &pc->flags);
3524 pc->c[0] = IDETAPE_MODE_SELECT_CMD;
3525 pc->c[1] = 0x10;
3526 put_unaligned (htons(length), (unsigned short *) &pc->c[3]);
3527 pc->request_transfer = 255;
3528 pc->callback = &idetape_pc_callback;
3529 }
3530
3531 static void idetape_create_erase_cmd (idetape_pc_t *pc)
3532 {
3533 idetape_init_pc (pc);
3534 pc->c[0] = IDETAPE_ERASE_CMD;
3535 pc->c[1] = 1;
3536 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3537 pc->callback = &idetape_pc_callback;
3538 }
3539
3540 static void idetape_create_space_cmd (idetape_pc_t *pc,int count,byte cmd)
3541 {
3542 idetape_init_pc (pc);
3543 pc->c[0] = IDETAPE_SPACE_CMD;
3544 put_unaligned (htonl (count), (unsigned int *) &pc->c[1]);
3545 pc->c[1] = cmd;
3546 set_bit (PC_WAIT_FOR_DSC, &pc->flags);
3547 pc->callback = &idetape_pc_callback;
3548 }
3549
3550 /*
3551 * Verify that we have the correct tape frame
3552 */
3553 static int idetape_verify_stage (ide_drive_t *drive, idetape_stage_t *stage, int logical_blk_num, int quiet)
3554 {
3555 idetape_tape_t *tape = drive->driver_data;
3556 os_aux_t *aux = stage->aux;
3557 os_partition_t *par = &aux->partition;
3558 struct request *rq = &stage->rq;
3559 struct buffer_head *bh;
3560
3561 if (!tape->onstream)
3562 return 1;
3563 if (tape->raw) {
3564 if (rq->errors) {
3565 bh = stage->bh;
3566 while (bh) {
3567 memset(bh->b_data, 0, bh->b_size);
3568 bh = bh->b_reqnext;
3569 }
3570 strcpy(stage->bh->b_data, "READ ERROR ON FRAME");
3571 }
3572 return 1;
3573 }
3574 if (rq->errors == IDETAPE_ERROR_GENERAL) {
3575 printk(KERN_INFO "ide-tape: %s: skipping frame, read error\n", tape->name);
3576 return 0;
3577 }
3578 if (rq->errors == IDETAPE_ERROR_EOD) {
3579 printk(KERN_INFO "ide-tape: %s: skipping frame, eod\n", tape->name);
3580 return 0;
3581 }
3582 if (ntohl(aux->format_id) != 0) {
3583 printk(KERN_INFO "ide-tape: %s: skipping frame, format_id %u\n", tape->name, ntohl(aux->format_id));
3584 return 0;
3585 }
3586 if (memcmp(aux->application_sig, tape->application_sig, 4) != 0) {
3587 printk(KERN_INFO "ide-tape: %s: skipping frame, incorrect application signature\n", tape->name);
3588 return 0;
3589 }
3590 if (aux->frame_type != OS_FRAME_TYPE_DATA &&
3591 aux->frame_type != OS_FRAME_TYPE_EOD &&
3592 aux->frame_type != OS_FRAME_TYPE_MARKER) {
3593 printk(KERN_INFO "ide-tape: %s: skipping frame, frame type %x\n", tape->name, aux->frame_type);
3594 return 0;
3595 }
3596 if (par->partition_num != OS_DATA_PARTITION) {
3597 if (!tape->linux_media || tape->linux_media_version != 2) {
3598 printk(KERN_INFO "ide-tape: %s: skipping frame, partition num %d\n", tape->name, par->partition_num);
3599 return 0;
3600 }
3601 }
3602 if (par->par_desc_ver != OS_PARTITION_VERSION) {
3603 printk(KERN_INFO "ide-tape: %s: skipping frame, partition version %d\n", tape->name, par->par_desc_ver);
3604 return 0;
3605 }
3606 if (ntohs(par->wrt_pass_cntr) != tape->wrt_pass_cntr) {
3607 printk(KERN_INFO "ide-tape: %s: skipping frame, wrt_pass_cntr %d (expected %d)(logical_blk_num %u)\n", tape->name, ntohs(par->wrt_pass_cntr), tape->wrt_pass_cntr, ntohl(aux->logical_blk_num));
3608 return 0;
3609 }
3610 if (aux->frame_seq_num != aux->logical_blk_num) {
3611 printk(KERN_INFO "ide-tape: %s: skipping frame, seq != logical\n", tape->name);
3612 return 0;
3613 }
3614 if (logical_blk_num != -1 && ntohl(aux->logical_blk_num) != logical_blk_num) {
3615 if (!quiet)
3616 printk(KERN_INFO "ide-tape: %s: skipping frame, logical_blk_num %u (expected %d)\n", tape->name, ntohl(aux->logical_blk_num), logical_blk_num);
3617 return 0;
3618 }
3619 if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
3620 rq->errors = IDETAPE_ERROR_FILEMARK;
3621 rq->current_nr_sectors = rq->nr_sectors;
3622 }
3623 return 1;
3624 }
3625
3626 static void idetape_wait_first_stage (ide_drive_t *drive)
3627 {
3628 idetape_tape_t *tape = drive->driver_data;
3629 unsigned long flags;
3630
3631 if (tape->first_stage == NULL)
3632 return;
3633 spin_lock_irqsave(&tape->spinlock, flags);
3634 if (tape->active_stage == tape->first_stage)
3635 idetape_wait_for_request(drive, tape->active_data_request);
3636 spin_unlock_irqrestore(&tape->spinlock, flags);
3637 }
3638
3639 /*
3640 * idetape_add_chrdev_write_request tries to add a character device
3641 * originated write request to our pipeline. In case we don't succeed,
3642 * we revert to non-pipelined operation mode for this request.
3643 *
3644 * 1. Try to allocate a new pipeline stage.
3645 * 2. If we can't, wait for more and more requests to be serviced
3646 * and try again each time.
3647 * 3. If we still can't allocate a stage, fallback to
3648 * non-pipelined operation mode for this request.
3649 */
3650 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3651 {
3652 idetape_tape_t *tape = drive->driver_data;
3653 idetape_stage_t *new_stage;
3654 unsigned long flags;
3655 struct request *rq;
3656
3657 #if IDETAPE_DEBUG_LOG
3658 if (tape->debug_level >= 3)
3659 printk (KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
3660 #endif /* IDETAPE_DEBUG_LOG */
3661
3662 /*
3663 * Attempt to allocate a new stage.
3664 * Pay special attention to possible race conditions.
3665 */
3666 while ((new_stage = idetape_kmalloc_stage (tape)) == NULL) {
3667 spin_lock_irqsave(&tape->spinlock, flags);
3668 if (idetape_pipeline_active (tape)) {
3669 idetape_wait_for_request(drive, tape->active_data_request);
3670 spin_unlock_irqrestore(&tape->spinlock, flags);
3671 } else {
3672 spin_unlock_irqrestore(&tape->spinlock, flags);
3673 idetape_insert_pipeline_into_queue (drive);
3674 if (idetape_pipeline_active (tape))
3675 continue;
3676 /*
3677 * Linux is short on memory. Fallback to
3678 * non-pipelined operation mode for this request.
3679 */
3680 return idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
3681 }
3682 }
3683 rq = &new_stage->rq;
3684 ide_init_drive_cmd (rq);
3685 rq->cmd = IDETAPE_WRITE_RQ;
3686 rq->sector = tape->first_frame_position; /* Doesn't actually matter - We always assume sequential access */
3687 rq->nr_sectors = rq->current_nr_sectors = blocks;
3688
3689 idetape_switch_buffers (tape, new_stage);
3690 idetape_init_stage(drive, new_stage, OS_FRAME_TYPE_DATA, tape->logical_blk_num);
3691 tape->logical_blk_num++;
3692 idetape_add_stage_tail (drive,new_stage);
3693 tape->pipeline_head++;
3694 #if USE_IOTRACE
3695 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3696 #endif
3697 calculate_speeds(drive);
3698
3699 /*
3700 * Estimate whether the tape has stopped writing by checking
3701 * if our write pipeline is currently empty. If we are not
3702 * writing anymore, wait for the pipeline to be full enough
3703 * (90%) before starting to service requests, so that we will
3704 * be able to keep up with the higher speeds of the tape.
3705 *
3706 * For the OnStream drive, we can query the number of pending
3707 * frames in the drive's internal buffer. As long as the tape
3708 * is still writing, it is better to write frames immediately
3709 * rather than gather them in the pipeline. This will give the
3710 * tape's firmware the ability to sense the current incoming
3711 * data rate more accurately, and since the OnStream tape
3712 * supports variable speeds, it can try to adjust itself to the
3713 * incoming data rate.
3714 */
3715 if (!idetape_pipeline_active(tape)) {
3716 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
3717 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
3718 tape->measure_insert_time = 1;
3719 tape->insert_time = jiffies;
3720 tape->insert_size = 0;
3721 tape->insert_speed = 0;
3722 idetape_insert_pipeline_into_queue (drive);
3723 } else if (tape->onstream) {
3724 idetape_update_stats(drive);
3725 if (tape->cur_frames > 5)
3726 idetape_insert_pipeline_into_queue (drive);
3727 }
3728 }
3729 if (