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