blob: f04791a58df0b0784e3e754cad9a0275034f0712 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-tape.c Version 1.19 Nov, 2003
3 *
4 * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 *
6 * $Header$
7 *
8 * This driver was constructed as a student project in the software laboratory
9 * of the faculty of electrical engineering in the Technion - Israel's
10 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
11 *
12 * It is hereby placed under the terms of the GNU general public license.
13 * (See linux/COPYING).
14 */
15
16/*
17 * IDE ATAPI streaming tape driver.
18 *
19 * This driver is a part of the Linux ide driver and works in co-operation
20 * with linux/drivers/block/ide.c.
21 *
22 * The driver, in co-operation with ide.c, basically traverses the
23 * request-list for the block device interface. The character device
24 * interface, on the other hand, creates new requests, adds them
25 * to the request-list of the block device, and waits for their completion.
26 *
27 * Pipelined operation mode is now supported on both reads and writes.
28 *
29 * The block device major and minor numbers are determined from the
30 * tape's relative position in the ide interfaces, as explained in ide.c.
31 *
32 * The character device interface consists of the following devices:
33 *
34 * ht0 major 37, minor 0 first IDE tape, rewind on close.
35 * ht1 major 37, minor 1 second IDE tape, rewind on close.
36 * ...
37 * nht0 major 37, minor 128 first IDE tape, no rewind on close.
38 * nht1 major 37, minor 129 second IDE tape, no rewind on close.
39 * ...
40 *
41 * Run linux/scripts/MAKEDEV.ide to create the above entries.
42 *
43 * The general magnetic tape commands compatible interface, as defined by
44 * include/linux/mtio.h, is accessible through the character device.
45 *
46 * General ide driver configuration options, such as the interrupt-unmask
47 * flag, can be configured by issuing an ioctl to the block device interface,
48 * as any other ide device.
49 *
50 * Our own ide-tape ioctl's can be issued to either the block device or
51 * the character device interface.
52 *
53 * Maximal throughput with minimal bus load will usually be achieved in the
54 * following scenario:
55 *
56 * 1. ide-tape is operating in the pipelined operation mode.
57 * 2. No buffering is performed by the user backup program.
58 *
59 * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
60 *
61 * Ver 0.1 Nov 1 95 Pre-working code :-)
62 * Ver 0.2 Nov 23 95 A short backup (few megabytes) and restore procedure
63 * was successful ! (Using tar cvf ... on the block
64 * device interface).
65 * A longer backup resulted in major swapping, bad
66 * overall Linux performance and eventually failed as
67 * we received non serial read-ahead requests from the
68 * buffer cache.
69 * Ver 0.3 Nov 28 95 Long backups are now possible, thanks to the
70 * character device interface. Linux's responsiveness
71 * and performance doesn't seem to be much affected
72 * from the background backup procedure.
73 * Some general mtio.h magnetic tape operations are
74 * now supported by our character device. As a result,
75 * popular tape utilities are starting to work with
76 * ide tapes :-)
77 * The following configurations were tested:
78 * 1. An IDE ATAPI TAPE shares the same interface
79 * and irq with an IDE ATAPI CDROM.
80 * 2. An IDE ATAPI TAPE shares the same interface
81 * and irq with a normal IDE disk.
82 * Both configurations seemed to work just fine !
83 * However, to be on the safe side, it is meanwhile
84 * recommended to give the IDE TAPE its own interface
85 * and irq.
86 * The one thing which needs to be done here is to
87 * add a "request postpone" feature to ide.c,
88 * so that we won't have to wait for the tape to finish
89 * performing a long media access (DSC) request (such
90 * as a rewind) before we can access the other device
91 * on the same interface. This effect doesn't disturb
92 * normal operation most of the time because read/write
93 * requests are relatively fast, and once we are
94 * performing one tape r/w request, a lot of requests
95 * from the other device can be queued and ide.c will
96 * service all of them after this single tape request.
97 * Ver 1.0 Dec 11 95 Integrated into Linux 1.3.46 development tree.
98 * On each read / write request, we now ask the drive
99 * if we can transfer a constant number of bytes
100 * (a parameter of the drive) only to its buffers,
101 * without causing actual media access. If we can't,
102 * we just wait until we can by polling the DSC bit.
103 * This ensures that while we are not transferring
104 * more bytes than the constant referred to above, the
105 * interrupt latency will not become too high and
106 * we won't cause an interrupt timeout, as happened
107 * occasionally in the previous version.
108 * While polling for DSC, the current request is
109 * postponed and ide.c is free to handle requests from
110 * the other device. This is handled transparently to
111 * ide.c. The hwgroup locking method which was used
112 * in the previous version was removed.
113 * Use of new general features which are provided by
114 * ide.c for use with atapi devices.
115 * (Programming done by Mark Lord)
116 * Few potential bug fixes (Again, suggested by Mark)
117 * Single character device data transfers are now
118 * not limited in size, as they were before.
119 * We are asking the tape about its recommended
120 * transfer unit and send a larger data transfer
121 * as several transfers of the above size.
122 * For best results, use an integral number of this
123 * basic unit (which is shown during driver
124 * initialization). I will soon add an ioctl to get
125 * this important parameter.
126 * Our data transfer buffer is allocated on startup,
127 * rather than before each data transfer. This should
128 * ensure that we will indeed have a data buffer.
129 * Ver 1.1 Dec 14 95 Fixed random problems which occurred when the tape
130 * shared an interface with another device.
131 * (poll_for_dsc was a complete mess).
132 * Removed some old (non-active) code which had
133 * to do with supporting buffer cache originated
134 * requests.
135 * The block device interface can now be opened, so
136 * that general ide driver features like the unmask
137 * interrupts flag can be selected with an ioctl.
138 * This is the only use of the block device interface.
139 * New fast pipelined operation mode (currently only on
140 * writes). When using the pipelined mode, the
141 * throughput can potentially reach the maximum
142 * tape supported throughput, regardless of the
143 * user backup program. On my tape drive, it sometimes
144 * boosted performance by a factor of 2. Pipelined
145 * mode is enabled by default, but since it has a few
146 * downfalls as well, you may want to disable it.
147 * A short explanation of the pipelined operation mode
148 * is available below.
149 * Ver 1.2 Jan 1 96 Eliminated pipelined mode race condition.
150 * Added pipeline read mode. As a result, restores
151 * are now as fast as backups.
152 * Optimized shared interface behavior. The new behavior
153 * typically results in better IDE bus efficiency and
154 * higher tape throughput.
155 * Pre-calculation of the expected read/write request
156 * service time, based on the tape's parameters. In
157 * the pipelined operation mode, this allows us to
158 * adjust our polling frequency to a much lower value,
159 * and thus to dramatically reduce our load on Linux,
160 * without any decrease in performance.
161 * Implemented additional mtio.h operations.
162 * The recommended user block size is returned by
163 * the MTIOCGET ioctl.
164 * Additional minor changes.
165 * Ver 1.3 Feb 9 96 Fixed pipelined read mode bug which prevented the
166 * use of some block sizes during a restore procedure.
167 * The character device interface will now present a
168 * continuous view of the media - any mix of block sizes
169 * during a backup/restore procedure is supported. The
170 * driver will buffer the requests internally and
171 * convert them to the tape's recommended transfer
172 * unit, making performance almost independent of the
173 * chosen user block size.
174 * Some improvements in error recovery.
175 * By cooperating with ide-dma.c, bus mastering DMA can
176 * now sometimes be used with IDE tape drives as well.
177 * Bus mastering DMA has the potential to dramatically
178 * reduce the CPU's overhead when accessing the device,
179 * and can be enabled by using hdparm -d1 on the tape's
180 * block device interface. For more info, read the
181 * comments in ide-dma.c.
182 * Ver 1.4 Mar 13 96 Fixed serialize support.
183 * Ver 1.5 Apr 12 96 Fixed shared interface operation, broken in 1.3.85.
184 * Fixed pipelined read mode inefficiency.
185 * Fixed nasty null dereferencing bug.
186 * Ver 1.6 Aug 16 96 Fixed FPU usage in the driver.
187 * Fixed end of media bug.
188 * Ver 1.7 Sep 10 96 Minor changes for the CONNER CTT8000-A model.
189 * Ver 1.8 Sep 26 96 Attempt to find a better balance between good
190 * interactive response and high system throughput.
191 * Ver 1.9 Nov 5 96 Automatically cross encountered filemarks rather
192 * than requiring an explicit FSF command.
193 * Abort pending requests at end of media.
194 * MTTELL was sometimes returning incorrect results.
195 * Return the real block size in the MTIOCGET ioctl.
196 * Some error recovery bug fixes.
197 * Ver 1.10 Nov 5 96 Major reorganization.
198 * Reduced CPU overhead a bit by eliminating internal
199 * bounce buffers.
200 * Added module support.
201 * Added multiple tape drives support.
202 * Added partition support.
203 * Rewrote DSC handling.
204 * Some portability fixes.
205 * Removed ide-tape.h.
206 * Additional minor changes.
207 * Ver 1.11 Dec 2 96 Bug fix in previous DSC timeout handling.
208 * Use ide_stall_queue() for DSC overlap.
209 * Use the maximum speed rather than the current speed
210 * to compute the request service time.
211 * Ver 1.12 Dec 7 97 Fix random memory overwriting and/or last block data
212 * corruption, which could occur if the total number
213 * of bytes written to the tape was not an integral
214 * number of tape blocks.
215 * Add support for INTERRUPT DRQ devices.
216 * Ver 1.13 Jan 2 98 Add "speed == 0" work-around for HP COLORADO 5GB
217 * Ver 1.14 Dec 30 98 Partial fixes for the Sony/AIWA tape drives.
218 * Replace cli()/sti() with hwgroup spinlocks.
219 * Ver 1.15 Mar 25 99 Fix SMP race condition by replacing hwgroup
220 * spinlock with private per-tape spinlock.
221 * Ver 1.16 Sep 1 99 Add OnStream tape support.
222 * Abort read pipeline on EOD.
223 * Wait for the tape to become ready in case it returns
224 * "in the process of becoming ready" on open().
225 * Fix zero padding of the last written block in
226 * case the tape block size is larger than PAGE_SIZE.
227 * Decrease the default disconnection time to tn.
228 * Ver 1.16e Oct 3 99 Minor fixes.
229 * Ver 1.16e1 Oct 13 99 Patches by Arnold Niessen,
230 * niessen@iae.nl / arnold.niessen@philips.com
231 * GO-1) Undefined code in idetape_read_position
232 * according to Gadi's email
233 * AJN-1) Minor fix asc == 11 should be asc == 0x11
234 * in idetape_issue_packet_command (did effect
235 * debugging output only)
236 * AJN-2) Added more debugging output, and
237 * added ide-tape: where missing. I would also
238 * like to add tape->name where possible
239 * AJN-3) Added different debug_level's
240 * via /proc/ide/hdc/settings
241 * "debug_level" determines amount of debugging output;
242 * can be changed using /proc/ide/hdx/settings
243 * 0 : almost no debugging output
244 * 1 : 0+output errors only
245 * 2 : 1+output all sensekey/asc
246 * 3 : 2+follow all chrdev related procedures
247 * 4 : 3+follow all procedures
248 * 5 : 4+include pc_stack rq_stack info
249 * 6 : 5+USE_COUNT updates
250 * AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
251 * from 5 to 10 minutes
252 * AJN-5) Changed maximum number of blocks to skip when
253 * reading tapes with multiple consecutive write
254 * errors from 100 to 1000 in idetape_get_logical_blk
255 * Proposed changes to code:
256 * 1) output "logical_blk_num" via /proc
257 * 2) output "current_operation" via /proc
258 * 3) Either solve or document the fact that `mt rewind' is
259 * required after reading from /dev/nhtx to be
260 * able to rmmod the idetape module;
261 * Also, sometimes an application finishes but the
262 * device remains `busy' for some time. Same cause ?
263 * Proposed changes to release-notes:
264 * 4) write a simple `quickstart' section in the
265 * release notes; I volunteer if you don't want to
266 * 5) include a pointer to video4linux in the doc
267 * to stimulate video applications
268 * 6) release notes lines 331 and 362: explain what happens
269 * if the application data rate is higher than 1100 KB/s;
270 * similar approach to lower-than-500 kB/s ?
271 * 7) 6.6 Comparison; wouldn't it be better to allow different
272 * strategies for read and write ?
273 * Wouldn't it be better to control the tape buffer
274 * contents instead of the bandwidth ?
275 * 8) line 536: replace will by would (if I understand
276 * this section correctly, a hypothetical and unwanted situation
277 * is being described)
278 * Ver 1.16f Dec 15 99 Change place of the secondary OnStream header frames.
279 * Ver 1.17 Nov 2000 / Jan 2001 Marcel Mol, marcel@mesa.nl
280 * - Add idetape_onstream_mode_sense_tape_parameter_page
281 * function to get tape capacity in frames: tape->capacity.
282 * - Add support for DI-50 drives( or any DI- drive).
283 * - 'workaround' for read error/blank block around block 3000.
284 * - Implement Early warning for end of media for Onstream.
285 * - Cosmetic code changes for readability.
286 * - Idetape_position_tape should not use SKIP bit during
287 * Onstream read recovery.
288 * - Add capacity, logical_blk_num and first/last_frame_position
289 * to /proc/ide/hd?/settings.
290 * - Module use count was gone in the Linux 2.4 driver.
291 * Ver 1.17a Apr 2001 Willem Riede osst@riede.org
292 * - Get drive's actual block size from mode sense block descriptor
293 * - Limit size of pipeline
294 * Ver 1.17b Oct 2002 Alan Stern <stern@rowland.harvard.edu>
295 * Changed IDETAPE_MIN_PIPELINE_STAGES to 1 and actually used
296 * it in the code!
297 * Actually removed aborted stages in idetape_abort_pipeline
298 * instead of just changing the command code.
299 * Made the transfer byte count for Request Sense equal to the
300 * actual length of the data transfer.
301 * Changed handling of partial data transfers: they do not
302 * cause DMA errors.
303 * Moved initiation of DMA transfers to the correct place.
304 * Removed reference to unallocated memory.
305 * Made __idetape_discard_read_pipeline return the number of
306 * sectors skipped, not the number of stages.
307 * Replaced errant kfree() calls with __idetape_kfree_stage().
308 * Fixed off-by-one error in testing the pipeline length.
309 * Fixed handling of filemarks in the read pipeline.
310 * Small code optimization for MTBSF and MTBSFM ioctls.
311 * Don't try to unlock the door during device close if is
312 * already unlocked!
313 * Cosmetic fixes to miscellaneous debugging output messages.
314 * Set the minimum /proc/ide/hd?/settings values for "pipeline",
315 * "pipeline_min", and "pipeline_max" to 1.
316 *
317 * Here are some words from the first releases of hd.c, which are quoted
318 * in ide.c and apply here as well:
319 *
320 * | Special care is recommended. Have Fun!
321 *
322 */
323
324/*
325 * An overview of the pipelined operation mode.
326 *
327 * In the pipelined write mode, we will usually just add requests to our
328 * pipeline and return immediately, before we even start to service them. The
329 * user program will then have enough time to prepare the next request while
330 * we are still busy servicing previous requests. In the pipelined read mode,
331 * the situation is similar - we add read-ahead requests into the pipeline,
332 * before the user even requested them.
333 *
334 * The pipeline can be viewed as a "safety net" which will be activated when
335 * the system load is high and prevents the user backup program from keeping up
336 * with the current tape speed. At this point, the pipeline will get
337 * shorter and shorter but the tape will still be streaming at the same speed.
338 * Assuming we have enough pipeline stages, the system load will hopefully
339 * decrease before the pipeline is completely empty, and the backup program
340 * will be able to "catch up" and refill the pipeline again.
341 *
342 * When using the pipelined mode, it would be best to disable any type of
343 * buffering done by the user program, as ide-tape already provides all the
344 * benefits in the kernel, where it can be done in a more efficient way.
345 * As we will usually not block the user program on a request, the most
346 * efficient user code will then be a simple read-write-read-... cycle.
347 * Any additional logic will usually just slow down the backup process.
348 *
349 * Using the pipelined mode, I get a constant over 400 KBps throughput,
350 * which seems to be the maximum throughput supported by my tape.
351 *
352 * However, there are some downfalls:
353 *
354 * 1. We use memory (for data buffers) in proportional to the number
355 * of pipeline stages (each stage is about 26 KB with my tape).
356 * 2. In the pipelined write mode, we cheat and postpone error codes
357 * to the user task. In read mode, the actual tape position
358 * will be a bit further than the last requested block.
359 *
360 * Concerning (1):
361 *
362 * 1. We allocate stages dynamically only when we need them. When
363 * we don't need them, we don't consume additional memory. In
364 * case we can't allocate stages, we just manage without them
365 * (at the expense of decreased throughput) so when Linux is
366 * tight in memory, we will not pose additional difficulties.
367 *
368 * 2. The maximum number of stages (which is, in fact, the maximum
369 * amount of memory) which we allocate is limited by the compile
370 * time parameter IDETAPE_MAX_PIPELINE_STAGES.
371 *
372 * 3. The maximum number of stages is a controlled parameter - We
373 * don't start from the user defined maximum number of stages
374 * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
375 * will not even allocate this amount of stages if the user
376 * program can't handle the speed). We then implement a feedback
377 * loop which checks if the pipeline is empty, and if it is, we
378 * increase the maximum number of stages as necessary until we
379 * reach the optimum value which just manages to keep the tape
380 * busy with minimum allocated memory or until we reach
381 * IDETAPE_MAX_PIPELINE_STAGES.
382 *
383 * Concerning (2):
384 *
385 * In pipelined write mode, ide-tape can not return accurate error codes
386 * to the user program since we usually just add the request to the
387 * pipeline without waiting for it to be serviced. In case an error
388 * occurs, I will report it on the next user request.
389 *
390 * In the pipelined read mode, subsequent read requests or forward
391 * filemark spacing will perform correctly, as we preserve all blocks
392 * and filemarks which we encountered during our excess read-ahead.
393 *
394 * For accurate tape positioning and error reporting, disabling
395 * pipelined mode might be the best option.
396 *
397 * You can enable/disable/tune the pipelined operation mode by adjusting
398 * the compile time parameters below.
399 */
400
401/*
402 * Possible improvements.
403 *
404 * 1. Support for the ATAPI overlap protocol.
405 *
406 * In order to maximize bus throughput, we currently use the DSC
407 * overlap method which enables ide.c to service requests from the
408 * other device while the tape is busy executing a command. The
409 * DSC overlap method involves polling the tape's status register
410 * for the DSC bit, and servicing the other device while the tape
411 * isn't ready.
412 *
413 * In the current QIC development standard (December 1995),
414 * it is recommended that new tape drives will *in addition*
415 * implement the ATAPI overlap protocol, which is used for the
416 * same purpose - efficient use of the IDE bus, but is interrupt
417 * driven and thus has much less CPU overhead.
418 *
419 * ATAPI overlap is likely to be supported in most new ATAPI
420 * devices, including new ATAPI cdroms, and thus provides us
421 * a method by which we can achieve higher throughput when
422 * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
423 */
424
425#define IDETAPE_VERSION "1.19"
426
427#include <linux/config.h>
428#include <linux/module.h>
429#include <linux/types.h>
430#include <linux/string.h>
431#include <linux/kernel.h>
432#include <linux/delay.h>
433#include <linux/timer.h>
434#include <linux/mm.h>
435#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -0800436#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#include <linux/major.h>
438#include <linux/devfs_fs_kernel.h>
439#include <linux/errno.h>
440#include <linux/genhd.h>
441#include <linux/slab.h>
442#include <linux/pci.h>
443#include <linux/ide.h>
444#include <linux/smp_lock.h>
445#include <linux/completion.h>
446#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800447#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449#include <asm/byteorder.h>
450#include <asm/irq.h>
451#include <asm/uaccess.h>
452#include <asm/io.h>
453#include <asm/unaligned.h>
454
455/*
456 * partition
457 */
458typedef struct os_partition_s {
459 __u8 partition_num;
460 __u8 par_desc_ver;
461 __u16 wrt_pass_cntr;
462 __u32 first_frame_addr;
463 __u32 last_frame_addr;
464 __u32 eod_frame_addr;
465} os_partition_t;
466
467/*
468 * DAT entry
469 */
470typedef struct os_dat_entry_s {
471 __u32 blk_sz;
472 __u16 blk_cnt;
473 __u8 flags;
474 __u8 reserved;
475} os_dat_entry_t;
476
477/*
478 * DAT
479 */
480#define OS_DAT_FLAGS_DATA (0xc)
481#define OS_DAT_FLAGS_MARK (0x1)
482
483typedef struct os_dat_s {
484 __u8 dat_sz;
485 __u8 reserved1;
486 __u8 entry_cnt;
487 __u8 reserved3;
488 os_dat_entry_t dat_list[16];
489} os_dat_t;
490
491#include <linux/mtio.h>
492
493/**************************** Tunable parameters *****************************/
494
495
496/*
497 * Pipelined mode parameters.
498 *
499 * We try to use the minimum number of stages which is enough to
500 * keep the tape constantly streaming. To accomplish that, we implement
501 * a feedback loop around the maximum number of stages:
502 *
503 * We start from MIN maximum stages (we will not even use MIN stages
504 * if we don't need them), increment it by RATE*(MAX-MIN)
505 * whenever we sense that the pipeline is empty, until we reach
506 * the optimum value or until we reach MAX.
507 *
508 * Setting the following parameter to 0 is illegal: the pipelined mode
509 * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
510 */
511#define IDETAPE_MIN_PIPELINE_STAGES 1
512#define IDETAPE_MAX_PIPELINE_STAGES 400
513#define IDETAPE_INCREASE_STAGES_RATE 20
514
515/*
516 * The following are used to debug the driver:
517 *
518 * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
519 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
520 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
521 * some places.
522 *
523 * Setting them to 0 will restore normal operation mode:
524 *
525 * 1. Disable logging normal successful operations.
526 * 2. Disable self-sanity checks.
527 * 3. Errors will still be logged, of course.
528 *
529 * All the #if DEBUG code will be removed some day, when the driver
530 * is verified to be stable enough. This will make it much more
531 * esthetic.
532 */
533#define IDETAPE_DEBUG_INFO 0
534#define IDETAPE_DEBUG_LOG 0
535#define IDETAPE_DEBUG_BUGS 1
536
537/*
538 * After each failed packet command we issue a request sense command
539 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
540 *
541 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
542 */
543#define IDETAPE_MAX_PC_RETRIES 3
544
545/*
546 * With each packet command, we allocate a buffer of
547 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
548 * commands (Not for READ/WRITE commands).
549 */
550#define IDETAPE_PC_BUFFER_SIZE 256
551
552/*
553 * In various places in the driver, we need to allocate storage
554 * for packet commands and requests, which will remain valid while
555 * we leave the driver to wait for an interrupt or a timeout event.
556 */
557#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
558
559/*
560 * Some drives (for example, Seagate STT3401A Travan) require a very long
561 * timeout, because they don't return an interrupt or clear their busy bit
562 * until after the command completes (even retension commands).
563 */
564#define IDETAPE_WAIT_CMD (900*HZ)
565
566/*
567 * The following parameter is used to select the point in the internal
568 * tape fifo in which we will start to refill the buffer. Decreasing
569 * the following parameter will improve the system's latency and
570 * interactive response, while using a high value might improve sytem
571 * throughput.
572 */
573#define IDETAPE_FIFO_THRESHOLD 2
574
575/*
576 * DSC polling parameters.
577 *
578 * Polling for DSC (a single bit in the status register) is a very
579 * important function in ide-tape. There are two cases in which we
580 * poll for DSC:
581 *
582 * 1. Before a read/write packet command, to ensure that we
583 * can transfer data from/to the tape's data buffers, without
584 * causing an actual media access. In case the tape is not
585 * ready yet, we take out our request from the device
586 * request queue, so that ide.c will service requests from
587 * the other device on the same interface meanwhile.
588 *
589 * 2. After the successful initialization of a "media access
590 * packet command", which is a command which can take a long
591 * time to complete (it can be several seconds or even an hour).
592 *
593 * Again, we postpone our request in the middle to free the bus
594 * for the other device. The polling frequency here should be
595 * lower than the read/write frequency since those media access
596 * commands are slow. We start from a "fast" frequency -
597 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
598 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
599 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
600 *
601 * We also set a timeout for the timer, in case something goes wrong.
602 * The timeout should be longer then the maximum execution time of a
603 * tape operation.
604 */
605
606/*
607 * DSC timings.
608 */
609#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
610#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
611#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
612#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
613#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
614#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
615#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
616
617/*************************** End of tunable parameters ***********************/
618
619/*
620 * Debugging/Performance analysis
621 *
622 * I/O trace support
623 */
624#define USE_IOTRACE 0
625#if USE_IOTRACE
626#include <linux/io_trace.h>
627#define IO_IDETAPE_FIFO 500
628#endif
629
630/*
631 * Read/Write error simulation
632 */
633#define SIMULATE_ERRORS 0
634
635/*
636 * For general magnetic tape device compatibility.
637 */
638typedef enum {
639 idetape_direction_none,
640 idetape_direction_read,
641 idetape_direction_write
642} idetape_chrdev_direction_t;
643
644struct idetape_bh {
645 unsigned short b_size;
646 atomic_t b_count;
647 struct idetape_bh *b_reqnext;
648 char *b_data;
649};
650
651/*
652 * Our view of a packet command.
653 */
654typedef struct idetape_packet_command_s {
655 u8 c[12]; /* Actual packet bytes */
656 int retries; /* On each retry, we increment retries */
657 int error; /* Error code */
658 int request_transfer; /* Bytes to transfer */
659 int actually_transferred; /* Bytes actually transferred */
660 int buffer_size; /* Size of our data buffer */
661 struct idetape_bh *bh;
662 char *b_data;
663 int b_count;
664 u8 *buffer; /* Data buffer */
665 u8 *current_position; /* Pointer into the above buffer */
666 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
667 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
668 unsigned long flags; /* Status/Action bit flags: long for set_bit */
669} idetape_pc_t;
670
671/*
672 * Packet command flag bits.
673 */
674/* Set when an error is considered normal - We won't retry */
675#define PC_ABORT 0
676/* 1 When polling for DSC on a media access command */
677#define PC_WAIT_FOR_DSC 1
678/* 1 when we prefer to use DMA if possible */
679#define PC_DMA_RECOMMENDED 2
680/* 1 while DMA in progress */
681#define PC_DMA_IN_PROGRESS 3
682/* 1 when encountered problem during DMA */
683#define PC_DMA_ERROR 4
684/* Data direction */
685#define PC_WRITING 5
686
687/*
688 * Capabilities and Mechanical Status Page
689 */
690typedef struct {
691 unsigned page_code :6; /* Page code - Should be 0x2a */
692 __u8 reserved0_6 :1;
693 __u8 ps :1; /* parameters saveable */
694 __u8 page_length; /* Page Length - Should be 0x12 */
695 __u8 reserved2, reserved3;
696 unsigned ro :1; /* Read Only Mode */
697 unsigned reserved4_1234 :4;
698 unsigned sprev :1; /* Supports SPACE in the reverse direction */
699 unsigned reserved4_67 :2;
700 unsigned reserved5_012 :3;
701 unsigned efmt :1; /* Supports ERASE command initiated formatting */
702 unsigned reserved5_4 :1;
703 unsigned qfa :1; /* Supports the QFA two partition formats */
704 unsigned reserved5_67 :2;
705 unsigned lock :1; /* Supports locking the volume */
706 unsigned locked :1; /* The volume is locked */
707 unsigned prevent :1; /* The device defaults in the prevent state after power up */
708 unsigned eject :1; /* The device can eject the volume */
709 __u8 disconnect :1; /* The device can break request > ctl */
710 __u8 reserved6_5 :1;
711 unsigned ecc :1; /* Supports error correction */
712 unsigned cmprs :1; /* Supports data compression */
713 unsigned reserved7_0 :1;
714 unsigned blk512 :1; /* Supports 512 bytes block size */
715 unsigned blk1024 :1; /* Supports 1024 bytes block size */
716 unsigned reserved7_3_6 :4;
717 unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
718 /* transfers for slow buffer memory ??? */
719 /* Also 32768 block size in some cases */
720 __u16 max_speed; /* Maximum speed supported in KBps */
721 __u8 reserved10, reserved11;
722 __u16 ctl; /* Continuous Transfer Limit in blocks */
723 __u16 speed; /* Current Speed, in KBps */
724 __u16 buffer_size; /* Buffer Size, in 512 bytes */
725 __u8 reserved18, reserved19;
726} idetape_capabilities_page_t;
727
728/*
729 * Block Size Page
730 */
731typedef struct {
732 unsigned page_code :6; /* Page code - Should be 0x30 */
733 unsigned reserved1_6 :1;
734 unsigned ps :1;
735 __u8 page_length; /* Page Length - Should be 2 */
736 __u8 reserved2;
737 unsigned play32 :1;
738 unsigned play32_5 :1;
739 unsigned reserved2_23 :2;
740 unsigned record32 :1;
741 unsigned record32_5 :1;
742 unsigned reserved2_6 :1;
743 unsigned one :1;
744} idetape_block_size_page_t;
745
746/*
747 * A pipeline stage.
748 */
749typedef struct idetape_stage_s {
750 struct request rq; /* The corresponding request */
751 struct idetape_bh *bh; /* The data buffers */
752 struct idetape_stage_s *next; /* Pointer to the next stage */
753} idetape_stage_t;
754
755/*
756 * REQUEST SENSE packet command result - Data Format.
757 */
758typedef struct {
759 unsigned error_code :7; /* Current of deferred errors */
760 unsigned valid :1; /* The information field conforms to QIC-157C */
761 __u8 reserved1 :8; /* Segment Number - Reserved */
762 unsigned sense_key :4; /* Sense Key */
763 unsigned reserved2_4 :1; /* Reserved */
764 unsigned ili :1; /* Incorrect Length Indicator */
765 unsigned eom :1; /* End Of Medium */
766 unsigned filemark :1; /* Filemark */
767 __u32 information __attribute__ ((packed));
768 __u8 asl; /* Additional sense length (n-7) */
769 __u32 command_specific; /* Additional command specific information */
770 __u8 asc; /* Additional Sense Code */
771 __u8 ascq; /* Additional Sense Code Qualifier */
772 __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
773 unsigned sk_specific1 :7; /* Sense Key Specific */
774 unsigned sksv :1; /* Sense Key Specific information is valid */
775 __u8 sk_specific2; /* Sense Key Specific */
776 __u8 sk_specific3; /* Sense Key Specific */
777 __u8 pad[2]; /* Padding to 20 bytes */
778} idetape_request_sense_result_t;
779
780
781/*
782 * Most of our global data which we need to save even as we leave the
783 * driver due to an interrupt or a timer event is stored in a variable
784 * of type idetape_tape_t, defined below.
785 */
786typedef struct ide_tape_obj {
787 ide_drive_t *drive;
788 ide_driver_t *driver;
789 struct gendisk *disk;
790 struct kref kref;
791
792 /*
793 * Since a typical character device operation requires more
794 * than one packet command, we provide here enough memory
795 * for the maximum of interconnected packet commands.
796 * The packet commands are stored in the circular array pc_stack.
797 * pc_stack_index points to the last used entry, and warps around
798 * to the start when we get to the last array entry.
799 *
800 * pc points to the current processed packet command.
801 *
802 * failed_pc points to the last failed packet command, or contains
803 * NULL if we do not need to retry any packet command. This is
804 * required since an additional packet command is needed before the
805 * retry, to get detailed information on what went wrong.
806 */
807 /* Current packet command */
808 idetape_pc_t *pc;
809 /* Last failed packet command */
810 idetape_pc_t *failed_pc;
811 /* Packet command stack */
812 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
813 /* Next free packet command storage space */
814 int pc_stack_index;
815 struct request rq_stack[IDETAPE_PC_STACK];
816 /* We implement a circular array */
817 int rq_stack_index;
818
819 /*
820 * DSC polling variables.
821 *
822 * While polling for DSC we use postponed_rq to postpone the
823 * current request so that ide.c will be able to service
824 * pending requests on the other device. Note that at most
825 * we will have only one DSC (usually data transfer) request
826 * in the device request queue. Additional requests can be
827 * queued in our internal pipeline, but they will be visible
828 * to ide.c only one at a time.
829 */
830 struct request *postponed_rq;
831 /* The time in which we started polling for DSC */
832 unsigned long dsc_polling_start;
833 /* Timer used to poll for dsc */
834 struct timer_list dsc_timer;
835 /* Read/Write dsc polling frequency */
836 unsigned long best_dsc_rw_frequency;
837 /* The current polling frequency */
838 unsigned long dsc_polling_frequency;
839 /* Maximum waiting time */
840 unsigned long dsc_timeout;
841
842 /*
843 * Read position information
844 */
845 u8 partition;
846 /* Current block */
847 unsigned int first_frame_position;
848 unsigned int last_frame_position;
849 unsigned int blocks_in_buffer;
850
851 /*
852 * Last error information
853 */
854 u8 sense_key, asc, ascq;
855
856 /*
857 * Character device operation
858 */
859 unsigned int minor;
860 /* device name */
861 char name[4];
862 /* Current character device data transfer direction */
863 idetape_chrdev_direction_t chrdev_direction;
864
865 /*
866 * Device information
867 */
868 /* Usually 512 or 1024 bytes */
869 unsigned short tape_block_size;
870 int user_bs_factor;
871 /* Copy of the tape's Capabilities and Mechanical Page */
872 idetape_capabilities_page_t capabilities;
873
874 /*
875 * Active data transfer request parameters.
876 *
877 * At most, there is only one ide-tape originated data transfer
878 * request in the device request queue. This allows ide.c to
879 * easily service requests from the other device when we
880 * postpone our active request. In the pipelined operation
881 * mode, we use our internal pipeline structure to hold
882 * more data requests.
883 *
884 * The data buffer size is chosen based on the tape's
885 * recommendation.
886 */
887 /* Pointer to the request which is waiting in the device request queue */
888 struct request *active_data_request;
889 /* Data buffer size (chosen based on the tape's recommendation */
890 int stage_size;
891 idetape_stage_t *merge_stage;
892 int merge_stage_size;
893 struct idetape_bh *bh;
894 char *b_data;
895 int b_count;
896
897 /*
898 * Pipeline parameters.
899 *
900 * To accomplish non-pipelined mode, we simply set the following
901 * variables to zero (or NULL, where appropriate).
902 */
903 /* Number of currently used stages */
904 int nr_stages;
905 /* Number of pending stages */
906 int nr_pending_stages;
907 /* We will not allocate more than this number of stages */
908 int max_stages, min_pipeline, max_pipeline;
909 /* The first stage which will be removed from the pipeline */
910 idetape_stage_t *first_stage;
911 /* The currently active stage */
912 idetape_stage_t *active_stage;
913 /* Will be serviced after the currently active request */
914 idetape_stage_t *next_stage;
915 /* New requests will be added to the pipeline here */
916 idetape_stage_t *last_stage;
917 /* Optional free stage which we can use */
918 idetape_stage_t *cache_stage;
919 int pages_per_stage;
920 /* Wasted space in each stage */
921 int excess_bh_size;
922
923 /* Status/Action flags: long for set_bit */
924 unsigned long flags;
925 /* protects the ide-tape queue */
926 spinlock_t spinlock;
927
928 /*
929 * Measures average tape speed
930 */
931 unsigned long avg_time;
932 int avg_size;
933 int avg_speed;
934
935 /* last sense information */
936 idetape_request_sense_result_t sense;
937
938 char vendor_id[10];
939 char product_id[18];
940 char firmware_revision[6];
941 int firmware_revision_num;
942
943 /* the door is currently locked */
944 int door_locked;
945 /* the tape hardware is write protected */
946 char drv_write_prot;
947 /* the tape is write protected (hardware or opened as read-only) */
948 char write_prot;
949
950 /*
951 * Limit the number of times a request can
952 * be postponed, to avoid an infinite postpone
953 * deadlock.
954 */
955 /* request postpone count limit */
956 int postpone_cnt;
957
958 /*
959 * Measures number of frames:
960 *
961 * 1. written/read to/from the driver pipeline (pipeline_head).
962 * 2. written/read to/from the tape buffers (idetape_bh).
963 * 3. written/read by the tape to/from the media (tape_head).
964 */
965 int pipeline_head;
966 int buffer_head;
967 int tape_head;
968 int last_tape_head;
969
970 /*
971 * Speed control at the tape buffers input/output
972 */
973 unsigned long insert_time;
974 int insert_size;
975 int insert_speed;
976 int max_insert_speed;
977 int measure_insert_time;
978
979 /*
980 * Measure tape still time, in milliseconds
981 */
982 unsigned long tape_still_time_begin;
983 int tape_still_time;
984
985 /*
986 * Speed regulation negative feedback loop
987 */
988 int speed_control;
989 int pipeline_head_speed;
990 int controlled_pipeline_head_speed;
991 int uncontrolled_pipeline_head_speed;
992 int controlled_last_pipeline_head;
993 int uncontrolled_last_pipeline_head;
994 unsigned long uncontrolled_pipeline_head_time;
995 unsigned long controlled_pipeline_head_time;
996 int controlled_previous_pipeline_head;
997 int uncontrolled_previous_pipeline_head;
998 unsigned long controlled_previous_head_time;
999 unsigned long uncontrolled_previous_head_time;
1000 int restart_speed_control_req;
1001
1002 /*
1003 * Debug_level determines amount of debugging output;
1004 * can be changed using /proc/ide/hdx/settings
1005 * 0 : almost no debugging output
1006 * 1 : 0+output errors only
1007 * 2 : 1+output all sensekey/asc
1008 * 3 : 2+follow all chrdev related procedures
1009 * 4 : 3+follow all procedures
1010 * 5 : 4+include pc_stack rq_stack info
1011 * 6 : 5+USE_COUNT updates
1012 */
1013 int debug_level;
1014} idetape_tape_t;
1015
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001016static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Will Dysond5dee802005-09-16 02:55:07 -07001018static struct class *idetape_sysfs_class;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
1021
1022#define ide_tape_g(disk) \
1023 container_of((disk)->private_data, struct ide_tape_obj, driver)
1024
1025static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
1026{
1027 struct ide_tape_obj *tape = NULL;
1028
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001029 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 tape = ide_tape_g(disk);
1031 if (tape)
1032 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001033 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return tape;
1035}
1036
1037static void ide_tape_release(struct kref *);
1038
1039static void ide_tape_put(struct ide_tape_obj *tape)
1040{
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001041 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001043 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046/*
1047 * Tape door status
1048 */
1049#define DOOR_UNLOCKED 0
1050#define DOOR_LOCKED 1
1051#define DOOR_EXPLICITLY_LOCKED 2
1052
1053/*
1054 * Tape flag bits values.
1055 */
1056#define IDETAPE_IGNORE_DSC 0
1057#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
1058#define IDETAPE_BUSY 2 /* Device already opened */
1059#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
1060#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
1061#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
1062#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
1063#define IDETAPE_READ_ERROR 7
1064#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
1065/* 0 = no tape is loaded, so we don't rewind after ejecting */
1066#define IDETAPE_MEDIUM_PRESENT 9
1067
1068/*
1069 * Supported ATAPI tape drives packet commands
1070 */
1071#define IDETAPE_TEST_UNIT_READY_CMD 0x00
1072#define IDETAPE_REWIND_CMD 0x01
1073#define IDETAPE_REQUEST_SENSE_CMD 0x03
1074#define IDETAPE_READ_CMD 0x08
1075#define IDETAPE_WRITE_CMD 0x0a
1076#define IDETAPE_WRITE_FILEMARK_CMD 0x10
1077#define IDETAPE_SPACE_CMD 0x11
1078#define IDETAPE_INQUIRY_CMD 0x12
1079#define IDETAPE_ERASE_CMD 0x19
1080#define IDETAPE_MODE_SENSE_CMD 0x1a
1081#define IDETAPE_MODE_SELECT_CMD 0x15
1082#define IDETAPE_LOAD_UNLOAD_CMD 0x1b
1083#define IDETAPE_PREVENT_CMD 0x1e
1084#define IDETAPE_LOCATE_CMD 0x2b
1085#define IDETAPE_READ_POSITION_CMD 0x34
1086#define IDETAPE_READ_BUFFER_CMD 0x3c
1087#define IDETAPE_SET_SPEED_CMD 0xbb
1088
1089/*
1090 * Some defines for the READ BUFFER command
1091 */
1092#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
1093
1094/*
1095 * Some defines for the SPACE command
1096 */
1097#define IDETAPE_SPACE_OVER_FILEMARK 1
1098#define IDETAPE_SPACE_TO_EOD 3
1099
1100/*
1101 * Some defines for the LOAD UNLOAD command
1102 */
1103#define IDETAPE_LU_LOAD_MASK 1
1104#define IDETAPE_LU_RETENSION_MASK 2
1105#define IDETAPE_LU_EOT_MASK 4
1106
1107/*
1108 * Special requests for our block device strategy routine.
1109 *
1110 * In order to service a character device command, we add special
1111 * requests to the tail of our block device request queue and wait
1112 * for their completion.
1113 */
1114
1115enum {
1116 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
1117 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
1118 REQ_IDETAPE_READ = (1 << 2),
1119 REQ_IDETAPE_WRITE = (1 << 3),
1120 REQ_IDETAPE_READ_BUFFER = (1 << 4),
1121};
1122
1123/*
1124 * Error codes which are returned in rq->errors to the higher part
1125 * of the driver.
1126 */
1127#define IDETAPE_ERROR_GENERAL 101
1128#define IDETAPE_ERROR_FILEMARK 102
1129#define IDETAPE_ERROR_EOD 103
1130
1131/*
1132 * The following is used to format the general configuration word of
1133 * the ATAPI IDENTIFY DEVICE command.
1134 */
1135struct idetape_id_gcw {
1136 unsigned packet_size :2; /* Packet Size */
1137 unsigned reserved234 :3; /* Reserved */
1138 unsigned drq_type :2; /* Command packet DRQ type */
1139 unsigned removable :1; /* Removable media */
1140 unsigned device_type :5; /* Device type */
1141 unsigned reserved13 :1; /* Reserved */
1142 unsigned protocol :2; /* Protocol type */
1143};
1144
1145/*
1146 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
1147 */
1148typedef struct {
1149 unsigned device_type :5; /* Peripheral Device Type */
1150 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
1151 unsigned reserved1_6t0 :7; /* Reserved */
1152 unsigned rmb :1; /* Removable Medium Bit */
1153 unsigned ansi_version :3; /* ANSI Version */
1154 unsigned ecma_version :3; /* ECMA Version */
1155 unsigned iso_version :2; /* ISO Version */
1156 unsigned response_format :4; /* Response Data Format */
1157 unsigned reserved3_45 :2; /* Reserved */
1158 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
1159 unsigned reserved3_7 :1; /* AENC - Reserved */
1160 __u8 additional_length; /* Additional Length (total_length-4) */
1161 __u8 rsv5, rsv6, rsv7; /* Reserved */
1162 __u8 vendor_id[8]; /* Vendor Identification */
1163 __u8 product_id[16]; /* Product Identification */
1164 __u8 revision_level[4]; /* Revision Level */
1165 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
1166 __u8 reserved56t95[40]; /* Reserved - Optional */
1167 /* Additional information may be returned */
1168} idetape_inquiry_result_t;
1169
1170/*
1171 * READ POSITION packet command - Data Format (From Table 6-57)
1172 */
1173typedef struct {
1174 unsigned reserved0_10 :2; /* Reserved */
1175 unsigned bpu :1; /* Block Position Unknown */
1176 unsigned reserved0_543 :3; /* Reserved */
1177 unsigned eop :1; /* End Of Partition */
1178 unsigned bop :1; /* Beginning Of Partition */
1179 u8 partition; /* Partition Number */
1180 u8 reserved2, reserved3; /* Reserved */
1181 u32 first_block; /* First Block Location */
1182 u32 last_block; /* Last Block Location (Optional) */
1183 u8 reserved12; /* Reserved */
1184 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
1185 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
1186} idetape_read_position_result_t;
1187
1188/*
1189 * Follows structures which are related to the SELECT SENSE / MODE SENSE
1190 * packet commands. Those packet commands are still not supported
1191 * by ide-tape.
1192 */
1193#define IDETAPE_BLOCK_DESCRIPTOR 0
1194#define IDETAPE_CAPABILITIES_PAGE 0x2a
1195#define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
1196#define IDETAPE_BLOCK_SIZE_PAGE 0x30
1197#define IDETAPE_BUFFER_FILLING_PAGE 0x33
1198
1199/*
1200 * Mode Parameter Header for the MODE SENSE packet command
1201 */
1202typedef struct {
1203 __u8 mode_data_length; /* Length of the following data transfer */
1204 __u8 medium_type; /* Medium Type */
1205 __u8 dsp; /* Device Specific Parameter */
1206 __u8 bdl; /* Block Descriptor Length */
1207#if 0
1208 /* data transfer page */
1209 __u8 page_code :6;
1210 __u8 reserved0_6 :1;
1211 __u8 ps :1; /* parameters saveable */
1212 __u8 page_length; /* page Length == 0x02 */
1213 __u8 reserved2;
1214 __u8 read32k :1; /* 32k blk size (data only) */
1215 __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
1216 __u8 reserved3_23 :2;
1217 __u8 write32k :1; /* 32k blk size (data only) */
1218 __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
1219 __u8 reserved3_6 :1;
1220 __u8 streaming :1; /* streaming mode enable */
1221#endif
1222} idetape_mode_parameter_header_t;
1223
1224/*
1225 * Mode Parameter Block Descriptor the MODE SENSE packet command
1226 *
1227 * Support for block descriptors is optional.
1228 */
1229typedef struct {
1230 __u8 density_code; /* Medium density code */
1231 __u8 blocks[3]; /* Number of blocks */
1232 __u8 reserved4; /* Reserved */
1233 __u8 length[3]; /* Block Length */
1234} idetape_parameter_block_descriptor_t;
1235
1236/*
1237 * The Data Compression Page, as returned by the MODE SENSE packet command.
1238 */
1239typedef struct {
1240 unsigned page_code :6; /* Page Code - Should be 0xf */
1241 unsigned reserved0 :1; /* Reserved */
1242 unsigned ps :1;
1243 __u8 page_length; /* Page Length - Should be 14 */
1244 unsigned reserved2 :6; /* Reserved */
1245 unsigned dcc :1; /* Data Compression Capable */
1246 unsigned dce :1; /* Data Compression Enable */
1247 unsigned reserved3 :5; /* Reserved */
1248 unsigned red :2; /* Report Exception on Decompression */
1249 unsigned dde :1; /* Data Decompression Enable */
1250 __u32 ca; /* Compression Algorithm */
1251 __u32 da; /* Decompression Algorithm */
1252 __u8 reserved[4]; /* Reserved */
1253} idetape_data_compression_page_t;
1254
1255/*
1256 * The Medium Partition Page, as returned by the MODE SENSE packet command.
1257 */
1258typedef struct {
1259 unsigned page_code :6; /* Page Code - Should be 0x11 */
1260 unsigned reserved1_6 :1; /* Reserved */
1261 unsigned ps :1;
1262 __u8 page_length; /* Page Length - Should be 6 */
1263 __u8 map; /* Maximum Additional Partitions - Should be 0 */
1264 __u8 apd; /* Additional Partitions Defined - Should be 0 */
1265 unsigned reserved4_012 :3; /* Reserved */
1266 unsigned psum :2; /* Should be 0 */
1267 unsigned idp :1; /* Should be 0 */
1268 unsigned sdp :1; /* Should be 0 */
1269 unsigned fdp :1; /* Fixed Data Partitions */
1270 __u8 mfr; /* Medium Format Recognition */
1271 __u8 reserved[2]; /* Reserved */
1272} idetape_medium_partition_page_t;
1273
1274/*
1275 * Run time configurable parameters.
1276 */
1277typedef struct {
1278 int dsc_rw_frequency;
1279 int dsc_media_access_frequency;
1280 int nr_stages;
1281} idetape_config_t;
1282
1283/*
1284 * The variables below are used for the character device interface.
1285 * Additional state variables are defined in our ide_drive_t structure.
1286 */
1287static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
1288
1289#define ide_tape_f(file) ((file)->private_data)
1290
1291static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
1292{
1293 struct ide_tape_obj *tape = NULL;
1294
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001295 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tape = idetape_devs[i];
1297 if (tape)
1298 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001299 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return tape;
1301}
1302
1303/*
1304 * Function declarations
1305 *
1306 */
1307static int idetape_chrdev_release (struct inode *inode, struct file *filp);
1308static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
1309
1310/*
1311 * Too bad. The drive wants to send us data which we are not ready to accept.
1312 * Just throw it away.
1313 */
1314static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1315{
1316 while (bcount--)
1317 (void) HWIF(drive)->INB(IDE_DATA_REG);
1318}
1319
1320static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1321{
1322 struct idetape_bh *bh = pc->bh;
1323 int count;
1324
1325 while (bcount) {
1326#if IDETAPE_DEBUG_BUGS
1327 if (bh == NULL) {
1328 printk(KERN_ERR "ide-tape: bh == NULL in "
1329 "idetape_input_buffers\n");
1330 idetape_discard_data(drive, bcount);
1331 return;
1332 }
1333#endif /* IDETAPE_DEBUG_BUGS */
1334 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
1335 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
1336 bcount -= count;
1337 atomic_add(count, &bh->b_count);
1338 if (atomic_read(&bh->b_count) == bh->b_size) {
1339 bh = bh->b_reqnext;
1340 if (bh)
1341 atomic_set(&bh->b_count, 0);
1342 }
1343 }
1344 pc->bh = bh;
1345}
1346
1347static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1348{
1349 struct idetape_bh *bh = pc->bh;
1350 int count;
1351
1352 while (bcount) {
1353#if IDETAPE_DEBUG_BUGS
1354 if (bh == NULL) {
1355 printk(KERN_ERR "ide-tape: bh == NULL in "
1356 "idetape_output_buffers\n");
1357 return;
1358 }
1359#endif /* IDETAPE_DEBUG_BUGS */
1360 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
1361 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
1362 bcount -= count;
1363 pc->b_data += count;
1364 pc->b_count -= count;
1365 if (!pc->b_count) {
1366 pc->bh = bh = bh->b_reqnext;
1367 if (bh) {
1368 pc->b_data = bh->b_data;
1369 pc->b_count = atomic_read(&bh->b_count);
1370 }
1371 }
1372 }
1373}
1374
1375static void idetape_update_buffers (idetape_pc_t *pc)
1376{
1377 struct idetape_bh *bh = pc->bh;
1378 int count;
1379 unsigned int bcount = pc->actually_transferred;
1380
1381 if (test_bit(PC_WRITING, &pc->flags))
1382 return;
1383 while (bcount) {
1384#if IDETAPE_DEBUG_BUGS
1385 if (bh == NULL) {
1386 printk(KERN_ERR "ide-tape: bh == NULL in "
1387 "idetape_update_buffers\n");
1388 return;
1389 }
1390#endif /* IDETAPE_DEBUG_BUGS */
1391 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
1392 atomic_set(&bh->b_count, count);
1393 if (atomic_read(&bh->b_count) == bh->b_size)
1394 bh = bh->b_reqnext;
1395 bcount -= count;
1396 }
1397 pc->bh = bh;
1398}
1399
1400/*
1401 * idetape_next_pc_storage returns a pointer to a place in which we can
1402 * safely store a packet command, even though we intend to leave the
1403 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1404 * commands is allocated at initialization time.
1405 */
1406static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1407{
1408 idetape_tape_t *tape = drive->driver_data;
1409
1410#if IDETAPE_DEBUG_LOG
1411 if (tape->debug_level >= 5)
1412 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
1413 tape->pc_stack_index);
1414#endif /* IDETAPE_DEBUG_LOG */
1415 if (tape->pc_stack_index == IDETAPE_PC_STACK)
1416 tape->pc_stack_index=0;
1417 return (&tape->pc_stack[tape->pc_stack_index++]);
1418}
1419
1420/*
1421 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1422 * Since we queue packet commands in the request queue, we need to
1423 * allocate a request, along with the allocation of a packet command.
1424 */
1425
1426/**************************************************************
1427 * *
1428 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
1429 * followed later on by kfree(). -ml *
1430 * *
1431 **************************************************************/
1432
1433static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1434{
1435 idetape_tape_t *tape = drive->driver_data;
1436
1437#if IDETAPE_DEBUG_LOG
1438 if (tape->debug_level >= 5)
1439 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
1440 tape->rq_stack_index);
1441#endif /* IDETAPE_DEBUG_LOG */
1442 if (tape->rq_stack_index == IDETAPE_PC_STACK)
1443 tape->rq_stack_index=0;
1444 return (&tape->rq_stack[tape->rq_stack_index++]);
1445}
1446
1447/*
1448 * idetape_init_pc initializes a packet command.
1449 */
1450static void idetape_init_pc (idetape_pc_t *pc)
1451{
1452 memset(pc->c, 0, 12);
1453 pc->retries = 0;
1454 pc->flags = 0;
1455 pc->request_transfer = 0;
1456 pc->buffer = pc->pc_buffer;
1457 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1458 pc->bh = NULL;
1459 pc->b_data = NULL;
1460}
1461
1462/*
1463 * idetape_analyze_error is called on each failed packet command retry
1464 * to analyze the request sense. We currently do not utilize this
1465 * information.
1466 */
1467static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
1468{
1469 idetape_tape_t *tape = drive->driver_data;
1470 idetape_pc_t *pc = tape->failed_pc;
1471
1472 tape->sense = *result;
1473 tape->sense_key = result->sense_key;
1474 tape->asc = result->asc;
1475 tape->ascq = result->ascq;
1476#if IDETAPE_DEBUG_LOG
1477 /*
1478 * Without debugging, we only log an error if we decided to
1479 * give up retrying.
1480 */
1481 if (tape->debug_level >= 1)
1482 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1483 "asc = %x, ascq = %x\n",
1484 pc->c[0], result->sense_key,
1485 result->asc, result->ascq);
1486#endif /* IDETAPE_DEBUG_LOG */
1487
1488 /*
1489 * Correct pc->actually_transferred by asking the tape.
1490 */
1491 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1492 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information));
1493 idetape_update_buffers(pc);
1494 }
1495
1496 /*
1497 * If error was the result of a zero-length read or write command,
1498 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1499 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1500 */
1501 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1502 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { /* length==0 */
1503 if (result->sense_key == 5) {
1504 /* don't report an error, everything's ok */
1505 pc->error = 0;
1506 /* don't retry read/write */
1507 set_bit(PC_ABORT, &pc->flags);
1508 }
1509 }
1510 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1511 pc->error = IDETAPE_ERROR_FILEMARK;
1512 set_bit(PC_ABORT, &pc->flags);
1513 }
1514 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1515 if (result->eom ||
1516 (result->sense_key == 0xd && result->asc == 0x0 &&
1517 result->ascq == 0x2)) {
1518 pc->error = IDETAPE_ERROR_EOD;
1519 set_bit(PC_ABORT, &pc->flags);
1520 }
1521 }
1522 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1523 if (result->sense_key == 8) {
1524 pc->error = IDETAPE_ERROR_EOD;
1525 set_bit(PC_ABORT, &pc->flags);
1526 }
1527 if (!test_bit(PC_ABORT, &pc->flags) &&
1528 pc->actually_transferred)
1529 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1530 }
1531}
1532
1533/*
1534 * idetape_active_next_stage will declare the next stage as "active".
1535 */
1536static void idetape_active_next_stage (ide_drive_t *drive)
1537{
1538 idetape_tape_t *tape = drive->driver_data;
1539 idetape_stage_t *stage = tape->next_stage;
1540 struct request *rq = &stage->rq;
1541
1542#if IDETAPE_DEBUG_LOG
1543 if (tape->debug_level >= 4)
1544 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1545#endif /* IDETAPE_DEBUG_LOG */
1546#if IDETAPE_DEBUG_BUGS
1547 if (stage == NULL) {
1548 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1549 return;
1550 }
1551#endif /* IDETAPE_DEBUG_BUGS */
1552
1553 rq->rq_disk = tape->disk;
1554 rq->buffer = NULL;
1555 rq->special = (void *)stage->bh;
1556 tape->active_data_request = rq;
1557 tape->active_stage = stage;
1558 tape->next_stage = stage->next;
1559}
1560
1561/*
1562 * idetape_increase_max_pipeline_stages is a part of the feedback
1563 * loop which tries to find the optimum number of stages. In the
1564 * feedback loop, we are starting from a minimum maximum number of
1565 * stages, and if we sense that the pipeline is empty, we try to
1566 * increase it, until we reach the user compile time memory limit.
1567 */
1568static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1569{
1570 idetape_tape_t *tape = drive->driver_data;
1571 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1572
1573#if IDETAPE_DEBUG_LOG
1574 if (tape->debug_level >= 4)
1575 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1576#endif /* IDETAPE_DEBUG_LOG */
1577
1578 tape->max_stages += max(increase, 1);
1579 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1580 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1581}
1582
1583/*
1584 * idetape_kfree_stage calls kfree to completely free a stage, along with
1585 * its related buffers.
1586 */
1587static void __idetape_kfree_stage (idetape_stage_t *stage)
1588{
1589 struct idetape_bh *prev_bh, *bh = stage->bh;
1590 int size;
1591
1592 while (bh != NULL) {
1593 if (bh->b_data != NULL) {
1594 size = (int) bh->b_size;
1595 while (size > 0) {
1596 free_page((unsigned long) bh->b_data);
1597 size -= PAGE_SIZE;
1598 bh->b_data += PAGE_SIZE;
1599 }
1600 }
1601 prev_bh = bh;
1602 bh = bh->b_reqnext;
1603 kfree(prev_bh);
1604 }
1605 kfree(stage);
1606}
1607
1608static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1609{
1610 __idetape_kfree_stage(stage);
1611}
1612
1613/*
1614 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1615 * The caller should avoid race conditions.
1616 */
1617static void idetape_remove_stage_head (ide_drive_t *drive)
1618{
1619 idetape_tape_t *tape = drive->driver_data;
1620 idetape_stage_t *stage;
1621
1622#if IDETAPE_DEBUG_LOG
1623 if (tape->debug_level >= 4)
1624 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1625#endif /* IDETAPE_DEBUG_LOG */
1626#if IDETAPE_DEBUG_BUGS
1627 if (tape->first_stage == NULL) {
1628 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1629 return;
1630 }
1631 if (tape->active_stage == tape->first_stage) {
1632 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1633 return;
1634 }
1635#endif /* IDETAPE_DEBUG_BUGS */
1636 stage = tape->first_stage;
1637 tape->first_stage = stage->next;
1638 idetape_kfree_stage(tape, stage);
1639 tape->nr_stages--;
1640 if (tape->first_stage == NULL) {
1641 tape->last_stage = NULL;
1642#if IDETAPE_DEBUG_BUGS
1643 if (tape->next_stage != NULL)
1644 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1645 if (tape->nr_stages)
1646 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1647#endif /* IDETAPE_DEBUG_BUGS */
1648 }
1649}
1650
1651/*
1652 * This will free all the pipeline stages starting from new_last_stage->next
1653 * to the end of the list, and point tape->last_stage to new_last_stage.
1654 */
1655static void idetape_abort_pipeline(ide_drive_t *drive,
1656 idetape_stage_t *new_last_stage)
1657{
1658 idetape_tape_t *tape = drive->driver_data;
1659 idetape_stage_t *stage = new_last_stage->next;
1660 idetape_stage_t *nstage;
1661
1662#if IDETAPE_DEBUG_LOG
1663 if (tape->debug_level >= 4)
1664 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1665#endif
1666 while (stage) {
1667 nstage = stage->next;
1668 idetape_kfree_stage(tape, stage);
1669 --tape->nr_stages;
1670 --tape->nr_pending_stages;
1671 stage = nstage;
1672 }
1673 if (new_last_stage)
1674 new_last_stage->next = NULL;
1675 tape->last_stage = new_last_stage;
1676 tape->next_stage = NULL;
1677}
1678
1679/*
1680 * idetape_end_request is used to finish servicing a request, and to
1681 * insert a pending pipeline request into the main device queue.
1682 */
1683static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1684{
1685 struct request *rq = HWGROUP(drive)->rq;
1686 idetape_tape_t *tape = drive->driver_data;
1687 unsigned long flags;
1688 int error;
1689 int remove_stage = 0;
1690 idetape_stage_t *active_stage;
1691
1692#if IDETAPE_DEBUG_LOG
1693 if (tape->debug_level >= 4)
1694 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1695#endif /* IDETAPE_DEBUG_LOG */
1696
1697 switch (uptodate) {
1698 case 0: error = IDETAPE_ERROR_GENERAL; break;
1699 case 1: error = 0; break;
1700 default: error = uptodate;
1701 }
1702 rq->errors = error;
1703 if (error)
1704 tape->failed_pc = NULL;
1705
1706 spin_lock_irqsave(&tape->spinlock, flags);
1707
1708 /* The request was a pipelined data transfer request */
1709 if (tape->active_data_request == rq) {
1710 active_stage = tape->active_stage;
1711 tape->active_stage = NULL;
1712 tape->active_data_request = NULL;
1713 tape->nr_pending_stages--;
1714 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1715 remove_stage = 1;
1716 if (error) {
1717 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1718 if (error == IDETAPE_ERROR_EOD)
1719 idetape_abort_pipeline(drive, active_stage);
1720 }
1721 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1722 if (error == IDETAPE_ERROR_EOD) {
1723 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1724 idetape_abort_pipeline(drive, active_stage);
1725 }
1726 }
1727 if (tape->next_stage != NULL) {
1728 idetape_active_next_stage(drive);
1729
1730 /*
1731 * Insert the next request into the request queue.
1732 */
1733 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1734 } else if (!error) {
1735 idetape_increase_max_pipeline_stages(drive);
1736 }
1737 }
1738 ide_end_drive_cmd(drive, 0, 0);
1739// blkdev_dequeue_request(rq);
1740// drive->rq = NULL;
1741// end_that_request_last(rq);
1742
1743 if (remove_stage)
1744 idetape_remove_stage_head(drive);
1745 if (tape->active_data_request == NULL)
1746 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1747 spin_unlock_irqrestore(&tape->spinlock, flags);
1748 return 0;
1749}
1750
1751static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1752{
1753 idetape_tape_t *tape = drive->driver_data;
1754
1755#if IDETAPE_DEBUG_LOG
1756 if (tape->debug_level >= 4)
1757 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1758#endif /* IDETAPE_DEBUG_LOG */
1759 if (!tape->pc->error) {
1760 idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
1761 idetape_end_request(drive, 1, 0);
1762 } else {
1763 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1764 idetape_end_request(drive, 0, 0);
1765 }
1766 return ide_stopped;
1767}
1768
1769static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1770{
1771 idetape_init_pc(pc);
1772 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1773 pc->c[4] = 20;
1774 pc->request_transfer = 20;
1775 pc->callback = &idetape_request_sense_callback;
1776}
1777
1778static void idetape_init_rq(struct request *rq, u8 cmd)
1779{
1780 memset(rq, 0, sizeof(*rq));
1781 rq->flags = REQ_SPECIAL;
1782 rq->cmd[0] = cmd;
1783}
1784
1785/*
1786 * idetape_queue_pc_head generates a new packet command request in front
1787 * of the request queue, before the current request, so that it will be
1788 * processed immediately, on the next pass through the driver.
1789 *
1790 * idetape_queue_pc_head is called from the request handling part of
1791 * the driver (the "bottom" part). Safe storage for the request should
1792 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1793 * before calling idetape_queue_pc_head.
1794 *
1795 * Memory for those requests is pre-allocated at initialization time, and
1796 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1797 * space for the maximum possible number of inter-dependent packet commands.
1798 *
1799 * The higher level of the driver - The ioctl handler and the character
1800 * device handling functions should queue request to the lower level part
1801 * and wait for their completion using idetape_queue_pc_tail or
1802 * idetape_queue_rw_tail.
1803 */
1804static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1805{
1806 struct ide_tape_obj *tape = drive->driver_data;
1807
1808 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1809 rq->buffer = (char *) pc;
1810 rq->rq_disk = tape->disk;
1811 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1812}
1813
1814/*
1815 * idetape_retry_pc is called when an error was detected during the
1816 * last packet command. We queue a request sense packet command in
1817 * the head of the request list.
1818 */
1819static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1820{
1821 idetape_tape_t *tape = drive->driver_data;
1822 idetape_pc_t *pc;
1823 struct request *rq;
1824 atapi_error_t error;
1825
1826 error.all = HWIF(drive)->INB(IDE_ERROR_REG);
1827 pc = idetape_next_pc_storage(drive);
1828 rq = idetape_next_rq_storage(drive);
1829 idetape_create_request_sense_cmd(pc);
1830 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1831 idetape_queue_pc_head(drive, pc, rq);
1832 return ide_stopped;
1833}
1834
1835/*
1836 * idetape_postpone_request postpones the current request so that
1837 * ide.c will be able to service requests from another device on
1838 * the same hwgroup while we are polling for DSC.
1839 */
1840static void idetape_postpone_request (ide_drive_t *drive)
1841{
1842 idetape_tape_t *tape = drive->driver_data;
1843
1844#if IDETAPE_DEBUG_LOG
1845 if (tape->debug_level >= 4)
1846 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1847#endif
1848 tape->postponed_rq = HWGROUP(drive)->rq;
1849 ide_stall_queue(drive, tape->dsc_polling_frequency);
1850}
1851
1852/*
1853 * idetape_pc_intr is the usual interrupt handler which will be called
1854 * during a packet command. We will transfer some of the data (as
1855 * requested by the drive) and will re-point interrupt handler to us.
1856 * When data transfer is finished, we will act according to the
1857 * algorithm described before idetape_issue_packet_command.
1858 *
1859 */
1860static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1861{
1862 ide_hwif_t *hwif = drive->hwif;
1863 idetape_tape_t *tape = drive->driver_data;
1864 atapi_status_t status;
1865 atapi_bcount_t bcount;
1866 atapi_ireason_t ireason;
1867 idetape_pc_t *pc = tape->pc;
1868
1869 unsigned int temp;
1870#if SIMULATE_ERRORS
1871 static int error_sim_count = 0;
1872#endif
1873
1874#if IDETAPE_DEBUG_LOG
1875 if (tape->debug_level >= 4)
1876 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1877 "interrupt handler\n");
1878#endif /* IDETAPE_DEBUG_LOG */
1879
1880 /* Clear the interrupt */
1881 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
1882
1883 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1884 if (HWIF(drive)->ide_dma_end(drive) || status.b.check) {
1885 /*
1886 * A DMA error is sometimes expected. For example,
1887 * if the tape is crossing a filemark during a
1888 * READ command, it will issue an irq and position
1889 * itself before the filemark, so that only a partial
1890 * data transfer will occur (which causes the DMA
1891 * error). In that case, we will later ask the tape
1892 * how much bytes of the original request were
1893 * actually transferred (we can't receive that
1894 * information from the DMA engine on most chipsets).
1895 */
1896
1897 /*
1898 * On the contrary, a DMA error is never expected;
1899 * it usually indicates a hardware error or abort.
1900 * If the tape crosses a filemark during a READ
1901 * command, it will issue an irq and position itself
1902 * after the filemark (not before). Only a partial
1903 * data transfer will occur, but no DMA error.
1904 * (AS, 19 Apr 2001)
1905 */
1906 set_bit(PC_DMA_ERROR, &pc->flags);
1907 } else {
1908 pc->actually_transferred = pc->request_transfer;
1909 idetape_update_buffers(pc);
1910 }
1911#if IDETAPE_DEBUG_LOG
1912 if (tape->debug_level >= 4)
1913 printk(KERN_INFO "ide-tape: DMA finished\n");
1914#endif /* IDETAPE_DEBUG_LOG */
1915 }
1916
1917 /* No more interrupts */
1918 if (!status.b.drq) {
1919#if IDETAPE_DEBUG_LOG
1920 if (tape->debug_level >= 2)
1921 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1922#endif /* IDETAPE_DEBUG_LOG */
1923 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1924
1925 local_irq_enable();
1926
1927#if SIMULATE_ERRORS
1928 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1929 pc->c[0] == IDETAPE_READ_CMD) &&
1930 (++error_sim_count % 100) == 0) {
1931 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1932 tape->name);
1933 status.b.check = 1;
1934 }
1935#endif
1936 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1937 status.b.check = 0;
1938 if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) { /* Error detected */
1939#if IDETAPE_DEBUG_LOG
1940 if (tape->debug_level >= 1)
1941 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1942 tape->name);
1943#endif /* IDETAPE_DEBUG_LOG */
1944 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1945 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1946 return ide_do_reset(drive);
1947 }
1948#if IDETAPE_DEBUG_LOG
1949 if (tape->debug_level >= 1)
1950 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1951#endif
1952 /* Retry operation */
1953 return idetape_retry_pc(drive);
1954 }
1955 pc->error = 0;
1956 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1957 !status.b.dsc) {
1958 /* Media access command */
1959 tape->dsc_polling_start = jiffies;
1960 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1961 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1962 /* Allow ide.c to handle other requests */
1963 idetape_postpone_request(drive);
1964 return ide_stopped;
1965 }
1966 if (tape->failed_pc == pc)
1967 tape->failed_pc = NULL;
1968 /* Command finished - Call the callback function */
1969 return pc->callback(drive);
1970 }
1971 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1972 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1973 "interrupts in DMA mode\n");
1974 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1975 (void)__ide_dma_off(drive);
1976 return ide_do_reset(drive);
1977 }
1978 /* Get the number of bytes to transfer on this interrupt. */
1979 bcount.b.high = hwif->INB(IDE_BCOUNTH_REG);
1980 bcount.b.low = hwif->INB(IDE_BCOUNTL_REG);
1981
1982 ireason.all = hwif->INB(IDE_IREASON_REG);
1983
1984 if (ireason.b.cod) {
1985 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1986 return ide_do_reset(drive);
1987 }
1988 if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) {
1989 /* Hopefully, we will never get here */
1990 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1991 ireason.b.io ? "Write":"Read");
1992 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1993 ireason.b.io ? "Read":"Write");
1994 return ide_do_reset(drive);
1995 }
1996 if (!test_bit(PC_WRITING, &pc->flags)) {
1997 /* Reading - Check that we have enough space */
1998 temp = pc->actually_transferred + bcount.all;
1999 if (temp > pc->request_transfer) {
2000 if (temp > pc->buffer_size) {
2001 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
2002 idetape_discard_data(drive, bcount.all);
2003 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2004 return ide_started;
2005 }
2006#if IDETAPE_DEBUG_LOG
2007 if (tape->debug_level >= 2)
2008 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
2009#endif /* IDETAPE_DEBUG_LOG */
2010 }
2011 }
2012 if (test_bit(PC_WRITING, &pc->flags)) {
2013 if (pc->bh != NULL)
2014 idetape_output_buffers(drive, pc, bcount.all);
2015 else
2016 /* Write the current buffer */
2017 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
2018 } else {
2019 if (pc->bh != NULL)
2020 idetape_input_buffers(drive, pc, bcount.all);
2021 else
2022 /* Read the current buffer */
2023 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
2024 }
2025 /* Update the current position */
2026 pc->actually_transferred += bcount.all;
2027 pc->current_position += bcount.all;
2028#if IDETAPE_DEBUG_LOG
2029 if (tape->debug_level >= 2)
2030 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all);
2031#endif
2032 /* And set the interrupt handler again */
2033 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2034 return ide_started;
2035}
2036
2037/*
2038 * Packet Command Interface
2039 *
2040 * The current Packet Command is available in tape->pc, and will not
2041 * change until we finish handling it. Each packet command is associated
2042 * with a callback function that will be called when the command is
2043 * finished.
2044 *
2045 * The handling will be done in three stages:
2046 *
2047 * 1. idetape_issue_packet_command will send the packet command to the
2048 * drive, and will set the interrupt handler to idetape_pc_intr.
2049 *
2050 * 2. On each interrupt, idetape_pc_intr will be called. This step
2051 * will be repeated until the device signals us that no more
2052 * interrupts will be issued.
2053 *
2054 * 3. ATAPI Tape media access commands have immediate status with a
2055 * delayed process. In case of a successful initiation of a
2056 * media access packet command, the DSC bit will be set when the
2057 * actual execution of the command is finished.
2058 * Since the tape drive will not issue an interrupt, we have to
2059 * poll for this event. In this case, we define the request as
2060 * "low priority request" by setting rq_status to
2061 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
2062 * the driver.
2063 *
2064 * ide.c will then give higher priority to requests which
2065 * originate from the other device, until will change rq_status
2066 * to RQ_ACTIVE.
2067 *
2068 * 4. When the packet command is finished, it will be checked for errors.
2069 *
2070 * 5. In case an error was found, we queue a request sense packet
2071 * command in front of the request queue and retry the operation
2072 * up to IDETAPE_MAX_PC_RETRIES times.
2073 *
2074 * 6. In case no error was found, or we decided to give up and not
2075 * to retry again, the callback function will be called and then
2076 * we will handle the next request.
2077 *
2078 */
2079static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2080{
2081 ide_hwif_t *hwif = drive->hwif;
2082 idetape_tape_t *tape = drive->driver_data;
2083 idetape_pc_t *pc = tape->pc;
2084 atapi_ireason_t ireason;
2085 int retries = 100;
2086 ide_startstop_t startstop;
2087
2088 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2089 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
2090 return startstop;
2091 }
2092 ireason.all = hwif->INB(IDE_IREASON_REG);
2093 while (retries-- && (!ireason.b.cod || ireason.b.io)) {
2094 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
2095 "a packet command, retrying\n");
2096 udelay(100);
2097 ireason.all = hwif->INB(IDE_IREASON_REG);
2098 if (retries == 0) {
2099 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
2100 "issuing a packet command, ignoring\n");
2101 ireason.b.cod = 1;
2102 ireason.b.io = 0;
2103 }
2104 }
2105 if (!ireason.b.cod || ireason.b.io) {
2106 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
2107 "a packet command\n");
2108 return ide_do_reset(drive);
2109 }
2110 /* Set the interrupt routine */
2111 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2112#ifdef CONFIG_BLK_DEV_IDEDMA
2113 /* Begin DMA, if necessary */
2114 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
2115 hwif->dma_start(drive);
2116#endif
2117 /* Send the actual packet */
2118 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
2119 return ide_started;
2120}
2121
2122static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
2123{
2124 ide_hwif_t *hwif = drive->hwif;
2125 idetape_tape_t *tape = drive->driver_data;
2126 atapi_bcount_t bcount;
2127 int dma_ok = 0;
2128
2129#if IDETAPE_DEBUG_BUGS
2130 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
2131 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2132 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
2133 "Two request sense in serial were issued\n");
2134 }
2135#endif /* IDETAPE_DEBUG_BUGS */
2136
2137 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
2138 tape->failed_pc = pc;
2139 /* Set the current packet command */
2140 tape->pc = pc;
2141
2142 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
2143 test_bit(PC_ABORT, &pc->flags)) {
2144 /*
2145 * We will "abort" retrying a packet command in case
2146 * a legitimate error code was received (crossing a
2147 * filemark, or end of the media, for example).
2148 */
2149 if (!test_bit(PC_ABORT, &pc->flags)) {
2150 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
2151 tape->sense_key == 2 && tape->asc == 4 &&
2152 (tape->ascq == 1 || tape->ascq == 8))) {
2153 printk(KERN_ERR "ide-tape: %s: I/O error, "
2154 "pc = %2x, key = %2x, "
2155 "asc = %2x, ascq = %2x\n",
2156 tape->name, pc->c[0],
2157 tape->sense_key, tape->asc,
2158 tape->ascq);
2159 }
2160 /* Giving up */
2161 pc->error = IDETAPE_ERROR_GENERAL;
2162 }
2163 tape->failed_pc = NULL;
2164 return pc->callback(drive);
2165 }
2166#if IDETAPE_DEBUG_LOG
2167 if (tape->debug_level >= 2)
2168 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
2169#endif /* IDETAPE_DEBUG_LOG */
2170
2171 pc->retries++;
2172 /* We haven't transferred any data yet */
2173 pc->actually_transferred = 0;
2174 pc->current_position = pc->buffer;
2175 /* Request to transfer the entire buffer at once */
2176 bcount.all = pc->request_transfer;
2177
2178 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
2179 printk(KERN_WARNING "ide-tape: DMA disabled, "
2180 "reverting to PIO\n");
2181 (void)__ide_dma_off(drive);
2182 }
2183 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
2184 dma_ok = !hwif->dma_setup(drive);
2185
2186 if (IDE_CONTROL_REG)
2187 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
2188 hwif->OUTB(dma_ok ? 1 : 0, IDE_FEATURE_REG); /* Use PIO/DMA */
2189 hwif->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
2190 hwif->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
2191 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
2192 if (dma_ok) /* Will begin DMA later */
2193 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
2194 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
2195 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
2196 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2197 return ide_started;
2198 } else {
2199 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2200 return idetape_transfer_pc(drive);
2201 }
2202}
2203
2204/*
2205 * General packet command callback function.
2206 */
2207static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2208{
2209 idetape_tape_t *tape = drive->driver_data;
2210
2211#if IDETAPE_DEBUG_LOG
2212 if (tape->debug_level >= 4)
2213 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2214#endif /* IDETAPE_DEBUG_LOG */
2215
2216 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
2217 return ide_stopped;
2218}
2219
2220/*
2221 * A mode sense command is used to "sense" tape parameters.
2222 */
2223static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
2224{
2225 idetape_init_pc(pc);
2226 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2227 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
2228 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
2229 pc->c[2] = page_code;
2230 /*
2231 * Changed pc->c[3] to 0 (255 will at best return unused info).
2232 *
2233 * For SCSI this byte is defined as subpage instead of high byte
2234 * of length and some IDE drives seem to interpret it this way
2235 * and return an error when 255 is used.
2236 */
2237 pc->c[3] = 0;
2238 pc->c[4] = 255; /* (We will just discard data in that case) */
2239 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
2240 pc->request_transfer = 12;
2241 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
2242 pc->request_transfer = 24;
2243 else
2244 pc->request_transfer = 50;
2245 pc->callback = &idetape_pc_callback;
2246}
2247
2248static void calculate_speeds(ide_drive_t *drive)
2249{
2250 idetape_tape_t *tape = drive->driver_data;
2251 int full = 125, empty = 75;
2252
2253 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
2254 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
2255 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
2256 tape->controlled_last_pipeline_head = tape->pipeline_head;
2257 tape->controlled_pipeline_head_time = jiffies;
2258 }
2259 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
2260 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
2261 else if (time_after(jiffies, tape->controlled_previous_head_time))
2262 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
2263
2264 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
2265 /* -1 for read mode error recovery */
2266 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
2267 tape->uncontrolled_pipeline_head_time = jiffies;
2268 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
2269 }
2270 } else {
2271 tape->uncontrolled_previous_head_time = jiffies;
2272 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
2273 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
2274 tape->uncontrolled_pipeline_head_time = jiffies;
2275 }
2276 }
2277 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
2278 if (tape->speed_control == 0) {
2279 tape->max_insert_speed = 5000;
2280 } else if (tape->speed_control == 1) {
2281 if (tape->nr_pending_stages >= tape->max_stages / 2)
2282 tape->max_insert_speed = tape->pipeline_head_speed +
2283 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
2284 else
2285 tape->max_insert_speed = 500 +
2286 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
2287 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
2288 tape->max_insert_speed = 5000;
2289 } else if (tape->speed_control == 2) {
2290 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
2291 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
2292 } else
2293 tape->max_insert_speed = tape->speed_control;
2294 tape->max_insert_speed = max(tape->max_insert_speed, 500);
2295}
2296
2297static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2298{
2299 idetape_tape_t *tape = drive->driver_data;
2300 idetape_pc_t *pc = tape->pc;
2301 atapi_status_t status;
2302
2303 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2304 if (status.b.dsc) {
2305 if (status.b.check) {
2306 /* Error detected */
2307 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
2308 printk(KERN_ERR "ide-tape: %s: I/O error, ",
2309 tape->name);
2310 /* Retry operation */
2311 return idetape_retry_pc(drive);
2312 }
2313 pc->error = 0;
2314 if (tape->failed_pc == pc)
2315 tape->failed_pc = NULL;
2316 } else {
2317 pc->error = IDETAPE_ERROR_GENERAL;
2318 tape->failed_pc = NULL;
2319 }
2320 return pc->callback(drive);
2321}
2322
2323static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2324{
2325 idetape_tape_t *tape = drive->driver_data;
2326 struct request *rq = HWGROUP(drive)->rq;
2327 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2328
2329 tape->avg_size += blocks * tape->tape_block_size;
2330 tape->insert_size += blocks * tape->tape_block_size;
2331 if (tape->insert_size > 1024 * 1024)
2332 tape->measure_insert_time = 1;
2333 if (tape->measure_insert_time) {
2334 tape->measure_insert_time = 0;
2335 tape->insert_time = jiffies;
2336 tape->insert_size = 0;
2337 }
2338 if (time_after(jiffies, tape->insert_time))
2339 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08002340 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
2342 tape->avg_size = 0;
2343 tape->avg_time = jiffies;
2344 }
2345
2346#if IDETAPE_DEBUG_LOG
2347 if (tape->debug_level >= 4)
2348 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2349#endif /* IDETAPE_DEBUG_LOG */
2350
2351 tape->first_frame_position += blocks;
2352 rq->current_nr_sectors -= blocks;
2353
2354 if (!tape->pc->error)
2355 idetape_end_request(drive, 1, 0);
2356 else
2357 idetape_end_request(drive, tape->pc->error, 0);
2358 return ide_stopped;
2359}
2360
2361static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2362{
2363 idetape_init_pc(pc);
2364 pc->c[0] = IDETAPE_READ_CMD;
2365 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
2366 pc->c[1] = 1;
2367 pc->callback = &idetape_rw_callback;
2368 pc->bh = bh;
2369 atomic_set(&bh->b_count, 0);
2370 pc->buffer = NULL;
2371 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2372 if (pc->request_transfer == tape->stage_size)
2373 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2374}
2375
2376static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2377{
2378 int size = 32768;
2379 struct idetape_bh *p = bh;
2380
2381 idetape_init_pc(pc);
2382 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
2383 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
2384 pc->c[7] = size >> 8;
2385 pc->c[8] = size & 0xff;
2386 pc->callback = &idetape_pc_callback;
2387 pc->bh = bh;
2388 atomic_set(&bh->b_count, 0);
2389 pc->buffer = NULL;
2390 while (p) {
2391 atomic_set(&p->b_count, 0);
2392 p = p->b_reqnext;
2393 }
2394 pc->request_transfer = pc->buffer_size = size;
2395}
2396
2397static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2398{
2399 idetape_init_pc(pc);
2400 pc->c[0] = IDETAPE_WRITE_CMD;
2401 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
2402 pc->c[1] = 1;
2403 pc->callback = &idetape_rw_callback;
2404 set_bit(PC_WRITING, &pc->flags);
2405 pc->bh = bh;
2406 pc->b_data = bh->b_data;
2407 pc->b_count = atomic_read(&bh->b_count);
2408 pc->buffer = NULL;
2409 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2410 if (pc->request_transfer == tape->stage_size)
2411 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2412}
2413
2414/*
2415 * idetape_do_request is our request handling function.
2416 */
2417static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2418 struct request *rq, sector_t block)
2419{
2420 idetape_tape_t *tape = drive->driver_data;
2421 idetape_pc_t *pc = NULL;
2422 struct request *postponed_rq = tape->postponed_rq;
2423 atapi_status_t status;
2424
2425#if IDETAPE_DEBUG_LOG
2426#if 0
2427 if (tape->debug_level >= 5)
2428 printk(KERN_INFO "ide-tape: rq_status: %d, "
2429 "dev: %s, cmd: %ld, errors: %d\n", rq->rq_status,
2430 rq->rq_disk->disk_name, rq->cmd[0], rq->errors);
2431#endif
2432 if (tape->debug_level >= 2)
2433 printk(KERN_INFO "ide-tape: sector: %ld, "
2434 "nr_sectors: %ld, current_nr_sectors: %d\n",
2435 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
2436#endif /* IDETAPE_DEBUG_LOG */
2437
2438 if ((rq->flags & REQ_SPECIAL) == 0) {
2439 /*
2440 * We do not support buffer cache originated requests.
2441 */
2442 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
2443 "request queue (%ld)\n", drive->name, rq->flags);
2444 ide_end_request(drive, 0, 0);
2445 return ide_stopped;
2446 }
2447
2448 /*
2449 * Retry a failed packet command
2450 */
2451 if (tape->failed_pc != NULL &&
2452 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2453 return idetape_issue_packet_command(drive, tape->failed_pc);
2454 }
2455#if IDETAPE_DEBUG_BUGS
2456 if (postponed_rq != NULL)
2457 if (rq != postponed_rq) {
2458 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
2459 "Two DSC requests were queued\n");
2460 idetape_end_request(drive, 0, 0);
2461 return ide_stopped;
2462 }
2463#endif /* IDETAPE_DEBUG_BUGS */
2464
2465 tape->postponed_rq = NULL;
2466
2467 /*
2468 * If the tape is still busy, postpone our request and service
2469 * the other device meanwhile.
2470 */
2471 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2472
2473 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
2474 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2475
2476 if (drive->post_reset == 1) {
2477 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2478 drive->post_reset = 0;
2479 }
2480
2481 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2482 tape->measure_insert_time = 1;
2483 if (time_after(jiffies, tape->insert_time))
2484 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2485 calculate_speeds(drive);
2486 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2487 !status.b.dsc) {
2488 if (postponed_rq == NULL) {
2489 tape->dsc_polling_start = jiffies;
2490 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2491 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2492 } else if (time_after(jiffies, tape->dsc_timeout)) {
2493 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2494 tape->name);
2495 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2496 idetape_media_access_finished(drive);
2497 return ide_stopped;
2498 } else {
2499 return ide_do_reset(drive);
2500 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08002501 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2503 idetape_postpone_request(drive);
2504 return ide_stopped;
2505 }
2506 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2507 tape->buffer_head++;
2508#if USE_IOTRACE
2509 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2510#endif
2511 tape->postpone_cnt = 0;
2512 pc = idetape_next_pc_storage(drive);
2513 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2514 goto out;
2515 }
2516 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2517 tape->buffer_head++;
2518#if USE_IOTRACE
2519 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2520#endif
2521 tape->postpone_cnt = 0;
2522 pc = idetape_next_pc_storage(drive);
2523 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2524 goto out;
2525 }
2526 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2527 tape->postpone_cnt = 0;
2528 pc = idetape_next_pc_storage(drive);
2529 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2530 goto out;
2531 }
2532 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2533 pc = (idetape_pc_t *) rq->buffer;
2534 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2535 rq->cmd[0] |= REQ_IDETAPE_PC2;
2536 goto out;
2537 }
2538 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2539 idetape_media_access_finished(drive);
2540 return ide_stopped;
2541 }
2542 BUG();
2543out:
2544 return idetape_issue_packet_command(drive, pc);
2545}
2546
2547/*
2548 * Pipeline related functions
2549 */
2550static inline int idetape_pipeline_active (idetape_tape_t *tape)
2551{
2552 int rc1, rc2;
2553
2554 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2555 rc2 = (tape->active_data_request != NULL);
2556 return rc1;
2557}
2558
2559/*
2560 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2561 * stage, along with all the necessary small buffers which together make
2562 * a buffer of size tape->stage_size (or a bit more). We attempt to
2563 * combine sequential pages as much as possible.
2564 *
2565 * Returns a pointer to the new allocated stage, or NULL if we
2566 * can't (or don't want to) allocate a stage.
2567 *
2568 * Pipeline stages are optional and are used to increase performance.
2569 * If we can't allocate them, we'll manage without them.
2570 */
2571static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2572{
2573 idetape_stage_t *stage;
2574 struct idetape_bh *prev_bh, *bh;
2575 int pages = tape->pages_per_stage;
2576 char *b_data = NULL;
2577
2578 if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2579 return NULL;
2580 stage->next = NULL;
2581
2582 bh = stage->bh = (struct idetape_bh *)kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
2583 if (bh == NULL)
2584 goto abort;
2585 bh->b_reqnext = NULL;
2586 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2587 goto abort;
2588 if (clear)
2589 memset(bh->b_data, 0, PAGE_SIZE);
2590 bh->b_size = PAGE_SIZE;
2591 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2592
2593 while (--pages) {
2594 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2595 goto abort;
2596 if (clear)
2597 memset(b_data, 0, PAGE_SIZE);
2598 if (bh->b_data == b_data + PAGE_SIZE) {
2599 bh->b_size += PAGE_SIZE;
2600 bh->b_data -= PAGE_SIZE;
2601 if (full)
2602 atomic_add(PAGE_SIZE, &bh->b_count);
2603 continue;
2604 }
2605 if (b_data == bh->b_data + bh->b_size) {
2606 bh->b_size += PAGE_SIZE;
2607 if (full)
2608 atomic_add(PAGE_SIZE, &bh->b_count);
2609 continue;
2610 }
2611 prev_bh = bh;
2612 if ((bh = (struct idetape_bh *)kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
2613 free_page((unsigned long) b_data);
2614 goto abort;
2615 }
2616 bh->b_reqnext = NULL;
2617 bh->b_data = b_data;
2618 bh->b_size = PAGE_SIZE;
2619 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2620 prev_bh->b_reqnext = bh;
2621 }
2622 bh->b_size -= tape->excess_bh_size;
2623 if (full)
2624 atomic_sub(tape->excess_bh_size, &bh->b_count);
2625 return stage;
2626abort:
2627 __idetape_kfree_stage(stage);
2628 return NULL;
2629}
2630
2631static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2632{
2633 idetape_stage_t *cache_stage = tape->cache_stage;
2634
2635#if IDETAPE_DEBUG_LOG
2636 if (tape->debug_level >= 4)
2637 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2638#endif /* IDETAPE_DEBUG_LOG */
2639
2640 if (tape->nr_stages >= tape->max_stages)
2641 return NULL;
2642 if (cache_stage != NULL) {
2643 tape->cache_stage = NULL;
2644 return cache_stage;
2645 }
2646 return __idetape_kmalloc_stage(tape, 0, 0);
2647}
2648
2649static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2650{
2651 struct idetape_bh *bh = tape->bh;
2652 int count;
2653
2654 while (n) {
2655#if IDETAPE_DEBUG_BUGS
2656 if (bh == NULL) {
2657 printk(KERN_ERR "ide-tape: bh == NULL in "
2658 "idetape_copy_stage_from_user\n");
2659 return;
2660 }
2661#endif /* IDETAPE_DEBUG_BUGS */
2662 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2663 copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count);
2664 n -= count;
2665 atomic_add(count, &bh->b_count);
2666 buf += count;
2667 if (atomic_read(&bh->b_count) == bh->b_size) {
2668 bh = bh->b_reqnext;
2669 if (bh)
2670 atomic_set(&bh->b_count, 0);
2671 }
2672 }
2673 tape->bh = bh;
2674}
2675
2676static void idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2677{
2678 struct idetape_bh *bh = tape->bh;
2679 int count;
2680
2681 while (n) {
2682#if IDETAPE_DEBUG_BUGS
2683 if (bh == NULL) {
2684 printk(KERN_ERR "ide-tape: bh == NULL in "
2685 "idetape_copy_stage_to_user\n");
2686 return;
2687 }
2688#endif /* IDETAPE_DEBUG_BUGS */
2689 count = min(tape->b_count, n);
2690 copy_to_user(buf, tape->b_data, count);
2691 n -= count;
2692 tape->b_data += count;
2693 tape->b_count -= count;
2694 buf += count;
2695 if (!tape->b_count) {
2696 tape->bh = bh = bh->b_reqnext;
2697 if (bh) {
2698 tape->b_data = bh->b_data;
2699 tape->b_count = atomic_read(&bh->b_count);
2700 }
2701 }
2702 }
2703}
2704
2705static void idetape_init_merge_stage (idetape_tape_t *tape)
2706{
2707 struct idetape_bh *bh = tape->merge_stage->bh;
2708
2709 tape->bh = bh;
2710 if (tape->chrdev_direction == idetape_direction_write)
2711 atomic_set(&bh->b_count, 0);
2712 else {
2713 tape->b_data = bh->b_data;
2714 tape->b_count = atomic_read(&bh->b_count);
2715 }
2716}
2717
2718static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2719{
2720 struct idetape_bh *tmp;
2721
2722 tmp = stage->bh;
2723 stage->bh = tape->merge_stage->bh;
2724 tape->merge_stage->bh = tmp;
2725 idetape_init_merge_stage(tape);
2726}
2727
2728/*
2729 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2730 */
2731static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2732{
2733 idetape_tape_t *tape = drive->driver_data;
2734 unsigned long flags;
2735
2736#if IDETAPE_DEBUG_LOG
2737 if (tape->debug_level >= 4)
2738 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2739#endif /* IDETAPE_DEBUG_LOG */
2740 spin_lock_irqsave(&tape->spinlock, flags);
2741 stage->next = NULL;
2742 if (tape->last_stage != NULL)
2743 tape->last_stage->next=stage;
2744 else
2745 tape->first_stage = tape->next_stage=stage;
2746 tape->last_stage = stage;
2747 if (tape->next_stage == NULL)
2748 tape->next_stage = tape->last_stage;
2749 tape->nr_stages++;
2750 tape->nr_pending_stages++;
2751 spin_unlock_irqrestore(&tape->spinlock, flags);
2752}
2753
2754/*
2755 * idetape_wait_for_request installs a completion in a pending request
2756 * and sleeps until it is serviced.
2757 *
2758 * The caller should ensure that the request will not be serviced
2759 * before we install the completion (usually by disabling interrupts).
2760 */
2761static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2762{
2763 DECLARE_COMPLETION(wait);
2764 idetape_tape_t *tape = drive->driver_data;
2765
2766#if IDETAPE_DEBUG_BUGS
2767 if (rq == NULL || (rq->flags & REQ_SPECIAL) == 0) {
2768 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2769 return;
2770 }
2771#endif /* IDETAPE_DEBUG_BUGS */
2772 rq->waiting = &wait;
2773 rq->end_io = blk_end_sync_rq;
2774 spin_unlock_irq(&tape->spinlock);
2775 wait_for_completion(&wait);
2776 /* The stage and its struct request have been deallocated */
2777 spin_lock_irq(&tape->spinlock);
2778}
2779
2780static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2781{
2782 idetape_tape_t *tape = drive->driver_data;
2783 idetape_read_position_result_t *result;
2784
2785#if IDETAPE_DEBUG_LOG
2786 if (tape->debug_level >= 4)
2787 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2788#endif /* IDETAPE_DEBUG_LOG */
2789
2790 if (!tape->pc->error) {
2791 result = (idetape_read_position_result_t *) tape->pc->buffer;
2792#if IDETAPE_DEBUG_LOG
2793 if (tape->debug_level >= 2)
2794 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2795 if (tape->debug_level >= 2)
2796 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2797#endif /* IDETAPE_DEBUG_LOG */
2798 if (result->bpu) {
2799 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2800 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2801 idetape_end_request(drive, 0, 0);
2802 } else {
2803#if IDETAPE_DEBUG_LOG
2804 if (tape->debug_level >= 2)
2805 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2806#endif /* IDETAPE_DEBUG_LOG */
2807 tape->partition = result->partition;
2808 tape->first_frame_position = ntohl(result->first_block);
2809 tape->last_frame_position = ntohl(result->last_block);
2810 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2811 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2812 idetape_end_request(drive, 1, 0);
2813 }
2814 } else {
2815 idetape_end_request(drive, 0, 0);
2816 }
2817 return ide_stopped;
2818}
2819
2820/*
2821 * idetape_create_write_filemark_cmd will:
2822 *
2823 * 1. Write a filemark if write_filemark=1.
2824 * 2. Flush the device buffers without writing a filemark
2825 * if write_filemark=0.
2826 *
2827 */
2828static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2829{
2830 idetape_init_pc(pc);
2831 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2832 pc->c[4] = write_filemark;
2833 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2834 pc->callback = &idetape_pc_callback;
2835}
2836
2837static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2838{
2839 idetape_init_pc(pc);
2840 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2841 pc->callback = &idetape_pc_callback;
2842}
2843
2844/*
2845 * idetape_queue_pc_tail is based on the following functions:
2846 *
2847 * ide_do_drive_cmd from ide.c
2848 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2849 *
2850 * We add a special packet command request to the tail of the request
2851 * queue, and wait for it to be serviced.
2852 *
2853 * This is not to be called from within the request handling part
2854 * of the driver ! We allocate here data in the stack, and it is valid
2855 * until the request is finished. This is not the case for the bottom
2856 * part of the driver, where we are always leaving the functions to wait
2857 * for an interrupt or a timer event.
2858 *
2859 * From the bottom part of the driver, we should allocate safe memory
2860 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2861 * the request to the request list without waiting for it to be serviced !
2862 * In that case, we usually use idetape_queue_pc_head.
2863 */
2864static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2865{
2866 struct ide_tape_obj *tape = drive->driver_data;
2867 struct request rq;
2868
2869 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2870 rq.buffer = (char *) pc;
2871 rq.rq_disk = tape->disk;
2872 return ide_do_drive_cmd(drive, &rq, ide_wait);
2873}
2874
2875static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2876{
2877 idetape_init_pc(pc);
2878 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2879 pc->c[4] = cmd;
2880 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2881 pc->callback = &idetape_pc_callback;
2882}
2883
2884static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2885{
2886 idetape_tape_t *tape = drive->driver_data;
2887 idetape_pc_t pc;
2888 int load_attempted = 0;
2889
2890 /*
2891 * Wait for the tape to become ready
2892 */
2893 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2894 timeout += jiffies;
2895 while (time_before(jiffies, timeout)) {
2896 idetape_create_test_unit_ready_cmd(&pc);
2897 if (!__idetape_queue_pc_tail(drive, &pc))
2898 return 0;
2899 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2900 || (tape->asc == 0x3A)) { /* no media */
2901 if (load_attempted)
2902 return -ENOMEDIUM;
2903 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2904 __idetape_queue_pc_tail(drive, &pc);
2905 load_attempted = 1;
2906 /* not about to be ready */
2907 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2908 (tape->ascq == 1 || tape->ascq == 8)))
2909 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002910 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912 return -EIO;
2913}
2914
2915static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2916{
2917 return __idetape_queue_pc_tail(drive, pc);
2918}
2919
2920static int idetape_flush_tape_buffers (ide_drive_t *drive)
2921{
2922 idetape_pc_t pc;
2923 int rc;
2924
2925 idetape_create_write_filemark_cmd(drive, &pc, 0);
2926 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2927 return rc;
2928 idetape_wait_ready(drive, 60 * 5 * HZ);
2929 return 0;
2930}
2931
2932static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2933{
2934 idetape_init_pc(pc);
2935 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2936 pc->request_transfer = 20;
2937 pc->callback = &idetape_read_position_callback;
2938}
2939
2940static int idetape_read_position (ide_drive_t *drive)
2941{
2942 idetape_tape_t *tape = drive->driver_data;
2943 idetape_pc_t pc;
2944 int position;
2945
2946#if IDETAPE_DEBUG_LOG
2947 if (tape->debug_level >= 4)
2948 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2949#endif /* IDETAPE_DEBUG_LOG */
2950
2951 idetape_create_read_position_cmd(&pc);
2952 if (idetape_queue_pc_tail(drive, &pc))
2953 return -1;
2954 position = tape->first_frame_position;
2955 return position;
2956}
2957
2958static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2959{
2960 idetape_init_pc(pc);
2961 pc->c[0] = IDETAPE_LOCATE_CMD;
2962 pc->c[1] = 2;
2963 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2964 pc->c[8] = partition;
2965 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2966 pc->callback = &idetape_pc_callback;
2967}
2968
2969static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2970{
2971 idetape_tape_t *tape = drive->driver_data;
2972
2973 if (!tape->capabilities.lock)
2974 return 0;
2975
2976 idetape_init_pc(pc);
2977 pc->c[0] = IDETAPE_PREVENT_CMD;
2978 pc->c[4] = prevent;
2979 pc->callback = &idetape_pc_callback;
2980 return 1;
2981}
2982
2983static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2984{
2985 idetape_tape_t *tape = drive->driver_data;
2986 unsigned long flags;
2987 int cnt;
2988
2989 if (tape->chrdev_direction != idetape_direction_read)
2990 return 0;
2991
2992 /* Remove merge stage. */
2993 cnt = tape->merge_stage_size / tape->tape_block_size;
2994 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2995 ++cnt; /* Filemarks count as 1 sector */
2996 tape->merge_stage_size = 0;
2997 if (tape->merge_stage != NULL) {
2998 __idetape_kfree_stage(tape->merge_stage);
2999 tape->merge_stage = NULL;
3000 }
3001
3002 /* Clear pipeline flags. */
3003 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3004 tape->chrdev_direction = idetape_direction_none;
3005
3006 /* Remove pipeline stages. */
3007 if (tape->first_stage == NULL)
3008 return 0;
3009
3010 spin_lock_irqsave(&tape->spinlock, flags);
3011 tape->next_stage = NULL;
3012 if (idetape_pipeline_active(tape))
3013 idetape_wait_for_request(drive, tape->active_data_request);
3014 spin_unlock_irqrestore(&tape->spinlock, flags);
3015
3016 while (tape->first_stage != NULL) {
3017 struct request *rq_ptr = &tape->first_stage->rq;
3018
3019 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
3020 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3021 ++cnt;
3022 idetape_remove_stage_head(drive);
3023 }
3024 tape->nr_pending_stages = 0;
3025 tape->max_stages = tape->min_pipeline;
3026 return cnt;
3027}
3028
3029/*
3030 * idetape_position_tape positions the tape to the requested block
3031 * using the LOCATE packet command. A READ POSITION command is then
3032 * issued to check where we are positioned.
3033 *
3034 * Like all higher level operations, we queue the commands at the tail
3035 * of the request queue and wait for their completion.
3036 *
3037 */
3038static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
3039{
3040 idetape_tape_t *tape = drive->driver_data;
3041 int retval;
3042 idetape_pc_t pc;
3043
3044 if (tape->chrdev_direction == idetape_direction_read)
3045 __idetape_discard_read_pipeline(drive);
3046 idetape_wait_ready(drive, 60 * 5 * HZ);
3047 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
3048 retval = idetape_queue_pc_tail(drive, &pc);
3049 if (retval)
3050 return (retval);
3051
3052 idetape_create_read_position_cmd(&pc);
3053 return (idetape_queue_pc_tail(drive, &pc));
3054}
3055
3056static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
3057{
3058 idetape_tape_t *tape = drive->driver_data;
3059 int cnt;
3060 int seek, position;
3061
3062 cnt = __idetape_discard_read_pipeline(drive);
3063 if (restore_position) {
3064 position = idetape_read_position(drive);
3065 seek = position > cnt ? position - cnt : 0;
3066 if (idetape_position_tape(drive, seek, 0, 0)) {
3067 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
3068 return;
3069 }
3070 }
3071}
3072
3073/*
3074 * idetape_queue_rw_tail generates a read/write request for the block
3075 * device interface and wait for it to be serviced.
3076 */
3077static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
3078{
3079 idetape_tape_t *tape = drive->driver_data;
3080 struct request rq;
3081
3082#if IDETAPE_DEBUG_LOG
3083 if (tape->debug_level >= 2)
3084 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
3085#endif /* IDETAPE_DEBUG_LOG */
3086#if IDETAPE_DEBUG_BUGS
3087 if (idetape_pipeline_active(tape)) {
3088 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3089 return (0);
3090 }
3091#endif /* IDETAPE_DEBUG_BUGS */
3092
3093 idetape_init_rq(&rq, cmd);
3094 rq.rq_disk = tape->disk;
3095 rq.special = (void *)bh;
3096 rq.sector = tape->first_frame_position;
3097 rq.nr_sectors = rq.current_nr_sectors = blocks;
3098 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
3099
3100 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
3101 return 0;
3102
3103 if (tape->merge_stage)
3104 idetape_init_merge_stage(tape);
3105 if (rq.errors == IDETAPE_ERROR_GENERAL)
3106 return -EIO;
3107 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
3108}
3109
3110/*
3111 * idetape_insert_pipeline_into_queue is used to start servicing the
3112 * pipeline stages, starting from tape->next_stage.
3113 */
3114static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
3115{
3116 idetape_tape_t *tape = drive->driver_data;
3117
3118 if (tape->next_stage == NULL)
3119 return;
3120 if (!idetape_pipeline_active(tape)) {
3121 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
3122 idetape_active_next_stage(drive);
3123 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
3124 }
3125}
3126
3127static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
3128{
3129 idetape_init_pc(pc);
3130 pc->c[0] = IDETAPE_INQUIRY_CMD;
3131 pc->c[4] = pc->request_transfer = 254;
3132 pc->callback = &idetape_pc_callback;
3133}
3134
3135static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
3136{
3137 idetape_init_pc(pc);
3138 pc->c[0] = IDETAPE_REWIND_CMD;
3139 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3140 pc->callback = &idetape_pc_callback;
3141}
3142
3143#if 0
3144static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
3145{
3146 idetape_init_pc(pc);
3147 set_bit(PC_WRITING, &pc->flags);
3148 pc->c[0] = IDETAPE_MODE_SELECT_CMD;
3149 pc->c[1] = 0x10;
3150 put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
3151 pc->request_transfer = 255;
3152 pc->callback = &idetape_pc_callback;
3153}
3154#endif
3155
3156static void idetape_create_erase_cmd (idetape_pc_t *pc)
3157{
3158 idetape_init_pc(pc);
3159 pc->c[0] = IDETAPE_ERASE_CMD;
3160 pc->c[1] = 1;
3161 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3162 pc->callback = &idetape_pc_callback;
3163}
3164
3165static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
3166{
3167 idetape_init_pc(pc);
3168 pc->c[0] = IDETAPE_SPACE_CMD;
3169 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
3170 pc->c[1] = cmd;
3171 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3172 pc->callback = &idetape_pc_callback;
3173}
3174
3175static void idetape_wait_first_stage (ide_drive_t *drive)
3176{
3177 idetape_tape_t *tape = drive->driver_data;
3178 unsigned long flags;
3179
3180 if (tape->first_stage == NULL)
3181 return;
3182 spin_lock_irqsave(&tape->spinlock, flags);
3183 if (tape->active_stage == tape->first_stage)
3184 idetape_wait_for_request(drive, tape->active_data_request);
3185 spin_unlock_irqrestore(&tape->spinlock, flags);
3186}
3187
3188/*
3189 * idetape_add_chrdev_write_request tries to add a character device
3190 * originated write request to our pipeline. In case we don't succeed,
3191 * we revert to non-pipelined operation mode for this request.
3192 *
3193 * 1. Try to allocate a new pipeline stage.
3194 * 2. If we can't, wait for more and more requests to be serviced
3195 * and try again each time.
3196 * 3. If we still can't allocate a stage, fallback to
3197 * non-pipelined operation mode for this request.
3198 */
3199static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3200{
3201 idetape_tape_t *tape = drive->driver_data;
3202 idetape_stage_t *new_stage;
3203 unsigned long flags;
3204 struct request *rq;
3205
3206#if IDETAPE_DEBUG_LOG
3207 if (tape->debug_level >= 3)
3208 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
3209#endif /* IDETAPE_DEBUG_LOG */
3210
3211 /*
3212 * Attempt to allocate a new stage.
3213 * Pay special attention to possible race conditions.
3214 */
3215 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
3216 spin_lock_irqsave(&tape->spinlock, flags);
3217 if (idetape_pipeline_active(tape)) {
3218 idetape_wait_for_request(drive, tape->active_data_request);
3219 spin_unlock_irqrestore(&tape->spinlock, flags);
3220 } else {
3221 spin_unlock_irqrestore(&tape->spinlock, flags);
3222 idetape_insert_pipeline_into_queue(drive);
3223 if (idetape_pipeline_active(tape))
3224 continue;
3225 /*
3226 * Linux is short on memory. Fallback to
3227 * non-pipelined operation mode for this request.
3228 */
3229 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3230 }
3231 }
3232 rq = &new_stage->rq;
3233 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
3234 /* Doesn't actually matter - We always assume sequential access */
3235 rq->sector = tape->first_frame_position;
3236 rq->nr_sectors = rq->current_nr_sectors = blocks;
3237
3238 idetape_switch_buffers(tape, new_stage);
3239 idetape_add_stage_tail(drive, new_stage);
3240 tape->pipeline_head++;
3241#if USE_IOTRACE
3242 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3243#endif
3244 calculate_speeds(drive);
3245
3246 /*
3247 * Estimate whether the tape has stopped writing by checking
3248 * if our write pipeline is currently empty. If we are not
3249 * writing anymore, wait for the pipeline to be full enough
3250 * (90%) before starting to service requests, so that we will
3251 * be able to keep up with the higher speeds of the tape.
3252 */
3253 if (!idetape_pipeline_active(tape)) {
3254 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
3255 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
3256 tape->measure_insert_time = 1;
3257 tape->insert_time = jiffies;
3258 tape->insert_size = 0;
3259 tape->insert_speed = 0;
3260 idetape_insert_pipeline_into_queue(drive);
3261 }
3262 }
3263 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3264 /* Return a deferred error */
3265 return -EIO;
3266 return blocks;
3267}
3268
3269/*
3270 * idetape_wait_for_pipeline will wait until all pending pipeline
3271 * requests are serviced. Typically called on device close.
3272 */
3273static void idetape_wait_for_pipeline (ide_drive_t *drive)
3274{
3275 idetape_tape_t *tape = drive->driver_data;
3276 unsigned long flags;
3277
3278 while (tape->next_stage || idetape_pipeline_active(tape)) {
3279 idetape_insert_pipeline_into_queue(drive);
3280 spin_lock_irqsave(&tape->spinlock, flags);
3281 if (idetape_pipeline_active(tape))
3282 idetape_wait_for_request(drive, tape->active_data_request);
3283 spin_unlock_irqrestore(&tape->spinlock, flags);
3284 }
3285}
3286
3287static void idetape_empty_write_pipeline (ide_drive_t *drive)
3288{
3289 idetape_tape_t *tape = drive->driver_data;
3290 int blocks, min;
3291 struct idetape_bh *bh;
3292
3293#if IDETAPE_DEBUG_BUGS
3294 if (tape->chrdev_direction != idetape_direction_write) {
3295 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3296 return;
3297 }
3298 if (tape->merge_stage_size > tape->stage_size) {
3299 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
3300 tape->merge_stage_size = tape->stage_size;
3301 }
3302#endif /* IDETAPE_DEBUG_BUGS */
3303 if (tape->merge_stage_size) {
3304 blocks = tape->merge_stage_size / tape->tape_block_size;
3305 if (tape->merge_stage_size % tape->tape_block_size) {
3306 unsigned int i;
3307
3308 blocks++;
3309 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
3310 bh = tape->bh->b_reqnext;
3311 while (bh) {
3312 atomic_set(&bh->b_count, 0);
3313 bh = bh->b_reqnext;
3314 }
3315 bh = tape->bh;
3316 while (i) {
3317 if (bh == NULL) {
3318
3319 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
3320 break;
3321 }
3322 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
3323 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
3324 atomic_add(min, &bh->b_count);
3325 i -= min;
3326 bh = bh->b_reqnext;
3327 }
3328 }
3329 (void) idetape_add_chrdev_write_request(drive, blocks);
3330 tape->merge_stage_size = 0;
3331 }
3332 idetape_wait_for_pipeline(drive);
3333 if (tape->merge_stage != NULL) {
3334 __idetape_kfree_stage(tape->merge_stage);
3335 tape->merge_stage = NULL;
3336 }
3337 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3338 tape->chrdev_direction = idetape_direction_none;
3339
3340 /*
3341 * On the next backup, perform the feedback loop again.
3342 * (I don't want to keep sense information between backups,
3343 * as some systems are constantly on, and the system load
3344 * can be totally different on the next backup).
3345 */
3346 tape->max_stages = tape->min_pipeline;
3347#if IDETAPE_DEBUG_BUGS
3348 if (tape->first_stage != NULL ||
3349 tape->next_stage != NULL ||
3350 tape->last_stage != NULL ||
3351 tape->nr_stages != 0) {
3352 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
3353 "first_stage %p, next_stage %p, "
3354 "last_stage %p, nr_stages %d\n",
3355 tape->first_stage, tape->next_stage,
3356 tape->last_stage, tape->nr_stages);
3357 }
3358#endif /* IDETAPE_DEBUG_BUGS */
3359}
3360
3361static void idetape_restart_speed_control (ide_drive_t *drive)
3362{
3363 idetape_tape_t *tape = drive->driver_data;
3364
3365 tape->restart_speed_control_req = 0;
3366 tape->pipeline_head = 0;
3367 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
3368 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
3369 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
3370 tape->uncontrolled_pipeline_head_speed = 0;
3371 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
3372 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
3373}
3374
3375static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
3376{
3377 idetape_tape_t *tape = drive->driver_data;
3378 idetape_stage_t *new_stage;
3379 struct request rq;
3380 int bytes_read;
3381 int blocks = tape->capabilities.ctl;
3382
3383 /* Initialize read operation */
3384 if (tape->chrdev_direction != idetape_direction_read) {
3385 if (tape->chrdev_direction == idetape_direction_write) {
3386 idetape_empty_write_pipeline(drive);
3387 idetape_flush_tape_buffers(drive);
3388 }
3389#if IDETAPE_DEBUG_BUGS
3390 if (tape->merge_stage || tape->merge_stage_size) {
3391 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
3392 tape->merge_stage_size = 0;
3393 }
3394#endif /* IDETAPE_DEBUG_BUGS */
3395 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3396 return -ENOMEM;
3397 tape->chrdev_direction = idetape_direction_read;
3398
3399 /*
3400 * Issue a read 0 command to ensure that DSC handshake
3401 * is switched from completion mode to buffer available
3402 * mode.
3403 * No point in issuing this if DSC overlap isn't supported,
3404 * some drives (Seagate STT3401A) will return an error.
3405 */
3406 if (drive->dsc_overlap) {
3407 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
3408 if (bytes_read < 0) {
3409 __idetape_kfree_stage(tape->merge_stage);
3410 tape->merge_stage = NULL;
3411 tape->chrdev_direction = idetape_direction_none;
3412 return bytes_read;
3413 }
3414 }
3415 }
3416 if (tape->restart_speed_control_req)
3417 idetape_restart_speed_control(drive);
3418 idetape_init_rq(&rq, REQ_IDETAPE_READ);
3419 rq.sector = tape->first_frame_position;
3420 rq.nr_sectors = rq.current_nr_sectors = blocks;
3421 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
3422 tape->nr_stages < max_stages) {
3423 new_stage = idetape_kmalloc_stage(tape);
3424 while (new_stage != NULL) {
3425 new_stage->rq = rq;
3426 idetape_add_stage_tail(drive, new_stage);
3427 if (tape->nr_stages >= max_stages)
3428 break;
3429 new_stage = idetape_kmalloc_stage(tape);
3430 }
3431 }
3432 if (!idetape_pipeline_active(tape)) {
3433 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
3434 tape->measure_insert_time = 1;
3435 tape->insert_time = jiffies;
3436 tape->insert_size = 0;
3437 tape->insert_speed = 0;
3438 idetape_insert_pipeline_into_queue(drive);
3439 }
3440 }
3441 return 0;
3442}
3443
3444/*
3445 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
3446 * to service a character device read request and add read-ahead
3447 * requests to our pipeline.
3448 */
3449static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
3450{
3451 idetape_tape_t *tape = drive->driver_data;
3452 unsigned long flags;
3453 struct request *rq_ptr;
3454 int bytes_read;
3455
3456#if IDETAPE_DEBUG_LOG
3457 if (tape->debug_level >= 4)
3458 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
3459#endif /* IDETAPE_DEBUG_LOG */
3460
3461 /*
3462 * If we are at a filemark, return a read length of 0
3463 */
3464 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
3465 return 0;
3466
3467 /*
3468 * Wait for the next block to be available at the head
3469 * of the pipeline
3470 */
3471 idetape_initiate_read(drive, tape->max_stages);
3472 if (tape->first_stage == NULL) {
3473 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3474 return 0;
3475 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
3476 }
3477 idetape_wait_first_stage(drive);
3478 rq_ptr = &tape->first_stage->rq;
3479 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
3480 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
3481
3482
3483 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
3484 return 0;
3485 else {
3486 idetape_switch_buffers(tape, tape->first_stage);
3487 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3488 set_bit(IDETAPE_FILEMARK, &tape->flags);
3489 spin_lock_irqsave(&tape->spinlock, flags);
3490 idetape_remove_stage_head(drive);
3491 spin_unlock_irqrestore(&tape->spinlock, flags);
3492 tape->pipeline_head++;
3493#if USE_IOTRACE
3494 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3495#endif
3496 calculate_speeds(drive);
3497 }
3498#if IDETAPE_DEBUG_BUGS
3499 if (bytes_read > blocks * tape->tape_block_size) {
3500 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3501 bytes_read = blocks * tape->tape_block_size;
3502 }
3503#endif /* IDETAPE_DEBUG_BUGS */
3504 return (bytes_read);
3505}
3506
3507static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3508{
3509 idetape_tape_t *tape = drive->driver_data;
3510 struct idetape_bh *bh;
3511 int blocks;
3512
3513 while (bcount) {
3514 unsigned int count;
3515
3516 bh = tape->merge_stage->bh;
3517 count = min(tape->stage_size, bcount);
3518 bcount -= count;
3519 blocks = count / tape->tape_block_size;
3520 while (count) {
3521 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3522 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3523 count -= atomic_read(&bh->b_count);
3524 bh = bh->b_reqnext;
3525 }
3526 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3527 }
3528}
3529
3530static int idetape_pipeline_size (ide_drive_t *drive)
3531{
3532 idetape_tape_t *tape = drive->driver_data;
3533 idetape_stage_t *stage;
3534 struct request *rq;
3535 int size = 0;
3536
3537 idetape_wait_for_pipeline(drive);
3538 stage = tape->first_stage;
3539 while (stage != NULL) {
3540 rq = &stage->rq;
3541 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3542 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3543 size += tape->tape_block_size;
3544 stage = stage->next;
3545 }
3546 size += tape->merge_stage_size;
3547 return size;
3548}
3549
3550/*
3551 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3552 *
3553 * We currently support only one partition.
3554 */
3555static int idetape_rewind_tape (ide_drive_t *drive)
3556{
3557 int retval;
3558 idetape_pc_t pc;
3559#if IDETAPE_DEBUG_LOG
3560 idetape_tape_t *tape = drive->driver_data;
3561 if (tape->debug_level >= 2)
3562 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3563#endif /* IDETAPE_DEBUG_LOG */
3564
3565 idetape_create_rewind_cmd(drive, &pc);
3566 retval = idetape_queue_pc_tail(drive, &pc);
3567 if (retval)
3568 return retval;
3569
3570 idetape_create_read_position_cmd(&pc);
3571 retval = idetape_queue_pc_tail(drive, &pc);
3572 if (retval)
3573 return retval;
3574 return 0;
3575}
3576
3577/*
3578 * Our special ide-tape ioctl's.
3579 *
3580 * Currently there aren't any ioctl's.
3581 * mtio.h compatible commands should be issued to the character device
3582 * interface.
3583 */
3584static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3585{
3586 idetape_tape_t *tape = drive->driver_data;
3587 idetape_config_t config;
3588 void __user *argp = (void __user *)arg;
3589
3590#if IDETAPE_DEBUG_LOG
3591 if (tape->debug_level >= 4)
3592 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3593#endif /* IDETAPE_DEBUG_LOG */
3594 switch (cmd) {
3595 case 0x0340:
3596 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3597 return -EFAULT;
3598 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3599 tape->max_stages = config.nr_stages;
3600 break;
3601 case 0x0350:
3602 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3603 config.nr_stages = tape->max_stages;
3604 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3605 return -EFAULT;
3606 break;
3607 default:
3608 return -EIO;
3609 }
3610 return 0;
3611}
3612
3613/*
3614 * idetape_space_over_filemarks is now a bit more complicated than just
3615 * passing the command to the tape since we may have crossed some
3616 * filemarks during our pipelined read-ahead mode.
3617 *
3618 * As a minor side effect, the pipeline enables us to support MTFSFM when
3619 * the filemark is in our internal pipeline even if the tape doesn't
3620 * support spacing over filemarks in the reverse direction.
3621 */
3622static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3623{
3624 idetape_tape_t *tape = drive->driver_data;
3625 idetape_pc_t pc;
3626 unsigned long flags;
3627 int retval,count=0;
3628
3629 if (mt_count == 0)
3630 return 0;
3631 if (MTBSF == mt_op || MTBSFM == mt_op) {
3632 if (!tape->capabilities.sprev)
3633 return -EIO;
3634 mt_count = - mt_count;
3635 }
3636
3637 if (tape->chrdev_direction == idetape_direction_read) {
3638 /*
3639 * We have a read-ahead buffer. Scan it for crossed
3640 * filemarks.
3641 */
3642 tape->merge_stage_size = 0;
3643 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3644 ++count;
3645 while (tape->first_stage != NULL) {
3646 if (count == mt_count) {
3647 if (mt_op == MTFSFM)
3648 set_bit(IDETAPE_FILEMARK, &tape->flags);
3649 return 0;
3650 }
3651 spin_lock_irqsave(&tape->spinlock, flags);
3652 if (tape->first_stage == tape->active_stage) {
3653 /*
3654 * We have reached the active stage in the read pipeline.
3655 * There is no point in allowing the drive to continue
3656 * reading any farther, so we stop the pipeline.
3657 *
3658 * This section should be moved to a separate subroutine,
3659 * because a similar function is performed in
3660 * __idetape_discard_read_pipeline(), for example.
3661 */
3662 tape->next_stage = NULL;
3663 spin_unlock_irqrestore(&tape->spinlock, flags);
3664 idetape_wait_first_stage(drive);
3665 tape->next_stage = tape->first_stage->next;
3666 } else
3667 spin_unlock_irqrestore(&tape->spinlock, flags);
3668 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3669 ++count;
3670 idetape_remove_stage_head(drive);
3671 }
3672 idetape_discard_read_pipeline(drive, 0);
3673 }
3674
3675 /*
3676 * The filemark was not found in our internal pipeline.
3677 * Now we can issue the space command.
3678 */
3679 switch (mt_op) {
3680 case MTFSF:
3681 case MTBSF:
3682 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3683 return (idetape_queue_pc_tail(drive, &pc));
3684 case MTFSFM:
3685 case MTBSFM:
3686 if (!tape->capabilities.sprev)
3687 return (-EIO);
3688 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3689 if (retval) return (retval);
3690 count = (MTBSFM == mt_op ? 1 : -1);
3691 return (idetape_space_over_filemarks(drive, MTFSF, count));
3692 default:
3693 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3694 return (-EIO);
3695 }
3696}
3697
3698
3699/*
3700 * Our character device read / write functions.
3701 *
3702 * The tape is optimized to maximize throughput when it is transferring
3703 * an integral number of the "continuous transfer limit", which is
3704 * a parameter of the specific tape (26 KB on my particular tape).
3705 * (32 kB for Onstream)
3706 *
3707 * As of version 1.3 of the driver, the character device provides an
3708 * abstract continuous view of the media - any mix of block sizes (even 1
3709 * byte) on the same backup/restore procedure is supported. The driver
3710 * will internally convert the requests to the recommended transfer unit,
3711 * so that an unmatch between the user's block size to the recommended
3712 * size will only result in a (slightly) increased driver overhead, but
3713 * will no longer hit performance.
3714 * This is not applicable to Onstream.
3715 */
3716static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3717 size_t count, loff_t *ppos)
3718{
3719 struct ide_tape_obj *tape = ide_tape_f(file);
3720 ide_drive_t *drive = tape->drive;
3721 ssize_t bytes_read,temp, actually_read = 0, rc;
3722
3723#if IDETAPE_DEBUG_LOG
3724 if (tape->debug_level >= 3)
3725 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3726#endif /* IDETAPE_DEBUG_LOG */
3727
3728 if (tape->chrdev_direction != idetape_direction_read) {
3729 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3730 if (count > tape->tape_block_size &&
3731 (count % tape->tape_block_size) == 0)
3732 tape->user_bs_factor = count / tape->tape_block_size;
3733 }
3734 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3735 return rc;
3736 if (count == 0)
3737 return (0);
3738 if (tape->merge_stage_size) {
3739 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
3740 idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read);
3741 buf += actually_read;
3742 tape->merge_stage_size -= actually_read;
3743 count -= actually_read;
3744 }
3745 while (count >= tape->stage_size) {
3746 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3747 if (bytes_read <= 0)
3748 goto finish;
3749 idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read);
3750 buf += bytes_read;
3751 count -= bytes_read;
3752 actually_read += bytes_read;
3753 }
3754 if (count) {
3755 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3756 if (bytes_read <= 0)
3757 goto finish;
3758 temp = min((unsigned long)count, (unsigned long)bytes_read);
3759 idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp);
3760 actually_read += temp;
3761 tape->merge_stage_size = bytes_read-temp;
3762 }
3763finish:
3764 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3765#if IDETAPE_DEBUG_LOG
3766 if (tape->debug_level >= 2)
3767 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3768#endif
3769 idetape_space_over_filemarks(drive, MTFSF, 1);
3770 return 0;
3771 }
3772 return actually_read;
3773}
3774
3775static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3776 size_t count, loff_t *ppos)
3777{
3778 struct ide_tape_obj *tape = ide_tape_f(file);
3779 ide_drive_t *drive = tape->drive;
3780 ssize_t retval, actually_written = 0;
3781
3782 /* The drive is write protected. */
3783 if (tape->write_prot)
3784 return -EACCES;
3785
3786#if IDETAPE_DEBUG_LOG
3787 if (tape->debug_level >= 3)
3788 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3789 "count %Zd\n", count);
3790#endif /* IDETAPE_DEBUG_LOG */
3791
3792 /* Initialize write operation */
3793 if (tape->chrdev_direction != idetape_direction_write) {
3794 if (tape->chrdev_direction == idetape_direction_read)
3795 idetape_discard_read_pipeline(drive, 1);
3796#if IDETAPE_DEBUG_BUGS
3797 if (tape->merge_stage || tape->merge_stage_size) {
3798 printk(KERN_ERR "ide-tape: merge_stage_size "
3799 "should be 0 now\n");
3800 tape->merge_stage_size = 0;
3801 }
3802#endif /* IDETAPE_DEBUG_BUGS */
3803 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3804 return -ENOMEM;
3805 tape->chrdev_direction = idetape_direction_write;
3806 idetape_init_merge_stage(tape);
3807
3808 /*
3809 * Issue a write 0 command to ensure that DSC handshake
3810 * is switched from completion mode to buffer available
3811 * mode.
3812 * No point in issuing this if DSC overlap isn't supported,
3813 * some drives (Seagate STT3401A) will return an error.
3814 */
3815 if (drive->dsc_overlap) {
3816 retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
3817 if (retval < 0) {
3818 __idetape_kfree_stage(tape->merge_stage);
3819 tape->merge_stage = NULL;
3820 tape->chrdev_direction = idetape_direction_none;
3821 return retval;
3822 }
3823 }
3824 }
3825 if (count == 0)
3826 return (0);
3827 if (tape->restart_speed_control_req)
3828 idetape_restart_speed_control(drive);
3829 if (tape->merge_stage_size) {
3830#if IDETAPE_DEBUG_BUGS
3831 if (tape->merge_stage_size >= tape->stage_size) {
3832 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3833 tape->merge_stage_size = 0;
3834 }
3835#endif /* IDETAPE_DEBUG_BUGS */
3836 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
3837 idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written);
3838 buf += actually_written;
3839 tape->merge_stage_size += actually_written;
3840 count -= actually_written;
3841
3842 if (tape->merge_stage_size == tape->stage_size) {
3843 tape->merge_stage_size = 0;
3844 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3845 if (retval <= 0)
3846 return (retval);
3847 }
3848 }
3849 while (count >= tape->stage_size) {
3850 idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size);
3851 buf += tape->stage_size;
3852 count -= tape->stage_size;
3853 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3854 actually_written += tape->stage_size;
3855 if (retval <= 0)
3856 return (retval);
3857 }
3858 if (count) {
3859 actually_written += count;
3860 idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count);
3861 tape->merge_stage_size += count;
3862 }
3863 return (actually_written);
3864}
3865
3866static int idetape_write_filemark (ide_drive_t *drive)
3867{
3868 idetape_pc_t pc;
3869
3870 /* Write a filemark */
3871 idetape_create_write_filemark_cmd(drive, &pc, 1);
3872 if (idetape_queue_pc_tail(drive, &pc)) {
3873 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3874 return -EIO;
3875 }
3876 return 0;
3877}
3878
3879/*
3880 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3881 * the general mtio MTIOCTOP ioctl is requested.
3882 *
3883 * We currently support the following mtio.h operations:
3884 *
3885 * MTFSF - Space over mt_count filemarks in the positive direction.
3886 * The tape is positioned after the last spaced filemark.
3887 *
3888 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3889 * last filemark.
3890 *
3891 * MTBSF - Steps background over mt_count filemarks, tape is
3892 * positioned before the last filemark.
3893 *
3894 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3895 *
3896 * Note:
3897 *
3898 * MTBSF and MTBSFM are not supported when the tape doesn't
3899 * support spacing over filemarks in the reverse direction.
3900 * In this case, MTFSFM is also usually not supported (it is
3901 * supported in the rare case in which we crossed the filemark
3902 * during our read-ahead pipelined operation mode).
3903 *
3904 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3905 * the last written filemark.
3906 *
3907 * MTREW - Rewinds tape.
3908 *
3909 * MTLOAD - Loads the tape.
3910 *
3911 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3912 * MTUNLOAD prevents further access until the media is replaced.
3913 *
3914 * MTNOP - Flushes tape buffers.
3915 *
3916 * MTRETEN - Retension media. This typically consists of one end
3917 * to end pass on the media.
3918 *
3919 * MTEOM - Moves to the end of recorded data.
3920 *
3921 * MTERASE - Erases tape.
3922 *
3923 * MTSETBLK - Sets the user block size to mt_count bytes. If
3924 * mt_count is 0, we will attempt to autodetect
3925 * the block size.
3926 *
3927 * MTSEEK - Positions the tape in a specific block number, where
3928 * each block is assumed to contain which user_block_size
3929 * bytes.
3930 *
3931 * MTSETPART - Switches to another tape partition.
3932 *
3933 * MTLOCK - Locks the tape door.
3934 *
3935 * MTUNLOCK - Unlocks the tape door.
3936 *
3937 * The following commands are currently not supported:
3938 *
3939 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3940 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3941 */
3942static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3943{
3944 idetape_tape_t *tape = drive->driver_data;
3945 idetape_pc_t pc;
3946 int i,retval;
3947
3948#if IDETAPE_DEBUG_LOG
3949 if (tape->debug_level >= 1)
3950 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3951 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3952#endif /* IDETAPE_DEBUG_LOG */
3953 /*
3954 * Commands which need our pipelined read-ahead stages.
3955 */
3956 switch (mt_op) {
3957 case MTFSF:
3958 case MTFSFM:
3959 case MTBSF:
3960 case MTBSFM:
3961 if (!mt_count)
3962 return (0);
3963 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3964 default:
3965 break;
3966 }
3967 switch (mt_op) {
3968 case MTWEOF:
3969 if (tape->write_prot)
3970 return -EACCES;
3971 idetape_discard_read_pipeline(drive, 1);
3972 for (i = 0; i < mt_count; i++) {
3973 retval = idetape_write_filemark(drive);
3974 if (retval)
3975 return retval;
3976 }
3977 return (0);
3978 case MTREW:
3979 idetape_discard_read_pipeline(drive, 0);
3980 if (idetape_rewind_tape(drive))
3981 return -EIO;
3982 return 0;
3983 case MTLOAD:
3984 idetape_discard_read_pipeline(drive, 0);
3985 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3986 return (idetape_queue_pc_tail(drive, &pc));
3987 case MTUNLOAD:
3988 case MTOFFL:
3989 /*
3990 * If door is locked, attempt to unlock before
3991 * attempting to eject.
3992 */
3993 if (tape->door_locked) {
3994 if (idetape_create_prevent_cmd(drive, &pc, 0))
3995 if (!idetape_queue_pc_tail(drive, &pc))
3996 tape->door_locked = DOOR_UNLOCKED;
3997 }
3998 idetape_discard_read_pipeline(drive, 0);
3999 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
4000 retval = idetape_queue_pc_tail(drive, &pc);
4001 if (!retval)
4002 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
4003 return retval;
4004 case MTNOP:
4005 idetape_discard_read_pipeline(drive, 0);
4006 return (idetape_flush_tape_buffers(drive));
4007 case MTRETEN:
4008 idetape_discard_read_pipeline(drive, 0);
4009 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
4010 return (idetape_queue_pc_tail(drive, &pc));
4011 case MTEOM:
4012 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
4013 return (idetape_queue_pc_tail(drive, &pc));
4014 case MTERASE:
4015 (void) idetape_rewind_tape(drive);
4016 idetape_create_erase_cmd(&pc);
4017 return (idetape_queue_pc_tail(drive, &pc));
4018 case MTSETBLK:
4019 if (mt_count) {
4020 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
4021 return -EIO;
4022 tape->user_bs_factor = mt_count / tape->tape_block_size;
4023 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
4024 } else
4025 set_bit(IDETAPE_DETECT_BS, &tape->flags);
4026 return 0;
4027 case MTSEEK:
4028 idetape_discard_read_pipeline(drive, 0);
4029 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
4030 case MTSETPART:
4031 idetape_discard_read_pipeline(drive, 0);
4032 return (idetape_position_tape(drive, 0, mt_count, 0));
4033 case MTFSR:
4034 case MTBSR:
4035 case MTLOCK:
4036 if (!idetape_create_prevent_cmd(drive, &pc, 1))
4037 return 0;
4038 retval = idetape_queue_pc_tail(drive, &pc);
4039 if (retval) return retval;
4040 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
4041 return 0;
4042 case MTUNLOCK:
4043 if (!idetape_create_prevent_cmd(drive, &pc, 0))
4044 return 0;
4045 retval = idetape_queue_pc_tail(drive, &pc);
4046 if (retval) return retval;
4047 tape->door_locked = DOOR_UNLOCKED;
4048 return 0;
4049 default:
4050 printk(KERN_ERR "ide-tape: MTIO operation %d not "
4051 "supported\n", mt_op);
4052 return (-EIO);
4053 }
4054}
4055
4056/*
4057 * Our character device ioctls.
4058 *
4059 * General mtio.h magnetic io commands are supported here, and not in
4060 * the corresponding block interface.
4061 *
4062 * The following ioctls are supported:
4063 *
4064 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
4065 *
4066 * MTIOCGET - The mt_dsreg field in the returned mtget structure
4067 * will be set to (user block size in bytes <<
4068 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
4069 *
4070 * The mt_blkno is set to the current user block number.
4071 * The other mtget fields are not supported.
4072 *
4073 * MTIOCPOS - The current tape "block position" is returned. We
4074 * assume that each block contains user_block_size
4075 * bytes.
4076 *
4077 * Our own ide-tape ioctls are supported on both interfaces.
4078 */
4079static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
4080{
4081 struct ide_tape_obj *tape = ide_tape_f(file);
4082 ide_drive_t *drive = tape->drive;
4083 struct mtop mtop;
4084 struct mtget mtget;
4085 struct mtpos mtpos;
4086 int block_offset = 0, position = tape->first_frame_position;
4087 void __user *argp = (void __user *)arg;
4088
4089#if IDETAPE_DEBUG_LOG
4090 if (tape->debug_level >= 3)
4091 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
4092 "cmd=%u\n", cmd);
4093#endif /* IDETAPE_DEBUG_LOG */
4094
4095 tape->restart_speed_control_req = 1;
4096 if (tape->chrdev_direction == idetape_direction_write) {
4097 idetape_empty_write_pipeline(drive);
4098 idetape_flush_tape_buffers(drive);
4099 }
4100 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
4101 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
4102 if ((position = idetape_read_position(drive)) < 0)
4103 return -EIO;
4104 }
4105 switch (cmd) {
4106 case MTIOCTOP:
4107 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
4108 return -EFAULT;
4109 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
4110 case MTIOCGET:
4111 memset(&mtget, 0, sizeof (struct mtget));
4112 mtget.mt_type = MT_ISSCSI2;
4113 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
4114 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
4115 if (tape->drv_write_prot) {
4116 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
4117 }
4118 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
4119 return -EFAULT;
4120 return 0;
4121 case MTIOCPOS:
4122 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
4123 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
4124 return -EFAULT;
4125 return 0;
4126 default:
4127 if (tape->chrdev_direction == idetape_direction_read)
4128 idetape_discard_read_pipeline(drive, 1);
4129 return idetape_blkdev_ioctl(drive, cmd, arg);
4130 }
4131}
4132
4133static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
4134
4135/*
4136 * Our character device open function.
4137 */
4138static int idetape_chrdev_open (struct inode *inode, struct file *filp)
4139{
4140 unsigned int minor = iminor(inode), i = minor & ~0xc0;
4141 ide_drive_t *drive;
4142 idetape_tape_t *tape;
4143 idetape_pc_t pc;
4144 int retval;
4145
4146 /*
4147 * We really want to do nonseekable_open(inode, filp); here, but some
4148 * versions of tar incorrectly call lseek on tapes and bail out if that
4149 * fails. So we disallow pread() and pwrite(), but permit lseeks.
4150 */
4151 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
4152
4153#if IDETAPE_DEBUG_LOG
4154 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
4155#endif /* IDETAPE_DEBUG_LOG */
4156
4157 if (i >= MAX_HWIFS * MAX_DRIVES)
4158 return -ENXIO;
4159
4160 if (!(tape = ide_tape_chrdev_get(i)))
4161 return -ENXIO;
4162
4163 drive = tape->drive;
4164
4165 filp->private_data = tape;
4166
4167 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
4168 retval = -EBUSY;
4169 goto out_put_tape;
4170 }
4171
4172 retval = idetape_wait_ready(drive, 60 * HZ);
4173 if (retval) {
4174 clear_bit(IDETAPE_BUSY, &tape->flags);
4175 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
4176 goto out_put_tape;
4177 }
4178
4179 idetape_read_position(drive);
4180 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
4181 (void)idetape_rewind_tape(drive);
4182
4183 if (tape->chrdev_direction != idetape_direction_read)
4184 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
4185
4186 /* Read block size and write protect status from drive. */
4187 idetape_get_blocksize_from_block_descriptor(drive);
4188
4189 /* Set write protect flag if device is opened as read-only. */
4190 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
4191 tape->write_prot = 1;
4192 else
4193 tape->write_prot = tape->drv_write_prot;
4194
4195 /* Make sure drive isn't write protected if user wants to write. */
4196 if (tape->write_prot) {
4197 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
4198 (filp->f_flags & O_ACCMODE) == O_RDWR) {
4199 clear_bit(IDETAPE_BUSY, &tape->flags);
4200 retval = -EROFS;
4201 goto out_put_tape;
4202 }
4203 }
4204
4205 /*
4206 * Lock the tape drive door so user can't eject.
4207 */
4208 if (tape->chrdev_direction == idetape_direction_none) {
4209 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
4210 if (!idetape_queue_pc_tail(drive, &pc)) {
4211 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
4212 tape->door_locked = DOOR_LOCKED;
4213 }
4214 }
4215 }
4216 idetape_restart_speed_control(drive);
4217 tape->restart_speed_control_req = 0;
4218 return 0;
4219
4220out_put_tape:
4221 ide_tape_put(tape);
4222 return retval;
4223}
4224
4225static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
4226{
4227 idetape_tape_t *tape = drive->driver_data;
4228
4229 idetape_empty_write_pipeline(drive);
4230 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
4231 if (tape->merge_stage != NULL) {
4232 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
4233 __idetape_kfree_stage(tape->merge_stage);
4234 tape->merge_stage = NULL;
4235 }
4236 idetape_write_filemark(drive);
4237 idetape_flush_tape_buffers(drive);
4238 idetape_flush_tape_buffers(drive);
4239}
4240
4241/*
4242 * Our character device release function.
4243 */
4244static int idetape_chrdev_release (struct inode *inode, struct file *filp)
4245{
4246 struct ide_tape_obj *tape = ide_tape_f(filp);
4247 ide_drive_t *drive = tape->drive;
4248 idetape_pc_t pc;
4249 unsigned int minor = iminor(inode);
4250
4251 lock_kernel();
4252 tape = drive->driver_data;
4253#if IDETAPE_DEBUG_LOG
4254 if (tape->debug_level >= 3)
4255 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
4256#endif /* IDETAPE_DEBUG_LOG */
4257
4258 if (tape->chrdev_direction == idetape_direction_write)
4259 idetape_write_release(drive, minor);
4260 if (tape->chrdev_direction == idetape_direction_read) {
4261 if (minor < 128)
4262 idetape_discard_read_pipeline(drive, 1);
4263 else
4264 idetape_wait_for_pipeline(drive);
4265 }
4266 if (tape->cache_stage != NULL) {
4267 __idetape_kfree_stage(tape->cache_stage);
4268 tape->cache_stage = NULL;
4269 }
4270 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
4271 (void) idetape_rewind_tape(drive);
4272 if (tape->chrdev_direction == idetape_direction_none) {
4273 if (tape->door_locked == DOOR_LOCKED) {
4274 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
4275 if (!idetape_queue_pc_tail(drive, &pc))
4276 tape->door_locked = DOOR_UNLOCKED;
4277 }
4278 }
4279 }
4280 clear_bit(IDETAPE_BUSY, &tape->flags);
4281 ide_tape_put(tape);
4282 unlock_kernel();
4283 return 0;
4284}
4285
4286/*
4287 * idetape_identify_device is called to check the contents of the
4288 * ATAPI IDENTIFY command results. We return:
4289 *
4290 * 1 If the tape can be supported by us, based on the information
4291 * we have so far.
4292 *
4293 * 0 If this tape driver is not currently supported by us.
4294 */
4295static int idetape_identify_device (ide_drive_t *drive)
4296{
4297 struct idetape_id_gcw gcw;
4298 struct hd_driveid *id = drive->id;
4299#if IDETAPE_DEBUG_INFO
4300 unsigned short mask,i;
4301#endif /* IDETAPE_DEBUG_INFO */
4302
4303 if (drive->id_read == 0)
4304 return 1;
4305
4306 *((unsigned short *) &gcw) = id->config;
4307
4308#if IDETAPE_DEBUG_INFO
4309 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
4310 printk(KERN_INFO "ide-tape: Protocol Type: ");
4311 switch (gcw.protocol) {
4312 case 0: case 1: printk("ATA\n");break;
4313 case 2: printk("ATAPI\n");break;
4314 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
4315 }
4316 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
4317 switch (gcw.device_type) {
4318 case 0: printk("Direct-access Device\n");break;
4319 case 1: printk("Streaming Tape Device\n");break;
4320 case 2: case 3: case 4: printk("Reserved\n");break;
4321 case 5: printk("CD-ROM Device\n");break;
4322 case 6: printk("Reserved\n");
4323 case 7: printk("Optical memory Device\n");break;
4324 case 0x1f: printk("Unknown or no Device type\n");break;
4325 default: printk("Reserved\n");
4326 }
4327 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
4328 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
4329 switch (gcw.drq_type) {
4330 case 0: printk("Microprocessor DRQ\n");break;
4331 case 1: printk("Interrupt DRQ\n");break;
4332 case 2: printk("Accelerated DRQ\n");break;
4333 case 3: printk("Reserved\n");break;
4334 }
4335 printk(KERN_INFO "ide-tape: Command Packet Size: ");
4336 switch (gcw.packet_size) {
4337 case 0: printk("12 bytes\n");break;
4338 case 1: printk("16 bytes\n");break;
4339 default: printk("Reserved\n");break;
4340 }
4341 printk(KERN_INFO "ide-tape: Model: %.40s\n",id->model);
4342 printk(KERN_INFO "ide-tape: Firmware Revision: %.8s\n",id->fw_rev);
4343 printk(KERN_INFO "ide-tape: Serial Number: %.20s\n",id->serial_no);
4344 printk(KERN_INFO "ide-tape: Write buffer size: %d bytes\n",id->buf_size*512);
4345 printk(KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
4346 printk(KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
4347 printk(KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
4348 printk(KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
4349 printk(KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
4350 printk(KERN_INFO "ide-tape: PIO Cycle Timing Category: %d\n",id->tPIO);
4351 printk(KERN_INFO "ide-tape: DMA Cycle Timing Category: %d\n",id->tDMA);
4352 printk(KERN_INFO "ide-tape: Single Word DMA supported modes: ");
4353 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
4354 if (id->dma_1word & mask)
4355 printk("%d ",i);
4356 if (id->dma_1word & (mask << 8))
4357 printk("(active) ");
4358 }
4359 printk("\n");
4360 printk(KERN_INFO "ide-tape: Multi Word DMA supported modes: ");
4361 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
4362 if (id->dma_mword & mask)
4363 printk("%d ",i);
4364 if (id->dma_mword & (mask << 8))
4365 printk("(active) ");
4366 }
4367 printk("\n");
4368 if (id->field_valid & 0x0002) {
4369 printk(KERN_INFO "ide-tape: Enhanced PIO Modes: %s\n",
4370 id->eide_pio_modes & 1 ? "Mode 3":"None");
4371 printk(KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: ");
4372 if (id->eide_dma_min == 0)
4373 printk("Not supported\n");
4374 else
4375 printk("%d ns\n",id->eide_dma_min);
4376
4377 printk(KERN_INFO "ide-tape: Manufacturer\'s Recommended Multi-word cycle: ");
4378 if (id->eide_dma_time == 0)
4379 printk("Not supported\n");
4380 else
4381 printk("%d ns\n",id->eide_dma_time);
4382
4383 printk(KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: ");
4384 if (id->eide_pio == 0)
4385 printk("Not supported\n");
4386 else
4387 printk("%d ns\n",id->eide_pio);
4388
4389 printk(KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: ");
4390 if (id->eide_pio_iordy == 0)
4391 printk("Not supported\n");
4392 else
4393 printk("%d ns\n",id->eide_pio_iordy);
4394
4395 } else
4396 printk(KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.\n");
4397#endif /* IDETAPE_DEBUG_INFO */
4398
4399 /* Check that we can support this device */
4400
4401 if (gcw.protocol !=2 )
4402 printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
4403 else if (gcw.device_type != 1)
4404 printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
4405 else if (!gcw.removable)
4406 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
4407 else if (gcw.packet_size != 0) {
4408 printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
4409 if (gcw.packet_size == 1)
4410 printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
4411 } else
4412 return 1;
4413 return 0;
4414}
4415
4416/*
4417 * Use INQUIRY to get the firmware revision
4418 */
4419static void idetape_get_inquiry_results (ide_drive_t *drive)
4420{
4421 char *r;
4422 idetape_tape_t *tape = drive->driver_data;
4423 idetape_pc_t pc;
4424 idetape_inquiry_result_t *inquiry;
4425
4426 idetape_create_inquiry_cmd(&pc);
4427 if (idetape_queue_pc_tail(drive, &pc)) {
4428 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
4429 return;
4430 }
4431 inquiry = (idetape_inquiry_result_t *) pc.buffer;
4432 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
4433 memcpy(tape->product_id, inquiry->product_id, 16);
4434 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
4435 ide_fixstring(tape->vendor_id, 10, 0);
4436 ide_fixstring(tape->product_id, 18, 0);
4437 ide_fixstring(tape->firmware_revision, 6, 0);
4438 r = tape->firmware_revision;
4439 if (*(r + 1) == '.')
4440 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
4441 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);
4442}
4443
4444/*
4445 * idetape_get_mode_sense_results asks the tape about its various
4446 * parameters. In particular, we will adjust our data transfer buffer
4447 * size to the recommended value as returned by the tape.
4448 */
4449static void idetape_get_mode_sense_results (ide_drive_t *drive)
4450{
4451 idetape_tape_t *tape = drive->driver_data;
4452 idetape_pc_t pc;
4453 idetape_mode_parameter_header_t *header;
4454 idetape_capabilities_page_t *capabilities;
4455
4456 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
4457 if (idetape_queue_pc_tail(drive, &pc)) {
4458 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
4459 tape->tape_block_size = 512;
4460 tape->capabilities.ctl = 52;
4461 tape->capabilities.speed = 450;
4462 tape->capabilities.buffer_size = 6 * 52;
4463 return;
4464 }
4465 header = (idetape_mode_parameter_header_t *) pc.buffer;
4466 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
4467
4468 capabilities->max_speed = ntohs(capabilities->max_speed);
4469 capabilities->ctl = ntohs(capabilities->ctl);
4470 capabilities->speed = ntohs(capabilities->speed);
4471 capabilities->buffer_size = ntohs(capabilities->buffer_size);
4472
4473 if (!capabilities->speed) {
4474 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
4475 capabilities->speed = 650;
4476 }
4477 if (!capabilities->max_speed) {
4478 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
4479 capabilities->max_speed = 650;
4480 }
4481
4482 tape->capabilities = *capabilities; /* Save us a copy */
4483 if (capabilities->blk512)
4484 tape->tape_block_size = 512;
4485 else if (capabilities->blk1024)
4486 tape->tape_block_size = 1024;
4487
4488#if IDETAPE_DEBUG_INFO
4489 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
4490 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
4491 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
4492 printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
4493 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
4494 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
4495
4496 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
4497 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
4498 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
4499 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
4500 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
4501 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
4502 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
4503 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
4504 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
4505 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
4506 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
4507 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
4508 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
4509 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
4510 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
4511 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
4512 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
4513 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
4514 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
4515 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
4516#endif /* IDETAPE_DEBUG_INFO */
4517}
4518
4519/*
4520 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
4521 * and if it succeeds sets the tape block size with the reported value
4522 */
4523static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
4524{
4525
4526 idetape_tape_t *tape = drive->driver_data;
4527 idetape_pc_t pc;
4528 idetape_mode_parameter_header_t *header;
4529 idetape_parameter_block_descriptor_t *block_descrp;
4530
4531 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
4532 if (idetape_queue_pc_tail(drive, &pc)) {
4533 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
4534 if (tape->tape_block_size == 0) {
4535 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
4536 tape->tape_block_size = 32768;
4537 }
4538 return;
4539 }
4540 header = (idetape_mode_parameter_header_t *) pc.buffer;
4541 block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
4542 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
4543 tape->drv_write_prot = (header->dsp & 0x80) >> 7;
4544
4545#if IDETAPE_DEBUG_INFO
4546 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
4547#endif /* IDETAPE_DEBUG_INFO */
4548}
4549static void idetape_add_settings (ide_drive_t *drive)
4550{
4551 idetape_tape_t *tape = drive->driver_data;
4552
4553/*
4554 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
4555 */
4556 ide_add_setting(drive, "buffer", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
4557 ide_add_setting(drive, "pipeline_min", SETTING_RW, -1, -1, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
4558 ide_add_setting(drive, "pipeline", SETTING_RW, -1, -1, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
4559 ide_add_setting(drive, "pipeline_max", SETTING_RW, -1, -1, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
4560 ide_add_setting(drive, "pipeline_used",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
4561 ide_add_setting(drive, "pipeline_pending",SETTING_READ,-1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
4562 ide_add_setting(drive, "speed", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
4563 ide_add_setting(drive, "stage", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
4564 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);
4565 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
4566 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
4567 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed, NULL);
4568 ide_add_setting(drive, "avg_speed", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
4569 ide_add_setting(drive, "debug_level",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
4570}
4571
4572/*
4573 * ide_setup is called to:
4574 *
4575 * 1. Initialize our various state variables.
4576 * 2. Ask the tape for its capabilities.
4577 * 3. Allocate a buffer which will be used for data
4578 * transfer. The buffer size is chosen based on
4579 * the recommendation which we received in step (2).
4580 *
4581 * Note that at this point ide.c already assigned us an irq, so that
4582 * we can queue requests here and wait for their completion.
4583 */
4584static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
4585{
4586 unsigned long t1, tmid, tn, t;
4587 int speed;
4588 struct idetape_id_gcw gcw;
4589 int stage_size;
4590 struct sysinfo si;
4591
4592 spin_lock_init(&tape->spinlock);
4593 drive->dsc_overlap = 1;
4594#ifdef CONFIG_BLK_DEV_IDEPCI
4595 if (HWIF(drive)->pci_dev != NULL) {
4596 /*
4597 * These two ide-pci host adapters appear to need DSC overlap disabled.
4598 * This probably needs further analysis.
4599 */
4600 if ((HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
4601 (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) {
4602 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", tape->name);
4603 drive->dsc_overlap = 0;
4604 }
4605 }
4606#endif /* CONFIG_BLK_DEV_IDEPCI */
4607 /* Seagate Travan drives do not support DSC overlap. */
4608 if (strstr(drive->id->model, "Seagate STT3401"))
4609 drive->dsc_overlap = 0;
4610 tape->minor = minor;
4611 tape->name[0] = 'h';
4612 tape->name[1] = 't';
4613 tape->name[2] = '0' + minor;
4614 tape->chrdev_direction = idetape_direction_none;
4615 tape->pc = tape->pc_stack;
4616 tape->max_insert_speed = 10000;
4617 tape->speed_control = 1;
4618 *((unsigned short *) &gcw) = drive->id->config;
4619 if (gcw.drq_type == 1)
4620 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4621
4622 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4623
4624 idetape_get_inquiry_results(drive);
4625 idetape_get_mode_sense_results(drive);
4626 idetape_get_blocksize_from_block_descriptor(drive);
4627 tape->user_bs_factor = 1;
4628 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4629 while (tape->stage_size > 0xffff) {
4630 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4631 tape->capabilities.ctl /= 2;
4632 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4633 }
4634 stage_size = tape->stage_size;
4635 tape->pages_per_stage = stage_size / PAGE_SIZE;
4636 if (stage_size % PAGE_SIZE) {
4637 tape->pages_per_stage++;
4638 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4639 }
4640
4641 /*
4642 * Select the "best" DSC read/write polling frequency
4643 * and pipeline size.
4644 */
4645 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4646
4647 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4648
4649 /*
4650 * Limit memory use for pipeline to 10% of physical memory
4651 */
4652 si_meminfo(&si);
4653 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4654 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4655 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4656 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4657 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4658 if (tape->max_stages == 0)
4659 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4660
4661 t1 = (tape->stage_size * HZ) / (speed * 1000);
4662 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4663 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4664
4665 if (tape->max_stages)
4666 t = tn;
4667 else
4668 t = t1;
4669
4670 /*
4671 * Ensure that the number we got makes sense; limit
4672 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4673 */
4674 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4675 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4676 "%dkB pipeline, %lums tDSC%s\n",
4677 drive->name, tape->name, tape->capabilities.speed,
4678 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4679 tape->stage_size / 1024,
4680 tape->max_stages * tape->stage_size / 1024,
4681 tape->best_dsc_rw_frequency * 1000 / HZ,
4682 drive->using_dma ? ", DMA":"");
4683
4684 idetape_add_settings(drive);
4685}
4686
Russell King4031bbe2006-01-06 11:41:00 +00004687static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
4689 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004691 ide_unregister_subdriver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693 ide_unregister_region(tape->disk);
4694
4695 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
4697
4698static void ide_tape_release(struct kref *kref)
4699{
4700 struct ide_tape_obj *tape = to_ide_tape(kref);
4701 ide_drive_t *drive = tape->drive;
4702 struct gendisk *g = tape->disk;
4703
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004704 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 drive->dsc_overlap = 0;
4707 drive->driver_data = NULL;
Will Dysond5dee802005-09-16 02:55:07 -07004708 class_device_destroy(idetape_sysfs_class,
4709 MKDEV(IDETAPE_MAJOR, tape->minor));
4710 class_device_destroy(idetape_sysfs_class,
4711 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 devfs_remove("%s/mt", drive->devfs_name);
4713 devfs_remove("%s/mtn", drive->devfs_name);
4714 devfs_unregister_tape(g->number);
4715 idetape_devs[tape->minor] = NULL;
4716 g->private_data = NULL;
4717 put_disk(g);
4718 kfree(tape);
4719}
4720
4721#ifdef CONFIG_PROC_FS
4722
4723static int proc_idetape_read_name
4724 (char *page, char **start, off_t off, int count, int *eof, void *data)
4725{
4726 ide_drive_t *drive = (ide_drive_t *) data;
4727 idetape_tape_t *tape = drive->driver_data;
4728 char *out = page;
4729 int len;
4730
4731 len = sprintf(out, "%s\n", tape->name);
4732 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4733}
4734
4735static ide_proc_entry_t idetape_proc[] = {
4736 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4737 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4738 { NULL, 0, NULL, NULL }
4739};
4740
4741#else
4742
4743#define idetape_proc NULL
4744
4745#endif
4746
Russell King4031bbe2006-01-06 11:41:00 +00004747static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004750 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01004751 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004752 .name = "ide-tape",
4753 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004754 },
Russell King4031bbe2006-01-06 11:41:00 +00004755 .probe = ide_tape_probe,
4756 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 .version = IDETAPE_VERSION,
4758 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 .do_request = idetape_do_request,
4761 .end_request = idetape_end_request,
4762 .error = __ide_error,
4763 .abort = __ide_abort,
4764 .proc = idetape_proc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765};
4766
4767/*
4768 * Our character device supporting functions, passed to register_chrdev.
4769 */
4770static struct file_operations idetape_fops = {
4771 .owner = THIS_MODULE,
4772 .read = idetape_chrdev_read,
4773 .write = idetape_chrdev_write,
4774 .ioctl = idetape_chrdev_ioctl,
4775 .open = idetape_chrdev_open,
4776 .release = idetape_chrdev_release,
4777};
4778
4779static int idetape_open(struct inode *inode, struct file *filp)
4780{
4781 struct gendisk *disk = inode->i_bdev->bd_disk;
4782 struct ide_tape_obj *tape;
4783 ide_drive_t *drive;
4784
4785 if (!(tape = ide_tape_get(disk)))
4786 return -ENXIO;
4787
4788 drive = tape->drive;
4789
4790 drive->usage++;
4791
4792 return 0;
4793}
4794
4795static int idetape_release(struct inode *inode, struct file *filp)
4796{
4797 struct gendisk *disk = inode->i_bdev->bd_disk;
4798 struct ide_tape_obj *tape = ide_tape_g(disk);
4799 ide_drive_t *drive = tape->drive;
4800
4801 drive->usage--;
4802
4803 ide_tape_put(tape);
4804
4805 return 0;
4806}
4807
4808static int idetape_ioctl(struct inode *inode, struct file *file,
4809 unsigned int cmd, unsigned long arg)
4810{
4811 struct block_device *bdev = inode->i_bdev;
4812 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4813 ide_drive_t *drive = tape->drive;
4814 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4815 if (err == -EINVAL)
4816 err = idetape_blkdev_ioctl(drive, cmd, arg);
4817 return err;
4818}
4819
4820static struct block_device_operations idetape_block_ops = {
4821 .owner = THIS_MODULE,
4822 .open = idetape_open,
4823 .release = idetape_release,
4824 .ioctl = idetape_ioctl,
4825};
4826
Russell King4031bbe2006-01-06 11:41:00 +00004827static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
4829 idetape_tape_t *tape;
4830 struct gendisk *g;
4831 int minor;
4832
4833 if (!strstr("ide-tape", drive->driver_req))
4834 goto failed;
4835 if (!drive->present)
4836 goto failed;
4837 if (drive->media != ide_tape)
4838 goto failed;
4839 if (!idetape_identify_device (drive)) {
4840 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4841 goto failed;
4842 }
4843 if (drive->scsi) {
4844 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4845 goto failed;
4846 }
4847 if (strstr(drive->id->model, "OnStream DI-")) {
4848 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4849 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4850 }
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08004851 tape = (idetape_tape_t *) kzalloc (sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 if (tape == NULL) {
4853 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4854 goto failed;
4855 }
4856
4857 g = alloc_disk(1 << PARTN_BITS);
4858 if (!g)
4859 goto out_free_tape;
4860
4861 ide_init_disk(g, drive);
4862
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004863 ide_register_subdriver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 kref_init(&tape->kref);
4866
4867 tape->drive = drive;
4868 tape->driver = &idetape_driver;
4869 tape->disk = g;
4870
4871 g->private_data = &tape->driver;
4872
4873 drive->driver_data = tape;
4874
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004875 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 for (minor = 0; idetape_devs[minor]; minor++)
4877 ;
4878 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004879 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 idetape_setup(drive, tape, minor);
4882
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07004883 class_device_create(idetape_sysfs_class, NULL,
Russell King4031bbe2006-01-06 11:41:00 +00004884 MKDEV(IDETAPE_MAJOR, minor), &drive->gendev, "%s", tape->name);
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07004885 class_device_create(idetape_sysfs_class, NULL,
Russell King4031bbe2006-01-06 11:41:00 +00004886 MKDEV(IDETAPE_MAJOR, minor + 128), &drive->gendev, "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07004887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 devfs_mk_cdev(MKDEV(HWIF(drive)->major, minor),
4889 S_IFCHR | S_IRUGO | S_IWUGO,
4890 "%s/mt", drive->devfs_name);
4891 devfs_mk_cdev(MKDEV(HWIF(drive)->major, minor + 128),
4892 S_IFCHR | S_IRUGO | S_IWUGO,
4893 "%s/mtn", drive->devfs_name);
4894
4895 g->number = devfs_register_tape(drive->devfs_name);
4896 g->fops = &idetape_block_ops;
4897 ide_register_region(g);
4898
4899 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901out_free_tape:
4902 kfree(tape);
4903failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004904 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905}
4906
4907MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4908MODULE_LICENSE("GPL");
4909
4910static void __exit idetape_exit (void)
4911{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004912 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07004913 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 unregister_chrdev(IDETAPE_MAJOR, "ht");
4915}
4916
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01004917static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Will Dysond5dee802005-09-16 02:55:07 -07004919 int error = 1;
4920 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4921 if (IS_ERR(idetape_sysfs_class)) {
4922 idetape_sysfs_class = NULL;
4923 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4924 error = -EBUSY;
4925 goto out;
4926 }
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4929 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07004930 error = -EBUSY;
4931 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
Will Dysond5dee802005-09-16 02:55:07 -07004933
4934 error = driver_register(&idetape_driver.gen_driver);
4935 if (error)
4936 goto out_free_driver;
4937
4938 return 0;
4939
4940out_free_driver:
4941 driver_unregister(&idetape_driver.gen_driver);
4942out_free_class:
4943 class_destroy(idetape_sysfs_class);
4944out:
4945 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
4947
Kay Sievers263756e2005-12-12 18:03:44 +01004948MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949module_init(idetape_init);
4950module_exit(idetape_exit);
4951MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);