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